nuklear.h 1.1 MB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766187671876818769187701877118772187731877418775187761877718778187791878018781187821878318784187851878618787187881878918790187911879218793187941879518796187971879818799188001880118802188031880418805188061880718808188091881018811188121881318814188151881618817188181881918820188211882218823188241882518826188271882818829188301883118832188331883418835188361883718838188391884018841188421884318844188451884618847188481884918850188511885218853188541885518856188571885818859188601886118862188631886418865188661886718868188691887018871188721887318874188751887618877188781887918880188811888218883188841888518886188871888818889188901889118892188931889418895188961889718898188991890018901189021890318904189051890618907189081890918910189111891218913189141891518916189171891818919189201892118922189231892418925189261892718928189291893018931189321893318934189351893618937189381893918940189411894218943189441894518946189471894818949189501895118952189531895418955189561895718958189591896018961189621896318964189651896618967189681896918970189711897218973189741897518976189771897818979189801898118982189831898418985189861898718988189891899018991189921899318994189951899618997189981899919000190011900219003190041900519006190071900819009190101901119012190131901419015190161901719018190191902019021190221902319024190251902619027190281902919030190311903219033190341903519036190371903819039190401904119042190431904419045190461904719048190491905019051190521905319054190551905619057190581905919060190611906219063190641906519066190671906819069190701907119072190731907419075190761907719078190791908019081190821908319084190851908619087190881908919090190911909219093190941909519096190971909819099191001910119102191031910419105191061910719108191091911019111191121911319114191151911619117191181911919120191211912219123191241912519126191271912819129191301913119132191331913419135191361913719138191391914019141191421914319144191451914619147191481914919150191511915219153191541915519156191571915819159191601916119162191631916419165191661916719168191691917019171191721917319174191751917619177191781917919180191811918219183191841918519186191871918819189191901919119192191931919419195191961919719198191991920019201192021920319204192051920619207192081920919210192111921219213192141921519216192171921819219192201922119222192231922419225192261922719228192291923019231192321923319234192351923619237192381923919240192411924219243192441924519246192471924819249192501925119252192531925419255192561925719258192591926019261192621926319264192651926619267192681926919270192711927219273192741927519276192771927819279192801928119282192831928419285192861928719288192891929019291192921929319294192951929619297192981929919300193011930219303193041930519306193071930819309193101931119312193131931419315193161931719318193191932019321193221932319324193251932619327193281932919330193311933219333193341933519336193371933819339193401934119342193431934419345193461934719348193491935019351193521935319354193551935619357193581935919360193611936219363193641936519366193671936819369193701937119372193731937419375193761937719378193791938019381193821938319384193851938619387193881938919390193911939219393193941939519396193971939819399194001940119402194031940419405194061940719408194091941019411194121941319414194151941619417194181941919420194211942219423194241942519426194271942819429194301943119432194331943419435194361943719438194391944019441194421944319444194451944619447194481944919450194511945219453194541945519456194571945819459194601946119462194631946419465194661946719468194691947019471194721947319474194751947619477194781947919480194811948219483194841948519486194871948819489194901949119492194931949419495194961949719498194991950019501195021950319504195051950619507195081950919510195111951219513195141951519516195171951819519195201952119522195231952419525195261952719528195291953019531195321953319534195351953619537195381953919540195411954219543195441954519546195471954819549195501955119552195531955419555195561955719558195591956019561195621956319564195651956619567195681956919570195711957219573195741957519576195771957819579195801958119582195831958419585195861958719588195891959019591195921959319594195951959619597195981959919600196011960219603196041960519606196071960819609196101961119612196131961419615196161961719618196191962019621196221962319624196251962619627196281962919630196311963219633196341963519636196371963819639196401964119642196431964419645196461964719648196491965019651196521965319654196551965619657196581965919660196611966219663196641966519666196671966819669196701967119672196731967419675196761967719678196791968019681196821968319684196851968619687196881968919690196911969219693196941969519696196971969819699197001970119702197031970419705197061970719708197091971019711197121971319714197151971619717197181971919720197211972219723197241972519726197271972819729197301973119732197331973419735197361973719738197391974019741197421974319744197451974619747197481974919750197511975219753197541975519756197571975819759197601976119762197631976419765197661976719768197691977019771197721977319774197751977619777197781977919780197811978219783197841978519786197871978819789197901979119792197931979419795197961979719798197991980019801198021980319804198051980619807198081980919810198111981219813198141981519816198171981819819198201982119822198231982419825198261982719828198291983019831198321983319834198351983619837198381983919840198411984219843198441984519846198471984819849198501985119852198531985419855198561985719858198591986019861198621986319864198651986619867198681986919870198711987219873198741987519876198771987819879198801988119882198831988419885198861988719888198891989019891198921989319894198951989619897198981989919900199011990219903199041990519906199071990819909199101991119912199131991419915199161991719918199191992019921199221992319924199251992619927199281992919930199311993219933199341993519936199371993819939199401994119942199431994419945199461994719948199491995019951199521995319954199551995619957199581995919960199611996219963199641996519966199671996819969199701997119972199731997419975199761997719978199791998019981199821998319984199851998619987199881998919990199911999219993199941999519996199971999819999200002000120002200032000420005200062000720008200092001020011200122001320014200152001620017200182001920020200212002220023200242002520026200272002820029200302003120032200332003420035200362003720038200392004020041200422004320044200452004620047200482004920050200512005220053200542005520056200572005820059200602006120062200632006420065200662006720068200692007020071200722007320074200752007620077200782007920080200812008220083200842008520086200872008820089200902009120092200932009420095200962009720098200992010020101201022010320104201052010620107201082010920110201112011220113201142011520116201172011820119201202012120122201232012420125201262012720128201292013020131201322013320134201352013620137201382013920140201412014220143201442014520146201472014820149201502015120152201532015420155201562015720158201592016020161201622016320164201652016620167201682016920170201712017220173201742017520176201772017820179201802018120182201832018420185201862018720188201892019020191201922019320194201952019620197201982019920200202012020220203202042020520206202072020820209202102021120212202132021420215202162021720218202192022020221202222022320224202252022620227202282022920230202312023220233202342023520236202372023820239202402024120242202432024420245202462024720248202492025020251202522025320254202552025620257202582025920260202612026220263202642026520266202672026820269202702027120272202732027420275202762027720278202792028020281202822028320284202852028620287202882028920290202912029220293202942029520296202972029820299203002030120302203032030420305203062030720308203092031020311203122031320314203152031620317203182031920320203212032220323203242032520326203272032820329203302033120332203332033420335203362033720338203392034020341203422034320344203452034620347203482034920350203512035220353203542035520356203572035820359203602036120362203632036420365203662036720368203692037020371203722037320374203752037620377203782037920380203812038220383203842038520386203872038820389203902039120392203932039420395203962039720398203992040020401204022040320404204052040620407204082040920410204112041220413204142041520416204172041820419204202042120422204232042420425204262042720428204292043020431204322043320434204352043620437204382043920440204412044220443204442044520446204472044820449204502045120452204532045420455204562045720458204592046020461204622046320464204652046620467204682046920470204712047220473204742047520476204772047820479204802048120482204832048420485204862048720488204892049020491204922049320494204952049620497204982049920500205012050220503205042050520506205072050820509205102051120512205132051420515205162051720518205192052020521205222052320524205252052620527205282052920530205312053220533205342053520536205372053820539205402054120542205432054420545205462054720548205492055020551205522055320554205552055620557205582055920560205612056220563205642056520566205672056820569205702057120572205732057420575205762057720578205792058020581205822058320584205852058620587205882058920590205912059220593205942059520596205972059820599206002060120602206032060420605206062060720608206092061020611206122061320614206152061620617206182061920620206212062220623206242062520626206272062820629206302063120632206332063420635206362063720638206392064020641206422064320644206452064620647206482064920650206512065220653206542065520656206572065820659206602066120662206632066420665206662066720668206692067020671206722067320674206752067620677206782067920680206812068220683206842068520686206872068820689206902069120692206932069420695206962069720698206992070020701207022070320704207052070620707207082070920710207112071220713207142071520716207172071820719207202072120722207232072420725207262072720728207292073020731207322073320734207352073620737207382073920740207412074220743207442074520746207472074820749207502075120752207532075420755207562075720758207592076020761207622076320764207652076620767207682076920770207712077220773207742077520776207772077820779207802078120782207832078420785207862078720788207892079020791207922079320794207952079620797207982079920800208012080220803208042080520806208072080820809208102081120812208132081420815208162081720818208192082020821208222082320824208252082620827208282082920830208312083220833208342083520836208372083820839208402084120842208432084420845208462084720848208492085020851208522085320854208552085620857208582085920860208612086220863208642086520866208672086820869208702087120872208732087420875208762087720878208792088020881208822088320884208852088620887208882088920890208912089220893208942089520896208972089820899209002090120902209032090420905209062090720908209092091020911209122091320914209152091620917209182091920920209212092220923209242092520926209272092820929209302093120932209332093420935209362093720938209392094020941209422094320944209452094620947209482094920950209512095220953209542095520956209572095820959209602096120962209632096420965209662096720968209692097020971209722097320974209752097620977209782097920980209812098220983209842098520986209872098820989209902099120992209932099420995209962099720998209992100021001210022100321004210052100621007210082100921010210112101221013210142101521016210172101821019210202102121022210232102421025210262102721028210292103021031210322103321034210352103621037210382103921040210412104221043210442104521046210472104821049210502105121052210532105421055210562105721058210592106021061210622106321064210652106621067210682106921070210712107221073210742107521076210772107821079210802108121082210832108421085210862108721088210892109021091210922109321094210952109621097210982109921100211012110221103211042110521106211072110821109211102111121112211132111421115211162111721118211192112021121211222112321124211252112621127211282112921130211312113221133211342113521136211372113821139211402114121142211432114421145211462114721148211492115021151211522115321154211552115621157211582115921160211612116221163211642116521166211672116821169211702117121172211732117421175211762117721178211792118021181211822118321184211852118621187211882118921190211912119221193211942119521196211972119821199212002120121202212032120421205212062120721208212092121021211212122121321214212152121621217212182121921220212212122221223212242122521226212272122821229212302123121232212332123421235212362123721238212392124021241212422124321244212452124621247212482124921250212512125221253212542125521256212572125821259212602126121262212632126421265212662126721268212692127021271212722127321274212752127621277212782127921280212812128221283212842128521286212872128821289212902129121292212932129421295212962129721298212992130021301213022130321304213052130621307213082130921310213112131221313213142131521316213172131821319213202132121322213232132421325213262132721328213292133021331213322133321334213352133621337213382133921340213412134221343213442134521346213472134821349213502135121352213532135421355213562135721358213592136021361213622136321364213652136621367213682136921370213712137221373213742137521376213772137821379213802138121382213832138421385213862138721388213892139021391213922139321394213952139621397213982139921400214012140221403214042140521406214072140821409214102141121412214132141421415214162141721418214192142021421214222142321424214252142621427214282142921430214312143221433214342143521436214372143821439214402144121442214432144421445214462144721448214492145021451214522145321454214552145621457214582145921460214612146221463214642146521466214672146821469214702147121472214732147421475214762147721478214792148021481214822148321484214852148621487214882148921490214912149221493214942149521496214972149821499215002150121502215032150421505215062150721508215092151021511215122151321514215152151621517215182151921520215212152221523215242152521526215272152821529215302153121532215332153421535215362153721538215392154021541215422154321544215452154621547215482154921550215512155221553215542155521556215572155821559215602156121562215632156421565215662156721568215692157021571215722157321574215752157621577215782157921580215812158221583215842158521586215872158821589215902159121592215932159421595215962159721598215992160021601216022160321604216052160621607216082160921610216112161221613216142161521616216172161821619216202162121622216232162421625216262162721628216292163021631216322163321634216352163621637216382163921640216412164221643216442164521646216472164821649216502165121652216532165421655216562165721658216592166021661216622166321664216652166621667216682166921670216712167221673216742167521676216772167821679216802168121682216832168421685216862168721688216892169021691216922169321694216952169621697216982169921700217012170221703217042170521706217072170821709217102171121712217132171421715217162171721718217192172021721217222172321724217252172621727217282172921730217312173221733217342173521736217372173821739217402174121742217432174421745217462174721748217492175021751217522175321754217552175621757217582175921760217612176221763217642176521766217672176821769217702177121772217732177421775217762177721778217792178021781217822178321784217852178621787217882178921790217912179221793217942179521796217972179821799218002180121802218032180421805218062180721808218092181021811218122181321814218152181621817218182181921820218212182221823218242182521826218272182821829218302183121832218332183421835218362183721838218392184021841218422184321844218452184621847218482184921850218512185221853218542185521856218572185821859218602186121862218632186421865218662186721868218692187021871218722187321874218752187621877218782187921880218812188221883218842188521886218872188821889218902189121892218932189421895218962189721898218992190021901219022190321904219052190621907219082190921910219112191221913219142191521916219172191821919219202192121922219232192421925219262192721928219292193021931219322193321934219352193621937219382193921940219412194221943219442194521946219472194821949219502195121952219532195421955219562195721958219592196021961219622196321964219652196621967219682196921970219712197221973219742197521976219772197821979219802198121982219832198421985219862198721988219892199021991219922199321994219952199621997219982199922000220012200222003220042200522006220072200822009220102201122012220132201422015220162201722018220192202022021220222202322024220252202622027220282202922030220312203222033220342203522036220372203822039220402204122042220432204422045220462204722048220492205022051220522205322054220552205622057220582205922060220612206222063220642206522066220672206822069220702207122072220732207422075220762207722078220792208022081220822208322084220852208622087220882208922090220912209222093220942209522096220972209822099221002210122102221032210422105221062210722108221092211022111221122211322114221152211622117221182211922120221212212222123221242212522126221272212822129221302213122132221332213422135221362213722138221392214022141221422214322144221452214622147221482214922150221512215222153221542215522156221572215822159221602216122162221632216422165221662216722168221692217022171221722217322174221752217622177221782217922180221812218222183221842218522186221872218822189221902219122192221932219422195221962219722198221992220022201222022220322204222052220622207222082220922210222112221222213222142221522216222172221822219222202222122222222232222422225222262222722228222292223022231222322223322234222352223622237222382223922240222412224222243222442224522246222472224822249222502225122252222532225422255222562225722258222592226022261222622226322264222652226622267222682226922270222712227222273222742227522276222772227822279222802228122282222832228422285222862228722288222892229022291222922229322294222952229622297222982229922300223012230222303223042230522306223072230822309223102231122312223132231422315223162231722318223192232022321223222232322324223252232622327223282232922330223312233222333223342233522336223372233822339223402234122342223432234422345223462234722348223492235022351223522235322354223552235622357223582235922360223612236222363223642236522366223672236822369223702237122372223732237422375223762237722378223792238022381223822238322384223852238622387223882238922390223912239222393223942239522396223972239822399224002240122402224032240422405224062240722408224092241022411224122241322414224152241622417224182241922420224212242222423224242242522426224272242822429224302243122432224332243422435224362243722438224392244022441224422244322444224452244622447224482244922450224512245222453224542245522456224572245822459224602246122462224632246422465224662246722468224692247022471224722247322474224752247622477224782247922480224812248222483224842248522486224872248822489224902249122492224932249422495224962249722498224992250022501225022250322504225052250622507225082250922510225112251222513225142251522516225172251822519225202252122522225232252422525225262252722528225292253022531225322253322534225352253622537225382253922540225412254222543225442254522546225472254822549225502255122552225532255422555225562255722558225592256022561225622256322564225652256622567225682256922570225712257222573225742257522576225772257822579225802258122582225832258422585225862258722588225892259022591225922259322594225952259622597225982259922600226012260222603226042260522606226072260822609226102261122612226132261422615226162261722618226192262022621226222262322624226252262622627226282262922630226312263222633226342263522636226372263822639226402264122642226432264422645226462264722648226492265022651226522265322654226552265622657226582265922660226612266222663226642266522666226672266822669226702267122672226732267422675226762267722678226792268022681226822268322684226852268622687226882268922690226912269222693226942269522696226972269822699227002270122702227032270422705227062270722708227092271022711227122271322714227152271622717227182271922720227212272222723227242272522726227272272822729227302273122732227332273422735227362273722738227392274022741227422274322744227452274622747227482274922750227512275222753227542275522756227572275822759227602276122762227632276422765227662276722768227692277022771227722277322774227752277622777227782277922780227812278222783227842278522786227872278822789227902279122792227932279422795227962279722798227992280022801228022280322804228052280622807228082280922810228112281222813228142281522816228172281822819228202282122822228232282422825228262282722828228292283022831228322283322834228352283622837228382283922840228412284222843228442284522846228472284822849228502285122852228532285422855228562285722858228592286022861228622286322864228652286622867228682286922870228712287222873228742287522876228772287822879228802288122882228832288422885228862288722888228892289022891228922289322894228952289622897228982289922900229012290222903229042290522906229072290822909229102291122912229132291422915229162291722918229192292022921229222292322924229252292622927229282292922930229312293222933229342293522936229372293822939229402294122942229432294422945229462294722948229492295022951229522295322954229552295622957229582295922960229612296222963229642296522966229672296822969229702297122972229732297422975229762297722978229792298022981229822298322984229852298622987229882298922990229912299222993229942299522996229972299822999230002300123002230032300423005230062300723008230092301023011230122301323014230152301623017230182301923020230212302223023230242302523026230272302823029230302303123032230332303423035230362303723038230392304023041230422304323044230452304623047230482304923050230512305223053230542305523056230572305823059230602306123062230632306423065230662306723068230692307023071230722307323074230752307623077230782307923080230812308223083230842308523086230872308823089230902309123092230932309423095230962309723098230992310023101231022310323104231052310623107231082310923110231112311223113231142311523116231172311823119231202312123122231232312423125231262312723128231292313023131231322313323134231352313623137231382313923140231412314223143231442314523146231472314823149231502315123152231532315423155231562315723158231592316023161231622316323164231652316623167231682316923170231712317223173231742317523176231772317823179231802318123182231832318423185231862318723188231892319023191231922319323194231952319623197231982319923200232012320223203232042320523206232072320823209232102321123212232132321423215232162321723218232192322023221232222322323224232252322623227232282322923230232312323223233232342323523236232372323823239232402324123242232432324423245232462324723248232492325023251232522325323254232552325623257232582325923260232612326223263232642326523266232672326823269232702327123272232732327423275232762327723278232792328023281232822328323284232852328623287232882328923290232912329223293232942329523296232972329823299233002330123302233032330423305233062330723308233092331023311233122331323314233152331623317233182331923320233212332223323233242332523326233272332823329233302333123332233332333423335233362333723338233392334023341233422334323344233452334623347233482334923350233512335223353233542335523356233572335823359233602336123362233632336423365233662336723368233692337023371233722337323374233752337623377233782337923380233812338223383233842338523386233872338823389233902339123392233932339423395233962339723398233992340023401234022340323404234052340623407234082340923410234112341223413234142341523416234172341823419234202342123422234232342423425234262342723428234292343023431234322343323434234352343623437234382343923440234412344223443234442344523446234472344823449234502345123452234532345423455234562345723458234592346023461234622346323464234652346623467234682346923470234712347223473234742347523476234772347823479234802348123482234832348423485234862348723488234892349023491234922349323494234952349623497234982349923500235012350223503235042350523506235072350823509235102351123512235132351423515235162351723518235192352023521235222352323524235252352623527235282352923530235312353223533235342353523536235372353823539235402354123542235432354423545235462354723548235492355023551235522355323554235552355623557235582355923560235612356223563235642356523566235672356823569235702357123572235732357423575235762357723578235792358023581235822358323584235852358623587235882358923590235912359223593235942359523596235972359823599236002360123602236032360423605236062360723608236092361023611236122361323614236152361623617236182361923620236212362223623236242362523626236272362823629236302363123632236332363423635236362363723638236392364023641236422364323644236452364623647236482364923650236512365223653236542365523656236572365823659236602366123662236632366423665236662366723668236692367023671236722367323674236752367623677236782367923680236812368223683236842368523686236872368823689236902369123692236932369423695236962369723698236992370023701237022370323704237052370623707237082370923710237112371223713237142371523716237172371823719237202372123722237232372423725237262372723728237292373023731237322373323734237352373623737237382373923740237412374223743237442374523746237472374823749237502375123752237532375423755237562375723758237592376023761237622376323764237652376623767237682376923770237712377223773237742377523776237772377823779237802378123782237832378423785237862378723788237892379023791237922379323794237952379623797237982379923800238012380223803238042380523806238072380823809238102381123812238132381423815238162381723818238192382023821238222382323824238252382623827238282382923830238312383223833238342383523836238372383823839238402384123842238432384423845238462384723848238492385023851238522385323854238552385623857238582385923860238612386223863238642386523866238672386823869238702387123872238732387423875238762387723878238792388023881238822388323884238852388623887238882388923890238912389223893238942389523896238972389823899239002390123902239032390423905239062390723908239092391023911239122391323914239152391623917239182391923920239212392223923239242392523926239272392823929239302393123932239332393423935239362393723938239392394023941239422394323944239452394623947239482394923950239512395223953239542395523956239572395823959239602396123962239632396423965239662396723968239692397023971239722397323974239752397623977239782397923980239812398223983239842398523986239872398823989239902399123992239932399423995239962399723998239992400024001240022400324004240052400624007240082400924010240112401224013240142401524016240172401824019240202402124022240232402424025240262402724028240292403024031240322403324034240352403624037240382403924040240412404224043240442404524046240472404824049240502405124052240532405424055240562405724058240592406024061240622406324064240652406624067240682406924070240712407224073240742407524076240772407824079240802408124082240832408424085240862408724088240892409024091240922409324094240952409624097240982409924100241012410224103241042410524106241072410824109241102411124112241132411424115241162411724118241192412024121241222412324124241252412624127241282412924130241312413224133241342413524136241372413824139241402414124142241432414424145241462414724148241492415024151241522415324154241552415624157241582415924160241612416224163241642416524166241672416824169241702417124172241732417424175241762417724178241792418024181241822418324184241852418624187241882418924190241912419224193241942419524196241972419824199242002420124202242032420424205242062420724208242092421024211242122421324214242152421624217242182421924220242212422224223242242422524226242272422824229242302423124232242332423424235242362423724238242392424024241242422424324244242452424624247242482424924250242512425224253242542425524256242572425824259242602426124262242632426424265242662426724268242692427024271242722427324274242752427624277242782427924280242812428224283242842428524286242872428824289242902429124292242932429424295242962429724298242992430024301243022430324304243052430624307243082430924310243112431224313243142431524316243172431824319243202432124322243232432424325243262432724328243292433024331243322433324334243352433624337243382433924340243412434224343243442434524346243472434824349243502435124352243532435424355243562435724358243592436024361243622436324364243652436624367243682436924370243712437224373243742437524376243772437824379243802438124382243832438424385243862438724388243892439024391243922439324394243952439624397243982439924400244012440224403244042440524406244072440824409244102441124412244132441424415244162441724418244192442024421244222442324424244252442624427244282442924430244312443224433244342443524436244372443824439244402444124442244432444424445244462444724448244492445024451244522445324454244552445624457244582445924460244612446224463244642446524466244672446824469244702447124472244732447424475244762447724478244792448024481244822448324484244852448624487244882448924490244912449224493244942449524496244972449824499245002450124502245032450424505245062450724508245092451024511245122451324514245152451624517245182451924520245212452224523245242452524526245272452824529245302453124532245332453424535245362453724538245392454024541245422454324544245452454624547245482454924550245512455224553245542455524556245572455824559245602456124562245632456424565245662456724568245692457024571245722457324574245752457624577245782457924580245812458224583245842458524586245872458824589245902459124592245932459424595245962459724598245992460024601246022460324604246052460624607246082460924610246112461224613246142461524616246172461824619246202462124622246232462424625246262462724628246292463024631246322463324634246352463624637246382463924640246412464224643246442464524646246472464824649246502465124652246532465424655246562465724658246592466024661246622466324664246652466624667246682466924670246712467224673246742467524676246772467824679246802468124682246832468424685246862468724688246892469024691246922469324694246952469624697246982469924700247012470224703247042470524706247072470824709247102471124712247132471424715247162471724718247192472024721247222472324724247252472624727247282472924730247312473224733247342473524736247372473824739247402474124742247432474424745247462474724748247492475024751247522475324754247552475624757247582475924760247612476224763247642476524766247672476824769247702477124772247732477424775247762477724778247792478024781247822478324784247852478624787247882478924790247912479224793247942479524796247972479824799248002480124802248032480424805248062480724808248092481024811248122481324814248152481624817248182481924820248212482224823248242482524826248272482824829248302483124832248332483424835248362483724838248392484024841248422484324844248452484624847248482484924850248512485224853248542485524856248572485824859248602486124862248632486424865248662486724868248692487024871248722487324874248752487624877248782487924880248812488224883248842488524886248872488824889248902489124892248932489424895248962489724898248992490024901249022490324904249052490624907249082490924910249112491224913249142491524916249172491824919249202492124922249232492424925249262492724928249292493024931249322493324934249352493624937249382493924940249412494224943249442494524946249472494824949249502495124952249532495424955249562495724958249592496024961249622496324964249652496624967249682496924970249712497224973249742497524976249772497824979249802498124982249832498424985249862498724988249892499024991249922499324994249952499624997249982499925000250012500225003250042500525006250072500825009250102501125012250132501425015250162501725018250192502025021250222502325024250252502625027250282502925030250312503225033250342503525036250372503825039250402504125042250432504425045250462504725048250492505025051250522505325054250552505625057250582505925060250612506225063250642506525066250672506825069250702507125072250732507425075250762507725078250792508025081250822508325084250852508625087250882508925090250912509225093250942509525096250972509825099251002510125102251032510425105251062510725108251092511025111251122511325114251152511625117251182511925120251212512225123251242512525126251272512825129251302513125132251332513425135251362513725138251392514025141251422514325144251452514625147251482514925150251512515225153251542515525156251572515825159251602516125162251632516425165251662516725168251692517025171251722517325174251752517625177251782517925180251812518225183251842518525186251872518825189251902519125192251932519425195251962519725198251992520025201252022520325204252052520625207252082520925210252112521225213252142521525216252172521825219252202522125222252232522425225252262522725228252292523025231252322523325234252352523625237252382523925240252412524225243252442524525246252472524825249252502525125252252532525425255252562525725258252592526025261252622526325264252652526625267252682526925270252712527225273252742527525276252772527825279252802528125282252832528425285252862528725288252892529025291252922529325294252952529625297252982529925300253012530225303253042530525306253072530825309253102531125312253132531425315253162531725318253192532025321253222532325324253252532625327253282532925330253312533225333253342533525336253372533825339253402534125342253432534425345253462534725348253492535025351253522535325354253552535625357253582535925360253612536225363253642536525366253672536825369253702537125372253732537425375253762537725378253792538025381253822538325384253852538625387253882538925390253912539225393253942539525396253972539825399254002540125402254032540425405254062540725408254092541025411254122541325414254152541625417254182541925420254212542225423254242542525426254272542825429254302543125432254332543425435254362543725438254392544025441254422544325444254452544625447254482544925450254512545225453254542545525456254572545825459254602546125462254632546425465254662546725468254692547025471254722547325474254752547625477254782547925480254812548225483254842548525486254872548825489254902549125492254932549425495254962549725498254992550025501255022550325504255052550625507255082550925510255112551225513255142551525516255172551825519255202552125522255232552425525255262552725528255292553025531255322553325534255352553625537255382553925540255412554225543255442554525546255472554825549255502555125552255532555425555255562555725558255592556025561255622556325564255652556625567255682556925570255712557225573255742557525576255772557825579255802558125582255832558425585255862558725588255892559025591255922559325594255952559625597255982559925600256012560225603256042560525606256072560825609256102561125612256132561425615256162561725618256192562025621256222562325624256252562625627256282562925630256312563225633256342563525636256372563825639256402564125642256432564425645256462564725648256492565025651256522565325654256552565625657256582565925660256612566225663256642566525666256672566825669256702567125672256732567425675256762567725678256792568025681256822568325684256852568625687256882568925690256912569225693256942569525696256972569825699257002570125702257032570425705257062570725708257092571025711257122571325714257152571625717257182571925720257212572225723257242572525726257272572825729257302573125732257332573425735257362573725738257392574025741257422574325744257452574625747257482574925750257512575225753257542575525756257572575825759257602576125762257632576425765257662576725768257692577025771257722577325774257752577625777257782577925780257812578225783257842578525786257872578825789257902579125792257932579425795257962579725798257992580025801258022580325804258052580625807258082580925810258112581225813258142581525816258172581825819258202582125822258232582425825258262582725828258292583025831258322583325834258352583625837258382583925840258412584225843258442584525846258472584825849258502585125852258532585425855258562585725858258592586025861258622586325864258652586625867258682586925870258712587225873258742587525876258772587825879258802588125882258832588425885258862588725888258892589025891258922589325894258952589625897258982589925900259012590225903259042590525906259072590825909259102591125912259132591425915259162591725918259192592025921259222592325924259252592625927259282592925930259312593225933259342593525936259372593825939259402594125942259432594425945259462594725948259492595025951259522595325954259552595625957259582595925960259612596225963259642596525966259672596825969259702597125972259732597425975259762597725978259792598025981259822598325984259852598625987259882598925990259912599225993259942599525996259972599825999260002600126002260032600426005260062600726008260092601026011260122601326014260152601626017260182601926020260212602226023260242602526026260272602826029260302603126032260332603426035260362603726038260392604026041260422604326044260452604626047260482604926050260512605226053260542605526056260572605826059260602606126062260632606426065260662606726068260692607026071260722607326074260752607626077260782607926080260812608226083260842608526086260872608826089260902609126092260932609426095260962609726098260992610026101261022610326104261052610626107261082610926110261112611226113261142611526116261172611826119261202612126122261232612426125261262612726128261292613026131261322613326134261352613626137261382613926140261412614226143261442614526146261472614826149261502615126152261532615426155261562615726158261592616026161261622616326164261652616626167261682616926170261712617226173261742617526176261772617826179261802618126182261832618426185261862618726188261892619026191261922619326194261952619626197261982619926200262012620226203262042620526206262072620826209262102621126212262132621426215262162621726218262192622026221262222622326224262252622626227262282622926230262312623226233262342623526236262372623826239262402624126242262432624426245262462624726248262492625026251262522625326254262552625626257262582625926260262612626226263262642626526266262672626826269262702627126272262732627426275262762627726278262792628026281262822628326284262852628626287262882628926290262912629226293262942629526296262972629826299263002630126302263032630426305263062630726308263092631026311263122631326314263152631626317263182631926320263212632226323263242632526326263272632826329263302633126332263332633426335263362633726338263392634026341263422634326344263452634626347263482634926350263512635226353263542635526356263572635826359263602636126362263632636426365263662636726368263692637026371263722637326374263752637626377263782637926380263812638226383263842638526386263872638826389263902639126392263932639426395263962639726398263992640026401264022640326404264052640626407264082640926410264112641226413264142641526416264172641826419264202642126422264232642426425264262642726428264292643026431264322643326434264352643626437264382643926440264412644226443264442644526446264472644826449264502645126452264532645426455264562645726458264592646026461264622646326464264652646626467264682646926470264712647226473264742647526476264772647826479264802648126482264832648426485264862648726488264892649026491264922649326494264952649626497264982649926500265012650226503265042650526506265072650826509265102651126512265132651426515265162651726518265192652026521265222652326524265252652626527265282652926530265312653226533265342653526536265372653826539265402654126542265432654426545265462654726548265492655026551265522655326554265552655626557265582655926560265612656226563265642656526566265672656826569265702657126572265732657426575265762657726578265792658026581265822658326584265852658626587265882658926590265912659226593265942659526596265972659826599266002660126602266032660426605266062660726608266092661026611266122661326614266152661626617266182661926620266212662226623266242662526626266272662826629266302663126632266332663426635266362663726638266392664026641266422664326644266452664626647266482664926650266512665226653266542665526656266572665826659266602666126662266632666426665266662666726668266692667026671266722667326674266752667626677266782667926680266812668226683266842668526686266872668826689266902669126692266932669426695266962669726698266992670026701267022670326704267052670626707267082670926710267112671226713267142671526716267172671826719267202672126722267232672426725267262672726728267292673026731267322673326734267352673626737267382673926740267412674226743267442674526746267472674826749267502675126752267532675426755267562675726758267592676026761267622676326764267652676626767267682676926770267712677226773267742677526776267772677826779267802678126782267832678426785267862678726788267892679026791267922679326794267952679626797267982679926800268012680226803268042680526806268072680826809268102681126812268132681426815268162681726818268192682026821268222682326824268252682626827268282682926830268312683226833268342683526836268372683826839268402684126842268432684426845268462684726848268492685026851268522685326854268552685626857268582685926860268612686226863268642686526866268672686826869268702687126872268732687426875268762687726878268792688026881268822688326884268852688626887268882688926890268912689226893268942689526896268972689826899269002690126902269032690426905269062690726908269092691026911269122691326914269152691626917269182691926920269212692226923269242692526926269272692826929269302693126932269332693426935269362693726938269392694026941269422694326944269452694626947269482694926950269512695226953269542695526956269572695826959269602696126962269632696426965269662696726968269692697026971269722697326974269752697626977269782697926980269812698226983269842698526986269872698826989269902699126992269932699426995269962699726998269992700027001270022700327004270052700627007270082700927010270112701227013270142701527016270172701827019270202702127022270232702427025270262702727028270292703027031270322703327034270352703627037270382703927040270412704227043270442704527046270472704827049270502705127052270532705427055270562705727058270592706027061270622706327064270652706627067270682706927070270712707227073270742707527076270772707827079270802708127082270832708427085270862708727088270892709027091270922709327094270952709627097270982709927100271012710227103271042710527106271072710827109271102711127112271132711427115271162711727118271192712027121271222712327124271252712627127271282712927130271312713227133271342713527136271372713827139271402714127142271432714427145271462714727148271492715027151271522715327154271552715627157271582715927160271612716227163271642716527166271672716827169271702717127172271732717427175271762717727178271792718027181271822718327184271852718627187271882718927190271912719227193271942719527196271972719827199272002720127202272032720427205272062720727208272092721027211272122721327214272152721627217272182721927220272212722227223272242722527226272272722827229272302723127232272332723427235272362723727238272392724027241272422724327244272452724627247272482724927250272512725227253272542725527256272572725827259272602726127262272632726427265272662726727268272692727027271272722727327274272752727627277272782727927280272812728227283272842728527286272872728827289272902729127292272932729427295272962729727298272992730027301273022730327304273052730627307273082730927310273112731227313273142731527316273172731827319273202732127322273232732427325273262732727328273292733027331273322733327334273352733627337273382733927340273412734227343273442734527346273472734827349273502735127352273532735427355273562735727358273592736027361273622736327364273652736627367273682736927370273712737227373273742737527376273772737827379273802738127382273832738427385273862738727388273892739027391273922739327394273952739627397273982739927400274012740227403274042740527406274072740827409274102741127412274132741427415274162741727418274192742027421274222742327424274252742627427274282742927430274312743227433274342743527436274372743827439274402744127442274432744427445274462744727448274492745027451274522745327454274552745627457274582745927460274612746227463274642746527466274672746827469274702747127472274732747427475274762747727478274792748027481274822748327484274852748627487274882748927490274912749227493274942749527496274972749827499275002750127502275032750427505275062750727508275092751027511275122751327514275152751627517275182751927520275212752227523275242752527526275272752827529275302753127532275332753427535275362753727538275392754027541275422754327544275452754627547275482754927550275512755227553275542755527556275572755827559275602756127562275632756427565275662756727568275692757027571275722757327574275752757627577275782757927580275812758227583275842758527586275872758827589275902759127592275932759427595275962759727598275992760027601276022760327604276052760627607276082760927610276112761227613276142761527616276172761827619276202762127622276232762427625276262762727628276292763027631276322763327634276352763627637276382763927640276412764227643276442764527646276472764827649276502765127652276532765427655276562765727658276592766027661276622766327664276652766627667276682766927670276712767227673276742767527676276772767827679276802768127682276832768427685276862768727688276892769027691276922769327694276952769627697276982769927700277012770227703277042770527706277072770827709277102771127712277132771427715277162771727718277192772027721277222772327724277252772627727277282772927730277312773227733277342773527736277372773827739277402774127742277432774427745277462774727748277492775027751277522775327754277552775627757277582775927760277612776227763277642776527766277672776827769277702777127772277732777427775277762777727778277792778027781277822778327784277852778627787277882778927790277912779227793277942779527796277972779827799278002780127802278032780427805278062780727808278092781027811278122781327814278152781627817278182781927820278212782227823278242782527826278272782827829278302783127832278332783427835278362783727838278392784027841278422784327844278452784627847278482784927850278512785227853278542785527856278572785827859278602786127862278632786427865278662786727868278692787027871278722787327874278752787627877278782787927880278812788227883278842788527886278872788827889278902789127892278932789427895278962789727898278992790027901279022790327904279052790627907279082790927910279112791227913279142791527916279172791827919279202792127922279232792427925279262792727928279292793027931279322793327934279352793627937279382793927940279412794227943279442794527946279472794827949279502795127952279532795427955279562795727958279592796027961279622796327964279652796627967279682796927970279712797227973279742797527976279772797827979279802798127982279832798427985279862798727988279892799027991279922799327994279952799627997279982799928000280012800228003280042800528006280072800828009280102801128012280132801428015280162801728018280192802028021280222802328024280252802628027280282802928030280312803228033280342803528036280372803828039280402804128042280432804428045280462804728048280492805028051280522805328054280552805628057280582805928060280612806228063280642806528066280672806828069280702807128072280732807428075280762807728078280792808028081280822808328084280852808628087280882808928090280912809228093280942809528096280972809828099281002810128102281032810428105281062810728108281092811028111281122811328114281152811628117281182811928120281212812228123281242812528126281272812828129281302813128132281332813428135281362813728138281392814028141281422814328144281452814628147281482814928150281512815228153281542815528156281572815828159281602816128162281632816428165281662816728168281692817028171281722817328174281752817628177281782817928180281812818228183281842818528186281872818828189281902819128192281932819428195281962819728198281992820028201282022820328204282052820628207282082820928210282112821228213282142821528216282172821828219282202822128222282232822428225282262822728228282292823028231282322823328234282352823628237282382823928240282412824228243282442824528246282472824828249282502825128252282532825428255282562825728258282592826028261282622826328264282652826628267282682826928270282712827228273282742827528276282772827828279282802828128282282832828428285282862828728288282892829028291282922829328294282952829628297282982829928300283012830228303283042830528306283072830828309283102831128312283132831428315283162831728318283192832028321283222832328324283252832628327283282832928330283312833228333283342833528336283372833828339283402834128342283432834428345283462834728348283492835028351283522835328354283552835628357283582835928360283612836228363283642836528366283672836828369283702837128372283732837428375283762837728378283792838028381283822838328384283852838628387283882838928390283912839228393283942839528396283972839828399284002840128402284032840428405284062840728408284092841028411284122841328414284152841628417284182841928420284212842228423284242842528426284272842828429284302843128432284332843428435284362843728438284392844028441284422844328444284452844628447284482844928450284512845228453284542845528456284572845828459284602846128462284632846428465284662846728468284692847028471284722847328474284752847628477284782847928480284812848228483284842848528486284872848828489284902849128492284932849428495284962849728498284992850028501285022850328504285052850628507285082850928510285112851228513285142851528516285172851828519285202852128522285232852428525285262852728528285292853028531285322853328534285352853628537285382853928540285412854228543285442854528546285472854828549285502855128552285532855428555285562855728558285592856028561285622856328564285652856628567285682856928570285712857228573285742857528576285772857828579285802858128582285832858428585285862858728588285892859028591285922859328594285952859628597285982859928600286012860228603286042860528606286072860828609286102861128612286132861428615286162861728618286192862028621286222862328624286252862628627286282862928630286312863228633286342863528636286372863828639286402864128642286432864428645286462864728648286492865028651286522865328654286552865628657286582865928660286612866228663286642866528666286672866828669286702867128672286732867428675286762867728678286792868028681286822868328684286852868628687286882868928690286912869228693286942869528696286972869828699287002870128702287032870428705287062870728708287092871028711287122871328714287152871628717287182871928720287212872228723287242872528726287272872828729287302873128732287332873428735287362873728738287392874028741287422874328744287452874628747287482874928750287512875228753287542875528756287572875828759287602876128762287632876428765287662876728768287692877028771287722877328774287752877628777287782877928780287812878228783287842878528786287872878828789287902879128792287932879428795287962879728798287992880028801288022880328804288052880628807288082880928810288112881228813288142881528816288172881828819288202882128822288232882428825288262882728828288292883028831288322883328834288352883628837288382883928840288412884228843288442884528846288472884828849288502885128852288532885428855288562885728858288592886028861288622886328864288652886628867288682886928870288712887228873288742887528876288772887828879288802888128882288832888428885288862888728888288892889028891288922889328894288952889628897288982889928900289012890228903289042890528906289072890828909289102891128912289132891428915289162891728918289192892028921289222892328924289252892628927289282892928930289312893228933289342893528936289372893828939289402894128942289432894428945289462894728948289492895028951289522895328954289552895628957289582895928960289612896228963289642896528966289672896828969289702897128972289732897428975289762897728978289792898028981289822898328984289852898628987289882898928990289912899228993289942899528996289972899828999290002900129002290032900429005290062900729008290092901029011290122901329014290152901629017290182901929020290212902229023290242902529026290272902829029290302903129032290332903429035290362903729038290392904029041290422904329044290452904629047290482904929050290512905229053290542905529056290572905829059290602906129062290632906429065290662906729068290692907029071290722907329074290752907629077290782907929080290812908229083290842908529086290872908829089290902909129092290932909429095290962909729098290992910029101291022910329104291052910629107291082910929110291112911229113291142911529116291172911829119291202912129122291232912429125291262912729128291292913029131291322913329134291352913629137291382913929140291412914229143291442914529146291472914829149291502915129152291532915429155291562915729158291592916029161291622916329164291652916629167291682916929170291712917229173291742917529176291772917829179291802918129182291832918429185291862918729188291892919029191291922919329194291952919629197291982919929200292012920229203292042920529206292072920829209292102921129212292132921429215292162921729218292192922029221292222922329224292252922629227292282922929230292312923229233292342923529236292372923829239292402924129242292432924429245292462924729248292492925029251292522925329254292552925629257292582925929260292612926229263292642926529266292672926829269292702927129272292732927429275292762927729278292792928029281292822928329284292852928629287292882928929290292912929229293292942929529296292972929829299293002930129302293032930429305293062930729308293092931029311293122931329314293152931629317293182931929320293212932229323293242932529326293272932829329293302933129332293332933429335293362933729338293392934029341293422934329344293452934629347293482934929350293512935229353293542935529356293572935829359293602936129362293632936429365293662936729368293692937029371293722937329374293752937629377293782937929380293812938229383293842938529386293872938829389293902939129392293932939429395293962939729398293992940029401294022940329404294052940629407294082940929410294112941229413294142941529416294172941829419294202942129422294232942429425294262942729428294292943029431294322943329434294352943629437294382943929440294412944229443294442944529446294472944829449294502945129452294532945429455294562945729458294592946029461294622946329464294652946629467294682946929470294712947229473294742947529476294772947829479294802948129482294832948429485294862948729488294892949029491294922949329494294952949629497294982949929500295012950229503295042950529506295072950829509295102951129512295132951429515295162951729518295192952029521295222952329524295252952629527295282952929530295312953229533295342953529536295372953829539295402954129542295432954429545295462954729548295492955029551295522955329554295552955629557295582955929560295612956229563295642956529566295672956829569295702957129572295732957429575295762957729578295792958029581295822958329584295852958629587295882958929590295912959229593295942959529596295972959829599296002960129602296032960429605296062960729608296092961029611296122961329614296152961629617296182961929620296212962229623296242962529626296272962829629296302963129632296332963429635296362963729638296392964029641296422964329644296452964629647296482964929650296512965229653296542965529656296572965829659296602966129662296632966429665296662966729668296692967029671296722967329674296752967629677296782967929680296812968229683296842968529686296872968829689296902969129692296932969429695296962969729698296992970029701297022970329704297052970629707297082970929710297112971229713297142971529716297172971829719297202972129722297232972429725297262972729728297292973029731297322973329734297352973629737297382973929740297412974229743297442974529746297472974829749297502975129752297532975429755297562975729758297592976029761297622976329764297652976629767297682976929770297712977229773297742977529776297772977829779297802978129782297832978429785297862978729788297892979029791297922979329794297952979629797297982979929800298012980229803298042980529806298072980829809298102981129812298132981429815298162981729818298192982029821298222982329824298252982629827298282982929830298312983229833298342983529836298372983829839298402984129842298432984429845298462984729848298492985029851298522985329854298552985629857298582985929860298612986229863298642986529866298672986829869298702987129872298732987429875298762987729878298792988029881298822988329884298852988629887298882988929890298912989229893298942989529896298972989829899299002990129902299032990429905299062990729908299092991029911299122991329914299152991629917299182991929920299212992229923299242992529926299272992829929299302993129932299332993429935299362993729938299392994029941299422994329944299452994629947299482994929950299512995229953299542995529956299572995829959299602996129962299632996429965299662996729968299692997029971299722997329974299752997629977299782997929980299812998229983299842998529986299872998829989299902999129992299932999429995299962999729998299993000030001300023000330004300053000630007300083000930010300113001230013300143001530016300173001830019300203002130022300233002430025300263002730028300293003030031300323003330034300353003630037300383003930040300413004230043300443004530046300473004830049300503005130052300533005430055300563005730058300593006030061300623006330064300653006630067300683006930070300713007230073300743007530076300773007830079300803008130082300833008430085300863008730088300893009030091300923009330094300953009630097300983009930100301013010230103301043010530106301073010830109301103011130112301133011430115301163011730118301193012030121301223012330124301253012630127301283012930130301313013230133301343013530136301373013830139301403014130142301433014430145301463014730148301493015030151301523015330154301553015630157301583015930160301613016230163301643016530166301673016830169301703017130172301733017430175301763017730178301793018030181301823018330184301853018630187301883018930190301913019230193301943019530196301973019830199302003020130202302033020430205302063020730208302093021030211302123021330214302153021630217302183021930220302213022230223302243022530226302273022830229302303023130232302333023430235302363023730238302393024030241302423024330244302453024630247302483024930250302513025230253302543025530256302573025830259302603026130262302633026430265302663026730268302693027030271302723027330274302753027630277302783027930280302813028230283302843028530286302873028830289302903029130292302933029430295302963029730298302993030030301303023030330304303053030630307303083030930310303113031230313303143031530316303173031830319303203032130322303233032430325303263032730328303293033030331303323033330334
  1. /*
  2. /// # Nuklear
  3. /// ![](https://cloud.githubusercontent.com/assets/8057201/11761525/ae06f0ca-a0c6-11e5-819d-5610b25f6ef4.gif)
  4. ///
  5. /// ## Contents
  6. /// 1. About section
  7. /// 2. Highlights section
  8. /// 3. Features section
  9. /// 4. Usage section
  10. /// 1. Flags section
  11. /// 2. Constants section
  12. /// 3. Dependencies section
  13. /// 5. Example section
  14. /// 6. API section
  15. /// 1. Context section
  16. /// 2. Input section
  17. /// 3. Drawing section
  18. /// 4. Window section
  19. /// 5. Layouting section
  20. /// 6. Groups section
  21. /// 7. Tree section
  22. /// 8. Properties section
  23. /// 7. License section
  24. /// 8. Changelog section
  25. /// 9. Gallery section
  26. /// 10. Credits section
  27. ///
  28. /// ## About
  29. /// This is a minimal state immediate mode graphical user interface toolkit
  30. /// written in ANSI C and licensed under public domain. It was designed as a simple
  31. /// embeddable user interface for application and does not have any dependencies,
  32. /// a default renderbackend or OS window and input handling but instead provides a very modular
  33. /// library approach by using simple input state for input and draw
  34. /// commands describing primitive shapes as output. So instead of providing a
  35. /// layered library that tries to abstract over a number of platform and
  36. /// render backends it only focuses on the actual UI.
  37. ///
  38. /// ## Highlights
  39. /// - Graphical user interface toolkit
  40. /// - Single header library
  41. /// - Written in C89 (a.k.a. ANSI C or ISO C90)
  42. /// - Small codebase (~18kLOC)
  43. /// - Focus on portability, efficiency and simplicity
  44. /// - No dependencies (not even the standard library if not wanted)
  45. /// - Fully skinnable and customizable
  46. /// - Low memory footprint with total memory control if needed or wanted
  47. /// - UTF-8 support
  48. /// - No global or hidden state
  49. /// - Customizable library modules (you can compile and use only what you need)
  50. /// - Optional font baker and vertex buffer output
  51. /// - [Code available on github](https://github.com/Immediate-Mode-UI/Nuklear/)
  52. ///
  53. /// ## Features
  54. /// - Absolutely no platform dependent code
  55. /// - Memory management control ranging from/to
  56. /// - Ease of use by allocating everything from standard library
  57. /// - Control every byte of memory inside the library
  58. /// - Font handling control ranging from/to
  59. /// - Use your own font implementation for everything
  60. /// - Use this libraries internal font baking and handling API
  61. /// - Drawing output control ranging from/to
  62. /// - Simple shapes for more high level APIs which already have drawing capabilities
  63. /// - Hardware accessible anti-aliased vertex buffer output
  64. /// - Customizable colors and properties ranging from/to
  65. /// - Simple changes to color by filling a simple color table
  66. /// - Complete control with ability to use skinning to decorate widgets
  67. /// - Bendable UI library with widget ranging from/to
  68. /// - Basic widgets like buttons, checkboxes, slider, ...
  69. /// - Advanced widget like abstract comboboxes, contextual menus,...
  70. /// - Compile time configuration to only compile what you need
  71. /// - Subset which can be used if you do not want to link or use the standard library
  72. /// - Can be easily modified to only update on user input instead of frame updates
  73. ///
  74. /// ## Usage
  75. /// This library is self contained in one single header file and can be used either
  76. /// in header only mode or in implementation mode. The header only mode is used
  77. /// by default when included and allows including this header in other headers
  78. /// and does not contain the actual implementation. <br /><br />
  79. ///
  80. /// The implementation mode requires to define the preprocessor macro
  81. /// NK_IMPLEMENTATION in *one* .c/.cpp file before #including this file, e.g.:
  82. ///
  83. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~C
  84. /// #define NK_IMPLEMENTATION
  85. /// #include "nuklear.h"
  86. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  87. ///
  88. /// Also optionally define the symbols listed in the section "OPTIONAL DEFINES"
  89. /// below in header and implementation mode if you want to use additional functionality
  90. /// or need more control over the library.
  91. ///
  92. /// !!! WARNING
  93. /// Every time nuklear is included define the same compiler flags. This very important not doing so could lead to compiler errors or even worse stack corruptions.
  94. ///
  95. /// ### Flags
  96. /// Flag | Description
  97. /// --------------------------------|------------------------------------------
  98. /// NK_PRIVATE | If defined declares all functions as static, so they can only be accessed inside the file that contains the implementation
  99. /// NK_INCLUDE_FIXED_TYPES | If defined it will include header `<stdint.h>` for fixed sized types otherwise nuklear tries to select the correct type. If that fails it will throw a compiler error and you have to select the correct types yourself.
  100. /// NK_INCLUDE_DEFAULT_ALLOCATOR | If defined it will include header `<stdlib.h>` and provide additional functions to use this library without caring for memory allocation control and therefore ease memory management.
  101. /// NK_INCLUDE_STANDARD_IO | If defined it will include header `<stdio.h>` and provide additional functions depending on file loading.
  102. /// NK_INCLUDE_STANDARD_VARARGS | If defined it will include header <stdarg.h> and provide additional functions depending on file loading.
  103. /// NK_INCLUDE_STANDARD_BOOL | If defined it will include header `<stdbool.h>` for nk_bool otherwise nuklear defines nk_bool as int.
  104. /// NK_INCLUDE_VERTEX_BUFFER_OUTPUT | Defining this adds a vertex draw command list backend to this library, which allows you to convert queue commands into vertex draw commands. This is mainly if you need a hardware accessible format for OpenGL, DirectX, Vulkan, Metal,...
  105. /// NK_INCLUDE_FONT_BAKING | Defining this adds `stb_truetype` and `stb_rect_pack` implementation to this library and provides font baking and rendering. If you already have font handling or do not want to use this font handler you don't have to define it.
  106. /// NK_INCLUDE_DEFAULT_FONT | Defining this adds the default font: ProggyClean.ttf into this library which can be loaded into a font atlas and allows using this library without having a truetype font
  107. /// NK_INCLUDE_COMMAND_USERDATA | Defining this adds a userdata pointer into each command. Can be useful for example if you want to provide custom shaders depending on the used widget. Can be combined with the style structures.
  108. /// NK_BUTTON_TRIGGER_ON_RELEASE | Different platforms require button clicks occurring either on buttons being pressed (up to down) or released (down to up). By default this library will react on buttons being pressed, but if you define this it will only trigger if a button is released.
  109. /// NK_ZERO_COMMAND_MEMORY | Defining this will zero out memory for each drawing command added to a drawing queue (inside nk_command_buffer_push). Zeroing command memory is very useful for fast checking (using memcmp) if command buffers are equal and avoid drawing frames when nothing on screen has changed since previous frame.
  110. /// NK_UINT_DRAW_INDEX | Defining this will set the size of vertex index elements when using NK_VERTEX_BUFFER_OUTPUT to 32bit instead of the default of 16bit
  111. /// NK_KEYSTATE_BASED_INPUT | Define this if your backend uses key state for each frame rather than key press/release events
  112. ///
  113. /// !!! WARNING
  114. /// The following flags will pull in the standard C library:
  115. /// - NK_INCLUDE_DEFAULT_ALLOCATOR
  116. /// - NK_INCLUDE_STANDARD_IO
  117. /// - NK_INCLUDE_STANDARD_VARARGS
  118. ///
  119. /// !!! WARNING
  120. /// The following flags if defined need to be defined for both header and implementation:
  121. /// - NK_INCLUDE_FIXED_TYPES
  122. /// - NK_INCLUDE_DEFAULT_ALLOCATOR
  123. /// - NK_INCLUDE_STANDARD_VARARGS
  124. /// - NK_INCLUDE_STANDARD_BOOL
  125. /// - NK_INCLUDE_VERTEX_BUFFER_OUTPUT
  126. /// - NK_INCLUDE_FONT_BAKING
  127. /// - NK_INCLUDE_DEFAULT_FONT
  128. /// - NK_INCLUDE_STANDARD_VARARGS
  129. /// - NK_INCLUDE_COMMAND_USERDATA
  130. /// - NK_UINT_DRAW_INDEX
  131. ///
  132. /// ### Constants
  133. /// Define | Description
  134. /// --------------------------------|---------------------------------------
  135. /// NK_BUFFER_DEFAULT_INITIAL_SIZE | Initial buffer size allocated by all buffers while using the default allocator functions included by defining NK_INCLUDE_DEFAULT_ALLOCATOR. If you don't want to allocate the default 4k memory then redefine it.
  136. /// NK_MAX_NUMBER_BUFFER | Maximum buffer size for the conversion buffer between float and string Under normal circumstances this should be more than sufficient.
  137. /// NK_INPUT_MAX | Defines the max number of bytes which can be added as text input in one frame. Under normal circumstances this should be more than sufficient.
  138. ///
  139. /// !!! WARNING
  140. /// The following constants if defined need to be defined for both header and implementation:
  141. /// - NK_MAX_NUMBER_BUFFER
  142. /// - NK_BUFFER_DEFAULT_INITIAL_SIZE
  143. /// - NK_INPUT_MAX
  144. ///
  145. /// ### Dependencies
  146. /// Function | Description
  147. /// ------------|---------------------------------------------------------------
  148. /// NK_ASSERT | If you don't define this, nuklear will use <assert.h> with assert().
  149. /// NK_MEMSET | You can define this to 'memset' or your own memset implementation replacement. If not nuklear will use its own version.
  150. /// NK_MEMCPY | You can define this to 'memcpy' or your own memcpy implementation replacement. If not nuklear will use its own version.
  151. /// NK_INV_SQRT | You can define this to your own inverse sqrt implementation replacement. If not nuklear will use its own slow and not highly accurate version.
  152. /// NK_SIN | You can define this to 'sinf' or your own sine implementation replacement. If not nuklear will use its own approximation implementation.
  153. /// NK_COS | You can define this to 'cosf' or your own cosine implementation replacement. If not nuklear will use its own approximation implementation.
  154. /// NK_STRTOD | You can define this to `strtod` or your own string to double conversion implementation replacement. If not defined nuklear will use its own imprecise and possibly unsafe version (does not handle nan or infinity!).
  155. /// NK_DTOA | You can define this to `dtoa` or your own double to string conversion implementation replacement. If not defined nuklear will use its own imprecise and possibly unsafe version (does not handle nan or infinity!).
  156. /// NK_VSNPRINTF| If you define `NK_INCLUDE_STANDARD_VARARGS` as well as `NK_INCLUDE_STANDARD_IO` and want to be safe define this to `vsnprintf` on compilers supporting later versions of C or C++. By default nuklear will check for your stdlib version in C as well as compiler version in C++. if `vsnprintf` is available it will define it to `vsnprintf` directly. If not defined and if you have older versions of C or C++ it will be defined to `vsprintf` which is unsafe.
  157. ///
  158. /// !!! WARNING
  159. /// The following dependencies will pull in the standard C library if not redefined:
  160. /// - NK_ASSERT
  161. ///
  162. /// !!! WARNING
  163. /// The following dependencies if defined need to be defined for both header and implementation:
  164. /// - NK_ASSERT
  165. ///
  166. /// !!! WARNING
  167. /// The following dependencies if defined need to be defined only for the implementation part:
  168. /// - NK_MEMSET
  169. /// - NK_MEMCPY
  170. /// - NK_SQRT
  171. /// - NK_SIN
  172. /// - NK_COS
  173. /// - NK_STRTOD
  174. /// - NK_DTOA
  175. /// - NK_VSNPRINTF
  176. ///
  177. /// ## Example
  178. ///
  179. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  180. /// // init gui state
  181. /// enum {EASY, HARD};
  182. /// static int op = EASY;
  183. /// static float value = 0.6f;
  184. /// static int i = 20;
  185. /// struct nk_context ctx;
  186. ///
  187. /// nk_init_fixed(&ctx, calloc(1, MAX_MEMORY), MAX_MEMORY, &font);
  188. /// if (nk_begin(&ctx, "Show", nk_rect(50, 50, 220, 220),
  189. /// NK_WINDOW_BORDER|NK_WINDOW_MOVABLE|NK_WINDOW_CLOSABLE)) {
  190. /// // fixed widget pixel width
  191. /// nk_layout_row_static(&ctx, 30, 80, 1);
  192. /// if (nk_button_label(&ctx, "button")) {
  193. /// // event handling
  194. /// }
  195. ///
  196. /// // fixed widget window ratio width
  197. /// nk_layout_row_dynamic(&ctx, 30, 2);
  198. /// if (nk_option_label(&ctx, "easy", op == EASY)) op = EASY;
  199. /// if (nk_option_label(&ctx, "hard", op == HARD)) op = HARD;
  200. ///
  201. /// // custom widget pixel width
  202. /// nk_layout_row_begin(&ctx, NK_STATIC, 30, 2);
  203. /// {
  204. /// nk_layout_row_push(&ctx, 50);
  205. /// nk_label(&ctx, "Volume:", NK_TEXT_LEFT);
  206. /// nk_layout_row_push(&ctx, 110);
  207. /// nk_slider_float(&ctx, 0, &value, 1.0f, 0.1f);
  208. /// }
  209. /// nk_layout_row_end(&ctx);
  210. /// }
  211. /// nk_end(&ctx);
  212. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  213. ///
  214. /// ![](https://cloud.githubusercontent.com/assets/8057201/10187981/584ecd68-675c-11e5-897c-822ef534a876.png)
  215. ///
  216. /// ## API
  217. ///
  218. */
  219. #ifndef NK_SINGLE_FILE
  220. #define NK_SINGLE_FILE
  221. #endif
  222. #ifndef NK_NUKLEAR_H_
  223. #define NK_NUKLEAR_H_
  224. #ifdef __cplusplus
  225. extern "C" {
  226. #endif
  227. /*
  228. * ==============================================================
  229. *
  230. * CONSTANTS
  231. *
  232. * ===============================================================
  233. */
  234. #define NK_UNDEFINED (-1.0f)
  235. #define NK_UTF_INVALID 0xFFFD /* internal invalid utf8 rune */
  236. #define NK_UTF_SIZE 4 /* describes the number of bytes a glyph consists of*/
  237. #ifndef NK_INPUT_MAX
  238. #define NK_INPUT_MAX 16
  239. #endif
  240. #ifndef NK_MAX_NUMBER_BUFFER
  241. #define NK_MAX_NUMBER_BUFFER 64
  242. #endif
  243. #ifndef NK_SCROLLBAR_HIDING_TIMEOUT
  244. #define NK_SCROLLBAR_HIDING_TIMEOUT 4.0f
  245. #endif
  246. /*
  247. * ==============================================================
  248. *
  249. * HELPER
  250. *
  251. * ===============================================================
  252. */
  253. #ifndef NK_API
  254. #ifdef NK_PRIVATE
  255. #if (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199409L))
  256. #define NK_API static inline
  257. #elif defined(__cplusplus)
  258. #define NK_API static inline
  259. #else
  260. #define NK_API static
  261. #endif
  262. #else
  263. #define NK_API extern
  264. #endif
  265. #endif
  266. #ifndef NK_LIB
  267. #ifdef NK_SINGLE_FILE
  268. #define NK_LIB static
  269. #else
  270. #define NK_LIB extern
  271. #endif
  272. #endif
  273. #define NK_INTERN static
  274. #define NK_STORAGE static
  275. #define NK_GLOBAL static
  276. #define NK_FLAG(x) (1 << (x))
  277. #define NK_STRINGIFY(x) #x
  278. #define NK_MACRO_STRINGIFY(x) NK_STRINGIFY(x)
  279. #define NK_STRING_JOIN_IMMEDIATE(arg1, arg2) arg1 ## arg2
  280. #define NK_STRING_JOIN_DELAY(arg1, arg2) NK_STRING_JOIN_IMMEDIATE(arg1, arg2)
  281. #define NK_STRING_JOIN(arg1, arg2) NK_STRING_JOIN_DELAY(arg1, arg2)
  282. #ifdef _MSC_VER
  283. #define NK_UNIQUE_NAME(name) NK_STRING_JOIN(name,__COUNTER__)
  284. #else
  285. #define NK_UNIQUE_NAME(name) NK_STRING_JOIN(name,__LINE__)
  286. #endif
  287. #ifndef NK_STATIC_ASSERT
  288. #define NK_STATIC_ASSERT(exp) typedef char NK_UNIQUE_NAME(_dummy_array)[(exp)?1:-1]
  289. #endif
  290. #ifndef NK_FILE_LINE
  291. #ifdef _MSC_VER
  292. #define NK_FILE_LINE __FILE__ ":" NK_MACRO_STRINGIFY(__COUNTER__)
  293. #else
  294. #define NK_FILE_LINE __FILE__ ":" NK_MACRO_STRINGIFY(__LINE__)
  295. #endif
  296. #endif
  297. #define NK_MIN(a,b) ((a) < (b) ? (a) : (b))
  298. #define NK_MAX(a,b) ((a) < (b) ? (b) : (a))
  299. #define NK_CLAMP(i,v,x) (NK_MAX(NK_MIN(v,x), i))
  300. #ifdef NK_INCLUDE_STANDARD_VARARGS
  301. #include <stdarg.h>
  302. #if defined(_MSC_VER) && (_MSC_VER >= 1600) /* VS 2010 and above */
  303. #include <sal.h>
  304. #define NK_PRINTF_FORMAT_STRING _Printf_format_string_
  305. #else
  306. #define NK_PRINTF_FORMAT_STRING
  307. #endif
  308. #if defined(__GNUC__)
  309. #define NK_PRINTF_VARARG_FUNC(fmtargnumber) __attribute__((format(__printf__, fmtargnumber, fmtargnumber+1)))
  310. #define NK_PRINTF_VALIST_FUNC(fmtargnumber) __attribute__((format(__printf__, fmtargnumber, 0)))
  311. #else
  312. #define NK_PRINTF_VARARG_FUNC(fmtargnumber)
  313. #define NK_PRINTF_VALIST_FUNC(fmtargnumber)
  314. #endif
  315. #endif
  316. /*
  317. * ===============================================================
  318. *
  319. * BASIC
  320. *
  321. * ===============================================================
  322. */
  323. #ifdef NK_INCLUDE_FIXED_TYPES
  324. #include <stdint.h>
  325. #define NK_INT8 int8_t
  326. #define NK_UINT8 uint8_t
  327. #define NK_INT16 int16_t
  328. #define NK_UINT16 uint16_t
  329. #define NK_INT32 int32_t
  330. #define NK_UINT32 uint32_t
  331. #define NK_SIZE_TYPE uintptr_t
  332. #define NK_POINTER_TYPE uintptr_t
  333. #else
  334. #ifndef NK_INT8
  335. #define NK_INT8 signed char
  336. #endif
  337. #ifndef NK_UINT8
  338. #define NK_UINT8 unsigned char
  339. #endif
  340. #ifndef NK_INT16
  341. #define NK_INT16 signed short
  342. #endif
  343. #ifndef NK_UINT16
  344. #define NK_UINT16 unsigned short
  345. #endif
  346. #ifndef NK_INT32
  347. #if defined(_MSC_VER)
  348. #define NK_INT32 __int32
  349. #else
  350. #define NK_INT32 signed int
  351. #endif
  352. #endif
  353. #ifndef NK_UINT32
  354. #if defined(_MSC_VER)
  355. #define NK_UINT32 unsigned __int32
  356. #else
  357. #define NK_UINT32 unsigned int
  358. #endif
  359. #endif
  360. #ifndef NK_SIZE_TYPE
  361. #if defined(_WIN64) && defined(_MSC_VER)
  362. #define NK_SIZE_TYPE unsigned __int64
  363. #elif (defined(_WIN32) || defined(WIN32)) && defined(_MSC_VER)
  364. #define NK_SIZE_TYPE unsigned __int32
  365. #elif defined(__GNUC__) || defined(__clang__)
  366. #if defined(__x86_64__) || defined(__ppc64__) || defined(__PPC64__) || defined(__aarch64__)
  367. #define NK_SIZE_TYPE unsigned long
  368. #else
  369. #define NK_SIZE_TYPE unsigned int
  370. #endif
  371. #else
  372. #define NK_SIZE_TYPE unsigned long
  373. #endif
  374. #endif
  375. #ifndef NK_POINTER_TYPE
  376. #if defined(_WIN64) && defined(_MSC_VER)
  377. #define NK_POINTER_TYPE unsigned __int64
  378. #elif (defined(_WIN32) || defined(WIN32)) && defined(_MSC_VER)
  379. #define NK_POINTER_TYPE unsigned __int32
  380. #elif defined(__GNUC__) || defined(__clang__)
  381. #if defined(__x86_64__) || defined(__ppc64__) || defined(__PPC64__) || defined(__aarch64__)
  382. #define NK_POINTER_TYPE unsigned long
  383. #else
  384. #define NK_POINTER_TYPE unsigned int
  385. #endif
  386. #else
  387. #define NK_POINTER_TYPE unsigned long
  388. #endif
  389. #endif
  390. #endif
  391. #ifndef NK_BOOL
  392. #ifdef NK_INCLUDE_STANDARD_BOOL
  393. #include <stdbool.h>
  394. #define NK_BOOL bool
  395. #else
  396. #define NK_BOOL int /* could be char, use int for drop-in replacement backwards compatibility */
  397. #endif
  398. #endif
  399. typedef NK_INT8 nk_char;
  400. typedef NK_UINT8 nk_uchar;
  401. typedef NK_UINT8 nk_byte;
  402. typedef NK_INT16 nk_short;
  403. typedef NK_UINT16 nk_ushort;
  404. typedef NK_INT32 nk_int;
  405. typedef NK_UINT32 nk_uint;
  406. typedef NK_SIZE_TYPE nk_size;
  407. typedef NK_POINTER_TYPE nk_ptr;
  408. typedef NK_BOOL nk_bool;
  409. typedef nk_uint nk_hash;
  410. typedef nk_uint nk_flags;
  411. typedef nk_uint nk_rune;
  412. /* Make sure correct type size:
  413. * This will fire with a negative subscript error if the type sizes
  414. * are set incorrectly by the compiler, and compile out if not */
  415. NK_STATIC_ASSERT(sizeof(nk_short) == 2);
  416. NK_STATIC_ASSERT(sizeof(nk_ushort) == 2);
  417. NK_STATIC_ASSERT(sizeof(nk_uint) == 4);
  418. NK_STATIC_ASSERT(sizeof(nk_int) == 4);
  419. NK_STATIC_ASSERT(sizeof(nk_byte) == 1);
  420. NK_STATIC_ASSERT(sizeof(nk_flags) >= 4);
  421. NK_STATIC_ASSERT(sizeof(nk_rune) >= 4);
  422. NK_STATIC_ASSERT(sizeof(nk_size) >= sizeof(void*));
  423. NK_STATIC_ASSERT(sizeof(nk_ptr) >= sizeof(void*));
  424. #ifdef NK_INCLUDE_STANDARD_BOOL
  425. NK_STATIC_ASSERT(sizeof(nk_bool) == sizeof(bool));
  426. #else
  427. NK_STATIC_ASSERT(sizeof(nk_bool) >= 2);
  428. #endif
  429. /* ============================================================================
  430. *
  431. * API
  432. *
  433. * =========================================================================== */
  434. struct nk_buffer;
  435. struct nk_allocator;
  436. struct nk_command_buffer;
  437. struct nk_draw_command;
  438. struct nk_convert_config;
  439. struct nk_style_item;
  440. struct nk_text_edit;
  441. struct nk_draw_list;
  442. struct nk_user_font;
  443. struct nk_panel;
  444. struct nk_context;
  445. struct nk_draw_vertex_layout_element;
  446. struct nk_style_button;
  447. struct nk_style_toggle;
  448. struct nk_style_selectable;
  449. struct nk_style_slide;
  450. struct nk_style_progress;
  451. struct nk_style_scrollbar;
  452. struct nk_style_edit;
  453. struct nk_style_property;
  454. struct nk_style_chart;
  455. struct nk_style_combo;
  456. struct nk_style_tab;
  457. struct nk_style_window_header;
  458. struct nk_style_window;
  459. enum { nk_false, nk_true };
  460. struct nk_color { nk_byte r, g, b, a; };
  461. struct nk_colorf { float r, g, b, a; };
  462. struct nk_vec2 { float x, y; };
  463. struct nk_vec2i { short x, y; };
  464. struct nk_rect { float x, y, w, h; };
  465. struct nk_recti { short x, y, w, h; };
  466. typedef char nk_glyph[NK_UTF_SIZE];
  467. typedef union { void* ptr; int id; } nk_handle;
  468. struct nk_image { nk_handle handle; nk_ushort w, h; nk_ushort region[4]; };
  469. struct nk_nine_slice { struct nk_image img; nk_ushort l, t, r, b; };
  470. struct nk_cursor { struct nk_image img; struct nk_vec2 size, offset; };
  471. struct nk_scroll { nk_uint x, y; };
  472. enum nk_heading { NK_UP, NK_RIGHT, NK_DOWN, NK_LEFT };
  473. enum nk_button_behavior { NK_BUTTON_DEFAULT, NK_BUTTON_REPEATER };
  474. enum nk_modify { NK_FIXED = nk_false, NK_MODIFIABLE = nk_true };
  475. enum nk_orientation { NK_VERTICAL, NK_HORIZONTAL };
  476. enum nk_collapse_states { NK_MINIMIZED = nk_false, NK_MAXIMIZED = nk_true };
  477. enum nk_show_states { NK_HIDDEN = nk_false, NK_SHOWN = nk_true };
  478. enum nk_chart_type { NK_CHART_LINES, NK_CHART_COLUMN, NK_CHART_MAX };
  479. enum nk_chart_event { NK_CHART_HOVERING = 0x01, NK_CHART_CLICKED = 0x02 };
  480. enum nk_color_format { NK_RGB, NK_RGBA };
  481. enum nk_popup_type { NK_POPUP_STATIC, NK_POPUP_DYNAMIC };
  482. enum nk_layout_format { NK_DYNAMIC, NK_STATIC };
  483. enum nk_tree_type { NK_TREE_NODE, NK_TREE_TAB };
  484. typedef void* (*nk_plugin_alloc)(nk_handle, void* old, nk_size);
  485. typedef void (*nk_plugin_free)(nk_handle, void* old);
  486. typedef nk_bool(*nk_plugin_filter)(const struct nk_text_edit*, nk_rune unicode);
  487. typedef void(*nk_plugin_paste)(nk_handle, struct nk_text_edit*);
  488. typedef void(*nk_plugin_copy)(nk_handle, const char*, int len);
  489. struct nk_allocator {
  490. nk_handle userdata;
  491. nk_plugin_alloc alloc;
  492. nk_plugin_free free;
  493. };
  494. enum nk_symbol_type {
  495. NK_SYMBOL_NONE,
  496. NK_SYMBOL_X,
  497. NK_SYMBOL_UNDERSCORE,
  498. NK_SYMBOL_CIRCLE_SOLID,
  499. NK_SYMBOL_CIRCLE_OUTLINE,
  500. NK_SYMBOL_RECT_SOLID,
  501. NK_SYMBOL_RECT_OUTLINE,
  502. NK_SYMBOL_TRIANGLE_UP,
  503. NK_SYMBOL_TRIANGLE_DOWN,
  504. NK_SYMBOL_TRIANGLE_LEFT,
  505. NK_SYMBOL_TRIANGLE_RIGHT,
  506. NK_SYMBOL_PLUS,
  507. NK_SYMBOL_MINUS,
  508. NK_SYMBOL_MAX
  509. };
  510. /* =============================================================================
  511. *
  512. * CONTEXT
  513. *
  514. * =============================================================================*/
  515. /*/// ### Context
  516. /// Contexts are the main entry point and the majestro of nuklear and contain all required state.
  517. /// They are used for window, memory, input, style, stack, commands and time management and need
  518. /// to be passed into all nuklear GUI specific functions.
  519. ///
  520. /// #### Usage
  521. /// To use a context it first has to be initialized which can be achieved by calling
  522. /// one of either `nk_init_default`, `nk_init_fixed`, `nk_init`, `nk_init_custom`.
  523. /// Each takes in a font handle and a specific way of handling memory. Memory control
  524. /// hereby ranges from standard library to just specifying a fixed sized block of memory
  525. /// which nuklear has to manage itself from.
  526. ///
  527. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  528. /// struct nk_context ctx;
  529. /// nk_init_xxx(&ctx, ...);
  530. /// while (1) {
  531. /// // [...]
  532. /// nk_clear(&ctx);
  533. /// }
  534. /// nk_free(&ctx);
  535. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  536. ///
  537. /// #### Reference
  538. /// Function | Description
  539. /// --------------------|-------------------------------------------------------
  540. /// __nk_init_default__ | Initializes context with standard library memory allocation (malloc,free)
  541. /// __nk_init_fixed__ | Initializes context from single fixed size memory block
  542. /// __nk_init__ | Initializes context with memory allocator callbacks for alloc and free
  543. /// __nk_init_custom__ | Initializes context from two buffers. One for draw commands the other for window/panel/table allocations
  544. /// __nk_clear__ | Called at the end of the frame to reset and prepare the context for the next frame
  545. /// __nk_free__ | Shutdown and free all memory allocated inside the context
  546. /// __nk_set_user_data__| Utility function to pass user data to draw command
  547. */
  548. #ifdef NK_INCLUDE_DEFAULT_ALLOCATOR
  549. /*/// #### nk_init_default
  550. /// Initializes a `nk_context` struct with a default standard library allocator.
  551. /// Should be used if you don't want to be bothered with memory management in nuklear.
  552. ///
  553. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  554. /// nk_bool nk_init_default(struct nk_context *ctx, const struct nk_user_font *font);
  555. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  556. ///
  557. /// Parameter | Description
  558. /// ------------|---------------------------------------------------------------
  559. /// __ctx__ | Must point to an either stack or heap allocated `nk_context` struct
  560. /// __font__ | Must point to a previously initialized font handle for more info look at font documentation
  561. ///
  562. /// Returns either `false(0)` on failure or `true(1)` on success.
  563. ///
  564. */
  565. NK_API nk_bool nk_init_default(struct nk_context*, const struct nk_user_font*);
  566. #endif
  567. /*/// #### nk_init_fixed
  568. /// Initializes a `nk_context` struct from single fixed size memory block
  569. /// Should be used if you want complete control over nuklear's memory management.
  570. /// Especially recommended for system with little memory or systems with virtual memory.
  571. /// For the later case you can just allocate for example 16MB of virtual memory
  572. /// and only the required amount of memory will actually be committed.
  573. ///
  574. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  575. /// nk_bool nk_init_fixed(struct nk_context *ctx, void *memory, nk_size size, const struct nk_user_font *font);
  576. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  577. ///
  578. /// !!! Warning
  579. /// make sure the passed memory block is aligned correctly for `nk_draw_commands`.
  580. ///
  581. /// Parameter | Description
  582. /// ------------|--------------------------------------------------------------
  583. /// __ctx__ | Must point to an either stack or heap allocated `nk_context` struct
  584. /// __memory__ | Must point to a previously allocated memory block
  585. /// __size__ | Must contain the total size of __memory__
  586. /// __font__ | Must point to a previously initialized font handle for more info look at font documentation
  587. ///
  588. /// Returns either `false(0)` on failure or `true(1)` on success.
  589. */
  590. NK_API nk_bool nk_init_fixed(struct nk_context*, void* memory, nk_size size, const struct nk_user_font*);
  591. /*/// #### nk_init
  592. /// Initializes a `nk_context` struct with memory allocation callbacks for nuklear to allocate
  593. /// memory from. Used internally for `nk_init_default` and provides a kitchen sink allocation
  594. /// interface to nuklear. Can be useful for cases like monitoring memory consumption.
  595. ///
  596. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  597. /// nk_bool nk_init(struct nk_context *ctx, struct nk_allocator *alloc, const struct nk_user_font *font);
  598. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  599. ///
  600. /// Parameter | Description
  601. /// ------------|---------------------------------------------------------------
  602. /// __ctx__ | Must point to an either stack or heap allocated `nk_context` struct
  603. /// __alloc__ | Must point to a previously allocated memory allocator
  604. /// __font__ | Must point to a previously initialized font handle for more info look at font documentation
  605. ///
  606. /// Returns either `false(0)` on failure or `true(1)` on success.
  607. */
  608. NK_API nk_bool nk_init(struct nk_context*, struct nk_allocator*, const struct nk_user_font*);
  609. /*/// #### nk_init_custom
  610. /// Initializes a `nk_context` struct from two different either fixed or growing
  611. /// buffers. The first buffer is for allocating draw commands while the second buffer is
  612. /// used for allocating windows, panels and state tables.
  613. ///
  614. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  615. /// nk_bool nk_init_custom(struct nk_context *ctx, struct nk_buffer *cmds, struct nk_buffer *pool, const struct nk_user_font *font);
  616. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  617. ///
  618. /// Parameter | Description
  619. /// ------------|---------------------------------------------------------------
  620. /// __ctx__ | Must point to an either stack or heap allocated `nk_context` struct
  621. /// __cmds__ | Must point to a previously initialized memory buffer either fixed or dynamic to store draw commands into
  622. /// __pool__ | Must point to a previously initialized memory buffer either fixed or dynamic to store windows, panels and tables
  623. /// __font__ | Must point to a previously initialized font handle for more info look at font documentation
  624. ///
  625. /// Returns either `false(0)` on failure or `true(1)` on success.
  626. */
  627. NK_API nk_bool nk_init_custom(struct nk_context*, struct nk_buffer* cmds, struct nk_buffer* pool, const struct nk_user_font*);
  628. /*/// #### nk_clear
  629. /// Resets the context state at the end of the frame. This includes mostly
  630. /// garbage collector tasks like removing windows or table not called and therefore
  631. /// used anymore.
  632. ///
  633. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  634. /// void nk_clear(struct nk_context *ctx);
  635. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  636. ///
  637. /// Parameter | Description
  638. /// ------------|-----------------------------------------------------------
  639. /// __ctx__ | Must point to a previously initialized `nk_context` struct
  640. */
  641. NK_API void nk_clear(struct nk_context*);
  642. /*/// #### nk_free
  643. /// Frees all memory allocated by nuklear. Not needed if context was
  644. /// initialized with `nk_init_fixed`.
  645. ///
  646. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  647. /// void nk_free(struct nk_context *ctx);
  648. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  649. ///
  650. /// Parameter | Description
  651. /// ------------|-----------------------------------------------------------
  652. /// __ctx__ | Must point to a previously initialized `nk_context` struct
  653. */
  654. NK_API void nk_free(struct nk_context*);
  655. #ifdef NK_INCLUDE_COMMAND_USERDATA
  656. /*/// #### nk_set_user_data
  657. /// Sets the currently passed userdata passed down into each draw command.
  658. ///
  659. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  660. /// void nk_set_user_data(struct nk_context *ctx, nk_handle data);
  661. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  662. ///
  663. /// Parameter | Description
  664. /// ------------|--------------------------------------------------------------
  665. /// __ctx__ | Must point to a previously initialized `nk_context` struct
  666. /// __data__ | Handle with either pointer or index to be passed into every draw commands
  667. */
  668. NK_API void nk_set_user_data(struct nk_context*, nk_handle handle);
  669. #endif
  670. /* =============================================================================
  671. *
  672. * INPUT
  673. *
  674. * =============================================================================*/
  675. /*/// ### Input
  676. /// The input API is responsible for holding the current input state composed of
  677. /// mouse, key and text input states.
  678. /// It is worth noting that no direct OS or window handling is done in nuklear.
  679. /// Instead all input state has to be provided by platform specific code. This on one hand
  680. /// expects more work from the user and complicates usage but on the other hand
  681. /// provides simple abstraction over a big number of platforms, libraries and other
  682. /// already provided functionality.
  683. ///
  684. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  685. /// nk_input_begin(&ctx);
  686. /// while (GetEvent(&evt)) {
  687. /// if (evt.type == MOUSE_MOVE)
  688. /// nk_input_motion(&ctx, evt.motion.x, evt.motion.y);
  689. /// else if (evt.type == [...]) {
  690. /// // [...]
  691. /// }
  692. /// } nk_input_end(&ctx);
  693. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  694. ///
  695. /// #### Usage
  696. /// Input state needs to be provided to nuklear by first calling `nk_input_begin`
  697. /// which resets internal state like delta mouse position and button transitions.
  698. /// After `nk_input_begin` all current input state needs to be provided. This includes
  699. /// mouse motion, button and key pressed and released, text input and scrolling.
  700. /// Both event- or state-based input handling are supported by this API
  701. /// and should work without problems. Finally after all input state has been
  702. /// mirrored `nk_input_end` needs to be called to finish input process.
  703. ///
  704. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  705. /// struct nk_context ctx;
  706. /// nk_init_xxx(&ctx, ...);
  707. /// while (1) {
  708. /// Event evt;
  709. /// nk_input_begin(&ctx);
  710. /// while (GetEvent(&evt)) {
  711. /// if (evt.type == MOUSE_MOVE)
  712. /// nk_input_motion(&ctx, evt.motion.x, evt.motion.y);
  713. /// else if (evt.type == [...]) {
  714. /// // [...]
  715. /// }
  716. /// }
  717. /// nk_input_end(&ctx);
  718. /// // [...]
  719. /// nk_clear(&ctx);
  720. /// } nk_free(&ctx);
  721. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  722. ///
  723. /// #### Reference
  724. /// Function | Description
  725. /// --------------------|-------------------------------------------------------
  726. /// __nk_input_begin__ | Begins the input mirroring process. Needs to be called before all other `nk_input_xxx` calls
  727. /// __nk_input_motion__ | Mirrors mouse cursor position
  728. /// __nk_input_key__ | Mirrors key state with either pressed or released
  729. /// __nk_input_button__ | Mirrors mouse button state with either pressed or released
  730. /// __nk_input_scroll__ | Mirrors mouse scroll values
  731. /// __nk_input_char__ | Adds a single ASCII text character into an internal text buffer
  732. /// __nk_input_glyph__ | Adds a single multi-byte UTF-8 character into an internal text buffer
  733. /// __nk_input_unicode__| Adds a single unicode rune into an internal text buffer
  734. /// __nk_input_end__ | Ends the input mirroring process by calculating state changes. Don't call any `nk_input_xxx` function referenced above after this call
  735. */
  736. enum nk_keys {
  737. NK_KEY_NONE,
  738. NK_KEY_SHIFT,
  739. NK_KEY_CTRL,
  740. NK_KEY_DEL,
  741. NK_KEY_ENTER,
  742. NK_KEY_TAB,
  743. NK_KEY_BACKSPACE,
  744. NK_KEY_COPY,
  745. NK_KEY_CUT,
  746. NK_KEY_PASTE,
  747. NK_KEY_UP,
  748. NK_KEY_DOWN,
  749. NK_KEY_LEFT,
  750. NK_KEY_RIGHT,
  751. /* Shortcuts: text field */
  752. NK_KEY_TEXT_INSERT_MODE,
  753. NK_KEY_TEXT_REPLACE_MODE,
  754. NK_KEY_TEXT_RESET_MODE,
  755. NK_KEY_TEXT_LINE_START,
  756. NK_KEY_TEXT_LINE_END,
  757. NK_KEY_TEXT_START,
  758. NK_KEY_TEXT_END,
  759. NK_KEY_TEXT_UNDO,
  760. NK_KEY_TEXT_REDO,
  761. NK_KEY_TEXT_SELECT_ALL,
  762. NK_KEY_TEXT_WORD_LEFT,
  763. NK_KEY_TEXT_WORD_RIGHT,
  764. /* Shortcuts: scrollbar */
  765. NK_KEY_SCROLL_START,
  766. NK_KEY_SCROLL_END,
  767. NK_KEY_SCROLL_DOWN,
  768. NK_KEY_SCROLL_UP,
  769. NK_KEY_MAX
  770. };
  771. enum nk_buttons {
  772. NK_BUTTON_LEFT,
  773. NK_BUTTON_MIDDLE,
  774. NK_BUTTON_RIGHT,
  775. NK_BUTTON_DOUBLE,
  776. NK_BUTTON_MAX
  777. };
  778. /*/// #### nk_input_begin
  779. /// Begins the input mirroring process by resetting text, scroll
  780. /// mouse, previous mouse position and movement as well as key state transitions,
  781. ///
  782. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  783. /// void nk_input_begin(struct nk_context*);
  784. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  785. ///
  786. /// Parameter | Description
  787. /// ------------|-----------------------------------------------------------
  788. /// __ctx__ | Must point to a previously initialized `nk_context` struct
  789. */
  790. NK_API void nk_input_begin(struct nk_context*);
  791. /*/// #### nk_input_motion
  792. /// Mirrors current mouse position to nuklear
  793. ///
  794. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  795. /// void nk_input_motion(struct nk_context *ctx, int x, int y);
  796. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  797. ///
  798. /// Parameter | Description
  799. /// ------------|-----------------------------------------------------------
  800. /// __ctx__ | Must point to a previously initialized `nk_context` struct
  801. /// __x__ | Must hold an integer describing the current mouse cursor x-position
  802. /// __y__ | Must hold an integer describing the current mouse cursor y-position
  803. */
  804. NK_API void nk_input_motion(struct nk_context*, int x, int y);
  805. /*/// #### nk_input_key
  806. /// Mirrors the state of a specific key to nuklear
  807. ///
  808. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  809. /// void nk_input_key(struct nk_context*, enum nk_keys key, nk_bool down);
  810. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  811. ///
  812. /// Parameter | Description
  813. /// ------------|-----------------------------------------------------------
  814. /// __ctx__ | Must point to a previously initialized `nk_context` struct
  815. /// __key__ | Must be any value specified in enum `nk_keys` that needs to be mirrored
  816. /// __down__ | Must be 0 for key is up and 1 for key is down
  817. */
  818. NK_API void nk_input_key(struct nk_context*, enum nk_keys, nk_bool down);
  819. /*/// #### nk_input_button
  820. /// Mirrors the state of a specific mouse button to nuklear
  821. ///
  822. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  823. /// void nk_input_button(struct nk_context *ctx, enum nk_buttons btn, int x, int y, nk_bool down);
  824. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  825. ///
  826. /// Parameter | Description
  827. /// ------------|-----------------------------------------------------------
  828. /// __ctx__ | Must point to a previously initialized `nk_context` struct
  829. /// __btn__ | Must be any value specified in enum `nk_buttons` that needs to be mirrored
  830. /// __x__ | Must contain an integer describing mouse cursor x-position on click up/down
  831. /// __y__ | Must contain an integer describing mouse cursor y-position on click up/down
  832. /// __down__ | Must be 0 for key is up and 1 for key is down
  833. */
  834. NK_API void nk_input_button(struct nk_context*, enum nk_buttons, int x, int y, nk_bool down);
  835. /*/// #### nk_input_scroll
  836. /// Copies the last mouse scroll value to nuklear. Is generally
  837. /// a scroll value. So does not have to come from mouse and could also originate
  838. /// TODO finish this sentence
  839. ///
  840. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  841. /// void nk_input_scroll(struct nk_context *ctx, struct nk_vec2 val);
  842. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  843. ///
  844. /// Parameter | Description
  845. /// ------------|-----------------------------------------------------------
  846. /// __ctx__ | Must point to a previously initialized `nk_context` struct
  847. /// __val__ | vector with both X- as well as Y-scroll value
  848. */
  849. NK_API void nk_input_scroll(struct nk_context*, struct nk_vec2 val);
  850. /*/// #### nk_input_char
  851. /// Copies a single ASCII character into an internal text buffer
  852. /// This is basically a helper function to quickly push ASCII characters into
  853. /// nuklear.
  854. ///
  855. /// !!! Note
  856. /// Stores up to NK_INPUT_MAX bytes between `nk_input_begin` and `nk_input_end`.
  857. ///
  858. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  859. /// void nk_input_char(struct nk_context *ctx, char c);
  860. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  861. ///
  862. /// Parameter | Description
  863. /// ------------|-----------------------------------------------------------
  864. /// __ctx__ | Must point to a previously initialized `nk_context` struct
  865. /// __c__ | Must be a single ASCII character preferable one that can be printed
  866. */
  867. NK_API void nk_input_char(struct nk_context*, char);
  868. /*/// #### nk_input_glyph
  869. /// Converts an encoded unicode rune into UTF-8 and copies the result into an
  870. /// internal text buffer.
  871. ///
  872. /// !!! Note
  873. /// Stores up to NK_INPUT_MAX bytes between `nk_input_begin` and `nk_input_end`.
  874. ///
  875. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  876. /// void nk_input_glyph(struct nk_context *ctx, const nk_glyph g);
  877. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  878. ///
  879. /// Parameter | Description
  880. /// ------------|-----------------------------------------------------------
  881. /// __ctx__ | Must point to a previously initialized `nk_context` struct
  882. /// __g__ | UTF-32 unicode codepoint
  883. */
  884. NK_API void nk_input_glyph(struct nk_context*, const nk_glyph);
  885. /*/// #### nk_input_unicode
  886. /// Converts a unicode rune into UTF-8 and copies the result
  887. /// into an internal text buffer.
  888. /// !!! Note
  889. /// Stores up to NK_INPUT_MAX bytes between `nk_input_begin` and `nk_input_end`.
  890. ///
  891. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  892. /// void nk_input_unicode(struct nk_context*, nk_rune rune);
  893. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  894. ///
  895. /// Parameter | Description
  896. /// ------------|-----------------------------------------------------------
  897. /// __ctx__ | Must point to a previously initialized `nk_context` struct
  898. /// __rune__ | UTF-32 unicode codepoint
  899. */
  900. NK_API void nk_input_unicode(struct nk_context*, nk_rune);
  901. /*/// #### nk_input_end
  902. /// End the input mirroring process by resetting mouse grabbing
  903. /// state to ensure the mouse cursor is not grabbed indefinitely.
  904. ///
  905. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  906. /// void nk_input_end(struct nk_context *ctx);
  907. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  908. ///
  909. /// Parameter | Description
  910. /// ------------|-----------------------------------------------------------
  911. /// __ctx__ | Must point to a previously initialized `nk_context` struct
  912. */
  913. NK_API void nk_input_end(struct nk_context*);
  914. /* =============================================================================
  915. *
  916. * DRAWING
  917. *
  918. * =============================================================================*/
  919. /*/// ### Drawing
  920. /// This library was designed to be render backend agnostic so it does
  921. /// not draw anything to screen directly. Instead all drawn shapes, widgets
  922. /// are made of, are buffered into memory and make up a command queue.
  923. /// Each frame therefore fills the command buffer with draw commands
  924. /// that then need to be executed by the user and his own render backend.
  925. /// After that the command buffer needs to be cleared and a new frame can be
  926. /// started. It is probably important to note that the command buffer is the main
  927. /// drawing API and the optional vertex buffer API only takes this format and
  928. /// converts it into a hardware accessible format.
  929. ///
  930. /// #### Usage
  931. /// To draw all draw commands accumulated over a frame you need your own render
  932. /// backend able to draw a number of 2D primitives. This includes at least
  933. /// filled and stroked rectangles, circles, text, lines, triangles and scissors.
  934. /// As soon as this criterion is met you can iterate over each draw command
  935. /// and execute each draw command in a interpreter like fashion:
  936. ///
  937. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  938. /// const struct nk_command *cmd = 0;
  939. /// nk_foreach(cmd, &ctx) {
  940. /// switch (cmd->type) {
  941. /// case NK_COMMAND_LINE:
  942. /// your_draw_line_function(...)
  943. /// break;
  944. /// case NK_COMMAND_RECT
  945. /// your_draw_rect_function(...)
  946. /// break;
  947. /// case //...:
  948. /// //[...]
  949. /// }
  950. /// }
  951. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  952. ///
  953. /// In program flow context draw commands need to be executed after input has been
  954. /// gathered and the complete UI with windows and their contained widgets have
  955. /// been executed and before calling `nk_clear` which frees all previously
  956. /// allocated draw commands.
  957. ///
  958. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  959. /// struct nk_context ctx;
  960. /// nk_init_xxx(&ctx, ...);
  961. /// while (1) {
  962. /// Event evt;
  963. /// nk_input_begin(&ctx);
  964. /// while (GetEvent(&evt)) {
  965. /// if (evt.type == MOUSE_MOVE)
  966. /// nk_input_motion(&ctx, evt.motion.x, evt.motion.y);
  967. /// else if (evt.type == [...]) {
  968. /// [...]
  969. /// }
  970. /// }
  971. /// nk_input_end(&ctx);
  972. /// //
  973. /// // [...]
  974. /// //
  975. /// const struct nk_command *cmd = 0;
  976. /// nk_foreach(cmd, &ctx) {
  977. /// switch (cmd->type) {
  978. /// case NK_COMMAND_LINE:
  979. /// your_draw_line_function(...)
  980. /// break;
  981. /// case NK_COMMAND_RECT
  982. /// your_draw_rect_function(...)
  983. /// break;
  984. /// case ...:
  985. /// // [...]
  986. /// }
  987. /// nk_clear(&ctx);
  988. /// }
  989. /// nk_free(&ctx);
  990. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  991. ///
  992. /// You probably noticed that you have to draw all of the UI each frame which is
  993. /// quite wasteful. While the actual UI updating loop is quite fast rendering
  994. /// without actually needing it is not. So there are multiple things you could do.
  995. ///
  996. /// First is only update on input. This of course is only an option if your
  997. /// application only depends on the UI and does not require any outside calculations.
  998. /// If you actually only update on input make sure to update the UI two times each
  999. /// frame and call `nk_clear` directly after the first pass and only draw in
  1000. /// the second pass. In addition it is recommended to also add additional timers
  1001. /// to make sure the UI is not drawn more than a fixed number of frames per second.
  1002. ///
  1003. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1004. /// struct nk_context ctx;
  1005. /// nk_init_xxx(&ctx, ...);
  1006. /// while (1) {
  1007. /// // [...wait for input ]
  1008. /// // [...do two UI passes ...]
  1009. /// do_ui(...)
  1010. /// nk_clear(&ctx);
  1011. /// do_ui(...)
  1012. /// //
  1013. /// // draw
  1014. /// const struct nk_command *cmd = 0;
  1015. /// nk_foreach(cmd, &ctx) {
  1016. /// switch (cmd->type) {
  1017. /// case NK_COMMAND_LINE:
  1018. /// your_draw_line_function(...)
  1019. /// break;
  1020. /// case NK_COMMAND_RECT
  1021. /// your_draw_rect_function(...)
  1022. /// break;
  1023. /// case ...:
  1024. /// //[...]
  1025. /// }
  1026. /// nk_clear(&ctx);
  1027. /// }
  1028. /// nk_free(&ctx);
  1029. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1030. ///
  1031. /// The second probably more applicable trick is to only draw if anything changed.
  1032. /// It is not really useful for applications with continuous draw loop but
  1033. /// quite useful for desktop applications. To actually get nuklear to only
  1034. /// draw on changes you first have to define `NK_ZERO_COMMAND_MEMORY` and
  1035. /// allocate a memory buffer that will store each unique drawing output.
  1036. /// After each frame you compare the draw command memory inside the library
  1037. /// with your allocated buffer by memcmp. If memcmp detects differences
  1038. /// you have to copy the command buffer into the allocated buffer
  1039. /// and then draw like usual (this example uses fixed memory but you could
  1040. /// use dynamically allocated memory).
  1041. ///
  1042. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1043. /// //[... other defines ...]
  1044. /// #define NK_ZERO_COMMAND_MEMORY
  1045. /// #include "nuklear.h"
  1046. /// //
  1047. /// // setup context
  1048. /// struct nk_context ctx;
  1049. /// void *last = calloc(1,64*1024);
  1050. /// void *buf = calloc(1,64*1024);
  1051. /// nk_init_fixed(&ctx, buf, 64*1024);
  1052. /// //
  1053. /// // loop
  1054. /// while (1) {
  1055. /// // [...input...]
  1056. /// // [...ui...]
  1057. /// void *cmds = nk_buffer_memory(&ctx.memory);
  1058. /// if (memcmp(cmds, last, ctx.memory.allocated)) {
  1059. /// memcpy(last,cmds,ctx.memory.allocated);
  1060. /// const struct nk_command *cmd = 0;
  1061. /// nk_foreach(cmd, &ctx) {
  1062. /// switch (cmd->type) {
  1063. /// case NK_COMMAND_LINE:
  1064. /// your_draw_line_function(...)
  1065. /// break;
  1066. /// case NK_COMMAND_RECT
  1067. /// your_draw_rect_function(...)
  1068. /// break;
  1069. /// case ...:
  1070. /// // [...]
  1071. /// }
  1072. /// }
  1073. /// }
  1074. /// nk_clear(&ctx);
  1075. /// }
  1076. /// nk_free(&ctx);
  1077. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1078. ///
  1079. /// Finally while using draw commands makes sense for higher abstracted platforms like
  1080. /// X11 and Win32 or drawing libraries it is often desirable to use graphics
  1081. /// hardware directly. Therefore it is possible to just define
  1082. /// `NK_INCLUDE_VERTEX_BUFFER_OUTPUT` which includes optional vertex output.
  1083. /// To access the vertex output you first have to convert all draw commands into
  1084. /// vertexes by calling `nk_convert` which takes in your preferred vertex format.
  1085. /// After successfully converting all draw commands just iterate over and execute all
  1086. /// vertex draw commands:
  1087. ///
  1088. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1089. /// // fill configuration
  1090. /// struct your_vertex
  1091. /// {
  1092. /// float pos[2]; // important to keep it to 2 floats
  1093. /// float uv[2];
  1094. /// unsigned char col[4];
  1095. /// };
  1096. /// struct nk_convert_config cfg = {};
  1097. /// static const struct nk_draw_vertex_layout_element vertex_layout[] = {
  1098. /// {NK_VERTEX_POSITION, NK_FORMAT_FLOAT, NK_OFFSETOF(struct your_vertex, pos)},
  1099. /// {NK_VERTEX_TEXCOORD, NK_FORMAT_FLOAT, NK_OFFSETOF(struct your_vertex, uv)},
  1100. /// {NK_VERTEX_COLOR, NK_FORMAT_R8G8B8A8, NK_OFFSETOF(struct your_vertex, col)},
  1101. /// {NK_VERTEX_LAYOUT_END}
  1102. /// };
  1103. /// cfg.shape_AA = NK_ANTI_ALIASING_ON;
  1104. /// cfg.line_AA = NK_ANTI_ALIASING_ON;
  1105. /// cfg.vertex_layout = vertex_layout;
  1106. /// cfg.vertex_size = sizeof(struct your_vertex);
  1107. /// cfg.vertex_alignment = NK_ALIGNOF(struct your_vertex);
  1108. /// cfg.circle_segment_count = 22;
  1109. /// cfg.curve_segment_count = 22;
  1110. /// cfg.arc_segment_count = 22;
  1111. /// cfg.global_alpha = 1.0f;
  1112. /// cfg.tex_null = dev->tex_null;
  1113. /// //
  1114. /// // setup buffers and convert
  1115. /// struct nk_buffer cmds, verts, idx;
  1116. /// nk_buffer_init_default(&cmds);
  1117. /// nk_buffer_init_default(&verts);
  1118. /// nk_buffer_init_default(&idx);
  1119. /// nk_convert(&ctx, &cmds, &verts, &idx, &cfg);
  1120. /// //
  1121. /// // draw
  1122. /// nk_draw_foreach(cmd, &ctx, &cmds) {
  1123. /// if (!cmd->elem_count) continue;
  1124. /// //[...]
  1125. /// }
  1126. /// nk_buffer_free(&cms);
  1127. /// nk_buffer_free(&verts);
  1128. /// nk_buffer_free(&idx);
  1129. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1130. ///
  1131. /// #### Reference
  1132. /// Function | Description
  1133. /// --------------------|-------------------------------------------------------
  1134. /// __nk__begin__ | Returns the first draw command in the context draw command list to be drawn
  1135. /// __nk__next__ | Increments the draw command iterator to the next command inside the context draw command list
  1136. /// __nk_foreach__ | Iterates over each draw command inside the context draw command list
  1137. /// __nk_convert__ | Converts from the abstract draw commands list into a hardware accessible vertex format
  1138. /// __nk_draw_begin__ | Returns the first vertex command in the context vertex draw list to be executed
  1139. /// __nk__draw_next__ | Increments the vertex command iterator to the next command inside the context vertex command list
  1140. /// __nk__draw_end__ | Returns the end of the vertex draw list
  1141. /// __nk_draw_foreach__ | Iterates over each vertex draw command inside the vertex draw list
  1142. */
  1143. enum nk_anti_aliasing { NK_ANTI_ALIASING_OFF, NK_ANTI_ALIASING_ON };
  1144. enum nk_convert_result {
  1145. NK_CONVERT_SUCCESS = 0,
  1146. NK_CONVERT_INVALID_PARAM = 1,
  1147. NK_CONVERT_COMMAND_BUFFER_FULL = NK_FLAG(1),
  1148. NK_CONVERT_VERTEX_BUFFER_FULL = NK_FLAG(2),
  1149. NK_CONVERT_ELEMENT_BUFFER_FULL = NK_FLAG(3)
  1150. };
  1151. struct nk_draw_null_texture {
  1152. nk_handle texture; /* texture handle to a texture with a white pixel */
  1153. struct nk_vec2 uv; /* coordinates to a white pixel in the texture */
  1154. };
  1155. struct nk_convert_config {
  1156. float global_alpha; /* global alpha value */
  1157. enum nk_anti_aliasing line_AA; /* line anti-aliasing flag can be turned off if you are tight on memory */
  1158. enum nk_anti_aliasing shape_AA; /* shape anti-aliasing flag can be turned off if you are tight on memory */
  1159. unsigned circle_segment_count; /* number of segments used for circles: default to 22 */
  1160. unsigned arc_segment_count; /* number of segments used for arcs: default to 22 */
  1161. unsigned curve_segment_count; /* number of segments used for curves: default to 22 */
  1162. struct nk_draw_null_texture tex_null; /* handle to texture with a white pixel for shape drawing */
  1163. const struct nk_draw_vertex_layout_element* vertex_layout; /* describes the vertex output format and packing */
  1164. nk_size vertex_size; /* sizeof one vertex for vertex packing */
  1165. nk_size vertex_alignment; /* vertex alignment: Can be obtained by NK_ALIGNOF */
  1166. };
  1167. /*/// #### nk__begin
  1168. /// Returns a draw command list iterator to iterate all draw
  1169. /// commands accumulated over one frame.
  1170. ///
  1171. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1172. /// const struct nk_command* nk__begin(struct nk_context*);
  1173. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1174. ///
  1175. /// Parameter | Description
  1176. /// ------------|-----------------------------------------------------------
  1177. /// __ctx__ | must point to an previously initialized `nk_context` struct at the end of a frame
  1178. ///
  1179. /// Returns draw command pointer pointing to the first command inside the draw command list
  1180. */
  1181. NK_API const struct nk_command* nk__begin(struct nk_context*);
  1182. /*/// #### nk__next
  1183. /// Returns draw command pointer pointing to the next command inside the draw command list
  1184. ///
  1185. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1186. /// const struct nk_command* nk__next(struct nk_context*, const struct nk_command*);
  1187. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1188. ///
  1189. /// Parameter | Description
  1190. /// ------------|-----------------------------------------------------------
  1191. /// __ctx__ | Must point to an previously initialized `nk_context` struct at the end of a frame
  1192. /// __cmd__ | Must point to an previously a draw command either returned by `nk__begin` or `nk__next`
  1193. ///
  1194. /// Returns draw command pointer pointing to the next command inside the draw command list
  1195. */
  1196. NK_API const struct nk_command* nk__next(struct nk_context*, const struct nk_command*);
  1197. /*/// #### nk_foreach
  1198. /// Iterates over each draw command inside the context draw command list
  1199. ///
  1200. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1201. /// #define nk_foreach(c, ctx)
  1202. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1203. ///
  1204. /// Parameter | Description
  1205. /// ------------|-----------------------------------------------------------
  1206. /// __ctx__ | Must point to an previously initialized `nk_context` struct at the end of a frame
  1207. /// __cmd__ | Command pointer initialized to NULL
  1208. ///
  1209. /// Iterates over each draw command inside the context draw command list
  1210. */
  1211. #define nk_foreach(c, ctx) for((c) = nk__begin(ctx); (c) != 0; (c) = nk__next(ctx,c))
  1212. #ifdef NK_INCLUDE_VERTEX_BUFFER_OUTPUT
  1213. /*/// #### nk_convert
  1214. /// Converts all internal draw commands into vertex draw commands and fills
  1215. /// three buffers with vertexes, vertex draw commands and vertex indices. The vertex format
  1216. /// as well as some other configuration values have to be configured by filling out a
  1217. /// `nk_convert_config` struct.
  1218. ///
  1219. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1220. /// nk_flags nk_convert(struct nk_context *ctx, struct nk_buffer *cmds,
  1221. /// struct nk_buffer *vertices, struct nk_buffer *elements, const struct nk_convert_config*);
  1222. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1223. ///
  1224. /// Parameter | Description
  1225. /// ------------|-----------------------------------------------------------
  1226. /// __ctx__ | Must point to an previously initialized `nk_context` struct at the end of a frame
  1227. /// __cmds__ | Must point to a previously initialized buffer to hold converted vertex draw commands
  1228. /// __vertices__| Must point to a previously initialized buffer to hold all produced vertices
  1229. /// __elements__| Must point to a previously initialized buffer to hold all produced vertex indices
  1230. /// __config__ | Must point to a filled out `nk_config` struct to configure the conversion process
  1231. ///
  1232. /// Returns one of enum nk_convert_result error codes
  1233. ///
  1234. /// Parameter | Description
  1235. /// --------------------------------|-----------------------------------------------------------
  1236. /// NK_CONVERT_SUCCESS | Signals a successful draw command to vertex buffer conversion
  1237. /// NK_CONVERT_INVALID_PARAM | An invalid argument was passed in the function call
  1238. /// NK_CONVERT_COMMAND_BUFFER_FULL | The provided buffer for storing draw commands is full or failed to allocate more memory
  1239. /// NK_CONVERT_VERTEX_BUFFER_FULL | The provided buffer for storing vertices is full or failed to allocate more memory
  1240. /// NK_CONVERT_ELEMENT_BUFFER_FULL | The provided buffer for storing indices is full or failed to allocate more memory
  1241. */
  1242. NK_API nk_flags nk_convert(struct nk_context*, struct nk_buffer* cmds, struct nk_buffer* vertices, struct nk_buffer* elements, const struct nk_convert_config*);
  1243. /*/// #### nk__draw_begin
  1244. /// Returns a draw vertex command buffer iterator to iterate over the vertex draw command buffer
  1245. ///
  1246. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1247. /// const struct nk_draw_command* nk__draw_begin(const struct nk_context*, const struct nk_buffer*);
  1248. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1249. ///
  1250. /// Parameter | Description
  1251. /// ------------|-----------------------------------------------------------
  1252. /// __ctx__ | Must point to an previously initialized `nk_context` struct at the end of a frame
  1253. /// __buf__ | Must point to an previously by `nk_convert` filled out vertex draw command buffer
  1254. ///
  1255. /// Returns vertex draw command pointer pointing to the first command inside the vertex draw command buffer
  1256. */
  1257. NK_API const struct nk_draw_command* nk__draw_begin(const struct nk_context*, const struct nk_buffer*);
  1258. /*/// #### nk__draw_end
  1259. /// Returns the vertex draw command at the end of the vertex draw command buffer
  1260. ///
  1261. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1262. /// const struct nk_draw_command* nk__draw_end(const struct nk_context *ctx, const struct nk_buffer *buf);
  1263. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1264. ///
  1265. /// Parameter | Description
  1266. /// ------------|-----------------------------------------------------------
  1267. /// __ctx__ | Must point to an previously initialized `nk_context` struct at the end of a frame
  1268. /// __buf__ | Must point to an previously by `nk_convert` filled out vertex draw command buffer
  1269. ///
  1270. /// Returns vertex draw command pointer pointing to the end of the last vertex draw command inside the vertex draw command buffer
  1271. */
  1272. NK_API const struct nk_draw_command* nk__draw_end(const struct nk_context*, const struct nk_buffer*);
  1273. /*/// #### nk__draw_next
  1274. /// Increments the vertex draw command buffer iterator
  1275. ///
  1276. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1277. /// const struct nk_draw_command* nk__draw_next(const struct nk_draw_command*, const struct nk_buffer*, const struct nk_context*);
  1278. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1279. ///
  1280. /// Parameter | Description
  1281. /// ------------|-----------------------------------------------------------
  1282. /// __cmd__ | Must point to an previously either by `nk__draw_begin` or `nk__draw_next` returned vertex draw command
  1283. /// __buf__ | Must point to an previously by `nk_convert` filled out vertex draw command buffer
  1284. /// __ctx__ | Must point to an previously initialized `nk_context` struct at the end of a frame
  1285. ///
  1286. /// Returns vertex draw command pointer pointing to the end of the last vertex draw command inside the vertex draw command buffer
  1287. */
  1288. NK_API const struct nk_draw_command* nk__draw_next(const struct nk_draw_command*, const struct nk_buffer*, const struct nk_context*);
  1289. /*/// #### nk_draw_foreach
  1290. /// Iterates over each vertex draw command inside a vertex draw command buffer
  1291. ///
  1292. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1293. /// #define nk_draw_foreach(cmd,ctx, b)
  1294. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1295. ///
  1296. /// Parameter | Description
  1297. /// ------------|-----------------------------------------------------------
  1298. /// __cmd__ | `nk_draw_command`iterator set to NULL
  1299. /// __buf__ | Must point to an previously by `nk_convert` filled out vertex draw command buffer
  1300. /// __ctx__ | Must point to an previously initialized `nk_context` struct at the end of a frame
  1301. */
  1302. #define nk_draw_foreach(cmd,ctx, b) for((cmd)=nk__draw_begin(ctx, b); (cmd)!=0; (cmd)=nk__draw_next(cmd, b, ctx))
  1303. #endif
  1304. /* =============================================================================
  1305. *
  1306. * WINDOW
  1307. *
  1308. * =============================================================================
  1309. /// ### Window
  1310. /// Windows are the main persistent state used inside nuklear and are life time
  1311. /// controlled by simply "retouching" (i.e. calling) each window each frame.
  1312. /// All widgets inside nuklear can only be added inside the function pair `nk_begin_xxx`
  1313. /// and `nk_end`. Calling any widgets outside these two functions will result in an
  1314. /// assert in debug or no state change in release mode.<br /><br />
  1315. ///
  1316. /// Each window holds frame persistent state like position, size, flags, state tables,
  1317. /// and some garbage collected internal persistent widget state. Each window
  1318. /// is linked into a window stack list which determines the drawing and overlapping
  1319. /// order. The topmost window thereby is the currently active window.<br /><br />
  1320. ///
  1321. /// To change window position inside the stack occurs either automatically by
  1322. /// user input by being clicked on or programmatically by calling `nk_window_focus`.
  1323. /// Windows by default are visible unless explicitly being defined with flag
  1324. /// `NK_WINDOW_HIDDEN`, the user clicked the close button on windows with flag
  1325. /// `NK_WINDOW_CLOSABLE` or if a window was explicitly hidden by calling
  1326. /// `nk_window_show`. To explicitly close and destroy a window call `nk_window_close`.<br /><br />
  1327. ///
  1328. /// #### Usage
  1329. /// To create and keep a window you have to call one of the two `nk_begin_xxx`
  1330. /// functions to start window declarations and `nk_end` at the end. Furthermore it
  1331. /// is recommended to check the return value of `nk_begin_xxx` and only process
  1332. /// widgets inside the window if the value is not 0. Either way you have to call
  1333. /// `nk_end` at the end of window declarations. Furthermore, do not attempt to
  1334. /// nest `nk_begin_xxx` calls which will hopefully result in an assert or if not
  1335. /// in a segmentation fault.
  1336. ///
  1337. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1338. /// if (nk_begin_xxx(...) {
  1339. /// // [... widgets ...]
  1340. /// }
  1341. /// nk_end(ctx);
  1342. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1343. ///
  1344. /// In the grand concept window and widget declarations need to occur after input
  1345. /// handling and before drawing to screen. Not doing so can result in higher
  1346. /// latency or at worst invalid behavior. Furthermore make sure that `nk_clear`
  1347. /// is called at the end of the frame. While nuklear's default platform backends
  1348. /// already call `nk_clear` for you if you write your own backend not calling
  1349. /// `nk_clear` can cause asserts or even worse undefined behavior.
  1350. ///
  1351. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1352. /// struct nk_context ctx;
  1353. /// nk_init_xxx(&ctx, ...);
  1354. /// while (1) {
  1355. /// Event evt;
  1356. /// nk_input_begin(&ctx);
  1357. /// while (GetEvent(&evt)) {
  1358. /// if (evt.type == MOUSE_MOVE)
  1359. /// nk_input_motion(&ctx, evt.motion.x, evt.motion.y);
  1360. /// else if (evt.type == [...]) {
  1361. /// nk_input_xxx(...);
  1362. /// }
  1363. /// }
  1364. /// nk_input_end(&ctx);
  1365. ///
  1366. /// if (nk_begin_xxx(...) {
  1367. /// //[...]
  1368. /// }
  1369. /// nk_end(ctx);
  1370. ///
  1371. /// const struct nk_command *cmd = 0;
  1372. /// nk_foreach(cmd, &ctx) {
  1373. /// case NK_COMMAND_LINE:
  1374. /// your_draw_line_function(...)
  1375. /// break;
  1376. /// case NK_COMMAND_RECT
  1377. /// your_draw_rect_function(...)
  1378. /// break;
  1379. /// case //...:
  1380. /// //[...]
  1381. /// }
  1382. /// nk_clear(&ctx);
  1383. /// }
  1384. /// nk_free(&ctx);
  1385. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1386. ///
  1387. /// #### Reference
  1388. /// Function | Description
  1389. /// ------------------------------------|----------------------------------------
  1390. /// nk_begin | Starts a new window; needs to be called every frame for every window (unless hidden) or otherwise the window gets removed
  1391. /// nk_begin_titled | Extended window start with separated title and identifier to allow multiple windows with same name but not title
  1392. /// nk_end | Needs to be called at the end of the window building process to process scaling, scrollbars and general cleanup
  1393. //
  1394. /// nk_window_find | Finds and returns the window with give name
  1395. /// nk_window_get_bounds | Returns a rectangle with screen position and size of the currently processed window.
  1396. /// nk_window_get_position | Returns the position of the currently processed window
  1397. /// nk_window_get_size | Returns the size with width and height of the currently processed window
  1398. /// nk_window_get_width | Returns the width of the currently processed window
  1399. /// nk_window_get_height | Returns the height of the currently processed window
  1400. /// nk_window_get_panel | Returns the underlying panel which contains all processing state of the current window
  1401. /// nk_window_get_content_region | Returns the position and size of the currently visible and non-clipped space inside the currently processed window
  1402. /// nk_window_get_content_region_min | Returns the upper rectangle position of the currently visible and non-clipped space inside the currently processed window
  1403. /// nk_window_get_content_region_max | Returns the upper rectangle position of the currently visible and non-clipped space inside the currently processed window
  1404. /// nk_window_get_content_region_size | Returns the size of the currently visible and non-clipped space inside the currently processed window
  1405. /// nk_window_get_canvas | Returns the draw command buffer. Can be used to draw custom widgets
  1406. /// nk_window_get_scroll | Gets the scroll offset of the current window
  1407. /// nk_window_has_focus | Returns if the currently processed window is currently active
  1408. /// nk_window_is_collapsed | Returns if the window with given name is currently minimized/collapsed
  1409. /// nk_window_is_closed | Returns if the currently processed window was closed
  1410. /// nk_window_is_hidden | Returns if the currently processed window was hidden
  1411. /// nk_window_is_active | Same as nk_window_has_focus for some reason
  1412. /// nk_window_is_hovered | Returns if the currently processed window is currently being hovered by mouse
  1413. /// nk_window_is_any_hovered | Return if any window currently hovered
  1414. /// nk_item_is_any_active | Returns if any window or widgets is currently hovered or active
  1415. //
  1416. /// nk_window_set_bounds | Updates position and size of the currently processed window
  1417. /// nk_window_set_position | Updates position of the currently process window
  1418. /// nk_window_set_size | Updates the size of the currently processed window
  1419. /// nk_window_set_focus | Set the currently processed window as active window
  1420. /// nk_window_set_scroll | Sets the scroll offset of the current window
  1421. //
  1422. /// nk_window_close | Closes the window with given window name which deletes the window at the end of the frame
  1423. /// nk_window_collapse | Collapses the window with given window name
  1424. /// nk_window_collapse_if | Collapses the window with given window name if the given condition was met
  1425. /// nk_window_show | Hides a visible or reshows a hidden window
  1426. /// nk_window_show_if | Hides/shows a window depending on condition
  1427. */
  1428. /*
  1429. /// #### nk_panel_flags
  1430. /// Flag | Description
  1431. /// ----------------------------|----------------------------------------
  1432. /// NK_WINDOW_BORDER | Draws a border around the window to visually separate window from the background
  1433. /// NK_WINDOW_MOVABLE | The movable flag indicates that a window can be moved by user input or by dragging the window header
  1434. /// NK_WINDOW_SCALABLE | The scalable flag indicates that a window can be scaled by user input by dragging a scaler icon at the button of the window
  1435. /// NK_WINDOW_CLOSABLE | Adds a closable icon into the header
  1436. /// NK_WINDOW_MINIMIZABLE | Adds a minimize icon into the header
  1437. /// NK_WINDOW_NO_SCROLLBAR | Removes the scrollbar from the window
  1438. /// NK_WINDOW_TITLE | Forces a header at the top at the window showing the title
  1439. /// NK_WINDOW_SCROLL_AUTO_HIDE | Automatically hides the window scrollbar if no user interaction: also requires delta time in `nk_context` to be set each frame
  1440. /// NK_WINDOW_BACKGROUND | Always keep window in the background
  1441. /// NK_WINDOW_SCALE_LEFT | Puts window scaler in the left-bottom corner instead right-bottom
  1442. /// NK_WINDOW_NO_INPUT | Prevents window of scaling, moving or getting focus
  1443. ///
  1444. /// #### nk_collapse_states
  1445. /// State | Description
  1446. /// ----------------|-----------------------------------------------------------
  1447. /// __NK_MINIMIZED__| UI section is collased and not visible until maximized
  1448. /// __NK_MAXIMIZED__| UI section is extended and visible until minimized
  1449. /// <br /><br />
  1450. */
  1451. enum nk_panel_flags {
  1452. NK_WINDOW_BORDER = NK_FLAG(0),
  1453. NK_WINDOW_MOVABLE = NK_FLAG(1),
  1454. NK_WINDOW_SCALABLE = NK_FLAG(2),
  1455. NK_WINDOW_CLOSABLE = NK_FLAG(3),
  1456. NK_WINDOW_MINIMIZABLE = NK_FLAG(4),
  1457. NK_WINDOW_NO_SCROLLBAR = NK_FLAG(5),
  1458. NK_WINDOW_TITLE = NK_FLAG(6),
  1459. NK_WINDOW_SCROLL_AUTO_HIDE = NK_FLAG(7),
  1460. NK_WINDOW_BACKGROUND = NK_FLAG(8),
  1461. NK_WINDOW_SCALE_LEFT = NK_FLAG(9),
  1462. NK_WINDOW_NO_INPUT = NK_FLAG(10)
  1463. };
  1464. /*/// #### nk_begin
  1465. /// Starts a new window; needs to be called every frame for every
  1466. /// window (unless hidden) or otherwise the window gets removed
  1467. ///
  1468. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1469. /// nk_bool nk_begin(struct nk_context *ctx, const char *title, struct nk_rect bounds, nk_flags flags);
  1470. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1471. ///
  1472. /// Parameter | Description
  1473. /// ------------|-----------------------------------------------------------
  1474. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  1475. /// __title__ | Window title and identifier. Needs to be persistent over frames to identify the window
  1476. /// __bounds__ | Initial position and window size. However if you do not define `NK_WINDOW_SCALABLE` or `NK_WINDOW_MOVABLE` you can set window position and size every frame
  1477. /// __flags__ | Window flags defined in the nk_panel_flags section with a number of different window behaviors
  1478. ///
  1479. /// Returns `true(1)` if the window can be filled up with widgets from this point
  1480. /// until `nk_end` or `false(0)` otherwise for example if minimized
  1481. */
  1482. NK_API nk_bool nk_begin(struct nk_context* ctx, const char* title, struct nk_rect bounds, nk_flags flags);
  1483. /*/// #### nk_begin_titled
  1484. /// Extended window start with separated title and identifier to allow multiple
  1485. /// windows with same title but not name
  1486. ///
  1487. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1488. /// nk_bool nk_begin_titled(struct nk_context *ctx, const char *name, const char *title, struct nk_rect bounds, nk_flags flags);
  1489. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1490. ///
  1491. /// Parameter | Description
  1492. /// ------------|-----------------------------------------------------------
  1493. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  1494. /// __name__ | Window identifier. Needs to be persistent over frames to identify the window
  1495. /// __title__ | Window title displayed inside header if flag `NK_WINDOW_TITLE` or either `NK_WINDOW_CLOSABLE` or `NK_WINDOW_MINIMIZED` was set
  1496. /// __bounds__ | Initial position and window size. However if you do not define `NK_WINDOW_SCALABLE` or `NK_WINDOW_MOVABLE` you can set window position and size every frame
  1497. /// __flags__ | Window flags defined in the nk_panel_flags section with a number of different window behaviors
  1498. ///
  1499. /// Returns `true(1)` if the window can be filled up with widgets from this point
  1500. /// until `nk_end` or `false(0)` otherwise for example if minimized
  1501. */
  1502. NK_API nk_bool nk_begin_titled(struct nk_context* ctx, const char* name, const char* title, struct nk_rect bounds, nk_flags flags);
  1503. /*/// #### nk_end
  1504. /// Needs to be called at the end of the window building process to process scaling, scrollbars and general cleanup.
  1505. /// All widget calls after this functions will result in asserts or no state changes
  1506. ///
  1507. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1508. /// void nk_end(struct nk_context *ctx);
  1509. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1510. ///
  1511. /// Parameter | Description
  1512. /// ------------|-----------------------------------------------------------
  1513. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  1514. */
  1515. NK_API void nk_end(struct nk_context* ctx);
  1516. /*/// #### nk_window_find
  1517. /// Finds and returns a window from passed name
  1518. ///
  1519. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1520. /// struct nk_window *nk_window_find(struct nk_context *ctx, const char *name);
  1521. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1522. ///
  1523. /// Parameter | Description
  1524. /// ------------|-----------------------------------------------------------
  1525. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  1526. /// __name__ | Window identifier
  1527. ///
  1528. /// Returns a `nk_window` struct pointing to the identified window or NULL if
  1529. /// no window with the given name was found
  1530. */
  1531. NK_API struct nk_window* nk_window_find(struct nk_context* ctx, const char* name);
  1532. /*/// #### nk_window_get_bounds
  1533. /// Returns a rectangle with screen position and size of the currently processed window
  1534. ///
  1535. /// !!! WARNING
  1536. /// Only call this function between calls `nk_begin_xxx` and `nk_end`
  1537. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1538. /// struct nk_rect nk_window_get_bounds(const struct nk_context *ctx);
  1539. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1540. ///
  1541. /// Parameter | Description
  1542. /// ------------|-----------------------------------------------------------
  1543. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  1544. ///
  1545. /// Returns a `nk_rect` struct with window upper left window position and size
  1546. */
  1547. NK_API struct nk_rect nk_window_get_bounds(const struct nk_context* ctx);
  1548. /*/// #### nk_window_get_position
  1549. /// Returns the position of the currently processed window.
  1550. ///
  1551. /// !!! WARNING
  1552. /// Only call this function between calls `nk_begin_xxx` and `nk_end`
  1553. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1554. /// struct nk_vec2 nk_window_get_position(const struct nk_context *ctx);
  1555. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1556. ///
  1557. /// Parameter | Description
  1558. /// ------------|-----------------------------------------------------------
  1559. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  1560. ///
  1561. /// Returns a `nk_vec2` struct with window upper left position
  1562. */
  1563. NK_API struct nk_vec2 nk_window_get_position(const struct nk_context* ctx);
  1564. /*/// #### nk_window_get_size
  1565. /// Returns the size with width and height of the currently processed window.
  1566. ///
  1567. /// !!! WARNING
  1568. /// Only call this function between calls `nk_begin_xxx` and `nk_end`
  1569. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1570. /// struct nk_vec2 nk_window_get_size(const struct nk_context *ctx);
  1571. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1572. ///
  1573. /// Parameter | Description
  1574. /// ------------|-----------------------------------------------------------
  1575. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  1576. ///
  1577. /// Returns a `nk_vec2` struct with window width and height
  1578. */
  1579. NK_API struct nk_vec2 nk_window_get_size(const struct nk_context*);
  1580. /*/// #### nk_window_get_width
  1581. /// Returns the width of the currently processed window.
  1582. ///
  1583. /// !!! WARNING
  1584. /// Only call this function between calls `nk_begin_xxx` and `nk_end`
  1585. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1586. /// float nk_window_get_width(const struct nk_context *ctx);
  1587. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1588. ///
  1589. /// Parameter | Description
  1590. /// ------------|-----------------------------------------------------------
  1591. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  1592. ///
  1593. /// Returns the current window width
  1594. */
  1595. NK_API float nk_window_get_width(const struct nk_context*);
  1596. /*/// #### nk_window_get_height
  1597. /// Returns the height of the currently processed window.
  1598. ///
  1599. /// !!! WARNING
  1600. /// Only call this function between calls `nk_begin_xxx` and `nk_end`
  1601. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1602. /// float nk_window_get_height(const struct nk_context *ctx);
  1603. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1604. ///
  1605. /// Parameter | Description
  1606. /// ------------|-----------------------------------------------------------
  1607. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  1608. ///
  1609. /// Returns the current window height
  1610. */
  1611. NK_API float nk_window_get_height(const struct nk_context*);
  1612. /*/// #### nk_window_get_panel
  1613. /// Returns the underlying panel which contains all processing state of the current window.
  1614. ///
  1615. /// !!! WARNING
  1616. /// Only call this function between calls `nk_begin_xxx` and `nk_end`
  1617. /// !!! WARNING
  1618. /// Do not keep the returned panel pointer around, it is only valid until `nk_end`
  1619. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1620. /// struct nk_panel* nk_window_get_panel(struct nk_context *ctx);
  1621. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1622. ///
  1623. /// Parameter | Description
  1624. /// ------------|-----------------------------------------------------------
  1625. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  1626. ///
  1627. /// Returns a pointer to window internal `nk_panel` state.
  1628. */
  1629. NK_API struct nk_panel* nk_window_get_panel(struct nk_context*);
  1630. /*/// #### nk_window_get_content_region
  1631. /// Returns the position and size of the currently visible and non-clipped space
  1632. /// inside the currently processed window.
  1633. ///
  1634. /// !!! WARNING
  1635. /// Only call this function between calls `nk_begin_xxx` and `nk_end`
  1636. ///
  1637. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1638. /// struct nk_rect nk_window_get_content_region(struct nk_context *ctx);
  1639. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1640. ///
  1641. /// Parameter | Description
  1642. /// ------------|-----------------------------------------------------------
  1643. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  1644. ///
  1645. /// Returns `nk_rect` struct with screen position and size (no scrollbar offset)
  1646. /// of the visible space inside the current window
  1647. */
  1648. NK_API struct nk_rect nk_window_get_content_region(struct nk_context*);
  1649. /*/// #### nk_window_get_content_region_min
  1650. /// Returns the upper left position of the currently visible and non-clipped
  1651. /// space inside the currently processed window.
  1652. ///
  1653. /// !!! WARNING
  1654. /// Only call this function between calls `nk_begin_xxx` and `nk_end`
  1655. ///
  1656. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1657. /// struct nk_vec2 nk_window_get_content_region_min(struct nk_context *ctx);
  1658. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1659. ///
  1660. /// Parameter | Description
  1661. /// ------------|-----------------------------------------------------------
  1662. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  1663. ///
  1664. /// returns `nk_vec2` struct with upper left screen position (no scrollbar offset)
  1665. /// of the visible space inside the current window
  1666. */
  1667. NK_API struct nk_vec2 nk_window_get_content_region_min(struct nk_context*);
  1668. /*/// #### nk_window_get_content_region_max
  1669. /// Returns the lower right screen position of the currently visible and
  1670. /// non-clipped space inside the currently processed window.
  1671. ///
  1672. /// !!! WARNING
  1673. /// Only call this function between calls `nk_begin_xxx` and `nk_end`
  1674. ///
  1675. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1676. /// struct nk_vec2 nk_window_get_content_region_max(struct nk_context *ctx);
  1677. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1678. ///
  1679. /// Parameter | Description
  1680. /// ------------|-----------------------------------------------------------
  1681. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  1682. ///
  1683. /// Returns `nk_vec2` struct with lower right screen position (no scrollbar offset)
  1684. /// of the visible space inside the current window
  1685. */
  1686. NK_API struct nk_vec2 nk_window_get_content_region_max(struct nk_context*);
  1687. /*/// #### nk_window_get_content_region_size
  1688. /// Returns the size of the currently visible and non-clipped space inside the
  1689. /// currently processed window
  1690. ///
  1691. /// !!! WARNING
  1692. /// Only call this function between calls `nk_begin_xxx` and `nk_end`
  1693. ///
  1694. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1695. /// struct nk_vec2 nk_window_get_content_region_size(struct nk_context *ctx);
  1696. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1697. ///
  1698. /// Parameter | Description
  1699. /// ------------|-----------------------------------------------------------
  1700. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  1701. ///
  1702. /// Returns `nk_vec2` struct with size the visible space inside the current window
  1703. */
  1704. NK_API struct nk_vec2 nk_window_get_content_region_size(struct nk_context*);
  1705. /*/// #### nk_window_get_canvas
  1706. /// Returns the draw command buffer. Can be used to draw custom widgets
  1707. /// !!! WARNING
  1708. /// Only call this function between calls `nk_begin_xxx` and `nk_end`
  1709. /// !!! WARNING
  1710. /// Do not keep the returned command buffer pointer around it is only valid until `nk_end`
  1711. ///
  1712. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1713. /// struct nk_command_buffer* nk_window_get_canvas(struct nk_context *ctx);
  1714. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1715. ///
  1716. /// Parameter | Description
  1717. /// ------------|-----------------------------------------------------------
  1718. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  1719. ///
  1720. /// Returns a pointer to window internal `nk_command_buffer` struct used as
  1721. /// drawing canvas. Can be used to do custom drawing.
  1722. */
  1723. NK_API struct nk_command_buffer* nk_window_get_canvas(struct nk_context*);
  1724. /*/// #### nk_window_get_scroll
  1725. /// Gets the scroll offset for the current window
  1726. /// !!! WARNING
  1727. /// Only call this function between calls `nk_begin_xxx` and `nk_end`
  1728. ///
  1729. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1730. /// void nk_window_get_scroll(struct nk_context *ctx, nk_uint *offset_x, nk_uint *offset_y);
  1731. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1732. ///
  1733. /// Parameter | Description
  1734. /// -------------|-----------------------------------------------------------
  1735. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  1736. /// __offset_x__ | A pointer to the x offset output (or NULL to ignore)
  1737. /// __offset_y__ | A pointer to the y offset output (or NULL to ignore)
  1738. */
  1739. NK_API void nk_window_get_scroll(struct nk_context*, nk_uint* offset_x, nk_uint* offset_y);
  1740. /*/// #### nk_window_has_focus
  1741. /// Returns if the currently processed window is currently active
  1742. /// !!! WARNING
  1743. /// Only call this function between calls `nk_begin_xxx` and `nk_end`
  1744. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1745. /// nk_bool nk_window_has_focus(const struct nk_context *ctx);
  1746. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1747. ///
  1748. /// Parameter | Description
  1749. /// ------------|-----------------------------------------------------------
  1750. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  1751. ///
  1752. /// Returns `false(0)` if current window is not active or `true(1)` if it is
  1753. */
  1754. NK_API nk_bool nk_window_has_focus(const struct nk_context*);
  1755. /*/// #### nk_window_is_hovered
  1756. /// Return if the current window is being hovered
  1757. /// !!! WARNING
  1758. /// Only call this function between calls `nk_begin_xxx` and `nk_end`
  1759. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1760. /// nk_bool nk_window_is_hovered(struct nk_context *ctx);
  1761. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1762. ///
  1763. /// Parameter | Description
  1764. /// ------------|-----------------------------------------------------------
  1765. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  1766. ///
  1767. /// Returns `true(1)` if current window is hovered or `false(0)` otherwise
  1768. */
  1769. NK_API nk_bool nk_window_is_hovered(struct nk_context*);
  1770. /*/// #### nk_window_is_collapsed
  1771. /// Returns if the window with given name is currently minimized/collapsed
  1772. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1773. /// nk_bool nk_window_is_collapsed(struct nk_context *ctx, const char *name);
  1774. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1775. ///
  1776. /// Parameter | Description
  1777. /// ------------|-----------------------------------------------------------
  1778. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  1779. /// __name__ | Identifier of window you want to check if it is collapsed
  1780. ///
  1781. /// Returns `true(1)` if current window is minimized and `false(0)` if window not
  1782. /// found or is not minimized
  1783. */
  1784. NK_API nk_bool nk_window_is_collapsed(struct nk_context* ctx, const char* name);
  1785. /*/// #### nk_window_is_closed
  1786. /// Returns if the window with given name was closed by calling `nk_close`
  1787. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1788. /// nk_bool nk_window_is_closed(struct nk_context *ctx, const char *name);
  1789. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1790. ///
  1791. /// Parameter | Description
  1792. /// ------------|-----------------------------------------------------------
  1793. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  1794. /// __name__ | Identifier of window you want to check if it is closed
  1795. ///
  1796. /// Returns `true(1)` if current window was closed or `false(0)` window not found or not closed
  1797. */
  1798. NK_API nk_bool nk_window_is_closed(struct nk_context*, const char*);
  1799. /*/// #### nk_window_is_hidden
  1800. /// Returns if the window with given name is hidden
  1801. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1802. /// nk_bool nk_window_is_hidden(struct nk_context *ctx, const char *name);
  1803. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1804. ///
  1805. /// Parameter | Description
  1806. /// ------------|-----------------------------------------------------------
  1807. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  1808. /// __name__ | Identifier of window you want to check if it is hidden
  1809. ///
  1810. /// Returns `true(1)` if current window is hidden or `false(0)` window not found or visible
  1811. */
  1812. NK_API nk_bool nk_window_is_hidden(struct nk_context*, const char*);
  1813. /*/// #### nk_window_is_active
  1814. /// Same as nk_window_has_focus for some reason
  1815. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1816. /// nk_bool nk_window_is_active(struct nk_context *ctx, const char *name);
  1817. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1818. ///
  1819. /// Parameter | Description
  1820. /// ------------|-----------------------------------------------------------
  1821. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  1822. /// __name__ | Identifier of window you want to check if it is active
  1823. ///
  1824. /// Returns `true(1)` if current window is active or `false(0)` window not found or not active
  1825. */
  1826. NK_API nk_bool nk_window_is_active(struct nk_context*, const char*);
  1827. /*/// #### nk_window_is_any_hovered
  1828. /// Returns if the any window is being hovered
  1829. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1830. /// nk_bool nk_window_is_any_hovered(struct nk_context*);
  1831. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1832. ///
  1833. /// Parameter | Description
  1834. /// ------------|-----------------------------------------------------------
  1835. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  1836. ///
  1837. /// Returns `true(1)` if any window is hovered or `false(0)` otherwise
  1838. */
  1839. NK_API nk_bool nk_window_is_any_hovered(struct nk_context*);
  1840. /*/// #### nk_item_is_any_active
  1841. /// Returns if the any window is being hovered or any widget is currently active.
  1842. /// Can be used to decide if input should be processed by UI or your specific input handling.
  1843. /// Example could be UI and 3D camera to move inside a 3D space.
  1844. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1845. /// nk_bool nk_item_is_any_active(struct nk_context*);
  1846. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1847. ///
  1848. /// Parameter | Description
  1849. /// ------------|-----------------------------------------------------------
  1850. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  1851. ///
  1852. /// Returns `true(1)` if any window is hovered or any item is active or `false(0)` otherwise
  1853. */
  1854. NK_API nk_bool nk_item_is_any_active(struct nk_context*);
  1855. /*/// #### nk_window_set_bounds
  1856. /// Updates position and size of window with passed in name
  1857. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1858. /// void nk_window_set_bounds(struct nk_context*, const char *name, struct nk_rect bounds);
  1859. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1860. ///
  1861. /// Parameter | Description
  1862. /// ------------|-----------------------------------------------------------
  1863. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  1864. /// __name__ | Identifier of the window to modify both position and size
  1865. /// __bounds__ | Must point to a `nk_rect` struct with the new position and size
  1866. */
  1867. NK_API void nk_window_set_bounds(struct nk_context*, const char* name, struct nk_rect bounds);
  1868. /*/// #### nk_window_set_position
  1869. /// Updates position of window with passed name
  1870. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1871. /// void nk_window_set_position(struct nk_context*, const char *name, struct nk_vec2 pos);
  1872. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1873. ///
  1874. /// Parameter | Description
  1875. /// ------------|-----------------------------------------------------------
  1876. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  1877. /// __name__ | Identifier of the window to modify both position
  1878. /// __pos__ | Must point to a `nk_vec2` struct with the new position
  1879. */
  1880. NK_API void nk_window_set_position(struct nk_context*, const char* name, struct nk_vec2 pos);
  1881. /*/// #### nk_window_set_size
  1882. /// Updates size of window with passed in name
  1883. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1884. /// void nk_window_set_size(struct nk_context*, const char *name, struct nk_vec2);
  1885. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1886. ///
  1887. /// Parameter | Description
  1888. /// ------------|-----------------------------------------------------------
  1889. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  1890. /// __name__ | Identifier of the window to modify both window size
  1891. /// __size__ | Must point to a `nk_vec2` struct with new window size
  1892. */
  1893. NK_API void nk_window_set_size(struct nk_context*, const char* name, struct nk_vec2);
  1894. /*/// #### nk_window_set_focus
  1895. /// Sets the window with given name as active
  1896. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1897. /// void nk_window_set_focus(struct nk_context*, const char *name);
  1898. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1899. ///
  1900. /// Parameter | Description
  1901. /// ------------|-----------------------------------------------------------
  1902. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  1903. /// __name__ | Identifier of the window to set focus on
  1904. */
  1905. NK_API void nk_window_set_focus(struct nk_context*, const char* name);
  1906. /*/// #### nk_window_set_scroll
  1907. /// Sets the scroll offset for the current window
  1908. /// !!! WARNING
  1909. /// Only call this function between calls `nk_begin_xxx` and `nk_end`
  1910. ///
  1911. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1912. /// void nk_window_set_scroll(struct nk_context *ctx, nk_uint offset_x, nk_uint offset_y);
  1913. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1914. ///
  1915. /// Parameter | Description
  1916. /// -------------|-----------------------------------------------------------
  1917. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  1918. /// __offset_x__ | The x offset to scroll to
  1919. /// __offset_y__ | The y offset to scroll to
  1920. */
  1921. NK_API void nk_window_set_scroll(struct nk_context*, nk_uint offset_x, nk_uint offset_y);
  1922. /*/// #### nk_window_close
  1923. /// Closes a window and marks it for being freed at the end of the frame
  1924. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1925. /// void nk_window_close(struct nk_context *ctx, const char *name);
  1926. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1927. ///
  1928. /// Parameter | Description
  1929. /// ------------|-----------------------------------------------------------
  1930. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  1931. /// __name__ | Identifier of the window to close
  1932. */
  1933. NK_API void nk_window_close(struct nk_context* ctx, const char* name);
  1934. /*/// #### nk_window_collapse
  1935. /// Updates collapse state of a window with given name
  1936. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1937. /// void nk_window_collapse(struct nk_context*, const char *name, enum nk_collapse_states state);
  1938. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1939. ///
  1940. /// Parameter | Description
  1941. /// ------------|-----------------------------------------------------------
  1942. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  1943. /// __name__ | Identifier of the window to close
  1944. /// __state__ | value out of nk_collapse_states section
  1945. */
  1946. NK_API void nk_window_collapse(struct nk_context*, const char* name, enum nk_collapse_states state);
  1947. /*/// #### nk_window_collapse_if
  1948. /// Updates collapse state of a window with given name if given condition is met
  1949. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1950. /// void nk_window_collapse_if(struct nk_context*, const char *name, enum nk_collapse_states, int cond);
  1951. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1952. ///
  1953. /// Parameter | Description
  1954. /// ------------|-----------------------------------------------------------
  1955. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  1956. /// __name__ | Identifier of the window to either collapse or maximize
  1957. /// __state__ | value out of nk_collapse_states section the window should be put into
  1958. /// __cond__ | condition that has to be met to actually commit the collapse state change
  1959. */
  1960. NK_API void nk_window_collapse_if(struct nk_context*, const char* name, enum nk_collapse_states, int cond);
  1961. /*/// #### nk_window_show
  1962. /// updates visibility state of a window with given name
  1963. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1964. /// void nk_window_show(struct nk_context*, const char *name, enum nk_show_states);
  1965. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1966. ///
  1967. /// Parameter | Description
  1968. /// ------------|-----------------------------------------------------------
  1969. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  1970. /// __name__ | Identifier of the window to either collapse or maximize
  1971. /// __state__ | state with either visible or hidden to modify the window with
  1972. */
  1973. NK_API void nk_window_show(struct nk_context*, const char* name, enum nk_show_states);
  1974. /*/// #### nk_window_show_if
  1975. /// Updates visibility state of a window with given name if a given condition is met
  1976. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1977. /// void nk_window_show_if(struct nk_context*, const char *name, enum nk_show_states, int cond);
  1978. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1979. ///
  1980. /// Parameter | Description
  1981. /// ------------|-----------------------------------------------------------
  1982. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  1983. /// __name__ | Identifier of the window to either hide or show
  1984. /// __state__ | state with either visible or hidden to modify the window with
  1985. /// __cond__ | condition that has to be met to actually commit the visbility state change
  1986. */
  1987. NK_API void nk_window_show_if(struct nk_context*, const char* name, enum nk_show_states, int cond);
  1988. /*/// #### nk_window_show_if
  1989. /// Line for visual seperation. Draws a line with thickness determined by the current row height.
  1990. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  1991. /// void nk_rule_horizontal(struct nk_context *ctx, struct nk_color color, NK_BOOL rounding)
  1992. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1993. ///
  1994. /// Parameter | Description
  1995. /// ----------------|-------------------------------------------------------
  1996. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  1997. /// __color__ | Color of the horizontal line
  1998. /// __rounding__ | Whether or not to make the line round
  1999. */
  2000. NK_API void nk_rule_horizontal(struct nk_context* ctx, struct nk_color color, nk_bool rounding);
  2001. /* =============================================================================
  2002. *
  2003. * LAYOUT
  2004. *
  2005. * =============================================================================
  2006. /// ### Layouting
  2007. /// Layouting in general describes placing widget inside a window with position and size.
  2008. /// While in this particular implementation there are five different APIs for layouting
  2009. /// each with different trade offs between control and ease of use. <br /><br />
  2010. ///
  2011. /// All layouting methods in this library are based around the concept of a row.
  2012. /// A row has a height the window content grows by and a number of columns and each
  2013. /// layouting method specifies how each widget is placed inside the row.
  2014. /// After a row has been allocated by calling a layouting functions and then
  2015. /// filled with widgets will advance an internal pointer over the allocated row. <br /><br />
  2016. ///
  2017. /// To actually define a layout you just call the appropriate layouting function
  2018. /// and each subsequent widget call will place the widget as specified. Important
  2019. /// here is that if you define more widgets then columns defined inside the layout
  2020. /// functions it will allocate the next row without you having to make another layouting <br /><br />
  2021. /// call.
  2022. ///
  2023. /// Biggest limitation with using all these APIs outside the `nk_layout_space_xxx` API
  2024. /// is that you have to define the row height for each. However the row height
  2025. /// often depends on the height of the font. <br /><br />
  2026. ///
  2027. /// To fix that internally nuklear uses a minimum row height that is set to the
  2028. /// height plus padding of currently active font and overwrites the row height
  2029. /// value if zero. <br /><br />
  2030. ///
  2031. /// If you manually want to change the minimum row height then
  2032. /// use nk_layout_set_min_row_height, and use nk_layout_reset_min_row_height to
  2033. /// reset it back to be derived from font height. <br /><br />
  2034. ///
  2035. /// Also if you change the font in nuklear it will automatically change the minimum
  2036. /// row height for you and. This means if you change the font but still want
  2037. /// a minimum row height smaller than the font you have to repush your value. <br /><br />
  2038. ///
  2039. /// For actually more advanced UI I would even recommend using the `nk_layout_space_xxx`
  2040. /// layouting method in combination with a cassowary constraint solver (there are
  2041. /// some versions on github with permissive license model) to take over all control over widget
  2042. /// layouting yourself. However for quick and dirty layouting using all the other layouting
  2043. /// functions should be fine.
  2044. ///
  2045. /// #### Usage
  2046. /// 1. __nk_layout_row_dynamic__<br /><br />
  2047. /// The easiest layouting function is `nk_layout_row_dynamic`. It provides each
  2048. /// widgets with same horizontal space inside the row and dynamically grows
  2049. /// if the owning window grows in width. So the number of columns dictates
  2050. /// the size of each widget dynamically by formula:
  2051. ///
  2052. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2053. /// widget_width = (window_width - padding - spacing) * (1/colum_count)
  2054. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2055. ///
  2056. /// Just like all other layouting APIs if you define more widget than columns this
  2057. /// library will allocate a new row and keep all layouting parameters previously
  2058. /// defined.
  2059. ///
  2060. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2061. /// if (nk_begin_xxx(...) {
  2062. /// // first row with height: 30 composed of two widgets
  2063. /// nk_layout_row_dynamic(&ctx, 30, 2);
  2064. /// nk_widget(...);
  2065. /// nk_widget(...);
  2066. /// //
  2067. /// // second row with same parameter as defined above
  2068. /// nk_widget(...);
  2069. /// nk_widget(...);
  2070. /// //
  2071. /// // third row uses 0 for height which will use auto layouting
  2072. /// nk_layout_row_dynamic(&ctx, 0, 2);
  2073. /// nk_widget(...);
  2074. /// nk_widget(...);
  2075. /// }
  2076. /// nk_end(...);
  2077. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2078. ///
  2079. /// 2. __nk_layout_row_static__<br /><br />
  2080. /// Another easy layouting function is `nk_layout_row_static`. It provides each
  2081. /// widget with same horizontal pixel width inside the row and does not grow
  2082. /// if the owning window scales smaller or bigger.
  2083. ///
  2084. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2085. /// if (nk_begin_xxx(...) {
  2086. /// // first row with height: 30 composed of two widgets with width: 80
  2087. /// nk_layout_row_static(&ctx, 30, 80, 2);
  2088. /// nk_widget(...);
  2089. /// nk_widget(...);
  2090. /// //
  2091. /// // second row with same parameter as defined above
  2092. /// nk_widget(...);
  2093. /// nk_widget(...);
  2094. /// //
  2095. /// // third row uses 0 for height which will use auto layouting
  2096. /// nk_layout_row_static(&ctx, 0, 80, 2);
  2097. /// nk_widget(...);
  2098. /// nk_widget(...);
  2099. /// }
  2100. /// nk_end(...);
  2101. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2102. ///
  2103. /// 3. __nk_layout_row_xxx__<br /><br />
  2104. /// A little bit more advanced layouting API are functions `nk_layout_row_begin`,
  2105. /// `nk_layout_row_push` and `nk_layout_row_end`. They allow to directly
  2106. /// specify each column pixel or window ratio in a row. It supports either
  2107. /// directly setting per column pixel width or widget window ratio but not
  2108. /// both. Furthermore it is a immediate mode API so each value is directly
  2109. /// pushed before calling a widget. Therefore the layout is not automatically
  2110. /// repeating like the last two layouting functions.
  2111. ///
  2112. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2113. /// if (nk_begin_xxx(...) {
  2114. /// // first row with height: 25 composed of two widgets with width 60 and 40
  2115. /// nk_layout_row_begin(ctx, NK_STATIC, 25, 2);
  2116. /// nk_layout_row_push(ctx, 60);
  2117. /// nk_widget(...);
  2118. /// nk_layout_row_push(ctx, 40);
  2119. /// nk_widget(...);
  2120. /// nk_layout_row_end(ctx);
  2121. /// //
  2122. /// // second row with height: 25 composed of two widgets with window ratio 0.25 and 0.75
  2123. /// nk_layout_row_begin(ctx, NK_DYNAMIC, 25, 2);
  2124. /// nk_layout_row_push(ctx, 0.25f);
  2125. /// nk_widget(...);
  2126. /// nk_layout_row_push(ctx, 0.75f);
  2127. /// nk_widget(...);
  2128. /// nk_layout_row_end(ctx);
  2129. /// //
  2130. /// // third row with auto generated height: composed of two widgets with window ratio 0.25 and 0.75
  2131. /// nk_layout_row_begin(ctx, NK_DYNAMIC, 0, 2);
  2132. /// nk_layout_row_push(ctx, 0.25f);
  2133. /// nk_widget(...);
  2134. /// nk_layout_row_push(ctx, 0.75f);
  2135. /// nk_widget(...);
  2136. /// nk_layout_row_end(ctx);
  2137. /// }
  2138. /// nk_end(...);
  2139. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2140. ///
  2141. /// 4. __nk_layout_row__<br /><br />
  2142. /// The array counterpart to API nk_layout_row_xxx is the single nk_layout_row
  2143. /// functions. Instead of pushing either pixel or window ratio for every widget
  2144. /// it allows to define it by array. The trade of for less control is that
  2145. /// `nk_layout_row` is automatically repeating. Otherwise the behavior is the
  2146. /// same.
  2147. ///
  2148. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2149. /// if (nk_begin_xxx(...) {
  2150. /// // two rows with height: 30 composed of two widgets with width 60 and 40
  2151. /// const float ratio[] = {60,40};
  2152. /// nk_layout_row(ctx, NK_STATIC, 30, 2, ratio);
  2153. /// nk_widget(...);
  2154. /// nk_widget(...);
  2155. /// nk_widget(...);
  2156. /// nk_widget(...);
  2157. /// //
  2158. /// // two rows with height: 30 composed of two widgets with window ratio 0.25 and 0.75
  2159. /// const float ratio[] = {0.25, 0.75};
  2160. /// nk_layout_row(ctx, NK_DYNAMIC, 30, 2, ratio);
  2161. /// nk_widget(...);
  2162. /// nk_widget(...);
  2163. /// nk_widget(...);
  2164. /// nk_widget(...);
  2165. /// //
  2166. /// // two rows with auto generated height composed of two widgets with window ratio 0.25 and 0.75
  2167. /// const float ratio[] = {0.25, 0.75};
  2168. /// nk_layout_row(ctx, NK_DYNAMIC, 30, 2, ratio);
  2169. /// nk_widget(...);
  2170. /// nk_widget(...);
  2171. /// nk_widget(...);
  2172. /// nk_widget(...);
  2173. /// }
  2174. /// nk_end(...);
  2175. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2176. ///
  2177. /// 5. __nk_layout_row_template_xxx__<br /><br />
  2178. /// The most complex and second most flexible API is a simplified flexbox version without
  2179. /// line wrapping and weights for dynamic widgets. It is an immediate mode API but
  2180. /// unlike `nk_layout_row_xxx` it has auto repeat behavior and needs to be called
  2181. /// before calling the templated widgets.
  2182. /// The row template layout has three different per widget size specifier. The first
  2183. /// one is the `nk_layout_row_template_push_static` with fixed widget pixel width.
  2184. /// They do not grow if the row grows and will always stay the same.
  2185. /// The second size specifier is `nk_layout_row_template_push_variable`
  2186. /// which defines a minimum widget size but it also can grow if more space is available
  2187. /// not taken by other widgets.
  2188. /// Finally there are dynamic widgets with `nk_layout_row_template_push_dynamic`
  2189. /// which are completely flexible and unlike variable widgets can even shrink
  2190. /// to zero if not enough space is provided.
  2191. ///
  2192. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2193. /// if (nk_begin_xxx(...) {
  2194. /// // two rows with height: 30 composed of three widgets
  2195. /// nk_layout_row_template_begin(ctx, 30);
  2196. /// nk_layout_row_template_push_dynamic(ctx);
  2197. /// nk_layout_row_template_push_variable(ctx, 80);
  2198. /// nk_layout_row_template_push_static(ctx, 80);
  2199. /// nk_layout_row_template_end(ctx);
  2200. /// //
  2201. /// // first row
  2202. /// nk_widget(...); // dynamic widget can go to zero if not enough space
  2203. /// nk_widget(...); // variable widget with min 80 pixel but can grow bigger if enough space
  2204. /// nk_widget(...); // static widget with fixed 80 pixel width
  2205. /// //
  2206. /// // second row same layout
  2207. /// nk_widget(...);
  2208. /// nk_widget(...);
  2209. /// nk_widget(...);
  2210. /// }
  2211. /// nk_end(...);
  2212. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2213. ///
  2214. /// 6. __nk_layout_space_xxx__<br /><br />
  2215. /// Finally the most flexible API directly allows you to place widgets inside the
  2216. /// window. The space layout API is an immediate mode API which does not support
  2217. /// row auto repeat and directly sets position and size of a widget. Position
  2218. /// and size hereby can be either specified as ratio of allocated space or
  2219. /// allocated space local position and pixel size. Since this API is quite
  2220. /// powerful there are a number of utility functions to get the available space
  2221. /// and convert between local allocated space and screen space.
  2222. ///
  2223. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2224. /// if (nk_begin_xxx(...) {
  2225. /// // static row with height: 500 (you can set column count to INT_MAX if you don't want to be bothered)
  2226. /// nk_layout_space_begin(ctx, NK_STATIC, 500, INT_MAX);
  2227. /// nk_layout_space_push(ctx, nk_rect(0,0,150,200));
  2228. /// nk_widget(...);
  2229. /// nk_layout_space_push(ctx, nk_rect(200,200,100,200));
  2230. /// nk_widget(...);
  2231. /// nk_layout_space_end(ctx);
  2232. /// //
  2233. /// // dynamic row with height: 500 (you can set column count to INT_MAX if you don't want to be bothered)
  2234. /// nk_layout_space_begin(ctx, NK_DYNAMIC, 500, INT_MAX);
  2235. /// nk_layout_space_push(ctx, nk_rect(0.5,0.5,0.1,0.1));
  2236. /// nk_widget(...);
  2237. /// nk_layout_space_push(ctx, nk_rect(0.7,0.6,0.1,0.1));
  2238. /// nk_widget(...);
  2239. /// }
  2240. /// nk_end(...);
  2241. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2242. ///
  2243. /// #### Reference
  2244. /// Function | Description
  2245. /// ----------------------------------------|------------------------------------
  2246. /// nk_layout_set_min_row_height | Set the currently used minimum row height to a specified value
  2247. /// nk_layout_reset_min_row_height | Resets the currently used minimum row height to font height
  2248. /// nk_layout_widget_bounds | Calculates current width a static layout row can fit inside a window
  2249. /// nk_layout_ratio_from_pixel | Utility functions to calculate window ratio from pixel size
  2250. //
  2251. /// nk_layout_row_dynamic | Current layout is divided into n same sized growing columns
  2252. /// nk_layout_row_static | Current layout is divided into n same fixed sized columns
  2253. /// nk_layout_row_begin | Starts a new row with given height and number of columns
  2254. /// nk_layout_row_push | Pushes another column with given size or window ratio
  2255. /// nk_layout_row_end | Finished previously started row
  2256. /// nk_layout_row | Specifies row columns in array as either window ratio or size
  2257. //
  2258. /// nk_layout_row_template_begin | Begins the row template declaration
  2259. /// nk_layout_row_template_push_dynamic | Adds a dynamic column that dynamically grows and can go to zero if not enough space
  2260. /// nk_layout_row_template_push_variable | Adds a variable column that dynamically grows but does not shrink below specified pixel width
  2261. /// nk_layout_row_template_push_static | Adds a static column that does not grow and will always have the same size
  2262. /// nk_layout_row_template_end | Marks the end of the row template
  2263. //
  2264. /// nk_layout_space_begin | Begins a new layouting space that allows to specify each widgets position and size
  2265. /// nk_layout_space_push | Pushes position and size of the next widget in own coordinate space either as pixel or ratio
  2266. /// nk_layout_space_end | Marks the end of the layouting space
  2267. //
  2268. /// nk_layout_space_bounds | Callable after nk_layout_space_begin and returns total space allocated
  2269. /// nk_layout_space_to_screen | Converts vector from nk_layout_space coordinate space into screen space
  2270. /// nk_layout_space_to_local | Converts vector from screen space into nk_layout_space coordinates
  2271. /// nk_layout_space_rect_to_screen | Converts rectangle from nk_layout_space coordinate space into screen space
  2272. /// nk_layout_space_rect_to_local | Converts rectangle from screen space into nk_layout_space coordinates
  2273. */
  2274. /*/// #### nk_layout_set_min_row_height
  2275. /// Sets the currently used minimum row height.
  2276. /// !!! WARNING
  2277. /// The passed height needs to include both your preferred row height
  2278. /// as well as padding. No internal padding is added.
  2279. ///
  2280. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2281. /// void nk_layout_set_min_row_height(struct nk_context*, float height);
  2282. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2283. ///
  2284. /// Parameter | Description
  2285. /// ------------|-----------------------------------------------------------
  2286. /// __ctx__ | Must point to an previously initialized `nk_context` struct after call `nk_begin_xxx`
  2287. /// __height__ | New minimum row height to be used for auto generating the row height
  2288. */
  2289. NK_API void nk_layout_set_min_row_height(struct nk_context*, float height);
  2290. /*/// #### nk_layout_reset_min_row_height
  2291. /// Reset the currently used minimum row height back to `font_height + text_padding + padding`
  2292. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2293. /// void nk_layout_reset_min_row_height(struct nk_context*);
  2294. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2295. ///
  2296. /// Parameter | Description
  2297. /// ------------|-----------------------------------------------------------
  2298. /// __ctx__ | Must point to an previously initialized `nk_context` struct after call `nk_begin_xxx`
  2299. */
  2300. NK_API void nk_layout_reset_min_row_height(struct nk_context*);
  2301. /*/// #### nk_layout_widget_bounds
  2302. /// Returns the width of the next row allocate by one of the layouting functions
  2303. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2304. /// struct nk_rect nk_layout_widget_bounds(struct nk_context*);
  2305. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2306. ///
  2307. /// Parameter | Description
  2308. /// ------------|-----------------------------------------------------------
  2309. /// __ctx__ | Must point to an previously initialized `nk_context` struct after call `nk_begin_xxx`
  2310. ///
  2311. /// Return `nk_rect` with both position and size of the next row
  2312. */
  2313. NK_API struct nk_rect nk_layout_widget_bounds(struct nk_context*);
  2314. /*/// #### nk_layout_ratio_from_pixel
  2315. /// Utility functions to calculate window ratio from pixel size
  2316. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2317. /// float nk_layout_ratio_from_pixel(struct nk_context*, float pixel_width);
  2318. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2319. ///
  2320. /// Parameter | Description
  2321. /// ------------|-----------------------------------------------------------
  2322. /// __ctx__ | Must point to an previously initialized `nk_context` struct after call `nk_begin_xxx`
  2323. /// __pixel__ | Pixel_width to convert to window ratio
  2324. ///
  2325. /// Returns `nk_rect` with both position and size of the next row
  2326. */
  2327. NK_API float nk_layout_ratio_from_pixel(struct nk_context*, float pixel_width);
  2328. /*/// #### nk_layout_row_dynamic
  2329. /// Sets current row layout to share horizontal space
  2330. /// between @cols number of widgets evenly. Once called all subsequent widget
  2331. /// calls greater than @cols will allocate a new row with same layout.
  2332. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2333. /// void nk_layout_row_dynamic(struct nk_context *ctx, float height, int cols);
  2334. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2335. ///
  2336. /// Parameter | Description
  2337. /// ------------|-----------------------------------------------------------
  2338. /// __ctx__ | Must point to an previously initialized `nk_context` struct after call `nk_begin_xxx`
  2339. /// __height__ | Holds height of each widget in row or zero for auto layouting
  2340. /// __columns__ | Number of widget inside row
  2341. */
  2342. NK_API void nk_layout_row_dynamic(struct nk_context* ctx, float height, int cols);
  2343. /*/// #### nk_layout_row_static
  2344. /// Sets current row layout to fill @cols number of widgets
  2345. /// in row with same @item_width horizontal size. Once called all subsequent widget
  2346. /// calls greater than @cols will allocate a new row with same layout.
  2347. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2348. /// void nk_layout_row_static(struct nk_context *ctx, float height, int item_width, int cols);
  2349. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2350. ///
  2351. /// Parameter | Description
  2352. /// ------------|-----------------------------------------------------------
  2353. /// __ctx__ | Must point to an previously initialized `nk_context` struct after call `nk_begin_xxx`
  2354. /// __height__ | Holds height of each widget in row or zero for auto layouting
  2355. /// __width__ | Holds pixel width of each widget in the row
  2356. /// __columns__ | Number of widget inside row
  2357. */
  2358. NK_API void nk_layout_row_static(struct nk_context* ctx, float height, int item_width, int cols);
  2359. /*/// #### nk_layout_row_begin
  2360. /// Starts a new dynamic or fixed row with given height and columns.
  2361. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2362. /// void nk_layout_row_begin(struct nk_context *ctx, enum nk_layout_format fmt, float row_height, int cols);
  2363. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2364. ///
  2365. /// Parameter | Description
  2366. /// ------------|-----------------------------------------------------------
  2367. /// __ctx__ | Must point to an previously initialized `nk_context` struct after call `nk_begin_xxx`
  2368. /// __fmt__ | either `NK_DYNAMIC` for window ratio or `NK_STATIC` for fixed size columns
  2369. /// __height__ | holds height of each widget in row or zero for auto layouting
  2370. /// __columns__ | Number of widget inside row
  2371. */
  2372. NK_API void nk_layout_row_begin(struct nk_context* ctx, enum nk_layout_format fmt, float row_height, int cols);
  2373. /*/// #### nk_layout_row_push
  2374. /// Specifies either window ratio or width of a single column
  2375. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2376. /// void nk_layout_row_push(struct nk_context*, float value);
  2377. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2378. ///
  2379. /// Parameter | Description
  2380. /// ------------|-----------------------------------------------------------
  2381. /// __ctx__ | Must point to an previously initialized `nk_context` struct after call `nk_begin_xxx`
  2382. /// __value__ | either a window ratio or fixed width depending on @fmt in previous `nk_layout_row_begin` call
  2383. */
  2384. NK_API void nk_layout_row_push(struct nk_context*, float value);
  2385. /*/// #### nk_layout_row_end
  2386. /// Finished previously started row
  2387. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2388. /// void nk_layout_row_end(struct nk_context*);
  2389. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2390. ///
  2391. /// Parameter | Description
  2392. /// ------------|-----------------------------------------------------------
  2393. /// __ctx__ | Must point to an previously initialized `nk_context` struct after call `nk_begin_xxx`
  2394. */
  2395. NK_API void nk_layout_row_end(struct nk_context*);
  2396. /*/// #### nk_layout_row
  2397. /// Specifies row columns in array as either window ratio or size
  2398. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2399. /// void nk_layout_row(struct nk_context*, enum nk_layout_format, float height, int cols, const float *ratio);
  2400. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2401. ///
  2402. /// Parameter | Description
  2403. /// ------------|-----------------------------------------------------------
  2404. /// __ctx__ | Must point to an previously initialized `nk_context` struct after call `nk_begin_xxx`
  2405. /// __fmt__ | Either `NK_DYNAMIC` for window ratio or `NK_STATIC` for fixed size columns
  2406. /// __height__ | Holds height of each widget in row or zero for auto layouting
  2407. /// __columns__ | Number of widget inside row
  2408. */
  2409. NK_API void nk_layout_row(struct nk_context*, enum nk_layout_format, float height, int cols, const float* ratio);
  2410. /*/// #### nk_layout_row_template_begin
  2411. /// Begins the row template declaration
  2412. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2413. /// void nk_layout_row_template_begin(struct nk_context*, float row_height);
  2414. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2415. ///
  2416. /// Parameter | Description
  2417. /// ------------|-----------------------------------------------------------
  2418. /// __ctx__ | Must point to an previously initialized `nk_context` struct after call `nk_begin_xxx`
  2419. /// __height__ | Holds height of each widget in row or zero for auto layouting
  2420. */
  2421. NK_API void nk_layout_row_template_begin(struct nk_context*, float row_height);
  2422. /*/// #### nk_layout_row_template_push_dynamic
  2423. /// Adds a dynamic column that dynamically grows and can go to zero if not enough space
  2424. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2425. /// void nk_layout_row_template_push_dynamic(struct nk_context*);
  2426. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2427. ///
  2428. /// Parameter | Description
  2429. /// ------------|-----------------------------------------------------------
  2430. /// __ctx__ | Must point to an previously initialized `nk_context` struct after call `nk_begin_xxx`
  2431. /// __height__ | Holds height of each widget in row or zero for auto layouting
  2432. */
  2433. NK_API void nk_layout_row_template_push_dynamic(struct nk_context*);
  2434. /*/// #### nk_layout_row_template_push_variable
  2435. /// Adds a variable column that dynamically grows but does not shrink below specified pixel width
  2436. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2437. /// void nk_layout_row_template_push_variable(struct nk_context*, float min_width);
  2438. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2439. ///
  2440. /// Parameter | Description
  2441. /// ------------|-----------------------------------------------------------
  2442. /// __ctx__ | Must point to an previously initialized `nk_context` struct after call `nk_begin_xxx`
  2443. /// __width__ | Holds the minimum pixel width the next column must always be
  2444. */
  2445. NK_API void nk_layout_row_template_push_variable(struct nk_context*, float min_width);
  2446. /*/// #### nk_layout_row_template_push_static
  2447. /// Adds a static column that does not grow and will always have the same size
  2448. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2449. /// void nk_layout_row_template_push_static(struct nk_context*, float width);
  2450. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2451. ///
  2452. /// Parameter | Description
  2453. /// ------------|-----------------------------------------------------------
  2454. /// __ctx__ | Must point to an previously initialized `nk_context` struct after call `nk_begin_xxx`
  2455. /// __width__ | Holds the absolute pixel width value the next column must be
  2456. */
  2457. NK_API void nk_layout_row_template_push_static(struct nk_context*, float width);
  2458. /*/// #### nk_layout_row_template_end
  2459. /// Marks the end of the row template
  2460. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2461. /// void nk_layout_row_template_end(struct nk_context*);
  2462. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2463. ///
  2464. /// Parameter | Description
  2465. /// ------------|-----------------------------------------------------------
  2466. /// __ctx__ | Must point to an previously initialized `nk_context` struct after call `nk_begin_xxx`
  2467. */
  2468. NK_API void nk_layout_row_template_end(struct nk_context*);
  2469. /*/// #### nk_layout_space_begin
  2470. /// Begins a new layouting space that allows to specify each widgets position and size.
  2471. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2472. /// void nk_layout_space_begin(struct nk_context*, enum nk_layout_format, float height, int widget_count);
  2473. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2474. ///
  2475. /// Parameter | Description
  2476. /// ------------|-----------------------------------------------------------
  2477. /// __ctx__ | Must point to an previously initialized `nk_context` struct after call `nk_begin_xxx`
  2478. /// __fmt__ | Either `NK_DYNAMIC` for window ratio or `NK_STATIC` for fixed size columns
  2479. /// __height__ | Holds height of each widget in row or zero for auto layouting
  2480. /// __columns__ | Number of widgets inside row
  2481. */
  2482. NK_API void nk_layout_space_begin(struct nk_context*, enum nk_layout_format, float height, int widget_count);
  2483. /*/// #### nk_layout_space_push
  2484. /// Pushes position and size of the next widget in own coordinate space either as pixel or ratio
  2485. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2486. /// void nk_layout_space_push(struct nk_context *ctx, struct nk_rect bounds);
  2487. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2488. ///
  2489. /// Parameter | Description
  2490. /// ------------|-----------------------------------------------------------
  2491. /// __ctx__ | Must point to an previously initialized `nk_context` struct after call `nk_layout_space_begin`
  2492. /// __bounds__ | Position and size in laoyut space local coordinates
  2493. */
  2494. NK_API void nk_layout_space_push(struct nk_context*, struct nk_rect bounds);
  2495. /*/// #### nk_layout_space_end
  2496. /// Marks the end of the layout space
  2497. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2498. /// void nk_layout_space_end(struct nk_context*);
  2499. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2500. ///
  2501. /// Parameter | Description
  2502. /// ------------|-----------------------------------------------------------
  2503. /// __ctx__ | Must point to an previously initialized `nk_context` struct after call `nk_layout_space_begin`
  2504. */
  2505. NK_API void nk_layout_space_end(struct nk_context*);
  2506. /*/// #### nk_layout_space_bounds
  2507. /// Utility function to calculate total space allocated for `nk_layout_space`
  2508. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2509. /// struct nk_rect nk_layout_space_bounds(struct nk_context*);
  2510. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2511. ///
  2512. /// Parameter | Description
  2513. /// ------------|-----------------------------------------------------------
  2514. /// __ctx__ | Must point to an previously initialized `nk_context` struct after call `nk_layout_space_begin`
  2515. ///
  2516. /// Returns `nk_rect` holding the total space allocated
  2517. */
  2518. NK_API struct nk_rect nk_layout_space_bounds(struct nk_context*);
  2519. /*/// #### nk_layout_space_to_screen
  2520. /// Converts vector from nk_layout_space coordinate space into screen space
  2521. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2522. /// struct nk_vec2 nk_layout_space_to_screen(struct nk_context*, struct nk_vec2);
  2523. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2524. ///
  2525. /// Parameter | Description
  2526. /// ------------|-----------------------------------------------------------
  2527. /// __ctx__ | Must point to an previously initialized `nk_context` struct after call `nk_layout_space_begin`
  2528. /// __vec__ | Position to convert from layout space into screen coordinate space
  2529. ///
  2530. /// Returns transformed `nk_vec2` in screen space coordinates
  2531. */
  2532. NK_API struct nk_vec2 nk_layout_space_to_screen(struct nk_context*, struct nk_vec2);
  2533. /*/// #### nk_layout_space_to_local
  2534. /// Converts vector from layout space into screen space
  2535. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2536. /// struct nk_vec2 nk_layout_space_to_local(struct nk_context*, struct nk_vec2);
  2537. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2538. ///
  2539. /// Parameter | Description
  2540. /// ------------|-----------------------------------------------------------
  2541. /// __ctx__ | Must point to an previously initialized `nk_context` struct after call `nk_layout_space_begin`
  2542. /// __vec__ | Position to convert from screen space into layout coordinate space
  2543. ///
  2544. /// Returns transformed `nk_vec2` in layout space coordinates
  2545. */
  2546. NK_API struct nk_vec2 nk_layout_space_to_local(struct nk_context*, struct nk_vec2);
  2547. /*/// #### nk_layout_space_rect_to_screen
  2548. /// Converts rectangle from screen space into layout space
  2549. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2550. /// struct nk_rect nk_layout_space_rect_to_screen(struct nk_context*, struct nk_rect);
  2551. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2552. ///
  2553. /// Parameter | Description
  2554. /// ------------|-----------------------------------------------------------
  2555. /// __ctx__ | Must point to an previously initialized `nk_context` struct after call `nk_layout_space_begin`
  2556. /// __bounds__ | Rectangle to convert from layout space into screen space
  2557. ///
  2558. /// Returns transformed `nk_rect` in screen space coordinates
  2559. */
  2560. NK_API struct nk_rect nk_layout_space_rect_to_screen(struct nk_context*, struct nk_rect);
  2561. /*/// #### nk_layout_space_rect_to_local
  2562. /// Converts rectangle from layout space into screen space
  2563. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2564. /// struct nk_rect nk_layout_space_rect_to_local(struct nk_context*, struct nk_rect);
  2565. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2566. ///
  2567. /// Parameter | Description
  2568. /// ------------|-----------------------------------------------------------
  2569. /// __ctx__ | Must point to an previously initialized `nk_context` struct after call `nk_layout_space_begin`
  2570. /// __bounds__ | Rectangle to convert from layout space into screen space
  2571. ///
  2572. /// Returns transformed `nk_rect` in layout space coordinates
  2573. */
  2574. NK_API struct nk_rect nk_layout_space_rect_to_local(struct nk_context*, struct nk_rect);
  2575. /*/// #### nk_spacer
  2576. /// Spacer is a dummy widget that consumes space as usual but doesn't draw anything
  2577. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2578. /// void nk_spacer(struct nk_context* );
  2579. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2580. ///
  2581. /// Parameter | Description
  2582. /// ------------|-----------------------------------------------------------
  2583. /// __ctx__ | Must point to an previously initialized `nk_context` struct after call `nk_layout_space_begin`
  2584. ///
  2585. */
  2586. NK_API void nk_spacer(struct nk_context*);
  2587. /* =============================================================================
  2588. *
  2589. * GROUP
  2590. *
  2591. * =============================================================================
  2592. /// ### Groups
  2593. /// Groups are basically windows inside windows. They allow to subdivide space
  2594. /// in a window to layout widgets as a group. Almost all more complex widget
  2595. /// layouting requirements can be solved using groups and basic layouting
  2596. /// fuctionality. Groups just like windows are identified by an unique name and
  2597. /// internally keep track of scrollbar offsets by default. However additional
  2598. /// versions are provided to directly manage the scrollbar.
  2599. ///
  2600. /// #### Usage
  2601. /// To create a group you have to call one of the three `nk_group_begin_xxx`
  2602. /// functions to start group declarations and `nk_group_end` at the end. Furthermore it
  2603. /// is required to check the return value of `nk_group_begin_xxx` and only process
  2604. /// widgets inside the window if the value is not 0.
  2605. /// Nesting groups is possible and even encouraged since many layouting schemes
  2606. /// can only be achieved by nesting. Groups, unlike windows, need `nk_group_end`
  2607. /// to be only called if the corresponding `nk_group_begin_xxx` call does not return 0:
  2608. ///
  2609. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2610. /// if (nk_group_begin_xxx(ctx, ...) {
  2611. /// // [... widgets ...]
  2612. /// nk_group_end(ctx);
  2613. /// }
  2614. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2615. ///
  2616. /// In the grand concept groups can be called after starting a window
  2617. /// with `nk_begin_xxx` and before calling `nk_end`:
  2618. ///
  2619. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2620. /// struct nk_context ctx;
  2621. /// nk_init_xxx(&ctx, ...);
  2622. /// while (1) {
  2623. /// // Input
  2624. /// Event evt;
  2625. /// nk_input_begin(&ctx);
  2626. /// while (GetEvent(&evt)) {
  2627. /// if (evt.type == MOUSE_MOVE)
  2628. /// nk_input_motion(&ctx, evt.motion.x, evt.motion.y);
  2629. /// else if (evt.type == [...]) {
  2630. /// nk_input_xxx(...);
  2631. /// }
  2632. /// }
  2633. /// nk_input_end(&ctx);
  2634. /// //
  2635. /// // Window
  2636. /// if (nk_begin_xxx(...) {
  2637. /// // [...widgets...]
  2638. /// nk_layout_row_dynamic(...);
  2639. /// if (nk_group_begin_xxx(ctx, ...) {
  2640. /// //[... widgets ...]
  2641. /// nk_group_end(ctx);
  2642. /// }
  2643. /// }
  2644. /// nk_end(ctx);
  2645. /// //
  2646. /// // Draw
  2647. /// const struct nk_command *cmd = 0;
  2648. /// nk_foreach(cmd, &ctx) {
  2649. /// switch (cmd->type) {
  2650. /// case NK_COMMAND_LINE:
  2651. /// your_draw_line_function(...)
  2652. /// break;
  2653. /// case NK_COMMAND_RECT
  2654. /// your_draw_rect_function(...)
  2655. /// break;
  2656. /// case ...:
  2657. /// // [...]
  2658. /// }
  2659. /// nk_clear(&ctx);
  2660. /// }
  2661. /// nk_free(&ctx);
  2662. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2663. /// #### Reference
  2664. /// Function | Description
  2665. /// --------------------------------|-------------------------------------------
  2666. /// nk_group_begin | Start a new group with internal scrollbar handling
  2667. /// nk_group_begin_titled | Start a new group with separated name and title and internal scrollbar handling
  2668. /// nk_group_end | Ends a group. Should only be called if nk_group_begin returned non-zero
  2669. /// nk_group_scrolled_offset_begin | Start a new group with manual separated handling of scrollbar x- and y-offset
  2670. /// nk_group_scrolled_begin | Start a new group with manual scrollbar handling
  2671. /// nk_group_scrolled_end | Ends a group with manual scrollbar handling. Should only be called if nk_group_begin returned non-zero
  2672. /// nk_group_get_scroll | Gets the scroll offset for the given group
  2673. /// nk_group_set_scroll | Sets the scroll offset for the given group
  2674. */
  2675. /*/// #### nk_group_begin
  2676. /// Starts a new widget group. Requires a previous layouting function to specify a pos/size.
  2677. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2678. /// nk_bool nk_group_begin(struct nk_context*, const char *title, nk_flags);
  2679. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2680. ///
  2681. /// Parameter | Description
  2682. /// ------------|-----------------------------------------------------------
  2683. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  2684. /// __title__ | Must be an unique identifier for this group that is also used for the group header
  2685. /// __flags__ | Window flags defined in the nk_panel_flags section with a number of different group behaviors
  2686. ///
  2687. /// Returns `true(1)` if visible and fillable with widgets or `false(0)` otherwise
  2688. */
  2689. NK_API nk_bool nk_group_begin(struct nk_context*, const char* title, nk_flags);
  2690. /*/// #### nk_group_begin_titled
  2691. /// Starts a new widget group. Requires a previous layouting function to specify a pos/size.
  2692. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2693. /// nk_bool nk_group_begin_titled(struct nk_context*, const char *name, const char *title, nk_flags);
  2694. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2695. ///
  2696. /// Parameter | Description
  2697. /// ------------|-----------------------------------------------------------
  2698. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  2699. /// __id__ | Must be an unique identifier for this group
  2700. /// __title__ | Group header title
  2701. /// __flags__ | Window flags defined in the nk_panel_flags section with a number of different group behaviors
  2702. ///
  2703. /// Returns `true(1)` if visible and fillable with widgets or `false(0)` otherwise
  2704. */
  2705. NK_API nk_bool nk_group_begin_titled(struct nk_context*, const char* name, const char* title, nk_flags);
  2706. /*/// #### nk_group_end
  2707. /// Ends a widget group
  2708. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2709. /// void nk_group_end(struct nk_context*);
  2710. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2711. ///
  2712. /// Parameter | Description
  2713. /// ------------|-----------------------------------------------------------
  2714. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  2715. */
  2716. NK_API void nk_group_end(struct nk_context*);
  2717. /*/// #### nk_group_scrolled_offset_begin
  2718. /// starts a new widget group. requires a previous layouting function to specify
  2719. /// a size. Does not keep track of scrollbar.
  2720. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2721. /// nk_bool nk_group_scrolled_offset_begin(struct nk_context*, nk_uint *x_offset, nk_uint *y_offset, const char *title, nk_flags flags);
  2722. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2723. ///
  2724. /// Parameter | Description
  2725. /// ------------|-----------------------------------------------------------
  2726. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  2727. /// __x_offset__| Scrollbar x-offset to offset all widgets inside the group horizontally.
  2728. /// __y_offset__| Scrollbar y-offset to offset all widgets inside the group vertically
  2729. /// __title__ | Window unique group title used to both identify and display in the group header
  2730. /// __flags__ | Window flags from the nk_panel_flags section
  2731. ///
  2732. /// Returns `true(1)` if visible and fillable with widgets or `false(0)` otherwise
  2733. */
  2734. NK_API nk_bool nk_group_scrolled_offset_begin(struct nk_context*, nk_uint* x_offset, nk_uint* y_offset, const char* title, nk_flags flags);
  2735. /*/// #### nk_group_scrolled_begin
  2736. /// Starts a new widget group. requires a previous
  2737. /// layouting function to specify a size. Does not keep track of scrollbar.
  2738. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2739. /// nk_bool nk_group_scrolled_begin(struct nk_context*, struct nk_scroll *off, const char *title, nk_flags);
  2740. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2741. ///
  2742. /// Parameter | Description
  2743. /// ------------|-----------------------------------------------------------
  2744. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  2745. /// __off__ | Both x- and y- scroll offset. Allows for manual scrollbar control
  2746. /// __title__ | Window unique group title used to both identify and display in the group header
  2747. /// __flags__ | Window flags from nk_panel_flags section
  2748. ///
  2749. /// Returns `true(1)` if visible and fillable with widgets or `false(0)` otherwise
  2750. */
  2751. NK_API nk_bool nk_group_scrolled_begin(struct nk_context*, struct nk_scroll* off, const char* title, nk_flags);
  2752. /*/// #### nk_group_scrolled_end
  2753. /// Ends a widget group after calling nk_group_scrolled_offset_begin or nk_group_scrolled_begin.
  2754. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2755. /// void nk_group_scrolled_end(struct nk_context*);
  2756. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2757. ///
  2758. /// Parameter | Description
  2759. /// ------------|-----------------------------------------------------------
  2760. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  2761. */
  2762. NK_API void nk_group_scrolled_end(struct nk_context*);
  2763. /*/// #### nk_group_get_scroll
  2764. /// Gets the scroll position of the given group.
  2765. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2766. /// void nk_group_get_scroll(struct nk_context*, const char *id, nk_uint *x_offset, nk_uint *y_offset);
  2767. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2768. ///
  2769. /// Parameter | Description
  2770. /// -------------|-----------------------------------------------------------
  2771. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  2772. /// __id__ | The id of the group to get the scroll position of
  2773. /// __x_offset__ | A pointer to the x offset output (or NULL to ignore)
  2774. /// __y_offset__ | A pointer to the y offset output (or NULL to ignore)
  2775. */
  2776. NK_API void nk_group_get_scroll(struct nk_context*, const char* id, nk_uint* x_offset, nk_uint* y_offset);
  2777. /*/// #### nk_group_set_scroll
  2778. /// Sets the scroll position of the given group.
  2779. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2780. /// void nk_group_set_scroll(struct nk_context*, const char *id, nk_uint x_offset, nk_uint y_offset);
  2781. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2782. ///
  2783. /// Parameter | Description
  2784. /// -------------|-----------------------------------------------------------
  2785. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  2786. /// __id__ | The id of the group to scroll
  2787. /// __x_offset__ | The x offset to scroll to
  2788. /// __y_offset__ | The y offset to scroll to
  2789. */
  2790. NK_API void nk_group_set_scroll(struct nk_context*, const char* id, nk_uint x_offset, nk_uint y_offset);
  2791. /* =============================================================================
  2792. *
  2793. * TREE
  2794. *
  2795. * =============================================================================
  2796. /// ### Tree
  2797. /// Trees represent two different concept. First the concept of a collapsible
  2798. /// UI section that can be either in a hidden or visible state. They allow the UI
  2799. /// user to selectively minimize the current set of visible UI to comprehend.
  2800. /// The second concept are tree widgets for visual UI representation of trees.<br /><br />
  2801. ///
  2802. /// Trees thereby can be nested for tree representations and multiple nested
  2803. /// collapsible UI sections. All trees are started by calling of the
  2804. /// `nk_tree_xxx_push_tree` functions and ended by calling one of the
  2805. /// `nk_tree_xxx_pop_xxx()` functions. Each starting functions takes a title label
  2806. /// and optionally an image to be displayed and the initial collapse state from
  2807. /// the nk_collapse_states section.<br /><br />
  2808. ///
  2809. /// The runtime state of the tree is either stored outside the library by the caller
  2810. /// or inside which requires a unique ID. The unique ID can either be generated
  2811. /// automatically from `__FILE__` and `__LINE__` with function `nk_tree_push`,
  2812. /// by `__FILE__` and a user provided ID generated for example by loop index with
  2813. /// function `nk_tree_push_id` or completely provided from outside by user with
  2814. /// function `nk_tree_push_hashed`.
  2815. ///
  2816. /// #### Usage
  2817. /// To create a tree you have to call one of the seven `nk_tree_xxx_push_xxx`
  2818. /// functions to start a collapsible UI section and `nk_tree_xxx_pop` to mark the
  2819. /// end.
  2820. /// Each starting function will either return `false(0)` if the tree is collapsed
  2821. /// or hidden and therefore does not need to be filled with content or `true(1)`
  2822. /// if visible and required to be filled.
  2823. ///
  2824. /// !!! Note
  2825. /// The tree header does not require and layouting function and instead
  2826. /// calculates a auto height based on the currently used font size
  2827. ///
  2828. /// The tree ending functions only need to be called if the tree content is
  2829. /// actually visible. So make sure the tree push function is guarded by `if`
  2830. /// and the pop call is only taken if the tree is visible.
  2831. ///
  2832. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2833. /// if (nk_tree_push(ctx, NK_TREE_TAB, "Tree", NK_MINIMIZED)) {
  2834. /// nk_layout_row_dynamic(...);
  2835. /// nk_widget(...);
  2836. /// nk_tree_pop(ctx);
  2837. /// }
  2838. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2839. ///
  2840. /// #### Reference
  2841. /// Function | Description
  2842. /// ----------------------------|-------------------------------------------
  2843. /// nk_tree_push | Start a collapsible UI section with internal state management
  2844. /// nk_tree_push_id | Start a collapsible UI section with internal state management callable in a look
  2845. /// nk_tree_push_hashed | Start a collapsible UI section with internal state management with full control over internal unique ID use to store state
  2846. /// nk_tree_image_push | Start a collapsible UI section with image and label header
  2847. /// nk_tree_image_push_id | Start a collapsible UI section with image and label header and internal state management callable in a look
  2848. /// nk_tree_image_push_hashed | Start a collapsible UI section with image and label header and internal state management with full control over internal unique ID use to store state
  2849. /// nk_tree_pop | Ends a collapsible UI section
  2850. //
  2851. /// nk_tree_state_push | Start a collapsible UI section with external state management
  2852. /// nk_tree_state_image_push | Start a collapsible UI section with image and label header and external state management
  2853. /// nk_tree_state_pop | Ends a collapsabale UI section
  2854. ///
  2855. /// #### nk_tree_type
  2856. /// Flag | Description
  2857. /// ----------------|----------------------------------------
  2858. /// NK_TREE_NODE | Highlighted tree header to mark a collapsible UI section
  2859. /// NK_TREE_TAB | Non-highlighted tree header closer to tree representations
  2860. */
  2861. /*/// #### nk_tree_push
  2862. /// Starts a collapsible UI section with internal state management
  2863. /// !!! WARNING
  2864. /// To keep track of the runtime tree collapsible state this function uses
  2865. /// defines `__FILE__` and `__LINE__` to generate a unique ID. If you want
  2866. /// to call this function in a loop please use `nk_tree_push_id` or
  2867. /// `nk_tree_push_hashed` instead.
  2868. ///
  2869. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2870. /// #define nk_tree_push(ctx, type, title, state)
  2871. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2872. ///
  2873. /// Parameter | Description
  2874. /// ------------|-----------------------------------------------------------
  2875. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  2876. /// __type__ | Value from the nk_tree_type section to visually mark a tree node header as either a collapseable UI section or tree node
  2877. /// __title__ | Label printed in the tree header
  2878. /// __state__ | Initial tree state value out of nk_collapse_states
  2879. ///
  2880. /// Returns `true(1)` if visible and fillable with widgets or `false(0)` otherwise
  2881. */
  2882. #define nk_tree_push(ctx, type, title, state) nk_tree_push_hashed(ctx, type, title, state, NK_FILE_LINE,nk_strlen(NK_FILE_LINE),__LINE__)
  2883. /*/// #### nk_tree_push_id
  2884. /// Starts a collapsible UI section with internal state management callable in a look
  2885. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2886. /// #define nk_tree_push_id(ctx, type, title, state, id)
  2887. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2888. ///
  2889. /// Parameter | Description
  2890. /// ------------|-----------------------------------------------------------
  2891. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  2892. /// __type__ | Value from the nk_tree_type section to visually mark a tree node header as either a collapseable UI section or tree node
  2893. /// __title__ | Label printed in the tree header
  2894. /// __state__ | Initial tree state value out of nk_collapse_states
  2895. /// __id__ | Loop counter index if this function is called in a loop
  2896. ///
  2897. /// Returns `true(1)` if visible and fillable with widgets or `false(0)` otherwise
  2898. */
  2899. #define nk_tree_push_id(ctx, type, title, state, id) nk_tree_push_hashed(ctx, type, title, state, NK_FILE_LINE,nk_strlen(NK_FILE_LINE),id)
  2900. /*/// #### nk_tree_push_hashed
  2901. /// Start a collapsible UI section with internal state management with full
  2902. /// control over internal unique ID used to store state
  2903. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2904. /// nk_bool nk_tree_push_hashed(struct nk_context*, enum nk_tree_type, const char *title, enum nk_collapse_states initial_state, const char *hash, int len,int seed);
  2905. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2906. ///
  2907. /// Parameter | Description
  2908. /// ------------|-----------------------------------------------------------
  2909. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  2910. /// __type__ | Value from the nk_tree_type section to visually mark a tree node header as either a collapseable UI section or tree node
  2911. /// __title__ | Label printed in the tree header
  2912. /// __state__ | Initial tree state value out of nk_collapse_states
  2913. /// __hash__ | Memory block or string to generate the ID from
  2914. /// __len__ | Size of passed memory block or string in __hash__
  2915. /// __seed__ | Seeding value if this function is called in a loop or default to `0`
  2916. ///
  2917. /// Returns `true(1)` if visible and fillable with widgets or `false(0)` otherwise
  2918. */
  2919. NK_API nk_bool nk_tree_push_hashed(struct nk_context*, enum nk_tree_type, const char* title, enum nk_collapse_states initial_state, const char* hash, int len, int seed);
  2920. /*/// #### nk_tree_image_push
  2921. /// Start a collapsible UI section with image and label header
  2922. /// !!! WARNING
  2923. /// To keep track of the runtime tree collapsible state this function uses
  2924. /// defines `__FILE__` and `__LINE__` to generate a unique ID. If you want
  2925. /// to call this function in a loop please use `nk_tree_image_push_id` or
  2926. /// `nk_tree_image_push_hashed` instead.
  2927. ///
  2928. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2929. /// #define nk_tree_image_push(ctx, type, img, title, state)
  2930. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2931. //
  2932. /// Parameter | Description
  2933. /// ------------|-----------------------------------------------------------
  2934. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  2935. /// __type__ | Value from the nk_tree_type section to visually mark a tree node header as either a collapseable UI section or tree node
  2936. /// __img__ | Image to display inside the header on the left of the label
  2937. /// __title__ | Label printed in the tree header
  2938. /// __state__ | Initial tree state value out of nk_collapse_states
  2939. ///
  2940. /// Returns `true(1)` if visible and fillable with widgets or `false(0)` otherwise
  2941. */
  2942. #define nk_tree_image_push(ctx, type, img, title, state) nk_tree_image_push_hashed(ctx, type, img, title, state, NK_FILE_LINE,nk_strlen(NK_FILE_LINE),__LINE__)
  2943. /*/// #### nk_tree_image_push_id
  2944. /// Start a collapsible UI section with image and label header and internal state
  2945. /// management callable in a look
  2946. ///
  2947. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2948. /// #define nk_tree_image_push_id(ctx, type, img, title, state, id)
  2949. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2950. ///
  2951. /// Parameter | Description
  2952. /// ------------|-----------------------------------------------------------
  2953. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  2954. /// __type__ | Value from the nk_tree_type section to visually mark a tree node header as either a collapseable UI section or tree node
  2955. /// __img__ | Image to display inside the header on the left of the label
  2956. /// __title__ | Label printed in the tree header
  2957. /// __state__ | Initial tree state value out of nk_collapse_states
  2958. /// __id__ | Loop counter index if this function is called in a loop
  2959. ///
  2960. /// Returns `true(1)` if visible and fillable with widgets or `false(0)` otherwise
  2961. */
  2962. #define nk_tree_image_push_id(ctx, type, img, title, state, id) nk_tree_image_push_hashed(ctx, type, img, title, state, NK_FILE_LINE,nk_strlen(NK_FILE_LINE),id)
  2963. /*/// #### nk_tree_image_push_hashed
  2964. /// Start a collapsible UI section with internal state management with full
  2965. /// control over internal unique ID used to store state
  2966. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2967. /// nk_bool nk_tree_image_push_hashed(struct nk_context*, enum nk_tree_type, struct nk_image, const char *title, enum nk_collapse_states initial_state, const char *hash, int len,int seed);
  2968. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2969. ///
  2970. /// Parameter | Description
  2971. /// ------------|-----------------------------------------------------------
  2972. /// __ctx__ | Must point to an previously initialized `nk_context` struct
  2973. /// __type__ | Value from the nk_tree_type section to visually mark a tree node header as either a collapseable UI section or tree node
  2974. /// __img__ | Image to display inside the header on the left of the label
  2975. /// __title__ | Label printed in the tree header
  2976. /// __state__ | Initial tree state value out of nk_collapse_states
  2977. /// __hash__ | Memory block or string to generate the ID from
  2978. /// __len__ | Size of passed memory block or string in __hash__
  2979. /// __seed__ | Seeding value if this function is called in a loop or default to `0`
  2980. ///
  2981. /// Returns `true(1)` if visible and fillable with widgets or `false(0)` otherwise
  2982. */
  2983. NK_API nk_bool nk_tree_image_push_hashed(struct nk_context*, enum nk_tree_type, struct nk_image, const char* title, enum nk_collapse_states initial_state, const char* hash, int len, int seed);
  2984. /*/// #### nk_tree_pop
  2985. /// Ends a collapsabale UI section
  2986. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2987. /// void nk_tree_pop(struct nk_context*);
  2988. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2989. ///
  2990. /// Parameter | Description
  2991. /// ------------|-----------------------------------------------------------
  2992. /// __ctx__ | Must point to an previously initialized `nk_context` struct after calling `nk_tree_xxx_push_xxx`
  2993. */
  2994. NK_API void nk_tree_pop(struct nk_context*);
  2995. /*/// #### nk_tree_state_push
  2996. /// Start a collapsible UI section with external state management
  2997. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  2998. /// nk_bool nk_tree_state_push(struct nk_context*, enum nk_tree_type, const char *title, enum nk_collapse_states *state);
  2999. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  3000. ///
  3001. /// Parameter | Description
  3002. /// ------------|-----------------------------------------------------------
  3003. /// __ctx__ | Must point to an previously initialized `nk_context` struct after calling `nk_tree_xxx_push_xxx`
  3004. /// __type__ | Value from the nk_tree_type section to visually mark a tree node header as either a collapseable UI section or tree node
  3005. /// __title__ | Label printed in the tree header
  3006. /// __state__ | Persistent state to update
  3007. ///
  3008. /// Returns `true(1)` if visible and fillable with widgets or `false(0)` otherwise
  3009. */
  3010. NK_API nk_bool nk_tree_state_push(struct nk_context*, enum nk_tree_type, const char* title, enum nk_collapse_states* state);
  3011. /*/// #### nk_tree_state_image_push
  3012. /// Start a collapsible UI section with image and label header and external state management
  3013. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  3014. /// nk_bool nk_tree_state_image_push(struct nk_context*, enum nk_tree_type, struct nk_image, const char *title, enum nk_collapse_states *state);
  3015. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  3016. ///
  3017. /// Parameter | Description
  3018. /// ------------|-----------------------------------------------------------
  3019. /// __ctx__ | Must point to an previously initialized `nk_context` struct after calling `nk_tree_xxx_push_xxx`
  3020. /// __img__ | Image to display inside the header on the left of the label
  3021. /// __type__ | Value from the nk_tree_type section to visually mark a tree node header as either a collapseable UI section or tree node
  3022. /// __title__ | Label printed in the tree header
  3023. /// __state__ | Persistent state to update
  3024. ///
  3025. /// Returns `true(1)` if visible and fillable with widgets or `false(0)` otherwise
  3026. */
  3027. NK_API nk_bool nk_tree_state_image_push(struct nk_context*, enum nk_tree_type, struct nk_image, const char* title, enum nk_collapse_states* state);
  3028. /*/// #### nk_tree_state_pop
  3029. /// Ends a collapsabale UI section
  3030. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  3031. /// void nk_tree_state_pop(struct nk_context*);
  3032. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  3033. ///
  3034. /// Parameter | Description
  3035. /// ------------|-----------------------------------------------------------
  3036. /// __ctx__ | Must point to an previously initialized `nk_context` struct after calling `nk_tree_xxx_push_xxx`
  3037. */
  3038. NK_API void nk_tree_state_pop(struct nk_context*);
  3039. #define nk_tree_element_push(ctx, type, title, state, sel) nk_tree_element_push_hashed(ctx, type, title, state, sel, NK_FILE_LINE,nk_strlen(NK_FILE_LINE),__LINE__)
  3040. #define nk_tree_element_push_id(ctx, type, title, state, sel, id) nk_tree_element_push_hashed(ctx, type, title, state, sel, NK_FILE_LINE,nk_strlen(NK_FILE_LINE),id)
  3041. NK_API nk_bool nk_tree_element_push_hashed(struct nk_context*, enum nk_tree_type, const char* title, enum nk_collapse_states initial_state, nk_bool* selected, const char* hash, int len, int seed);
  3042. NK_API nk_bool nk_tree_element_image_push_hashed(struct nk_context*, enum nk_tree_type, struct nk_image, const char* title, enum nk_collapse_states initial_state, nk_bool* selected, const char* hash, int len, int seed);
  3043. NK_API void nk_tree_element_pop(struct nk_context*);
  3044. /* =============================================================================
  3045. *
  3046. * LIST VIEW
  3047. *
  3048. * ============================================================================= */
  3049. struct nk_list_view {
  3050. /* public: */
  3051. int begin, end, count;
  3052. /* private: */
  3053. int total_height;
  3054. struct nk_context* ctx;
  3055. nk_uint* scroll_pointer;
  3056. nk_uint scroll_value;
  3057. };
  3058. NK_API nk_bool nk_list_view_begin(struct nk_context*, struct nk_list_view* out, const char* id, nk_flags, int row_height, int row_count);
  3059. NK_API void nk_list_view_end(struct nk_list_view*);
  3060. /* =============================================================================
  3061. *
  3062. * WIDGET
  3063. *
  3064. * ============================================================================= */
  3065. enum nk_widget_layout_states {
  3066. NK_WIDGET_INVALID, /* The widget cannot be seen and is completely out of view */
  3067. NK_WIDGET_VALID, /* The widget is completely inside the window and can be updated and drawn */
  3068. NK_WIDGET_ROM /* The widget is partially visible and cannot be updated */
  3069. };
  3070. enum nk_widget_states {
  3071. NK_WIDGET_STATE_MODIFIED = NK_FLAG(1),
  3072. NK_WIDGET_STATE_INACTIVE = NK_FLAG(2), /* widget is neither active nor hovered */
  3073. NK_WIDGET_STATE_ENTERED = NK_FLAG(3), /* widget has been hovered on the current frame */
  3074. NK_WIDGET_STATE_HOVER = NK_FLAG(4), /* widget is being hovered */
  3075. NK_WIDGET_STATE_ACTIVED = NK_FLAG(5),/* widget is currently activated */
  3076. NK_WIDGET_STATE_LEFT = NK_FLAG(6), /* widget is from this frame on not hovered anymore */
  3077. NK_WIDGET_STATE_HOVERED = NK_WIDGET_STATE_HOVER | NK_WIDGET_STATE_MODIFIED, /* widget is being hovered */
  3078. NK_WIDGET_STATE_ACTIVE = NK_WIDGET_STATE_ACTIVED | NK_WIDGET_STATE_MODIFIED /* widget is currently activated */
  3079. };
  3080. NK_API enum nk_widget_layout_states nk_widget(struct nk_rect*, const struct nk_context*);
  3081. NK_API enum nk_widget_layout_states nk_widget_fitting(struct nk_rect*, struct nk_context*, struct nk_vec2);
  3082. NK_API struct nk_rect nk_widget_bounds(struct nk_context*);
  3083. NK_API struct nk_vec2 nk_widget_position(struct nk_context*);
  3084. NK_API struct nk_vec2 nk_widget_size(struct nk_context*);
  3085. NK_API float nk_widget_width(struct nk_context*);
  3086. NK_API float nk_widget_height(struct nk_context*);
  3087. NK_API nk_bool nk_widget_is_hovered(struct nk_context*);
  3088. NK_API nk_bool nk_widget_is_mouse_clicked(struct nk_context*, enum nk_buttons);
  3089. NK_API nk_bool nk_widget_has_mouse_click_down(struct nk_context*, enum nk_buttons, nk_bool down);
  3090. NK_API void nk_spacing(struct nk_context*, int cols);
  3091. /* =============================================================================
  3092. *
  3093. * TEXT
  3094. *
  3095. * ============================================================================= */
  3096. enum nk_text_align {
  3097. NK_TEXT_ALIGN_LEFT = 0x01,
  3098. NK_TEXT_ALIGN_CENTERED = 0x02,
  3099. NK_TEXT_ALIGN_RIGHT = 0x04,
  3100. NK_TEXT_ALIGN_TOP = 0x08,
  3101. NK_TEXT_ALIGN_MIDDLE = 0x10,
  3102. NK_TEXT_ALIGN_BOTTOM = 0x20
  3103. };
  3104. enum nk_text_alignment {
  3105. NK_TEXT_LEFT = NK_TEXT_ALIGN_MIDDLE | NK_TEXT_ALIGN_LEFT,
  3106. NK_TEXT_CENTERED = NK_TEXT_ALIGN_MIDDLE | NK_TEXT_ALIGN_CENTERED,
  3107. NK_TEXT_RIGHT = NK_TEXT_ALIGN_MIDDLE | NK_TEXT_ALIGN_RIGHT
  3108. };
  3109. NK_API void nk_text(struct nk_context*, const char*, int, nk_flags);
  3110. NK_API void nk_text_colored(struct nk_context*, const char*, int, nk_flags, struct nk_color);
  3111. NK_API void nk_text_wrap(struct nk_context*, const char*, int);
  3112. NK_API void nk_text_wrap_colored(struct nk_context*, const char*, int, struct nk_color);
  3113. NK_API void nk_label(struct nk_context*, const char*, nk_flags align);
  3114. NK_API void nk_label_colored(struct nk_context*, const char*, nk_flags align, struct nk_color);
  3115. NK_API void nk_label_wrap(struct nk_context*, const char*);
  3116. NK_API void nk_label_colored_wrap(struct nk_context*, const char*, struct nk_color);
  3117. NK_API void nk_image(struct nk_context*, struct nk_image);
  3118. NK_API void nk_image_color(struct nk_context*, struct nk_image, struct nk_color);
  3119. #ifdef NK_INCLUDE_STANDARD_VARARGS
  3120. NK_API void nk_labelf(struct nk_context*, nk_flags, NK_PRINTF_FORMAT_STRING const char*, ...) NK_PRINTF_VARARG_FUNC(3);
  3121. NK_API void nk_labelf_colored(struct nk_context*, nk_flags, struct nk_color, NK_PRINTF_FORMAT_STRING const char*, ...) NK_PRINTF_VARARG_FUNC(4);
  3122. NK_API void nk_labelf_wrap(struct nk_context*, NK_PRINTF_FORMAT_STRING const char*, ...) NK_PRINTF_VARARG_FUNC(2);
  3123. NK_API void nk_labelf_colored_wrap(struct nk_context*, struct nk_color, NK_PRINTF_FORMAT_STRING const char*, ...) NK_PRINTF_VARARG_FUNC(3);
  3124. NK_API void nk_labelfv(struct nk_context*, nk_flags, NK_PRINTF_FORMAT_STRING const char*, va_list) NK_PRINTF_VALIST_FUNC(3);
  3125. NK_API void nk_labelfv_colored(struct nk_context*, nk_flags, struct nk_color, NK_PRINTF_FORMAT_STRING const char*, va_list) NK_PRINTF_VALIST_FUNC(4);
  3126. NK_API void nk_labelfv_wrap(struct nk_context*, NK_PRINTF_FORMAT_STRING const char*, va_list) NK_PRINTF_VALIST_FUNC(2);
  3127. NK_API void nk_labelfv_colored_wrap(struct nk_context*, struct nk_color, NK_PRINTF_FORMAT_STRING const char*, va_list) NK_PRINTF_VALIST_FUNC(3);
  3128. NK_API void nk_value_bool(struct nk_context*, const char* prefix, int);
  3129. NK_API void nk_value_int(struct nk_context*, const char* prefix, int);
  3130. NK_API void nk_value_uint(struct nk_context*, const char* prefix, unsigned int);
  3131. NK_API void nk_value_float(struct nk_context*, const char* prefix, float);
  3132. NK_API void nk_value_color_byte(struct nk_context*, const char* prefix, struct nk_color);
  3133. NK_API void nk_value_color_float(struct nk_context*, const char* prefix, struct nk_color);
  3134. NK_API void nk_value_color_hex(struct nk_context*, const char* prefix, struct nk_color);
  3135. #endif
  3136. /* =============================================================================
  3137. *
  3138. * BUTTON
  3139. *
  3140. * ============================================================================= */
  3141. NK_API nk_bool nk_button_text(struct nk_context*, const char* title, int len);
  3142. NK_API nk_bool nk_button_label(struct nk_context*, const char* title);
  3143. NK_API nk_bool nk_button_color(struct nk_context*, struct nk_color);
  3144. NK_API nk_bool nk_button_symbol(struct nk_context*, enum nk_symbol_type);
  3145. NK_API nk_bool nk_button_image(struct nk_context*, struct nk_image img);
  3146. NK_API nk_bool nk_button_symbol_label(struct nk_context*, enum nk_symbol_type, const char*, nk_flags text_alignment);
  3147. NK_API nk_bool nk_button_symbol_text(struct nk_context*, enum nk_symbol_type, const char*, int, nk_flags alignment);
  3148. NK_API nk_bool nk_button_image_label(struct nk_context*, struct nk_image img, const char*, nk_flags text_alignment);
  3149. NK_API nk_bool nk_button_image_text(struct nk_context*, struct nk_image img, const char*, int, nk_flags alignment);
  3150. NK_API nk_bool nk_button_text_styled(struct nk_context*, const struct nk_style_button*, const char* title, int len);
  3151. NK_API nk_bool nk_button_label_styled(struct nk_context*, const struct nk_style_button*, const char* title);
  3152. NK_API nk_bool nk_button_symbol_styled(struct nk_context*, const struct nk_style_button*, enum nk_symbol_type);
  3153. NK_API nk_bool nk_button_image_styled(struct nk_context*, const struct nk_style_button*, struct nk_image img);
  3154. NK_API nk_bool nk_button_symbol_text_styled(struct nk_context*, const struct nk_style_button*, enum nk_symbol_type, const char*, int, nk_flags alignment);
  3155. NK_API nk_bool nk_button_symbol_label_styled(struct nk_context* ctx, const struct nk_style_button* style, enum nk_symbol_type symbol, const char* title, nk_flags align);
  3156. NK_API nk_bool nk_button_image_label_styled(struct nk_context*, const struct nk_style_button*, struct nk_image img, const char*, nk_flags text_alignment);
  3157. NK_API nk_bool nk_button_image_text_styled(struct nk_context*, const struct nk_style_button*, struct nk_image img, const char*, int, nk_flags alignment);
  3158. NK_API void nk_button_set_behavior(struct nk_context*, enum nk_button_behavior);
  3159. NK_API nk_bool nk_button_push_behavior(struct nk_context*, enum nk_button_behavior);
  3160. NK_API nk_bool nk_button_pop_behavior(struct nk_context*);
  3161. /* =============================================================================
  3162. *
  3163. * CHECKBOX
  3164. *
  3165. * ============================================================================= */
  3166. NK_API nk_bool nk_check_label(struct nk_context*, const char*, nk_bool active);
  3167. NK_API nk_bool nk_check_text(struct nk_context*, const char*, int, nk_bool active);
  3168. NK_API unsigned nk_check_flags_label(struct nk_context*, const char*, unsigned int flags, unsigned int value);
  3169. NK_API unsigned nk_check_flags_text(struct nk_context*, const char*, int, unsigned int flags, unsigned int value);
  3170. NK_API nk_bool nk_checkbox_label(struct nk_context*, const char*, nk_bool* active);
  3171. NK_API nk_bool nk_checkbox_text(struct nk_context*, const char*, int, nk_bool* active);
  3172. NK_API nk_bool nk_checkbox_flags_label(struct nk_context*, const char*, unsigned int* flags, unsigned int value);
  3173. NK_API nk_bool nk_checkbox_flags_text(struct nk_context*, const char*, int, unsigned int* flags, unsigned int value);
  3174. /* =============================================================================
  3175. *
  3176. * RADIO BUTTON
  3177. *
  3178. * ============================================================================= */
  3179. NK_API nk_bool nk_radio_label(struct nk_context*, const char*, nk_bool* active);
  3180. NK_API nk_bool nk_radio_text(struct nk_context*, const char*, int, nk_bool* active);
  3181. NK_API nk_bool nk_option_label(struct nk_context*, const char*, nk_bool active);
  3182. NK_API nk_bool nk_option_text(struct nk_context*, const char*, int, nk_bool active);
  3183. /* =============================================================================
  3184. *
  3185. * SELECTABLE
  3186. *
  3187. * ============================================================================= */
  3188. NK_API nk_bool nk_selectable_label(struct nk_context*, const char*, nk_flags align, nk_bool* value);
  3189. NK_API nk_bool nk_selectable_text(struct nk_context*, const char*, int, nk_flags align, nk_bool* value);
  3190. NK_API nk_bool nk_selectable_image_label(struct nk_context*, struct nk_image, const char*, nk_flags align, nk_bool* value);
  3191. NK_API nk_bool nk_selectable_image_text(struct nk_context*, struct nk_image, const char*, int, nk_flags align, nk_bool* value);
  3192. NK_API nk_bool nk_selectable_symbol_label(struct nk_context*, enum nk_symbol_type, const char*, nk_flags align, nk_bool* value);
  3193. NK_API nk_bool nk_selectable_symbol_text(struct nk_context*, enum nk_symbol_type, const char*, int, nk_flags align, nk_bool* value);
  3194. NK_API nk_bool nk_select_label(struct nk_context*, const char*, nk_flags align, nk_bool value);
  3195. NK_API nk_bool nk_select_text(struct nk_context*, const char*, int, nk_flags align, nk_bool value);
  3196. NK_API nk_bool nk_select_image_label(struct nk_context*, struct nk_image, const char*, nk_flags align, nk_bool value);
  3197. NK_API nk_bool nk_select_image_text(struct nk_context*, struct nk_image, const char*, int, nk_flags align, nk_bool value);
  3198. NK_API nk_bool nk_select_symbol_label(struct nk_context*, enum nk_symbol_type, const char*, nk_flags align, nk_bool value);
  3199. NK_API nk_bool nk_select_symbol_text(struct nk_context*, enum nk_symbol_type, const char*, int, nk_flags align, nk_bool value);
  3200. /* =============================================================================
  3201. *
  3202. * SLIDER
  3203. *
  3204. * ============================================================================= */
  3205. NK_API float nk_slide_float(struct nk_context*, float min, float val, float max, float step);
  3206. NK_API int nk_slide_int(struct nk_context*, int min, int val, int max, int step);
  3207. NK_API nk_bool nk_slider_float(struct nk_context*, float min, float* val, float max, float step);
  3208. NK_API nk_bool nk_slider_int(struct nk_context*, int min, int* val, int max, int step);
  3209. /* =============================================================================
  3210. *
  3211. * PROGRESSBAR
  3212. *
  3213. * ============================================================================= */
  3214. NK_API nk_bool nk_progress(struct nk_context*, nk_size* cur, nk_size max, nk_bool modifyable);
  3215. NK_API nk_size nk_prog(struct nk_context*, nk_size cur, nk_size max, nk_bool modifyable);
  3216. /* =============================================================================
  3217. *
  3218. * COLOR PICKER
  3219. *
  3220. * ============================================================================= */
  3221. NK_API struct nk_colorf nk_color_picker(struct nk_context*, struct nk_colorf, enum nk_color_format);
  3222. NK_API nk_bool nk_color_pick(struct nk_context*, struct nk_colorf*, enum nk_color_format);
  3223. /* =============================================================================
  3224. *
  3225. * PROPERTIES
  3226. *
  3227. * =============================================================================
  3228. /// ### Properties
  3229. /// Properties are the main value modification widgets in Nuklear. Changing a value
  3230. /// can be achieved by dragging, adding/removing incremental steps on button click
  3231. /// or by directly typing a number.
  3232. ///
  3233. /// #### Usage
  3234. /// Each property requires a unique name for identification that is also used for
  3235. /// displaying a label. If you want to use the same name multiple times make sure
  3236. /// add a '#' before your name. The '#' will not be shown but will generate a
  3237. /// unique ID. Each property also takes in a minimum and maximum value. If you want
  3238. /// to make use of the complete number range of a type just use the provided
  3239. /// type limits from `limits.h`. For example `INT_MIN` and `INT_MAX` for
  3240. /// `nk_property_int` and `nk_propertyi`. In additional each property takes in
  3241. /// a increment value that will be added or subtracted if either the increment
  3242. /// decrement button is clicked. Finally there is a value for increment per pixel
  3243. /// dragged that is added or subtracted from the value.
  3244. ///
  3245. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  3246. /// int value = 0;
  3247. /// struct nk_context ctx;
  3248. /// nk_init_xxx(&ctx, ...);
  3249. /// while (1) {
  3250. /// // Input
  3251. /// Event evt;
  3252. /// nk_input_begin(&ctx);
  3253. /// while (GetEvent(&evt)) {
  3254. /// if (evt.type == MOUSE_MOVE)
  3255. /// nk_input_motion(&ctx, evt.motion.x, evt.motion.y);
  3256. /// else if (evt.type == [...]) {
  3257. /// nk_input_xxx(...);
  3258. /// }
  3259. /// }
  3260. /// nk_input_end(&ctx);
  3261. /// //
  3262. /// // Window
  3263. /// if (nk_begin_xxx(...) {
  3264. /// // Property
  3265. /// nk_layout_row_dynamic(...);
  3266. /// nk_property_int(ctx, "ID", INT_MIN, &value, INT_MAX, 1, 1);
  3267. /// }
  3268. /// nk_end(ctx);
  3269. /// //
  3270. /// // Draw
  3271. /// const struct nk_command *cmd = 0;
  3272. /// nk_foreach(cmd, &ctx) {
  3273. /// switch (cmd->type) {
  3274. /// case NK_COMMAND_LINE:
  3275. /// your_draw_line_function(...)
  3276. /// break;
  3277. /// case NK_COMMAND_RECT
  3278. /// your_draw_rect_function(...)
  3279. /// break;
  3280. /// case ...:
  3281. /// // [...]
  3282. /// }
  3283. /// nk_clear(&ctx);
  3284. /// }
  3285. /// nk_free(&ctx);
  3286. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  3287. ///
  3288. /// #### Reference
  3289. /// Function | Description
  3290. /// --------------------|-------------------------------------------
  3291. /// nk_property_int | Integer property directly modifying a passed in value
  3292. /// nk_property_float | Float property directly modifying a passed in value
  3293. /// nk_property_double | Double property directly modifying a passed in value
  3294. /// nk_propertyi | Integer property returning the modified int value
  3295. /// nk_propertyf | Float property returning the modified float value
  3296. /// nk_propertyd | Double property returning the modified double value
  3297. ///
  3298. */
  3299. /*/// #### nk_property_int
  3300. /// Integer property directly modifying a passed in value
  3301. /// !!! WARNING
  3302. /// To generate a unique property ID using the same label make sure to insert
  3303. /// a `#` at the beginning. It will not be shown but guarantees correct behavior.
  3304. ///
  3305. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  3306. /// void nk_property_int(struct nk_context *ctx, const char *name, int min, int *val, int max, int step, float inc_per_pixel);
  3307. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  3308. ///
  3309. /// Parameter | Description
  3310. /// --------------------|-----------------------------------------------------------
  3311. /// __ctx__ | Must point to an previously initialized `nk_context` struct after calling a layouting function
  3312. /// __name__ | String used both as a label as well as a unique identifier
  3313. /// __min__ | Minimum value not allowed to be underflown
  3314. /// __val__ | Integer pointer to be modified
  3315. /// __max__ | Maximum value not allowed to be overflown
  3316. /// __step__ | Increment added and subtracted on increment and decrement button
  3317. /// __inc_per_pixel__ | Value per pixel added or subtracted on dragging
  3318. */
  3319. NK_API void nk_property_int(struct nk_context*, const char* name, int min, int* val, int max, int step, float inc_per_pixel);
  3320. /*/// #### nk_property_float
  3321. /// Float property directly modifying a passed in value
  3322. /// !!! WARNING
  3323. /// To generate a unique property ID using the same label make sure to insert
  3324. /// a `#` at the beginning. It will not be shown but guarantees correct behavior.
  3325. ///
  3326. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  3327. /// void nk_property_float(struct nk_context *ctx, const char *name, float min, float *val, float max, float step, float inc_per_pixel);
  3328. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  3329. ///
  3330. /// Parameter | Description
  3331. /// --------------------|-----------------------------------------------------------
  3332. /// __ctx__ | Must point to an previously initialized `nk_context` struct after calling a layouting function
  3333. /// __name__ | String used both as a label as well as a unique identifier
  3334. /// __min__ | Minimum value not allowed to be underflown
  3335. /// __val__ | Float pointer to be modified
  3336. /// __max__ | Maximum value not allowed to be overflown
  3337. /// __step__ | Increment added and subtracted on increment and decrement button
  3338. /// __inc_per_pixel__ | Value per pixel added or subtracted on dragging
  3339. */
  3340. NK_API void nk_property_float(struct nk_context*, const char* name, float min, float* val, float max, float step, float inc_per_pixel);
  3341. /*/// #### nk_property_double
  3342. /// Double property directly modifying a passed in value
  3343. /// !!! WARNING
  3344. /// To generate a unique property ID using the same label make sure to insert
  3345. /// a `#` at the beginning. It will not be shown but guarantees correct behavior.
  3346. ///
  3347. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  3348. /// void nk_property_double(struct nk_context *ctx, const char *name, double min, double *val, double max, double step, double inc_per_pixel);
  3349. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  3350. ///
  3351. /// Parameter | Description
  3352. /// --------------------|-----------------------------------------------------------
  3353. /// __ctx__ | Must point to an previously initialized `nk_context` struct after calling a layouting function
  3354. /// __name__ | String used both as a label as well as a unique identifier
  3355. /// __min__ | Minimum value not allowed to be underflown
  3356. /// __val__ | Double pointer to be modified
  3357. /// __max__ | Maximum value not allowed to be overflown
  3358. /// __step__ | Increment added and subtracted on increment and decrement button
  3359. /// __inc_per_pixel__ | Value per pixel added or subtracted on dragging
  3360. */
  3361. NK_API void nk_property_double(struct nk_context*, const char* name, double min, double* val, double max, double step, float inc_per_pixel);
  3362. /*/// #### nk_propertyi
  3363. /// Integer property modifying a passed in value and returning the new value
  3364. /// !!! WARNING
  3365. /// To generate a unique property ID using the same label make sure to insert
  3366. /// a `#` at the beginning. It will not be shown but guarantees correct behavior.
  3367. ///
  3368. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  3369. /// int nk_propertyi(struct nk_context *ctx, const char *name, int min, int val, int max, int step, float inc_per_pixel);
  3370. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  3371. ///
  3372. /// Parameter | Description
  3373. /// --------------------|-----------------------------------------------------------
  3374. /// __ctx__ | Must point to an previously initialized `nk_context` struct after calling a layouting function
  3375. /// __name__ | String used both as a label as well as a unique identifier
  3376. /// __min__ | Minimum value not allowed to be underflown
  3377. /// __val__ | Current integer value to be modified and returned
  3378. /// __max__ | Maximum value not allowed to be overflown
  3379. /// __step__ | Increment added and subtracted on increment and decrement button
  3380. /// __inc_per_pixel__ | Value per pixel added or subtracted on dragging
  3381. ///
  3382. /// Returns the new modified integer value
  3383. */
  3384. NK_API int nk_propertyi(struct nk_context*, const char* name, int min, int val, int max, int step, float inc_per_pixel);
  3385. /*/// #### nk_propertyf
  3386. /// Float property modifying a passed in value and returning the new value
  3387. /// !!! WARNING
  3388. /// To generate a unique property ID using the same label make sure to insert
  3389. /// a `#` at the beginning. It will not be shown but guarantees correct behavior.
  3390. ///
  3391. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  3392. /// float nk_propertyf(struct nk_context *ctx, const char *name, float min, float val, float max, float step, float inc_per_pixel);
  3393. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  3394. ///
  3395. /// Parameter | Description
  3396. /// --------------------|-----------------------------------------------------------
  3397. /// __ctx__ | Must point to an previously initialized `nk_context` struct after calling a layouting function
  3398. /// __name__ | String used both as a label as well as a unique identifier
  3399. /// __min__ | Minimum value not allowed to be underflown
  3400. /// __val__ | Current float value to be modified and returned
  3401. /// __max__ | Maximum value not allowed to be overflown
  3402. /// __step__ | Increment added and subtracted on increment and decrement button
  3403. /// __inc_per_pixel__ | Value per pixel added or subtracted on dragging
  3404. ///
  3405. /// Returns the new modified float value
  3406. */
  3407. NK_API float nk_propertyf(struct nk_context*, const char* name, float min, float val, float max, float step, float inc_per_pixel);
  3408. /*/// #### nk_propertyd
  3409. /// Float property modifying a passed in value and returning the new value
  3410. /// !!! WARNING
  3411. /// To generate a unique property ID using the same label make sure to insert
  3412. /// a `#` at the beginning. It will not be shown but guarantees correct behavior.
  3413. ///
  3414. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  3415. /// float nk_propertyd(struct nk_context *ctx, const char *name, double min, double val, double max, double step, double inc_per_pixel);
  3416. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  3417. ///
  3418. /// Parameter | Description
  3419. /// --------------------|-----------------------------------------------------------
  3420. /// __ctx__ | Must point to an previously initialized `nk_context` struct after calling a layouting function
  3421. /// __name__ | String used both as a label as well as a unique identifier
  3422. /// __min__ | Minimum value not allowed to be underflown
  3423. /// __val__ | Current double value to be modified and returned
  3424. /// __max__ | Maximum value not allowed to be overflown
  3425. /// __step__ | Increment added and subtracted on increment and decrement button
  3426. /// __inc_per_pixel__ | Value per pixel added or subtracted on dragging
  3427. ///
  3428. /// Returns the new modified double value
  3429. */
  3430. NK_API double nk_propertyd(struct nk_context*, const char* name, double min, double val, double max, double step, float inc_per_pixel);
  3431. /* =============================================================================
  3432. *
  3433. * TEXT EDIT
  3434. *
  3435. * ============================================================================= */
  3436. enum nk_edit_flags {
  3437. NK_EDIT_DEFAULT = 0,
  3438. NK_EDIT_READ_ONLY = NK_FLAG(0),
  3439. NK_EDIT_AUTO_SELECT = NK_FLAG(1),
  3440. NK_EDIT_SIG_ENTER = NK_FLAG(2),
  3441. NK_EDIT_ALLOW_TAB = NK_FLAG(3),
  3442. NK_EDIT_NO_CURSOR = NK_FLAG(4),
  3443. NK_EDIT_SELECTABLE = NK_FLAG(5),
  3444. NK_EDIT_CLIPBOARD = NK_FLAG(6),
  3445. NK_EDIT_CTRL_ENTER_NEWLINE = NK_FLAG(7),
  3446. NK_EDIT_NO_HORIZONTAL_SCROLL = NK_FLAG(8),
  3447. NK_EDIT_ALWAYS_INSERT_MODE = NK_FLAG(9),
  3448. NK_EDIT_MULTILINE = NK_FLAG(10),
  3449. NK_EDIT_GOTO_END_ON_ACTIVATE = NK_FLAG(11)
  3450. };
  3451. enum nk_edit_types {
  3452. NK_EDIT_SIMPLE = NK_EDIT_ALWAYS_INSERT_MODE,
  3453. NK_EDIT_FIELD = NK_EDIT_SIMPLE | NK_EDIT_SELECTABLE | NK_EDIT_CLIPBOARD,
  3454. NK_EDIT_BOX = NK_EDIT_ALWAYS_INSERT_MODE | NK_EDIT_SELECTABLE | NK_EDIT_MULTILINE | NK_EDIT_ALLOW_TAB | NK_EDIT_CLIPBOARD,
  3455. NK_EDIT_EDITOR = NK_EDIT_SELECTABLE | NK_EDIT_MULTILINE | NK_EDIT_ALLOW_TAB | NK_EDIT_CLIPBOARD
  3456. };
  3457. enum nk_edit_events {
  3458. NK_EDIT_ACTIVE = NK_FLAG(0), /* edit widget is currently being modified */
  3459. NK_EDIT_INACTIVE = NK_FLAG(1), /* edit widget is not active and is not being modified */
  3460. NK_EDIT_ACTIVATED = NK_FLAG(2), /* edit widget went from state inactive to state active */
  3461. NK_EDIT_DEACTIVATED = NK_FLAG(3), /* edit widget went from state active to state inactive */
  3462. NK_EDIT_COMMITED = NK_FLAG(4) /* edit widget has received an enter and lost focus */
  3463. };
  3464. NK_API nk_flags nk_edit_string(struct nk_context*, nk_flags, char* buffer, int* len, int max, nk_plugin_filter);
  3465. NK_API nk_flags nk_edit_string_zero_terminated(struct nk_context*, nk_flags, char* buffer, int max, nk_plugin_filter);
  3466. NK_API nk_flags nk_edit_buffer(struct nk_context*, nk_flags, struct nk_text_edit*, nk_plugin_filter);
  3467. NK_API void nk_edit_focus(struct nk_context*, nk_flags flags);
  3468. NK_API void nk_edit_unfocus(struct nk_context*);
  3469. /* =============================================================================
  3470. *
  3471. * CHART
  3472. *
  3473. * ============================================================================= */
  3474. NK_API nk_bool nk_chart_begin(struct nk_context*, enum nk_chart_type, int num, float min, float max);
  3475. NK_API nk_bool nk_chart_begin_colored(struct nk_context*, enum nk_chart_type, struct nk_color, struct nk_color active, int num, float min, float max);
  3476. NK_API void nk_chart_add_slot(struct nk_context* ctx, const enum nk_chart_type, int count, float min_value, float max_value);
  3477. NK_API void nk_chart_add_slot_colored(struct nk_context* ctx, const enum nk_chart_type, struct nk_color, struct nk_color active, int count, float min_value, float max_value);
  3478. NK_API nk_flags nk_chart_push(struct nk_context*, float);
  3479. NK_API nk_flags nk_chart_push_slot(struct nk_context*, float, int);
  3480. NK_API void nk_chart_end(struct nk_context*);
  3481. NK_API void nk_plot(struct nk_context*, enum nk_chart_type, const float* values, int count, int offset);
  3482. NK_API void nk_plot_function(struct nk_context*, enum nk_chart_type, void* userdata, float(*value_getter)(void* user, int index), int count, int offset);
  3483. /* =============================================================================
  3484. *
  3485. * POPUP
  3486. *
  3487. * ============================================================================= */
  3488. NK_API nk_bool nk_popup_begin(struct nk_context*, enum nk_popup_type, const char*, nk_flags, struct nk_rect bounds);
  3489. NK_API void nk_popup_close(struct nk_context*);
  3490. NK_API void nk_popup_end(struct nk_context*);
  3491. NK_API void nk_popup_get_scroll(struct nk_context*, nk_uint* offset_x, nk_uint* offset_y);
  3492. NK_API void nk_popup_set_scroll(struct nk_context*, nk_uint offset_x, nk_uint offset_y);
  3493. /* =============================================================================
  3494. *
  3495. * COMBOBOX
  3496. *
  3497. * ============================================================================= */
  3498. NK_API int nk_combo(struct nk_context*, const char** items, int count, int selected, int item_height, struct nk_vec2 size);
  3499. NK_API int nk_combo_separator(struct nk_context*, const char* items_separated_by_separator, int separator, int selected, int count, int item_height, struct nk_vec2 size);
  3500. NK_API int nk_combo_string(struct nk_context*, const char* items_separated_by_zeros, int selected, int count, int item_height, struct nk_vec2 size);
  3501. NK_API int nk_combo_callback(struct nk_context*, void(*item_getter)(void*, int, const char**), void* userdata, int selected, int count, int item_height, struct nk_vec2 size);
  3502. NK_API void nk_combobox(struct nk_context*, const char** items, int count, int* selected, int item_height, struct nk_vec2 size);
  3503. NK_API void nk_combobox_string(struct nk_context*, const char* items_separated_by_zeros, int* selected, int count, int item_height, struct nk_vec2 size);
  3504. NK_API void nk_combobox_separator(struct nk_context*, const char* items_separated_by_separator, int separator, int* selected, int count, int item_height, struct nk_vec2 size);
  3505. NK_API void nk_combobox_callback(struct nk_context*, void(*item_getter)(void*, int, const char**), void*, int* selected, int count, int item_height, struct nk_vec2 size);
  3506. /* =============================================================================
  3507. *
  3508. * ABSTRACT COMBOBOX
  3509. *
  3510. * ============================================================================= */
  3511. NK_API nk_bool nk_combo_begin_text(struct nk_context*, const char* selected, int, struct nk_vec2 size);
  3512. NK_API nk_bool nk_combo_begin_label(struct nk_context*, const char* selected, struct nk_vec2 size);
  3513. NK_API nk_bool nk_combo_begin_color(struct nk_context*, struct nk_color color, struct nk_vec2 size);
  3514. NK_API nk_bool nk_combo_begin_symbol(struct nk_context*, enum nk_symbol_type, struct nk_vec2 size);
  3515. NK_API nk_bool nk_combo_begin_symbol_label(struct nk_context*, const char* selected, enum nk_symbol_type, struct nk_vec2 size);
  3516. NK_API nk_bool nk_combo_begin_symbol_text(struct nk_context*, const char* selected, int, enum nk_symbol_type, struct nk_vec2 size);
  3517. NK_API nk_bool nk_combo_begin_image(struct nk_context*, struct nk_image img, struct nk_vec2 size);
  3518. NK_API nk_bool nk_combo_begin_image_label(struct nk_context*, const char* selected, struct nk_image, struct nk_vec2 size);
  3519. NK_API nk_bool nk_combo_begin_image_text(struct nk_context*, const char* selected, int, struct nk_image, struct nk_vec2 size);
  3520. NK_API nk_bool nk_combo_item_label(struct nk_context*, const char*, nk_flags alignment);
  3521. NK_API nk_bool nk_combo_item_text(struct nk_context*, const char*, int, nk_flags alignment);
  3522. NK_API nk_bool nk_combo_item_image_label(struct nk_context*, struct nk_image, const char*, nk_flags alignment);
  3523. NK_API nk_bool nk_combo_item_image_text(struct nk_context*, struct nk_image, const char*, int, nk_flags alignment);
  3524. NK_API nk_bool nk_combo_item_symbol_label(struct nk_context*, enum nk_symbol_type, const char*, nk_flags alignment);
  3525. NK_API nk_bool nk_combo_item_symbol_text(struct nk_context*, enum nk_symbol_type, const char*, int, nk_flags alignment);
  3526. NK_API void nk_combo_close(struct nk_context*);
  3527. NK_API void nk_combo_end(struct nk_context*);
  3528. /* =============================================================================
  3529. *
  3530. * CONTEXTUAL
  3531. *
  3532. * ============================================================================= */
  3533. NK_API nk_bool nk_contextual_begin(struct nk_context*, nk_flags, struct nk_vec2, struct nk_rect trigger_bounds);
  3534. NK_API nk_bool nk_contextual_item_text(struct nk_context*, const char*, int, nk_flags align);
  3535. NK_API nk_bool nk_contextual_item_label(struct nk_context*, const char*, nk_flags align);
  3536. NK_API nk_bool nk_contextual_item_image_label(struct nk_context*, struct nk_image, const char*, nk_flags alignment);
  3537. NK_API nk_bool nk_contextual_item_image_text(struct nk_context*, struct nk_image, const char*, int len, nk_flags alignment);
  3538. NK_API nk_bool nk_contextual_item_symbol_label(struct nk_context*, enum nk_symbol_type, const char*, nk_flags alignment);
  3539. NK_API nk_bool nk_contextual_item_symbol_text(struct nk_context*, enum nk_symbol_type, const char*, int, nk_flags alignment);
  3540. NK_API void nk_contextual_close(struct nk_context*);
  3541. NK_API void nk_contextual_end(struct nk_context*);
  3542. /* =============================================================================
  3543. *
  3544. * TOOLTIP
  3545. *
  3546. * ============================================================================= */
  3547. NK_API void nk_tooltip(struct nk_context*, const char*);
  3548. #ifdef NK_INCLUDE_STANDARD_VARARGS
  3549. NK_API void nk_tooltipf(struct nk_context*, NK_PRINTF_FORMAT_STRING const char*, ...) NK_PRINTF_VARARG_FUNC(2);
  3550. NK_API void nk_tooltipfv(struct nk_context*, NK_PRINTF_FORMAT_STRING const char*, va_list) NK_PRINTF_VALIST_FUNC(2);
  3551. #endif
  3552. NK_API nk_bool nk_tooltip_begin(struct nk_context*, float width);
  3553. NK_API void nk_tooltip_end(struct nk_context*);
  3554. /* =============================================================================
  3555. *
  3556. * MENU
  3557. *
  3558. * ============================================================================= */
  3559. NK_API void nk_menubar_begin(struct nk_context*);
  3560. NK_API void nk_menubar_end(struct nk_context*);
  3561. NK_API nk_bool nk_menu_begin_text(struct nk_context*, const char* title, int title_len, nk_flags align, struct nk_vec2 size);
  3562. NK_API nk_bool nk_menu_begin_label(struct nk_context*, const char*, nk_flags align, struct nk_vec2 size);
  3563. NK_API nk_bool nk_menu_begin_image(struct nk_context*, const char*, struct nk_image, struct nk_vec2 size);
  3564. NK_API nk_bool nk_menu_begin_image_text(struct nk_context*, const char*, int, nk_flags align, struct nk_image, struct nk_vec2 size);
  3565. NK_API nk_bool nk_menu_begin_image_label(struct nk_context*, const char*, nk_flags align, struct nk_image, struct nk_vec2 size);
  3566. NK_API nk_bool nk_menu_begin_symbol(struct nk_context*, const char*, enum nk_symbol_type, struct nk_vec2 size);
  3567. NK_API nk_bool nk_menu_begin_symbol_text(struct nk_context*, const char*, int, nk_flags align, enum nk_symbol_type, struct nk_vec2 size);
  3568. NK_API nk_bool nk_menu_begin_symbol_label(struct nk_context*, const char*, nk_flags align, enum nk_symbol_type, struct nk_vec2 size);
  3569. NK_API nk_bool nk_menu_item_text(struct nk_context*, const char*, int, nk_flags align);
  3570. NK_API nk_bool nk_menu_item_label(struct nk_context*, const char*, nk_flags alignment);
  3571. NK_API nk_bool nk_menu_item_image_label(struct nk_context*, struct nk_image, const char*, nk_flags alignment);
  3572. NK_API nk_bool nk_menu_item_image_text(struct nk_context*, struct nk_image, const char*, int len, nk_flags alignment);
  3573. NK_API nk_bool nk_menu_item_symbol_text(struct nk_context*, enum nk_symbol_type, const char*, int, nk_flags alignment);
  3574. NK_API nk_bool nk_menu_item_symbol_label(struct nk_context*, enum nk_symbol_type, const char*, nk_flags alignment);
  3575. NK_API void nk_menu_close(struct nk_context*);
  3576. NK_API void nk_menu_end(struct nk_context*);
  3577. /* =============================================================================
  3578. *
  3579. * STYLE
  3580. *
  3581. * ============================================================================= */
  3582. enum nk_style_colors {
  3583. NK_COLOR_TEXT,
  3584. NK_COLOR_WINDOW,
  3585. NK_COLOR_HEADER,
  3586. NK_COLOR_BORDER,
  3587. NK_COLOR_BUTTON,
  3588. NK_COLOR_BUTTON_HOVER,
  3589. NK_COLOR_BUTTON_ACTIVE,
  3590. NK_COLOR_TOGGLE,
  3591. NK_COLOR_TOGGLE_HOVER,
  3592. NK_COLOR_TOGGLE_CURSOR,
  3593. NK_COLOR_SELECT,
  3594. NK_COLOR_SELECT_ACTIVE,
  3595. NK_COLOR_SLIDER,
  3596. NK_COLOR_SLIDER_CURSOR,
  3597. NK_COLOR_SLIDER_CURSOR_HOVER,
  3598. NK_COLOR_SLIDER_CURSOR_ACTIVE,
  3599. NK_COLOR_PROPERTY,
  3600. NK_COLOR_EDIT,
  3601. NK_COLOR_EDIT_CURSOR,
  3602. NK_COLOR_COMBO,
  3603. NK_COLOR_CHART,
  3604. NK_COLOR_CHART_COLOR,
  3605. NK_COLOR_CHART_COLOR_HIGHLIGHT,
  3606. NK_COLOR_SCROLLBAR,
  3607. NK_COLOR_SCROLLBAR_CURSOR,
  3608. NK_COLOR_SCROLLBAR_CURSOR_HOVER,
  3609. NK_COLOR_SCROLLBAR_CURSOR_ACTIVE,
  3610. NK_COLOR_TAB_HEADER,
  3611. NK_COLOR_COUNT
  3612. };
  3613. enum nk_style_cursor {
  3614. NK_CURSOR_ARROW,
  3615. NK_CURSOR_TEXT,
  3616. NK_CURSOR_MOVE,
  3617. NK_CURSOR_RESIZE_VERTICAL,
  3618. NK_CURSOR_RESIZE_HORIZONTAL,
  3619. NK_CURSOR_RESIZE_TOP_LEFT_DOWN_RIGHT,
  3620. NK_CURSOR_RESIZE_TOP_RIGHT_DOWN_LEFT,
  3621. NK_CURSOR_COUNT
  3622. };
  3623. NK_API void nk_style_default(struct nk_context*);
  3624. NK_API void nk_style_from_table(struct nk_context*, const struct nk_color*);
  3625. NK_API void nk_style_load_cursor(struct nk_context*, enum nk_style_cursor, const struct nk_cursor*);
  3626. NK_API void nk_style_load_all_cursors(struct nk_context*, struct nk_cursor*);
  3627. NK_API const char* nk_style_get_color_by_name(enum nk_style_colors);
  3628. NK_API void nk_style_set_font(struct nk_context*, const struct nk_user_font*);
  3629. NK_API nk_bool nk_style_set_cursor(struct nk_context*, enum nk_style_cursor);
  3630. NK_API void nk_style_show_cursor(struct nk_context*);
  3631. NK_API void nk_style_hide_cursor(struct nk_context*);
  3632. NK_API nk_bool nk_style_push_font(struct nk_context*, const struct nk_user_font*);
  3633. NK_API nk_bool nk_style_push_float(struct nk_context*, float*, float);
  3634. NK_API nk_bool nk_style_push_vec2(struct nk_context*, struct nk_vec2*, struct nk_vec2);
  3635. NK_API nk_bool nk_style_push_style_item(struct nk_context*, struct nk_style_item*, struct nk_style_item);
  3636. NK_API nk_bool nk_style_push_flags(struct nk_context*, nk_flags*, nk_flags);
  3637. NK_API nk_bool nk_style_push_color(struct nk_context*, struct nk_color*, struct nk_color);
  3638. NK_API nk_bool nk_style_pop_font(struct nk_context*);
  3639. NK_API nk_bool nk_style_pop_float(struct nk_context*);
  3640. NK_API nk_bool nk_style_pop_vec2(struct nk_context*);
  3641. NK_API nk_bool nk_style_pop_style_item(struct nk_context*);
  3642. NK_API nk_bool nk_style_pop_flags(struct nk_context*);
  3643. NK_API nk_bool nk_style_pop_color(struct nk_context*);
  3644. /* =============================================================================
  3645. *
  3646. * COLOR
  3647. *
  3648. * ============================================================================= */
  3649. NK_API struct nk_color nk_rgb(int r, int g, int b);
  3650. NK_API struct nk_color nk_rgb_iv(const int* rgb);
  3651. NK_API struct nk_color nk_rgb_bv(const nk_byte* rgb);
  3652. NK_API struct nk_color nk_rgb_f(float r, float g, float b);
  3653. NK_API struct nk_color nk_rgb_fv(const float* rgb);
  3654. NK_API struct nk_color nk_rgb_cf(struct nk_colorf c);
  3655. NK_API struct nk_color nk_rgb_hex(const char* rgb);
  3656. NK_API struct nk_color nk_rgba(int r, int g, int b, int a);
  3657. NK_API struct nk_color nk_rgba_u32(nk_uint);
  3658. NK_API struct nk_color nk_rgba_iv(const int* rgba);
  3659. NK_API struct nk_color nk_rgba_bv(const nk_byte* rgba);
  3660. NK_API struct nk_color nk_rgba_f(float r, float g, float b, float a);
  3661. NK_API struct nk_color nk_rgba_fv(const float* rgba);
  3662. NK_API struct nk_color nk_rgba_cf(struct nk_colorf c);
  3663. NK_API struct nk_color nk_rgba_hex(const char* rgb);
  3664. NK_API struct nk_colorf nk_hsva_colorf(float h, float s, float v, float a);
  3665. NK_API struct nk_colorf nk_hsva_colorfv(float* c);
  3666. NK_API void nk_colorf_hsva_f(float* out_h, float* out_s, float* out_v, float* out_a, struct nk_colorf in);
  3667. NK_API void nk_colorf_hsva_fv(float* hsva, struct nk_colorf in);
  3668. NK_API struct nk_color nk_hsv(int h, int s, int v);
  3669. NK_API struct nk_color nk_hsv_iv(const int* hsv);
  3670. NK_API struct nk_color nk_hsv_bv(const nk_byte* hsv);
  3671. NK_API struct nk_color nk_hsv_f(float h, float s, float v);
  3672. NK_API struct nk_color nk_hsv_fv(const float* hsv);
  3673. NK_API struct nk_color nk_hsva(int h, int s, int v, int a);
  3674. NK_API struct nk_color nk_hsva_iv(const int* hsva);
  3675. NK_API struct nk_color nk_hsva_bv(const nk_byte* hsva);
  3676. NK_API struct nk_color nk_hsva_f(float h, float s, float v, float a);
  3677. NK_API struct nk_color nk_hsva_fv(const float* hsva);
  3678. /* color (conversion nuklear --> user) */
  3679. NK_API void nk_color_f(float* r, float* g, float* b, float* a, struct nk_color);
  3680. NK_API void nk_color_fv(float* rgba_out, struct nk_color);
  3681. NK_API struct nk_colorf nk_color_cf(struct nk_color);
  3682. NK_API void nk_color_d(double* r, double* g, double* b, double* a, struct nk_color);
  3683. NK_API void nk_color_dv(double* rgba_out, struct nk_color);
  3684. NK_API nk_uint nk_color_u32(struct nk_color);
  3685. NK_API void nk_color_hex_rgba(char* output, struct nk_color);
  3686. NK_API void nk_color_hex_rgb(char* output, struct nk_color);
  3687. NK_API void nk_color_hsv_i(int* out_h, int* out_s, int* out_v, struct nk_color);
  3688. NK_API void nk_color_hsv_b(nk_byte* out_h, nk_byte* out_s, nk_byte* out_v, struct nk_color);
  3689. NK_API void nk_color_hsv_iv(int* hsv_out, struct nk_color);
  3690. NK_API void nk_color_hsv_bv(nk_byte* hsv_out, struct nk_color);
  3691. NK_API void nk_color_hsv_f(float* out_h, float* out_s, float* out_v, struct nk_color);
  3692. NK_API void nk_color_hsv_fv(float* hsv_out, struct nk_color);
  3693. NK_API void nk_color_hsva_i(int* h, int* s, int* v, int* a, struct nk_color);
  3694. NK_API void nk_color_hsva_b(nk_byte* h, nk_byte* s, nk_byte* v, nk_byte* a, struct nk_color);
  3695. NK_API void nk_color_hsva_iv(int* hsva_out, struct nk_color);
  3696. NK_API void nk_color_hsva_bv(nk_byte* hsva_out, struct nk_color);
  3697. NK_API void nk_color_hsva_f(float* out_h, float* out_s, float* out_v, float* out_a, struct nk_color);
  3698. NK_API void nk_color_hsva_fv(float* hsva_out, struct nk_color);
  3699. /* =============================================================================
  3700. *
  3701. * IMAGE
  3702. *
  3703. * ============================================================================= */
  3704. NK_API nk_handle nk_handle_ptr(void*);
  3705. NK_API nk_handle nk_handle_id(int);
  3706. NK_API struct nk_image nk_image_handle(nk_handle);
  3707. NK_API struct nk_image nk_image_ptr(void*);
  3708. NK_API struct nk_image nk_image_id(int);
  3709. NK_API nk_bool nk_image_is_subimage(const struct nk_image* img);
  3710. NK_API struct nk_image nk_subimage_ptr(void*, nk_ushort w, nk_ushort h, struct nk_rect sub_region);
  3711. NK_API struct nk_image nk_subimage_id(int, nk_ushort w, nk_ushort h, struct nk_rect sub_region);
  3712. NK_API struct nk_image nk_subimage_handle(nk_handle, nk_ushort w, nk_ushort h, struct nk_rect sub_region);
  3713. /* =============================================================================
  3714. *
  3715. * 9-SLICE
  3716. *
  3717. * ============================================================================= */
  3718. NK_API struct nk_nine_slice nk_nine_slice_handle(nk_handle, nk_ushort l, nk_ushort t, nk_ushort r, nk_ushort b);
  3719. NK_API struct nk_nine_slice nk_nine_slice_ptr(void*, nk_ushort l, nk_ushort t, nk_ushort r, nk_ushort b);
  3720. NK_API struct nk_nine_slice nk_nine_slice_id(int, nk_ushort l, nk_ushort t, nk_ushort r, nk_ushort b);
  3721. NK_API int nk_nine_slice_is_sub9slice(const struct nk_nine_slice* img);
  3722. NK_API struct nk_nine_slice nk_sub9slice_ptr(void*, nk_ushort w, nk_ushort h, struct nk_rect sub_region, nk_ushort l, nk_ushort t, nk_ushort r, nk_ushort b);
  3723. NK_API struct nk_nine_slice nk_sub9slice_id(int, nk_ushort w, nk_ushort h, struct nk_rect sub_region, nk_ushort l, nk_ushort t, nk_ushort r, nk_ushort b);
  3724. NK_API struct nk_nine_slice nk_sub9slice_handle(nk_handle, nk_ushort w, nk_ushort h, struct nk_rect sub_region, nk_ushort l, nk_ushort t, nk_ushort r, nk_ushort b);
  3725. /* =============================================================================
  3726. *
  3727. * MATH
  3728. *
  3729. * ============================================================================= */
  3730. NK_API nk_hash nk_murmur_hash(const void* key, int len, nk_hash seed);
  3731. NK_API void nk_triangle_from_direction(struct nk_vec2* result, struct nk_rect r, float pad_x, float pad_y, enum nk_heading);
  3732. NK_API struct nk_vec2 nk_vec2(float x, float y);
  3733. NK_API struct nk_vec2 nk_vec2i(int x, int y);
  3734. NK_API struct nk_vec2 nk_vec2v(const float* xy);
  3735. NK_API struct nk_vec2 nk_vec2iv(const int* xy);
  3736. NK_API struct nk_rect nk_get_null_rect(void);
  3737. NK_API struct nk_rect nk_rect(float x, float y, float w, float h);
  3738. NK_API struct nk_rect nk_recti(int x, int y, int w, int h);
  3739. NK_API struct nk_rect nk_recta(struct nk_vec2 pos, struct nk_vec2 size);
  3740. NK_API struct nk_rect nk_rectv(const float* xywh);
  3741. NK_API struct nk_rect nk_rectiv(const int* xywh);
  3742. NK_API struct nk_vec2 nk_rect_pos(struct nk_rect);
  3743. NK_API struct nk_vec2 nk_rect_size(struct nk_rect);
  3744. /* =============================================================================
  3745. *
  3746. * STRING
  3747. *
  3748. * ============================================================================= */
  3749. NK_API int nk_strlen(const char* str);
  3750. NK_API int nk_stricmp(const char* s1, const char* s2);
  3751. NK_API int nk_stricmpn(const char* s1, const char* s2, int n);
  3752. NK_API int nk_strtoi(const char* str, const char** endptr);
  3753. NK_API float nk_strtof(const char* str, const char** endptr);
  3754. #ifndef NK_STRTOD
  3755. #define NK_STRTOD nk_strtod
  3756. NK_API double nk_strtod(const char* str, const char** endptr);
  3757. #endif
  3758. NK_API int nk_strfilter(const char* text, const char* regexp);
  3759. NK_API int nk_strmatch_fuzzy_string(char const* str, char const* pattern, int* out_score);
  3760. NK_API int nk_strmatch_fuzzy_text(const char* txt, int txt_len, const char* pattern, int* out_score);
  3761. /* =============================================================================
  3762. *
  3763. * UTF-8
  3764. *
  3765. * ============================================================================= */
  3766. NK_API int nk_utf_decode(const char*, nk_rune*, int);
  3767. NK_API int nk_utf_encode(nk_rune, char*, int);
  3768. NK_API int nk_utf_len(const char*, int byte_len);
  3769. NK_API const char* nk_utf_at(const char* buffer, int length, int index, nk_rune* unicode, int* len);
  3770. /* ===============================================================
  3771. *
  3772. * FONT
  3773. *
  3774. * ===============================================================*/
  3775. /*/// ### Font
  3776. /// Font handling in this library was designed to be quite customizable and lets
  3777. /// you decide what you want to use and what you want to provide. There are three
  3778. /// different ways to use the font atlas. The first two will use your font
  3779. /// handling scheme and only requires essential data to run nuklear. The next
  3780. /// slightly more advanced features is font handling with vertex buffer output.
  3781. /// Finally the most complex API wise is using nuklear's font baking API.
  3782. //
  3783. /// #### Using your own implementation without vertex buffer output
  3784. ///
  3785. /// So first up the easiest way to do font handling is by just providing a
  3786. /// `nk_user_font` struct which only requires the height in pixel of the used
  3787. /// font and a callback to calculate the width of a string. This way of handling
  3788. /// fonts is best fitted for using the normal draw shape command API where you
  3789. /// do all the text drawing yourself and the library does not require any kind
  3790. /// of deeper knowledge about which font handling mechanism you use.
  3791. /// IMPORTANT: the `nk_user_font` pointer provided to nuklear has to persist
  3792. /// over the complete life time! I know this sucks but it is currently the only
  3793. /// way to switch between fonts.
  3794. ///
  3795. /// ```c
  3796. /// float your_text_width_calculation(nk_handle handle, float height, const char *text, int len)
  3797. /// {
  3798. /// your_font_type *type = handle.ptr;
  3799. /// float text_width = ...;
  3800. /// return text_width;
  3801. /// }
  3802. ///
  3803. /// struct nk_user_font font;
  3804. /// font.userdata.ptr = &your_font_class_or_struct;
  3805. /// font.height = your_font_height;
  3806. /// font.width = your_text_width_calculation;
  3807. ///
  3808. /// struct nk_context ctx;
  3809. /// nk_init_default(&ctx, &font);
  3810. /// ```
  3811. /// #### Using your own implementation with vertex buffer output
  3812. ///
  3813. /// While the first approach works fine if you don't want to use the optional
  3814. /// vertex buffer output it is not enough if you do. To get font handling working
  3815. /// for these cases you have to provide two additional parameters inside the
  3816. /// `nk_user_font`. First a texture atlas handle used to draw text as subimages
  3817. /// of a bigger font atlas texture and a callback to query a character's glyph
  3818. /// information (offset, size, ...). So it is still possible to provide your own
  3819. /// font and use the vertex buffer output.
  3820. ///
  3821. /// ```c
  3822. /// float your_text_width_calculation(nk_handle handle, float height, const char *text, int len)
  3823. /// {
  3824. /// your_font_type *type = handle.ptr;
  3825. /// float text_width = ...;
  3826. /// return text_width;
  3827. /// }
  3828. /// void query_your_font_glyph(nk_handle handle, float font_height, struct nk_user_font_glyph *glyph, nk_rune codepoint, nk_rune next_codepoint)
  3829. /// {
  3830. /// your_font_type *type = handle.ptr;
  3831. /// glyph.width = ...;
  3832. /// glyph.height = ...;
  3833. /// glyph.xadvance = ...;
  3834. /// glyph.uv[0].x = ...;
  3835. /// glyph.uv[0].y = ...;
  3836. /// glyph.uv[1].x = ...;
  3837. /// glyph.uv[1].y = ...;
  3838. /// glyph.offset.x = ...;
  3839. /// glyph.offset.y = ...;
  3840. /// }
  3841. ///
  3842. /// struct nk_user_font font;
  3843. /// font.userdata.ptr = &your_font_class_or_struct;
  3844. /// font.height = your_font_height;
  3845. /// font.width = your_text_width_calculation;
  3846. /// font.query = query_your_font_glyph;
  3847. /// font.texture.id = your_font_texture;
  3848. ///
  3849. /// struct nk_context ctx;
  3850. /// nk_init_default(&ctx, &font);
  3851. /// ```
  3852. ///
  3853. /// #### Nuklear font baker
  3854. ///
  3855. /// The final approach if you do not have a font handling functionality or don't
  3856. /// want to use it in this library is by using the optional font baker.
  3857. /// The font baker APIs can be used to create a font plus font atlas texture
  3858. /// and can be used with or without the vertex buffer output.
  3859. ///
  3860. /// It still uses the `nk_user_font` struct and the two different approaches
  3861. /// previously stated still work. The font baker is not located inside
  3862. /// `nk_context` like all other systems since it can be understood as more of
  3863. /// an extension to nuklear and does not really depend on any `nk_context` state.
  3864. ///
  3865. /// Font baker need to be initialized first by one of the nk_font_atlas_init_xxx
  3866. /// functions. If you don't care about memory just call the default version
  3867. /// `nk_font_atlas_init_default` which will allocate all memory from the standard library.
  3868. /// If you want to control memory allocation but you don't care if the allocated
  3869. /// memory is temporary and therefore can be freed directly after the baking process
  3870. /// is over or permanent you can call `nk_font_atlas_init`.
  3871. ///
  3872. /// After successfully initializing the font baker you can add Truetype(.ttf) fonts from
  3873. /// different sources like memory or from file by calling one of the `nk_font_atlas_add_xxx`.
  3874. /// functions. Adding font will permanently store each font, font config and ttf memory block(!)
  3875. /// inside the font atlas and allows to reuse the font atlas. If you don't want to reuse
  3876. /// the font baker by for example adding additional fonts you can call
  3877. /// `nk_font_atlas_cleanup` after the baking process is over (after calling nk_font_atlas_end).
  3878. ///
  3879. /// As soon as you added all fonts you wanted you can now start the baking process
  3880. /// for every selected glyph to image by calling `nk_font_atlas_bake`.
  3881. /// The baking process returns image memory, width and height which can be used to
  3882. /// either create your own image object or upload it to any graphics library.
  3883. /// No matter which case you finally have to call `nk_font_atlas_end` which
  3884. /// will free all temporary memory including the font atlas image so make sure
  3885. /// you created our texture beforehand. `nk_font_atlas_end` requires a handle
  3886. /// to your font texture or object and optionally fills a `struct nk_draw_null_texture`
  3887. /// which can be used for the optional vertex output. If you don't want it just
  3888. /// set the argument to `NULL`.
  3889. ///
  3890. /// At this point you are done and if you don't want to reuse the font atlas you
  3891. /// can call `nk_font_atlas_cleanup` to free all truetype blobs and configuration
  3892. /// memory. Finally if you don't use the font atlas and any of it's fonts anymore
  3893. /// you need to call `nk_font_atlas_clear` to free all memory still being used.
  3894. ///
  3895. /// ```c
  3896. /// struct nk_font_atlas atlas;
  3897. /// nk_font_atlas_init_default(&atlas);
  3898. /// nk_font_atlas_begin(&atlas);
  3899. /// nk_font *font = nk_font_atlas_add_from_file(&atlas, "Path/To/Your/TTF_Font.ttf", 13, 0);
  3900. /// nk_font *font2 = nk_font_atlas_add_from_file(&atlas, "Path/To/Your/TTF_Font2.ttf", 16, 0);
  3901. /// const void* img = nk_font_atlas_bake(&atlas, &img_width, &img_height, NK_FONT_ATLAS_RGBA32);
  3902. /// nk_font_atlas_end(&atlas, nk_handle_id(texture), 0);
  3903. ///
  3904. /// struct nk_context ctx;
  3905. /// nk_init_default(&ctx, &font->handle);
  3906. /// while (1) {
  3907. ///
  3908. /// }
  3909. /// nk_font_atlas_clear(&atlas);
  3910. /// ```
  3911. /// The font baker API is probably the most complex API inside this library and
  3912. /// I would suggest reading some of my examples `example/` to get a grip on how
  3913. /// to use the font atlas. There are a number of details I left out. For example
  3914. /// how to merge fonts, configure a font with `nk_font_config` to use other languages,
  3915. /// use another texture coordinate format and a lot more:
  3916. ///
  3917. /// ```c
  3918. /// struct nk_font_config cfg = nk_font_config(font_pixel_height);
  3919. /// cfg.merge_mode = nk_false or nk_true;
  3920. /// cfg.range = nk_font_korean_glyph_ranges();
  3921. /// cfg.coord_type = NK_COORD_PIXEL;
  3922. /// nk_font *font = nk_font_atlas_add_from_file(&atlas, "Path/To/Your/TTF_Font.ttf", 13, &cfg);
  3923. /// ```
  3924. */
  3925. struct nk_user_font_glyph;
  3926. typedef float(*nk_text_width_f)(nk_handle, float h, const char*, int len);
  3927. typedef void(*nk_query_font_glyph_f)(nk_handle handle, float font_height,
  3928. struct nk_user_font_glyph* glyph,
  3929. nk_rune codepoint, nk_rune next_codepoint);
  3930. #if defined(NK_INCLUDE_VERTEX_BUFFER_OUTPUT) || defined(NK_INCLUDE_SOFTWARE_FONT)
  3931. struct nk_user_font_glyph {
  3932. struct nk_vec2 uv[2];
  3933. /* texture coordinates */
  3934. struct nk_vec2 offset;
  3935. /* offset between top left and glyph */
  3936. float width, height;
  3937. /* size of the glyph */
  3938. float xadvance;
  3939. /* offset to the next glyph */
  3940. };
  3941. #endif
  3942. struct nk_user_font {
  3943. nk_handle userdata;
  3944. /* user provided font handle */
  3945. float height;
  3946. /* max height of the font */
  3947. nk_text_width_f width;
  3948. /* font string width in pixel callback */
  3949. #ifdef NK_INCLUDE_VERTEX_BUFFER_OUTPUT
  3950. nk_query_font_glyph_f query;
  3951. /* font glyph callback to query drawing info */
  3952. nk_handle texture;
  3953. /* texture handle to the used font atlas or texture */
  3954. #endif
  3955. };
  3956. #ifdef NK_INCLUDE_FONT_BAKING
  3957. enum nk_font_coord_type {
  3958. NK_COORD_UV, /* texture coordinates inside font glyphs are clamped between 0-1 */
  3959. NK_COORD_PIXEL /* texture coordinates inside font glyphs are in absolute pixel */
  3960. };
  3961. struct nk_font;
  3962. struct nk_baked_font {
  3963. float height;
  3964. /* height of the font */
  3965. float ascent, descent;
  3966. /* font glyphs ascent and descent */
  3967. nk_rune glyph_offset;
  3968. /* glyph array offset inside the font glyph baking output array */
  3969. nk_rune glyph_count;
  3970. /* number of glyphs of this font inside the glyph baking array output */
  3971. const nk_rune* ranges;
  3972. /* font codepoint ranges as pairs of (from/to) and 0 as last element */
  3973. };
  3974. struct nk_font_config {
  3975. struct nk_font_config* next;
  3976. /* NOTE: only used internally */
  3977. void* ttf_blob;
  3978. /* pointer to loaded TTF file memory block.
  3979. * NOTE: not needed for nk_font_atlas_add_from_memory and nk_font_atlas_add_from_file. */
  3980. nk_size ttf_size;
  3981. /* size of the loaded TTF file memory block
  3982. * NOTE: not needed for nk_font_atlas_add_from_memory and nk_font_atlas_add_from_file. */
  3983. unsigned char ttf_data_owned_by_atlas;
  3984. /* used inside font atlas: default to: 0*/
  3985. unsigned char merge_mode;
  3986. /* merges this font into the last font */
  3987. unsigned char pixel_snap;
  3988. /* align every character to pixel boundary (if true set oversample (1,1)) */
  3989. unsigned char oversample_v, oversample_h;
  3990. /* rasterize at high quality for sub-pixel position */
  3991. unsigned char padding[3];
  3992. float size;
  3993. /* baked pixel height of the font */
  3994. enum nk_font_coord_type coord_type;
  3995. /* texture coordinate format with either pixel or UV coordinates */
  3996. struct nk_vec2 spacing;
  3997. /* extra pixel spacing between glyphs */
  3998. const nk_rune* range;
  3999. /* list of unicode ranges (2 values per range, zero terminated) */
  4000. struct nk_baked_font* font;
  4001. /* font to setup in the baking process: NOTE: not needed for font atlas */
  4002. nk_rune fallback_glyph;
  4003. /* fallback glyph to use if a given rune is not found */
  4004. struct nk_font_config* n;
  4005. struct nk_font_config* p;
  4006. };
  4007. struct nk_font_glyph {
  4008. nk_rune codepoint;
  4009. float xadvance;
  4010. float x0, y0, x1, y1, w, h;
  4011. float u0, v0, u1, v1;
  4012. };
  4013. struct nk_font {
  4014. struct nk_font* next;
  4015. struct nk_user_font handle;
  4016. struct nk_baked_font info;
  4017. float scale;
  4018. struct nk_font_glyph* glyphs;
  4019. const struct nk_font_glyph* fallback;
  4020. nk_rune fallback_codepoint;
  4021. nk_handle texture;
  4022. struct nk_font_config* config;
  4023. };
  4024. enum nk_font_atlas_format {
  4025. NK_FONT_ATLAS_ALPHA8,
  4026. NK_FONT_ATLAS_RGBA32
  4027. };
  4028. struct nk_font_atlas {
  4029. void* pixel;
  4030. int tex_width;
  4031. int tex_height;
  4032. struct nk_allocator permanent;
  4033. struct nk_allocator temporary;
  4034. struct nk_recti custom;
  4035. struct nk_cursor cursors[NK_CURSOR_COUNT];
  4036. int glyph_count;
  4037. struct nk_font_glyph* glyphs;
  4038. struct nk_font* default_font;
  4039. struct nk_font* fonts;
  4040. struct nk_font_config* config;
  4041. int font_num;
  4042. };
  4043. /* some language glyph codepoint ranges */
  4044. NK_API const nk_rune* nk_font_default_glyph_ranges(void);
  4045. NK_API const nk_rune* nk_font_chinese_glyph_ranges(void);
  4046. NK_API const nk_rune* nk_font_cyrillic_glyph_ranges(void);
  4047. NK_API const nk_rune* nk_font_korean_glyph_ranges(void);
  4048. #ifdef NK_INCLUDE_DEFAULT_ALLOCATOR
  4049. NK_API void nk_font_atlas_init_default(struct nk_font_atlas*);
  4050. #endif
  4051. NK_API void nk_font_atlas_init(struct nk_font_atlas*, struct nk_allocator*);
  4052. NK_API void nk_font_atlas_init_custom(struct nk_font_atlas*, struct nk_allocator* persistent, struct nk_allocator* transient);
  4053. NK_API void nk_font_atlas_begin(struct nk_font_atlas*);
  4054. NK_API struct nk_font_config nk_font_config(float pixel_height);
  4055. NK_API struct nk_font* nk_font_atlas_add(struct nk_font_atlas*, const struct nk_font_config*);
  4056. #ifdef NK_INCLUDE_DEFAULT_FONT
  4057. NK_API struct nk_font* nk_font_atlas_add_default(struct nk_font_atlas*, float height, const struct nk_font_config*);
  4058. #endif
  4059. NK_API struct nk_font* nk_font_atlas_add_from_memory(struct nk_font_atlas* atlas, void* memory, nk_size size, float height, const struct nk_font_config* config);
  4060. #ifdef NK_INCLUDE_STANDARD_IO
  4061. NK_API struct nk_font* nk_font_atlas_add_from_file(struct nk_font_atlas* atlas, const char* file_path, float height, const struct nk_font_config*);
  4062. #endif
  4063. NK_API struct nk_font* nk_font_atlas_add_compressed(struct nk_font_atlas*, void* memory, nk_size size, float height, const struct nk_font_config*);
  4064. NK_API struct nk_font* nk_font_atlas_add_compressed_base85(struct nk_font_atlas*, const char* data, float height, const struct nk_font_config* config);
  4065. NK_API const void* nk_font_atlas_bake(struct nk_font_atlas*, int* width, int* height, enum nk_font_atlas_format);
  4066. NK_API void nk_font_atlas_end(struct nk_font_atlas*, nk_handle tex, struct nk_draw_null_texture*);
  4067. NK_API const struct nk_font_glyph* nk_font_find_glyph(struct nk_font*, nk_rune unicode);
  4068. NK_API void nk_font_atlas_cleanup(struct nk_font_atlas* atlas);
  4069. NK_API void nk_font_atlas_clear(struct nk_font_atlas*);
  4070. #endif
  4071. /* ==============================================================
  4072. *
  4073. * MEMORY BUFFER
  4074. *
  4075. * ===============================================================*/
  4076. /*/// ### Memory Buffer
  4077. /// A basic (double)-buffer with linear allocation and resetting as only
  4078. /// freeing policy. The buffer's main purpose is to control all memory management
  4079. /// inside the GUI toolkit and still leave memory control as much as possible in
  4080. /// the hand of the user while also making sure the library is easy to use if
  4081. /// not as much control is needed.
  4082. /// In general all memory inside this library can be provided from the user in
  4083. /// three different ways.
  4084. ///
  4085. /// The first way and the one providing most control is by just passing a fixed
  4086. /// size memory block. In this case all control lies in the hand of the user
  4087. /// since he can exactly control where the memory comes from and how much memory
  4088. /// the library should consume. Of course using the fixed size API removes the
  4089. /// ability to automatically resize a buffer if not enough memory is provided so
  4090. /// you have to take over the resizing. While being a fixed sized buffer sounds
  4091. /// quite limiting, it is very effective in this library since the actual memory
  4092. /// consumption is quite stable and has a fixed upper bound for a lot of cases.
  4093. ///
  4094. /// If you don't want to think about how much memory the library should allocate
  4095. /// at all time or have a very dynamic UI with unpredictable memory consumption
  4096. /// habits but still want control over memory allocation you can use the dynamic
  4097. /// allocator based API. The allocator consists of two callbacks for allocating
  4098. /// and freeing memory and optional userdata so you can plugin your own allocator.
  4099. ///
  4100. /// The final and easiest way can be used by defining
  4101. /// NK_INCLUDE_DEFAULT_ALLOCATOR which uses the standard library memory
  4102. /// allocation functions malloc and free and takes over complete control over
  4103. /// memory in this library.
  4104. */
  4105. struct nk_memory_status {
  4106. void* memory;
  4107. unsigned int type;
  4108. nk_size size;
  4109. nk_size allocated;
  4110. nk_size needed;
  4111. nk_size calls;
  4112. };
  4113. enum nk_allocation_type {
  4114. NK_BUFFER_FIXED,
  4115. NK_BUFFER_DYNAMIC
  4116. };
  4117. enum nk_buffer_allocation_type {
  4118. NK_BUFFER_FRONT,
  4119. NK_BUFFER_BACK,
  4120. NK_BUFFER_MAX
  4121. };
  4122. struct nk_buffer_marker {
  4123. nk_bool active;
  4124. nk_size offset;
  4125. };
  4126. struct nk_memory { void* ptr; nk_size size; };
  4127. struct nk_buffer {
  4128. struct nk_buffer_marker marker[NK_BUFFER_MAX];
  4129. /* buffer marker to free a buffer to a certain offset */
  4130. struct nk_allocator pool;
  4131. /* allocator callback for dynamic buffers */
  4132. enum nk_allocation_type type;
  4133. /* memory management type */
  4134. struct nk_memory memory;
  4135. /* memory and size of the current memory block */
  4136. float grow_factor;
  4137. /* growing factor for dynamic memory management */
  4138. nk_size allocated;
  4139. /* total amount of memory allocated */
  4140. nk_size needed;
  4141. /* totally consumed memory given that enough memory is present */
  4142. nk_size calls;
  4143. /* number of allocation calls */
  4144. nk_size size;
  4145. /* current size of the buffer */
  4146. };
  4147. #ifdef NK_INCLUDE_DEFAULT_ALLOCATOR
  4148. NK_API void nk_buffer_init_default(struct nk_buffer*);
  4149. #endif
  4150. NK_API void nk_buffer_init(struct nk_buffer*, const struct nk_allocator*, nk_size size);
  4151. NK_API void nk_buffer_init_fixed(struct nk_buffer*, void* memory, nk_size size);
  4152. NK_API void nk_buffer_info(struct nk_memory_status*, struct nk_buffer*);
  4153. NK_API void nk_buffer_push(struct nk_buffer*, enum nk_buffer_allocation_type type, const void* memory, nk_size size, nk_size align);
  4154. NK_API void nk_buffer_mark(struct nk_buffer*, enum nk_buffer_allocation_type type);
  4155. NK_API void nk_buffer_reset(struct nk_buffer*, enum nk_buffer_allocation_type type);
  4156. NK_API void nk_buffer_clear(struct nk_buffer*);
  4157. NK_API void nk_buffer_free(struct nk_buffer*);
  4158. NK_API void* nk_buffer_memory(struct nk_buffer*);
  4159. NK_API const void* nk_buffer_memory_const(const struct nk_buffer*);
  4160. NK_API nk_size nk_buffer_total(struct nk_buffer*);
  4161. /* ==============================================================
  4162. *
  4163. * STRING
  4164. *
  4165. * ===============================================================*/
  4166. /* Basic string buffer which is only used in context with the text editor
  4167. * to manage and manipulate dynamic or fixed size string content. This is _NOT_
  4168. * the default string handling method. The only instance you should have any contact
  4169. * with this API is if you interact with an `nk_text_edit` object inside one of the
  4170. * copy and paste functions and even there only for more advanced cases. */
  4171. struct nk_str {
  4172. struct nk_buffer buffer;
  4173. int len; /* in codepoints/runes/glyphs */
  4174. };
  4175. #ifdef NK_INCLUDE_DEFAULT_ALLOCATOR
  4176. NK_API void nk_str_init_default(struct nk_str*);
  4177. #endif
  4178. NK_API void nk_str_init(struct nk_str*, const struct nk_allocator*, nk_size size);
  4179. NK_API void nk_str_init_fixed(struct nk_str*, void* memory, nk_size size);
  4180. NK_API void nk_str_clear(struct nk_str*);
  4181. NK_API void nk_str_free(struct nk_str*);
  4182. NK_API int nk_str_append_text_char(struct nk_str*, const char*, int);
  4183. NK_API int nk_str_append_str_char(struct nk_str*, const char*);
  4184. NK_API int nk_str_append_text_utf8(struct nk_str*, const char*, int);
  4185. NK_API int nk_str_append_str_utf8(struct nk_str*, const char*);
  4186. NK_API int nk_str_append_text_runes(struct nk_str*, const nk_rune*, int);
  4187. NK_API int nk_str_append_str_runes(struct nk_str*, const nk_rune*);
  4188. NK_API int nk_str_insert_at_char(struct nk_str*, int pos, const char*, int);
  4189. NK_API int nk_str_insert_at_rune(struct nk_str*, int pos, const char*, int);
  4190. NK_API int nk_str_insert_text_char(struct nk_str*, int pos, const char*, int);
  4191. NK_API int nk_str_insert_str_char(struct nk_str*, int pos, const char*);
  4192. NK_API int nk_str_insert_text_utf8(struct nk_str*, int pos, const char*, int);
  4193. NK_API int nk_str_insert_str_utf8(struct nk_str*, int pos, const char*);
  4194. NK_API int nk_str_insert_text_runes(struct nk_str*, int pos, const nk_rune*, int);
  4195. NK_API int nk_str_insert_str_runes(struct nk_str*, int pos, const nk_rune*);
  4196. NK_API void nk_str_remove_chars(struct nk_str*, int len);
  4197. NK_API void nk_str_remove_runes(struct nk_str* str, int len);
  4198. NK_API void nk_str_delete_chars(struct nk_str*, int pos, int len);
  4199. NK_API void nk_str_delete_runes(struct nk_str*, int pos, int len);
  4200. NK_API char* nk_str_at_char(struct nk_str*, int pos);
  4201. NK_API char* nk_str_at_rune(struct nk_str*, int pos, nk_rune* unicode, int* len);
  4202. NK_API nk_rune nk_str_rune_at(const struct nk_str*, int pos);
  4203. NK_API const char* nk_str_at_char_const(const struct nk_str*, int pos);
  4204. NK_API const char* nk_str_at_const(const struct nk_str*, int pos, nk_rune* unicode, int* len);
  4205. NK_API char* nk_str_get(struct nk_str*);
  4206. NK_API const char* nk_str_get_const(const struct nk_str*);
  4207. NK_API int nk_str_len(struct nk_str*);
  4208. NK_API int nk_str_len_char(struct nk_str*);
  4209. /*===============================================================
  4210. *
  4211. * TEXT EDITOR
  4212. *
  4213. * ===============================================================*/
  4214. /*/// ### Text Editor
  4215. /// Editing text in this library is handled by either `nk_edit_string` or
  4216. /// `nk_edit_buffer`. But like almost everything in this library there are multiple
  4217. /// ways of doing it and a balance between control and ease of use with memory
  4218. /// as well as functionality controlled by flags.
  4219. ///
  4220. /// This library generally allows three different levels of memory control:
  4221. /// First of is the most basic way of just providing a simple char array with
  4222. /// string length. This method is probably the easiest way of handling simple
  4223. /// user text input. Main upside is complete control over memory while the biggest
  4224. /// downside in comparison with the other two approaches is missing undo/redo.
  4225. ///
  4226. /// For UIs that require undo/redo the second way was created. It is based on
  4227. /// a fixed size nk_text_edit struct, which has an internal undo/redo stack.
  4228. /// This is mainly useful if you want something more like a text editor but don't want
  4229. /// to have a dynamically growing buffer.
  4230. ///
  4231. /// The final way is using a dynamically growing nk_text_edit struct, which
  4232. /// has both a default version if you don't care where memory comes from and an
  4233. /// allocator version if you do. While the text editor is quite powerful for its
  4234. /// complexity I would not recommend editing gigabytes of data with it.
  4235. /// It is rather designed for uses cases which make sense for a GUI library not for
  4236. /// an full blown text editor.
  4237. */
  4238. #ifndef NK_TEXTEDIT_UNDOSTATECOUNT
  4239. #define NK_TEXTEDIT_UNDOSTATECOUNT 99
  4240. #endif
  4241. #ifndef NK_TEXTEDIT_UNDOCHARCOUNT
  4242. #define NK_TEXTEDIT_UNDOCHARCOUNT 999
  4243. #endif
  4244. struct nk_text_edit;
  4245. struct nk_clipboard {
  4246. nk_handle userdata;
  4247. nk_plugin_paste paste;
  4248. nk_plugin_copy copy;
  4249. };
  4250. struct nk_text_undo_record {
  4251. int where;
  4252. short insert_length;
  4253. short delete_length;
  4254. short char_storage;
  4255. };
  4256. struct nk_text_undo_state {
  4257. struct nk_text_undo_record undo_rec[NK_TEXTEDIT_UNDOSTATECOUNT];
  4258. nk_rune undo_char[NK_TEXTEDIT_UNDOCHARCOUNT];
  4259. short undo_point;
  4260. short redo_point;
  4261. short undo_char_point;
  4262. short redo_char_point;
  4263. };
  4264. enum nk_text_edit_type {
  4265. NK_TEXT_EDIT_SINGLE_LINE,
  4266. NK_TEXT_EDIT_MULTI_LINE
  4267. };
  4268. enum nk_text_edit_mode {
  4269. NK_TEXT_EDIT_MODE_VIEW,
  4270. NK_TEXT_EDIT_MODE_INSERT,
  4271. NK_TEXT_EDIT_MODE_REPLACE
  4272. };
  4273. struct nk_text_edit {
  4274. struct nk_clipboard clip;
  4275. struct nk_str string;
  4276. nk_plugin_filter filter;
  4277. struct nk_vec2 scrollbar;
  4278. int cursor;
  4279. int select_start;
  4280. int select_end;
  4281. unsigned char mode;
  4282. unsigned char cursor_at_end_of_line;
  4283. unsigned char initialized;
  4284. unsigned char has_preferred_x;
  4285. unsigned char single_line;
  4286. unsigned char active;
  4287. unsigned char padding1;
  4288. float preferred_x;
  4289. struct nk_text_undo_state undo;
  4290. };
  4291. /* filter function */
  4292. NK_API nk_bool nk_filter_default(const struct nk_text_edit*, nk_rune unicode);
  4293. NK_API nk_bool nk_filter_ascii(const struct nk_text_edit*, nk_rune unicode);
  4294. NK_API nk_bool nk_filter_float(const struct nk_text_edit*, nk_rune unicode);
  4295. NK_API nk_bool nk_filter_decimal(const struct nk_text_edit*, nk_rune unicode);
  4296. NK_API nk_bool nk_filter_hex(const struct nk_text_edit*, nk_rune unicode);
  4297. NK_API nk_bool nk_filter_oct(const struct nk_text_edit*, nk_rune unicode);
  4298. NK_API nk_bool nk_filter_binary(const struct nk_text_edit*, nk_rune unicode);
  4299. /* text editor */
  4300. #ifdef NK_INCLUDE_DEFAULT_ALLOCATOR
  4301. NK_API void nk_textedit_init_default(struct nk_text_edit*);
  4302. #endif
  4303. NK_API void nk_textedit_init(struct nk_text_edit*, struct nk_allocator*, nk_size size);
  4304. NK_API void nk_textedit_init_fixed(struct nk_text_edit*, void* memory, nk_size size);
  4305. NK_API void nk_textedit_free(struct nk_text_edit*);
  4306. NK_API void nk_textedit_text(struct nk_text_edit*, const char*, int total_len);
  4307. NK_API void nk_textedit_delete(struct nk_text_edit*, int where, int len);
  4308. NK_API void nk_textedit_delete_selection(struct nk_text_edit*);
  4309. NK_API void nk_textedit_select_all(struct nk_text_edit*);
  4310. NK_API nk_bool nk_textedit_cut(struct nk_text_edit*);
  4311. NK_API nk_bool nk_textedit_paste(struct nk_text_edit*, char const*, int len);
  4312. NK_API void nk_textedit_undo(struct nk_text_edit*);
  4313. NK_API void nk_textedit_redo(struct nk_text_edit*);
  4314. /* ===============================================================
  4315. *
  4316. * DRAWING
  4317. *
  4318. * ===============================================================*/
  4319. /*/// ### Drawing
  4320. /// This library was designed to be render backend agnostic so it does
  4321. /// not draw anything to screen. Instead all drawn shapes, widgets
  4322. /// are made of, are buffered into memory and make up a command queue.
  4323. /// Each frame therefore fills the command buffer with draw commands
  4324. /// that then need to be executed by the user and his own render backend.
  4325. /// After that the command buffer needs to be cleared and a new frame can be
  4326. /// started. It is probably important to note that the command buffer is the main
  4327. /// drawing API and the optional vertex buffer API only takes this format and
  4328. /// converts it into a hardware accessible format.
  4329. ///
  4330. /// To use the command queue to draw your own widgets you can access the
  4331. /// command buffer of each window by calling `nk_window_get_canvas` after
  4332. /// previously having called `nk_begin`:
  4333. ///
  4334. /// ```c
  4335. /// void draw_red_rectangle_widget(struct nk_context *ctx)
  4336. /// {
  4337. /// struct nk_command_buffer *canvas;
  4338. /// struct nk_input *input = &ctx->input;
  4339. /// canvas = nk_window_get_canvas(ctx);
  4340. ///
  4341. /// struct nk_rect space;
  4342. /// enum nk_widget_layout_states state;
  4343. /// state = nk_widget(&space, ctx);
  4344. /// if (!state) return;
  4345. ///
  4346. /// if (state != NK_WIDGET_ROM)
  4347. /// update_your_widget_by_user_input(...);
  4348. /// nk_fill_rect(canvas, space, 0, nk_rgb(255,0,0));
  4349. /// }
  4350. ///
  4351. /// if (nk_begin(...)) {
  4352. /// nk_layout_row_dynamic(ctx, 25, 1);
  4353. /// draw_red_rectangle_widget(ctx);
  4354. /// }
  4355. /// nk_end(..)
  4356. ///
  4357. /// ```
  4358. /// Important to know if you want to create your own widgets is the `nk_widget`
  4359. /// call. It allocates space on the panel reserved for this widget to be used,
  4360. /// but also returns the state of the widget space. If your widget is not seen and does
  4361. /// not have to be updated it is '0' and you can just return. If it only has
  4362. /// to be drawn the state will be `NK_WIDGET_ROM` otherwise you can do both
  4363. /// update and draw your widget. The reason for separating is to only draw and
  4364. /// update what is actually necessary which is crucial for performance.
  4365. */
  4366. enum nk_command_type {
  4367. NK_COMMAND_NOP,
  4368. NK_COMMAND_SCISSOR,
  4369. NK_COMMAND_LINE,
  4370. NK_COMMAND_CURVE,
  4371. NK_COMMAND_RECT,
  4372. NK_COMMAND_RECT_FILLED,
  4373. NK_COMMAND_RECT_MULTI_COLOR,
  4374. NK_COMMAND_CIRCLE,
  4375. NK_COMMAND_CIRCLE_FILLED,
  4376. NK_COMMAND_ARC,
  4377. NK_COMMAND_ARC_FILLED,
  4378. NK_COMMAND_TRIANGLE,
  4379. NK_COMMAND_TRIANGLE_FILLED,
  4380. NK_COMMAND_POLYGON,
  4381. NK_COMMAND_POLYGON_FILLED,
  4382. NK_COMMAND_POLYLINE,
  4383. NK_COMMAND_TEXT,
  4384. NK_COMMAND_IMAGE,
  4385. NK_COMMAND_CUSTOM
  4386. };
  4387. /* command base and header of every command inside the buffer */
  4388. struct nk_command {
  4389. enum nk_command_type type;
  4390. nk_size next;
  4391. #ifdef NK_INCLUDE_COMMAND_USERDATA
  4392. nk_handle userdata;
  4393. #endif
  4394. };
  4395. struct nk_command_scissor {
  4396. struct nk_command header;
  4397. short x, y;
  4398. unsigned short w, h;
  4399. };
  4400. struct nk_command_line {
  4401. struct nk_command header;
  4402. unsigned short line_thickness;
  4403. struct nk_vec2i begin;
  4404. struct nk_vec2i end;
  4405. struct nk_color color;
  4406. };
  4407. struct nk_command_curve {
  4408. struct nk_command header;
  4409. unsigned short line_thickness;
  4410. struct nk_vec2i begin;
  4411. struct nk_vec2i end;
  4412. struct nk_vec2i ctrl[2];
  4413. struct nk_color color;
  4414. };
  4415. struct nk_command_rect {
  4416. struct nk_command header;
  4417. unsigned short rounding;
  4418. unsigned short line_thickness;
  4419. short x, y;
  4420. unsigned short w, h;
  4421. struct nk_color color;
  4422. };
  4423. struct nk_command_rect_filled {
  4424. struct nk_command header;
  4425. unsigned short rounding;
  4426. short x, y;
  4427. unsigned short w, h;
  4428. struct nk_color color;
  4429. };
  4430. struct nk_command_rect_multi_color {
  4431. struct nk_command header;
  4432. short x, y;
  4433. unsigned short w, h;
  4434. struct nk_color left;
  4435. struct nk_color top;
  4436. struct nk_color bottom;
  4437. struct nk_color right;
  4438. };
  4439. struct nk_command_triangle {
  4440. struct nk_command header;
  4441. unsigned short line_thickness;
  4442. struct nk_vec2i a;
  4443. struct nk_vec2i b;
  4444. struct nk_vec2i c;
  4445. struct nk_color color;
  4446. };
  4447. struct nk_command_triangle_filled {
  4448. struct nk_command header;
  4449. struct nk_vec2i a;
  4450. struct nk_vec2i b;
  4451. struct nk_vec2i c;
  4452. struct nk_color color;
  4453. };
  4454. struct nk_command_circle {
  4455. struct nk_command header;
  4456. short x, y;
  4457. unsigned short line_thickness;
  4458. unsigned short w, h;
  4459. struct nk_color color;
  4460. };
  4461. struct nk_command_circle_filled {
  4462. struct nk_command header;
  4463. short x, y;
  4464. unsigned short w, h;
  4465. struct nk_color color;
  4466. };
  4467. struct nk_command_arc {
  4468. struct nk_command header;
  4469. short cx, cy;
  4470. unsigned short r;
  4471. unsigned short line_thickness;
  4472. float a[2];
  4473. struct nk_color color;
  4474. };
  4475. struct nk_command_arc_filled {
  4476. struct nk_command header;
  4477. short cx, cy;
  4478. unsigned short r;
  4479. float a[2];
  4480. struct nk_color color;
  4481. };
  4482. struct nk_command_polygon {
  4483. struct nk_command header;
  4484. struct nk_color color;
  4485. unsigned short line_thickness;
  4486. unsigned short point_count;
  4487. struct nk_vec2i points[1];
  4488. };
  4489. struct nk_command_polygon_filled {
  4490. struct nk_command header;
  4491. struct nk_color color;
  4492. unsigned short point_count;
  4493. struct nk_vec2i points[1];
  4494. };
  4495. struct nk_command_polyline {
  4496. struct nk_command header;
  4497. struct nk_color color;
  4498. unsigned short line_thickness;
  4499. unsigned short point_count;
  4500. struct nk_vec2i points[1];
  4501. };
  4502. struct nk_command_image {
  4503. struct nk_command header;
  4504. short x, y;
  4505. unsigned short w, h;
  4506. struct nk_image img;
  4507. struct nk_color col;
  4508. };
  4509. typedef void (*nk_command_custom_callback)(void* canvas, short x, short y,
  4510. unsigned short w, unsigned short h, nk_handle callback_data);
  4511. struct nk_command_custom {
  4512. struct nk_command header;
  4513. short x, y;
  4514. unsigned short w, h;
  4515. nk_handle callback_data;
  4516. nk_command_custom_callback callback;
  4517. };
  4518. struct nk_command_text {
  4519. struct nk_command header;
  4520. const struct nk_user_font* font;
  4521. struct nk_color background;
  4522. struct nk_color foreground;
  4523. short x, y;
  4524. unsigned short w, h;
  4525. float height;
  4526. int length;
  4527. char string[1];
  4528. };
  4529. enum nk_command_clipping {
  4530. NK_CLIPPING_OFF = nk_false,
  4531. NK_CLIPPING_ON = nk_true
  4532. };
  4533. struct nk_command_buffer {
  4534. struct nk_buffer* base;
  4535. struct nk_rect clip;
  4536. int use_clipping;
  4537. nk_handle userdata;
  4538. nk_size begin, end, last;
  4539. };
  4540. /* shape outlines */
  4541. NK_API void nk_stroke_line(struct nk_command_buffer* b, float x0, float y0, float x1, float y1, float line_thickness, struct nk_color);
  4542. NK_API void nk_stroke_curve(struct nk_command_buffer*, float, float, float, float, float, float, float, float, float line_thickness, struct nk_color);
  4543. NK_API void nk_stroke_rect(struct nk_command_buffer*, struct nk_rect, float rounding, float line_thickness, struct nk_color);
  4544. NK_API void nk_stroke_circle(struct nk_command_buffer*, struct nk_rect, float line_thickness, struct nk_color);
  4545. NK_API void nk_stroke_arc(struct nk_command_buffer*, float cx, float cy, float radius, float a_min, float a_max, float line_thickness, struct nk_color);
  4546. NK_API void nk_stroke_triangle(struct nk_command_buffer*, float, float, float, float, float, float, float line_thichness, struct nk_color);
  4547. NK_API void nk_stroke_polyline(struct nk_command_buffer*, float* points, int point_count, float line_thickness, struct nk_color col);
  4548. NK_API void nk_stroke_polygon(struct nk_command_buffer*, float*, int point_count, float line_thickness, struct nk_color);
  4549. /* filled shades */
  4550. NK_API void nk_fill_rect(struct nk_command_buffer*, struct nk_rect, float rounding, struct nk_color);
  4551. NK_API void nk_fill_rect_multi_color(struct nk_command_buffer*, struct nk_rect, struct nk_color left, struct nk_color top, struct nk_color right, struct nk_color bottom);
  4552. NK_API void nk_fill_circle(struct nk_command_buffer*, struct nk_rect, struct nk_color);
  4553. NK_API void nk_fill_arc(struct nk_command_buffer*, float cx, float cy, float radius, float a_min, float a_max, struct nk_color);
  4554. NK_API void nk_fill_triangle(struct nk_command_buffer*, float x0, float y0, float x1, float y1, float x2, float y2, struct nk_color);
  4555. NK_API void nk_fill_polygon(struct nk_command_buffer*, float*, int point_count, struct nk_color);
  4556. /* misc */
  4557. NK_API void nk_draw_image(struct nk_command_buffer*, struct nk_rect, const struct nk_image*, struct nk_color);
  4558. NK_API void nk_draw_nine_slice(struct nk_command_buffer*, struct nk_rect, const struct nk_nine_slice*, struct nk_color);
  4559. NK_API void nk_draw_text(struct nk_command_buffer*, struct nk_rect, const char* text, int len, const struct nk_user_font*, struct nk_color, struct nk_color);
  4560. NK_API void nk_push_scissor(struct nk_command_buffer*, struct nk_rect);
  4561. NK_API void nk_push_custom(struct nk_command_buffer*, struct nk_rect, nk_command_custom_callback, nk_handle usr);
  4562. /* ===============================================================
  4563. *
  4564. * INPUT
  4565. *
  4566. * ===============================================================*/
  4567. struct nk_mouse_button {
  4568. nk_bool down;
  4569. unsigned int clicked;
  4570. struct nk_vec2 clicked_pos;
  4571. };
  4572. struct nk_mouse {
  4573. struct nk_mouse_button buttons[NK_BUTTON_MAX];
  4574. struct nk_vec2 pos;
  4575. #ifdef NK_BUTTON_TRIGGER_ON_RELEASE
  4576. struct nk_vec2 down_pos;
  4577. #endif
  4578. struct nk_vec2 prev;
  4579. struct nk_vec2 delta;
  4580. struct nk_vec2 scroll_delta;
  4581. unsigned char grab;
  4582. unsigned char grabbed;
  4583. unsigned char ungrab;
  4584. };
  4585. struct nk_key {
  4586. nk_bool down;
  4587. unsigned int clicked;
  4588. };
  4589. struct nk_keyboard {
  4590. struct nk_key keys[NK_KEY_MAX];
  4591. char text[NK_INPUT_MAX];
  4592. int text_len;
  4593. };
  4594. struct nk_input {
  4595. struct nk_keyboard keyboard;
  4596. struct nk_mouse mouse;
  4597. };
  4598. NK_API nk_bool nk_input_has_mouse_click(const struct nk_input*, enum nk_buttons);
  4599. NK_API nk_bool nk_input_has_mouse_click_in_rect(const struct nk_input*, enum nk_buttons, struct nk_rect);
  4600. NK_API nk_bool nk_input_has_mouse_click_in_button_rect(const struct nk_input*, enum nk_buttons, struct nk_rect);
  4601. NK_API nk_bool nk_input_has_mouse_click_down_in_rect(const struct nk_input*, enum nk_buttons, struct nk_rect, nk_bool down);
  4602. NK_API nk_bool nk_input_is_mouse_click_in_rect(const struct nk_input*, enum nk_buttons, struct nk_rect);
  4603. NK_API nk_bool nk_input_is_mouse_click_down_in_rect(const struct nk_input* i, enum nk_buttons id, struct nk_rect b, nk_bool down);
  4604. NK_API nk_bool nk_input_any_mouse_click_in_rect(const struct nk_input*, struct nk_rect);
  4605. NK_API nk_bool nk_input_is_mouse_prev_hovering_rect(const struct nk_input*, struct nk_rect);
  4606. NK_API nk_bool nk_input_is_mouse_hovering_rect(const struct nk_input*, struct nk_rect);
  4607. NK_API nk_bool nk_input_mouse_clicked(const struct nk_input*, enum nk_buttons, struct nk_rect);
  4608. NK_API nk_bool nk_input_is_mouse_down(const struct nk_input*, enum nk_buttons);
  4609. NK_API nk_bool nk_input_is_mouse_pressed(const struct nk_input*, enum nk_buttons);
  4610. NK_API nk_bool nk_input_is_mouse_released(const struct nk_input*, enum nk_buttons);
  4611. NK_API nk_bool nk_input_is_key_pressed(const struct nk_input*, enum nk_keys);
  4612. NK_API nk_bool nk_input_is_key_released(const struct nk_input*, enum nk_keys);
  4613. NK_API nk_bool nk_input_is_key_down(const struct nk_input*, enum nk_keys);
  4614. /* ===============================================================
  4615. *
  4616. * DRAW LIST
  4617. *
  4618. * ===============================================================*/
  4619. #ifdef NK_INCLUDE_VERTEX_BUFFER_OUTPUT
  4620. /* ### Draw List
  4621. /// The optional vertex buffer draw list provides a 2D drawing context
  4622. /// with antialiasing functionality which takes basic filled or outlined shapes
  4623. /// or a path and outputs vertexes, elements and draw commands.
  4624. /// The actual draw list API is not required to be used directly while using this
  4625. /// library since converting the default library draw command output is done by
  4626. /// just calling `nk_convert` but I decided to still make this library accessible
  4627. /// since it can be useful.
  4628. ///
  4629. /// The draw list is based on a path buffering and polygon and polyline
  4630. /// rendering API which allows a lot of ways to draw 2D content to screen.
  4631. /// In fact it is probably more powerful than needed but allows even more crazy
  4632. /// things than this library provides by default.
  4633. */
  4634. #ifdef NK_UINT_DRAW_INDEX
  4635. typedef nk_uint nk_draw_index;
  4636. #else
  4637. typedef nk_ushort nk_draw_index;
  4638. #endif
  4639. enum nk_draw_list_stroke {
  4640. NK_STROKE_OPEN = nk_false,
  4641. /* build up path has no connection back to the beginning */
  4642. NK_STROKE_CLOSED = nk_true
  4643. /* build up path has a connection back to the beginning */
  4644. };
  4645. enum nk_draw_vertex_layout_attribute {
  4646. NK_VERTEX_POSITION,
  4647. NK_VERTEX_COLOR,
  4648. NK_VERTEX_TEXCOORD,
  4649. NK_VERTEX_ATTRIBUTE_COUNT
  4650. };
  4651. enum nk_draw_vertex_layout_format {
  4652. NK_FORMAT_SCHAR,
  4653. NK_FORMAT_SSHORT,
  4654. NK_FORMAT_SINT,
  4655. NK_FORMAT_UCHAR,
  4656. NK_FORMAT_USHORT,
  4657. NK_FORMAT_UINT,
  4658. NK_FORMAT_FLOAT,
  4659. NK_FORMAT_DOUBLE,
  4660. NK_FORMAT_COLOR_BEGIN,
  4661. NK_FORMAT_R8G8B8 = NK_FORMAT_COLOR_BEGIN,
  4662. NK_FORMAT_R16G15B16,
  4663. NK_FORMAT_R32G32B32,
  4664. NK_FORMAT_R8G8B8A8,
  4665. NK_FORMAT_B8G8R8A8,
  4666. NK_FORMAT_R16G15B16A16,
  4667. NK_FORMAT_R32G32B32A32,
  4668. NK_FORMAT_R32G32B32A32_FLOAT,
  4669. NK_FORMAT_R32G32B32A32_DOUBLE,
  4670. NK_FORMAT_RGB32,
  4671. NK_FORMAT_RGBA32,
  4672. NK_FORMAT_COLOR_END = NK_FORMAT_RGBA32,
  4673. NK_FORMAT_COUNT
  4674. };
  4675. #define NK_VERTEX_LAYOUT_END NK_VERTEX_ATTRIBUTE_COUNT,NK_FORMAT_COUNT,0
  4676. struct nk_draw_vertex_layout_element {
  4677. enum nk_draw_vertex_layout_attribute attribute;
  4678. enum nk_draw_vertex_layout_format format;
  4679. nk_size offset;
  4680. };
  4681. struct nk_draw_command {
  4682. unsigned int elem_count;
  4683. /* number of elements in the current draw batch */
  4684. struct nk_rect clip_rect;
  4685. /* current screen clipping rectangle */
  4686. nk_handle texture;
  4687. /* current texture to set */
  4688. #ifdef NK_INCLUDE_COMMAND_USERDATA
  4689. nk_handle userdata;
  4690. #endif
  4691. };
  4692. struct nk_draw_list {
  4693. struct nk_rect clip_rect;
  4694. struct nk_vec2 circle_vtx[12];
  4695. struct nk_convert_config config;
  4696. struct nk_buffer* buffer;
  4697. struct nk_buffer* vertices;
  4698. struct nk_buffer* elements;
  4699. unsigned int element_count;
  4700. unsigned int vertex_count;
  4701. unsigned int cmd_count;
  4702. nk_size cmd_offset;
  4703. unsigned int path_count;
  4704. unsigned int path_offset;
  4705. enum nk_anti_aliasing line_AA;
  4706. enum nk_anti_aliasing shape_AA;
  4707. #ifdef NK_INCLUDE_COMMAND_USERDATA
  4708. nk_handle userdata;
  4709. #endif
  4710. };
  4711. /* draw list */
  4712. NK_API void nk_draw_list_init(struct nk_draw_list*);
  4713. NK_API void nk_draw_list_setup(struct nk_draw_list*, const struct nk_convert_config*, struct nk_buffer* cmds, struct nk_buffer* vertices, struct nk_buffer* elements, enum nk_anti_aliasing line_aa, enum nk_anti_aliasing shape_aa);
  4714. /* drawing */
  4715. #define nk_draw_list_foreach(cmd, can, b) for((cmd)=nk__draw_list_begin(can, b); (cmd)!=0; (cmd)=nk__draw_list_next(cmd, b, can))
  4716. NK_API const struct nk_draw_command* nk__draw_list_begin(const struct nk_draw_list*, const struct nk_buffer*);
  4717. NK_API const struct nk_draw_command* nk__draw_list_next(const struct nk_draw_command*, const struct nk_buffer*, const struct nk_draw_list*);
  4718. NK_API const struct nk_draw_command* nk__draw_list_end(const struct nk_draw_list*, const struct nk_buffer*);
  4719. /* path */
  4720. NK_API void nk_draw_list_path_clear(struct nk_draw_list*);
  4721. NK_API void nk_draw_list_path_line_to(struct nk_draw_list*, struct nk_vec2 pos);
  4722. NK_API void nk_draw_list_path_arc_to_fast(struct nk_draw_list*, struct nk_vec2 center, float radius, int a_min, int a_max);
  4723. NK_API void nk_draw_list_path_arc_to(struct nk_draw_list*, struct nk_vec2 center, float radius, float a_min, float a_max, unsigned int segments);
  4724. NK_API void nk_draw_list_path_rect_to(struct nk_draw_list*, struct nk_vec2 a, struct nk_vec2 b, float rounding);
  4725. NK_API void nk_draw_list_path_curve_to(struct nk_draw_list*, struct nk_vec2 p2, struct nk_vec2 p3, struct nk_vec2 p4, unsigned int num_segments);
  4726. NK_API void nk_draw_list_path_fill(struct nk_draw_list*, struct nk_color);
  4727. NK_API void nk_draw_list_path_stroke(struct nk_draw_list*, struct nk_color, enum nk_draw_list_stroke closed, float thickness);
  4728. /* stroke */
  4729. NK_API void nk_draw_list_stroke_line(struct nk_draw_list*, struct nk_vec2 a, struct nk_vec2 b, struct nk_color, float thickness);
  4730. NK_API void nk_draw_list_stroke_rect(struct nk_draw_list*, struct nk_rect rect, struct nk_color, float rounding, float thickness);
  4731. NK_API void nk_draw_list_stroke_triangle(struct nk_draw_list*, struct nk_vec2 a, struct nk_vec2 b, struct nk_vec2 c, struct nk_color, float thickness);
  4732. NK_API void nk_draw_list_stroke_circle(struct nk_draw_list*, struct nk_vec2 center, float radius, struct nk_color, unsigned int segs, float thickness);
  4733. NK_API void nk_draw_list_stroke_curve(struct nk_draw_list*, struct nk_vec2 p0, struct nk_vec2 cp0, struct nk_vec2 cp1, struct nk_vec2 p1, struct nk_color, unsigned int segments, float thickness);
  4734. NK_API void nk_draw_list_stroke_poly_line(struct nk_draw_list*, const struct nk_vec2* pnts, const unsigned int cnt, struct nk_color, enum nk_draw_list_stroke, float thickness, enum nk_anti_aliasing);
  4735. /* fill */
  4736. NK_API void nk_draw_list_fill_rect(struct nk_draw_list*, struct nk_rect rect, struct nk_color, float rounding);
  4737. NK_API void nk_draw_list_fill_rect_multi_color(struct nk_draw_list*, struct nk_rect rect, struct nk_color left, struct nk_color top, struct nk_color right, struct nk_color bottom);
  4738. NK_API void nk_draw_list_fill_triangle(struct nk_draw_list*, struct nk_vec2 a, struct nk_vec2 b, struct nk_vec2 c, struct nk_color);
  4739. NK_API void nk_draw_list_fill_circle(struct nk_draw_list*, struct nk_vec2 center, float radius, struct nk_color col, unsigned int segs);
  4740. NK_API void nk_draw_list_fill_poly_convex(struct nk_draw_list*, const struct nk_vec2* points, const unsigned int count, struct nk_color, enum nk_anti_aliasing);
  4741. /* misc */
  4742. NK_API void nk_draw_list_add_image(struct nk_draw_list*, struct nk_image texture, struct nk_rect rect, struct nk_color);
  4743. NK_API void nk_draw_list_add_text(struct nk_draw_list*, const struct nk_user_font*, struct nk_rect, const char* text, int len, float font_height, struct nk_color);
  4744. #ifdef NK_INCLUDE_COMMAND_USERDATA
  4745. NK_API void nk_draw_list_push_userdata(struct nk_draw_list*, nk_handle userdata);
  4746. #endif
  4747. #endif
  4748. /* ===============================================================
  4749. *
  4750. * GUI
  4751. *
  4752. * ===============================================================*/
  4753. enum nk_style_item_type {
  4754. NK_STYLE_ITEM_COLOR,
  4755. NK_STYLE_ITEM_IMAGE,
  4756. NK_STYLE_ITEM_NINE_SLICE
  4757. };
  4758. union nk_style_item_data {
  4759. struct nk_color color;
  4760. struct nk_image image;
  4761. struct nk_nine_slice slice;
  4762. };
  4763. struct nk_style_item {
  4764. enum nk_style_item_type type;
  4765. union nk_style_item_data data;
  4766. };
  4767. struct nk_style_text {
  4768. struct nk_color color;
  4769. struct nk_vec2 padding;
  4770. };
  4771. struct nk_style_button {
  4772. /* background */
  4773. struct nk_style_item normal;
  4774. struct nk_style_item hover;
  4775. struct nk_style_item active;
  4776. struct nk_color border_color;
  4777. /* text */
  4778. struct nk_color text_background;
  4779. struct nk_color text_normal;
  4780. struct nk_color text_hover;
  4781. struct nk_color text_active;
  4782. nk_flags text_alignment;
  4783. /* properties */
  4784. float border;
  4785. float rounding;
  4786. struct nk_vec2 padding;
  4787. struct nk_vec2 image_padding;
  4788. struct nk_vec2 touch_padding;
  4789. /* optional user callbacks */
  4790. nk_handle userdata;
  4791. void(*draw_begin)(struct nk_command_buffer*, nk_handle userdata);
  4792. void(*draw_end)(struct nk_command_buffer*, nk_handle userdata);
  4793. };
  4794. struct nk_style_toggle {
  4795. /* background */
  4796. struct nk_style_item normal;
  4797. struct nk_style_item hover;
  4798. struct nk_style_item active;
  4799. struct nk_color border_color;
  4800. /* cursor */
  4801. struct nk_style_item cursor_normal;
  4802. struct nk_style_item cursor_hover;
  4803. /* text */
  4804. struct nk_color text_normal;
  4805. struct nk_color text_hover;
  4806. struct nk_color text_active;
  4807. struct nk_color text_background;
  4808. nk_flags text_alignment;
  4809. /* properties */
  4810. struct nk_vec2 padding;
  4811. struct nk_vec2 touch_padding;
  4812. float spacing;
  4813. float border;
  4814. /* optional user callbacks */
  4815. nk_handle userdata;
  4816. void(*draw_begin)(struct nk_command_buffer*, nk_handle);
  4817. void(*draw_end)(struct nk_command_buffer*, nk_handle);
  4818. };
  4819. struct nk_style_selectable {
  4820. /* background (inactive) */
  4821. struct nk_style_item normal;
  4822. struct nk_style_item hover;
  4823. struct nk_style_item pressed;
  4824. /* background (active) */
  4825. struct nk_style_item normal_active;
  4826. struct nk_style_item hover_active;
  4827. struct nk_style_item pressed_active;
  4828. /* text color (inactive) */
  4829. struct nk_color text_normal;
  4830. struct nk_color text_hover;
  4831. struct nk_color text_pressed;
  4832. /* text color (active) */
  4833. struct nk_color text_normal_active;
  4834. struct nk_color text_hover_active;
  4835. struct nk_color text_pressed_active;
  4836. struct nk_color text_background;
  4837. nk_flags text_alignment;
  4838. /* properties */
  4839. float rounding;
  4840. struct nk_vec2 padding;
  4841. struct nk_vec2 touch_padding;
  4842. struct nk_vec2 image_padding;
  4843. /* optional user callbacks */
  4844. nk_handle userdata;
  4845. void(*draw_begin)(struct nk_command_buffer*, nk_handle);
  4846. void(*draw_end)(struct nk_command_buffer*, nk_handle);
  4847. };
  4848. struct nk_style_slider {
  4849. /* background */
  4850. struct nk_style_item normal;
  4851. struct nk_style_item hover;
  4852. struct nk_style_item active;
  4853. struct nk_color border_color;
  4854. /* background bar */
  4855. struct nk_color bar_normal;
  4856. struct nk_color bar_hover;
  4857. struct nk_color bar_active;
  4858. struct nk_color bar_filled;
  4859. /* cursor */
  4860. struct nk_style_item cursor_normal;
  4861. struct nk_style_item cursor_hover;
  4862. struct nk_style_item cursor_active;
  4863. /* properties */
  4864. float border;
  4865. float rounding;
  4866. float bar_height;
  4867. struct nk_vec2 padding;
  4868. struct nk_vec2 spacing;
  4869. struct nk_vec2 cursor_size;
  4870. /* optional buttons */
  4871. int show_buttons;
  4872. struct nk_style_button inc_button;
  4873. struct nk_style_button dec_button;
  4874. enum nk_symbol_type inc_symbol;
  4875. enum nk_symbol_type dec_symbol;
  4876. /* optional user callbacks */
  4877. nk_handle userdata;
  4878. void(*draw_begin)(struct nk_command_buffer*, nk_handle);
  4879. void(*draw_end)(struct nk_command_buffer*, nk_handle);
  4880. };
  4881. struct nk_style_progress {
  4882. /* background */
  4883. struct nk_style_item normal;
  4884. struct nk_style_item hover;
  4885. struct nk_style_item active;
  4886. struct nk_color border_color;
  4887. /* cursor */
  4888. struct nk_style_item cursor_normal;
  4889. struct nk_style_item cursor_hover;
  4890. struct nk_style_item cursor_active;
  4891. struct nk_color cursor_border_color;
  4892. /* properties */
  4893. float rounding;
  4894. float border;
  4895. float cursor_border;
  4896. float cursor_rounding;
  4897. struct nk_vec2 padding;
  4898. /* optional user callbacks */
  4899. nk_handle userdata;
  4900. void(*draw_begin)(struct nk_command_buffer*, nk_handle);
  4901. void(*draw_end)(struct nk_command_buffer*, nk_handle);
  4902. };
  4903. struct nk_style_scrollbar {
  4904. /* background */
  4905. struct nk_style_item normal;
  4906. struct nk_style_item hover;
  4907. struct nk_style_item active;
  4908. struct nk_color border_color;
  4909. /* cursor */
  4910. struct nk_style_item cursor_normal;
  4911. struct nk_style_item cursor_hover;
  4912. struct nk_style_item cursor_active;
  4913. struct nk_color cursor_border_color;
  4914. /* properties */
  4915. float border;
  4916. float rounding;
  4917. float border_cursor;
  4918. float rounding_cursor;
  4919. struct nk_vec2 padding;
  4920. /* optional buttons */
  4921. int show_buttons;
  4922. struct nk_style_button inc_button;
  4923. struct nk_style_button dec_button;
  4924. enum nk_symbol_type inc_symbol;
  4925. enum nk_symbol_type dec_symbol;
  4926. /* optional user callbacks */
  4927. nk_handle userdata;
  4928. void(*draw_begin)(struct nk_command_buffer*, nk_handle);
  4929. void(*draw_end)(struct nk_command_buffer*, nk_handle);
  4930. };
  4931. struct nk_style_edit {
  4932. /* background */
  4933. struct nk_style_item normal;
  4934. struct nk_style_item hover;
  4935. struct nk_style_item active;
  4936. struct nk_color border_color;
  4937. struct nk_style_scrollbar scrollbar;
  4938. /* cursor */
  4939. struct nk_color cursor_normal;
  4940. struct nk_color cursor_hover;
  4941. struct nk_color cursor_text_normal;
  4942. struct nk_color cursor_text_hover;
  4943. /* text (unselected) */
  4944. struct nk_color text_normal;
  4945. struct nk_color text_hover;
  4946. struct nk_color text_active;
  4947. /* text (selected) */
  4948. struct nk_color selected_normal;
  4949. struct nk_color selected_hover;
  4950. struct nk_color selected_text_normal;
  4951. struct nk_color selected_text_hover;
  4952. /* properties */
  4953. float border;
  4954. float rounding;
  4955. float cursor_size;
  4956. struct nk_vec2 scrollbar_size;
  4957. struct nk_vec2 padding;
  4958. float row_padding;
  4959. };
  4960. struct nk_style_property {
  4961. /* background */
  4962. struct nk_style_item normal;
  4963. struct nk_style_item hover;
  4964. struct nk_style_item active;
  4965. struct nk_color border_color;
  4966. /* text */
  4967. struct nk_color label_normal;
  4968. struct nk_color label_hover;
  4969. struct nk_color label_active;
  4970. /* symbols */
  4971. enum nk_symbol_type sym_left;
  4972. enum nk_symbol_type sym_right;
  4973. /* properties */
  4974. float border;
  4975. float rounding;
  4976. struct nk_vec2 padding;
  4977. struct nk_style_edit edit;
  4978. struct nk_style_button inc_button;
  4979. struct nk_style_button dec_button;
  4980. /* optional user callbacks */
  4981. nk_handle userdata;
  4982. void(*draw_begin)(struct nk_command_buffer*, nk_handle);
  4983. void(*draw_end)(struct nk_command_buffer*, nk_handle);
  4984. };
  4985. struct nk_style_chart {
  4986. /* colors */
  4987. struct nk_style_item background;
  4988. struct nk_color border_color;
  4989. struct nk_color selected_color;
  4990. struct nk_color color;
  4991. /* properties */
  4992. float border;
  4993. float rounding;
  4994. struct nk_vec2 padding;
  4995. };
  4996. struct nk_style_combo {
  4997. /* background */
  4998. struct nk_style_item normal;
  4999. struct nk_style_item hover;
  5000. struct nk_style_item active;
  5001. struct nk_color border_color;
  5002. /* label */
  5003. struct nk_color label_normal;
  5004. struct nk_color label_hover;
  5005. struct nk_color label_active;
  5006. /* symbol */
  5007. struct nk_color symbol_normal;
  5008. struct nk_color symbol_hover;
  5009. struct nk_color symbol_active;
  5010. /* button */
  5011. struct nk_style_button button;
  5012. enum nk_symbol_type sym_normal;
  5013. enum nk_symbol_type sym_hover;
  5014. enum nk_symbol_type sym_active;
  5015. /* properties */
  5016. float border;
  5017. float rounding;
  5018. struct nk_vec2 content_padding;
  5019. struct nk_vec2 button_padding;
  5020. struct nk_vec2 spacing;
  5021. };
  5022. struct nk_style_tab {
  5023. /* background */
  5024. struct nk_style_item background;
  5025. struct nk_color border_color;
  5026. struct nk_color text;
  5027. /* button */
  5028. struct nk_style_button tab_maximize_button;
  5029. struct nk_style_button tab_minimize_button;
  5030. struct nk_style_button node_maximize_button;
  5031. struct nk_style_button node_minimize_button;
  5032. enum nk_symbol_type sym_minimize;
  5033. enum nk_symbol_type sym_maximize;
  5034. /* properties */
  5035. float border;
  5036. float rounding;
  5037. float indent;
  5038. struct nk_vec2 padding;
  5039. struct nk_vec2 spacing;
  5040. };
  5041. enum nk_style_header_align {
  5042. NK_HEADER_LEFT,
  5043. NK_HEADER_RIGHT
  5044. };
  5045. struct nk_style_window_header {
  5046. /* background */
  5047. struct nk_style_item normal;
  5048. struct nk_style_item hover;
  5049. struct nk_style_item active;
  5050. /* button */
  5051. struct nk_style_button close_button;
  5052. struct nk_style_button minimize_button;
  5053. enum nk_symbol_type close_symbol;
  5054. enum nk_symbol_type minimize_symbol;
  5055. enum nk_symbol_type maximize_symbol;
  5056. /* title */
  5057. struct nk_color label_normal;
  5058. struct nk_color label_hover;
  5059. struct nk_color label_active;
  5060. /* properties */
  5061. enum nk_style_header_align align;
  5062. struct nk_vec2 padding;
  5063. struct nk_vec2 label_padding;
  5064. struct nk_vec2 spacing;
  5065. };
  5066. struct nk_style_window {
  5067. struct nk_style_window_header header;
  5068. struct nk_style_item fixed_background;
  5069. struct nk_color background;
  5070. struct nk_color border_color;
  5071. struct nk_color popup_border_color;
  5072. struct nk_color combo_border_color;
  5073. struct nk_color contextual_border_color;
  5074. struct nk_color menu_border_color;
  5075. struct nk_color group_border_color;
  5076. struct nk_color tooltip_border_color;
  5077. struct nk_style_item scaler;
  5078. float border;
  5079. float combo_border;
  5080. float contextual_border;
  5081. float menu_border;
  5082. float group_border;
  5083. float tooltip_border;
  5084. float popup_border;
  5085. float min_row_height_padding;
  5086. float rounding;
  5087. struct nk_vec2 spacing;
  5088. struct nk_vec2 scrollbar_size;
  5089. struct nk_vec2 min_size;
  5090. struct nk_vec2 padding;
  5091. struct nk_vec2 group_padding;
  5092. struct nk_vec2 popup_padding;
  5093. struct nk_vec2 combo_padding;
  5094. struct nk_vec2 contextual_padding;
  5095. struct nk_vec2 menu_padding;
  5096. struct nk_vec2 tooltip_padding;
  5097. };
  5098. struct nk_style {
  5099. const struct nk_user_font* font;
  5100. const struct nk_cursor* cursors[NK_CURSOR_COUNT];
  5101. const struct nk_cursor* cursor_active;
  5102. struct nk_cursor* cursor_last;
  5103. int cursor_visible;
  5104. struct nk_style_text text;
  5105. struct nk_style_button button;
  5106. struct nk_style_button contextual_button;
  5107. struct nk_style_button menu_button;
  5108. struct nk_style_toggle option;
  5109. struct nk_style_toggle checkbox;
  5110. struct nk_style_selectable selectable;
  5111. struct nk_style_slider slider;
  5112. struct nk_style_progress progress;
  5113. struct nk_style_property property;
  5114. struct nk_style_edit edit;
  5115. struct nk_style_chart chart;
  5116. struct nk_style_scrollbar scrollh;
  5117. struct nk_style_scrollbar scrollv;
  5118. struct nk_style_tab tab;
  5119. struct nk_style_combo combo;
  5120. struct nk_style_window window;
  5121. };
  5122. NK_API struct nk_style_item nk_style_item_color(struct nk_color);
  5123. NK_API struct nk_style_item nk_style_item_image(struct nk_image img);
  5124. NK_API struct nk_style_item nk_style_item_nine_slice(struct nk_nine_slice slice);
  5125. NK_API struct nk_style_item nk_style_item_hide(void);
  5126. /*==============================================================
  5127. * PANEL
  5128. * =============================================================*/
  5129. #ifndef NK_MAX_LAYOUT_ROW_TEMPLATE_COLUMNS
  5130. #define NK_MAX_LAYOUT_ROW_TEMPLATE_COLUMNS 16
  5131. #endif
  5132. #ifndef NK_CHART_MAX_SLOT
  5133. #define NK_CHART_MAX_SLOT 4
  5134. #endif
  5135. enum nk_panel_type {
  5136. NK_PANEL_NONE = 0,
  5137. NK_PANEL_WINDOW = NK_FLAG(0),
  5138. NK_PANEL_GROUP = NK_FLAG(1),
  5139. NK_PANEL_POPUP = NK_FLAG(2),
  5140. NK_PANEL_CONTEXTUAL = NK_FLAG(4),
  5141. NK_PANEL_COMBO = NK_FLAG(5),
  5142. NK_PANEL_MENU = NK_FLAG(6),
  5143. NK_PANEL_TOOLTIP = NK_FLAG(7)
  5144. };
  5145. enum nk_panel_set {
  5146. NK_PANEL_SET_NONBLOCK = NK_PANEL_CONTEXTUAL | NK_PANEL_COMBO | NK_PANEL_MENU | NK_PANEL_TOOLTIP,
  5147. NK_PANEL_SET_POPUP = NK_PANEL_SET_NONBLOCK | NK_PANEL_POPUP,
  5148. NK_PANEL_SET_SUB = NK_PANEL_SET_POPUP | NK_PANEL_GROUP
  5149. };
  5150. struct nk_chart_slot {
  5151. enum nk_chart_type type;
  5152. struct nk_color color;
  5153. struct nk_color highlight;
  5154. float min, max, range;
  5155. int count;
  5156. struct nk_vec2 last;
  5157. int index;
  5158. };
  5159. struct nk_chart {
  5160. int slot;
  5161. float x, y, w, h;
  5162. struct nk_chart_slot slots[NK_CHART_MAX_SLOT];
  5163. };
  5164. enum nk_panel_row_layout_type {
  5165. NK_LAYOUT_DYNAMIC_FIXED = 0,
  5166. NK_LAYOUT_DYNAMIC_ROW,
  5167. NK_LAYOUT_DYNAMIC_FREE,
  5168. NK_LAYOUT_DYNAMIC,
  5169. NK_LAYOUT_STATIC_FIXED,
  5170. NK_LAYOUT_STATIC_ROW,
  5171. NK_LAYOUT_STATIC_FREE,
  5172. NK_LAYOUT_STATIC,
  5173. NK_LAYOUT_TEMPLATE,
  5174. NK_LAYOUT_COUNT
  5175. };
  5176. struct nk_row_layout {
  5177. enum nk_panel_row_layout_type type;
  5178. int index;
  5179. float height;
  5180. float min_height;
  5181. int columns;
  5182. const float* ratio;
  5183. float item_width;
  5184. float item_height;
  5185. float item_offset;
  5186. float filled;
  5187. struct nk_rect item;
  5188. int tree_depth;
  5189. float templates[NK_MAX_LAYOUT_ROW_TEMPLATE_COLUMNS];
  5190. };
  5191. struct nk_popup_buffer {
  5192. nk_size begin;
  5193. nk_size parent;
  5194. nk_size last;
  5195. nk_size end;
  5196. nk_bool active;
  5197. };
  5198. struct nk_menu_state {
  5199. float x, y, w, h;
  5200. struct nk_scroll offset;
  5201. };
  5202. struct nk_panel {
  5203. enum nk_panel_type type;
  5204. nk_flags flags;
  5205. struct nk_rect bounds;
  5206. nk_uint* offset_x;
  5207. nk_uint* offset_y;
  5208. float at_x, at_y, max_x;
  5209. float footer_height;
  5210. float header_height;
  5211. float border;
  5212. unsigned int has_scrolling;
  5213. struct nk_rect clip;
  5214. struct nk_menu_state menu;
  5215. struct nk_row_layout row;
  5216. struct nk_chart chart;
  5217. struct nk_command_buffer* buffer;
  5218. struct nk_panel* parent;
  5219. };
  5220. /*==============================================================
  5221. * WINDOW
  5222. * =============================================================*/
  5223. #ifndef NK_WINDOW_MAX_NAME
  5224. #define NK_WINDOW_MAX_NAME 64
  5225. #endif
  5226. struct nk_table;
  5227. enum nk_window_flags {
  5228. NK_WINDOW_PRIVATE = NK_FLAG(11),
  5229. NK_WINDOW_DYNAMIC = NK_WINDOW_PRIVATE,
  5230. /* special window type growing up in height while being filled to a certain maximum height */
  5231. NK_WINDOW_ROM = NK_FLAG(12),
  5232. /* sets window widgets into a read only mode and does not allow input changes */
  5233. NK_WINDOW_NOT_INTERACTIVE = NK_WINDOW_ROM | NK_WINDOW_NO_INPUT,
  5234. /* prevents all interaction caused by input to either window or widgets inside */
  5235. NK_WINDOW_HIDDEN = NK_FLAG(13),
  5236. /* Hides window and stops any window interaction and drawing */
  5237. NK_WINDOW_CLOSED = NK_FLAG(14),
  5238. /* Directly closes and frees the window at the end of the frame */
  5239. NK_WINDOW_MINIMIZED = NK_FLAG(15),
  5240. /* marks the window as minimized */
  5241. NK_WINDOW_REMOVE_ROM = NK_FLAG(16)
  5242. /* Removes read only mode at the end of the window */
  5243. };
  5244. struct nk_popup_state {
  5245. struct nk_window* win;
  5246. enum nk_panel_type type;
  5247. struct nk_popup_buffer buf;
  5248. nk_hash name;
  5249. nk_bool active;
  5250. unsigned combo_count;
  5251. unsigned con_count, con_old;
  5252. unsigned active_con;
  5253. struct nk_rect header;
  5254. };
  5255. struct nk_edit_state {
  5256. nk_hash name;
  5257. unsigned int seq;
  5258. unsigned int old;
  5259. int active, prev;
  5260. int cursor;
  5261. int sel_start;
  5262. int sel_end;
  5263. struct nk_scroll scrollbar;
  5264. unsigned char mode;
  5265. unsigned char single_line;
  5266. };
  5267. struct nk_property_state {
  5268. int active, prev;
  5269. char buffer[NK_MAX_NUMBER_BUFFER];
  5270. int length;
  5271. int cursor;
  5272. int select_start;
  5273. int select_end;
  5274. nk_hash name;
  5275. unsigned int seq;
  5276. unsigned int old;
  5277. int state;
  5278. };
  5279. struct nk_window {
  5280. unsigned int seq;
  5281. nk_hash name;
  5282. char name_string[NK_WINDOW_MAX_NAME];
  5283. nk_flags flags;
  5284. struct nk_rect bounds;
  5285. struct nk_scroll scrollbar;
  5286. struct nk_command_buffer buffer;
  5287. struct nk_panel* layout;
  5288. float scrollbar_hiding_timer;
  5289. /* persistent widget state */
  5290. struct nk_property_state property;
  5291. struct nk_popup_state popup;
  5292. struct nk_edit_state edit;
  5293. unsigned int scrolled;
  5294. struct nk_table* tables;
  5295. unsigned int table_count;
  5296. /* window list hooks */
  5297. struct nk_window* next;
  5298. struct nk_window* prev;
  5299. struct nk_window* parent;
  5300. };
  5301. /*==============================================================
  5302. * STACK
  5303. * =============================================================*/
  5304. /*/// ### Stack
  5305. /// The style modifier stack can be used to temporarily change a
  5306. /// property inside `nk_style`. For example if you want a special
  5307. /// red button you can temporarily push the old button color onto a stack
  5308. /// draw the button with a red color and then you just pop the old color
  5309. /// back from the stack:
  5310. ///
  5311. /// nk_style_push_style_item(ctx, &ctx->style.button.normal, nk_style_item_color(nk_rgb(255,0,0)));
  5312. /// nk_style_push_style_item(ctx, &ctx->style.button.hover, nk_style_item_color(nk_rgb(255,0,0)));
  5313. /// nk_style_push_style_item(ctx, &ctx->style.button.active, nk_style_item_color(nk_rgb(255,0,0)));
  5314. /// nk_style_push_vec2(ctx, &cx->style.button.padding, nk_vec2(2,2));
  5315. ///
  5316. /// nk_button(...);
  5317. ///
  5318. /// nk_style_pop_style_item(ctx);
  5319. /// nk_style_pop_style_item(ctx);
  5320. /// nk_style_pop_style_item(ctx);
  5321. /// nk_style_pop_vec2(ctx);
  5322. ///
  5323. /// Nuklear has a stack for style_items, float properties, vector properties,
  5324. /// flags, colors, fonts and for button_behavior. Each has it's own fixed size stack
  5325. /// which can be changed at compile time.
  5326. */
  5327. #ifndef NK_BUTTON_BEHAVIOR_STACK_SIZE
  5328. #define NK_BUTTON_BEHAVIOR_STACK_SIZE 8
  5329. #endif
  5330. #ifndef NK_FONT_STACK_SIZE
  5331. #define NK_FONT_STACK_SIZE 8
  5332. #endif
  5333. #ifndef NK_STYLE_ITEM_STACK_SIZE
  5334. #define NK_STYLE_ITEM_STACK_SIZE 16
  5335. #endif
  5336. #ifndef NK_FLOAT_STACK_SIZE
  5337. #define NK_FLOAT_STACK_SIZE 32
  5338. #endif
  5339. #ifndef NK_VECTOR_STACK_SIZE
  5340. #define NK_VECTOR_STACK_SIZE 16
  5341. #endif
  5342. #ifndef NK_FLAGS_STACK_SIZE
  5343. #define NK_FLAGS_STACK_SIZE 32
  5344. #endif
  5345. #ifndef NK_COLOR_STACK_SIZE
  5346. #define NK_COLOR_STACK_SIZE 32
  5347. #endif
  5348. #define NK_CONFIGURATION_STACK_TYPE(prefix, name, type)\
  5349. struct nk_config_stack_##name##_element {\
  5350. prefix##_##type *address;\
  5351. prefix##_##type old_value;\
  5352. }
  5353. #define NK_CONFIG_STACK(type,size)\
  5354. struct nk_config_stack_##type {\
  5355. int head;\
  5356. struct nk_config_stack_##type##_element elements[size];\
  5357. }
  5358. #define nk_float float
  5359. NK_CONFIGURATION_STACK_TYPE(struct nk, style_item, style_item);
  5360. NK_CONFIGURATION_STACK_TYPE(nk, float, float);
  5361. NK_CONFIGURATION_STACK_TYPE(struct nk, vec2, vec2);
  5362. NK_CONFIGURATION_STACK_TYPE(nk, flags, flags);
  5363. NK_CONFIGURATION_STACK_TYPE(struct nk, color, color);
  5364. NK_CONFIGURATION_STACK_TYPE(const struct nk, user_font, user_font*);
  5365. NK_CONFIGURATION_STACK_TYPE(enum nk, button_behavior, button_behavior);
  5366. NK_CONFIG_STACK(style_item, NK_STYLE_ITEM_STACK_SIZE);
  5367. NK_CONFIG_STACK(float, NK_FLOAT_STACK_SIZE);
  5368. NK_CONFIG_STACK(vec2, NK_VECTOR_STACK_SIZE);
  5369. NK_CONFIG_STACK(flags, NK_FLAGS_STACK_SIZE);
  5370. NK_CONFIG_STACK(color, NK_COLOR_STACK_SIZE);
  5371. NK_CONFIG_STACK(user_font, NK_FONT_STACK_SIZE);
  5372. NK_CONFIG_STACK(button_behavior, NK_BUTTON_BEHAVIOR_STACK_SIZE);
  5373. struct nk_configuration_stacks {
  5374. struct nk_config_stack_style_item style_items;
  5375. struct nk_config_stack_float floats;
  5376. struct nk_config_stack_vec2 vectors;
  5377. struct nk_config_stack_flags flags;
  5378. struct nk_config_stack_color colors;
  5379. struct nk_config_stack_user_font fonts;
  5380. struct nk_config_stack_button_behavior button_behaviors;
  5381. };
  5382. /*==============================================================
  5383. * CONTEXT
  5384. * =============================================================*/
  5385. #define NK_VALUE_PAGE_CAPACITY \
  5386. (((NK_MAX(sizeof(struct nk_window),sizeof(struct nk_panel)) / sizeof(nk_uint))) / 2)
  5387. struct nk_table {
  5388. unsigned int seq;
  5389. unsigned int size;
  5390. nk_hash keys[NK_VALUE_PAGE_CAPACITY];
  5391. nk_uint values[NK_VALUE_PAGE_CAPACITY];
  5392. struct nk_table* next, * prev;
  5393. };
  5394. union nk_page_data {
  5395. struct nk_table tbl;
  5396. struct nk_panel pan;
  5397. struct nk_window win;
  5398. };
  5399. struct nk_page_element {
  5400. union nk_page_data data;
  5401. struct nk_page_element* next;
  5402. struct nk_page_element* prev;
  5403. };
  5404. struct nk_page {
  5405. unsigned int size;
  5406. struct nk_page* next;
  5407. struct nk_page_element win[1];
  5408. };
  5409. struct nk_pool {
  5410. struct nk_allocator alloc;
  5411. enum nk_allocation_type type;
  5412. unsigned int page_count;
  5413. struct nk_page* pages;
  5414. struct nk_page_element* freelist;
  5415. unsigned capacity;
  5416. nk_size size;
  5417. nk_size cap;
  5418. };
  5419. struct nk_context {
  5420. /* public: can be accessed freely */
  5421. struct nk_input input;
  5422. struct nk_style style;
  5423. struct nk_buffer memory;
  5424. struct nk_clipboard clip;
  5425. nk_flags last_widget_state;
  5426. enum nk_button_behavior button_behavior;
  5427. struct nk_configuration_stacks stacks;
  5428. float delta_time_seconds;
  5429. /* private:
  5430. should only be accessed if you
  5431. know what you are doing */
  5432. #ifdef NK_INCLUDE_VERTEX_BUFFER_OUTPUT
  5433. struct nk_draw_list draw_list;
  5434. #endif
  5435. #ifdef NK_INCLUDE_COMMAND_USERDATA
  5436. nk_handle userdata;
  5437. #endif
  5438. /* text editor objects are quite big because of an internal
  5439. * undo/redo stack. Therefore it does not make sense to have one for
  5440. * each window for temporary use cases, so I only provide *one* instance
  5441. * for all windows. This works because the content is cleared anyway */
  5442. struct nk_text_edit text_edit;
  5443. /* draw buffer used for overlay drawing operation like cursor */
  5444. struct nk_command_buffer overlay;
  5445. /* windows */
  5446. int build;
  5447. int use_pool;
  5448. struct nk_pool pool;
  5449. struct nk_window* begin;
  5450. struct nk_window* end;
  5451. struct nk_window* active;
  5452. struct nk_window* current;
  5453. struct nk_page_element* freelist;
  5454. unsigned int count;
  5455. unsigned int seq;
  5456. };
  5457. /* ==============================================================
  5458. * MATH
  5459. * =============================================================== */
  5460. #define NK_PI 3.141592654f
  5461. #define NK_UTF_INVALID 0xFFFD
  5462. #define NK_MAX_FLOAT_PRECISION 2
  5463. #define NK_UNUSED(x) ((void)(x))
  5464. #define NK_SATURATE(x) (NK_MAX(0, NK_MIN(1.0f, x)))
  5465. #define NK_LEN(a) (sizeof(a)/sizeof(a)[0])
  5466. #define NK_ABS(a) (((a) < 0) ? -(a) : (a))
  5467. #define NK_BETWEEN(x, a, b) ((a) <= (x) && (x) < (b))
  5468. #define NK_INBOX(px, py, x, y, w, h)\
  5469. (NK_BETWEEN(px,x,x+w) && NK_BETWEEN(py,y,y+h))
  5470. #define NK_INTERSECT(x0, y0, w0, h0, x1, y1, w1, h1) \
  5471. ((x1 < (x0 + w0)) && (x0 < (x1 + w1)) && \
  5472. (y1 < (y0 + h0)) && (y0 < (y1 + h1)))
  5473. #define NK_CONTAINS(x, y, w, h, bx, by, bw, bh)\
  5474. (NK_INBOX(x,y, bx, by, bw, bh) && NK_INBOX(x+w,y+h, bx, by, bw, bh))
  5475. #define nk_vec2_sub(a, b) nk_vec2((a).x - (b).x, (a).y - (b).y)
  5476. #define nk_vec2_add(a, b) nk_vec2((a).x + (b).x, (a).y + (b).y)
  5477. #define nk_vec2_len_sqr(a) ((a).x*(a).x+(a).y*(a).y)
  5478. #define nk_vec2_muls(a, t) nk_vec2((a).x * (t), (a).y * (t))
  5479. #define nk_ptr_add(t, p, i) ((t*)((void*)((nk_byte*)(p) + (i))))
  5480. #define nk_ptr_add_const(t, p, i) ((const t*)((const void*)((const nk_byte*)(p) + (i))))
  5481. #define nk_zero_struct(s) nk_zero(&s, sizeof(s))
  5482. /* ==============================================================
  5483. * ALIGNMENT
  5484. * =============================================================== */
  5485. /* Pointer to Integer type conversion for pointer alignment */
  5486. #if defined(__PTRDIFF_TYPE__) /* This case should work for GCC*/
  5487. # define NK_UINT_TO_PTR(x) ((void*)(__PTRDIFF_TYPE__)(x))
  5488. # define NK_PTR_TO_UINT(x) ((nk_size)(__PTRDIFF_TYPE__)(x))
  5489. #elif !defined(__GNUC__) /* works for compilers other than LLVM */
  5490. # define NK_UINT_TO_PTR(x) ((void*)&((char*)0)[x])
  5491. # define NK_PTR_TO_UINT(x) ((nk_size)(((char*)x)-(char*)0))
  5492. #elif defined(NK_USE_FIXED_TYPES) /* used if we have <stdint.h> */
  5493. # define NK_UINT_TO_PTR(x) ((void*)(uintptr_t)(x))
  5494. # define NK_PTR_TO_UINT(x) ((uintptr_t)(x))
  5495. #else /* generates warning but works */
  5496. # define NK_UINT_TO_PTR(x) ((void*)(x))
  5497. # define NK_PTR_TO_UINT(x) ((nk_size)(x))
  5498. #endif
  5499. #define NK_ALIGN_PTR(x, mask)\
  5500. (NK_UINT_TO_PTR((NK_PTR_TO_UINT((nk_byte*)(x) + (mask-1)) & ~(mask-1))))
  5501. #define NK_ALIGN_PTR_BACK(x, mask)\
  5502. (NK_UINT_TO_PTR((NK_PTR_TO_UINT((nk_byte*)(x)) & ~(mask-1))))
  5503. #if (defined(__GNUC__) && __GNUC__ >= 4) || defined(__clang__)
  5504. #define NK_OFFSETOF(st,m) (__builtin_offsetof(st,m))
  5505. #else
  5506. #define NK_OFFSETOF(st,m) ((nk_ptr)&(((st*)0)->m))
  5507. #endif
  5508. #ifdef __cplusplus
  5509. }
  5510. #endif
  5511. #ifdef __cplusplus
  5512. template<typename T> struct nk_alignof;
  5513. template<typename T, int size_diff> struct nk_helper { enum { value = size_diff }; };
  5514. template<typename T> struct nk_helper<T, 0> { enum { value = nk_alignof<T>::value }; };
  5515. template<typename T> struct nk_alignof {
  5516. struct Big { T x; char c; }; enum {
  5517. diff = sizeof(Big) - sizeof(T), value = nk_helper<Big, diff>::value
  5518. };
  5519. };
  5520. #define NK_ALIGNOF(t) (nk_alignof<t>::value)
  5521. #else
  5522. #define NK_ALIGNOF(t) NK_OFFSETOF(struct {char c; t _h;}, _h)
  5523. #endif
  5524. #define NK_CONTAINER_OF(ptr,type,member)\
  5525. (type*)((void*)((char*)(1 ? (ptr): &((type*)0)->member) - NK_OFFSETOF(type, member)))
  5526. #endif /* NK_NUKLEAR_H_ */
  5527. #ifdef NK_IMPLEMENTATION
  5528. #ifndef NK_INTERNAL_H
  5529. #define NK_INTERNAL_H
  5530. #ifndef NK_POOL_DEFAULT_CAPACITY
  5531. #define NK_POOL_DEFAULT_CAPACITY 16
  5532. #endif
  5533. #ifndef NK_DEFAULT_COMMAND_BUFFER_SIZE
  5534. #define NK_DEFAULT_COMMAND_BUFFER_SIZE (4*1024)
  5535. #endif
  5536. #ifndef NK_BUFFER_DEFAULT_INITIAL_SIZE
  5537. #define NK_BUFFER_DEFAULT_INITIAL_SIZE (4*1024)
  5538. #endif
  5539. /* standard library headers */
  5540. #ifdef NK_INCLUDE_DEFAULT_ALLOCATOR
  5541. #include <stdlib.h> /* malloc, free */
  5542. #endif
  5543. #ifdef NK_INCLUDE_STANDARD_IO
  5544. #include <stdio.h> /* fopen, fclose,... */
  5545. #endif
  5546. #ifdef NK_INCLUDE_STANDARD_VARARGS
  5547. #include <stdarg.h> /* valist, va_start, va_end, ... */
  5548. #endif
  5549. #ifndef NK_ASSERT
  5550. #include <assert.h>
  5551. #define NK_ASSERT(expr) assert(expr)
  5552. #endif
  5553. #define NK_DEFAULT (-1)
  5554. #ifndef NK_VSNPRINTF
  5555. /* If your compiler does support `vsnprintf` I would highly recommend
  5556. * defining this to vsnprintf instead since `vsprintf` is basically
  5557. * unbelievable unsafe and should *NEVER* be used. But I have to support
  5558. * it since C89 only provides this unsafe version. */
  5559. #if (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)) ||\
  5560. (defined(__cplusplus) && (__cplusplus >= 201103L)) || \
  5561. (defined(_POSIX_C_SOURCE) && (_POSIX_C_SOURCE >= 200112L)) ||\
  5562. (defined(_XOPEN_SOURCE) && (_XOPEN_SOURCE >= 500)) ||\
  5563. defined(_ISOC99_SOURCE) || defined(_BSD_SOURCE)
  5564. #define NK_VSNPRINTF(s,n,f,a) vsnprintf(s,n,f,a)
  5565. #else
  5566. #define NK_VSNPRINTF(s,n,f,a) vsprintf(s,f,a)
  5567. #endif
  5568. #endif
  5569. #define NK_SCHAR_MIN (-127)
  5570. #define NK_SCHAR_MAX 127
  5571. #define NK_UCHAR_MIN 0
  5572. #define NK_UCHAR_MAX 256
  5573. #define NK_SSHORT_MIN (-32767)
  5574. #define NK_SSHORT_MAX 32767
  5575. #define NK_USHORT_MIN 0
  5576. #define NK_USHORT_MAX 65535
  5577. #define NK_SINT_MIN (-2147483647)
  5578. #define NK_SINT_MAX 2147483647
  5579. #define NK_UINT_MIN 0
  5580. #define NK_UINT_MAX 4294967295u
  5581. /* Make sure correct type size:
  5582. * This will fire with a negative subscript error if the type sizes
  5583. * are set incorrectly by the compiler, and compile out if not */
  5584. NK_STATIC_ASSERT(sizeof(nk_size) >= sizeof(void*));
  5585. NK_STATIC_ASSERT(sizeof(nk_ptr) == sizeof(void*));
  5586. NK_STATIC_ASSERT(sizeof(nk_flags) >= 4);
  5587. NK_STATIC_ASSERT(sizeof(nk_rune) >= 4);
  5588. NK_STATIC_ASSERT(sizeof(nk_ushort) == 2);
  5589. NK_STATIC_ASSERT(sizeof(nk_short) == 2);
  5590. NK_STATIC_ASSERT(sizeof(nk_uint) == 4);
  5591. NK_STATIC_ASSERT(sizeof(nk_int) == 4);
  5592. NK_STATIC_ASSERT(sizeof(nk_byte) == 1);
  5593. #ifdef NK_INCLUDE_STANDARD_BOOL
  5594. NK_STATIC_ASSERT(sizeof(nk_bool) == sizeof(bool));
  5595. #else
  5596. NK_STATIC_ASSERT(sizeof(nk_bool) == 4);
  5597. #endif
  5598. NK_GLOBAL const struct nk_rect nk_null_rect = { -8192.0f, -8192.0f, 16384, 16384 };
  5599. #define NK_FLOAT_PRECISION 0.00000000000001
  5600. NK_GLOBAL const struct nk_color nk_red = { 255,0,0,255 };
  5601. NK_GLOBAL const struct nk_color nk_green = { 0,255,0,255 };
  5602. NK_GLOBAL const struct nk_color nk_blue = { 0,0,255,255 };
  5603. NK_GLOBAL const struct nk_color nk_white = { 255,255,255,255 };
  5604. NK_GLOBAL const struct nk_color nk_black = { 0,0,0,255 };
  5605. NK_GLOBAL const struct nk_color nk_yellow = { 255,255,0,255 };
  5606. /* widget */
  5607. #define nk_widget_state_reset(s)\
  5608. if ((*(s)) & NK_WIDGET_STATE_MODIFIED)\
  5609. (*(s)) = NK_WIDGET_STATE_INACTIVE|NK_WIDGET_STATE_MODIFIED;\
  5610. else (*(s)) = NK_WIDGET_STATE_INACTIVE;
  5611. /* math */
  5612. #ifndef NK_INV_SQRT
  5613. NK_LIB float nk_inv_sqrt(float n);
  5614. #endif
  5615. #ifndef NK_SIN
  5616. NK_LIB float nk_sin(float x);
  5617. #endif
  5618. #ifndef NK_COS
  5619. NK_LIB float nk_cos(float x);
  5620. #endif
  5621. NK_LIB nk_uint nk_round_up_pow2(nk_uint v);
  5622. NK_LIB struct nk_rect nk_shrink_rect(struct nk_rect r, float amount);
  5623. NK_LIB struct nk_rect nk_pad_rect(struct nk_rect r, struct nk_vec2 pad);
  5624. NK_LIB void nk_unify(struct nk_rect* clip, const struct nk_rect* a, float x0, float y0, float x1, float y1);
  5625. NK_LIB double nk_pow(double x, int n);
  5626. NK_LIB int nk_ifloord(double x);
  5627. NK_LIB int nk_ifloorf(float x);
  5628. NK_LIB int nk_iceilf(float x);
  5629. NK_LIB int nk_log10(double n);
  5630. /* util */
  5631. enum { NK_DO_NOT_STOP_ON_NEW_LINE, NK_STOP_ON_NEW_LINE };
  5632. NK_LIB nk_bool nk_is_lower(int c);
  5633. NK_LIB nk_bool nk_is_upper(int c);
  5634. NK_LIB int nk_to_upper(int c);
  5635. NK_LIB int nk_to_lower(int c);
  5636. #ifndef NK_MEMCPY
  5637. NK_LIB void* nk_memcopy(void* dst, const void* src, nk_size n);
  5638. #endif
  5639. #ifndef NK_MEMSET
  5640. NK_LIB void nk_memset(void* ptr, int c0, nk_size size);
  5641. #endif
  5642. NK_LIB void nk_zero(void* ptr, nk_size size);
  5643. NK_LIB char* nk_itoa(char* s, long n);
  5644. NK_LIB int nk_string_float_limit(char* string, int prec);
  5645. #ifndef NK_DTOA
  5646. NK_LIB char* nk_dtoa(char* s, double n);
  5647. #endif
  5648. NK_LIB int nk_text_clamp(const struct nk_user_font* font, const char* text, int text_len, float space, int* glyphs, float* text_width, nk_rune* sep_list, int sep_count);
  5649. NK_LIB struct nk_vec2 nk_text_calculate_text_bounds(const struct nk_user_font* font, const char* begin, int byte_len, float row_height, const char** remaining, struct nk_vec2* out_offset, int* glyphs, int op);
  5650. #ifdef NK_INCLUDE_STANDARD_VARARGS
  5651. NK_LIB int nk_strfmt(char* buf, int buf_size, const char* fmt, va_list args);
  5652. #endif
  5653. #ifdef NK_INCLUDE_STANDARD_IO
  5654. NK_LIB char* nk_file_load(const char* path, nk_size* siz, struct nk_allocator* alloc);
  5655. #endif
  5656. /* buffer */
  5657. #ifdef NK_INCLUDE_DEFAULT_ALLOCATOR
  5658. NK_LIB void* nk_malloc(nk_handle unused, void* old, nk_size size);
  5659. NK_LIB void nk_mfree(nk_handle unused, void* ptr);
  5660. #endif
  5661. NK_LIB void* nk_buffer_align(void* unaligned, nk_size align, nk_size* alignment, enum nk_buffer_allocation_type type);
  5662. NK_LIB void* nk_buffer_alloc(struct nk_buffer* b, enum nk_buffer_allocation_type type, nk_size size, nk_size align);
  5663. NK_LIB void* nk_buffer_realloc(struct nk_buffer* b, nk_size capacity, nk_size* size);
  5664. /* draw */
  5665. NK_LIB void nk_command_buffer_init(struct nk_command_buffer* cb, struct nk_buffer* b, enum nk_command_clipping clip);
  5666. NK_LIB void nk_command_buffer_reset(struct nk_command_buffer* b);
  5667. NK_LIB void* nk_command_buffer_push(struct nk_command_buffer* b, enum nk_command_type t, nk_size size);
  5668. NK_LIB void nk_draw_symbol(struct nk_command_buffer* out, enum nk_symbol_type type, struct nk_rect content, struct nk_color background, struct nk_color foreground, float border_width, const struct nk_user_font* font);
  5669. /* buffering */
  5670. NK_LIB void nk_start_buffer(struct nk_context* ctx, struct nk_command_buffer* b);
  5671. NK_LIB void nk_start(struct nk_context* ctx, struct nk_window* win);
  5672. NK_LIB void nk_start_popup(struct nk_context* ctx, struct nk_window* win);
  5673. NK_LIB void nk_finish_popup(struct nk_context* ctx, struct nk_window*);
  5674. NK_LIB void nk_finish_buffer(struct nk_context* ctx, struct nk_command_buffer* b);
  5675. NK_LIB void nk_finish(struct nk_context* ctx, struct nk_window* w);
  5676. NK_LIB void nk_build(struct nk_context* ctx);
  5677. /* text editor */
  5678. NK_LIB void nk_textedit_clear_state(struct nk_text_edit* state, enum nk_text_edit_type type, nk_plugin_filter filter);
  5679. NK_LIB void nk_textedit_click(struct nk_text_edit* state, float x, float y, const struct nk_user_font* font, float row_height);
  5680. NK_LIB void nk_textedit_drag(struct nk_text_edit* state, float x, float y, const struct nk_user_font* font, float row_height);
  5681. NK_LIB void nk_textedit_key(struct nk_text_edit* state, enum nk_keys key, int shift_mod, const struct nk_user_font* font, float row_height);
  5682. /* window */
  5683. enum nk_window_insert_location {
  5684. NK_INSERT_BACK, /* inserts window into the back of list (front of screen) */
  5685. NK_INSERT_FRONT /* inserts window into the front of list (back of screen) */
  5686. };
  5687. NK_LIB void* nk_create_window(struct nk_context* ctx);
  5688. NK_LIB void nk_remove_window(struct nk_context*, struct nk_window*);
  5689. NK_LIB void nk_free_window(struct nk_context* ctx, struct nk_window* win);
  5690. NK_LIB struct nk_window* nk_find_window(struct nk_context* ctx, nk_hash hash, const char* name);
  5691. NK_LIB void nk_insert_window(struct nk_context* ctx, struct nk_window* win, enum nk_window_insert_location loc);
  5692. /* pool */
  5693. NK_LIB void nk_pool_init(struct nk_pool* pool, struct nk_allocator* alloc, unsigned int capacity);
  5694. NK_LIB void nk_pool_free(struct nk_pool* pool);
  5695. NK_LIB void nk_pool_init_fixed(struct nk_pool* pool, void* memory, nk_size size);
  5696. NK_LIB struct nk_page_element* nk_pool_alloc(struct nk_pool* pool);
  5697. /* page-element */
  5698. NK_LIB struct nk_page_element* nk_create_page_element(struct nk_context* ctx);
  5699. NK_LIB void nk_link_page_element_into_freelist(struct nk_context* ctx, struct nk_page_element* elem);
  5700. NK_LIB void nk_free_page_element(struct nk_context* ctx, struct nk_page_element* elem);
  5701. /* table */
  5702. NK_LIB struct nk_table* nk_create_table(struct nk_context* ctx);
  5703. NK_LIB void nk_remove_table(struct nk_window* win, struct nk_table* tbl);
  5704. NK_LIB void nk_free_table(struct nk_context* ctx, struct nk_table* tbl);
  5705. NK_LIB void nk_push_table(struct nk_window* win, struct nk_table* tbl);
  5706. NK_LIB nk_uint* nk_add_value(struct nk_context* ctx, struct nk_window* win, nk_hash name, nk_uint value);
  5707. NK_LIB nk_uint* nk_find_value(struct nk_window* win, nk_hash name);
  5708. /* panel */
  5709. NK_LIB void* nk_create_panel(struct nk_context* ctx);
  5710. NK_LIB void nk_free_panel(struct nk_context*, struct nk_panel* pan);
  5711. NK_LIB nk_bool nk_panel_has_header(nk_flags flags, const char* title);
  5712. NK_LIB struct nk_vec2 nk_panel_get_padding(const struct nk_style* style, enum nk_panel_type type);
  5713. NK_LIB float nk_panel_get_border(const struct nk_style* style, nk_flags flags, enum nk_panel_type type);
  5714. NK_LIB struct nk_color nk_panel_get_border_color(const struct nk_style* style, enum nk_panel_type type);
  5715. NK_LIB nk_bool nk_panel_is_sub(enum nk_panel_type type);
  5716. NK_LIB nk_bool nk_panel_is_nonblock(enum nk_panel_type type);
  5717. NK_LIB nk_bool nk_panel_begin(struct nk_context* ctx, const char* title, enum nk_panel_type panel_type);
  5718. NK_LIB void nk_panel_end(struct nk_context* ctx);
  5719. /* layout */
  5720. NK_LIB float nk_layout_row_calculate_usable_space(const struct nk_style* style, enum nk_panel_type type, float total_space, int columns);
  5721. NK_LIB void nk_panel_layout(const struct nk_context* ctx, struct nk_window* win, float height, int cols);
  5722. NK_LIB void nk_row_layout(struct nk_context* ctx, enum nk_layout_format fmt, float height, int cols, int width);
  5723. NK_LIB void nk_panel_alloc_row(const struct nk_context* ctx, struct nk_window* win);
  5724. NK_LIB void nk_layout_widget_space(struct nk_rect* bounds, const struct nk_context* ctx, struct nk_window* win, int modify);
  5725. NK_LIB void nk_panel_alloc_space(struct nk_rect* bounds, const struct nk_context* ctx);
  5726. NK_LIB void nk_layout_peek(struct nk_rect* bounds, struct nk_context* ctx);
  5727. /* popup */
  5728. NK_LIB nk_bool nk_nonblock_begin(struct nk_context* ctx, nk_flags flags, struct nk_rect body, struct nk_rect header, enum nk_panel_type panel_type);
  5729. /* text */
  5730. struct nk_text {
  5731. struct nk_vec2 padding;
  5732. struct nk_color background;
  5733. struct nk_color text;
  5734. };
  5735. NK_LIB void nk_widget_text(struct nk_command_buffer* o, struct nk_rect b, const char* string, int len, const struct nk_text* t, nk_flags a, const struct nk_user_font* f);
  5736. NK_LIB void nk_widget_text_wrap(struct nk_command_buffer* o, struct nk_rect b, const char* string, int len, const struct nk_text* t, const struct nk_user_font* f);
  5737. /* button */
  5738. NK_LIB nk_bool nk_button_behavior(nk_flags* state, struct nk_rect r, const struct nk_input* i, enum nk_button_behavior behavior);
  5739. NK_LIB const struct nk_style_item* nk_draw_button(struct nk_command_buffer* out, const struct nk_rect* bounds, nk_flags state, const struct nk_style_button* style);
  5740. NK_LIB nk_bool nk_do_button(nk_flags* state, struct nk_command_buffer* out, struct nk_rect r, const struct nk_style_button* style, const struct nk_input* in, enum nk_button_behavior behavior, struct nk_rect* content);
  5741. NK_LIB void nk_draw_button_text(struct nk_command_buffer* out, const struct nk_rect* bounds, const struct nk_rect* content, nk_flags state, const struct nk_style_button* style, const char* txt, int len, nk_flags text_alignment, const struct nk_user_font* font);
  5742. NK_LIB nk_bool nk_do_button_text(nk_flags* state, struct nk_command_buffer* out, struct nk_rect bounds, const char* string, int len, nk_flags align, enum nk_button_behavior behavior, const struct nk_style_button* style, const struct nk_input* in, const struct nk_user_font* font);
  5743. NK_LIB void nk_draw_button_symbol(struct nk_command_buffer* out, const struct nk_rect* bounds, const struct nk_rect* content, nk_flags state, const struct nk_style_button* style, enum nk_symbol_type type, const struct nk_user_font* font);
  5744. NK_LIB nk_bool nk_do_button_symbol(nk_flags* state, struct nk_command_buffer* out, struct nk_rect bounds, enum nk_symbol_type symbol, enum nk_button_behavior behavior, const struct nk_style_button* style, const struct nk_input* in, const struct nk_user_font* font);
  5745. NK_LIB void nk_draw_button_image(struct nk_command_buffer* out, const struct nk_rect* bounds, const struct nk_rect* content, nk_flags state, const struct nk_style_button* style, const struct nk_image* img);
  5746. NK_LIB nk_bool nk_do_button_image(nk_flags* state, struct nk_command_buffer* out, struct nk_rect bounds, struct nk_image img, enum nk_button_behavior b, const struct nk_style_button* style, const struct nk_input* in);
  5747. NK_LIB void nk_draw_button_text_symbol(struct nk_command_buffer* out, const struct nk_rect* bounds, const struct nk_rect* label, const struct nk_rect* symbol, nk_flags state, const struct nk_style_button* style, const char* str, int len, enum nk_symbol_type type, const struct nk_user_font* font);
  5748. NK_LIB nk_bool nk_do_button_text_symbol(nk_flags* state, struct nk_command_buffer* out, struct nk_rect bounds, enum nk_symbol_type symbol, const char* str, int len, nk_flags align, enum nk_button_behavior behavior, const struct nk_style_button* style, const struct nk_user_font* font, const struct nk_input* in);
  5749. NK_LIB void nk_draw_button_text_image(struct nk_command_buffer* out, const struct nk_rect* bounds, const struct nk_rect* label, const struct nk_rect* image, nk_flags state, const struct nk_style_button* style, const char* str, int len, const struct nk_user_font* font, const struct nk_image* img);
  5750. NK_LIB nk_bool nk_do_button_text_image(nk_flags* state, struct nk_command_buffer* out, struct nk_rect bounds, struct nk_image img, const char* str, int len, nk_flags align, enum nk_button_behavior behavior, const struct nk_style_button* style, const struct nk_user_font* font, const struct nk_input* in);
  5751. /* toggle */
  5752. enum nk_toggle_type {
  5753. NK_TOGGLE_CHECK,
  5754. NK_TOGGLE_OPTION
  5755. };
  5756. NK_LIB nk_bool nk_toggle_behavior(const struct nk_input* in, struct nk_rect select, nk_flags* state, nk_bool active);
  5757. NK_LIB void nk_draw_checkbox(struct nk_command_buffer* out, nk_flags state, const struct nk_style_toggle* style, nk_bool active, const struct nk_rect* label, const struct nk_rect* selector, const struct nk_rect* cursors, const char* string, int len, const struct nk_user_font* font);
  5758. NK_LIB void nk_draw_option(struct nk_command_buffer* out, nk_flags state, const struct nk_style_toggle* style, nk_bool active, const struct nk_rect* label, const struct nk_rect* selector, const struct nk_rect* cursors, const char* string, int len, const struct nk_user_font* font);
  5759. NK_LIB nk_bool nk_do_toggle(nk_flags* state, struct nk_command_buffer* out, struct nk_rect r, nk_bool* active, const char* str, int len, enum nk_toggle_type type, const struct nk_style_toggle* style, const struct nk_input* in, const struct nk_user_font* font);
  5760. /* progress */
  5761. NK_LIB nk_size nk_progress_behavior(nk_flags* state, struct nk_input* in, struct nk_rect r, struct nk_rect cursor, nk_size max, nk_size value, nk_bool modifiable);
  5762. NK_LIB void nk_draw_progress(struct nk_command_buffer* out, nk_flags state, const struct nk_style_progress* style, const struct nk_rect* bounds, const struct nk_rect* scursor, nk_size value, nk_size max);
  5763. NK_LIB nk_size nk_do_progress(nk_flags* state, struct nk_command_buffer* out, struct nk_rect bounds, nk_size value, nk_size max, nk_bool modifiable, const struct nk_style_progress* style, struct nk_input* in);
  5764. /* slider */
  5765. NK_LIB float nk_slider_behavior(nk_flags* state, struct nk_rect* logical_cursor, struct nk_rect* visual_cursor, struct nk_input* in, struct nk_rect bounds, float slider_min, float slider_max, float slider_value, float slider_step, float slider_steps);
  5766. NK_LIB void nk_draw_slider(struct nk_command_buffer* out, nk_flags state, const struct nk_style_slider* style, const struct nk_rect* bounds, const struct nk_rect* visual_cursor, float min, float value, float max);
  5767. NK_LIB float nk_do_slider(nk_flags* state, struct nk_command_buffer* out, struct nk_rect bounds, float min, float val, float max, float step, const struct nk_style_slider* style, struct nk_input* in, const struct nk_user_font* font);
  5768. /* scrollbar */
  5769. NK_LIB float nk_scrollbar_behavior(nk_flags* state, struct nk_input* in, int has_scrolling, const struct nk_rect* scroll, const struct nk_rect* cursor, const struct nk_rect* empty0, const struct nk_rect* empty1, float scroll_offset, float target, float scroll_step, enum nk_orientation o);
  5770. NK_LIB void nk_draw_scrollbar(struct nk_command_buffer* out, nk_flags state, const struct nk_style_scrollbar* style, const struct nk_rect* bounds, const struct nk_rect* scroll);
  5771. NK_LIB float nk_do_scrollbarv(nk_flags* state, struct nk_command_buffer* out, struct nk_rect scroll, int has_scrolling, float offset, float target, float step, float button_pixel_inc, const struct nk_style_scrollbar* style, struct nk_input* in, const struct nk_user_font* font);
  5772. NK_LIB float nk_do_scrollbarh(nk_flags* state, struct nk_command_buffer* out, struct nk_rect scroll, int has_scrolling, float offset, float target, float step, float button_pixel_inc, const struct nk_style_scrollbar* style, struct nk_input* in, const struct nk_user_font* font);
  5773. /* selectable */
  5774. NK_LIB void nk_draw_selectable(struct nk_command_buffer* out, nk_flags state, const struct nk_style_selectable* style, nk_bool active, const struct nk_rect* bounds, const struct nk_rect* icon, const struct nk_image* img, enum nk_symbol_type sym, const char* string, int len, nk_flags align, const struct nk_user_font* font);
  5775. NK_LIB nk_bool nk_do_selectable(nk_flags* state, struct nk_command_buffer* out, struct nk_rect bounds, const char* str, int len, nk_flags align, nk_bool* value, const struct nk_style_selectable* style, const struct nk_input* in, const struct nk_user_font* font);
  5776. NK_LIB nk_bool nk_do_selectable_image(nk_flags* state, struct nk_command_buffer* out, struct nk_rect bounds, const char* str, int len, nk_flags align, nk_bool* value, const struct nk_image* img, const struct nk_style_selectable* style, const struct nk_input* in, const struct nk_user_font* font);
  5777. /* edit */
  5778. NK_LIB void nk_edit_draw_text(struct nk_command_buffer* out, const struct nk_style_edit* style, float pos_x, float pos_y, float x_offset, const char* text, int byte_len, float row_height, const struct nk_user_font* font, struct nk_color background, struct nk_color foreground, nk_bool is_selected);
  5779. NK_LIB nk_flags nk_do_edit(nk_flags* state, struct nk_command_buffer* out, struct nk_rect bounds, nk_flags flags, nk_plugin_filter filter, struct nk_text_edit* edit, const struct nk_style_edit* style, struct nk_input* in, const struct nk_user_font* font);
  5780. /* color-picker */
  5781. NK_LIB nk_bool nk_color_picker_behavior(nk_flags* state, const struct nk_rect* bounds, const struct nk_rect* matrix, const struct nk_rect* hue_bar, const struct nk_rect* alpha_bar, struct nk_colorf* color, const struct nk_input* in);
  5782. NK_LIB void nk_draw_color_picker(struct nk_command_buffer* o, const struct nk_rect* matrix, const struct nk_rect* hue_bar, const struct nk_rect* alpha_bar, struct nk_colorf col);
  5783. NK_LIB nk_bool nk_do_color_picker(nk_flags* state, struct nk_command_buffer* out, struct nk_colorf* col, enum nk_color_format fmt, struct nk_rect bounds, struct nk_vec2 padding, const struct nk_input* in, const struct nk_user_font* font);
  5784. /* property */
  5785. enum nk_property_status {
  5786. NK_PROPERTY_DEFAULT,
  5787. NK_PROPERTY_EDIT,
  5788. NK_PROPERTY_DRAG
  5789. };
  5790. enum nk_property_filter {
  5791. NK_FILTER_INT,
  5792. NK_FILTER_FLOAT
  5793. };
  5794. enum nk_property_kind {
  5795. NK_PROPERTY_INT,
  5796. NK_PROPERTY_FLOAT,
  5797. NK_PROPERTY_DOUBLE
  5798. };
  5799. union nk_property {
  5800. int i;
  5801. float f;
  5802. double d;
  5803. };
  5804. struct nk_property_variant {
  5805. enum nk_property_kind kind;
  5806. union nk_property value;
  5807. union nk_property min_value;
  5808. union nk_property max_value;
  5809. union nk_property step;
  5810. };
  5811. NK_LIB struct nk_property_variant nk_property_variant_int(int value, int min_value, int max_value, int step);
  5812. NK_LIB struct nk_property_variant nk_property_variant_float(float value, float min_value, float max_value, float step);
  5813. NK_LIB struct nk_property_variant nk_property_variant_double(double value, double min_value, double max_value, double step);
  5814. NK_LIB void nk_drag_behavior(nk_flags* state, const struct nk_input* in, struct nk_rect drag, struct nk_property_variant* variant, float inc_per_pixel);
  5815. NK_LIB void nk_property_behavior(nk_flags* ws, const struct nk_input* in, struct nk_rect property, struct nk_rect label, struct nk_rect edit, struct nk_rect empty, int* state, struct nk_property_variant* variant, float inc_per_pixel);
  5816. NK_LIB void nk_draw_property(struct nk_command_buffer* out, const struct nk_style_property* style, const struct nk_rect* bounds, const struct nk_rect* label, nk_flags state, const char* name, int len, const struct nk_user_font* font);
  5817. NK_LIB void nk_do_property(nk_flags* ws, struct nk_command_buffer* out, struct nk_rect property, const char* name, struct nk_property_variant* variant, float inc_per_pixel, char* buffer, int* len, int* state, int* cursor, int* select_begin, int* select_end, const struct nk_style_property* style, enum nk_property_filter filter, struct nk_input* in, const struct nk_user_font* font, struct nk_text_edit* text_edit, enum nk_button_behavior behavior);
  5818. NK_LIB void nk_property(struct nk_context* ctx, const char* name, struct nk_property_variant* variant, float inc_per_pixel, const enum nk_property_filter filter);
  5819. #ifdef NK_INCLUDE_FONT_BAKING
  5820. /**
  5821. * @def NK_NO_STB_RECT_PACK_IMPLEMENTATION
  5822. *
  5823. * When defined, will avoid enabling STB_RECT_PACK_IMPLEMENTATION for when stb_rect_pack.h is already implemented elsewhere.
  5824. */
  5825. #ifndef NK_NO_STB_RECT_PACK_IMPLEMENTATION
  5826. #define STB_RECT_PACK_IMPLEMENTATION
  5827. #endif /* NK_NO_STB_RECT_PACK_IMPLEMENTATION */
  5828. /**
  5829. * @def NK_NO_STB_TRUETYPE_IMPLEMENTATION
  5830. *
  5831. * When defined, will avoid enabling STB_TRUETYPE_IMPLEMENTATION for when stb_truetype.h is already implemented elsewhere.
  5832. */
  5833. #ifndef NK_NO_STB_TRUETYPE_IMPLEMENTATION
  5834. #define STB_TRUETYPE_IMPLEMENTATION
  5835. #endif /* NK_NO_STB_TRUETYPE_IMPLEMENTATION */
  5836. /* Allow consumer to define own STBTT_malloc/STBTT_free, and use the font atlas' allocator otherwise */
  5837. #ifndef STBTT_malloc
  5838. static void*
  5839. nk_stbtt_malloc(nk_size size, void* user_data) {
  5840. struct nk_allocator* alloc = (struct nk_allocator*)user_data;
  5841. return alloc->alloc(alloc->userdata, 0, size);
  5842. }
  5843. static void
  5844. nk_stbtt_free(void* ptr, void* user_data) {
  5845. struct nk_allocator* alloc = (struct nk_allocator*)user_data;
  5846. alloc->free(alloc->userdata, ptr);
  5847. }
  5848. #define STBTT_malloc(x,u) nk_stbtt_malloc(x,u)
  5849. #define STBTT_free(x,u) nk_stbtt_free(x,u)
  5850. #endif /* STBTT_malloc */
  5851. #endif /* NK_INCLUDE_FONT_BAKING */
  5852. #endif
  5853. /* ===============================================================
  5854. *
  5855. * MATH
  5856. *
  5857. * ===============================================================*/
  5858. /*/// ### Math
  5859. /// Since nuklear is supposed to work on all systems providing floating point
  5860. /// math without any dependencies I also had to implement my own math functions
  5861. /// for sqrt, sin and cos. Since the actual highly accurate implementations for
  5862. /// the standard library functions are quite complex and I do not need high
  5863. /// precision for my use cases I use approximations.
  5864. ///
  5865. /// Sqrt
  5866. /// ----
  5867. /// For square root nuklear uses the famous fast inverse square root:
  5868. /// https://en.wikipedia.org/wiki/Fast_inverse_square_root with
  5869. /// slightly tweaked magic constant. While on today's hardware it is
  5870. /// probably not faster it is still fast and accurate enough for
  5871. /// nuklear's use cases. IMPORTANT: this requires float format IEEE 754
  5872. ///
  5873. /// Sine/Cosine
  5874. /// -----------
  5875. /// All constants inside both function are generated Remez's minimax
  5876. /// approximations for value range 0...2*PI. The reason why I decided to
  5877. /// approximate exactly that range is that nuklear only needs sine and
  5878. /// cosine to generate circles which only requires that exact range.
  5879. /// In addition I used Remez instead of Taylor for additional precision:
  5880. /// www.lolengine.net/blog/2011/12/21/better-function-approximations.
  5881. ///
  5882. /// The tool I used to generate constants for both sine and cosine
  5883. /// (it can actually approximate a lot more functions) can be
  5884. /// found here: www.lolengine.net/wiki/oss/lolremez
  5885. */
  5886. #ifndef NK_INV_SQRT
  5887. #define NK_INV_SQRT nk_inv_sqrt
  5888. NK_LIB float
  5889. nk_inv_sqrt(float n)
  5890. {
  5891. float x2;
  5892. const float threehalfs = 1.5f;
  5893. union { nk_uint i; float f; } conv = { 0 };
  5894. conv.f = n;
  5895. x2 = n * 0.5f;
  5896. conv.i = 0x5f375A84 - (conv.i >> 1);
  5897. conv.f = conv.f * (threehalfs - (x2 * conv.f * conv.f));
  5898. return conv.f;
  5899. }
  5900. #endif
  5901. #ifndef NK_SIN
  5902. #define NK_SIN nk_sin
  5903. NK_LIB float
  5904. nk_sin(float x)
  5905. {
  5906. NK_STORAGE const float a0 = +1.91059300966915117e-31f;
  5907. NK_STORAGE const float a1 = +1.00086760103908896f;
  5908. NK_STORAGE const float a2 = -1.21276126894734565e-2f;
  5909. NK_STORAGE const float a3 = -1.38078780785773762e-1f;
  5910. NK_STORAGE const float a4 = -2.67353392911981221e-2f;
  5911. NK_STORAGE const float a5 = +2.08026600266304389e-2f;
  5912. NK_STORAGE const float a6 = -3.03996055049204407e-3f;
  5913. NK_STORAGE const float a7 = +1.38235642404333740e-4f;
  5914. return a0 + x * (a1 + x * (a2 + x * (a3 + x * (a4 + x * (a5 + x * (a6 + x * a7))))));
  5915. }
  5916. #endif
  5917. #ifndef NK_COS
  5918. #define NK_COS nk_cos
  5919. NK_LIB float
  5920. nk_cos(float x)
  5921. {
  5922. /* New implementation. Also generated using lolremez. */
  5923. /* Old version significantly deviated from expected results. */
  5924. NK_STORAGE const float a0 = 9.9995999154986614e-1f;
  5925. NK_STORAGE const float a1 = 1.2548995793001028e-3f;
  5926. NK_STORAGE const float a2 = -5.0648546280678015e-1f;
  5927. NK_STORAGE const float a3 = 1.2942246466519995e-2f;
  5928. NK_STORAGE const float a4 = 2.8668384702547972e-2f;
  5929. NK_STORAGE const float a5 = 7.3726485210586547e-3f;
  5930. NK_STORAGE const float a6 = -3.8510875386947414e-3f;
  5931. NK_STORAGE const float a7 = 4.7196604604366623e-4f;
  5932. NK_STORAGE const float a8 = -1.8776444013090451e-5f;
  5933. return a0 + x * (a1 + x * (a2 + x * (a3 + x * (a4 + x * (a5 + x * (a6 + x * (a7 + x * a8)))))));
  5934. }
  5935. #endif
  5936. NK_LIB nk_uint
  5937. nk_round_up_pow2(nk_uint v)
  5938. {
  5939. v--;
  5940. v |= v >> 1;
  5941. v |= v >> 2;
  5942. v |= v >> 4;
  5943. v |= v >> 8;
  5944. v |= v >> 16;
  5945. v++;
  5946. return v;
  5947. }
  5948. NK_LIB double
  5949. nk_pow(double x, int n)
  5950. {
  5951. /* check the sign of n */
  5952. double r = 1;
  5953. int plus = n >= 0;
  5954. n = (plus) ? n : -n;
  5955. while (n > 0) {
  5956. if ((n & 1) == 1)
  5957. r *= x;
  5958. n /= 2;
  5959. x *= x;
  5960. }
  5961. return plus ? r : 1.0 / r;
  5962. }
  5963. NK_LIB int
  5964. nk_ifloord(double x)
  5965. {
  5966. x = (double)((int)x - ((x < 0.0) ? 1 : 0));
  5967. return (int)x;
  5968. }
  5969. NK_LIB int
  5970. nk_ifloorf(float x)
  5971. {
  5972. x = (float)((int)x - ((x < 0.0f) ? 1 : 0));
  5973. return (int)x;
  5974. }
  5975. NK_LIB int
  5976. nk_iceilf(float x)
  5977. {
  5978. if (x >= 0) {
  5979. int i = (int)x;
  5980. return (x > i) ? i + 1 : i;
  5981. }
  5982. else {
  5983. int t = (int)x;
  5984. float r = x - (float)t;
  5985. return (r > 0.0f) ? t + 1 : t;
  5986. }
  5987. }
  5988. NK_LIB int
  5989. nk_log10(double n)
  5990. {
  5991. int neg;
  5992. int ret;
  5993. int exp = 0;
  5994. neg = (n < 0) ? 1 : 0;
  5995. ret = (neg) ? (int)-n : (int)n;
  5996. while ((ret / 10) > 0) {
  5997. ret /= 10;
  5998. exp++;
  5999. }
  6000. if (neg) exp = -exp;
  6001. return exp;
  6002. }
  6003. NK_API struct nk_rect
  6004. nk_get_null_rect(void)
  6005. {
  6006. return nk_null_rect;
  6007. }
  6008. NK_API struct nk_rect
  6009. nk_rect(float x, float y, float w, float h)
  6010. {
  6011. struct nk_rect r;
  6012. r.x = x; r.y = y;
  6013. r.w = w; r.h = h;
  6014. return r;
  6015. }
  6016. NK_API struct nk_rect
  6017. nk_recti(int x, int y, int w, int h)
  6018. {
  6019. struct nk_rect r;
  6020. r.x = (float)x;
  6021. r.y = (float)y;
  6022. r.w = (float)w;
  6023. r.h = (float)h;
  6024. return r;
  6025. }
  6026. NK_API struct nk_rect
  6027. nk_recta(struct nk_vec2 pos, struct nk_vec2 size)
  6028. {
  6029. return nk_rect(pos.x, pos.y, size.x, size.y);
  6030. }
  6031. NK_API struct nk_rect
  6032. nk_rectv(const float* r)
  6033. {
  6034. return nk_rect(r[0], r[1], r[2], r[3]);
  6035. }
  6036. NK_API struct nk_rect
  6037. nk_rectiv(const int* r)
  6038. {
  6039. return nk_recti(r[0], r[1], r[2], r[3]);
  6040. }
  6041. NK_API struct nk_vec2
  6042. nk_rect_pos(struct nk_rect r)
  6043. {
  6044. struct nk_vec2 ret;
  6045. ret.x = r.x; ret.y = r.y;
  6046. return ret;
  6047. }
  6048. NK_API struct nk_vec2
  6049. nk_rect_size(struct nk_rect r)
  6050. {
  6051. struct nk_vec2 ret;
  6052. ret.x = r.w; ret.y = r.h;
  6053. return ret;
  6054. }
  6055. NK_LIB struct nk_rect
  6056. nk_shrink_rect(struct nk_rect r, float amount)
  6057. {
  6058. struct nk_rect res;
  6059. r.w = NK_MAX(r.w, 2 * amount);
  6060. r.h = NK_MAX(r.h, 2 * amount);
  6061. res.x = r.x + amount;
  6062. res.y = r.y + amount;
  6063. res.w = r.w - 2 * amount;
  6064. res.h = r.h - 2 * amount;
  6065. return res;
  6066. }
  6067. NK_LIB struct nk_rect
  6068. nk_pad_rect(struct nk_rect r, struct nk_vec2 pad)
  6069. {
  6070. r.w = NK_MAX(r.w, 2 * pad.x);
  6071. r.h = NK_MAX(r.h, 2 * pad.y);
  6072. r.x += pad.x; r.y += pad.y;
  6073. r.w -= 2 * pad.x;
  6074. r.h -= 2 * pad.y;
  6075. return r;
  6076. }
  6077. NK_API struct nk_vec2
  6078. nk_vec2(float x, float y)
  6079. {
  6080. struct nk_vec2 ret;
  6081. ret.x = x; ret.y = y;
  6082. return ret;
  6083. }
  6084. NK_API struct nk_vec2
  6085. nk_vec2i(int x, int y)
  6086. {
  6087. struct nk_vec2 ret;
  6088. ret.x = (float)x;
  6089. ret.y = (float)y;
  6090. return ret;
  6091. }
  6092. NK_API struct nk_vec2
  6093. nk_vec2v(const float* v)
  6094. {
  6095. return nk_vec2(v[0], v[1]);
  6096. }
  6097. NK_API struct nk_vec2
  6098. nk_vec2iv(const int* v)
  6099. {
  6100. return nk_vec2i(v[0], v[1]);
  6101. }
  6102. NK_LIB void
  6103. nk_unify(struct nk_rect* clip, const struct nk_rect* a, float x0, float y0,
  6104. float x1, float y1)
  6105. {
  6106. NK_ASSERT(a);
  6107. NK_ASSERT(clip);
  6108. clip->x = NK_MAX(a->x, x0);
  6109. clip->y = NK_MAX(a->y, y0);
  6110. clip->w = NK_MIN(a->x + a->w, x1) - clip->x;
  6111. clip->h = NK_MIN(a->y + a->h, y1) - clip->y;
  6112. clip->w = NK_MAX(0, clip->w);
  6113. clip->h = NK_MAX(0, clip->h);
  6114. }
  6115. NK_API void
  6116. nk_triangle_from_direction(struct nk_vec2* result, struct nk_rect r,
  6117. float pad_x, float pad_y, enum nk_heading direction)
  6118. {
  6119. float w_half, h_half;
  6120. NK_ASSERT(result);
  6121. r.w = NK_MAX(2 * pad_x, r.w);
  6122. r.h = NK_MAX(2 * pad_y, r.h);
  6123. r.w = r.w - 2 * pad_x;
  6124. r.h = r.h - 2 * pad_y;
  6125. r.x = r.x + pad_x;
  6126. r.y = r.y + pad_y;
  6127. w_half = r.w / 2.0f;
  6128. h_half = r.h / 2.0f;
  6129. if (direction == NK_UP) {
  6130. result[0] = nk_vec2(r.x + w_half, r.y);
  6131. result[1] = nk_vec2(r.x + r.w, r.y + r.h);
  6132. result[2] = nk_vec2(r.x, r.y + r.h);
  6133. }
  6134. else if (direction == NK_RIGHT) {
  6135. result[0] = nk_vec2(r.x, r.y);
  6136. result[1] = nk_vec2(r.x + r.w, r.y + h_half);
  6137. result[2] = nk_vec2(r.x, r.y + r.h);
  6138. }
  6139. else if (direction == NK_DOWN) {
  6140. result[0] = nk_vec2(r.x, r.y);
  6141. result[1] = nk_vec2(r.x + r.w, r.y);
  6142. result[2] = nk_vec2(r.x + w_half, r.y + r.h);
  6143. }
  6144. else {
  6145. result[0] = nk_vec2(r.x, r.y + h_half);
  6146. result[1] = nk_vec2(r.x + r.w, r.y);
  6147. result[2] = nk_vec2(r.x + r.w, r.y + r.h);
  6148. }
  6149. }
  6150. /* ===============================================================
  6151. *
  6152. * UTIL
  6153. *
  6154. * ===============================================================*/
  6155. NK_INTERN int nk_str_match_here(const char* regexp, const char* text);
  6156. NK_INTERN int nk_str_match_star(int c, const char* regexp, const char* text);
  6157. NK_LIB nk_bool nk_is_lower(int c) { return (c >= 'a' && c <= 'z') || (c >= 0xE0 && c <= 0xFF); }
  6158. NK_LIB nk_bool nk_is_upper(int c) { return (c >= 'A' && c <= 'Z') || (c >= 0xC0 && c <= 0xDF); }
  6159. NK_LIB int nk_to_upper(int c) { return (c >= 'a' && c <= 'z') ? (c - ('a' - 'A')) : c; }
  6160. NK_LIB int nk_to_lower(int c) { return (c >= 'A' && c <= 'Z') ? (c - ('a' + 'A')) : c; }
  6161. #ifndef NK_MEMCPY
  6162. #define NK_MEMCPY nk_memcopy
  6163. NK_LIB void*
  6164. nk_memcopy(void* dst0, const void* src0, nk_size length)
  6165. {
  6166. nk_ptr t;
  6167. char* dst = (char*)dst0;
  6168. const char* src = (const char*)src0;
  6169. if (length == 0 || dst == src)
  6170. goto done;
  6171. #define nk_word int
  6172. #define nk_wsize sizeof(nk_word)
  6173. #define nk_wmask (nk_wsize-1)
  6174. #define NK_TLOOP(s) if (t) NK_TLOOP1(s)
  6175. #define NK_TLOOP1(s) do { s; } while (--t)
  6176. if (dst < src) {
  6177. t = (nk_ptr)src; /* only need low bits */
  6178. if ((t | (nk_ptr)dst) & nk_wmask) {
  6179. if ((t ^ (nk_ptr)dst) & nk_wmask || length < nk_wsize)
  6180. t = length;
  6181. else
  6182. t = nk_wsize - (t & nk_wmask);
  6183. length -= t;
  6184. NK_TLOOP1(*dst++ = *src++);
  6185. }
  6186. t = length / nk_wsize;
  6187. NK_TLOOP(*(nk_word*)(void*)dst = *(const nk_word*)(const void*)src;
  6188. src += nk_wsize; dst += nk_wsize);
  6189. t = length & nk_wmask;
  6190. NK_TLOOP(*dst++ = *src++);
  6191. }
  6192. else {
  6193. src += length;
  6194. dst += length;
  6195. t = (nk_ptr)src;
  6196. if ((t | (nk_ptr)dst) & nk_wmask) {
  6197. if ((t ^ (nk_ptr)dst) & nk_wmask || length <= nk_wsize)
  6198. t = length;
  6199. else
  6200. t &= nk_wmask;
  6201. length -= t;
  6202. NK_TLOOP1(*--dst = *--src);
  6203. }
  6204. t = length / nk_wsize;
  6205. NK_TLOOP(src -= nk_wsize; dst -= nk_wsize;
  6206. *(nk_word*)(void*)dst = *(const nk_word*)(const void*)src);
  6207. t = length & nk_wmask;
  6208. NK_TLOOP(*--dst = *--src);
  6209. }
  6210. #undef nk_word
  6211. #undef nk_wsize
  6212. #undef nk_wmask
  6213. #undef NK_TLOOP
  6214. #undef NK_TLOOP1
  6215. done :
  6216. return (dst0);
  6217. }
  6218. #endif
  6219. #ifndef NK_MEMSET
  6220. #define NK_MEMSET nk_memset
  6221. NK_LIB void
  6222. nk_memset(void* ptr, int c0, nk_size size)
  6223. {
  6224. #define nk_word unsigned
  6225. #define nk_wsize sizeof(nk_word)
  6226. #define nk_wmask (nk_wsize - 1)
  6227. nk_byte* dst = (nk_byte*)ptr;
  6228. unsigned c = 0;
  6229. nk_size t = 0;
  6230. if ((c = (nk_byte)c0) != 0) {
  6231. c = (c << 8) | c; /* at least 16-bits */
  6232. if (sizeof(unsigned int) > 2)
  6233. c = (c << 16) | c; /* at least 32-bits*/
  6234. }
  6235. /* too small of a word count */
  6236. dst = (nk_byte*)ptr;
  6237. if (size < 3 * nk_wsize) {
  6238. while (size--) *dst++ = (nk_byte)c0;
  6239. return;
  6240. }
  6241. /* align destination */
  6242. if ((t = NK_PTR_TO_UINT(dst) & nk_wmask) != 0) {
  6243. t = nk_wsize - t;
  6244. size -= t;
  6245. do {
  6246. *dst++ = (nk_byte)c0;
  6247. } while (--t != 0);
  6248. }
  6249. /* fill word */
  6250. t = size / nk_wsize;
  6251. do {
  6252. *(nk_word*)((void*)dst) = c;
  6253. dst += nk_wsize;
  6254. } while (--t != 0);
  6255. /* fill trailing bytes */
  6256. t = (size & nk_wmask);
  6257. if (t != 0) {
  6258. do {
  6259. *dst++ = (nk_byte)c0;
  6260. } while (--t != 0);
  6261. }
  6262. #undef nk_word
  6263. #undef nk_wsize
  6264. #undef nk_wmask
  6265. }
  6266. #endif
  6267. NK_LIB void
  6268. nk_zero(void* ptr, nk_size size)
  6269. {
  6270. NK_ASSERT(ptr);
  6271. NK_MEMSET(ptr, 0, size);
  6272. }
  6273. NK_API int
  6274. nk_strlen(const char* str)
  6275. {
  6276. int siz = 0;
  6277. NK_ASSERT(str);
  6278. while (str && *str++ != '\0') siz++;
  6279. return siz;
  6280. }
  6281. NK_API int
  6282. nk_strtoi(const char* str, const char** endptr)
  6283. {
  6284. int neg = 1;
  6285. const char* p = str;
  6286. int value = 0;
  6287. NK_ASSERT(str);
  6288. if (!str) return 0;
  6289. /* skip whitespace */
  6290. while (*p == ' ') p++;
  6291. if (*p == '-') {
  6292. neg = -1;
  6293. p++;
  6294. }
  6295. while (*p && *p >= '0' && *p <= '9') {
  6296. value = value * 10 + (int)(*p - '0');
  6297. p++;
  6298. }
  6299. if (endptr)
  6300. *endptr = p;
  6301. return neg * value;
  6302. }
  6303. NK_API double
  6304. nk_strtod(const char* str, const char** endptr)
  6305. {
  6306. double m;
  6307. double neg = 1.0;
  6308. const char* p = str;
  6309. double value = 0;
  6310. double number = 0;
  6311. NK_ASSERT(str);
  6312. if (!str) return 0;
  6313. /* skip whitespace */
  6314. while (*p == ' ') p++;
  6315. if (*p == '-') {
  6316. neg = -1.0;
  6317. p++;
  6318. }
  6319. while (*p && *p != '.' && *p != 'e') {
  6320. value = value * 10.0 + (double)(*p - '0');
  6321. p++;
  6322. }
  6323. if (*p == '.') {
  6324. p++;
  6325. for (m = 0.1; *p && *p != 'e'; p++) {
  6326. value = value + (double)(*p - '0') * m;
  6327. m *= 0.1;
  6328. }
  6329. }
  6330. if (*p == 'e') {
  6331. int i, pow, div;
  6332. p++;
  6333. if (*p == '-') {
  6334. div = nk_true;
  6335. p++;
  6336. }
  6337. else if (*p == '+') {
  6338. div = nk_false;
  6339. p++;
  6340. }
  6341. else div = nk_false;
  6342. for (pow = 0; *p; p++)
  6343. pow = pow * 10 + (int)(*p - '0');
  6344. for (m = 1.0, i = 0; i < pow; i++)
  6345. m *= 10.0;
  6346. if (div)
  6347. value /= m;
  6348. else value *= m;
  6349. }
  6350. number = value * neg;
  6351. if (endptr)
  6352. *endptr = p;
  6353. return number;
  6354. }
  6355. NK_API float
  6356. nk_strtof(const char* str, const char** endptr)
  6357. {
  6358. float float_value;
  6359. double double_value;
  6360. double_value = NK_STRTOD(str, endptr);
  6361. float_value = (float)double_value;
  6362. return float_value;
  6363. }
  6364. NK_API int
  6365. nk_stricmp(const char* s1, const char* s2)
  6366. {
  6367. nk_int c1, c2, d;
  6368. do {
  6369. c1 = *s1++;
  6370. c2 = *s2++;
  6371. d = c1 - c2;
  6372. while (d) {
  6373. if (c1 <= 'Z' && c1 >= 'A') {
  6374. d += ('a' - 'A');
  6375. if (!d) break;
  6376. }
  6377. if (c2 <= 'Z' && c2 >= 'A') {
  6378. d -= ('a' - 'A');
  6379. if (!d) break;
  6380. }
  6381. return ((d >= 0) << 1) - 1;
  6382. }
  6383. } while (c1);
  6384. return 0;
  6385. }
  6386. NK_API int
  6387. nk_stricmpn(const char* s1, const char* s2, int n)
  6388. {
  6389. int c1, c2, d;
  6390. NK_ASSERT(n >= 0);
  6391. do {
  6392. c1 = *s1++;
  6393. c2 = *s2++;
  6394. if (!n--) return 0;
  6395. d = c1 - c2;
  6396. while (d) {
  6397. if (c1 <= 'Z' && c1 >= 'A') {
  6398. d += ('a' - 'A');
  6399. if (!d) break;
  6400. }
  6401. if (c2 <= 'Z' && c2 >= 'A') {
  6402. d -= ('a' - 'A');
  6403. if (!d) break;
  6404. }
  6405. return ((d >= 0) << 1) - 1;
  6406. }
  6407. } while (c1);
  6408. return 0;
  6409. }
  6410. NK_INTERN int
  6411. nk_str_match_here(const char* regexp, const char* text)
  6412. {
  6413. if (regexp[0] == '\0')
  6414. return 1;
  6415. if (regexp[1] == '*')
  6416. return nk_str_match_star(regexp[0], regexp + 2, text);
  6417. if (regexp[0] == '$' && regexp[1] == '\0')
  6418. return *text == '\0';
  6419. if (*text != '\0' && (regexp[0] == '.' || regexp[0] == *text))
  6420. return nk_str_match_here(regexp + 1, text + 1);
  6421. return 0;
  6422. }
  6423. NK_INTERN int
  6424. nk_str_match_star(int c, const char* regexp, const char* text)
  6425. {
  6426. do {/* a '* matches zero or more instances */
  6427. if (nk_str_match_here(regexp, text))
  6428. return 1;
  6429. } while (*text != '\0' && (*text++ == c || c == '.'));
  6430. return 0;
  6431. }
  6432. NK_API int
  6433. nk_strfilter(const char* text, const char* regexp)
  6434. {
  6435. /*
  6436. c matches any literal character c
  6437. . matches any single character
  6438. ^ matches the beginning of the input string
  6439. $ matches the end of the input string
  6440. * matches zero or more occurrences of the previous character*/
  6441. if (regexp[0] == '^')
  6442. return nk_str_match_here(regexp + 1, text);
  6443. do { /* must look even if string is empty */
  6444. if (nk_str_match_here(regexp, text))
  6445. return 1;
  6446. } while (*text++ != '\0');
  6447. return 0;
  6448. }
  6449. NK_API int
  6450. nk_strmatch_fuzzy_text(const char* str, int str_len,
  6451. const char* pattern, int* out_score)
  6452. {
  6453. /* Returns true if each character in pattern is found sequentially within str
  6454. * if found then out_score is also set. Score value has no intrinsic meaning.
  6455. * Range varies with pattern. Can only compare scores with same search pattern. */
  6456. /* bonus for adjacent matches */
  6457. #define NK_ADJACENCY_BONUS 5
  6458. /* bonus if match occurs after a separator */
  6459. #define NK_SEPARATOR_BONUS 10
  6460. /* bonus if match is uppercase and prev is lower */
  6461. #define NK_CAMEL_BONUS 10
  6462. /* penalty applied for every letter in str before the first match */
  6463. #define NK_LEADING_LETTER_PENALTY (-3)
  6464. /* maximum penalty for leading letters */
  6465. #define NK_MAX_LEADING_LETTER_PENALTY (-9)
  6466. /* penalty for every letter that doesn't matter */
  6467. #define NK_UNMATCHED_LETTER_PENALTY (-1)
  6468. /* loop variables */
  6469. int score = 0;
  6470. char const* pattern_iter = pattern;
  6471. int str_iter = 0;
  6472. int prev_matched = nk_false;
  6473. int prev_lower = nk_false;
  6474. /* true so if first letter match gets separator bonus*/
  6475. int prev_separator = nk_true;
  6476. /* use "best" matched letter if multiple string letters match the pattern */
  6477. char const* best_letter = 0;
  6478. int best_letter_score = 0;
  6479. /* loop over strings */
  6480. NK_ASSERT(str);
  6481. NK_ASSERT(pattern);
  6482. if (!str || !str_len || !pattern) return 0;
  6483. while (str_iter < str_len)
  6484. {
  6485. const char pattern_letter = *pattern_iter;
  6486. const char str_letter = str[str_iter];
  6487. int next_match = *pattern_iter != '\0' &&
  6488. nk_to_lower(pattern_letter) == nk_to_lower(str_letter);
  6489. int rematch = best_letter && nk_to_upper(*best_letter) == nk_to_upper(str_letter);
  6490. int advanced = next_match && best_letter;
  6491. int pattern_repeat = best_letter && *pattern_iter != '\0';
  6492. pattern_repeat = pattern_repeat &&
  6493. nk_to_lower(*best_letter) == nk_to_lower(pattern_letter);
  6494. if (advanced || pattern_repeat) {
  6495. score += best_letter_score;
  6496. best_letter = 0;
  6497. best_letter_score = 0;
  6498. }
  6499. if (next_match || rematch)
  6500. {
  6501. int new_score = 0;
  6502. /* Apply penalty for each letter before the first pattern match */
  6503. if (pattern_iter == pattern) {
  6504. int count = (int)(&str[str_iter] - str);
  6505. int penalty = NK_LEADING_LETTER_PENALTY * count;
  6506. if (penalty < NK_MAX_LEADING_LETTER_PENALTY)
  6507. penalty = NK_MAX_LEADING_LETTER_PENALTY;
  6508. score += penalty;
  6509. }
  6510. /* apply bonus for consecutive bonuses */
  6511. if (prev_matched)
  6512. new_score += NK_ADJACENCY_BONUS;
  6513. /* apply bonus for matches after a separator */
  6514. if (prev_separator)
  6515. new_score += NK_SEPARATOR_BONUS;
  6516. /* apply bonus across camel case boundaries */
  6517. if (prev_lower && nk_is_upper(str_letter))
  6518. new_score += NK_CAMEL_BONUS;
  6519. /* update pattern iter IFF the next pattern letter was matched */
  6520. if (next_match)
  6521. ++pattern_iter;
  6522. /* update best letter in str which may be for a "next" letter or a rematch */
  6523. if (new_score >= best_letter_score) {
  6524. /* apply penalty for now skipped letter */
  6525. if (best_letter != 0)
  6526. score += NK_UNMATCHED_LETTER_PENALTY;
  6527. best_letter = &str[str_iter];
  6528. best_letter_score = new_score;
  6529. }
  6530. prev_matched = nk_true;
  6531. }
  6532. else {
  6533. score += NK_UNMATCHED_LETTER_PENALTY;
  6534. prev_matched = nk_false;
  6535. }
  6536. /* separators should be more easily defined */
  6537. prev_lower = nk_is_lower(str_letter) != 0;
  6538. prev_separator = str_letter == '_' || str_letter == ' ';
  6539. ++str_iter;
  6540. }
  6541. /* apply score for last match */
  6542. if (best_letter)
  6543. score += best_letter_score;
  6544. /* did not match full pattern */
  6545. if (*pattern_iter != '\0')
  6546. return nk_false;
  6547. if (out_score)
  6548. *out_score = score;
  6549. return nk_true;
  6550. }
  6551. NK_API int
  6552. nk_strmatch_fuzzy_string(char const* str, char const* pattern, int* out_score)
  6553. {
  6554. return nk_strmatch_fuzzy_text(str, nk_strlen(str), pattern, out_score);
  6555. }
  6556. NK_LIB int
  6557. nk_string_float_limit(char* string, int prec)
  6558. {
  6559. int dot = 0;
  6560. char* c = string;
  6561. while (*c) {
  6562. if (*c == '.') {
  6563. dot = 1;
  6564. c++;
  6565. continue;
  6566. }
  6567. if (dot == (prec + 1)) {
  6568. *c = 0;
  6569. break;
  6570. }
  6571. if (dot > 0) dot++;
  6572. c++;
  6573. }
  6574. return (int)(c - string);
  6575. }
  6576. NK_INTERN void
  6577. nk_strrev_ascii(char* s)
  6578. {
  6579. int len = nk_strlen(s);
  6580. int end = len / 2;
  6581. int i = 0;
  6582. char t;
  6583. for (; i < end; ++i) {
  6584. t = s[i];
  6585. s[i] = s[len - 1 - i];
  6586. s[len - 1 - i] = t;
  6587. }
  6588. }
  6589. NK_LIB char*
  6590. nk_itoa(char* s, long n)
  6591. {
  6592. long i = 0;
  6593. if (n == 0) {
  6594. s[i++] = '0';
  6595. s[i] = 0;
  6596. return s;
  6597. }
  6598. if (n < 0) {
  6599. s[i++] = '-';
  6600. n = -n;
  6601. }
  6602. while (n > 0) {
  6603. s[i++] = (char)('0' + (n % 10));
  6604. n /= 10;
  6605. }
  6606. s[i] = 0;
  6607. if (s[0] == '-')
  6608. ++s;
  6609. nk_strrev_ascii(s);
  6610. return s;
  6611. }
  6612. #ifndef NK_DTOA
  6613. #define NK_DTOA nk_dtoa
  6614. NK_LIB char*
  6615. nk_dtoa(char* s, double n)
  6616. {
  6617. int useExp = 0;
  6618. int digit = 0, m = 0, m1 = 0;
  6619. char* c = s;
  6620. int neg = 0;
  6621. NK_ASSERT(s);
  6622. if (!s) return 0;
  6623. if (n == 0.0) {
  6624. s[0] = '0'; s[1] = '\0';
  6625. return s;
  6626. }
  6627. neg = (n < 0);
  6628. if (neg) n = -n;
  6629. /* calculate magnitude */
  6630. m = nk_log10(n);
  6631. useExp = (m >= 14 || (neg && m >= 9) || m <= -9);
  6632. if (neg) *(c++) = '-';
  6633. /* set up for scientific notation */
  6634. if (useExp) {
  6635. if (m < 0)
  6636. m -= 1;
  6637. n = n / (double)nk_pow(10.0, m);
  6638. m1 = m;
  6639. m = 0;
  6640. }
  6641. if (m < 1.0) {
  6642. m = 0;
  6643. }
  6644. /* convert the number */
  6645. while (n > NK_FLOAT_PRECISION || m >= 0) {
  6646. double weight = nk_pow(10.0, m);
  6647. if (weight > 0) {
  6648. double t = (double)n / weight;
  6649. digit = nk_ifloord(t);
  6650. n -= ((double)digit * weight);
  6651. *(c++) = (char)('0' + (char)digit);
  6652. }
  6653. if (m == 0 && n > 0)
  6654. *(c++) = '.';
  6655. m--;
  6656. }
  6657. if (useExp) {
  6658. /* convert the exponent */
  6659. int i, j;
  6660. *(c++) = 'e';
  6661. if (m1 > 0) {
  6662. *(c++) = '+';
  6663. }
  6664. else {
  6665. *(c++) = '-';
  6666. m1 = -m1;
  6667. }
  6668. m = 0;
  6669. while (m1 > 0) {
  6670. *(c++) = (char)('0' + (char)(m1 % 10));
  6671. m1 /= 10;
  6672. m++;
  6673. }
  6674. c -= m;
  6675. for (i = 0, j = m - 1; i < j; i++, j--) {
  6676. /* swap without temporary */
  6677. c[i] ^= c[j];
  6678. c[j] ^= c[i];
  6679. c[i] ^= c[j];
  6680. }
  6681. c += m;
  6682. }
  6683. *(c) = '\0';
  6684. return s;
  6685. }
  6686. #endif
  6687. #ifdef NK_INCLUDE_STANDARD_VARARGS
  6688. #ifndef NK_INCLUDE_STANDARD_IO
  6689. NK_INTERN int
  6690. nk_vsnprintf(char* buf, int buf_size, const char* fmt, va_list args)
  6691. {
  6692. enum nk_arg_type {
  6693. NK_ARG_TYPE_CHAR,
  6694. NK_ARG_TYPE_SHORT,
  6695. NK_ARG_TYPE_DEFAULT,
  6696. NK_ARG_TYPE_LONG
  6697. };
  6698. enum nk_arg_flags {
  6699. NK_ARG_FLAG_LEFT = 0x01,
  6700. NK_ARG_FLAG_PLUS = 0x02,
  6701. NK_ARG_FLAG_SPACE = 0x04,
  6702. NK_ARG_FLAG_NUM = 0x10,
  6703. NK_ARG_FLAG_ZERO = 0x20
  6704. };
  6705. char number_buffer[NK_MAX_NUMBER_BUFFER];
  6706. enum nk_arg_type arg_type = NK_ARG_TYPE_DEFAULT;
  6707. int precision = NK_DEFAULT;
  6708. int width = NK_DEFAULT;
  6709. nk_flags flag = 0;
  6710. int len = 0;
  6711. int result = -1;
  6712. const char* iter = fmt;
  6713. NK_ASSERT(buf);
  6714. NK_ASSERT(buf_size);
  6715. if (!buf || !buf_size || !fmt) return 0;
  6716. for (iter = fmt; *iter && len < buf_size; iter++) {
  6717. /* copy all non-format characters */
  6718. while (*iter && (*iter != '%') && (len < buf_size))
  6719. buf[len++] = *iter++;
  6720. if (!(*iter) || len >= buf_size) break;
  6721. iter++;
  6722. /* flag arguments */
  6723. while (*iter) {
  6724. if (*iter == '-') flag |= NK_ARG_FLAG_LEFT;
  6725. else if (*iter == '+') flag |= NK_ARG_FLAG_PLUS;
  6726. else if (*iter == ' ') flag |= NK_ARG_FLAG_SPACE;
  6727. else if (*iter == '#') flag |= NK_ARG_FLAG_NUM;
  6728. else if (*iter == '0') flag |= NK_ARG_FLAG_ZERO;
  6729. else break;
  6730. iter++;
  6731. }
  6732. /* width argument */
  6733. width = NK_DEFAULT;
  6734. if (*iter >= '1' && *iter <= '9') {
  6735. const char* end;
  6736. width = nk_strtoi(iter, &end);
  6737. if (end == iter)
  6738. width = -1;
  6739. else iter = end;
  6740. }
  6741. else if (*iter == '*') {
  6742. width = va_arg(args, int);
  6743. iter++;
  6744. }
  6745. /* precision argument */
  6746. precision = NK_DEFAULT;
  6747. if (*iter == '.') {
  6748. iter++;
  6749. if (*iter == '*') {
  6750. precision = va_arg(args, int);
  6751. iter++;
  6752. }
  6753. else {
  6754. const char* end;
  6755. precision = nk_strtoi(iter, &end);
  6756. if (end == iter)
  6757. precision = -1;
  6758. else iter = end;
  6759. }
  6760. }
  6761. /* length modifier */
  6762. if (*iter == 'h') {
  6763. if (*(iter + 1) == 'h') {
  6764. arg_type = NK_ARG_TYPE_CHAR;
  6765. iter++;
  6766. }
  6767. else arg_type = NK_ARG_TYPE_SHORT;
  6768. iter++;
  6769. }
  6770. else if (*iter == 'l') {
  6771. arg_type = NK_ARG_TYPE_LONG;
  6772. iter++;
  6773. }
  6774. else arg_type = NK_ARG_TYPE_DEFAULT;
  6775. /* specifier */
  6776. if (*iter == '%') {
  6777. NK_ASSERT(arg_type == NK_ARG_TYPE_DEFAULT);
  6778. NK_ASSERT(precision == NK_DEFAULT);
  6779. NK_ASSERT(width == NK_DEFAULT);
  6780. if (len < buf_size)
  6781. buf[len++] = '%';
  6782. }
  6783. else if (*iter == 's') {
  6784. /* string */
  6785. const char* str = va_arg(args, const char*);
  6786. NK_ASSERT(str != buf && "buffer and argument are not allowed to overlap!");
  6787. NK_ASSERT(arg_type == NK_ARG_TYPE_DEFAULT);
  6788. NK_ASSERT(precision == NK_DEFAULT);
  6789. NK_ASSERT(width == NK_DEFAULT);
  6790. if (str == buf) return -1;
  6791. while (str && *str && len < buf_size)
  6792. buf[len++] = *str++;
  6793. }
  6794. else if (*iter == 'n') {
  6795. /* current length callback */
  6796. signed int* n = va_arg(args, int*);
  6797. NK_ASSERT(arg_type == NK_ARG_TYPE_DEFAULT);
  6798. NK_ASSERT(precision == NK_DEFAULT);
  6799. NK_ASSERT(width == NK_DEFAULT);
  6800. if (n) *n = len;
  6801. }
  6802. else if (*iter == 'c' || *iter == 'i' || *iter == 'd') {
  6803. /* signed integer */
  6804. long value = 0;
  6805. const char* num_iter;
  6806. int num_len, num_print, padding;
  6807. int cur_precision = NK_MAX(precision, 1);
  6808. int cur_width = NK_MAX(width, 0);
  6809. /* retrieve correct value type */
  6810. if (arg_type == NK_ARG_TYPE_CHAR)
  6811. value = (signed char)va_arg(args, int);
  6812. else if (arg_type == NK_ARG_TYPE_SHORT)
  6813. value = (signed short)va_arg(args, int);
  6814. else if (arg_type == NK_ARG_TYPE_LONG)
  6815. value = va_arg(args, signed long);
  6816. else if (*iter == 'c')
  6817. value = (unsigned char)va_arg(args, int);
  6818. else value = va_arg(args, signed int);
  6819. /* convert number to string */
  6820. nk_itoa(number_buffer, value);
  6821. num_len = nk_strlen(number_buffer);
  6822. padding = NK_MAX(cur_width - NK_MAX(cur_precision, num_len), 0);
  6823. if ((flag & NK_ARG_FLAG_PLUS) || (flag & NK_ARG_FLAG_SPACE))
  6824. padding = NK_MAX(padding - 1, 0);
  6825. /* fill left padding up to a total of `width` characters */
  6826. if (!(flag & NK_ARG_FLAG_LEFT)) {
  6827. while (padding-- > 0 && (len < buf_size)) {
  6828. if ((flag & NK_ARG_FLAG_ZERO) && (precision == NK_DEFAULT))
  6829. buf[len++] = '0';
  6830. else buf[len++] = ' ';
  6831. }
  6832. }
  6833. /* copy string value representation into buffer */
  6834. if ((flag & NK_ARG_FLAG_PLUS) && value >= 0 && len < buf_size)
  6835. buf[len++] = '+';
  6836. else if ((flag & NK_ARG_FLAG_SPACE) && value >= 0 && len < buf_size)
  6837. buf[len++] = ' ';
  6838. /* fill up to precision number of digits with '0' */
  6839. num_print = NK_MAX(cur_precision, num_len);
  6840. while (precision && (num_print > num_len) && (len < buf_size)) {
  6841. buf[len++] = '0';
  6842. num_print--;
  6843. }
  6844. /* copy string value representation into buffer */
  6845. num_iter = number_buffer;
  6846. while (precision && *num_iter && len < buf_size)
  6847. buf[len++] = *num_iter++;
  6848. /* fill right padding up to width characters */
  6849. if (flag & NK_ARG_FLAG_LEFT) {
  6850. while ((padding-- > 0) && (len < buf_size))
  6851. buf[len++] = ' ';
  6852. }
  6853. }
  6854. else if (*iter == 'o' || *iter == 'x' || *iter == 'X' || *iter == 'u') {
  6855. /* unsigned integer */
  6856. unsigned long value = 0;
  6857. int num_len = 0, num_print, padding = 0;
  6858. int cur_precision = NK_MAX(precision, 1);
  6859. int cur_width = NK_MAX(width, 0);
  6860. unsigned int base = (*iter == 'o') ? 8 : (*iter == 'u') ? 10 : 16;
  6861. /* print oct/hex/dec value */
  6862. const char* upper_output_format = "0123456789ABCDEF";
  6863. const char* lower_output_format = "0123456789abcdef";
  6864. const char* output_format = (*iter == 'x') ?
  6865. lower_output_format : upper_output_format;
  6866. /* retrieve correct value type */
  6867. if (arg_type == NK_ARG_TYPE_CHAR)
  6868. value = (unsigned char)va_arg(args, int);
  6869. else if (arg_type == NK_ARG_TYPE_SHORT)
  6870. value = (unsigned short)va_arg(args, int);
  6871. else if (arg_type == NK_ARG_TYPE_LONG)
  6872. value = va_arg(args, unsigned long);
  6873. else value = va_arg(args, unsigned int);
  6874. do {
  6875. /* convert decimal number into hex/oct number */
  6876. int digit = output_format[value % base];
  6877. if (num_len < NK_MAX_NUMBER_BUFFER)
  6878. number_buffer[num_len++] = (char)digit;
  6879. value /= base;
  6880. } while (value > 0);
  6881. num_print = NK_MAX(cur_precision, num_len);
  6882. padding = NK_MAX(cur_width - NK_MAX(cur_precision, num_len), 0);
  6883. if (flag & NK_ARG_FLAG_NUM)
  6884. padding = NK_MAX(padding - 1, 0);
  6885. /* fill left padding up to a total of `width` characters */
  6886. if (!(flag & NK_ARG_FLAG_LEFT)) {
  6887. while ((padding-- > 0) && (len < buf_size)) {
  6888. if ((flag & NK_ARG_FLAG_ZERO) && (precision == NK_DEFAULT))
  6889. buf[len++] = '0';
  6890. else buf[len++] = ' ';
  6891. }
  6892. }
  6893. /* fill up to precision number of digits */
  6894. if (num_print && (flag & NK_ARG_FLAG_NUM)) {
  6895. if ((*iter == 'o') && (len < buf_size)) {
  6896. buf[len++] = '0';
  6897. }
  6898. else if ((*iter == 'x') && ((len + 1) < buf_size)) {
  6899. buf[len++] = '0';
  6900. buf[len++] = 'x';
  6901. }
  6902. else if ((*iter == 'X') && ((len + 1) < buf_size)) {
  6903. buf[len++] = '0';
  6904. buf[len++] = 'X';
  6905. }
  6906. }
  6907. while (precision && (num_print > num_len) && (len < buf_size)) {
  6908. buf[len++] = '0';
  6909. num_print--;
  6910. }
  6911. /* reverse number direction */
  6912. while (num_len > 0) {
  6913. if (precision && (len < buf_size))
  6914. buf[len++] = number_buffer[num_len - 1];
  6915. num_len--;
  6916. }
  6917. /* fill right padding up to width characters */
  6918. if (flag & NK_ARG_FLAG_LEFT) {
  6919. while ((padding-- > 0) && (len < buf_size))
  6920. buf[len++] = ' ';
  6921. }
  6922. }
  6923. else if (*iter == 'f') {
  6924. /* floating point */
  6925. const char* num_iter;
  6926. int cur_precision = (precision < 0) ? 6 : precision;
  6927. int prefix, cur_width = NK_MAX(width, 0);
  6928. double value = va_arg(args, double);
  6929. int num_len = 0, frac_len = 0, dot = 0;
  6930. int padding = 0;
  6931. NK_ASSERT(arg_type == NK_ARG_TYPE_DEFAULT);
  6932. NK_DTOA(number_buffer, value);
  6933. num_len = nk_strlen(number_buffer);
  6934. /* calculate padding */
  6935. num_iter = number_buffer;
  6936. while (*num_iter && *num_iter != '.')
  6937. num_iter++;
  6938. prefix = (*num_iter == '.') ? (int)(num_iter - number_buffer) + 1 : 0;
  6939. padding = NK_MAX(cur_width - (prefix + NK_MIN(cur_precision, num_len - prefix)), 0);
  6940. if ((flag & NK_ARG_FLAG_PLUS) || (flag & NK_ARG_FLAG_SPACE))
  6941. padding = NK_MAX(padding - 1, 0);
  6942. /* fill left padding up to a total of `width` characters */
  6943. if (!(flag & NK_ARG_FLAG_LEFT)) {
  6944. while (padding-- > 0 && (len < buf_size)) {
  6945. if (flag & NK_ARG_FLAG_ZERO)
  6946. buf[len++] = '0';
  6947. else buf[len++] = ' ';
  6948. }
  6949. }
  6950. /* copy string value representation into buffer */
  6951. num_iter = number_buffer;
  6952. if ((flag & NK_ARG_FLAG_PLUS) && (value >= 0) && (len < buf_size))
  6953. buf[len++] = '+';
  6954. else if ((flag & NK_ARG_FLAG_SPACE) && (value >= 0) && (len < buf_size))
  6955. buf[len++] = ' ';
  6956. while (*num_iter) {
  6957. if (dot) frac_len++;
  6958. if (len < buf_size)
  6959. buf[len++] = *num_iter;
  6960. if (*num_iter == '.') dot = 1;
  6961. if (frac_len >= cur_precision) break;
  6962. num_iter++;
  6963. }
  6964. /* fill number up to precision */
  6965. while (frac_len < cur_precision) {
  6966. if (!dot && len < buf_size) {
  6967. buf[len++] = '.';
  6968. dot = 1;
  6969. }
  6970. if (len < buf_size)
  6971. buf[len++] = '0';
  6972. frac_len++;
  6973. }
  6974. /* fill right padding up to width characters */
  6975. if (flag & NK_ARG_FLAG_LEFT) {
  6976. while ((padding-- > 0) && (len < buf_size))
  6977. buf[len++] = ' ';
  6978. }
  6979. }
  6980. else {
  6981. /* Specifier not supported: g,G,e,E,p,z */
  6982. NK_ASSERT(0 && "specifier is not supported!");
  6983. return result;
  6984. }
  6985. }
  6986. buf[(len >= buf_size) ? (buf_size - 1) : len] = 0;
  6987. result = (len >= buf_size) ? -1 : len;
  6988. return result;
  6989. }
  6990. #endif
  6991. NK_LIB int
  6992. nk_strfmt(char* buf, int buf_size, const char* fmt, va_list args)
  6993. {
  6994. int result = -1;
  6995. NK_ASSERT(buf);
  6996. NK_ASSERT(buf_size);
  6997. if (!buf || !buf_size || !fmt) return 0;
  6998. #ifdef NK_INCLUDE_STANDARD_IO
  6999. result = NK_VSNPRINTF(buf, (nk_size)buf_size, fmt, args);
  7000. result = (result >= buf_size) ? -1 : result;
  7001. buf[buf_size - 1] = 0;
  7002. #else
  7003. result = nk_vsnprintf(buf, buf_size, fmt, args);
  7004. #endif
  7005. return result;
  7006. }
  7007. #endif
  7008. NK_API nk_hash
  7009. nk_murmur_hash(const void* key, int len, nk_hash seed)
  7010. {
  7011. /* 32-Bit MurmurHash3: https://code.google.com/p/smhasher/wiki/MurmurHash3*/
  7012. #define NK_ROTL(x,r) ((x) << (r) | ((x) >> (32 - r)))
  7013. nk_uint h1 = seed;
  7014. nk_uint k1;
  7015. const nk_byte* data = (const nk_byte*)key;
  7016. const nk_byte* keyptr = data;
  7017. nk_byte* k1ptr;
  7018. const int bsize = sizeof(k1);
  7019. const int nblocks = len / 4;
  7020. const nk_uint c1 = 0xcc9e2d51;
  7021. const nk_uint c2 = 0x1b873593;
  7022. const nk_byte* tail;
  7023. int i;
  7024. /* body */
  7025. if (!key) return 0;
  7026. for (i = 0; i < nblocks; ++i, keyptr += bsize) {
  7027. k1ptr = (nk_byte*)&k1;
  7028. k1ptr[0] = keyptr[0];
  7029. k1ptr[1] = keyptr[1];
  7030. k1ptr[2] = keyptr[2];
  7031. k1ptr[3] = keyptr[3];
  7032. k1 *= c1;
  7033. k1 = NK_ROTL(k1, 15);
  7034. k1 *= c2;
  7035. h1 ^= k1;
  7036. h1 = NK_ROTL(h1, 13);
  7037. h1 = h1 * 5 + 0xe6546b64;
  7038. }
  7039. /* tail */
  7040. tail = (const nk_byte*)(data + nblocks * 4);
  7041. k1 = 0;
  7042. switch (len & 3) {
  7043. case 3: k1 ^= (nk_uint)(tail[2] << 16); /* fallthrough */
  7044. case 2: k1 ^= (nk_uint)(tail[1] << 8u); /* fallthrough */
  7045. case 1: k1 ^= tail[0];
  7046. k1 *= c1;
  7047. k1 = NK_ROTL(k1, 15);
  7048. k1 *= c2;
  7049. h1 ^= k1;
  7050. break;
  7051. default: break;
  7052. }
  7053. /* finalization */
  7054. h1 ^= (nk_uint)len;
  7055. /* fmix32 */
  7056. h1 ^= h1 >> 16;
  7057. h1 *= 0x85ebca6b;
  7058. h1 ^= h1 >> 13;
  7059. h1 *= 0xc2b2ae35;
  7060. h1 ^= h1 >> 16;
  7061. #undef NK_ROTL
  7062. return h1;
  7063. }
  7064. #ifdef NK_INCLUDE_STANDARD_IO
  7065. NK_LIB char*
  7066. nk_file_load(const char* path, nk_size* siz, struct nk_allocator* alloc)
  7067. {
  7068. char* buf;
  7069. FILE* fd;
  7070. long ret;
  7071. NK_ASSERT(path);
  7072. NK_ASSERT(siz);
  7073. NK_ASSERT(alloc);
  7074. if (!path || !siz || !alloc)
  7075. return 0;
  7076. fd = fopen(path, "rb");
  7077. if (!fd) return 0;
  7078. fseek(fd, 0, SEEK_END);
  7079. ret = ftell(fd);
  7080. if (ret < 0) {
  7081. fclose(fd);
  7082. return 0;
  7083. }
  7084. *siz = (nk_size)ret;
  7085. fseek(fd, 0, SEEK_SET);
  7086. buf = (char*)alloc->alloc(alloc->userdata, 0, *siz);
  7087. NK_ASSERT(buf);
  7088. if (!buf) {
  7089. fclose(fd);
  7090. return 0;
  7091. }
  7092. *siz = (nk_size)fread(buf, 1, *siz, fd);
  7093. fclose(fd);
  7094. return buf;
  7095. }
  7096. #endif
  7097. NK_LIB int
  7098. nk_text_clamp(const struct nk_user_font* font, const char* text,
  7099. int text_len, float space, int* glyphs, float* text_width,
  7100. nk_rune* sep_list, int sep_count)
  7101. {
  7102. int i = 0;
  7103. int glyph_len = 0;
  7104. float last_width = 0;
  7105. nk_rune unicode = 0;
  7106. float width = 0;
  7107. int len = 0;
  7108. int g = 0;
  7109. float s;
  7110. int sep_len = 0;
  7111. int sep_g = 0;
  7112. float sep_width = 0;
  7113. sep_count = NK_MAX(sep_count, 0);
  7114. glyph_len = nk_utf_decode(text, &unicode, text_len);
  7115. while (glyph_len && (width < space) && (len < text_len)) {
  7116. len += glyph_len;
  7117. s = font->width(font->userdata, font->height, text, len);
  7118. for (i = 0; i < sep_count; ++i) {
  7119. if (unicode != sep_list[i]) continue;
  7120. sep_width = last_width = width;
  7121. sep_g = g + 1;
  7122. sep_len = len;
  7123. break;
  7124. }
  7125. if (i == sep_count) {
  7126. last_width = sep_width = width;
  7127. sep_g = g + 1;
  7128. }
  7129. width = s;
  7130. glyph_len = nk_utf_decode(&text[len], &unicode, text_len - len);
  7131. g++;
  7132. }
  7133. if (len >= text_len) {
  7134. *glyphs = g;
  7135. *text_width = last_width;
  7136. return len;
  7137. }
  7138. else {
  7139. *glyphs = sep_g;
  7140. *text_width = sep_width;
  7141. return (!sep_len) ? len : sep_len;
  7142. }
  7143. }
  7144. NK_LIB struct nk_vec2
  7145. nk_text_calculate_text_bounds(const struct nk_user_font* font,
  7146. const char* begin, int byte_len, float row_height, const char** remaining,
  7147. struct nk_vec2* out_offset, int* glyphs, int op)
  7148. {
  7149. float line_height = row_height;
  7150. struct nk_vec2 text_size = nk_vec2(0, 0);
  7151. float line_width = 0.0f;
  7152. float glyph_width;
  7153. int glyph_len = 0;
  7154. nk_rune unicode = 0;
  7155. int text_len = 0;
  7156. if (!begin || byte_len <= 0 || !font)
  7157. return nk_vec2(0, row_height);
  7158. glyph_len = nk_utf_decode(begin, &unicode, byte_len);
  7159. if (!glyph_len) return text_size;
  7160. glyph_width = font->width(font->userdata, font->height, begin, glyph_len);
  7161. *glyphs = 0;
  7162. while ((text_len < byte_len) && glyph_len) {
  7163. if (unicode == '\n') {
  7164. text_size.x = NK_MAX(text_size.x, line_width);
  7165. text_size.y += line_height;
  7166. line_width = 0;
  7167. *glyphs += 1;
  7168. if (op == NK_STOP_ON_NEW_LINE)
  7169. break;
  7170. text_len++;
  7171. glyph_len = nk_utf_decode(begin + text_len, &unicode, byte_len - text_len);
  7172. continue;
  7173. }
  7174. if (unicode == '\r') {
  7175. text_len++;
  7176. *glyphs += 1;
  7177. glyph_len = nk_utf_decode(begin + text_len, &unicode, byte_len - text_len);
  7178. continue;
  7179. }
  7180. *glyphs = *glyphs + 1;
  7181. text_len += glyph_len;
  7182. line_width += (float)glyph_width;
  7183. glyph_len = nk_utf_decode(begin + text_len, &unicode, byte_len - text_len);
  7184. glyph_width = font->width(font->userdata, font->height, begin + text_len, glyph_len);
  7185. continue;
  7186. }
  7187. if (text_size.x < line_width)
  7188. text_size.x = line_width;
  7189. if (out_offset)
  7190. *out_offset = nk_vec2(line_width, text_size.y + line_height);
  7191. if (line_width > 0 || text_size.y == 0.0f)
  7192. text_size.y += line_height;
  7193. if (remaining)
  7194. *remaining = begin + text_len;
  7195. return text_size;
  7196. }
  7197. /* ==============================================================
  7198. *
  7199. * COLOR
  7200. *
  7201. * ===============================================================*/
  7202. NK_INTERN int
  7203. nk_parse_hex(const char* p, int length)
  7204. {
  7205. int i = 0;
  7206. int len = 0;
  7207. while (len < length) {
  7208. i <<= 4;
  7209. if (p[len] >= 'a' && p[len] <= 'f')
  7210. i += ((p[len] - 'a') + 10);
  7211. else if (p[len] >= 'A' && p[len] <= 'F')
  7212. i += ((p[len] - 'A') + 10);
  7213. else i += (p[len] - '0');
  7214. len++;
  7215. }
  7216. return i;
  7217. }
  7218. NK_API struct nk_color
  7219. nk_rgba(int r, int g, int b, int a)
  7220. {
  7221. struct nk_color ret;
  7222. ret.r = (nk_byte)NK_CLAMP(0, r, 255);
  7223. ret.g = (nk_byte)NK_CLAMP(0, g, 255);
  7224. ret.b = (nk_byte)NK_CLAMP(0, b, 255);
  7225. ret.a = (nk_byte)NK_CLAMP(0, a, 255);
  7226. return ret;
  7227. }
  7228. NK_API struct nk_color
  7229. nk_rgb_hex(const char* rgb)
  7230. {
  7231. struct nk_color col;
  7232. const char* c = rgb;
  7233. if (*c == '#') c++;
  7234. col.r = (nk_byte)nk_parse_hex(c, 2);
  7235. col.g = (nk_byte)nk_parse_hex(c + 2, 2);
  7236. col.b = (nk_byte)nk_parse_hex(c + 4, 2);
  7237. col.a = 255;
  7238. return col;
  7239. }
  7240. NK_API struct nk_color
  7241. nk_rgba_hex(const char* rgb)
  7242. {
  7243. struct nk_color col;
  7244. const char* c = rgb;
  7245. if (*c == '#') c++;
  7246. col.r = (nk_byte)nk_parse_hex(c, 2);
  7247. col.g = (nk_byte)nk_parse_hex(c + 2, 2);
  7248. col.b = (nk_byte)nk_parse_hex(c + 4, 2);
  7249. col.a = (nk_byte)nk_parse_hex(c + 6, 2);
  7250. return col;
  7251. }
  7252. NK_API void
  7253. nk_color_hex_rgba(char* output, struct nk_color col)
  7254. {
  7255. #define NK_TO_HEX(i) ((i) <= 9 ? '0' + (i): 'A' - 10 + (i))
  7256. output[0] = (char)NK_TO_HEX((col.r & 0xF0) >> 4);
  7257. output[1] = (char)NK_TO_HEX((col.r & 0x0F));
  7258. output[2] = (char)NK_TO_HEX((col.g & 0xF0) >> 4);
  7259. output[3] = (char)NK_TO_HEX((col.g & 0x0F));
  7260. output[4] = (char)NK_TO_HEX((col.b & 0xF0) >> 4);
  7261. output[5] = (char)NK_TO_HEX((col.b & 0x0F));
  7262. output[6] = (char)NK_TO_HEX((col.a & 0xF0) >> 4);
  7263. output[7] = (char)NK_TO_HEX((col.a & 0x0F));
  7264. output[8] = '\0';
  7265. #undef NK_TO_HEX
  7266. }
  7267. NK_API void
  7268. nk_color_hex_rgb(char* output, struct nk_color col)
  7269. {
  7270. #define NK_TO_HEX(i) ((i) <= 9 ? '0' + (i): 'A' - 10 + (i))
  7271. output[0] = (char)NK_TO_HEX((col.r & 0xF0) >> 4);
  7272. output[1] = (char)NK_TO_HEX((col.r & 0x0F));
  7273. output[2] = (char)NK_TO_HEX((col.g & 0xF0) >> 4);
  7274. output[3] = (char)NK_TO_HEX((col.g & 0x0F));
  7275. output[4] = (char)NK_TO_HEX((col.b & 0xF0) >> 4);
  7276. output[5] = (char)NK_TO_HEX((col.b & 0x0F));
  7277. output[6] = '\0';
  7278. #undef NK_TO_HEX
  7279. }
  7280. NK_API struct nk_color
  7281. nk_rgba_iv(const int* c)
  7282. {
  7283. return nk_rgba(c[0], c[1], c[2], c[3]);
  7284. }
  7285. NK_API struct nk_color
  7286. nk_rgba_bv(const nk_byte* c)
  7287. {
  7288. return nk_rgba(c[0], c[1], c[2], c[3]);
  7289. }
  7290. NK_API struct nk_color
  7291. nk_rgb(int r, int g, int b)
  7292. {
  7293. struct nk_color ret;
  7294. ret.r = (nk_byte)NK_CLAMP(0, r, 255);
  7295. ret.g = (nk_byte)NK_CLAMP(0, g, 255);
  7296. ret.b = (nk_byte)NK_CLAMP(0, b, 255);
  7297. ret.a = (nk_byte)255;
  7298. return ret;
  7299. }
  7300. NK_API struct nk_color
  7301. nk_rgb_iv(const int* c)
  7302. {
  7303. return nk_rgb(c[0], c[1], c[2]);
  7304. }
  7305. NK_API struct nk_color
  7306. nk_rgb_bv(const nk_byte* c)
  7307. {
  7308. return nk_rgb(c[0], c[1], c[2]);
  7309. }
  7310. NK_API struct nk_color
  7311. nk_rgba_u32(nk_uint in)
  7312. {
  7313. struct nk_color ret;
  7314. ret.r = (in & 0xFF);
  7315. ret.g = ((in >> 8) & 0xFF);
  7316. ret.b = ((in >> 16) & 0xFF);
  7317. ret.a = (nk_byte)((in >> 24) & 0xFF);
  7318. return ret;
  7319. }
  7320. NK_API struct nk_color
  7321. nk_rgba_f(float r, float g, float b, float a)
  7322. {
  7323. struct nk_color ret;
  7324. ret.r = (nk_byte)(NK_SATURATE(r) * 255.0f);
  7325. ret.g = (nk_byte)(NK_SATURATE(g) * 255.0f);
  7326. ret.b = (nk_byte)(NK_SATURATE(b) * 255.0f);
  7327. ret.a = (nk_byte)(NK_SATURATE(a) * 255.0f);
  7328. return ret;
  7329. }
  7330. NK_API struct nk_color
  7331. nk_rgba_fv(const float* c)
  7332. {
  7333. return nk_rgba_f(c[0], c[1], c[2], c[3]);
  7334. }
  7335. NK_API struct nk_color
  7336. nk_rgba_cf(struct nk_colorf c)
  7337. {
  7338. return nk_rgba_f(c.r, c.g, c.b, c.a);
  7339. }
  7340. NK_API struct nk_color
  7341. nk_rgb_f(float r, float g, float b)
  7342. {
  7343. struct nk_color ret;
  7344. ret.r = (nk_byte)(NK_SATURATE(r) * 255.0f);
  7345. ret.g = (nk_byte)(NK_SATURATE(g) * 255.0f);
  7346. ret.b = (nk_byte)(NK_SATURATE(b) * 255.0f);
  7347. ret.a = 255;
  7348. return ret;
  7349. }
  7350. NK_API struct nk_color
  7351. nk_rgb_fv(const float* c)
  7352. {
  7353. return nk_rgb_f(c[0], c[1], c[2]);
  7354. }
  7355. NK_API struct nk_color
  7356. nk_rgb_cf(struct nk_colorf c)
  7357. {
  7358. return nk_rgb_f(c.r, c.g, c.b);
  7359. }
  7360. NK_API struct nk_color
  7361. nk_hsv(int h, int s, int v)
  7362. {
  7363. return nk_hsva(h, s, v, 255);
  7364. }
  7365. NK_API struct nk_color
  7366. nk_hsv_iv(const int* c)
  7367. {
  7368. return nk_hsv(c[0], c[1], c[2]);
  7369. }
  7370. NK_API struct nk_color
  7371. nk_hsv_bv(const nk_byte* c)
  7372. {
  7373. return nk_hsv(c[0], c[1], c[2]);
  7374. }
  7375. NK_API struct nk_color
  7376. nk_hsv_f(float h, float s, float v)
  7377. {
  7378. return nk_hsva_f(h, s, v, 1.0f);
  7379. }
  7380. NK_API struct nk_color
  7381. nk_hsv_fv(const float* c)
  7382. {
  7383. return nk_hsv_f(c[0], c[1], c[2]);
  7384. }
  7385. NK_API struct nk_color
  7386. nk_hsva(int h, int s, int v, int a)
  7387. {
  7388. float hf = ((float)NK_CLAMP(0, h, 255)) / 255.0f;
  7389. float sf = ((float)NK_CLAMP(0, s, 255)) / 255.0f;
  7390. float vf = ((float)NK_CLAMP(0, v, 255)) / 255.0f;
  7391. float af = ((float)NK_CLAMP(0, a, 255)) / 255.0f;
  7392. return nk_hsva_f(hf, sf, vf, af);
  7393. }
  7394. NK_API struct nk_color
  7395. nk_hsva_iv(const int* c)
  7396. {
  7397. return nk_hsva(c[0], c[1], c[2], c[3]);
  7398. }
  7399. NK_API struct nk_color
  7400. nk_hsva_bv(const nk_byte* c)
  7401. {
  7402. return nk_hsva(c[0], c[1], c[2], c[3]);
  7403. }
  7404. NK_API struct nk_colorf
  7405. nk_hsva_colorf(float h, float s, float v, float a)
  7406. {
  7407. int i;
  7408. float p, q, t, f;
  7409. struct nk_colorf out = { 0,0,0,0 };
  7410. if (s <= 0.0f) {
  7411. out.r = v; out.g = v; out.b = v; out.a = a;
  7412. return out;
  7413. }
  7414. h = h / (60.0f / 360.0f);
  7415. i = (int)h;
  7416. f = h - (float)i;
  7417. p = v * (1.0f - s);
  7418. q = v * (1.0f - (s * f));
  7419. t = v * (1.0f - s * (1.0f - f));
  7420. switch (i) {
  7421. case 0: default: out.r = v; out.g = t; out.b = p; break;
  7422. case 1: out.r = q; out.g = v; out.b = p; break;
  7423. case 2: out.r = p; out.g = v; out.b = t; break;
  7424. case 3: out.r = p; out.g = q; out.b = v; break;
  7425. case 4: out.r = t; out.g = p; out.b = v; break;
  7426. case 5: out.r = v; out.g = p; out.b = q; break;
  7427. }
  7428. out.a = a;
  7429. return out;
  7430. }
  7431. NK_API struct nk_colorf
  7432. nk_hsva_colorfv(float* c)
  7433. {
  7434. return nk_hsva_colorf(c[0], c[1], c[2], c[3]);
  7435. }
  7436. NK_API struct nk_color
  7437. nk_hsva_f(float h, float s, float v, float a)
  7438. {
  7439. struct nk_colorf c = nk_hsva_colorf(h, s, v, a);
  7440. return nk_rgba_f(c.r, c.g, c.b, c.a);
  7441. }
  7442. NK_API struct nk_color
  7443. nk_hsva_fv(const float* c)
  7444. {
  7445. return nk_hsva_f(c[0], c[1], c[2], c[3]);
  7446. }
  7447. NK_API nk_uint
  7448. nk_color_u32(struct nk_color in)
  7449. {
  7450. nk_uint out = (nk_uint)in.r;
  7451. out |= ((nk_uint)in.g << 8);
  7452. out |= ((nk_uint)in.b << 16);
  7453. out |= ((nk_uint)in.a << 24);
  7454. return out;
  7455. }
  7456. NK_API void
  7457. nk_color_f(float* r, float* g, float* b, float* a, struct nk_color in)
  7458. {
  7459. NK_STORAGE const float s = 1.0f / 255.0f;
  7460. *r = (float)in.r * s;
  7461. *g = (float)in.g * s;
  7462. *b = (float)in.b * s;
  7463. *a = (float)in.a * s;
  7464. }
  7465. NK_API void
  7466. nk_color_fv(float* c, struct nk_color in)
  7467. {
  7468. nk_color_f(&c[0], &c[1], &c[2], &c[3], in);
  7469. }
  7470. NK_API struct nk_colorf
  7471. nk_color_cf(struct nk_color in)
  7472. {
  7473. struct nk_colorf o;
  7474. nk_color_f(&o.r, &o.g, &o.b, &o.a, in);
  7475. return o;
  7476. }
  7477. NK_API void
  7478. nk_color_d(double* r, double* g, double* b, double* a, struct nk_color in)
  7479. {
  7480. NK_STORAGE const double s = 1.0 / 255.0;
  7481. *r = (double)in.r * s;
  7482. *g = (double)in.g * s;
  7483. *b = (double)in.b * s;
  7484. *a = (double)in.a * s;
  7485. }
  7486. NK_API void
  7487. nk_color_dv(double* c, struct nk_color in)
  7488. {
  7489. nk_color_d(&c[0], &c[1], &c[2], &c[3], in);
  7490. }
  7491. NK_API void
  7492. nk_color_hsv_f(float* out_h, float* out_s, float* out_v, struct nk_color in)
  7493. {
  7494. float a;
  7495. nk_color_hsva_f(out_h, out_s, out_v, &a, in);
  7496. }
  7497. NK_API void
  7498. nk_color_hsv_fv(float* out, struct nk_color in)
  7499. {
  7500. float a;
  7501. nk_color_hsva_f(&out[0], &out[1], &out[2], &a, in);
  7502. }
  7503. NK_API void
  7504. nk_colorf_hsva_f(float* out_h, float* out_s,
  7505. float* out_v, float* out_a, struct nk_colorf in)
  7506. {
  7507. float chroma;
  7508. float K = 0.0f;
  7509. if (in.g < in.b) {
  7510. const float t = in.g; in.g = in.b; in.b = t;
  7511. K = -1.f;
  7512. }
  7513. if (in.r < in.g) {
  7514. const float t = in.r; in.r = in.g; in.g = t;
  7515. K = -2.f / 6.0f - K;
  7516. }
  7517. chroma = in.r - ((in.g < in.b) ? in.g : in.b);
  7518. *out_h = NK_ABS(K + (in.g - in.b) / (6.0f * chroma + 1e-20f));
  7519. *out_s = chroma / (in.r + 1e-20f);
  7520. *out_v = in.r;
  7521. *out_a = in.a;
  7522. }
  7523. NK_API void
  7524. nk_colorf_hsva_fv(float* hsva, struct nk_colorf in)
  7525. {
  7526. nk_colorf_hsva_f(&hsva[0], &hsva[1], &hsva[2], &hsva[3], in);
  7527. }
  7528. NK_API void
  7529. nk_color_hsva_f(float* out_h, float* out_s,
  7530. float* out_v, float* out_a, struct nk_color in)
  7531. {
  7532. struct nk_colorf col;
  7533. nk_color_f(&col.r, &col.g, &col.b, &col.a, in);
  7534. nk_colorf_hsva_f(out_h, out_s, out_v, out_a, col);
  7535. }
  7536. NK_API void
  7537. nk_color_hsva_fv(float* out, struct nk_color in)
  7538. {
  7539. nk_color_hsva_f(&out[0], &out[1], &out[2], &out[3], in);
  7540. }
  7541. NK_API void
  7542. nk_color_hsva_i(int* out_h, int* out_s, int* out_v,
  7543. int* out_a, struct nk_color in)
  7544. {
  7545. float h, s, v, a;
  7546. nk_color_hsva_f(&h, &s, &v, &a, in);
  7547. *out_h = (nk_byte)(h * 255.0f);
  7548. *out_s = (nk_byte)(s * 255.0f);
  7549. *out_v = (nk_byte)(v * 255.0f);
  7550. *out_a = (nk_byte)(a * 255.0f);
  7551. }
  7552. NK_API void
  7553. nk_color_hsva_iv(int* out, struct nk_color in)
  7554. {
  7555. nk_color_hsva_i(&out[0], &out[1], &out[2], &out[3], in);
  7556. }
  7557. NK_API void
  7558. nk_color_hsva_bv(nk_byte* out, struct nk_color in)
  7559. {
  7560. int tmp[4];
  7561. nk_color_hsva_i(&tmp[0], &tmp[1], &tmp[2], &tmp[3], in);
  7562. out[0] = (nk_byte)tmp[0];
  7563. out[1] = (nk_byte)tmp[1];
  7564. out[2] = (nk_byte)tmp[2];
  7565. out[3] = (nk_byte)tmp[3];
  7566. }
  7567. NK_API void
  7568. nk_color_hsva_b(nk_byte* h, nk_byte* s, nk_byte* v, nk_byte* a, struct nk_color in)
  7569. {
  7570. int tmp[4];
  7571. nk_color_hsva_i(&tmp[0], &tmp[1], &tmp[2], &tmp[3], in);
  7572. *h = (nk_byte)tmp[0];
  7573. *s = (nk_byte)tmp[1];
  7574. *v = (nk_byte)tmp[2];
  7575. *a = (nk_byte)tmp[3];
  7576. }
  7577. NK_API void
  7578. nk_color_hsv_i(int* out_h, int* out_s, int* out_v, struct nk_color in)
  7579. {
  7580. int a;
  7581. nk_color_hsva_i(out_h, out_s, out_v, &a, in);
  7582. }
  7583. NK_API void
  7584. nk_color_hsv_b(nk_byte* out_h, nk_byte* out_s, nk_byte* out_v, struct nk_color in)
  7585. {
  7586. int tmp[4];
  7587. nk_color_hsva_i(&tmp[0], &tmp[1], &tmp[2], &tmp[3], in);
  7588. *out_h = (nk_byte)tmp[0];
  7589. *out_s = (nk_byte)tmp[1];
  7590. *out_v = (nk_byte)tmp[2];
  7591. }
  7592. NK_API void
  7593. nk_color_hsv_iv(int* out, struct nk_color in)
  7594. {
  7595. nk_color_hsv_i(&out[0], &out[1], &out[2], in);
  7596. }
  7597. NK_API void
  7598. nk_color_hsv_bv(nk_byte* out, struct nk_color in)
  7599. {
  7600. int tmp[4];
  7601. nk_color_hsv_i(&tmp[0], &tmp[1], &tmp[2], in);
  7602. out[0] = (nk_byte)tmp[0];
  7603. out[1] = (nk_byte)tmp[1];
  7604. out[2] = (nk_byte)tmp[2];
  7605. }
  7606. /* ===============================================================
  7607. *
  7608. * UTF-8
  7609. *
  7610. * ===============================================================*/
  7611. NK_GLOBAL const nk_byte nk_utfbyte[NK_UTF_SIZE + 1] = { 0x80, 0, 0xC0, 0xE0, 0xF0 };
  7612. NK_GLOBAL const nk_byte nk_utfmask[NK_UTF_SIZE + 1] = { 0xC0, 0x80, 0xE0, 0xF0, 0xF8 };
  7613. NK_GLOBAL const nk_uint nk_utfmin[NK_UTF_SIZE + 1] = { 0, 0, 0x80, 0x800, 0x10000 };
  7614. NK_GLOBAL const nk_uint nk_utfmax[NK_UTF_SIZE + 1] = { 0x10FFFF, 0x7F, 0x7FF, 0xFFFF, 0x10FFFF };
  7615. NK_INTERN int
  7616. nk_utf_validate(nk_rune* u, int i)
  7617. {
  7618. NK_ASSERT(u);
  7619. if (!u) return 0;
  7620. if (!NK_BETWEEN(*u, nk_utfmin[i], nk_utfmax[i]) ||
  7621. NK_BETWEEN(*u, 0xD800, 0xDFFF))
  7622. *u = NK_UTF_INVALID;
  7623. for (i = 1; *u > nk_utfmax[i]; ++i);
  7624. return i;
  7625. }
  7626. NK_INTERN nk_rune
  7627. nk_utf_decode_byte(char c, int* i)
  7628. {
  7629. NK_ASSERT(i);
  7630. if (!i) return 0;
  7631. for (*i = 0; *i < (int)NK_LEN(nk_utfmask); ++(*i)) {
  7632. if (((nk_byte)c & nk_utfmask[*i]) == nk_utfbyte[*i])
  7633. return (nk_byte)(c & ~nk_utfmask[*i]);
  7634. }
  7635. return 0;
  7636. }
  7637. NK_API int
  7638. nk_utf_decode(const char* c, nk_rune* u, int clen)
  7639. {
  7640. int i, j, len, type = 0;
  7641. nk_rune udecoded;
  7642. NK_ASSERT(c);
  7643. NK_ASSERT(u);
  7644. if (!c || !u) return 0;
  7645. if (!clen) return 0;
  7646. *u = NK_UTF_INVALID;
  7647. udecoded = nk_utf_decode_byte(c[0], &len);
  7648. if (!NK_BETWEEN(len, 1, NK_UTF_SIZE))
  7649. return 1;
  7650. for (i = 1, j = 1; i < clen && j < len; ++i, ++j) {
  7651. udecoded = (udecoded << 6) | nk_utf_decode_byte(c[i], &type);
  7652. if (type != 0)
  7653. return j;
  7654. }
  7655. if (j < len)
  7656. return 0;
  7657. *u = udecoded;
  7658. nk_utf_validate(u, len);
  7659. return len;
  7660. }
  7661. NK_INTERN char
  7662. nk_utf_encode_byte(nk_rune u, int i)
  7663. {
  7664. return (char)((nk_utfbyte[i]) | ((nk_byte)u & ~nk_utfmask[i]));
  7665. }
  7666. NK_API int
  7667. nk_utf_encode(nk_rune u, char* c, int clen)
  7668. {
  7669. int len, i;
  7670. len = nk_utf_validate(&u, 0);
  7671. if (clen < len || !len || len > NK_UTF_SIZE)
  7672. return 0;
  7673. for (i = len - 1; i != 0; --i) {
  7674. c[i] = nk_utf_encode_byte(u, 0);
  7675. u >>= 6;
  7676. }
  7677. c[0] = nk_utf_encode_byte(u, len);
  7678. return len;
  7679. }
  7680. NK_API int
  7681. nk_utf_len(const char* str, int len)
  7682. {
  7683. const char* text;
  7684. int glyphs = 0;
  7685. int text_len;
  7686. int glyph_len;
  7687. int src_len = 0;
  7688. nk_rune unicode;
  7689. NK_ASSERT(str);
  7690. if (!str || !len) return 0;
  7691. text = str;
  7692. text_len = len;
  7693. glyph_len = nk_utf_decode(text, &unicode, text_len);
  7694. while (glyph_len && src_len < len) {
  7695. glyphs++;
  7696. src_len = src_len + glyph_len;
  7697. glyph_len = nk_utf_decode(text + src_len, &unicode, text_len - src_len);
  7698. }
  7699. return glyphs;
  7700. }
  7701. NK_API const char*
  7702. nk_utf_at(const char* buffer, int length, int index,
  7703. nk_rune* unicode, int* len)
  7704. {
  7705. int i = 0;
  7706. int src_len = 0;
  7707. int glyph_len = 0;
  7708. const char* text;
  7709. int text_len;
  7710. NK_ASSERT(buffer);
  7711. NK_ASSERT(unicode);
  7712. NK_ASSERT(len);
  7713. if (!buffer || !unicode || !len) return 0;
  7714. if (index < 0) {
  7715. *unicode = NK_UTF_INVALID;
  7716. *len = 0;
  7717. return 0;
  7718. }
  7719. text = buffer;
  7720. text_len = length;
  7721. glyph_len = nk_utf_decode(text, unicode, text_len);
  7722. while (glyph_len) {
  7723. if (i == index) {
  7724. *len = glyph_len;
  7725. break;
  7726. }
  7727. i++;
  7728. src_len = src_len + glyph_len;
  7729. glyph_len = nk_utf_decode(text + src_len, unicode, text_len - src_len);
  7730. }
  7731. if (i != index) return 0;
  7732. return buffer + src_len;
  7733. }
  7734. /* ==============================================================
  7735. *
  7736. * BUFFER
  7737. *
  7738. * ===============================================================*/
  7739. #ifdef NK_INCLUDE_DEFAULT_ALLOCATOR
  7740. NK_LIB void*
  7741. nk_malloc(nk_handle unused, void* old, nk_size size)
  7742. {
  7743. NK_UNUSED(unused);
  7744. NK_UNUSED(old);
  7745. return malloc(size);
  7746. }
  7747. NK_LIB void
  7748. nk_mfree(nk_handle unused, void* ptr)
  7749. {
  7750. NK_UNUSED(unused);
  7751. free(ptr);
  7752. }
  7753. NK_API void
  7754. nk_buffer_init_default(struct nk_buffer* buffer)
  7755. {
  7756. struct nk_allocator alloc;
  7757. alloc.userdata.ptr = 0;
  7758. alloc.alloc = nk_malloc;
  7759. alloc.free = nk_mfree;
  7760. nk_buffer_init(buffer, &alloc, NK_BUFFER_DEFAULT_INITIAL_SIZE);
  7761. }
  7762. #endif
  7763. NK_API void
  7764. nk_buffer_init(struct nk_buffer* b, const struct nk_allocator* a,
  7765. nk_size initial_size)
  7766. {
  7767. NK_ASSERT(b);
  7768. NK_ASSERT(a);
  7769. NK_ASSERT(initial_size);
  7770. if (!b || !a || !initial_size) return;
  7771. nk_zero(b, sizeof(*b));
  7772. b->type = NK_BUFFER_DYNAMIC;
  7773. b->memory.ptr = a->alloc(a->userdata, 0, initial_size);
  7774. b->memory.size = initial_size;
  7775. b->size = initial_size;
  7776. b->grow_factor = 2.0f;
  7777. b->pool = *a;
  7778. }
  7779. NK_API void
  7780. nk_buffer_init_fixed(struct nk_buffer* b, void* m, nk_size size)
  7781. {
  7782. NK_ASSERT(b);
  7783. NK_ASSERT(m);
  7784. NK_ASSERT(size);
  7785. if (!b || !m || !size) return;
  7786. nk_zero(b, sizeof(*b));
  7787. b->type = NK_BUFFER_FIXED;
  7788. b->memory.ptr = m;
  7789. b->memory.size = size;
  7790. b->size = size;
  7791. }
  7792. NK_LIB void*
  7793. nk_buffer_align(void* unaligned,
  7794. nk_size align, nk_size* alignment,
  7795. enum nk_buffer_allocation_type type)
  7796. {
  7797. void* memory = 0;
  7798. switch (type) {
  7799. default:
  7800. case NK_BUFFER_MAX:
  7801. case NK_BUFFER_FRONT:
  7802. if (align) {
  7803. memory = NK_ALIGN_PTR(unaligned, align);
  7804. *alignment = (nk_size)((nk_byte*)memory - (nk_byte*)unaligned);
  7805. }
  7806. else {
  7807. memory = unaligned;
  7808. *alignment = 0;
  7809. }
  7810. break;
  7811. case NK_BUFFER_BACK:
  7812. if (align) {
  7813. memory = NK_ALIGN_PTR_BACK(unaligned, align);
  7814. *alignment = (nk_size)((nk_byte*)unaligned - (nk_byte*)memory);
  7815. }
  7816. else {
  7817. memory = unaligned;
  7818. *alignment = 0;
  7819. }
  7820. break;
  7821. }
  7822. return memory;
  7823. }
  7824. NK_LIB void*
  7825. nk_buffer_realloc(struct nk_buffer* b, nk_size capacity, nk_size* size)
  7826. {
  7827. void* temp;
  7828. nk_size buffer_size;
  7829. NK_ASSERT(b);
  7830. NK_ASSERT(size);
  7831. if (!b || !size || !b->pool.alloc || !b->pool.free)
  7832. return 0;
  7833. buffer_size = b->memory.size;
  7834. temp = b->pool.alloc(b->pool.userdata, b->memory.ptr, capacity);
  7835. NK_ASSERT(temp);
  7836. if (!temp) return 0;
  7837. *size = capacity;
  7838. if (temp != b->memory.ptr) {
  7839. NK_MEMCPY(temp, b->memory.ptr, buffer_size);
  7840. b->pool.free(b->pool.userdata, b->memory.ptr);
  7841. }
  7842. if (b->size == buffer_size) {
  7843. /* no back buffer so just set correct size */
  7844. b->size = capacity;
  7845. return temp;
  7846. }
  7847. else {
  7848. /* copy back buffer to the end of the new buffer */
  7849. void* dst, * src;
  7850. nk_size back_size;
  7851. back_size = buffer_size - b->size;
  7852. dst = nk_ptr_add(void, temp, capacity - back_size);
  7853. src = nk_ptr_add(void, temp, b->size);
  7854. NK_MEMCPY(dst, src, back_size);
  7855. b->size = capacity - back_size;
  7856. }
  7857. return temp;
  7858. }
  7859. NK_LIB void*
  7860. nk_buffer_alloc(struct nk_buffer* b, enum nk_buffer_allocation_type type,
  7861. nk_size size, nk_size align)
  7862. {
  7863. int full;
  7864. nk_size alignment;
  7865. void* unaligned;
  7866. void* memory;
  7867. NK_ASSERT(b);
  7868. NK_ASSERT(size);
  7869. if (!b || !size) return 0;
  7870. b->needed += size;
  7871. /* calculate total size with needed alignment + size */
  7872. if (type == NK_BUFFER_FRONT)
  7873. unaligned = nk_ptr_add(void, b->memory.ptr, b->allocated);
  7874. else unaligned = nk_ptr_add(void, b->memory.ptr, b->size - size);
  7875. memory = nk_buffer_align(unaligned, align, &alignment, type);
  7876. /* check if buffer has enough memory*/
  7877. if (type == NK_BUFFER_FRONT)
  7878. full = ((b->allocated + size + alignment) > b->size);
  7879. else full = ((b->size - NK_MIN(b->size, (size + alignment))) <= b->allocated);
  7880. if (full) {
  7881. nk_size capacity;
  7882. if (b->type != NK_BUFFER_DYNAMIC)
  7883. return 0;
  7884. NK_ASSERT(b->pool.alloc && b->pool.free);
  7885. if (b->type != NK_BUFFER_DYNAMIC || !b->pool.alloc || !b->pool.free)
  7886. return 0;
  7887. /* buffer is full so allocate bigger buffer if dynamic */
  7888. capacity = (nk_size)((float)b->memory.size * b->grow_factor);
  7889. capacity = NK_MAX(capacity, nk_round_up_pow2((nk_uint)(b->allocated + size)));
  7890. b->memory.ptr = nk_buffer_realloc(b, capacity, &b->memory.size);
  7891. if (!b->memory.ptr) return 0;
  7892. /* align newly allocated pointer */
  7893. if (type == NK_BUFFER_FRONT)
  7894. unaligned = nk_ptr_add(void, b->memory.ptr, b->allocated);
  7895. else unaligned = nk_ptr_add(void, b->memory.ptr, b->size - size);
  7896. memory = nk_buffer_align(unaligned, align, &alignment, type);
  7897. }
  7898. if (type == NK_BUFFER_FRONT)
  7899. b->allocated += size + alignment;
  7900. else b->size -= (size + alignment);
  7901. b->needed += alignment;
  7902. b->calls++;
  7903. return memory;
  7904. }
  7905. NK_API void
  7906. nk_buffer_push(struct nk_buffer* b, enum nk_buffer_allocation_type type,
  7907. const void* memory, nk_size size, nk_size align)
  7908. {
  7909. void* mem = nk_buffer_alloc(b, type, size, align);
  7910. if (!mem) return;
  7911. NK_MEMCPY(mem, memory, size);
  7912. }
  7913. NK_API void
  7914. nk_buffer_mark(struct nk_buffer* buffer, enum nk_buffer_allocation_type type)
  7915. {
  7916. NK_ASSERT(buffer);
  7917. if (!buffer) return;
  7918. buffer->marker[type].active = nk_true;
  7919. if (type == NK_BUFFER_BACK)
  7920. buffer->marker[type].offset = buffer->size;
  7921. else buffer->marker[type].offset = buffer->allocated;
  7922. }
  7923. NK_API void
  7924. nk_buffer_reset(struct nk_buffer* buffer, enum nk_buffer_allocation_type type)
  7925. {
  7926. NK_ASSERT(buffer);
  7927. if (!buffer) return;
  7928. if (type == NK_BUFFER_BACK) {
  7929. /* reset back buffer either back to marker or empty */
  7930. buffer->needed -= (buffer->memory.size - buffer->marker[type].offset);
  7931. if (buffer->marker[type].active)
  7932. buffer->size = buffer->marker[type].offset;
  7933. else buffer->size = buffer->memory.size;
  7934. buffer->marker[type].active = nk_false;
  7935. }
  7936. else {
  7937. /* reset front buffer either back to back marker or empty */
  7938. buffer->needed -= (buffer->allocated - buffer->marker[type].offset);
  7939. if (buffer->marker[type].active)
  7940. buffer->allocated = buffer->marker[type].offset;
  7941. else buffer->allocated = 0;
  7942. buffer->marker[type].active = nk_false;
  7943. }
  7944. }
  7945. NK_API void
  7946. nk_buffer_clear(struct nk_buffer* b)
  7947. {
  7948. NK_ASSERT(b);
  7949. if (!b) return;
  7950. b->allocated = 0;
  7951. b->size = b->memory.size;
  7952. b->calls = 0;
  7953. b->needed = 0;
  7954. }
  7955. NK_API void
  7956. nk_buffer_free(struct nk_buffer* b)
  7957. {
  7958. NK_ASSERT(b);
  7959. if (!b || !b->memory.ptr) return;
  7960. if (b->type == NK_BUFFER_FIXED) return;
  7961. if (!b->pool.free) return;
  7962. NK_ASSERT(b->pool.free);
  7963. b->pool.free(b->pool.userdata, b->memory.ptr);
  7964. }
  7965. NK_API void
  7966. nk_buffer_info(struct nk_memory_status* s, struct nk_buffer* b)
  7967. {
  7968. NK_ASSERT(b);
  7969. NK_ASSERT(s);
  7970. if (!s || !b) return;
  7971. s->allocated = b->allocated;
  7972. s->size = b->memory.size;
  7973. s->needed = b->needed;
  7974. s->memory = b->memory.ptr;
  7975. s->calls = b->calls;
  7976. }
  7977. NK_API void*
  7978. nk_buffer_memory(struct nk_buffer* buffer)
  7979. {
  7980. NK_ASSERT(buffer);
  7981. if (!buffer) return 0;
  7982. return buffer->memory.ptr;
  7983. }
  7984. NK_API const void*
  7985. nk_buffer_memory_const(const struct nk_buffer* buffer)
  7986. {
  7987. NK_ASSERT(buffer);
  7988. if (!buffer) return 0;
  7989. return buffer->memory.ptr;
  7990. }
  7991. NK_API nk_size
  7992. nk_buffer_total(struct nk_buffer* buffer)
  7993. {
  7994. NK_ASSERT(buffer);
  7995. if (!buffer) return 0;
  7996. return buffer->memory.size;
  7997. }
  7998. /* ===============================================================
  7999. *
  8000. * STRING
  8001. *
  8002. * ===============================================================*/
  8003. #ifdef NK_INCLUDE_DEFAULT_ALLOCATOR
  8004. NK_API void
  8005. nk_str_init_default(struct nk_str* str)
  8006. {
  8007. struct nk_allocator alloc;
  8008. alloc.userdata.ptr = 0;
  8009. alloc.alloc = nk_malloc;
  8010. alloc.free = nk_mfree;
  8011. nk_buffer_init(&str->buffer, &alloc, 32);
  8012. str->len = 0;
  8013. }
  8014. #endif
  8015. NK_API void
  8016. nk_str_init(struct nk_str* str, const struct nk_allocator* alloc, nk_size size)
  8017. {
  8018. nk_buffer_init(&str->buffer, alloc, size);
  8019. str->len = 0;
  8020. }
  8021. NK_API void
  8022. nk_str_init_fixed(struct nk_str* str, void* memory, nk_size size)
  8023. {
  8024. nk_buffer_init_fixed(&str->buffer, memory, size);
  8025. str->len = 0;
  8026. }
  8027. NK_API int
  8028. nk_str_append_text_char(struct nk_str* s, const char* str, int len)
  8029. {
  8030. char* mem;
  8031. NK_ASSERT(s);
  8032. NK_ASSERT(str);
  8033. if (!s || !str || !len) return 0;
  8034. mem = (char*)nk_buffer_alloc(&s->buffer, NK_BUFFER_FRONT, (nk_size)len * sizeof(char), 0);
  8035. if (!mem) return 0;
  8036. NK_MEMCPY(mem, str, (nk_size)len * sizeof(char));
  8037. s->len += nk_utf_len(str, len);
  8038. return len;
  8039. }
  8040. NK_API int
  8041. nk_str_append_str_char(struct nk_str* s, const char* str)
  8042. {
  8043. return nk_str_append_text_char(s, str, nk_strlen(str));
  8044. }
  8045. NK_API int
  8046. nk_str_append_text_utf8(struct nk_str* str, const char* text, int len)
  8047. {
  8048. int i = 0;
  8049. int byte_len = 0;
  8050. nk_rune unicode;
  8051. if (!str || !text || !len) return 0;
  8052. for (i = 0; i < len; ++i)
  8053. byte_len += nk_utf_decode(text + byte_len, &unicode, 4);
  8054. nk_str_append_text_char(str, text, byte_len);
  8055. return len;
  8056. }
  8057. NK_API int
  8058. nk_str_append_str_utf8(struct nk_str* str, const char* text)
  8059. {
  8060. int byte_len = 0;
  8061. int num_runes = 0;
  8062. int glyph_len = 0;
  8063. nk_rune unicode;
  8064. if (!str || !text) return 0;
  8065. glyph_len = byte_len = nk_utf_decode(text + byte_len, &unicode, 4);
  8066. while (unicode != '\0' && glyph_len) {
  8067. glyph_len = nk_utf_decode(text + byte_len, &unicode, 4);
  8068. byte_len += glyph_len;
  8069. num_runes++;
  8070. }
  8071. nk_str_append_text_char(str, text, byte_len);
  8072. return num_runes;
  8073. }
  8074. NK_API int
  8075. nk_str_append_text_runes(struct nk_str* str, const nk_rune* text, int len)
  8076. {
  8077. int i = 0;
  8078. int byte_len = 0;
  8079. nk_glyph glyph;
  8080. NK_ASSERT(str);
  8081. if (!str || !text || !len) return 0;
  8082. for (i = 0; i < len; ++i) {
  8083. byte_len = nk_utf_encode(text[i], glyph, NK_UTF_SIZE);
  8084. if (!byte_len) break;
  8085. nk_str_append_text_char(str, glyph, byte_len);
  8086. }
  8087. return len;
  8088. }
  8089. NK_API int
  8090. nk_str_append_str_runes(struct nk_str* str, const nk_rune* runes)
  8091. {
  8092. int i = 0;
  8093. nk_glyph glyph;
  8094. int byte_len;
  8095. NK_ASSERT(str);
  8096. if (!str || !runes) return 0;
  8097. while (runes[i] != '\0') {
  8098. byte_len = nk_utf_encode(runes[i], glyph, NK_UTF_SIZE);
  8099. nk_str_append_text_char(str, glyph, byte_len);
  8100. i++;
  8101. }
  8102. return i;
  8103. }
  8104. NK_API int
  8105. nk_str_insert_at_char(struct nk_str* s, int pos, const char* str, int len)
  8106. {
  8107. int i;
  8108. void* mem;
  8109. char* src;
  8110. char* dst;
  8111. int copylen;
  8112. NK_ASSERT(s);
  8113. NK_ASSERT(str);
  8114. NK_ASSERT(len >= 0);
  8115. if (!s || !str || !len || (nk_size)pos > s->buffer.allocated) return 0;
  8116. if ((s->buffer.allocated + (nk_size)len >= s->buffer.memory.size) &&
  8117. (s->buffer.type == NK_BUFFER_FIXED)) return 0;
  8118. copylen = (int)s->buffer.allocated - pos;
  8119. if (!copylen) {
  8120. nk_str_append_text_char(s, str, len);
  8121. return 1;
  8122. }
  8123. mem = nk_buffer_alloc(&s->buffer, NK_BUFFER_FRONT, (nk_size)len * sizeof(char), 0);
  8124. if (!mem) return 0;
  8125. /* memmove */
  8126. NK_ASSERT(((int)pos + (int)len + ((int)copylen - 1)) >= 0);
  8127. NK_ASSERT(((int)pos + ((int)copylen - 1)) >= 0);
  8128. dst = nk_ptr_add(char, s->buffer.memory.ptr, pos + len + (copylen - 1));
  8129. src = nk_ptr_add(char, s->buffer.memory.ptr, pos + (copylen - 1));
  8130. for (i = 0; i < copylen; ++i) *dst-- = *src--;
  8131. mem = nk_ptr_add(void, s->buffer.memory.ptr, pos);
  8132. NK_MEMCPY(mem, str, (nk_size)len * sizeof(char));
  8133. s->len = nk_utf_len((char*)s->buffer.memory.ptr, (int)s->buffer.allocated);
  8134. return 1;
  8135. }
  8136. NK_API int
  8137. nk_str_insert_at_rune(struct nk_str* str, int pos, const char* cstr, int len)
  8138. {
  8139. int glyph_len;
  8140. nk_rune unicode;
  8141. const char* begin;
  8142. const char* buffer;
  8143. NK_ASSERT(str);
  8144. NK_ASSERT(cstr);
  8145. NK_ASSERT(len);
  8146. if (!str || !cstr || !len) return 0;
  8147. begin = nk_str_at_rune(str, pos, &unicode, &glyph_len);
  8148. if (!str->len)
  8149. return nk_str_append_text_char(str, cstr, len);
  8150. buffer = nk_str_get_const(str);
  8151. if (!begin) return 0;
  8152. return nk_str_insert_at_char(str, (int)(begin - buffer), cstr, len);
  8153. }
  8154. NK_API int
  8155. nk_str_insert_text_char(struct nk_str* str, int pos, const char* text, int len)
  8156. {
  8157. return nk_str_insert_text_utf8(str, pos, text, len);
  8158. }
  8159. NK_API int
  8160. nk_str_insert_str_char(struct nk_str* str, int pos, const char* text)
  8161. {
  8162. return nk_str_insert_text_utf8(str, pos, text, nk_strlen(text));
  8163. }
  8164. NK_API int
  8165. nk_str_insert_text_utf8(struct nk_str* str, int pos, const char* text, int len)
  8166. {
  8167. int i = 0;
  8168. int byte_len = 0;
  8169. nk_rune unicode;
  8170. NK_ASSERT(str);
  8171. NK_ASSERT(text);
  8172. if (!str || !text || !len) return 0;
  8173. for (i = 0; i < len; ++i)
  8174. byte_len += nk_utf_decode(text + byte_len, &unicode, 4);
  8175. nk_str_insert_at_rune(str, pos, text, byte_len);
  8176. return len;
  8177. }
  8178. NK_API int
  8179. nk_str_insert_str_utf8(struct nk_str* str, int pos, const char* text)
  8180. {
  8181. int byte_len = 0;
  8182. int num_runes = 0;
  8183. int glyph_len = 0;
  8184. nk_rune unicode;
  8185. if (!str || !text) return 0;
  8186. glyph_len = byte_len = nk_utf_decode(text + byte_len, &unicode, 4);
  8187. while (unicode != '\0' && glyph_len) {
  8188. glyph_len = nk_utf_decode(text + byte_len, &unicode, 4);
  8189. byte_len += glyph_len;
  8190. num_runes++;
  8191. }
  8192. nk_str_insert_at_rune(str, pos, text, byte_len);
  8193. return num_runes;
  8194. }
  8195. NK_API int
  8196. nk_str_insert_text_runes(struct nk_str* str, int pos, const nk_rune* runes, int len)
  8197. {
  8198. int i = 0;
  8199. int byte_len = 0;
  8200. nk_glyph glyph;
  8201. NK_ASSERT(str);
  8202. if (!str || !runes || !len) return 0;
  8203. for (i = 0; i < len; ++i) {
  8204. byte_len = nk_utf_encode(runes[i], glyph, NK_UTF_SIZE);
  8205. if (!byte_len) break;
  8206. nk_str_insert_at_rune(str, pos + i, glyph, byte_len);
  8207. }
  8208. return len;
  8209. }
  8210. NK_API int
  8211. nk_str_insert_str_runes(struct nk_str* str, int pos, const nk_rune* runes)
  8212. {
  8213. int i = 0;
  8214. nk_glyph glyph;
  8215. int byte_len;
  8216. NK_ASSERT(str);
  8217. if (!str || !runes) return 0;
  8218. while (runes[i] != '\0') {
  8219. byte_len = nk_utf_encode(runes[i], glyph, NK_UTF_SIZE);
  8220. nk_str_insert_at_rune(str, pos + i, glyph, byte_len);
  8221. i++;
  8222. }
  8223. return i;
  8224. }
  8225. NK_API void
  8226. nk_str_remove_chars(struct nk_str* s, int len)
  8227. {
  8228. NK_ASSERT(s);
  8229. NK_ASSERT(len >= 0);
  8230. if (!s || len < 0 || (nk_size)len > s->buffer.allocated) return;
  8231. NK_ASSERT(((int)s->buffer.allocated - (int)len) >= 0);
  8232. s->buffer.allocated -= (nk_size)len;
  8233. s->len = nk_utf_len((char*)s->buffer.memory.ptr, (int)s->buffer.allocated);
  8234. }
  8235. NK_API void
  8236. nk_str_remove_runes(struct nk_str* str, int len)
  8237. {
  8238. int index;
  8239. const char* begin;
  8240. const char* end;
  8241. nk_rune unicode;
  8242. NK_ASSERT(str);
  8243. NK_ASSERT(len >= 0);
  8244. if (!str || len < 0) return;
  8245. if (len >= str->len) {
  8246. str->len = 0;
  8247. return;
  8248. }
  8249. index = str->len - len;
  8250. begin = nk_str_at_rune(str, index, &unicode, &len);
  8251. end = (const char*)str->buffer.memory.ptr + str->buffer.allocated;
  8252. nk_str_remove_chars(str, (int)(end - begin) + 1);
  8253. }
  8254. NK_API void
  8255. nk_str_delete_chars(struct nk_str* s, int pos, int len)
  8256. {
  8257. NK_ASSERT(s);
  8258. if (!s || !len || (nk_size)pos > s->buffer.allocated ||
  8259. (nk_size)(pos + len) > s->buffer.allocated) return;
  8260. if ((nk_size)(pos + len) < s->buffer.allocated) {
  8261. /* memmove */
  8262. char* dst = nk_ptr_add(char, s->buffer.memory.ptr, pos);
  8263. char* src = nk_ptr_add(char, s->buffer.memory.ptr, pos + len);
  8264. NK_MEMCPY(dst, src, s->buffer.allocated - (nk_size)(pos + len));
  8265. NK_ASSERT(((int)s->buffer.allocated - (int)len) >= 0);
  8266. s->buffer.allocated -= (nk_size)len;
  8267. }
  8268. else nk_str_remove_chars(s, len);
  8269. s->len = nk_utf_len((char*)s->buffer.memory.ptr, (int)s->buffer.allocated);
  8270. }
  8271. NK_API void
  8272. nk_str_delete_runes(struct nk_str* s, int pos, int len)
  8273. {
  8274. char* temp;
  8275. nk_rune unicode;
  8276. char* begin;
  8277. char* end;
  8278. int unused;
  8279. NK_ASSERT(s);
  8280. NK_ASSERT(s->len >= pos + len);
  8281. if (s->len < pos + len)
  8282. len = NK_CLAMP(0, (s->len - pos), s->len);
  8283. if (!len) return;
  8284. temp = (char*)s->buffer.memory.ptr;
  8285. begin = nk_str_at_rune(s, pos, &unicode, &unused);
  8286. if (!begin) return;
  8287. s->buffer.memory.ptr = begin;
  8288. end = nk_str_at_rune(s, len, &unicode, &unused);
  8289. s->buffer.memory.ptr = temp;
  8290. if (!end) return;
  8291. nk_str_delete_chars(s, (int)(begin - temp), (int)(end - begin));
  8292. }
  8293. NK_API char*
  8294. nk_str_at_char(struct nk_str* s, int pos)
  8295. {
  8296. NK_ASSERT(s);
  8297. if (!s || pos > (int)s->buffer.allocated) return 0;
  8298. return nk_ptr_add(char, s->buffer.memory.ptr, pos);
  8299. }
  8300. NK_API char*
  8301. nk_str_at_rune(struct nk_str* str, int pos, nk_rune* unicode, int* len)
  8302. {
  8303. int i = 0;
  8304. int src_len = 0;
  8305. int glyph_len = 0;
  8306. char* text;
  8307. int text_len;
  8308. NK_ASSERT(str);
  8309. NK_ASSERT(unicode);
  8310. NK_ASSERT(len);
  8311. if (!str || !unicode || !len) return 0;
  8312. if (pos < 0) {
  8313. *unicode = 0;
  8314. *len = 0;
  8315. return 0;
  8316. }
  8317. text = (char*)str->buffer.memory.ptr;
  8318. text_len = (int)str->buffer.allocated;
  8319. glyph_len = nk_utf_decode(text, unicode, text_len);
  8320. while (glyph_len) {
  8321. if (i == pos) {
  8322. *len = glyph_len;
  8323. break;
  8324. }
  8325. i++;
  8326. src_len = src_len + glyph_len;
  8327. glyph_len = nk_utf_decode(text + src_len, unicode, text_len - src_len);
  8328. }
  8329. if (i != pos) return 0;
  8330. return text + src_len;
  8331. }
  8332. NK_API const char*
  8333. nk_str_at_char_const(const struct nk_str* s, int pos)
  8334. {
  8335. NK_ASSERT(s);
  8336. if (!s || pos > (int)s->buffer.allocated) return 0;
  8337. return nk_ptr_add(char, s->buffer.memory.ptr, pos);
  8338. }
  8339. NK_API const char*
  8340. nk_str_at_const(const struct nk_str* str, int pos, nk_rune* unicode, int* len)
  8341. {
  8342. int i = 0;
  8343. int src_len = 0;
  8344. int glyph_len = 0;
  8345. char* text;
  8346. int text_len;
  8347. NK_ASSERT(str);
  8348. NK_ASSERT(unicode);
  8349. NK_ASSERT(len);
  8350. if (!str || !unicode || !len) return 0;
  8351. if (pos < 0) {
  8352. *unicode = 0;
  8353. *len = 0;
  8354. return 0;
  8355. }
  8356. text = (char*)str->buffer.memory.ptr;
  8357. text_len = (int)str->buffer.allocated;
  8358. glyph_len = nk_utf_decode(text, unicode, text_len);
  8359. while (glyph_len) {
  8360. if (i == pos) {
  8361. *len = glyph_len;
  8362. break;
  8363. }
  8364. i++;
  8365. src_len = src_len + glyph_len;
  8366. glyph_len = nk_utf_decode(text + src_len, unicode, text_len - src_len);
  8367. }
  8368. if (i != pos) return 0;
  8369. return text + src_len;
  8370. }
  8371. NK_API nk_rune
  8372. nk_str_rune_at(const struct nk_str* str, int pos)
  8373. {
  8374. int len;
  8375. nk_rune unicode = 0;
  8376. nk_str_at_const(str, pos, &unicode, &len);
  8377. return unicode;
  8378. }
  8379. NK_API char*
  8380. nk_str_get(struct nk_str* s)
  8381. {
  8382. NK_ASSERT(s);
  8383. if (!s || !s->len || !s->buffer.allocated) return 0;
  8384. return (char*)s->buffer.memory.ptr;
  8385. }
  8386. NK_API const char*
  8387. nk_str_get_const(const struct nk_str* s)
  8388. {
  8389. NK_ASSERT(s);
  8390. if (!s || !s->len || !s->buffer.allocated) return 0;
  8391. return (const char*)s->buffer.memory.ptr;
  8392. }
  8393. NK_API int
  8394. nk_str_len(struct nk_str* s)
  8395. {
  8396. NK_ASSERT(s);
  8397. if (!s || !s->len || !s->buffer.allocated) return 0;
  8398. return s->len;
  8399. }
  8400. NK_API int
  8401. nk_str_len_char(struct nk_str* s)
  8402. {
  8403. NK_ASSERT(s);
  8404. if (!s || !s->len || !s->buffer.allocated) return 0;
  8405. return (int)s->buffer.allocated;
  8406. }
  8407. NK_API void
  8408. nk_str_clear(struct nk_str* str)
  8409. {
  8410. NK_ASSERT(str);
  8411. nk_buffer_clear(&str->buffer);
  8412. str->len = 0;
  8413. }
  8414. NK_API void
  8415. nk_str_free(struct nk_str* str)
  8416. {
  8417. NK_ASSERT(str);
  8418. nk_buffer_free(&str->buffer);
  8419. str->len = 0;
  8420. }
  8421. /* ==============================================================
  8422. *
  8423. * DRAW
  8424. *
  8425. * ===============================================================*/
  8426. NK_LIB void
  8427. nk_command_buffer_init(struct nk_command_buffer* cb,
  8428. struct nk_buffer* b, enum nk_command_clipping clip)
  8429. {
  8430. NK_ASSERT(cb);
  8431. NK_ASSERT(b);
  8432. if (!cb || !b) return;
  8433. cb->base = b;
  8434. cb->use_clipping = (int)clip;
  8435. cb->begin = b->allocated;
  8436. cb->end = b->allocated;
  8437. cb->last = b->allocated;
  8438. }
  8439. NK_LIB void
  8440. nk_command_buffer_reset(struct nk_command_buffer* b)
  8441. {
  8442. NK_ASSERT(b);
  8443. if (!b) return;
  8444. b->begin = 0;
  8445. b->end = 0;
  8446. b->last = 0;
  8447. b->clip = nk_null_rect;
  8448. #ifdef NK_INCLUDE_COMMAND_USERDATA
  8449. b->userdata.ptr = 0;
  8450. #endif
  8451. }
  8452. NK_LIB void*
  8453. nk_command_buffer_push(struct nk_command_buffer* b,
  8454. enum nk_command_type t, nk_size size)
  8455. {
  8456. NK_STORAGE const nk_size align = NK_ALIGNOF(struct nk_command);
  8457. struct nk_command* cmd;
  8458. nk_size alignment;
  8459. void* unaligned;
  8460. void* memory;
  8461. NK_ASSERT(b);
  8462. NK_ASSERT(b->base);
  8463. if (!b) return 0;
  8464. cmd = (struct nk_command*)nk_buffer_alloc(b->base, NK_BUFFER_FRONT, size, align);
  8465. if (!cmd) return 0;
  8466. /* make sure the offset to the next command is aligned */
  8467. b->last = (nk_size)((nk_byte*)cmd - (nk_byte*)b->base->memory.ptr);
  8468. unaligned = (nk_byte*)cmd + size;
  8469. memory = NK_ALIGN_PTR(unaligned, align);
  8470. alignment = (nk_size)((nk_byte*)memory - (nk_byte*)unaligned);
  8471. #ifdef NK_ZERO_COMMAND_MEMORY
  8472. NK_MEMSET(cmd, 0, size + alignment);
  8473. #endif
  8474. cmd->type = t;
  8475. cmd->next = b->base->allocated + alignment;
  8476. #ifdef NK_INCLUDE_COMMAND_USERDATA
  8477. cmd->userdata = b->userdata;
  8478. #endif
  8479. b->end = cmd->next;
  8480. return cmd;
  8481. }
  8482. NK_API void
  8483. nk_push_scissor(struct nk_command_buffer* b, struct nk_rect r)
  8484. {
  8485. struct nk_command_scissor* cmd;
  8486. NK_ASSERT(b);
  8487. if (!b) return;
  8488. b->clip.x = r.x;
  8489. b->clip.y = r.y;
  8490. b->clip.w = r.w;
  8491. b->clip.h = r.h;
  8492. cmd = (struct nk_command_scissor*)
  8493. nk_command_buffer_push(b, NK_COMMAND_SCISSOR, sizeof(*cmd));
  8494. if (!cmd) return;
  8495. cmd->x = (short)r.x;
  8496. cmd->y = (short)r.y;
  8497. cmd->w = (unsigned short)NK_MAX(0, r.w);
  8498. cmd->h = (unsigned short)NK_MAX(0, r.h);
  8499. }
  8500. NK_API void
  8501. nk_stroke_line(struct nk_command_buffer* b, float x0, float y0,
  8502. float x1, float y1, float line_thickness, struct nk_color c)
  8503. {
  8504. struct nk_command_line* cmd;
  8505. NK_ASSERT(b);
  8506. if (!b || line_thickness <= 0) return;
  8507. cmd = (struct nk_command_line*)
  8508. nk_command_buffer_push(b, NK_COMMAND_LINE, sizeof(*cmd));
  8509. if (!cmd) return;
  8510. cmd->line_thickness = (unsigned short)line_thickness;
  8511. cmd->begin.x = (short)x0;
  8512. cmd->begin.y = (short)y0;
  8513. cmd->end.x = (short)x1;
  8514. cmd->end.y = (short)y1;
  8515. cmd->color = c;
  8516. }
  8517. NK_API void
  8518. nk_stroke_curve(struct nk_command_buffer* b, float ax, float ay,
  8519. float ctrl0x, float ctrl0y, float ctrl1x, float ctrl1y,
  8520. float bx, float by, float line_thickness, struct nk_color col)
  8521. {
  8522. struct nk_command_curve* cmd;
  8523. NK_ASSERT(b);
  8524. if (!b || col.a == 0 || line_thickness <= 0) return;
  8525. cmd = (struct nk_command_curve*)
  8526. nk_command_buffer_push(b, NK_COMMAND_CURVE, sizeof(*cmd));
  8527. if (!cmd) return;
  8528. cmd->line_thickness = (unsigned short)line_thickness;
  8529. cmd->begin.x = (short)ax;
  8530. cmd->begin.y = (short)ay;
  8531. cmd->ctrl[0].x = (short)ctrl0x;
  8532. cmd->ctrl[0].y = (short)ctrl0y;
  8533. cmd->ctrl[1].x = (short)ctrl1x;
  8534. cmd->ctrl[1].y = (short)ctrl1y;
  8535. cmd->end.x = (short)bx;
  8536. cmd->end.y = (short)by;
  8537. cmd->color = col;
  8538. }
  8539. NK_API void
  8540. nk_stroke_rect(struct nk_command_buffer* b, struct nk_rect rect,
  8541. float rounding, float line_thickness, struct nk_color c)
  8542. {
  8543. struct nk_command_rect* cmd;
  8544. NK_ASSERT(b);
  8545. if (!b || c.a == 0 || rect.w == 0 || rect.h == 0 || line_thickness <= 0) return;
  8546. if (b->use_clipping) {
  8547. const struct nk_rect* clip = &b->clip;
  8548. if (!NK_INTERSECT(rect.x, rect.y, rect.w, rect.h,
  8549. clip->x, clip->y, clip->w, clip->h)) return;
  8550. }
  8551. cmd = (struct nk_command_rect*)
  8552. nk_command_buffer_push(b, NK_COMMAND_RECT, sizeof(*cmd));
  8553. if (!cmd) return;
  8554. cmd->rounding = (unsigned short)rounding;
  8555. cmd->line_thickness = (unsigned short)line_thickness;
  8556. cmd->x = (short)rect.x;
  8557. cmd->y = (short)rect.y;
  8558. cmd->w = (unsigned short)NK_MAX(0, rect.w);
  8559. cmd->h = (unsigned short)NK_MAX(0, rect.h);
  8560. cmd->color = c;
  8561. }
  8562. NK_API void
  8563. nk_fill_rect(struct nk_command_buffer* b, struct nk_rect rect,
  8564. float rounding, struct nk_color c)
  8565. {
  8566. struct nk_command_rect_filled* cmd;
  8567. NK_ASSERT(b);
  8568. if (!b || c.a == 0 || rect.w == 0 || rect.h == 0) return;
  8569. if (b->use_clipping) {
  8570. const struct nk_rect* clip = &b->clip;
  8571. if (!NK_INTERSECT(rect.x, rect.y, rect.w, rect.h,
  8572. clip->x, clip->y, clip->w, clip->h)) return;
  8573. }
  8574. cmd = (struct nk_command_rect_filled*)
  8575. nk_command_buffer_push(b, NK_COMMAND_RECT_FILLED, sizeof(*cmd));
  8576. if (!cmd) return;
  8577. cmd->rounding = (unsigned short)rounding;
  8578. cmd->x = (short)rect.x;
  8579. cmd->y = (short)rect.y;
  8580. cmd->w = (unsigned short)NK_MAX(0, rect.w);
  8581. cmd->h = (unsigned short)NK_MAX(0, rect.h);
  8582. cmd->color = c;
  8583. }
  8584. NK_API void
  8585. nk_fill_rect_multi_color(struct nk_command_buffer* b, struct nk_rect rect,
  8586. struct nk_color left, struct nk_color top, struct nk_color right,
  8587. struct nk_color bottom)
  8588. {
  8589. struct nk_command_rect_multi_color* cmd;
  8590. NK_ASSERT(b);
  8591. if (!b || rect.w == 0 || rect.h == 0) return;
  8592. if (b->use_clipping) {
  8593. const struct nk_rect* clip = &b->clip;
  8594. if (!NK_INTERSECT(rect.x, rect.y, rect.w, rect.h,
  8595. clip->x, clip->y, clip->w, clip->h)) return;
  8596. }
  8597. cmd = (struct nk_command_rect_multi_color*)
  8598. nk_command_buffer_push(b, NK_COMMAND_RECT_MULTI_COLOR, sizeof(*cmd));
  8599. if (!cmd) return;
  8600. cmd->x = (short)rect.x;
  8601. cmd->y = (short)rect.y;
  8602. cmd->w = (unsigned short)NK_MAX(0, rect.w);
  8603. cmd->h = (unsigned short)NK_MAX(0, rect.h);
  8604. cmd->left = left;
  8605. cmd->top = top;
  8606. cmd->right = right;
  8607. cmd->bottom = bottom;
  8608. }
  8609. NK_API void
  8610. nk_stroke_circle(struct nk_command_buffer* b, struct nk_rect r,
  8611. float line_thickness, struct nk_color c)
  8612. {
  8613. struct nk_command_circle* cmd;
  8614. if (!b || r.w == 0 || r.h == 0 || line_thickness <= 0) return;
  8615. if (b->use_clipping) {
  8616. const struct nk_rect* clip = &b->clip;
  8617. if (!NK_INTERSECT(r.x, r.y, r.w, r.h, clip->x, clip->y, clip->w, clip->h))
  8618. return;
  8619. }
  8620. cmd = (struct nk_command_circle*)
  8621. nk_command_buffer_push(b, NK_COMMAND_CIRCLE, sizeof(*cmd));
  8622. if (!cmd) return;
  8623. cmd->line_thickness = (unsigned short)line_thickness;
  8624. cmd->x = (short)r.x;
  8625. cmd->y = (short)r.y;
  8626. cmd->w = (unsigned short)NK_MAX(r.w, 0);
  8627. cmd->h = (unsigned short)NK_MAX(r.h, 0);
  8628. cmd->color = c;
  8629. }
  8630. NK_API void
  8631. nk_fill_circle(struct nk_command_buffer* b, struct nk_rect r, struct nk_color c)
  8632. {
  8633. struct nk_command_circle_filled* cmd;
  8634. NK_ASSERT(b);
  8635. if (!b || c.a == 0 || r.w == 0 || r.h == 0) return;
  8636. if (b->use_clipping) {
  8637. const struct nk_rect* clip = &b->clip;
  8638. if (!NK_INTERSECT(r.x, r.y, r.w, r.h, clip->x, clip->y, clip->w, clip->h))
  8639. return;
  8640. }
  8641. cmd = (struct nk_command_circle_filled*)
  8642. nk_command_buffer_push(b, NK_COMMAND_CIRCLE_FILLED, sizeof(*cmd));
  8643. if (!cmd) return;
  8644. cmd->x = (short)r.x;
  8645. cmd->y = (short)r.y;
  8646. cmd->w = (unsigned short)NK_MAX(r.w, 0);
  8647. cmd->h = (unsigned short)NK_MAX(r.h, 0);
  8648. cmd->color = c;
  8649. }
  8650. NK_API void
  8651. nk_stroke_arc(struct nk_command_buffer* b, float cx, float cy, float radius,
  8652. float a_min, float a_max, float line_thickness, struct nk_color c)
  8653. {
  8654. struct nk_command_arc* cmd;
  8655. if (!b || c.a == 0 || line_thickness <= 0) return;
  8656. cmd = (struct nk_command_arc*)
  8657. nk_command_buffer_push(b, NK_COMMAND_ARC, sizeof(*cmd));
  8658. if (!cmd) return;
  8659. cmd->line_thickness = (unsigned short)line_thickness;
  8660. cmd->cx = (short)cx;
  8661. cmd->cy = (short)cy;
  8662. cmd->r = (unsigned short)radius;
  8663. cmd->a[0] = a_min;
  8664. cmd->a[1] = a_max;
  8665. cmd->color = c;
  8666. }
  8667. NK_API void
  8668. nk_fill_arc(struct nk_command_buffer* b, float cx, float cy, float radius,
  8669. float a_min, float a_max, struct nk_color c)
  8670. {
  8671. struct nk_command_arc_filled* cmd;
  8672. NK_ASSERT(b);
  8673. if (!b || c.a == 0) return;
  8674. cmd = (struct nk_command_arc_filled*)
  8675. nk_command_buffer_push(b, NK_COMMAND_ARC_FILLED, sizeof(*cmd));
  8676. if (!cmd) return;
  8677. cmd->cx = (short)cx;
  8678. cmd->cy = (short)cy;
  8679. cmd->r = (unsigned short)radius;
  8680. cmd->a[0] = a_min;
  8681. cmd->a[1] = a_max;
  8682. cmd->color = c;
  8683. }
  8684. NK_API void
  8685. nk_stroke_triangle(struct nk_command_buffer* b, float x0, float y0, float x1,
  8686. float y1, float x2, float y2, float line_thickness, struct nk_color c)
  8687. {
  8688. struct nk_command_triangle* cmd;
  8689. NK_ASSERT(b);
  8690. if (!b || c.a == 0 || line_thickness <= 0) return;
  8691. if (b->use_clipping) {
  8692. const struct nk_rect* clip = &b->clip;
  8693. if (!NK_INBOX(x0, y0, clip->x, clip->y, clip->w, clip->h) &&
  8694. !NK_INBOX(x1, y1, clip->x, clip->y, clip->w, clip->h) &&
  8695. !NK_INBOX(x2, y2, clip->x, clip->y, clip->w, clip->h))
  8696. return;
  8697. }
  8698. cmd = (struct nk_command_triangle*)
  8699. nk_command_buffer_push(b, NK_COMMAND_TRIANGLE, sizeof(*cmd));
  8700. if (!cmd) return;
  8701. cmd->line_thickness = (unsigned short)line_thickness;
  8702. cmd->a.x = (short)x0;
  8703. cmd->a.y = (short)y0;
  8704. cmd->b.x = (short)x1;
  8705. cmd->b.y = (short)y1;
  8706. cmd->c.x = (short)x2;
  8707. cmd->c.y = (short)y2;
  8708. cmd->color = c;
  8709. }
  8710. NK_API void
  8711. nk_fill_triangle(struct nk_command_buffer* b, float x0, float y0, float x1,
  8712. float y1, float x2, float y2, struct nk_color c)
  8713. {
  8714. struct nk_command_triangle_filled* cmd;
  8715. NK_ASSERT(b);
  8716. if (!b || c.a == 0) return;
  8717. if (!b) return;
  8718. if (b->use_clipping) {
  8719. const struct nk_rect* clip = &b->clip;
  8720. if (!NK_INBOX(x0, y0, clip->x, clip->y, clip->w, clip->h) &&
  8721. !NK_INBOX(x1, y1, clip->x, clip->y, clip->w, clip->h) &&
  8722. !NK_INBOX(x2, y2, clip->x, clip->y, clip->w, clip->h))
  8723. return;
  8724. }
  8725. cmd = (struct nk_command_triangle_filled*)
  8726. nk_command_buffer_push(b, NK_COMMAND_TRIANGLE_FILLED, sizeof(*cmd));
  8727. if (!cmd) return;
  8728. cmd->a.x = (short)x0;
  8729. cmd->a.y = (short)y0;
  8730. cmd->b.x = (short)x1;
  8731. cmd->b.y = (short)y1;
  8732. cmd->c.x = (short)x2;
  8733. cmd->c.y = (short)y2;
  8734. cmd->color = c;
  8735. }
  8736. NK_API void
  8737. nk_stroke_polygon(struct nk_command_buffer* b, float* points, int point_count,
  8738. float line_thickness, struct nk_color col)
  8739. {
  8740. int i;
  8741. nk_size size = 0;
  8742. struct nk_command_polygon* cmd;
  8743. NK_ASSERT(b);
  8744. if (!b || col.a == 0 || line_thickness <= 0) return;
  8745. size = sizeof(*cmd) + sizeof(short) * 2 * (nk_size)point_count;
  8746. cmd = (struct nk_command_polygon*)nk_command_buffer_push(b, NK_COMMAND_POLYGON, size);
  8747. if (!cmd) return;
  8748. cmd->color = col;
  8749. cmd->line_thickness = (unsigned short)line_thickness;
  8750. cmd->point_count = (unsigned short)point_count;
  8751. for (i = 0; i < point_count; ++i) {
  8752. cmd->points[i].x = (short)points[i * 2];
  8753. cmd->points[i].y = (short)points[i * 2 + 1];
  8754. }
  8755. }
  8756. NK_API void
  8757. nk_fill_polygon(struct nk_command_buffer* b, float* points, int point_count,
  8758. struct nk_color col)
  8759. {
  8760. int i;
  8761. nk_size size = 0;
  8762. struct nk_command_polygon_filled* cmd;
  8763. NK_ASSERT(b);
  8764. if (!b || col.a == 0) return;
  8765. size = sizeof(*cmd) + sizeof(short) * 2 * (nk_size)point_count;
  8766. cmd = (struct nk_command_polygon_filled*)
  8767. nk_command_buffer_push(b, NK_COMMAND_POLYGON_FILLED, size);
  8768. if (!cmd) return;
  8769. cmd->color = col;
  8770. cmd->point_count = (unsigned short)point_count;
  8771. for (i = 0; i < point_count; ++i) {
  8772. cmd->points[i].x = (short)points[i * 2 + 0];
  8773. cmd->points[i].y = (short)points[i * 2 + 1];
  8774. }
  8775. }
  8776. NK_API void
  8777. nk_stroke_polyline(struct nk_command_buffer* b, float* points, int point_count,
  8778. float line_thickness, struct nk_color col)
  8779. {
  8780. int i;
  8781. nk_size size = 0;
  8782. struct nk_command_polyline* cmd;
  8783. NK_ASSERT(b);
  8784. if (!b || col.a == 0 || line_thickness <= 0) return;
  8785. size = sizeof(*cmd) + sizeof(short) * 2 * (nk_size)point_count;
  8786. cmd = (struct nk_command_polyline*)nk_command_buffer_push(b, NK_COMMAND_POLYLINE, size);
  8787. if (!cmd) return;
  8788. cmd->color = col;
  8789. cmd->point_count = (unsigned short)point_count;
  8790. cmd->line_thickness = (unsigned short)line_thickness;
  8791. for (i = 0; i < point_count; ++i) {
  8792. cmd->points[i].x = (short)points[i * 2];
  8793. cmd->points[i].y = (short)points[i * 2 + 1];
  8794. }
  8795. }
  8796. NK_API void
  8797. nk_draw_image(struct nk_command_buffer* b, struct nk_rect r,
  8798. const struct nk_image* img, struct nk_color col)
  8799. {
  8800. struct nk_command_image* cmd;
  8801. NK_ASSERT(b);
  8802. if (!b) return;
  8803. if (b->use_clipping) {
  8804. const struct nk_rect* c = &b->clip;
  8805. if (c->w == 0 || c->h == 0 || !NK_INTERSECT(r.x, r.y, r.w, r.h, c->x, c->y, c->w, c->h))
  8806. return;
  8807. }
  8808. cmd = (struct nk_command_image*)
  8809. nk_command_buffer_push(b, NK_COMMAND_IMAGE, sizeof(*cmd));
  8810. if (!cmd) return;
  8811. cmd->x = (short)r.x;
  8812. cmd->y = (short)r.y;
  8813. cmd->w = (unsigned short)NK_MAX(0, r.w);
  8814. cmd->h = (unsigned short)NK_MAX(0, r.h);
  8815. cmd->img = *img;
  8816. cmd->col = col;
  8817. }
  8818. NK_API void
  8819. nk_draw_nine_slice(struct nk_command_buffer* b, struct nk_rect r,
  8820. const struct nk_nine_slice* slc, struct nk_color col)
  8821. {
  8822. struct nk_image img;
  8823. const struct nk_image* slcimg = (const struct nk_image*)slc;
  8824. nk_ushort rgnX, rgnY, rgnW, rgnH;
  8825. rgnX = slcimg->region[0];
  8826. rgnY = slcimg->region[1];
  8827. rgnW = slcimg->region[2];
  8828. rgnH = slcimg->region[3];
  8829. /* top-left */
  8830. img.handle = slcimg->handle;
  8831. img.w = slcimg->w;
  8832. img.h = slcimg->h;
  8833. img.region[0] = rgnX;
  8834. img.region[1] = rgnY;
  8835. img.region[2] = slc->l;
  8836. img.region[3] = slc->t;
  8837. nk_draw_image(b,
  8838. nk_rect(r.x, r.y, (float)slc->l, (float)slc->t),
  8839. &img, col);
  8840. #define IMG_RGN(x, y, w, h) img.region[0] = (nk_ushort)(x); img.region[1] = (nk_ushort)(y); img.region[2] = (nk_ushort)(w); img.region[3] = (nk_ushort)(h);
  8841. /* top-center */
  8842. IMG_RGN(rgnX + slc->l, rgnY, rgnW - slc->l - slc->r, slc->t);
  8843. nk_draw_image(b,
  8844. nk_rect(r.x + (float)slc->l, r.y, (float)(r.w - slc->l - slc->r), (float)slc->t),
  8845. &img, col);
  8846. /* top-right */
  8847. IMG_RGN(rgnX + rgnW - slc->r, rgnY, slc->r, slc->t);
  8848. nk_draw_image(b,
  8849. nk_rect(r.x + r.w - (float)slc->r, r.y, (float)slc->r, (float)slc->t),
  8850. &img, col);
  8851. /* center-left */
  8852. IMG_RGN(rgnX, rgnY + slc->t, slc->l, rgnH - slc->t - slc->b);
  8853. nk_draw_image(b,
  8854. nk_rect(r.x, r.y + (float)slc->t, (float)slc->l, (float)(r.h - slc->t - slc->b)),
  8855. &img, col);
  8856. /* center */
  8857. IMG_RGN(rgnX + slc->l, rgnY + slc->t, rgnW - slc->l - slc->r, rgnH - slc->t - slc->b);
  8858. nk_draw_image(b,
  8859. nk_rect(r.x + (float)slc->l, r.y + (float)slc->t, (float)(r.w - slc->l - slc->r), (float)(r.h - slc->t - slc->b)),
  8860. &img, col);
  8861. /* center-right */
  8862. IMG_RGN(rgnX + rgnW - slc->r, rgnY + slc->t, slc->r, rgnH - slc->t - slc->b);
  8863. nk_draw_image(b,
  8864. nk_rect(r.x + r.w - (float)slc->r, r.y + (float)slc->t, (float)slc->r, (float)(r.h - slc->t - slc->b)),
  8865. &img, col);
  8866. /* bottom-left */
  8867. IMG_RGN(rgnX, rgnY + rgnH - slc->b, slc->l, slc->b);
  8868. nk_draw_image(b,
  8869. nk_rect(r.x, r.y + r.h - (float)slc->b, (float)slc->l, (float)slc->b),
  8870. &img, col);
  8871. /* bottom-center */
  8872. IMG_RGN(rgnX + slc->l, rgnY + rgnH - slc->b, rgnW - slc->l - slc->r, slc->b);
  8873. nk_draw_image(b,
  8874. nk_rect(r.x + (float)slc->l, r.y + r.h - (float)slc->b, (float)(r.w - slc->l - slc->r), (float)slc->b),
  8875. &img, col);
  8876. /* bottom-right */
  8877. IMG_RGN(rgnX + rgnW - slc->r, rgnY + rgnH - slc->b, slc->r, slc->b);
  8878. nk_draw_image(b,
  8879. nk_rect(r.x + r.w - (float)slc->r, r.y + r.h - (float)slc->b, (float)slc->r, (float)slc->b),
  8880. &img, col);
  8881. #undef IMG_RGN
  8882. }
  8883. NK_API void
  8884. nk_push_custom(struct nk_command_buffer* b, struct nk_rect r,
  8885. nk_command_custom_callback cb, nk_handle usr)
  8886. {
  8887. struct nk_command_custom* cmd;
  8888. NK_ASSERT(b);
  8889. if (!b) return;
  8890. if (b->use_clipping) {
  8891. const struct nk_rect* c = &b->clip;
  8892. if (c->w == 0 || c->h == 0 || !NK_INTERSECT(r.x, r.y, r.w, r.h, c->x, c->y, c->w, c->h))
  8893. return;
  8894. }
  8895. cmd = (struct nk_command_custom*)
  8896. nk_command_buffer_push(b, NK_COMMAND_CUSTOM, sizeof(*cmd));
  8897. if (!cmd) return;
  8898. cmd->x = (short)r.x;
  8899. cmd->y = (short)r.y;
  8900. cmd->w = (unsigned short)NK_MAX(0, r.w);
  8901. cmd->h = (unsigned short)NK_MAX(0, r.h);
  8902. cmd->callback_data = usr;
  8903. cmd->callback = cb;
  8904. }
  8905. NK_API void
  8906. nk_draw_text(struct nk_command_buffer* b, struct nk_rect r,
  8907. const char* string, int length, const struct nk_user_font* font,
  8908. struct nk_color bg, struct nk_color fg)
  8909. {
  8910. float text_width = 0;
  8911. struct nk_command_text* cmd;
  8912. NK_ASSERT(b);
  8913. NK_ASSERT(font);
  8914. if (!b || !string || !length || (bg.a == 0 && fg.a == 0)) return;
  8915. if (b->use_clipping) {
  8916. const struct nk_rect* c = &b->clip;
  8917. if (c->w == 0 || c->h == 0 || !NK_INTERSECT(r.x, r.y, r.w, r.h, c->x, c->y, c->w, c->h))
  8918. return;
  8919. }
  8920. /* make sure text fits inside bounds */
  8921. text_width = font->width(font->userdata, font->height, string, length);
  8922. if (text_width > r.w) {
  8923. int glyphs = 0;
  8924. float txt_width = (float)text_width;
  8925. length = nk_text_clamp(font, string, length, r.w, &glyphs, &txt_width, 0, 0);
  8926. }
  8927. if (!length) return;
  8928. cmd = (struct nk_command_text*)
  8929. nk_command_buffer_push(b, NK_COMMAND_TEXT, sizeof(*cmd) + (nk_size)(length + 1));
  8930. if (!cmd) return;
  8931. cmd->x = (short)r.x;
  8932. cmd->y = (short)r.y;
  8933. cmd->w = (unsigned short)r.w;
  8934. cmd->h = (unsigned short)r.h;
  8935. cmd->background = bg;
  8936. cmd->foreground = fg;
  8937. cmd->font = font;
  8938. cmd->length = length;
  8939. cmd->height = font->height;
  8940. NK_MEMCPY(cmd->string, string, (nk_size)length);
  8941. cmd->string[length] = '\0';
  8942. }
  8943. /* ===============================================================
  8944. *
  8945. * VERTEX
  8946. *
  8947. * ===============================================================*/
  8948. #ifdef NK_INCLUDE_VERTEX_BUFFER_OUTPUT
  8949. NK_API void
  8950. nk_draw_list_init(struct nk_draw_list* list)
  8951. {
  8952. nk_size i = 0;
  8953. NK_ASSERT(list);
  8954. if (!list) return;
  8955. nk_zero(list, sizeof(*list));
  8956. for (i = 0; i < NK_LEN(list->circle_vtx); ++i) {
  8957. const float a = ((float)i / (float)NK_LEN(list->circle_vtx)) * 2 * NK_PI;
  8958. list->circle_vtx[i].x = (float)NK_COS(a);
  8959. list->circle_vtx[i].y = (float)NK_SIN(a);
  8960. }
  8961. }
  8962. NK_API void
  8963. nk_draw_list_setup(struct nk_draw_list* canvas, const struct nk_convert_config* config,
  8964. struct nk_buffer* cmds, struct nk_buffer* vertices, struct nk_buffer* elements,
  8965. enum nk_anti_aliasing line_aa, enum nk_anti_aliasing shape_aa)
  8966. {
  8967. NK_ASSERT(canvas);
  8968. NK_ASSERT(config);
  8969. NK_ASSERT(cmds);
  8970. NK_ASSERT(vertices);
  8971. NK_ASSERT(elements);
  8972. if (!canvas || !config || !cmds || !vertices || !elements)
  8973. return;
  8974. canvas->buffer = cmds;
  8975. canvas->config = *config;
  8976. canvas->elements = elements;
  8977. canvas->vertices = vertices;
  8978. canvas->line_AA = line_aa;
  8979. canvas->shape_AA = shape_aa;
  8980. canvas->clip_rect = nk_null_rect;
  8981. canvas->cmd_offset = 0;
  8982. canvas->element_count = 0;
  8983. canvas->vertex_count = 0;
  8984. canvas->cmd_offset = 0;
  8985. canvas->cmd_count = 0;
  8986. canvas->path_count = 0;
  8987. }
  8988. NK_API const struct nk_draw_command*
  8989. nk__draw_list_begin(const struct nk_draw_list* canvas, const struct nk_buffer* buffer)
  8990. {
  8991. nk_byte* memory;
  8992. nk_size offset;
  8993. const struct nk_draw_command* cmd;
  8994. NK_ASSERT(buffer);
  8995. if (!buffer || !buffer->size || !canvas->cmd_count)
  8996. return 0;
  8997. memory = (nk_byte*)buffer->memory.ptr;
  8998. offset = buffer->memory.size - canvas->cmd_offset;
  8999. cmd = nk_ptr_add(const struct nk_draw_command, memory, offset);
  9000. return cmd;
  9001. }
  9002. NK_API const struct nk_draw_command*
  9003. nk__draw_list_end(const struct nk_draw_list* canvas, const struct nk_buffer* buffer)
  9004. {
  9005. nk_size size;
  9006. nk_size offset;
  9007. nk_byte* memory;
  9008. const struct nk_draw_command* end;
  9009. NK_ASSERT(buffer);
  9010. NK_ASSERT(canvas);
  9011. if (!buffer || !canvas)
  9012. return 0;
  9013. memory = (nk_byte*)buffer->memory.ptr;
  9014. size = buffer->memory.size;
  9015. offset = size - canvas->cmd_offset;
  9016. end = nk_ptr_add(const struct nk_draw_command, memory, offset);
  9017. end -= (canvas->cmd_count - 1);
  9018. return end;
  9019. }
  9020. NK_API const struct nk_draw_command*
  9021. nk__draw_list_next(const struct nk_draw_command* cmd,
  9022. const struct nk_buffer* buffer, const struct nk_draw_list* canvas)
  9023. {
  9024. const struct nk_draw_command* end;
  9025. NK_ASSERT(buffer);
  9026. NK_ASSERT(canvas);
  9027. if (!cmd || !buffer || !canvas)
  9028. return 0;
  9029. end = nk__draw_list_end(canvas, buffer);
  9030. if (cmd <= end) return 0;
  9031. return (cmd - 1);
  9032. }
  9033. NK_INTERN struct nk_vec2*
  9034. nk_draw_list_alloc_path(struct nk_draw_list* list, int count)
  9035. {
  9036. struct nk_vec2* points;
  9037. NK_STORAGE const nk_size point_align = NK_ALIGNOF(struct nk_vec2);
  9038. NK_STORAGE const nk_size point_size = sizeof(struct nk_vec2);
  9039. points = (struct nk_vec2*)
  9040. nk_buffer_alloc(list->buffer, NK_BUFFER_FRONT,
  9041. point_size * (nk_size)count, point_align);
  9042. if (!points) return 0;
  9043. if (!list->path_offset) {
  9044. void* memory = nk_buffer_memory(list->buffer);
  9045. list->path_offset = (unsigned int)((nk_byte*)points - (nk_byte*)memory);
  9046. }
  9047. list->path_count += (unsigned int)count;
  9048. return points;
  9049. }
  9050. NK_INTERN struct nk_vec2
  9051. nk_draw_list_path_last(struct nk_draw_list* list)
  9052. {
  9053. void* memory;
  9054. struct nk_vec2* point;
  9055. NK_ASSERT(list->path_count);
  9056. memory = nk_buffer_memory(list->buffer);
  9057. point = nk_ptr_add(struct nk_vec2, memory, list->path_offset);
  9058. point += (list->path_count - 1);
  9059. return *point;
  9060. }
  9061. NK_INTERN struct nk_draw_command*
  9062. nk_draw_list_push_command(struct nk_draw_list* list, struct nk_rect clip,
  9063. nk_handle texture)
  9064. {
  9065. NK_STORAGE const nk_size cmd_align = NK_ALIGNOF(struct nk_draw_command);
  9066. NK_STORAGE const nk_size cmd_size = sizeof(struct nk_draw_command);
  9067. struct nk_draw_command* cmd;
  9068. NK_ASSERT(list);
  9069. cmd = (struct nk_draw_command*)
  9070. nk_buffer_alloc(list->buffer, NK_BUFFER_BACK, cmd_size, cmd_align);
  9071. if (!cmd) return 0;
  9072. if (!list->cmd_count) {
  9073. nk_byte* memory = (nk_byte*)nk_buffer_memory(list->buffer);
  9074. nk_size total = nk_buffer_total(list->buffer);
  9075. memory = nk_ptr_add(nk_byte, memory, total);
  9076. list->cmd_offset = (nk_size)(memory - (nk_byte*)cmd);
  9077. }
  9078. cmd->elem_count = 0;
  9079. cmd->clip_rect = clip;
  9080. cmd->texture = texture;
  9081. #ifdef NK_INCLUDE_COMMAND_USERDATA
  9082. cmd->userdata = list->userdata;
  9083. #endif
  9084. list->cmd_count++;
  9085. list->clip_rect = clip;
  9086. return cmd;
  9087. }
  9088. NK_INTERN struct nk_draw_command*
  9089. nk_draw_list_command_last(struct nk_draw_list* list)
  9090. {
  9091. void* memory;
  9092. nk_size size;
  9093. struct nk_draw_command* cmd;
  9094. NK_ASSERT(list->cmd_count);
  9095. memory = nk_buffer_memory(list->buffer);
  9096. size = nk_buffer_total(list->buffer);
  9097. cmd = nk_ptr_add(struct nk_draw_command, memory, size - list->cmd_offset);
  9098. return (cmd - (list->cmd_count - 1));
  9099. }
  9100. NK_INTERN void
  9101. nk_draw_list_add_clip(struct nk_draw_list* list, struct nk_rect rect)
  9102. {
  9103. NK_ASSERT(list);
  9104. if (!list) return;
  9105. if (!list->cmd_count) {
  9106. nk_draw_list_push_command(list, rect, list->config.tex_null.texture);
  9107. }
  9108. else {
  9109. struct nk_draw_command* prev = nk_draw_list_command_last(list);
  9110. if (prev->elem_count == 0)
  9111. prev->clip_rect = rect;
  9112. nk_draw_list_push_command(list, rect, prev->texture);
  9113. }
  9114. }
  9115. NK_INTERN void
  9116. nk_draw_list_push_image(struct nk_draw_list* list, nk_handle texture)
  9117. {
  9118. NK_ASSERT(list);
  9119. if (!list) return;
  9120. if (!list->cmd_count) {
  9121. nk_draw_list_push_command(list, nk_null_rect, texture);
  9122. }
  9123. else {
  9124. struct nk_draw_command* prev = nk_draw_list_command_last(list);
  9125. if (prev->elem_count == 0) {
  9126. prev->texture = texture;
  9127. #ifdef NK_INCLUDE_COMMAND_USERDATA
  9128. prev->userdata = list->userdata;
  9129. #endif
  9130. }
  9131. else if (prev->texture.id != texture.id
  9132. #ifdef NK_INCLUDE_COMMAND_USERDATA
  9133. || prev->userdata.id != list->userdata.id
  9134. #endif
  9135. ) nk_draw_list_push_command(list, prev->clip_rect, texture);
  9136. }
  9137. }
  9138. #ifdef NK_INCLUDE_COMMAND_USERDATA
  9139. NK_API void
  9140. nk_draw_list_push_userdata(struct nk_draw_list* list, nk_handle userdata)
  9141. {
  9142. list->userdata = userdata;
  9143. }
  9144. #endif
  9145. NK_INTERN void*
  9146. nk_draw_list_alloc_vertices(struct nk_draw_list* list, nk_size count)
  9147. {
  9148. void* vtx;
  9149. NK_ASSERT(list);
  9150. if (!list) return 0;
  9151. vtx = nk_buffer_alloc(list->vertices, NK_BUFFER_FRONT,
  9152. list->config.vertex_size * count, list->config.vertex_alignment);
  9153. if (!vtx) return 0;
  9154. list->vertex_count += (unsigned int)count;
  9155. /* This assert triggers because your are drawing a lot of stuff and nuklear
  9156. * defined `nk_draw_index` as `nk_ushort` to safe space be default.
  9157. *
  9158. * So you reached the maximum number of indices or rather vertexes.
  9159. * To solve this issue please change typedef `nk_draw_index` to `nk_uint`
  9160. * and don't forget to specify the new element size in your drawing
  9161. * backend (OpenGL, DirectX, ...). For example in OpenGL for `glDrawElements`
  9162. * instead of specifying `GL_UNSIGNED_SHORT` you have to define `GL_UNSIGNED_INT`.
  9163. * Sorry for the inconvenience. */
  9164. if (sizeof(nk_draw_index) == 2) NK_ASSERT((list->vertex_count < NK_USHORT_MAX &&
  9165. "To many vertices for 16-bit vertex indices. Please read comment above on how to solve this problem"));
  9166. return vtx;
  9167. }
  9168. NK_INTERN nk_draw_index*
  9169. nk_draw_list_alloc_elements(struct nk_draw_list* list, nk_size count)
  9170. {
  9171. nk_draw_index* ids;
  9172. struct nk_draw_command* cmd;
  9173. NK_STORAGE const nk_size elem_align = NK_ALIGNOF(nk_draw_index);
  9174. NK_STORAGE const nk_size elem_size = sizeof(nk_draw_index);
  9175. NK_ASSERT(list);
  9176. if (!list) return 0;
  9177. ids = (nk_draw_index*)
  9178. nk_buffer_alloc(list->elements, NK_BUFFER_FRONT, elem_size * count, elem_align);
  9179. if (!ids) return 0;
  9180. cmd = nk_draw_list_command_last(list);
  9181. list->element_count += (unsigned int)count;
  9182. cmd->elem_count += (unsigned int)count;
  9183. return ids;
  9184. }
  9185. NK_INTERN int
  9186. nk_draw_vertex_layout_element_is_end_of_layout(
  9187. const struct nk_draw_vertex_layout_element* element)
  9188. {
  9189. return (element->attribute == NK_VERTEX_ATTRIBUTE_COUNT ||
  9190. element->format == NK_FORMAT_COUNT);
  9191. }
  9192. NK_INTERN void
  9193. nk_draw_vertex_color(void* attr, const float* vals,
  9194. enum nk_draw_vertex_layout_format format)
  9195. {
  9196. /* if this triggers you tried to provide a value format for a color */
  9197. float val[4];
  9198. NK_ASSERT(format >= NK_FORMAT_COLOR_BEGIN);
  9199. NK_ASSERT(format <= NK_FORMAT_COLOR_END);
  9200. if (format < NK_FORMAT_COLOR_BEGIN || format > NK_FORMAT_COLOR_END) return;
  9201. val[0] = NK_SATURATE(vals[0]);
  9202. val[1] = NK_SATURATE(vals[1]);
  9203. val[2] = NK_SATURATE(vals[2]);
  9204. val[3] = NK_SATURATE(vals[3]);
  9205. switch (format) {
  9206. default: NK_ASSERT(0 && "Invalid vertex layout color format"); break;
  9207. case NK_FORMAT_R8G8B8A8:
  9208. case NK_FORMAT_R8G8B8: {
  9209. struct nk_color col = nk_rgba_fv(val);
  9210. NK_MEMCPY(attr, &col.r, sizeof(col));
  9211. } break;
  9212. case NK_FORMAT_B8G8R8A8: {
  9213. struct nk_color col = nk_rgba_fv(val);
  9214. struct nk_color bgra = nk_rgba(col.b, col.g, col.r, col.a);
  9215. NK_MEMCPY(attr, &bgra, sizeof(bgra));
  9216. } break;
  9217. case NK_FORMAT_R16G15B16: {
  9218. nk_ushort col[3];
  9219. col[0] = (nk_ushort)(val[0] * (float)NK_USHORT_MAX);
  9220. col[1] = (nk_ushort)(val[1] * (float)NK_USHORT_MAX);
  9221. col[2] = (nk_ushort)(val[2] * (float)NK_USHORT_MAX);
  9222. NK_MEMCPY(attr, col, sizeof(col));
  9223. } break;
  9224. case NK_FORMAT_R16G15B16A16: {
  9225. nk_ushort col[4];
  9226. col[0] = (nk_ushort)(val[0] * (float)NK_USHORT_MAX);
  9227. col[1] = (nk_ushort)(val[1] * (float)NK_USHORT_MAX);
  9228. col[2] = (nk_ushort)(val[2] * (float)NK_USHORT_MAX);
  9229. col[3] = (nk_ushort)(val[3] * (float)NK_USHORT_MAX);
  9230. NK_MEMCPY(attr, col, sizeof(col));
  9231. } break;
  9232. case NK_FORMAT_R32G32B32: {
  9233. nk_uint col[3];
  9234. col[0] = (nk_uint)(val[0] * (float)NK_UINT_MAX);
  9235. col[1] = (nk_uint)(val[1] * (float)NK_UINT_MAX);
  9236. col[2] = (nk_uint)(val[2] * (float)NK_UINT_MAX);
  9237. NK_MEMCPY(attr, col, sizeof(col));
  9238. } break;
  9239. case NK_FORMAT_R32G32B32A32: {
  9240. nk_uint col[4];
  9241. col[0] = (nk_uint)(val[0] * (float)NK_UINT_MAX);
  9242. col[1] = (nk_uint)(val[1] * (float)NK_UINT_MAX);
  9243. col[2] = (nk_uint)(val[2] * (float)NK_UINT_MAX);
  9244. col[3] = (nk_uint)(val[3] * (float)NK_UINT_MAX);
  9245. NK_MEMCPY(attr, col, sizeof(col));
  9246. } break;
  9247. case NK_FORMAT_R32G32B32A32_FLOAT:
  9248. NK_MEMCPY(attr, val, sizeof(float) * 4);
  9249. break;
  9250. case NK_FORMAT_R32G32B32A32_DOUBLE: {
  9251. double col[4];
  9252. col[0] = (double)val[0];
  9253. col[1] = (double)val[1];
  9254. col[2] = (double)val[2];
  9255. col[3] = (double)val[3];
  9256. NK_MEMCPY(attr, col, sizeof(col));
  9257. } break;
  9258. case NK_FORMAT_RGB32:
  9259. case NK_FORMAT_RGBA32: {
  9260. struct nk_color col = nk_rgba_fv(val);
  9261. nk_uint color = nk_color_u32(col);
  9262. NK_MEMCPY(attr, &color, sizeof(color));
  9263. } break;
  9264. }
  9265. }
  9266. NK_INTERN void
  9267. nk_draw_vertex_element(void* dst, const float* values, int value_count,
  9268. enum nk_draw_vertex_layout_format format)
  9269. {
  9270. int value_index;
  9271. void* attribute = dst;
  9272. /* if this triggers you tried to provide a color format for a value */
  9273. NK_ASSERT(format < NK_FORMAT_COLOR_BEGIN);
  9274. if (format >= NK_FORMAT_COLOR_BEGIN && format <= NK_FORMAT_COLOR_END) return;
  9275. for (value_index = 0; value_index < value_count; ++value_index) {
  9276. switch (format) {
  9277. default: NK_ASSERT(0 && "invalid vertex layout format"); break;
  9278. case NK_FORMAT_SCHAR: {
  9279. char value = (char)NK_CLAMP((float)NK_SCHAR_MIN, values[value_index], (float)NK_SCHAR_MAX);
  9280. NK_MEMCPY(attribute, &value, sizeof(value));
  9281. attribute = (void*)((char*)attribute + sizeof(char));
  9282. } break;
  9283. case NK_FORMAT_SSHORT: {
  9284. nk_short value = (nk_short)NK_CLAMP((float)NK_SSHORT_MIN, values[value_index], (float)NK_SSHORT_MAX);
  9285. NK_MEMCPY(attribute, &value, sizeof(value));
  9286. attribute = (void*)((char*)attribute + sizeof(value));
  9287. } break;
  9288. case NK_FORMAT_SINT: {
  9289. nk_int value = (nk_int)NK_CLAMP((float)NK_SINT_MIN, values[value_index], (float)NK_SINT_MAX);
  9290. NK_MEMCPY(attribute, &value, sizeof(value));
  9291. attribute = (void*)((char*)attribute + sizeof(nk_int));
  9292. } break;
  9293. case NK_FORMAT_UCHAR: {
  9294. unsigned char value = (unsigned char)NK_CLAMP((float)NK_UCHAR_MIN, values[value_index], (float)NK_UCHAR_MAX);
  9295. NK_MEMCPY(attribute, &value, sizeof(value));
  9296. attribute = (void*)((char*)attribute + sizeof(unsigned char));
  9297. } break;
  9298. case NK_FORMAT_USHORT: {
  9299. nk_ushort value = (nk_ushort)NK_CLAMP((float)NK_USHORT_MIN, values[value_index], (float)NK_USHORT_MAX);
  9300. NK_MEMCPY(attribute, &value, sizeof(value));
  9301. attribute = (void*)((char*)attribute + sizeof(value));
  9302. } break;
  9303. case NK_FORMAT_UINT: {
  9304. nk_uint value = (nk_uint)NK_CLAMP((float)NK_UINT_MIN, values[value_index], (float)NK_UINT_MAX);
  9305. NK_MEMCPY(attribute, &value, sizeof(value));
  9306. attribute = (void*)((char*)attribute + sizeof(nk_uint));
  9307. } break;
  9308. case NK_FORMAT_FLOAT:
  9309. NK_MEMCPY(attribute, &values[value_index], sizeof(values[value_index]));
  9310. attribute = (void*)((char*)attribute + sizeof(float));
  9311. break;
  9312. case NK_FORMAT_DOUBLE: {
  9313. double value = (double)values[value_index];
  9314. NK_MEMCPY(attribute, &value, sizeof(value));
  9315. attribute = (void*)((char*)attribute + sizeof(double));
  9316. } break;
  9317. }
  9318. }
  9319. }
  9320. NK_INTERN void*
  9321. nk_draw_vertex(void* dst, const struct nk_convert_config* config,
  9322. struct nk_vec2 pos, struct nk_vec2 uv, struct nk_colorf color)
  9323. {
  9324. void* result = (void*)((char*)dst + config->vertex_size);
  9325. const struct nk_draw_vertex_layout_element* elem_iter = config->vertex_layout;
  9326. while (!nk_draw_vertex_layout_element_is_end_of_layout(elem_iter)) {
  9327. void* address = (void*)((char*)dst + elem_iter->offset);
  9328. switch (elem_iter->attribute) {
  9329. case NK_VERTEX_ATTRIBUTE_COUNT:
  9330. default: NK_ASSERT(0 && "wrong element attribute"); break;
  9331. case NK_VERTEX_POSITION: nk_draw_vertex_element(address, &pos.x, 2, elem_iter->format); break;
  9332. case NK_VERTEX_TEXCOORD: nk_draw_vertex_element(address, &uv.x, 2, elem_iter->format); break;
  9333. case NK_VERTEX_COLOR: nk_draw_vertex_color(address, &color.r, elem_iter->format); break;
  9334. }
  9335. elem_iter++;
  9336. }
  9337. return result;
  9338. }
  9339. NK_API void
  9340. nk_draw_list_stroke_poly_line(struct nk_draw_list* list, const struct nk_vec2* points,
  9341. const unsigned int points_count, struct nk_color color, enum nk_draw_list_stroke closed,
  9342. float thickness, enum nk_anti_aliasing aliasing)
  9343. {
  9344. nk_size count;
  9345. int thick_line;
  9346. struct nk_colorf col;
  9347. struct nk_colorf col_trans;
  9348. NK_ASSERT(list);
  9349. if (!list || points_count < 2) return;
  9350. color.a = (nk_byte)((float)color.a * list->config.global_alpha);
  9351. count = points_count;
  9352. if (!closed) count = points_count - 1;
  9353. thick_line = thickness > 1.0f;
  9354. #ifdef NK_INCLUDE_COMMAND_USERDATA
  9355. nk_draw_list_push_userdata(list, list->userdata);
  9356. #endif
  9357. color.a = (nk_byte)((float)color.a * list->config.global_alpha);
  9358. nk_color_fv(&col.r, color);
  9359. col_trans = col;
  9360. col_trans.a = 0;
  9361. if (aliasing == NK_ANTI_ALIASING_ON) {
  9362. /* ANTI-ALIASED STROKE */
  9363. const float AA_SIZE = 1.0f;
  9364. NK_STORAGE const nk_size pnt_align = NK_ALIGNOF(struct nk_vec2);
  9365. NK_STORAGE const nk_size pnt_size = sizeof(struct nk_vec2);
  9366. /* allocate vertices and elements */
  9367. nk_size i1 = 0;
  9368. nk_size vertex_offset;
  9369. nk_size index = list->vertex_count;
  9370. const nk_size idx_count = (thick_line) ? (count * 18) : (count * 12);
  9371. const nk_size vtx_count = (thick_line) ? (points_count * 4) : (points_count * 3);
  9372. void* vtx = nk_draw_list_alloc_vertices(list, vtx_count);
  9373. nk_draw_index* ids = nk_draw_list_alloc_elements(list, idx_count);
  9374. nk_size size;
  9375. struct nk_vec2* normals, * temp;
  9376. if (!vtx || !ids) return;
  9377. /* temporary allocate normals + points */
  9378. vertex_offset = (nk_size)((nk_byte*)vtx - (nk_byte*)list->vertices->memory.ptr);
  9379. nk_buffer_mark(list->vertices, NK_BUFFER_FRONT);
  9380. size = pnt_size * ((thick_line) ? 5 : 3) * points_count;
  9381. normals = (struct nk_vec2*)nk_buffer_alloc(list->vertices, NK_BUFFER_FRONT, size, pnt_align);
  9382. if (!normals) return;
  9383. temp = normals + points_count;
  9384. /* make sure vertex pointer is still correct */
  9385. vtx = (void*)((nk_byte*)list->vertices->memory.ptr + vertex_offset);
  9386. /* calculate normals */
  9387. for (i1 = 0; i1 < count; ++i1) {
  9388. const nk_size i2 = ((i1 + 1) == points_count) ? 0 : (i1 + 1);
  9389. struct nk_vec2 diff = nk_vec2_sub(points[i2], points[i1]);
  9390. float len;
  9391. /* vec2 inverted length */
  9392. len = nk_vec2_len_sqr(diff);
  9393. if (len != 0.0f)
  9394. len = NK_INV_SQRT(len);
  9395. else len = 1.0f;
  9396. diff = nk_vec2_muls(diff, len);
  9397. normals[i1].x = diff.y;
  9398. normals[i1].y = -diff.x;
  9399. }
  9400. if (!closed)
  9401. normals[points_count - 1] = normals[points_count - 2];
  9402. if (!thick_line) {
  9403. nk_size idx1, i;
  9404. if (!closed) {
  9405. struct nk_vec2 d;
  9406. temp[0] = nk_vec2_add(points[0], nk_vec2_muls(normals[0], AA_SIZE));
  9407. temp[1] = nk_vec2_sub(points[0], nk_vec2_muls(normals[0], AA_SIZE));
  9408. d = nk_vec2_muls(normals[points_count - 1], AA_SIZE);
  9409. temp[(points_count - 1) * 2 + 0] = nk_vec2_add(points[points_count - 1], d);
  9410. temp[(points_count - 1) * 2 + 1] = nk_vec2_sub(points[points_count - 1], d);
  9411. }
  9412. /* fill elements */
  9413. idx1 = index;
  9414. for (i1 = 0; i1 < count; i1++) {
  9415. struct nk_vec2 dm;
  9416. float dmr2;
  9417. nk_size i2 = ((i1 + 1) == points_count) ? 0 : (i1 + 1);
  9418. nk_size idx2 = ((i1 + 1) == points_count) ? index : (idx1 + 3);
  9419. /* average normals */
  9420. dm = nk_vec2_muls(nk_vec2_add(normals[i1], normals[i2]), 0.5f);
  9421. dmr2 = dm.x * dm.x + dm.y * dm.y;
  9422. if (dmr2 > 0.000001f) {
  9423. float scale = 1.0f / dmr2;
  9424. scale = NK_MIN(100.0f, scale);
  9425. dm = nk_vec2_muls(dm, scale);
  9426. }
  9427. dm = nk_vec2_muls(dm, AA_SIZE);
  9428. temp[i2 * 2 + 0] = nk_vec2_add(points[i2], dm);
  9429. temp[i2 * 2 + 1] = nk_vec2_sub(points[i2], dm);
  9430. ids[0] = (nk_draw_index)(idx2 + 0); ids[1] = (nk_draw_index)(idx1 + 0);
  9431. ids[2] = (nk_draw_index)(idx1 + 2); ids[3] = (nk_draw_index)(idx1 + 2);
  9432. ids[4] = (nk_draw_index)(idx2 + 2); ids[5] = (nk_draw_index)(idx2 + 0);
  9433. ids[6] = (nk_draw_index)(idx2 + 1); ids[7] = (nk_draw_index)(idx1 + 1);
  9434. ids[8] = (nk_draw_index)(idx1 + 0); ids[9] = (nk_draw_index)(idx1 + 0);
  9435. ids[10] = (nk_draw_index)(idx2 + 0); ids[11] = (nk_draw_index)(idx2 + 1);
  9436. ids += 12;
  9437. idx1 = idx2;
  9438. }
  9439. /* fill vertices */
  9440. for (i = 0; i < points_count; ++i) {
  9441. const struct nk_vec2 uv = list->config.tex_null.uv;
  9442. vtx = nk_draw_vertex(vtx, &list->config, points[i], uv, col);
  9443. vtx = nk_draw_vertex(vtx, &list->config, temp[i * 2 + 0], uv, col_trans);
  9444. vtx = nk_draw_vertex(vtx, &list->config, temp[i * 2 + 1], uv, col_trans);
  9445. }
  9446. }
  9447. else {
  9448. nk_size idx1, i;
  9449. const float half_inner_thickness = (thickness - AA_SIZE) * 0.5f;
  9450. if (!closed) {
  9451. struct nk_vec2 d1 = nk_vec2_muls(normals[0], half_inner_thickness + AA_SIZE);
  9452. struct nk_vec2 d2 = nk_vec2_muls(normals[0], half_inner_thickness);
  9453. temp[0] = nk_vec2_add(points[0], d1);
  9454. temp[1] = nk_vec2_add(points[0], d2);
  9455. temp[2] = nk_vec2_sub(points[0], d2);
  9456. temp[3] = nk_vec2_sub(points[0], d1);
  9457. d1 = nk_vec2_muls(normals[points_count - 1], half_inner_thickness + AA_SIZE);
  9458. d2 = nk_vec2_muls(normals[points_count - 1], half_inner_thickness);
  9459. temp[(points_count - 1) * 4 + 0] = nk_vec2_add(points[points_count - 1], d1);
  9460. temp[(points_count - 1) * 4 + 1] = nk_vec2_add(points[points_count - 1], d2);
  9461. temp[(points_count - 1) * 4 + 2] = nk_vec2_sub(points[points_count - 1], d2);
  9462. temp[(points_count - 1) * 4 + 3] = nk_vec2_sub(points[points_count - 1], d1);
  9463. }
  9464. /* add all elements */
  9465. idx1 = index;
  9466. for (i1 = 0; i1 < count; ++i1) {
  9467. struct nk_vec2 dm_out, dm_in;
  9468. const nk_size i2 = ((i1 + 1) == points_count) ? 0 : (i1 + 1);
  9469. nk_size idx2 = ((i1 + 1) == points_count) ? index : (idx1 + 4);
  9470. /* average normals */
  9471. struct nk_vec2 dm = nk_vec2_muls(nk_vec2_add(normals[i1], normals[i2]), 0.5f);
  9472. float dmr2 = dm.x * dm.x + dm.y * dm.y;
  9473. if (dmr2 > 0.000001f) {
  9474. float scale = 1.0f / dmr2;
  9475. scale = NK_MIN(100.0f, scale);
  9476. dm = nk_vec2_muls(dm, scale);
  9477. }
  9478. dm_out = nk_vec2_muls(dm, ((half_inner_thickness)+AA_SIZE));
  9479. dm_in = nk_vec2_muls(dm, half_inner_thickness);
  9480. temp[i2 * 4 + 0] = nk_vec2_add(points[i2], dm_out);
  9481. temp[i2 * 4 + 1] = nk_vec2_add(points[i2], dm_in);
  9482. temp[i2 * 4 + 2] = nk_vec2_sub(points[i2], dm_in);
  9483. temp[i2 * 4 + 3] = nk_vec2_sub(points[i2], dm_out);
  9484. /* add indexes */
  9485. ids[0] = (nk_draw_index)(idx2 + 1); ids[1] = (nk_draw_index)(idx1 + 1);
  9486. ids[2] = (nk_draw_index)(idx1 + 2); ids[3] = (nk_draw_index)(idx1 + 2);
  9487. ids[4] = (nk_draw_index)(idx2 + 2); ids[5] = (nk_draw_index)(idx2 + 1);
  9488. ids[6] = (nk_draw_index)(idx2 + 1); ids[7] = (nk_draw_index)(idx1 + 1);
  9489. ids[8] = (nk_draw_index)(idx1 + 0); ids[9] = (nk_draw_index)(idx1 + 0);
  9490. ids[10] = (nk_draw_index)(idx2 + 0); ids[11] = (nk_draw_index)(idx2 + 1);
  9491. ids[12] = (nk_draw_index)(idx2 + 2); ids[13] = (nk_draw_index)(idx1 + 2);
  9492. ids[14] = (nk_draw_index)(idx1 + 3); ids[15] = (nk_draw_index)(idx1 + 3);
  9493. ids[16] = (nk_draw_index)(idx2 + 3); ids[17] = (nk_draw_index)(idx2 + 2);
  9494. ids += 18;
  9495. idx1 = idx2;
  9496. }
  9497. /* add vertices */
  9498. for (i = 0; i < points_count; ++i) {
  9499. const struct nk_vec2 uv = list->config.tex_null.uv;
  9500. vtx = nk_draw_vertex(vtx, &list->config, temp[i * 4 + 0], uv, col_trans);
  9501. vtx = nk_draw_vertex(vtx, &list->config, temp[i * 4 + 1], uv, col);
  9502. vtx = nk_draw_vertex(vtx, &list->config, temp[i * 4 + 2], uv, col);
  9503. vtx = nk_draw_vertex(vtx, &list->config, temp[i * 4 + 3], uv, col_trans);
  9504. }
  9505. }
  9506. /* free temporary normals + points */
  9507. nk_buffer_reset(list->vertices, NK_BUFFER_FRONT);
  9508. }
  9509. else {
  9510. /* NON ANTI-ALIASED STROKE */
  9511. nk_size i1 = 0;
  9512. nk_size idx = list->vertex_count;
  9513. const nk_size idx_count = count * 6;
  9514. const nk_size vtx_count = count * 4;
  9515. void* vtx = nk_draw_list_alloc_vertices(list, vtx_count);
  9516. nk_draw_index* ids = nk_draw_list_alloc_elements(list, idx_count);
  9517. if (!vtx || !ids) return;
  9518. for (i1 = 0; i1 < count; ++i1) {
  9519. float dx, dy;
  9520. const struct nk_vec2 uv = list->config.tex_null.uv;
  9521. const nk_size i2 = ((i1 + 1) == points_count) ? 0 : i1 + 1;
  9522. const struct nk_vec2 p1 = points[i1];
  9523. const struct nk_vec2 p2 = points[i2];
  9524. struct nk_vec2 diff = nk_vec2_sub(p2, p1);
  9525. float len;
  9526. /* vec2 inverted length */
  9527. len = nk_vec2_len_sqr(diff);
  9528. if (len != 0.0f)
  9529. len = NK_INV_SQRT(len);
  9530. else len = 1.0f;
  9531. diff = nk_vec2_muls(diff, len);
  9532. /* add vertices */
  9533. dx = diff.x * (thickness * 0.5f);
  9534. dy = diff.y * (thickness * 0.5f);
  9535. vtx = nk_draw_vertex(vtx, &list->config, nk_vec2(p1.x + dy, p1.y - dx), uv, col);
  9536. vtx = nk_draw_vertex(vtx, &list->config, nk_vec2(p2.x + dy, p2.y - dx), uv, col);
  9537. vtx = nk_draw_vertex(vtx, &list->config, nk_vec2(p2.x - dy, p2.y + dx), uv, col);
  9538. vtx = nk_draw_vertex(vtx, &list->config, nk_vec2(p1.x - dy, p1.y + dx), uv, col);
  9539. ids[0] = (nk_draw_index)(idx + 0); ids[1] = (nk_draw_index)(idx + 1);
  9540. ids[2] = (nk_draw_index)(idx + 2); ids[3] = (nk_draw_index)(idx + 0);
  9541. ids[4] = (nk_draw_index)(idx + 2); ids[5] = (nk_draw_index)(idx + 3);
  9542. ids += 6;
  9543. idx += 4;
  9544. }
  9545. }
  9546. }
  9547. NK_API void
  9548. nk_draw_list_fill_poly_convex(struct nk_draw_list* list,
  9549. const struct nk_vec2* points, const unsigned int points_count,
  9550. struct nk_color color, enum nk_anti_aliasing aliasing)
  9551. {
  9552. struct nk_colorf col;
  9553. struct nk_colorf col_trans;
  9554. NK_STORAGE const nk_size pnt_align = NK_ALIGNOF(struct nk_vec2);
  9555. NK_STORAGE const nk_size pnt_size = sizeof(struct nk_vec2);
  9556. NK_ASSERT(list);
  9557. if (!list || points_count < 3) return;
  9558. #ifdef NK_INCLUDE_COMMAND_USERDATA
  9559. nk_draw_list_push_userdata(list, list->userdata);
  9560. #endif
  9561. color.a = (nk_byte)((float)color.a * list->config.global_alpha);
  9562. nk_color_fv(&col.r, color);
  9563. col_trans = col;
  9564. col_trans.a = 0;
  9565. if (aliasing == NK_ANTI_ALIASING_ON) {
  9566. nk_size i = 0;
  9567. nk_size i0 = 0;
  9568. nk_size i1 = 0;
  9569. const float AA_SIZE = 1.0f;
  9570. nk_size vertex_offset = 0;
  9571. nk_size index = list->vertex_count;
  9572. const nk_size idx_count = (points_count - 2) * 3 + points_count * 6;
  9573. const nk_size vtx_count = (points_count * 2);
  9574. void* vtx = nk_draw_list_alloc_vertices(list, vtx_count);
  9575. nk_draw_index* ids = nk_draw_list_alloc_elements(list, idx_count);
  9576. nk_size size = 0;
  9577. struct nk_vec2* normals = 0;
  9578. unsigned int vtx_inner_idx = (unsigned int)(index + 0);
  9579. unsigned int vtx_outer_idx = (unsigned int)(index + 1);
  9580. if (!vtx || !ids) return;
  9581. /* temporary allocate normals */
  9582. vertex_offset = (nk_size)((nk_byte*)vtx - (nk_byte*)list->vertices->memory.ptr);
  9583. nk_buffer_mark(list->vertices, NK_BUFFER_FRONT);
  9584. size = pnt_size * points_count;
  9585. normals = (struct nk_vec2*)nk_buffer_alloc(list->vertices, NK_BUFFER_FRONT, size, pnt_align);
  9586. if (!normals) return;
  9587. vtx = (void*)((nk_byte*)list->vertices->memory.ptr + vertex_offset);
  9588. /* add elements */
  9589. for (i = 2; i < points_count; i++) {
  9590. ids[0] = (nk_draw_index)(vtx_inner_idx);
  9591. ids[1] = (nk_draw_index)(vtx_inner_idx + ((i - 1) << 1));
  9592. ids[2] = (nk_draw_index)(vtx_inner_idx + (i << 1));
  9593. ids += 3;
  9594. }
  9595. /* compute normals */
  9596. for (i0 = points_count - 1, i1 = 0; i1 < points_count; i0 = i1++) {
  9597. struct nk_vec2 p0 = points[i0];
  9598. struct nk_vec2 p1 = points[i1];
  9599. struct nk_vec2 diff = nk_vec2_sub(p1, p0);
  9600. /* vec2 inverted length */
  9601. float len = nk_vec2_len_sqr(diff);
  9602. if (len != 0.0f)
  9603. len = NK_INV_SQRT(len);
  9604. else len = 1.0f;
  9605. diff = nk_vec2_muls(diff, len);
  9606. normals[i0].x = diff.y;
  9607. normals[i0].y = -diff.x;
  9608. }
  9609. /* add vertices + indexes */
  9610. for (i0 = points_count - 1, i1 = 0; i1 < points_count; i0 = i1++) {
  9611. const struct nk_vec2 uv = list->config.tex_null.uv;
  9612. struct nk_vec2 n0 = normals[i0];
  9613. struct nk_vec2 n1 = normals[i1];
  9614. struct nk_vec2 dm = nk_vec2_muls(nk_vec2_add(n0, n1), 0.5f);
  9615. float dmr2 = dm.x * dm.x + dm.y * dm.y;
  9616. if (dmr2 > 0.000001f) {
  9617. float scale = 1.0f / dmr2;
  9618. scale = NK_MIN(scale, 100.0f);
  9619. dm = nk_vec2_muls(dm, scale);
  9620. }
  9621. dm = nk_vec2_muls(dm, AA_SIZE * 0.5f);
  9622. /* add vertices */
  9623. vtx = nk_draw_vertex(vtx, &list->config, nk_vec2_sub(points[i1], dm), uv, col);
  9624. vtx = nk_draw_vertex(vtx, &list->config, nk_vec2_add(points[i1], dm), uv, col_trans);
  9625. /* add indexes */
  9626. ids[0] = (nk_draw_index)(vtx_inner_idx + (i1 << 1));
  9627. ids[1] = (nk_draw_index)(vtx_inner_idx + (i0 << 1));
  9628. ids[2] = (nk_draw_index)(vtx_outer_idx + (i0 << 1));
  9629. ids[3] = (nk_draw_index)(vtx_outer_idx + (i0 << 1));
  9630. ids[4] = (nk_draw_index)(vtx_outer_idx + (i1 << 1));
  9631. ids[5] = (nk_draw_index)(vtx_inner_idx + (i1 << 1));
  9632. ids += 6;
  9633. }
  9634. /* free temporary normals + points */
  9635. nk_buffer_reset(list->vertices, NK_BUFFER_FRONT);
  9636. }
  9637. else {
  9638. nk_size i = 0;
  9639. nk_size index = list->vertex_count;
  9640. const nk_size idx_count = (points_count - 2) * 3;
  9641. const nk_size vtx_count = points_count;
  9642. void* vtx = nk_draw_list_alloc_vertices(list, vtx_count);
  9643. nk_draw_index* ids = nk_draw_list_alloc_elements(list, idx_count);
  9644. if (!vtx || !ids) return;
  9645. for (i = 0; i < vtx_count; ++i)
  9646. vtx = nk_draw_vertex(vtx, &list->config, points[i], list->config.tex_null.uv, col);
  9647. for (i = 2; i < points_count; ++i) {
  9648. ids[0] = (nk_draw_index)index;
  9649. ids[1] = (nk_draw_index)(index + i - 1);
  9650. ids[2] = (nk_draw_index)(index + i);
  9651. ids += 3;
  9652. }
  9653. }
  9654. }
  9655. NK_API void
  9656. nk_draw_list_path_clear(struct nk_draw_list* list)
  9657. {
  9658. NK_ASSERT(list);
  9659. if (!list) return;
  9660. nk_buffer_reset(list->buffer, NK_BUFFER_FRONT);
  9661. list->path_count = 0;
  9662. list->path_offset = 0;
  9663. }
  9664. NK_API void
  9665. nk_draw_list_path_line_to(struct nk_draw_list* list, struct nk_vec2 pos)
  9666. {
  9667. struct nk_vec2* points = 0;
  9668. struct nk_draw_command* cmd = 0;
  9669. NK_ASSERT(list);
  9670. if (!list) return;
  9671. if (!list->cmd_count)
  9672. nk_draw_list_add_clip(list, nk_null_rect);
  9673. cmd = nk_draw_list_command_last(list);
  9674. if (cmd && cmd->texture.ptr != list->config.tex_null.texture.ptr)
  9675. nk_draw_list_push_image(list, list->config.tex_null.texture);
  9676. points = nk_draw_list_alloc_path(list, 1);
  9677. if (!points) return;
  9678. points[0] = pos;
  9679. }
  9680. NK_API void
  9681. nk_draw_list_path_arc_to_fast(struct nk_draw_list* list, struct nk_vec2 center,
  9682. float radius, int a_min, int a_max)
  9683. {
  9684. int a = 0;
  9685. NK_ASSERT(list);
  9686. if (!list) return;
  9687. if (a_min <= a_max) {
  9688. for (a = a_min; a <= a_max; a++) {
  9689. const struct nk_vec2 c = list->circle_vtx[(nk_size)a % NK_LEN(list->circle_vtx)];
  9690. const float x = center.x + c.x * radius;
  9691. const float y = center.y + c.y * radius;
  9692. nk_draw_list_path_line_to(list, nk_vec2(x, y));
  9693. }
  9694. }
  9695. }
  9696. NK_API void
  9697. nk_draw_list_path_arc_to(struct nk_draw_list* list, struct nk_vec2 center,
  9698. float radius, float a_min, float a_max, unsigned int segments)
  9699. {
  9700. unsigned int i = 0;
  9701. NK_ASSERT(list);
  9702. if (!list) return;
  9703. if (radius == 0.0f) return;
  9704. /* This algorithm for arc drawing relies on these two trigonometric identities[1]:
  9705. sin(a + b) = sin(a) * cos(b) + cos(a) * sin(b)
  9706. cos(a + b) = cos(a) * cos(b) - sin(a) * sin(b)
  9707. Two coordinates (x, y) of a point on a circle centered on
  9708. the origin can be written in polar form as:
  9709. x = r * cos(a)
  9710. y = r * sin(a)
  9711. where r is the radius of the circle,
  9712. a is the angle between (x, y) and the origin.
  9713. This allows us to rotate the coordinates around the
  9714. origin by an angle b using the following transformation:
  9715. x' = r * cos(a + b) = x * cos(b) - y * sin(b)
  9716. y' = r * sin(a + b) = y * cos(b) + x * sin(b)
  9717. [1] https://en.wikipedia.org/wiki/List_of_trigonometric_identities#Angle_sum_and_difference_identities
  9718. */
  9719. {const float d_angle = (a_max - a_min) / (float)segments;
  9720. const float sin_d = (float)NK_SIN(d_angle);
  9721. const float cos_d = (float)NK_COS(d_angle);
  9722. float cx = (float)NK_COS(a_min) * radius;
  9723. float cy = (float)NK_SIN(a_min) * radius;
  9724. for (i = 0; i <= segments; ++i) {
  9725. float new_cx, new_cy;
  9726. const float x = center.x + cx;
  9727. const float y = center.y + cy;
  9728. nk_draw_list_path_line_to(list, nk_vec2(x, y));
  9729. new_cx = cx * cos_d - cy * sin_d;
  9730. new_cy = cy * cos_d + cx * sin_d;
  9731. cx = new_cx;
  9732. cy = new_cy;
  9733. }}
  9734. }
  9735. NK_API void
  9736. nk_draw_list_path_rect_to(struct nk_draw_list* list, struct nk_vec2 a,
  9737. struct nk_vec2 b, float rounding)
  9738. {
  9739. float r;
  9740. NK_ASSERT(list);
  9741. if (!list) return;
  9742. r = rounding;
  9743. r = NK_MIN(r, ((b.x - a.x) < 0) ? -(b.x - a.x) : (b.x - a.x));
  9744. r = NK_MIN(r, ((b.y - a.y) < 0) ? -(b.y - a.y) : (b.y - a.y));
  9745. if (r == 0.0f) {
  9746. nk_draw_list_path_line_to(list, a);
  9747. nk_draw_list_path_line_to(list, nk_vec2(b.x, a.y));
  9748. nk_draw_list_path_line_to(list, b);
  9749. nk_draw_list_path_line_to(list, nk_vec2(a.x, b.y));
  9750. }
  9751. else {
  9752. nk_draw_list_path_arc_to_fast(list, nk_vec2(a.x + r, a.y + r), r, 6, 9);
  9753. nk_draw_list_path_arc_to_fast(list, nk_vec2(b.x - r, a.y + r), r, 9, 12);
  9754. nk_draw_list_path_arc_to_fast(list, nk_vec2(b.x - r, b.y - r), r, 0, 3);
  9755. nk_draw_list_path_arc_to_fast(list, nk_vec2(a.x + r, b.y - r), r, 3, 6);
  9756. }
  9757. }
  9758. NK_API void
  9759. nk_draw_list_path_curve_to(struct nk_draw_list* list, struct nk_vec2 p2,
  9760. struct nk_vec2 p3, struct nk_vec2 p4, unsigned int num_segments)
  9761. {
  9762. float t_step;
  9763. unsigned int i_step;
  9764. struct nk_vec2 p1;
  9765. NK_ASSERT(list);
  9766. NK_ASSERT(list->path_count);
  9767. if (!list || !list->path_count) return;
  9768. num_segments = NK_MAX(num_segments, 1);
  9769. p1 = nk_draw_list_path_last(list);
  9770. t_step = 1.0f / (float)num_segments;
  9771. for (i_step = 1; i_step <= num_segments; ++i_step) {
  9772. float t = t_step * (float)i_step;
  9773. float u = 1.0f - t;
  9774. float w1 = u * u * u;
  9775. float w2 = 3 * u * u * t;
  9776. float w3 = 3 * u * t * t;
  9777. float w4 = t * t * t;
  9778. float x = w1 * p1.x + w2 * p2.x + w3 * p3.x + w4 * p4.x;
  9779. float y = w1 * p1.y + w2 * p2.y + w3 * p3.y + w4 * p4.y;
  9780. nk_draw_list_path_line_to(list, nk_vec2(x, y));
  9781. }
  9782. }
  9783. NK_API void
  9784. nk_draw_list_path_fill(struct nk_draw_list* list, struct nk_color color)
  9785. {
  9786. struct nk_vec2* points;
  9787. NK_ASSERT(list);
  9788. if (!list) return;
  9789. points = (struct nk_vec2*)nk_buffer_memory(list->buffer);
  9790. nk_draw_list_fill_poly_convex(list, points, list->path_count, color, list->config.shape_AA);
  9791. nk_draw_list_path_clear(list);
  9792. }
  9793. NK_API void
  9794. nk_draw_list_path_stroke(struct nk_draw_list* list, struct nk_color color,
  9795. enum nk_draw_list_stroke closed, float thickness)
  9796. {
  9797. struct nk_vec2* points;
  9798. NK_ASSERT(list);
  9799. if (!list) return;
  9800. points = (struct nk_vec2*)nk_buffer_memory(list->buffer);
  9801. nk_draw_list_stroke_poly_line(list, points, list->path_count, color,
  9802. closed, thickness, list->config.line_AA);
  9803. nk_draw_list_path_clear(list);
  9804. }
  9805. NK_API void
  9806. nk_draw_list_stroke_line(struct nk_draw_list* list, struct nk_vec2 a,
  9807. struct nk_vec2 b, struct nk_color col, float thickness)
  9808. {
  9809. NK_ASSERT(list);
  9810. if (!list || !col.a) return;
  9811. if (list->line_AA == NK_ANTI_ALIASING_ON) {
  9812. nk_draw_list_path_line_to(list, a);
  9813. nk_draw_list_path_line_to(list, b);
  9814. }
  9815. else {
  9816. nk_draw_list_path_line_to(list, nk_vec2_sub(a, nk_vec2(0.5f, 0.5f)));
  9817. nk_draw_list_path_line_to(list, nk_vec2_sub(b, nk_vec2(0.5f, 0.5f)));
  9818. }
  9819. nk_draw_list_path_stroke(list, col, NK_STROKE_OPEN, thickness);
  9820. }
  9821. NK_API void
  9822. nk_draw_list_fill_rect(struct nk_draw_list* list, struct nk_rect rect,
  9823. struct nk_color col, float rounding)
  9824. {
  9825. NK_ASSERT(list);
  9826. if (!list || !col.a) return;
  9827. if (list->line_AA == NK_ANTI_ALIASING_ON) {
  9828. nk_draw_list_path_rect_to(list, nk_vec2(rect.x, rect.y),
  9829. nk_vec2(rect.x + rect.w, rect.y + rect.h), rounding);
  9830. }
  9831. else {
  9832. nk_draw_list_path_rect_to(list, nk_vec2(rect.x - 0.5f, rect.y - 0.5f),
  9833. nk_vec2(rect.x + rect.w, rect.y + rect.h), rounding);
  9834. } nk_draw_list_path_fill(list, col);
  9835. }
  9836. NK_API void
  9837. nk_draw_list_stroke_rect(struct nk_draw_list* list, struct nk_rect rect,
  9838. struct nk_color col, float rounding, float thickness)
  9839. {
  9840. NK_ASSERT(list);
  9841. if (!list || !col.a) return;
  9842. if (list->line_AA == NK_ANTI_ALIASING_ON) {
  9843. nk_draw_list_path_rect_to(list, nk_vec2(rect.x, rect.y),
  9844. nk_vec2(rect.x + rect.w, rect.y + rect.h), rounding);
  9845. }
  9846. else {
  9847. nk_draw_list_path_rect_to(list, nk_vec2(rect.x - 0.5f, rect.y - 0.5f),
  9848. nk_vec2(rect.x + rect.w, rect.y + rect.h), rounding);
  9849. } nk_draw_list_path_stroke(list, col, NK_STROKE_CLOSED, thickness);
  9850. }
  9851. NK_API void
  9852. nk_draw_list_fill_rect_multi_color(struct nk_draw_list* list, struct nk_rect rect,
  9853. struct nk_color left, struct nk_color top, struct nk_color right,
  9854. struct nk_color bottom)
  9855. {
  9856. void* vtx;
  9857. struct nk_colorf col_left, col_top;
  9858. struct nk_colorf col_right, col_bottom;
  9859. nk_draw_index* idx;
  9860. nk_draw_index index;
  9861. nk_color_fv(&col_left.r, left);
  9862. nk_color_fv(&col_right.r, right);
  9863. nk_color_fv(&col_top.r, top);
  9864. nk_color_fv(&col_bottom.r, bottom);
  9865. NK_ASSERT(list);
  9866. if (!list) return;
  9867. nk_draw_list_push_image(list, list->config.tex_null.texture);
  9868. index = (nk_draw_index)list->vertex_count;
  9869. vtx = nk_draw_list_alloc_vertices(list, 4);
  9870. idx = nk_draw_list_alloc_elements(list, 6);
  9871. if (!vtx || !idx) return;
  9872. idx[0] = (nk_draw_index)(index + 0); idx[1] = (nk_draw_index)(index + 1);
  9873. idx[2] = (nk_draw_index)(index + 2); idx[3] = (nk_draw_index)(index + 0);
  9874. idx[4] = (nk_draw_index)(index + 2); idx[5] = (nk_draw_index)(index + 3);
  9875. vtx = nk_draw_vertex(vtx, &list->config, nk_vec2(rect.x, rect.y), list->config.tex_null.uv, col_left);
  9876. vtx = nk_draw_vertex(vtx, &list->config, nk_vec2(rect.x + rect.w, rect.y), list->config.tex_null.uv, col_top);
  9877. vtx = nk_draw_vertex(vtx, &list->config, nk_vec2(rect.x + rect.w, rect.y + rect.h), list->config.tex_null.uv, col_right);
  9878. vtx = nk_draw_vertex(vtx, &list->config, nk_vec2(rect.x, rect.y + rect.h), list->config.tex_null.uv, col_bottom);
  9879. }
  9880. NK_API void
  9881. nk_draw_list_fill_triangle(struct nk_draw_list* list, struct nk_vec2 a,
  9882. struct nk_vec2 b, struct nk_vec2 c, struct nk_color col)
  9883. {
  9884. NK_ASSERT(list);
  9885. if (!list || !col.a) return;
  9886. nk_draw_list_path_line_to(list, a);
  9887. nk_draw_list_path_line_to(list, b);
  9888. nk_draw_list_path_line_to(list, c);
  9889. nk_draw_list_path_fill(list, col);
  9890. }
  9891. NK_API void
  9892. nk_draw_list_stroke_triangle(struct nk_draw_list* list, struct nk_vec2 a,
  9893. struct nk_vec2 b, struct nk_vec2 c, struct nk_color col, float thickness)
  9894. {
  9895. NK_ASSERT(list);
  9896. if (!list || !col.a) return;
  9897. nk_draw_list_path_line_to(list, a);
  9898. nk_draw_list_path_line_to(list, b);
  9899. nk_draw_list_path_line_to(list, c);
  9900. nk_draw_list_path_stroke(list, col, NK_STROKE_CLOSED, thickness);
  9901. }
  9902. NK_API void
  9903. nk_draw_list_fill_circle(struct nk_draw_list* list, struct nk_vec2 center,
  9904. float radius, struct nk_color col, unsigned int segs)
  9905. {
  9906. float a_max;
  9907. NK_ASSERT(list);
  9908. if (!list || !col.a) return;
  9909. a_max = NK_PI * 2.0f * ((float)segs - 1.0f) / (float)segs;
  9910. nk_draw_list_path_arc_to(list, center, radius, 0.0f, a_max, segs);
  9911. nk_draw_list_path_fill(list, col);
  9912. }
  9913. NK_API void
  9914. nk_draw_list_stroke_circle(struct nk_draw_list* list, struct nk_vec2 center,
  9915. float radius, struct nk_color col, unsigned int segs, float thickness)
  9916. {
  9917. float a_max;
  9918. NK_ASSERT(list);
  9919. if (!list || !col.a) return;
  9920. a_max = NK_PI * 2.0f * ((float)segs - 1.0f) / (float)segs;
  9921. nk_draw_list_path_arc_to(list, center, radius, 0.0f, a_max, segs);
  9922. nk_draw_list_path_stroke(list, col, NK_STROKE_CLOSED, thickness);
  9923. }
  9924. NK_API void
  9925. nk_draw_list_stroke_curve(struct nk_draw_list* list, struct nk_vec2 p0,
  9926. struct nk_vec2 cp0, struct nk_vec2 cp1, struct nk_vec2 p1,
  9927. struct nk_color col, unsigned int segments, float thickness)
  9928. {
  9929. NK_ASSERT(list);
  9930. if (!list || !col.a) return;
  9931. nk_draw_list_path_line_to(list, p0);
  9932. nk_draw_list_path_curve_to(list, cp0, cp1, p1, segments);
  9933. nk_draw_list_path_stroke(list, col, NK_STROKE_OPEN, thickness);
  9934. }
  9935. NK_INTERN void
  9936. nk_draw_list_push_rect_uv(struct nk_draw_list* list, struct nk_vec2 a,
  9937. struct nk_vec2 c, struct nk_vec2 uva, struct nk_vec2 uvc,
  9938. struct nk_color color)
  9939. {
  9940. void* vtx;
  9941. struct nk_vec2 uvb;
  9942. struct nk_vec2 uvd;
  9943. struct nk_vec2 b;
  9944. struct nk_vec2 d;
  9945. struct nk_colorf col;
  9946. nk_draw_index* idx;
  9947. nk_draw_index index;
  9948. NK_ASSERT(list);
  9949. if (!list) return;
  9950. nk_color_fv(&col.r, color);
  9951. uvb = nk_vec2(uvc.x, uva.y);
  9952. uvd = nk_vec2(uva.x, uvc.y);
  9953. b = nk_vec2(c.x, a.y);
  9954. d = nk_vec2(a.x, c.y);
  9955. index = (nk_draw_index)list->vertex_count;
  9956. vtx = nk_draw_list_alloc_vertices(list, 4);
  9957. idx = nk_draw_list_alloc_elements(list, 6);
  9958. if (!vtx || !idx) return;
  9959. idx[0] = (nk_draw_index)(index + 0); idx[1] = (nk_draw_index)(index + 1);
  9960. idx[2] = (nk_draw_index)(index + 2); idx[3] = (nk_draw_index)(index + 0);
  9961. idx[4] = (nk_draw_index)(index + 2); idx[5] = (nk_draw_index)(index + 3);
  9962. vtx = nk_draw_vertex(vtx, &list->config, a, uva, col);
  9963. vtx = nk_draw_vertex(vtx, &list->config, b, uvb, col);
  9964. vtx = nk_draw_vertex(vtx, &list->config, c, uvc, col);
  9965. vtx = nk_draw_vertex(vtx, &list->config, d, uvd, col);
  9966. }
  9967. NK_API void
  9968. nk_draw_list_add_image(struct nk_draw_list* list, struct nk_image texture,
  9969. struct nk_rect rect, struct nk_color color)
  9970. {
  9971. NK_ASSERT(list);
  9972. if (!list) return;
  9973. /* push new command with given texture */
  9974. nk_draw_list_push_image(list, texture.handle);
  9975. if (nk_image_is_subimage(&texture)) {
  9976. /* add region inside of the texture */
  9977. struct nk_vec2 uv[2];
  9978. uv[0].x = (float)texture.region[0] / (float)texture.w;
  9979. uv[0].y = (float)texture.region[1] / (float)texture.h;
  9980. uv[1].x = (float)(texture.region[0] + texture.region[2]) / (float)texture.w;
  9981. uv[1].y = (float)(texture.region[1] + texture.region[3]) / (float)texture.h;
  9982. nk_draw_list_push_rect_uv(list, nk_vec2(rect.x, rect.y),
  9983. nk_vec2(rect.x + rect.w, rect.y + rect.h), uv[0], uv[1], color);
  9984. }
  9985. else nk_draw_list_push_rect_uv(list, nk_vec2(rect.x, rect.y),
  9986. nk_vec2(rect.x + rect.w, rect.y + rect.h),
  9987. nk_vec2(0.0f, 0.0f), nk_vec2(1.0f, 1.0f), color);
  9988. }
  9989. NK_API void
  9990. nk_draw_list_add_text(struct nk_draw_list* list, const struct nk_user_font* font,
  9991. struct nk_rect rect, const char* text, int len, float font_height,
  9992. struct nk_color fg)
  9993. {
  9994. float x = 0;
  9995. int text_len = 0;
  9996. nk_rune unicode = 0;
  9997. nk_rune next = 0;
  9998. int glyph_len = 0;
  9999. int next_glyph_len = 0;
  10000. struct nk_user_font_glyph g;
  10001. NK_ASSERT(list);
  10002. if (!list || !len || !text) return;
  10003. if (!NK_INTERSECT(rect.x, rect.y, rect.w, rect.h,
  10004. list->clip_rect.x, list->clip_rect.y, list->clip_rect.w, list->clip_rect.h)) return;
  10005. nk_draw_list_push_image(list, font->texture);
  10006. x = rect.x;
  10007. glyph_len = nk_utf_decode(text, &unicode, len);
  10008. if (!glyph_len) return;
  10009. /* draw every glyph image */
  10010. fg.a = (nk_byte)((float)fg.a * list->config.global_alpha);
  10011. while (text_len < len && glyph_len) {
  10012. float gx, gy, gh, gw;
  10013. float char_width = 0;
  10014. if (unicode == NK_UTF_INVALID) break;
  10015. /* query currently drawn glyph information */
  10016. next_glyph_len = nk_utf_decode(text + text_len + glyph_len, &next, (int)len - text_len);
  10017. font->query(font->userdata, font_height, &g, unicode,
  10018. (next == NK_UTF_INVALID) ? '\0' : next);
  10019. /* calculate and draw glyph drawing rectangle and image */
  10020. gx = x + g.offset.x;
  10021. gy = rect.y + g.offset.y;
  10022. gw = g.width; gh = g.height;
  10023. char_width = g.xadvance;
  10024. nk_draw_list_push_rect_uv(list, nk_vec2(gx, gy), nk_vec2(gx + gw, gy + gh),
  10025. g.uv[0], g.uv[1], fg);
  10026. /* offset next glyph */
  10027. text_len += glyph_len;
  10028. x += char_width;
  10029. glyph_len = next_glyph_len;
  10030. unicode = next;
  10031. }
  10032. }
  10033. NK_API nk_flags
  10034. nk_convert(struct nk_context* ctx, struct nk_buffer* cmds,
  10035. struct nk_buffer* vertices, struct nk_buffer* elements,
  10036. const struct nk_convert_config* config)
  10037. {
  10038. nk_flags res = NK_CONVERT_SUCCESS;
  10039. const struct nk_command* cmd;
  10040. NK_ASSERT(ctx);
  10041. NK_ASSERT(cmds);
  10042. NK_ASSERT(vertices);
  10043. NK_ASSERT(elements);
  10044. NK_ASSERT(config);
  10045. NK_ASSERT(config->vertex_layout);
  10046. NK_ASSERT(config->vertex_size);
  10047. if (!ctx || !cmds || !vertices || !elements || !config || !config->vertex_layout)
  10048. return NK_CONVERT_INVALID_PARAM;
  10049. nk_draw_list_setup(&ctx->draw_list, config, cmds, vertices, elements,
  10050. config->line_AA, config->shape_AA);
  10051. nk_foreach(cmd, ctx)
  10052. {
  10053. #ifdef NK_INCLUDE_COMMAND_USERDATA
  10054. ctx->draw_list.userdata = cmd->userdata;
  10055. #endif
  10056. switch (cmd->type) {
  10057. case NK_COMMAND_NOP: break;
  10058. case NK_COMMAND_SCISSOR: {
  10059. const struct nk_command_scissor* s = (const struct nk_command_scissor*)cmd;
  10060. nk_draw_list_add_clip(&ctx->draw_list, nk_rect(s->x, s->y, s->w, s->h));
  10061. } break;
  10062. case NK_COMMAND_LINE: {
  10063. const struct nk_command_line* l = (const struct nk_command_line*)cmd;
  10064. nk_draw_list_stroke_line(&ctx->draw_list, nk_vec2(l->begin.x, l->begin.y),
  10065. nk_vec2(l->end.x, l->end.y), l->color, l->line_thickness);
  10066. } break;
  10067. case NK_COMMAND_CURVE: {
  10068. const struct nk_command_curve* q = (const struct nk_command_curve*)cmd;
  10069. nk_draw_list_stroke_curve(&ctx->draw_list, nk_vec2(q->begin.x, q->begin.y),
  10070. nk_vec2(q->ctrl[0].x, q->ctrl[0].y), nk_vec2(q->ctrl[1].x,
  10071. q->ctrl[1].y), nk_vec2(q->end.x, q->end.y), q->color,
  10072. config->curve_segment_count, q->line_thickness);
  10073. } break;
  10074. case NK_COMMAND_RECT: {
  10075. const struct nk_command_rect* r = (const struct nk_command_rect*)cmd;
  10076. nk_draw_list_stroke_rect(&ctx->draw_list, nk_rect(r->x, r->y, r->w, r->h),
  10077. r->color, (float)r->rounding, r->line_thickness);
  10078. } break;
  10079. case NK_COMMAND_RECT_FILLED: {
  10080. const struct nk_command_rect_filled* r = (const struct nk_command_rect_filled*)cmd;
  10081. nk_draw_list_fill_rect(&ctx->draw_list, nk_rect(r->x, r->y, r->w, r->h),
  10082. r->color, (float)r->rounding);
  10083. } break;
  10084. case NK_COMMAND_RECT_MULTI_COLOR: {
  10085. const struct nk_command_rect_multi_color* r = (const struct nk_command_rect_multi_color*)cmd;
  10086. nk_draw_list_fill_rect_multi_color(&ctx->draw_list, nk_rect(r->x, r->y, r->w, r->h),
  10087. r->left, r->top, r->right, r->bottom);
  10088. } break;
  10089. case NK_COMMAND_CIRCLE: {
  10090. const struct nk_command_circle* c = (const struct nk_command_circle*)cmd;
  10091. nk_draw_list_stroke_circle(&ctx->draw_list, nk_vec2((float)c->x + (float)c->w / 2,
  10092. (float)c->y + (float)c->h / 2), (float)c->w / 2, c->color,
  10093. config->circle_segment_count, c->line_thickness);
  10094. } break;
  10095. case NK_COMMAND_CIRCLE_FILLED: {
  10096. const struct nk_command_circle_filled* c = (const struct nk_command_circle_filled*)cmd;
  10097. nk_draw_list_fill_circle(&ctx->draw_list, nk_vec2((float)c->x + (float)c->w / 2,
  10098. (float)c->y + (float)c->h / 2), (float)c->w / 2, c->color,
  10099. config->circle_segment_count);
  10100. } break;
  10101. case NK_COMMAND_ARC: {
  10102. const struct nk_command_arc* c = (const struct nk_command_arc*)cmd;
  10103. nk_draw_list_path_line_to(&ctx->draw_list, nk_vec2(c->cx, c->cy));
  10104. nk_draw_list_path_arc_to(&ctx->draw_list, nk_vec2(c->cx, c->cy), c->r,
  10105. c->a[0], c->a[1], config->arc_segment_count);
  10106. nk_draw_list_path_stroke(&ctx->draw_list, c->color, NK_STROKE_CLOSED, c->line_thickness);
  10107. } break;
  10108. case NK_COMMAND_ARC_FILLED: {
  10109. const struct nk_command_arc_filled* c = (const struct nk_command_arc_filled*)cmd;
  10110. nk_draw_list_path_line_to(&ctx->draw_list, nk_vec2(c->cx, c->cy));
  10111. nk_draw_list_path_arc_to(&ctx->draw_list, nk_vec2(c->cx, c->cy), c->r,
  10112. c->a[0], c->a[1], config->arc_segment_count);
  10113. nk_draw_list_path_fill(&ctx->draw_list, c->color);
  10114. } break;
  10115. case NK_COMMAND_TRIANGLE: {
  10116. const struct nk_command_triangle* t = (const struct nk_command_triangle*)cmd;
  10117. nk_draw_list_stroke_triangle(&ctx->draw_list, nk_vec2(t->a.x, t->a.y),
  10118. nk_vec2(t->b.x, t->b.y), nk_vec2(t->c.x, t->c.y), t->color,
  10119. t->line_thickness);
  10120. } break;
  10121. case NK_COMMAND_TRIANGLE_FILLED: {
  10122. const struct nk_command_triangle_filled* t = (const struct nk_command_triangle_filled*)cmd;
  10123. nk_draw_list_fill_triangle(&ctx->draw_list, nk_vec2(t->a.x, t->a.y),
  10124. nk_vec2(t->b.x, t->b.y), nk_vec2(t->c.x, t->c.y), t->color);
  10125. } break;
  10126. case NK_COMMAND_POLYGON: {
  10127. int i;
  10128. const struct nk_command_polygon* p = (const struct nk_command_polygon*)cmd;
  10129. for (i = 0; i < p->point_count; ++i) {
  10130. struct nk_vec2 pnt = nk_vec2((float)p->points[i].x, (float)p->points[i].y);
  10131. nk_draw_list_path_line_to(&ctx->draw_list, pnt);
  10132. }
  10133. nk_draw_list_path_stroke(&ctx->draw_list, p->color, NK_STROKE_CLOSED, p->line_thickness);
  10134. } break;
  10135. case NK_COMMAND_POLYGON_FILLED: {
  10136. int i;
  10137. const struct nk_command_polygon_filled* p = (const struct nk_command_polygon_filled*)cmd;
  10138. for (i = 0; i < p->point_count; ++i) {
  10139. struct nk_vec2 pnt = nk_vec2((float)p->points[i].x, (float)p->points[i].y);
  10140. nk_draw_list_path_line_to(&ctx->draw_list, pnt);
  10141. }
  10142. nk_draw_list_path_fill(&ctx->draw_list, p->color);
  10143. } break;
  10144. case NK_COMMAND_POLYLINE: {
  10145. int i;
  10146. const struct nk_command_polyline* p = (const struct nk_command_polyline*)cmd;
  10147. for (i = 0; i < p->point_count; ++i) {
  10148. struct nk_vec2 pnt = nk_vec2((float)p->points[i].x, (float)p->points[i].y);
  10149. nk_draw_list_path_line_to(&ctx->draw_list, pnt);
  10150. }
  10151. nk_draw_list_path_stroke(&ctx->draw_list, p->color, NK_STROKE_OPEN, p->line_thickness);
  10152. } break;
  10153. case NK_COMMAND_TEXT: {
  10154. const struct nk_command_text* t = (const struct nk_command_text*)cmd;
  10155. nk_draw_list_add_text(&ctx->draw_list, t->font, nk_rect(t->x, t->y, t->w, t->h),
  10156. t->string, t->length, t->height, t->foreground);
  10157. } break;
  10158. case NK_COMMAND_IMAGE: {
  10159. const struct nk_command_image* i = (const struct nk_command_image*)cmd;
  10160. nk_draw_list_add_image(&ctx->draw_list, i->img, nk_rect(i->x, i->y, i->w, i->h), i->col);
  10161. } break;
  10162. case NK_COMMAND_CUSTOM: {
  10163. const struct nk_command_custom* c = (const struct nk_command_custom*)cmd;
  10164. c->callback(&ctx->draw_list, c->x, c->y, c->w, c->h, c->callback_data);
  10165. } break;
  10166. default: break;
  10167. }
  10168. }
  10169. res |= (cmds->needed > cmds->allocated + (cmds->memory.size - cmds->size)) ? NK_CONVERT_COMMAND_BUFFER_FULL : 0;
  10170. res |= (vertices->needed > vertices->allocated) ? NK_CONVERT_VERTEX_BUFFER_FULL : 0;
  10171. res |= (elements->needed > elements->allocated) ? NK_CONVERT_ELEMENT_BUFFER_FULL : 0;
  10172. return res;
  10173. }
  10174. NK_API const struct nk_draw_command*
  10175. nk__draw_begin(const struct nk_context* ctx,
  10176. const struct nk_buffer* buffer)
  10177. {
  10178. return nk__draw_list_begin(&ctx->draw_list, buffer);
  10179. }
  10180. NK_API const struct nk_draw_command*
  10181. nk__draw_end(const struct nk_context* ctx, const struct nk_buffer* buffer)
  10182. {
  10183. return nk__draw_list_end(&ctx->draw_list, buffer);
  10184. }
  10185. NK_API const struct nk_draw_command*
  10186. nk__draw_next(const struct nk_draw_command* cmd,
  10187. const struct nk_buffer* buffer, const struct nk_context* ctx)
  10188. {
  10189. return nk__draw_list_next(cmd, buffer, &ctx->draw_list);
  10190. }
  10191. #endif
  10192. /* stb_rect_pack.h - v1.01 - public domain - rectangle packing */
  10193. /* Sean Barrett 2014 */
  10194. /* */
  10195. /* Useful for e.g. packing rectangular textures into an atlas. */
  10196. /* Does not do rotation. */
  10197. /* */
  10198. /* Before #including, */
  10199. /* */
  10200. /* #define STB_RECT_PACK_IMPLEMENTATION */
  10201. /* */
  10202. /* in the file that you want to have the implementation. */
  10203. /* */
  10204. /* Not necessarily the awesomest packing method, but better than */
  10205. /* the totally naive one in stb_truetype (which is primarily what */
  10206. /* this is meant to replace). */
  10207. /* */
  10208. /* Has only had a few tests run, may have issues. */
  10209. /* */
  10210. /* More docs to come. */
  10211. /* */
  10212. /* No memory allocations; uses qsort() and assert() from stdlib. */
  10213. /* Can override those by defining STBRP_SORT and STBRP_ASSERT. */
  10214. /* */
  10215. /* This library currently uses the Skyline Bottom-Left algorithm. */
  10216. /* */
  10217. /* Please note: better rectangle packers are welcome! Please */
  10218. /* implement them to the same API, but with a different init */
  10219. /* function. */
  10220. /* */
  10221. /* Credits */
  10222. /* */
  10223. /* Library */
  10224. /* Sean Barrett */
  10225. /* Minor features */
  10226. /* Martins Mozeiko */
  10227. /* github:IntellectualKitty */
  10228. /* */
  10229. /* Bugfixes / warning fixes */
  10230. /* Jeremy Jaussaud */
  10231. /* Fabian Giesen */
  10232. /* */
  10233. /* Version history: */
  10234. /* */
  10235. /* 1.01 (2021-07-11) always use large rect mode, expose STBRP__MAXVAL in public section */
  10236. /* 1.00 (2019-02-25) avoid small space waste; gracefully fail too-wide rectangles */
  10237. /* 0.99 (2019-02-07) warning fixes */
  10238. /* 0.11 (2017-03-03) return packing success/fail result */
  10239. /* 0.10 (2016-10-25) remove cast-away-const to avoid warnings */
  10240. /* 0.09 (2016-08-27) fix compiler warnings */
  10241. /* 0.08 (2015-09-13) really fix bug with empty rects (w=0 or h=0) */
  10242. /* 0.07 (2015-09-13) fix bug with empty rects (w=0 or h=0) */
  10243. /* 0.06 (2015-04-15) added STBRP_SORT to allow replacing qsort */
  10244. /* 0.05: added STBRP_ASSERT to allow replacing assert */
  10245. /* 0.04: fixed minor bug in STBRP_LARGE_RECTS support */
  10246. /* 0.01: initial release */
  10247. /* */
  10248. /* LICENSE */
  10249. /* */
  10250. /* See end of file for license information. */
  10251. /* //////////////////////////////////////////////////////////////////////////// */
  10252. /* */
  10253. /* INCLUDE SECTION */
  10254. /* */
  10255. #ifndef STB_INCLUDE_STB_RECT_PACK_H
  10256. #define STB_INCLUDE_STB_RECT_PACK_H
  10257. #define STB_RECT_PACK_VERSION 1
  10258. #ifdef STBRP_STATIC
  10259. #define STBRP_DEF static
  10260. #else
  10261. #define STBRP_DEF extern
  10262. #endif
  10263. #ifdef __cplusplus
  10264. extern "C" {
  10265. #endif
  10266. typedef struct stbrp_context stbrp_context;
  10267. typedef struct stbrp_node stbrp_node;
  10268. typedef struct stbrp_rect stbrp_rect;
  10269. typedef int stbrp_coord;
  10270. #define STBRP__MAXVAL 0x7fffffff
  10271. /* Mostly for internal use, but this is the maximum supported coordinate value. */
  10272. STBRP_DEF int stbrp_pack_rects(stbrp_context* context, stbrp_rect* rects, int num_rects);
  10273. /* Assign packed locations to rectangles. The rectangles are of type */
  10274. /* 'stbrp_rect' defined below, stored in the array 'rects', and there */
  10275. /* are 'num_rects' many of them. */
  10276. /* */
  10277. /* Rectangles which are successfully packed have the 'was_packed' flag */
  10278. /* set to a non-zero value and 'x' and 'y' store the minimum location */
  10279. /* on each axis (i.e. bottom-left in cartesian coordinates, top-left */
  10280. /* if you imagine y increasing downwards). Rectangles which do not fit */
  10281. /* have the 'was_packed' flag set to 0. */
  10282. /* */
  10283. /* You should not try to access the 'rects' array from another thread */
  10284. /* while this function is running, as the function temporarily reorders */
  10285. /* the array while it executes. */
  10286. /* */
  10287. /* To pack into another rectangle, you need to call stbrp_init_target */
  10288. /* again. To continue packing into the same rectangle, you can call */
  10289. /* this function again. Calling this multiple times with multiple rect */
  10290. /* arrays will probably produce worse packing results than calling it */
  10291. /* a single time with the full rectangle array, but the option is */
  10292. /* available. */
  10293. /* */
  10294. /* The function returns 1 if all of the rectangles were successfully */
  10295. /* packed and 0 otherwise. */
  10296. struct stbrp_rect
  10297. {
  10298. /* reserved for your use: */
  10299. int id;
  10300. /* input: */
  10301. stbrp_coord w, h;
  10302. /* output: */
  10303. stbrp_coord x, y;
  10304. int was_packed; /* non-zero if valid packing */
  10305. }; /* 16 bytes, nominally */
  10306. STBRP_DEF void stbrp_init_target(stbrp_context* context, int width, int height, stbrp_node* nodes, int num_nodes);
  10307. /* Initialize a rectangle packer to: */
  10308. /* pack a rectangle that is 'width' by 'height' in dimensions */
  10309. /* using temporary storage provided by the array 'nodes', which is 'num_nodes' long */
  10310. /* */
  10311. /* You must call this function every time you start packing into a new target. */
  10312. /* */
  10313. /* There is no "shutdown" function. The 'nodes' memory must stay valid for */
  10314. /* the following stbrp_pack_rects() call (or calls), but can be freed after */
  10315. /* the call (or calls) finish. */
  10316. /* */
  10317. /* Note: to guarantee best results, either: */
  10318. /* 1. make sure 'num_nodes' >= 'width' */
  10319. /* or 2. call stbrp_allow_out_of_mem() defined below with 'allow_out_of_mem = 1' */
  10320. /* */
  10321. /* If you don't do either of the above things, widths will be quantized to multiples */
  10322. /* of small integers to guarantee the algorithm doesn't run out of temporary storage. */
  10323. /* */
  10324. /* If you do #2, then the non-quantized algorithm will be used, but the algorithm */
  10325. /* may run out of temporary storage and be unable to pack some rectangles. */
  10326. STBRP_DEF void stbrp_setup_allow_out_of_mem(stbrp_context* context, int allow_out_of_mem);
  10327. /* Optionally call this function after init but before doing any packing to */
  10328. /* change the handling of the out-of-temp-memory scenario, described above. */
  10329. /* If you call init again, this will be reset to the default (false). */
  10330. STBRP_DEF void stbrp_setup_heuristic(stbrp_context* context, int heuristic);
  10331. /* Optionally select which packing heuristic the library should use. Different */
  10332. /* heuristics will produce better/worse results for different data sets. */
  10333. /* If you call init again, this will be reset to the default. */
  10334. enum
  10335. {
  10336. STBRP_HEURISTIC_Skyline_default = 0,
  10337. STBRP_HEURISTIC_Skyline_BL_sortHeight = STBRP_HEURISTIC_Skyline_default,
  10338. STBRP_HEURISTIC_Skyline_BF_sortHeight
  10339. };
  10340. /* //////////////////////////////////////////////////////////////////////////// */
  10341. /* */
  10342. /* the details of the following structures don't matter to you, but they must */
  10343. /* be visible so you can handle the memory allocations for them */
  10344. struct stbrp_node
  10345. {
  10346. stbrp_coord x, y;
  10347. stbrp_node* next;
  10348. };
  10349. struct stbrp_context
  10350. {
  10351. int width;
  10352. int height;
  10353. int align;
  10354. int init_mode;
  10355. int heuristic;
  10356. int num_nodes;
  10357. stbrp_node* active_head;
  10358. stbrp_node* free_head;
  10359. stbrp_node extra[2]; /* we allocate two extra nodes so optimal user-node-count is 'width' not 'width+2' */
  10360. };
  10361. #ifdef __cplusplus
  10362. }
  10363. #endif
  10364. #endif
  10365. /* //////////////////////////////////////////////////////////////////////////// */
  10366. /* */
  10367. /* IMPLEMENTATION SECTION */
  10368. /* */
  10369. #ifdef STB_RECT_PACK_IMPLEMENTATION
  10370. #ifndef STBRP_SORT
  10371. #include <stdlib.h>
  10372. #define STBRP_SORT qsort
  10373. #endif
  10374. #ifndef STBRP_ASSERT
  10375. #include <assert.h>
  10376. #define STBRP_ASSERT assert
  10377. #endif
  10378. #ifdef _MSC_VER
  10379. #define STBRP__NOTUSED(v) (void)(v)
  10380. #define STBRP__CDECL __cdecl
  10381. #else
  10382. #define STBRP__NOTUSED(v) (void)sizeof(v)
  10383. #define STBRP__CDECL
  10384. #endif
  10385. enum
  10386. {
  10387. STBRP__INIT_skyline = 1
  10388. };
  10389. STBRP_DEF void stbrp_setup_heuristic(stbrp_context* context, int heuristic)
  10390. {
  10391. switch (context->init_mode) {
  10392. case STBRP__INIT_skyline:
  10393. STBRP_ASSERT(heuristic == STBRP_HEURISTIC_Skyline_BL_sortHeight || heuristic == STBRP_HEURISTIC_Skyline_BF_sortHeight);
  10394. context->heuristic = heuristic;
  10395. break;
  10396. default:
  10397. STBRP_ASSERT(0);
  10398. }
  10399. }
  10400. STBRP_DEF void stbrp_setup_allow_out_of_mem(stbrp_context* context, int allow_out_of_mem)
  10401. {
  10402. if (allow_out_of_mem)
  10403. /* if it's ok to run out of memory, then don't bother aligning them; */
  10404. /* this gives better packing, but may fail due to OOM (even though */
  10405. /* the rectangles easily fit). @TODO a smarter approach would be to only */
  10406. /* quantize once we've hit OOM, then we could get rid of this parameter. */
  10407. context->align = 1;
  10408. else {
  10409. /* if it's not ok to run out of memory, then quantize the widths */
  10410. /* so that num_nodes is always enough nodes. */
  10411. /* */
  10412. /* I.e. num_nodes * align >= width */
  10413. /* align >= width / num_nodes */
  10414. /* align = ceil(width/num_nodes) */
  10415. context->align = (context->width + context->num_nodes - 1) / context->num_nodes;
  10416. }
  10417. }
  10418. STBRP_DEF void stbrp_init_target(stbrp_context* context, int width, int height, stbrp_node* nodes, int num_nodes)
  10419. {
  10420. int i;
  10421. for (i = 0; i < num_nodes - 1; ++i)
  10422. nodes[i].next = &nodes[i + 1];
  10423. nodes[i].next = NULL;
  10424. context->init_mode = STBRP__INIT_skyline;
  10425. context->heuristic = STBRP_HEURISTIC_Skyline_default;
  10426. context->free_head = &nodes[0];
  10427. context->active_head = &context->extra[0];
  10428. context->width = width;
  10429. context->height = height;
  10430. context->num_nodes = num_nodes;
  10431. stbrp_setup_allow_out_of_mem(context, 0);
  10432. /* node 0 is the full width, node 1 is the sentinel (lets us not store width explicitly) */
  10433. context->extra[0].x = 0;
  10434. context->extra[0].y = 0;
  10435. context->extra[0].next = &context->extra[1];
  10436. context->extra[1].x = (stbrp_coord)width;
  10437. context->extra[1].y = (1 << 30);
  10438. context->extra[1].next = NULL;
  10439. }
  10440. /* find minimum y position if it starts at x1 */
  10441. static int stbrp__skyline_find_min_y(stbrp_context* c, stbrp_node* first, int x0, int width, int* pwaste)
  10442. {
  10443. stbrp_node* node = first;
  10444. int x1 = x0 + width;
  10445. int min_y, visited_width, waste_area;
  10446. STBRP__NOTUSED(c);
  10447. STBRP_ASSERT(first->x <= x0);
  10448. #if 0
  10449. /* skip in case we're past the node */
  10450. while (node->next->x <= x0)
  10451. ++node;
  10452. #else
  10453. STBRP_ASSERT(node->next->x > x0); /* we ended up handling this in the caller for efficiency */
  10454. #endif
  10455. STBRP_ASSERT(node->x <= x0);
  10456. min_y = 0;
  10457. waste_area = 0;
  10458. visited_width = 0;
  10459. while (node->x < x1) {
  10460. if (node->y > min_y) {
  10461. /* raise min_y higher. */
  10462. /* we've accounted for all waste up to min_y, */
  10463. /* but we'll now add more waste for everything we've visted */
  10464. waste_area += visited_width * (node->y - min_y);
  10465. min_y = node->y;
  10466. /* the first time through, visited_width might be reduced */
  10467. if (node->x < x0)
  10468. visited_width += node->next->x - x0;
  10469. else
  10470. visited_width += node->next->x - node->x;
  10471. }
  10472. else {
  10473. /* add waste area */
  10474. int under_width = node->next->x - node->x;
  10475. if (under_width + visited_width > width)
  10476. under_width = width - visited_width;
  10477. waste_area += under_width * (min_y - node->y);
  10478. visited_width += under_width;
  10479. }
  10480. node = node->next;
  10481. }
  10482. *pwaste = waste_area;
  10483. return min_y;
  10484. }
  10485. typedef struct
  10486. {
  10487. int x, y;
  10488. stbrp_node** prev_link;
  10489. } stbrp__findresult;
  10490. static stbrp__findresult stbrp__skyline_find_best_pos(stbrp_context* c, int width, int height)
  10491. {
  10492. int best_waste = (1 << 30), best_x, best_y = (1 << 30);
  10493. stbrp__findresult fr;
  10494. stbrp_node** prev, * node, * tail, ** best = NULL;
  10495. /* align to multiple of c->align */
  10496. width = (width + c->align - 1);
  10497. width -= width % c->align;
  10498. STBRP_ASSERT(width % c->align == 0);
  10499. /* if it can't possibly fit, bail immediately */
  10500. if (width > c->width || height > c->height) {
  10501. fr.prev_link = NULL;
  10502. fr.x = fr.y = 0;
  10503. return fr;
  10504. }
  10505. node = c->active_head;
  10506. prev = &c->active_head;
  10507. while (node->x + width <= c->width) {
  10508. int y, waste;
  10509. y = stbrp__skyline_find_min_y(c, node, node->x, width, &waste);
  10510. if (c->heuristic == STBRP_HEURISTIC_Skyline_BL_sortHeight) { /* actually just want to test BL */
  10511. /* bottom left */
  10512. if (y < best_y) {
  10513. best_y = y;
  10514. best = prev;
  10515. }
  10516. }
  10517. else {
  10518. /* best-fit */
  10519. if (y + height <= c->height) {
  10520. /* can only use it if it first vertically */
  10521. if (y < best_y || (y == best_y && waste < best_waste)) {
  10522. best_y = y;
  10523. best_waste = waste;
  10524. best = prev;
  10525. }
  10526. }
  10527. }
  10528. prev = &node->next;
  10529. node = node->next;
  10530. }
  10531. best_x = (best == NULL) ? 0 : (*best)->x;
  10532. /* if doing best-fit (BF), we also have to try aligning right edge to each node position */
  10533. /* */
  10534. /* e.g, if fitting */
  10535. /* */
  10536. /* ____________________ */
  10537. /* |____________________| */
  10538. /* */
  10539. /* into */
  10540. /* */
  10541. /* | | */
  10542. /* | ____________| */
  10543. /* |____________| */
  10544. /* */
  10545. /* then right-aligned reduces waste, but bottom-left BL is always chooses left-aligned */
  10546. /* */
  10547. /* This makes BF take about 2x the time */
  10548. if (c->heuristic == STBRP_HEURISTIC_Skyline_BF_sortHeight) {
  10549. tail = c->active_head;
  10550. node = c->active_head;
  10551. prev = &c->active_head;
  10552. /* find first node that's admissible */
  10553. while (tail->x < width)
  10554. tail = tail->next;
  10555. while (tail) {
  10556. int xpos = tail->x - width;
  10557. int y, waste;
  10558. STBRP_ASSERT(xpos >= 0);
  10559. /* find the left position that matches this */
  10560. while (node->next->x <= xpos) {
  10561. prev = &node->next;
  10562. node = node->next;
  10563. }
  10564. STBRP_ASSERT(node->next->x > xpos && node->x <= xpos);
  10565. y = stbrp__skyline_find_min_y(c, node, xpos, width, &waste);
  10566. if (y + height <= c->height) {
  10567. if (y <= best_y) {
  10568. if (y < best_y || waste < best_waste || (waste == best_waste && xpos < best_x)) {
  10569. best_x = xpos;
  10570. STBRP_ASSERT(y <= best_y);
  10571. best_y = y;
  10572. best_waste = waste;
  10573. best = prev;
  10574. }
  10575. }
  10576. }
  10577. tail = tail->next;
  10578. }
  10579. }
  10580. fr.prev_link = best;
  10581. fr.x = best_x;
  10582. fr.y = best_y;
  10583. return fr;
  10584. }
  10585. static stbrp__findresult stbrp__skyline_pack_rectangle(stbrp_context* context, int width, int height)
  10586. {
  10587. /* find best position according to heuristic */
  10588. stbrp__findresult res = stbrp__skyline_find_best_pos(context, width, height);
  10589. stbrp_node* node, * cur;
  10590. /* bail if: */
  10591. /* 1. it failed */
  10592. /* 2. the best node doesn't fit (we don't always check this) */
  10593. /* 3. we're out of memory */
  10594. if (res.prev_link == NULL || res.y + height > context->height || context->free_head == NULL) {
  10595. res.prev_link = NULL;
  10596. return res;
  10597. }
  10598. /* on success, create new node */
  10599. node = context->free_head;
  10600. node->x = (stbrp_coord)res.x;
  10601. node->y = (stbrp_coord)(res.y + height);
  10602. context->free_head = node->next;
  10603. /* insert the new node into the right starting point, and */
  10604. /* let 'cur' point to the remaining nodes needing to be */
  10605. /* stiched back in */
  10606. cur = *res.prev_link;
  10607. if (cur->x < res.x) {
  10608. /* preserve the existing one, so start testing with the next one */
  10609. stbrp_node* next = cur->next;
  10610. cur->next = node;
  10611. cur = next;
  10612. }
  10613. else {
  10614. *res.prev_link = node;
  10615. }
  10616. /* from here, traverse cur and free the nodes, until we get to one */
  10617. /* that shouldn't be freed */
  10618. while (cur->next && cur->next->x <= res.x + width) {
  10619. stbrp_node* next = cur->next;
  10620. /* move the current node to the free list */
  10621. cur->next = context->free_head;
  10622. context->free_head = cur;
  10623. cur = next;
  10624. }
  10625. /* stitch the list back in */
  10626. node->next = cur;
  10627. if (cur->x < res.x + width)
  10628. cur->x = (stbrp_coord)(res.x + width);
  10629. #ifdef _DEBUG
  10630. cur = context->active_head;
  10631. while (cur->x < context->width) {
  10632. STBRP_ASSERT(cur->x < cur->next->x);
  10633. cur = cur->next;
  10634. }
  10635. STBRP_ASSERT(cur->next == NULL);
  10636. {
  10637. int count = 0;
  10638. cur = context->active_head;
  10639. while (cur) {
  10640. cur = cur->next;
  10641. ++count;
  10642. }
  10643. cur = context->free_head;
  10644. while (cur) {
  10645. cur = cur->next;
  10646. ++count;
  10647. }
  10648. STBRP_ASSERT(count == context->num_nodes + 2);
  10649. }
  10650. #endif
  10651. return res;
  10652. }
  10653. static int STBRP__CDECL rect_height_compare(const void* a, const void* b)
  10654. {
  10655. const stbrp_rect* p = (const stbrp_rect*)a;
  10656. const stbrp_rect* q = (const stbrp_rect*)b;
  10657. if (p->h > q->h)
  10658. return -1;
  10659. if (p->h < q->h)
  10660. return 1;
  10661. return (p->w > q->w) ? -1 : (p->w < q->w);
  10662. }
  10663. static int STBRP__CDECL rect_original_order(const void* a, const void* b)
  10664. {
  10665. const stbrp_rect* p = (const stbrp_rect*)a;
  10666. const stbrp_rect* q = (const stbrp_rect*)b;
  10667. return (p->was_packed < q->was_packed) ? -1 : (p->was_packed > q->was_packed);
  10668. }
  10669. STBRP_DEF int stbrp_pack_rects(stbrp_context* context, stbrp_rect* rects, int num_rects)
  10670. {
  10671. int i, all_rects_packed = 1;
  10672. /* we use the 'was_packed' field internally to allow sorting/unsorting */
  10673. for (i = 0; i < num_rects; ++i) {
  10674. rects[i].was_packed = i;
  10675. }
  10676. /* sort according to heuristic */
  10677. STBRP_SORT(rects, num_rects, sizeof(rects[0]), rect_height_compare);
  10678. for (i = 0; i < num_rects; ++i) {
  10679. if (rects[i].w == 0 || rects[i].h == 0) {
  10680. rects[i].x = rects[i].y = 0; /* empty rect needs no space */
  10681. }
  10682. else {
  10683. stbrp__findresult fr = stbrp__skyline_pack_rectangle(context, rects[i].w, rects[i].h);
  10684. if (fr.prev_link) {
  10685. rects[i].x = (stbrp_coord)fr.x;
  10686. rects[i].y = (stbrp_coord)fr.y;
  10687. }
  10688. else {
  10689. rects[i].x = rects[i].y = STBRP__MAXVAL;
  10690. }
  10691. }
  10692. }
  10693. /* unsort */
  10694. STBRP_SORT(rects, num_rects, sizeof(rects[0]), rect_original_order);
  10695. /* set was_packed flags and all_rects_packed status */
  10696. for (i = 0; i < num_rects; ++i) {
  10697. rects[i].was_packed = !(rects[i].x == STBRP__MAXVAL && rects[i].y == STBRP__MAXVAL);
  10698. if (!rects[i].was_packed)
  10699. all_rects_packed = 0;
  10700. }
  10701. /* return the all_rects_packed status */
  10702. return all_rects_packed;
  10703. }
  10704. #endif
  10705. /*
  10706. ------------------------------------------------------------------------------
  10707. This software is available under 2 licenses -- choose whichever you prefer.
  10708. ------------------------------------------------------------------------------
  10709. ALTERNATIVE A - MIT License
  10710. Copyright (c) 2017 Sean Barrett
  10711. Permission is hereby granted, free of charge, to any person obtaining a copy of
  10712. this software and associated documentation files (the "Software"), to deal in
  10713. the Software without restriction, including without limitation the rights to
  10714. use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
  10715. of the Software, and to permit persons to whom the Software is furnished to do
  10716. so, subject to the following conditions:
  10717. The above copyright notice and this permission notice shall be included in all
  10718. copies or substantial portions of the Software.
  10719. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  10720. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  10721. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  10722. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  10723. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  10724. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  10725. SOFTWARE.
  10726. ------------------------------------------------------------------------------
  10727. ALTERNATIVE B - Public Domain (www.unlicense.org)
  10728. This is free and unencumbered software released into the public domain.
  10729. Anyone is free to copy, modify, publish, use, compile, sell, or distribute this
  10730. software, either in source code form or as a compiled binary, for any purpose,
  10731. commercial or non-commercial, and by any means.
  10732. In jurisdictions that recognize copyright laws, the author or authors of this
  10733. software dedicate any and all copyright interest in the software to the public
  10734. domain. We make this dedication for the benefit of the public at large and to
  10735. the detriment of our heirs and successors. We intend this dedication to be an
  10736. overt act of relinquishment in perpetuity of all present and future rights to
  10737. this software under copyright law.
  10738. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  10739. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  10740. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  10741. AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  10742. ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  10743. WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  10744. ------------------------------------------------------------------------------
  10745. */
  10746. /* stb_truetype.h - v1.26 - public domain */
  10747. /* authored from 2009-2021 by Sean Barrett / RAD Game Tools */
  10748. /* */
  10749. /* ======================================================================= */
  10750. /* */
  10751. /* NO SECURITY GUARANTEE -- DO NOT USE THIS ON UNTRUSTED FONT FILES */
  10752. /* */
  10753. /* This library does no range checking of the offsets found in the file, */
  10754. /* meaning an attacker can use it to read arbitrary memory. */
  10755. /* */
  10756. /* ======================================================================= */
  10757. /* */
  10758. /* This library processes TrueType files: */
  10759. /* parse files */
  10760. /* extract glyph metrics */
  10761. /* extract glyph shapes */
  10762. /* render glyphs to one-channel bitmaps with antialiasing (box filter) */
  10763. /* render glyphs to one-channel SDF bitmaps (signed-distance field/function) */
  10764. /* */
  10765. /* Todo: */
  10766. /* non-MS cmaps */
  10767. /* crashproof on bad data */
  10768. /* hinting? (no longer patented) */
  10769. /* cleartype-style AA? */
  10770. /* optimize: use simple memory allocator for intermediates */
  10771. /* optimize: build edge-list directly from curves */
  10772. /* optimize: rasterize directly from curves? */
  10773. /* */
  10774. /* ADDITIONAL CONTRIBUTORS */
  10775. /* */
  10776. /* Mikko Mononen: compound shape support, more cmap formats */
  10777. /* Tor Andersson: kerning, subpixel rendering */
  10778. /* Dougall Johnson: OpenType / Type 2 font handling */
  10779. /* Daniel Ribeiro Maciel: basic GPOS-based kerning */
  10780. /* */
  10781. /* Misc other: */
  10782. /* Ryan Gordon */
  10783. /* Simon Glass */
  10784. /* github:IntellectualKitty */
  10785. /* Imanol Celaya */
  10786. /* Daniel Ribeiro Maciel */
  10787. /* */
  10788. /* Bug/warning reports/fixes: */
  10789. /* "Zer" on mollyrocket Fabian "ryg" Giesen github:NiLuJe */
  10790. /* Cass Everitt Martins Mozeiko github:aloucks */
  10791. /* stoiko (Haemimont Games) Cap Petschulat github:oyvindjam */
  10792. /* Brian Hook Omar Cornut github:vassvik */
  10793. /* Walter van Niftrik Ryan Griege */
  10794. /* David Gow Peter LaValle */
  10795. /* David Given Sergey Popov */
  10796. /* Ivan-Assen Ivanov Giumo X. Clanjor */
  10797. /* Anthony Pesch Higor Euripedes */
  10798. /* Johan Duparc Thomas Fields */
  10799. /* Hou Qiming Derek Vinyard */
  10800. /* Rob Loach Cort Stratton */
  10801. /* Kenney Phillis Jr. Brian Costabile */
  10802. /* Ken Voskuil (kaesve) */
  10803. /* */
  10804. /* VERSION HISTORY */
  10805. /* */
  10806. /* 1.26 (2021-08-28) fix broken rasterizer */
  10807. /* 1.25 (2021-07-11) many fixes */
  10808. /* 1.24 (2020-02-05) fix warning */
  10809. /* 1.23 (2020-02-02) query SVG data for glyphs; query whole kerning table (but only kern not GPOS) */
  10810. /* 1.22 (2019-08-11) minimize missing-glyph duplication; fix kerning if both 'GPOS' and 'kern' are defined */
  10811. /* 1.21 (2019-02-25) fix warning */
  10812. /* 1.20 (2019-02-07) PackFontRange skips missing codepoints; GetScaleFontVMetrics() */
  10813. /* 1.19 (2018-02-11) GPOS kerning, STBTT_fmod */
  10814. /* 1.18 (2018-01-29) add missing function */
  10815. /* 1.17 (2017-07-23) make more arguments const; doc fix */
  10816. /* 1.16 (2017-07-12) SDF support */
  10817. /* 1.15 (2017-03-03) make more arguments const */
  10818. /* 1.14 (2017-01-16) num-fonts-in-TTC function */
  10819. /* 1.13 (2017-01-02) support OpenType fonts, certain Apple fonts */
  10820. /* 1.12 (2016-10-25) suppress warnings about casting away const with -Wcast-qual */
  10821. /* 1.11 (2016-04-02) fix unused-variable warning */
  10822. /* 1.10 (2016-04-02) user-defined fabs(); rare memory leak; remove duplicate typedef */
  10823. /* 1.09 (2016-01-16) warning fix; avoid crash on outofmem; use allocation userdata properly */
  10824. /* 1.08 (2015-09-13) document stbtt_Rasterize(); fixes for vertical & horizontal edges */
  10825. /* 1.07 (2015-08-01) allow PackFontRanges to accept arrays of sparse codepoints; */
  10826. /* variant PackFontRanges to pack and render in separate phases; */
  10827. /* fix stbtt_GetFontOFfsetForIndex (never worked for non-0 input?); */
  10828. /* fixed an assert() bug in the new rasterizer */
  10829. /* replace assert() with STBTT_assert() in new rasterizer */
  10830. /* */
  10831. /* Full history can be found at the end of this file. */
  10832. /* */
  10833. /* LICENSE */
  10834. /* */
  10835. /* See end of file for license information. */
  10836. /* */
  10837. /* USAGE */
  10838. /* */
  10839. /* Include this file in whatever places need to refer to it. In ONE C/C++ */
  10840. /* file, write: */
  10841. /* #define STB_TRUETYPE_IMPLEMENTATION */
  10842. /* before the #include of this file. This expands out the actual */
  10843. /* implementation into that C/C++ file. */
  10844. /* */
  10845. /* To make the implementation private to the file that generates the implementation, */
  10846. /* #define STBTT_STATIC */
  10847. /* */
  10848. /* Simple 3D API (don't ship this, but it's fine for tools and quick start) */
  10849. /* stbtt_BakeFontBitmap() -- bake a font to a bitmap for use as texture */
  10850. /* stbtt_GetBakedQuad() -- compute quad to draw for a given char */
  10851. /* */
  10852. /* Improved 3D API (more shippable): */
  10853. /* #include "stb_rect_pack.h" -- optional, but you really want it */
  10854. /* stbtt_PackBegin() */
  10855. /* stbtt_PackSetOversampling() -- for improved quality on small fonts */
  10856. /* stbtt_PackFontRanges() -- pack and renders */
  10857. /* stbtt_PackEnd() */
  10858. /* stbtt_GetPackedQuad() */
  10859. /* */
  10860. /* "Load" a font file from a memory buffer (you have to keep the buffer loaded) */
  10861. /* stbtt_InitFont() */
  10862. /* stbtt_GetFontOffsetForIndex() -- indexing for TTC font collections */
  10863. /* stbtt_GetNumberOfFonts() -- number of fonts for TTC font collections */
  10864. /* */
  10865. /* Render a unicode codepoint to a bitmap */
  10866. /* stbtt_GetCodepointBitmap() -- allocates and returns a bitmap */
  10867. /* stbtt_MakeCodepointBitmap() -- renders into bitmap you provide */
  10868. /* stbtt_GetCodepointBitmapBox() -- how big the bitmap must be */
  10869. /* */
  10870. /* Character advance/positioning */
  10871. /* stbtt_GetCodepointHMetrics() */
  10872. /* stbtt_GetFontVMetrics() */
  10873. /* stbtt_GetFontVMetricsOS2() */
  10874. /* stbtt_GetCodepointKernAdvance() */
  10875. /* */
  10876. /* Starting with version 1.06, the rasterizer was replaced with a new, */
  10877. /* faster and generally-more-precise rasterizer. The new rasterizer more */
  10878. /* accurately measures pixel coverage for anti-aliasing, except in the case */
  10879. /* where multiple shapes overlap, in which case it overestimates the AA pixel */
  10880. /* coverage. Thus, anti-aliasing of intersecting shapes may look wrong. If */
  10881. /* this turns out to be a problem, you can re-enable the old rasterizer with */
  10882. /* #define STBTT_RASTERIZER_VERSION 1 */
  10883. /* which will incur about a 15% speed hit. */
  10884. /* */
  10885. /* ADDITIONAL DOCUMENTATION */
  10886. /* */
  10887. /* Immediately after this block comment are a series of sample programs. */
  10888. /* */
  10889. /* After the sample programs is the "header file" section. This section */
  10890. /* includes documentation for each API function. */
  10891. /* */
  10892. /* Some important concepts to understand to use this library: */
  10893. /* */
  10894. /* Codepoint */
  10895. /* Characters are defined by unicode codepoints, e.g. 65 is */
  10896. /* uppercase A, 231 is lowercase c with a cedilla, 0x7e30 is */
  10897. /* the hiragana for "ma". */
  10898. /* */
  10899. /* Glyph */
  10900. /* A visual character shape (every codepoint is rendered as */
  10901. /* some glyph) */
  10902. /* */
  10903. /* Glyph index */
  10904. /* A font-specific integer ID representing a glyph */
  10905. /* */
  10906. /* Baseline */
  10907. /* Glyph shapes are defined relative to a baseline, which is the */
  10908. /* bottom of uppercase characters. Characters extend both above */
  10909. /* and below the baseline. */
  10910. /* */
  10911. /* Current Point */
  10912. /* As you draw text to the screen, you keep track of a "current point" */
  10913. /* which is the origin of each character. The current point's vertical */
  10914. /* position is the baseline. Even "baked fonts" use this model. */
  10915. /* */
  10916. /* Vertical Font Metrics */
  10917. /* The vertical qualities of the font, used to vertically position */
  10918. /* and space the characters. See docs for stbtt_GetFontVMetrics. */
  10919. /* */
  10920. /* Font Size in Pixels or Points */
  10921. /* The preferred interface for specifying font sizes in stb_truetype */
  10922. /* is to specify how tall the font's vertical extent should be in pixels. */
  10923. /* If that sounds good enough, skip the next paragraph. */
  10924. /* */
  10925. /* Most font APIs instead use "points", which are a common typographic */
  10926. /* measurement for describing font size, defined as 72 points per inch. */
  10927. /* stb_truetype provides a point API for compatibility. However, true */
  10928. /* "per inch" conventions don't make much sense on computer displays */
  10929. /* since different monitors have different number of pixels per */
  10930. /* inch. For example, Windows traditionally uses a convention that */
  10931. /* there are 96 pixels per inch, thus making 'inch' measurements have */
  10932. /* nothing to do with inches, and thus effectively defining a point to */
  10933. /* be 1.333 pixels. Additionally, the TrueType font data provides */
  10934. /* an explicit scale factor to scale a given font's glyphs to points, */
  10935. /* but the author has observed that this scale factor is often wrong */
  10936. /* for non-commercial fonts, thus making fonts scaled in points */
  10937. /* according to the TrueType spec incoherently sized in practice. */
  10938. /* */
  10939. /* DETAILED USAGE: */
  10940. /* */
  10941. /* Scale: */
  10942. /* Select how high you want the font to be, in points or pixels. */
  10943. /* Call ScaleForPixelHeight or ScaleForMappingEmToPixels to compute */
  10944. /* a scale factor SF that will be used by all other functions. */
  10945. /* */
  10946. /* Baseline: */
  10947. /* You need to select a y-coordinate that is the baseline of where */
  10948. /* your text will appear. Call GetFontBoundingBox to get the baseline-relative */
  10949. /* bounding box for all characters. SF*-y0 will be the distance in pixels */
  10950. /* that the worst-case character could extend above the baseline, so if */
  10951. /* you want the top edge of characters to appear at the top of the */
  10952. /* screen where y=0, then you would set the baseline to SF*-y0. */
  10953. /* */
  10954. /* Current point: */
  10955. /* Set the current point where the first character will appear. The */
  10956. /* first character could extend left of the current point; this is font */
  10957. /* dependent. You can either choose a current point that is the leftmost */
  10958. /* point and hope, or add some padding, or check the bounding box or */
  10959. /* left-side-bearing of the first character to be displayed and set */
  10960. /* the current point based on that. */
  10961. /* */
  10962. /* Displaying a character: */
  10963. /* Compute the bounding box of the character. It will contain signed values */
  10964. /* relative to <current_point, baseline>. I.e. if it returns x0,y0,x1,y1, */
  10965. /* then the character should be displayed in the rectangle from */
  10966. /* <current_point+SF*x0, baseline+SF*y0> to <current_point+SF*x1,baseline+SF*y1). */
  10967. /* */
  10968. /* Advancing for the next character: */
  10969. /* Call GlyphHMetrics, and compute 'current_point += SF * advance'. */
  10970. /* */
  10971. /* */
  10972. /* ADVANCED USAGE */
  10973. /* */
  10974. /* Quality: */
  10975. /* */
  10976. /* - Use the functions with Subpixel at the end to allow your characters */
  10977. /* to have subpixel positioning. Since the font is anti-aliased, not */
  10978. /* hinted, this is very import for quality. (This is not possible with */
  10979. /* baked fonts.) */
  10980. /* */
  10981. /* - Kerning is now supported, and if you're supporting subpixel rendering */
  10982. /* then kerning is worth using to give your text a polished look. */
  10983. /* */
  10984. /* Performance: */
  10985. /* */
  10986. /* - Convert Unicode codepoints to glyph indexes and operate on the glyphs; */
  10987. /* if you don't do this, stb_truetype is forced to do the conversion on */
  10988. /* every call. */
  10989. /* */
  10990. /* - There are a lot of memory allocations. We should modify it to take */
  10991. /* a temp buffer and allocate from the temp buffer (without freeing), */
  10992. /* should help performance a lot. */
  10993. /* */
  10994. /* NOTES */
  10995. /* */
  10996. /* The system uses the raw data found in the .ttf file without changing it */
  10997. /* and without building auxiliary data structures. This is a bit inefficient */
  10998. /* on little-endian systems (the data is big-endian), but assuming you're */
  10999. /* caching the bitmaps or glyph shapes this shouldn't be a big deal. */
  11000. /* */
  11001. /* It appears to be very hard to programmatically determine what font a */
  11002. /* given file is in a general way. I provide an API for this, but I don't */
  11003. /* recommend it. */
  11004. /* */
  11005. /* */
  11006. /* PERFORMANCE MEASUREMENTS FOR 1.06: */
  11007. /* */
  11008. /* 32-bit 64-bit */
  11009. /* Previous release: 8.83 s 7.68 s */
  11010. /* Pool allocations: 7.72 s 6.34 s */
  11011. /* Inline sort : 6.54 s 5.65 s */
  11012. /* New rasterizer : 5.63 s 5.00 s */
  11013. /* //////////////////////////////////////////////////////////////////////////// */
  11014. /* //////////////////////////////////////////////////////////////////////////// */
  11015. /* // */
  11016. /* // SAMPLE PROGRAMS */
  11017. /* // */
  11018. /* */
  11019. /* Incomplete text-in-3d-api example, which draws quads properly aligned to be lossless. */
  11020. /* See "tests/truetype_demo_win32.c" for a complete version. */
  11021. #if 0
  11022. #define STB_TRUETYPE_IMPLEMENTATION /* force following include to generate implementation */
  11023. #include "stb_truetype.h"
  11024. unsigned char ttf_buffer[1 << 20];
  11025. unsigned char temp_bitmap[512 * 512];
  11026. stbtt_bakedchar cdata[96]; /* ASCII 32..126 is 95 glyphs */
  11027. GLuint ftex;
  11028. void my_stbtt_initfont(void)
  11029. {
  11030. fread(ttf_buffer, 1, 1 << 20, fopen("c:/windows/fonts/times.ttf", "rb"));
  11031. stbtt_BakeFontBitmap(ttf_buffer, 0, 32.0, temp_bitmap, 512, 512, 32, 96, cdata); /* no guarantee this fits! */
  11032. /* can free ttf_buffer at this point */
  11033. glGenTextures(1, &ftex);
  11034. glBindTexture(GL_TEXTURE_2D, ftex);
  11035. glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, 512, 512, 0, GL_ALPHA, GL_UNSIGNED_BYTE, temp_bitmap);
  11036. /* can free temp_bitmap at this point */
  11037. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  11038. }
  11039. void my_stbtt_print(float x, float y, char* text)
  11040. {
  11041. /* assume orthographic projection with units = screen pixels, origin at top left */
  11042. glEnable(GL_BLEND);
  11043. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  11044. glEnable(GL_TEXTURE_2D);
  11045. glBindTexture(GL_TEXTURE_2D, ftex);
  11046. glBegin(GL_QUADS);
  11047. while (*text) {
  11048. if (*text >= 32 && *text < 128) {
  11049. stbtt_aligned_quad q;
  11050. stbtt_GetBakedQuad(cdata, 512, 512, *text - 32, &x, &y, &q, 1);/* 1=opengl & d3d10+,0=d3d9 */
  11051. glTexCoord2f(q.s0, q.t0); glVertex2f(q.x0, q.y0);
  11052. glTexCoord2f(q.s1, q.t0); glVertex2f(q.x1, q.y0);
  11053. glTexCoord2f(q.s1, q.t1); glVertex2f(q.x1, q.y1);
  11054. glTexCoord2f(q.s0, q.t1); glVertex2f(q.x0, q.y1);
  11055. }
  11056. ++text;
  11057. }
  11058. glEnd();
  11059. }
  11060. #endif
  11061. /* */
  11062. /* */
  11063. /* //////////////////////////////////////////////////////////////////////////// */
  11064. /* */
  11065. /* Complete program (this compiles): get a single bitmap, print as ASCII art */
  11066. /* */
  11067. #if 0
  11068. #include <stdio.h>
  11069. #define STB_TRUETYPE_IMPLEMENTATION /* force following include to generate implementation */
  11070. #include "stb_truetype.h"
  11071. char ttf_buffer[1 << 25];
  11072. int main(int argc, char** argv)
  11073. {
  11074. stbtt_fontinfo font;
  11075. unsigned char* bitmap;
  11076. int w, h, i, j, c = (argc > 1 ? atoi(argv[1]) : 'a'), s = (argc > 2 ? atoi(argv[2]) : 20);
  11077. fread(ttf_buffer, 1, 1 << 25, fopen(argc > 3 ? argv[3] : "c:/windows/fonts/arialbd.ttf", "rb"));
  11078. stbtt_InitFont(&font, ttf_buffer, stbtt_GetFontOffsetForIndex(ttf_buffer, 0));
  11079. bitmap = stbtt_GetCodepointBitmap(&font, 0, stbtt_ScaleForPixelHeight(&font, s), c, &w, &h, 0, 0);
  11080. for (j = 0; j < h; ++j) {
  11081. for (i = 0; i < w; ++i)
  11082. putchar(" .:ioVM@"[bitmap[j * w + i] >> 5]);
  11083. putchar('\n');
  11084. }
  11085. return 0;
  11086. }
  11087. #endif
  11088. /* */
  11089. /* Output: */
  11090. /* */
  11091. /* .ii. */
  11092. /* @@@@@@. */
  11093. /* V@Mio@@o */
  11094. /* :i. V@V */
  11095. /* :oM@@M */
  11096. /* :@@@MM@M */
  11097. /* @@o o@M */
  11098. /* :@@. M@M */
  11099. /* @@@o@@@@ */
  11100. /* :M@@V:@@. */
  11101. /* */
  11102. /* //////////////////////////////////////////////////////////////////////////// */
  11103. /* */
  11104. /* Complete program: print "Hello World!" banner, with bugs */
  11105. /* */
  11106. #if 0
  11107. char buffer[24 << 20];
  11108. unsigned char screen[20][79];
  11109. int main(int arg, char** argv)
  11110. {
  11111. stbtt_fontinfo font;
  11112. int i, j, ascent, baseline, ch = 0;
  11113. float scale, xpos = 2; /* leave a little padding in case the character extends left */
  11114. char* text = "Heljo World!"; /* intentionally misspelled to show 'lj' brokenness */
  11115. fread(buffer, 1, 1000000, fopen("c:/windows/fonts/arialbd.ttf", "rb"));
  11116. stbtt_InitFont(&font, buffer, 0);
  11117. scale = stbtt_ScaleForPixelHeight(&font, 15);
  11118. stbtt_GetFontVMetrics(&font, &ascent, 0, 0);
  11119. baseline = (int)(ascent * scale);
  11120. while (text[ch]) {
  11121. int advance, lsb, x0, y0, x1, y1;
  11122. float x_shift = xpos - (float)floor(xpos);
  11123. stbtt_GetCodepointHMetrics(&font, text[ch], &advance, &lsb);
  11124. stbtt_GetCodepointBitmapBoxSubpixel(&font, text[ch], scale, scale, x_shift, 0, &x0, &y0, &x1, &y1);
  11125. stbtt_MakeCodepointBitmapSubpixel(&font, &screen[baseline + y0][(int)xpos + x0], x1 - x0, y1 - y0, 79, scale, scale, x_shift, 0, text[ch]);
  11126. /* note that this stomps the old data, so where character boxes overlap (e.g. 'lj') it's wrong */
  11127. /* because this API is really for baking character bitmaps into textures. if you want to render */
  11128. /* a sequence of characters, you really need to render each bitmap to a temp buffer, then */
  11129. /* "alpha blend" that into the working buffer */
  11130. xpos += (advance * scale);
  11131. if (text[ch + 1])
  11132. xpos += scale * stbtt_GetCodepointKernAdvance(&font, text[ch], text[ch + 1]);
  11133. ++ch;
  11134. }
  11135. for (j = 0; j < 20; ++j) {
  11136. for (i = 0; i < 78; ++i)
  11137. putchar(" .:ioVM@"[screen[j][i] >> 5]);
  11138. putchar('\n');
  11139. }
  11140. return 0;
  11141. }
  11142. #endif
  11143. /* //////////////////////////////////////////////////////////////////////////// */
  11144. /* //////////////////////////////////////////////////////////////////////////// */
  11145. /* // */
  11146. /* // INTEGRATION WITH YOUR CODEBASE */
  11147. /* // */
  11148. /* // The following sections allow you to supply alternate definitions */
  11149. /* // of C library functions used by stb_truetype, e.g. if you don't */
  11150. /* // link with the C runtime library. */
  11151. #ifdef STB_TRUETYPE_IMPLEMENTATION
  11152. /* #define your own (u)stbtt_int8/16/32 before including to override this */
  11153. #ifndef stbtt_uint8
  11154. typedef unsigned char stbtt_uint8;
  11155. typedef signed char stbtt_int8;
  11156. typedef unsigned short stbtt_uint16;
  11157. typedef signed short stbtt_int16;
  11158. typedef unsigned int stbtt_uint32;
  11159. typedef signed int stbtt_int32;
  11160. #endif
  11161. typedef char stbtt__check_size32[sizeof(stbtt_int32) == 4 ? 1 : -1];
  11162. typedef char stbtt__check_size16[sizeof(stbtt_int16) == 2 ? 1 : -1];
  11163. /* e.g. #define your own STBTT_ifloor/STBTT_iceil() to avoid math.h */
  11164. #ifndef STBTT_ifloor
  11165. #include <math.h>
  11166. #define STBTT_ifloor(x) ((int) floor(x))
  11167. #define STBTT_iceil(x) ((int) ceil(x))
  11168. #endif
  11169. #ifndef STBTT_sqrt
  11170. #include <math.h>
  11171. #define STBTT_sqrt(x) sqrt(x)
  11172. #define STBTT_pow(x,y) pow(x,y)
  11173. #endif
  11174. #ifndef STBTT_fmod
  11175. #include <math.h>
  11176. #define STBTT_fmod(x,y) fmod(x,y)
  11177. #endif
  11178. #ifndef STBTT_cos
  11179. #include <math.h>
  11180. #define STBTT_cos(x) cos(x)
  11181. #define STBTT_acos(x) acos(x)
  11182. #endif
  11183. #ifndef STBTT_fabs
  11184. #include <math.h>
  11185. #define STBTT_fabs(x) fabs(x)
  11186. #endif
  11187. /* #define your own functions "STBTT_malloc" / "STBTT_free" to avoid malloc.h */
  11188. #ifndef STBTT_malloc
  11189. #include <stdlib.h>
  11190. #define STBTT_malloc(x,u) ((void)(u),malloc(x))
  11191. #define STBTT_free(x,u) ((void)(u),free(x))
  11192. #endif
  11193. #ifndef STBTT_assert
  11194. #include <assert.h>
  11195. #define STBTT_assert(x) assert(x)
  11196. #endif
  11197. #ifndef STBTT_strlen
  11198. #include <string.h>
  11199. #define STBTT_strlen(x) strlen(x)
  11200. #endif
  11201. #ifndef STBTT_memcpy
  11202. #include <string.h>
  11203. #define STBTT_memcpy memcpy
  11204. #define STBTT_memset memset
  11205. #endif
  11206. #endif
  11207. /* ///////////////////////////////////////////////////////////////////////////// */
  11208. /* ///////////////////////////////////////////////////////////////////////////// */
  11209. /* // */
  11210. /* // INTERFACE */
  11211. /* // */
  11212. /* // */
  11213. #ifndef __STB_INCLUDE_STB_TRUETYPE_H__
  11214. #define __STB_INCLUDE_STB_TRUETYPE_H__
  11215. #ifdef STBTT_STATIC
  11216. #define STBTT_DEF static
  11217. #else
  11218. #define STBTT_DEF extern
  11219. #endif
  11220. #ifdef __cplusplus
  11221. extern "C" {
  11222. #endif
  11223. /* private structure */
  11224. typedef struct
  11225. {
  11226. unsigned char* data;
  11227. int cursor;
  11228. int size;
  11229. } stbtt__buf;
  11230. /* //////////////////////////////////////////////////////////////////////////// */
  11231. /* */
  11232. /* TEXTURE BAKING API */
  11233. /* */
  11234. /* If you use this API, you only have to call two functions ever. */
  11235. /* */
  11236. typedef struct
  11237. {
  11238. unsigned short x0, y0, x1, y1; /* coordinates of bbox in bitmap */
  11239. float xoff, yoff, xadvance;
  11240. } stbtt_bakedchar;
  11241. STBTT_DEF int stbtt_BakeFontBitmap(const unsigned char* data, int offset, /* font location (use offset=0 for plain .ttf) */
  11242. float pixel_height, /* height of font in pixels */
  11243. unsigned char* pixels, int pw, int ph, /* bitmap to be filled in */
  11244. int first_char, int num_chars, /* characters to bake */
  11245. stbtt_bakedchar* chardata); /* you allocate this, it's num_chars long */
  11246. /* if return is positive, the first unused row of the bitmap */
  11247. /* if return is negative, returns the negative of the number of characters that fit */
  11248. /* if return is 0, no characters fit and no rows were used */
  11249. /* This uses a very crappy packing. */
  11250. typedef struct
  11251. {
  11252. float x0, y0, s0, t0; /* top-left */
  11253. float x1, y1, s1, t1; /* bottom-right */
  11254. } stbtt_aligned_quad;
  11255. STBTT_DEF void stbtt_GetBakedQuad(const stbtt_bakedchar* chardata, int pw, int ph, /* same data as above */
  11256. int char_index, /* character to display */
  11257. float* xpos, float* ypos, /* pointers to current position in screen pixel space */
  11258. stbtt_aligned_quad* q, /* output: quad to draw */
  11259. int opengl_fillrule); /* true if opengl fill rule; false if DX9 or earlier */
  11260. /* Call GetBakedQuad with char_index = 'character - first_char', and it */
  11261. /* creates the quad you need to draw and advances the current position. */
  11262. /* */
  11263. /* The coordinate system used assumes y increases downwards. */
  11264. /* */
  11265. /* Characters will extend both above and below the current position; */
  11266. /* see discussion of "BASELINE" above. */
  11267. /* */
  11268. /* It's inefficient; you might want to c&p it and optimize it. */
  11269. STBTT_DEF void stbtt_GetScaledFontVMetrics(const unsigned char* fontdata, int index, float size, float* ascent, float* descent, float* lineGap);
  11270. /* Query the font vertical metrics without having to create a font first. */
  11271. /* //////////////////////////////////////////////////////////////////////////// */
  11272. /* */
  11273. /* NEW TEXTURE BAKING API */
  11274. /* */
  11275. /* This provides options for packing multiple fonts into one atlas, not */
  11276. /* perfectly but better than nothing. */
  11277. typedef struct
  11278. {
  11279. unsigned short x0, y0, x1, y1; /* coordinates of bbox in bitmap */
  11280. float xoff, yoff, xadvance;
  11281. float xoff2, yoff2;
  11282. } stbtt_packedchar;
  11283. typedef struct stbtt_pack_context stbtt_pack_context;
  11284. typedef struct stbtt_fontinfo stbtt_fontinfo;
  11285. #ifndef STB_RECT_PACK_VERSION
  11286. typedef struct stbrp_rect stbrp_rect;
  11287. #endif
  11288. STBTT_DEF int stbtt_PackBegin(stbtt_pack_context* spc, unsigned char* pixels, int width, int height, int stride_in_bytes, int padding, void* alloc_context);
  11289. /* Initializes a packing context stored in the passed-in stbtt_pack_context. */
  11290. /* Future calls using this context will pack characters into the bitmap passed */
  11291. /* in here: a 1-channel bitmap that is width * height. stride_in_bytes is */
  11292. /* the distance from one row to the next (or 0 to mean they are packed tightly */
  11293. /* together). "padding" is the amount of padding to leave between each */
  11294. /* character (normally you want '1' for bitmaps you'll use as textures with */
  11295. /* bilinear filtering). */
  11296. /* */
  11297. /* Returns 0 on failure, 1 on success. */
  11298. STBTT_DEF void stbtt_PackEnd(stbtt_pack_context* spc);
  11299. /* Cleans up the packing context and frees all memory. */
  11300. #define STBTT_POINT_SIZE(x) (-(x))
  11301. STBTT_DEF int stbtt_PackFontRange(stbtt_pack_context* spc, const unsigned char* fontdata, int font_index, float font_size,
  11302. int first_unicode_char_in_range, int num_chars_in_range, stbtt_packedchar* chardata_for_range);
  11303. /* Creates character bitmaps from the font_index'th font found in fontdata (use */
  11304. /* font_index=0 if you don't know what that is). It creates num_chars_in_range */
  11305. /* bitmaps for characters with unicode values starting at first_unicode_char_in_range */
  11306. /* and increasing. Data for how to render them is stored in chardata_for_range; */
  11307. /* pass these to stbtt_GetPackedQuad to get back renderable quads. */
  11308. /* */
  11309. /* font_size is the full height of the character from ascender to descender, */
  11310. /* as computed by stbtt_ScaleForPixelHeight. To use a point size as computed */
  11311. /* by stbtt_ScaleForMappingEmToPixels, wrap the point size in STBTT_POINT_SIZE() */
  11312. /* and pass that result as 'font_size': */
  11313. /* ..., 20 , ... // font max minus min y is 20 pixels tall */
  11314. /* ..., STBTT_POINT_SIZE(20), ... // 'M' is 20 pixels tall */
  11315. typedef struct
  11316. {
  11317. float font_size;
  11318. int first_unicode_codepoint_in_range; /* if non-zero, then the chars are continuous, and this is the first codepoint */
  11319. int* array_of_unicode_codepoints; /* if non-zero, then this is an array of unicode codepoints */
  11320. int num_chars;
  11321. stbtt_packedchar* chardata_for_range; /* output */
  11322. unsigned char h_oversample, v_oversample; /* don't set these, they're used internally */
  11323. } stbtt_pack_range;
  11324. STBTT_DEF int stbtt_PackFontRanges(stbtt_pack_context* spc, const unsigned char* fontdata, int font_index, stbtt_pack_range* ranges, int num_ranges);
  11325. /* Creates character bitmaps from multiple ranges of characters stored in */
  11326. /* ranges. This will usually create a better-packed bitmap than multiple */
  11327. /* calls to stbtt_PackFontRange. Note that you can call this multiple */
  11328. /* times within a single PackBegin/PackEnd. */
  11329. STBTT_DEF void stbtt_PackSetOversampling(stbtt_pack_context* spc, unsigned int h_oversample, unsigned int v_oversample);
  11330. /* Oversampling a font increases the quality by allowing higher-quality subpixel */
  11331. /* positioning, and is especially valuable at smaller text sizes. */
  11332. /* */
  11333. /* This function sets the amount of oversampling for all following calls to */
  11334. /* stbtt_PackFontRange(s) or stbtt_PackFontRangesGatherRects for a given */
  11335. /* pack context. The default (no oversampling) is achieved by h_oversample=1 */
  11336. /* and v_oversample=1. The total number of pixels required is */
  11337. /* h_oversample*v_oversample larger than the default; for example, 2x2 */
  11338. /* oversampling requires 4x the storage of 1x1. For best results, render */
  11339. /* oversampled textures with bilinear filtering. Look at the readme in */
  11340. /* stb/tests/oversample for information about oversampled fonts */
  11341. /* */
  11342. /* To use with PackFontRangesGather etc., you must set it before calls */
  11343. /* call to PackFontRangesGatherRects. */
  11344. STBTT_DEF void stbtt_PackSetSkipMissingCodepoints(stbtt_pack_context* spc, int skip);
  11345. /* If skip != 0, this tells stb_truetype to skip any codepoints for which */
  11346. /* there is no corresponding glyph. If skip=0, which is the default, then */
  11347. /* codepoints without a glyph recived the font's "missing character" glyph, */
  11348. /* typically an empty box by convention. */
  11349. STBTT_DEF void stbtt_GetPackedQuad(const stbtt_packedchar* chardata, int pw, int ph, /* same data as above */
  11350. int char_index, /* character to display */
  11351. float* xpos, float* ypos, /* pointers to current position in screen pixel space */
  11352. stbtt_aligned_quad* q, /* output: quad to draw */
  11353. int align_to_integer);
  11354. STBTT_DEF int stbtt_PackFontRangesGatherRects(stbtt_pack_context* spc, const stbtt_fontinfo* info, stbtt_pack_range* ranges, int num_ranges, stbrp_rect* rects);
  11355. STBTT_DEF void stbtt_PackFontRangesPackRects(stbtt_pack_context* spc, stbrp_rect* rects, int num_rects);
  11356. STBTT_DEF int stbtt_PackFontRangesRenderIntoRects(stbtt_pack_context* spc, const stbtt_fontinfo* info, stbtt_pack_range* ranges, int num_ranges, stbrp_rect* rects);
  11357. /* Calling these functions in sequence is roughly equivalent to calling */
  11358. /* stbtt_PackFontRanges(). If you more control over the packing of multiple */
  11359. /* fonts, or if you want to pack custom data into a font texture, take a look */
  11360. /* at the source to of stbtt_PackFontRanges() and create a custom version */
  11361. /* using these functions, e.g. call GatherRects multiple times, */
  11362. /* building up a single array of rects, then call PackRects once, */
  11363. /* then call RenderIntoRects repeatedly. This may result in a */
  11364. /* better packing than calling PackFontRanges multiple times */
  11365. /* (or it may not). */
  11366. /* this is an opaque structure that you shouldn't mess with which holds */
  11367. /* all the context needed from PackBegin to PackEnd. */
  11368. struct stbtt_pack_context {
  11369. void* user_allocator_context;
  11370. void* pack_info;
  11371. int width;
  11372. int height;
  11373. int stride_in_bytes;
  11374. int padding;
  11375. int skip_missing;
  11376. unsigned int h_oversample, v_oversample;
  11377. unsigned char* pixels;
  11378. void* nodes;
  11379. };
  11380. /* //////////////////////////////////////////////////////////////////////////// */
  11381. /* */
  11382. /* FONT LOADING */
  11383. /* */
  11384. /* */
  11385. STBTT_DEF int stbtt_GetNumberOfFonts(const unsigned char* data);
  11386. /* This function will determine the number of fonts in a font file. TrueType */
  11387. /* collection (.ttc) files may contain multiple fonts, while TrueType font */
  11388. /* (.ttf) files only contain one font. The number of fonts can be used for */
  11389. /* indexing with the previous function where the index is between zero and one */
  11390. /* less than the total fonts. If an error occurs, -1 is returned. */
  11391. STBTT_DEF int stbtt_GetFontOffsetForIndex(const unsigned char* data, int index);
  11392. /* Each .ttf/.ttc file may have more than one font. Each font has a sequential */
  11393. /* index number starting from 0. Call this function to get the font offset for */
  11394. /* a given index; it returns -1 if the index is out of range. A regular .ttf */
  11395. /* file will only define one font and it always be at offset 0, so it will */
  11396. /* return '0' for index 0, and -1 for all other indices. */
  11397. /* The following structure is defined publicly so you can declare one on */
  11398. /* the stack or as a global or etc, but you should treat it as opaque. */
  11399. struct stbtt_fontinfo
  11400. {
  11401. void* userdata;
  11402. unsigned char* data; /* pointer to .ttf file */
  11403. int fontstart; /* offset of start of font */
  11404. int numGlyphs; /* number of glyphs, needed for range checking */
  11405. int loca, head, glyf, hhea, hmtx, kern, gpos, svg; /* table locations as offset from start of .ttf */
  11406. int index_map; /* a cmap mapping for our chosen character encoding */
  11407. int indexToLocFormat; /* format needed to map from glyph index to glyph */
  11408. stbtt__buf cff; /* cff font data */
  11409. stbtt__buf charstrings; /* the charstring index */
  11410. stbtt__buf gsubrs; /* global charstring subroutines index */
  11411. stbtt__buf subrs; /* private charstring subroutines index */
  11412. stbtt__buf fontdicts; /* array of font dicts */
  11413. stbtt__buf fdselect; /* map from glyph to fontdict */
  11414. };
  11415. STBTT_DEF int stbtt_InitFont(stbtt_fontinfo* info, const unsigned char* data, int offset);
  11416. /* Given an offset into the file that defines a font, this function builds */
  11417. /* the necessary cached info for the rest of the system. You must allocate */
  11418. /* the stbtt_fontinfo yourself, and stbtt_InitFont will fill it out. You don't */
  11419. /* need to do anything special to free it, because the contents are pure */
  11420. /* value data with no additional data structures. Returns 0 on failure. */
  11421. /* //////////////////////////////////////////////////////////////////////////// */
  11422. /* */
  11423. /* CHARACTER TO GLYPH-INDEX CONVERSIOn */
  11424. STBTT_DEF int stbtt_FindGlyphIndex(const stbtt_fontinfo* info, int unicode_codepoint);
  11425. /* If you're going to perform multiple operations on the same character */
  11426. /* and you want a speed-up, call this function with the character you're */
  11427. /* going to process, then use glyph-based functions instead of the */
  11428. /* codepoint-based functions. */
  11429. /* Returns 0 if the character codepoint is not defined in the font. */
  11430. /* //////////////////////////////////////////////////////////////////////////// */
  11431. /* */
  11432. /* CHARACTER PROPERTIES */
  11433. /* */
  11434. STBTT_DEF float stbtt_ScaleForPixelHeight(const stbtt_fontinfo* info, float pixels);
  11435. /* computes a scale factor to produce a font whose "height" is 'pixels' tall. */
  11436. /* Height is measured as the distance from the highest ascender to the lowest */
  11437. /* descender; in other words, it's equivalent to calling stbtt_GetFontVMetrics */
  11438. /* and computing: */
  11439. /* scale = pixels / (ascent - descent) */
  11440. /* so if you prefer to measure height by the ascent only, use a similar calculation. */
  11441. STBTT_DEF float stbtt_ScaleForMappingEmToPixels(const stbtt_fontinfo* info, float pixels);
  11442. /* computes a scale factor to produce a font whose EM size is mapped to */
  11443. /* 'pixels' tall. This is probably what traditional APIs compute, but */
  11444. /* I'm not positive. */
  11445. STBTT_DEF void stbtt_GetFontVMetrics(const stbtt_fontinfo* info, int* ascent, int* descent, int* lineGap);
  11446. /* ascent is the coordinate above the baseline the font extends; descent */
  11447. /* is the coordinate below the baseline the font extends (i.e. it is typically negative) */
  11448. /* lineGap is the spacing between one row's descent and the next row's ascent... */
  11449. /* so you should advance the vertical position by "*ascent - *descent + *lineGap" */
  11450. /* these are expressed in unscaled coordinates, so you must multiply by */
  11451. /* the scale factor for a given size */
  11452. STBTT_DEF int stbtt_GetFontVMetricsOS2(const stbtt_fontinfo* info, int* typoAscent, int* typoDescent, int* typoLineGap);
  11453. /* analogous to GetFontVMetrics, but returns the "typographic" values from the OS/2 */
  11454. /* table (specific to MS/Windows TTF files). */
  11455. /* */
  11456. /* Returns 1 on success (table present), 0 on failure. */
  11457. STBTT_DEF void stbtt_GetFontBoundingBox(const stbtt_fontinfo* info, int* x0, int* y0, int* x1, int* y1);
  11458. /* the bounding box around all possible characters */
  11459. STBTT_DEF void stbtt_GetCodepointHMetrics(const stbtt_fontinfo* info, int codepoint, int* advanceWidth, int* leftSideBearing);
  11460. /* leftSideBearing is the offset from the current horizontal position to the left edge of the character */
  11461. /* advanceWidth is the offset from the current horizontal position to the next horizontal position */
  11462. /* these are expressed in unscaled coordinates */
  11463. STBTT_DEF int stbtt_GetCodepointKernAdvance(const stbtt_fontinfo* info, int ch1, int ch2);
  11464. /* an additional amount to add to the 'advance' value between ch1 and ch2 */
  11465. STBTT_DEF int stbtt_GetCodepointBox(const stbtt_fontinfo* info, int codepoint, int* x0, int* y0, int* x1, int* y1);
  11466. /* Gets the bounding box of the visible part of the glyph, in unscaled coordinates */
  11467. STBTT_DEF void stbtt_GetGlyphHMetrics(const stbtt_fontinfo* info, int glyph_index, int* advanceWidth, int* leftSideBearing);
  11468. STBTT_DEF int stbtt_GetGlyphKernAdvance(const stbtt_fontinfo* info, int glyph1, int glyph2);
  11469. STBTT_DEF int stbtt_GetGlyphBox(const stbtt_fontinfo* info, int glyph_index, int* x0, int* y0, int* x1, int* y1);
  11470. /* as above, but takes one or more glyph indices for greater efficiency */
  11471. typedef struct stbtt_kerningentry
  11472. {
  11473. int glyph1; /* use stbtt_FindGlyphIndex */
  11474. int glyph2;
  11475. int advance;
  11476. } stbtt_kerningentry;
  11477. STBTT_DEF int stbtt_GetKerningTableLength(const stbtt_fontinfo* info);
  11478. STBTT_DEF int stbtt_GetKerningTable(const stbtt_fontinfo* info, stbtt_kerningentry* table, int table_length);
  11479. /* Retrieves a complete list of all of the kerning pairs provided by the font */
  11480. /* stbtt_GetKerningTable never writes more than table_length entries and returns how many entries it did write. */
  11481. /* The table will be sorted by (a.glyph1 == b.glyph1)?(a.glyph2 < b.glyph2):(a.glyph1 < b.glyph1) */
  11482. /* //////////////////////////////////////////////////////////////////////////// */
  11483. /* */
  11484. /* GLYPH SHAPES (you probably don't need these, but they have to go before */
  11485. /* the bitmaps for C declaration-order reasons) */
  11486. /* */
  11487. #ifndef STBTT_vmove /* you can predefine these to use different values (but why?) */
  11488. enum {
  11489. STBTT_vmove = 1,
  11490. STBTT_vline,
  11491. STBTT_vcurve,
  11492. STBTT_vcubic
  11493. };
  11494. #endif
  11495. #ifndef stbtt_vertex /* you can predefine this to use different values */
  11496. /* (we share this with other code at RAD) */
  11497. #define stbtt_vertex_type short /* can't use stbtt_int16 because that's not visible in the header file */
  11498. typedef struct
  11499. {
  11500. stbtt_vertex_type x, y, cx, cy, cx1, cy1;
  11501. unsigned char type, padding;
  11502. } stbtt_vertex;
  11503. #endif
  11504. STBTT_DEF int stbtt_IsGlyphEmpty(const stbtt_fontinfo* info, int glyph_index);
  11505. /* returns non-zero if nothing is drawn for this glyph */
  11506. STBTT_DEF int stbtt_GetCodepointShape(const stbtt_fontinfo* info, int unicode_codepoint, stbtt_vertex** vertices);
  11507. STBTT_DEF int stbtt_GetGlyphShape(const stbtt_fontinfo* info, int glyph_index, stbtt_vertex** vertices);
  11508. /* returns # of vertices and fills *vertices with the pointer to them */
  11509. /* these are expressed in "unscaled" coordinates */
  11510. /* */
  11511. /* The shape is a series of contours. Each one starts with */
  11512. /* a STBTT_moveto, then consists of a series of mixed */
  11513. /* STBTT_lineto and STBTT_curveto segments. A lineto */
  11514. /* draws a line from previous endpoint to its x,y; a curveto */
  11515. /* draws a quadratic bezier from previous endpoint to */
  11516. /* its x,y, using cx,cy as the bezier control point. */
  11517. STBTT_DEF void stbtt_FreeShape(const stbtt_fontinfo* info, stbtt_vertex* vertices);
  11518. /* frees the data allocated above */
  11519. STBTT_DEF unsigned char* stbtt_FindSVGDoc(const stbtt_fontinfo* info, int gl);
  11520. STBTT_DEF int stbtt_GetCodepointSVG(const stbtt_fontinfo* info, int unicode_codepoint, const char** svg);
  11521. STBTT_DEF int stbtt_GetGlyphSVG(const stbtt_fontinfo* info, int gl, const char** svg);
  11522. /* fills svg with the character's SVG data. */
  11523. /* returns data size or 0 if SVG not found. */
  11524. /* //////////////////////////////////////////////////////////////////////////// */
  11525. /* */
  11526. /* BITMAP RENDERING */
  11527. /* */
  11528. STBTT_DEF void stbtt_FreeBitmap(unsigned char* bitmap, void* userdata);
  11529. /* frees the bitmap allocated below */
  11530. STBTT_DEF unsigned char* stbtt_GetCodepointBitmap(const stbtt_fontinfo* info, float scale_x, float scale_y, int codepoint, int* width, int* height, int* xoff, int* yoff);
  11531. /* allocates a large-enough single-channel 8bpp bitmap and renders the */
  11532. /* specified character/glyph at the specified scale into it, with */
  11533. /* antialiasing. 0 is no coverage (transparent), 255 is fully covered (opaque). */
  11534. /* *width & *height are filled out with the width & height of the bitmap, */
  11535. /* which is stored left-to-right, top-to-bottom. */
  11536. /* */
  11537. /* xoff/yoff are the offset it pixel space from the glyph origin to the top-left of the bitmap */
  11538. STBTT_DEF unsigned char* stbtt_GetCodepointBitmapSubpixel(const stbtt_fontinfo* info, float scale_x, float scale_y, float shift_x, float shift_y, int codepoint, int* width, int* height, int* xoff, int* yoff);
  11539. /* the same as stbtt_GetCodepoitnBitmap, but you can specify a subpixel */
  11540. /* shift for the character */
  11541. STBTT_DEF void stbtt_MakeCodepointBitmap(const stbtt_fontinfo* info, unsigned char* output, int out_w, int out_h, int out_stride, float scale_x, float scale_y, int codepoint);
  11542. /* the same as stbtt_GetCodepointBitmap, but you pass in storage for the bitmap */
  11543. /* in the form of 'output', with row spacing of 'out_stride' bytes. the bitmap */
  11544. /* is clipped to out_w/out_h bytes. Call stbtt_GetCodepointBitmapBox to get the */
  11545. /* width and height and positioning info for it first. */
  11546. STBTT_DEF void stbtt_MakeCodepointBitmapSubpixel(const stbtt_fontinfo* info, unsigned char* output, int out_w, int out_h, int out_stride, float scale_x, float scale_y, float shift_x, float shift_y, int codepoint);
  11547. /* same as stbtt_MakeCodepointBitmap, but you can specify a subpixel */
  11548. /* shift for the character */
  11549. STBTT_DEF void stbtt_MakeCodepointBitmapSubpixelPrefilter(const stbtt_fontinfo* info, unsigned char* output, int out_w, int out_h, int out_stride, float scale_x, float scale_y, float shift_x, float shift_y, int oversample_x, int oversample_y, float* sub_x, float* sub_y, int codepoint);
  11550. /* same as stbtt_MakeCodepointBitmapSubpixel, but prefiltering */
  11551. /* is performed (see stbtt_PackSetOversampling) */
  11552. STBTT_DEF void stbtt_GetCodepointBitmapBox(const stbtt_fontinfo* font, int codepoint, float scale_x, float scale_y, int* ix0, int* iy0, int* ix1, int* iy1);
  11553. /* get the bbox of the bitmap centered around the glyph origin; so the */
  11554. /* bitmap width is ix1-ix0, height is iy1-iy0, and location to place */
  11555. /* the bitmap top left is (leftSideBearing*scale,iy0). */
  11556. /* (Note that the bitmap uses y-increases-down, but the shape uses */
  11557. /* y-increases-up, so CodepointBitmapBox and CodepointBox are inverted.) */
  11558. STBTT_DEF void stbtt_GetCodepointBitmapBoxSubpixel(const stbtt_fontinfo* font, int codepoint, float scale_x, float scale_y, float shift_x, float shift_y, int* ix0, int* iy0, int* ix1, int* iy1);
  11559. /* same as stbtt_GetCodepointBitmapBox, but you can specify a subpixel */
  11560. /* shift for the character */
  11561. /* the following functions are equivalent to the above functions, but operate */
  11562. /* on glyph indices instead of Unicode codepoints (for efficiency) */
  11563. STBTT_DEF unsigned char* stbtt_GetGlyphBitmap(const stbtt_fontinfo* info, float scale_x, float scale_y, int glyph, int* width, int* height, int* xoff, int* yoff);
  11564. STBTT_DEF unsigned char* stbtt_GetGlyphBitmapSubpixel(const stbtt_fontinfo* info, float scale_x, float scale_y, float shift_x, float shift_y, int glyph, int* width, int* height, int* xoff, int* yoff);
  11565. STBTT_DEF void stbtt_MakeGlyphBitmap(const stbtt_fontinfo* info, unsigned char* output, int out_w, int out_h, int out_stride, float scale_x, float scale_y, int glyph);
  11566. STBTT_DEF void stbtt_MakeGlyphBitmapSubpixel(const stbtt_fontinfo* info, unsigned char* output, int out_w, int out_h, int out_stride, float scale_x, float scale_y, float shift_x, float shift_y, int glyph);
  11567. STBTT_DEF void stbtt_MakeGlyphBitmapSubpixelPrefilter(const stbtt_fontinfo* info, unsigned char* output, int out_w, int out_h, int out_stride, float scale_x, float scale_y, float shift_x, float shift_y, int oversample_x, int oversample_y, float* sub_x, float* sub_y, int glyph);
  11568. STBTT_DEF void stbtt_GetGlyphBitmapBox(const stbtt_fontinfo* font, int glyph, float scale_x, float scale_y, int* ix0, int* iy0, int* ix1, int* iy1);
  11569. STBTT_DEF void stbtt_GetGlyphBitmapBoxSubpixel(const stbtt_fontinfo* font, int glyph, float scale_x, float scale_y, float shift_x, float shift_y, int* ix0, int* iy0, int* ix1, int* iy1);
  11570. /* @TODO: don't expose this structure */
  11571. typedef struct
  11572. {
  11573. int w, h, stride;
  11574. unsigned char* pixels;
  11575. } stbtt__bitmap;
  11576. /* rasterize a shape with quadratic beziers into a bitmap */
  11577. STBTT_DEF void stbtt_Rasterize(stbtt__bitmap* result, /* 1-channel bitmap to draw into */
  11578. float flatness_in_pixels, /* allowable error of curve in pixels */
  11579. stbtt_vertex* vertices, /* array of vertices defining shape */
  11580. int num_verts, /* number of vertices in above array */
  11581. float scale_x, float scale_y, /* scale applied to input vertices */
  11582. float shift_x, float shift_y, /* translation applied to input vertices */
  11583. int x_off, int y_off, /* another translation applied to input */
  11584. int invert, /* if non-zero, vertically flip shape */
  11585. void* userdata); /* context for to STBTT_MALLOC */
  11586. /* //////////////////////////////////////////////////////////////////////////// */
  11587. /* */
  11588. /* Signed Distance Function (or Field) rendering */
  11589. STBTT_DEF void stbtt_FreeSDF(unsigned char* bitmap, void* userdata);
  11590. /* frees the SDF bitmap allocated below */
  11591. STBTT_DEF unsigned char* stbtt_GetGlyphSDF(const stbtt_fontinfo* info, float scale, int glyph, int padding, unsigned char onedge_value, float pixel_dist_scale, int* width, int* height, int* xoff, int* yoff);
  11592. STBTT_DEF unsigned char* stbtt_GetCodepointSDF(const stbtt_fontinfo* info, float scale, int codepoint, int padding, unsigned char onedge_value, float pixel_dist_scale, int* width, int* height, int* xoff, int* yoff);
  11593. /* These functions compute a discretized SDF field for a single character, suitable for storing */
  11594. /* in a single-channel texture, sampling with bilinear filtering, and testing against */
  11595. /* larger than some threshold to produce scalable fonts. */
  11596. /* info -- the font */
  11597. /* scale -- controls the size of the resulting SDF bitmap, same as it would be creating a regular bitmap */
  11598. /* glyph/codepoint -- the character to generate the SDF for */
  11599. /* padding -- extra "pixels" around the character which are filled with the distance to the character (not 0), */
  11600. /* which allows effects like bit outlines */
  11601. /* onedge_value -- value 0-255 to test the SDF against to reconstruct the character (i.e. the isocontour of the character) */
  11602. /* pixel_dist_scale -- what value the SDF should increase by when moving one SDF "pixel" away from the edge (on the 0..255 scale) */
  11603. /* if positive, > onedge_value is inside; if negative, < onedge_value is inside */
  11604. /* width,height -- output height & width of the SDF bitmap (including padding) */
  11605. /* xoff,yoff -- output origin of the character */
  11606. /* return value -- a 2D array of bytes 0..255, width*height in size */
  11607. /* */
  11608. /* pixel_dist_scale & onedge_value are a scale & bias that allows you to make */
  11609. /* optimal use of the limited 0..255 for your application, trading off precision */
  11610. /* and special effects. SDF values outside the range 0..255 are clamped to 0..255. */
  11611. /* */
  11612. /* Example: */
  11613. /* scale = stbtt_ScaleForPixelHeight(22) */
  11614. /* padding = 5 */
  11615. /* onedge_value = 180 */
  11616. /* pixel_dist_scale = 180/5.0 = 36.0 */
  11617. /* */
  11618. /* This will create an SDF bitmap in which the character is about 22 pixels */
  11619. /* high but the whole bitmap is about 22+5+5=32 pixels high. To produce a filled */
  11620. /* shape, sample the SDF at each pixel and fill the pixel if the SDF value */
  11621. /* is greater than or equal to 180/255. (You'll actually want to antialias, */
  11622. /* which is beyond the scope of this example.) Additionally, you can compute */
  11623. /* offset outlines (e.g. to stroke the character border inside & outside, */
  11624. /* or only outside). For example, to fill outside the character up to 3 SDF */
  11625. /* pixels, you would compare against (180-36.0*3)/255 = 72/255. The above */
  11626. /* choice of variables maps a range from 5 pixels outside the shape to */
  11627. /* 2 pixels inside the shape to 0..255; this is intended primarily for apply */
  11628. /* outside effects only (the interior range is needed to allow proper */
  11629. /* antialiasing of the font at *smaller* sizes) */
  11630. /* */
  11631. /* The function computes the SDF analytically at each SDF pixel, not by e.g. */
  11632. /* building a higher-res bitmap and approximating it. In theory the quality */
  11633. /* should be as high as possible for an SDF of this size & representation, but */
  11634. /* unclear if this is true in practice (perhaps building a higher-res bitmap */
  11635. /* and computing from that can allow drop-out prevention). */
  11636. /* */
  11637. /* The algorithm has not been optimized at all, so expect it to be slow */
  11638. /* if computing lots of characters or very large sizes. */
  11639. /* //////////////////////////////////////////////////////////////////////////// */
  11640. /* */
  11641. /* Finding the right font... */
  11642. /* */
  11643. /* You should really just solve this offline, keep your own tables */
  11644. /* of what font is what, and don't try to get it out of the .ttf file. */
  11645. /* That's because getting it out of the .ttf file is really hard, because */
  11646. /* the names in the file can appear in many possible encodings, in many */
  11647. /* possible languages, and e.g. if you need a case-insensitive comparison, */
  11648. /* the details of that depend on the encoding & language in a complex way */
  11649. /* (actually underspecified in truetype, but also gigantic). */
  11650. /* */
  11651. /* But you can use the provided functions in two possible ways: */
  11652. /* stbtt_FindMatchingFont() will use *case-sensitive* comparisons on */
  11653. /* unicode-encoded names to try to find the font you want; */
  11654. /* you can run this before calling stbtt_InitFont() */
  11655. /* */
  11656. /* stbtt_GetFontNameString() lets you get any of the various strings */
  11657. /* from the file yourself and do your own comparisons on them. */
  11658. /* You have to have called stbtt_InitFont() first. */
  11659. STBTT_DEF int stbtt_FindMatchingFont(const unsigned char* fontdata, const char* name, int flags);
  11660. /* returns the offset (not index) of the font that matches, or -1 if none */
  11661. /* if you use STBTT_MACSTYLE_DONTCARE, use a font name like "Arial Bold". */
  11662. /* if you use any other flag, use a font name like "Arial"; this checks */
  11663. /* the 'macStyle' header field; i don't know if fonts set this consistently */
  11664. #define STBTT_MACSTYLE_DONTCARE 0
  11665. #define STBTT_MACSTYLE_BOLD 1
  11666. #define STBTT_MACSTYLE_ITALIC 2
  11667. #define STBTT_MACSTYLE_UNDERSCORE 4
  11668. #define STBTT_MACSTYLE_NONE 8 /* <= not same as 0, this makes us check the bitfield is 0 */
  11669. STBTT_DEF int stbtt_CompareUTF8toUTF16_bigendian(const char* s1, int len1, const char* s2, int len2);
  11670. /* returns 1/0 whether the first string interpreted as utf8 is identical to */
  11671. /* the second string interpreted as big-endian utf16... useful for strings from next func */
  11672. STBTT_DEF const char* stbtt_GetFontNameString(const stbtt_fontinfo* font, int* length, int platformID, int encodingID, int languageID, int nameID);
  11673. /* returns the string (which may be big-endian double byte, e.g. for unicode) */
  11674. /* and puts the length in bytes in *length. */
  11675. /* */
  11676. /* some of the values for the IDs are below; for more see the truetype spec: */
  11677. /* http://developer.apple.com/textfonts/TTRefMan/RM06/Chap6name.html */
  11678. /* http://www.microsoft.com/typography/otspec/name.htm */
  11679. enum { /* platformID */
  11680. STBTT_PLATFORM_ID_UNICODE = 0,
  11681. STBTT_PLATFORM_ID_MAC = 1,
  11682. STBTT_PLATFORM_ID_ISO = 2,
  11683. STBTT_PLATFORM_ID_MICROSOFT = 3
  11684. };
  11685. enum { /* encodingID for STBTT_PLATFORM_ID_UNICODE */
  11686. STBTT_UNICODE_EID_UNICODE_1_0 = 0,
  11687. STBTT_UNICODE_EID_UNICODE_1_1 = 1,
  11688. STBTT_UNICODE_EID_ISO_10646 = 2,
  11689. STBTT_UNICODE_EID_UNICODE_2_0_BMP = 3,
  11690. STBTT_UNICODE_EID_UNICODE_2_0_FULL = 4
  11691. };
  11692. enum { /* encodingID for STBTT_PLATFORM_ID_MICROSOFT */
  11693. STBTT_MS_EID_SYMBOL = 0,
  11694. STBTT_MS_EID_UNICODE_BMP = 1,
  11695. STBTT_MS_EID_SHIFTJIS = 2,
  11696. STBTT_MS_EID_UNICODE_FULL = 10
  11697. };
  11698. enum { /* encodingID for STBTT_PLATFORM_ID_MAC; same as Script Manager codes */
  11699. STBTT_MAC_EID_ROMAN = 0, STBTT_MAC_EID_ARABIC = 4,
  11700. STBTT_MAC_EID_JAPANESE = 1, STBTT_MAC_EID_HEBREW = 5,
  11701. STBTT_MAC_EID_CHINESE_TRAD = 2, STBTT_MAC_EID_GREEK = 6,
  11702. STBTT_MAC_EID_KOREAN = 3, STBTT_MAC_EID_RUSSIAN = 7
  11703. };
  11704. enum { /* languageID for STBTT_PLATFORM_ID_MICROSOFT; same as LCID... */
  11705. /* problematic because there are e.g. 16 english LCIDs and 16 arabic LCIDs */
  11706. STBTT_MS_LANG_ENGLISH = 0x0409, STBTT_MS_LANG_ITALIAN = 0x0410,
  11707. STBTT_MS_LANG_CHINESE = 0x0804, STBTT_MS_LANG_JAPANESE = 0x0411,
  11708. STBTT_MS_LANG_DUTCH = 0x0413, STBTT_MS_LANG_KOREAN = 0x0412,
  11709. STBTT_MS_LANG_FRENCH = 0x040c, STBTT_MS_LANG_RUSSIAN = 0x0419,
  11710. STBTT_MS_LANG_GERMAN = 0x0407, STBTT_MS_LANG_SPANISH = 0x0409,
  11711. STBTT_MS_LANG_HEBREW = 0x040d, STBTT_MS_LANG_SWEDISH = 0x041D
  11712. };
  11713. enum { /* languageID for STBTT_PLATFORM_ID_MAC */
  11714. STBTT_MAC_LANG_ENGLISH = 0, STBTT_MAC_LANG_JAPANESE = 11,
  11715. STBTT_MAC_LANG_ARABIC = 12, STBTT_MAC_LANG_KOREAN = 23,
  11716. STBTT_MAC_LANG_DUTCH = 4, STBTT_MAC_LANG_RUSSIAN = 32,
  11717. STBTT_MAC_LANG_FRENCH = 1, STBTT_MAC_LANG_SPANISH = 6,
  11718. STBTT_MAC_LANG_GERMAN = 2, STBTT_MAC_LANG_SWEDISH = 5,
  11719. STBTT_MAC_LANG_HEBREW = 10, STBTT_MAC_LANG_CHINESE_SIMPLIFIED = 33,
  11720. STBTT_MAC_LANG_ITALIAN = 3, STBTT_MAC_LANG_CHINESE_TRAD = 19
  11721. };
  11722. #ifdef __cplusplus
  11723. }
  11724. #endif
  11725. #endif /* __STB_INCLUDE_STB_TRUETYPE_H__ */
  11726. /* ///////////////////////////////////////////////////////////////////////////// */
  11727. /* ///////////////////////////////////////////////////////////////////////////// */
  11728. /* // */
  11729. /* // IMPLEMENTATION */
  11730. /* // */
  11731. /* // */
  11732. #ifdef STB_TRUETYPE_IMPLEMENTATION
  11733. #ifndef STBTT_MAX_OVERSAMPLE
  11734. #define STBTT_MAX_OVERSAMPLE 8
  11735. #endif
  11736. #if STBTT_MAX_OVERSAMPLE > 255
  11737. #error "STBTT_MAX_OVERSAMPLE cannot be > 255"
  11738. #endif
  11739. typedef int stbtt__test_oversample_pow2[(STBTT_MAX_OVERSAMPLE & (STBTT_MAX_OVERSAMPLE - 1)) == 0 ? 1 : -1];
  11740. #ifndef STBTT_RASTERIZER_VERSION
  11741. #define STBTT_RASTERIZER_VERSION 2
  11742. #endif
  11743. #ifdef _MSC_VER
  11744. #define STBTT__NOTUSED(v) (void)(v)
  11745. #else
  11746. #define STBTT__NOTUSED(v) (void)sizeof(v)
  11747. #endif
  11748. /* //////////////////////////////////////////////////////////////////////// */
  11749. /* */
  11750. /* stbtt__buf helpers to parse data from file */
  11751. /* */
  11752. static stbtt_uint8 stbtt__buf_get8(stbtt__buf* b)
  11753. {
  11754. if (b->cursor >= b->size)
  11755. return 0;
  11756. return b->data[b->cursor++];
  11757. }
  11758. static stbtt_uint8 stbtt__buf_peek8(stbtt__buf* b)
  11759. {
  11760. if (b->cursor >= b->size)
  11761. return 0;
  11762. return b->data[b->cursor];
  11763. }
  11764. static void stbtt__buf_seek(stbtt__buf* b, int o)
  11765. {
  11766. STBTT_assert(!(o > b->size || o < 0));
  11767. b->cursor = (o > b->size || o < 0) ? b->size : o;
  11768. }
  11769. static void stbtt__buf_skip(stbtt__buf* b, int o)
  11770. {
  11771. stbtt__buf_seek(b, b->cursor + o);
  11772. }
  11773. static stbtt_uint32 stbtt__buf_get(stbtt__buf* b, int n)
  11774. {
  11775. stbtt_uint32 v = 0;
  11776. int i;
  11777. STBTT_assert(n >= 1 && n <= 4);
  11778. for (i = 0; i < n; i++)
  11779. v = (v << 8) | stbtt__buf_get8(b);
  11780. return v;
  11781. }
  11782. static stbtt__buf stbtt__new_buf(const void* p, size_t size)
  11783. {
  11784. stbtt__buf r;
  11785. STBTT_assert(size < 0x40000000);
  11786. r.data = (stbtt_uint8*)p;
  11787. r.size = (int)size;
  11788. r.cursor = 0;
  11789. return r;
  11790. }
  11791. #define stbtt__buf_get16(b) stbtt__buf_get((b), 2)
  11792. #define stbtt__buf_get32(b) stbtt__buf_get((b), 4)
  11793. static stbtt__buf stbtt__buf_range(const stbtt__buf* b, int o, int s)
  11794. {
  11795. stbtt__buf r = stbtt__new_buf(NULL, 0);
  11796. if (o < 0 || s < 0 || o > b->size || s > b->size - o) return r;
  11797. r.data = b->data + o;
  11798. r.size = s;
  11799. return r;
  11800. }
  11801. static stbtt__buf stbtt__cff_get_index(stbtt__buf* b)
  11802. {
  11803. int count, start, offsize;
  11804. start = b->cursor;
  11805. count = stbtt__buf_get16(b);
  11806. if (count) {
  11807. offsize = stbtt__buf_get8(b);
  11808. STBTT_assert(offsize >= 1 && offsize <= 4);
  11809. stbtt__buf_skip(b, offsize * count);
  11810. stbtt__buf_skip(b, stbtt__buf_get(b, offsize) - 1);
  11811. }
  11812. return stbtt__buf_range(b, start, b->cursor - start);
  11813. }
  11814. static stbtt_uint32 stbtt__cff_int(stbtt__buf* b)
  11815. {
  11816. int b0 = stbtt__buf_get8(b);
  11817. if (b0 >= 32 && b0 <= 246) return b0 - 139;
  11818. else if (b0 >= 247 && b0 <= 250) return (b0 - 247) * 256 + stbtt__buf_get8(b) + 108;
  11819. else if (b0 >= 251 && b0 <= 254) return -(b0 - 251) * 256 - stbtt__buf_get8(b) - 108;
  11820. else if (b0 == 28) return stbtt__buf_get16(b);
  11821. else if (b0 == 29) return stbtt__buf_get32(b);
  11822. STBTT_assert(0);
  11823. return 0;
  11824. }
  11825. static void stbtt__cff_skip_operand(stbtt__buf* b) {
  11826. int v, b0 = stbtt__buf_peek8(b);
  11827. STBTT_assert(b0 >= 28);
  11828. if (b0 == 30) {
  11829. stbtt__buf_skip(b, 1);
  11830. while (b->cursor < b->size) {
  11831. v = stbtt__buf_get8(b);
  11832. if ((v & 0xF) == 0xF || (v >> 4) == 0xF)
  11833. break;
  11834. }
  11835. }
  11836. else {
  11837. stbtt__cff_int(b);
  11838. }
  11839. }
  11840. static stbtt__buf stbtt__dict_get(stbtt__buf* b, int key)
  11841. {
  11842. stbtt__buf_seek(b, 0);
  11843. while (b->cursor < b->size) {
  11844. int start = b->cursor, end, op;
  11845. while (stbtt__buf_peek8(b) >= 28)
  11846. stbtt__cff_skip_operand(b);
  11847. end = b->cursor;
  11848. op = stbtt__buf_get8(b);
  11849. if (op == 12) op = stbtt__buf_get8(b) | 0x100;
  11850. if (op == key) return stbtt__buf_range(b, start, end - start);
  11851. }
  11852. return stbtt__buf_range(b, 0, 0);
  11853. }
  11854. static void stbtt__dict_get_ints(stbtt__buf* b, int key, int outcount, stbtt_uint32* out)
  11855. {
  11856. int i;
  11857. stbtt__buf operands = stbtt__dict_get(b, key);
  11858. for (i = 0; i < outcount && operands.cursor < operands.size; i++)
  11859. out[i] = stbtt__cff_int(&operands);
  11860. }
  11861. static int stbtt__cff_index_count(stbtt__buf* b)
  11862. {
  11863. stbtt__buf_seek(b, 0);
  11864. return stbtt__buf_get16(b);
  11865. }
  11866. static stbtt__buf stbtt__cff_index_get(stbtt__buf b, int i)
  11867. {
  11868. int count, offsize, start, end;
  11869. stbtt__buf_seek(&b, 0);
  11870. count = stbtt__buf_get16(&b);
  11871. offsize = stbtt__buf_get8(&b);
  11872. STBTT_assert(i >= 0 && i < count);
  11873. STBTT_assert(offsize >= 1 && offsize <= 4);
  11874. stbtt__buf_skip(&b, i * offsize);
  11875. start = stbtt__buf_get(&b, offsize);
  11876. end = stbtt__buf_get(&b, offsize);
  11877. return stbtt__buf_range(&b, 2 + (count + 1) * offsize + start, end - start);
  11878. }
  11879. /* //////////////////////////////////////////////////////////////////////// */
  11880. /* */
  11881. /* accessors to parse data from file */
  11882. /* */
  11883. /* on platforms that don't allow misaligned reads, if we want to allow */
  11884. /* truetype fonts that aren't padded to alignment, define ALLOW_UNALIGNED_TRUETYPE */
  11885. #define ttBYTE(p) (* (stbtt_uint8 *) (p))
  11886. #define ttCHAR(p) (* (stbtt_int8 *) (p))
  11887. #define ttFixed(p) ttLONG(p)
  11888. static stbtt_uint16 ttUSHORT(stbtt_uint8* p) { return p[0] * 256 + p[1]; }
  11889. static stbtt_int16 ttSHORT(stbtt_uint8* p) { return p[0] * 256 + p[1]; }
  11890. static stbtt_uint32 ttULONG(stbtt_uint8* p) { return (p[0] << 24) + (p[1] << 16) + (p[2] << 8) + p[3]; }
  11891. static stbtt_int32 ttLONG(stbtt_uint8* p) { return (p[0] << 24) + (p[1] << 16) + (p[2] << 8) + p[3]; }
  11892. #define stbtt_tag4(p,c0,c1,c2,c3) ((p)[0] == (c0) && (p)[1] == (c1) && (p)[2] == (c2) && (p)[3] == (c3))
  11893. #define stbtt_tag(p,str) stbtt_tag4(p,str[0],str[1],str[2],str[3])
  11894. static int stbtt__isfont(stbtt_uint8* font)
  11895. {
  11896. /* check the version number */
  11897. if (stbtt_tag4(font, '1', 0, 0, 0)) return 1; /* TrueType 1 */
  11898. if (stbtt_tag(font, "typ1")) return 1; /* TrueType with type 1 font -- we don't support this! */
  11899. if (stbtt_tag(font, "OTTO")) return 1; /* OpenType with CFF */
  11900. if (stbtt_tag4(font, 0, 1, 0, 0)) return 1; /* OpenType 1.0 */
  11901. if (stbtt_tag(font, "true")) return 1; /* Apple specification for TrueType fonts */
  11902. return 0;
  11903. }
  11904. /* @OPTIMIZE: binary search */
  11905. static stbtt_uint32 stbtt__find_table(stbtt_uint8* data, stbtt_uint32 fontstart, const char* tag)
  11906. {
  11907. stbtt_int32 num_tables = ttUSHORT(data + fontstart + 4);
  11908. stbtt_uint32 tabledir = fontstart + 12;
  11909. stbtt_int32 i;
  11910. for (i = 0; i < num_tables; ++i) {
  11911. stbtt_uint32 loc = tabledir + 16 * i;
  11912. if (stbtt_tag(data + loc + 0, tag))
  11913. return ttULONG(data + loc + 8);
  11914. }
  11915. return 0;
  11916. }
  11917. static int stbtt_GetFontOffsetForIndex_internal(unsigned char* font_collection, int index)
  11918. {
  11919. /* if it's just a font, there's only one valid index */
  11920. if (stbtt__isfont(font_collection))
  11921. return index == 0 ? 0 : -1;
  11922. /* check if it's a TTC */
  11923. if (stbtt_tag(font_collection, "ttcf")) {
  11924. /* version 1? */
  11925. if (ttULONG(font_collection + 4) == 0x00010000 || ttULONG(font_collection + 4) == 0x00020000) {
  11926. stbtt_int32 n = ttLONG(font_collection + 8);
  11927. if (index >= n)
  11928. return -1;
  11929. return ttULONG(font_collection + 12 + index * 4);
  11930. }
  11931. }
  11932. return -1;
  11933. }
  11934. static int stbtt_GetNumberOfFonts_internal(unsigned char* font_collection)
  11935. {
  11936. /* if it's just a font, there's only one valid font */
  11937. if (stbtt__isfont(font_collection))
  11938. return 1;
  11939. /* check if it's a TTC */
  11940. if (stbtt_tag(font_collection, "ttcf")) {
  11941. /* version 1? */
  11942. if (ttULONG(font_collection + 4) == 0x00010000 || ttULONG(font_collection + 4) == 0x00020000) {
  11943. return ttLONG(font_collection + 8);
  11944. }
  11945. }
  11946. return 0;
  11947. }
  11948. static stbtt__buf stbtt__get_subrs(stbtt__buf cff, stbtt__buf fontdict)
  11949. {
  11950. stbtt_uint32 subrsoff = 0, private_loc[2] = { 0, 0 };
  11951. stbtt__buf pdict;
  11952. stbtt__dict_get_ints(&fontdict, 18, 2, private_loc);
  11953. if (!private_loc[1] || !private_loc[0]) return stbtt__new_buf(NULL, 0);
  11954. pdict = stbtt__buf_range(&cff, private_loc[1], private_loc[0]);
  11955. stbtt__dict_get_ints(&pdict, 19, 1, &subrsoff);
  11956. if (!subrsoff) return stbtt__new_buf(NULL, 0);
  11957. stbtt__buf_seek(&cff, private_loc[1] + subrsoff);
  11958. return stbtt__cff_get_index(&cff);
  11959. }
  11960. /* since most people won't use this, find this table the first time it's needed */
  11961. static int stbtt__get_svg(stbtt_fontinfo* info)
  11962. {
  11963. stbtt_uint32 t;
  11964. if (info->svg < 0) {
  11965. t = stbtt__find_table(info->data, info->fontstart, "SVG ");
  11966. if (t) {
  11967. stbtt_uint32 offset = ttULONG(info->data + t + 2);
  11968. info->svg = t + offset;
  11969. }
  11970. else {
  11971. info->svg = 0;
  11972. }
  11973. }
  11974. return info->svg;
  11975. }
  11976. static int stbtt_InitFont_internal(stbtt_fontinfo* info, unsigned char* data, int fontstart)
  11977. {
  11978. stbtt_uint32 cmap, t;
  11979. stbtt_int32 i, numTables;
  11980. info->data = data;
  11981. info->fontstart = fontstart;
  11982. info->cff = stbtt__new_buf(NULL, 0);
  11983. cmap = stbtt__find_table(data, fontstart, "cmap"); /* required */
  11984. info->loca = stbtt__find_table(data, fontstart, "loca"); /* required */
  11985. info->head = stbtt__find_table(data, fontstart, "head"); /* required */
  11986. info->glyf = stbtt__find_table(data, fontstart, "glyf"); /* required */
  11987. info->hhea = stbtt__find_table(data, fontstart, "hhea"); /* required */
  11988. info->hmtx = stbtt__find_table(data, fontstart, "hmtx"); /* required */
  11989. info->kern = stbtt__find_table(data, fontstart, "kern"); /* not required */
  11990. info->gpos = stbtt__find_table(data, fontstart, "GPOS"); /* not required */
  11991. if (!cmap || !info->head || !info->hhea || !info->hmtx)
  11992. return 0;
  11993. if (info->glyf) {
  11994. /* required for truetype */
  11995. if (!info->loca) return 0;
  11996. }
  11997. else {
  11998. /* initialization for CFF / Type2 fonts (OTF) */
  11999. stbtt__buf b, topdict, topdictidx;
  12000. stbtt_uint32 cstype = 2, charstrings = 0, fdarrayoff = 0, fdselectoff = 0;
  12001. stbtt_uint32 cff;
  12002. cff = stbtt__find_table(data, fontstart, "CFF ");
  12003. if (!cff) return 0;
  12004. info->fontdicts = stbtt__new_buf(NULL, 0);
  12005. info->fdselect = stbtt__new_buf(NULL, 0);
  12006. /* @TODO this should use size from table (not 512MB) */
  12007. info->cff = stbtt__new_buf(data + cff, 512 * 1024 * 1024);
  12008. b = info->cff;
  12009. /* read the header */
  12010. stbtt__buf_skip(&b, 2);
  12011. stbtt__buf_seek(&b, stbtt__buf_get8(&b)); /* hdrsize */
  12012. /* @TODO the name INDEX could list multiple fonts, */
  12013. /* but we just use the first one. */
  12014. stbtt__cff_get_index(&b); /* name INDEX */
  12015. topdictidx = stbtt__cff_get_index(&b);
  12016. topdict = stbtt__cff_index_get(topdictidx, 0);
  12017. stbtt__cff_get_index(&b); /* string INDEX */
  12018. info->gsubrs = stbtt__cff_get_index(&b);
  12019. stbtt__dict_get_ints(&topdict, 17, 1, &charstrings);
  12020. stbtt__dict_get_ints(&topdict, 0x100 | 6, 1, &cstype);
  12021. stbtt__dict_get_ints(&topdict, 0x100 | 36, 1, &fdarrayoff);
  12022. stbtt__dict_get_ints(&topdict, 0x100 | 37, 1, &fdselectoff);
  12023. info->subrs = stbtt__get_subrs(b, topdict);
  12024. /* we only support Type 2 charstrings */
  12025. if (cstype != 2) return 0;
  12026. if (charstrings == 0) return 0;
  12027. if (fdarrayoff) {
  12028. /* looks like a CID font */
  12029. if (!fdselectoff) return 0;
  12030. stbtt__buf_seek(&b, fdarrayoff);
  12031. info->fontdicts = stbtt__cff_get_index(&b);
  12032. info->fdselect = stbtt__buf_range(&b, fdselectoff, b.size - fdselectoff);
  12033. }
  12034. stbtt__buf_seek(&b, charstrings);
  12035. info->charstrings = stbtt__cff_get_index(&b);
  12036. }
  12037. t = stbtt__find_table(data, fontstart, "maxp");
  12038. if (t)
  12039. info->numGlyphs = ttUSHORT(data + t + 4);
  12040. else
  12041. info->numGlyphs = 0xffff;
  12042. info->svg = -1;
  12043. /* find a cmap encoding table we understand *now* to avoid searching */
  12044. /* later. (todo: could make this installable) */
  12045. /* the same regardless of glyph. */
  12046. numTables = ttUSHORT(data + cmap + 2);
  12047. info->index_map = 0;
  12048. for (i = 0; i < numTables; ++i) {
  12049. stbtt_uint32 encoding_record = cmap + 4 + 8 * i;
  12050. /* find an encoding we understand: */
  12051. switch (ttUSHORT(data + encoding_record)) {
  12052. case STBTT_PLATFORM_ID_MICROSOFT:
  12053. switch (ttUSHORT(data + encoding_record + 2)) {
  12054. case STBTT_MS_EID_UNICODE_BMP:
  12055. case STBTT_MS_EID_UNICODE_FULL:
  12056. /* MS/Unicode */
  12057. info->index_map = cmap + ttULONG(data + encoding_record + 4);
  12058. break;
  12059. }
  12060. break;
  12061. case STBTT_PLATFORM_ID_UNICODE:
  12062. /* Mac/iOS has these */
  12063. /* all the encodingIDs are unicode, so we don't bother to check it */
  12064. info->index_map = cmap + ttULONG(data + encoding_record + 4);
  12065. break;
  12066. }
  12067. }
  12068. if (info->index_map == 0)
  12069. return 0;
  12070. info->indexToLocFormat = ttUSHORT(data + info->head + 50);
  12071. return 1;
  12072. }
  12073. STBTT_DEF int stbtt_FindGlyphIndex(const stbtt_fontinfo* info, int unicode_codepoint)
  12074. {
  12075. stbtt_uint8* data = info->data;
  12076. stbtt_uint32 index_map = info->index_map;
  12077. stbtt_uint16 format = ttUSHORT(data + index_map + 0);
  12078. if (format == 0) { /* apple byte encoding */
  12079. stbtt_int32 bytes = ttUSHORT(data + index_map + 2);
  12080. if (unicode_codepoint < bytes - 6)
  12081. return ttBYTE(data + index_map + 6 + unicode_codepoint);
  12082. return 0;
  12083. }
  12084. else if (format == 6) {
  12085. stbtt_uint32 first = ttUSHORT(data + index_map + 6);
  12086. stbtt_uint32 count = ttUSHORT(data + index_map + 8);
  12087. if ((stbtt_uint32)unicode_codepoint >= first && (stbtt_uint32)unicode_codepoint < first + count)
  12088. return ttUSHORT(data + index_map + 10 + (unicode_codepoint - first) * 2);
  12089. return 0;
  12090. }
  12091. else if (format == 2) {
  12092. STBTT_assert(0); /* @TODO: high-byte mapping for japanese/chinese/korean */
  12093. return 0;
  12094. }
  12095. else if (format == 4) { /* standard mapping for windows fonts: binary search collection of ranges */
  12096. stbtt_uint16 segcount = ttUSHORT(data + index_map + 6) >> 1;
  12097. stbtt_uint16 searchRange = ttUSHORT(data + index_map + 8) >> 1;
  12098. stbtt_uint16 entrySelector = ttUSHORT(data + index_map + 10);
  12099. stbtt_uint16 rangeShift = ttUSHORT(data + index_map + 12) >> 1;
  12100. /* do a binary search of the segments */
  12101. stbtt_uint32 endCount = index_map + 14;
  12102. stbtt_uint32 search = endCount;
  12103. if (unicode_codepoint > 0xffff)
  12104. return 0;
  12105. /* they lie from endCount .. endCount + segCount */
  12106. /* but searchRange is the nearest power of two, so... */
  12107. if (unicode_codepoint >= ttUSHORT(data + search + rangeShift * 2))
  12108. search += rangeShift * 2;
  12109. /* now decrement to bias correctly to find smallest */
  12110. search -= 2;
  12111. while (entrySelector) {
  12112. stbtt_uint16 end;
  12113. searchRange >>= 1;
  12114. end = ttUSHORT(data + search + searchRange * 2);
  12115. if (unicode_codepoint > end)
  12116. search += searchRange * 2;
  12117. --entrySelector;
  12118. }
  12119. search += 2;
  12120. {
  12121. stbtt_uint16 offset, start, last;
  12122. stbtt_uint16 item = (stbtt_uint16)((search - endCount) >> 1);
  12123. start = ttUSHORT(data + index_map + 14 + segcount * 2 + 2 + 2 * item);
  12124. last = ttUSHORT(data + endCount + 2 * item);
  12125. if (unicode_codepoint < start || unicode_codepoint > last)
  12126. return 0;
  12127. offset = ttUSHORT(data + index_map + 14 + segcount * 6 + 2 + 2 * item);
  12128. if (offset == 0)
  12129. return (stbtt_uint16)(unicode_codepoint + ttSHORT(data + index_map + 14 + segcount * 4 + 2 + 2 * item));
  12130. return ttUSHORT(data + offset + (unicode_codepoint - start) * 2 + index_map + 14 + segcount * 6 + 2 + 2 * item);
  12131. }
  12132. }
  12133. else if (format == 12 || format == 13) {
  12134. stbtt_uint32 ngroups = ttULONG(data + index_map + 12);
  12135. stbtt_int32 low, high;
  12136. low = 0; high = (stbtt_int32)ngroups;
  12137. /* Binary search the right group. */
  12138. while (low < high) {
  12139. stbtt_int32 mid = low + ((high - low) >> 1); /* rounds down, so low <= mid < high */
  12140. stbtt_uint32 start_char = ttULONG(data + index_map + 16 + mid * 12);
  12141. stbtt_uint32 end_char = ttULONG(data + index_map + 16 + mid * 12 + 4);
  12142. if ((stbtt_uint32)unicode_codepoint < start_char)
  12143. high = mid;
  12144. else if ((stbtt_uint32)unicode_codepoint > end_char)
  12145. low = mid + 1;
  12146. else {
  12147. stbtt_uint32 start_glyph = ttULONG(data + index_map + 16 + mid * 12 + 8);
  12148. if (format == 12)
  12149. return start_glyph + unicode_codepoint - start_char;
  12150. else /* format == 13 */
  12151. return start_glyph;
  12152. }
  12153. }
  12154. return 0; /* not found */
  12155. }
  12156. /* @TODO */
  12157. STBTT_assert(0);
  12158. return 0;
  12159. }
  12160. STBTT_DEF int stbtt_GetCodepointShape(const stbtt_fontinfo* info, int unicode_codepoint, stbtt_vertex** vertices)
  12161. {
  12162. return stbtt_GetGlyphShape(info, stbtt_FindGlyphIndex(info, unicode_codepoint), vertices);
  12163. }
  12164. static void stbtt_setvertex(stbtt_vertex* v, stbtt_uint8 type, stbtt_int32 x, stbtt_int32 y, stbtt_int32 cx, stbtt_int32 cy)
  12165. {
  12166. v->type = type;
  12167. v->x = (stbtt_int16)x;
  12168. v->y = (stbtt_int16)y;
  12169. v->cx = (stbtt_int16)cx;
  12170. v->cy = (stbtt_int16)cy;
  12171. }
  12172. static int stbtt__GetGlyfOffset(const stbtt_fontinfo* info, int glyph_index)
  12173. {
  12174. int g1, g2;
  12175. STBTT_assert(!info->cff.size);
  12176. if (glyph_index >= info->numGlyphs) return -1; /* glyph index out of range */
  12177. if (info->indexToLocFormat >= 2) return -1; /* unknown index->glyph map format */
  12178. if (info->indexToLocFormat == 0) {
  12179. g1 = info->glyf + ttUSHORT(info->data + info->loca + glyph_index * 2) * 2;
  12180. g2 = info->glyf + ttUSHORT(info->data + info->loca + glyph_index * 2 + 2) * 2;
  12181. }
  12182. else {
  12183. g1 = info->glyf + ttULONG(info->data + info->loca + glyph_index * 4);
  12184. g2 = info->glyf + ttULONG(info->data + info->loca + glyph_index * 4 + 4);
  12185. }
  12186. return g1 == g2 ? -1 : g1; /* if length is 0, return -1 */
  12187. }
  12188. static int stbtt__GetGlyphInfoT2(const stbtt_fontinfo* info, int glyph_index, int* x0, int* y0, int* x1, int* y1);
  12189. STBTT_DEF int stbtt_GetGlyphBox(const stbtt_fontinfo* info, int glyph_index, int* x0, int* y0, int* x1, int* y1)
  12190. {
  12191. if (info->cff.size) {
  12192. stbtt__GetGlyphInfoT2(info, glyph_index, x0, y0, x1, y1);
  12193. }
  12194. else {
  12195. int g = stbtt__GetGlyfOffset(info, glyph_index);
  12196. if (g < 0) return 0;
  12197. if (x0) *x0 = ttSHORT(info->data + g + 2);
  12198. if (y0) *y0 = ttSHORT(info->data + g + 4);
  12199. if (x1) *x1 = ttSHORT(info->data + g + 6);
  12200. if (y1) *y1 = ttSHORT(info->data + g + 8);
  12201. }
  12202. return 1;
  12203. }
  12204. STBTT_DEF int stbtt_GetCodepointBox(const stbtt_fontinfo* info, int codepoint, int* x0, int* y0, int* x1, int* y1)
  12205. {
  12206. return stbtt_GetGlyphBox(info, stbtt_FindGlyphIndex(info, codepoint), x0, y0, x1, y1);
  12207. }
  12208. STBTT_DEF int stbtt_IsGlyphEmpty(const stbtt_fontinfo* info, int glyph_index)
  12209. {
  12210. stbtt_int16 numberOfContours;
  12211. int g;
  12212. if (info->cff.size)
  12213. return stbtt__GetGlyphInfoT2(info, glyph_index, NULL, NULL, NULL, NULL) == 0;
  12214. g = stbtt__GetGlyfOffset(info, glyph_index);
  12215. if (g < 0) return 1;
  12216. numberOfContours = ttSHORT(info->data + g);
  12217. return numberOfContours == 0;
  12218. }
  12219. static int stbtt__close_shape(stbtt_vertex* vertices, int num_vertices, int was_off, int start_off,
  12220. stbtt_int32 sx, stbtt_int32 sy, stbtt_int32 scx, stbtt_int32 scy, stbtt_int32 cx, stbtt_int32 cy)
  12221. {
  12222. if (start_off) {
  12223. if (was_off)
  12224. stbtt_setvertex(&vertices[num_vertices++], STBTT_vcurve, (cx + scx) >> 1, (cy + scy) >> 1, cx, cy);
  12225. stbtt_setvertex(&vertices[num_vertices++], STBTT_vcurve, sx, sy, scx, scy);
  12226. }
  12227. else {
  12228. if (was_off)
  12229. stbtt_setvertex(&vertices[num_vertices++], STBTT_vcurve, sx, sy, cx, cy);
  12230. else
  12231. stbtt_setvertex(&vertices[num_vertices++], STBTT_vline, sx, sy, 0, 0);
  12232. }
  12233. return num_vertices;
  12234. }
  12235. static int stbtt__GetGlyphShapeTT(const stbtt_fontinfo* info, int glyph_index, stbtt_vertex** pvertices)
  12236. {
  12237. stbtt_int16 numberOfContours;
  12238. stbtt_uint8* endPtsOfContours;
  12239. stbtt_uint8* data = info->data;
  12240. stbtt_vertex* vertices = 0;
  12241. int num_vertices = 0;
  12242. int g = stbtt__GetGlyfOffset(info, glyph_index);
  12243. *pvertices = NULL;
  12244. if (g < 0) return 0;
  12245. numberOfContours = ttSHORT(data + g);
  12246. if (numberOfContours > 0) {
  12247. stbtt_uint8 flags = 0, flagcount;
  12248. stbtt_int32 ins, i, j = 0, m, n, next_move, was_off = 0, off, start_off = 0;
  12249. stbtt_int32 x, y, cx, cy, sx, sy, scx, scy;
  12250. stbtt_uint8* points;
  12251. endPtsOfContours = (data + g + 10);
  12252. ins = ttUSHORT(data + g + 10 + numberOfContours * 2);
  12253. points = data + g + 10 + numberOfContours * 2 + 2 + ins;
  12254. n = 1 + ttUSHORT(endPtsOfContours + numberOfContours * 2 - 2);
  12255. m = n + 2 * numberOfContours; /* a loose bound on how many vertices we might need */
  12256. vertices = (stbtt_vertex*)STBTT_malloc(m * sizeof(vertices[0]), info->userdata);
  12257. if (vertices == 0)
  12258. return 0;
  12259. next_move = 0;
  12260. flagcount = 0;
  12261. /* in first pass, we load uninterpreted data into the allocated array */
  12262. /* above, shifted to the end of the array so we won't overwrite it when */
  12263. /* we create our final data starting from the front */
  12264. off = m - n; /* starting offset for uninterpreted data, regardless of how m ends up being calculated */
  12265. /* first load flags */
  12266. for (i = 0; i < n; ++i) {
  12267. if (flagcount == 0) {
  12268. flags = *points++;
  12269. if (flags & 8)
  12270. flagcount = *points++;
  12271. }
  12272. else
  12273. --flagcount;
  12274. vertices[off + i].type = flags;
  12275. }
  12276. /* now load x coordinates */
  12277. x = 0;
  12278. for (i = 0; i < n; ++i) {
  12279. flags = vertices[off + i].type;
  12280. if (flags & 2) {
  12281. stbtt_int16 dx = *points++;
  12282. x += (flags & 16) ? dx : -dx; /* ??? */
  12283. }
  12284. else {
  12285. if (!(flags & 16)) {
  12286. x = x + (stbtt_int16)(points[0] * 256 + points[1]);
  12287. points += 2;
  12288. }
  12289. }
  12290. vertices[off + i].x = (stbtt_int16)x;
  12291. }
  12292. /* now load y coordinates */
  12293. y = 0;
  12294. for (i = 0; i < n; ++i) {
  12295. flags = vertices[off + i].type;
  12296. if (flags & 4) {
  12297. stbtt_int16 dy = *points++;
  12298. y += (flags & 32) ? dy : -dy; /* ??? */
  12299. }
  12300. else {
  12301. if (!(flags & 32)) {
  12302. y = y + (stbtt_int16)(points[0] * 256 + points[1]);
  12303. points += 2;
  12304. }
  12305. }
  12306. vertices[off + i].y = (stbtt_int16)y;
  12307. }
  12308. /* now convert them to our format */
  12309. num_vertices = 0;
  12310. sx = sy = cx = cy = scx = scy = 0;
  12311. for (i = 0; i < n; ++i) {
  12312. flags = vertices[off + i].type;
  12313. x = (stbtt_int16)vertices[off + i].x;
  12314. y = (stbtt_int16)vertices[off + i].y;
  12315. if (next_move == i) {
  12316. if (i != 0)
  12317. num_vertices = stbtt__close_shape(vertices, num_vertices, was_off, start_off, sx, sy, scx, scy, cx, cy);
  12318. /* now start the new one */
  12319. start_off = !(flags & 1);
  12320. if (start_off) {
  12321. /* if we start off with an off-curve point, then when we need to find a point on the curve */
  12322. /* where we can start, and we need to save some state for when we wraparound. */
  12323. scx = x;
  12324. scy = y;
  12325. if (!(vertices[off + i + 1].type & 1)) {
  12326. /* next point is also a curve point, so interpolate an on-point curve */
  12327. sx = (x + (stbtt_int32)vertices[off + i + 1].x) >> 1;
  12328. sy = (y + (stbtt_int32)vertices[off + i + 1].y) >> 1;
  12329. }
  12330. else {
  12331. /* otherwise just use the next point as our start point */
  12332. sx = (stbtt_int32)vertices[off + i + 1].x;
  12333. sy = (stbtt_int32)vertices[off + i + 1].y;
  12334. ++i; /* we're using point i+1 as the starting point, so skip it */
  12335. }
  12336. }
  12337. else {
  12338. sx = x;
  12339. sy = y;
  12340. }
  12341. stbtt_setvertex(&vertices[num_vertices++], STBTT_vmove, sx, sy, 0, 0);
  12342. was_off = 0;
  12343. next_move = 1 + ttUSHORT(endPtsOfContours + j * 2);
  12344. ++j;
  12345. }
  12346. else {
  12347. if (!(flags & 1)) { /* if it's a curve */
  12348. if (was_off) /* two off-curve control points in a row means interpolate an on-curve midpoint */
  12349. stbtt_setvertex(&vertices[num_vertices++], STBTT_vcurve, (cx + x) >> 1, (cy + y) >> 1, cx, cy);
  12350. cx = x;
  12351. cy = y;
  12352. was_off = 1;
  12353. }
  12354. else {
  12355. if (was_off)
  12356. stbtt_setvertex(&vertices[num_vertices++], STBTT_vcurve, x, y, cx, cy);
  12357. else
  12358. stbtt_setvertex(&vertices[num_vertices++], STBTT_vline, x, y, 0, 0);
  12359. was_off = 0;
  12360. }
  12361. }
  12362. }
  12363. num_vertices = stbtt__close_shape(vertices, num_vertices, was_off, start_off, sx, sy, scx, scy, cx, cy);
  12364. }
  12365. else if (numberOfContours < 0) {
  12366. /* Compound shapes. */
  12367. int more = 1;
  12368. stbtt_uint8* comp = data + g + 10;
  12369. num_vertices = 0;
  12370. vertices = 0;
  12371. while (more) {
  12372. stbtt_uint16 flags, gidx;
  12373. int comp_num_verts = 0, i;
  12374. stbtt_vertex* comp_verts = 0, * tmp = 0;
  12375. float mtx[6] = { 1,0,0,1,0,0 }, m, n;
  12376. flags = ttSHORT(comp); comp += 2;
  12377. gidx = ttSHORT(comp); comp += 2;
  12378. if (flags & 2) { /* XY values */
  12379. if (flags & 1) { /* shorts */
  12380. mtx[4] = ttSHORT(comp); comp += 2;
  12381. mtx[5] = ttSHORT(comp); comp += 2;
  12382. }
  12383. else {
  12384. mtx[4] = ttCHAR(comp); comp += 1;
  12385. mtx[5] = ttCHAR(comp); comp += 1;
  12386. }
  12387. }
  12388. else {
  12389. /* @TODO handle matching point */
  12390. STBTT_assert(0);
  12391. }
  12392. if (flags & (1 << 3)) { /* WE_HAVE_A_SCALE */
  12393. mtx[0] = mtx[3] = ttSHORT(comp) / 16384.0f; comp += 2;
  12394. mtx[1] = mtx[2] = 0;
  12395. }
  12396. else if (flags & (1 << 6)) { /* WE_HAVE_AN_X_AND_YSCALE */
  12397. mtx[0] = ttSHORT(comp) / 16384.0f; comp += 2;
  12398. mtx[1] = mtx[2] = 0;
  12399. mtx[3] = ttSHORT(comp) / 16384.0f; comp += 2;
  12400. }
  12401. else if (flags & (1 << 7)) { /* WE_HAVE_A_TWO_BY_TWO */
  12402. mtx[0] = ttSHORT(comp) / 16384.0f; comp += 2;
  12403. mtx[1] = ttSHORT(comp) / 16384.0f; comp += 2;
  12404. mtx[2] = ttSHORT(comp) / 16384.0f; comp += 2;
  12405. mtx[3] = ttSHORT(comp) / 16384.0f; comp += 2;
  12406. }
  12407. /* Find transformation scales. */
  12408. m = (float)STBTT_sqrt(mtx[0] * mtx[0] + mtx[1] * mtx[1]);
  12409. n = (float)STBTT_sqrt(mtx[2] * mtx[2] + mtx[3] * mtx[3]);
  12410. /* Get indexed glyph. */
  12411. comp_num_verts = stbtt_GetGlyphShape(info, gidx, &comp_verts);
  12412. if (comp_num_verts > 0) {
  12413. /* Transform vertices. */
  12414. for (i = 0; i < comp_num_verts; ++i) {
  12415. stbtt_vertex* v = &comp_verts[i];
  12416. stbtt_vertex_type x, y;
  12417. x = v->x; y = v->y;
  12418. v->x = (stbtt_vertex_type)(m * (mtx[0] * x + mtx[2] * y + mtx[4]));
  12419. v->y = (stbtt_vertex_type)(n * (mtx[1] * x + mtx[3] * y + mtx[5]));
  12420. x = v->cx; y = v->cy;
  12421. v->cx = (stbtt_vertex_type)(m * (mtx[0] * x + mtx[2] * y + mtx[4]));
  12422. v->cy = (stbtt_vertex_type)(n * (mtx[1] * x + mtx[3] * y + mtx[5]));
  12423. }
  12424. /* Append vertices. */
  12425. tmp = (stbtt_vertex*)STBTT_malloc((num_vertices + comp_num_verts) * sizeof(stbtt_vertex), info->userdata);
  12426. if (!tmp) {
  12427. if (vertices) STBTT_free(vertices, info->userdata);
  12428. if (comp_verts) STBTT_free(comp_verts, info->userdata);
  12429. return 0;
  12430. }
  12431. if (num_vertices > 0 && vertices) STBTT_memcpy(tmp, vertices, num_vertices * sizeof(stbtt_vertex));
  12432. STBTT_memcpy(tmp + num_vertices, comp_verts, comp_num_verts * sizeof(stbtt_vertex));
  12433. if (vertices) STBTT_free(vertices, info->userdata);
  12434. vertices = tmp;
  12435. STBTT_free(comp_verts, info->userdata);
  12436. num_vertices += comp_num_verts;
  12437. }
  12438. /* More components ? */
  12439. more = flags & (1 << 5);
  12440. }
  12441. }
  12442. else {
  12443. /* numberOfCounters == 0, do nothing */
  12444. }
  12445. *pvertices = vertices;
  12446. return num_vertices;
  12447. }
  12448. typedef struct
  12449. {
  12450. int bounds;
  12451. int started;
  12452. float first_x, first_y;
  12453. float x, y;
  12454. stbtt_int32 min_x, max_x, min_y, max_y;
  12455. stbtt_vertex* pvertices;
  12456. int num_vertices;
  12457. } stbtt__csctx;
  12458. #define STBTT__CSCTX_INIT(bounds) {bounds,0, 0,0, 0,0, 0,0,0,0, NULL, 0}
  12459. static void stbtt__track_vertex(stbtt__csctx* c, stbtt_int32 x, stbtt_int32 y)
  12460. {
  12461. if (x > c->max_x || !c->started) c->max_x = x;
  12462. if (y > c->max_y || !c->started) c->max_y = y;
  12463. if (x < c->min_x || !c->started) c->min_x = x;
  12464. if (y < c->min_y || !c->started) c->min_y = y;
  12465. c->started = 1;
  12466. }
  12467. static void stbtt__csctx_v(stbtt__csctx* c, stbtt_uint8 type, stbtt_int32 x, stbtt_int32 y, stbtt_int32 cx, stbtt_int32 cy, stbtt_int32 cx1, stbtt_int32 cy1)
  12468. {
  12469. if (c->bounds) {
  12470. stbtt__track_vertex(c, x, y);
  12471. if (type == STBTT_vcubic) {
  12472. stbtt__track_vertex(c, cx, cy);
  12473. stbtt__track_vertex(c, cx1, cy1);
  12474. }
  12475. }
  12476. else {
  12477. stbtt_setvertex(&c->pvertices[c->num_vertices], type, x, y, cx, cy);
  12478. c->pvertices[c->num_vertices].cx1 = (stbtt_int16)cx1;
  12479. c->pvertices[c->num_vertices].cy1 = (stbtt_int16)cy1;
  12480. }
  12481. c->num_vertices++;
  12482. }
  12483. static void stbtt__csctx_close_shape(stbtt__csctx* ctx)
  12484. {
  12485. if (ctx->first_x != ctx->x || ctx->first_y != ctx->y)
  12486. stbtt__csctx_v(ctx, STBTT_vline, (int)ctx->first_x, (int)ctx->first_y, 0, 0, 0, 0);
  12487. }
  12488. static void stbtt__csctx_rmove_to(stbtt__csctx* ctx, float dx, float dy)
  12489. {
  12490. stbtt__csctx_close_shape(ctx);
  12491. ctx->first_x = ctx->x = ctx->x + dx;
  12492. ctx->first_y = ctx->y = ctx->y + dy;
  12493. stbtt__csctx_v(ctx, STBTT_vmove, (int)ctx->x, (int)ctx->y, 0, 0, 0, 0);
  12494. }
  12495. static void stbtt__csctx_rline_to(stbtt__csctx* ctx, float dx, float dy)
  12496. {
  12497. ctx->x += dx;
  12498. ctx->y += dy;
  12499. stbtt__csctx_v(ctx, STBTT_vline, (int)ctx->x, (int)ctx->y, 0, 0, 0, 0);
  12500. }
  12501. static void stbtt__csctx_rccurve_to(stbtt__csctx* ctx, float dx1, float dy1, float dx2, float dy2, float dx3, float dy3)
  12502. {
  12503. float cx1 = ctx->x + dx1;
  12504. float cy1 = ctx->y + dy1;
  12505. float cx2 = cx1 + dx2;
  12506. float cy2 = cy1 + dy2;
  12507. ctx->x = cx2 + dx3;
  12508. ctx->y = cy2 + dy3;
  12509. stbtt__csctx_v(ctx, STBTT_vcubic, (int)ctx->x, (int)ctx->y, (int)cx1, (int)cy1, (int)cx2, (int)cy2);
  12510. }
  12511. static stbtt__buf stbtt__get_subr(stbtt__buf idx, int n)
  12512. {
  12513. int count = stbtt__cff_index_count(&idx);
  12514. int bias = 107;
  12515. if (count >= 33900)
  12516. bias = 32768;
  12517. else if (count >= 1240)
  12518. bias = 1131;
  12519. n += bias;
  12520. if (n < 0 || n >= count)
  12521. return stbtt__new_buf(NULL, 0);
  12522. return stbtt__cff_index_get(idx, n);
  12523. }
  12524. static stbtt__buf stbtt__cid_get_glyph_subrs(const stbtt_fontinfo* info, int glyph_index)
  12525. {
  12526. stbtt__buf fdselect = info->fdselect;
  12527. int nranges, start, end, v, fmt, fdselector = -1, i;
  12528. stbtt__buf_seek(&fdselect, 0);
  12529. fmt = stbtt__buf_get8(&fdselect);
  12530. if (fmt == 0) {
  12531. /* untested */
  12532. stbtt__buf_skip(&fdselect, glyph_index);
  12533. fdselector = stbtt__buf_get8(&fdselect);
  12534. }
  12535. else if (fmt == 3) {
  12536. nranges = stbtt__buf_get16(&fdselect);
  12537. start = stbtt__buf_get16(&fdselect);
  12538. for (i = 0; i < nranges; i++) {
  12539. v = stbtt__buf_get8(&fdselect);
  12540. end = stbtt__buf_get16(&fdselect);
  12541. if (glyph_index >= start && glyph_index < end) {
  12542. fdselector = v;
  12543. break;
  12544. }
  12545. start = end;
  12546. }
  12547. }
  12548. if (fdselector == -1) stbtt__new_buf(NULL, 0);
  12549. return stbtt__get_subrs(info->cff, stbtt__cff_index_get(info->fontdicts, fdselector));
  12550. }
  12551. static int stbtt__run_charstring(const stbtt_fontinfo* info, int glyph_index, stbtt__csctx* c)
  12552. {
  12553. int in_header = 1, maskbits = 0, subr_stack_height = 0, sp = 0, v, i, b0;
  12554. int has_subrs = 0, clear_stack;
  12555. float s[48];
  12556. stbtt__buf subr_stack[10], subrs = info->subrs, b;
  12557. float f;
  12558. #define STBTT__CSERR(s) (0)
  12559. /* this currently ignores the initial width value, which isn't needed if we have hmtx */
  12560. b = stbtt__cff_index_get(info->charstrings, glyph_index);
  12561. while (b.cursor < b.size) {
  12562. i = 0;
  12563. clear_stack = 1;
  12564. b0 = stbtt__buf_get8(&b);
  12565. switch (b0) {
  12566. /* @TODO implement hinting */
  12567. case 0x13: /* hintmask */
  12568. case 0x14: /* cntrmask */
  12569. if (in_header)
  12570. maskbits += (sp / 2); /* implicit "vstem" */
  12571. in_header = 0;
  12572. stbtt__buf_skip(&b, (maskbits + 7) / 8);
  12573. break;
  12574. case 0x01: /* hstem */
  12575. case 0x03: /* vstem */
  12576. case 0x12: /* hstemhm */
  12577. case 0x17: /* vstemhm */
  12578. maskbits += (sp / 2);
  12579. break;
  12580. case 0x15: /* rmoveto */
  12581. in_header = 0;
  12582. if (sp < 2) return STBTT__CSERR("rmoveto stack");
  12583. stbtt__csctx_rmove_to(c, s[sp - 2], s[sp - 1]);
  12584. break;
  12585. case 0x04: /* vmoveto */
  12586. in_header = 0;
  12587. if (sp < 1) return STBTT__CSERR("vmoveto stack");
  12588. stbtt__csctx_rmove_to(c, 0, s[sp - 1]);
  12589. break;
  12590. case 0x16: /* hmoveto */
  12591. in_header = 0;
  12592. if (sp < 1) return STBTT__CSERR("hmoveto stack");
  12593. stbtt__csctx_rmove_to(c, s[sp - 1], 0);
  12594. break;
  12595. case 0x05: /* rlineto */
  12596. if (sp < 2) return STBTT__CSERR("rlineto stack");
  12597. for (; i + 1 < sp; i += 2)
  12598. stbtt__csctx_rline_to(c, s[i], s[i + 1]);
  12599. break;
  12600. /* hlineto/vlineto and vhcurveto/hvcurveto alternate horizontal and vertical */
  12601. /* starting from a different place. */
  12602. case 0x07: /* vlineto */
  12603. if (sp < 1) return STBTT__CSERR("vlineto stack");
  12604. goto vlineto;
  12605. case 0x06: /* hlineto */
  12606. if (sp < 1) return STBTT__CSERR("hlineto stack");
  12607. for (;;) {
  12608. if (i >= sp) break;
  12609. stbtt__csctx_rline_to(c, s[i], 0);
  12610. i++;
  12611. vlineto:
  12612. if (i >= sp) break;
  12613. stbtt__csctx_rline_to(c, 0, s[i]);
  12614. i++;
  12615. }
  12616. break;
  12617. case 0x1F: /* hvcurveto */
  12618. if (sp < 4) return STBTT__CSERR("hvcurveto stack");
  12619. goto hvcurveto;
  12620. case 0x1E: /* vhcurveto */
  12621. if (sp < 4) return STBTT__CSERR("vhcurveto stack");
  12622. for (;;) {
  12623. if (i + 3 >= sp) break;
  12624. stbtt__csctx_rccurve_to(c, 0, s[i], s[i + 1], s[i + 2], s[i + 3], (sp - i == 5) ? s[i + 4] : 0.0f);
  12625. i += 4;
  12626. hvcurveto:
  12627. if (i + 3 >= sp) break;
  12628. stbtt__csctx_rccurve_to(c, s[i], 0, s[i + 1], s[i + 2], (sp - i == 5) ? s[i + 4] : 0.0f, s[i + 3]);
  12629. i += 4;
  12630. }
  12631. break;
  12632. case 0x08: /* rrcurveto */
  12633. if (sp < 6) return STBTT__CSERR("rcurveline stack");
  12634. for (; i + 5 < sp; i += 6)
  12635. stbtt__csctx_rccurve_to(c, s[i], s[i + 1], s[i + 2], s[i + 3], s[i + 4], s[i + 5]);
  12636. break;
  12637. case 0x18: /* rcurveline */
  12638. if (sp < 8) return STBTT__CSERR("rcurveline stack");
  12639. for (; i + 5 < sp - 2; i += 6)
  12640. stbtt__csctx_rccurve_to(c, s[i], s[i + 1], s[i + 2], s[i + 3], s[i + 4], s[i + 5]);
  12641. if (i + 1 >= sp) return STBTT__CSERR("rcurveline stack");
  12642. stbtt__csctx_rline_to(c, s[i], s[i + 1]);
  12643. break;
  12644. case 0x19: /* rlinecurve */
  12645. if (sp < 8) return STBTT__CSERR("rlinecurve stack");
  12646. for (; i + 1 < sp - 6; i += 2)
  12647. stbtt__csctx_rline_to(c, s[i], s[i + 1]);
  12648. if (i + 5 >= sp) return STBTT__CSERR("rlinecurve stack");
  12649. stbtt__csctx_rccurve_to(c, s[i], s[i + 1], s[i + 2], s[i + 3], s[i + 4], s[i + 5]);
  12650. break;
  12651. case 0x1A: /* vvcurveto */
  12652. case 0x1B: /* hhcurveto */
  12653. if (sp < 4) return STBTT__CSERR("(vv|hh)curveto stack");
  12654. f = 0.0;
  12655. if (sp & 1) { f = s[i]; i++; }
  12656. for (; i + 3 < sp; i += 4) {
  12657. if (b0 == 0x1B)
  12658. stbtt__csctx_rccurve_to(c, s[i], f, s[i + 1], s[i + 2], s[i + 3], 0.0);
  12659. else
  12660. stbtt__csctx_rccurve_to(c, f, s[i], s[i + 1], s[i + 2], 0.0, s[i + 3]);
  12661. f = 0.0;
  12662. }
  12663. break;
  12664. case 0x0A: /* callsubr */
  12665. if (!has_subrs) {
  12666. if (info->fdselect.size)
  12667. subrs = stbtt__cid_get_glyph_subrs(info, glyph_index);
  12668. has_subrs = 1;
  12669. }
  12670. /* FALLTHROUGH */
  12671. case 0x1D: /* callgsubr */
  12672. if (sp < 1) return STBTT__CSERR("call(g|)subr stack");
  12673. v = (int)s[--sp];
  12674. if (subr_stack_height >= 10) return STBTT__CSERR("recursion limit");
  12675. subr_stack[subr_stack_height++] = b;
  12676. b = stbtt__get_subr(b0 == 0x0A ? subrs : info->gsubrs, v);
  12677. if (b.size == 0) return STBTT__CSERR("subr not found");
  12678. b.cursor = 0;
  12679. clear_stack = 0;
  12680. break;
  12681. case 0x0B: /* return */
  12682. if (subr_stack_height <= 0) return STBTT__CSERR("return outside subr");
  12683. b = subr_stack[--subr_stack_height];
  12684. clear_stack = 0;
  12685. break;
  12686. case 0x0E: /* endchar */
  12687. stbtt__csctx_close_shape(c);
  12688. return 1;
  12689. case 0x0C: { /* two-byte escape */
  12690. float dx1, dx2, dx3, dx4, dx5, dx6, dy1, dy2, dy3, dy4, dy5, dy6;
  12691. float dx, dy;
  12692. int b1 = stbtt__buf_get8(&b);
  12693. switch (b1) {
  12694. /* @TODO These "flex" implementations ignore the flex-depth and resolution, */
  12695. /* and always draw beziers. */
  12696. case 0x22: /* hflex */
  12697. if (sp < 7) return STBTT__CSERR("hflex stack");
  12698. dx1 = s[0];
  12699. dx2 = s[1];
  12700. dy2 = s[2];
  12701. dx3 = s[3];
  12702. dx4 = s[4];
  12703. dx5 = s[5];
  12704. dx6 = s[6];
  12705. stbtt__csctx_rccurve_to(c, dx1, 0, dx2, dy2, dx3, 0);
  12706. stbtt__csctx_rccurve_to(c, dx4, 0, dx5, -dy2, dx6, 0);
  12707. break;
  12708. case 0x23: /* flex */
  12709. if (sp < 13) return STBTT__CSERR("flex stack");
  12710. dx1 = s[0];
  12711. dy1 = s[1];
  12712. dx2 = s[2];
  12713. dy2 = s[3];
  12714. dx3 = s[4];
  12715. dy3 = s[5];
  12716. dx4 = s[6];
  12717. dy4 = s[7];
  12718. dx5 = s[8];
  12719. dy5 = s[9];
  12720. dx6 = s[10];
  12721. dy6 = s[11];
  12722. /* fd is s[12] */
  12723. stbtt__csctx_rccurve_to(c, dx1, dy1, dx2, dy2, dx3, dy3);
  12724. stbtt__csctx_rccurve_to(c, dx4, dy4, dx5, dy5, dx6, dy6);
  12725. break;
  12726. case 0x24: /* hflex1 */
  12727. if (sp < 9) return STBTT__CSERR("hflex1 stack");
  12728. dx1 = s[0];
  12729. dy1 = s[1];
  12730. dx2 = s[2];
  12731. dy2 = s[3];
  12732. dx3 = s[4];
  12733. dx4 = s[5];
  12734. dx5 = s[6];
  12735. dy5 = s[7];
  12736. dx6 = s[8];
  12737. stbtt__csctx_rccurve_to(c, dx1, dy1, dx2, dy2, dx3, 0);
  12738. stbtt__csctx_rccurve_to(c, dx4, 0, dx5, dy5, dx6, -(dy1 + dy2 + dy5));
  12739. break;
  12740. case 0x25: /* flex1 */
  12741. if (sp < 11) return STBTT__CSERR("flex1 stack");
  12742. dx1 = s[0];
  12743. dy1 = s[1];
  12744. dx2 = s[2];
  12745. dy2 = s[3];
  12746. dx3 = s[4];
  12747. dy3 = s[5];
  12748. dx4 = s[6];
  12749. dy4 = s[7];
  12750. dx5 = s[8];
  12751. dy5 = s[9];
  12752. dx6 = dy6 = s[10];
  12753. dx = dx1 + dx2 + dx3 + dx4 + dx5;
  12754. dy = dy1 + dy2 + dy3 + dy4 + dy5;
  12755. if (STBTT_fabs(dx) > STBTT_fabs(dy))
  12756. dy6 = -dy;
  12757. else
  12758. dx6 = -dx;
  12759. stbtt__csctx_rccurve_to(c, dx1, dy1, dx2, dy2, dx3, dy3);
  12760. stbtt__csctx_rccurve_to(c, dx4, dy4, dx5, dy5, dx6, dy6);
  12761. break;
  12762. default:
  12763. return STBTT__CSERR("unimplemented");
  12764. }
  12765. } break;
  12766. default:
  12767. if (b0 != 255 && b0 != 28 && b0 < 32)
  12768. return STBTT__CSERR("reserved operator");
  12769. /* push immediate */
  12770. if (b0 == 255) {
  12771. f = (float)(stbtt_int32)stbtt__buf_get32(&b) / 0x10000;
  12772. }
  12773. else {
  12774. stbtt__buf_skip(&b, -1);
  12775. f = (float)(stbtt_int16)stbtt__cff_int(&b);
  12776. }
  12777. if (sp >= 48) return STBTT__CSERR("push stack overflow");
  12778. s[sp++] = f;
  12779. clear_stack = 0;
  12780. break;
  12781. }
  12782. if (clear_stack) sp = 0;
  12783. }
  12784. return STBTT__CSERR("no endchar");
  12785. #undef STBTT__CSERR
  12786. }
  12787. static int stbtt__GetGlyphShapeT2(const stbtt_fontinfo* info, int glyph_index, stbtt_vertex** pvertices)
  12788. {
  12789. /* runs the charstring twice, once to count and once to output (to avoid realloc) */
  12790. stbtt__csctx count_ctx = STBTT__CSCTX_INIT(1);
  12791. stbtt__csctx output_ctx = STBTT__CSCTX_INIT(0);
  12792. if (stbtt__run_charstring(info, glyph_index, &count_ctx)) {
  12793. *pvertices = (stbtt_vertex*)STBTT_malloc(count_ctx.num_vertices * sizeof(stbtt_vertex), info->userdata);
  12794. output_ctx.pvertices = *pvertices;
  12795. if (stbtt__run_charstring(info, glyph_index, &output_ctx)) {
  12796. STBTT_assert(output_ctx.num_vertices == count_ctx.num_vertices);
  12797. return output_ctx.num_vertices;
  12798. }
  12799. }
  12800. *pvertices = NULL;
  12801. return 0;
  12802. }
  12803. static int stbtt__GetGlyphInfoT2(const stbtt_fontinfo* info, int glyph_index, int* x0, int* y0, int* x1, int* y1)
  12804. {
  12805. stbtt__csctx c = STBTT__CSCTX_INIT(1);
  12806. int r = stbtt__run_charstring(info, glyph_index, &c);
  12807. if (x0) *x0 = r ? c.min_x : 0;
  12808. if (y0) *y0 = r ? c.min_y : 0;
  12809. if (x1) *x1 = r ? c.max_x : 0;
  12810. if (y1) *y1 = r ? c.max_y : 0;
  12811. return r ? c.num_vertices : 0;
  12812. }
  12813. STBTT_DEF int stbtt_GetGlyphShape(const stbtt_fontinfo* info, int glyph_index, stbtt_vertex** pvertices)
  12814. {
  12815. if (!info->cff.size)
  12816. return stbtt__GetGlyphShapeTT(info, glyph_index, pvertices);
  12817. else
  12818. return stbtt__GetGlyphShapeT2(info, glyph_index, pvertices);
  12819. }
  12820. STBTT_DEF void stbtt_GetGlyphHMetrics(const stbtt_fontinfo* info, int glyph_index, int* advanceWidth, int* leftSideBearing)
  12821. {
  12822. stbtt_uint16 numOfLongHorMetrics = ttUSHORT(info->data + info->hhea + 34);
  12823. if (glyph_index < numOfLongHorMetrics) {
  12824. if (advanceWidth) *advanceWidth = ttSHORT(info->data + info->hmtx + 4 * glyph_index);
  12825. if (leftSideBearing) *leftSideBearing = ttSHORT(info->data + info->hmtx + 4 * glyph_index + 2);
  12826. }
  12827. else {
  12828. if (advanceWidth) *advanceWidth = ttSHORT(info->data + info->hmtx + 4 * (numOfLongHorMetrics - 1));
  12829. if (leftSideBearing) *leftSideBearing = ttSHORT(info->data + info->hmtx + 4 * numOfLongHorMetrics + 2 * (glyph_index - numOfLongHorMetrics));
  12830. }
  12831. }
  12832. STBTT_DEF int stbtt_GetKerningTableLength(const stbtt_fontinfo* info)
  12833. {
  12834. stbtt_uint8* data = info->data + info->kern;
  12835. /* we only look at the first table. it must be 'horizontal' and format 0. */
  12836. if (!info->kern)
  12837. return 0;
  12838. if (ttUSHORT(data + 2) < 1) /* number of tables, need at least 1 */
  12839. return 0;
  12840. if (ttUSHORT(data + 8) != 1) /* horizontal flag must be set in format */
  12841. return 0;
  12842. return ttUSHORT(data + 10);
  12843. }
  12844. STBTT_DEF int stbtt_GetKerningTable(const stbtt_fontinfo* info, stbtt_kerningentry* table, int table_length)
  12845. {
  12846. stbtt_uint8* data = info->data + info->kern;
  12847. int k, length;
  12848. /* we only look at the first table. it must be 'horizontal' and format 0. */
  12849. if (!info->kern)
  12850. return 0;
  12851. if (ttUSHORT(data + 2) < 1) /* number of tables, need at least 1 */
  12852. return 0;
  12853. if (ttUSHORT(data + 8) != 1) /* horizontal flag must be set in format */
  12854. return 0;
  12855. length = ttUSHORT(data + 10);
  12856. if (table_length < length)
  12857. length = table_length;
  12858. for (k = 0; k < length; k++)
  12859. {
  12860. table[k].glyph1 = ttUSHORT(data + 18 + (k * 6));
  12861. table[k].glyph2 = ttUSHORT(data + 20 + (k * 6));
  12862. table[k].advance = ttSHORT(data + 22 + (k * 6));
  12863. }
  12864. return length;
  12865. }
  12866. static int stbtt__GetGlyphKernInfoAdvance(const stbtt_fontinfo* info, int glyph1, int glyph2)
  12867. {
  12868. stbtt_uint8* data = info->data + info->kern;
  12869. stbtt_uint32 needle, straw;
  12870. int l, r, m;
  12871. /* we only look at the first table. it must be 'horizontal' and format 0. */
  12872. if (!info->kern)
  12873. return 0;
  12874. if (ttUSHORT(data + 2) < 1) /* number of tables, need at least 1 */
  12875. return 0;
  12876. if (ttUSHORT(data + 8) != 1) /* horizontal flag must be set in format */
  12877. return 0;
  12878. l = 0;
  12879. r = ttUSHORT(data + 10) - 1;
  12880. needle = glyph1 << 16 | glyph2;
  12881. while (l <= r) {
  12882. m = (l + r) >> 1;
  12883. straw = ttULONG(data + 18 + (m * 6)); /* note: unaligned read */
  12884. if (needle < straw)
  12885. r = m - 1;
  12886. else if (needle > straw)
  12887. l = m + 1;
  12888. else
  12889. return ttSHORT(data + 22 + (m * 6));
  12890. }
  12891. return 0;
  12892. }
  12893. static stbtt_int32 stbtt__GetCoverageIndex(stbtt_uint8* coverageTable, int glyph)
  12894. {
  12895. stbtt_uint16 coverageFormat = ttUSHORT(coverageTable);
  12896. switch (coverageFormat) {
  12897. case 1: {
  12898. stbtt_uint16 glyphCount = ttUSHORT(coverageTable + 2);
  12899. /* Binary search. */
  12900. stbtt_int32 l = 0, r = glyphCount - 1, m;
  12901. int straw, needle = glyph;
  12902. while (l <= r) {
  12903. stbtt_uint8* glyphArray = coverageTable + 4;
  12904. stbtt_uint16 glyphID;
  12905. m = (l + r) >> 1;
  12906. glyphID = ttUSHORT(glyphArray + 2 * m);
  12907. straw = glyphID;
  12908. if (needle < straw)
  12909. r = m - 1;
  12910. else if (needle > straw)
  12911. l = m + 1;
  12912. else {
  12913. return m;
  12914. }
  12915. }
  12916. break;
  12917. }
  12918. case 2: {
  12919. stbtt_uint16 rangeCount = ttUSHORT(coverageTable + 2);
  12920. stbtt_uint8* rangeArray = coverageTable + 4;
  12921. /* Binary search. */
  12922. stbtt_int32 l = 0, r = rangeCount - 1, m;
  12923. int strawStart, strawEnd, needle = glyph;
  12924. while (l <= r) {
  12925. stbtt_uint8* rangeRecord;
  12926. m = (l + r) >> 1;
  12927. rangeRecord = rangeArray + 6 * m;
  12928. strawStart = ttUSHORT(rangeRecord);
  12929. strawEnd = ttUSHORT(rangeRecord + 2);
  12930. if (needle < strawStart)
  12931. r = m - 1;
  12932. else if (needle > strawEnd)
  12933. l = m + 1;
  12934. else {
  12935. stbtt_uint16 startCoverageIndex = ttUSHORT(rangeRecord + 4);
  12936. return startCoverageIndex + glyph - strawStart;
  12937. }
  12938. }
  12939. break;
  12940. }
  12941. default: return -1; /* unsupported */
  12942. }
  12943. return -1;
  12944. }
  12945. static stbtt_int32 stbtt__GetGlyphClass(stbtt_uint8* classDefTable, int glyph)
  12946. {
  12947. stbtt_uint16 classDefFormat = ttUSHORT(classDefTable);
  12948. switch (classDefFormat)
  12949. {
  12950. case 1: {
  12951. stbtt_uint16 startGlyphID = ttUSHORT(classDefTable + 2);
  12952. stbtt_uint16 glyphCount = ttUSHORT(classDefTable + 4);
  12953. stbtt_uint8* classDef1ValueArray = classDefTable + 6;
  12954. if (glyph >= startGlyphID && glyph < startGlyphID + glyphCount)
  12955. return (stbtt_int32)ttUSHORT(classDef1ValueArray + 2 * (glyph - startGlyphID));
  12956. break;
  12957. }
  12958. case 2: {
  12959. stbtt_uint16 classRangeCount = ttUSHORT(classDefTable + 2);
  12960. stbtt_uint8* classRangeRecords = classDefTable + 4;
  12961. /* Binary search. */
  12962. stbtt_int32 l = 0, r = classRangeCount - 1, m;
  12963. int strawStart, strawEnd, needle = glyph;
  12964. while (l <= r) {
  12965. stbtt_uint8* classRangeRecord;
  12966. m = (l + r) >> 1;
  12967. classRangeRecord = classRangeRecords + 6 * m;
  12968. strawStart = ttUSHORT(classRangeRecord);
  12969. strawEnd = ttUSHORT(classRangeRecord + 2);
  12970. if (needle < strawStart)
  12971. r = m - 1;
  12972. else if (needle > strawEnd)
  12973. l = m + 1;
  12974. else
  12975. return (stbtt_int32)ttUSHORT(classRangeRecord + 4);
  12976. }
  12977. break;
  12978. }
  12979. default:
  12980. return -1; /* Unsupported definition type, return an error. */
  12981. }
  12982. /* "All glyphs not assigned to a class fall into class 0". (OpenType spec) */
  12983. return 0;
  12984. }
  12985. /* Define to STBTT_assert(x) if you want to break on unimplemented formats. */
  12986. #define STBTT_GPOS_TODO_assert(x)
  12987. static stbtt_int32 stbtt__GetGlyphGPOSInfoAdvance(const stbtt_fontinfo* info, int glyph1, int glyph2)
  12988. {
  12989. stbtt_uint16 lookupListOffset;
  12990. stbtt_uint8* lookupList;
  12991. stbtt_uint16 lookupCount;
  12992. stbtt_uint8* data;
  12993. stbtt_int32 i, sti;
  12994. if (!info->gpos) return 0;
  12995. data = info->data + info->gpos;
  12996. if (ttUSHORT(data + 0) != 1) return 0; /* Major version 1 */
  12997. if (ttUSHORT(data + 2) != 0) return 0; /* Minor version 0 */
  12998. lookupListOffset = ttUSHORT(data + 8);
  12999. lookupList = data + lookupListOffset;
  13000. lookupCount = ttUSHORT(lookupList);
  13001. for (i = 0; i < lookupCount; ++i) {
  13002. stbtt_uint16 lookupOffset = ttUSHORT(lookupList + 2 + 2 * i);
  13003. stbtt_uint8* lookupTable = lookupList + lookupOffset;
  13004. stbtt_uint16 lookupType = ttUSHORT(lookupTable);
  13005. stbtt_uint16 subTableCount = ttUSHORT(lookupTable + 4);
  13006. stbtt_uint8* subTableOffsets = lookupTable + 6;
  13007. if (lookupType != 2) /* Pair Adjustment Positioning Subtable */
  13008. continue;
  13009. for (sti = 0; sti < subTableCount; sti++) {
  13010. stbtt_uint16 subtableOffset = ttUSHORT(subTableOffsets + 2 * sti);
  13011. stbtt_uint8* table = lookupTable + subtableOffset;
  13012. stbtt_uint16 posFormat = ttUSHORT(table);
  13013. stbtt_uint16 coverageOffset = ttUSHORT(table + 2);
  13014. stbtt_int32 coverageIndex = stbtt__GetCoverageIndex(table + coverageOffset, glyph1);
  13015. if (coverageIndex == -1) continue;
  13016. switch (posFormat) {
  13017. case 1: {
  13018. stbtt_int32 l, r, m;
  13019. int straw, needle;
  13020. stbtt_uint16 valueFormat1 = ttUSHORT(table + 4);
  13021. stbtt_uint16 valueFormat2 = ttUSHORT(table + 6);
  13022. if (valueFormat1 == 4 && valueFormat2 == 0) { /* Support more formats? */
  13023. stbtt_int32 valueRecordPairSizeInBytes = 2;
  13024. stbtt_uint16 pairSetCount = ttUSHORT(table + 8);
  13025. stbtt_uint16 pairPosOffset = ttUSHORT(table + 10 + 2 * coverageIndex);
  13026. stbtt_uint8* pairValueTable = table + pairPosOffset;
  13027. stbtt_uint16 pairValueCount = ttUSHORT(pairValueTable);
  13028. stbtt_uint8* pairValueArray = pairValueTable + 2;
  13029. if (coverageIndex >= pairSetCount) return 0;
  13030. needle = glyph2;
  13031. r = pairValueCount - 1;
  13032. l = 0;
  13033. /* Binary search. */
  13034. while (l <= r) {
  13035. stbtt_uint16 secondGlyph;
  13036. stbtt_uint8* pairValue;
  13037. m = (l + r) >> 1;
  13038. pairValue = pairValueArray + (2 + valueRecordPairSizeInBytes) * m;
  13039. secondGlyph = ttUSHORT(pairValue);
  13040. straw = secondGlyph;
  13041. if (needle < straw)
  13042. r = m - 1;
  13043. else if (needle > straw)
  13044. l = m + 1;
  13045. else {
  13046. stbtt_int16 xAdvance = ttSHORT(pairValue + 2);
  13047. return xAdvance;
  13048. }
  13049. }
  13050. }
  13051. else
  13052. return 0;
  13053. break;
  13054. }
  13055. case 2: {
  13056. stbtt_uint16 valueFormat1 = ttUSHORT(table + 4);
  13057. stbtt_uint16 valueFormat2 = ttUSHORT(table + 6);
  13058. if (valueFormat1 == 4 && valueFormat2 == 0) { /* Support more formats? */
  13059. stbtt_uint16 classDef1Offset = ttUSHORT(table + 8);
  13060. stbtt_uint16 classDef2Offset = ttUSHORT(table + 10);
  13061. int glyph1class = stbtt__GetGlyphClass(table + classDef1Offset, glyph1);
  13062. int glyph2class = stbtt__GetGlyphClass(table + classDef2Offset, glyph2);
  13063. stbtt_uint16 class1Count = ttUSHORT(table + 12);
  13064. stbtt_uint16 class2Count = ttUSHORT(table + 14);
  13065. stbtt_uint8* class1Records, * class2Records;
  13066. stbtt_int16 xAdvance;
  13067. if (glyph1class < 0 || glyph1class >= class1Count) return 0; /* malformed */
  13068. if (glyph2class < 0 || glyph2class >= class2Count) return 0; /* malformed */
  13069. class1Records = table + 16;
  13070. class2Records = class1Records + 2 * (glyph1class * class2Count);
  13071. xAdvance = ttSHORT(class2Records + 2 * glyph2class);
  13072. return xAdvance;
  13073. }
  13074. else
  13075. return 0;
  13076. break;
  13077. }
  13078. default:
  13079. return 0; /* Unsupported position format */
  13080. }
  13081. }
  13082. }
  13083. return 0;
  13084. }
  13085. STBTT_DEF int stbtt_GetGlyphKernAdvance(const stbtt_fontinfo* info, int g1, int g2)
  13086. {
  13087. int xAdvance = 0;
  13088. if (info->gpos)
  13089. xAdvance += stbtt__GetGlyphGPOSInfoAdvance(info, g1, g2);
  13090. else if (info->kern)
  13091. xAdvance += stbtt__GetGlyphKernInfoAdvance(info, g1, g2);
  13092. return xAdvance;
  13093. }
  13094. STBTT_DEF int stbtt_GetCodepointKernAdvance(const stbtt_fontinfo* info, int ch1, int ch2)
  13095. {
  13096. if (!info->kern && !info->gpos) /* if no kerning table, don't waste time looking up both codepoint->glyphs */
  13097. return 0;
  13098. return stbtt_GetGlyphKernAdvance(info, stbtt_FindGlyphIndex(info, ch1), stbtt_FindGlyphIndex(info, ch2));
  13099. }
  13100. STBTT_DEF void stbtt_GetCodepointHMetrics(const stbtt_fontinfo* info, int codepoint, int* advanceWidth, int* leftSideBearing)
  13101. {
  13102. stbtt_GetGlyphHMetrics(info, stbtt_FindGlyphIndex(info, codepoint), advanceWidth, leftSideBearing);
  13103. }
  13104. STBTT_DEF void stbtt_GetFontVMetrics(const stbtt_fontinfo* info, int* ascent, int* descent, int* lineGap)
  13105. {
  13106. if (ascent) *ascent = ttSHORT(info->data + info->hhea + 4);
  13107. if (descent) *descent = ttSHORT(info->data + info->hhea + 6);
  13108. if (lineGap) *lineGap = ttSHORT(info->data + info->hhea + 8);
  13109. }
  13110. STBTT_DEF int stbtt_GetFontVMetricsOS2(const stbtt_fontinfo* info, int* typoAscent, int* typoDescent, int* typoLineGap)
  13111. {
  13112. int tab = stbtt__find_table(info->data, info->fontstart, "OS/2");
  13113. if (!tab)
  13114. return 0;
  13115. if (typoAscent) *typoAscent = ttSHORT(info->data + tab + 68);
  13116. if (typoDescent) *typoDescent = ttSHORT(info->data + tab + 70);
  13117. if (typoLineGap) *typoLineGap = ttSHORT(info->data + tab + 72);
  13118. return 1;
  13119. }
  13120. STBTT_DEF void stbtt_GetFontBoundingBox(const stbtt_fontinfo* info, int* x0, int* y0, int* x1, int* y1)
  13121. {
  13122. *x0 = ttSHORT(info->data + info->head + 36);
  13123. *y0 = ttSHORT(info->data + info->head + 38);
  13124. *x1 = ttSHORT(info->data + info->head + 40);
  13125. *y1 = ttSHORT(info->data + info->head + 42);
  13126. }
  13127. STBTT_DEF float stbtt_ScaleForPixelHeight(const stbtt_fontinfo* info, float height)
  13128. {
  13129. int fheight = ttSHORT(info->data + info->hhea + 4) - ttSHORT(info->data + info->hhea + 6);
  13130. return (float)height / fheight;
  13131. }
  13132. STBTT_DEF float stbtt_ScaleForMappingEmToPixels(const stbtt_fontinfo* info, float pixels)
  13133. {
  13134. int unitsPerEm = ttUSHORT(info->data + info->head + 18);
  13135. return pixels / unitsPerEm;
  13136. }
  13137. STBTT_DEF void stbtt_FreeShape(const stbtt_fontinfo* info, stbtt_vertex* v)
  13138. {
  13139. STBTT_free(v, info->userdata);
  13140. }
  13141. STBTT_DEF stbtt_uint8* stbtt_FindSVGDoc(const stbtt_fontinfo* info, int gl)
  13142. {
  13143. int i;
  13144. stbtt_uint8* data = info->data;
  13145. stbtt_uint8* svg_doc_list = data + stbtt__get_svg((stbtt_fontinfo*)info);
  13146. int numEntries = ttUSHORT(svg_doc_list);
  13147. stbtt_uint8* svg_docs = svg_doc_list + 2;
  13148. for (i = 0; i < numEntries; i++) {
  13149. stbtt_uint8* svg_doc = svg_docs + (12 * i);
  13150. if ((gl >= ttUSHORT(svg_doc)) && (gl <= ttUSHORT(svg_doc + 2)))
  13151. return svg_doc;
  13152. }
  13153. return 0;
  13154. }
  13155. STBTT_DEF int stbtt_GetGlyphSVG(const stbtt_fontinfo* info, int gl, const char** svg)
  13156. {
  13157. stbtt_uint8* data = info->data;
  13158. stbtt_uint8* svg_doc;
  13159. if (info->svg == 0)
  13160. return 0;
  13161. svg_doc = stbtt_FindSVGDoc(info, gl);
  13162. if (svg_doc != NULL) {
  13163. *svg = (char*)data + info->svg + ttULONG(svg_doc + 4);
  13164. return ttULONG(svg_doc + 8);
  13165. }
  13166. else {
  13167. return 0;
  13168. }
  13169. }
  13170. STBTT_DEF int stbtt_GetCodepointSVG(const stbtt_fontinfo* info, int unicode_codepoint, const char** svg)
  13171. {
  13172. return stbtt_GetGlyphSVG(info, stbtt_FindGlyphIndex(info, unicode_codepoint), svg);
  13173. }
  13174. /* //////////////////////////////////////////////////////////////////////////// */
  13175. /* */
  13176. /* antialiasing software rasterizer */
  13177. /* */
  13178. STBTT_DEF void stbtt_GetGlyphBitmapBoxSubpixel(const stbtt_fontinfo* font, int glyph, float scale_x, float scale_y, float shift_x, float shift_y, int* ix0, int* iy0, int* ix1, int* iy1)
  13179. {
  13180. int x0 = 0, y0 = 0, x1, y1; /* =0 suppresses compiler warning */
  13181. if (!stbtt_GetGlyphBox(font, glyph, &x0, &y0, &x1, &y1)) {
  13182. /* e.g. space character */
  13183. if (ix0) *ix0 = 0;
  13184. if (iy0) *iy0 = 0;
  13185. if (ix1) *ix1 = 0;
  13186. if (iy1) *iy1 = 0;
  13187. }
  13188. else {
  13189. /* move to integral bboxes (treating pixels as little squares, what pixels get touched)? */
  13190. if (ix0) *ix0 = STBTT_ifloor(x0 * scale_x + shift_x);
  13191. if (iy0) *iy0 = STBTT_ifloor(-y1 * scale_y + shift_y);
  13192. if (ix1) *ix1 = STBTT_iceil(x1 * scale_x + shift_x);
  13193. if (iy1) *iy1 = STBTT_iceil(-y0 * scale_y + shift_y);
  13194. }
  13195. }
  13196. STBTT_DEF void stbtt_GetGlyphBitmapBox(const stbtt_fontinfo* font, int glyph, float scale_x, float scale_y, int* ix0, int* iy0, int* ix1, int* iy1)
  13197. {
  13198. stbtt_GetGlyphBitmapBoxSubpixel(font, glyph, scale_x, scale_y, 0.0f, 0.0f, ix0, iy0, ix1, iy1);
  13199. }
  13200. STBTT_DEF void stbtt_GetCodepointBitmapBoxSubpixel(const stbtt_fontinfo* font, int codepoint, float scale_x, float scale_y, float shift_x, float shift_y, int* ix0, int* iy0, int* ix1, int* iy1)
  13201. {
  13202. stbtt_GetGlyphBitmapBoxSubpixel(font, stbtt_FindGlyphIndex(font, codepoint), scale_x, scale_y, shift_x, shift_y, ix0, iy0, ix1, iy1);
  13203. }
  13204. STBTT_DEF void stbtt_GetCodepointBitmapBox(const stbtt_fontinfo* font, int codepoint, float scale_x, float scale_y, int* ix0, int* iy0, int* ix1, int* iy1)
  13205. {
  13206. stbtt_GetCodepointBitmapBoxSubpixel(font, codepoint, scale_x, scale_y, 0.0f, 0.0f, ix0, iy0, ix1, iy1);
  13207. }
  13208. /* //////////////////////////////////////////////////////////////////////////// */
  13209. /* */
  13210. /* Rasterizer */
  13211. typedef struct stbtt__hheap_chunk
  13212. {
  13213. struct stbtt__hheap_chunk* next;
  13214. } stbtt__hheap_chunk;
  13215. typedef struct stbtt__hheap
  13216. {
  13217. struct stbtt__hheap_chunk* head;
  13218. void* first_free;
  13219. int num_remaining_in_head_chunk;
  13220. } stbtt__hheap;
  13221. static void* stbtt__hheap_alloc(stbtt__hheap* hh, size_t size, void* userdata)
  13222. {
  13223. if (hh->first_free) {
  13224. void* p = hh->first_free;
  13225. hh->first_free = *(void**)p;
  13226. return p;
  13227. }
  13228. else {
  13229. if (hh->num_remaining_in_head_chunk == 0) {
  13230. int count = (size < 32 ? 2000 : size < 128 ? 800 : 100);
  13231. stbtt__hheap_chunk* c = (stbtt__hheap_chunk*)STBTT_malloc(sizeof(stbtt__hheap_chunk) + size * count, userdata);
  13232. if (c == NULL)
  13233. return NULL;
  13234. c->next = hh->head;
  13235. hh->head = c;
  13236. hh->num_remaining_in_head_chunk = count;
  13237. }
  13238. --hh->num_remaining_in_head_chunk;
  13239. return (char*)(hh->head) + sizeof(stbtt__hheap_chunk) + size * hh->num_remaining_in_head_chunk;
  13240. }
  13241. }
  13242. static void stbtt__hheap_free(stbtt__hheap* hh, void* p)
  13243. {
  13244. *(void**)p = hh->first_free;
  13245. hh->first_free = p;
  13246. }
  13247. static void stbtt__hheap_cleanup(stbtt__hheap* hh, void* userdata)
  13248. {
  13249. stbtt__hheap_chunk* c = hh->head;
  13250. while (c) {
  13251. stbtt__hheap_chunk* n = c->next;
  13252. STBTT_free(c, userdata);
  13253. c = n;
  13254. }
  13255. }
  13256. typedef struct stbtt__edge {
  13257. float x0, y0, x1, y1;
  13258. int invert;
  13259. } stbtt__edge;
  13260. typedef struct stbtt__active_edge
  13261. {
  13262. struct stbtt__active_edge* next;
  13263. #if STBTT_RASTERIZER_VERSION==1
  13264. int x, dx;
  13265. float ey;
  13266. int direction;
  13267. #elif STBTT_RASTERIZER_VERSION==2
  13268. float fx, fdx, fdy;
  13269. float direction;
  13270. float sy;
  13271. float ey;
  13272. #else
  13273. #error "Unrecognized value of STBTT_RASTERIZER_VERSION"
  13274. #endif
  13275. } stbtt__active_edge;
  13276. #if STBTT_RASTERIZER_VERSION == 1
  13277. #define STBTT_FIXSHIFT 10
  13278. #define STBTT_FIX (1 << STBTT_FIXSHIFT)
  13279. #define STBTT_FIXMASK (STBTT_FIX-1)
  13280. static stbtt__active_edge* stbtt__new_active(stbtt__hheap* hh, stbtt__edge* e, int off_x, float start_point, void* userdata)
  13281. {
  13282. stbtt__active_edge* z = (stbtt__active_edge*)stbtt__hheap_alloc(hh, sizeof(*z), userdata);
  13283. float dxdy = (e->x1 - e->x0) / (e->y1 - e->y0);
  13284. STBTT_assert(z != NULL);
  13285. if (!z) return z;
  13286. /* round dx down to avoid overshooting */
  13287. if (dxdy < 0)
  13288. z->dx = -STBTT_ifloor(STBTT_FIX * -dxdy);
  13289. else
  13290. z->dx = STBTT_ifloor(STBTT_FIX * dxdy);
  13291. z->x = STBTT_ifloor(STBTT_FIX * e->x0 + z->dx * (start_point - e->y0)); /* use z->dx so when we offset later it's by the same amount */
  13292. z->x -= off_x * STBTT_FIX;
  13293. z->ey = e->y1;
  13294. z->next = 0;
  13295. z->direction = e->invert ? 1 : -1;
  13296. return z;
  13297. }
  13298. #elif STBTT_RASTERIZER_VERSION == 2
  13299. static stbtt__active_edge* stbtt__new_active(stbtt__hheap* hh, stbtt__edge* e, int off_x, float start_point, void* userdata)
  13300. {
  13301. stbtt__active_edge* z = (stbtt__active_edge*)stbtt__hheap_alloc(hh, sizeof(*z), userdata);
  13302. float dxdy = (e->x1 - e->x0) / (e->y1 - e->y0);
  13303. STBTT_assert(z != NULL);
  13304. /* STBTT_assert(e->y0 <= start_point); */
  13305. if (!z) return z;
  13306. z->fdx = dxdy;
  13307. z->fdy = dxdy != 0.0f ? (1.0f / dxdy) : 0.0f;
  13308. z->fx = e->x0 + dxdy * (start_point - e->y0);
  13309. z->fx -= off_x;
  13310. z->direction = e->invert ? 1.0f : -1.0f;
  13311. z->sy = e->y0;
  13312. z->ey = e->y1;
  13313. z->next = 0;
  13314. return z;
  13315. }
  13316. #else
  13317. #error "Unrecognized value of STBTT_RASTERIZER_VERSION"
  13318. #endif
  13319. #if STBTT_RASTERIZER_VERSION == 1
  13320. /* note: this routine clips fills that extend off the edges... ideally this */
  13321. /* wouldn't happen, but it could happen if the truetype glyph bounding boxes */
  13322. /* are wrong, or if the user supplies a too-small bitmap */
  13323. static void stbtt__fill_active_edges(unsigned char* scanline, int len, stbtt__active_edge* e, int max_weight)
  13324. {
  13325. /* non-zero winding fill */
  13326. int x0 = 0, w = 0;
  13327. while (e) {
  13328. if (w == 0) {
  13329. /* if we're currently at zero, we need to record the edge start point */
  13330. x0 = e->x; w += e->direction;
  13331. }
  13332. else {
  13333. int x1 = e->x; w += e->direction;
  13334. /* if we went to zero, we need to draw */
  13335. if (w == 0) {
  13336. int i = x0 >> STBTT_FIXSHIFT;
  13337. int j = x1 >> STBTT_FIXSHIFT;
  13338. if (i < len && j >= 0) {
  13339. if (i == j) {
  13340. /* x0,x1 are the same pixel, so compute combined coverage */
  13341. scanline[i] = scanline[i] + (stbtt_uint8)((x1 - x0) * max_weight >> STBTT_FIXSHIFT);
  13342. }
  13343. else {
  13344. if (i >= 0) /* add antialiasing for x0 */
  13345. scanline[i] = scanline[i] + (stbtt_uint8)(((STBTT_FIX - (x0 & STBTT_FIXMASK)) * max_weight) >> STBTT_FIXSHIFT);
  13346. else
  13347. i = -1; /* clip */
  13348. if (j < len) /* add antialiasing for x1 */
  13349. scanline[j] = scanline[j] + (stbtt_uint8)(((x1 & STBTT_FIXMASK) * max_weight) >> STBTT_FIXSHIFT);
  13350. else
  13351. j = len; /* clip */
  13352. for (++i; i < j; ++i) /* fill pixels between x0 and x1 */
  13353. scanline[i] = scanline[i] + (stbtt_uint8)max_weight;
  13354. }
  13355. }
  13356. }
  13357. }
  13358. e = e->next;
  13359. }
  13360. }
  13361. static void stbtt__rasterize_sorted_edges(stbtt__bitmap* result, stbtt__edge* e, int n, int vsubsample, int off_x, int off_y, void* userdata)
  13362. {
  13363. stbtt__hheap hh = { 0, 0, 0 };
  13364. stbtt__active_edge* active = NULL;
  13365. int y, j = 0;
  13366. int max_weight = (255 / vsubsample); /* weight per vertical scanline */
  13367. int s; /* vertical subsample index */
  13368. unsigned char scanline_data[512], * scanline;
  13369. if (result->w > 512)
  13370. scanline = (unsigned char*)STBTT_malloc(result->w, userdata);
  13371. else
  13372. scanline = scanline_data;
  13373. y = off_y * vsubsample;
  13374. e[n].y0 = (off_y + result->h) * (float)vsubsample + 1;
  13375. while (j < result->h) {
  13376. STBTT_memset(scanline, 0, result->w);
  13377. for (s = 0; s < vsubsample; ++s) {
  13378. /* find center of pixel for this scanline */
  13379. float scan_y = y + 0.5f;
  13380. stbtt__active_edge** step = &active;
  13381. /* update all active edges; */
  13382. /* remove all active edges that terminate before the center of this scanline */
  13383. while (*step) {
  13384. stbtt__active_edge* z = *step;
  13385. if (z->ey <= scan_y) {
  13386. *step = z->next; /* delete from list */
  13387. STBTT_assert(z->direction);
  13388. z->direction = 0;
  13389. stbtt__hheap_free(&hh, z);
  13390. }
  13391. else {
  13392. z->x += z->dx; /* advance to position for current scanline */
  13393. step = &((*step)->next); /* advance through list */
  13394. }
  13395. }
  13396. /* resort the list if needed */
  13397. for (;;) {
  13398. int changed = 0;
  13399. step = &active;
  13400. while (*step && (*step)->next) {
  13401. if ((*step)->x > (*step)->next->x) {
  13402. stbtt__active_edge* t = *step;
  13403. stbtt__active_edge* q = t->next;
  13404. t->next = q->next;
  13405. q->next = t;
  13406. *step = q;
  13407. changed = 1;
  13408. }
  13409. step = &(*step)->next;
  13410. }
  13411. if (!changed) break;
  13412. }
  13413. /* insert all edges that start before the center of this scanline -- omit ones that also end on this scanline */
  13414. while (e->y0 <= scan_y) {
  13415. if (e->y1 > scan_y) {
  13416. stbtt__active_edge* z = stbtt__new_active(&hh, e, off_x, scan_y, userdata);
  13417. if (z != NULL) {
  13418. /* find insertion point */
  13419. if (active == NULL)
  13420. active = z;
  13421. else if (z->x < active->x) {
  13422. /* insert at front */
  13423. z->next = active;
  13424. active = z;
  13425. }
  13426. else {
  13427. /* find thing to insert AFTER */
  13428. stbtt__active_edge* p = active;
  13429. while (p->next && p->next->x < z->x)
  13430. p = p->next;
  13431. /* at this point, p->next->x is NOT < z->x */
  13432. z->next = p->next;
  13433. p->next = z;
  13434. }
  13435. }
  13436. }
  13437. ++e;
  13438. }
  13439. /* now process all active edges in XOR fashion */
  13440. if (active)
  13441. stbtt__fill_active_edges(scanline, result->w, active, max_weight);
  13442. ++y;
  13443. }
  13444. STBTT_memcpy(result->pixels + j * result->stride, scanline, result->w);
  13445. ++j;
  13446. }
  13447. stbtt__hheap_cleanup(&hh, userdata);
  13448. if (scanline != scanline_data)
  13449. STBTT_free(scanline, userdata);
  13450. }
  13451. #elif STBTT_RASTERIZER_VERSION == 2
  13452. /* the edge passed in here does not cross the vertical line at x or the vertical line at x+1 */
  13453. /* (i.e. it has already been clipped to those) */
  13454. static void stbtt__handle_clipped_edge(float* scanline, int x, stbtt__active_edge* e, float x0, float y0, float x1, float y1)
  13455. {
  13456. if (y0 == y1) return;
  13457. STBTT_assert(y0 < y1);
  13458. STBTT_assert(e->sy <= e->ey);
  13459. if (y0 > e->ey) return;
  13460. if (y1 < e->sy) return;
  13461. if (y0 < e->sy) {
  13462. x0 += (x1 - x0) * (e->sy - y0) / (y1 - y0);
  13463. y0 = e->sy;
  13464. }
  13465. if (y1 > e->ey) {
  13466. x1 += (x1 - x0) * (e->ey - y1) / (y1 - y0);
  13467. y1 = e->ey;
  13468. }
  13469. if (x0 == x)
  13470. STBTT_assert(x1 <= x + 1);
  13471. else if (x0 == x + 1)
  13472. STBTT_assert(x1 >= x);
  13473. else if (x0 <= x)
  13474. STBTT_assert(x1 <= x);
  13475. else if (x0 >= x + 1)
  13476. STBTT_assert(x1 >= x + 1);
  13477. else
  13478. STBTT_assert(x1 >= x && x1 <= x + 1);
  13479. if (x0 <= x && x1 <= x)
  13480. scanline[x] += e->direction * (y1 - y0);
  13481. else if (x0 >= x + 1 && x1 >= x + 1)
  13482. ;
  13483. else {
  13484. STBTT_assert(x0 >= x && x0 <= x + 1 && x1 >= x && x1 <= x + 1);
  13485. scanline[x] += e->direction * (y1 - y0) * (1 - ((x0 - x) + (x1 - x)) / 2); /* coverage = 1 - average x position */
  13486. }
  13487. }
  13488. static float stbtt__sized_trapezoid_area(float height, float top_width, float bottom_width)
  13489. {
  13490. STBTT_assert(top_width >= 0);
  13491. STBTT_assert(bottom_width >= 0);
  13492. return (top_width + bottom_width) / 2.0f * height;
  13493. }
  13494. static float stbtt__position_trapezoid_area(float height, float tx0, float tx1, float bx0, float bx1)
  13495. {
  13496. return stbtt__sized_trapezoid_area(height, tx1 - tx0, bx1 - bx0);
  13497. }
  13498. static float stbtt__sized_triangle_area(float height, float width)
  13499. {
  13500. return height * width / 2;
  13501. }
  13502. static void stbtt__fill_active_edges_new(float* scanline, float* scanline_fill, int len, stbtt__active_edge* e, float y_top)
  13503. {
  13504. float y_bottom = y_top + 1;
  13505. while (e) {
  13506. /* brute force every pixel */
  13507. /* compute intersection points with top & bottom */
  13508. STBTT_assert(e->ey >= y_top);
  13509. if (e->fdx == 0) {
  13510. float x0 = e->fx;
  13511. if (x0 < len) {
  13512. if (x0 >= 0) {
  13513. stbtt__handle_clipped_edge(scanline, (int)x0, e, x0, y_top, x0, y_bottom);
  13514. stbtt__handle_clipped_edge(scanline_fill - 1, (int)x0 + 1, e, x0, y_top, x0, y_bottom);
  13515. }
  13516. else {
  13517. stbtt__handle_clipped_edge(scanline_fill - 1, 0, e, x0, y_top, x0, y_bottom);
  13518. }
  13519. }
  13520. }
  13521. else {
  13522. float x0 = e->fx;
  13523. float dx = e->fdx;
  13524. float xb = x0 + dx;
  13525. float x_top, x_bottom;
  13526. float sy0, sy1;
  13527. float dy = e->fdy;
  13528. STBTT_assert(e->sy <= y_bottom && e->ey >= y_top);
  13529. /* compute endpoints of line segment clipped to this scanline (if the */
  13530. /* line segment starts on this scanline. x0 is the intersection of the */
  13531. /* line with y_top, but that may be off the line segment. */
  13532. if (e->sy > y_top) {
  13533. x_top = x0 + dx * (e->sy - y_top);
  13534. sy0 = e->sy;
  13535. }
  13536. else {
  13537. x_top = x0;
  13538. sy0 = y_top;
  13539. }
  13540. if (e->ey < y_bottom) {
  13541. x_bottom = x0 + dx * (e->ey - y_top);
  13542. sy1 = e->ey;
  13543. }
  13544. else {
  13545. x_bottom = xb;
  13546. sy1 = y_bottom;
  13547. }
  13548. if (x_top >= 0 && x_bottom >= 0 && x_top < len && x_bottom < len) {
  13549. /* from here on, we don't have to range check x values */
  13550. if ((int)x_top == (int)x_bottom) {
  13551. float height;
  13552. /* simple case, only spans one pixel */
  13553. int x = (int)x_top;
  13554. height = (sy1 - sy0) * e->direction;
  13555. STBTT_assert(x >= 0 && x < len);
  13556. scanline[x] += stbtt__position_trapezoid_area(height, x_top, x + 1.0f, x_bottom, x + 1.0f);
  13557. scanline_fill[x] += height; /* everything right of this pixel is filled */
  13558. }
  13559. else {
  13560. int x, x1, x2;
  13561. float y_crossing, y_final, step, sign, area;
  13562. /* covers 2+ pixels */
  13563. if (x_top > x_bottom) {
  13564. /* flip scanline vertically; signed area is the same */
  13565. float t;
  13566. sy0 = y_bottom - (sy0 - y_top);
  13567. sy1 = y_bottom - (sy1 - y_top);
  13568. t = sy0, sy0 = sy1, sy1 = t;
  13569. t = x_bottom, x_bottom = x_top, x_top = t;
  13570. dx = -dx;
  13571. dy = -dy;
  13572. t = x0, x0 = xb, xb = t;
  13573. }
  13574. STBTT_assert(dy >= 0);
  13575. STBTT_assert(dx >= 0);
  13576. x1 = (int)x_top;
  13577. x2 = (int)x_bottom;
  13578. /* compute intersection with y axis at x1+1 */
  13579. y_crossing = y_top + dy * (x1 + 1 - x0);
  13580. /* compute intersection with y axis at x2 */
  13581. y_final = y_top + dy * (x2 - x0);
  13582. /* x1 x_top x2 x_bottom */
  13583. /* y_top +------|-----+------------+------------+--------|---+------------+ */
  13584. /* | | | | | | */
  13585. /* | | | | | | */
  13586. /* sy0 | Txxxxx|............|............|............|............| */
  13587. /* y_crossing | *xxxxx.......|............|............|............| */
  13588. /* | | xxxxx..|............|............|............| */
  13589. /* | | /- xx*xxxx........|............|............| */
  13590. /* | | dy < | xxxxxx..|............|............| */
  13591. /* y_final | | \- | xx*xxx.........|............| */
  13592. /* sy1 | | | | xxxxxB...|............| */
  13593. /* | | | | | | */
  13594. /* | | | | | | */
  13595. /* y_bottom +------------+------------+------------+------------+------------+ */
  13596. /* */
  13597. /* goal is to measure the area covered by '.' in each pixel */
  13598. /* if x2 is right at the right edge of x1, y_crossing can blow up, github #1057 */
  13599. /* @TODO: maybe test against sy1 rather than y_bottom? */
  13600. if (y_crossing > y_bottom)
  13601. y_crossing = y_bottom;
  13602. sign = e->direction;
  13603. /* area of the rectangle covered from sy0..y_crossing */
  13604. area = sign * (y_crossing - sy0);
  13605. /* area of the triangle (x_top,sy0), (x1+1,sy0), (x1+1,y_crossing) */
  13606. scanline[x1] += stbtt__sized_triangle_area(area, x1 + 1 - x_top);
  13607. /* check if final y_crossing is blown up; no test case for this */
  13608. if (y_final > y_bottom) {
  13609. y_final = y_bottom;
  13610. dy = (y_final - y_crossing) / (x2 - (x1 + 1)); /* if denom=0, y_final = y_crossing, so y_final <= y_bottom */
  13611. }
  13612. /* in second pixel, area covered by line segment found in first pixel */
  13613. /* is always a rectangle 1 wide * the height of that line segment; this */
  13614. /* is exactly what the variable 'area' stores. it also gets a contribution */
  13615. /* from the line segment within it. the THIRD pixel will get the first */
  13616. /* pixel's rectangle contribution, the second pixel's rectangle contribution, */
  13617. /* and its own contribution. the 'own contribution' is the same in every pixel except */
  13618. /* the leftmost and rightmost, a trapezoid that slides down in each pixel. */
  13619. /* the second pixel's contribution to the third pixel will be the */
  13620. /* rectangle 1 wide times the height change in the second pixel, which is dy. */
  13621. step = sign * dy * 1; /* dy is dy/dx, change in y for every 1 change in x, */
  13622. /* which multiplied by 1-pixel-width is how much pixel area changes for each step in x */
  13623. /* so the area advances by 'step' every time */
  13624. for (x = x1 + 1; x < x2; ++x) {
  13625. scanline[x] += area + step / 2; /* area of trapezoid is 1*step/2 */
  13626. area += step;
  13627. }
  13628. STBTT_assert(STBTT_fabs(area) <= 1.01f); /* accumulated error from area += step unless we round step down */
  13629. STBTT_assert(sy1 > y_final - 0.01f);
  13630. /* area covered in the last pixel is the rectangle from all the pixels to the left, */
  13631. /* plus the trapezoid filled by the line segment in this pixel all the way to the right edge */
  13632. scanline[x2] += area + sign * stbtt__position_trapezoid_area(sy1 - y_final, (float)x2, x2 + 1.0f, x_bottom, x2 + 1.0f);
  13633. /* the rest of the line is filled based on the total height of the line segment in this pixel */
  13634. scanline_fill[x2] += sign * (sy1 - sy0);
  13635. }
  13636. }
  13637. else {
  13638. /* if edge goes outside of box we're drawing, we require */
  13639. /* clipping logic. since this does not match the intended use */
  13640. /* of this library, we use a different, very slow brute */
  13641. /* force implementation */
  13642. /* note though that this does happen some of the time because */
  13643. /* x_top and x_bottom can be extrapolated at the top & bottom of */
  13644. /* the shape and actually lie outside the bounding box */
  13645. int x;
  13646. for (x = 0; x < len; ++x) {
  13647. /* cases: */
  13648. /* */
  13649. /* there can be up to two intersections with the pixel. any intersection */
  13650. /* with left or right edges can be handled by splitting into two (or three) */
  13651. /* regions. intersections with top & bottom do not necessitate case-wise logic. */
  13652. /* */
  13653. /* the old way of doing this found the intersections with the left & right edges, */
  13654. /* then used some simple logic to produce up to three segments in sorted order */
  13655. /* from top-to-bottom. however, this had a problem: if an x edge was epsilon */
  13656. /* across the x border, then the corresponding y position might not be distinct */
  13657. /* from the other y segment, and it might ignored as an empty segment. to avoid */
  13658. /* that, we need to explicitly produce segments based on x positions. */
  13659. /* rename variables to clearly-defined pairs */
  13660. float y0 = y_top;
  13661. float x1 = (float)(x);
  13662. float x2 = (float)(x + 1);
  13663. float x3 = xb;
  13664. float y3 = y_bottom;
  13665. /* x = e->x + e->dx * (y-y_top) */
  13666. /* (y-y_top) = (x - e->x) / e->dx */
  13667. /* y = (x - e->x) / e->dx + y_top */
  13668. float y1 = (x - x0) / dx + y_top;
  13669. float y2 = (x + 1 - x0) / dx + y_top;
  13670. if (x0 < x1 && x3 > x2) { /* three segments descending down-right */
  13671. stbtt__handle_clipped_edge(scanline, x, e, x0, y0, x1, y1);
  13672. stbtt__handle_clipped_edge(scanline, x, e, x1, y1, x2, y2);
  13673. stbtt__handle_clipped_edge(scanline, x, e, x2, y2, x3, y3);
  13674. }
  13675. else if (x3 < x1 && x0 > x2) { /* three segments descending down-left */
  13676. stbtt__handle_clipped_edge(scanline, x, e, x0, y0, x2, y2);
  13677. stbtt__handle_clipped_edge(scanline, x, e, x2, y2, x1, y1);
  13678. stbtt__handle_clipped_edge(scanline, x, e, x1, y1, x3, y3);
  13679. }
  13680. else if (x0 < x1 && x3 > x1) { /* two segments across x, down-right */
  13681. stbtt__handle_clipped_edge(scanline, x, e, x0, y0, x1, y1);
  13682. stbtt__handle_clipped_edge(scanline, x, e, x1, y1, x3, y3);
  13683. }
  13684. else if (x3 < x1 && x0 > x1) { /* two segments across x, down-left */
  13685. stbtt__handle_clipped_edge(scanline, x, e, x0, y0, x1, y1);
  13686. stbtt__handle_clipped_edge(scanline, x, e, x1, y1, x3, y3);
  13687. }
  13688. else if (x0 < x2 && x3 > x2) { /* two segments across x+1, down-right */
  13689. stbtt__handle_clipped_edge(scanline, x, e, x0, y0, x2, y2);
  13690. stbtt__handle_clipped_edge(scanline, x, e, x2, y2, x3, y3);
  13691. }
  13692. else if (x3 < x2 && x0 > x2) { /* two segments across x+1, down-left */
  13693. stbtt__handle_clipped_edge(scanline, x, e, x0, y0, x2, y2);
  13694. stbtt__handle_clipped_edge(scanline, x, e, x2, y2, x3, y3);
  13695. }
  13696. else { /* one segment */
  13697. stbtt__handle_clipped_edge(scanline, x, e, x0, y0, x3, y3);
  13698. }
  13699. }
  13700. }
  13701. }
  13702. e = e->next;
  13703. }
  13704. }
  13705. /* directly AA rasterize edges w/o supersampling */
  13706. static void stbtt__rasterize_sorted_edges(stbtt__bitmap* result, stbtt__edge* e, int n, int vsubsample, int off_x, int off_y, void* userdata)
  13707. {
  13708. stbtt__hheap hh = { 0, 0, 0 };
  13709. stbtt__active_edge* active = NULL;
  13710. int y, j = 0, i;
  13711. float scanline_data[129], * scanline, * scanline2;
  13712. STBTT__NOTUSED(vsubsample);
  13713. if (result->w > 64)
  13714. scanline = (float*)STBTT_malloc((result->w * 2 + 1) * sizeof(float), userdata);
  13715. else
  13716. scanline = scanline_data;
  13717. scanline2 = scanline + result->w;
  13718. y = off_y;
  13719. e[n].y0 = (float)(off_y + result->h) + 1;
  13720. while (j < result->h) {
  13721. /* find center of pixel for this scanline */
  13722. float scan_y_top = y + 0.0f;
  13723. float scan_y_bottom = y + 1.0f;
  13724. stbtt__active_edge** step = &active;
  13725. STBTT_memset(scanline, 0, result->w * sizeof(scanline[0]));
  13726. STBTT_memset(scanline2, 0, (result->w + 1) * sizeof(scanline[0]));
  13727. /* update all active edges; */
  13728. /* remove all active edges that terminate before the top of this scanline */
  13729. while (*step) {
  13730. stbtt__active_edge* z = *step;
  13731. if (z->ey <= scan_y_top) {
  13732. *step = z->next; /* delete from list */
  13733. STBTT_assert(z->direction);
  13734. z->direction = 0;
  13735. stbtt__hheap_free(&hh, z);
  13736. }
  13737. else {
  13738. step = &((*step)->next); /* advance through list */
  13739. }
  13740. }
  13741. /* insert all edges that start before the bottom of this scanline */
  13742. while (e->y0 <= scan_y_bottom) {
  13743. if (e->y0 != e->y1) {
  13744. stbtt__active_edge* z = stbtt__new_active(&hh, e, off_x, scan_y_top, userdata);
  13745. if (z != NULL) {
  13746. if (j == 0 && off_y != 0) {
  13747. if (z->ey < scan_y_top) {
  13748. /* this can happen due to subpixel positioning and some kind of fp rounding error i think */
  13749. z->ey = scan_y_top;
  13750. }
  13751. }
  13752. STBTT_assert(z->ey >= scan_y_top); /* if we get really unlucky a tiny bit of an edge can be out of bounds */
  13753. /* insert at front */
  13754. z->next = active;
  13755. active = z;
  13756. }
  13757. }
  13758. ++e;
  13759. }
  13760. /* now process all active edges */
  13761. if (active)
  13762. stbtt__fill_active_edges_new(scanline, scanline2 + 1, result->w, active, scan_y_top);
  13763. {
  13764. float sum = 0;
  13765. for (i = 0; i < result->w; ++i) {
  13766. float k;
  13767. int m;
  13768. sum += scanline2[i];
  13769. k = scanline[i] + sum;
  13770. k = (float)STBTT_fabs(k) * 255 + 0.5f;
  13771. m = (int)k;
  13772. if (m > 255) m = 255;
  13773. result->pixels[j * result->stride + i] = (unsigned char)m;
  13774. }
  13775. }
  13776. /* advance all the edges */
  13777. step = &active;
  13778. while (*step) {
  13779. stbtt__active_edge* z = *step;
  13780. z->fx += z->fdx; /* advance to position for current scanline */
  13781. step = &((*step)->next); /* advance through list */
  13782. }
  13783. ++y;
  13784. ++j;
  13785. }
  13786. stbtt__hheap_cleanup(&hh, userdata);
  13787. if (scanline != scanline_data)
  13788. STBTT_free(scanline, userdata);
  13789. }
  13790. #else
  13791. #error "Unrecognized value of STBTT_RASTERIZER_VERSION"
  13792. #endif
  13793. #define STBTT__COMPARE(a,b) ((a)->y0 < (b)->y0)
  13794. static void stbtt__sort_edges_ins_sort(stbtt__edge* p, int n)
  13795. {
  13796. int i, j;
  13797. for (i = 1; i < n; ++i) {
  13798. stbtt__edge t = p[i], * a = &t;
  13799. j = i;
  13800. while (j > 0) {
  13801. stbtt__edge* b = &p[j - 1];
  13802. int c = STBTT__COMPARE(a, b);
  13803. if (!c) break;
  13804. p[j] = p[j - 1];
  13805. --j;
  13806. }
  13807. if (i != j)
  13808. p[j] = t;
  13809. }
  13810. }
  13811. static void stbtt__sort_edges_quicksort(stbtt__edge* p, int n)
  13812. {
  13813. /* threshold for transitioning to insertion sort */
  13814. while (n > 12) {
  13815. stbtt__edge t;
  13816. int c01, c12, c, m, i, j;
  13817. /* compute median of three */
  13818. m = n >> 1;
  13819. c01 = STBTT__COMPARE(&p[0], &p[m]);
  13820. c12 = STBTT__COMPARE(&p[m], &p[n - 1]);
  13821. /* if 0 >= mid >= end, or 0 < mid < end, then use mid */
  13822. if (c01 != c12) {
  13823. /* otherwise, we'll need to swap something else to middle */
  13824. int z;
  13825. c = STBTT__COMPARE(&p[0], &p[n - 1]);
  13826. /* 0>mid && mid<n: 0>n => n; 0<n => 0 */
  13827. /* 0<mid && mid>n: 0>n => 0; 0<n => n */
  13828. z = (c == c12) ? 0 : n - 1;
  13829. t = p[z];
  13830. p[z] = p[m];
  13831. p[m] = t;
  13832. }
  13833. /* now p[m] is the median-of-three */
  13834. /* swap it to the beginning so it won't move around */
  13835. t = p[0];
  13836. p[0] = p[m];
  13837. p[m] = t;
  13838. /* partition loop */
  13839. i = 1;
  13840. j = n - 1;
  13841. for (;;) {
  13842. /* handling of equality is crucial here */
  13843. /* for sentinels & efficiency with duplicates */
  13844. for (;; ++i) {
  13845. if (!STBTT__COMPARE(&p[i], &p[0])) break;
  13846. }
  13847. for (;; --j) {
  13848. if (!STBTT__COMPARE(&p[0], &p[j])) break;
  13849. }
  13850. /* make sure we haven't crossed */
  13851. if (i >= j) break;
  13852. t = p[i];
  13853. p[i] = p[j];
  13854. p[j] = t;
  13855. ++i;
  13856. --j;
  13857. }
  13858. /* recurse on smaller side, iterate on larger */
  13859. if (j < (n - i)) {
  13860. stbtt__sort_edges_quicksort(p, j);
  13861. p = p + i;
  13862. n = n - i;
  13863. }
  13864. else {
  13865. stbtt__sort_edges_quicksort(p + i, n - i);
  13866. n = j;
  13867. }
  13868. }
  13869. }
  13870. static void stbtt__sort_edges(stbtt__edge* p, int n)
  13871. {
  13872. stbtt__sort_edges_quicksort(p, n);
  13873. stbtt__sort_edges_ins_sort(p, n);
  13874. }
  13875. typedef struct
  13876. {
  13877. float x, y;
  13878. } stbtt__point;
  13879. static void stbtt__rasterize(stbtt__bitmap* result, stbtt__point* pts, int* wcount, int windings, float scale_x, float scale_y, float shift_x, float shift_y, int off_x, int off_y, int invert, void* userdata)
  13880. {
  13881. float y_scale_inv = invert ? -scale_y : scale_y;
  13882. stbtt__edge* e;
  13883. int n, i, j, k, m;
  13884. #if STBTT_RASTERIZER_VERSION == 1
  13885. int vsubsample = result->h < 8 ? 15 : 5;
  13886. #elif STBTT_RASTERIZER_VERSION == 2
  13887. int vsubsample = 1;
  13888. #else
  13889. #error "Unrecognized value of STBTT_RASTERIZER_VERSION"
  13890. #endif
  13891. /* vsubsample should divide 255 evenly; otherwise we won't reach full opacity */
  13892. /* now we have to blow out the windings into explicit edge lists */
  13893. n = 0;
  13894. for (i = 0; i < windings; ++i)
  13895. n += wcount[i];
  13896. e = (stbtt__edge*)STBTT_malloc(sizeof(*e) * (n + 1), userdata); /* add an extra one as a sentinel */
  13897. if (e == 0) return;
  13898. n = 0;
  13899. m = 0;
  13900. for (i = 0; i < windings; ++i) {
  13901. stbtt__point* p = pts + m;
  13902. m += wcount[i];
  13903. j = wcount[i] - 1;
  13904. for (k = 0; k < wcount[i]; j = k++) {
  13905. int a = k, b = j;
  13906. /* skip the edge if horizontal */
  13907. if (p[j].y == p[k].y)
  13908. continue;
  13909. /* add edge from j to k to the list */
  13910. e[n].invert = 0;
  13911. if (invert ? p[j].y > p[k].y : p[j].y < p[k].y) {
  13912. e[n].invert = 1;
  13913. a = j, b = k;
  13914. }
  13915. e[n].x0 = p[a].x * scale_x + shift_x;
  13916. e[n].y0 = (p[a].y * y_scale_inv + shift_y) * vsubsample;
  13917. e[n].x1 = p[b].x * scale_x + shift_x;
  13918. e[n].y1 = (p[b].y * y_scale_inv + shift_y) * vsubsample;
  13919. ++n;
  13920. }
  13921. }
  13922. /* now sort the edges by their highest point (should snap to integer, and then by x) */
  13923. /* STBTT_sort(e, n, sizeof(e[0]), stbtt__edge_compare); */
  13924. stbtt__sort_edges(e, n);
  13925. /* now, traverse the scanlines and find the intersections on each scanline, use xor winding rule */
  13926. stbtt__rasterize_sorted_edges(result, e, n, vsubsample, off_x, off_y, userdata);
  13927. STBTT_free(e, userdata);
  13928. }
  13929. static void stbtt__add_point(stbtt__point* points, int n, float x, float y)
  13930. {
  13931. if (!points) return; /* during first pass, it's unallocated */
  13932. points[n].x = x;
  13933. points[n].y = y;
  13934. }
  13935. /* tessellate until threshold p is happy... @TODO warped to compensate for non-linear stretching */
  13936. static int stbtt__tesselate_curve(stbtt__point* points, int* num_points, float x0, float y0, float x1, float y1, float x2, float y2, float objspace_flatness_squared, int n)
  13937. {
  13938. /* midpoint */
  13939. float mx = (x0 + 2 * x1 + x2) / 4;
  13940. float my = (y0 + 2 * y1 + y2) / 4;
  13941. /* versus directly drawn line */
  13942. float dx = (x0 + x2) / 2 - mx;
  13943. float dy = (y0 + y2) / 2 - my;
  13944. if (n > 16) /* 65536 segments on one curve better be enough! */
  13945. return 1;
  13946. if (dx * dx + dy * dy > objspace_flatness_squared) { /* half-pixel error allowed... need to be smaller if AA */
  13947. stbtt__tesselate_curve(points, num_points, x0, y0, (x0 + x1) / 2.0f, (y0 + y1) / 2.0f, mx, my, objspace_flatness_squared, n + 1);
  13948. stbtt__tesselate_curve(points, num_points, mx, my, (x1 + x2) / 2.0f, (y1 + y2) / 2.0f, x2, y2, objspace_flatness_squared, n + 1);
  13949. }
  13950. else {
  13951. stbtt__add_point(points, *num_points, x2, y2);
  13952. *num_points = *num_points + 1;
  13953. }
  13954. return 1;
  13955. }
  13956. static void stbtt__tesselate_cubic(stbtt__point* points, int* num_points, float x0, float y0, float x1, float y1, float x2, float y2, float x3, float y3, float objspace_flatness_squared, int n)
  13957. {
  13958. /* @TODO this "flatness" calculation is just made-up nonsense that seems to work well enough */
  13959. float dx0 = x1 - x0;
  13960. float dy0 = y1 - y0;
  13961. float dx1 = x2 - x1;
  13962. float dy1 = y2 - y1;
  13963. float dx2 = x3 - x2;
  13964. float dy2 = y3 - y2;
  13965. float dx = x3 - x0;
  13966. float dy = y3 - y0;
  13967. float longlen = (float)(STBTT_sqrt(dx0 * dx0 + dy0 * dy0) + STBTT_sqrt(dx1 * dx1 + dy1 * dy1) + STBTT_sqrt(dx2 * dx2 + dy2 * dy2));
  13968. float shortlen = (float)STBTT_sqrt(dx * dx + dy * dy);
  13969. float flatness_squared = longlen * longlen - shortlen * shortlen;
  13970. if (n > 16) /* 65536 segments on one curve better be enough! */
  13971. return;
  13972. if (flatness_squared > objspace_flatness_squared) {
  13973. float x01 = (x0 + x1) / 2;
  13974. float y01 = (y0 + y1) / 2;
  13975. float x12 = (x1 + x2) / 2;
  13976. float y12 = (y1 + y2) / 2;
  13977. float x23 = (x2 + x3) / 2;
  13978. float y23 = (y2 + y3) / 2;
  13979. float xa = (x01 + x12) / 2;
  13980. float ya = (y01 + y12) / 2;
  13981. float xb = (x12 + x23) / 2;
  13982. float yb = (y12 + y23) / 2;
  13983. float mx = (xa + xb) / 2;
  13984. float my = (ya + yb) / 2;
  13985. stbtt__tesselate_cubic(points, num_points, x0, y0, x01, y01, xa, ya, mx, my, objspace_flatness_squared, n + 1);
  13986. stbtt__tesselate_cubic(points, num_points, mx, my, xb, yb, x23, y23, x3, y3, objspace_flatness_squared, n + 1);
  13987. }
  13988. else {
  13989. stbtt__add_point(points, *num_points, x3, y3);
  13990. *num_points = *num_points + 1;
  13991. }
  13992. }
  13993. /* returns number of contours */
  13994. static stbtt__point* stbtt_FlattenCurves(stbtt_vertex* vertices, int num_verts, float objspace_flatness, int** contour_lengths, int* num_contours, void* userdata)
  13995. {
  13996. stbtt__point* points = 0;
  13997. int num_points = 0;
  13998. float objspace_flatness_squared = objspace_flatness * objspace_flatness;
  13999. int i, n = 0, start = 0, pass;
  14000. /* count how many "moves" there are to get the contour count */
  14001. for (i = 0; i < num_verts; ++i)
  14002. if (vertices[i].type == STBTT_vmove)
  14003. ++n;
  14004. *num_contours = n;
  14005. if (n == 0) return 0;
  14006. *contour_lengths = (int*)STBTT_malloc(sizeof(**contour_lengths) * n, userdata);
  14007. if (*contour_lengths == 0) {
  14008. *num_contours = 0;
  14009. return 0;
  14010. }
  14011. /* make two passes through the points so we don't need to realloc */
  14012. for (pass = 0; pass < 2; ++pass) {
  14013. float x = 0, y = 0;
  14014. if (pass == 1) {
  14015. points = (stbtt__point*)STBTT_malloc(num_points * sizeof(points[0]), userdata);
  14016. if (points == NULL) goto error;
  14017. }
  14018. num_points = 0;
  14019. n = -1;
  14020. for (i = 0; i < num_verts; ++i) {
  14021. switch (vertices[i].type) {
  14022. case STBTT_vmove:
  14023. /* start the next contour */
  14024. if (n >= 0)
  14025. (*contour_lengths)[n] = num_points - start;
  14026. ++n;
  14027. start = num_points;
  14028. x = vertices[i].x, y = vertices[i].y;
  14029. stbtt__add_point(points, num_points++, x, y);
  14030. break;
  14031. case STBTT_vline:
  14032. x = vertices[i].x, y = vertices[i].y;
  14033. stbtt__add_point(points, num_points++, x, y);
  14034. break;
  14035. case STBTT_vcurve:
  14036. stbtt__tesselate_curve(points, &num_points, x, y,
  14037. vertices[i].cx, vertices[i].cy,
  14038. vertices[i].x, vertices[i].y,
  14039. objspace_flatness_squared, 0);
  14040. x = vertices[i].x, y = vertices[i].y;
  14041. break;
  14042. case STBTT_vcubic:
  14043. stbtt__tesselate_cubic(points, &num_points, x, y,
  14044. vertices[i].cx, vertices[i].cy,
  14045. vertices[i].cx1, vertices[i].cy1,
  14046. vertices[i].x, vertices[i].y,
  14047. objspace_flatness_squared, 0);
  14048. x = vertices[i].x, y = vertices[i].y;
  14049. break;
  14050. }
  14051. }
  14052. (*contour_lengths)[n] = num_points - start;
  14053. }
  14054. return points;
  14055. error:
  14056. STBTT_free(points, userdata);
  14057. STBTT_free(*contour_lengths, userdata);
  14058. *contour_lengths = 0;
  14059. *num_contours = 0;
  14060. return NULL;
  14061. }
  14062. STBTT_DEF void stbtt_Rasterize(stbtt__bitmap* result, float flatness_in_pixels, stbtt_vertex* vertices, int num_verts, float scale_x, float scale_y, float shift_x, float shift_y, int x_off, int y_off, int invert, void* userdata)
  14063. {
  14064. float scale = scale_x > scale_y ? scale_y : scale_x;
  14065. int winding_count = 0;
  14066. int* winding_lengths = NULL;
  14067. stbtt__point* windings = stbtt_FlattenCurves(vertices, num_verts, flatness_in_pixels / scale, &winding_lengths, &winding_count, userdata);
  14068. if (windings) {
  14069. stbtt__rasterize(result, windings, winding_lengths, winding_count, scale_x, scale_y, shift_x, shift_y, x_off, y_off, invert, userdata);
  14070. STBTT_free(winding_lengths, userdata);
  14071. STBTT_free(windings, userdata);
  14072. }
  14073. }
  14074. STBTT_DEF void stbtt_FreeBitmap(unsigned char* bitmap, void* userdata)
  14075. {
  14076. STBTT_free(bitmap, userdata);
  14077. }
  14078. STBTT_DEF unsigned char* stbtt_GetGlyphBitmapSubpixel(const stbtt_fontinfo* info, float scale_x, float scale_y, float shift_x, float shift_y, int glyph, int* width, int* height, int* xoff, int* yoff)
  14079. {
  14080. int ix0, iy0, ix1, iy1;
  14081. stbtt__bitmap gbm;
  14082. stbtt_vertex* vertices;
  14083. int num_verts = stbtt_GetGlyphShape(info, glyph, &vertices);
  14084. if (scale_x == 0) scale_x = scale_y;
  14085. if (scale_y == 0) {
  14086. if (scale_x == 0) {
  14087. STBTT_free(vertices, info->userdata);
  14088. return NULL;
  14089. }
  14090. scale_y = scale_x;
  14091. }
  14092. stbtt_GetGlyphBitmapBoxSubpixel(info, glyph, scale_x, scale_y, shift_x, shift_y, &ix0, &iy0, &ix1, &iy1);
  14093. /* now we get the size */
  14094. gbm.w = (ix1 - ix0);
  14095. gbm.h = (iy1 - iy0);
  14096. gbm.pixels = NULL; /* in case we error */
  14097. if (width) *width = gbm.w;
  14098. if (height) *height = gbm.h;
  14099. if (xoff) *xoff = ix0;
  14100. if (yoff) *yoff = iy0;
  14101. if (gbm.w && gbm.h) {
  14102. gbm.pixels = (unsigned char*)STBTT_malloc(gbm.w * gbm.h, info->userdata);
  14103. if (gbm.pixels) {
  14104. gbm.stride = gbm.w;
  14105. stbtt_Rasterize(&gbm, 0.35f, vertices, num_verts, scale_x, scale_y, shift_x, shift_y, ix0, iy0, 1, info->userdata);
  14106. }
  14107. }
  14108. STBTT_free(vertices, info->userdata);
  14109. return gbm.pixels;
  14110. }
  14111. STBTT_DEF unsigned char* stbtt_GetGlyphBitmap(const stbtt_fontinfo* info, float scale_x, float scale_y, int glyph, int* width, int* height, int* xoff, int* yoff)
  14112. {
  14113. return stbtt_GetGlyphBitmapSubpixel(info, scale_x, scale_y, 0.0f, 0.0f, glyph, width, height, xoff, yoff);
  14114. }
  14115. STBTT_DEF void stbtt_MakeGlyphBitmapSubpixel(const stbtt_fontinfo* info, unsigned char* output, int out_w, int out_h, int out_stride, float scale_x, float scale_y, float shift_x, float shift_y, int glyph)
  14116. {
  14117. int ix0, iy0;
  14118. stbtt_vertex* vertices;
  14119. int num_verts = stbtt_GetGlyphShape(info, glyph, &vertices);
  14120. stbtt__bitmap gbm;
  14121. stbtt_GetGlyphBitmapBoxSubpixel(info, glyph, scale_x, scale_y, shift_x, shift_y, &ix0, &iy0, 0, 0);
  14122. gbm.pixels = output;
  14123. gbm.w = out_w;
  14124. gbm.h = out_h;
  14125. gbm.stride = out_stride;
  14126. if (gbm.w && gbm.h)
  14127. stbtt_Rasterize(&gbm, 0.35f, vertices, num_verts, scale_x, scale_y, shift_x, shift_y, ix0, iy0, 1, info->userdata);
  14128. STBTT_free(vertices, info->userdata);
  14129. }
  14130. STBTT_DEF void stbtt_MakeGlyphBitmap(const stbtt_fontinfo* info, unsigned char* output, int out_w, int out_h, int out_stride, float scale_x, float scale_y, int glyph)
  14131. {
  14132. stbtt_MakeGlyphBitmapSubpixel(info, output, out_w, out_h, out_stride, scale_x, scale_y, 0.0f, 0.0f, glyph);
  14133. }
  14134. STBTT_DEF unsigned char* stbtt_GetCodepointBitmapSubpixel(const stbtt_fontinfo* info, float scale_x, float scale_y, float shift_x, float shift_y, int codepoint, int* width, int* height, int* xoff, int* yoff)
  14135. {
  14136. return stbtt_GetGlyphBitmapSubpixel(info, scale_x, scale_y, shift_x, shift_y, stbtt_FindGlyphIndex(info, codepoint), width, height, xoff, yoff);
  14137. }
  14138. STBTT_DEF void stbtt_MakeCodepointBitmapSubpixelPrefilter(const stbtt_fontinfo* info, unsigned char* output, int out_w, int out_h, int out_stride, float scale_x, float scale_y, float shift_x, float shift_y, int oversample_x, int oversample_y, float* sub_x, float* sub_y, int codepoint)
  14139. {
  14140. stbtt_MakeGlyphBitmapSubpixelPrefilter(info, output, out_w, out_h, out_stride, scale_x, scale_y, shift_x, shift_y, oversample_x, oversample_y, sub_x, sub_y, stbtt_FindGlyphIndex(info, codepoint));
  14141. }
  14142. STBTT_DEF void stbtt_MakeCodepointBitmapSubpixel(const stbtt_fontinfo* info, unsigned char* output, int out_w, int out_h, int out_stride, float scale_x, float scale_y, float shift_x, float shift_y, int codepoint)
  14143. {
  14144. stbtt_MakeGlyphBitmapSubpixel(info, output, out_w, out_h, out_stride, scale_x, scale_y, shift_x, shift_y, stbtt_FindGlyphIndex(info, codepoint));
  14145. }
  14146. STBTT_DEF unsigned char* stbtt_GetCodepointBitmap(const stbtt_fontinfo* info, float scale_x, float scale_y, int codepoint, int* width, int* height, int* xoff, int* yoff)
  14147. {
  14148. return stbtt_GetCodepointBitmapSubpixel(info, scale_x, scale_y, 0.0f, 0.0f, codepoint, width, height, xoff, yoff);
  14149. }
  14150. STBTT_DEF void stbtt_MakeCodepointBitmap(const stbtt_fontinfo* info, unsigned char* output, int out_w, int out_h, int out_stride, float scale_x, float scale_y, int codepoint)
  14151. {
  14152. stbtt_MakeCodepointBitmapSubpixel(info, output, out_w, out_h, out_stride, scale_x, scale_y, 0.0f, 0.0f, codepoint);
  14153. }
  14154. /* //////////////////////////////////////////////////////////////////////////// */
  14155. /* */
  14156. /* bitmap baking */
  14157. /* */
  14158. /* This is SUPER-CRAPPY packing to keep source code small */
  14159. static int stbtt_BakeFontBitmap_internal(unsigned char* data, int offset, /* font location (use offset=0 for plain .ttf) */
  14160. float pixel_height, /* height of font in pixels */
  14161. unsigned char* pixels, int pw, int ph, /* bitmap to be filled in */
  14162. int first_char, int num_chars, /* characters to bake */
  14163. stbtt_bakedchar* chardata)
  14164. {
  14165. float scale;
  14166. int x, y, bottom_y, i;
  14167. stbtt_fontinfo f;
  14168. f.userdata = NULL;
  14169. if (!stbtt_InitFont(&f, data, offset))
  14170. return -1;
  14171. STBTT_memset(pixels, 0, pw * ph); /* background of 0 around pixels */
  14172. x = y = 1;
  14173. bottom_y = 1;
  14174. scale = stbtt_ScaleForPixelHeight(&f, pixel_height);
  14175. for (i = 0; i < num_chars; ++i) {
  14176. int advance, lsb, x0, y0, x1, y1, gw, gh;
  14177. int g = stbtt_FindGlyphIndex(&f, first_char + i);
  14178. stbtt_GetGlyphHMetrics(&f, g, &advance, &lsb);
  14179. stbtt_GetGlyphBitmapBox(&f, g, scale, scale, &x0, &y0, &x1, &y1);
  14180. gw = x1 - x0;
  14181. gh = y1 - y0;
  14182. if (x + gw + 1 >= pw)
  14183. y = bottom_y, x = 1; /* advance to next row */
  14184. if (y + gh + 1 >= ph) /* check if it fits vertically AFTER potentially moving to next row */
  14185. return -i;
  14186. STBTT_assert(x + gw < pw);
  14187. STBTT_assert(y + gh < ph);
  14188. stbtt_MakeGlyphBitmap(&f, pixels + x + y * pw, gw, gh, pw, scale, scale, g);
  14189. chardata[i].x0 = (stbtt_int16)x;
  14190. chardata[i].y0 = (stbtt_int16)y;
  14191. chardata[i].x1 = (stbtt_int16)(x + gw);
  14192. chardata[i].y1 = (stbtt_int16)(y + gh);
  14193. chardata[i].xadvance = scale * advance;
  14194. chardata[i].xoff = (float)x0;
  14195. chardata[i].yoff = (float)y0;
  14196. x = x + gw + 1;
  14197. if (y + gh + 1 > bottom_y)
  14198. bottom_y = y + gh + 1;
  14199. }
  14200. return bottom_y;
  14201. }
  14202. STBTT_DEF void stbtt_GetBakedQuad(const stbtt_bakedchar* chardata, int pw, int ph, int char_index, float* xpos, float* ypos, stbtt_aligned_quad* q, int opengl_fillrule)
  14203. {
  14204. float d3d_bias = opengl_fillrule ? 0 : -0.5f;
  14205. float ipw = 1.0f / pw, iph = 1.0f / ph;
  14206. const stbtt_bakedchar* b = chardata + char_index;
  14207. int round_x = STBTT_ifloor((*xpos + b->xoff) + 0.5f);
  14208. int round_y = STBTT_ifloor((*ypos + b->yoff) + 0.5f);
  14209. q->x0 = round_x + d3d_bias;
  14210. q->y0 = round_y + d3d_bias;
  14211. q->x1 = round_x + b->x1 - b->x0 + d3d_bias;
  14212. q->y1 = round_y + b->y1 - b->y0 + d3d_bias;
  14213. q->s0 = b->x0 * ipw;
  14214. q->t0 = b->y0 * iph;
  14215. q->s1 = b->x1 * ipw;
  14216. q->t1 = b->y1 * iph;
  14217. *xpos += b->xadvance;
  14218. }
  14219. /* //////////////////////////////////////////////////////////////////////////// */
  14220. /* */
  14221. /* rectangle packing replacement routines if you don't have stb_rect_pack.h */
  14222. /* */
  14223. #ifndef STB_RECT_PACK_VERSION
  14224. typedef int stbrp_coord;
  14225. /* ////////////////////////////////////////////////////////////////////////////////// */
  14226. /* // */
  14227. /* // */
  14228. /* COMPILER WARNING ?!?!? // */
  14229. /* // */
  14230. /* // */
  14231. /* if you get a compile warning due to these symbols being defined more than // */
  14232. /* once, move #include "stb_rect_pack.h" before #include "stb_truetype.h" // */
  14233. /* // */
  14234. /* ////////////////////////////////////////////////////////////////////////////////// */
  14235. typedef struct
  14236. {
  14237. int width, height;
  14238. int x, y, bottom_y;
  14239. } stbrp_context;
  14240. typedef struct
  14241. {
  14242. unsigned char x;
  14243. } stbrp_node;
  14244. struct stbrp_rect
  14245. {
  14246. stbrp_coord x, y;
  14247. int id, w, h, was_packed;
  14248. };
  14249. static void stbrp_init_target(stbrp_context* con, int pw, int ph, stbrp_node* nodes, int num_nodes)
  14250. {
  14251. con->width = pw;
  14252. con->height = ph;
  14253. con->x = 0;
  14254. con->y = 0;
  14255. con->bottom_y = 0;
  14256. STBTT__NOTUSED(nodes);
  14257. STBTT__NOTUSED(num_nodes);
  14258. }
  14259. static void stbrp_pack_rects(stbrp_context* con, stbrp_rect* rects, int num_rects)
  14260. {
  14261. int i;
  14262. for (i = 0; i < num_rects; ++i) {
  14263. if (con->x + rects[i].w > con->width) {
  14264. con->x = 0;
  14265. con->y = con->bottom_y;
  14266. }
  14267. if (con->y + rects[i].h > con->height)
  14268. break;
  14269. rects[i].x = con->x;
  14270. rects[i].y = con->y;
  14271. rects[i].was_packed = 1;
  14272. con->x += rects[i].w;
  14273. if (con->y + rects[i].h > con->bottom_y)
  14274. con->bottom_y = con->y + rects[i].h;
  14275. }
  14276. for (; i < num_rects; ++i)
  14277. rects[i].was_packed = 0;
  14278. }
  14279. #endif
  14280. /* //////////////////////////////////////////////////////////////////////////// */
  14281. /* */
  14282. /* bitmap baking */
  14283. /* */
  14284. /* This is SUPER-AWESOME (tm Ryan Gordon) packing using stb_rect_pack.h. If */
  14285. /* stb_rect_pack.h isn't available, it uses the BakeFontBitmap strategy. */
  14286. STBTT_DEF int stbtt_PackBegin(stbtt_pack_context* spc, unsigned char* pixels, int pw, int ph, int stride_in_bytes, int padding, void* alloc_context)
  14287. {
  14288. stbrp_context* context = (stbrp_context*)STBTT_malloc(sizeof(*context), alloc_context);
  14289. int num_nodes = pw - padding;
  14290. stbrp_node* nodes = (stbrp_node*)STBTT_malloc(sizeof(*nodes) * num_nodes, alloc_context);
  14291. if (context == NULL || nodes == NULL) {
  14292. if (context != NULL) STBTT_free(context, alloc_context);
  14293. if (nodes != NULL) STBTT_free(nodes, alloc_context);
  14294. return 0;
  14295. }
  14296. spc->user_allocator_context = alloc_context;
  14297. spc->width = pw;
  14298. spc->height = ph;
  14299. spc->pixels = pixels;
  14300. spc->pack_info = context;
  14301. spc->nodes = nodes;
  14302. spc->padding = padding;
  14303. spc->stride_in_bytes = stride_in_bytes != 0 ? stride_in_bytes : pw;
  14304. spc->h_oversample = 1;
  14305. spc->v_oversample = 1;
  14306. spc->skip_missing = 0;
  14307. stbrp_init_target(context, pw - padding, ph - padding, nodes, num_nodes);
  14308. if (pixels)
  14309. STBTT_memset(pixels, 0, pw * ph); /* background of 0 around pixels */
  14310. return 1;
  14311. }
  14312. STBTT_DEF void stbtt_PackEnd(stbtt_pack_context* spc)
  14313. {
  14314. STBTT_free(spc->nodes, spc->user_allocator_context);
  14315. STBTT_free(spc->pack_info, spc->user_allocator_context);
  14316. }
  14317. STBTT_DEF void stbtt_PackSetOversampling(stbtt_pack_context* spc, unsigned int h_oversample, unsigned int v_oversample)
  14318. {
  14319. STBTT_assert(h_oversample <= STBTT_MAX_OVERSAMPLE);
  14320. STBTT_assert(v_oversample <= STBTT_MAX_OVERSAMPLE);
  14321. if (h_oversample <= STBTT_MAX_OVERSAMPLE)
  14322. spc->h_oversample = h_oversample;
  14323. if (v_oversample <= STBTT_MAX_OVERSAMPLE)
  14324. spc->v_oversample = v_oversample;
  14325. }
  14326. STBTT_DEF void stbtt_PackSetSkipMissingCodepoints(stbtt_pack_context* spc, int skip)
  14327. {
  14328. spc->skip_missing = skip;
  14329. }
  14330. #define STBTT__OVER_MASK (STBTT_MAX_OVERSAMPLE-1)
  14331. static void stbtt__h_prefilter(unsigned char* pixels, int w, int h, int stride_in_bytes, unsigned int kernel_width)
  14332. {
  14333. unsigned char buffer[STBTT_MAX_OVERSAMPLE];
  14334. int safe_w = w - kernel_width;
  14335. int j;
  14336. STBTT_memset(buffer, 0, STBTT_MAX_OVERSAMPLE); /* suppress bogus warning from VS2013 -analyze */
  14337. for (j = 0; j < h; ++j) {
  14338. int i;
  14339. unsigned int total;
  14340. STBTT_memset(buffer, 0, kernel_width);
  14341. total = 0;
  14342. /* make kernel_width a constant in common cases so compiler can optimize out the divide */
  14343. switch (kernel_width) {
  14344. case 2:
  14345. for (i = 0; i <= safe_w; ++i) {
  14346. total += pixels[i] - buffer[i & STBTT__OVER_MASK];
  14347. buffer[(i + kernel_width) & STBTT__OVER_MASK] = pixels[i];
  14348. pixels[i] = (unsigned char)(total / 2);
  14349. }
  14350. break;
  14351. case 3:
  14352. for (i = 0; i <= safe_w; ++i) {
  14353. total += pixels[i] - buffer[i & STBTT__OVER_MASK];
  14354. buffer[(i + kernel_width) & STBTT__OVER_MASK] = pixels[i];
  14355. pixels[i] = (unsigned char)(total / 3);
  14356. }
  14357. break;
  14358. case 4:
  14359. for (i = 0; i <= safe_w; ++i) {
  14360. total += pixels[i] - buffer[i & STBTT__OVER_MASK];
  14361. buffer[(i + kernel_width) & STBTT__OVER_MASK] = pixels[i];
  14362. pixels[i] = (unsigned char)(total / 4);
  14363. }
  14364. break;
  14365. case 5:
  14366. for (i = 0; i <= safe_w; ++i) {
  14367. total += pixels[i] - buffer[i & STBTT__OVER_MASK];
  14368. buffer[(i + kernel_width) & STBTT__OVER_MASK] = pixels[i];
  14369. pixels[i] = (unsigned char)(total / 5);
  14370. }
  14371. break;
  14372. default:
  14373. for (i = 0; i <= safe_w; ++i) {
  14374. total += pixels[i] - buffer[i & STBTT__OVER_MASK];
  14375. buffer[(i + kernel_width) & STBTT__OVER_MASK] = pixels[i];
  14376. pixels[i] = (unsigned char)(total / kernel_width);
  14377. }
  14378. break;
  14379. }
  14380. for (; i < w; ++i) {
  14381. STBTT_assert(pixels[i] == 0);
  14382. total -= buffer[i & STBTT__OVER_MASK];
  14383. pixels[i] = (unsigned char)(total / kernel_width);
  14384. }
  14385. pixels += stride_in_bytes;
  14386. }
  14387. }
  14388. static void stbtt__v_prefilter(unsigned char* pixels, int w, int h, int stride_in_bytes, unsigned int kernel_width)
  14389. {
  14390. unsigned char buffer[STBTT_MAX_OVERSAMPLE];
  14391. int safe_h = h - kernel_width;
  14392. int j;
  14393. STBTT_memset(buffer, 0, STBTT_MAX_OVERSAMPLE); /* suppress bogus warning from VS2013 -analyze */
  14394. for (j = 0; j < w; ++j) {
  14395. int i;
  14396. unsigned int total;
  14397. STBTT_memset(buffer, 0, kernel_width);
  14398. total = 0;
  14399. /* make kernel_width a constant in common cases so compiler can optimize out the divide */
  14400. switch (kernel_width) {
  14401. case 2:
  14402. for (i = 0; i <= safe_h; ++i) {
  14403. total += pixels[i * stride_in_bytes] - buffer[i & STBTT__OVER_MASK];
  14404. buffer[(i + kernel_width) & STBTT__OVER_MASK] = pixels[i * stride_in_bytes];
  14405. pixels[i * stride_in_bytes] = (unsigned char)(total / 2);
  14406. }
  14407. break;
  14408. case 3:
  14409. for (i = 0; i <= safe_h; ++i) {
  14410. total += pixels[i * stride_in_bytes] - buffer[i & STBTT__OVER_MASK];
  14411. buffer[(i + kernel_width) & STBTT__OVER_MASK] = pixels[i * stride_in_bytes];
  14412. pixels[i * stride_in_bytes] = (unsigned char)(total / 3);
  14413. }
  14414. break;
  14415. case 4:
  14416. for (i = 0; i <= safe_h; ++i) {
  14417. total += pixels[i * stride_in_bytes] - buffer[i & STBTT__OVER_MASK];
  14418. buffer[(i + kernel_width) & STBTT__OVER_MASK] = pixels[i * stride_in_bytes];
  14419. pixels[i * stride_in_bytes] = (unsigned char)(total / 4);
  14420. }
  14421. break;
  14422. case 5:
  14423. for (i = 0; i <= safe_h; ++i) {
  14424. total += pixels[i * stride_in_bytes] - buffer[i & STBTT__OVER_MASK];
  14425. buffer[(i + kernel_width) & STBTT__OVER_MASK] = pixels[i * stride_in_bytes];
  14426. pixels[i * stride_in_bytes] = (unsigned char)(total / 5);
  14427. }
  14428. break;
  14429. default:
  14430. for (i = 0; i <= safe_h; ++i) {
  14431. total += pixels[i * stride_in_bytes] - buffer[i & STBTT__OVER_MASK];
  14432. buffer[(i + kernel_width) & STBTT__OVER_MASK] = pixels[i * stride_in_bytes];
  14433. pixels[i * stride_in_bytes] = (unsigned char)(total / kernel_width);
  14434. }
  14435. break;
  14436. }
  14437. for (; i < h; ++i) {
  14438. STBTT_assert(pixels[i * stride_in_bytes] == 0);
  14439. total -= buffer[i & STBTT__OVER_MASK];
  14440. pixels[i * stride_in_bytes] = (unsigned char)(total / kernel_width);
  14441. }
  14442. pixels += 1;
  14443. }
  14444. }
  14445. static float stbtt__oversample_shift(int oversample)
  14446. {
  14447. if (!oversample)
  14448. return 0.0f;
  14449. /* The prefilter is a box filter of width "oversample", */
  14450. /* which shifts phase by (oversample - 1)/2 pixels in */
  14451. /* oversampled space. We want to shift in the opposite */
  14452. /* direction to counter this. */
  14453. return (float)-(oversample - 1) / (2.0f * (float)oversample);
  14454. }
  14455. /* rects array must be big enough to accommodate all characters in the given ranges */
  14456. STBTT_DEF int stbtt_PackFontRangesGatherRects(stbtt_pack_context* spc, const stbtt_fontinfo* info, stbtt_pack_range* ranges, int num_ranges, stbrp_rect* rects)
  14457. {
  14458. int i, j, k;
  14459. int missing_glyph_added = 0;
  14460. k = 0;
  14461. for (i = 0; i < num_ranges; ++i) {
  14462. float fh = ranges[i].font_size;
  14463. float scale = fh > 0 ? stbtt_ScaleForPixelHeight(info, fh) : stbtt_ScaleForMappingEmToPixels(info, -fh);
  14464. ranges[i].h_oversample = (unsigned char)spc->h_oversample;
  14465. ranges[i].v_oversample = (unsigned char)spc->v_oversample;
  14466. for (j = 0; j < ranges[i].num_chars; ++j) {
  14467. int x0, y0, x1, y1;
  14468. int codepoint = ranges[i].array_of_unicode_codepoints == NULL ? ranges[i].first_unicode_codepoint_in_range + j : ranges[i].array_of_unicode_codepoints[j];
  14469. int glyph = stbtt_FindGlyphIndex(info, codepoint);
  14470. if (glyph == 0 && (spc->skip_missing || missing_glyph_added)) {
  14471. rects[k].w = rects[k].h = 0;
  14472. }
  14473. else {
  14474. stbtt_GetGlyphBitmapBoxSubpixel(info, glyph,
  14475. scale * spc->h_oversample,
  14476. scale * spc->v_oversample,
  14477. 0, 0,
  14478. &x0, &y0, &x1, &y1);
  14479. rects[k].w = (stbrp_coord)(x1 - x0 + spc->padding + spc->h_oversample - 1);
  14480. rects[k].h = (stbrp_coord)(y1 - y0 + spc->padding + spc->v_oversample - 1);
  14481. if (glyph == 0)
  14482. missing_glyph_added = 1;
  14483. }
  14484. ++k;
  14485. }
  14486. }
  14487. return k;
  14488. }
  14489. STBTT_DEF void stbtt_MakeGlyphBitmapSubpixelPrefilter(const stbtt_fontinfo* info, unsigned char* output, int out_w, int out_h, int out_stride, float scale_x, float scale_y, float shift_x, float shift_y, int prefilter_x, int prefilter_y, float* sub_x, float* sub_y, int glyph)
  14490. {
  14491. stbtt_MakeGlyphBitmapSubpixel(info,
  14492. output,
  14493. out_w - (prefilter_x - 1),
  14494. out_h - (prefilter_y - 1),
  14495. out_stride,
  14496. scale_x,
  14497. scale_y,
  14498. shift_x,
  14499. shift_y,
  14500. glyph);
  14501. if (prefilter_x > 1)
  14502. stbtt__h_prefilter(output, out_w, out_h, out_stride, prefilter_x);
  14503. if (prefilter_y > 1)
  14504. stbtt__v_prefilter(output, out_w, out_h, out_stride, prefilter_y);
  14505. *sub_x = stbtt__oversample_shift(prefilter_x);
  14506. *sub_y = stbtt__oversample_shift(prefilter_y);
  14507. }
  14508. /* rects array must be big enough to accommodate all characters in the given ranges */
  14509. STBTT_DEF int stbtt_PackFontRangesRenderIntoRects(stbtt_pack_context* spc, const stbtt_fontinfo* info, stbtt_pack_range* ranges, int num_ranges, stbrp_rect* rects)
  14510. {
  14511. int i, j, k, missing_glyph = -1, return_value = 1;
  14512. /* save current values */
  14513. int old_h_over = spc->h_oversample;
  14514. int old_v_over = spc->v_oversample;
  14515. k = 0;
  14516. for (i = 0; i < num_ranges; ++i) {
  14517. float fh = ranges[i].font_size;
  14518. float scale = fh > 0 ? stbtt_ScaleForPixelHeight(info, fh) : stbtt_ScaleForMappingEmToPixels(info, -fh);
  14519. float recip_h, recip_v, sub_x, sub_y;
  14520. spc->h_oversample = ranges[i].h_oversample;
  14521. spc->v_oversample = ranges[i].v_oversample;
  14522. recip_h = 1.0f / spc->h_oversample;
  14523. recip_v = 1.0f / spc->v_oversample;
  14524. sub_x = stbtt__oversample_shift(spc->h_oversample);
  14525. sub_y = stbtt__oversample_shift(spc->v_oversample);
  14526. for (j = 0; j < ranges[i].num_chars; ++j) {
  14527. stbrp_rect* r = &rects[k];
  14528. if (r->was_packed && r->w != 0 && r->h != 0) {
  14529. stbtt_packedchar* bc = &ranges[i].chardata_for_range[j];
  14530. int advance, lsb, x0, y0, x1, y1;
  14531. int codepoint = ranges[i].array_of_unicode_codepoints == NULL ? ranges[i].first_unicode_codepoint_in_range + j : ranges[i].array_of_unicode_codepoints[j];
  14532. int glyph = stbtt_FindGlyphIndex(info, codepoint);
  14533. stbrp_coord pad = (stbrp_coord)spc->padding;
  14534. /* pad on left and top */
  14535. r->x += pad;
  14536. r->y += pad;
  14537. r->w -= pad;
  14538. r->h -= pad;
  14539. stbtt_GetGlyphHMetrics(info, glyph, &advance, &lsb);
  14540. stbtt_GetGlyphBitmapBox(info, glyph,
  14541. scale * spc->h_oversample,
  14542. scale * spc->v_oversample,
  14543. &x0, &y0, &x1, &y1);
  14544. stbtt_MakeGlyphBitmapSubpixel(info,
  14545. spc->pixels + r->x + r->y * spc->stride_in_bytes,
  14546. r->w - spc->h_oversample + 1,
  14547. r->h - spc->v_oversample + 1,
  14548. spc->stride_in_bytes,
  14549. scale * spc->h_oversample,
  14550. scale * spc->v_oversample,
  14551. 0, 0,
  14552. glyph);
  14553. if (spc->h_oversample > 1)
  14554. stbtt__h_prefilter(spc->pixels + r->x + r->y * spc->stride_in_bytes,
  14555. r->w, r->h, spc->stride_in_bytes,
  14556. spc->h_oversample);
  14557. if (spc->v_oversample > 1)
  14558. stbtt__v_prefilter(spc->pixels + r->x + r->y * spc->stride_in_bytes,
  14559. r->w, r->h, spc->stride_in_bytes,
  14560. spc->v_oversample);
  14561. bc->x0 = (stbtt_int16)r->x;
  14562. bc->y0 = (stbtt_int16)r->y;
  14563. bc->x1 = (stbtt_int16)(r->x + r->w);
  14564. bc->y1 = (stbtt_int16)(r->y + r->h);
  14565. bc->xadvance = scale * advance;
  14566. bc->xoff = (float)x0 * recip_h + sub_x;
  14567. bc->yoff = (float)y0 * recip_v + sub_y;
  14568. bc->xoff2 = (x0 + r->w) * recip_h + sub_x;
  14569. bc->yoff2 = (y0 + r->h) * recip_v + sub_y;
  14570. if (glyph == 0)
  14571. missing_glyph = j;
  14572. }
  14573. else if (spc->skip_missing) {
  14574. return_value = 0;
  14575. }
  14576. else if (r->was_packed && r->w == 0 && r->h == 0 && missing_glyph >= 0) {
  14577. ranges[i].chardata_for_range[j] = ranges[i].chardata_for_range[missing_glyph];
  14578. }
  14579. else {
  14580. return_value = 0; /* if any fail, report failure */
  14581. }
  14582. ++k;
  14583. }
  14584. }
  14585. /* restore original values */
  14586. spc->h_oversample = old_h_over;
  14587. spc->v_oversample = old_v_over;
  14588. return return_value;
  14589. }
  14590. STBTT_DEF void stbtt_PackFontRangesPackRects(stbtt_pack_context* spc, stbrp_rect* rects, int num_rects)
  14591. {
  14592. stbrp_pack_rects((stbrp_context*)spc->pack_info, rects, num_rects);
  14593. }
  14594. STBTT_DEF int stbtt_PackFontRanges(stbtt_pack_context* spc, const unsigned char* fontdata, int font_index, stbtt_pack_range* ranges, int num_ranges)
  14595. {
  14596. stbtt_fontinfo info;
  14597. int i, j, n, return_value = 1;
  14598. /* stbrp_context *context = (stbrp_context *) spc->pack_info; */
  14599. stbrp_rect* rects;
  14600. /* flag all characters as NOT packed */
  14601. for (i = 0; i < num_ranges; ++i)
  14602. for (j = 0; j < ranges[i].num_chars; ++j)
  14603. ranges[i].chardata_for_range[j].x0 =
  14604. ranges[i].chardata_for_range[j].y0 =
  14605. ranges[i].chardata_for_range[j].x1 =
  14606. ranges[i].chardata_for_range[j].y1 = 0;
  14607. n = 0;
  14608. for (i = 0; i < num_ranges; ++i)
  14609. n += ranges[i].num_chars;
  14610. rects = (stbrp_rect*)STBTT_malloc(sizeof(*rects) * n, spc->user_allocator_context);
  14611. if (rects == NULL)
  14612. return 0;
  14613. info.userdata = spc->user_allocator_context;
  14614. stbtt_InitFont(&info, fontdata, stbtt_GetFontOffsetForIndex(fontdata, font_index));
  14615. n = stbtt_PackFontRangesGatherRects(spc, &info, ranges, num_ranges, rects);
  14616. stbtt_PackFontRangesPackRects(spc, rects, n);
  14617. return_value = stbtt_PackFontRangesRenderIntoRects(spc, &info, ranges, num_ranges, rects);
  14618. STBTT_free(rects, spc->user_allocator_context);
  14619. return return_value;
  14620. }
  14621. STBTT_DEF int stbtt_PackFontRange(stbtt_pack_context* spc, const unsigned char* fontdata, int font_index, float font_size,
  14622. int first_unicode_codepoint_in_range, int num_chars_in_range, stbtt_packedchar* chardata_for_range)
  14623. {
  14624. stbtt_pack_range range;
  14625. range.first_unicode_codepoint_in_range = first_unicode_codepoint_in_range;
  14626. range.array_of_unicode_codepoints = NULL;
  14627. range.num_chars = num_chars_in_range;
  14628. range.chardata_for_range = chardata_for_range;
  14629. range.font_size = font_size;
  14630. return stbtt_PackFontRanges(spc, fontdata, font_index, &range, 1);
  14631. }
  14632. STBTT_DEF void stbtt_GetScaledFontVMetrics(const unsigned char* fontdata, int index, float size, float* ascent, float* descent, float* lineGap)
  14633. {
  14634. int i_ascent, i_descent, i_lineGap;
  14635. float scale;
  14636. stbtt_fontinfo info;
  14637. stbtt_InitFont(&info, fontdata, stbtt_GetFontOffsetForIndex(fontdata, index));
  14638. scale = size > 0 ? stbtt_ScaleForPixelHeight(&info, size) : stbtt_ScaleForMappingEmToPixels(&info, -size);
  14639. stbtt_GetFontVMetrics(&info, &i_ascent, &i_descent, &i_lineGap);
  14640. *ascent = (float)i_ascent * scale;
  14641. *descent = (float)i_descent * scale;
  14642. *lineGap = (float)i_lineGap * scale;
  14643. }
  14644. STBTT_DEF void stbtt_GetPackedQuad(const stbtt_packedchar* chardata, int pw, int ph, int char_index, float* xpos, float* ypos, stbtt_aligned_quad* q, int align_to_integer)
  14645. {
  14646. float ipw = 1.0f / pw, iph = 1.0f / ph;
  14647. const stbtt_packedchar* b = chardata + char_index;
  14648. if (align_to_integer) {
  14649. float x = (float)STBTT_ifloor((*xpos + b->xoff) + 0.5f);
  14650. float y = (float)STBTT_ifloor((*ypos + b->yoff) + 0.5f);
  14651. q->x0 = x;
  14652. q->y0 = y;
  14653. q->x1 = x + b->xoff2 - b->xoff;
  14654. q->y1 = y + b->yoff2 - b->yoff;
  14655. }
  14656. else {
  14657. q->x0 = *xpos + b->xoff;
  14658. q->y0 = *ypos + b->yoff;
  14659. q->x1 = *xpos + b->xoff2;
  14660. q->y1 = *ypos + b->yoff2;
  14661. }
  14662. q->s0 = b->x0 * ipw;
  14663. q->t0 = b->y0 * iph;
  14664. q->s1 = b->x1 * ipw;
  14665. q->t1 = b->y1 * iph;
  14666. *xpos += b->xadvance;
  14667. }
  14668. /* //////////////////////////////////////////////////////////////////////////// */
  14669. /* */
  14670. /* sdf computation */
  14671. /* */
  14672. #define STBTT_min(a,b) ((a) < (b) ? (a) : (b))
  14673. #define STBTT_max(a,b) ((a) < (b) ? (b) : (a))
  14674. static int stbtt__ray_intersect_bezier(float orig[2], float ray[2], float q0[2], float q1[2], float q2[2], float hits[2][2])
  14675. {
  14676. float q0perp = q0[1] * ray[0] - q0[0] * ray[1];
  14677. float q1perp = q1[1] * ray[0] - q1[0] * ray[1];
  14678. float q2perp = q2[1] * ray[0] - q2[0] * ray[1];
  14679. float roperp = orig[1] * ray[0] - orig[0] * ray[1];
  14680. float a = q0perp - 2 * q1perp + q2perp;
  14681. float b = q1perp - q0perp;
  14682. float c = q0perp - roperp;
  14683. float s0 = 0., s1 = 0.;
  14684. int num_s = 0;
  14685. if (a != 0.0) {
  14686. float discr = b * b - a * c;
  14687. if (discr > 0.0) {
  14688. float rcpna = -1 / a;
  14689. float d = (float)STBTT_sqrt(discr);
  14690. s0 = (b + d) * rcpna;
  14691. s1 = (b - d) * rcpna;
  14692. if (s0 >= 0.0 && s0 <= 1.0)
  14693. num_s = 1;
  14694. if (d > 0.0 && s1 >= 0.0 && s1 <= 1.0) {
  14695. if (num_s == 0) s0 = s1;
  14696. ++num_s;
  14697. }
  14698. }
  14699. }
  14700. else {
  14701. /* 2*b*s + c = 0 */
  14702. /* s = -c / (2*b) */
  14703. s0 = c / (-2 * b);
  14704. if (s0 >= 0.0 && s0 <= 1.0)
  14705. num_s = 1;
  14706. }
  14707. if (num_s == 0)
  14708. return 0;
  14709. else {
  14710. float rcp_len2 = 1 / (ray[0] * ray[0] + ray[1] * ray[1]);
  14711. float rayn_x = ray[0] * rcp_len2, rayn_y = ray[1] * rcp_len2;
  14712. float q0d = q0[0] * rayn_x + q0[1] * rayn_y;
  14713. float q1d = q1[0] * rayn_x + q1[1] * rayn_y;
  14714. float q2d = q2[0] * rayn_x + q2[1] * rayn_y;
  14715. float rod = orig[0] * rayn_x + orig[1] * rayn_y;
  14716. float q10d = q1d - q0d;
  14717. float q20d = q2d - q0d;
  14718. float q0rd = q0d - rod;
  14719. hits[0][0] = q0rd + s0 * (2.0f - 2.0f * s0) * q10d + s0 * s0 * q20d;
  14720. hits[0][1] = a * s0 + b;
  14721. if (num_s > 1) {
  14722. hits[1][0] = q0rd + s1 * (2.0f - 2.0f * s1) * q10d + s1 * s1 * q20d;
  14723. hits[1][1] = a * s1 + b;
  14724. return 2;
  14725. }
  14726. else {
  14727. return 1;
  14728. }
  14729. }
  14730. }
  14731. static int equal(float* a, float* b)
  14732. {
  14733. return (a[0] == b[0] && a[1] == b[1]);
  14734. }
  14735. static int stbtt__compute_crossings_x(float x, float y, int nverts, stbtt_vertex* verts)
  14736. {
  14737. int i;
  14738. float orig[2], ray[2] = { 1, 0 };
  14739. float y_frac;
  14740. int winding = 0;
  14741. /* make sure y never passes through a vertex of the shape */
  14742. y_frac = (float)STBTT_fmod(y, 1.0f);
  14743. if (y_frac < 0.01f)
  14744. y += 0.01f;
  14745. else if (y_frac > 0.99f)
  14746. y -= 0.01f;
  14747. orig[0] = x;
  14748. orig[1] = y;
  14749. /* test a ray from (-infinity,y) to (x,y) */
  14750. for (i = 0; i < nverts; ++i) {
  14751. if (verts[i].type == STBTT_vline) {
  14752. int x0 = (int)verts[i - 1].x, y0 = (int)verts[i - 1].y;
  14753. int x1 = (int)verts[i].x, y1 = (int)verts[i].y;
  14754. if (y > STBTT_min(y0, y1) && y < STBTT_max(y0, y1) && x > STBTT_min(x0, x1)) {
  14755. float x_inter = (y - y0) / (y1 - y0) * (x1 - x0) + x0;
  14756. if (x_inter < x)
  14757. winding += (y0 < y1) ? 1 : -1;
  14758. }
  14759. }
  14760. if (verts[i].type == STBTT_vcurve) {
  14761. int x0 = (int)verts[i - 1].x, y0 = (int)verts[i - 1].y;
  14762. int x1 = (int)verts[i].cx, y1 = (int)verts[i].cy;
  14763. int x2 = (int)verts[i].x, y2 = (int)verts[i].y;
  14764. int ax = STBTT_min(x0, STBTT_min(x1, x2)), ay = STBTT_min(y0, STBTT_min(y1, y2));
  14765. int by = STBTT_max(y0, STBTT_max(y1, y2));
  14766. if (y > ay && y < by && x > ax) {
  14767. float q0[2], q1[2], q2[2];
  14768. float hits[2][2];
  14769. q0[0] = (float)x0;
  14770. q0[1] = (float)y0;
  14771. q1[0] = (float)x1;
  14772. q1[1] = (float)y1;
  14773. q2[0] = (float)x2;
  14774. q2[1] = (float)y2;
  14775. if (equal(q0, q1) || equal(q1, q2)) {
  14776. x0 = (int)verts[i - 1].x;
  14777. y0 = (int)verts[i - 1].y;
  14778. x1 = (int)verts[i].x;
  14779. y1 = (int)verts[i].y;
  14780. if (y > STBTT_min(y0, y1) && y < STBTT_max(y0, y1) && x > STBTT_min(x0, x1)) {
  14781. float x_inter = (y - y0) / (y1 - y0) * (x1 - x0) + x0;
  14782. if (x_inter < x)
  14783. winding += (y0 < y1) ? 1 : -1;
  14784. }
  14785. }
  14786. else {
  14787. int num_hits = stbtt__ray_intersect_bezier(orig, ray, q0, q1, q2, hits);
  14788. if (num_hits >= 1)
  14789. if (hits[0][0] < 0)
  14790. winding += (hits[0][1] < 0 ? -1 : 1);
  14791. if (num_hits >= 2)
  14792. if (hits[1][0] < 0)
  14793. winding += (hits[1][1] < 0 ? -1 : 1);
  14794. }
  14795. }
  14796. }
  14797. }
  14798. return winding;
  14799. }
  14800. static float stbtt__cuberoot(float x)
  14801. {
  14802. if (x < 0)
  14803. return -(float)STBTT_pow(-x, 1.0f / 3.0f);
  14804. else
  14805. return (float)STBTT_pow(x, 1.0f / 3.0f);
  14806. }
  14807. /* x^3 + a*x^2 + b*x + c = 0 */
  14808. static int stbtt__solve_cubic(float a, float b, float c, float* r)
  14809. {
  14810. float s = -a / 3;
  14811. float p = b - a * a / 3;
  14812. float q = a * (2 * a * a - 9 * b) / 27 + c;
  14813. float p3 = p * p * p;
  14814. float d = q * q + 4 * p3 / 27;
  14815. if (d >= 0) {
  14816. float z = (float)STBTT_sqrt(d);
  14817. float u = (-q + z) / 2;
  14818. float v = (-q - z) / 2;
  14819. u = stbtt__cuberoot(u);
  14820. v = stbtt__cuberoot(v);
  14821. r[0] = s + u + v;
  14822. return 1;
  14823. }
  14824. else {
  14825. float u = (float)STBTT_sqrt(-p / 3);
  14826. float v = (float)STBTT_acos(-STBTT_sqrt(-27 / p3) * q / 2) / 3; /* p3 must be negative, since d is negative */
  14827. float m = (float)STBTT_cos(v);
  14828. float n = (float)STBTT_cos(v - 3.141592 / 2) * 1.732050808f;
  14829. r[0] = s + u * 2 * m;
  14830. r[1] = s - u * (m + n);
  14831. r[2] = s - u * (m - n);
  14832. /* STBTT_assert( STBTT_fabs(((r[0]+a)*r[0]+b)*r[0]+c) < 0.05f); // these asserts may not be safe at all scales, though they're in bezier t parameter units so maybe? */
  14833. /* STBTT_assert( STBTT_fabs(((r[1]+a)*r[1]+b)*r[1]+c) < 0.05f); */
  14834. /* STBTT_assert( STBTT_fabs(((r[2]+a)*r[2]+b)*r[2]+c) < 0.05f); */
  14835. return 3;
  14836. }
  14837. }
  14838. STBTT_DEF unsigned char* stbtt_GetGlyphSDF(const stbtt_fontinfo* info, float scale, int glyph, int padding, unsigned char onedge_value, float pixel_dist_scale, int* width, int* height, int* xoff, int* yoff)
  14839. {
  14840. float scale_x = scale, scale_y = scale;
  14841. int ix0, iy0, ix1, iy1;
  14842. int w, h;
  14843. unsigned char* data;
  14844. if (scale == 0) return NULL;
  14845. stbtt_GetGlyphBitmapBoxSubpixel(info, glyph, scale, scale, 0.0f, 0.0f, &ix0, &iy0, &ix1, &iy1);
  14846. /* if empty, return NULL */
  14847. if (ix0 == ix1 || iy0 == iy1)
  14848. return NULL;
  14849. ix0 -= padding;
  14850. iy0 -= padding;
  14851. ix1 += padding;
  14852. iy1 += padding;
  14853. w = (ix1 - ix0);
  14854. h = (iy1 - iy0);
  14855. if (width) *width = w;
  14856. if (height) *height = h;
  14857. if (xoff) *xoff = ix0;
  14858. if (yoff) *yoff = iy0;
  14859. /* invert for y-downwards bitmaps */
  14860. scale_y = -scale_y;
  14861. {
  14862. int x, y, i, j;
  14863. float* precompute;
  14864. stbtt_vertex* verts;
  14865. int num_verts = stbtt_GetGlyphShape(info, glyph, &verts);
  14866. data = (unsigned char*)STBTT_malloc(w * h, info->userdata);
  14867. precompute = (float*)STBTT_malloc(num_verts * sizeof(float), info->userdata);
  14868. for (i = 0, j = num_verts - 1; i < num_verts; j = i++) {
  14869. if (verts[i].type == STBTT_vline) {
  14870. float x0 = verts[i].x * scale_x, y0 = verts[i].y * scale_y;
  14871. float x1 = verts[j].x * scale_x, y1 = verts[j].y * scale_y;
  14872. float dist = (float)STBTT_sqrt((x1 - x0) * (x1 - x0) + (y1 - y0) * (y1 - y0));
  14873. precompute[i] = (dist == 0) ? 0.0f : 1.0f / dist;
  14874. }
  14875. else if (verts[i].type == STBTT_vcurve) {
  14876. float x2 = verts[j].x * scale_x, y2 = verts[j].y * scale_y;
  14877. float x1 = verts[i].cx * scale_x, y1 = verts[i].cy * scale_y;
  14878. float x0 = verts[i].x * scale_x, y0 = verts[i].y * scale_y;
  14879. float bx = x0 - 2 * x1 + x2, by = y0 - 2 * y1 + y2;
  14880. float len2 = bx * bx + by * by;
  14881. if (len2 != 0.0f)
  14882. precompute[i] = 1.0f / (bx * bx + by * by);
  14883. else
  14884. precompute[i] = 0.0f;
  14885. }
  14886. else
  14887. precompute[i] = 0.0f;
  14888. }
  14889. for (y = iy0; y < iy1; ++y) {
  14890. for (x = ix0; x < ix1; ++x) {
  14891. float val;
  14892. float min_dist = 999999.0f;
  14893. float sx = (float)x + 0.5f;
  14894. float sy = (float)y + 0.5f;
  14895. float x_gspace = (sx / scale_x);
  14896. float y_gspace = (sy / scale_y);
  14897. int winding = stbtt__compute_crossings_x(x_gspace, y_gspace, num_verts, verts); /* @OPTIMIZE: this could just be a rasterization, but needs to be line vs. non-tesselated curves so a new path */
  14898. for (i = 0; i < num_verts; ++i) {
  14899. float x0 = verts[i].x * scale_x, y0 = verts[i].y * scale_y;
  14900. if (verts[i].type == STBTT_vline && precompute[i] != 0.0f) {
  14901. float x1 = verts[i - 1].x * scale_x, y1 = verts[i - 1].y * scale_y;
  14902. float dist, dist2 = (x0 - sx) * (x0 - sx) + (y0 - sy) * (y0 - sy);
  14903. if (dist2 < min_dist * min_dist)
  14904. min_dist = (float)STBTT_sqrt(dist2);
  14905. /* coarse culling against bbox */
  14906. /* if (sx > STBTT_min(x0,x1)-min_dist && sx < STBTT_max(x0,x1)+min_dist && */
  14907. /* sy > STBTT_min(y0,y1)-min_dist && sy < STBTT_max(y0,y1)+min_dist) */
  14908. dist = (float)STBTT_fabs((x1 - x0) * (y0 - sy) - (y1 - y0) * (x0 - sx)) * precompute[i];
  14909. STBTT_assert(i != 0);
  14910. if (dist < min_dist) {
  14911. /* check position along line */
  14912. /* x' = x0 + t*(x1-x0), y' = y0 + t*(y1-y0) */
  14913. /* minimize (x'-sx)*(x'-sx)+(y'-sy)*(y'-sy) */
  14914. float dx = x1 - x0, dy = y1 - y0;
  14915. float px = x0 - sx, py = y0 - sy;
  14916. /* minimize (px+t*dx)^2 + (py+t*dy)^2 = px*px + 2*px*dx*t + t^2*dx*dx + py*py + 2*py*dy*t + t^2*dy*dy */
  14917. /* derivative: 2*px*dx + 2*py*dy + (2*dx*dx+2*dy*dy)*t, set to 0 and solve */
  14918. float t = -(px * dx + py * dy) / (dx * dx + dy * dy);
  14919. if (t >= 0.0f && t <= 1.0f)
  14920. min_dist = dist;
  14921. }
  14922. }
  14923. else if (verts[i].type == STBTT_vcurve) {
  14924. float x2 = verts[i - 1].x * scale_x, y2 = verts[i - 1].y * scale_y;
  14925. float x1 = verts[i].cx * scale_x, y1 = verts[i].cy * scale_y;
  14926. float box_x0 = STBTT_min(STBTT_min(x0, x1), x2);
  14927. float box_y0 = STBTT_min(STBTT_min(y0, y1), y2);
  14928. float box_x1 = STBTT_max(STBTT_max(x0, x1), x2);
  14929. float box_y1 = STBTT_max(STBTT_max(y0, y1), y2);
  14930. /* coarse culling against bbox to avoid computing cubic unnecessarily */
  14931. if (sx > box_x0 - min_dist && sx < box_x1 + min_dist && sy > box_y0 - min_dist && sy < box_y1 + min_dist) {
  14932. int num = 0;
  14933. float ax = x1 - x0, ay = y1 - y0;
  14934. float bx = x0 - 2 * x1 + x2, by = y0 - 2 * y1 + y2;
  14935. float mx = x0 - sx, my = y0 - sy;
  14936. float res[3] = { 0.f,0.f,0.f };
  14937. float px, py, t, it, dist2;
  14938. float a_inv = precompute[i];
  14939. if (a_inv == 0.0) { /* if a_inv is 0, it's 2nd degree so use quadratic formula */
  14940. float a = 3 * (ax * bx + ay * by);
  14941. float b = 2 * (ax * ax + ay * ay) + (mx * bx + my * by);
  14942. float c = mx * ax + my * ay;
  14943. if (a == 0.0) { /* if a is 0, it's linear */
  14944. if (b != 0.0) {
  14945. res[num++] = -c / b;
  14946. }
  14947. }
  14948. else {
  14949. float discriminant = b * b - 4 * a * c;
  14950. if (discriminant < 0)
  14951. num = 0;
  14952. else {
  14953. float root = (float)STBTT_sqrt(discriminant);
  14954. res[0] = (-b - root) / (2 * a);
  14955. res[1] = (-b + root) / (2 * a);
  14956. num = 2; /* don't bother distinguishing 1-solution case, as code below will still work */
  14957. }
  14958. }
  14959. }
  14960. else {
  14961. float b = 3 * (ax * bx + ay * by) * a_inv; /* could precompute this as it doesn't depend on sample point */
  14962. float c = (2 * (ax * ax + ay * ay) + (mx * bx + my * by)) * a_inv;
  14963. float d = (mx * ax + my * ay) * a_inv;
  14964. num = stbtt__solve_cubic(b, c, d, res);
  14965. }
  14966. dist2 = (x0 - sx) * (x0 - sx) + (y0 - sy) * (y0 - sy);
  14967. if (dist2 < min_dist * min_dist)
  14968. min_dist = (float)STBTT_sqrt(dist2);
  14969. if (num >= 1 && res[0] >= 0.0f && res[0] <= 1.0f) {
  14970. t = res[0], it = 1.0f - t;
  14971. px = it * it * x0 + 2 * t * it * x1 + t * t * x2;
  14972. py = it * it * y0 + 2 * t * it * y1 + t * t * y2;
  14973. dist2 = (px - sx) * (px - sx) + (py - sy) * (py - sy);
  14974. if (dist2 < min_dist * min_dist)
  14975. min_dist = (float)STBTT_sqrt(dist2);
  14976. }
  14977. if (num >= 2 && res[1] >= 0.0f && res[1] <= 1.0f) {
  14978. t = res[1], it = 1.0f - t;
  14979. px = it * it * x0 + 2 * t * it * x1 + t * t * x2;
  14980. py = it * it * y0 + 2 * t * it * y1 + t * t * y2;
  14981. dist2 = (px - sx) * (px - sx) + (py - sy) * (py - sy);
  14982. if (dist2 < min_dist * min_dist)
  14983. min_dist = (float)STBTT_sqrt(dist2);
  14984. }
  14985. if (num >= 3 && res[2] >= 0.0f && res[2] <= 1.0f) {
  14986. t = res[2], it = 1.0f - t;
  14987. px = it * it * x0 + 2 * t * it * x1 + t * t * x2;
  14988. py = it * it * y0 + 2 * t * it * y1 + t * t * y2;
  14989. dist2 = (px - sx) * (px - sx) + (py - sy) * (py - sy);
  14990. if (dist2 < min_dist * min_dist)
  14991. min_dist = (float)STBTT_sqrt(dist2);
  14992. }
  14993. }
  14994. }
  14995. }
  14996. if (winding == 0)
  14997. min_dist = -min_dist; /* if outside the shape, value is negative */
  14998. val = onedge_value + pixel_dist_scale * min_dist;
  14999. if (val < 0)
  15000. val = 0;
  15001. else if (val > 255)
  15002. val = 255;
  15003. data[(y - iy0) * w + (x - ix0)] = (unsigned char)val;
  15004. }
  15005. }
  15006. STBTT_free(precompute, info->userdata);
  15007. STBTT_free(verts, info->userdata);
  15008. }
  15009. return data;
  15010. }
  15011. STBTT_DEF unsigned char* stbtt_GetCodepointSDF(const stbtt_fontinfo* info, float scale, int codepoint, int padding, unsigned char onedge_value, float pixel_dist_scale, int* width, int* height, int* xoff, int* yoff)
  15012. {
  15013. return stbtt_GetGlyphSDF(info, scale, stbtt_FindGlyphIndex(info, codepoint), padding, onedge_value, pixel_dist_scale, width, height, xoff, yoff);
  15014. }
  15015. STBTT_DEF void stbtt_FreeSDF(unsigned char* bitmap, void* userdata)
  15016. {
  15017. STBTT_free(bitmap, userdata);
  15018. }
  15019. /* //////////////////////////////////////////////////////////////////////////// */
  15020. /* */
  15021. /* font name matching -- recommended not to use this */
  15022. /* */
  15023. /* check if a utf8 string contains a prefix which is the utf16 string; if so return length of matching utf8 string */
  15024. static stbtt_int32 stbtt__CompareUTF8toUTF16_bigendian_prefix(stbtt_uint8* s1, stbtt_int32 len1, stbtt_uint8* s2, stbtt_int32 len2)
  15025. {
  15026. stbtt_int32 i = 0;
  15027. /* convert utf16 to utf8 and compare the results while converting */
  15028. while (len2) {
  15029. stbtt_uint16 ch = s2[0] * 256 + s2[1];
  15030. if (ch < 0x80) {
  15031. if (i >= len1) return -1;
  15032. if (s1[i++] != ch) return -1;
  15033. }
  15034. else if (ch < 0x800) {
  15035. if (i + 1 >= len1) return -1;
  15036. if (s1[i++] != 0xc0 + (ch >> 6)) return -1;
  15037. if (s1[i++] != 0x80 + (ch & 0x3f)) return -1;
  15038. }
  15039. else if (ch >= 0xd800 && ch < 0xdc00) {
  15040. stbtt_uint32 c;
  15041. stbtt_uint16 ch2 = s2[2] * 256 + s2[3];
  15042. if (i + 3 >= len1) return -1;
  15043. c = ((ch - 0xd800) << 10) + (ch2 - 0xdc00) + 0x10000;
  15044. if (s1[i++] != 0xf0 + (c >> 18)) return -1;
  15045. if (s1[i++] != 0x80 + ((c >> 12) & 0x3f)) return -1;
  15046. if (s1[i++] != 0x80 + ((c >> 6) & 0x3f)) return -1;
  15047. if (s1[i++] != 0x80 + ((c) & 0x3f)) return -1;
  15048. s2 += 2; /* plus another 2 below */
  15049. len2 -= 2;
  15050. }
  15051. else if (ch >= 0xdc00 && ch < 0xe000) {
  15052. return -1;
  15053. }
  15054. else {
  15055. if (i + 2 >= len1) return -1;
  15056. if (s1[i++] != 0xe0 + (ch >> 12)) return -1;
  15057. if (s1[i++] != 0x80 + ((ch >> 6) & 0x3f)) return -1;
  15058. if (s1[i++] != 0x80 + ((ch) & 0x3f)) return -1;
  15059. }
  15060. s2 += 2;
  15061. len2 -= 2;
  15062. }
  15063. return i;
  15064. }
  15065. static int stbtt_CompareUTF8toUTF16_bigendian_internal(char* s1, int len1, char* s2, int len2)
  15066. {
  15067. return len1 == stbtt__CompareUTF8toUTF16_bigendian_prefix((stbtt_uint8*)s1, len1, (stbtt_uint8*)s2, len2);
  15068. }
  15069. /* returns results in whatever encoding you request... but note that 2-byte encodings */
  15070. /* will be BIG-ENDIAN... use stbtt_CompareUTF8toUTF16_bigendian() to compare */
  15071. STBTT_DEF const char* stbtt_GetFontNameString(const stbtt_fontinfo* font, int* length, int platformID, int encodingID, int languageID, int nameID)
  15072. {
  15073. stbtt_int32 i, count, stringOffset;
  15074. stbtt_uint8* fc = font->data;
  15075. stbtt_uint32 offset = font->fontstart;
  15076. stbtt_uint32 nm = stbtt__find_table(fc, offset, "name");
  15077. if (!nm) return NULL;
  15078. count = ttUSHORT(fc + nm + 2);
  15079. stringOffset = nm + ttUSHORT(fc + nm + 4);
  15080. for (i = 0; i < count; ++i) {
  15081. stbtt_uint32 loc = nm + 6 + 12 * i;
  15082. if (platformID == ttUSHORT(fc + loc + 0) && encodingID == ttUSHORT(fc + loc + 2)
  15083. && languageID == ttUSHORT(fc + loc + 4) && nameID == ttUSHORT(fc + loc + 6)) {
  15084. *length = ttUSHORT(fc + loc + 8);
  15085. return (const char*)(fc + stringOffset + ttUSHORT(fc + loc + 10));
  15086. }
  15087. }
  15088. return NULL;
  15089. }
  15090. static int stbtt__matchpair(stbtt_uint8* fc, stbtt_uint32 nm, stbtt_uint8* name, stbtt_int32 nlen, stbtt_int32 target_id, stbtt_int32 next_id)
  15091. {
  15092. stbtt_int32 i;
  15093. stbtt_int32 count = ttUSHORT(fc + nm + 2);
  15094. stbtt_int32 stringOffset = nm + ttUSHORT(fc + nm + 4);
  15095. for (i = 0; i < count; ++i) {
  15096. stbtt_uint32 loc = nm + 6 + 12 * i;
  15097. stbtt_int32 id = ttUSHORT(fc + loc + 6);
  15098. if (id == target_id) {
  15099. /* find the encoding */
  15100. stbtt_int32 platform = ttUSHORT(fc + loc + 0), encoding = ttUSHORT(fc + loc + 2), language = ttUSHORT(fc + loc + 4);
  15101. /* is this a Unicode encoding? */
  15102. if (platform == 0 || (platform == 3 && encoding == 1) || (platform == 3 && encoding == 10)) {
  15103. stbtt_int32 slen = ttUSHORT(fc + loc + 8);
  15104. stbtt_int32 off = ttUSHORT(fc + loc + 10);
  15105. /* check if there's a prefix match */
  15106. stbtt_int32 matchlen = stbtt__CompareUTF8toUTF16_bigendian_prefix(name, nlen, fc + stringOffset + off, slen);
  15107. if (matchlen >= 0) {
  15108. /* check for target_id+1 immediately following, with same encoding & language */
  15109. if (i + 1 < count && ttUSHORT(fc + loc + 12 + 6) == next_id && ttUSHORT(fc + loc + 12) == platform && ttUSHORT(fc + loc + 12 + 2) == encoding && ttUSHORT(fc + loc + 12 + 4) == language) {
  15110. slen = ttUSHORT(fc + loc + 12 + 8);
  15111. off = ttUSHORT(fc + loc + 12 + 10);
  15112. if (slen == 0) {
  15113. if (matchlen == nlen)
  15114. return 1;
  15115. }
  15116. else if (matchlen < nlen && name[matchlen] == ' ') {
  15117. ++matchlen;
  15118. if (stbtt_CompareUTF8toUTF16_bigendian_internal((char*)(name + matchlen), nlen - matchlen, (char*)(fc + stringOffset + off), slen))
  15119. return 1;
  15120. }
  15121. }
  15122. else {
  15123. /* if nothing immediately following */
  15124. if (matchlen == nlen)
  15125. return 1;
  15126. }
  15127. }
  15128. }
  15129. /* @TODO handle other encodings */
  15130. }
  15131. }
  15132. return 0;
  15133. }
  15134. static int stbtt__matches(stbtt_uint8* fc, stbtt_uint32 offset, stbtt_uint8* name, stbtt_int32 flags)
  15135. {
  15136. stbtt_int32 nlen = (stbtt_int32)STBTT_strlen((char*)name);
  15137. stbtt_uint32 nm, hd;
  15138. if (!stbtt__isfont(fc + offset)) return 0;
  15139. /* check italics/bold/underline flags in macStyle... */
  15140. if (flags) {
  15141. hd = stbtt__find_table(fc, offset, "head");
  15142. if ((ttUSHORT(fc + hd + 44) & 7) != (flags & 7)) return 0;
  15143. }
  15144. nm = stbtt__find_table(fc, offset, "name");
  15145. if (!nm) return 0;
  15146. if (flags) {
  15147. /* if we checked the macStyle flags, then just check the family and ignore the subfamily */
  15148. if (stbtt__matchpair(fc, nm, name, nlen, 16, -1)) return 1;
  15149. if (stbtt__matchpair(fc, nm, name, nlen, 1, -1)) return 1;
  15150. if (stbtt__matchpair(fc, nm, name, nlen, 3, -1)) return 1;
  15151. }
  15152. else {
  15153. if (stbtt__matchpair(fc, nm, name, nlen, 16, 17)) return 1;
  15154. if (stbtt__matchpair(fc, nm, name, nlen, 1, 2)) return 1;
  15155. if (stbtt__matchpair(fc, nm, name, nlen, 3, -1)) return 1;
  15156. }
  15157. return 0;
  15158. }
  15159. static int stbtt_FindMatchingFont_internal(unsigned char* font_collection, char* name_utf8, stbtt_int32 flags)
  15160. {
  15161. stbtt_int32 i;
  15162. for (i = 0;; ++i) {
  15163. stbtt_int32 off = stbtt_GetFontOffsetForIndex(font_collection, i);
  15164. if (off < 0) return off;
  15165. if (stbtt__matches((stbtt_uint8*)font_collection, off, (stbtt_uint8*)name_utf8, flags))
  15166. return off;
  15167. }
  15168. }
  15169. #if defined(__GNUC__) || defined(__clang__)
  15170. #pragma GCC diagnostic push
  15171. #pragma GCC diagnostic ignored "-Wcast-qual"
  15172. #endif
  15173. STBTT_DEF int stbtt_BakeFontBitmap(const unsigned char* data, int offset,
  15174. float pixel_height, unsigned char* pixels, int pw, int ph,
  15175. int first_char, int num_chars, stbtt_bakedchar* chardata)
  15176. {
  15177. return stbtt_BakeFontBitmap_internal((unsigned char*)data, offset, pixel_height, pixels, pw, ph, first_char, num_chars, chardata);
  15178. }
  15179. STBTT_DEF int stbtt_GetFontOffsetForIndex(const unsigned char* data, int index)
  15180. {
  15181. return stbtt_GetFontOffsetForIndex_internal((unsigned char*)data, index);
  15182. }
  15183. STBTT_DEF int stbtt_GetNumberOfFonts(const unsigned char* data)
  15184. {
  15185. return stbtt_GetNumberOfFonts_internal((unsigned char*)data);
  15186. }
  15187. STBTT_DEF int stbtt_InitFont(stbtt_fontinfo* info, const unsigned char* data, int offset)
  15188. {
  15189. return stbtt_InitFont_internal(info, (unsigned char*)data, offset);
  15190. }
  15191. STBTT_DEF int stbtt_FindMatchingFont(const unsigned char* fontdata, const char* name, int flags)
  15192. {
  15193. return stbtt_FindMatchingFont_internal((unsigned char*)fontdata, (char*)name, flags);
  15194. }
  15195. STBTT_DEF int stbtt_CompareUTF8toUTF16_bigendian(const char* s1, int len1, const char* s2, int len2)
  15196. {
  15197. return stbtt_CompareUTF8toUTF16_bigendian_internal((char*)s1, len1, (char*)s2, len2);
  15198. }
  15199. #if defined(__GNUC__) || defined(__clang__)
  15200. #pragma GCC diagnostic pop
  15201. #endif
  15202. #endif /* STB_TRUETYPE_IMPLEMENTATION */
  15203. /* FULL VERSION HISTORY */
  15204. /* */
  15205. /* 1.25 (2021-07-11) many fixes */
  15206. /* 1.24 (2020-02-05) fix warning */
  15207. /* 1.23 (2020-02-02) query SVG data for glyphs; query whole kerning table (but only kern not GPOS) */
  15208. /* 1.22 (2019-08-11) minimize missing-glyph duplication; fix kerning if both 'GPOS' and 'kern' are defined */
  15209. /* 1.21 (2019-02-25) fix warning */
  15210. /* 1.20 (2019-02-07) PackFontRange skips missing codepoints; GetScaleFontVMetrics() */
  15211. /* 1.19 (2018-02-11) OpenType GPOS kerning (horizontal only), STBTT_fmod */
  15212. /* 1.18 (2018-01-29) add missing function */
  15213. /* 1.17 (2017-07-23) make more arguments const; doc fix */
  15214. /* 1.16 (2017-07-12) SDF support */
  15215. /* 1.15 (2017-03-03) make more arguments const */
  15216. /* 1.14 (2017-01-16) num-fonts-in-TTC function */
  15217. /* 1.13 (2017-01-02) support OpenType fonts, certain Apple fonts */
  15218. /* 1.12 (2016-10-25) suppress warnings about casting away const with -Wcast-qual */
  15219. /* 1.11 (2016-04-02) fix unused-variable warning */
  15220. /* 1.10 (2016-04-02) allow user-defined fabs() replacement */
  15221. /* fix memory leak if fontsize=0.0 */
  15222. /* fix warning from duplicate typedef */
  15223. /* 1.09 (2016-01-16) warning fix; avoid crash on outofmem; use alloc userdata for PackFontRanges */
  15224. /* 1.08 (2015-09-13) document stbtt_Rasterize(); fixes for vertical & horizontal edges */
  15225. /* 1.07 (2015-08-01) allow PackFontRanges to accept arrays of sparse codepoints; */
  15226. /* allow PackFontRanges to pack and render in separate phases; */
  15227. /* fix stbtt_GetFontOFfsetForIndex (never worked for non-0 input?); */
  15228. /* fixed an assert() bug in the new rasterizer */
  15229. /* replace assert() with STBTT_assert() in new rasterizer */
  15230. /* 1.06 (2015-07-14) performance improvements (~35% faster on x86 and x64 on test machine) */
  15231. /* also more precise AA rasterizer, except if shapes overlap */
  15232. /* remove need for STBTT_sort */
  15233. /* 1.05 (2015-04-15) fix misplaced definitions for STBTT_STATIC */
  15234. /* 1.04 (2015-04-15) typo in example */
  15235. /* 1.03 (2015-04-12) STBTT_STATIC, fix memory leak in new packing, various fixes */
  15236. /* 1.02 (2014-12-10) fix various warnings & compile issues w/ stb_rect_pack, C++ */
  15237. /* 1.01 (2014-12-08) fix subpixel position when oversampling to exactly match */
  15238. /* non-oversampled; STBTT_POINT_SIZE for packed case only */
  15239. /* 1.00 (2014-12-06) add new PackBegin etc. API, w/ support for oversampling */
  15240. /* 0.99 (2014-09-18) fix multiple bugs with subpixel rendering (ryg) */
  15241. /* 0.9 (2014-08-07) support certain mac/iOS fonts without an MS platformID */
  15242. /* 0.8b (2014-07-07) fix a warning */
  15243. /* 0.8 (2014-05-25) fix a few more warnings */
  15244. /* 0.7 (2013-09-25) bugfix: subpixel glyph bug fixed in 0.5 had come back */
  15245. /* 0.6c (2012-07-24) improve documentation */
  15246. /* 0.6b (2012-07-20) fix a few more warnings */
  15247. /* 0.6 (2012-07-17) fix warnings; added stbtt_ScaleForMappingEmToPixels, */
  15248. /* stbtt_GetFontBoundingBox, stbtt_IsGlyphEmpty */
  15249. /* 0.5 (2011-12-09) bugfixes: */
  15250. /* subpixel glyph renderer computed wrong bounding box */
  15251. /* first vertex of shape can be off-curve (FreeSans) */
  15252. /* 0.4b (2011-12-03) fixed an error in the font baking example */
  15253. /* 0.4 (2011-12-01) kerning, subpixel rendering (tor) */
  15254. /* bugfixes for: */
  15255. /* codepoint-to-glyph conversion using table fmt=12 */
  15256. /* codepoint-to-glyph conversion using table fmt=4 */
  15257. /* stbtt_GetBakedQuad with non-square texture (Zer) */
  15258. /* updated Hello World! sample to use kerning and subpixel */
  15259. /* fixed some warnings */
  15260. /* 0.3 (2009-06-24) cmap fmt=12, compound shapes (MM) */
  15261. /* userdata, malloc-from-userdata, non-zero fill (stb) */
  15262. /* 0.2 (2009-03-11) Fix unsigned/signed char warnings */
  15263. /* 0.1 (2009-03-09) First public release */
  15264. /* */
  15265. /*
  15266. ------------------------------------------------------------------------------
  15267. This software is available under 2 licenses -- choose whichever you prefer.
  15268. ------------------------------------------------------------------------------
  15269. ALTERNATIVE A - MIT License
  15270. Copyright (c) 2017 Sean Barrett
  15271. Permission is hereby granted, free of charge, to any person obtaining a copy of
  15272. this software and associated documentation files (the "Software"), to deal in
  15273. the Software without restriction, including without limitation the rights to
  15274. use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
  15275. of the Software, and to permit persons to whom the Software is furnished to do
  15276. so, subject to the following conditions:
  15277. The above copyright notice and this permission notice shall be included in all
  15278. copies or substantial portions of the Software.
  15279. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15280. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15281. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15282. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15283. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  15284. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  15285. SOFTWARE.
  15286. ------------------------------------------------------------------------------
  15287. ALTERNATIVE B - Public Domain (www.unlicense.org)
  15288. This is free and unencumbered software released into the public domain.
  15289. Anyone is free to copy, modify, publish, use, compile, sell, or distribute this
  15290. software, either in source code form or as a compiled binary, for any purpose,
  15291. commercial or non-commercial, and by any means.
  15292. In jurisdictions that recognize copyright laws, the author or authors of this
  15293. software dedicate any and all copyright interest in the software to the public
  15294. domain. We make this dedication for the benefit of the public at large and to
  15295. the detriment of our heirs and successors. We intend this dedication to be an
  15296. overt act of relinquishment in perpetuity of all present and future rights to
  15297. this software under copyright law.
  15298. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15299. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15300. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15301. AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  15302. ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  15303. WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  15304. ------------------------------------------------------------------------------
  15305. */
  15306. #ifdef NK_INCLUDE_FONT_BAKING
  15307. /* -------------------------------------------------------------
  15308. *
  15309. * RECT PACK
  15310. *
  15311. * --------------------------------------------------------------*/
  15312. /*
  15313. * ==============================================================
  15314. *
  15315. * TRUETYPE
  15316. *
  15317. * ===============================================================
  15318. */
  15319. #define STBTT_MAX_OVERSAMPLE 8
  15320. /* -------------------------------------------------------------
  15321. *
  15322. * FONT BAKING
  15323. *
  15324. * --------------------------------------------------------------*/
  15325. struct nk_font_bake_data {
  15326. struct stbtt_fontinfo info;
  15327. struct stbrp_rect* rects;
  15328. stbtt_pack_range* ranges;
  15329. nk_rune range_count;
  15330. };
  15331. struct nk_font_baker {
  15332. struct nk_allocator alloc;
  15333. struct stbtt_pack_context spc;
  15334. struct nk_font_bake_data* build;
  15335. stbtt_packedchar* packed_chars;
  15336. struct stbrp_rect* rects;
  15337. stbtt_pack_range* ranges;
  15338. };
  15339. NK_GLOBAL const nk_size nk_rect_align = NK_ALIGNOF(struct stbrp_rect);
  15340. NK_GLOBAL const nk_size nk_range_align = NK_ALIGNOF(stbtt_pack_range);
  15341. NK_GLOBAL const nk_size nk_char_align = NK_ALIGNOF(stbtt_packedchar);
  15342. NK_GLOBAL const nk_size nk_build_align = NK_ALIGNOF(struct nk_font_bake_data);
  15343. NK_GLOBAL const nk_size nk_baker_align = NK_ALIGNOF(struct nk_font_baker);
  15344. NK_INTERN int
  15345. nk_range_count(const nk_rune* range)
  15346. {
  15347. const nk_rune* iter = range;
  15348. NK_ASSERT(range);
  15349. if (!range) return 0;
  15350. while (*(iter++) != 0);
  15351. return (iter == range) ? 0 : (int)((iter - range) / 2);
  15352. }
  15353. NK_INTERN int
  15354. nk_range_glyph_count(const nk_rune* range, int count)
  15355. {
  15356. int i = 0;
  15357. int total_glyphs = 0;
  15358. for (i = 0; i < count; ++i) {
  15359. int diff;
  15360. nk_rune f = range[(i * 2) + 0];
  15361. nk_rune t = range[(i * 2) + 1];
  15362. NK_ASSERT(t >= f);
  15363. diff = (int)((t - f) + 1);
  15364. total_glyphs += diff;
  15365. }
  15366. return total_glyphs;
  15367. }
  15368. NK_API const nk_rune*
  15369. nk_font_default_glyph_ranges(void)
  15370. {
  15371. NK_STORAGE const nk_rune ranges[] = { 0x0020, 0x00FF, 0 };
  15372. return ranges;
  15373. }
  15374. NK_API const nk_rune*
  15375. nk_font_chinese_glyph_ranges(void)
  15376. {
  15377. NK_STORAGE const nk_rune ranges[] = {
  15378. 0x0020, 0x00FF,
  15379. 0x3000, 0x30FF,
  15380. 0x31F0, 0x31FF,
  15381. 0xFF00, 0xFFEF,
  15382. 0x4E00, 0x9FAF,
  15383. 0
  15384. };
  15385. return ranges;
  15386. }
  15387. NK_API const nk_rune*
  15388. nk_font_cyrillic_glyph_ranges(void)
  15389. {
  15390. NK_STORAGE const nk_rune ranges[] = {
  15391. 0x0020, 0x00FF,
  15392. 0x0400, 0x052F,
  15393. 0x2DE0, 0x2DFF,
  15394. 0xA640, 0xA69F,
  15395. 0
  15396. };
  15397. return ranges;
  15398. }
  15399. NK_API const nk_rune*
  15400. nk_font_korean_glyph_ranges(void)
  15401. {
  15402. NK_STORAGE const nk_rune ranges[] = {
  15403. 0x0020, 0x00FF,
  15404. 0x3131, 0x3163,
  15405. 0xAC00, 0xD79D,
  15406. 0
  15407. };
  15408. return ranges;
  15409. }
  15410. NK_INTERN void
  15411. nk_font_baker_memory(nk_size* temp, int* glyph_count,
  15412. struct nk_font_config* config_list, int count)
  15413. {
  15414. int range_count = 0;
  15415. int total_range_count = 0;
  15416. struct nk_font_config* iter, * i;
  15417. NK_ASSERT(config_list);
  15418. NK_ASSERT(glyph_count);
  15419. if (!config_list) {
  15420. *temp = 0;
  15421. *glyph_count = 0;
  15422. return;
  15423. }
  15424. *glyph_count = 0;
  15425. for (iter = config_list; iter; iter = iter->next) {
  15426. i = iter;
  15427. do {
  15428. if (!i->range) iter->range = nk_font_default_glyph_ranges();
  15429. range_count = nk_range_count(i->range);
  15430. total_range_count += range_count;
  15431. *glyph_count += nk_range_glyph_count(i->range, range_count);
  15432. } while ((i = i->n) != iter);
  15433. }
  15434. *temp = (nk_size)*glyph_count * sizeof(struct stbrp_rect);
  15435. *temp += (nk_size)total_range_count * sizeof(stbtt_pack_range);
  15436. *temp += (nk_size)*glyph_count * sizeof(stbtt_packedchar);
  15437. *temp += (nk_size)count * sizeof(struct nk_font_bake_data);
  15438. *temp += sizeof(struct nk_font_baker);
  15439. *temp += nk_rect_align + nk_range_align + nk_char_align;
  15440. *temp += nk_build_align + nk_baker_align;
  15441. }
  15442. NK_INTERN struct nk_font_baker*
  15443. nk_font_baker(void* memory, int glyph_count, int count, struct nk_allocator* alloc)
  15444. {
  15445. struct nk_font_baker* baker;
  15446. if (!memory) return 0;
  15447. /* setup baker inside a memory block */
  15448. baker = (struct nk_font_baker*)NK_ALIGN_PTR(memory, nk_baker_align);
  15449. baker->build = (struct nk_font_bake_data*)NK_ALIGN_PTR((baker + 1), nk_build_align);
  15450. baker->packed_chars = (stbtt_packedchar*)NK_ALIGN_PTR((baker->build + count), nk_char_align);
  15451. baker->rects = (struct stbrp_rect*)NK_ALIGN_PTR((baker->packed_chars + glyph_count), nk_rect_align);
  15452. baker->ranges = (stbtt_pack_range*)NK_ALIGN_PTR((baker->rects + glyph_count), nk_range_align);
  15453. baker->alloc = *alloc;
  15454. return baker;
  15455. }
  15456. NK_INTERN int
  15457. nk_font_bake_pack(struct nk_font_baker* baker,
  15458. nk_size* image_memory, int* width, int* height, struct nk_recti* custom,
  15459. const struct nk_font_config* config_list, int count,
  15460. struct nk_allocator* alloc)
  15461. {
  15462. NK_STORAGE const nk_size max_height = 1024 * 32;
  15463. const struct nk_font_config* config_iter, * it;
  15464. int total_glyph_count = 0;
  15465. int total_range_count = 0;
  15466. int range_count = 0;
  15467. int i = 0;
  15468. NK_ASSERT(image_memory);
  15469. NK_ASSERT(width);
  15470. NK_ASSERT(height);
  15471. NK_ASSERT(config_list);
  15472. NK_ASSERT(count);
  15473. NK_ASSERT(alloc);
  15474. if (!image_memory || !width || !height || !config_list || !count) return nk_false;
  15475. for (config_iter = config_list; config_iter; config_iter = config_iter->next) {
  15476. it = config_iter;
  15477. do {
  15478. range_count = nk_range_count(it->range);
  15479. total_range_count += range_count;
  15480. total_glyph_count += nk_range_glyph_count(it->range, range_count);
  15481. } while ((it = it->n) != config_iter);
  15482. }
  15483. /* setup font baker from temporary memory */
  15484. for (config_iter = config_list; config_iter; config_iter = config_iter->next) {
  15485. it = config_iter;
  15486. do {
  15487. struct stbtt_fontinfo* font_info = &baker->build[i++].info;
  15488. font_info->userdata = alloc;
  15489. if (!stbtt_InitFont(font_info, (const unsigned char*)it->ttf_blob, stbtt_GetFontOffsetForIndex((const unsigned char*)it->ttf_blob, 0)))
  15490. return nk_false;
  15491. } while ((it = it->n) != config_iter);
  15492. }
  15493. *height = 0;
  15494. *width = (total_glyph_count > 1000) ? 1024 : 512;
  15495. stbtt_PackBegin(&baker->spc, 0, (int)*width, (int)max_height, 0, 1, alloc);
  15496. {
  15497. int input_i = 0;
  15498. int range_n = 0;
  15499. int rect_n = 0;
  15500. int char_n = 0;
  15501. if (custom) {
  15502. /* pack custom user data first so it will be in the upper left corner*/
  15503. struct stbrp_rect custom_space;
  15504. nk_zero(&custom_space, sizeof(custom_space));
  15505. custom_space.w = (stbrp_coord)(custom->w);
  15506. custom_space.h = (stbrp_coord)(custom->h);
  15507. stbtt_PackSetOversampling(&baker->spc, 1, 1);
  15508. stbrp_pack_rects((struct stbrp_context*)baker->spc.pack_info, &custom_space, 1);
  15509. *height = NK_MAX(*height, (int)(custom_space.y + custom_space.h));
  15510. custom->x = (short)custom_space.x;
  15511. custom->y = (short)custom_space.y;
  15512. custom->w = (short)custom_space.w;
  15513. custom->h = (short)custom_space.h;
  15514. }
  15515. /* first font pass: pack all glyphs */
  15516. for (input_i = 0, config_iter = config_list; input_i < count && config_iter;
  15517. config_iter = config_iter->next) {
  15518. it = config_iter;
  15519. do {
  15520. int n = 0;
  15521. int glyph_count;
  15522. const nk_rune* in_range;
  15523. const struct nk_font_config* cfg = it;
  15524. struct nk_font_bake_data* tmp = &baker->build[input_i++];
  15525. /* count glyphs + ranges in current font */
  15526. glyph_count = 0; range_count = 0;
  15527. for (in_range = cfg->range; in_range[0] && in_range[1]; in_range += 2) {
  15528. glyph_count += (int)(in_range[1] - in_range[0]) + 1;
  15529. range_count++;
  15530. }
  15531. /* setup ranges */
  15532. tmp->ranges = baker->ranges + range_n;
  15533. tmp->range_count = (nk_rune)range_count;
  15534. range_n += range_count;
  15535. for (i = 0; i < range_count; ++i) {
  15536. in_range = &cfg->range[i * 2];
  15537. tmp->ranges[i].font_size = cfg->size;
  15538. tmp->ranges[i].first_unicode_codepoint_in_range = (int)in_range[0];
  15539. tmp->ranges[i].num_chars = (int)(in_range[1] - in_range[0]) + 1;
  15540. tmp->ranges[i].chardata_for_range = baker->packed_chars + char_n;
  15541. char_n += tmp->ranges[i].num_chars;
  15542. }
  15543. /* pack */
  15544. tmp->rects = baker->rects + rect_n;
  15545. rect_n += glyph_count;
  15546. stbtt_PackSetOversampling(&baker->spc, cfg->oversample_h, cfg->oversample_v);
  15547. n = stbtt_PackFontRangesGatherRects(&baker->spc, &tmp->info,
  15548. tmp->ranges, (int)tmp->range_count, tmp->rects);
  15549. stbrp_pack_rects((struct stbrp_context*)baker->spc.pack_info, tmp->rects, (int)n);
  15550. /* texture height */
  15551. for (i = 0; i < n; ++i) {
  15552. if (tmp->rects[i].was_packed)
  15553. *height = NK_MAX(*height, tmp->rects[i].y + tmp->rects[i].h);
  15554. }
  15555. } while ((it = it->n) != config_iter);
  15556. }
  15557. NK_ASSERT(rect_n == total_glyph_count);
  15558. NK_ASSERT(char_n == total_glyph_count);
  15559. NK_ASSERT(range_n == total_range_count);
  15560. }
  15561. *height = (int)nk_round_up_pow2((nk_uint)*height);
  15562. *image_memory = (nk_size)(*width) * (nk_size)(*height);
  15563. return nk_true;
  15564. }
  15565. NK_INTERN void
  15566. nk_font_bake(struct nk_font_baker* baker, void* image_memory, int width, int height,
  15567. struct nk_font_glyph* glyphs, int glyphs_count,
  15568. const struct nk_font_config* config_list, int font_count)
  15569. {
  15570. int input_i = 0;
  15571. nk_rune glyph_n = 0;
  15572. const struct nk_font_config* config_iter;
  15573. const struct nk_font_config* it;
  15574. NK_ASSERT(image_memory);
  15575. NK_ASSERT(width);
  15576. NK_ASSERT(height);
  15577. NK_ASSERT(config_list);
  15578. NK_ASSERT(baker);
  15579. NK_ASSERT(font_count);
  15580. NK_ASSERT(glyphs_count);
  15581. if (!image_memory || !width || !height || !config_list ||
  15582. !font_count || !glyphs || !glyphs_count)
  15583. return;
  15584. /* second font pass: render glyphs */
  15585. nk_zero(image_memory, (nk_size)((nk_size)width * (nk_size)height));
  15586. baker->spc.pixels = (unsigned char*)image_memory;
  15587. baker->spc.height = (int)height;
  15588. for (input_i = 0, config_iter = config_list; input_i < font_count && config_iter;
  15589. config_iter = config_iter->next) {
  15590. it = config_iter;
  15591. do {
  15592. const struct nk_font_config* cfg = it;
  15593. struct nk_font_bake_data* tmp = &baker->build[input_i++];
  15594. stbtt_PackSetOversampling(&baker->spc, cfg->oversample_h, cfg->oversample_v);
  15595. stbtt_PackFontRangesRenderIntoRects(&baker->spc, &tmp->info, tmp->ranges, (int)tmp->range_count, tmp->rects);
  15596. } while ((it = it->n) != config_iter);
  15597. } stbtt_PackEnd(&baker->spc);
  15598. /* third pass: setup font and glyphs */
  15599. for (input_i = 0, config_iter = config_list; input_i < font_count && config_iter;
  15600. config_iter = config_iter->next) {
  15601. it = config_iter;
  15602. do {
  15603. nk_size i = 0;
  15604. int char_idx = 0;
  15605. nk_rune glyph_count = 0;
  15606. const struct nk_font_config* cfg = it;
  15607. struct nk_font_bake_data* tmp = &baker->build[input_i++];
  15608. struct nk_baked_font* dst_font = cfg->font;
  15609. float font_scale = stbtt_ScaleForPixelHeight(&tmp->info, cfg->size);
  15610. int unscaled_ascent, unscaled_descent, unscaled_line_gap;
  15611. stbtt_GetFontVMetrics(&tmp->info, &unscaled_ascent, &unscaled_descent,
  15612. &unscaled_line_gap);
  15613. /* fill baked font */
  15614. if (!cfg->merge_mode) {
  15615. dst_font->ranges = cfg->range;
  15616. dst_font->height = cfg->size;
  15617. dst_font->ascent = ((float)unscaled_ascent * font_scale);
  15618. dst_font->descent = ((float)unscaled_descent * font_scale);
  15619. dst_font->glyph_offset = glyph_n;
  15620. /*
  15621. Need to zero this, or it will carry over from a previous
  15622. bake, and cause a segfault when accessing glyphs[].
  15623. */
  15624. dst_font->glyph_count = 0;
  15625. }
  15626. /* fill own baked font glyph array */
  15627. for (i = 0; i < tmp->range_count; ++i) {
  15628. stbtt_pack_range* range = &tmp->ranges[i];
  15629. for (char_idx = 0; char_idx < range->num_chars; char_idx++)
  15630. {
  15631. nk_rune codepoint = 0;
  15632. float dummy_x = 0, dummy_y = 0;
  15633. stbtt_aligned_quad q;
  15634. struct nk_font_glyph* glyph;
  15635. /* query glyph bounds from stb_truetype */
  15636. const stbtt_packedchar* pc = &range->chardata_for_range[char_idx];
  15637. codepoint = (nk_rune)(range->first_unicode_codepoint_in_range + char_idx);
  15638. stbtt_GetPackedQuad(range->chardata_for_range, (int)width,
  15639. (int)height, char_idx, &dummy_x, &dummy_y, &q, 0);
  15640. /* fill own glyph type with data */
  15641. glyph = &glyphs[dst_font->glyph_offset + dst_font->glyph_count + (unsigned int)glyph_count];
  15642. glyph->codepoint = codepoint;
  15643. glyph->x0 = q.x0; glyph->y0 = q.y0;
  15644. glyph->x1 = q.x1; glyph->y1 = q.y1;
  15645. glyph->y0 += (dst_font->ascent + 0.5f);
  15646. glyph->y1 += (dst_font->ascent + 0.5f);
  15647. glyph->w = glyph->x1 - glyph->x0 + 0.5f;
  15648. glyph->h = glyph->y1 - glyph->y0;
  15649. if (cfg->coord_type == NK_COORD_PIXEL) {
  15650. glyph->u0 = q.s0 * (float)width;
  15651. glyph->v0 = q.t0 * (float)height;
  15652. glyph->u1 = q.s1 * (float)width;
  15653. glyph->v1 = q.t1 * (float)height;
  15654. }
  15655. else {
  15656. glyph->u0 = q.s0;
  15657. glyph->v0 = q.t0;
  15658. glyph->u1 = q.s1;
  15659. glyph->v1 = q.t1;
  15660. }
  15661. glyph->xadvance = (pc->xadvance + cfg->spacing.x);
  15662. if (cfg->pixel_snap)
  15663. glyph->xadvance = (float)(int)(glyph->xadvance + 0.5f);
  15664. glyph_count++;
  15665. }
  15666. }
  15667. dst_font->glyph_count += glyph_count;
  15668. glyph_n += glyph_count;
  15669. } while ((it = it->n) != config_iter);
  15670. }
  15671. }
  15672. NK_INTERN void
  15673. nk_font_bake_custom_data(void* img_memory, int img_width, int img_height,
  15674. struct nk_recti img_dst, const char* texture_data_mask, int tex_width,
  15675. int tex_height, char white, char black)
  15676. {
  15677. nk_byte* pixels;
  15678. int y = 0;
  15679. int x = 0;
  15680. int n = 0;
  15681. NK_ASSERT(img_memory);
  15682. NK_ASSERT(img_width);
  15683. NK_ASSERT(img_height);
  15684. NK_ASSERT(texture_data_mask);
  15685. NK_UNUSED(tex_height);
  15686. if (!img_memory || !img_width || !img_height || !texture_data_mask)
  15687. return;
  15688. pixels = (nk_byte*)img_memory;
  15689. for (y = 0, n = 0; y < tex_height; ++y) {
  15690. for (x = 0; x < tex_width; ++x, ++n) {
  15691. const int off0 = ((img_dst.x + x) + (img_dst.y + y) * img_width);
  15692. const int off1 = off0 + 1 + tex_width;
  15693. pixels[off0] = (texture_data_mask[n] == white) ? 0xFF : 0x00;
  15694. pixels[off1] = (texture_data_mask[n] == black) ? 0xFF : 0x00;
  15695. }
  15696. }
  15697. }
  15698. NK_INTERN void
  15699. nk_font_bake_convert(void* out_memory, int img_width, int img_height,
  15700. const void* in_memory)
  15701. {
  15702. int n = 0;
  15703. nk_rune* dst;
  15704. const nk_byte* src;
  15705. NK_ASSERT(out_memory);
  15706. NK_ASSERT(in_memory);
  15707. NK_ASSERT(img_width);
  15708. NK_ASSERT(img_height);
  15709. if (!out_memory || !in_memory || !img_height || !img_width) return;
  15710. dst = (nk_rune*)out_memory;
  15711. src = (const nk_byte*)in_memory;
  15712. for (n = (int)(img_width * img_height); n > 0; n--)
  15713. *dst++ = ((nk_rune)(*src++) << 24) | 0x00FFFFFF;
  15714. }
  15715. /* -------------------------------------------------------------
  15716. *
  15717. * FONT
  15718. *
  15719. * --------------------------------------------------------------*/
  15720. NK_INTERN float
  15721. nk_font_text_width(nk_handle handle, float height, const char* text, int len)
  15722. {
  15723. nk_rune unicode;
  15724. int text_len = 0;
  15725. float text_width = 0;
  15726. int glyph_len = 0;
  15727. float scale = 0;
  15728. struct nk_font* font = (struct nk_font*)handle.ptr;
  15729. NK_ASSERT(font);
  15730. NK_ASSERT(font->glyphs);
  15731. if (!font || !text || !len)
  15732. return 0;
  15733. scale = height / font->info.height;
  15734. glyph_len = text_len = nk_utf_decode(text, &unicode, (int)len);
  15735. if (!glyph_len) return 0;
  15736. while (text_len <= (int)len && glyph_len) {
  15737. const struct nk_font_glyph* g;
  15738. if (unicode == NK_UTF_INVALID) break;
  15739. /* query currently drawn glyph information */
  15740. g = nk_font_find_glyph(font, unicode);
  15741. text_width += g->xadvance * scale;
  15742. /* offset next glyph */
  15743. glyph_len = nk_utf_decode(text + text_len, &unicode, (int)len - text_len);
  15744. text_len += glyph_len;
  15745. }
  15746. return text_width;
  15747. }
  15748. #ifdef NK_INCLUDE_VERTEX_BUFFER_OUTPUT
  15749. NK_INTERN void
  15750. nk_font_query_font_glyph(nk_handle handle, float height,
  15751. struct nk_user_font_glyph* glyph, nk_rune codepoint, nk_rune next_codepoint)
  15752. {
  15753. float scale;
  15754. const struct nk_font_glyph* g;
  15755. struct nk_font* font;
  15756. NK_ASSERT(glyph);
  15757. NK_UNUSED(next_codepoint);
  15758. font = (struct nk_font*)handle.ptr;
  15759. NK_ASSERT(font);
  15760. NK_ASSERT(font->glyphs);
  15761. if (!font || !glyph)
  15762. return;
  15763. scale = height / font->info.height;
  15764. g = nk_font_find_glyph(font, codepoint);
  15765. glyph->width = (g->x1 - g->x0) * scale;
  15766. glyph->height = (g->y1 - g->y0) * scale;
  15767. glyph->offset = nk_vec2(g->x0 * scale, g->y0 * scale);
  15768. glyph->xadvance = (g->xadvance * scale);
  15769. glyph->uv[0] = nk_vec2(g->u0, g->v0);
  15770. glyph->uv[1] = nk_vec2(g->u1, g->v1);
  15771. }
  15772. #endif
  15773. NK_API const struct nk_font_glyph*
  15774. nk_font_find_glyph(struct nk_font* font, nk_rune unicode)
  15775. {
  15776. int i = 0;
  15777. int count;
  15778. int total_glyphs = 0;
  15779. const struct nk_font_glyph* glyph = 0;
  15780. const struct nk_font_config* iter = 0;
  15781. NK_ASSERT(font);
  15782. NK_ASSERT(font->glyphs);
  15783. NK_ASSERT(font->info.ranges);
  15784. if (!font || !font->glyphs) return 0;
  15785. glyph = font->fallback;
  15786. iter = font->config;
  15787. do {
  15788. count = nk_range_count(iter->range);
  15789. for (i = 0; i < count; ++i) {
  15790. nk_rune f = iter->range[(i * 2) + 0];
  15791. nk_rune t = iter->range[(i * 2) + 1];
  15792. int diff = (int)((t - f) + 1);
  15793. if (unicode >= f && unicode <= t)
  15794. return &font->glyphs[((nk_rune)total_glyphs + (unicode - f))];
  15795. total_glyphs += diff;
  15796. }
  15797. } while ((iter = iter->n) != font->config);
  15798. return glyph;
  15799. }
  15800. NK_INTERN void
  15801. nk_font_init(struct nk_font* font, float pixel_height,
  15802. nk_rune fallback_codepoint, struct nk_font_glyph* glyphs,
  15803. const struct nk_baked_font* baked_font, nk_handle atlas)
  15804. {
  15805. struct nk_baked_font baked;
  15806. NK_ASSERT(font);
  15807. NK_ASSERT(glyphs);
  15808. NK_ASSERT(baked_font);
  15809. if (!font || !glyphs || !baked_font)
  15810. return;
  15811. baked = *baked_font;
  15812. font->fallback = 0;
  15813. font->info = baked;
  15814. font->scale = (float)pixel_height / (float)font->info.height;
  15815. font->glyphs = &glyphs[baked_font->glyph_offset];
  15816. font->texture = atlas;
  15817. font->fallback_codepoint = fallback_codepoint;
  15818. font->fallback = nk_font_find_glyph(font, fallback_codepoint);
  15819. font->handle.height = font->info.height * font->scale;
  15820. font->handle.width = nk_font_text_width;
  15821. font->handle.userdata.ptr = font;
  15822. #ifdef NK_INCLUDE_VERTEX_BUFFER_OUTPUT
  15823. font->handle.query = nk_font_query_font_glyph;
  15824. font->handle.texture = font->texture;
  15825. #endif
  15826. }
  15827. /* ---------------------------------------------------------------------------
  15828. *
  15829. * DEFAULT FONT
  15830. *
  15831. * ProggyClean.ttf
  15832. * Copyright (c) 2004, 2005 Tristan Grimmer
  15833. * MIT license (see License.txt in http://www.upperbounds.net/download/ProggyClean.ttf.zip)
  15834. * Download and more information at http://upperbounds.net
  15835. *-----------------------------------------------------------------------------*/
  15836. #ifdef __clang__
  15837. #pragma clang diagnostic push
  15838. #pragma clang diagnostic ignored "-Woverlength-strings"
  15839. #elif defined(__GNUC__) || defined(__GNUG__)
  15840. #pragma GCC diagnostic push
  15841. #pragma GCC diagnostic ignored "-Woverlength-strings"
  15842. #endif
  15843. #ifdef NK_INCLUDE_DEFAULT_FONT
  15844. NK_GLOBAL const char nk_proggy_clean_ttf_compressed_data_base85[11980 + 1] =
  15845. "7])#######hV0qs'/###[),##/l:$#Q6>##5[n42>c-TH`->>#/e>11NNV=Bv(*:.F?uu#(gRU.o0XGH`$vhLG1hxt9?W`#,5LsCp#-i>.r$<$6pD>Lb';9Crc6tgXmKVeU2cD4Eo3R/"
  15846. "2*>]b(MC;$jPfY.;h^`IWM9<Lh2TlS+f-s$o6Q<BWH`YiU.xfLq$N;$0iR/GX:U(jcW2p/W*q?-qmnUCI;jHSAiFWM.R*kU@C=GH?a9wp8f$e.-4^Qg1)Q-GL(lf(r/7GrRgwV%MS=C#"
  15847. "`8ND>Qo#t'X#(v#Y9w0#1D$CIf;W'#pWUPXOuxXuU(H9M(1<q-UE31#^-V'8IRUo7Qf./L>=Ke$$'5F%)]0^#0X@U.a<r:QLtFsLcL6##lOj)#.Y5<-R&KgLwqJfLgN&;Q?gI^#DY2uL"
  15848. "i@^rMl9t=cWq6##weg>$FBjVQTSDgEKnIS7EM9>ZY9w0#L;>>#Mx&4Mvt//L[MkA#W@lK.N'[0#7RL_&#w+F%HtG9M#XL`N&.,GM4Pg;-<nLENhvx>-VsM.M0rJfLH2eTM`*oJMHRC`N"
  15849. "kfimM2J,W-jXS:)r0wK#@Fge$U>`w'N7G#$#fB#$E^$#:9:hk+eOe--6x)F7*E%?76%^GMHePW-Z5l'&GiF#$956:rS?dA#fiK:)Yr+`&#0j@'DbG&#^$PG.Ll+DNa<XCMKEV*N)LN/N"
  15850. "*b=%Q6pia-Xg8I$<MR&,VdJe$<(7G;Ckl'&hF;;$<_=X(b.RS%%)###MPBuuE1V:v&cX&#2m#(&cV]`k9OhLMbn%s$G2,B$BfD3X*sp5#l,$R#]x_X1xKX%b5U*[r5iMfUo9U`N99hG)"
  15851. "tm+/Us9pG)XPu`<0s-)WTt(gCRxIg(%6sfh=ktMKn3j)<6<b5Sk_/0(^]AaN#(p/L>&VZ>1i%h1S9u5o@YaaW$e+b<TWFn/Z:Oh(Cx2$lNEoN^e)#CFY@@I;BOQ*sRwZtZxRcU7uW6CX"
  15852. "ow0i(?$Q[cjOd[P4d)]>ROPOpxTO7Stwi1::iB1q)C_=dV26J;2,]7op$]uQr@_V7$q^%lQwtuHY]=DX,n3L#0PHDO4f9>dC@O>HBuKPpP*E,N+b3L#lpR/MrTEH.IAQk.a>D[.e;mc."
  15853. "x]Ip.PH^'/aqUO/$1WxLoW0[iLA<QT;5HKD+@qQ'NQ(3_PLhE48R.qAPSwQ0/WK?Z,[x?-J;jQTWA0X@KJ(_Y8N-:/M74:/-ZpKrUss?d#dZq]DAbkU*JqkL+nwX@@47`5>w=4h(9.`G"
  15854. "CRUxHPeR`5Mjol(dUWxZa(>STrPkrJiWx`5U7F#.g*jrohGg`cg:lSTvEY/EV_7H4Q9[Z%cnv;JQYZ5q.l7Zeas:HOIZOB?G<Nald$qs]@]L<J7bR*>gv:[7MI2k).'2($5FNP&EQ(,)"
  15855. "U]W]+fh18.vsai00);D3@4ku5P?DP8aJt+;qUM]=+b'8@;mViBKx0DE[-auGl8:PJ&Dj+M6OC]O^((##]`0i)drT;-7X`=-H3[igUnPG-NZlo.#k@h#=Ork$m>a>$-?Tm$UV(?#P6YY#"
  15856. "'/###xe7q.73rI3*pP/$1>s9)W,JrM7SN]'/4C#v$U`0#V.[0>xQsH$fEmPMgY2u7Kh(G%siIfLSoS+MK2eTM$=5,M8p`A.;_R%#u[K#$x4AG8.kK/HSB==-'Ie/QTtG?-.*^N-4B/ZM"
  15857. "_3YlQC7(p7q)&](`6_c)$/*JL(L-^(]$wIM`dPtOdGA,U3:w2M-0<q-]L_?^)1vw'.,MRsqVr.L;aN&#/EgJ)PBc[-f>+WomX2u7lqM2iEumMTcsF?-aT=Z-97UEnXglEn1K-bnEO`gu"
  15858. "Ft(c%=;Am_Qs@jLooI&NX;]0#j4#F14;gl8-GQpgwhrq8'=l_f-b49'UOqkLu7-##oDY2L(te+Mch&gLYtJ,MEtJfLh'x'M=$CS-ZZ%P]8bZ>#S?YY#%Q&q'3^Fw&?D)UDNrocM3A76/"
  15859. "/oL?#h7gl85[qW/NDOk%16ij;+:1a'iNIdb-ou8.P*w,v5#EI$TWS>Pot-R*H'-SEpA:g)f+O$%%`kA#G=8RMmG1&O`>to8bC]T&$,n.LoO>29sp3dt-52U%VM#q7'DHpg+#Z9%H[K<L"
  15860. "%a2E-grWVM3@2=-k22tL]4$##6We'8UJCKE[d_=%wI;'6X-GsLX4j^SgJ$##R*w,vP3wK#iiW&#*h^D&R?jp7+/u&#(AP##XU8c$fSYW-J95_-Dp[g9wcO&#M-h1OcJlc-*vpw0xUX&#"
  15861. "OQFKNX@QI'IoPp7nb,QU//MQ&ZDkKP)X<WSVL(68uVl&#c'[0#(s1X&xm$Y%B7*K:eDA323j998GXbA#pwMs-jgD$9QISB-A_(aN4xoFM^@C58D0+Q+q3n0#3U1InDjF682-SjMXJK)("
  15862. "h$hxua_K]ul92%'BOU&#BRRh-slg8KDlr:%L71Ka:.A;%YULjDPmL<LYs8i#XwJOYaKPKc1h:'9Ke,g)b),78=I39B;xiY$bgGw-&.Zi9InXDuYa%G*f2Bq7mn9^#p1vv%#(Wi-;/Z5h"
  15863. "o;#2:;%d&#x9v68C5g?ntX0X)pT`;%pB3q7mgGN)3%(P8nTd5L7GeA-GL@+%J3u2:(Yf>et`e;)f#Km8&+DC$I46>#Kr]]u-[=99tts1.qb#q72g1WJO81q+eN'03'eM>&1XxY-caEnO"
  15864. "j%2n8)),?ILR5^.Ibn<-X-Mq7[a82Lq:F&#ce+S9wsCK*x`569E8ew'He]h:sI[2LM$[guka3ZRd6:t%IG:;$%YiJ:Nq=?eAw;/:nnDq0(CYcMpG)qLN4$##&J<j$UpK<Q4a1]MupW^-"
  15865. "sj_$%[HK%'F####QRZJ::Y3EGl4'@%FkiAOg#p[##O`gukTfBHagL<LHw%q&OV0##F=6/:chIm0@eCP8X]:kFI%hl8hgO@RcBhS-@Qb$%+m=hPDLg*%K8ln(wcf3/'DW-$.lR?n[nCH-"
  15866. "eXOONTJlh:.RYF%3'p6sq:UIMA945&^HFS87@$EP2iG<-lCO$%c`uKGD3rC$x0BL8aFn--`ke%#HMP'vh1/R&O_J9'um,.<tx[@%wsJk&bUT2`0uMv7gg#qp/ij.L56'hl;.s5CUrxjO"
  15867. "M7-##.l+Au'A&O:-T72L]P`&=;ctp'XScX*rU.>-XTt,%OVU4)S1+R-#dg0/Nn?Ku1^0f$B*P:Rowwm-`0PKjYDDM'3]d39VZHEl4,.j']Pk-M.h^&:0FACm$maq-&sgw0t7/6(^xtk%"
  15868. "LuH88Fj-ekm>GA#_>568x6(OFRl-IZp`&b,_P'$M<Jnq79VsJW/mWS*PUiq76;]/NM_>hLbxfc$mj`,O;&%W2m`Zh:/)Uetw:aJ%]K9h:TcF]u_-Sj9,VK3M.*'&0D[Ca]J9gp8,kAW]"
  15869. "%(?A%R$f<->Zts'^kn=-^@c4%-pY6qI%J%1IGxfLU9CP8cbPlXv);C=b),<2mOvP8up,UVf3839acAWAW-W?#ao/^#%KYo8fRULNd2.>%m]UK:n%r$'sw]J;5pAoO_#2mO3n,'=H5(et"
  15870. "Hg*`+RLgv>=4U8guD$I%D:W>-r5V*%j*W:Kvej.Lp$<M-SGZ':+Q_k+uvOSLiEo(<aD/K<CCc`'Lx>'?;++O'>()jLR-^u68PHm8ZFWe+ej8h:9r6L*0//c&iH&R8pRbA#Kjm%upV1g:"
  15871. "a_#Ur7FuA#(tRh#.Y5K+@?3<-8m0$PEn;J:rh6?I6uG<-`wMU'ircp0LaE_OtlMb&1#6T.#FDKu#1Lw%u%+GM+X'e?YLfjM[VO0MbuFp7;>Q&#WIo)0@F%q7c#4XAXN-U&VB<HFF*qL("
  15872. "$/V,;(kXZejWO`<[5?\?ewY(*9=%wDc;,u<'9t3W-(H1th3+G]ucQ]kLs7df($/*JL]@*t7Bu_G3_7mp7<iaQjO@.kLg;x3B0lqp7Hf,^Ze7-##@/c58Mo(3;knp0%)A7?-W+eI'o8)b<"
  15873. "nKnw'Ho8C=Y>pqB>0ie&jhZ[?iLR@@_AvA-iQC(=ksRZRVp7`.=+NpBC%rh&3]R:8XDmE5^V8O(x<<aG/1N$#FX$0V5Y6x'aErI3I$7x%E`v<-BY,)%-?Psf*l?%C3.mM(=/M0:JxG'?"
  15874. "7WhH%o'a<-80g0NBxoO(GH<dM]n.+%q@jH?f.UsJ2Ggs&4<-e47&Kl+f//9@`b+?.TeN_&B8Ss?v;^Trk;f#YvJkl&w$]>-+k?'(<S:68tq*WoDfZu';mM?8X[ma8W%*`-=;D.(nc7/;"
  15875. ")g:T1=^J$&BRV(-lTmNB6xqB[@0*o.erM*<SWF]u2=st-*(6v>^](H.aREZSi,#1:[IXaZFOm<-ui#qUq2$##Ri;u75OK#(RtaW-K-F`S+cF]uN`-KMQ%rP/Xri.LRcB##=YL3BgM/3M"
  15876. "D?@f&1'BW-)Ju<L25gl8uhVm1hL$##*8###'A3/LkKW+(^rWX?5W_8g)a(m&K8P>#bmmWCMkk&#TR`C,5d>g)F;t,4:@_l8G/5h4vUd%&%950:VXD'QdWoY-F$BtUwmfe$YqL'8(PWX("
  15877. "P?^@Po3$##`MSs?DWBZ/S>+4%>fX,VWv/w'KD`LP5IbH;rTV>n3cEK8U#bX]l-/V+^lj3;vlMb&[5YQ8#pekX9JP3XUC72L,,?+Ni&co7ApnO*5NK,((W-i:$,kp'UDAO(G0Sq7MVjJs"
  15878. "bIu)'Z,*[>br5fX^:FPAWr-m2KgL<LUN098kTF&#lvo58=/vjDo;.;)Ka*hLR#/k=rKbxuV`>Q_nN6'8uTG&#1T5g)uLv:873UpTLgH+#FgpH'_o1780Ph8KmxQJ8#H72L4@768@Tm&Q"
  15879. "h4CB/5OvmA&,Q&QbUoi$a_%3M01H)4x7I^&KQVgtFnV+;[Pc>[m4k//,]1?#`VY[Jr*3&&slRfLiVZJ:]?=K3Sw=[$=uRB?3xk48@aeg<Z'<$#4H)6,>e0jT6'N#(q%.O=?2S]u*(m<-"
  15880. "V8J'(1)G][68hW$5'q[GC&5j`TE?m'esFGNRM)j,ffZ?-qx8;->g4t*:CIP/[Qap7/9'#(1sao7w-.qNUdkJ)tCF&#B^;xGvn2r9FEPFFFcL@.iFNkTve$m%#QvQS8U@)2Z+3K:AKM5i"
  15881. "sZ88+dKQ)W6>J%CL<KE>`.d*(B`-n8D9oK<Up]c$X$(,)M8Zt7/[rdkqTgl-0cuGMv'?>-XV1q['-5k'cAZ69e;D_?$ZPP&s^+7])$*$#@QYi9,5P&#9r+$%CE=68>K8r0=dSC%%(@p7"
  15882. ".m7jilQ02'0-VWAg<a/''3u.=4L$Y)6k/K:_[3=&jvL<L0C/2'v:^;-DIBW,B4E68:kZ;%?8(Q8BH=kO65BW?xSG&#@uU,DS*,?.+(o(#1vCS8#CHF>TlGW'b)Tq7VT9q^*^$$.:&N@@"
  15883. "$&)WHtPm*5_rO0&e%K&#-30j(E4#'Zb.o/(Tpm$>K'f@[PvFl,hfINTNU6u'0pao7%XUp9]5.>%h`8_=VYbxuel.NTSsJfLacFu3B'lQSu/m6-Oqem8T+oE--$0a/k]uj9EwsG>%veR*"
  15884. "hv^BFpQj:K'#SJ,sB-'#](j.Lg92rTw-*n%@/;39rrJF,l#qV%OrtBeC6/,;qB3ebNW[?,Hqj2L.1NP&GjUR=1D8QaS3Up&@*9wP?+lo7b?@%'k4`p0Z$22%K3+iCZj?XJN4Nm&+YF]u"
  15885. "@-W$U%VEQ/,,>>#)D<h#`)h0:<Q6909ua+&VU%n2:cG3FJ-%@Bj-DgLr`Hw&HAKjKjseK</xKT*)B,N9X3]krc12t'pgTV(Lv-tL[xg_%=M_q7a^x?7Ubd>#%8cY#YZ?=,`Wdxu/ae&#"
  15886. "w6)R89tI#6@s'(6Bf7a&?S=^ZI_kS&ai`&=tE72L_D,;^R)7[$s<Eh#c&)q.MXI%#v9ROa5FZO%sF7q7Nwb&#ptUJ:aqJe$Sl68%.D###EC><?-aF&#RNQv>o8lKN%5/$(vdfq7+ebA#"
  15887. "u1p]ovUKW&Y%q]'>$1@-[xfn$7ZTp7mM,G,Ko7a&Gu%G[RMxJs[0MM%wci.LFDK)(<c`Q8N)jEIF*+?P2a8g%)$q]o2aH8C&<SibC/q,(e:v;-b#6[$NtDZ84Je2KNvB#$P5?tQ3nt(0"
  15888. "d=j.LQf./Ll33+(;q3L-w=8dX$#WF&uIJ@-bfI>%:_i2B5CsR8&9Z&#=mPEnm0f`<&c)QL5uJ#%u%lJj+D-r;BoF&#4DoS97h5g)E#o:&S4weDF,9^Hoe`h*L+_a*NrLW-1pG_&2UdB8"
  15889. "6e%B/:=>)N4xeW.*wft-;$'58-ESqr<b?UI(_%@[P46>#U`'6AQ]m&6/`Z>#S?YY#Vc;r7U2&326d=w&H####?TZ`*4?&.MK?LP8Vxg>$[QXc%QJv92.(Db*B)gb*BM9dM*hJMAo*c&#"
  15890. "b0v=Pjer]$gG&JXDf->'StvU7505l9$AFvgYRI^&<^b68?j#q9QX4SM'RO#&sL1IM.rJfLUAj221]d##DW=m83u5;'bYx,*Sl0hL(W;;$doB&O/TQ:(Z^xBdLjL<Lni;''X.`$#8+1GD"
  15891. ":k$YUWsbn8ogh6rxZ2Z9]%nd+>V#*8U_72Lh+2Q8Cj0i:6hp&$C/:p(HK>T8Y[gHQ4`4)'$Ab(Nof%V'8hL&#<NEdtg(n'=S1A(Q1/I&4([%dM`,Iu'1:_hL>SfD07&6D<fp8dHM7/g+"
  15892. "tlPN9J*rKaPct&?'uBCem^jn%9_K)<,C5K3s=5g&GmJb*[SYq7K;TRLGCsM-$$;S%:Y@r7AK0pprpL<Lrh,q7e/%KWK:50I^+m'vi`3?%Zp+<-d+$L-Sv:@.o19n$s0&39;kn;S%BSq*"
  15893. "$3WoJSCLweV[aZ'MQIjO<7;X-X;&+dMLvu#^UsGEC9WEc[X(wI7#2.(F0jV*eZf<-Qv3J-c+J5AlrB#$p(H68LvEA'q3n0#m,[`*8Ft)FcYgEud]CWfm68,(aLA$@EFTgLXoBq/UPlp7"
  15894. ":d[/;r_ix=:TF`S5H-b<LI&HY(K=h#)]Lk$K14lVfm:x$H<3^Ql<M`$OhapBnkup'D#L$Pb_`N*g]2e;X/Dtg,bsj&K#2[-:iYr'_wgH)NUIR8a1n#S?Yej'h8^58UbZd+^FKD*T@;6A"
  15895. "7aQC[K8d-(v6GI$x:T<&'Gp5Uf>@M.*J:;$-rv29'M]8qMv-tLp,'886iaC=Hb*YJoKJ,(j%K=H`K.v9HggqBIiZu'QvBT.#=)0ukruV&.)3=(^1`o*Pj4<-<aN((^7('#Z0wK#5GX@7"
  15896. "u][`*S^43933A4rl][`*O4CgLEl]v$1Q3AeF37dbXk,.)vj#x'd`;qgbQR%FW,2(?LO=s%Sc68%NP'##Aotl8x=BE#j1UD([3$M(]UI2LX3RpKN@;/#f'f/&_mt&F)XdF<9t4)Qa.*kT"
  15897. "LwQ'(TTB9.xH'>#MJ+gLq9-##@HuZPN0]u:h7.T..G:;$/Usj(T7`Q8tT72LnYl<-qx8;-HV7Q-&Xdx%1a,hC=0u+HlsV>nuIQL-5<N?)NBS)QN*_I,?&)2'IM%L3I)X((e/dl2&8'<M"
  15898. ":^#M*Q+[T.Xri.LYS3v%fF`68h;b-X[/En'CR.q7E)p'/kle2HM,u;^%OKC-N+Ll%F9CF<Nf'^#t2L,;27W:0O@6##U6W7:$rJfLWHj$#)woqBefIZ.PK<b*t7ed;p*_m;4ExK#h@&]>"
  15899. "_>@kXQtMacfD.m-VAb8;IReM3$wf0''hra*so568'Ip&vRs849'MRYSp%:t:h5qSgwpEr$B>Q,;s(C#$)`svQuF$##-D,##,g68@2[T;.XSdN9Qe)rpt._K-#5wF)sP'##p#C0c%-Gb%"
  15900. "hd+<-j'Ai*x&&HMkT]C'OSl##5RG[JXaHN;d'uA#x._U;.`PU@(Z3dt4r152@:v,'R.Sj'w#0<-;kPI)FfJ&#AYJ&#//)>-k=m=*XnK$>=)72L]0I%>.G690a:$##<,);?;72#?x9+d;"
  15901. "^V'9;jY@;)br#q^YQpx:X#Te$Z^'=-=bGhLf:D6&bNwZ9-ZD#n^9HhLMr5G;']d&6'wYmTFmL<LD)F^%[tC'8;+9E#C$g%#5Y>q9wI>P(9mI[>kC-ekLC/R&CH+s'B;K-M6$EB%is00:"
  15902. "+A4[7xks.LrNk0&E)wILYF@2L'0Nb$+pv<(2.768/FrY&h$^3i&@+G%JT'<-,v`3;_)I9M^AE]CN?Cl2AZg+%4iTpT3<n-&%H%b<FDj2M<hH=&Eh<2Len$b*aTX=-8QxN)k11IM1c^j%"
  15903. "9s<L<NFSo)B?+<-(GxsF,^-Eh@$4dXhN$+#rxK8'je'D7k`e;)2pYwPA'_p9&@^18ml1^[@g4t*[JOa*[=Qp7(qJ_oOL^('7fB&Hq-:sf,sNj8xq^>$U4O]GKx'm9)b@p7YsvK3w^YR-"
  15904. "CdQ*:Ir<($u&)#(&?L9Rg3H)4fiEp^iI9O8KnTj,]H?D*r7'M;PwZ9K0E^k&-cpI;.p/6_vwoFMV<->#%Xi.LxVnrU(4&8/P+:hLSKj$#U%]49t'I:rgMi'FL@a:0Y-uA[39',(vbma*"
  15905. "hU%<-SRF`Tt:542R_VV$p@[p8DV[A,?1839FWdF<TddF<9Ah-6&9tWoDlh]&1SpGMq>Ti1O*H&#(AL8[_P%.M>v^-))qOT*F5Cq0`Ye%+$B6i:7@0IX<N+T+0MlMBPQ*Vj>SsD<U4JHY"
  15906. "8kD2)2fU/M#$e.)T4,_=8hLim[&);?UkK'-x?'(:siIfL<$pFM`i<?%W(mGDHM%>iWP,##P`%/L<eXi:@Z9C.7o=@(pXdAO/NLQ8lPl+HPOQa8wD8=^GlPa8TKI1CjhsCTSLJM'/Wl>-"
  15907. "S(qw%sf/@%#B6;/U7K]uZbi^Oc^2n<bhPmUkMw>%t<)'mEVE''n`WnJra$^TKvX5B>;_aSEK',(hwa0:i4G?.Bci.(X[?b*($,=-n<.Q%`(X=?+@Am*Js0&=3bh8K]mL<LoNs'6,'85`"
  15908. "0?t/'_U59@]ddF<#LdF<eWdF<OuN/45rY<-L@&#+fm>69=Lb,OcZV/);TTm8VI;?%OtJ<(b4mq7M6:u?KRdF<gR@2L=FNU-<b[(9c/ML3m;Z[$oF3g)GAWqpARc=<ROu7cL5l;-[A]%/"
  15909. "+fsd;l#SafT/f*W]0=O'$(Tb<[)*@e775R-:Yob%g*>l*:xP?Yb.5)%w_I?7uk5JC+FS(m#i'k.'a0i)9<7b'fs'59hq$*5Uhv##pi^8+hIEBF`nvo`;'l0.^S1<-wUK2/Coh58KKhLj"
  15910. "M=SO*rfO`+qC`W-On.=AJ56>>i2@2LH6A:&5q`?9I3@@'04&p2/LVa*T-4<-i3;M9UvZd+N7>b*eIwg:CC)c<>nO&#<IGe;__.thjZl<%w(Wk2xmp4Q@I#I9,DF]u7-P=.-_:YJ]aS@V"
  15911. "?6*C()dOp7:WL,b&3Rg/.cmM9&r^>$(>.Z-I&J(Q0Hd5Q%7Co-b`-c<N(6r@ip+AurK<m86QIth*#v;-OBqi+L7wDE-Ir8K['m+DDSLwK&/.?-V%U_%3:qKNu$_b*B-kp7NaD'QdWQPK"
  15912. "Yq[@>P)hI;*_F]u`Rb[.j8_Q/<&>uu+VsH$sM9TA%?)(vmJ80),P7E>)tjD%2L=-t#fK[%`v=Q8<FfNkgg^oIbah*#8/Qt$F&:K*-(N/'+1vMB,u()-a.VUU*#[e%gAAO(S>WlA2);Sa"
  15913. ">gXm8YB`1d@K#n]76-a$U,mF<fX]idqd)<3,]J7JmW4`6]uks=4-72L(jEk+:bJ0M^q-8Dm_Z?0olP1C9Sa&H[d&c$ooQUj]Exd*3ZM@-WGW2%s',B-_M%>%Ul:#/'xoFM9QX-$.QN'>"
  15914. "[%$Z$uF6pA6Ki2O5:8w*vP1<-1`[G,)-m#>0`P&#eb#.3i)rtB61(o'$?X3B</R90;eZ]%Ncq;-Tl]#F>2Qft^ae_5tKL9MUe9b*sLEQ95C&`=G?@Mj=wh*'3E>=-<)Gt*Iw)'QG:`@I"
  15915. "wOf7&]1i'S01B+Ev/Nac#9S;=;YQpg_6U`*kVY39xK,[/6Aj7:'1Bm-_1EYfa1+o&o4hp7KN_Q(OlIo@S%;jVdn0'1<Vc52=u`3^o-n1'g4v58Hj&6_t7$##?M)c<$bgQ_'SY((-xkA#"
  15916. "Y(,p'H9rIVY-b,'%bCPF7.J<Up^,(dU1VY*5#WkTU>h19w,WQhLI)3S#f$2(eb,jr*b;3Vw]*7NH%$c4Vs,eD9>XW8?N]o+(*pgC%/72LV-u<Hp,3@e^9UB1J+ak9-TN/mhKPg+AJYd$"
  15917. "MlvAF_jCK*.O-^(63adMT->W%iewS8W6m2rtCpo'RS1R84=@paTKt)>=%&1[)*vp'u+x,VrwN;&]kuO9JDbg=pO$J*.jVe;u'm0dr9l,<*wMK*Oe=g8lV_KEBFkO'oU]^=[-792#ok,)"
  15918. "i]lR8qQ2oA8wcRCZ^7w/Njh;?.stX?Q1>S1q4Bn$)K1<-rGdO'$Wr.Lc.CG)$/*JL4tNR/,SVO3,aUw'DJN:)Ss;wGn9A32ijw%FL+Z0Fn.U9;reSq)bmI32U==5ALuG&#Vf1398/pVo"
  15919. "1*c-(aY168o<`JsSbk-,1N;$>0:OUas(3:8Z972LSfF8eb=c-;>SPw7.6hn3m`9^Xkn(r.qS[0;T%&Qc=+STRxX'q1BNk3&*eu2;&8q$&x>Q#Q7^Tf+6<(d%ZVmj2bDi%.3L2n+4W'$P"
  15920. "iDDG)g,r%+?,$@?uou5tSe2aN_AQU*<h`e-GI7)?OK2A.d7_c)?wQ5AS@DL3r#7fSkgl6-++D:'A,uq7SvlB$pcpH'q3n0#_%dY#xCpr-l<F0NR@-##FEV6NTF6##$l84N1w?AO>'IAO"
  15921. "URQ##V^Fv-XFbGM7Fl(N<3DhLGF%q.1rC$#:T__&Pi68%0xi_&[qFJ(77j_&JWoF.V735&T,[R*:xFR*K5>>#`bW-?4Ne_&6Ne_&6Ne_&n`kr-#GJcM6X;uM6X;uM(.a..^2TkL%oR(#"
  15922. ";u.T%fAr%4tJ8&><1=GHZ_+m9/#H1F^R#SC#*N=BA9(D?v[UiFY>>^8p,KKF.W]L29uLkLlu/+4T<XoIB&hx=T1PcDaB&;HH+-AFr?(m9HZV)FKS8JCw;SD=6[^/DZUL`EUDf]GGlG&>"
  15923. "w$)F./^n3+rlo+DB;5sIYGNk+i1t-69Jg--0pao7Sm#K)pdHW&;LuDNH@H>#/X-TI(;P>#,Gc>#0Su>#4`1?#8lC?#<xU?#@.i?#D:%@#HF7@#LRI@#P_[@#Tkn@#Xw*A#]-=A#a9OA#"
  15924. "d<F&#*;G##.GY##2Sl##6`($#:l:$#>xL$#B.`$#F:r$#JF.%#NR@%#R_R%#Vke%#Zww%#_-4&#3^Rh%Sflr-k'MS.o?.5/sWel/wpEM0%3'/1)K^f1-d>G21&v(35>V`39V7A4=onx4"
  15925. "A1OY5EI0;6Ibgr6M$HS7Q<)58C5w,;WoA*#[%T*#`1g*#d=#+#hI5+#lUG+#pbY+#tnl+#x$),#&1;,#*=M,#.I`,#2Ur,#6b.-#;w[H#iQtA#m^0B#qjBB#uvTB##-hB#'9$C#+E6C#"
  15926. "/QHC#3^ZC#7jmC#;v)D#?,<D#C8ND#GDaD#KPsD#O]/E#g1A5#KA*1#gC17#MGd;#8(02#L-d3#rWM4#Hga1#,<w0#T.j<#O#'2#CYN1#qa^:#_4m3#o@/=#eG8=#t8J5#`+78#4uI-#"
  15927. "m3B2#SB[8#Q0@8#i[*9#iOn8#1Nm;#^sN9#qh<9#:=x-#P;K2#$%X9#bC+.#Rg;<#mN=.#MTF.#RZO.#2?)4#Y#(/#[)1/#b;L/#dAU/#0Sv;#lY$0#n`-0#sf60#(F24#wrH0#%/e0#"
  15928. "TmD<#%JSMFove:CTBEXI:<eh2g)B,3h2^G3i;#d3jD>)4kMYD4lVu`4m`:&5niUA5@(A5BA1]PBB:xlBCC=2CDLXMCEUtiCf&0g2'tN?PGT4CPGT4CPGT4CPGT4CPGT4CPGT4CPGT4CP"
  15929. "GT4CPGT4CPGT4CPGT4CPGT4CPGT4CP-qekC`.9kEg^+F$kwViFJTB&5KTB&5KTB&5KTB&5KTB&5KTB&5KTB&5KTB&5KTB&5KTB&5KTB&5KTB&5KTB&5KTB&5KTB&5o,^<-28ZI'O?;xp"
  15930. "O?;xpO?;xpO?;xpO?;xpO?;xpO?;xpO?;xpO?;xpO?;xpO?;xpO?;xpO?;xpO?;xp;7q-#lLYI:xvD=#";
  15931. #endif /* NK_INCLUDE_DEFAULT_FONT */
  15932. #define NK_CURSOR_DATA_W 90
  15933. #define NK_CURSOR_DATA_H 27
  15934. NK_GLOBAL const char nk_custom_cursor_data[NK_CURSOR_DATA_W * NK_CURSOR_DATA_H + 1] =
  15935. {
  15936. "..- -XXXXXXX- X - X -XXXXXXX - XXXXXXX"
  15937. "..- -X.....X- X.X - X.X -X.....X - X.....X"
  15938. "--- -XXX.XXX- X...X - X...X -X....X - X....X"
  15939. "X - X.X - X.....X - X.....X -X...X - X...X"
  15940. "XX - X.X -X.......X- X.......X -X..X.X - X.X..X"
  15941. "X.X - X.X -XXXX.XXXX- XXXX.XXXX -X.X X.X - X.X X.X"
  15942. "X..X - X.X - X.X - X.X -XX X.X - X.X XX"
  15943. "X...X - X.X - X.X - XX X.X XX - X.X - X.X "
  15944. "X....X - X.X - X.X - X.X X.X X.X - X.X - X.X "
  15945. "X.....X - X.X - X.X - X..X X.X X..X - X.X - X.X "
  15946. "X......X - X.X - X.X - X...XXXXXX.XXXXXX...X - X.X XX-XX X.X "
  15947. "X.......X - X.X - X.X -X.....................X- X.X X.X-X.X X.X "
  15948. "X........X - X.X - X.X - X...XXXXXX.XXXXXX...X - X.X..X-X..X.X "
  15949. "X.........X -XXX.XXX- X.X - X..X X.X X..X - X...X-X...X "
  15950. "X..........X-X.....X- X.X - X.X X.X X.X - X....X-X....X "
  15951. "X......XXXXX-XXXXXXX- X.X - XX X.X XX - X.....X-X.....X "
  15952. "X...X..X --------- X.X - X.X - XXXXXXX-XXXXXXX "
  15953. "X..X X..X - -XXXX.XXXX- XXXX.XXXX ------------------------------------"
  15954. "X.X X..X - -X.......X- X.......X - XX XX - "
  15955. "XX X..X - - X.....X - X.....X - X.X X.X - "
  15956. " X..X - X...X - X...X - X..X X..X - "
  15957. " XX - X.X - X.X - X...XXXXXXXXXXXXX...X - "
  15958. "------------ - X - X -X.....................X- "
  15959. " ----------------------------------- X...XXXXXXXXXXXXX...X - "
  15960. " - X..X X..X - "
  15961. " - X.X X.X - "
  15962. " - XX XX - "
  15963. };
  15964. #ifdef __clang__
  15965. #pragma clang diagnostic pop
  15966. #elif defined(__GNUC__) || defined(__GNUG__)
  15967. #pragma GCC diagnostic pop
  15968. #endif
  15969. NK_GLOBAL unsigned char* nk__barrier;
  15970. NK_GLOBAL unsigned char* nk__barrier2;
  15971. NK_GLOBAL unsigned char* nk__barrier3;
  15972. NK_GLOBAL unsigned char* nk__barrier4;
  15973. NK_GLOBAL unsigned char* nk__dout;
  15974. NK_INTERN unsigned int
  15975. nk_decompress_length(unsigned char* input)
  15976. {
  15977. return (unsigned int)((input[8] << 24) + (input[9] << 16) + (input[10] << 8) + input[11]);
  15978. }
  15979. NK_INTERN void
  15980. nk__match(unsigned char* data, unsigned int length)
  15981. {
  15982. /* INVERSE of memmove... write each byte before copying the next...*/
  15983. NK_ASSERT(nk__dout + length <= nk__barrier);
  15984. if (nk__dout + length > nk__barrier) { nk__dout += length; return; }
  15985. if (data < nk__barrier4) { nk__dout = nk__barrier + 1; return; }
  15986. while (length--) *nk__dout++ = *data++;
  15987. }
  15988. NK_INTERN void
  15989. nk__lit(unsigned char* data, unsigned int length)
  15990. {
  15991. NK_ASSERT(nk__dout + length <= nk__barrier);
  15992. if (nk__dout + length > nk__barrier) { nk__dout += length; return; }
  15993. if (data < nk__barrier2) { nk__dout = nk__barrier + 1; return; }
  15994. NK_MEMCPY(nk__dout, data, length);
  15995. nk__dout += length;
  15996. }
  15997. NK_INTERN unsigned char*
  15998. nk_decompress_token(unsigned char* i)
  15999. {
  16000. #define nk__in2(x) ((i[x] << 8) + i[(x)+1])
  16001. #define nk__in3(x) ((i[x] << 16) + nk__in2((x)+1))
  16002. #define nk__in4(x) ((i[x] << 24) + nk__in3((x)+1))
  16003. if (*i >= 0x20) { /* use fewer if's for cases that expand small */
  16004. if (*i >= 0x80) nk__match(nk__dout - i[1] - 1, (unsigned int)i[0] - 0x80 + 1), i += 2;
  16005. else if (*i >= 0x40) nk__match(nk__dout - (nk__in2(0) - 0x4000 + 1), (unsigned int)i[2] + 1), i += 3;
  16006. else /* *i >= 0x20 */ nk__lit(i + 1, (unsigned int)i[0] - 0x20 + 1), i += 1 + (i[0] - 0x20 + 1);
  16007. }
  16008. else { /* more ifs for cases that expand large, since overhead is amortized */
  16009. if (*i >= 0x18) nk__match(nk__dout - (unsigned int)(nk__in3(0) - 0x180000 + 1), (unsigned int)i[3] + 1), i += 4;
  16010. else if (*i >= 0x10) nk__match(nk__dout - (unsigned int)(nk__in3(0) - 0x100000 + 1), (unsigned int)nk__in2(3) + 1), i += 5;
  16011. else if (*i >= 0x08) nk__lit(i + 2, (unsigned int)nk__in2(0) - 0x0800 + 1), i += 2 + (nk__in2(0) - 0x0800 + 1);
  16012. else if (*i == 0x07) nk__lit(i + 3, (unsigned int)nk__in2(1) + 1), i += 3 + (nk__in2(1) + 1);
  16013. else if (*i == 0x06) nk__match(nk__dout - (unsigned int)(nk__in3(1) + 1), i[4] + 1u), i += 5;
  16014. else if (*i == 0x04) nk__match(nk__dout - (unsigned int)(nk__in3(1) + 1), (unsigned int)nk__in2(4) + 1u), i += 6;
  16015. }
  16016. return i;
  16017. }
  16018. NK_INTERN unsigned int
  16019. nk_adler32(unsigned int adler32, unsigned char* buffer, unsigned int buflen)
  16020. {
  16021. const unsigned long ADLER_MOD = 65521;
  16022. unsigned long s1 = adler32 & 0xffff, s2 = adler32 >> 16;
  16023. unsigned long blocklen, i;
  16024. blocklen = buflen % 5552;
  16025. while (buflen) {
  16026. for (i = 0; i + 7 < blocklen; i += 8) {
  16027. s1 += buffer[0]; s2 += s1;
  16028. s1 += buffer[1]; s2 += s1;
  16029. s1 += buffer[2]; s2 += s1;
  16030. s1 += buffer[3]; s2 += s1;
  16031. s1 += buffer[4]; s2 += s1;
  16032. s1 += buffer[5]; s2 += s1;
  16033. s1 += buffer[6]; s2 += s1;
  16034. s1 += buffer[7]; s2 += s1;
  16035. buffer += 8;
  16036. }
  16037. for (; i < blocklen; ++i) {
  16038. s1 += *buffer++; s2 += s1;
  16039. }
  16040. s1 %= ADLER_MOD; s2 %= ADLER_MOD;
  16041. buflen -= (unsigned int)blocklen;
  16042. blocklen = 5552;
  16043. }
  16044. return (unsigned int)(s2 << 16) + (unsigned int)s1;
  16045. }
  16046. NK_INTERN unsigned int
  16047. nk_decompress(unsigned char* output, unsigned char* i, unsigned int length)
  16048. {
  16049. unsigned int olen;
  16050. if (nk__in4(0) != 0x57bC0000) return 0;
  16051. if (nk__in4(4) != 0) return 0; /* error! stream is > 4GB */
  16052. olen = nk_decompress_length(i);
  16053. nk__barrier2 = i;
  16054. nk__barrier3 = i + length;
  16055. nk__barrier = output + olen;
  16056. nk__barrier4 = output;
  16057. i += 16;
  16058. nk__dout = output;
  16059. for (;;) {
  16060. unsigned char* old_i = i;
  16061. i = nk_decompress_token(i);
  16062. if (i == old_i) {
  16063. if (*i == 0x05 && i[1] == 0xfa) {
  16064. NK_ASSERT(nk__dout == output + olen);
  16065. if (nk__dout != output + olen) return 0;
  16066. if (nk_adler32(1, output, olen) != (unsigned int)nk__in4(2))
  16067. return 0;
  16068. return olen;
  16069. }
  16070. else {
  16071. NK_ASSERT(0); /* NOTREACHED */
  16072. return 0;
  16073. }
  16074. }
  16075. NK_ASSERT(nk__dout <= output + olen);
  16076. if (nk__dout > output + olen)
  16077. return 0;
  16078. }
  16079. }
  16080. NK_INTERN unsigned int
  16081. nk_decode_85_byte(char c)
  16082. {
  16083. return (unsigned int)((c >= '\\') ? c - 36 : c - 35);
  16084. }
  16085. NK_INTERN void
  16086. nk_decode_85(unsigned char* dst, const unsigned char* src)
  16087. {
  16088. while (*src)
  16089. {
  16090. unsigned int tmp =
  16091. nk_decode_85_byte((char)src[0]) +
  16092. 85 * (nk_decode_85_byte((char)src[1]) +
  16093. 85 * (nk_decode_85_byte((char)src[2]) +
  16094. 85 * (nk_decode_85_byte((char)src[3]) +
  16095. 85 * nk_decode_85_byte((char)src[4]))));
  16096. /* we can't assume little-endianess. */
  16097. dst[0] = (unsigned char)((tmp >> 0) & 0xFF);
  16098. dst[1] = (unsigned char)((tmp >> 8) & 0xFF);
  16099. dst[2] = (unsigned char)((tmp >> 16) & 0xFF);
  16100. dst[3] = (unsigned char)((tmp >> 24) & 0xFF);
  16101. src += 5;
  16102. dst += 4;
  16103. }
  16104. }
  16105. /* -------------------------------------------------------------
  16106. *
  16107. * FONT ATLAS
  16108. *
  16109. * --------------------------------------------------------------*/
  16110. NK_API struct nk_font_config
  16111. nk_font_config(float pixel_height)
  16112. {
  16113. struct nk_font_config cfg;
  16114. nk_zero_struct(cfg);
  16115. cfg.ttf_blob = 0;
  16116. cfg.ttf_size = 0;
  16117. cfg.ttf_data_owned_by_atlas = 0;
  16118. cfg.size = pixel_height;
  16119. cfg.oversample_h = 3;
  16120. cfg.oversample_v = 1;
  16121. cfg.pixel_snap = 0;
  16122. cfg.coord_type = NK_COORD_UV;
  16123. cfg.spacing = nk_vec2(0, 0);
  16124. cfg.range = nk_font_default_glyph_ranges();
  16125. cfg.merge_mode = 0;
  16126. cfg.fallback_glyph = '?';
  16127. cfg.font = 0;
  16128. cfg.n = 0;
  16129. return cfg;
  16130. }
  16131. #ifdef NK_INCLUDE_DEFAULT_ALLOCATOR
  16132. NK_API void
  16133. nk_font_atlas_init_default(struct nk_font_atlas* atlas)
  16134. {
  16135. NK_ASSERT(atlas);
  16136. if (!atlas) return;
  16137. nk_zero_struct(*atlas);
  16138. atlas->temporary.userdata.ptr = 0;
  16139. atlas->temporary.alloc = nk_malloc;
  16140. atlas->temporary.free = nk_mfree;
  16141. atlas->permanent.userdata.ptr = 0;
  16142. atlas->permanent.alloc = nk_malloc;
  16143. atlas->permanent.free = nk_mfree;
  16144. }
  16145. #endif
  16146. NK_API void
  16147. nk_font_atlas_init(struct nk_font_atlas* atlas, struct nk_allocator* alloc)
  16148. {
  16149. NK_ASSERT(atlas);
  16150. NK_ASSERT(alloc);
  16151. if (!atlas || !alloc) return;
  16152. nk_zero_struct(*atlas);
  16153. atlas->permanent = *alloc;
  16154. atlas->temporary = *alloc;
  16155. }
  16156. NK_API void
  16157. nk_font_atlas_init_custom(struct nk_font_atlas* atlas,
  16158. struct nk_allocator* permanent, struct nk_allocator* temporary)
  16159. {
  16160. NK_ASSERT(atlas);
  16161. NK_ASSERT(permanent);
  16162. NK_ASSERT(temporary);
  16163. if (!atlas || !permanent || !temporary) return;
  16164. nk_zero_struct(*atlas);
  16165. atlas->permanent = *permanent;
  16166. atlas->temporary = *temporary;
  16167. }
  16168. NK_API void
  16169. nk_font_atlas_begin(struct nk_font_atlas* atlas)
  16170. {
  16171. NK_ASSERT(atlas);
  16172. NK_ASSERT(atlas->temporary.alloc && atlas->temporary.free);
  16173. NK_ASSERT(atlas->permanent.alloc && atlas->permanent.free);
  16174. if (!atlas || !atlas->permanent.alloc || !atlas->permanent.free ||
  16175. !atlas->temporary.alloc || !atlas->temporary.free) return;
  16176. if (atlas->glyphs) {
  16177. atlas->permanent.free(atlas->permanent.userdata, atlas->glyphs);
  16178. atlas->glyphs = 0;
  16179. }
  16180. if (atlas->pixel) {
  16181. atlas->permanent.free(atlas->permanent.userdata, atlas->pixel);
  16182. atlas->pixel = 0;
  16183. }
  16184. }
  16185. NK_API struct nk_font*
  16186. nk_font_atlas_add(struct nk_font_atlas* atlas, const struct nk_font_config* config)
  16187. {
  16188. struct nk_font* font = 0;
  16189. struct nk_font_config* cfg;
  16190. NK_ASSERT(atlas);
  16191. NK_ASSERT(atlas->permanent.alloc);
  16192. NK_ASSERT(atlas->permanent.free);
  16193. NK_ASSERT(atlas->temporary.alloc);
  16194. NK_ASSERT(atlas->temporary.free);
  16195. NK_ASSERT(config);
  16196. NK_ASSERT(config->ttf_blob);
  16197. NK_ASSERT(config->ttf_size);
  16198. NK_ASSERT(config->size > 0.0f);
  16199. if (!atlas || !config || !config->ttf_blob || !config->ttf_size || config->size <= 0.0f ||
  16200. !atlas->permanent.alloc || !atlas->permanent.free ||
  16201. !atlas->temporary.alloc || !atlas->temporary.free)
  16202. return 0;
  16203. /* allocate font config */
  16204. cfg = (struct nk_font_config*)
  16205. atlas->permanent.alloc(atlas->permanent.userdata, 0, sizeof(struct nk_font_config));
  16206. NK_MEMCPY(cfg, config, sizeof(*config));
  16207. cfg->n = cfg;
  16208. cfg->p = cfg;
  16209. if (!config->merge_mode) {
  16210. /* insert font config into list */
  16211. if (!atlas->config) {
  16212. atlas->config = cfg;
  16213. cfg->next = 0;
  16214. }
  16215. else {
  16216. struct nk_font_config* i = atlas->config;
  16217. while (i->next) i = i->next;
  16218. i->next = cfg;
  16219. cfg->next = 0;
  16220. }
  16221. /* allocate new font */
  16222. font = (struct nk_font*)
  16223. atlas->permanent.alloc(atlas->permanent.userdata, 0, sizeof(struct nk_font));
  16224. NK_ASSERT(font);
  16225. nk_zero(font, sizeof(*font));
  16226. if (!font) return 0;
  16227. font->config = cfg;
  16228. /* insert font into list */
  16229. if (!atlas->fonts) {
  16230. atlas->fonts = font;
  16231. font->next = 0;
  16232. }
  16233. else {
  16234. struct nk_font* i = atlas->fonts;
  16235. while (i->next) i = i->next;
  16236. i->next = font;
  16237. font->next = 0;
  16238. }
  16239. cfg->font = &font->info;
  16240. }
  16241. else {
  16242. /* extend previously added font */
  16243. struct nk_font* f = 0;
  16244. struct nk_font_config* c = 0;
  16245. NK_ASSERT(atlas->font_num);
  16246. f = atlas->fonts;
  16247. c = f->config;
  16248. cfg->font = &f->info;
  16249. cfg->n = c;
  16250. cfg->p = c->p;
  16251. c->p->n = cfg;
  16252. c->p = cfg;
  16253. }
  16254. /* create own copy of .TTF font blob */
  16255. if (!config->ttf_data_owned_by_atlas) {
  16256. cfg->ttf_blob = atlas->permanent.alloc(atlas->permanent.userdata, 0, cfg->ttf_size);
  16257. NK_ASSERT(cfg->ttf_blob);
  16258. if (!cfg->ttf_blob) {
  16259. atlas->font_num++;
  16260. return 0;
  16261. }
  16262. NK_MEMCPY(cfg->ttf_blob, config->ttf_blob, cfg->ttf_size);
  16263. cfg->ttf_data_owned_by_atlas = 1;
  16264. }
  16265. atlas->font_num++;
  16266. return font;
  16267. }
  16268. NK_API struct nk_font*
  16269. nk_font_atlas_add_from_memory(struct nk_font_atlas* atlas, void* memory,
  16270. nk_size size, float height, const struct nk_font_config* config)
  16271. {
  16272. struct nk_font_config cfg;
  16273. NK_ASSERT(memory);
  16274. NK_ASSERT(size);
  16275. NK_ASSERT(atlas);
  16276. NK_ASSERT(atlas->temporary.alloc);
  16277. NK_ASSERT(atlas->temporary.free);
  16278. NK_ASSERT(atlas->permanent.alloc);
  16279. NK_ASSERT(atlas->permanent.free);
  16280. if (!atlas || !atlas->temporary.alloc || !atlas->temporary.free || !memory || !size ||
  16281. !atlas->permanent.alloc || !atlas->permanent.free)
  16282. return 0;
  16283. cfg = (config) ? *config : nk_font_config(height);
  16284. cfg.ttf_blob = memory;
  16285. cfg.ttf_size = size;
  16286. cfg.size = height;
  16287. cfg.ttf_data_owned_by_atlas = 0;
  16288. return nk_font_atlas_add(atlas, &cfg);
  16289. }
  16290. #ifdef NK_INCLUDE_STANDARD_IO
  16291. NK_API struct nk_font*
  16292. nk_font_atlas_add_from_file(struct nk_font_atlas* atlas, const char* file_path,
  16293. float height, const struct nk_font_config* config)
  16294. {
  16295. nk_size size;
  16296. char* memory;
  16297. struct nk_font_config cfg;
  16298. NK_ASSERT(atlas);
  16299. NK_ASSERT(atlas->temporary.alloc);
  16300. NK_ASSERT(atlas->temporary.free);
  16301. NK_ASSERT(atlas->permanent.alloc);
  16302. NK_ASSERT(atlas->permanent.free);
  16303. if (!atlas || !file_path) return 0;
  16304. memory = nk_file_load(file_path, &size, &atlas->permanent);
  16305. if (!memory) return 0;
  16306. cfg = (config) ? *config : nk_font_config(height);
  16307. cfg.ttf_blob = memory;
  16308. cfg.ttf_size = size;
  16309. cfg.size = height;
  16310. cfg.ttf_data_owned_by_atlas = 1;
  16311. return nk_font_atlas_add(atlas, &cfg);
  16312. }
  16313. #endif
  16314. NK_API struct nk_font*
  16315. nk_font_atlas_add_compressed(struct nk_font_atlas* atlas,
  16316. void* compressed_data, nk_size compressed_size, float height,
  16317. const struct nk_font_config* config)
  16318. {
  16319. unsigned int decompressed_size;
  16320. void* decompressed_data;
  16321. struct nk_font_config cfg;
  16322. NK_ASSERT(atlas);
  16323. NK_ASSERT(atlas->temporary.alloc);
  16324. NK_ASSERT(atlas->temporary.free);
  16325. NK_ASSERT(atlas->permanent.alloc);
  16326. NK_ASSERT(atlas->permanent.free);
  16327. NK_ASSERT(compressed_data);
  16328. NK_ASSERT(compressed_size);
  16329. if (!atlas || !compressed_data || !atlas->temporary.alloc || !atlas->temporary.free ||
  16330. !atlas->permanent.alloc || !atlas->permanent.free)
  16331. return 0;
  16332. decompressed_size = nk_decompress_length((unsigned char*)compressed_data);
  16333. decompressed_data = atlas->permanent.alloc(atlas->permanent.userdata, 0, decompressed_size);
  16334. NK_ASSERT(decompressed_data);
  16335. if (!decompressed_data) return 0;
  16336. nk_decompress((unsigned char*)decompressed_data, (unsigned char*)compressed_data,
  16337. (unsigned int)compressed_size);
  16338. cfg = (config) ? *config : nk_font_config(height);
  16339. cfg.ttf_blob = decompressed_data;
  16340. cfg.ttf_size = decompressed_size;
  16341. cfg.size = height;
  16342. cfg.ttf_data_owned_by_atlas = 1;
  16343. return nk_font_atlas_add(atlas, &cfg);
  16344. }
  16345. NK_API struct nk_font*
  16346. nk_font_atlas_add_compressed_base85(struct nk_font_atlas* atlas,
  16347. const char* data_base85, float height, const struct nk_font_config* config)
  16348. {
  16349. int compressed_size;
  16350. void* compressed_data;
  16351. struct nk_font* font;
  16352. NK_ASSERT(atlas);
  16353. NK_ASSERT(atlas->temporary.alloc);
  16354. NK_ASSERT(atlas->temporary.free);
  16355. NK_ASSERT(atlas->permanent.alloc);
  16356. NK_ASSERT(atlas->permanent.free);
  16357. NK_ASSERT(data_base85);
  16358. if (!atlas || !data_base85 || !atlas->temporary.alloc || !atlas->temporary.free ||
  16359. !atlas->permanent.alloc || !atlas->permanent.free)
  16360. return 0;
  16361. compressed_size = (((int)nk_strlen(data_base85) + 4) / 5) * 4;
  16362. compressed_data = atlas->temporary.alloc(atlas->temporary.userdata, 0, (nk_size)compressed_size);
  16363. NK_ASSERT(compressed_data);
  16364. if (!compressed_data) return 0;
  16365. nk_decode_85((unsigned char*)compressed_data, (const unsigned char*)data_base85);
  16366. font = nk_font_atlas_add_compressed(atlas, compressed_data,
  16367. (nk_size)compressed_size, height, config);
  16368. atlas->temporary.free(atlas->temporary.userdata, compressed_data);
  16369. return font;
  16370. }
  16371. #ifdef NK_INCLUDE_DEFAULT_FONT
  16372. NK_API struct nk_font*
  16373. nk_font_atlas_add_default(struct nk_font_atlas* atlas,
  16374. float pixel_height, const struct nk_font_config* config)
  16375. {
  16376. NK_ASSERT(atlas);
  16377. NK_ASSERT(atlas->temporary.alloc);
  16378. NK_ASSERT(atlas->temporary.free);
  16379. NK_ASSERT(atlas->permanent.alloc);
  16380. NK_ASSERT(atlas->permanent.free);
  16381. return nk_font_atlas_add_compressed_base85(atlas,
  16382. nk_proggy_clean_ttf_compressed_data_base85, pixel_height, config);
  16383. }
  16384. #endif
  16385. NK_API const void*
  16386. nk_font_atlas_bake(struct nk_font_atlas* atlas, int* width, int* height,
  16387. enum nk_font_atlas_format fmt)
  16388. {
  16389. int i = 0;
  16390. void* tmp = 0;
  16391. nk_size tmp_size, img_size;
  16392. struct nk_font* font_iter;
  16393. struct nk_font_baker* baker;
  16394. NK_ASSERT(atlas);
  16395. NK_ASSERT(atlas->temporary.alloc);
  16396. NK_ASSERT(atlas->temporary.free);
  16397. NK_ASSERT(atlas->permanent.alloc);
  16398. NK_ASSERT(atlas->permanent.free);
  16399. NK_ASSERT(width);
  16400. NK_ASSERT(height);
  16401. if (!atlas || !width || !height ||
  16402. !atlas->temporary.alloc || !atlas->temporary.free ||
  16403. !atlas->permanent.alloc || !atlas->permanent.free)
  16404. return 0;
  16405. #ifdef NK_INCLUDE_DEFAULT_FONT
  16406. /* no font added so just use default font */
  16407. if (!atlas->font_num)
  16408. atlas->default_font = nk_font_atlas_add_default(atlas, 13.0f, 0);
  16409. #endif
  16410. NK_ASSERT(atlas->font_num);
  16411. if (!atlas->font_num) return 0;
  16412. /* allocate temporary baker memory required for the baking process */
  16413. nk_font_baker_memory(&tmp_size, &atlas->glyph_count, atlas->config, atlas->font_num);
  16414. tmp = atlas->temporary.alloc(atlas->temporary.userdata, 0, tmp_size);
  16415. NK_ASSERT(tmp);
  16416. if (!tmp) goto failed;
  16417. NK_MEMSET(tmp, 0, tmp_size);
  16418. /* allocate glyph memory for all fonts */
  16419. baker = nk_font_baker(tmp, atlas->glyph_count, atlas->font_num, &atlas->temporary);
  16420. atlas->glyphs = (struct nk_font_glyph*)atlas->permanent.alloc(
  16421. atlas->permanent.userdata, 0, sizeof(struct nk_font_glyph) * (nk_size)atlas->glyph_count);
  16422. NK_ASSERT(atlas->glyphs);
  16423. if (!atlas->glyphs)
  16424. goto failed;
  16425. /* pack all glyphs into a tight fit space */
  16426. atlas->custom.w = (NK_CURSOR_DATA_W * 2) + 1;
  16427. atlas->custom.h = NK_CURSOR_DATA_H + 1;
  16428. if (!nk_font_bake_pack(baker, &img_size, width, height, &atlas->custom,
  16429. atlas->config, atlas->font_num, &atlas->temporary))
  16430. goto failed;
  16431. /* allocate memory for the baked image font atlas */
  16432. atlas->pixel = atlas->temporary.alloc(atlas->temporary.userdata, 0, img_size);
  16433. NK_ASSERT(atlas->pixel);
  16434. if (!atlas->pixel)
  16435. goto failed;
  16436. /* bake glyphs and custom white pixel into image */
  16437. nk_font_bake(baker, atlas->pixel, *width, *height,
  16438. atlas->glyphs, atlas->glyph_count, atlas->config, atlas->font_num);
  16439. nk_font_bake_custom_data(atlas->pixel, *width, *height, atlas->custom,
  16440. nk_custom_cursor_data, NK_CURSOR_DATA_W, NK_CURSOR_DATA_H, '.', 'X');
  16441. if (fmt == NK_FONT_ATLAS_RGBA32) {
  16442. /* convert alpha8 image into rgba32 image */
  16443. void* img_rgba = atlas->temporary.alloc(atlas->temporary.userdata, 0,
  16444. (nk_size)(*width * *height * 4));
  16445. NK_ASSERT(img_rgba);
  16446. if (!img_rgba) goto failed;
  16447. nk_font_bake_convert(img_rgba, *width, *height, atlas->pixel);
  16448. atlas->temporary.free(atlas->temporary.userdata, atlas->pixel);
  16449. atlas->pixel = img_rgba;
  16450. }
  16451. atlas->tex_width = *width;
  16452. atlas->tex_height = *height;
  16453. /* initialize each font */
  16454. for (font_iter = atlas->fonts; font_iter; font_iter = font_iter->next) {
  16455. struct nk_font* font = font_iter;
  16456. struct nk_font_config* config = font->config;
  16457. nk_font_init(font, config->size, config->fallback_glyph, atlas->glyphs,
  16458. config->font, nk_handle_ptr(0));
  16459. }
  16460. /* initialize each cursor */
  16461. {NK_STORAGE const struct nk_vec2 nk_cursor_data[NK_CURSOR_COUNT][3] = {
  16462. /* Pos Size Offset */
  16463. {{ 0, 3}, {12,19}, { 0, 0}},
  16464. {{13, 0}, { 7,16}, { 4, 8}},
  16465. {{31, 0}, {23,23}, {11,11}},
  16466. {{21, 0}, { 9, 23}, { 5,11}},
  16467. {{55,18}, {23, 9}, {11, 5}},
  16468. {{73, 0}, {17,17}, { 9, 9}},
  16469. {{55, 0}, {17,17}, { 9, 9}}
  16470. };
  16471. for (i = 0; i < NK_CURSOR_COUNT; ++i) {
  16472. struct nk_cursor* cursor = &atlas->cursors[i];
  16473. cursor->img.w = (unsigned short)*width;
  16474. cursor->img.h = (unsigned short)*height;
  16475. cursor->img.region[0] = (unsigned short)(atlas->custom.x + nk_cursor_data[i][0].x);
  16476. cursor->img.region[1] = (unsigned short)(atlas->custom.y + nk_cursor_data[i][0].y);
  16477. cursor->img.region[2] = (unsigned short)nk_cursor_data[i][1].x;
  16478. cursor->img.region[3] = (unsigned short)nk_cursor_data[i][1].y;
  16479. cursor->size = nk_cursor_data[i][1];
  16480. cursor->offset = nk_cursor_data[i][2];
  16481. }}
  16482. /* free temporary memory */
  16483. atlas->temporary.free(atlas->temporary.userdata, tmp);
  16484. return atlas->pixel;
  16485. failed:
  16486. /* error so cleanup all memory */
  16487. if (tmp) atlas->temporary.free(atlas->temporary.userdata, tmp);
  16488. if (atlas->glyphs) {
  16489. atlas->permanent.free(atlas->permanent.userdata, atlas->glyphs);
  16490. atlas->glyphs = 0;
  16491. }
  16492. if (atlas->pixel) {
  16493. atlas->temporary.free(atlas->temporary.userdata, atlas->pixel);
  16494. atlas->pixel = 0;
  16495. }
  16496. return 0;
  16497. }
  16498. NK_API void
  16499. nk_font_atlas_end(struct nk_font_atlas* atlas, nk_handle texture,
  16500. struct nk_draw_null_texture* tex_null)
  16501. {
  16502. int i = 0;
  16503. struct nk_font* font_iter;
  16504. NK_ASSERT(atlas);
  16505. if (!atlas) {
  16506. if (!tex_null) return;
  16507. tex_null->texture = texture;
  16508. tex_null->uv = nk_vec2(0.5f, 0.5f);
  16509. }
  16510. if (tex_null) {
  16511. tex_null->texture = texture;
  16512. tex_null->uv.x = (atlas->custom.x + 0.5f) / (float)atlas->tex_width;
  16513. tex_null->uv.y = (atlas->custom.y + 0.5f) / (float)atlas->tex_height;
  16514. }
  16515. for (font_iter = atlas->fonts; font_iter; font_iter = font_iter->next) {
  16516. font_iter->texture = texture;
  16517. #ifdef NK_INCLUDE_VERTEX_BUFFER_OUTPUT
  16518. font_iter->handle.texture = texture;
  16519. #endif
  16520. }
  16521. for (i = 0; i < NK_CURSOR_COUNT; ++i)
  16522. atlas->cursors[i].img.handle = texture;
  16523. atlas->temporary.free(atlas->temporary.userdata, atlas->pixel);
  16524. atlas->pixel = 0;
  16525. atlas->tex_width = 0;
  16526. atlas->tex_height = 0;
  16527. atlas->custom.x = 0;
  16528. atlas->custom.y = 0;
  16529. atlas->custom.w = 0;
  16530. atlas->custom.h = 0;
  16531. }
  16532. NK_API void
  16533. nk_font_atlas_cleanup(struct nk_font_atlas* atlas)
  16534. {
  16535. NK_ASSERT(atlas);
  16536. NK_ASSERT(atlas->temporary.alloc);
  16537. NK_ASSERT(atlas->temporary.free);
  16538. NK_ASSERT(atlas->permanent.alloc);
  16539. NK_ASSERT(atlas->permanent.free);
  16540. if (!atlas || !atlas->permanent.alloc || !atlas->permanent.free) return;
  16541. if (atlas->config) {
  16542. struct nk_font_config* iter;
  16543. for (iter = atlas->config; iter; iter = iter->next) {
  16544. struct nk_font_config* i;
  16545. for (i = iter->n; i != iter; i = i->n) {
  16546. atlas->permanent.free(atlas->permanent.userdata, i->ttf_blob);
  16547. i->ttf_blob = 0;
  16548. }
  16549. atlas->permanent.free(atlas->permanent.userdata, iter->ttf_blob);
  16550. iter->ttf_blob = 0;
  16551. }
  16552. }
  16553. }
  16554. NK_API void
  16555. nk_font_atlas_clear(struct nk_font_atlas* atlas)
  16556. {
  16557. NK_ASSERT(atlas);
  16558. NK_ASSERT(atlas->temporary.alloc);
  16559. NK_ASSERT(atlas->temporary.free);
  16560. NK_ASSERT(atlas->permanent.alloc);
  16561. NK_ASSERT(atlas->permanent.free);
  16562. if (!atlas || !atlas->permanent.alloc || !atlas->permanent.free) return;
  16563. if (atlas->config) {
  16564. struct nk_font_config* iter, * next;
  16565. for (iter = atlas->config; iter; iter = next) {
  16566. struct nk_font_config* i, * n;
  16567. for (i = iter->n; i != iter; i = n) {
  16568. n = i->n;
  16569. if (i->ttf_blob)
  16570. atlas->permanent.free(atlas->permanent.userdata, i->ttf_blob);
  16571. atlas->permanent.free(atlas->permanent.userdata, i);
  16572. }
  16573. next = iter->next;
  16574. if (i->ttf_blob)
  16575. atlas->permanent.free(atlas->permanent.userdata, iter->ttf_blob);
  16576. atlas->permanent.free(atlas->permanent.userdata, iter);
  16577. }
  16578. atlas->config = 0;
  16579. }
  16580. if (atlas->fonts) {
  16581. struct nk_font* iter, * next;
  16582. for (iter = atlas->fonts; iter; iter = next) {
  16583. next = iter->next;
  16584. atlas->permanent.free(atlas->permanent.userdata, iter);
  16585. }
  16586. atlas->fonts = 0;
  16587. }
  16588. if (atlas->glyphs)
  16589. atlas->permanent.free(atlas->permanent.userdata, atlas->glyphs);
  16590. nk_zero_struct(*atlas);
  16591. }
  16592. #endif
  16593. /* ===============================================================
  16594. *
  16595. * INPUT
  16596. *
  16597. * ===============================================================*/
  16598. NK_API void
  16599. nk_input_begin(struct nk_context* ctx)
  16600. {
  16601. int i;
  16602. struct nk_input* in;
  16603. NK_ASSERT(ctx);
  16604. if (!ctx) return;
  16605. in = &ctx->input;
  16606. for (i = 0; i < NK_BUTTON_MAX; ++i)
  16607. in->mouse.buttons[i].clicked = 0;
  16608. in->keyboard.text_len = 0;
  16609. in->mouse.scroll_delta = nk_vec2(0, 0);
  16610. in->mouse.prev.x = in->mouse.pos.x;
  16611. in->mouse.prev.y = in->mouse.pos.y;
  16612. in->mouse.delta.x = 0;
  16613. in->mouse.delta.y = 0;
  16614. for (i = 0; i < NK_KEY_MAX; i++)
  16615. in->keyboard.keys[i].clicked = 0;
  16616. }
  16617. NK_API void
  16618. nk_input_end(struct nk_context* ctx)
  16619. {
  16620. struct nk_input* in;
  16621. NK_ASSERT(ctx);
  16622. if (!ctx) return;
  16623. in = &ctx->input;
  16624. if (in->mouse.grab)
  16625. in->mouse.grab = 0;
  16626. if (in->mouse.ungrab) {
  16627. in->mouse.grabbed = 0;
  16628. in->mouse.ungrab = 0;
  16629. in->mouse.grab = 0;
  16630. }
  16631. }
  16632. NK_API void
  16633. nk_input_motion(struct nk_context* ctx, int x, int y)
  16634. {
  16635. struct nk_input* in;
  16636. NK_ASSERT(ctx);
  16637. if (!ctx) return;
  16638. in = &ctx->input;
  16639. in->mouse.pos.x = (float)x;
  16640. in->mouse.pos.y = (float)y;
  16641. in->mouse.delta.x = in->mouse.pos.x - in->mouse.prev.x;
  16642. in->mouse.delta.y = in->mouse.pos.y - in->mouse.prev.y;
  16643. }
  16644. NK_API void
  16645. nk_input_key(struct nk_context* ctx, enum nk_keys key, nk_bool down)
  16646. {
  16647. struct nk_input* in;
  16648. NK_ASSERT(ctx);
  16649. if (!ctx) return;
  16650. in = &ctx->input;
  16651. #ifdef NK_KEYSTATE_BASED_INPUT
  16652. if (in->keyboard.keys[key].down != down)
  16653. in->keyboard.keys[key].clicked++;
  16654. #else
  16655. in->keyboard.keys[key].clicked++;
  16656. #endif
  16657. in->keyboard.keys[key].down = down;
  16658. }
  16659. NK_API void
  16660. nk_input_button(struct nk_context* ctx, enum nk_buttons id, int x, int y, nk_bool down)
  16661. {
  16662. struct nk_mouse_button* btn;
  16663. struct nk_input* in;
  16664. NK_ASSERT(ctx);
  16665. if (!ctx) return;
  16666. in = &ctx->input;
  16667. if (in->mouse.buttons[id].down == down) return;
  16668. btn = &in->mouse.buttons[id];
  16669. btn->clicked_pos.x = (float)x;
  16670. btn->clicked_pos.y = (float)y;
  16671. btn->down = down;
  16672. btn->clicked++;
  16673. /* Fix Click-Drag for touch events. */
  16674. in->mouse.delta.x = 0;
  16675. in->mouse.delta.y = 0;
  16676. #ifdef NK_BUTTON_TRIGGER_ON_RELEASE
  16677. if (down == 1 && id == NK_BUTTON_LEFT)
  16678. {
  16679. in->mouse.down_pos.x = btn->clicked_pos.x;
  16680. in->mouse.down_pos.y = btn->clicked_pos.y;
  16681. }
  16682. #endif
  16683. }
  16684. NK_API void
  16685. nk_input_scroll(struct nk_context* ctx, struct nk_vec2 val)
  16686. {
  16687. NK_ASSERT(ctx);
  16688. if (!ctx) return;
  16689. ctx->input.mouse.scroll_delta.x += val.x;
  16690. ctx->input.mouse.scroll_delta.y += val.y;
  16691. }
  16692. NK_API void
  16693. nk_input_glyph(struct nk_context* ctx, const nk_glyph glyph)
  16694. {
  16695. int len = 0;
  16696. nk_rune unicode;
  16697. struct nk_input* in;
  16698. NK_ASSERT(ctx);
  16699. if (!ctx) return;
  16700. in = &ctx->input;
  16701. len = nk_utf_decode(glyph, &unicode, NK_UTF_SIZE);
  16702. if (len && ((in->keyboard.text_len + len) < NK_INPUT_MAX)) {
  16703. nk_utf_encode(unicode, &in->keyboard.text[in->keyboard.text_len],
  16704. NK_INPUT_MAX - in->keyboard.text_len);
  16705. in->keyboard.text_len += len;
  16706. }
  16707. }
  16708. NK_API void
  16709. nk_input_char(struct nk_context* ctx, char c)
  16710. {
  16711. nk_glyph glyph;
  16712. NK_ASSERT(ctx);
  16713. if (!ctx) return;
  16714. glyph[0] = c;
  16715. nk_input_glyph(ctx, glyph);
  16716. }
  16717. NK_API void
  16718. nk_input_unicode(struct nk_context* ctx, nk_rune unicode)
  16719. {
  16720. nk_glyph rune;
  16721. NK_ASSERT(ctx);
  16722. if (!ctx) return;
  16723. nk_utf_encode(unicode, rune, NK_UTF_SIZE);
  16724. nk_input_glyph(ctx, rune);
  16725. }
  16726. NK_API nk_bool
  16727. nk_input_has_mouse_click(const struct nk_input* i, enum nk_buttons id)
  16728. {
  16729. const struct nk_mouse_button* btn;
  16730. if (!i) return nk_false;
  16731. btn = &i->mouse.buttons[id];
  16732. return (btn->clicked && btn->down == nk_false) ? nk_true : nk_false;
  16733. }
  16734. NK_API nk_bool
  16735. nk_input_has_mouse_click_in_rect(const struct nk_input* i, enum nk_buttons id,
  16736. struct nk_rect b)
  16737. {
  16738. const struct nk_mouse_button* btn;
  16739. if (!i) return nk_false;
  16740. btn = &i->mouse.buttons[id];
  16741. if (!NK_INBOX(btn->clicked_pos.x, btn->clicked_pos.y, b.x, b.y, b.w, b.h))
  16742. return nk_false;
  16743. return nk_true;
  16744. }
  16745. NK_API nk_bool
  16746. nk_input_has_mouse_click_in_button_rect(const struct nk_input* i, enum nk_buttons id,
  16747. struct nk_rect b)
  16748. {
  16749. const struct nk_mouse_button* btn;
  16750. if (!i) return nk_false;
  16751. btn = &i->mouse.buttons[id];
  16752. #ifdef NK_BUTTON_TRIGGER_ON_RELEASE
  16753. if (!NK_INBOX(btn->clicked_pos.x, btn->clicked_pos.y, b.x, b.y, b.w, b.h)
  16754. || !NK_INBOX(i->mouse.down_pos.x, i->mouse.down_pos.y, b.x, b.y, b.w, b.h))
  16755. #else
  16756. if (!NK_INBOX(btn->clicked_pos.x, btn->clicked_pos.y, b.x, b.y, b.w, b.h))
  16757. #endif
  16758. return nk_false;
  16759. return nk_true;
  16760. }
  16761. NK_API nk_bool
  16762. nk_input_has_mouse_click_down_in_rect(const struct nk_input* i, enum nk_buttons id,
  16763. struct nk_rect b, nk_bool down)
  16764. {
  16765. const struct nk_mouse_button* btn;
  16766. if (!i) return nk_false;
  16767. btn = &i->mouse.buttons[id];
  16768. return nk_input_has_mouse_click_in_rect(i, id, b) && (btn->down == down);
  16769. }
  16770. NK_API nk_bool
  16771. nk_input_is_mouse_click_in_rect(const struct nk_input* i, enum nk_buttons id,
  16772. struct nk_rect b)
  16773. {
  16774. const struct nk_mouse_button* btn;
  16775. if (!i) return nk_false;
  16776. btn = &i->mouse.buttons[id];
  16777. return (nk_input_has_mouse_click_down_in_rect(i, id, b, nk_false) &&
  16778. btn->clicked) ? nk_true : nk_false;
  16779. }
  16780. NK_API nk_bool
  16781. nk_input_is_mouse_click_down_in_rect(const struct nk_input* i, enum nk_buttons id,
  16782. struct nk_rect b, nk_bool down)
  16783. {
  16784. const struct nk_mouse_button* btn;
  16785. if (!i) return nk_false;
  16786. btn = &i->mouse.buttons[id];
  16787. return (nk_input_has_mouse_click_down_in_rect(i, id, b, down) &&
  16788. btn->clicked) ? nk_true : nk_false;
  16789. }
  16790. NK_API nk_bool
  16791. nk_input_any_mouse_click_in_rect(const struct nk_input* in, struct nk_rect b)
  16792. {
  16793. int i, down = 0;
  16794. for (i = 0; i < NK_BUTTON_MAX; ++i)
  16795. down = down || nk_input_is_mouse_click_in_rect(in, (enum nk_buttons)i, b);
  16796. return down;
  16797. }
  16798. NK_API nk_bool
  16799. nk_input_is_mouse_hovering_rect(const struct nk_input* i, struct nk_rect rect)
  16800. {
  16801. if (!i) return nk_false;
  16802. return NK_INBOX(i->mouse.pos.x, i->mouse.pos.y, rect.x, rect.y, rect.w, rect.h);
  16803. }
  16804. NK_API nk_bool
  16805. nk_input_is_mouse_prev_hovering_rect(const struct nk_input* i, struct nk_rect rect)
  16806. {
  16807. if (!i) return nk_false;
  16808. return NK_INBOX(i->mouse.prev.x, i->mouse.prev.y, rect.x, rect.y, rect.w, rect.h);
  16809. }
  16810. NK_API nk_bool
  16811. nk_input_mouse_clicked(const struct nk_input* i, enum nk_buttons id, struct nk_rect rect)
  16812. {
  16813. if (!i) return nk_false;
  16814. if (!nk_input_is_mouse_hovering_rect(i, rect)) return nk_false;
  16815. return nk_input_is_mouse_click_in_rect(i, id, rect);
  16816. }
  16817. NK_API nk_bool
  16818. nk_input_is_mouse_down(const struct nk_input* i, enum nk_buttons id)
  16819. {
  16820. if (!i) return nk_false;
  16821. return i->mouse.buttons[id].down;
  16822. }
  16823. NK_API nk_bool
  16824. nk_input_is_mouse_pressed(const struct nk_input* i, enum nk_buttons id)
  16825. {
  16826. const struct nk_mouse_button* b;
  16827. if (!i) return nk_false;
  16828. b = &i->mouse.buttons[id];
  16829. if (b->down && b->clicked)
  16830. return nk_true;
  16831. return nk_false;
  16832. }
  16833. NK_API nk_bool
  16834. nk_input_is_mouse_released(const struct nk_input* i, enum nk_buttons id)
  16835. {
  16836. if (!i) return nk_false;
  16837. return (!i->mouse.buttons[id].down && i->mouse.buttons[id].clicked);
  16838. }
  16839. NK_API nk_bool
  16840. nk_input_is_key_pressed(const struct nk_input* i, enum nk_keys key)
  16841. {
  16842. const struct nk_key* k;
  16843. if (!i) return nk_false;
  16844. k = &i->keyboard.keys[key];
  16845. if ((k->down && k->clicked) || (!k->down && k->clicked >= 2))
  16846. return nk_true;
  16847. return nk_false;
  16848. }
  16849. NK_API nk_bool
  16850. nk_input_is_key_released(const struct nk_input* i, enum nk_keys key)
  16851. {
  16852. const struct nk_key* k;
  16853. if (!i) return nk_false;
  16854. k = &i->keyboard.keys[key];
  16855. if ((!k->down && k->clicked) || (k->down && k->clicked >= 2))
  16856. return nk_true;
  16857. return nk_false;
  16858. }
  16859. NK_API nk_bool
  16860. nk_input_is_key_down(const struct nk_input* i, enum nk_keys key)
  16861. {
  16862. const struct nk_key* k;
  16863. if (!i) return nk_false;
  16864. k = &i->keyboard.keys[key];
  16865. if (k->down) return nk_true;
  16866. return nk_false;
  16867. }
  16868. /* ===============================================================
  16869. *
  16870. * STYLE
  16871. *
  16872. * ===============================================================*/
  16873. NK_API void nk_style_default(struct nk_context* ctx) { nk_style_from_table(ctx, 0); }
  16874. #define NK_COLOR_MAP(NK_COLOR)\
  16875. NK_COLOR(NK_COLOR_TEXT, 175,175,175,255) \
  16876. NK_COLOR(NK_COLOR_WINDOW, 45, 45, 45, 255) \
  16877. NK_COLOR(NK_COLOR_HEADER, 40, 40, 40, 255) \
  16878. NK_COLOR(NK_COLOR_BORDER, 65, 65, 65, 255) \
  16879. NK_COLOR(NK_COLOR_BUTTON, 50, 50, 50, 255) \
  16880. NK_COLOR(NK_COLOR_BUTTON_HOVER, 40, 40, 40, 255) \
  16881. NK_COLOR(NK_COLOR_BUTTON_ACTIVE, 35, 35, 35, 255) \
  16882. NK_COLOR(NK_COLOR_TOGGLE, 100,100,100,255) \
  16883. NK_COLOR(NK_COLOR_TOGGLE_HOVER, 120,120,120,255) \
  16884. NK_COLOR(NK_COLOR_TOGGLE_CURSOR, 45, 45, 45, 255) \
  16885. NK_COLOR(NK_COLOR_SELECT, 45, 45, 45, 255) \
  16886. NK_COLOR(NK_COLOR_SELECT_ACTIVE, 35, 35, 35,255) \
  16887. NK_COLOR(NK_COLOR_SLIDER, 38, 38, 38, 255) \
  16888. NK_COLOR(NK_COLOR_SLIDER_CURSOR, 100,100,100,255) \
  16889. NK_COLOR(NK_COLOR_SLIDER_CURSOR_HOVER, 120,120,120,255) \
  16890. NK_COLOR(NK_COLOR_SLIDER_CURSOR_ACTIVE, 150,150,150,255) \
  16891. NK_COLOR(NK_COLOR_PROPERTY, 38, 38, 38, 255) \
  16892. NK_COLOR(NK_COLOR_EDIT, 38, 38, 38, 255) \
  16893. NK_COLOR(NK_COLOR_EDIT_CURSOR, 175,175,175,255) \
  16894. NK_COLOR(NK_COLOR_COMBO, 45, 45, 45, 255) \
  16895. NK_COLOR(NK_COLOR_CHART, 120,120,120,255) \
  16896. NK_COLOR(NK_COLOR_CHART_COLOR, 45, 45, 45, 255) \
  16897. NK_COLOR(NK_COLOR_CHART_COLOR_HIGHLIGHT, 255, 0, 0, 255) \
  16898. NK_COLOR(NK_COLOR_SCROLLBAR, 40, 40, 40, 255) \
  16899. NK_COLOR(NK_COLOR_SCROLLBAR_CURSOR, 100,100,100,255) \
  16900. NK_COLOR(NK_COLOR_SCROLLBAR_CURSOR_HOVER, 120,120,120,255) \
  16901. NK_COLOR(NK_COLOR_SCROLLBAR_CURSOR_ACTIVE, 150,150,150,255) \
  16902. NK_COLOR(NK_COLOR_TAB_HEADER, 40, 40, 40,255)
  16903. NK_GLOBAL const struct nk_color
  16904. nk_default_color_style[NK_COLOR_COUNT] = {
  16905. #define NK_COLOR(a,b,c,d,e) {b,c,d,e},
  16906. NK_COLOR_MAP(NK_COLOR)
  16907. #undef NK_COLOR
  16908. };
  16909. NK_GLOBAL const char* nk_color_names[NK_COLOR_COUNT] = {
  16910. #define NK_COLOR(a,b,c,d,e) #a,
  16911. NK_COLOR_MAP(NK_COLOR)
  16912. #undef NK_COLOR
  16913. };
  16914. NK_API const char*
  16915. nk_style_get_color_by_name(enum nk_style_colors c)
  16916. {
  16917. return nk_color_names[c];
  16918. }
  16919. NK_API struct nk_style_item
  16920. nk_style_item_color(struct nk_color col)
  16921. {
  16922. struct nk_style_item i;
  16923. i.type = NK_STYLE_ITEM_COLOR;
  16924. i.data.color = col;
  16925. return i;
  16926. }
  16927. NK_API struct nk_style_item
  16928. nk_style_item_image(struct nk_image img)
  16929. {
  16930. struct nk_style_item i;
  16931. i.type = NK_STYLE_ITEM_IMAGE;
  16932. i.data.image = img;
  16933. return i;
  16934. }
  16935. NK_API struct nk_style_item
  16936. nk_style_item_nine_slice(struct nk_nine_slice slice)
  16937. {
  16938. struct nk_style_item i;
  16939. i.type = NK_STYLE_ITEM_NINE_SLICE;
  16940. i.data.slice = slice;
  16941. return i;
  16942. }
  16943. NK_API struct nk_style_item
  16944. nk_style_item_hide(void)
  16945. {
  16946. struct nk_style_item i;
  16947. i.type = NK_STYLE_ITEM_COLOR;
  16948. i.data.color = nk_rgba(0, 0, 0, 0);
  16949. return i;
  16950. }
  16951. NK_API void
  16952. nk_style_from_table(struct nk_context* ctx, const struct nk_color* table)
  16953. {
  16954. struct nk_style* style;
  16955. struct nk_style_text* text;
  16956. struct nk_style_button* button;
  16957. struct nk_style_toggle* toggle;
  16958. struct nk_style_selectable* select;
  16959. struct nk_style_slider* slider;
  16960. struct nk_style_progress* prog;
  16961. struct nk_style_scrollbar* scroll;
  16962. struct nk_style_edit* edit;
  16963. struct nk_style_property* property;
  16964. struct nk_style_combo* combo;
  16965. struct nk_style_chart* chart;
  16966. struct nk_style_tab* tab;
  16967. struct nk_style_window* win;
  16968. NK_ASSERT(ctx);
  16969. if (!ctx) return;
  16970. style = &ctx->style;
  16971. table = (!table) ? nk_default_color_style : table;
  16972. /* default text */
  16973. text = &style->text;
  16974. text->color = table[NK_COLOR_TEXT];
  16975. text->padding = nk_vec2(0, 0);
  16976. /* default button */
  16977. button = &style->button;
  16978. nk_zero_struct(*button);
  16979. button->normal = nk_style_item_color(table[NK_COLOR_BUTTON]);
  16980. button->hover = nk_style_item_color(table[NK_COLOR_BUTTON_HOVER]);
  16981. button->active = nk_style_item_color(table[NK_COLOR_BUTTON_ACTIVE]);
  16982. button->border_color = table[NK_COLOR_BORDER];
  16983. button->text_background = table[NK_COLOR_BUTTON];
  16984. button->text_normal = table[NK_COLOR_TEXT];
  16985. button->text_hover = table[NK_COLOR_TEXT];
  16986. button->text_active = table[NK_COLOR_TEXT];
  16987. button->padding = nk_vec2(2.0f, 2.0f);
  16988. button->image_padding = nk_vec2(0.0f, 0.0f);
  16989. button->touch_padding = nk_vec2(0.0f, 0.0f);
  16990. button->userdata = nk_handle_ptr(0);
  16991. button->text_alignment = NK_TEXT_CENTERED;
  16992. button->border = 1.0f;
  16993. button->rounding = 4.0f;
  16994. button->draw_begin = 0;
  16995. button->draw_end = 0;
  16996. /* contextual button */
  16997. button = &style->contextual_button;
  16998. nk_zero_struct(*button);
  16999. button->normal = nk_style_item_color(table[NK_COLOR_WINDOW]);
  17000. button->hover = nk_style_item_color(table[NK_COLOR_BUTTON_HOVER]);
  17001. button->active = nk_style_item_color(table[NK_COLOR_BUTTON_ACTIVE]);
  17002. button->border_color = table[NK_COLOR_WINDOW];
  17003. button->text_background = table[NK_COLOR_WINDOW];
  17004. button->text_normal = table[NK_COLOR_TEXT];
  17005. button->text_hover = table[NK_COLOR_TEXT];
  17006. button->text_active = table[NK_COLOR_TEXT];
  17007. button->padding = nk_vec2(2.0f, 2.0f);
  17008. button->touch_padding = nk_vec2(0.0f, 0.0f);
  17009. button->userdata = nk_handle_ptr(0);
  17010. button->text_alignment = NK_TEXT_CENTERED;
  17011. button->border = 0.0f;
  17012. button->rounding = 0.0f;
  17013. button->draw_begin = 0;
  17014. button->draw_end = 0;
  17015. /* menu button */
  17016. button = &style->menu_button;
  17017. nk_zero_struct(*button);
  17018. button->normal = nk_style_item_color(table[NK_COLOR_WINDOW]);
  17019. button->hover = nk_style_item_color(table[NK_COLOR_WINDOW]);
  17020. button->active = nk_style_item_color(table[NK_COLOR_WINDOW]);
  17021. button->border_color = table[NK_COLOR_WINDOW];
  17022. button->text_background = table[NK_COLOR_WINDOW];
  17023. button->text_normal = table[NK_COLOR_TEXT];
  17024. button->text_hover = table[NK_COLOR_TEXT];
  17025. button->text_active = table[NK_COLOR_TEXT];
  17026. button->padding = nk_vec2(2.0f, 2.0f);
  17027. button->touch_padding = nk_vec2(0.0f, 0.0f);
  17028. button->userdata = nk_handle_ptr(0);
  17029. button->text_alignment = NK_TEXT_CENTERED;
  17030. button->border = 0.0f;
  17031. button->rounding = 1.0f;
  17032. button->draw_begin = 0;
  17033. button->draw_end = 0;
  17034. /* checkbox toggle */
  17035. toggle = &style->checkbox;
  17036. nk_zero_struct(*toggle);
  17037. toggle->normal = nk_style_item_color(table[NK_COLOR_TOGGLE]);
  17038. toggle->hover = nk_style_item_color(table[NK_COLOR_TOGGLE_HOVER]);
  17039. toggle->active = nk_style_item_color(table[NK_COLOR_TOGGLE_HOVER]);
  17040. toggle->cursor_normal = nk_style_item_color(table[NK_COLOR_TOGGLE_CURSOR]);
  17041. toggle->cursor_hover = nk_style_item_color(table[NK_COLOR_TOGGLE_CURSOR]);
  17042. toggle->userdata = nk_handle_ptr(0);
  17043. toggle->text_background = table[NK_COLOR_WINDOW];
  17044. toggle->text_normal = table[NK_COLOR_TEXT];
  17045. toggle->text_hover = table[NK_COLOR_TEXT];
  17046. toggle->text_active = table[NK_COLOR_TEXT];
  17047. toggle->padding = nk_vec2(2.0f, 2.0f);
  17048. toggle->touch_padding = nk_vec2(0, 0);
  17049. toggle->border_color = nk_rgba(0, 0, 0, 0);
  17050. toggle->border = 0.0f;
  17051. toggle->spacing = 4;
  17052. /* option toggle */
  17053. toggle = &style->option;
  17054. nk_zero_struct(*toggle);
  17055. toggle->normal = nk_style_item_color(table[NK_COLOR_TOGGLE]);
  17056. toggle->hover = nk_style_item_color(table[NK_COLOR_TOGGLE_HOVER]);
  17057. toggle->active = nk_style_item_color(table[NK_COLOR_TOGGLE_HOVER]);
  17058. toggle->cursor_normal = nk_style_item_color(table[NK_COLOR_TOGGLE_CURSOR]);
  17059. toggle->cursor_hover = nk_style_item_color(table[NK_COLOR_TOGGLE_CURSOR]);
  17060. toggle->userdata = nk_handle_ptr(0);
  17061. toggle->text_background = table[NK_COLOR_WINDOW];
  17062. toggle->text_normal = table[NK_COLOR_TEXT];
  17063. toggle->text_hover = table[NK_COLOR_TEXT];
  17064. toggle->text_active = table[NK_COLOR_TEXT];
  17065. toggle->padding = nk_vec2(3.0f, 3.0f);
  17066. toggle->touch_padding = nk_vec2(0, 0);
  17067. toggle->border_color = nk_rgba(0, 0, 0, 0);
  17068. toggle->border = 0.0f;
  17069. toggle->spacing = 4;
  17070. /* selectable */
  17071. select = &style->selectable;
  17072. nk_zero_struct(*select);
  17073. select->normal = nk_style_item_color(table[NK_COLOR_SELECT]);
  17074. select->hover = nk_style_item_color(table[NK_COLOR_SELECT]);
  17075. select->pressed = nk_style_item_color(table[NK_COLOR_SELECT]);
  17076. select->normal_active = nk_style_item_color(table[NK_COLOR_SELECT_ACTIVE]);
  17077. select->hover_active = nk_style_item_color(table[NK_COLOR_SELECT_ACTIVE]);
  17078. select->pressed_active = nk_style_item_color(table[NK_COLOR_SELECT_ACTIVE]);
  17079. select->text_normal = table[NK_COLOR_TEXT];
  17080. select->text_hover = table[NK_COLOR_TEXT];
  17081. select->text_pressed = table[NK_COLOR_TEXT];
  17082. select->text_normal_active = table[NK_COLOR_TEXT];
  17083. select->text_hover_active = table[NK_COLOR_TEXT];
  17084. select->text_pressed_active = table[NK_COLOR_TEXT];
  17085. select->padding = nk_vec2(2.0f, 2.0f);
  17086. select->image_padding = nk_vec2(2.0f, 2.0f);
  17087. select->touch_padding = nk_vec2(0, 0);
  17088. select->userdata = nk_handle_ptr(0);
  17089. select->rounding = 0.0f;
  17090. select->draw_begin = 0;
  17091. select->draw_end = 0;
  17092. /* slider */
  17093. slider = &style->slider;
  17094. nk_zero_struct(*slider);
  17095. slider->normal = nk_style_item_hide();
  17096. slider->hover = nk_style_item_hide();
  17097. slider->active = nk_style_item_hide();
  17098. slider->bar_normal = table[NK_COLOR_SLIDER];
  17099. slider->bar_hover = table[NK_COLOR_SLIDER];
  17100. slider->bar_active = table[NK_COLOR_SLIDER];
  17101. slider->bar_filled = table[NK_COLOR_SLIDER_CURSOR];
  17102. slider->cursor_normal = nk_style_item_color(table[NK_COLOR_SLIDER_CURSOR]);
  17103. slider->cursor_hover = nk_style_item_color(table[NK_COLOR_SLIDER_CURSOR_HOVER]);
  17104. slider->cursor_active = nk_style_item_color(table[NK_COLOR_SLIDER_CURSOR_ACTIVE]);
  17105. slider->inc_symbol = NK_SYMBOL_TRIANGLE_RIGHT;
  17106. slider->dec_symbol = NK_SYMBOL_TRIANGLE_LEFT;
  17107. slider->cursor_size = nk_vec2(16, 16);
  17108. slider->padding = nk_vec2(2, 2);
  17109. slider->spacing = nk_vec2(2, 2);
  17110. slider->userdata = nk_handle_ptr(0);
  17111. slider->show_buttons = nk_false;
  17112. slider->bar_height = 8;
  17113. slider->rounding = 0;
  17114. slider->draw_begin = 0;
  17115. slider->draw_end = 0;
  17116. /* slider buttons */
  17117. button = &style->slider.inc_button;
  17118. button->normal = nk_style_item_color(nk_rgb(40, 40, 40));
  17119. button->hover = nk_style_item_color(nk_rgb(42, 42, 42));
  17120. button->active = nk_style_item_color(nk_rgb(44, 44, 44));
  17121. button->border_color = nk_rgb(65, 65, 65);
  17122. button->text_background = nk_rgb(40, 40, 40);
  17123. button->text_normal = nk_rgb(175, 175, 175);
  17124. button->text_hover = nk_rgb(175, 175, 175);
  17125. button->text_active = nk_rgb(175, 175, 175);
  17126. button->padding = nk_vec2(8.0f, 8.0f);
  17127. button->touch_padding = nk_vec2(0.0f, 0.0f);
  17128. button->userdata = nk_handle_ptr(0);
  17129. button->text_alignment = NK_TEXT_CENTERED;
  17130. button->border = 1.0f;
  17131. button->rounding = 0.0f;
  17132. button->draw_begin = 0;
  17133. button->draw_end = 0;
  17134. style->slider.dec_button = style->slider.inc_button;
  17135. /* progressbar */
  17136. prog = &style->progress;
  17137. nk_zero_struct(*prog);
  17138. prog->normal = nk_style_item_color(table[NK_COLOR_SLIDER]);
  17139. prog->hover = nk_style_item_color(table[NK_COLOR_SLIDER]);
  17140. prog->active = nk_style_item_color(table[NK_COLOR_SLIDER]);
  17141. prog->cursor_normal = nk_style_item_color(table[NK_COLOR_SLIDER_CURSOR]);
  17142. prog->cursor_hover = nk_style_item_color(table[NK_COLOR_SLIDER_CURSOR_HOVER]);
  17143. prog->cursor_active = nk_style_item_color(table[NK_COLOR_SLIDER_CURSOR_ACTIVE]);
  17144. prog->border_color = nk_rgba(0, 0, 0, 0);
  17145. prog->cursor_border_color = nk_rgba(0, 0, 0, 0);
  17146. prog->userdata = nk_handle_ptr(0);
  17147. prog->padding = nk_vec2(4, 4);
  17148. prog->rounding = 0;
  17149. prog->border = 0;
  17150. prog->cursor_rounding = 0;
  17151. prog->cursor_border = 0;
  17152. prog->draw_begin = 0;
  17153. prog->draw_end = 0;
  17154. /* scrollbars */
  17155. scroll = &style->scrollh;
  17156. nk_zero_struct(*scroll);
  17157. scroll->normal = nk_style_item_color(table[NK_COLOR_SCROLLBAR]);
  17158. scroll->hover = nk_style_item_color(table[NK_COLOR_SCROLLBAR]);
  17159. scroll->active = nk_style_item_color(table[NK_COLOR_SCROLLBAR]);
  17160. scroll->cursor_normal = nk_style_item_color(table[NK_COLOR_SCROLLBAR_CURSOR]);
  17161. scroll->cursor_hover = nk_style_item_color(table[NK_COLOR_SCROLLBAR_CURSOR_HOVER]);
  17162. scroll->cursor_active = nk_style_item_color(table[NK_COLOR_SCROLLBAR_CURSOR_ACTIVE]);
  17163. scroll->dec_symbol = NK_SYMBOL_CIRCLE_SOLID;
  17164. scroll->inc_symbol = NK_SYMBOL_CIRCLE_SOLID;
  17165. scroll->userdata = nk_handle_ptr(0);
  17166. scroll->border_color = table[NK_COLOR_SCROLLBAR];
  17167. scroll->cursor_border_color = table[NK_COLOR_SCROLLBAR];
  17168. scroll->padding = nk_vec2(0, 0);
  17169. scroll->show_buttons = nk_false;
  17170. scroll->border = 0;
  17171. scroll->rounding = 0;
  17172. scroll->border_cursor = 0;
  17173. scroll->rounding_cursor = 0;
  17174. scroll->draw_begin = 0;
  17175. scroll->draw_end = 0;
  17176. style->scrollv = style->scrollh;
  17177. /* scrollbars buttons */
  17178. button = &style->scrollh.inc_button;
  17179. button->normal = nk_style_item_color(nk_rgb(40, 40, 40));
  17180. button->hover = nk_style_item_color(nk_rgb(42, 42, 42));
  17181. button->active = nk_style_item_color(nk_rgb(44, 44, 44));
  17182. button->border_color = nk_rgb(65, 65, 65);
  17183. button->text_background = nk_rgb(40, 40, 40);
  17184. button->text_normal = nk_rgb(175, 175, 175);
  17185. button->text_hover = nk_rgb(175, 175, 175);
  17186. button->text_active = nk_rgb(175, 175, 175);
  17187. button->padding = nk_vec2(4.0f, 4.0f);
  17188. button->touch_padding = nk_vec2(0.0f, 0.0f);
  17189. button->userdata = nk_handle_ptr(0);
  17190. button->text_alignment = NK_TEXT_CENTERED;
  17191. button->border = 1.0f;
  17192. button->rounding = 0.0f;
  17193. button->draw_begin = 0;
  17194. button->draw_end = 0;
  17195. style->scrollh.dec_button = style->scrollh.inc_button;
  17196. style->scrollv.inc_button = style->scrollh.inc_button;
  17197. style->scrollv.dec_button = style->scrollh.inc_button;
  17198. /* edit */
  17199. edit = &style->edit;
  17200. nk_zero_struct(*edit);
  17201. edit->normal = nk_style_item_color(table[NK_COLOR_EDIT]);
  17202. edit->hover = nk_style_item_color(table[NK_COLOR_EDIT]);
  17203. edit->active = nk_style_item_color(table[NK_COLOR_EDIT]);
  17204. edit->cursor_normal = table[NK_COLOR_TEXT];
  17205. edit->cursor_hover = table[NK_COLOR_TEXT];
  17206. edit->cursor_text_normal = table[NK_COLOR_EDIT];
  17207. edit->cursor_text_hover = table[NK_COLOR_EDIT];
  17208. edit->border_color = table[NK_COLOR_BORDER];
  17209. edit->text_normal = table[NK_COLOR_TEXT];
  17210. edit->text_hover = table[NK_COLOR_TEXT];
  17211. edit->text_active = table[NK_COLOR_TEXT];
  17212. edit->selected_normal = table[NK_COLOR_TEXT];
  17213. edit->selected_hover = table[NK_COLOR_TEXT];
  17214. edit->selected_text_normal = table[NK_COLOR_EDIT];
  17215. edit->selected_text_hover = table[NK_COLOR_EDIT];
  17216. edit->scrollbar_size = nk_vec2(10, 10);
  17217. edit->scrollbar = style->scrollv;
  17218. edit->padding = nk_vec2(4, 4);
  17219. edit->row_padding = 2;
  17220. edit->cursor_size = 4;
  17221. edit->border = 1;
  17222. edit->rounding = 0;
  17223. /* property */
  17224. property = &style->property;
  17225. nk_zero_struct(*property);
  17226. property->normal = nk_style_item_color(table[NK_COLOR_PROPERTY]);
  17227. property->hover = nk_style_item_color(table[NK_COLOR_PROPERTY]);
  17228. property->active = nk_style_item_color(table[NK_COLOR_PROPERTY]);
  17229. property->border_color = table[NK_COLOR_BORDER];
  17230. property->label_normal = table[NK_COLOR_TEXT];
  17231. property->label_hover = table[NK_COLOR_TEXT];
  17232. property->label_active = table[NK_COLOR_TEXT];
  17233. property->sym_left = NK_SYMBOL_TRIANGLE_LEFT;
  17234. property->sym_right = NK_SYMBOL_TRIANGLE_RIGHT;
  17235. property->userdata = nk_handle_ptr(0);
  17236. property->padding = nk_vec2(4, 4);
  17237. property->border = 1;
  17238. property->rounding = 10;
  17239. property->draw_begin = 0;
  17240. property->draw_end = 0;
  17241. /* property buttons */
  17242. button = &style->property.dec_button;
  17243. nk_zero_struct(*button);
  17244. button->normal = nk_style_item_color(table[NK_COLOR_PROPERTY]);
  17245. button->hover = nk_style_item_color(table[NK_COLOR_PROPERTY]);
  17246. button->active = nk_style_item_color(table[NK_COLOR_PROPERTY]);
  17247. button->border_color = nk_rgba(0, 0, 0, 0);
  17248. button->text_background = table[NK_COLOR_PROPERTY];
  17249. button->text_normal = table[NK_COLOR_TEXT];
  17250. button->text_hover = table[NK_COLOR_TEXT];
  17251. button->text_active = table[NK_COLOR_TEXT];
  17252. button->padding = nk_vec2(0.0f, 0.0f);
  17253. button->touch_padding = nk_vec2(0.0f, 0.0f);
  17254. button->userdata = nk_handle_ptr(0);
  17255. button->text_alignment = NK_TEXT_CENTERED;
  17256. button->border = 0.0f;
  17257. button->rounding = 0.0f;
  17258. button->draw_begin = 0;
  17259. button->draw_end = 0;
  17260. style->property.inc_button = style->property.dec_button;
  17261. /* property edit */
  17262. edit = &style->property.edit;
  17263. nk_zero_struct(*edit);
  17264. edit->normal = nk_style_item_color(table[NK_COLOR_PROPERTY]);
  17265. edit->hover = nk_style_item_color(table[NK_COLOR_PROPERTY]);
  17266. edit->active = nk_style_item_color(table[NK_COLOR_PROPERTY]);
  17267. edit->border_color = nk_rgba(0, 0, 0, 0);
  17268. edit->cursor_normal = table[NK_COLOR_TEXT];
  17269. edit->cursor_hover = table[NK_COLOR_TEXT];
  17270. edit->cursor_text_normal = table[NK_COLOR_EDIT];
  17271. edit->cursor_text_hover = table[NK_COLOR_EDIT];
  17272. edit->text_normal = table[NK_COLOR_TEXT];
  17273. edit->text_hover = table[NK_COLOR_TEXT];
  17274. edit->text_active = table[NK_COLOR_TEXT];
  17275. edit->selected_normal = table[NK_COLOR_TEXT];
  17276. edit->selected_hover = table[NK_COLOR_TEXT];
  17277. edit->selected_text_normal = table[NK_COLOR_EDIT];
  17278. edit->selected_text_hover = table[NK_COLOR_EDIT];
  17279. edit->padding = nk_vec2(0, 0);
  17280. edit->cursor_size = 8;
  17281. edit->border = 0;
  17282. edit->rounding = 0;
  17283. /* chart */
  17284. chart = &style->chart;
  17285. nk_zero_struct(*chart);
  17286. chart->background = nk_style_item_color(table[NK_COLOR_CHART]);
  17287. chart->border_color = table[NK_COLOR_BORDER];
  17288. chart->selected_color = table[NK_COLOR_CHART_COLOR_HIGHLIGHT];
  17289. chart->color = table[NK_COLOR_CHART_COLOR];
  17290. chart->padding = nk_vec2(4, 4);
  17291. chart->border = 0;
  17292. chart->rounding = 0;
  17293. /* combo */
  17294. combo = &style->combo;
  17295. combo->normal = nk_style_item_color(table[NK_COLOR_COMBO]);
  17296. combo->hover = nk_style_item_color(table[NK_COLOR_COMBO]);
  17297. combo->active = nk_style_item_color(table[NK_COLOR_COMBO]);
  17298. combo->border_color = table[NK_COLOR_BORDER];
  17299. combo->label_normal = table[NK_COLOR_TEXT];
  17300. combo->label_hover = table[NK_COLOR_TEXT];
  17301. combo->label_active = table[NK_COLOR_TEXT];
  17302. combo->sym_normal = NK_SYMBOL_TRIANGLE_DOWN;
  17303. combo->sym_hover = NK_SYMBOL_TRIANGLE_DOWN;
  17304. combo->sym_active = NK_SYMBOL_TRIANGLE_DOWN;
  17305. combo->content_padding = nk_vec2(4, 4);
  17306. combo->button_padding = nk_vec2(0, 4);
  17307. combo->spacing = nk_vec2(4, 0);
  17308. combo->border = 1;
  17309. combo->rounding = 0;
  17310. /* combo button */
  17311. button = &style->combo.button;
  17312. nk_zero_struct(*button);
  17313. button->normal = nk_style_item_color(table[NK_COLOR_COMBO]);
  17314. button->hover = nk_style_item_color(table[NK_COLOR_COMBO]);
  17315. button->active = nk_style_item_color(table[NK_COLOR_COMBO]);
  17316. button->border_color = nk_rgba(0, 0, 0, 0);
  17317. button->text_background = table[NK_COLOR_COMBO];
  17318. button->text_normal = table[NK_COLOR_TEXT];
  17319. button->text_hover = table[NK_COLOR_TEXT];
  17320. button->text_active = table[NK_COLOR_TEXT];
  17321. button->padding = nk_vec2(2.0f, 2.0f);
  17322. button->touch_padding = nk_vec2(0.0f, 0.0f);
  17323. button->userdata = nk_handle_ptr(0);
  17324. button->text_alignment = NK_TEXT_CENTERED;
  17325. button->border = 0.0f;
  17326. button->rounding = 0.0f;
  17327. button->draw_begin = 0;
  17328. button->draw_end = 0;
  17329. /* tab */
  17330. tab = &style->tab;
  17331. tab->background = nk_style_item_color(table[NK_COLOR_TAB_HEADER]);
  17332. tab->border_color = table[NK_COLOR_BORDER];
  17333. tab->text = table[NK_COLOR_TEXT];
  17334. tab->sym_minimize = NK_SYMBOL_TRIANGLE_RIGHT;
  17335. tab->sym_maximize = NK_SYMBOL_TRIANGLE_DOWN;
  17336. tab->padding = nk_vec2(4, 4);
  17337. tab->spacing = nk_vec2(4, 4);
  17338. tab->indent = 10.0f;
  17339. tab->border = 1;
  17340. tab->rounding = 0;
  17341. /* tab button */
  17342. button = &style->tab.tab_minimize_button;
  17343. nk_zero_struct(*button);
  17344. button->normal = nk_style_item_color(table[NK_COLOR_TAB_HEADER]);
  17345. button->hover = nk_style_item_color(table[NK_COLOR_TAB_HEADER]);
  17346. button->active = nk_style_item_color(table[NK_COLOR_TAB_HEADER]);
  17347. button->border_color = nk_rgba(0, 0, 0, 0);
  17348. button->text_background = table[NK_COLOR_TAB_HEADER];
  17349. button->text_normal = table[NK_COLOR_TEXT];
  17350. button->text_hover = table[NK_COLOR_TEXT];
  17351. button->text_active = table[NK_COLOR_TEXT];
  17352. button->padding = nk_vec2(2.0f, 2.0f);
  17353. button->touch_padding = nk_vec2(0.0f, 0.0f);
  17354. button->userdata = nk_handle_ptr(0);
  17355. button->text_alignment = NK_TEXT_CENTERED;
  17356. button->border = 0.0f;
  17357. button->rounding = 0.0f;
  17358. button->draw_begin = 0;
  17359. button->draw_end = 0;
  17360. style->tab.tab_maximize_button = *button;
  17361. /* node button */
  17362. button = &style->tab.node_minimize_button;
  17363. nk_zero_struct(*button);
  17364. button->normal = nk_style_item_color(table[NK_COLOR_WINDOW]);
  17365. button->hover = nk_style_item_color(table[NK_COLOR_WINDOW]);
  17366. button->active = nk_style_item_color(table[NK_COLOR_WINDOW]);
  17367. button->border_color = nk_rgba(0, 0, 0, 0);
  17368. button->text_background = table[NK_COLOR_TAB_HEADER];
  17369. button->text_normal = table[NK_COLOR_TEXT];
  17370. button->text_hover = table[NK_COLOR_TEXT];
  17371. button->text_active = table[NK_COLOR_TEXT];
  17372. button->padding = nk_vec2(2.0f, 2.0f);
  17373. button->touch_padding = nk_vec2(0.0f, 0.0f);
  17374. button->userdata = nk_handle_ptr(0);
  17375. button->text_alignment = NK_TEXT_CENTERED;
  17376. button->border = 0.0f;
  17377. button->rounding = 0.0f;
  17378. button->draw_begin = 0;
  17379. button->draw_end = 0;
  17380. style->tab.node_maximize_button = *button;
  17381. /* window header */
  17382. win = &style->window;
  17383. win->header.align = NK_HEADER_RIGHT;
  17384. win->header.close_symbol = NK_SYMBOL_X;
  17385. win->header.minimize_symbol = NK_SYMBOL_MINUS;
  17386. win->header.maximize_symbol = NK_SYMBOL_PLUS;
  17387. win->header.normal = nk_style_item_color(table[NK_COLOR_HEADER]);
  17388. win->header.hover = nk_style_item_color(table[NK_COLOR_HEADER]);
  17389. win->header.active = nk_style_item_color(table[NK_COLOR_HEADER]);
  17390. win->header.label_normal = table[NK_COLOR_TEXT];
  17391. win->header.label_hover = table[NK_COLOR_TEXT];
  17392. win->header.label_active = table[NK_COLOR_TEXT];
  17393. win->header.label_padding = nk_vec2(4, 4);
  17394. win->header.padding = nk_vec2(4, 4);
  17395. win->header.spacing = nk_vec2(0, 0);
  17396. /* window header close button */
  17397. button = &style->window.header.close_button;
  17398. nk_zero_struct(*button);
  17399. button->normal = nk_style_item_color(table[NK_COLOR_HEADER]);
  17400. button->hover = nk_style_item_color(table[NK_COLOR_HEADER]);
  17401. button->active = nk_style_item_color(table[NK_COLOR_HEADER]);
  17402. button->border_color = nk_rgba(0, 0, 0, 0);
  17403. button->text_background = table[NK_COLOR_HEADER];
  17404. button->text_normal = table[NK_COLOR_TEXT];
  17405. button->text_hover = table[NK_COLOR_TEXT];
  17406. button->text_active = table[NK_COLOR_TEXT];
  17407. button->padding = nk_vec2(0.0f, 0.0f);
  17408. button->touch_padding = nk_vec2(0.0f, 0.0f);
  17409. button->userdata = nk_handle_ptr(0);
  17410. button->text_alignment = NK_TEXT_CENTERED;
  17411. button->border = 0.0f;
  17412. button->rounding = 0.0f;
  17413. button->draw_begin = 0;
  17414. button->draw_end = 0;
  17415. /* window header minimize button */
  17416. button = &style->window.header.minimize_button;
  17417. nk_zero_struct(*button);
  17418. button->normal = nk_style_item_color(table[NK_COLOR_HEADER]);
  17419. button->hover = nk_style_item_color(table[NK_COLOR_HEADER]);
  17420. button->active = nk_style_item_color(table[NK_COLOR_HEADER]);
  17421. button->border_color = nk_rgba(0, 0, 0, 0);
  17422. button->text_background = table[NK_COLOR_HEADER];
  17423. button->text_normal = table[NK_COLOR_TEXT];
  17424. button->text_hover = table[NK_COLOR_TEXT];
  17425. button->text_active = table[NK_COLOR_TEXT];
  17426. button->padding = nk_vec2(0.0f, 0.0f);
  17427. button->touch_padding = nk_vec2(0.0f, 0.0f);
  17428. button->userdata = nk_handle_ptr(0);
  17429. button->text_alignment = NK_TEXT_CENTERED;
  17430. button->border = 0.0f;
  17431. button->rounding = 0.0f;
  17432. button->draw_begin = 0;
  17433. button->draw_end = 0;
  17434. /* window */
  17435. win->background = table[NK_COLOR_WINDOW];
  17436. win->fixed_background = nk_style_item_color(table[NK_COLOR_WINDOW]);
  17437. win->border_color = table[NK_COLOR_BORDER];
  17438. win->popup_border_color = table[NK_COLOR_BORDER];
  17439. win->combo_border_color = table[NK_COLOR_BORDER];
  17440. win->contextual_border_color = table[NK_COLOR_BORDER];
  17441. win->menu_border_color = table[NK_COLOR_BORDER];
  17442. win->group_border_color = table[NK_COLOR_BORDER];
  17443. win->tooltip_border_color = table[NK_COLOR_BORDER];
  17444. win->scaler = nk_style_item_color(table[NK_COLOR_TEXT]);
  17445. win->rounding = 0.0f;
  17446. win->spacing = nk_vec2(4, 4);
  17447. win->scrollbar_size = nk_vec2(10, 10);
  17448. win->min_size = nk_vec2(64, 64);
  17449. win->combo_border = 1.0f;
  17450. win->contextual_border = 1.0f;
  17451. win->menu_border = 1.0f;
  17452. win->group_border = 1.0f;
  17453. win->tooltip_border = 1.0f;
  17454. win->popup_border = 1.0f;
  17455. win->border = 2.0f;
  17456. win->min_row_height_padding = 8;
  17457. win->padding = nk_vec2(4, 4);
  17458. win->group_padding = nk_vec2(4, 4);
  17459. win->popup_padding = nk_vec2(4, 4);
  17460. win->combo_padding = nk_vec2(4, 4);
  17461. win->contextual_padding = nk_vec2(4, 4);
  17462. win->menu_padding = nk_vec2(4, 4);
  17463. win->tooltip_padding = nk_vec2(4, 4);
  17464. }
  17465. NK_API void
  17466. nk_style_set_font(struct nk_context* ctx, const struct nk_user_font* font)
  17467. {
  17468. struct nk_style* style;
  17469. NK_ASSERT(ctx);
  17470. if (!ctx) return;
  17471. style = &ctx->style;
  17472. style->font = font;
  17473. ctx->stacks.fonts.head = 0;
  17474. if (ctx->current)
  17475. nk_layout_reset_min_row_height(ctx);
  17476. }
  17477. NK_API nk_bool
  17478. nk_style_push_font(struct nk_context* ctx, const struct nk_user_font* font)
  17479. {
  17480. struct nk_config_stack_user_font* font_stack;
  17481. struct nk_config_stack_user_font_element* element;
  17482. NK_ASSERT(ctx);
  17483. if (!ctx) return 0;
  17484. font_stack = &ctx->stacks.fonts;
  17485. NK_ASSERT(font_stack->head < (int)NK_LEN(font_stack->elements));
  17486. if (font_stack->head >= (int)NK_LEN(font_stack->elements))
  17487. return 0;
  17488. element = &font_stack->elements[font_stack->head++];
  17489. element->address = &ctx->style.font;
  17490. element->old_value = ctx->style.font;
  17491. ctx->style.font = font;
  17492. return 1;
  17493. }
  17494. NK_API nk_bool
  17495. nk_style_pop_font(struct nk_context* ctx)
  17496. {
  17497. struct nk_config_stack_user_font* font_stack;
  17498. struct nk_config_stack_user_font_element* element;
  17499. NK_ASSERT(ctx);
  17500. if (!ctx) return 0;
  17501. font_stack = &ctx->stacks.fonts;
  17502. NK_ASSERT(font_stack->head > 0);
  17503. if (font_stack->head < 1)
  17504. return 0;
  17505. element = &font_stack->elements[--font_stack->head];
  17506. *element->address = element->old_value;
  17507. return 1;
  17508. }
  17509. #define NK_STYLE_PUSH_IMPLEMENATION(prefix, type, stack) \
  17510. nk_style_push_##type(struct nk_context *ctx, prefix##_##type *address, prefix##_##type value)\
  17511. {\
  17512. struct nk_config_stack_##type * type_stack;\
  17513. struct nk_config_stack_##type##_element *element;\
  17514. NK_ASSERT(ctx);\
  17515. if (!ctx) return 0;\
  17516. type_stack = &ctx->stacks.stack;\
  17517. NK_ASSERT(type_stack->head < (int)NK_LEN(type_stack->elements));\
  17518. if (type_stack->head >= (int)NK_LEN(type_stack->elements))\
  17519. return 0;\
  17520. element = &type_stack->elements[type_stack->head++];\
  17521. element->address = address;\
  17522. element->old_value = *address;\
  17523. *address = value;\
  17524. return 1;\
  17525. }
  17526. #define NK_STYLE_POP_IMPLEMENATION(type, stack) \
  17527. nk_style_pop_##type(struct nk_context *ctx)\
  17528. {\
  17529. struct nk_config_stack_##type *type_stack;\
  17530. struct nk_config_stack_##type##_element *element;\
  17531. NK_ASSERT(ctx);\
  17532. if (!ctx) return 0;\
  17533. type_stack = &ctx->stacks.stack;\
  17534. NK_ASSERT(type_stack->head > 0);\
  17535. if (type_stack->head < 1)\
  17536. return 0;\
  17537. element = &type_stack->elements[--type_stack->head];\
  17538. *element->address = element->old_value;\
  17539. return 1;\
  17540. }
  17541. NK_API nk_bool NK_STYLE_PUSH_IMPLEMENATION(struct nk, style_item, style_items)
  17542. NK_API nk_bool NK_STYLE_PUSH_IMPLEMENATION(nk, float, floats)
  17543. NK_API nk_bool NK_STYLE_PUSH_IMPLEMENATION(struct nk, vec2, vectors)
  17544. NK_API nk_bool NK_STYLE_PUSH_IMPLEMENATION(nk, flags, flags)
  17545. NK_API nk_bool NK_STYLE_PUSH_IMPLEMENATION(struct nk, color, colors)
  17546. NK_API nk_bool NK_STYLE_POP_IMPLEMENATION(style_item, style_items)
  17547. NK_API nk_bool NK_STYLE_POP_IMPLEMENATION(float, floats)
  17548. NK_API nk_bool NK_STYLE_POP_IMPLEMENATION(vec2, vectors)
  17549. NK_API nk_bool NK_STYLE_POP_IMPLEMENATION(flags, flags)
  17550. NK_API nk_bool NK_STYLE_POP_IMPLEMENATION(color, colors)
  17551. NK_API nk_bool
  17552. nk_style_set_cursor(struct nk_context* ctx, enum nk_style_cursor c)
  17553. {
  17554. struct nk_style* style;
  17555. NK_ASSERT(ctx);
  17556. if (!ctx) return 0;
  17557. style = &ctx->style;
  17558. if (style->cursors[c]) {
  17559. style->cursor_active = style->cursors[c];
  17560. return 1;
  17561. }
  17562. return 0;
  17563. }
  17564. NK_API void
  17565. nk_style_show_cursor(struct nk_context* ctx)
  17566. {
  17567. ctx->style.cursor_visible = nk_true;
  17568. }
  17569. NK_API void
  17570. nk_style_hide_cursor(struct nk_context* ctx)
  17571. {
  17572. ctx->style.cursor_visible = nk_false;
  17573. }
  17574. NK_API void
  17575. nk_style_load_cursor(struct nk_context* ctx, enum nk_style_cursor cursor,
  17576. const struct nk_cursor* c)
  17577. {
  17578. struct nk_style* style;
  17579. NK_ASSERT(ctx);
  17580. if (!ctx) return;
  17581. style = &ctx->style;
  17582. style->cursors[cursor] = c;
  17583. }
  17584. NK_API void
  17585. nk_style_load_all_cursors(struct nk_context* ctx, struct nk_cursor* cursors)
  17586. {
  17587. int i = 0;
  17588. struct nk_style* style;
  17589. NK_ASSERT(ctx);
  17590. if (!ctx) return;
  17591. style = &ctx->style;
  17592. for (i = 0; i < NK_CURSOR_COUNT; ++i)
  17593. style->cursors[i] = &cursors[i];
  17594. style->cursor_visible = nk_true;
  17595. }
  17596. /* ==============================================================
  17597. *
  17598. * CONTEXT
  17599. *
  17600. * ===============================================================*/
  17601. NK_INTERN void
  17602. nk_setup(struct nk_context* ctx, const struct nk_user_font* font)
  17603. {
  17604. NK_ASSERT(ctx);
  17605. if (!ctx) return;
  17606. nk_zero_struct(*ctx);
  17607. nk_style_default(ctx);
  17608. ctx->seq = 1;
  17609. if (font) ctx->style.font = font;
  17610. #ifdef NK_INCLUDE_VERTEX_BUFFER_OUTPUT
  17611. nk_draw_list_init(&ctx->draw_list);
  17612. #endif
  17613. }
  17614. #ifdef NK_INCLUDE_DEFAULT_ALLOCATOR
  17615. NK_API nk_bool
  17616. nk_init_default(struct nk_context* ctx, const struct nk_user_font* font)
  17617. {
  17618. struct nk_allocator alloc;
  17619. alloc.userdata.ptr = 0;
  17620. alloc.alloc = nk_malloc;
  17621. alloc.free = nk_mfree;
  17622. return nk_init(ctx, &alloc, font);
  17623. }
  17624. #endif
  17625. NK_API nk_bool
  17626. nk_init_fixed(struct nk_context* ctx, void* memory, nk_size size,
  17627. const struct nk_user_font* font)
  17628. {
  17629. NK_ASSERT(memory);
  17630. if (!memory) return 0;
  17631. nk_setup(ctx, font);
  17632. nk_buffer_init_fixed(&ctx->memory, memory, size);
  17633. ctx->use_pool = nk_false;
  17634. return 1;
  17635. }
  17636. NK_API nk_bool
  17637. nk_init_custom(struct nk_context* ctx, struct nk_buffer* cmds,
  17638. struct nk_buffer* pool, const struct nk_user_font* font)
  17639. {
  17640. NK_ASSERT(cmds);
  17641. NK_ASSERT(pool);
  17642. if (!cmds || !pool) return 0;
  17643. nk_setup(ctx, font);
  17644. ctx->memory = *cmds;
  17645. if (pool->type == NK_BUFFER_FIXED) {
  17646. /* take memory from buffer and alloc fixed pool */
  17647. nk_pool_init_fixed(&ctx->pool, pool->memory.ptr, pool->memory.size);
  17648. }
  17649. else {
  17650. /* create dynamic pool from buffer allocator */
  17651. struct nk_allocator* alloc = &pool->pool;
  17652. nk_pool_init(&ctx->pool, alloc, NK_POOL_DEFAULT_CAPACITY);
  17653. }
  17654. ctx->use_pool = nk_true;
  17655. return 1;
  17656. }
  17657. NK_API nk_bool
  17658. nk_init(struct nk_context* ctx, struct nk_allocator* alloc,
  17659. const struct nk_user_font* font)
  17660. {
  17661. NK_ASSERT(alloc);
  17662. if (!alloc) return 0;
  17663. nk_setup(ctx, font);
  17664. nk_buffer_init(&ctx->memory, alloc, NK_DEFAULT_COMMAND_BUFFER_SIZE);
  17665. nk_pool_init(&ctx->pool, alloc, NK_POOL_DEFAULT_CAPACITY);
  17666. ctx->use_pool = nk_true;
  17667. return 1;
  17668. }
  17669. #ifdef NK_INCLUDE_COMMAND_USERDATA
  17670. NK_API void
  17671. nk_set_user_data(struct nk_context* ctx, nk_handle handle)
  17672. {
  17673. if (!ctx) return;
  17674. ctx->userdata = handle;
  17675. if (ctx->current)
  17676. ctx->current->buffer.userdata = handle;
  17677. }
  17678. #endif
  17679. NK_API void
  17680. nk_free(struct nk_context* ctx)
  17681. {
  17682. NK_ASSERT(ctx);
  17683. if (!ctx) return;
  17684. nk_buffer_free(&ctx->memory);
  17685. if (ctx->use_pool)
  17686. nk_pool_free(&ctx->pool);
  17687. nk_zero(&ctx->input, sizeof(ctx->input));
  17688. nk_zero(&ctx->style, sizeof(ctx->style));
  17689. nk_zero(&ctx->memory, sizeof(ctx->memory));
  17690. ctx->seq = 0;
  17691. ctx->build = 0;
  17692. ctx->begin = 0;
  17693. ctx->end = 0;
  17694. ctx->active = 0;
  17695. ctx->current = 0;
  17696. ctx->freelist = 0;
  17697. ctx->count = 0;
  17698. }
  17699. NK_API void
  17700. nk_clear(struct nk_context* ctx)
  17701. {
  17702. struct nk_window* iter;
  17703. struct nk_window* next;
  17704. NK_ASSERT(ctx);
  17705. if (!ctx) return;
  17706. if (ctx->use_pool)
  17707. nk_buffer_clear(&ctx->memory);
  17708. else nk_buffer_reset(&ctx->memory, NK_BUFFER_FRONT);
  17709. ctx->build = 0;
  17710. ctx->memory.calls = 0;
  17711. ctx->last_widget_state = 0;
  17712. ctx->style.cursor_active = ctx->style.cursors[NK_CURSOR_ARROW];
  17713. NK_MEMSET(&ctx->overlay, 0, sizeof(ctx->overlay));
  17714. /* garbage collector */
  17715. iter = ctx->begin;
  17716. while (iter) {
  17717. /* make sure valid minimized windows do not get removed */
  17718. if ((iter->flags & NK_WINDOW_MINIMIZED) &&
  17719. !(iter->flags & NK_WINDOW_CLOSED) &&
  17720. iter->seq == ctx->seq) {
  17721. iter = iter->next;
  17722. continue;
  17723. }
  17724. /* remove hotness from hidden or closed windows*/
  17725. if (((iter->flags & NK_WINDOW_HIDDEN) ||
  17726. (iter->flags & NK_WINDOW_CLOSED)) &&
  17727. iter == ctx->active) {
  17728. ctx->active = iter->prev;
  17729. ctx->end = iter->prev;
  17730. if (!ctx->end)
  17731. ctx->begin = 0;
  17732. if (ctx->active)
  17733. ctx->active->flags &= ~(unsigned)NK_WINDOW_ROM;
  17734. }
  17735. /* free unused popup windows */
  17736. if (iter->popup.win && iter->popup.win->seq != ctx->seq) {
  17737. nk_free_window(ctx, iter->popup.win);
  17738. iter->popup.win = 0;
  17739. }
  17740. /* remove unused window state tables */
  17741. {struct nk_table* n, * it = iter->tables;
  17742. while (it) {
  17743. n = it->next;
  17744. if (it->seq != ctx->seq) {
  17745. nk_remove_table(iter, it);
  17746. nk_zero(it, sizeof(union nk_page_data));
  17747. nk_free_table(ctx, it);
  17748. if (it == iter->tables)
  17749. iter->tables = n;
  17750. } it = n;
  17751. }}
  17752. /* window itself is not used anymore so free */
  17753. if (iter->seq != ctx->seq || iter->flags & NK_WINDOW_CLOSED) {
  17754. next = iter->next;
  17755. nk_remove_window(ctx, iter);
  17756. nk_free_window(ctx, iter);
  17757. iter = next;
  17758. }
  17759. else iter = iter->next;
  17760. }
  17761. ctx->seq++;
  17762. }
  17763. NK_LIB void
  17764. nk_start_buffer(struct nk_context* ctx, struct nk_command_buffer* buffer)
  17765. {
  17766. NK_ASSERT(ctx);
  17767. NK_ASSERT(buffer);
  17768. if (!ctx || !buffer) return;
  17769. buffer->begin = ctx->memory.allocated;
  17770. buffer->end = buffer->begin;
  17771. buffer->last = buffer->begin;
  17772. buffer->clip = nk_null_rect;
  17773. }
  17774. NK_LIB void
  17775. nk_start(struct nk_context* ctx, struct nk_window* win)
  17776. {
  17777. NK_ASSERT(ctx);
  17778. NK_ASSERT(win);
  17779. nk_start_buffer(ctx, &win->buffer);
  17780. }
  17781. NK_LIB void
  17782. nk_start_popup(struct nk_context* ctx, struct nk_window* win)
  17783. {
  17784. struct nk_popup_buffer* buf;
  17785. NK_ASSERT(ctx);
  17786. NK_ASSERT(win);
  17787. if (!ctx || !win) return;
  17788. /* save buffer fill state for popup */
  17789. buf = &win->popup.buf;
  17790. buf->begin = win->buffer.end;
  17791. buf->end = win->buffer.end;
  17792. buf->parent = win->buffer.last;
  17793. buf->last = buf->begin;
  17794. buf->active = nk_true;
  17795. }
  17796. NK_LIB void
  17797. nk_finish_popup(struct nk_context* ctx, struct nk_window* win)
  17798. {
  17799. struct nk_popup_buffer* buf;
  17800. NK_ASSERT(ctx);
  17801. NK_ASSERT(win);
  17802. if (!ctx || !win) return;
  17803. buf = &win->popup.buf;
  17804. buf->last = win->buffer.last;
  17805. buf->end = win->buffer.end;
  17806. }
  17807. NK_LIB void
  17808. nk_finish_buffer(struct nk_context* ctx, struct nk_command_buffer* buffer)
  17809. {
  17810. NK_ASSERT(ctx);
  17811. NK_ASSERT(buffer);
  17812. if (!ctx || !buffer) return;
  17813. buffer->end = ctx->memory.allocated;
  17814. }
  17815. NK_LIB void
  17816. nk_finish(struct nk_context* ctx, struct nk_window* win)
  17817. {
  17818. struct nk_popup_buffer* buf;
  17819. struct nk_command* parent_last;
  17820. void* memory;
  17821. NK_ASSERT(ctx);
  17822. NK_ASSERT(win);
  17823. if (!ctx || !win) return;
  17824. nk_finish_buffer(ctx, &win->buffer);
  17825. if (!win->popup.buf.active) return;
  17826. buf = &win->popup.buf;
  17827. memory = ctx->memory.memory.ptr;
  17828. parent_last = nk_ptr_add(struct nk_command, memory, buf->parent);
  17829. parent_last->next = buf->end;
  17830. }
  17831. NK_LIB void
  17832. nk_build(struct nk_context* ctx)
  17833. {
  17834. struct nk_window* it = 0;
  17835. struct nk_command* cmd = 0;
  17836. nk_byte* buffer = 0;
  17837. /* draw cursor overlay */
  17838. if (!ctx->style.cursor_active)
  17839. ctx->style.cursor_active = ctx->style.cursors[NK_CURSOR_ARROW];
  17840. if (ctx->style.cursor_active && !ctx->input.mouse.grabbed && ctx->style.cursor_visible) {
  17841. struct nk_rect mouse_bounds;
  17842. const struct nk_cursor* cursor = ctx->style.cursor_active;
  17843. nk_command_buffer_init(&ctx->overlay, &ctx->memory, NK_CLIPPING_OFF);
  17844. nk_start_buffer(ctx, &ctx->overlay);
  17845. mouse_bounds.x = ctx->input.mouse.pos.x - cursor->offset.x;
  17846. mouse_bounds.y = ctx->input.mouse.pos.y - cursor->offset.y;
  17847. mouse_bounds.w = cursor->size.x;
  17848. mouse_bounds.h = cursor->size.y;
  17849. nk_draw_image(&ctx->overlay, mouse_bounds, &cursor->img, nk_white);
  17850. nk_finish_buffer(ctx, &ctx->overlay);
  17851. }
  17852. /* build one big draw command list out of all window buffers */
  17853. it = ctx->begin;
  17854. buffer = (nk_byte*)ctx->memory.memory.ptr;
  17855. while (it != 0) {
  17856. struct nk_window* next = it->next;
  17857. if (it->buffer.last == it->buffer.begin || (it->flags & NK_WINDOW_HIDDEN) ||
  17858. it->seq != ctx->seq)
  17859. goto cont;
  17860. cmd = nk_ptr_add(struct nk_command, buffer, it->buffer.last);
  17861. while (next && ((next->buffer.last == next->buffer.begin) ||
  17862. (next->flags & NK_WINDOW_HIDDEN) || next->seq != ctx->seq))
  17863. next = next->next; /* skip empty command buffers */
  17864. if (next) cmd->next = next->buffer.begin;
  17865. cont: it = next;
  17866. }
  17867. /* append all popup draw commands into lists */
  17868. it = ctx->begin;
  17869. while (it != 0) {
  17870. struct nk_window* next = it->next;
  17871. struct nk_popup_buffer* buf;
  17872. if (!it->popup.buf.active)
  17873. goto skip;
  17874. buf = &it->popup.buf;
  17875. cmd->next = buf->begin;
  17876. cmd = nk_ptr_add(struct nk_command, buffer, buf->last);
  17877. buf->active = nk_false;
  17878. skip: it = next;
  17879. }
  17880. if (cmd) {
  17881. /* append overlay commands */
  17882. if (ctx->overlay.end != ctx->overlay.begin)
  17883. cmd->next = ctx->overlay.begin;
  17884. else cmd->next = ctx->memory.allocated;
  17885. }
  17886. }
  17887. NK_API const struct nk_command*
  17888. nk__begin(struct nk_context* ctx)
  17889. {
  17890. struct nk_window* iter;
  17891. nk_byte* buffer;
  17892. NK_ASSERT(ctx);
  17893. if (!ctx) return 0;
  17894. if (!ctx->count) return 0;
  17895. buffer = (nk_byte*)ctx->memory.memory.ptr;
  17896. if (!ctx->build) {
  17897. nk_build(ctx);
  17898. ctx->build = nk_true;
  17899. }
  17900. iter = ctx->begin;
  17901. while (iter && ((iter->buffer.begin == iter->buffer.end) ||
  17902. (iter->flags & NK_WINDOW_HIDDEN) || iter->seq != ctx->seq))
  17903. iter = iter->next;
  17904. if (!iter) return 0;
  17905. return nk_ptr_add_const(struct nk_command, buffer, iter->buffer.begin);
  17906. }
  17907. NK_API const struct nk_command*
  17908. nk__next(struct nk_context* ctx, const struct nk_command* cmd)
  17909. {
  17910. nk_byte* buffer;
  17911. const struct nk_command* next;
  17912. NK_ASSERT(ctx);
  17913. if (!ctx || !cmd || !ctx->count) return 0;
  17914. if (cmd->next >= ctx->memory.allocated) return 0;
  17915. buffer = (nk_byte*)ctx->memory.memory.ptr;
  17916. next = nk_ptr_add_const(struct nk_command, buffer, cmd->next);
  17917. return next;
  17918. }
  17919. /* ===============================================================
  17920. *
  17921. * POOL
  17922. *
  17923. * ===============================================================*/
  17924. NK_LIB void
  17925. nk_pool_init(struct nk_pool* pool, struct nk_allocator* alloc,
  17926. unsigned int capacity)
  17927. {
  17928. NK_ASSERT(capacity >= 1);
  17929. nk_zero(pool, sizeof(*pool));
  17930. pool->alloc = *alloc;
  17931. pool->capacity = capacity;
  17932. pool->type = NK_BUFFER_DYNAMIC;
  17933. pool->pages = 0;
  17934. }
  17935. NK_LIB void
  17936. nk_pool_free(struct nk_pool* pool)
  17937. {
  17938. struct nk_page* iter;
  17939. if (!pool) return;
  17940. iter = pool->pages;
  17941. if (pool->type == NK_BUFFER_FIXED) return;
  17942. while (iter) {
  17943. struct nk_page* next = iter->next;
  17944. pool->alloc.free(pool->alloc.userdata, iter);
  17945. iter = next;
  17946. }
  17947. }
  17948. NK_LIB void
  17949. nk_pool_init_fixed(struct nk_pool* pool, void* memory, nk_size size)
  17950. {
  17951. nk_zero(pool, sizeof(*pool));
  17952. NK_ASSERT(size >= sizeof(struct nk_page));
  17953. if (size < sizeof(struct nk_page)) return;
  17954. /* first nk_page_element is embedded in nk_page, additional elements follow in adjacent space */
  17955. pool->capacity = (unsigned)(1 + (size - sizeof(struct nk_page)) / sizeof(struct nk_page_element));
  17956. pool->pages = (struct nk_page*)memory;
  17957. pool->type = NK_BUFFER_FIXED;
  17958. pool->size = size;
  17959. }
  17960. NK_LIB struct nk_page_element*
  17961. nk_pool_alloc(struct nk_pool* pool)
  17962. {
  17963. if (!pool->pages || pool->pages->size >= pool->capacity) {
  17964. /* allocate new page */
  17965. struct nk_page* page;
  17966. if (pool->type == NK_BUFFER_FIXED) {
  17967. NK_ASSERT(pool->pages);
  17968. if (!pool->pages) return 0;
  17969. NK_ASSERT(pool->pages->size < pool->capacity);
  17970. return 0;
  17971. }
  17972. else {
  17973. nk_size size = sizeof(struct nk_page);
  17974. size += (pool->capacity - 1) * sizeof(struct nk_page_element);
  17975. page = (struct nk_page*)pool->alloc.alloc(pool->alloc.userdata, 0, size);
  17976. page->next = pool->pages;
  17977. pool->pages = page;
  17978. page->size = 0;
  17979. }
  17980. } return &pool->pages->win[pool->pages->size++];
  17981. }
  17982. /* ===============================================================
  17983. *
  17984. * PAGE ELEMENT
  17985. *
  17986. * ===============================================================*/
  17987. NK_LIB struct nk_page_element*
  17988. nk_create_page_element(struct nk_context* ctx)
  17989. {
  17990. struct nk_page_element* elem;
  17991. if (ctx->freelist) {
  17992. /* unlink page element from free list */
  17993. elem = ctx->freelist;
  17994. ctx->freelist = elem->next;
  17995. }
  17996. else if (ctx->use_pool) {
  17997. /* allocate page element from memory pool */
  17998. elem = nk_pool_alloc(&ctx->pool);
  17999. NK_ASSERT(elem);
  18000. if (!elem) return 0;
  18001. }
  18002. else {
  18003. /* allocate new page element from back of fixed size memory buffer */
  18004. NK_STORAGE const nk_size size = sizeof(struct nk_page_element);
  18005. NK_STORAGE const nk_size align = NK_ALIGNOF(struct nk_page_element);
  18006. elem = (struct nk_page_element*)nk_buffer_alloc(&ctx->memory, NK_BUFFER_BACK, size, align);
  18007. NK_ASSERT(elem);
  18008. if (!elem) return 0;
  18009. }
  18010. nk_zero_struct(*elem);
  18011. elem->next = 0;
  18012. elem->prev = 0;
  18013. return elem;
  18014. }
  18015. NK_LIB void
  18016. nk_link_page_element_into_freelist(struct nk_context* ctx,
  18017. struct nk_page_element* elem)
  18018. {
  18019. /* link table into freelist */
  18020. if (!ctx->freelist) {
  18021. ctx->freelist = elem;
  18022. }
  18023. else {
  18024. elem->next = ctx->freelist;
  18025. ctx->freelist = elem;
  18026. }
  18027. }
  18028. NK_LIB void
  18029. nk_free_page_element(struct nk_context* ctx, struct nk_page_element* elem)
  18030. {
  18031. /* we have a pool so just add to free list */
  18032. if (ctx->use_pool) {
  18033. nk_link_page_element_into_freelist(ctx, elem);
  18034. return;
  18035. }
  18036. /* if possible remove last element from back of fixed memory buffer */
  18037. {void* elem_end = (void*)(elem + 1);
  18038. void* buffer_end = (nk_byte*)ctx->memory.memory.ptr + ctx->memory.size;
  18039. if (elem_end == buffer_end)
  18040. ctx->memory.size -= sizeof(struct nk_page_element);
  18041. else nk_link_page_element_into_freelist(ctx, elem); }
  18042. }
  18043. /* ===============================================================
  18044. *
  18045. * TABLE
  18046. *
  18047. * ===============================================================*/
  18048. NK_LIB struct nk_table*
  18049. nk_create_table(struct nk_context* ctx)
  18050. {
  18051. struct nk_page_element* elem;
  18052. elem = nk_create_page_element(ctx);
  18053. if (!elem) return 0;
  18054. nk_zero_struct(*elem);
  18055. return &elem->data.tbl;
  18056. }
  18057. NK_LIB void
  18058. nk_free_table(struct nk_context* ctx, struct nk_table* tbl)
  18059. {
  18060. union nk_page_data* pd = NK_CONTAINER_OF(tbl, union nk_page_data, tbl);
  18061. struct nk_page_element* pe = NK_CONTAINER_OF(pd, struct nk_page_element, data);
  18062. nk_free_page_element(ctx, pe);
  18063. }
  18064. NK_LIB void
  18065. nk_push_table(struct nk_window* win, struct nk_table* tbl)
  18066. {
  18067. if (!win->tables) {
  18068. win->tables = tbl;
  18069. tbl->next = 0;
  18070. tbl->prev = 0;
  18071. tbl->size = 0;
  18072. win->table_count = 1;
  18073. return;
  18074. }
  18075. win->tables->prev = tbl;
  18076. tbl->next = win->tables;
  18077. tbl->prev = 0;
  18078. tbl->size = 0;
  18079. win->tables = tbl;
  18080. win->table_count++;
  18081. }
  18082. NK_LIB void
  18083. nk_remove_table(struct nk_window* win, struct nk_table* tbl)
  18084. {
  18085. if (win->tables == tbl)
  18086. win->tables = tbl->next;
  18087. if (tbl->next)
  18088. tbl->next->prev = tbl->prev;
  18089. if (tbl->prev)
  18090. tbl->prev->next = tbl->next;
  18091. tbl->next = 0;
  18092. tbl->prev = 0;
  18093. }
  18094. NK_LIB nk_uint*
  18095. nk_add_value(struct nk_context* ctx, struct nk_window* win,
  18096. nk_hash name, nk_uint value)
  18097. {
  18098. NK_ASSERT(ctx);
  18099. NK_ASSERT(win);
  18100. if (!win || !ctx) return 0;
  18101. if (!win->tables || win->tables->size >= NK_VALUE_PAGE_CAPACITY) {
  18102. struct nk_table* tbl = nk_create_table(ctx);
  18103. NK_ASSERT(tbl);
  18104. if (!tbl) return 0;
  18105. nk_push_table(win, tbl);
  18106. }
  18107. win->tables->seq = win->seq;
  18108. win->tables->keys[win->tables->size] = name;
  18109. win->tables->values[win->tables->size] = value;
  18110. return &win->tables->values[win->tables->size++];
  18111. }
  18112. NK_LIB nk_uint*
  18113. nk_find_value(struct nk_window* win, nk_hash name)
  18114. {
  18115. struct nk_table* iter = win->tables;
  18116. while (iter) {
  18117. unsigned int i = 0;
  18118. unsigned int size = iter->size;
  18119. for (i = 0; i < size; ++i) {
  18120. if (iter->keys[i] == name) {
  18121. iter->seq = win->seq;
  18122. return &iter->values[i];
  18123. }
  18124. } size = NK_VALUE_PAGE_CAPACITY;
  18125. iter = iter->next;
  18126. }
  18127. return 0;
  18128. }
  18129. /* ===============================================================
  18130. *
  18131. * PANEL
  18132. *
  18133. * ===============================================================*/
  18134. NK_LIB void*
  18135. nk_create_panel(struct nk_context* ctx)
  18136. {
  18137. struct nk_page_element* elem;
  18138. elem = nk_create_page_element(ctx);
  18139. if (!elem) return 0;
  18140. nk_zero_struct(*elem);
  18141. return &elem->data.pan;
  18142. }
  18143. NK_LIB void
  18144. nk_free_panel(struct nk_context* ctx, struct nk_panel* pan)
  18145. {
  18146. union nk_page_data* pd = NK_CONTAINER_OF(pan, union nk_page_data, pan);
  18147. struct nk_page_element* pe = NK_CONTAINER_OF(pd, struct nk_page_element, data);
  18148. nk_free_page_element(ctx, pe);
  18149. }
  18150. NK_LIB nk_bool
  18151. nk_panel_has_header(nk_flags flags, const char* title)
  18152. {
  18153. nk_bool active = 0;
  18154. active = (flags & (NK_WINDOW_CLOSABLE | NK_WINDOW_MINIMIZABLE));
  18155. active = active || (flags & NK_WINDOW_TITLE);
  18156. active = active && !(flags & NK_WINDOW_HIDDEN) && title;
  18157. return active;
  18158. }
  18159. NK_LIB struct nk_vec2
  18160. nk_panel_get_padding(const struct nk_style* style, enum nk_panel_type type)
  18161. {
  18162. switch (type) {
  18163. default:
  18164. case NK_PANEL_WINDOW: return style->window.padding;
  18165. case NK_PANEL_GROUP: return style->window.group_padding;
  18166. case NK_PANEL_POPUP: return style->window.popup_padding;
  18167. case NK_PANEL_CONTEXTUAL: return style->window.contextual_padding;
  18168. case NK_PANEL_COMBO: return style->window.combo_padding;
  18169. case NK_PANEL_MENU: return style->window.menu_padding;
  18170. case NK_PANEL_TOOLTIP: return style->window.menu_padding;
  18171. }
  18172. }
  18173. NK_LIB float
  18174. nk_panel_get_border(const struct nk_style* style, nk_flags flags,
  18175. enum nk_panel_type type)
  18176. {
  18177. if (flags & NK_WINDOW_BORDER) {
  18178. switch (type) {
  18179. default:
  18180. case NK_PANEL_WINDOW: return style->window.border;
  18181. case NK_PANEL_GROUP: return style->window.group_border;
  18182. case NK_PANEL_POPUP: return style->window.popup_border;
  18183. case NK_PANEL_CONTEXTUAL: return style->window.contextual_border;
  18184. case NK_PANEL_COMBO: return style->window.combo_border;
  18185. case NK_PANEL_MENU: return style->window.menu_border;
  18186. case NK_PANEL_TOOLTIP: return style->window.menu_border;
  18187. }
  18188. }
  18189. else return 0;
  18190. }
  18191. NK_LIB struct nk_color
  18192. nk_panel_get_border_color(const struct nk_style* style, enum nk_panel_type type)
  18193. {
  18194. switch (type) {
  18195. default:
  18196. case NK_PANEL_WINDOW: return style->window.border_color;
  18197. case NK_PANEL_GROUP: return style->window.group_border_color;
  18198. case NK_PANEL_POPUP: return style->window.popup_border_color;
  18199. case NK_PANEL_CONTEXTUAL: return style->window.contextual_border_color;
  18200. case NK_PANEL_COMBO: return style->window.combo_border_color;
  18201. case NK_PANEL_MENU: return style->window.menu_border_color;
  18202. case NK_PANEL_TOOLTIP: return style->window.menu_border_color;
  18203. }
  18204. }
  18205. NK_LIB nk_bool
  18206. nk_panel_is_sub(enum nk_panel_type type)
  18207. {
  18208. return (type & NK_PANEL_SET_SUB) ? 1 : 0;
  18209. }
  18210. NK_LIB nk_bool
  18211. nk_panel_is_nonblock(enum nk_panel_type type)
  18212. {
  18213. return (type & NK_PANEL_SET_NONBLOCK) ? 1 : 0;
  18214. }
  18215. NK_LIB nk_bool
  18216. nk_panel_begin(struct nk_context* ctx, const char* title, enum nk_panel_type panel_type)
  18217. {
  18218. struct nk_input* in;
  18219. struct nk_window* win;
  18220. struct nk_panel* layout;
  18221. struct nk_command_buffer* out;
  18222. const struct nk_style* style;
  18223. const struct nk_user_font* font;
  18224. struct nk_vec2 scrollbar_size;
  18225. struct nk_vec2 panel_padding;
  18226. NK_ASSERT(ctx);
  18227. NK_ASSERT(ctx->current);
  18228. NK_ASSERT(ctx->current->layout);
  18229. if (!ctx || !ctx->current || !ctx->current->layout) return 0;
  18230. nk_zero(ctx->current->layout, sizeof(*ctx->current->layout));
  18231. if ((ctx->current->flags & NK_WINDOW_HIDDEN) || (ctx->current->flags & NK_WINDOW_CLOSED)) {
  18232. nk_zero(ctx->current->layout, sizeof(struct nk_panel));
  18233. ctx->current->layout->type = panel_type;
  18234. return 0;
  18235. }
  18236. /* pull state into local stack */
  18237. style = &ctx->style;
  18238. font = style->font;
  18239. win = ctx->current;
  18240. layout = win->layout;
  18241. out = &win->buffer;
  18242. in = (win->flags & NK_WINDOW_NO_INPUT) ? 0 : &ctx->input;
  18243. #ifdef NK_INCLUDE_COMMAND_USERDATA
  18244. win->buffer.userdata = ctx->userdata;
  18245. #endif
  18246. /* pull style configuration into local stack */
  18247. scrollbar_size = style->window.scrollbar_size;
  18248. panel_padding = nk_panel_get_padding(style, panel_type);
  18249. /* window movement */
  18250. if ((win->flags & NK_WINDOW_MOVABLE) && !(win->flags & NK_WINDOW_ROM)) {
  18251. nk_bool left_mouse_down;
  18252. unsigned int left_mouse_clicked;
  18253. int left_mouse_click_in_cursor;
  18254. /* calculate draggable window space */
  18255. struct nk_rect header;
  18256. header.x = win->bounds.x;
  18257. header.y = win->bounds.y;
  18258. header.w = win->bounds.w;
  18259. if (nk_panel_has_header(win->flags, title)) {
  18260. header.h = font->height + 2.0f * style->window.header.padding.y;
  18261. header.h += 2.0f * style->window.header.label_padding.y;
  18262. }
  18263. else header.h = panel_padding.y;
  18264. /* window movement by dragging */
  18265. left_mouse_down = in->mouse.buttons[NK_BUTTON_LEFT].down;
  18266. left_mouse_clicked = in->mouse.buttons[NK_BUTTON_LEFT].clicked;
  18267. left_mouse_click_in_cursor = nk_input_has_mouse_click_down_in_rect(in,
  18268. NK_BUTTON_LEFT, header, nk_true);
  18269. if (left_mouse_down && left_mouse_click_in_cursor && !left_mouse_clicked) {
  18270. win->bounds.x = win->bounds.x + in->mouse.delta.x;
  18271. win->bounds.y = win->bounds.y + in->mouse.delta.y;
  18272. in->mouse.buttons[NK_BUTTON_LEFT].clicked_pos.x += in->mouse.delta.x;
  18273. in->mouse.buttons[NK_BUTTON_LEFT].clicked_pos.y += in->mouse.delta.y;
  18274. ctx->style.cursor_active = ctx->style.cursors[NK_CURSOR_MOVE];
  18275. }
  18276. }
  18277. /* setup panel */
  18278. layout->type = panel_type;
  18279. layout->flags = win->flags;
  18280. layout->bounds = win->bounds;
  18281. layout->bounds.x += panel_padding.x;
  18282. layout->bounds.w -= 2 * panel_padding.x;
  18283. if (win->flags & NK_WINDOW_BORDER) {
  18284. layout->border = nk_panel_get_border(style, win->flags, panel_type);
  18285. layout->bounds = nk_shrink_rect(layout->bounds, layout->border);
  18286. }
  18287. else layout->border = 0;
  18288. layout->at_y = layout->bounds.y;
  18289. layout->at_x = layout->bounds.x;
  18290. layout->max_x = 0;
  18291. layout->header_height = 0;
  18292. layout->footer_height = 0;
  18293. nk_layout_reset_min_row_height(ctx);
  18294. layout->row.index = 0;
  18295. layout->row.columns = 0;
  18296. layout->row.ratio = 0;
  18297. layout->row.item_width = 0;
  18298. layout->row.tree_depth = 0;
  18299. layout->row.height = panel_padding.y;
  18300. layout->has_scrolling = nk_true;
  18301. if (!(win->flags & NK_WINDOW_NO_SCROLLBAR))
  18302. layout->bounds.w -= scrollbar_size.x;
  18303. if (!nk_panel_is_nonblock(panel_type)) {
  18304. layout->footer_height = 0;
  18305. if (!(win->flags & NK_WINDOW_NO_SCROLLBAR) || win->flags & NK_WINDOW_SCALABLE)
  18306. layout->footer_height = scrollbar_size.y;
  18307. layout->bounds.h -= layout->footer_height;
  18308. }
  18309. /* panel header */
  18310. if (nk_panel_has_header(win->flags, title))
  18311. {
  18312. struct nk_text text;
  18313. struct nk_rect header;
  18314. const struct nk_style_item* background = 0;
  18315. /* calculate header bounds */
  18316. header.x = win->bounds.x;
  18317. header.y = win->bounds.y;
  18318. header.w = win->bounds.w;
  18319. header.h = font->height + 2.0f * style->window.header.padding.y;
  18320. header.h += (2.0f * style->window.header.label_padding.y);
  18321. /* shrink panel by header */
  18322. layout->header_height = header.h;
  18323. layout->bounds.y += header.h;
  18324. layout->bounds.h -= header.h;
  18325. layout->at_y += header.h;
  18326. /* select correct header background and text color */
  18327. if (ctx->active == win) {
  18328. background = &style->window.header.active;
  18329. text.text = style->window.header.label_active;
  18330. }
  18331. else if (nk_input_is_mouse_hovering_rect(&ctx->input, header)) {
  18332. background = &style->window.header.hover;
  18333. text.text = style->window.header.label_hover;
  18334. }
  18335. else {
  18336. background = &style->window.header.normal;
  18337. text.text = style->window.header.label_normal;
  18338. }
  18339. /* draw header background */
  18340. header.h += 1.0f;
  18341. switch (background->type) {
  18342. case NK_STYLE_ITEM_IMAGE:
  18343. text.background = nk_rgba(0, 0, 0, 0);
  18344. nk_draw_image(&win->buffer, header, &background->data.image, nk_white);
  18345. break;
  18346. case NK_STYLE_ITEM_NINE_SLICE:
  18347. text.background = nk_rgba(0, 0, 0, 0);
  18348. nk_draw_nine_slice(&win->buffer, header, &background->data.slice, nk_white);
  18349. break;
  18350. case NK_STYLE_ITEM_COLOR:
  18351. text.background = background->data.color;
  18352. nk_fill_rect(out, header, 0, background->data.color);
  18353. break;
  18354. }
  18355. /* window close button */
  18356. {struct nk_rect button;
  18357. button.y = header.y + style->window.header.padding.y;
  18358. button.h = header.h - 2 * style->window.header.padding.y;
  18359. button.w = button.h;
  18360. if (win->flags & NK_WINDOW_CLOSABLE) {
  18361. nk_flags ws = 0;
  18362. if (style->window.header.align == NK_HEADER_RIGHT) {
  18363. button.x = (header.w + header.x) - (button.w + style->window.header.padding.x);
  18364. header.w -= button.w + style->window.header.spacing.x + style->window.header.padding.x;
  18365. }
  18366. else {
  18367. button.x = header.x + style->window.header.padding.x;
  18368. header.x += button.w + style->window.header.spacing.x + style->window.header.padding.x;
  18369. }
  18370. if (nk_do_button_symbol(&ws, &win->buffer, button,
  18371. style->window.header.close_symbol, NK_BUTTON_DEFAULT,
  18372. &style->window.header.close_button, in, style->font) && !(win->flags & NK_WINDOW_ROM))
  18373. {
  18374. layout->flags |= NK_WINDOW_HIDDEN;
  18375. layout->flags &= (nk_flags)~NK_WINDOW_MINIMIZED;
  18376. }
  18377. }
  18378. /* window minimize button */
  18379. if (win->flags & NK_WINDOW_MINIMIZABLE) {
  18380. nk_flags ws = 0;
  18381. if (style->window.header.align == NK_HEADER_RIGHT) {
  18382. button.x = (header.w + header.x) - button.w;
  18383. if (!(win->flags & NK_WINDOW_CLOSABLE)) {
  18384. button.x -= style->window.header.padding.x;
  18385. header.w -= style->window.header.padding.x;
  18386. }
  18387. header.w -= button.w + style->window.header.spacing.x;
  18388. }
  18389. else {
  18390. button.x = header.x;
  18391. header.x += button.w + style->window.header.spacing.x + style->window.header.padding.x;
  18392. }
  18393. if (nk_do_button_symbol(&ws, &win->buffer, button, (layout->flags & NK_WINDOW_MINIMIZED) ?
  18394. style->window.header.maximize_symbol : style->window.header.minimize_symbol,
  18395. NK_BUTTON_DEFAULT, &style->window.header.minimize_button, in, style->font) && !(win->flags & NK_WINDOW_ROM))
  18396. layout->flags = (layout->flags & NK_WINDOW_MINIMIZED) ?
  18397. layout->flags & (nk_flags)~NK_WINDOW_MINIMIZED :
  18398. layout->flags | NK_WINDOW_MINIMIZED;
  18399. }}
  18400. {/* window header title */
  18401. int text_len = nk_strlen(title);
  18402. struct nk_rect label = { 0,0,0,0 };
  18403. float t = font->width(font->userdata, font->height, title, text_len);
  18404. text.padding = nk_vec2(0, 0);
  18405. label.x = header.x + style->window.header.padding.x;
  18406. label.x += style->window.header.label_padding.x;
  18407. label.y = header.y + style->window.header.label_padding.y;
  18408. label.h = font->height + 2 * style->window.header.label_padding.y;
  18409. label.w = t + 2 * style->window.header.spacing.x;
  18410. label.w = NK_CLAMP(0, label.w, header.x + header.w - label.x);
  18411. nk_widget_text(out, label, (const char*)title, text_len, &text, NK_TEXT_LEFT, font); }
  18412. }
  18413. /* draw window background */
  18414. if (!(layout->flags & NK_WINDOW_MINIMIZED) && !(layout->flags & NK_WINDOW_DYNAMIC)) {
  18415. struct nk_rect body;
  18416. body.x = win->bounds.x;
  18417. body.w = win->bounds.w;
  18418. body.y = (win->bounds.y + layout->header_height);
  18419. body.h = (win->bounds.h - layout->header_height);
  18420. switch (style->window.fixed_background.type) {
  18421. case NK_STYLE_ITEM_IMAGE:
  18422. nk_draw_image(out, body, &style->window.fixed_background.data.image, nk_white);
  18423. break;
  18424. case NK_STYLE_ITEM_NINE_SLICE:
  18425. nk_draw_nine_slice(out, body, &style->window.fixed_background.data.slice, nk_white);
  18426. break;
  18427. case NK_STYLE_ITEM_COLOR:
  18428. nk_fill_rect(out, body, 0, style->window.fixed_background.data.color);
  18429. break;
  18430. }
  18431. }
  18432. /* set clipping rectangle */
  18433. {struct nk_rect clip;
  18434. layout->clip = layout->bounds;
  18435. nk_unify(&clip, &win->buffer.clip, layout->clip.x, layout->clip.y,
  18436. layout->clip.x + layout->clip.w, layout->clip.y + layout->clip.h);
  18437. nk_push_scissor(out, clip);
  18438. layout->clip = clip; }
  18439. return !(layout->flags & NK_WINDOW_HIDDEN) && !(layout->flags & NK_WINDOW_MINIMIZED);
  18440. }
  18441. NK_LIB void
  18442. nk_panel_end(struct nk_context* ctx)
  18443. {
  18444. struct nk_input* in;
  18445. struct nk_window* window;
  18446. struct nk_panel* layout;
  18447. const struct nk_style* style;
  18448. struct nk_command_buffer* out;
  18449. struct nk_vec2 scrollbar_size;
  18450. struct nk_vec2 panel_padding;
  18451. NK_ASSERT(ctx);
  18452. NK_ASSERT(ctx->current);
  18453. NK_ASSERT(ctx->current->layout);
  18454. if (!ctx || !ctx->current || !ctx->current->layout)
  18455. return;
  18456. window = ctx->current;
  18457. layout = window->layout;
  18458. style = &ctx->style;
  18459. out = &window->buffer;
  18460. in = (layout->flags & NK_WINDOW_ROM || layout->flags & NK_WINDOW_NO_INPUT) ? 0 : &ctx->input;
  18461. if (!nk_panel_is_sub(layout->type))
  18462. nk_push_scissor(out, nk_null_rect);
  18463. /* cache configuration data */
  18464. scrollbar_size = style->window.scrollbar_size;
  18465. panel_padding = nk_panel_get_padding(style, layout->type);
  18466. /* update the current cursor Y-position to point over the last added widget */
  18467. layout->at_y += layout->row.height;
  18468. /* dynamic panels */
  18469. if (layout->flags & NK_WINDOW_DYNAMIC && !(layout->flags & NK_WINDOW_MINIMIZED))
  18470. {
  18471. /* update panel height to fit dynamic growth */
  18472. struct nk_rect empty_space;
  18473. if (layout->at_y < (layout->bounds.y + layout->bounds.h))
  18474. layout->bounds.h = layout->at_y - layout->bounds.y;
  18475. /* fill top empty space */
  18476. empty_space.x = window->bounds.x;
  18477. empty_space.y = layout->bounds.y;
  18478. empty_space.h = panel_padding.y;
  18479. empty_space.w = window->bounds.w;
  18480. nk_fill_rect(out, empty_space, 0, style->window.background);
  18481. /* fill left empty space */
  18482. empty_space.x = window->bounds.x;
  18483. empty_space.y = layout->bounds.y;
  18484. empty_space.w = panel_padding.x + layout->border;
  18485. empty_space.h = layout->bounds.h;
  18486. nk_fill_rect(out, empty_space, 0, style->window.background);
  18487. /* fill right empty space */
  18488. empty_space.x = layout->bounds.x + layout->bounds.w;
  18489. empty_space.y = layout->bounds.y;
  18490. empty_space.w = panel_padding.x + layout->border;
  18491. empty_space.h = layout->bounds.h;
  18492. if (*layout->offset_y == 0 && !(layout->flags & NK_WINDOW_NO_SCROLLBAR))
  18493. empty_space.w += scrollbar_size.x;
  18494. nk_fill_rect(out, empty_space, 0, style->window.background);
  18495. /* fill bottom empty space */
  18496. if (layout->footer_height > 0) {
  18497. empty_space.x = window->bounds.x;
  18498. empty_space.y = layout->bounds.y + layout->bounds.h;
  18499. empty_space.w = window->bounds.w;
  18500. empty_space.h = layout->footer_height;
  18501. nk_fill_rect(out, empty_space, 0, style->window.background);
  18502. }
  18503. }
  18504. /* scrollbars */
  18505. if (!(layout->flags & NK_WINDOW_NO_SCROLLBAR) &&
  18506. !(layout->flags & NK_WINDOW_MINIMIZED) &&
  18507. window->scrollbar_hiding_timer < NK_SCROLLBAR_HIDING_TIMEOUT)
  18508. {
  18509. struct nk_rect scroll;
  18510. int scroll_has_scrolling;
  18511. float scroll_target;
  18512. float scroll_offset;
  18513. float scroll_step;
  18514. float scroll_inc;
  18515. /* mouse wheel scrolling */
  18516. if (nk_panel_is_sub(layout->type))
  18517. {
  18518. /* sub-window mouse wheel scrolling */
  18519. struct nk_window* root_window = window;
  18520. struct nk_panel* root_panel = window->layout;
  18521. while (root_panel->parent)
  18522. root_panel = root_panel->parent;
  18523. while (root_window->parent)
  18524. root_window = root_window->parent;
  18525. /* only allow scrolling if parent window is active */
  18526. scroll_has_scrolling = 0;
  18527. if ((root_window == ctx->active) && layout->has_scrolling) {
  18528. /* and panel is being hovered and inside clip rect*/
  18529. if (nk_input_is_mouse_hovering_rect(in, layout->bounds) &&
  18530. NK_INTERSECT(layout->bounds.x, layout->bounds.y, layout->bounds.w, layout->bounds.h,
  18531. root_panel->clip.x, root_panel->clip.y, root_panel->clip.w, root_panel->clip.h))
  18532. {
  18533. /* deactivate all parent scrolling */
  18534. root_panel = window->layout;
  18535. while (root_panel->parent) {
  18536. root_panel->has_scrolling = nk_false;
  18537. root_panel = root_panel->parent;
  18538. }
  18539. root_panel->has_scrolling = nk_false;
  18540. scroll_has_scrolling = nk_true;
  18541. }
  18542. }
  18543. }
  18544. else if (!nk_panel_is_sub(layout->type)) {
  18545. /* window mouse wheel scrolling */
  18546. scroll_has_scrolling = (window == ctx->active) && layout->has_scrolling;
  18547. if (in && (in->mouse.scroll_delta.y > 0 || in->mouse.scroll_delta.x > 0) && scroll_has_scrolling)
  18548. window->scrolled = nk_true;
  18549. else window->scrolled = nk_false;
  18550. }
  18551. else scroll_has_scrolling = nk_false;
  18552. {
  18553. /* vertical scrollbar */
  18554. nk_flags state = 0;
  18555. scroll.x = layout->bounds.x + layout->bounds.w + panel_padding.x;
  18556. scroll.y = layout->bounds.y;
  18557. scroll.w = scrollbar_size.x;
  18558. scroll.h = layout->bounds.h;
  18559. scroll_offset = (float)*layout->offset_y;
  18560. scroll_step = scroll.h * 0.10f;
  18561. scroll_inc = scroll.h * 0.01f;
  18562. scroll_target = (float)(int)(layout->at_y - scroll.y);
  18563. scroll_offset = nk_do_scrollbarv(&state, out, scroll, scroll_has_scrolling,
  18564. scroll_offset, scroll_target, scroll_step, scroll_inc,
  18565. &ctx->style.scrollv, in, style->font);
  18566. *layout->offset_y = (nk_uint)scroll_offset;
  18567. if (in && scroll_has_scrolling)
  18568. in->mouse.scroll_delta.y = 0;
  18569. }
  18570. {
  18571. /* horizontal scrollbar */
  18572. nk_flags state = 0;
  18573. scroll.x = layout->bounds.x;
  18574. scroll.y = layout->bounds.y + layout->bounds.h;
  18575. scroll.w = layout->bounds.w;
  18576. scroll.h = scrollbar_size.y;
  18577. scroll_offset = (float)*layout->offset_x;
  18578. scroll_target = (float)(int)(layout->max_x - scroll.x);
  18579. scroll_step = layout->max_x * 0.05f;
  18580. scroll_inc = layout->max_x * 0.005f;
  18581. scroll_offset = nk_do_scrollbarh(&state, out, scroll, scroll_has_scrolling,
  18582. scroll_offset, scroll_target, scroll_step, scroll_inc,
  18583. &ctx->style.scrollh, in, style->font);
  18584. *layout->offset_x = (nk_uint)scroll_offset;
  18585. }
  18586. }
  18587. /* hide scroll if no user input */
  18588. if (window->flags & NK_WINDOW_SCROLL_AUTO_HIDE) {
  18589. int has_input = ctx->input.mouse.delta.x != 0 || ctx->input.mouse.delta.y != 0 || ctx->input.mouse.scroll_delta.y != 0;
  18590. int is_window_hovered = nk_window_is_hovered(ctx);
  18591. int any_item_active = (ctx->last_widget_state & NK_WIDGET_STATE_MODIFIED);
  18592. if ((!has_input && is_window_hovered) || (!is_window_hovered && !any_item_active))
  18593. window->scrollbar_hiding_timer += ctx->delta_time_seconds;
  18594. else window->scrollbar_hiding_timer = 0;
  18595. }
  18596. else window->scrollbar_hiding_timer = 0;
  18597. /* window border */
  18598. if (layout->flags & NK_WINDOW_BORDER)
  18599. {
  18600. struct nk_color border_color = nk_panel_get_border_color(style, layout->type);
  18601. const float padding_y = (layout->flags & NK_WINDOW_MINIMIZED)
  18602. ? (style->window.border + window->bounds.y + layout->header_height)
  18603. : ((layout->flags & NK_WINDOW_DYNAMIC)
  18604. ? (layout->bounds.y + layout->bounds.h + layout->footer_height)
  18605. : (window->bounds.y + window->bounds.h));
  18606. struct nk_rect b = window->bounds;
  18607. b.h = padding_y - window->bounds.y;
  18608. nk_stroke_rect(out, b, 0, layout->border, border_color);
  18609. }
  18610. /* scaler */
  18611. if ((layout->flags & NK_WINDOW_SCALABLE) && in && !(layout->flags & NK_WINDOW_MINIMIZED))
  18612. {
  18613. /* calculate scaler bounds */
  18614. struct nk_rect scaler;
  18615. scaler.w = scrollbar_size.x;
  18616. scaler.h = scrollbar_size.y;
  18617. scaler.y = layout->bounds.y + layout->bounds.h;
  18618. if (layout->flags & NK_WINDOW_SCALE_LEFT)
  18619. scaler.x = layout->bounds.x - panel_padding.x * 0.5f;
  18620. else scaler.x = layout->bounds.x + layout->bounds.w + panel_padding.x;
  18621. if (layout->flags & NK_WINDOW_NO_SCROLLBAR)
  18622. scaler.x -= scaler.w;
  18623. /* draw scaler */
  18624. {const struct nk_style_item* item = &style->window.scaler;
  18625. if (item->type == NK_STYLE_ITEM_IMAGE)
  18626. nk_draw_image(out, scaler, &item->data.image, nk_white);
  18627. else {
  18628. if (layout->flags & NK_WINDOW_SCALE_LEFT) {
  18629. nk_fill_triangle(out, scaler.x, scaler.y, scaler.x,
  18630. scaler.y + scaler.h, scaler.x + scaler.w,
  18631. scaler.y + scaler.h, item->data.color);
  18632. }
  18633. else {
  18634. nk_fill_triangle(out, scaler.x + scaler.w, scaler.y, scaler.x + scaler.w,
  18635. scaler.y + scaler.h, scaler.x, scaler.y + scaler.h, item->data.color);
  18636. }
  18637. }}
  18638. /* do window scaling */
  18639. if (!(window->flags & NK_WINDOW_ROM)) {
  18640. struct nk_vec2 window_size = style->window.min_size;
  18641. int left_mouse_down = in->mouse.buttons[NK_BUTTON_LEFT].down;
  18642. int left_mouse_click_in_scaler = nk_input_has_mouse_click_down_in_rect(in,
  18643. NK_BUTTON_LEFT, scaler, nk_true);
  18644. if (left_mouse_down && left_mouse_click_in_scaler) {
  18645. float delta_x = in->mouse.delta.x;
  18646. if (layout->flags & NK_WINDOW_SCALE_LEFT) {
  18647. delta_x = -delta_x;
  18648. window->bounds.x += in->mouse.delta.x;
  18649. }
  18650. /* dragging in x-direction */
  18651. if (window->bounds.w + delta_x >= window_size.x) {
  18652. if ((delta_x < 0) || (delta_x > 0 && in->mouse.pos.x >= scaler.x)) {
  18653. window->bounds.w = window->bounds.w + delta_x;
  18654. scaler.x += in->mouse.delta.x;
  18655. }
  18656. }
  18657. /* dragging in y-direction (only possible if static window) */
  18658. if (!(layout->flags & NK_WINDOW_DYNAMIC)) {
  18659. if (window_size.y < window->bounds.h + in->mouse.delta.y) {
  18660. if ((in->mouse.delta.y < 0) || (in->mouse.delta.y > 0 && in->mouse.pos.y >= scaler.y)) {
  18661. window->bounds.h = window->bounds.h + in->mouse.delta.y;
  18662. scaler.y += in->mouse.delta.y;
  18663. }
  18664. }
  18665. }
  18666. ctx->style.cursor_active = ctx->style.cursors[NK_CURSOR_RESIZE_TOP_RIGHT_DOWN_LEFT];
  18667. in->mouse.buttons[NK_BUTTON_LEFT].clicked_pos.x = scaler.x + scaler.w / 2.0f;
  18668. in->mouse.buttons[NK_BUTTON_LEFT].clicked_pos.y = scaler.y + scaler.h / 2.0f;
  18669. }
  18670. }
  18671. }
  18672. if (!nk_panel_is_sub(layout->type)) {
  18673. /* window is hidden so clear command buffer */
  18674. if (layout->flags & NK_WINDOW_HIDDEN)
  18675. nk_command_buffer_reset(&window->buffer);
  18676. /* window is visible and not tab */
  18677. else nk_finish(ctx, window);
  18678. }
  18679. /* NK_WINDOW_REMOVE_ROM flag was set so remove NK_WINDOW_ROM */
  18680. if (layout->flags & NK_WINDOW_REMOVE_ROM) {
  18681. layout->flags &= ~(nk_flags)NK_WINDOW_ROM;
  18682. layout->flags &= ~(nk_flags)NK_WINDOW_REMOVE_ROM;
  18683. }
  18684. window->flags = layout->flags;
  18685. /* property garbage collector */
  18686. if (window->property.active && window->property.old != window->property.seq &&
  18687. window->property.active == window->property.prev) {
  18688. nk_zero(&window->property, sizeof(window->property));
  18689. }
  18690. else {
  18691. window->property.old = window->property.seq;
  18692. window->property.prev = window->property.active;
  18693. window->property.seq = 0;
  18694. }
  18695. /* edit garbage collector */
  18696. if (window->edit.active && window->edit.old != window->edit.seq &&
  18697. window->edit.active == window->edit.prev) {
  18698. nk_zero(&window->edit, sizeof(window->edit));
  18699. }
  18700. else {
  18701. window->edit.old = window->edit.seq;
  18702. window->edit.prev = window->edit.active;
  18703. window->edit.seq = 0;
  18704. }
  18705. /* contextual garbage collector */
  18706. if (window->popup.active_con && window->popup.con_old != window->popup.con_count) {
  18707. window->popup.con_count = 0;
  18708. window->popup.con_old = 0;
  18709. window->popup.active_con = 0;
  18710. }
  18711. else {
  18712. window->popup.con_old = window->popup.con_count;
  18713. window->popup.con_count = 0;
  18714. }
  18715. window->popup.combo_count = 0;
  18716. /* helper to make sure you have a 'nk_tree_push' for every 'nk_tree_pop' */
  18717. NK_ASSERT(!layout->row.tree_depth);
  18718. }
  18719. /* ===============================================================
  18720. *
  18721. * WINDOW
  18722. *
  18723. * ===============================================================*/
  18724. NK_LIB void*
  18725. nk_create_window(struct nk_context* ctx)
  18726. {
  18727. struct nk_page_element* elem;
  18728. elem = nk_create_page_element(ctx);
  18729. if (!elem) return 0;
  18730. elem->data.win.seq = ctx->seq;
  18731. return &elem->data.win;
  18732. }
  18733. NK_LIB void
  18734. nk_free_window(struct nk_context* ctx, struct nk_window* win)
  18735. {
  18736. /* unlink windows from list */
  18737. struct nk_table* it = win->tables;
  18738. if (win->popup.win) {
  18739. nk_free_window(ctx, win->popup.win);
  18740. win->popup.win = 0;
  18741. }
  18742. win->next = 0;
  18743. win->prev = 0;
  18744. while (it) {
  18745. /*free window state tables */
  18746. struct nk_table* n = it->next;
  18747. nk_remove_table(win, it);
  18748. nk_free_table(ctx, it);
  18749. if (it == win->tables)
  18750. win->tables = n;
  18751. it = n;
  18752. }
  18753. /* link windows into freelist */
  18754. {union nk_page_data* pd = NK_CONTAINER_OF(win, union nk_page_data, win);
  18755. struct nk_page_element* pe = NK_CONTAINER_OF(pd, struct nk_page_element, data);
  18756. nk_free_page_element(ctx, pe); }
  18757. }
  18758. NK_LIB struct nk_window*
  18759. nk_find_window(struct nk_context* ctx, nk_hash hash, const char* name)
  18760. {
  18761. struct nk_window* iter;
  18762. iter = ctx->begin;
  18763. while (iter) {
  18764. NK_ASSERT(iter != iter->next);
  18765. if (iter->name == hash) {
  18766. int max_len = nk_strlen(iter->name_string);
  18767. if (!nk_stricmpn(iter->name_string, name, max_len))
  18768. return iter;
  18769. }
  18770. iter = iter->next;
  18771. }
  18772. return 0;
  18773. }
  18774. NK_LIB void
  18775. nk_insert_window(struct nk_context* ctx, struct nk_window* win,
  18776. enum nk_window_insert_location loc)
  18777. {
  18778. const struct nk_window* iter;
  18779. NK_ASSERT(ctx);
  18780. NK_ASSERT(win);
  18781. if (!win || !ctx) return;
  18782. iter = ctx->begin;
  18783. while (iter) {
  18784. NK_ASSERT(iter != iter->next);
  18785. NK_ASSERT(iter != win);
  18786. if (iter == win) return;
  18787. iter = iter->next;
  18788. }
  18789. if (!ctx->begin) {
  18790. win->next = 0;
  18791. win->prev = 0;
  18792. ctx->begin = win;
  18793. ctx->end = win;
  18794. ctx->count = 1;
  18795. return;
  18796. }
  18797. if (loc == NK_INSERT_BACK) {
  18798. struct nk_window* end;
  18799. end = ctx->end;
  18800. end->flags |= NK_WINDOW_ROM;
  18801. end->next = win;
  18802. win->prev = ctx->end;
  18803. win->next = 0;
  18804. ctx->end = win;
  18805. ctx->active = ctx->end;
  18806. ctx->end->flags &= ~(nk_flags)NK_WINDOW_ROM;
  18807. }
  18808. else {
  18809. /*ctx->end->flags |= NK_WINDOW_ROM;*/
  18810. ctx->begin->prev = win;
  18811. win->next = ctx->begin;
  18812. win->prev = 0;
  18813. ctx->begin = win;
  18814. ctx->begin->flags &= ~(nk_flags)NK_WINDOW_ROM;
  18815. }
  18816. ctx->count++;
  18817. }
  18818. NK_LIB void
  18819. nk_remove_window(struct nk_context* ctx, struct nk_window* win)
  18820. {
  18821. if (win == ctx->begin || win == ctx->end) {
  18822. if (win == ctx->begin) {
  18823. ctx->begin = win->next;
  18824. if (win->next)
  18825. win->next->prev = 0;
  18826. }
  18827. if (win == ctx->end) {
  18828. ctx->end = win->prev;
  18829. if (win->prev)
  18830. win->prev->next = 0;
  18831. }
  18832. }
  18833. else {
  18834. if (win->next)
  18835. win->next->prev = win->prev;
  18836. if (win->prev)
  18837. win->prev->next = win->next;
  18838. }
  18839. if (win == ctx->active || !ctx->active) {
  18840. ctx->active = ctx->end;
  18841. if (ctx->end)
  18842. ctx->end->flags &= ~(nk_flags)NK_WINDOW_ROM;
  18843. }
  18844. win->next = 0;
  18845. win->prev = 0;
  18846. ctx->count--;
  18847. }
  18848. NK_API nk_bool
  18849. nk_begin(struct nk_context* ctx, const char* title,
  18850. struct nk_rect bounds, nk_flags flags)
  18851. {
  18852. return nk_begin_titled(ctx, title, title, bounds, flags);
  18853. }
  18854. NK_API nk_bool
  18855. nk_begin_titled(struct nk_context* ctx, const char* name, const char* title,
  18856. struct nk_rect bounds, nk_flags flags)
  18857. {
  18858. struct nk_window* win;
  18859. struct nk_style* style;
  18860. nk_hash name_hash;
  18861. int name_len;
  18862. int ret = 0;
  18863. NK_ASSERT(ctx);
  18864. NK_ASSERT(name);
  18865. NK_ASSERT(title);
  18866. NK_ASSERT(ctx->style.font && ctx->style.font->width && "if this triggers you forgot to add a font");
  18867. NK_ASSERT(!ctx->current && "if this triggers you missed a `nk_end` call");
  18868. if (!ctx || ctx->current || !title || !name)
  18869. return 0;
  18870. /* find or create window */
  18871. style = &ctx->style;
  18872. name_len = (int)nk_strlen(name);
  18873. name_hash = nk_murmur_hash(name, (int)name_len, NK_WINDOW_TITLE);
  18874. win = nk_find_window(ctx, name_hash, name);
  18875. if (!win) {
  18876. /* create new window */
  18877. nk_size name_length = (nk_size)name_len;
  18878. win = (struct nk_window*)nk_create_window(ctx);
  18879. NK_ASSERT(win);
  18880. if (!win) return 0;
  18881. if (flags & NK_WINDOW_BACKGROUND)
  18882. nk_insert_window(ctx, win, NK_INSERT_FRONT);
  18883. else nk_insert_window(ctx, win, NK_INSERT_BACK);
  18884. nk_command_buffer_init(&win->buffer, &ctx->memory, NK_CLIPPING_ON);
  18885. win->flags = flags;
  18886. win->bounds = bounds;
  18887. win->name = name_hash;
  18888. name_length = NK_MIN(name_length, NK_WINDOW_MAX_NAME - 1);
  18889. NK_MEMCPY(win->name_string, name, name_length);
  18890. win->name_string[name_length] = 0;
  18891. win->popup.win = 0;
  18892. if (!ctx->active)
  18893. ctx->active = win;
  18894. }
  18895. else {
  18896. /* update window */
  18897. win->flags &= ~(nk_flags)(NK_WINDOW_PRIVATE - 1);
  18898. win->flags |= flags;
  18899. if (!(win->flags & (NK_WINDOW_MOVABLE | NK_WINDOW_SCALABLE)))
  18900. win->bounds = bounds;
  18901. /* If this assert triggers you either:
  18902. *
  18903. * I.) Have more than one window with the same name or
  18904. * II.) You forgot to actually draw the window.
  18905. * More specific you did not call `nk_clear` (nk_clear will be
  18906. * automatically called for you if you are using one of the
  18907. * provided demo backends). */
  18908. NK_ASSERT(win->seq != ctx->seq);
  18909. win->seq = ctx->seq;
  18910. if (!ctx->active && !(win->flags & NK_WINDOW_HIDDEN)) {
  18911. ctx->active = win;
  18912. ctx->end = win;
  18913. }
  18914. }
  18915. if (win->flags & NK_WINDOW_HIDDEN) {
  18916. ctx->current = win;
  18917. win->layout = 0;
  18918. return 0;
  18919. }
  18920. else nk_start(ctx, win);
  18921. /* window overlapping */
  18922. if (!(win->flags & NK_WINDOW_HIDDEN) && !(win->flags & NK_WINDOW_NO_INPUT))
  18923. {
  18924. int inpanel, ishovered;
  18925. struct nk_window* iter = win;
  18926. float h = ctx->style.font->height + 2.0f * style->window.header.padding.y +
  18927. (2.0f * style->window.header.label_padding.y);
  18928. struct nk_rect win_bounds = (!(win->flags & NK_WINDOW_MINIMIZED)) ?
  18929. win->bounds : nk_rect(win->bounds.x, win->bounds.y, win->bounds.w, h);
  18930. /* activate window if hovered and no other window is overlapping this window */
  18931. inpanel = nk_input_has_mouse_click_down_in_rect(&ctx->input, NK_BUTTON_LEFT, win_bounds, nk_true);
  18932. inpanel = inpanel && ctx->input.mouse.buttons[NK_BUTTON_LEFT].clicked;
  18933. ishovered = nk_input_is_mouse_hovering_rect(&ctx->input, win_bounds);
  18934. if ((win != ctx->active) && ishovered && !ctx->input.mouse.buttons[NK_BUTTON_LEFT].down) {
  18935. iter = win->next;
  18936. while (iter) {
  18937. struct nk_rect iter_bounds = (!(iter->flags & NK_WINDOW_MINIMIZED)) ?
  18938. iter->bounds : nk_rect(iter->bounds.x, iter->bounds.y, iter->bounds.w, h);
  18939. if (NK_INTERSECT(win_bounds.x, win_bounds.y, win_bounds.w, win_bounds.h,
  18940. iter_bounds.x, iter_bounds.y, iter_bounds.w, iter_bounds.h) &&
  18941. (!(iter->flags & NK_WINDOW_HIDDEN)))
  18942. break;
  18943. if (iter->popup.win && iter->popup.active && !(iter->flags & NK_WINDOW_HIDDEN) &&
  18944. NK_INTERSECT(win->bounds.x, win_bounds.y, win_bounds.w, win_bounds.h,
  18945. iter->popup.win->bounds.x, iter->popup.win->bounds.y,
  18946. iter->popup.win->bounds.w, iter->popup.win->bounds.h))
  18947. break;
  18948. iter = iter->next;
  18949. }
  18950. }
  18951. /* activate window if clicked */
  18952. if (iter && inpanel && (win != ctx->end)) {
  18953. iter = win->next;
  18954. while (iter) {
  18955. /* try to find a panel with higher priority in the same position */
  18956. struct nk_rect iter_bounds = (!(iter->flags & NK_WINDOW_MINIMIZED)) ?
  18957. iter->bounds : nk_rect(iter->bounds.x, iter->bounds.y, iter->bounds.w, h);
  18958. if (NK_INBOX(ctx->input.mouse.pos.x, ctx->input.mouse.pos.y,
  18959. iter_bounds.x, iter_bounds.y, iter_bounds.w, iter_bounds.h) &&
  18960. !(iter->flags & NK_WINDOW_HIDDEN))
  18961. break;
  18962. if (iter->popup.win && iter->popup.active && !(iter->flags & NK_WINDOW_HIDDEN) &&
  18963. NK_INTERSECT(win_bounds.x, win_bounds.y, win_bounds.w, win_bounds.h,
  18964. iter->popup.win->bounds.x, iter->popup.win->bounds.y,
  18965. iter->popup.win->bounds.w, iter->popup.win->bounds.h))
  18966. break;
  18967. iter = iter->next;
  18968. }
  18969. }
  18970. if (iter && !(win->flags & NK_WINDOW_ROM) && (win->flags & NK_WINDOW_BACKGROUND)) {
  18971. win->flags |= (nk_flags)NK_WINDOW_ROM;
  18972. iter->flags &= ~(nk_flags)NK_WINDOW_ROM;
  18973. ctx->active = iter;
  18974. if (!(iter->flags & NK_WINDOW_BACKGROUND)) {
  18975. /* current window is active in that position so transfer to top
  18976. * at the highest priority in stack */
  18977. nk_remove_window(ctx, iter);
  18978. nk_insert_window(ctx, iter, NK_INSERT_BACK);
  18979. }
  18980. }
  18981. else {
  18982. if (!iter && ctx->end != win) {
  18983. if (!(win->flags & NK_WINDOW_BACKGROUND)) {
  18984. /* current window is active in that position so transfer to top
  18985. * at the highest priority in stack */
  18986. nk_remove_window(ctx, win);
  18987. nk_insert_window(ctx, win, NK_INSERT_BACK);
  18988. }
  18989. win->flags &= ~(nk_flags)NK_WINDOW_ROM;
  18990. ctx->active = win;
  18991. }
  18992. if (ctx->end != win && !(win->flags & NK_WINDOW_BACKGROUND))
  18993. win->flags |= NK_WINDOW_ROM;
  18994. }
  18995. }
  18996. win->layout = (struct nk_panel*)nk_create_panel(ctx);
  18997. ctx->current = win;
  18998. ret = nk_panel_begin(ctx, title, NK_PANEL_WINDOW);
  18999. win->layout->offset_x = &win->scrollbar.x;
  19000. win->layout->offset_y = &win->scrollbar.y;
  19001. return ret;
  19002. }
  19003. NK_API void
  19004. nk_end(struct nk_context* ctx)
  19005. {
  19006. struct nk_panel* layout;
  19007. NK_ASSERT(ctx);
  19008. NK_ASSERT(ctx->current && "if this triggers you forgot to call `nk_begin`");
  19009. if (!ctx || !ctx->current)
  19010. return;
  19011. layout = ctx->current->layout;
  19012. if (!layout || (layout->type == NK_PANEL_WINDOW && (ctx->current->flags & NK_WINDOW_HIDDEN))) {
  19013. ctx->current = 0;
  19014. return;
  19015. }
  19016. nk_panel_end(ctx);
  19017. nk_free_panel(ctx, ctx->current->layout);
  19018. ctx->current = 0;
  19019. }
  19020. NK_API struct nk_rect
  19021. nk_window_get_bounds(const struct nk_context* ctx)
  19022. {
  19023. NK_ASSERT(ctx);
  19024. NK_ASSERT(ctx->current);
  19025. if (!ctx || !ctx->current) return nk_rect(0, 0, 0, 0);
  19026. return ctx->current->bounds;
  19027. }
  19028. NK_API struct nk_vec2
  19029. nk_window_get_position(const struct nk_context* ctx)
  19030. {
  19031. NK_ASSERT(ctx);
  19032. NK_ASSERT(ctx->current);
  19033. if (!ctx || !ctx->current) return nk_vec2(0, 0);
  19034. return nk_vec2(ctx->current->bounds.x, ctx->current->bounds.y);
  19035. }
  19036. NK_API struct nk_vec2
  19037. nk_window_get_size(const struct nk_context* ctx)
  19038. {
  19039. NK_ASSERT(ctx);
  19040. NK_ASSERT(ctx->current);
  19041. if (!ctx || !ctx->current) return nk_vec2(0, 0);
  19042. return nk_vec2(ctx->current->bounds.w, ctx->current->bounds.h);
  19043. }
  19044. NK_API float
  19045. nk_window_get_width(const struct nk_context* ctx)
  19046. {
  19047. NK_ASSERT(ctx);
  19048. NK_ASSERT(ctx->current);
  19049. if (!ctx || !ctx->current) return 0;
  19050. return ctx->current->bounds.w;
  19051. }
  19052. NK_API float
  19053. nk_window_get_height(const struct nk_context* ctx)
  19054. {
  19055. NK_ASSERT(ctx);
  19056. NK_ASSERT(ctx->current);
  19057. if (!ctx || !ctx->current) return 0;
  19058. return ctx->current->bounds.h;
  19059. }
  19060. NK_API struct nk_rect
  19061. nk_window_get_content_region(struct nk_context* ctx)
  19062. {
  19063. NK_ASSERT(ctx);
  19064. NK_ASSERT(ctx->current);
  19065. if (!ctx || !ctx->current) return nk_rect(0, 0, 0, 0);
  19066. return ctx->current->layout->clip;
  19067. }
  19068. NK_API struct nk_vec2
  19069. nk_window_get_content_region_min(struct nk_context* ctx)
  19070. {
  19071. NK_ASSERT(ctx);
  19072. NK_ASSERT(ctx->current);
  19073. NK_ASSERT(ctx->current->layout);
  19074. if (!ctx || !ctx->current) return nk_vec2(0, 0);
  19075. return nk_vec2(ctx->current->layout->clip.x, ctx->current->layout->clip.y);
  19076. }
  19077. NK_API struct nk_vec2
  19078. nk_window_get_content_region_max(struct nk_context* ctx)
  19079. {
  19080. NK_ASSERT(ctx);
  19081. NK_ASSERT(ctx->current);
  19082. NK_ASSERT(ctx->current->layout);
  19083. if (!ctx || !ctx->current) return nk_vec2(0, 0);
  19084. return nk_vec2(ctx->current->layout->clip.x + ctx->current->layout->clip.w,
  19085. ctx->current->layout->clip.y + ctx->current->layout->clip.h);
  19086. }
  19087. NK_API struct nk_vec2
  19088. nk_window_get_content_region_size(struct nk_context* ctx)
  19089. {
  19090. NK_ASSERT(ctx);
  19091. NK_ASSERT(ctx->current);
  19092. NK_ASSERT(ctx->current->layout);
  19093. if (!ctx || !ctx->current) return nk_vec2(0, 0);
  19094. return nk_vec2(ctx->current->layout->clip.w, ctx->current->layout->clip.h);
  19095. }
  19096. NK_API struct nk_command_buffer*
  19097. nk_window_get_canvas(struct nk_context* ctx)
  19098. {
  19099. NK_ASSERT(ctx);
  19100. NK_ASSERT(ctx->current);
  19101. NK_ASSERT(ctx->current->layout);
  19102. if (!ctx || !ctx->current) return 0;
  19103. return &ctx->current->buffer;
  19104. }
  19105. NK_API struct nk_panel*
  19106. nk_window_get_panel(struct nk_context* ctx)
  19107. {
  19108. NK_ASSERT(ctx);
  19109. NK_ASSERT(ctx->current);
  19110. if (!ctx || !ctx->current) return 0;
  19111. return ctx->current->layout;
  19112. }
  19113. NK_API void
  19114. nk_window_get_scroll(struct nk_context* ctx, nk_uint* offset_x, nk_uint* offset_y)
  19115. {
  19116. struct nk_window* win;
  19117. NK_ASSERT(ctx);
  19118. NK_ASSERT(ctx->current);
  19119. if (!ctx || !ctx->current)
  19120. return;
  19121. win = ctx->current;
  19122. if (offset_x)
  19123. *offset_x = win->scrollbar.x;
  19124. if (offset_y)
  19125. *offset_y = win->scrollbar.y;
  19126. }
  19127. NK_API nk_bool
  19128. nk_window_has_focus(const struct nk_context* ctx)
  19129. {
  19130. NK_ASSERT(ctx);
  19131. NK_ASSERT(ctx->current);
  19132. NK_ASSERT(ctx->current->layout);
  19133. if (!ctx || !ctx->current) return 0;
  19134. return ctx->current == ctx->active;
  19135. }
  19136. NK_API nk_bool
  19137. nk_window_is_hovered(struct nk_context* ctx)
  19138. {
  19139. NK_ASSERT(ctx);
  19140. NK_ASSERT(ctx->current);
  19141. if (!ctx || !ctx->current || (ctx->current->flags & NK_WINDOW_HIDDEN))
  19142. return 0;
  19143. else {
  19144. struct nk_rect actual_bounds = ctx->current->bounds;
  19145. if (ctx->begin->flags & NK_WINDOW_MINIMIZED) {
  19146. actual_bounds.h = ctx->current->layout->header_height;
  19147. }
  19148. return nk_input_is_mouse_hovering_rect(&ctx->input, actual_bounds);
  19149. }
  19150. }
  19151. NK_API nk_bool
  19152. nk_window_is_any_hovered(struct nk_context* ctx)
  19153. {
  19154. struct nk_window* iter;
  19155. NK_ASSERT(ctx);
  19156. if (!ctx) return 0;
  19157. iter = ctx->begin;
  19158. while (iter) {
  19159. /* check if window is being hovered */
  19160. if (!(iter->flags & NK_WINDOW_HIDDEN)) {
  19161. /* check if window popup is being hovered */
  19162. if (iter->popup.active && iter->popup.win && nk_input_is_mouse_hovering_rect(&ctx->input, iter->popup.win->bounds))
  19163. return 1;
  19164. if (iter->flags & NK_WINDOW_MINIMIZED) {
  19165. struct nk_rect header = iter->bounds;
  19166. header.h = ctx->style.font->height + 2 * ctx->style.window.header.padding.y;
  19167. if (nk_input_is_mouse_hovering_rect(&ctx->input, header))
  19168. return 1;
  19169. }
  19170. else if (nk_input_is_mouse_hovering_rect(&ctx->input, iter->bounds)) {
  19171. return 1;
  19172. }
  19173. }
  19174. iter = iter->next;
  19175. }
  19176. return 0;
  19177. }
  19178. NK_API nk_bool
  19179. nk_item_is_any_active(struct nk_context* ctx)
  19180. {
  19181. int any_hovered = nk_window_is_any_hovered(ctx);
  19182. int any_active = (ctx->last_widget_state & NK_WIDGET_STATE_MODIFIED);
  19183. return any_hovered || any_active;
  19184. }
  19185. NK_API nk_bool
  19186. nk_window_is_collapsed(struct nk_context* ctx, const char* name)
  19187. {
  19188. int title_len;
  19189. nk_hash title_hash;
  19190. struct nk_window* win;
  19191. NK_ASSERT(ctx);
  19192. if (!ctx) return 0;
  19193. title_len = (int)nk_strlen(name);
  19194. title_hash = nk_murmur_hash(name, (int)title_len, NK_WINDOW_TITLE);
  19195. win = nk_find_window(ctx, title_hash, name);
  19196. if (!win) return 0;
  19197. return win->flags & NK_WINDOW_MINIMIZED;
  19198. }
  19199. NK_API nk_bool
  19200. nk_window_is_closed(struct nk_context* ctx, const char* name)
  19201. {
  19202. int title_len;
  19203. nk_hash title_hash;
  19204. struct nk_window* win;
  19205. NK_ASSERT(ctx);
  19206. if (!ctx) return 1;
  19207. title_len = (int)nk_strlen(name);
  19208. title_hash = nk_murmur_hash(name, (int)title_len, NK_WINDOW_TITLE);
  19209. win = nk_find_window(ctx, title_hash, name);
  19210. if (!win) return 1;
  19211. return (win->flags & NK_WINDOW_CLOSED);
  19212. }
  19213. NK_API nk_bool
  19214. nk_window_is_hidden(struct nk_context* ctx, const char* name)
  19215. {
  19216. int title_len;
  19217. nk_hash title_hash;
  19218. struct nk_window* win;
  19219. NK_ASSERT(ctx);
  19220. if (!ctx) return 1;
  19221. title_len = (int)nk_strlen(name);
  19222. title_hash = nk_murmur_hash(name, (int)title_len, NK_WINDOW_TITLE);
  19223. win = nk_find_window(ctx, title_hash, name);
  19224. if (!win) return 1;
  19225. return (win->flags & NK_WINDOW_HIDDEN);
  19226. }
  19227. NK_API nk_bool
  19228. nk_window_is_active(struct nk_context* ctx, const char* name)
  19229. {
  19230. int title_len;
  19231. nk_hash title_hash;
  19232. struct nk_window* win;
  19233. NK_ASSERT(ctx);
  19234. if (!ctx) return 0;
  19235. title_len = (int)nk_strlen(name);
  19236. title_hash = nk_murmur_hash(name, (int)title_len, NK_WINDOW_TITLE);
  19237. win = nk_find_window(ctx, title_hash, name);
  19238. if (!win) return 0;
  19239. return win == ctx->active;
  19240. }
  19241. NK_API struct nk_window*
  19242. nk_window_find(struct nk_context* ctx, const char* name)
  19243. {
  19244. int title_len;
  19245. nk_hash title_hash;
  19246. title_len = (int)nk_strlen(name);
  19247. title_hash = nk_murmur_hash(name, (int)title_len, NK_WINDOW_TITLE);
  19248. return nk_find_window(ctx, title_hash, name);
  19249. }
  19250. NK_API void
  19251. nk_window_close(struct nk_context* ctx, const char* name)
  19252. {
  19253. struct nk_window* win;
  19254. NK_ASSERT(ctx);
  19255. if (!ctx) return;
  19256. win = nk_window_find(ctx, name);
  19257. if (!win) return;
  19258. NK_ASSERT(ctx->current != win && "You cannot close a currently active window");
  19259. if (ctx->current == win) return;
  19260. win->flags |= NK_WINDOW_HIDDEN;
  19261. win->flags |= NK_WINDOW_CLOSED;
  19262. }
  19263. NK_API void
  19264. nk_window_set_bounds(struct nk_context* ctx,
  19265. const char* name, struct nk_rect bounds)
  19266. {
  19267. struct nk_window* win;
  19268. NK_ASSERT(ctx);
  19269. if (!ctx) return;
  19270. win = nk_window_find(ctx, name);
  19271. if (!win) return;
  19272. NK_ASSERT(ctx->current != win && "You cannot update a currently in procecss window");
  19273. win->bounds = bounds;
  19274. }
  19275. NK_API void
  19276. nk_window_set_position(struct nk_context* ctx,
  19277. const char* name, struct nk_vec2 pos)
  19278. {
  19279. struct nk_window* win = nk_window_find(ctx, name);
  19280. if (!win) return;
  19281. win->bounds.x = pos.x;
  19282. win->bounds.y = pos.y;
  19283. }
  19284. NK_API void
  19285. nk_window_set_size(struct nk_context* ctx,
  19286. const char* name, struct nk_vec2 size)
  19287. {
  19288. struct nk_window* win = nk_window_find(ctx, name);
  19289. if (!win) return;
  19290. win->bounds.w = size.x;
  19291. win->bounds.h = size.y;
  19292. }
  19293. NK_API void
  19294. nk_window_set_scroll(struct nk_context* ctx, nk_uint offset_x, nk_uint offset_y)
  19295. {
  19296. struct nk_window* win;
  19297. NK_ASSERT(ctx);
  19298. NK_ASSERT(ctx->current);
  19299. if (!ctx || !ctx->current)
  19300. return;
  19301. win = ctx->current;
  19302. win->scrollbar.x = offset_x;
  19303. win->scrollbar.y = offset_y;
  19304. }
  19305. NK_API void
  19306. nk_window_collapse(struct nk_context* ctx, const char* name,
  19307. enum nk_collapse_states c)
  19308. {
  19309. int title_len;
  19310. nk_hash title_hash;
  19311. struct nk_window* win;
  19312. NK_ASSERT(ctx);
  19313. if (!ctx) return;
  19314. title_len = (int)nk_strlen(name);
  19315. title_hash = nk_murmur_hash(name, (int)title_len, NK_WINDOW_TITLE);
  19316. win = nk_find_window(ctx, title_hash, name);
  19317. if (!win) return;
  19318. if (c == NK_MINIMIZED)
  19319. win->flags |= NK_WINDOW_MINIMIZED;
  19320. else win->flags &= ~(nk_flags)NK_WINDOW_MINIMIZED;
  19321. }
  19322. NK_API void
  19323. nk_window_collapse_if(struct nk_context* ctx, const char* name,
  19324. enum nk_collapse_states c, int cond)
  19325. {
  19326. NK_ASSERT(ctx);
  19327. if (!ctx || !cond) return;
  19328. nk_window_collapse(ctx, name, c);
  19329. }
  19330. NK_API void
  19331. nk_window_show(struct nk_context* ctx, const char* name, enum nk_show_states s)
  19332. {
  19333. int title_len;
  19334. nk_hash title_hash;
  19335. struct nk_window* win;
  19336. NK_ASSERT(ctx);
  19337. if (!ctx) return;
  19338. title_len = (int)nk_strlen(name);
  19339. title_hash = nk_murmur_hash(name, (int)title_len, NK_WINDOW_TITLE);
  19340. win = nk_find_window(ctx, title_hash, name);
  19341. if (!win) return;
  19342. if (s == NK_HIDDEN) {
  19343. win->flags |= NK_WINDOW_HIDDEN;
  19344. }
  19345. else win->flags &= ~(nk_flags)NK_WINDOW_HIDDEN;
  19346. }
  19347. NK_API void
  19348. nk_window_show_if(struct nk_context* ctx, const char* name,
  19349. enum nk_show_states s, int cond)
  19350. {
  19351. NK_ASSERT(ctx);
  19352. if (!ctx || !cond) return;
  19353. nk_window_show(ctx, name, s);
  19354. }
  19355. NK_API void
  19356. nk_window_set_focus(struct nk_context* ctx, const char* name)
  19357. {
  19358. int title_len;
  19359. nk_hash title_hash;
  19360. struct nk_window* win;
  19361. NK_ASSERT(ctx);
  19362. if (!ctx) return;
  19363. title_len = (int)nk_strlen(name);
  19364. title_hash = nk_murmur_hash(name, (int)title_len, NK_WINDOW_TITLE);
  19365. win = nk_find_window(ctx, title_hash, name);
  19366. if (win && ctx->end != win) {
  19367. nk_remove_window(ctx, win);
  19368. nk_insert_window(ctx, win, NK_INSERT_BACK);
  19369. }
  19370. ctx->active = win;
  19371. }
  19372. NK_API void
  19373. nk_rule_horizontal(struct nk_context* ctx, struct nk_color color, nk_bool rounding)
  19374. {
  19375. struct nk_rect space;
  19376. enum nk_widget_layout_states state = nk_widget(&space, ctx);
  19377. struct nk_command_buffer* canvas = nk_window_get_canvas(ctx);
  19378. if (!state) return;
  19379. nk_fill_rect(canvas, space, rounding && space.h > 1.5f ? space.h / 2.0f : 0, color);
  19380. }
  19381. /* ===============================================================
  19382. *
  19383. * POPUP
  19384. *
  19385. * ===============================================================*/
  19386. NK_API nk_bool
  19387. nk_popup_begin(struct nk_context* ctx, enum nk_popup_type type,
  19388. const char* title, nk_flags flags, struct nk_rect rect)
  19389. {
  19390. struct nk_window* popup;
  19391. struct nk_window* win;
  19392. struct nk_panel* panel;
  19393. int title_len;
  19394. nk_hash title_hash;
  19395. nk_size allocated;
  19396. NK_ASSERT(ctx);
  19397. NK_ASSERT(title);
  19398. NK_ASSERT(ctx->current);
  19399. NK_ASSERT(ctx->current->layout);
  19400. if (!ctx || !ctx->current || !ctx->current->layout)
  19401. return 0;
  19402. win = ctx->current;
  19403. panel = win->layout;
  19404. NK_ASSERT(!(panel->type & NK_PANEL_SET_POPUP) && "popups are not allowed to have popups");
  19405. (void)panel;
  19406. title_len = (int)nk_strlen(title);
  19407. title_hash = nk_murmur_hash(title, (int)title_len, NK_PANEL_POPUP);
  19408. popup = win->popup.win;
  19409. if (!popup) {
  19410. popup = (struct nk_window*)nk_create_window(ctx);
  19411. popup->parent = win;
  19412. win->popup.win = popup;
  19413. win->popup.active = 0;
  19414. win->popup.type = NK_PANEL_POPUP;
  19415. }
  19416. /* make sure we have correct popup */
  19417. if (win->popup.name != title_hash) {
  19418. if (!win->popup.active) {
  19419. nk_zero(popup, sizeof(*popup));
  19420. win->popup.name = title_hash;
  19421. win->popup.active = 1;
  19422. win->popup.type = NK_PANEL_POPUP;
  19423. }
  19424. else return 0;
  19425. }
  19426. /* popup position is local to window */
  19427. ctx->current = popup;
  19428. rect.x += win->layout->clip.x;
  19429. rect.y += win->layout->clip.y;
  19430. /* setup popup data */
  19431. popup->parent = win;
  19432. popup->bounds = rect;
  19433. popup->seq = ctx->seq;
  19434. popup->layout = (struct nk_panel*)nk_create_panel(ctx);
  19435. popup->flags = flags;
  19436. popup->flags |= NK_WINDOW_BORDER;
  19437. if (type == NK_POPUP_DYNAMIC)
  19438. popup->flags |= NK_WINDOW_DYNAMIC;
  19439. popup->buffer = win->buffer;
  19440. nk_start_popup(ctx, win);
  19441. allocated = ctx->memory.allocated;
  19442. nk_push_scissor(&popup->buffer, nk_null_rect);
  19443. if (nk_panel_begin(ctx, title, NK_PANEL_POPUP)) {
  19444. /* popup is running therefore invalidate parent panels */
  19445. struct nk_panel* root;
  19446. root = win->layout;
  19447. while (root) {
  19448. root->flags |= NK_WINDOW_ROM;
  19449. root->flags &= ~(nk_flags)NK_WINDOW_REMOVE_ROM;
  19450. root = root->parent;
  19451. }
  19452. win->popup.active = 1;
  19453. popup->layout->offset_x = &popup->scrollbar.x;
  19454. popup->layout->offset_y = &popup->scrollbar.y;
  19455. popup->layout->parent = win->layout;
  19456. return 1;
  19457. }
  19458. else {
  19459. /* popup was closed/is invalid so cleanup */
  19460. struct nk_panel* root;
  19461. root = win->layout;
  19462. while (root) {
  19463. root->flags |= NK_WINDOW_REMOVE_ROM;
  19464. root = root->parent;
  19465. }
  19466. win->popup.buf.active = 0;
  19467. win->popup.active = 0;
  19468. ctx->memory.allocated = allocated;
  19469. ctx->current = win;
  19470. nk_free_panel(ctx, popup->layout);
  19471. popup->layout = 0;
  19472. return 0;
  19473. }
  19474. }
  19475. NK_LIB nk_bool
  19476. nk_nonblock_begin(struct nk_context* ctx,
  19477. nk_flags flags, struct nk_rect body, struct nk_rect header,
  19478. enum nk_panel_type panel_type)
  19479. {
  19480. struct nk_window* popup;
  19481. struct nk_window* win;
  19482. struct nk_panel* panel;
  19483. int is_active = nk_true;
  19484. NK_ASSERT(ctx);
  19485. NK_ASSERT(ctx->current);
  19486. NK_ASSERT(ctx->current->layout);
  19487. if (!ctx || !ctx->current || !ctx->current->layout)
  19488. return 0;
  19489. /* popups cannot have popups */
  19490. win = ctx->current;
  19491. panel = win->layout;
  19492. NK_ASSERT(!(panel->type & NK_PANEL_SET_POPUP));
  19493. (void)panel;
  19494. popup = win->popup.win;
  19495. if (!popup) {
  19496. /* create window for nonblocking popup */
  19497. popup = (struct nk_window*)nk_create_window(ctx);
  19498. popup->parent = win;
  19499. win->popup.win = popup;
  19500. win->popup.type = panel_type;
  19501. nk_command_buffer_init(&popup->buffer, &ctx->memory, NK_CLIPPING_ON);
  19502. }
  19503. else {
  19504. /* close the popup if user pressed outside or in the header */
  19505. int pressed, in_body, in_header;
  19506. #ifdef NK_BUTTON_TRIGGER_ON_RELEASE
  19507. pressed = nk_input_is_mouse_released(&ctx->input, NK_BUTTON_LEFT);
  19508. #else
  19509. pressed = nk_input_is_mouse_pressed(&ctx->input, NK_BUTTON_LEFT);
  19510. #endif
  19511. in_body = nk_input_is_mouse_hovering_rect(&ctx->input, body);
  19512. in_header = nk_input_is_mouse_hovering_rect(&ctx->input, header);
  19513. if (pressed && (!in_body || in_header))
  19514. is_active = nk_false;
  19515. }
  19516. win->popup.header = header;
  19517. if (!is_active) {
  19518. /* remove read only mode from all parent panels */
  19519. struct nk_panel* root = win->layout;
  19520. while (root) {
  19521. root->flags |= NK_WINDOW_REMOVE_ROM;
  19522. root = root->parent;
  19523. }
  19524. return is_active;
  19525. }
  19526. popup->bounds = body;
  19527. popup->parent = win;
  19528. popup->layout = (struct nk_panel*)nk_create_panel(ctx);
  19529. popup->flags = flags;
  19530. popup->flags |= NK_WINDOW_BORDER;
  19531. popup->flags |= NK_WINDOW_DYNAMIC;
  19532. popup->seq = ctx->seq;
  19533. win->popup.active = 1;
  19534. NK_ASSERT(popup->layout);
  19535. nk_start_popup(ctx, win);
  19536. popup->buffer = win->buffer;
  19537. nk_push_scissor(&popup->buffer, nk_null_rect);
  19538. ctx->current = popup;
  19539. nk_panel_begin(ctx, 0, panel_type);
  19540. win->buffer = popup->buffer;
  19541. popup->layout->parent = win->layout;
  19542. popup->layout->offset_x = &popup->scrollbar.x;
  19543. popup->layout->offset_y = &popup->scrollbar.y;
  19544. /* set read only mode to all parent panels */
  19545. {struct nk_panel* root;
  19546. root = win->layout;
  19547. while (root) {
  19548. root->flags |= NK_WINDOW_ROM;
  19549. root = root->parent;
  19550. }}
  19551. return is_active;
  19552. }
  19553. NK_API void
  19554. nk_popup_close(struct nk_context* ctx)
  19555. {
  19556. struct nk_window* popup;
  19557. NK_ASSERT(ctx);
  19558. if (!ctx || !ctx->current) return;
  19559. popup = ctx->current;
  19560. NK_ASSERT(popup->parent);
  19561. NK_ASSERT(popup->layout->type & NK_PANEL_SET_POPUP);
  19562. popup->flags |= NK_WINDOW_HIDDEN;
  19563. }
  19564. NK_API void
  19565. nk_popup_end(struct nk_context* ctx)
  19566. {
  19567. struct nk_window* win;
  19568. struct nk_window* popup;
  19569. NK_ASSERT(ctx);
  19570. NK_ASSERT(ctx->current);
  19571. NK_ASSERT(ctx->current->layout);
  19572. if (!ctx || !ctx->current || !ctx->current->layout)
  19573. return;
  19574. popup = ctx->current;
  19575. if (!popup->parent) return;
  19576. win = popup->parent;
  19577. if (popup->flags & NK_WINDOW_HIDDEN) {
  19578. struct nk_panel* root;
  19579. root = win->layout;
  19580. while (root) {
  19581. root->flags |= NK_WINDOW_REMOVE_ROM;
  19582. root = root->parent;
  19583. }
  19584. win->popup.active = 0;
  19585. }
  19586. nk_push_scissor(&popup->buffer, nk_null_rect);
  19587. nk_end(ctx);
  19588. win->buffer = popup->buffer;
  19589. nk_finish_popup(ctx, win);
  19590. ctx->current = win;
  19591. nk_push_scissor(&win->buffer, win->layout->clip);
  19592. }
  19593. NK_API void
  19594. nk_popup_get_scroll(struct nk_context* ctx, nk_uint* offset_x, nk_uint* offset_y)
  19595. {
  19596. struct nk_window* popup;
  19597. NK_ASSERT(ctx);
  19598. NK_ASSERT(ctx->current);
  19599. NK_ASSERT(ctx->current->layout);
  19600. if (!ctx || !ctx->current || !ctx->current->layout)
  19601. return;
  19602. popup = ctx->current;
  19603. if (offset_x)
  19604. *offset_x = popup->scrollbar.x;
  19605. if (offset_y)
  19606. *offset_y = popup->scrollbar.y;
  19607. }
  19608. NK_API void
  19609. nk_popup_set_scroll(struct nk_context* ctx, nk_uint offset_x, nk_uint offset_y)
  19610. {
  19611. struct nk_window* popup;
  19612. NK_ASSERT(ctx);
  19613. NK_ASSERT(ctx->current);
  19614. NK_ASSERT(ctx->current->layout);
  19615. if (!ctx || !ctx->current || !ctx->current->layout)
  19616. return;
  19617. popup = ctx->current;
  19618. popup->scrollbar.x = offset_x;
  19619. popup->scrollbar.y = offset_y;
  19620. }
  19621. /* ==============================================================
  19622. *
  19623. * CONTEXTUAL
  19624. *
  19625. * ===============================================================*/
  19626. NK_API nk_bool
  19627. nk_contextual_begin(struct nk_context* ctx, nk_flags flags, struct nk_vec2 size,
  19628. struct nk_rect trigger_bounds)
  19629. {
  19630. struct nk_window* win;
  19631. struct nk_window* popup;
  19632. struct nk_rect body;
  19633. NK_STORAGE const struct nk_rect null_rect = { -1,-1,0,0 };
  19634. int is_clicked = 0;
  19635. int is_open = 0;
  19636. int ret = 0;
  19637. NK_ASSERT(ctx);
  19638. NK_ASSERT(ctx->current);
  19639. NK_ASSERT(ctx->current->layout);
  19640. if (!ctx || !ctx->current || !ctx->current->layout)
  19641. return 0;
  19642. win = ctx->current;
  19643. ++win->popup.con_count;
  19644. if (ctx->current != ctx->active)
  19645. return 0;
  19646. /* check if currently active contextual is active */
  19647. popup = win->popup.win;
  19648. is_open = (popup && win->popup.type == NK_PANEL_CONTEXTUAL);
  19649. is_clicked = nk_input_mouse_clicked(&ctx->input, NK_BUTTON_RIGHT, trigger_bounds);
  19650. if (win->popup.active_con && win->popup.con_count != win->popup.active_con)
  19651. return 0;
  19652. if (!is_open && win->popup.active_con)
  19653. win->popup.active_con = 0;
  19654. if ((!is_open && !is_clicked))
  19655. return 0;
  19656. /* calculate contextual position on click */
  19657. win->popup.active_con = win->popup.con_count;
  19658. if (is_clicked) {
  19659. body.x = ctx->input.mouse.pos.x;
  19660. body.y = ctx->input.mouse.pos.y;
  19661. }
  19662. else {
  19663. body.x = popup->bounds.x;
  19664. body.y = popup->bounds.y;
  19665. }
  19666. body.w = size.x;
  19667. body.h = size.y;
  19668. /* start nonblocking contextual popup */
  19669. ret = nk_nonblock_begin(ctx, flags | NK_WINDOW_NO_SCROLLBAR, body,
  19670. null_rect, NK_PANEL_CONTEXTUAL);
  19671. if (ret) win->popup.type = NK_PANEL_CONTEXTUAL;
  19672. else {
  19673. win->popup.active_con = 0;
  19674. win->popup.type = NK_PANEL_NONE;
  19675. if (win->popup.win)
  19676. win->popup.win->flags = 0;
  19677. }
  19678. return ret;
  19679. }
  19680. NK_API nk_bool
  19681. nk_contextual_item_text(struct nk_context* ctx, const char* text, int len,
  19682. nk_flags alignment)
  19683. {
  19684. struct nk_window* win;
  19685. const struct nk_input* in;
  19686. const struct nk_style* style;
  19687. struct nk_rect bounds;
  19688. enum nk_widget_layout_states state;
  19689. NK_ASSERT(ctx);
  19690. NK_ASSERT(ctx->current);
  19691. NK_ASSERT(ctx->current->layout);
  19692. if (!ctx || !ctx->current || !ctx->current->layout)
  19693. return 0;
  19694. win = ctx->current;
  19695. style = &ctx->style;
  19696. state = nk_widget_fitting(&bounds, ctx, style->contextual_button.padding);
  19697. if (!state) return nk_false;
  19698. in = (state == NK_WIDGET_ROM || win->layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  19699. if (nk_do_button_text(&ctx->last_widget_state, &win->buffer, bounds,
  19700. text, len, alignment, NK_BUTTON_DEFAULT, &style->contextual_button, in, style->font)) {
  19701. nk_contextual_close(ctx);
  19702. return nk_true;
  19703. }
  19704. return nk_false;
  19705. }
  19706. NK_API nk_bool
  19707. nk_contextual_item_label(struct nk_context* ctx, const char* label, nk_flags align)
  19708. {
  19709. return nk_contextual_item_text(ctx, label, nk_strlen(label), align);
  19710. }
  19711. NK_API nk_bool
  19712. nk_contextual_item_image_text(struct nk_context* ctx, struct nk_image img,
  19713. const char* text, int len, nk_flags align)
  19714. {
  19715. struct nk_window* win;
  19716. const struct nk_input* in;
  19717. const struct nk_style* style;
  19718. struct nk_rect bounds;
  19719. enum nk_widget_layout_states state;
  19720. NK_ASSERT(ctx);
  19721. NK_ASSERT(ctx->current);
  19722. NK_ASSERT(ctx->current->layout);
  19723. if (!ctx || !ctx->current || !ctx->current->layout)
  19724. return 0;
  19725. win = ctx->current;
  19726. style = &ctx->style;
  19727. state = nk_widget_fitting(&bounds, ctx, style->contextual_button.padding);
  19728. if (!state) return nk_false;
  19729. in = (state == NK_WIDGET_ROM || win->layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  19730. if (nk_do_button_text_image(&ctx->last_widget_state, &win->buffer, bounds,
  19731. img, text, len, align, NK_BUTTON_DEFAULT, &style->contextual_button, style->font, in)) {
  19732. nk_contextual_close(ctx);
  19733. return nk_true;
  19734. }
  19735. return nk_false;
  19736. }
  19737. NK_API nk_bool
  19738. nk_contextual_item_image_label(struct nk_context* ctx, struct nk_image img,
  19739. const char* label, nk_flags align)
  19740. {
  19741. return nk_contextual_item_image_text(ctx, img, label, nk_strlen(label), align);
  19742. }
  19743. NK_API nk_bool
  19744. nk_contextual_item_symbol_text(struct nk_context* ctx, enum nk_symbol_type symbol,
  19745. const char* text, int len, nk_flags align)
  19746. {
  19747. struct nk_window* win;
  19748. const struct nk_input* in;
  19749. const struct nk_style* style;
  19750. struct nk_rect bounds;
  19751. enum nk_widget_layout_states state;
  19752. NK_ASSERT(ctx);
  19753. NK_ASSERT(ctx->current);
  19754. NK_ASSERT(ctx->current->layout);
  19755. if (!ctx || !ctx->current || !ctx->current->layout)
  19756. return 0;
  19757. win = ctx->current;
  19758. style = &ctx->style;
  19759. state = nk_widget_fitting(&bounds, ctx, style->contextual_button.padding);
  19760. if (!state) return nk_false;
  19761. in = (state == NK_WIDGET_ROM || win->layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  19762. if (nk_do_button_text_symbol(&ctx->last_widget_state, &win->buffer, bounds,
  19763. symbol, text, len, align, NK_BUTTON_DEFAULT, &style->contextual_button, style->font, in)) {
  19764. nk_contextual_close(ctx);
  19765. return nk_true;
  19766. }
  19767. return nk_false;
  19768. }
  19769. NK_API nk_bool
  19770. nk_contextual_item_symbol_label(struct nk_context* ctx, enum nk_symbol_type symbol,
  19771. const char* text, nk_flags align)
  19772. {
  19773. return nk_contextual_item_symbol_text(ctx, symbol, text, nk_strlen(text), align);
  19774. }
  19775. NK_API void
  19776. nk_contextual_close(struct nk_context* ctx)
  19777. {
  19778. NK_ASSERT(ctx);
  19779. NK_ASSERT(ctx->current);
  19780. NK_ASSERT(ctx->current->layout);
  19781. if (!ctx || !ctx->current || !ctx->current->layout) return;
  19782. nk_popup_close(ctx);
  19783. }
  19784. NK_API void
  19785. nk_contextual_end(struct nk_context* ctx)
  19786. {
  19787. struct nk_window* popup;
  19788. struct nk_panel* panel;
  19789. NK_ASSERT(ctx);
  19790. NK_ASSERT(ctx->current);
  19791. if (!ctx || !ctx->current) return;
  19792. popup = ctx->current;
  19793. panel = popup->layout;
  19794. NK_ASSERT(popup->parent);
  19795. NK_ASSERT(panel->type & NK_PANEL_SET_POPUP);
  19796. if (panel->flags & NK_WINDOW_DYNAMIC) {
  19797. /* Close behavior
  19798. This is a bit of a hack solution since we do not know before we end our popup
  19799. how big it will be. We therefore do not directly know when a
  19800. click outside the non-blocking popup must close it at that direct frame.
  19801. Instead it will be closed in the next frame.*/
  19802. struct nk_rect body = { 0,0,0,0 };
  19803. if (panel->at_y < (panel->bounds.y + panel->bounds.h)) {
  19804. struct nk_vec2 padding = nk_panel_get_padding(&ctx->style, panel->type);
  19805. body = panel->bounds;
  19806. body.y = (panel->at_y + panel->footer_height + panel->border + padding.y + panel->row.height);
  19807. body.h = (panel->bounds.y + panel->bounds.h) - body.y;
  19808. }
  19809. {int pressed = nk_input_is_mouse_pressed(&ctx->input, NK_BUTTON_LEFT);
  19810. int in_body = nk_input_is_mouse_hovering_rect(&ctx->input, body);
  19811. if (pressed && in_body)
  19812. popup->flags |= NK_WINDOW_HIDDEN;
  19813. }
  19814. }
  19815. if (popup->flags & NK_WINDOW_HIDDEN)
  19816. popup->seq = 0;
  19817. nk_popup_end(ctx);
  19818. return;
  19819. }
  19820. /* ===============================================================
  19821. *
  19822. * MENU
  19823. *
  19824. * ===============================================================*/
  19825. NK_API void
  19826. nk_menubar_begin(struct nk_context* ctx)
  19827. {
  19828. struct nk_panel* layout;
  19829. NK_ASSERT(ctx);
  19830. NK_ASSERT(ctx->current);
  19831. NK_ASSERT(ctx->current->layout);
  19832. if (!ctx || !ctx->current || !ctx->current->layout)
  19833. return;
  19834. layout = ctx->current->layout;
  19835. NK_ASSERT(layout->at_y == layout->bounds.y);
  19836. /* if this assert triggers you allocated space between nk_begin and nk_menubar_begin.
  19837. If you want a menubar the first nuklear function after `nk_begin` has to be a
  19838. `nk_menubar_begin` call. Inside the menubar you then have to allocate space for
  19839. widgets (also supports multiple rows).
  19840. Example:
  19841. if (nk_begin(...)) {
  19842. nk_menubar_begin(...);
  19843. nk_layout_xxxx(...);
  19844. nk_button(...);
  19845. nk_layout_xxxx(...);
  19846. nk_button(...);
  19847. nk_menubar_end(...);
  19848. }
  19849. nk_end(...);
  19850. */
  19851. if (layout->flags & NK_WINDOW_HIDDEN || layout->flags & NK_WINDOW_MINIMIZED)
  19852. return;
  19853. layout->menu.x = layout->at_x;
  19854. layout->menu.y = layout->at_y + layout->row.height;
  19855. layout->menu.w = layout->bounds.w;
  19856. layout->menu.offset.x = *layout->offset_x;
  19857. layout->menu.offset.y = *layout->offset_y;
  19858. *layout->offset_y = 0;
  19859. }
  19860. NK_API void
  19861. nk_menubar_end(struct nk_context* ctx)
  19862. {
  19863. struct nk_window* win;
  19864. struct nk_panel* layout;
  19865. struct nk_command_buffer* out;
  19866. NK_ASSERT(ctx);
  19867. NK_ASSERT(ctx->current);
  19868. NK_ASSERT(ctx->current->layout);
  19869. if (!ctx || !ctx->current || !ctx->current->layout)
  19870. return;
  19871. win = ctx->current;
  19872. out = &win->buffer;
  19873. layout = win->layout;
  19874. if (layout->flags & NK_WINDOW_HIDDEN || layout->flags & NK_WINDOW_MINIMIZED)
  19875. return;
  19876. layout->menu.h = layout->at_y - layout->menu.y;
  19877. layout->menu.h += layout->row.height + ctx->style.window.spacing.y;
  19878. layout->bounds.y += layout->menu.h;
  19879. layout->bounds.h -= layout->menu.h;
  19880. *layout->offset_x = layout->menu.offset.x;
  19881. *layout->offset_y = layout->menu.offset.y;
  19882. layout->at_y = layout->bounds.y - layout->row.height;
  19883. layout->clip.y = layout->bounds.y;
  19884. layout->clip.h = layout->bounds.h;
  19885. nk_push_scissor(out, layout->clip);
  19886. }
  19887. NK_INTERN int
  19888. nk_menu_begin(struct nk_context* ctx, struct nk_window* win,
  19889. const char* id, int is_clicked, struct nk_rect header, struct nk_vec2 size)
  19890. {
  19891. int is_open = 0;
  19892. int is_active = 0;
  19893. struct nk_rect body;
  19894. struct nk_window* popup;
  19895. nk_hash hash = nk_murmur_hash(id, (int)nk_strlen(id), NK_PANEL_MENU);
  19896. NK_ASSERT(ctx);
  19897. NK_ASSERT(ctx->current);
  19898. NK_ASSERT(ctx->current->layout);
  19899. if (!ctx || !ctx->current || !ctx->current->layout)
  19900. return 0;
  19901. body.x = header.x;
  19902. body.w = size.x;
  19903. body.y = header.y + header.h;
  19904. body.h = size.y;
  19905. popup = win->popup.win;
  19906. is_open = popup ? nk_true : nk_false;
  19907. is_active = (popup && (win->popup.name == hash) && win->popup.type == NK_PANEL_MENU);
  19908. if ((is_clicked && is_open && !is_active) || (is_open && !is_active) ||
  19909. (!is_open && !is_active && !is_clicked)) return 0;
  19910. if (!nk_nonblock_begin(ctx, NK_WINDOW_NO_SCROLLBAR, body, header, NK_PANEL_MENU))
  19911. return 0;
  19912. win->popup.type = NK_PANEL_MENU;
  19913. win->popup.name = hash;
  19914. return 1;
  19915. }
  19916. NK_API nk_bool
  19917. nk_menu_begin_text(struct nk_context* ctx, const char* title, int len,
  19918. nk_flags align, struct nk_vec2 size)
  19919. {
  19920. struct nk_window* win;
  19921. const struct nk_input* in;
  19922. struct nk_rect header;
  19923. int is_clicked = nk_false;
  19924. nk_flags state;
  19925. NK_ASSERT(ctx);
  19926. NK_ASSERT(ctx->current);
  19927. NK_ASSERT(ctx->current->layout);
  19928. if (!ctx || !ctx->current || !ctx->current->layout)
  19929. return 0;
  19930. win = ctx->current;
  19931. state = nk_widget(&header, ctx);
  19932. if (!state) return 0;
  19933. in = (state == NK_WIDGET_ROM || win->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  19934. if (nk_do_button_text(&ctx->last_widget_state, &win->buffer, header,
  19935. title, len, align, NK_BUTTON_DEFAULT, &ctx->style.menu_button, in, ctx->style.font))
  19936. is_clicked = nk_true;
  19937. return nk_menu_begin(ctx, win, title, is_clicked, header, size);
  19938. }
  19939. NK_API nk_bool nk_menu_begin_label(struct nk_context* ctx,
  19940. const char* text, nk_flags align, struct nk_vec2 size)
  19941. {
  19942. return nk_menu_begin_text(ctx, text, nk_strlen(text), align, size);
  19943. }
  19944. NK_API nk_bool
  19945. nk_menu_begin_image(struct nk_context* ctx, const char* id, struct nk_image img,
  19946. struct nk_vec2 size)
  19947. {
  19948. struct nk_window* win;
  19949. struct nk_rect header;
  19950. const struct nk_input* in;
  19951. int is_clicked = nk_false;
  19952. nk_flags state;
  19953. NK_ASSERT(ctx);
  19954. NK_ASSERT(ctx->current);
  19955. NK_ASSERT(ctx->current->layout);
  19956. if (!ctx || !ctx->current || !ctx->current->layout)
  19957. return 0;
  19958. win = ctx->current;
  19959. state = nk_widget(&header, ctx);
  19960. if (!state) return 0;
  19961. in = (state == NK_WIDGET_ROM || win->layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  19962. if (nk_do_button_image(&ctx->last_widget_state, &win->buffer, header,
  19963. img, NK_BUTTON_DEFAULT, &ctx->style.menu_button, in))
  19964. is_clicked = nk_true;
  19965. return nk_menu_begin(ctx, win, id, is_clicked, header, size);
  19966. }
  19967. NK_API nk_bool
  19968. nk_menu_begin_symbol(struct nk_context* ctx, const char* id,
  19969. enum nk_symbol_type sym, struct nk_vec2 size)
  19970. {
  19971. struct nk_window* win;
  19972. const struct nk_input* in;
  19973. struct nk_rect header;
  19974. int is_clicked = nk_false;
  19975. nk_flags state;
  19976. NK_ASSERT(ctx);
  19977. NK_ASSERT(ctx->current);
  19978. NK_ASSERT(ctx->current->layout);
  19979. if (!ctx || !ctx->current || !ctx->current->layout)
  19980. return 0;
  19981. win = ctx->current;
  19982. state = nk_widget(&header, ctx);
  19983. if (!state) return 0;
  19984. in = (state == NK_WIDGET_ROM || win->layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  19985. if (nk_do_button_symbol(&ctx->last_widget_state, &win->buffer, header,
  19986. sym, NK_BUTTON_DEFAULT, &ctx->style.menu_button, in, ctx->style.font))
  19987. is_clicked = nk_true;
  19988. return nk_menu_begin(ctx, win, id, is_clicked, header, size);
  19989. }
  19990. NK_API nk_bool
  19991. nk_menu_begin_image_text(struct nk_context* ctx, const char* title, int len,
  19992. nk_flags align, struct nk_image img, struct nk_vec2 size)
  19993. {
  19994. struct nk_window* win;
  19995. struct nk_rect header;
  19996. const struct nk_input* in;
  19997. int is_clicked = nk_false;
  19998. nk_flags state;
  19999. NK_ASSERT(ctx);
  20000. NK_ASSERT(ctx->current);
  20001. NK_ASSERT(ctx->current->layout);
  20002. if (!ctx || !ctx->current || !ctx->current->layout)
  20003. return 0;
  20004. win = ctx->current;
  20005. state = nk_widget(&header, ctx);
  20006. if (!state) return 0;
  20007. in = (state == NK_WIDGET_ROM || win->layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  20008. if (nk_do_button_text_image(&ctx->last_widget_state, &win->buffer,
  20009. header, img, title, len, align, NK_BUTTON_DEFAULT, &ctx->style.menu_button,
  20010. ctx->style.font, in))
  20011. is_clicked = nk_true;
  20012. return nk_menu_begin(ctx, win, title, is_clicked, header, size);
  20013. }
  20014. NK_API nk_bool
  20015. nk_menu_begin_image_label(struct nk_context* ctx,
  20016. const char* title, nk_flags align, struct nk_image img, struct nk_vec2 size)
  20017. {
  20018. return nk_menu_begin_image_text(ctx, title, nk_strlen(title), align, img, size);
  20019. }
  20020. NK_API nk_bool
  20021. nk_menu_begin_symbol_text(struct nk_context* ctx, const char* title, int len,
  20022. nk_flags align, enum nk_symbol_type sym, struct nk_vec2 size)
  20023. {
  20024. struct nk_window* win;
  20025. struct nk_rect header;
  20026. const struct nk_input* in;
  20027. int is_clicked = nk_false;
  20028. nk_flags state;
  20029. NK_ASSERT(ctx);
  20030. NK_ASSERT(ctx->current);
  20031. NK_ASSERT(ctx->current->layout);
  20032. if (!ctx || !ctx->current || !ctx->current->layout)
  20033. return 0;
  20034. win = ctx->current;
  20035. state = nk_widget(&header, ctx);
  20036. if (!state) return 0;
  20037. in = (state == NK_WIDGET_ROM || win->layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  20038. if (nk_do_button_text_symbol(&ctx->last_widget_state, &win->buffer,
  20039. header, sym, title, len, align, NK_BUTTON_DEFAULT, &ctx->style.menu_button,
  20040. ctx->style.font, in)) is_clicked = nk_true;
  20041. return nk_menu_begin(ctx, win, title, is_clicked, header, size);
  20042. }
  20043. NK_API nk_bool
  20044. nk_menu_begin_symbol_label(struct nk_context* ctx,
  20045. const char* title, nk_flags align, enum nk_symbol_type sym, struct nk_vec2 size)
  20046. {
  20047. return nk_menu_begin_symbol_text(ctx, title, nk_strlen(title), align, sym, size);
  20048. }
  20049. NK_API nk_bool
  20050. nk_menu_item_text(struct nk_context* ctx, const char* title, int len, nk_flags align)
  20051. {
  20052. return nk_contextual_item_text(ctx, title, len, align);
  20053. }
  20054. NK_API nk_bool
  20055. nk_menu_item_label(struct nk_context* ctx, const char* label, nk_flags align)
  20056. {
  20057. return nk_contextual_item_label(ctx, label, align);
  20058. }
  20059. NK_API nk_bool
  20060. nk_menu_item_image_label(struct nk_context* ctx, struct nk_image img,
  20061. const char* label, nk_flags align)
  20062. {
  20063. return nk_contextual_item_image_label(ctx, img, label, align);
  20064. }
  20065. NK_API nk_bool
  20066. nk_menu_item_image_text(struct nk_context* ctx, struct nk_image img,
  20067. const char* text, int len, nk_flags align)
  20068. {
  20069. return nk_contextual_item_image_text(ctx, img, text, len, align);
  20070. }
  20071. NK_API nk_bool nk_menu_item_symbol_text(struct nk_context* ctx, enum nk_symbol_type sym,
  20072. const char* text, int len, nk_flags align)
  20073. {
  20074. return nk_contextual_item_symbol_text(ctx, sym, text, len, align);
  20075. }
  20076. NK_API nk_bool nk_menu_item_symbol_label(struct nk_context* ctx, enum nk_symbol_type sym,
  20077. const char* label, nk_flags align)
  20078. {
  20079. return nk_contextual_item_symbol_label(ctx, sym, label, align);
  20080. }
  20081. NK_API void nk_menu_close(struct nk_context* ctx)
  20082. {
  20083. nk_contextual_close(ctx);
  20084. }
  20085. NK_API void
  20086. nk_menu_end(struct nk_context* ctx)
  20087. {
  20088. nk_contextual_end(ctx);
  20089. }
  20090. /* ===============================================================
  20091. *
  20092. * LAYOUT
  20093. *
  20094. * ===============================================================*/
  20095. NK_API void
  20096. nk_layout_set_min_row_height(struct nk_context* ctx, float height)
  20097. {
  20098. struct nk_window* win;
  20099. struct nk_panel* layout;
  20100. NK_ASSERT(ctx);
  20101. NK_ASSERT(ctx->current);
  20102. NK_ASSERT(ctx->current->layout);
  20103. if (!ctx || !ctx->current || !ctx->current->layout)
  20104. return;
  20105. win = ctx->current;
  20106. layout = win->layout;
  20107. layout->row.min_height = height;
  20108. }
  20109. NK_API void
  20110. nk_layout_reset_min_row_height(struct nk_context* ctx)
  20111. {
  20112. struct nk_window* win;
  20113. struct nk_panel* layout;
  20114. NK_ASSERT(ctx);
  20115. NK_ASSERT(ctx->current);
  20116. NK_ASSERT(ctx->current->layout);
  20117. if (!ctx || !ctx->current || !ctx->current->layout)
  20118. return;
  20119. win = ctx->current;
  20120. layout = win->layout;
  20121. layout->row.min_height = ctx->style.font->height;
  20122. layout->row.min_height += ctx->style.text.padding.y * 2;
  20123. layout->row.min_height += ctx->style.window.min_row_height_padding * 2;
  20124. }
  20125. NK_LIB float
  20126. nk_layout_row_calculate_usable_space(const struct nk_style* style, enum nk_panel_type type,
  20127. float total_space, int columns)
  20128. {
  20129. float panel_spacing;
  20130. float panel_space;
  20131. struct nk_vec2 spacing;
  20132. NK_UNUSED(type);
  20133. spacing = style->window.spacing;
  20134. /* calculate the usable panel space */
  20135. panel_spacing = (float)NK_MAX(columns - 1, 0) * spacing.x;
  20136. panel_space = total_space - panel_spacing;
  20137. return panel_space;
  20138. }
  20139. NK_LIB void
  20140. nk_panel_layout(const struct nk_context* ctx, struct nk_window* win,
  20141. float height, int cols)
  20142. {
  20143. struct nk_panel* layout;
  20144. const struct nk_style* style;
  20145. struct nk_command_buffer* out;
  20146. struct nk_vec2 item_spacing;
  20147. struct nk_color color;
  20148. NK_ASSERT(ctx);
  20149. NK_ASSERT(ctx->current);
  20150. NK_ASSERT(ctx->current->layout);
  20151. if (!ctx || !ctx->current || !ctx->current->layout)
  20152. return;
  20153. /* prefetch some configuration data */
  20154. layout = win->layout;
  20155. style = &ctx->style;
  20156. out = &win->buffer;
  20157. color = style->window.background;
  20158. item_spacing = style->window.spacing;
  20159. /* if one of these triggers you forgot to add an `if` condition around either
  20160. a window, group, popup, combobox or contextual menu `begin` and `end` block.
  20161. Example:
  20162. if (nk_begin(...) {...} nk_end(...); or
  20163. if (nk_group_begin(...) { nk_group_end(...);} */
  20164. NK_ASSERT(!(layout->flags & NK_WINDOW_MINIMIZED));
  20165. NK_ASSERT(!(layout->flags & NK_WINDOW_HIDDEN));
  20166. NK_ASSERT(!(layout->flags & NK_WINDOW_CLOSED));
  20167. /* update the current row and set the current row layout */
  20168. layout->row.index = 0;
  20169. layout->at_y += layout->row.height;
  20170. layout->row.columns = cols;
  20171. if (height == 0.0f)
  20172. layout->row.height = NK_MAX(height, layout->row.min_height) + item_spacing.y;
  20173. else layout->row.height = height + item_spacing.y;
  20174. layout->row.item_offset = 0;
  20175. if (layout->flags & NK_WINDOW_DYNAMIC) {
  20176. /* draw background for dynamic panels */
  20177. struct nk_rect background;
  20178. background.x = win->bounds.x;
  20179. background.w = win->bounds.w;
  20180. background.y = layout->at_y - 1.0f;
  20181. background.h = layout->row.height + 1.0f;
  20182. nk_fill_rect(out, background, 0, color);
  20183. }
  20184. }
  20185. NK_LIB void
  20186. nk_row_layout(struct nk_context* ctx, enum nk_layout_format fmt,
  20187. float height, int cols, int width)
  20188. {
  20189. /* update the current row and set the current row layout */
  20190. struct nk_window* win;
  20191. NK_ASSERT(ctx);
  20192. NK_ASSERT(ctx->current);
  20193. NK_ASSERT(ctx->current->layout);
  20194. if (!ctx || !ctx->current || !ctx->current->layout)
  20195. return;
  20196. win = ctx->current;
  20197. nk_panel_layout(ctx, win, height, cols);
  20198. if (fmt == NK_DYNAMIC)
  20199. win->layout->row.type = NK_LAYOUT_DYNAMIC_FIXED;
  20200. else win->layout->row.type = NK_LAYOUT_STATIC_FIXED;
  20201. win->layout->row.ratio = 0;
  20202. win->layout->row.filled = 0;
  20203. win->layout->row.item_offset = 0;
  20204. win->layout->row.item_width = (float)width;
  20205. }
  20206. NK_API float
  20207. nk_layout_ratio_from_pixel(struct nk_context* ctx, float pixel_width)
  20208. {
  20209. struct nk_window* win;
  20210. NK_ASSERT(ctx);
  20211. NK_ASSERT(pixel_width);
  20212. if (!ctx || !ctx->current || !ctx->current->layout) return 0;
  20213. win = ctx->current;
  20214. return NK_CLAMP(0.0f, pixel_width / win->bounds.x, 1.0f);
  20215. }
  20216. NK_API void
  20217. nk_layout_row_dynamic(struct nk_context* ctx, float height, int cols)
  20218. {
  20219. nk_row_layout(ctx, NK_DYNAMIC, height, cols, 0);
  20220. }
  20221. NK_API void
  20222. nk_layout_row_static(struct nk_context* ctx, float height, int item_width, int cols)
  20223. {
  20224. nk_row_layout(ctx, NK_STATIC, height, cols, item_width);
  20225. }
  20226. NK_API void
  20227. nk_layout_row_begin(struct nk_context* ctx, enum nk_layout_format fmt,
  20228. float row_height, int cols)
  20229. {
  20230. struct nk_window* win;
  20231. struct nk_panel* layout;
  20232. NK_ASSERT(ctx);
  20233. NK_ASSERT(ctx->current);
  20234. NK_ASSERT(ctx->current->layout);
  20235. if (!ctx || !ctx->current || !ctx->current->layout)
  20236. return;
  20237. win = ctx->current;
  20238. layout = win->layout;
  20239. nk_panel_layout(ctx, win, row_height, cols);
  20240. if (fmt == NK_DYNAMIC)
  20241. layout->row.type = NK_LAYOUT_DYNAMIC_ROW;
  20242. else layout->row.type = NK_LAYOUT_STATIC_ROW;
  20243. layout->row.ratio = 0;
  20244. layout->row.filled = 0;
  20245. layout->row.item_width = 0;
  20246. layout->row.item_offset = 0;
  20247. layout->row.columns = cols;
  20248. }
  20249. NK_API void
  20250. nk_layout_row_push(struct nk_context* ctx, float ratio_or_width)
  20251. {
  20252. struct nk_window* win;
  20253. struct nk_panel* layout;
  20254. NK_ASSERT(ctx);
  20255. NK_ASSERT(ctx->current);
  20256. NK_ASSERT(ctx->current->layout);
  20257. if (!ctx || !ctx->current || !ctx->current->layout)
  20258. return;
  20259. win = ctx->current;
  20260. layout = win->layout;
  20261. NK_ASSERT(layout->row.type == NK_LAYOUT_STATIC_ROW || layout->row.type == NK_LAYOUT_DYNAMIC_ROW);
  20262. if (layout->row.type != NK_LAYOUT_STATIC_ROW && layout->row.type != NK_LAYOUT_DYNAMIC_ROW)
  20263. return;
  20264. if (layout->row.type == NK_LAYOUT_DYNAMIC_ROW) {
  20265. float ratio = ratio_or_width;
  20266. if ((ratio + layout->row.filled) > 1.0f) return;
  20267. if (ratio > 0.0f)
  20268. layout->row.item_width = NK_SATURATE(ratio);
  20269. else layout->row.item_width = 1.0f - layout->row.filled;
  20270. }
  20271. else layout->row.item_width = ratio_or_width;
  20272. }
  20273. NK_API void
  20274. nk_layout_row_end(struct nk_context* ctx)
  20275. {
  20276. struct nk_window* win;
  20277. struct nk_panel* layout;
  20278. NK_ASSERT(ctx);
  20279. NK_ASSERT(ctx->current);
  20280. NK_ASSERT(ctx->current->layout);
  20281. if (!ctx || !ctx->current || !ctx->current->layout)
  20282. return;
  20283. win = ctx->current;
  20284. layout = win->layout;
  20285. NK_ASSERT(layout->row.type == NK_LAYOUT_STATIC_ROW || layout->row.type == NK_LAYOUT_DYNAMIC_ROW);
  20286. if (layout->row.type != NK_LAYOUT_STATIC_ROW && layout->row.type != NK_LAYOUT_DYNAMIC_ROW)
  20287. return;
  20288. layout->row.item_width = 0;
  20289. layout->row.item_offset = 0;
  20290. }
  20291. NK_API void
  20292. nk_layout_row(struct nk_context* ctx, enum nk_layout_format fmt,
  20293. float height, int cols, const float* ratio)
  20294. {
  20295. int i;
  20296. int n_undef = 0;
  20297. struct nk_window* win;
  20298. struct nk_panel* layout;
  20299. NK_ASSERT(ctx);
  20300. NK_ASSERT(ctx->current);
  20301. NK_ASSERT(ctx->current->layout);
  20302. if (!ctx || !ctx->current || !ctx->current->layout)
  20303. return;
  20304. win = ctx->current;
  20305. layout = win->layout;
  20306. nk_panel_layout(ctx, win, height, cols);
  20307. if (fmt == NK_DYNAMIC) {
  20308. /* calculate width of undefined widget ratios */
  20309. float r = 0;
  20310. layout->row.ratio = ratio;
  20311. for (i = 0; i < cols; ++i) {
  20312. if (ratio[i] < 0.0f)
  20313. n_undef++;
  20314. else r += ratio[i];
  20315. }
  20316. r = NK_SATURATE(1.0f - r);
  20317. layout->row.type = NK_LAYOUT_DYNAMIC;
  20318. layout->row.item_width = (r > 0 && n_undef > 0) ? (r / (float)n_undef) : 0;
  20319. }
  20320. else {
  20321. layout->row.ratio = ratio;
  20322. layout->row.type = NK_LAYOUT_STATIC;
  20323. layout->row.item_width = 0;
  20324. layout->row.item_offset = 0;
  20325. }
  20326. layout->row.item_offset = 0;
  20327. layout->row.filled = 0;
  20328. }
  20329. NK_API void
  20330. nk_layout_row_template_begin(struct nk_context* ctx, float height)
  20331. {
  20332. struct nk_window* win;
  20333. struct nk_panel* layout;
  20334. NK_ASSERT(ctx);
  20335. NK_ASSERT(ctx->current);
  20336. NK_ASSERT(ctx->current->layout);
  20337. if (!ctx || !ctx->current || !ctx->current->layout)
  20338. return;
  20339. win = ctx->current;
  20340. layout = win->layout;
  20341. nk_panel_layout(ctx, win, height, 1);
  20342. layout->row.type = NK_LAYOUT_TEMPLATE;
  20343. layout->row.columns = 0;
  20344. layout->row.ratio = 0;
  20345. layout->row.item_width = 0;
  20346. layout->row.item_height = 0;
  20347. layout->row.item_offset = 0;
  20348. layout->row.filled = 0;
  20349. layout->row.item.x = 0;
  20350. layout->row.item.y = 0;
  20351. layout->row.item.w = 0;
  20352. layout->row.item.h = 0;
  20353. }
  20354. NK_API void
  20355. nk_layout_row_template_push_dynamic(struct nk_context* ctx)
  20356. {
  20357. struct nk_window* win;
  20358. struct nk_panel* layout;
  20359. NK_ASSERT(ctx);
  20360. NK_ASSERT(ctx->current);
  20361. NK_ASSERT(ctx->current->layout);
  20362. if (!ctx || !ctx->current || !ctx->current->layout)
  20363. return;
  20364. win = ctx->current;
  20365. layout = win->layout;
  20366. NK_ASSERT(layout->row.type == NK_LAYOUT_TEMPLATE);
  20367. NK_ASSERT(layout->row.columns < NK_MAX_LAYOUT_ROW_TEMPLATE_COLUMNS);
  20368. if (layout->row.type != NK_LAYOUT_TEMPLATE) return;
  20369. if (layout->row.columns >= NK_MAX_LAYOUT_ROW_TEMPLATE_COLUMNS) return;
  20370. layout->row.templates[layout->row.columns++] = -1.0f;
  20371. }
  20372. NK_API void
  20373. nk_layout_row_template_push_variable(struct nk_context* ctx, float min_width)
  20374. {
  20375. struct nk_window* win;
  20376. struct nk_panel* layout;
  20377. NK_ASSERT(ctx);
  20378. NK_ASSERT(ctx->current);
  20379. NK_ASSERT(ctx->current->layout);
  20380. if (!ctx || !ctx->current || !ctx->current->layout)
  20381. return;
  20382. win = ctx->current;
  20383. layout = win->layout;
  20384. NK_ASSERT(layout->row.type == NK_LAYOUT_TEMPLATE);
  20385. NK_ASSERT(layout->row.columns < NK_MAX_LAYOUT_ROW_TEMPLATE_COLUMNS);
  20386. if (layout->row.type != NK_LAYOUT_TEMPLATE) return;
  20387. if (layout->row.columns >= NK_MAX_LAYOUT_ROW_TEMPLATE_COLUMNS) return;
  20388. layout->row.templates[layout->row.columns++] = -min_width;
  20389. }
  20390. NK_API void
  20391. nk_layout_row_template_push_static(struct nk_context* ctx, float width)
  20392. {
  20393. struct nk_window* win;
  20394. struct nk_panel* layout;
  20395. NK_ASSERT(ctx);
  20396. NK_ASSERT(ctx->current);
  20397. NK_ASSERT(ctx->current->layout);
  20398. if (!ctx || !ctx->current || !ctx->current->layout)
  20399. return;
  20400. win = ctx->current;
  20401. layout = win->layout;
  20402. NK_ASSERT(layout->row.type == NK_LAYOUT_TEMPLATE);
  20403. NK_ASSERT(layout->row.columns < NK_MAX_LAYOUT_ROW_TEMPLATE_COLUMNS);
  20404. if (layout->row.type != NK_LAYOUT_TEMPLATE) return;
  20405. if (layout->row.columns >= NK_MAX_LAYOUT_ROW_TEMPLATE_COLUMNS) return;
  20406. layout->row.templates[layout->row.columns++] = width;
  20407. }
  20408. NK_API void
  20409. nk_layout_row_template_end(struct nk_context* ctx)
  20410. {
  20411. struct nk_window* win;
  20412. struct nk_panel* layout;
  20413. int i = 0;
  20414. int variable_count = 0;
  20415. int min_variable_count = 0;
  20416. float min_fixed_width = 0.0f;
  20417. float total_fixed_width = 0.0f;
  20418. float max_variable_width = 0.0f;
  20419. NK_ASSERT(ctx);
  20420. NK_ASSERT(ctx->current);
  20421. NK_ASSERT(ctx->current->layout);
  20422. if (!ctx || !ctx->current || !ctx->current->layout)
  20423. return;
  20424. win = ctx->current;
  20425. layout = win->layout;
  20426. NK_ASSERT(layout->row.type == NK_LAYOUT_TEMPLATE);
  20427. if (layout->row.type != NK_LAYOUT_TEMPLATE) return;
  20428. for (i = 0; i < layout->row.columns; ++i) {
  20429. float width = layout->row.templates[i];
  20430. if (width >= 0.0f) {
  20431. total_fixed_width += width;
  20432. min_fixed_width += width;
  20433. }
  20434. else if (width < -1.0f) {
  20435. width = -width;
  20436. total_fixed_width += width;
  20437. max_variable_width = NK_MAX(max_variable_width, width);
  20438. variable_count++;
  20439. }
  20440. else {
  20441. min_variable_count++;
  20442. variable_count++;
  20443. }
  20444. }
  20445. if (variable_count) {
  20446. float space = nk_layout_row_calculate_usable_space(&ctx->style, layout->type,
  20447. layout->bounds.w, layout->row.columns);
  20448. float var_width = (NK_MAX(space - min_fixed_width, 0.0f)) / (float)variable_count;
  20449. int enough_space = var_width >= max_variable_width;
  20450. if (!enough_space)
  20451. var_width = (NK_MAX(space - total_fixed_width, 0)) / (float)min_variable_count;
  20452. for (i = 0; i < layout->row.columns; ++i) {
  20453. float* width = &layout->row.templates[i];
  20454. *width = (*width >= 0.0f) ? *width : (*width < -1.0f && !enough_space) ? -(*width) : var_width;
  20455. }
  20456. }
  20457. }
  20458. NK_API void
  20459. nk_layout_space_begin(struct nk_context* ctx, enum nk_layout_format fmt,
  20460. float height, int widget_count)
  20461. {
  20462. struct nk_window* win;
  20463. struct nk_panel* layout;
  20464. NK_ASSERT(ctx);
  20465. NK_ASSERT(ctx->current);
  20466. NK_ASSERT(ctx->current->layout);
  20467. if (!ctx || !ctx->current || !ctx->current->layout)
  20468. return;
  20469. win = ctx->current;
  20470. layout = win->layout;
  20471. nk_panel_layout(ctx, win, height, widget_count);
  20472. if (fmt == NK_STATIC)
  20473. layout->row.type = NK_LAYOUT_STATIC_FREE;
  20474. else layout->row.type = NK_LAYOUT_DYNAMIC_FREE;
  20475. layout->row.ratio = 0;
  20476. layout->row.filled = 0;
  20477. layout->row.item_width = 0;
  20478. layout->row.item_offset = 0;
  20479. }
  20480. NK_API void
  20481. nk_layout_space_end(struct nk_context* ctx)
  20482. {
  20483. struct nk_window* win;
  20484. struct nk_panel* layout;
  20485. NK_ASSERT(ctx);
  20486. NK_ASSERT(ctx->current);
  20487. NK_ASSERT(ctx->current->layout);
  20488. if (!ctx || !ctx->current || !ctx->current->layout)
  20489. return;
  20490. win = ctx->current;
  20491. layout = win->layout;
  20492. layout->row.item_width = 0;
  20493. layout->row.item_height = 0;
  20494. layout->row.item_offset = 0;
  20495. nk_zero(&layout->row.item, sizeof(layout->row.item));
  20496. }
  20497. NK_API void
  20498. nk_layout_space_push(struct nk_context* ctx, struct nk_rect rect)
  20499. {
  20500. struct nk_window* win;
  20501. struct nk_panel* layout;
  20502. NK_ASSERT(ctx);
  20503. NK_ASSERT(ctx->current);
  20504. NK_ASSERT(ctx->current->layout);
  20505. if (!ctx || !ctx->current || !ctx->current->layout)
  20506. return;
  20507. win = ctx->current;
  20508. layout = win->layout;
  20509. layout->row.item = rect;
  20510. }
  20511. NK_API struct nk_rect
  20512. nk_layout_space_bounds(struct nk_context* ctx)
  20513. {
  20514. struct nk_rect ret;
  20515. struct nk_window* win;
  20516. struct nk_panel* layout;
  20517. NK_ASSERT(ctx);
  20518. NK_ASSERT(ctx->current);
  20519. NK_ASSERT(ctx->current->layout);
  20520. win = ctx->current;
  20521. layout = win->layout;
  20522. ret.x = layout->clip.x;
  20523. ret.y = layout->clip.y;
  20524. ret.w = layout->clip.w;
  20525. ret.h = layout->row.height;
  20526. return ret;
  20527. }
  20528. NK_API struct nk_rect
  20529. nk_layout_widget_bounds(struct nk_context* ctx)
  20530. {
  20531. struct nk_rect ret;
  20532. struct nk_window* win;
  20533. struct nk_panel* layout;
  20534. NK_ASSERT(ctx);
  20535. NK_ASSERT(ctx->current);
  20536. NK_ASSERT(ctx->current->layout);
  20537. win = ctx->current;
  20538. layout = win->layout;
  20539. ret.x = layout->at_x;
  20540. ret.y = layout->at_y;
  20541. ret.w = layout->bounds.w - NK_MAX(layout->at_x - layout->bounds.x, 0);
  20542. ret.h = layout->row.height;
  20543. return ret;
  20544. }
  20545. NK_API struct nk_vec2
  20546. nk_layout_space_to_screen(struct nk_context* ctx, struct nk_vec2 ret)
  20547. {
  20548. struct nk_window* win;
  20549. struct nk_panel* layout;
  20550. NK_ASSERT(ctx);
  20551. NK_ASSERT(ctx->current);
  20552. NK_ASSERT(ctx->current->layout);
  20553. win = ctx->current;
  20554. layout = win->layout;
  20555. ret.x += layout->at_x - (float)*layout->offset_x;
  20556. ret.y += layout->at_y - (float)*layout->offset_y;
  20557. return ret;
  20558. }
  20559. NK_API struct nk_vec2
  20560. nk_layout_space_to_local(struct nk_context* ctx, struct nk_vec2 ret)
  20561. {
  20562. struct nk_window* win;
  20563. struct nk_panel* layout;
  20564. NK_ASSERT(ctx);
  20565. NK_ASSERT(ctx->current);
  20566. NK_ASSERT(ctx->current->layout);
  20567. win = ctx->current;
  20568. layout = win->layout;
  20569. ret.x += -layout->at_x + (float)*layout->offset_x;
  20570. ret.y += -layout->at_y + (float)*layout->offset_y;
  20571. return ret;
  20572. }
  20573. NK_API struct nk_rect
  20574. nk_layout_space_rect_to_screen(struct nk_context* ctx, struct nk_rect ret)
  20575. {
  20576. struct nk_window* win;
  20577. struct nk_panel* layout;
  20578. NK_ASSERT(ctx);
  20579. NK_ASSERT(ctx->current);
  20580. NK_ASSERT(ctx->current->layout);
  20581. win = ctx->current;
  20582. layout = win->layout;
  20583. ret.x += layout->at_x - (float)*layout->offset_x;
  20584. ret.y += layout->at_y - (float)*layout->offset_y;
  20585. return ret;
  20586. }
  20587. NK_API struct nk_rect
  20588. nk_layout_space_rect_to_local(struct nk_context* ctx, struct nk_rect ret)
  20589. {
  20590. struct nk_window* win;
  20591. struct nk_panel* layout;
  20592. NK_ASSERT(ctx);
  20593. NK_ASSERT(ctx->current);
  20594. NK_ASSERT(ctx->current->layout);
  20595. win = ctx->current;
  20596. layout = win->layout;
  20597. ret.x += -layout->at_x + (float)*layout->offset_x;
  20598. ret.y += -layout->at_y + (float)*layout->offset_y;
  20599. return ret;
  20600. }
  20601. NK_LIB void
  20602. nk_panel_alloc_row(const struct nk_context* ctx, struct nk_window* win)
  20603. {
  20604. struct nk_panel* layout = win->layout;
  20605. struct nk_vec2 spacing = ctx->style.window.spacing;
  20606. const float row_height = layout->row.height - spacing.y;
  20607. nk_panel_layout(ctx, win, row_height, layout->row.columns);
  20608. }
  20609. NK_LIB void
  20610. nk_layout_widget_space(struct nk_rect* bounds, const struct nk_context* ctx,
  20611. struct nk_window* win, int modify)
  20612. {
  20613. struct nk_panel* layout;
  20614. const struct nk_style* style;
  20615. struct nk_vec2 spacing;
  20616. float item_offset = 0;
  20617. float item_width = 0;
  20618. float item_spacing = 0;
  20619. float panel_space = 0;
  20620. NK_ASSERT(ctx);
  20621. NK_ASSERT(ctx->current);
  20622. NK_ASSERT(ctx->current->layout);
  20623. if (!ctx || !ctx->current || !ctx->current->layout)
  20624. return;
  20625. win = ctx->current;
  20626. layout = win->layout;
  20627. style = &ctx->style;
  20628. NK_ASSERT(bounds);
  20629. spacing = style->window.spacing;
  20630. panel_space = nk_layout_row_calculate_usable_space(&ctx->style, layout->type,
  20631. layout->bounds.w, layout->row.columns);
  20632. #define NK_FRAC(x) (x - (float)(int)x) /* will be used to remove fookin gaps */
  20633. /* calculate the width of one item inside the current layout space */
  20634. switch (layout->row.type) {
  20635. case NK_LAYOUT_DYNAMIC_FIXED: {
  20636. /* scaling fixed size widgets item width */
  20637. float w = NK_MAX(1.0f, panel_space) / (float)layout->row.columns;
  20638. item_offset = (float)layout->row.index * w;
  20639. item_width = w + NK_FRAC(item_offset);
  20640. item_spacing = (float)layout->row.index * spacing.x;
  20641. } break;
  20642. case NK_LAYOUT_DYNAMIC_ROW: {
  20643. /* scaling single ratio widget width */
  20644. float w = layout->row.item_width * panel_space;
  20645. item_offset = layout->row.item_offset;
  20646. item_width = w + NK_FRAC(item_offset);
  20647. item_spacing = 0;
  20648. if (modify) {
  20649. layout->row.item_offset += w + spacing.x;
  20650. layout->row.filled += layout->row.item_width;
  20651. layout->row.index = 0;
  20652. }
  20653. } break;
  20654. case NK_LAYOUT_DYNAMIC_FREE: {
  20655. /* panel width depended free widget placing */
  20656. bounds->x = layout->at_x + (layout->bounds.w * layout->row.item.x);
  20657. bounds->x -= (float)*layout->offset_x;
  20658. bounds->y = layout->at_y + (layout->row.height * layout->row.item.y);
  20659. bounds->y -= (float)*layout->offset_y;
  20660. bounds->w = layout->bounds.w * layout->row.item.w + NK_FRAC(bounds->x);
  20661. bounds->h = layout->row.height * layout->row.item.h + NK_FRAC(bounds->y);
  20662. return;
  20663. }
  20664. case NK_LAYOUT_DYNAMIC: {
  20665. /* scaling arrays of panel width ratios for every widget */
  20666. float ratio, w;
  20667. NK_ASSERT(layout->row.ratio);
  20668. ratio = (layout->row.ratio[layout->row.index] < 0) ?
  20669. layout->row.item_width : layout->row.ratio[layout->row.index];
  20670. w = (ratio * panel_space);
  20671. item_spacing = (float)layout->row.index * spacing.x;
  20672. item_offset = layout->row.item_offset;
  20673. item_width = w + NK_FRAC(item_offset);
  20674. if (modify) {
  20675. layout->row.item_offset += w;
  20676. layout->row.filled += ratio;
  20677. }
  20678. } break;
  20679. case NK_LAYOUT_STATIC_FIXED: {
  20680. /* non-scaling fixed widgets item width */
  20681. item_width = layout->row.item_width;
  20682. item_offset = (float)layout->row.index * item_width;
  20683. item_spacing = (float)layout->row.index * spacing.x;
  20684. } break;
  20685. case NK_LAYOUT_STATIC_ROW: {
  20686. /* scaling single ratio widget width */
  20687. item_width = layout->row.item_width;
  20688. item_offset = layout->row.item_offset;
  20689. item_spacing = (float)layout->row.index * spacing.x;
  20690. if (modify) layout->row.item_offset += item_width;
  20691. } break;
  20692. case NK_LAYOUT_STATIC_FREE: {
  20693. /* free widget placing */
  20694. bounds->x = layout->at_x + layout->row.item.x;
  20695. bounds->w = layout->row.item.w;
  20696. if (((bounds->x + bounds->w) > layout->max_x) && modify)
  20697. layout->max_x = (bounds->x + bounds->w);
  20698. bounds->x -= (float)*layout->offset_x;
  20699. bounds->y = layout->at_y + layout->row.item.y;
  20700. bounds->y -= (float)*layout->offset_y;
  20701. bounds->h = layout->row.item.h;
  20702. return;
  20703. }
  20704. case NK_LAYOUT_STATIC: {
  20705. /* non-scaling array of panel pixel width for every widget */
  20706. item_spacing = (float)layout->row.index * spacing.x;
  20707. item_width = layout->row.ratio[layout->row.index];
  20708. item_offset = layout->row.item_offset;
  20709. if (modify) layout->row.item_offset += item_width;
  20710. } break;
  20711. case NK_LAYOUT_TEMPLATE: {
  20712. /* stretchy row layout with combined dynamic/static widget width*/
  20713. float w;
  20714. NK_ASSERT(layout->row.index < layout->row.columns);
  20715. NK_ASSERT(layout->row.index < NK_MAX_LAYOUT_ROW_TEMPLATE_COLUMNS);
  20716. w = layout->row.templates[layout->row.index];
  20717. item_offset = layout->row.item_offset;
  20718. item_width = w + NK_FRAC(item_offset);
  20719. item_spacing = (float)layout->row.index * spacing.x;
  20720. if (modify) layout->row.item_offset += w;
  20721. } break;
  20722. #undef NK_FRAC
  20723. default: NK_ASSERT(0); break;
  20724. };
  20725. /* set the bounds of the newly allocated widget */
  20726. bounds->w = item_width;
  20727. bounds->h = layout->row.height - spacing.y;
  20728. bounds->y = layout->at_y - (float)*layout->offset_y;
  20729. bounds->x = layout->at_x + item_offset + item_spacing;
  20730. if (((bounds->x + bounds->w) > layout->max_x) && modify)
  20731. layout->max_x = bounds->x + bounds->w;
  20732. bounds->x -= (float)*layout->offset_x;
  20733. }
  20734. NK_LIB void
  20735. nk_panel_alloc_space(struct nk_rect* bounds, const struct nk_context* ctx)
  20736. {
  20737. struct nk_window* win;
  20738. struct nk_panel* layout;
  20739. NK_ASSERT(ctx);
  20740. NK_ASSERT(ctx->current);
  20741. NK_ASSERT(ctx->current->layout);
  20742. if (!ctx || !ctx->current || !ctx->current->layout)
  20743. return;
  20744. /* check if the end of the row has been hit and begin new row if so */
  20745. win = ctx->current;
  20746. layout = win->layout;
  20747. if (layout->row.index >= layout->row.columns)
  20748. nk_panel_alloc_row(ctx, win);
  20749. /* calculate widget position and size */
  20750. nk_layout_widget_space(bounds, ctx, win, nk_true);
  20751. layout->row.index++;
  20752. }
  20753. NK_LIB void
  20754. nk_layout_peek(struct nk_rect* bounds, struct nk_context* ctx)
  20755. {
  20756. float y;
  20757. int index;
  20758. struct nk_window* win;
  20759. struct nk_panel* layout;
  20760. NK_ASSERT(ctx);
  20761. NK_ASSERT(ctx->current);
  20762. NK_ASSERT(ctx->current->layout);
  20763. if (!ctx || !ctx->current || !ctx->current->layout) {
  20764. *bounds = nk_rect(0, 0, 0, 0);
  20765. return;
  20766. }
  20767. win = ctx->current;
  20768. layout = win->layout;
  20769. y = layout->at_y;
  20770. index = layout->row.index;
  20771. if (layout->row.index >= layout->row.columns) {
  20772. layout->at_y += layout->row.height;
  20773. layout->row.index = 0;
  20774. }
  20775. nk_layout_widget_space(bounds, ctx, win, nk_false);
  20776. if (!layout->row.index) {
  20777. bounds->x -= layout->row.item_offset;
  20778. }
  20779. layout->at_y = y;
  20780. layout->row.index = index;
  20781. }
  20782. NK_API void
  20783. nk_spacer(struct nk_context* ctx)
  20784. {
  20785. struct nk_rect dummy_rect = { 0, 0, 0, 0 };
  20786. nk_panel_alloc_space(&dummy_rect, ctx);
  20787. }
  20788. /* ===============================================================
  20789. *
  20790. * TREE
  20791. *
  20792. * ===============================================================*/
  20793. NK_INTERN int
  20794. nk_tree_state_base(struct nk_context* ctx, enum nk_tree_type type,
  20795. struct nk_image* img, const char* title, enum nk_collapse_states* state)
  20796. {
  20797. struct nk_window* win;
  20798. struct nk_panel* layout;
  20799. const struct nk_style* style;
  20800. struct nk_command_buffer* out;
  20801. const struct nk_input* in;
  20802. const struct nk_style_button* button;
  20803. enum nk_symbol_type symbol;
  20804. float row_height;
  20805. struct nk_vec2 item_spacing;
  20806. struct nk_rect header = { 0,0,0,0 };
  20807. struct nk_rect sym = { 0,0,0,0 };
  20808. struct nk_text text;
  20809. nk_flags ws = 0;
  20810. enum nk_widget_layout_states widget_state;
  20811. NK_ASSERT(ctx);
  20812. NK_ASSERT(ctx->current);
  20813. NK_ASSERT(ctx->current->layout);
  20814. if (!ctx || !ctx->current || !ctx->current->layout)
  20815. return 0;
  20816. /* cache some data */
  20817. win = ctx->current;
  20818. layout = win->layout;
  20819. out = &win->buffer;
  20820. style = &ctx->style;
  20821. item_spacing = style->window.spacing;
  20822. /* calculate header bounds and draw background */
  20823. row_height = style->font->height + 2 * style->tab.padding.y;
  20824. nk_layout_set_min_row_height(ctx, row_height);
  20825. nk_layout_row_dynamic(ctx, row_height, 1);
  20826. nk_layout_reset_min_row_height(ctx);
  20827. widget_state = nk_widget(&header, ctx);
  20828. if (type == NK_TREE_TAB) {
  20829. const struct nk_style_item* background = &style->tab.background;
  20830. switch (background->type) {
  20831. case NK_STYLE_ITEM_IMAGE:
  20832. nk_draw_image(out, header, &background->data.image, nk_white);
  20833. break;
  20834. case NK_STYLE_ITEM_NINE_SLICE:
  20835. nk_draw_nine_slice(out, header, &background->data.slice, nk_white);
  20836. break;
  20837. case NK_STYLE_ITEM_COLOR:
  20838. nk_fill_rect(out, header, 0, style->tab.border_color);
  20839. nk_fill_rect(out, nk_shrink_rect(header, style->tab.border),
  20840. style->tab.rounding, background->data.color);
  20841. break;
  20842. }
  20843. }
  20844. else text.background = style->window.background;
  20845. /* update node state */
  20846. in = (!(layout->flags & NK_WINDOW_ROM)) ? &ctx->input : 0;
  20847. in = (in && widget_state == NK_WIDGET_VALID) ? &ctx->input : 0;
  20848. if (nk_button_behavior(&ws, header, in, NK_BUTTON_DEFAULT))
  20849. *state = (*state == NK_MAXIMIZED) ? NK_MINIMIZED : NK_MAXIMIZED;
  20850. /* select correct button style */
  20851. if (*state == NK_MAXIMIZED) {
  20852. symbol = style->tab.sym_maximize;
  20853. if (type == NK_TREE_TAB)
  20854. button = &style->tab.tab_maximize_button;
  20855. else button = &style->tab.node_maximize_button;
  20856. }
  20857. else {
  20858. symbol = style->tab.sym_minimize;
  20859. if (type == NK_TREE_TAB)
  20860. button = &style->tab.tab_minimize_button;
  20861. else button = &style->tab.node_minimize_button;
  20862. }
  20863. {/* draw triangle button */
  20864. sym.w = sym.h = style->font->height;
  20865. sym.y = header.y + style->tab.padding.y;
  20866. sym.x = header.x + style->tab.padding.x;
  20867. nk_do_button_symbol(&ws, &win->buffer, sym, symbol, NK_BUTTON_DEFAULT,
  20868. button, 0, style->font);
  20869. if (img) {
  20870. /* draw optional image icon */
  20871. sym.x = sym.x + sym.w + 4 * item_spacing.x;
  20872. nk_draw_image(&win->buffer, sym, img, nk_white);
  20873. sym.w = style->font->height + style->tab.spacing.x;
  20874. }
  20875. }
  20876. {/* draw label */
  20877. struct nk_rect label;
  20878. header.w = NK_MAX(header.w, sym.w + item_spacing.x);
  20879. label.x = sym.x + sym.w + item_spacing.x;
  20880. label.y = sym.y;
  20881. label.w = header.w - (sym.w + item_spacing.y + style->tab.indent);
  20882. label.h = style->font->height;
  20883. text.text = style->tab.text;
  20884. text.padding = nk_vec2(0, 0);
  20885. nk_widget_text(out, label, title, nk_strlen(title), &text,
  20886. NK_TEXT_LEFT, style->font); }
  20887. /* increase x-axis cursor widget position pointer */
  20888. if (*state == NK_MAXIMIZED) {
  20889. layout->at_x = header.x + (float)*layout->offset_x + style->tab.indent;
  20890. layout->bounds.w = NK_MAX(layout->bounds.w, style->tab.indent);
  20891. layout->bounds.w -= (style->tab.indent + style->window.padding.x);
  20892. layout->row.tree_depth++;
  20893. return nk_true;
  20894. }
  20895. else return nk_false;
  20896. }
  20897. NK_INTERN int
  20898. nk_tree_base(struct nk_context* ctx, enum nk_tree_type type,
  20899. struct nk_image* img, const char* title, enum nk_collapse_states initial_state,
  20900. const char* hash, int len, int line)
  20901. {
  20902. struct nk_window* win = ctx->current;
  20903. int title_len = 0;
  20904. nk_hash tree_hash = 0;
  20905. nk_uint* state = 0;
  20906. /* retrieve tree state from internal widget state tables */
  20907. if (!hash) {
  20908. title_len = (int)nk_strlen(title);
  20909. tree_hash = nk_murmur_hash(title, (int)title_len, (nk_hash)line);
  20910. }
  20911. else tree_hash = nk_murmur_hash(hash, len, (nk_hash)line);
  20912. state = nk_find_value(win, tree_hash);
  20913. if (!state) {
  20914. state = nk_add_value(ctx, win, tree_hash, 0);
  20915. *state = initial_state;
  20916. }
  20917. return nk_tree_state_base(ctx, type, img, title, (enum nk_collapse_states*)state);
  20918. }
  20919. NK_API nk_bool
  20920. nk_tree_state_push(struct nk_context* ctx, enum nk_tree_type type,
  20921. const char* title, enum nk_collapse_states* state)
  20922. {
  20923. return nk_tree_state_base(ctx, type, 0, title, state);
  20924. }
  20925. NK_API nk_bool
  20926. nk_tree_state_image_push(struct nk_context* ctx, enum nk_tree_type type,
  20927. struct nk_image img, const char* title, enum nk_collapse_states* state)
  20928. {
  20929. return nk_tree_state_base(ctx, type, &img, title, state);
  20930. }
  20931. NK_API void
  20932. nk_tree_state_pop(struct nk_context* ctx)
  20933. {
  20934. struct nk_window* win = 0;
  20935. struct nk_panel* layout = 0;
  20936. NK_ASSERT(ctx);
  20937. NK_ASSERT(ctx->current);
  20938. NK_ASSERT(ctx->current->layout);
  20939. if (!ctx || !ctx->current || !ctx->current->layout)
  20940. return;
  20941. win = ctx->current;
  20942. layout = win->layout;
  20943. layout->at_x -= ctx->style.tab.indent + (float)*layout->offset_x;
  20944. layout->bounds.w += ctx->style.tab.indent + ctx->style.window.padding.x;
  20945. NK_ASSERT(layout->row.tree_depth);
  20946. layout->row.tree_depth--;
  20947. }
  20948. NK_API nk_bool
  20949. nk_tree_push_hashed(struct nk_context* ctx, enum nk_tree_type type,
  20950. const char* title, enum nk_collapse_states initial_state,
  20951. const char* hash, int len, int line)
  20952. {
  20953. return nk_tree_base(ctx, type, 0, title, initial_state, hash, len, line);
  20954. }
  20955. NK_API nk_bool
  20956. nk_tree_image_push_hashed(struct nk_context* ctx, enum nk_tree_type type,
  20957. struct nk_image img, const char* title, enum nk_collapse_states initial_state,
  20958. const char* hash, int len, int seed)
  20959. {
  20960. return nk_tree_base(ctx, type, &img, title, initial_state, hash, len, seed);
  20961. }
  20962. NK_API void
  20963. nk_tree_pop(struct nk_context* ctx)
  20964. {
  20965. nk_tree_state_pop(ctx);
  20966. }
  20967. NK_INTERN int
  20968. nk_tree_element_image_push_hashed_base(struct nk_context* ctx, enum nk_tree_type type,
  20969. struct nk_image* img, const char* title, int title_len,
  20970. enum nk_collapse_states* state, nk_bool* selected)
  20971. {
  20972. struct nk_window* win;
  20973. struct nk_panel* layout;
  20974. const struct nk_style* style;
  20975. struct nk_command_buffer* out;
  20976. const struct nk_input* in;
  20977. const struct nk_style_button* button;
  20978. enum nk_symbol_type symbol;
  20979. float row_height;
  20980. struct nk_vec2 padding;
  20981. int text_len;
  20982. float text_width;
  20983. struct nk_vec2 item_spacing;
  20984. struct nk_rect header = { 0,0,0,0 };
  20985. struct nk_rect sym = { 0,0,0,0 };
  20986. nk_flags ws = 0;
  20987. enum nk_widget_layout_states widget_state;
  20988. NK_ASSERT(ctx);
  20989. NK_ASSERT(ctx->current);
  20990. NK_ASSERT(ctx->current->layout);
  20991. if (!ctx || !ctx->current || !ctx->current->layout)
  20992. return 0;
  20993. /* cache some data */
  20994. win = ctx->current;
  20995. layout = win->layout;
  20996. out = &win->buffer;
  20997. style = &ctx->style;
  20998. item_spacing = style->window.spacing;
  20999. padding = style->selectable.padding;
  21000. /* calculate header bounds and draw background */
  21001. row_height = style->font->height + 2 * style->tab.padding.y;
  21002. nk_layout_set_min_row_height(ctx, row_height);
  21003. nk_layout_row_dynamic(ctx, row_height, 1);
  21004. nk_layout_reset_min_row_height(ctx);
  21005. widget_state = nk_widget(&header, ctx);
  21006. if (type == NK_TREE_TAB) {
  21007. const struct nk_style_item* background = &style->tab.background;
  21008. switch (background->type) {
  21009. case NK_STYLE_ITEM_IMAGE:
  21010. nk_draw_image(out, header, &background->data.image, nk_white);
  21011. break;
  21012. case NK_STYLE_ITEM_NINE_SLICE:
  21013. nk_draw_nine_slice(out, header, &background->data.slice, nk_white);
  21014. break;
  21015. case NK_STYLE_ITEM_COLOR:
  21016. nk_fill_rect(out, header, 0, style->tab.border_color);
  21017. nk_fill_rect(out, nk_shrink_rect(header, style->tab.border),
  21018. style->tab.rounding, background->data.color);
  21019. break;
  21020. }
  21021. }
  21022. in = (!(layout->flags & NK_WINDOW_ROM)) ? &ctx->input : 0;
  21023. in = (in && widget_state == NK_WIDGET_VALID) ? &ctx->input : 0;
  21024. /* select correct button style */
  21025. if (*state == NK_MAXIMIZED) {
  21026. symbol = style->tab.sym_maximize;
  21027. if (type == NK_TREE_TAB)
  21028. button = &style->tab.tab_maximize_button;
  21029. else button = &style->tab.node_maximize_button;
  21030. }
  21031. else {
  21032. symbol = style->tab.sym_minimize;
  21033. if (type == NK_TREE_TAB)
  21034. button = &style->tab.tab_minimize_button;
  21035. else button = &style->tab.node_minimize_button;
  21036. }
  21037. {/* draw triangle button */
  21038. sym.w = sym.h = style->font->height;
  21039. sym.y = header.y + style->tab.padding.y;
  21040. sym.x = header.x + style->tab.padding.x;
  21041. if (nk_do_button_symbol(&ws, &win->buffer, sym, symbol, NK_BUTTON_DEFAULT, button, in, style->font))
  21042. *state = (*state == NK_MAXIMIZED) ? NK_MINIMIZED : NK_MAXIMIZED; }
  21043. /* draw label */
  21044. {nk_flags dummy = 0;
  21045. struct nk_rect label;
  21046. /* calculate size of the text and tooltip */
  21047. text_len = nk_strlen(title);
  21048. text_width = style->font->width(style->font->userdata, style->font->height, title, text_len);
  21049. text_width += (4 * padding.x);
  21050. header.w = NK_MAX(header.w, sym.w + item_spacing.x);
  21051. label.x = sym.x + sym.w + item_spacing.x;
  21052. label.y = sym.y;
  21053. label.w = NK_MIN(header.w - (sym.w + item_spacing.y + style->tab.indent), text_width);
  21054. label.h = style->font->height;
  21055. if (img) {
  21056. nk_do_selectable_image(&dummy, &win->buffer, label, title, title_len, NK_TEXT_LEFT,
  21057. selected, img, &style->selectable, in, style->font);
  21058. }
  21059. else nk_do_selectable(&dummy, &win->buffer, label, title, title_len, NK_TEXT_LEFT,
  21060. selected, &style->selectable, in, style->font);
  21061. }
  21062. /* increase x-axis cursor widget position pointer */
  21063. if (*state == NK_MAXIMIZED) {
  21064. layout->at_x = header.x + (float)*layout->offset_x + style->tab.indent;
  21065. layout->bounds.w = NK_MAX(layout->bounds.w, style->tab.indent);
  21066. layout->bounds.w -= (style->tab.indent + style->window.padding.x);
  21067. layout->row.tree_depth++;
  21068. return nk_true;
  21069. }
  21070. else return nk_false;
  21071. }
  21072. NK_INTERN int
  21073. nk_tree_element_base(struct nk_context* ctx, enum nk_tree_type type,
  21074. struct nk_image* img, const char* title, enum nk_collapse_states initial_state,
  21075. nk_bool* selected, const char* hash, int len, int line)
  21076. {
  21077. struct nk_window* win = ctx->current;
  21078. int title_len = 0;
  21079. nk_hash tree_hash = 0;
  21080. nk_uint* state = 0;
  21081. /* retrieve tree state from internal widget state tables */
  21082. if (!hash) {
  21083. title_len = (int)nk_strlen(title);
  21084. tree_hash = nk_murmur_hash(title, (int)title_len, (nk_hash)line);
  21085. }
  21086. else tree_hash = nk_murmur_hash(hash, len, (nk_hash)line);
  21087. state = nk_find_value(win, tree_hash);
  21088. if (!state) {
  21089. state = nk_add_value(ctx, win, tree_hash, 0);
  21090. *state = initial_state;
  21091. } return nk_tree_element_image_push_hashed_base(ctx, type, img, title,
  21092. nk_strlen(title), (enum nk_collapse_states*)state, selected);
  21093. }
  21094. NK_API nk_bool
  21095. nk_tree_element_push_hashed(struct nk_context* ctx, enum nk_tree_type type,
  21096. const char* title, enum nk_collapse_states initial_state,
  21097. nk_bool* selected, const char* hash, int len, int seed)
  21098. {
  21099. return nk_tree_element_base(ctx, type, 0, title, initial_state, selected, hash, len, seed);
  21100. }
  21101. NK_API nk_bool
  21102. nk_tree_element_image_push_hashed(struct nk_context* ctx, enum nk_tree_type type,
  21103. struct nk_image img, const char* title, enum nk_collapse_states initial_state,
  21104. nk_bool* selected, const char* hash, int len, int seed)
  21105. {
  21106. return nk_tree_element_base(ctx, type, &img, title, initial_state, selected, hash, len, seed);
  21107. }
  21108. NK_API void
  21109. nk_tree_element_pop(struct nk_context* ctx)
  21110. {
  21111. nk_tree_state_pop(ctx);
  21112. }
  21113. /* ===============================================================
  21114. *
  21115. * GROUP
  21116. *
  21117. * ===============================================================*/
  21118. NK_API nk_bool
  21119. nk_group_scrolled_offset_begin(struct nk_context* ctx,
  21120. nk_uint* x_offset, nk_uint* y_offset, const char* title, nk_flags flags)
  21121. {
  21122. struct nk_rect bounds;
  21123. struct nk_window panel;
  21124. struct nk_window* win;
  21125. win = ctx->current;
  21126. nk_panel_alloc_space(&bounds, ctx);
  21127. {const struct nk_rect* c = &win->layout->clip;
  21128. if (!NK_INTERSECT(c->x, c->y, c->w, c->h, bounds.x, bounds.y, bounds.w, bounds.h) &&
  21129. !(flags & NK_WINDOW_MOVABLE)) {
  21130. return 0;
  21131. }}
  21132. if (win->flags & NK_WINDOW_ROM)
  21133. flags |= NK_WINDOW_ROM;
  21134. /* initialize a fake window to create the panel from */
  21135. nk_zero(&panel, sizeof(panel));
  21136. panel.bounds = bounds;
  21137. panel.flags = flags;
  21138. panel.scrollbar.x = *x_offset;
  21139. panel.scrollbar.y = *y_offset;
  21140. panel.buffer = win->buffer;
  21141. panel.layout = (struct nk_panel*)nk_create_panel(ctx);
  21142. ctx->current = &panel;
  21143. nk_panel_begin(ctx, (flags & NK_WINDOW_TITLE) ? title : 0, NK_PANEL_GROUP);
  21144. win->buffer = panel.buffer;
  21145. win->buffer.clip = panel.layout->clip;
  21146. panel.layout->offset_x = x_offset;
  21147. panel.layout->offset_y = y_offset;
  21148. panel.layout->parent = win->layout;
  21149. win->layout = panel.layout;
  21150. ctx->current = win;
  21151. if ((panel.layout->flags & NK_WINDOW_CLOSED) ||
  21152. (panel.layout->flags & NK_WINDOW_MINIMIZED))
  21153. {
  21154. nk_flags f = panel.layout->flags;
  21155. nk_group_scrolled_end(ctx);
  21156. if (f & NK_WINDOW_CLOSED)
  21157. return NK_WINDOW_CLOSED;
  21158. if (f & NK_WINDOW_MINIMIZED)
  21159. return NK_WINDOW_MINIMIZED;
  21160. }
  21161. return 1;
  21162. }
  21163. NK_API void
  21164. nk_group_scrolled_end(struct nk_context* ctx)
  21165. {
  21166. struct nk_window* win;
  21167. struct nk_panel* parent;
  21168. struct nk_panel* g;
  21169. struct nk_rect clip;
  21170. struct nk_window pan;
  21171. struct nk_vec2 panel_padding;
  21172. NK_ASSERT(ctx);
  21173. NK_ASSERT(ctx->current);
  21174. if (!ctx || !ctx->current)
  21175. return;
  21176. /* make sure nk_group_begin was called correctly */
  21177. NK_ASSERT(ctx->current);
  21178. win = ctx->current;
  21179. NK_ASSERT(win->layout);
  21180. g = win->layout;
  21181. NK_ASSERT(g->parent);
  21182. parent = g->parent;
  21183. /* dummy window */
  21184. nk_zero_struct(pan);
  21185. panel_padding = nk_panel_get_padding(&ctx->style, NK_PANEL_GROUP);
  21186. pan.bounds.y = g->bounds.y - (g->header_height + g->menu.h);
  21187. pan.bounds.x = g->bounds.x - panel_padding.x;
  21188. pan.bounds.w = g->bounds.w + 2 * panel_padding.x;
  21189. pan.bounds.h = g->bounds.h + g->header_height + g->menu.h;
  21190. if (g->flags & NK_WINDOW_BORDER) {
  21191. pan.bounds.x -= g->border;
  21192. pan.bounds.y -= g->border;
  21193. pan.bounds.w += 2 * g->border;
  21194. pan.bounds.h += 2 * g->border;
  21195. }
  21196. if (!(g->flags & NK_WINDOW_NO_SCROLLBAR)) {
  21197. pan.bounds.w += ctx->style.window.scrollbar_size.x;
  21198. pan.bounds.h += ctx->style.window.scrollbar_size.y;
  21199. }
  21200. pan.scrollbar.x = *g->offset_x;
  21201. pan.scrollbar.y = *g->offset_y;
  21202. pan.flags = g->flags;
  21203. pan.buffer = win->buffer;
  21204. pan.layout = g;
  21205. pan.parent = win;
  21206. ctx->current = &pan;
  21207. /* make sure group has correct clipping rectangle */
  21208. nk_unify(&clip, &parent->clip, pan.bounds.x, pan.bounds.y,
  21209. pan.bounds.x + pan.bounds.w, pan.bounds.y + pan.bounds.h + panel_padding.x);
  21210. nk_push_scissor(&pan.buffer, clip);
  21211. nk_end(ctx);
  21212. win->buffer = pan.buffer;
  21213. nk_push_scissor(&win->buffer, parent->clip);
  21214. ctx->current = win;
  21215. win->layout = parent;
  21216. g->bounds = pan.bounds;
  21217. return;
  21218. }
  21219. NK_API nk_bool
  21220. nk_group_scrolled_begin(struct nk_context* ctx,
  21221. struct nk_scroll* scroll, const char* title, nk_flags flags)
  21222. {
  21223. return nk_group_scrolled_offset_begin(ctx, &scroll->x, &scroll->y, title, flags);
  21224. }
  21225. NK_API nk_bool
  21226. nk_group_begin_titled(struct nk_context* ctx, const char* id,
  21227. const char* title, nk_flags flags)
  21228. {
  21229. int id_len;
  21230. nk_hash id_hash;
  21231. struct nk_window* win;
  21232. nk_uint* x_offset;
  21233. nk_uint* y_offset;
  21234. NK_ASSERT(ctx);
  21235. NK_ASSERT(id);
  21236. NK_ASSERT(ctx->current);
  21237. NK_ASSERT(ctx->current->layout);
  21238. if (!ctx || !ctx->current || !ctx->current->layout || !id)
  21239. return 0;
  21240. /* find persistent group scrollbar value */
  21241. win = ctx->current;
  21242. id_len = (int)nk_strlen(id);
  21243. id_hash = nk_murmur_hash(id, (int)id_len, NK_PANEL_GROUP);
  21244. x_offset = nk_find_value(win, id_hash);
  21245. if (!x_offset) {
  21246. x_offset = nk_add_value(ctx, win, id_hash, 0);
  21247. y_offset = nk_add_value(ctx, win, id_hash + 1, 0);
  21248. NK_ASSERT(x_offset);
  21249. NK_ASSERT(y_offset);
  21250. if (!x_offset || !y_offset) return 0;
  21251. *x_offset = *y_offset = 0;
  21252. }
  21253. else y_offset = nk_find_value(win, id_hash + 1);
  21254. return nk_group_scrolled_offset_begin(ctx, x_offset, y_offset, title, flags);
  21255. }
  21256. NK_API nk_bool
  21257. nk_group_begin(struct nk_context* ctx, const char* title, nk_flags flags)
  21258. {
  21259. return nk_group_begin_titled(ctx, title, title, flags);
  21260. }
  21261. NK_API void
  21262. nk_group_end(struct nk_context* ctx)
  21263. {
  21264. nk_group_scrolled_end(ctx);
  21265. }
  21266. NK_API void
  21267. nk_group_get_scroll(struct nk_context* ctx, const char* id, nk_uint* x_offset, nk_uint* y_offset)
  21268. {
  21269. int id_len;
  21270. nk_hash id_hash;
  21271. struct nk_window* win;
  21272. nk_uint* x_offset_ptr;
  21273. nk_uint* y_offset_ptr;
  21274. NK_ASSERT(ctx);
  21275. NK_ASSERT(id);
  21276. NK_ASSERT(ctx->current);
  21277. NK_ASSERT(ctx->current->layout);
  21278. if (!ctx || !ctx->current || !ctx->current->layout || !id)
  21279. return;
  21280. /* find persistent group scrollbar value */
  21281. win = ctx->current;
  21282. id_len = (int)nk_strlen(id);
  21283. id_hash = nk_murmur_hash(id, (int)id_len, NK_PANEL_GROUP);
  21284. x_offset_ptr = nk_find_value(win, id_hash);
  21285. if (!x_offset_ptr) {
  21286. x_offset_ptr = nk_add_value(ctx, win, id_hash, 0);
  21287. y_offset_ptr = nk_add_value(ctx, win, id_hash + 1, 0);
  21288. NK_ASSERT(x_offset_ptr);
  21289. NK_ASSERT(y_offset_ptr);
  21290. if (!x_offset_ptr || !y_offset_ptr) return;
  21291. *x_offset_ptr = *y_offset_ptr = 0;
  21292. }
  21293. else y_offset_ptr = nk_find_value(win, id_hash + 1);
  21294. if (x_offset)
  21295. *x_offset = *x_offset_ptr;
  21296. if (y_offset)
  21297. *y_offset = *y_offset_ptr;
  21298. }
  21299. NK_API void
  21300. nk_group_set_scroll(struct nk_context* ctx, const char* id, nk_uint x_offset, nk_uint y_offset)
  21301. {
  21302. int id_len;
  21303. nk_hash id_hash;
  21304. struct nk_window* win;
  21305. nk_uint* x_offset_ptr;
  21306. nk_uint* y_offset_ptr;
  21307. NK_ASSERT(ctx);
  21308. NK_ASSERT(id);
  21309. NK_ASSERT(ctx->current);
  21310. NK_ASSERT(ctx->current->layout);
  21311. if (!ctx || !ctx->current || !ctx->current->layout || !id)
  21312. return;
  21313. /* find persistent group scrollbar value */
  21314. win = ctx->current;
  21315. id_len = (int)nk_strlen(id);
  21316. id_hash = nk_murmur_hash(id, (int)id_len, NK_PANEL_GROUP);
  21317. x_offset_ptr = nk_find_value(win, id_hash);
  21318. if (!x_offset_ptr) {
  21319. x_offset_ptr = nk_add_value(ctx, win, id_hash, 0);
  21320. y_offset_ptr = nk_add_value(ctx, win, id_hash + 1, 0);
  21321. NK_ASSERT(x_offset_ptr);
  21322. NK_ASSERT(y_offset_ptr);
  21323. if (!x_offset_ptr || !y_offset_ptr) return;
  21324. *x_offset_ptr = *y_offset_ptr = 0;
  21325. }
  21326. else y_offset_ptr = nk_find_value(win, id_hash + 1);
  21327. *x_offset_ptr = x_offset;
  21328. *y_offset_ptr = y_offset;
  21329. }
  21330. /* ===============================================================
  21331. *
  21332. * LIST VIEW
  21333. *
  21334. * ===============================================================*/
  21335. NK_API nk_bool
  21336. nk_list_view_begin(struct nk_context* ctx, struct nk_list_view* view,
  21337. const char* title, nk_flags flags, int row_height, int row_count)
  21338. {
  21339. int title_len;
  21340. nk_hash title_hash;
  21341. nk_uint* x_offset;
  21342. nk_uint* y_offset;
  21343. int result;
  21344. struct nk_window* win;
  21345. struct nk_panel* layout;
  21346. const struct nk_style* style;
  21347. struct nk_vec2 item_spacing;
  21348. NK_ASSERT(ctx);
  21349. NK_ASSERT(view);
  21350. NK_ASSERT(title);
  21351. if (!ctx || !view || !title) return 0;
  21352. win = ctx->current;
  21353. style = &ctx->style;
  21354. item_spacing = style->window.spacing;
  21355. row_height += NK_MAX(0, (int)item_spacing.y);
  21356. /* find persistent list view scrollbar offset */
  21357. title_len = (int)nk_strlen(title);
  21358. title_hash = nk_murmur_hash(title, (int)title_len, NK_PANEL_GROUP);
  21359. x_offset = nk_find_value(win, title_hash);
  21360. if (!x_offset) {
  21361. x_offset = nk_add_value(ctx, win, title_hash, 0);
  21362. y_offset = nk_add_value(ctx, win, title_hash + 1, 0);
  21363. NK_ASSERT(x_offset);
  21364. NK_ASSERT(y_offset);
  21365. if (!x_offset || !y_offset) return 0;
  21366. *x_offset = *y_offset = 0;
  21367. }
  21368. else y_offset = nk_find_value(win, title_hash + 1);
  21369. view->scroll_value = *y_offset;
  21370. view->scroll_pointer = y_offset;
  21371. *y_offset = 0;
  21372. result = nk_group_scrolled_offset_begin(ctx, x_offset, y_offset, title, flags);
  21373. win = ctx->current;
  21374. layout = win->layout;
  21375. view->total_height = row_height * NK_MAX(row_count, 1);
  21376. view->begin = (int)NK_MAX(((float)view->scroll_value / (float)row_height), 0.0f);
  21377. view->count = (int)NK_MAX(nk_iceilf((layout->clip.h) / (float)row_height), 0);
  21378. view->count = NK_MIN(view->count, row_count - view->begin);
  21379. view->end = view->begin + view->count;
  21380. view->ctx = ctx;
  21381. return result;
  21382. }
  21383. NK_API void
  21384. nk_list_view_end(struct nk_list_view* view)
  21385. {
  21386. struct nk_context* ctx;
  21387. struct nk_window* win;
  21388. struct nk_panel* layout;
  21389. NK_ASSERT(view);
  21390. NK_ASSERT(view->ctx);
  21391. NK_ASSERT(view->scroll_pointer);
  21392. if (!view || !view->ctx) return;
  21393. ctx = view->ctx;
  21394. win = ctx->current;
  21395. layout = win->layout;
  21396. layout->at_y = layout->bounds.y + (float)view->total_height;
  21397. *view->scroll_pointer = *view->scroll_pointer + view->scroll_value;
  21398. nk_group_end(view->ctx);
  21399. }
  21400. /* ===============================================================
  21401. *
  21402. * WIDGET
  21403. *
  21404. * ===============================================================*/
  21405. NK_API struct nk_rect
  21406. nk_widget_bounds(struct nk_context* ctx)
  21407. {
  21408. struct nk_rect bounds;
  21409. NK_ASSERT(ctx);
  21410. NK_ASSERT(ctx->current);
  21411. if (!ctx || !ctx->current)
  21412. return nk_rect(0, 0, 0, 0);
  21413. nk_layout_peek(&bounds, ctx);
  21414. return bounds;
  21415. }
  21416. NK_API struct nk_vec2
  21417. nk_widget_position(struct nk_context* ctx)
  21418. {
  21419. struct nk_rect bounds;
  21420. NK_ASSERT(ctx);
  21421. NK_ASSERT(ctx->current);
  21422. if (!ctx || !ctx->current)
  21423. return nk_vec2(0, 0);
  21424. nk_layout_peek(&bounds, ctx);
  21425. return nk_vec2(bounds.x, bounds.y);
  21426. }
  21427. NK_API struct nk_vec2
  21428. nk_widget_size(struct nk_context* ctx)
  21429. {
  21430. struct nk_rect bounds;
  21431. NK_ASSERT(ctx);
  21432. NK_ASSERT(ctx->current);
  21433. if (!ctx || !ctx->current)
  21434. return nk_vec2(0, 0);
  21435. nk_layout_peek(&bounds, ctx);
  21436. return nk_vec2(bounds.w, bounds.h);
  21437. }
  21438. NK_API float
  21439. nk_widget_width(struct nk_context* ctx)
  21440. {
  21441. struct nk_rect bounds;
  21442. NK_ASSERT(ctx);
  21443. NK_ASSERT(ctx->current);
  21444. if (!ctx || !ctx->current)
  21445. return 0;
  21446. nk_layout_peek(&bounds, ctx);
  21447. return bounds.w;
  21448. }
  21449. NK_API float
  21450. nk_widget_height(struct nk_context* ctx)
  21451. {
  21452. struct nk_rect bounds;
  21453. NK_ASSERT(ctx);
  21454. NK_ASSERT(ctx->current);
  21455. if (!ctx || !ctx->current)
  21456. return 0;
  21457. nk_layout_peek(&bounds, ctx);
  21458. return bounds.h;
  21459. }
  21460. NK_API nk_bool
  21461. nk_widget_is_hovered(struct nk_context* ctx)
  21462. {
  21463. struct nk_rect c, v;
  21464. struct nk_rect bounds;
  21465. NK_ASSERT(ctx);
  21466. NK_ASSERT(ctx->current);
  21467. if (!ctx || !ctx->current || ctx->active != ctx->current)
  21468. return 0;
  21469. c = ctx->current->layout->clip;
  21470. c.x = (float)((int)c.x);
  21471. c.y = (float)((int)c.y);
  21472. c.w = (float)((int)c.w);
  21473. c.h = (float)((int)c.h);
  21474. nk_layout_peek(&bounds, ctx);
  21475. nk_unify(&v, &c, bounds.x, bounds.y, bounds.x + bounds.w, bounds.y + bounds.h);
  21476. if (!NK_INTERSECT(c.x, c.y, c.w, c.h, bounds.x, bounds.y, bounds.w, bounds.h))
  21477. return 0;
  21478. return nk_input_is_mouse_hovering_rect(&ctx->input, bounds);
  21479. }
  21480. NK_API nk_bool
  21481. nk_widget_is_mouse_clicked(struct nk_context* ctx, enum nk_buttons btn)
  21482. {
  21483. struct nk_rect c, v;
  21484. struct nk_rect bounds;
  21485. NK_ASSERT(ctx);
  21486. NK_ASSERT(ctx->current);
  21487. if (!ctx || !ctx->current || ctx->active != ctx->current)
  21488. return 0;
  21489. c = ctx->current->layout->clip;
  21490. c.x = (float)((int)c.x);
  21491. c.y = (float)((int)c.y);
  21492. c.w = (float)((int)c.w);
  21493. c.h = (float)((int)c.h);
  21494. nk_layout_peek(&bounds, ctx);
  21495. nk_unify(&v, &c, bounds.x, bounds.y, bounds.x + bounds.w, bounds.y + bounds.h);
  21496. if (!NK_INTERSECT(c.x, c.y, c.w, c.h, bounds.x, bounds.y, bounds.w, bounds.h))
  21497. return 0;
  21498. return nk_input_mouse_clicked(&ctx->input, btn, bounds);
  21499. }
  21500. NK_API nk_bool
  21501. nk_widget_has_mouse_click_down(struct nk_context* ctx, enum nk_buttons btn, nk_bool down)
  21502. {
  21503. struct nk_rect c, v;
  21504. struct nk_rect bounds;
  21505. NK_ASSERT(ctx);
  21506. NK_ASSERT(ctx->current);
  21507. if (!ctx || !ctx->current || ctx->active != ctx->current)
  21508. return 0;
  21509. c = ctx->current->layout->clip;
  21510. c.x = (float)((int)c.x);
  21511. c.y = (float)((int)c.y);
  21512. c.w = (float)((int)c.w);
  21513. c.h = (float)((int)c.h);
  21514. nk_layout_peek(&bounds, ctx);
  21515. nk_unify(&v, &c, bounds.x, bounds.y, bounds.x + bounds.w, bounds.y + bounds.h);
  21516. if (!NK_INTERSECT(c.x, c.y, c.w, c.h, bounds.x, bounds.y, bounds.w, bounds.h))
  21517. return 0;
  21518. return nk_input_has_mouse_click_down_in_rect(&ctx->input, btn, bounds, down);
  21519. }
  21520. NK_API enum nk_widget_layout_states
  21521. nk_widget(struct nk_rect* bounds, const struct nk_context* ctx)
  21522. {
  21523. struct nk_rect c, v;
  21524. struct nk_window* win;
  21525. struct nk_panel* layout;
  21526. const struct nk_input* in;
  21527. NK_ASSERT(ctx);
  21528. NK_ASSERT(ctx->current);
  21529. NK_ASSERT(ctx->current->layout);
  21530. if (!ctx || !ctx->current || !ctx->current->layout)
  21531. return NK_WIDGET_INVALID;
  21532. /* allocate space and check if the widget needs to be updated and drawn */
  21533. nk_panel_alloc_space(bounds, ctx);
  21534. win = ctx->current;
  21535. layout = win->layout;
  21536. in = &ctx->input;
  21537. c = layout->clip;
  21538. /* if one of these triggers you forgot to add an `if` condition around either
  21539. a window, group, popup, combobox or contextual menu `begin` and `end` block.
  21540. Example:
  21541. if (nk_begin(...) {...} nk_end(...); or
  21542. if (nk_group_begin(...) { nk_group_end(...);} */
  21543. NK_ASSERT(!(layout->flags & NK_WINDOW_MINIMIZED));
  21544. NK_ASSERT(!(layout->flags & NK_WINDOW_HIDDEN));
  21545. NK_ASSERT(!(layout->flags & NK_WINDOW_CLOSED));
  21546. /* need to convert to int here to remove floating point errors */
  21547. bounds->x = (float)((int)bounds->x);
  21548. bounds->y = (float)((int)bounds->y);
  21549. bounds->w = (float)((int)bounds->w);
  21550. bounds->h = (float)((int)bounds->h);
  21551. c.x = (float)((int)c.x);
  21552. c.y = (float)((int)c.y);
  21553. c.w = (float)((int)c.w);
  21554. c.h = (float)((int)c.h);
  21555. nk_unify(&v, &c, bounds->x, bounds->y, bounds->x + bounds->w, bounds->y + bounds->h);
  21556. if (!NK_INTERSECT(c.x, c.y, c.w, c.h, bounds->x, bounds->y, bounds->w, bounds->h))
  21557. return NK_WIDGET_INVALID;
  21558. if (!NK_INBOX(in->mouse.pos.x, in->mouse.pos.y, v.x, v.y, v.w, v.h))
  21559. return NK_WIDGET_ROM;
  21560. return NK_WIDGET_VALID;
  21561. }
  21562. NK_API enum nk_widget_layout_states
  21563. nk_widget_fitting(struct nk_rect* bounds, struct nk_context* ctx,
  21564. struct nk_vec2 item_padding)
  21565. {
  21566. /* update the bounds to stand without padding */
  21567. enum nk_widget_layout_states state;
  21568. NK_UNUSED(item_padding);
  21569. NK_ASSERT(ctx);
  21570. NK_ASSERT(ctx->current);
  21571. NK_ASSERT(ctx->current->layout);
  21572. if (!ctx || !ctx->current || !ctx->current->layout)
  21573. return NK_WIDGET_INVALID;
  21574. state = nk_widget(bounds, ctx);
  21575. return state;
  21576. }
  21577. NK_API void
  21578. nk_spacing(struct nk_context* ctx, int cols)
  21579. {
  21580. struct nk_window* win;
  21581. struct nk_panel* layout;
  21582. struct nk_rect none;
  21583. int i, index, rows;
  21584. NK_ASSERT(ctx);
  21585. NK_ASSERT(ctx->current);
  21586. NK_ASSERT(ctx->current->layout);
  21587. if (!ctx || !ctx->current || !ctx->current->layout)
  21588. return;
  21589. /* spacing over row boundaries */
  21590. win = ctx->current;
  21591. layout = win->layout;
  21592. index = (layout->row.index + cols) % layout->row.columns;
  21593. rows = (layout->row.index + cols) / layout->row.columns;
  21594. if (rows) {
  21595. for (i = 0; i < rows; ++i)
  21596. nk_panel_alloc_row(ctx, win);
  21597. cols = index;
  21598. }
  21599. /* non table layout need to allocate space */
  21600. if (layout->row.type != NK_LAYOUT_DYNAMIC_FIXED &&
  21601. layout->row.type != NK_LAYOUT_STATIC_FIXED) {
  21602. for (i = 0; i < cols; ++i)
  21603. nk_panel_alloc_space(&none, ctx);
  21604. } layout->row.index = index;
  21605. }
  21606. /* ===============================================================
  21607. *
  21608. * TEXT
  21609. *
  21610. * ===============================================================*/
  21611. NK_LIB void
  21612. nk_widget_text(struct nk_command_buffer* o, struct nk_rect b,
  21613. const char* string, int len, const struct nk_text* t,
  21614. nk_flags a, const struct nk_user_font* f)
  21615. {
  21616. struct nk_rect label;
  21617. float text_width;
  21618. NK_ASSERT(o);
  21619. NK_ASSERT(t);
  21620. if (!o || !t) return;
  21621. b.h = NK_MAX(b.h, 2 * t->padding.y);
  21622. label.x = 0; label.w = 0;
  21623. label.y = b.y + t->padding.y;
  21624. label.h = NK_MIN(f->height, b.h - 2 * t->padding.y);
  21625. text_width = f->width(f->userdata, f->height, (const char*)string, len);
  21626. text_width += (2.0f * t->padding.x);
  21627. /* align in x-axis */
  21628. if (a & NK_TEXT_ALIGN_LEFT) {
  21629. label.x = b.x + t->padding.x;
  21630. label.w = NK_MAX(0, b.w - 2 * t->padding.x);
  21631. }
  21632. else if (a & NK_TEXT_ALIGN_CENTERED) {
  21633. label.w = NK_MAX(1, 2 * t->padding.x + (float)text_width);
  21634. label.x = (b.x + t->padding.x + ((b.w - 2 * t->padding.x) - label.w) / 2);
  21635. label.x = NK_MAX(b.x + t->padding.x, label.x);
  21636. label.w = NK_MIN(b.x + b.w, label.x + label.w);
  21637. if (label.w >= label.x) label.w -= label.x;
  21638. }
  21639. else if (a & NK_TEXT_ALIGN_RIGHT) {
  21640. label.x = NK_MAX(b.x + t->padding.x, (b.x + b.w) - (2 * t->padding.x + (float)text_width));
  21641. label.w = (float)text_width + 2 * t->padding.x;
  21642. }
  21643. else return;
  21644. /* align in y-axis */
  21645. if (a & NK_TEXT_ALIGN_MIDDLE) {
  21646. label.y = b.y + b.h / 2.0f - (float)f->height / 2.0f;
  21647. label.h = NK_MAX(b.h / 2.0f, b.h - (b.h / 2.0f + f->height / 2.0f));
  21648. }
  21649. else if (a & NK_TEXT_ALIGN_BOTTOM) {
  21650. label.y = b.y + b.h - f->height;
  21651. label.h = f->height;
  21652. }
  21653. nk_draw_text(o, label, (const char*)string, len, f, t->background, t->text);
  21654. }
  21655. NK_LIB void
  21656. nk_widget_text_wrap(struct nk_command_buffer* o, struct nk_rect b,
  21657. const char* string, int len, const struct nk_text* t,
  21658. const struct nk_user_font* f)
  21659. {
  21660. float width;
  21661. int glyphs = 0;
  21662. int fitting = 0;
  21663. int done = 0;
  21664. struct nk_rect line;
  21665. struct nk_text text;
  21666. NK_INTERN nk_rune seperator[] = { ' ' };
  21667. NK_ASSERT(o);
  21668. NK_ASSERT(t);
  21669. if (!o || !t) return;
  21670. text.padding = nk_vec2(0, 0);
  21671. text.background = t->background;
  21672. text.text = t->text;
  21673. b.w = NK_MAX(b.w, 2 * t->padding.x);
  21674. b.h = NK_MAX(b.h, 2 * t->padding.y);
  21675. b.h = b.h - 2 * t->padding.y;
  21676. line.x = b.x + t->padding.x;
  21677. line.y = b.y + t->padding.y;
  21678. line.w = b.w - 2 * t->padding.x;
  21679. line.h = 2 * t->padding.y + f->height;
  21680. fitting = nk_text_clamp(f, string, len, line.w, &glyphs, &width, seperator, NK_LEN(seperator));
  21681. while (done < len) {
  21682. if (!fitting || line.y + line.h >= (b.y + b.h)) break;
  21683. nk_widget_text(o, line, &string[done], fitting, &text, NK_TEXT_LEFT, f);
  21684. done += fitting;
  21685. line.y += f->height + 2 * t->padding.y;
  21686. fitting = nk_text_clamp(f, &string[done], len - done, line.w, &glyphs, &width, seperator, NK_LEN(seperator));
  21687. }
  21688. }
  21689. NK_API void
  21690. nk_text_colored(struct nk_context* ctx, const char* str, int len,
  21691. nk_flags alignment, struct nk_color color)
  21692. {
  21693. struct nk_window* win;
  21694. const struct nk_style* style;
  21695. struct nk_vec2 item_padding;
  21696. struct nk_rect bounds;
  21697. struct nk_text text;
  21698. NK_ASSERT(ctx);
  21699. NK_ASSERT(ctx->current);
  21700. NK_ASSERT(ctx->current->layout);
  21701. if (!ctx || !ctx->current || !ctx->current->layout) return;
  21702. win = ctx->current;
  21703. style = &ctx->style;
  21704. nk_panel_alloc_space(&bounds, ctx);
  21705. item_padding = style->text.padding;
  21706. text.padding.x = item_padding.x;
  21707. text.padding.y = item_padding.y;
  21708. text.background = style->window.background;
  21709. text.text = color;
  21710. nk_widget_text(&win->buffer, bounds, str, len, &text, alignment, style->font);
  21711. }
  21712. NK_API void
  21713. nk_text_wrap_colored(struct nk_context* ctx, const char* str,
  21714. int len, struct nk_color color)
  21715. {
  21716. struct nk_window* win;
  21717. const struct nk_style* style;
  21718. struct nk_vec2 item_padding;
  21719. struct nk_rect bounds;
  21720. struct nk_text text;
  21721. NK_ASSERT(ctx);
  21722. NK_ASSERT(ctx->current);
  21723. NK_ASSERT(ctx->current->layout);
  21724. if (!ctx || !ctx->current || !ctx->current->layout) return;
  21725. win = ctx->current;
  21726. style = &ctx->style;
  21727. nk_panel_alloc_space(&bounds, ctx);
  21728. item_padding = style->text.padding;
  21729. text.padding.x = item_padding.x;
  21730. text.padding.y = item_padding.y;
  21731. text.background = style->window.background;
  21732. text.text = color;
  21733. nk_widget_text_wrap(&win->buffer, bounds, str, len, &text, style->font);
  21734. }
  21735. #ifdef NK_INCLUDE_STANDARD_VARARGS
  21736. NK_API void
  21737. nk_labelf_colored(struct nk_context* ctx, nk_flags flags,
  21738. struct nk_color color, const char* fmt, ...)
  21739. {
  21740. va_list args;
  21741. va_start(args, fmt);
  21742. nk_labelfv_colored(ctx, flags, color, fmt, args);
  21743. va_end(args);
  21744. }
  21745. NK_API void
  21746. nk_labelf_colored_wrap(struct nk_context* ctx, struct nk_color color,
  21747. const char* fmt, ...)
  21748. {
  21749. va_list args;
  21750. va_start(args, fmt);
  21751. nk_labelfv_colored_wrap(ctx, color, fmt, args);
  21752. va_end(args);
  21753. }
  21754. NK_API void
  21755. nk_labelf(struct nk_context* ctx, nk_flags flags, const char* fmt, ...)
  21756. {
  21757. va_list args;
  21758. va_start(args, fmt);
  21759. nk_labelfv(ctx, flags, fmt, args);
  21760. va_end(args);
  21761. }
  21762. NK_API void
  21763. nk_labelf_wrap(struct nk_context* ctx, const char* fmt, ...)
  21764. {
  21765. va_list args;
  21766. va_start(args, fmt);
  21767. nk_labelfv_wrap(ctx, fmt, args);
  21768. va_end(args);
  21769. }
  21770. NK_API void
  21771. nk_labelfv_colored(struct nk_context* ctx, nk_flags flags,
  21772. struct nk_color color, const char* fmt, va_list args)
  21773. {
  21774. char buf[256];
  21775. nk_strfmt(buf, NK_LEN(buf), fmt, args);
  21776. nk_label_colored(ctx, buf, flags, color);
  21777. }
  21778. NK_API void
  21779. nk_labelfv_colored_wrap(struct nk_context* ctx, struct nk_color color,
  21780. const char* fmt, va_list args)
  21781. {
  21782. char buf[256];
  21783. nk_strfmt(buf, NK_LEN(buf), fmt, args);
  21784. nk_label_colored_wrap(ctx, buf, color);
  21785. }
  21786. NK_API void
  21787. nk_labelfv(struct nk_context* ctx, nk_flags flags, const char* fmt, va_list args)
  21788. {
  21789. char buf[256];
  21790. nk_strfmt(buf, NK_LEN(buf), fmt, args);
  21791. nk_label(ctx, buf, flags);
  21792. }
  21793. NK_API void
  21794. nk_labelfv_wrap(struct nk_context* ctx, const char* fmt, va_list args)
  21795. {
  21796. char buf[256];
  21797. nk_strfmt(buf, NK_LEN(buf), fmt, args);
  21798. nk_label_wrap(ctx, buf);
  21799. }
  21800. NK_API void
  21801. nk_value_bool(struct nk_context* ctx, const char* prefix, int value)
  21802. {
  21803. nk_labelf(ctx, NK_TEXT_LEFT, "%s: %s", prefix, ((value) ? "true" : "false"));
  21804. }
  21805. NK_API void
  21806. nk_value_int(struct nk_context* ctx, const char* prefix, int value)
  21807. {
  21808. nk_labelf(ctx, NK_TEXT_LEFT, "%s: %d", prefix, value);
  21809. }
  21810. NK_API void
  21811. nk_value_uint(struct nk_context* ctx, const char* prefix, unsigned int value)
  21812. {
  21813. nk_labelf(ctx, NK_TEXT_LEFT, "%s: %u", prefix, value);
  21814. }
  21815. NK_API void
  21816. nk_value_float(struct nk_context* ctx, const char* prefix, float value)
  21817. {
  21818. double double_value = (double)value;
  21819. nk_labelf(ctx, NK_TEXT_LEFT, "%s: %.3f", prefix, double_value);
  21820. }
  21821. NK_API void
  21822. nk_value_color_byte(struct nk_context* ctx, const char* p, struct nk_color c)
  21823. {
  21824. nk_labelf(ctx, NK_TEXT_LEFT, "%s: (%d, %d, %d, %d)", p, c.r, c.g, c.b, c.a);
  21825. }
  21826. NK_API void
  21827. nk_value_color_float(struct nk_context* ctx, const char* p, struct nk_color color)
  21828. {
  21829. double c[4]; nk_color_dv(c, color);
  21830. nk_labelf(ctx, NK_TEXT_LEFT, "%s: (%.2f, %.2f, %.2f, %.2f)",
  21831. p, c[0], c[1], c[2], c[3]);
  21832. }
  21833. NK_API void
  21834. nk_value_color_hex(struct nk_context* ctx, const char* prefix, struct nk_color color)
  21835. {
  21836. char hex[16];
  21837. nk_color_hex_rgba(hex, color);
  21838. nk_labelf(ctx, NK_TEXT_LEFT, "%s: %s", prefix, hex);
  21839. }
  21840. #endif
  21841. NK_API void
  21842. nk_text(struct nk_context* ctx, const char* str, int len, nk_flags alignment)
  21843. {
  21844. NK_ASSERT(ctx);
  21845. if (!ctx) return;
  21846. nk_text_colored(ctx, str, len, alignment, ctx->style.text.color);
  21847. }
  21848. NK_API void
  21849. nk_text_wrap(struct nk_context* ctx, const char* str, int len)
  21850. {
  21851. NK_ASSERT(ctx);
  21852. if (!ctx) return;
  21853. nk_text_wrap_colored(ctx, str, len, ctx->style.text.color);
  21854. }
  21855. NK_API void
  21856. nk_label(struct nk_context* ctx, const char* str, nk_flags alignment)
  21857. {
  21858. nk_text(ctx, str, nk_strlen(str), alignment);
  21859. }
  21860. NK_API void
  21861. nk_label_colored(struct nk_context* ctx, const char* str, nk_flags align,
  21862. struct nk_color color)
  21863. {
  21864. nk_text_colored(ctx, str, nk_strlen(str), align, color);
  21865. }
  21866. NK_API void
  21867. nk_label_wrap(struct nk_context* ctx, const char* str)
  21868. {
  21869. nk_text_wrap(ctx, str, nk_strlen(str));
  21870. }
  21871. NK_API void
  21872. nk_label_colored_wrap(struct nk_context* ctx, const char* str, struct nk_color color)
  21873. {
  21874. nk_text_wrap_colored(ctx, str, nk_strlen(str), color);
  21875. }
  21876. /* ===============================================================
  21877. *
  21878. * IMAGE
  21879. *
  21880. * ===============================================================*/
  21881. NK_API nk_handle
  21882. nk_handle_ptr(void* ptr)
  21883. {
  21884. nk_handle handle = { 0 };
  21885. handle.ptr = ptr;
  21886. return handle;
  21887. }
  21888. NK_API nk_handle
  21889. nk_handle_id(int id)
  21890. {
  21891. nk_handle handle;
  21892. nk_zero_struct(handle);
  21893. handle.id = id;
  21894. return handle;
  21895. }
  21896. NK_API struct nk_image
  21897. nk_subimage_ptr(void* ptr, nk_ushort w, nk_ushort h, struct nk_rect r)
  21898. {
  21899. struct nk_image s;
  21900. nk_zero(&s, sizeof(s));
  21901. s.handle.ptr = ptr;
  21902. s.w = w; s.h = h;
  21903. s.region[0] = (nk_ushort)r.x;
  21904. s.region[1] = (nk_ushort)r.y;
  21905. s.region[2] = (nk_ushort)r.w;
  21906. s.region[3] = (nk_ushort)r.h;
  21907. return s;
  21908. }
  21909. NK_API struct nk_image
  21910. nk_subimage_id(int id, nk_ushort w, nk_ushort h, struct nk_rect r)
  21911. {
  21912. struct nk_image s;
  21913. nk_zero(&s, sizeof(s));
  21914. s.handle.id = id;
  21915. s.w = w; s.h = h;
  21916. s.region[0] = (nk_ushort)r.x;
  21917. s.region[1] = (nk_ushort)r.y;
  21918. s.region[2] = (nk_ushort)r.w;
  21919. s.region[3] = (nk_ushort)r.h;
  21920. return s;
  21921. }
  21922. NK_API struct nk_image
  21923. nk_subimage_handle(nk_handle handle, nk_ushort w, nk_ushort h, struct nk_rect r)
  21924. {
  21925. struct nk_image s;
  21926. nk_zero(&s, sizeof(s));
  21927. s.handle = handle;
  21928. s.w = w; s.h = h;
  21929. s.region[0] = (nk_ushort)r.x;
  21930. s.region[1] = (nk_ushort)r.y;
  21931. s.region[2] = (nk_ushort)r.w;
  21932. s.region[3] = (nk_ushort)r.h;
  21933. return s;
  21934. }
  21935. NK_API struct nk_image
  21936. nk_image_handle(nk_handle handle)
  21937. {
  21938. struct nk_image s;
  21939. nk_zero(&s, sizeof(s));
  21940. s.handle = handle;
  21941. s.w = 0; s.h = 0;
  21942. s.region[0] = 0;
  21943. s.region[1] = 0;
  21944. s.region[2] = 0;
  21945. s.region[3] = 0;
  21946. return s;
  21947. }
  21948. NK_API struct nk_image
  21949. nk_image_ptr(void* ptr)
  21950. {
  21951. struct nk_image s;
  21952. nk_zero(&s, sizeof(s));
  21953. NK_ASSERT(ptr);
  21954. s.handle.ptr = ptr;
  21955. s.w = 0; s.h = 0;
  21956. s.region[0] = 0;
  21957. s.region[1] = 0;
  21958. s.region[2] = 0;
  21959. s.region[3] = 0;
  21960. return s;
  21961. }
  21962. NK_API struct nk_image
  21963. nk_image_id(int id)
  21964. {
  21965. struct nk_image s;
  21966. nk_zero(&s, sizeof(s));
  21967. s.handle.id = id;
  21968. s.w = 0; s.h = 0;
  21969. s.region[0] = 0;
  21970. s.region[1] = 0;
  21971. s.region[2] = 0;
  21972. s.region[3] = 0;
  21973. return s;
  21974. }
  21975. NK_API nk_bool
  21976. nk_image_is_subimage(const struct nk_image* img)
  21977. {
  21978. NK_ASSERT(img);
  21979. return !(img->w == 0 && img->h == 0);
  21980. }
  21981. NK_API void
  21982. nk_image(struct nk_context* ctx, struct nk_image img)
  21983. {
  21984. struct nk_window* win;
  21985. struct nk_rect bounds;
  21986. NK_ASSERT(ctx);
  21987. NK_ASSERT(ctx->current);
  21988. NK_ASSERT(ctx->current->layout);
  21989. if (!ctx || !ctx->current || !ctx->current->layout) return;
  21990. win = ctx->current;
  21991. if (!nk_widget(&bounds, ctx)) return;
  21992. nk_draw_image(&win->buffer, bounds, &img, nk_white);
  21993. }
  21994. NK_API void
  21995. nk_image_color(struct nk_context* ctx, struct nk_image img, struct nk_color col)
  21996. {
  21997. struct nk_window* win;
  21998. struct nk_rect bounds;
  21999. NK_ASSERT(ctx);
  22000. NK_ASSERT(ctx->current);
  22001. NK_ASSERT(ctx->current->layout);
  22002. if (!ctx || !ctx->current || !ctx->current->layout) return;
  22003. win = ctx->current;
  22004. if (!nk_widget(&bounds, ctx)) return;
  22005. nk_draw_image(&win->buffer, bounds, &img, col);
  22006. }
  22007. /* ===============================================================
  22008. *
  22009. * 9-SLICE
  22010. *
  22011. * ===============================================================*/
  22012. NK_API struct nk_nine_slice
  22013. nk_sub9slice_ptr(void* ptr, nk_ushort w, nk_ushort h, struct nk_rect rgn, nk_ushort l, nk_ushort t, nk_ushort r, nk_ushort b)
  22014. {
  22015. struct nk_nine_slice s;
  22016. struct nk_image* i = &s.img;
  22017. nk_zero(&s, sizeof(s));
  22018. i->handle.ptr = ptr;
  22019. i->w = w; i->h = h;
  22020. i->region[0] = (nk_ushort)rgn.x;
  22021. i->region[1] = (nk_ushort)rgn.y;
  22022. i->region[2] = (nk_ushort)rgn.w;
  22023. i->region[3] = (nk_ushort)rgn.h;
  22024. s.l = l; s.t = t; s.r = r; s.b = b;
  22025. return s;
  22026. }
  22027. NK_API struct nk_nine_slice
  22028. nk_sub9slice_id(int id, nk_ushort w, nk_ushort h, struct nk_rect rgn, nk_ushort l, nk_ushort t, nk_ushort r, nk_ushort b)
  22029. {
  22030. struct nk_nine_slice s;
  22031. struct nk_image* i = &s.img;
  22032. nk_zero(&s, sizeof(s));
  22033. i->handle.id = id;
  22034. i->w = w; i->h = h;
  22035. i->region[0] = (nk_ushort)rgn.x;
  22036. i->region[1] = (nk_ushort)rgn.y;
  22037. i->region[2] = (nk_ushort)rgn.w;
  22038. i->region[3] = (nk_ushort)rgn.h;
  22039. s.l = l; s.t = t; s.r = r; s.b = b;
  22040. return s;
  22041. }
  22042. NK_API struct nk_nine_slice
  22043. nk_sub9slice_handle(nk_handle handle, nk_ushort w, nk_ushort h, struct nk_rect rgn, nk_ushort l, nk_ushort t, nk_ushort r, nk_ushort b)
  22044. {
  22045. struct nk_nine_slice s;
  22046. struct nk_image* i = &s.img;
  22047. nk_zero(&s, sizeof(s));
  22048. i->handle = handle;
  22049. i->w = w; i->h = h;
  22050. i->region[0] = (nk_ushort)rgn.x;
  22051. i->region[1] = (nk_ushort)rgn.y;
  22052. i->region[2] = (nk_ushort)rgn.w;
  22053. i->region[3] = (nk_ushort)rgn.h;
  22054. s.l = l; s.t = t; s.r = r; s.b = b;
  22055. return s;
  22056. }
  22057. NK_API struct nk_nine_slice
  22058. nk_nine_slice_handle(nk_handle handle, nk_ushort l, nk_ushort t, nk_ushort r, nk_ushort b)
  22059. {
  22060. struct nk_nine_slice s;
  22061. struct nk_image* i = &s.img;
  22062. nk_zero(&s, sizeof(s));
  22063. i->handle = handle;
  22064. i->w = 0; i->h = 0;
  22065. i->region[0] = 0;
  22066. i->region[1] = 0;
  22067. i->region[2] = 0;
  22068. i->region[3] = 0;
  22069. s.l = l; s.t = t; s.r = r; s.b = b;
  22070. return s;
  22071. }
  22072. NK_API struct nk_nine_slice
  22073. nk_nine_slice_ptr(void* ptr, nk_ushort l, nk_ushort t, nk_ushort r, nk_ushort b)
  22074. {
  22075. struct nk_nine_slice s;
  22076. struct nk_image* i = &s.img;
  22077. nk_zero(&s, sizeof(s));
  22078. NK_ASSERT(ptr);
  22079. i->handle.ptr = ptr;
  22080. i->w = 0; i->h = 0;
  22081. i->region[0] = 0;
  22082. i->region[1] = 0;
  22083. i->region[2] = 0;
  22084. i->region[3] = 0;
  22085. s.l = l; s.t = t; s.r = r; s.b = b;
  22086. return s;
  22087. }
  22088. NK_API struct nk_nine_slice
  22089. nk_nine_slice_id(int id, nk_ushort l, nk_ushort t, nk_ushort r, nk_ushort b)
  22090. {
  22091. struct nk_nine_slice s;
  22092. struct nk_image* i = &s.img;
  22093. nk_zero(&s, sizeof(s));
  22094. i->handle.id = id;
  22095. i->w = 0; i->h = 0;
  22096. i->region[0] = 0;
  22097. i->region[1] = 0;
  22098. i->region[2] = 0;
  22099. i->region[3] = 0;
  22100. s.l = l; s.t = t; s.r = r; s.b = b;
  22101. return s;
  22102. }
  22103. NK_API int
  22104. nk_nine_slice_is_sub9slice(const struct nk_nine_slice* slice)
  22105. {
  22106. NK_ASSERT(slice);
  22107. return !(slice->img.w == 0 && slice->img.h == 0);
  22108. }
  22109. /* ==============================================================
  22110. *
  22111. * BUTTON
  22112. *
  22113. * ===============================================================*/
  22114. NK_LIB void
  22115. nk_draw_symbol(struct nk_command_buffer* out, enum nk_symbol_type type,
  22116. struct nk_rect content, struct nk_color background, struct nk_color foreground,
  22117. float border_width, const struct nk_user_font* font)
  22118. {
  22119. switch (type) {
  22120. case NK_SYMBOL_X:
  22121. case NK_SYMBOL_UNDERSCORE:
  22122. case NK_SYMBOL_PLUS:
  22123. case NK_SYMBOL_MINUS: {
  22124. /* single character text symbol */
  22125. const char* X = (type == NK_SYMBOL_X) ? "x" :
  22126. (type == NK_SYMBOL_UNDERSCORE) ? "_" :
  22127. (type == NK_SYMBOL_PLUS) ? "+" : "-";
  22128. struct nk_text text;
  22129. text.padding = nk_vec2(0, 0);
  22130. text.background = background;
  22131. text.text = foreground;
  22132. nk_widget_text(out, content, X, 1, &text, NK_TEXT_CENTERED, font);
  22133. } break;
  22134. case NK_SYMBOL_CIRCLE_SOLID:
  22135. case NK_SYMBOL_CIRCLE_OUTLINE:
  22136. case NK_SYMBOL_RECT_SOLID:
  22137. case NK_SYMBOL_RECT_OUTLINE: {
  22138. /* simple empty/filled shapes */
  22139. if (type == NK_SYMBOL_RECT_SOLID || type == NK_SYMBOL_RECT_OUTLINE) {
  22140. nk_fill_rect(out, content, 0, foreground);
  22141. if (type == NK_SYMBOL_RECT_OUTLINE)
  22142. nk_fill_rect(out, nk_shrink_rect(content, border_width), 0, background);
  22143. }
  22144. else {
  22145. nk_fill_circle(out, content, foreground);
  22146. if (type == NK_SYMBOL_CIRCLE_OUTLINE)
  22147. nk_fill_circle(out, nk_shrink_rect(content, 1), background);
  22148. }
  22149. } break;
  22150. case NK_SYMBOL_TRIANGLE_UP:
  22151. case NK_SYMBOL_TRIANGLE_DOWN:
  22152. case NK_SYMBOL_TRIANGLE_LEFT:
  22153. case NK_SYMBOL_TRIANGLE_RIGHT: {
  22154. enum nk_heading heading;
  22155. struct nk_vec2 points[3];
  22156. heading = (type == NK_SYMBOL_TRIANGLE_RIGHT) ? NK_RIGHT :
  22157. (type == NK_SYMBOL_TRIANGLE_LEFT) ? NK_LEFT :
  22158. (type == NK_SYMBOL_TRIANGLE_UP) ? NK_UP : NK_DOWN;
  22159. nk_triangle_from_direction(points, content, 0, 0, heading);
  22160. nk_fill_triangle(out, points[0].x, points[0].y, points[1].x, points[1].y,
  22161. points[2].x, points[2].y, foreground);
  22162. } break;
  22163. default:
  22164. case NK_SYMBOL_NONE:
  22165. case NK_SYMBOL_MAX: break;
  22166. }
  22167. }
  22168. NK_LIB nk_bool
  22169. nk_button_behavior(nk_flags* state, struct nk_rect r,
  22170. const struct nk_input* i, enum nk_button_behavior behavior)
  22171. {
  22172. int ret = 0;
  22173. nk_widget_state_reset(state);
  22174. if (!i) return 0;
  22175. if (nk_input_is_mouse_hovering_rect(i, r)) {
  22176. *state = NK_WIDGET_STATE_HOVERED;
  22177. if (nk_input_is_mouse_down(i, NK_BUTTON_LEFT))
  22178. *state = NK_WIDGET_STATE_ACTIVE;
  22179. if (nk_input_has_mouse_click_in_button_rect(i, NK_BUTTON_LEFT, r)) {
  22180. ret = (behavior != NK_BUTTON_DEFAULT) ?
  22181. nk_input_is_mouse_down(i, NK_BUTTON_LEFT) :
  22182. #ifdef NK_BUTTON_TRIGGER_ON_RELEASE
  22183. nk_input_is_mouse_released(i, NK_BUTTON_LEFT);
  22184. #else
  22185. nk_input_is_mouse_pressed(i, NK_BUTTON_LEFT);
  22186. #endif
  22187. }
  22188. }
  22189. if (*state & NK_WIDGET_STATE_HOVER && !nk_input_is_mouse_prev_hovering_rect(i, r))
  22190. *state |= NK_WIDGET_STATE_ENTERED;
  22191. else if (nk_input_is_mouse_prev_hovering_rect(i, r))
  22192. *state |= NK_WIDGET_STATE_LEFT;
  22193. return ret;
  22194. }
  22195. NK_LIB const struct nk_style_item*
  22196. nk_draw_button(struct nk_command_buffer* out,
  22197. const struct nk_rect* bounds, nk_flags state,
  22198. const struct nk_style_button* style)
  22199. {
  22200. const struct nk_style_item* background;
  22201. if (state & NK_WIDGET_STATE_HOVER)
  22202. background = &style->hover;
  22203. else if (state & NK_WIDGET_STATE_ACTIVED)
  22204. background = &style->active;
  22205. else background = &style->normal;
  22206. switch (background->type) {
  22207. case NK_STYLE_ITEM_IMAGE:
  22208. nk_draw_image(out, *bounds, &background->data.image, nk_white);
  22209. break;
  22210. case NK_STYLE_ITEM_NINE_SLICE:
  22211. nk_draw_nine_slice(out, *bounds, &background->data.slice, nk_white);
  22212. break;
  22213. case NK_STYLE_ITEM_COLOR:
  22214. nk_fill_rect(out, *bounds, style->rounding, background->data.color);
  22215. nk_stroke_rect(out, *bounds, style->rounding, style->border, style->border_color);
  22216. break;
  22217. }
  22218. return background;
  22219. }
  22220. NK_LIB nk_bool
  22221. nk_do_button(nk_flags* state, struct nk_command_buffer* out, struct nk_rect r,
  22222. const struct nk_style_button* style, const struct nk_input* in,
  22223. enum nk_button_behavior behavior, struct nk_rect* content)
  22224. {
  22225. struct nk_rect bounds;
  22226. NK_ASSERT(style);
  22227. NK_ASSERT(state);
  22228. NK_ASSERT(out);
  22229. if (!out || !style)
  22230. return nk_false;
  22231. /* calculate button content space */
  22232. content->x = r.x + style->padding.x + style->border + style->rounding;
  22233. content->y = r.y + style->padding.y + style->border + style->rounding;
  22234. content->w = r.w - (2 * style->padding.x + style->border + style->rounding * 2);
  22235. content->h = r.h - (2 * style->padding.y + style->border + style->rounding * 2);
  22236. /* execute button behavior */
  22237. bounds.x = r.x - style->touch_padding.x;
  22238. bounds.y = r.y - style->touch_padding.y;
  22239. bounds.w = r.w + 2 * style->touch_padding.x;
  22240. bounds.h = r.h + 2 * style->touch_padding.y;
  22241. return nk_button_behavior(state, bounds, in, behavior);
  22242. }
  22243. NK_LIB void
  22244. nk_draw_button_text(struct nk_command_buffer* out,
  22245. const struct nk_rect* bounds, const struct nk_rect* content, nk_flags state,
  22246. const struct nk_style_button* style, const char* txt, int len,
  22247. nk_flags text_alignment, const struct nk_user_font* font)
  22248. {
  22249. struct nk_text text;
  22250. const struct nk_style_item* background;
  22251. background = nk_draw_button(out, bounds, state, style);
  22252. /* select correct colors/images */
  22253. if (background->type == NK_STYLE_ITEM_COLOR)
  22254. text.background = background->data.color;
  22255. else text.background = style->text_background;
  22256. if (state & NK_WIDGET_STATE_HOVER)
  22257. text.text = style->text_hover;
  22258. else if (state & NK_WIDGET_STATE_ACTIVED)
  22259. text.text = style->text_active;
  22260. else text.text = style->text_normal;
  22261. text.padding = nk_vec2(0, 0);
  22262. nk_widget_text(out, *content, txt, len, &text, text_alignment, font);
  22263. }
  22264. NK_LIB nk_bool
  22265. nk_do_button_text(nk_flags* state,
  22266. struct nk_command_buffer* out, struct nk_rect bounds,
  22267. const char* string, int len, nk_flags align, enum nk_button_behavior behavior,
  22268. const struct nk_style_button* style, const struct nk_input* in,
  22269. const struct nk_user_font* font)
  22270. {
  22271. struct nk_rect content;
  22272. int ret = nk_false;
  22273. NK_ASSERT(state);
  22274. NK_ASSERT(style);
  22275. NK_ASSERT(out);
  22276. NK_ASSERT(string);
  22277. NK_ASSERT(font);
  22278. if (!out || !style || !font || !string)
  22279. return nk_false;
  22280. ret = nk_do_button(state, out, bounds, style, in, behavior, &content);
  22281. if (style->draw_begin) style->draw_begin(out, style->userdata);
  22282. nk_draw_button_text(out, &bounds, &content, *state, style, string, len, align, font);
  22283. if (style->draw_end) style->draw_end(out, style->userdata);
  22284. return ret;
  22285. }
  22286. NK_LIB void
  22287. nk_draw_button_symbol(struct nk_command_buffer* out,
  22288. const struct nk_rect* bounds, const struct nk_rect* content,
  22289. nk_flags state, const struct nk_style_button* style,
  22290. enum nk_symbol_type type, const struct nk_user_font* font)
  22291. {
  22292. struct nk_color sym, bg;
  22293. const struct nk_style_item* background;
  22294. /* select correct colors/images */
  22295. background = nk_draw_button(out, bounds, state, style);
  22296. if (background->type == NK_STYLE_ITEM_COLOR)
  22297. bg = background->data.color;
  22298. else bg = style->text_background;
  22299. if (state & NK_WIDGET_STATE_HOVER)
  22300. sym = style->text_hover;
  22301. else if (state & NK_WIDGET_STATE_ACTIVED)
  22302. sym = style->text_active;
  22303. else sym = style->text_normal;
  22304. nk_draw_symbol(out, type, *content, bg, sym, 1, font);
  22305. }
  22306. NK_LIB nk_bool
  22307. nk_do_button_symbol(nk_flags* state,
  22308. struct nk_command_buffer* out, struct nk_rect bounds,
  22309. enum nk_symbol_type symbol, enum nk_button_behavior behavior,
  22310. const struct nk_style_button* style, const struct nk_input* in,
  22311. const struct nk_user_font* font)
  22312. {
  22313. int ret;
  22314. struct nk_rect content;
  22315. NK_ASSERT(state);
  22316. NK_ASSERT(style);
  22317. NK_ASSERT(font);
  22318. NK_ASSERT(out);
  22319. if (!out || !style || !font || !state)
  22320. return nk_false;
  22321. ret = nk_do_button(state, out, bounds, style, in, behavior, &content);
  22322. if (style->draw_begin) style->draw_begin(out, style->userdata);
  22323. nk_draw_button_symbol(out, &bounds, &content, *state, style, symbol, font);
  22324. if (style->draw_end) style->draw_end(out, style->userdata);
  22325. return ret;
  22326. }
  22327. NK_LIB void
  22328. nk_draw_button_image(struct nk_command_buffer* out,
  22329. const struct nk_rect* bounds, const struct nk_rect* content,
  22330. nk_flags state, const struct nk_style_button* style, const struct nk_image* img)
  22331. {
  22332. nk_draw_button(out, bounds, state, style);
  22333. nk_draw_image(out, *content, img, nk_white);
  22334. }
  22335. NK_LIB nk_bool
  22336. nk_do_button_image(nk_flags* state,
  22337. struct nk_command_buffer* out, struct nk_rect bounds,
  22338. struct nk_image img, enum nk_button_behavior b,
  22339. const struct nk_style_button* style, const struct nk_input* in)
  22340. {
  22341. int ret;
  22342. struct nk_rect content;
  22343. NK_ASSERT(state);
  22344. NK_ASSERT(style);
  22345. NK_ASSERT(out);
  22346. if (!out || !style || !state)
  22347. return nk_false;
  22348. ret = nk_do_button(state, out, bounds, style, in, b, &content);
  22349. content.x += style->image_padding.x;
  22350. content.y += style->image_padding.y;
  22351. content.w -= 2 * style->image_padding.x;
  22352. content.h -= 2 * style->image_padding.y;
  22353. if (style->draw_begin) style->draw_begin(out, style->userdata);
  22354. nk_draw_button_image(out, &bounds, &content, *state, style, &img);
  22355. if (style->draw_end) style->draw_end(out, style->userdata);
  22356. return ret;
  22357. }
  22358. NK_LIB void
  22359. nk_draw_button_text_symbol(struct nk_command_buffer* out,
  22360. const struct nk_rect* bounds, const struct nk_rect* label,
  22361. const struct nk_rect* symbol, nk_flags state, const struct nk_style_button* style,
  22362. const char* str, int len, enum nk_symbol_type type,
  22363. const struct nk_user_font* font)
  22364. {
  22365. struct nk_color sym;
  22366. struct nk_text text;
  22367. const struct nk_style_item* background;
  22368. /* select correct background colors/images */
  22369. background = nk_draw_button(out, bounds, state, style);
  22370. if (background->type == NK_STYLE_ITEM_COLOR)
  22371. text.background = background->data.color;
  22372. else text.background = style->text_background;
  22373. /* select correct text colors */
  22374. if (state & NK_WIDGET_STATE_HOVER) {
  22375. sym = style->text_hover;
  22376. text.text = style->text_hover;
  22377. }
  22378. else if (state & NK_WIDGET_STATE_ACTIVED) {
  22379. sym = style->text_active;
  22380. text.text = style->text_active;
  22381. }
  22382. else {
  22383. sym = style->text_normal;
  22384. text.text = style->text_normal;
  22385. }
  22386. text.padding = nk_vec2(0, 0);
  22387. nk_draw_symbol(out, type, *symbol, style->text_background, sym, 0, font);
  22388. nk_widget_text(out, *label, str, len, &text, NK_TEXT_CENTERED, font);
  22389. }
  22390. NK_LIB nk_bool
  22391. nk_do_button_text_symbol(nk_flags* state,
  22392. struct nk_command_buffer* out, struct nk_rect bounds,
  22393. enum nk_symbol_type symbol, const char* str, int len, nk_flags align,
  22394. enum nk_button_behavior behavior, const struct nk_style_button* style,
  22395. const struct nk_user_font* font, const struct nk_input* in)
  22396. {
  22397. int ret;
  22398. struct nk_rect tri = { 0,0,0,0 };
  22399. struct nk_rect content;
  22400. NK_ASSERT(style);
  22401. NK_ASSERT(out);
  22402. NK_ASSERT(font);
  22403. if (!out || !style || !font)
  22404. return nk_false;
  22405. ret = nk_do_button(state, out, bounds, style, in, behavior, &content);
  22406. tri.y = content.y + (content.h / 2) - font->height / 2;
  22407. tri.w = font->height; tri.h = font->height;
  22408. if (align & NK_TEXT_ALIGN_LEFT) {
  22409. tri.x = (content.x + content.w) - (2 * style->padding.x + tri.w);
  22410. tri.x = NK_MAX(tri.x, 0);
  22411. }
  22412. else tri.x = content.x + 2 * style->padding.x;
  22413. /* draw button */
  22414. if (style->draw_begin) style->draw_begin(out, style->userdata);
  22415. nk_draw_button_text_symbol(out, &bounds, &content, &tri,
  22416. *state, style, str, len, symbol, font);
  22417. if (style->draw_end) style->draw_end(out, style->userdata);
  22418. return ret;
  22419. }
  22420. NK_LIB void
  22421. nk_draw_button_text_image(struct nk_command_buffer* out,
  22422. const struct nk_rect* bounds, const struct nk_rect* label,
  22423. const struct nk_rect* image, nk_flags state, const struct nk_style_button* style,
  22424. const char* str, int len, const struct nk_user_font* font,
  22425. const struct nk_image* img)
  22426. {
  22427. struct nk_text text;
  22428. const struct nk_style_item* background;
  22429. background = nk_draw_button(out, bounds, state, style);
  22430. /* select correct colors */
  22431. if (background->type == NK_STYLE_ITEM_COLOR)
  22432. text.background = background->data.color;
  22433. else text.background = style->text_background;
  22434. if (state & NK_WIDGET_STATE_HOVER)
  22435. text.text = style->text_hover;
  22436. else if (state & NK_WIDGET_STATE_ACTIVED)
  22437. text.text = style->text_active;
  22438. else text.text = style->text_normal;
  22439. text.padding = nk_vec2(0, 0);
  22440. nk_widget_text(out, *label, str, len, &text, NK_TEXT_CENTERED, font);
  22441. nk_draw_image(out, *image, img, nk_white);
  22442. }
  22443. NK_LIB nk_bool
  22444. nk_do_button_text_image(nk_flags* state,
  22445. struct nk_command_buffer* out, struct nk_rect bounds,
  22446. struct nk_image img, const char* str, int len, nk_flags align,
  22447. enum nk_button_behavior behavior, const struct nk_style_button* style,
  22448. const struct nk_user_font* font, const struct nk_input* in)
  22449. {
  22450. int ret;
  22451. struct nk_rect icon;
  22452. struct nk_rect content;
  22453. NK_ASSERT(style);
  22454. NK_ASSERT(state);
  22455. NK_ASSERT(font);
  22456. NK_ASSERT(out);
  22457. if (!out || !font || !style || !str)
  22458. return nk_false;
  22459. ret = nk_do_button(state, out, bounds, style, in, behavior, &content);
  22460. icon.y = bounds.y + style->padding.y;
  22461. icon.w = icon.h = bounds.h - 2 * style->padding.y;
  22462. if (align & NK_TEXT_ALIGN_LEFT) {
  22463. icon.x = (bounds.x + bounds.w) - (2 * style->padding.x + icon.w);
  22464. icon.x = NK_MAX(icon.x, 0);
  22465. }
  22466. else icon.x = bounds.x + 2 * style->padding.x;
  22467. icon.x += style->image_padding.x;
  22468. icon.y += style->image_padding.y;
  22469. icon.w -= 2 * style->image_padding.x;
  22470. icon.h -= 2 * style->image_padding.y;
  22471. if (style->draw_begin) style->draw_begin(out, style->userdata);
  22472. nk_draw_button_text_image(out, &bounds, &content, &icon, *state, style, str, len, font, &img);
  22473. if (style->draw_end) style->draw_end(out, style->userdata);
  22474. return ret;
  22475. }
  22476. NK_API void
  22477. nk_button_set_behavior(struct nk_context* ctx, enum nk_button_behavior behavior)
  22478. {
  22479. NK_ASSERT(ctx);
  22480. if (!ctx) return;
  22481. ctx->button_behavior = behavior;
  22482. }
  22483. NK_API nk_bool
  22484. nk_button_push_behavior(struct nk_context* ctx, enum nk_button_behavior behavior)
  22485. {
  22486. struct nk_config_stack_button_behavior* button_stack;
  22487. struct nk_config_stack_button_behavior_element* element;
  22488. NK_ASSERT(ctx);
  22489. if (!ctx) return 0;
  22490. button_stack = &ctx->stacks.button_behaviors;
  22491. NK_ASSERT(button_stack->head < (int)NK_LEN(button_stack->elements));
  22492. if (button_stack->head >= (int)NK_LEN(button_stack->elements))
  22493. return 0;
  22494. element = &button_stack->elements[button_stack->head++];
  22495. element->address = &ctx->button_behavior;
  22496. element->old_value = ctx->button_behavior;
  22497. ctx->button_behavior = behavior;
  22498. return 1;
  22499. }
  22500. NK_API nk_bool
  22501. nk_button_pop_behavior(struct nk_context* ctx)
  22502. {
  22503. struct nk_config_stack_button_behavior* button_stack;
  22504. struct nk_config_stack_button_behavior_element* element;
  22505. NK_ASSERT(ctx);
  22506. if (!ctx) return 0;
  22507. button_stack = &ctx->stacks.button_behaviors;
  22508. NK_ASSERT(button_stack->head > 0);
  22509. if (button_stack->head < 1)
  22510. return 0;
  22511. element = &button_stack->elements[--button_stack->head];
  22512. *element->address = element->old_value;
  22513. return 1;
  22514. }
  22515. NK_API nk_bool
  22516. nk_button_text_styled(struct nk_context* ctx,
  22517. const struct nk_style_button* style, const char* title, int len)
  22518. {
  22519. struct nk_window* win;
  22520. struct nk_panel* layout;
  22521. const struct nk_input* in;
  22522. struct nk_rect bounds;
  22523. enum nk_widget_layout_states state;
  22524. NK_ASSERT(ctx);
  22525. NK_ASSERT(style);
  22526. NK_ASSERT(ctx->current);
  22527. NK_ASSERT(ctx->current->layout);
  22528. if (!style || !ctx || !ctx->current || !ctx->current->layout) return 0;
  22529. win = ctx->current;
  22530. layout = win->layout;
  22531. state = nk_widget(&bounds, ctx);
  22532. if (!state) return 0;
  22533. in = (state == NK_WIDGET_ROM || layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  22534. return nk_do_button_text(&ctx->last_widget_state, &win->buffer, bounds,
  22535. title, len, style->text_alignment, ctx->button_behavior,
  22536. style, in, ctx->style.font);
  22537. }
  22538. NK_API nk_bool
  22539. nk_button_text(struct nk_context* ctx, const char* title, int len)
  22540. {
  22541. NK_ASSERT(ctx);
  22542. if (!ctx) return 0;
  22543. return nk_button_text_styled(ctx, &ctx->style.button, title, len);
  22544. }
  22545. NK_API nk_bool nk_button_label_styled(struct nk_context* ctx,
  22546. const struct nk_style_button* style, const char* title)
  22547. {
  22548. return nk_button_text_styled(ctx, style, title, nk_strlen(title));
  22549. }
  22550. NK_API nk_bool nk_button_label(struct nk_context* ctx, const char* title)
  22551. {
  22552. return nk_button_text(ctx, title, nk_strlen(title));
  22553. }
  22554. NK_API nk_bool
  22555. nk_button_color(struct nk_context* ctx, struct nk_color color)
  22556. {
  22557. struct nk_window* win;
  22558. struct nk_panel* layout;
  22559. const struct nk_input* in;
  22560. struct nk_style_button button;
  22561. int ret = 0;
  22562. struct nk_rect bounds;
  22563. struct nk_rect content;
  22564. enum nk_widget_layout_states state;
  22565. NK_ASSERT(ctx);
  22566. NK_ASSERT(ctx->current);
  22567. NK_ASSERT(ctx->current->layout);
  22568. if (!ctx || !ctx->current || !ctx->current->layout)
  22569. return 0;
  22570. win = ctx->current;
  22571. layout = win->layout;
  22572. state = nk_widget(&bounds, ctx);
  22573. if (!state) return 0;
  22574. in = (state == NK_WIDGET_ROM || layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  22575. button = ctx->style.button;
  22576. button.normal = nk_style_item_color(color);
  22577. button.hover = nk_style_item_color(color);
  22578. button.active = nk_style_item_color(color);
  22579. ret = nk_do_button(&ctx->last_widget_state, &win->buffer, bounds,
  22580. &button, in, ctx->button_behavior, &content);
  22581. nk_draw_button(&win->buffer, &bounds, ctx->last_widget_state, &button);
  22582. return ret;
  22583. }
  22584. NK_API nk_bool
  22585. nk_button_symbol_styled(struct nk_context* ctx,
  22586. const struct nk_style_button* style, enum nk_symbol_type symbol)
  22587. {
  22588. struct nk_window* win;
  22589. struct nk_panel* layout;
  22590. const struct nk_input* in;
  22591. struct nk_rect bounds;
  22592. enum nk_widget_layout_states state;
  22593. NK_ASSERT(ctx);
  22594. NK_ASSERT(ctx->current);
  22595. NK_ASSERT(ctx->current->layout);
  22596. if (!ctx || !ctx->current || !ctx->current->layout)
  22597. return 0;
  22598. win = ctx->current;
  22599. layout = win->layout;
  22600. state = nk_widget(&bounds, ctx);
  22601. if (!state) return 0;
  22602. in = (state == NK_WIDGET_ROM || layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  22603. return nk_do_button_symbol(&ctx->last_widget_state, &win->buffer, bounds,
  22604. symbol, ctx->button_behavior, style, in, ctx->style.font);
  22605. }
  22606. NK_API nk_bool
  22607. nk_button_symbol(struct nk_context* ctx, enum nk_symbol_type symbol)
  22608. {
  22609. NK_ASSERT(ctx);
  22610. if (!ctx) return 0;
  22611. return nk_button_symbol_styled(ctx, &ctx->style.button, symbol);
  22612. }
  22613. NK_API nk_bool
  22614. nk_button_image_styled(struct nk_context* ctx, const struct nk_style_button* style,
  22615. struct nk_image img)
  22616. {
  22617. struct nk_window* win;
  22618. struct nk_panel* layout;
  22619. const struct nk_input* in;
  22620. struct nk_rect bounds;
  22621. enum nk_widget_layout_states state;
  22622. NK_ASSERT(ctx);
  22623. NK_ASSERT(ctx->current);
  22624. NK_ASSERT(ctx->current->layout);
  22625. if (!ctx || !ctx->current || !ctx->current->layout)
  22626. return 0;
  22627. win = ctx->current;
  22628. layout = win->layout;
  22629. state = nk_widget(&bounds, ctx);
  22630. if (!state) return 0;
  22631. in = (state == NK_WIDGET_ROM || layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  22632. return nk_do_button_image(&ctx->last_widget_state, &win->buffer, bounds,
  22633. img, ctx->button_behavior, style, in);
  22634. }
  22635. NK_API nk_bool
  22636. nk_button_image(struct nk_context* ctx, struct nk_image img)
  22637. {
  22638. NK_ASSERT(ctx);
  22639. if (!ctx) return 0;
  22640. return nk_button_image_styled(ctx, &ctx->style.button, img);
  22641. }
  22642. NK_API nk_bool
  22643. nk_button_symbol_text_styled(struct nk_context* ctx,
  22644. const struct nk_style_button* style, enum nk_symbol_type symbol,
  22645. const char* text, int len, nk_flags align)
  22646. {
  22647. struct nk_window* win;
  22648. struct nk_panel* layout;
  22649. const struct nk_input* in;
  22650. struct nk_rect bounds;
  22651. enum nk_widget_layout_states state;
  22652. NK_ASSERT(ctx);
  22653. NK_ASSERT(ctx->current);
  22654. NK_ASSERT(ctx->current->layout);
  22655. if (!ctx || !ctx->current || !ctx->current->layout)
  22656. return 0;
  22657. win = ctx->current;
  22658. layout = win->layout;
  22659. state = nk_widget(&bounds, ctx);
  22660. if (!state) return 0;
  22661. in = (state == NK_WIDGET_ROM || layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  22662. return nk_do_button_text_symbol(&ctx->last_widget_state, &win->buffer, bounds,
  22663. symbol, text, len, align, ctx->button_behavior,
  22664. style, ctx->style.font, in);
  22665. }
  22666. NK_API nk_bool
  22667. nk_button_symbol_text(struct nk_context* ctx, enum nk_symbol_type symbol,
  22668. const char* text, int len, nk_flags align)
  22669. {
  22670. NK_ASSERT(ctx);
  22671. if (!ctx) return 0;
  22672. return nk_button_symbol_text_styled(ctx, &ctx->style.button, symbol, text, len, align);
  22673. }
  22674. NK_API nk_bool nk_button_symbol_label(struct nk_context* ctx, enum nk_symbol_type symbol,
  22675. const char* label, nk_flags align)
  22676. {
  22677. return nk_button_symbol_text(ctx, symbol, label, nk_strlen(label), align);
  22678. }
  22679. NK_API nk_bool nk_button_symbol_label_styled(struct nk_context* ctx,
  22680. const struct nk_style_button* style, enum nk_symbol_type symbol,
  22681. const char* title, nk_flags align)
  22682. {
  22683. return nk_button_symbol_text_styled(ctx, style, symbol, title, nk_strlen(title), align);
  22684. }
  22685. NK_API nk_bool
  22686. nk_button_image_text_styled(struct nk_context* ctx,
  22687. const struct nk_style_button* style, struct nk_image img, const char* text,
  22688. int len, nk_flags align)
  22689. {
  22690. struct nk_window* win;
  22691. struct nk_panel* layout;
  22692. const struct nk_input* in;
  22693. struct nk_rect bounds;
  22694. enum nk_widget_layout_states state;
  22695. NK_ASSERT(ctx);
  22696. NK_ASSERT(ctx->current);
  22697. NK_ASSERT(ctx->current->layout);
  22698. if (!ctx || !ctx->current || !ctx->current->layout)
  22699. return 0;
  22700. win = ctx->current;
  22701. layout = win->layout;
  22702. state = nk_widget(&bounds, ctx);
  22703. if (!state) return 0;
  22704. in = (state == NK_WIDGET_ROM || layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  22705. return nk_do_button_text_image(&ctx->last_widget_state, &win->buffer,
  22706. bounds, img, text, len, align, ctx->button_behavior,
  22707. style, ctx->style.font, in);
  22708. }
  22709. NK_API nk_bool
  22710. nk_button_image_text(struct nk_context* ctx, struct nk_image img,
  22711. const char* text, int len, nk_flags align)
  22712. {
  22713. return nk_button_image_text_styled(ctx, &ctx->style.button, img, text, len, align);
  22714. }
  22715. NK_API nk_bool nk_button_image_label(struct nk_context* ctx, struct nk_image img,
  22716. const char* label, nk_flags align)
  22717. {
  22718. return nk_button_image_text(ctx, img, label, nk_strlen(label), align);
  22719. }
  22720. NK_API nk_bool nk_button_image_label_styled(struct nk_context* ctx,
  22721. const struct nk_style_button* style, struct nk_image img,
  22722. const char* label, nk_flags text_alignment)
  22723. {
  22724. return nk_button_image_text_styled(ctx, style, img, label, nk_strlen(label), text_alignment);
  22725. }
  22726. /* ===============================================================
  22727. *
  22728. * TOGGLE
  22729. *
  22730. * ===============================================================*/
  22731. NK_LIB nk_bool
  22732. nk_toggle_behavior(const struct nk_input* in, struct nk_rect select,
  22733. nk_flags* state, nk_bool active)
  22734. {
  22735. nk_widget_state_reset(state);
  22736. if (nk_button_behavior(state, select, in, NK_BUTTON_DEFAULT)) {
  22737. *state = NK_WIDGET_STATE_ACTIVE;
  22738. active = !active;
  22739. }
  22740. if (*state & NK_WIDGET_STATE_HOVER && !nk_input_is_mouse_prev_hovering_rect(in, select))
  22741. *state |= NK_WIDGET_STATE_ENTERED;
  22742. else if (nk_input_is_mouse_prev_hovering_rect(in, select))
  22743. *state |= NK_WIDGET_STATE_LEFT;
  22744. return active;
  22745. }
  22746. NK_LIB void
  22747. nk_draw_checkbox(struct nk_command_buffer* out,
  22748. nk_flags state, const struct nk_style_toggle* style, nk_bool active,
  22749. const struct nk_rect* label, const struct nk_rect* selector,
  22750. const struct nk_rect* cursors, const char* string, int len,
  22751. const struct nk_user_font* font)
  22752. {
  22753. const struct nk_style_item* background;
  22754. const struct nk_style_item* cursor;
  22755. struct nk_text text;
  22756. /* select correct colors/images */
  22757. if (state & NK_WIDGET_STATE_HOVER) {
  22758. background = &style->hover;
  22759. cursor = &style->cursor_hover;
  22760. text.text = style->text_hover;
  22761. }
  22762. else if (state & NK_WIDGET_STATE_ACTIVED) {
  22763. background = &style->hover;
  22764. cursor = &style->cursor_hover;
  22765. text.text = style->text_active;
  22766. }
  22767. else {
  22768. background = &style->normal;
  22769. cursor = &style->cursor_normal;
  22770. text.text = style->text_normal;
  22771. }
  22772. /* draw background and cursor */
  22773. if (background->type == NK_STYLE_ITEM_COLOR) {
  22774. nk_fill_rect(out, *selector, 0, style->border_color);
  22775. nk_fill_rect(out, nk_shrink_rect(*selector, style->border), 0, background->data.color);
  22776. }
  22777. else nk_draw_image(out, *selector, &background->data.image, nk_white);
  22778. if (active) {
  22779. if (cursor->type == NK_STYLE_ITEM_IMAGE)
  22780. nk_draw_image(out, *cursors, &cursor->data.image, nk_white);
  22781. else nk_fill_rect(out, *cursors, 0, cursor->data.color);
  22782. }
  22783. text.padding.x = 0;
  22784. text.padding.y = 0;
  22785. text.background = style->text_background;
  22786. nk_widget_text(out, *label, string, len, &text, NK_TEXT_LEFT, font);
  22787. }
  22788. NK_LIB void
  22789. nk_draw_option(struct nk_command_buffer* out,
  22790. nk_flags state, const struct nk_style_toggle* style, nk_bool active,
  22791. const struct nk_rect* label, const struct nk_rect* selector,
  22792. const struct nk_rect* cursors, const char* string, int len,
  22793. const struct nk_user_font* font)
  22794. {
  22795. const struct nk_style_item* background;
  22796. const struct nk_style_item* cursor;
  22797. struct nk_text text;
  22798. /* select correct colors/images */
  22799. if (state & NK_WIDGET_STATE_HOVER) {
  22800. background = &style->hover;
  22801. cursor = &style->cursor_hover;
  22802. text.text = style->text_hover;
  22803. }
  22804. else if (state & NK_WIDGET_STATE_ACTIVED) {
  22805. background = &style->hover;
  22806. cursor = &style->cursor_hover;
  22807. text.text = style->text_active;
  22808. }
  22809. else {
  22810. background = &style->normal;
  22811. cursor = &style->cursor_normal;
  22812. text.text = style->text_normal;
  22813. }
  22814. /* draw background and cursor */
  22815. if (background->type == NK_STYLE_ITEM_COLOR) {
  22816. nk_fill_circle(out, *selector, style->border_color);
  22817. nk_fill_circle(out, nk_shrink_rect(*selector, style->border), background->data.color);
  22818. }
  22819. else nk_draw_image(out, *selector, &background->data.image, nk_white);
  22820. if (active) {
  22821. if (cursor->type == NK_STYLE_ITEM_IMAGE)
  22822. nk_draw_image(out, *cursors, &cursor->data.image, nk_white);
  22823. else nk_fill_circle(out, *cursors, cursor->data.color);
  22824. }
  22825. text.padding.x = 0;
  22826. text.padding.y = 0;
  22827. text.background = style->text_background;
  22828. nk_widget_text(out, *label, string, len, &text, NK_TEXT_LEFT, font);
  22829. }
  22830. NK_LIB nk_bool
  22831. nk_do_toggle(nk_flags* state,
  22832. struct nk_command_buffer* out, struct nk_rect r,
  22833. nk_bool* active, const char* str, int len, enum nk_toggle_type type,
  22834. const struct nk_style_toggle* style, const struct nk_input* in,
  22835. const struct nk_user_font* font)
  22836. {
  22837. int was_active;
  22838. struct nk_rect bounds;
  22839. struct nk_rect select;
  22840. struct nk_rect cursor;
  22841. struct nk_rect label;
  22842. NK_ASSERT(style);
  22843. NK_ASSERT(out);
  22844. NK_ASSERT(font);
  22845. if (!out || !style || !font || !active)
  22846. return 0;
  22847. r.w = NK_MAX(r.w, font->height + 2 * style->padding.x);
  22848. r.h = NK_MAX(r.h, font->height + 2 * style->padding.y);
  22849. /* add additional touch padding for touch screen devices */
  22850. bounds.x = r.x - style->touch_padding.x;
  22851. bounds.y = r.y - style->touch_padding.y;
  22852. bounds.w = r.w + 2 * style->touch_padding.x;
  22853. bounds.h = r.h + 2 * style->touch_padding.y;
  22854. /* calculate the selector space */
  22855. select.w = font->height;
  22856. select.h = select.w;
  22857. select.y = r.y + r.h / 2.0f - select.h / 2.0f;
  22858. select.x = r.x;
  22859. /* calculate the bounds of the cursor inside the selector */
  22860. cursor.x = select.x + style->padding.x + style->border;
  22861. cursor.y = select.y + style->padding.y + style->border;
  22862. cursor.w = select.w - (2 * style->padding.x + 2 * style->border);
  22863. cursor.h = select.h - (2 * style->padding.y + 2 * style->border);
  22864. /* label behind the selector */
  22865. label.x = select.x + select.w + style->spacing;
  22866. label.y = select.y;
  22867. label.w = NK_MAX(r.x + r.w, label.x) - label.x;
  22868. label.h = select.w;
  22869. /* update selector */
  22870. was_active = *active;
  22871. *active = nk_toggle_behavior(in, bounds, state, *active);
  22872. /* draw selector */
  22873. if (style->draw_begin)
  22874. style->draw_begin(out, style->userdata);
  22875. if (type == NK_TOGGLE_CHECK) {
  22876. nk_draw_checkbox(out, *state, style, *active, &label, &select, &cursor, str, len, font);
  22877. }
  22878. else {
  22879. nk_draw_option(out, *state, style, *active, &label, &select, &cursor, str, len, font);
  22880. }
  22881. if (style->draw_end)
  22882. style->draw_end(out, style->userdata);
  22883. return (was_active != *active);
  22884. }
  22885. /*----------------------------------------------------------------
  22886. *
  22887. * CHECKBOX
  22888. *
  22889. * --------------------------------------------------------------*/
  22890. NK_API nk_bool
  22891. nk_check_text(struct nk_context* ctx, const char* text, int len, nk_bool active)
  22892. {
  22893. struct nk_window* win;
  22894. struct nk_panel* layout;
  22895. const struct nk_input* in;
  22896. const struct nk_style* style;
  22897. struct nk_rect bounds;
  22898. enum nk_widget_layout_states state;
  22899. NK_ASSERT(ctx);
  22900. NK_ASSERT(ctx->current);
  22901. NK_ASSERT(ctx->current->layout);
  22902. if (!ctx || !ctx->current || !ctx->current->layout)
  22903. return active;
  22904. win = ctx->current;
  22905. style = &ctx->style;
  22906. layout = win->layout;
  22907. state = nk_widget(&bounds, ctx);
  22908. if (!state) return active;
  22909. in = (state == NK_WIDGET_ROM || layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  22910. nk_do_toggle(&ctx->last_widget_state, &win->buffer, bounds, &active,
  22911. text, len, NK_TOGGLE_CHECK, &style->checkbox, in, style->font);
  22912. return active;
  22913. }
  22914. NK_API unsigned int
  22915. nk_check_flags_text(struct nk_context* ctx, const char* text, int len,
  22916. unsigned int flags, unsigned int value)
  22917. {
  22918. int old_active;
  22919. NK_ASSERT(ctx);
  22920. NK_ASSERT(text);
  22921. if (!ctx || !text) return flags;
  22922. old_active = (int)((flags & value) & value);
  22923. if (nk_check_text(ctx, text, len, old_active))
  22924. flags |= value;
  22925. else flags &= ~value;
  22926. return flags;
  22927. }
  22928. NK_API nk_bool
  22929. nk_checkbox_text(struct nk_context* ctx, const char* text, int len, nk_bool* active)
  22930. {
  22931. int old_val;
  22932. NK_ASSERT(ctx);
  22933. NK_ASSERT(text);
  22934. NK_ASSERT(active);
  22935. if (!ctx || !text || !active) return 0;
  22936. old_val = *active;
  22937. *active = nk_check_text(ctx, text, len, *active);
  22938. return old_val != *active;
  22939. }
  22940. NK_API nk_bool
  22941. nk_checkbox_flags_text(struct nk_context* ctx, const char* text, int len,
  22942. unsigned int* flags, unsigned int value)
  22943. {
  22944. nk_bool active;
  22945. NK_ASSERT(ctx);
  22946. NK_ASSERT(text);
  22947. NK_ASSERT(flags);
  22948. if (!ctx || !text || !flags) return 0;
  22949. active = (int)((*flags & value) & value);
  22950. if (nk_checkbox_text(ctx, text, len, &active)) {
  22951. if (active) *flags |= value;
  22952. else *flags &= ~value;
  22953. return 1;
  22954. }
  22955. return 0;
  22956. }
  22957. NK_API nk_bool nk_check_label(struct nk_context* ctx, const char* label, nk_bool active)
  22958. {
  22959. return nk_check_text(ctx, label, nk_strlen(label), active);
  22960. }
  22961. NK_API unsigned int nk_check_flags_label(struct nk_context* ctx, const char* label,
  22962. unsigned int flags, unsigned int value)
  22963. {
  22964. return nk_check_flags_text(ctx, label, nk_strlen(label), flags, value);
  22965. }
  22966. NK_API nk_bool nk_checkbox_label(struct nk_context* ctx, const char* label, nk_bool* active)
  22967. {
  22968. return nk_checkbox_text(ctx, label, nk_strlen(label), active);
  22969. }
  22970. NK_API nk_bool nk_checkbox_flags_label(struct nk_context* ctx, const char* label,
  22971. unsigned int* flags, unsigned int value)
  22972. {
  22973. return nk_checkbox_flags_text(ctx, label, nk_strlen(label), flags, value);
  22974. }
  22975. /*----------------------------------------------------------------
  22976. *
  22977. * OPTION
  22978. *
  22979. * --------------------------------------------------------------*/
  22980. NK_API nk_bool
  22981. nk_option_text(struct nk_context* ctx, const char* text, int len, nk_bool is_active)
  22982. {
  22983. struct nk_window* win;
  22984. struct nk_panel* layout;
  22985. const struct nk_input* in;
  22986. const struct nk_style* style;
  22987. struct nk_rect bounds;
  22988. enum nk_widget_layout_states state;
  22989. NK_ASSERT(ctx);
  22990. NK_ASSERT(ctx->current);
  22991. NK_ASSERT(ctx->current->layout);
  22992. if (!ctx || !ctx->current || !ctx->current->layout)
  22993. return is_active;
  22994. win = ctx->current;
  22995. style = &ctx->style;
  22996. layout = win->layout;
  22997. state = nk_widget(&bounds, ctx);
  22998. if (!state) return (int)state;
  22999. in = (state == NK_WIDGET_ROM || layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  23000. nk_do_toggle(&ctx->last_widget_state, &win->buffer, bounds, &is_active,
  23001. text, len, NK_TOGGLE_OPTION, &style->option, in, style->font);
  23002. return is_active;
  23003. }
  23004. NK_API nk_bool
  23005. nk_radio_text(struct nk_context* ctx, const char* text, int len, nk_bool* active)
  23006. {
  23007. int old_value;
  23008. NK_ASSERT(ctx);
  23009. NK_ASSERT(text);
  23010. NK_ASSERT(active);
  23011. if (!ctx || !text || !active) return 0;
  23012. old_value = *active;
  23013. *active = nk_option_text(ctx, text, len, old_value);
  23014. return old_value != *active;
  23015. }
  23016. NK_API nk_bool
  23017. nk_option_label(struct nk_context* ctx, const char* label, nk_bool active)
  23018. {
  23019. return nk_option_text(ctx, label, nk_strlen(label), active);
  23020. }
  23021. NK_API nk_bool
  23022. nk_radio_label(struct nk_context* ctx, const char* label, nk_bool* active)
  23023. {
  23024. return nk_radio_text(ctx, label, nk_strlen(label), active);
  23025. }
  23026. /* ===============================================================
  23027. *
  23028. * SELECTABLE
  23029. *
  23030. * ===============================================================*/
  23031. NK_LIB void
  23032. nk_draw_selectable(struct nk_command_buffer* out,
  23033. nk_flags state, const struct nk_style_selectable* style, nk_bool active,
  23034. const struct nk_rect* bounds,
  23035. const struct nk_rect* icon, const struct nk_image* img, enum nk_symbol_type sym,
  23036. const char* string, int len, nk_flags align, const struct nk_user_font* font)
  23037. {
  23038. const struct nk_style_item* background;
  23039. struct nk_text text;
  23040. text.padding = style->padding;
  23041. /* select correct colors/images */
  23042. if (!active) {
  23043. if (state & NK_WIDGET_STATE_ACTIVED) {
  23044. background = &style->pressed;
  23045. text.text = style->text_pressed;
  23046. }
  23047. else if (state & NK_WIDGET_STATE_HOVER) {
  23048. background = &style->hover;
  23049. text.text = style->text_hover;
  23050. }
  23051. else {
  23052. background = &style->normal;
  23053. text.text = style->text_normal;
  23054. }
  23055. }
  23056. else {
  23057. if (state & NK_WIDGET_STATE_ACTIVED) {
  23058. background = &style->pressed_active;
  23059. text.text = style->text_pressed_active;
  23060. }
  23061. else if (state & NK_WIDGET_STATE_HOVER) {
  23062. background = &style->hover_active;
  23063. text.text = style->text_hover_active;
  23064. }
  23065. else {
  23066. background = &style->normal_active;
  23067. text.text = style->text_normal_active;
  23068. }
  23069. }
  23070. /* draw selectable background and text */
  23071. switch (background->type) {
  23072. case NK_STYLE_ITEM_IMAGE:
  23073. text.background = nk_rgba(0, 0, 0, 0);
  23074. nk_draw_image(out, *bounds, &background->data.image, nk_white);
  23075. break;
  23076. case NK_STYLE_ITEM_NINE_SLICE:
  23077. text.background = nk_rgba(0, 0, 0, 0);
  23078. nk_draw_nine_slice(out, *bounds, &background->data.slice, nk_white);
  23079. break;
  23080. case NK_STYLE_ITEM_COLOR:
  23081. text.background = background->data.color;
  23082. nk_fill_rect(out, *bounds, style->rounding, background->data.color);
  23083. break;
  23084. }
  23085. if (icon) {
  23086. if (img) nk_draw_image(out, *icon, img, nk_white);
  23087. else nk_draw_symbol(out, sym, *icon, text.background, text.text, 1, font);
  23088. }
  23089. nk_widget_text(out, *bounds, string, len, &text, align, font);
  23090. }
  23091. NK_LIB nk_bool
  23092. nk_do_selectable(nk_flags* state, struct nk_command_buffer* out,
  23093. struct nk_rect bounds, const char* str, int len, nk_flags align, nk_bool* value,
  23094. const struct nk_style_selectable* style, const struct nk_input* in,
  23095. const struct nk_user_font* font)
  23096. {
  23097. int old_value;
  23098. struct nk_rect touch;
  23099. NK_ASSERT(state);
  23100. NK_ASSERT(out);
  23101. NK_ASSERT(str);
  23102. NK_ASSERT(len);
  23103. NK_ASSERT(value);
  23104. NK_ASSERT(style);
  23105. NK_ASSERT(font);
  23106. if (!state || !out || !str || !len || !value || !style || !font) return 0;
  23107. old_value = *value;
  23108. /* remove padding */
  23109. touch.x = bounds.x - style->touch_padding.x;
  23110. touch.y = bounds.y - style->touch_padding.y;
  23111. touch.w = bounds.w + style->touch_padding.x * 2;
  23112. touch.h = bounds.h + style->touch_padding.y * 2;
  23113. /* update button */
  23114. if (nk_button_behavior(state, touch, in, NK_BUTTON_DEFAULT))
  23115. *value = !(*value);
  23116. /* draw selectable */
  23117. if (style->draw_begin) style->draw_begin(out, style->userdata);
  23118. nk_draw_selectable(out, *state, style, *value, &bounds, 0, 0, NK_SYMBOL_NONE, str, len, align, font);
  23119. if (style->draw_end) style->draw_end(out, style->userdata);
  23120. return old_value != *value;
  23121. }
  23122. NK_LIB nk_bool
  23123. nk_do_selectable_image(nk_flags* state, struct nk_command_buffer* out,
  23124. struct nk_rect bounds, const char* str, int len, nk_flags align, nk_bool* value,
  23125. const struct nk_image* img, const struct nk_style_selectable* style,
  23126. const struct nk_input* in, const struct nk_user_font* font)
  23127. {
  23128. nk_bool old_value;
  23129. struct nk_rect touch;
  23130. struct nk_rect icon;
  23131. NK_ASSERT(state);
  23132. NK_ASSERT(out);
  23133. NK_ASSERT(str);
  23134. NK_ASSERT(len);
  23135. NK_ASSERT(value);
  23136. NK_ASSERT(style);
  23137. NK_ASSERT(font);
  23138. if (!state || !out || !str || !len || !value || !style || !font) return 0;
  23139. old_value = *value;
  23140. /* toggle behavior */
  23141. touch.x = bounds.x - style->touch_padding.x;
  23142. touch.y = bounds.y - style->touch_padding.y;
  23143. touch.w = bounds.w + style->touch_padding.x * 2;
  23144. touch.h = bounds.h + style->touch_padding.y * 2;
  23145. if (nk_button_behavior(state, touch, in, NK_BUTTON_DEFAULT))
  23146. *value = !(*value);
  23147. icon.y = bounds.y + style->padding.y;
  23148. icon.w = icon.h = bounds.h - 2 * style->padding.y;
  23149. if (align & NK_TEXT_ALIGN_LEFT) {
  23150. icon.x = (bounds.x + bounds.w) - (2 * style->padding.x + icon.w);
  23151. icon.x = NK_MAX(icon.x, 0);
  23152. }
  23153. else icon.x = bounds.x + 2 * style->padding.x;
  23154. icon.x += style->image_padding.x;
  23155. icon.y += style->image_padding.y;
  23156. icon.w -= 2 * style->image_padding.x;
  23157. icon.h -= 2 * style->image_padding.y;
  23158. /* draw selectable */
  23159. if (style->draw_begin) style->draw_begin(out, style->userdata);
  23160. nk_draw_selectable(out, *state, style, *value, &bounds, &icon, img, NK_SYMBOL_NONE, str, len, align, font);
  23161. if (style->draw_end) style->draw_end(out, style->userdata);
  23162. return old_value != *value;
  23163. }
  23164. NK_LIB nk_bool
  23165. nk_do_selectable_symbol(nk_flags* state, struct nk_command_buffer* out,
  23166. struct nk_rect bounds, const char* str, int len, nk_flags align, nk_bool* value,
  23167. enum nk_symbol_type sym, const struct nk_style_selectable* style,
  23168. const struct nk_input* in, const struct nk_user_font* font)
  23169. {
  23170. int old_value;
  23171. struct nk_rect touch;
  23172. struct nk_rect icon;
  23173. NK_ASSERT(state);
  23174. NK_ASSERT(out);
  23175. NK_ASSERT(str);
  23176. NK_ASSERT(len);
  23177. NK_ASSERT(value);
  23178. NK_ASSERT(style);
  23179. NK_ASSERT(font);
  23180. if (!state || !out || !str || !len || !value || !style || !font) return 0;
  23181. old_value = *value;
  23182. /* toggle behavior */
  23183. touch.x = bounds.x - style->touch_padding.x;
  23184. touch.y = bounds.y - style->touch_padding.y;
  23185. touch.w = bounds.w + style->touch_padding.x * 2;
  23186. touch.h = bounds.h + style->touch_padding.y * 2;
  23187. if (nk_button_behavior(state, touch, in, NK_BUTTON_DEFAULT))
  23188. *value = !(*value);
  23189. icon.y = bounds.y + style->padding.y;
  23190. icon.w = icon.h = bounds.h - 2 * style->padding.y;
  23191. if (align & NK_TEXT_ALIGN_LEFT) {
  23192. icon.x = (bounds.x + bounds.w) - (2 * style->padding.x + icon.w);
  23193. icon.x = NK_MAX(icon.x, 0);
  23194. }
  23195. else icon.x = bounds.x + 2 * style->padding.x;
  23196. icon.x += style->image_padding.x;
  23197. icon.y += style->image_padding.y;
  23198. icon.w -= 2 * style->image_padding.x;
  23199. icon.h -= 2 * style->image_padding.y;
  23200. /* draw selectable */
  23201. if (style->draw_begin) style->draw_begin(out, style->userdata);
  23202. nk_draw_selectable(out, *state, style, *value, &bounds, &icon, 0, sym, str, len, align, font);
  23203. if (style->draw_end) style->draw_end(out, style->userdata);
  23204. return old_value != *value;
  23205. }
  23206. NK_API nk_bool
  23207. nk_selectable_text(struct nk_context* ctx, const char* str, int len,
  23208. nk_flags align, nk_bool* value)
  23209. {
  23210. struct nk_window* win;
  23211. struct nk_panel* layout;
  23212. const struct nk_input* in;
  23213. const struct nk_style* style;
  23214. enum nk_widget_layout_states state;
  23215. struct nk_rect bounds;
  23216. NK_ASSERT(ctx);
  23217. NK_ASSERT(value);
  23218. NK_ASSERT(ctx->current);
  23219. NK_ASSERT(ctx->current->layout);
  23220. if (!ctx || !ctx->current || !ctx->current->layout || !value)
  23221. return 0;
  23222. win = ctx->current;
  23223. layout = win->layout;
  23224. style = &ctx->style;
  23225. state = nk_widget(&bounds, ctx);
  23226. if (!state) return 0;
  23227. in = (state == NK_WIDGET_ROM || layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  23228. return nk_do_selectable(&ctx->last_widget_state, &win->buffer, bounds,
  23229. str, len, align, value, &style->selectable, in, style->font);
  23230. }
  23231. NK_API nk_bool
  23232. nk_selectable_image_text(struct nk_context* ctx, struct nk_image img,
  23233. const char* str, int len, nk_flags align, nk_bool* value)
  23234. {
  23235. struct nk_window* win;
  23236. struct nk_panel* layout;
  23237. const struct nk_input* in;
  23238. const struct nk_style* style;
  23239. enum nk_widget_layout_states state;
  23240. struct nk_rect bounds;
  23241. NK_ASSERT(ctx);
  23242. NK_ASSERT(value);
  23243. NK_ASSERT(ctx->current);
  23244. NK_ASSERT(ctx->current->layout);
  23245. if (!ctx || !ctx->current || !ctx->current->layout || !value)
  23246. return 0;
  23247. win = ctx->current;
  23248. layout = win->layout;
  23249. style = &ctx->style;
  23250. state = nk_widget(&bounds, ctx);
  23251. if (!state) return 0;
  23252. in = (state == NK_WIDGET_ROM || layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  23253. return nk_do_selectable_image(&ctx->last_widget_state, &win->buffer, bounds,
  23254. str, len, align, value, &img, &style->selectable, in, style->font);
  23255. }
  23256. NK_API nk_bool
  23257. nk_selectable_symbol_text(struct nk_context* ctx, enum nk_symbol_type sym,
  23258. const char* str, int len, nk_flags align, nk_bool* value)
  23259. {
  23260. struct nk_window* win;
  23261. struct nk_panel* layout;
  23262. const struct nk_input* in;
  23263. const struct nk_style* style;
  23264. enum nk_widget_layout_states state;
  23265. struct nk_rect bounds;
  23266. NK_ASSERT(ctx);
  23267. NK_ASSERT(value);
  23268. NK_ASSERT(ctx->current);
  23269. NK_ASSERT(ctx->current->layout);
  23270. if (!ctx || !ctx->current || !ctx->current->layout || !value)
  23271. return 0;
  23272. win = ctx->current;
  23273. layout = win->layout;
  23274. style = &ctx->style;
  23275. state = nk_widget(&bounds, ctx);
  23276. if (!state) return 0;
  23277. in = (state == NK_WIDGET_ROM || layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  23278. return nk_do_selectable_symbol(&ctx->last_widget_state, &win->buffer, bounds,
  23279. str, len, align, value, sym, &style->selectable, in, style->font);
  23280. }
  23281. NK_API nk_bool
  23282. nk_selectable_symbol_label(struct nk_context* ctx, enum nk_symbol_type sym,
  23283. const char* title, nk_flags align, nk_bool* value)
  23284. {
  23285. return nk_selectable_symbol_text(ctx, sym, title, nk_strlen(title), align, value);
  23286. }
  23287. NK_API nk_bool nk_select_text(struct nk_context* ctx, const char* str, int len,
  23288. nk_flags align, nk_bool value)
  23289. {
  23290. nk_selectable_text(ctx, str, len, align, &value); return value;
  23291. }
  23292. NK_API nk_bool nk_selectable_label(struct nk_context* ctx, const char* str, nk_flags align, nk_bool* value)
  23293. {
  23294. return nk_selectable_text(ctx, str, nk_strlen(str), align, value);
  23295. }
  23296. NK_API nk_bool nk_selectable_image_label(struct nk_context* ctx, struct nk_image img,
  23297. const char* str, nk_flags align, nk_bool* value)
  23298. {
  23299. return nk_selectable_image_text(ctx, img, str, nk_strlen(str), align, value);
  23300. }
  23301. NK_API nk_bool nk_select_label(struct nk_context* ctx, const char* str, nk_flags align, nk_bool value)
  23302. {
  23303. nk_selectable_text(ctx, str, nk_strlen(str), align, &value); return value;
  23304. }
  23305. NK_API nk_bool nk_select_image_label(struct nk_context* ctx, struct nk_image img,
  23306. const char* str, nk_flags align, nk_bool value)
  23307. {
  23308. nk_selectable_image_text(ctx, img, str, nk_strlen(str), align, &value); return value;
  23309. }
  23310. NK_API nk_bool nk_select_image_text(struct nk_context* ctx, struct nk_image img,
  23311. const char* str, int len, nk_flags align, nk_bool value)
  23312. {
  23313. nk_selectable_image_text(ctx, img, str, len, align, &value); return value;
  23314. }
  23315. NK_API nk_bool
  23316. nk_select_symbol_text(struct nk_context* ctx, enum nk_symbol_type sym,
  23317. const char* title, int title_len, nk_flags align, nk_bool value)
  23318. {
  23319. nk_selectable_symbol_text(ctx, sym, title, title_len, align, &value); return value;
  23320. }
  23321. NK_API nk_bool
  23322. nk_select_symbol_label(struct nk_context* ctx, enum nk_symbol_type sym,
  23323. const char* title, nk_flags align, nk_bool value)
  23324. {
  23325. return nk_select_symbol_text(ctx, sym, title, nk_strlen(title), align, value);
  23326. }
  23327. /* ===============================================================
  23328. *
  23329. * SLIDER
  23330. *
  23331. * ===============================================================*/
  23332. NK_LIB float
  23333. nk_slider_behavior(nk_flags* state, struct nk_rect* logical_cursor,
  23334. struct nk_rect* visual_cursor, struct nk_input* in,
  23335. struct nk_rect bounds, float slider_min, float slider_max, float slider_value,
  23336. float slider_step, float slider_steps)
  23337. {
  23338. int left_mouse_down;
  23339. int left_mouse_click_in_cursor;
  23340. /* check if visual cursor is being dragged */
  23341. nk_widget_state_reset(state);
  23342. left_mouse_down = in && in->mouse.buttons[NK_BUTTON_LEFT].down;
  23343. left_mouse_click_in_cursor = in && nk_input_has_mouse_click_down_in_rect(in,
  23344. NK_BUTTON_LEFT, *visual_cursor, nk_true);
  23345. if (left_mouse_down && left_mouse_click_in_cursor) {
  23346. float ratio = 0;
  23347. const float d = in->mouse.pos.x - (visual_cursor->x + visual_cursor->w * 0.5f);
  23348. const float pxstep = bounds.w / slider_steps;
  23349. /* only update value if the next slider step is reached */
  23350. *state = NK_WIDGET_STATE_ACTIVE;
  23351. if (NK_ABS(d) >= pxstep) {
  23352. const float steps = (float)((int)(NK_ABS(d) / pxstep));
  23353. slider_value += (d > 0) ? (slider_step * steps) : -(slider_step * steps);
  23354. slider_value = NK_CLAMP(slider_min, slider_value, slider_max);
  23355. ratio = (slider_value - slider_min) / slider_step;
  23356. logical_cursor->x = bounds.x + (logical_cursor->w * ratio);
  23357. in->mouse.buttons[NK_BUTTON_LEFT].clicked_pos.x = logical_cursor->x;
  23358. }
  23359. }
  23360. /* slider widget state */
  23361. if (nk_input_is_mouse_hovering_rect(in, bounds))
  23362. *state = NK_WIDGET_STATE_HOVERED;
  23363. if (*state & NK_WIDGET_STATE_HOVER &&
  23364. !nk_input_is_mouse_prev_hovering_rect(in, bounds))
  23365. *state |= NK_WIDGET_STATE_ENTERED;
  23366. else if (nk_input_is_mouse_prev_hovering_rect(in, bounds))
  23367. *state |= NK_WIDGET_STATE_LEFT;
  23368. return slider_value;
  23369. }
  23370. NK_LIB void
  23371. nk_draw_slider(struct nk_command_buffer* out, nk_flags state,
  23372. const struct nk_style_slider* style, const struct nk_rect* bounds,
  23373. const struct nk_rect* visual_cursor, float min, float value, float max)
  23374. {
  23375. struct nk_rect fill;
  23376. struct nk_rect bar;
  23377. const struct nk_style_item* background;
  23378. /* select correct slider images/colors */
  23379. struct nk_color bar_color;
  23380. const struct nk_style_item* cursor;
  23381. NK_UNUSED(min);
  23382. NK_UNUSED(max);
  23383. NK_UNUSED(value);
  23384. if (state & NK_WIDGET_STATE_ACTIVED) {
  23385. background = &style->active;
  23386. bar_color = style->bar_active;
  23387. cursor = &style->cursor_active;
  23388. }
  23389. else if (state & NK_WIDGET_STATE_HOVER) {
  23390. background = &style->hover;
  23391. bar_color = style->bar_hover;
  23392. cursor = &style->cursor_hover;
  23393. }
  23394. else {
  23395. background = &style->normal;
  23396. bar_color = style->bar_normal;
  23397. cursor = &style->cursor_normal;
  23398. }
  23399. /* calculate slider background bar */
  23400. bar.x = bounds->x;
  23401. bar.y = (visual_cursor->y + visual_cursor->h / 2) - bounds->h / 12;
  23402. bar.w = bounds->w;
  23403. bar.h = bounds->h / 6;
  23404. /* filled background bar style */
  23405. fill.w = (visual_cursor->x + (visual_cursor->w / 2.0f)) - bar.x;
  23406. fill.x = bar.x;
  23407. fill.y = bar.y;
  23408. fill.h = bar.h;
  23409. /* draw background */
  23410. switch (background->type) {
  23411. case NK_STYLE_ITEM_IMAGE:
  23412. nk_draw_image(out, *bounds, &background->data.image, nk_white);
  23413. break;
  23414. case NK_STYLE_ITEM_NINE_SLICE:
  23415. nk_draw_nine_slice(out, *bounds, &background->data.slice, nk_white);
  23416. break;
  23417. case NK_STYLE_ITEM_COLOR:
  23418. nk_fill_rect(out, *bounds, style->rounding, background->data.color);
  23419. nk_stroke_rect(out, *bounds, style->rounding, style->border, style->border_color);
  23420. break;
  23421. }
  23422. /* draw slider bar */
  23423. nk_fill_rect(out, bar, style->rounding, bar_color);
  23424. nk_fill_rect(out, fill, style->rounding, style->bar_filled);
  23425. /* draw cursor */
  23426. if (cursor->type == NK_STYLE_ITEM_IMAGE)
  23427. nk_draw_image(out, *visual_cursor, &cursor->data.image, nk_white);
  23428. else
  23429. nk_fill_circle(out, *visual_cursor, cursor->data.color);
  23430. }
  23431. NK_LIB float
  23432. nk_do_slider(nk_flags* state,
  23433. struct nk_command_buffer* out, struct nk_rect bounds,
  23434. float min, float val, float max, float step,
  23435. const struct nk_style_slider* style, struct nk_input* in,
  23436. const struct nk_user_font* font)
  23437. {
  23438. float slider_range;
  23439. float slider_min;
  23440. float slider_max;
  23441. float slider_value;
  23442. float slider_steps;
  23443. float cursor_offset;
  23444. struct nk_rect visual_cursor;
  23445. struct nk_rect logical_cursor;
  23446. NK_ASSERT(style);
  23447. NK_ASSERT(out);
  23448. if (!out || !style)
  23449. return 0;
  23450. /* remove padding from slider bounds */
  23451. bounds.x = bounds.x + style->padding.x;
  23452. bounds.y = bounds.y + style->padding.y;
  23453. bounds.h = NK_MAX(bounds.h, 2 * style->padding.y);
  23454. bounds.w = NK_MAX(bounds.w, 2 * style->padding.x + style->cursor_size.x);
  23455. bounds.w -= 2 * style->padding.x;
  23456. bounds.h -= 2 * style->padding.y;
  23457. /* optional buttons */
  23458. if (style->show_buttons) {
  23459. nk_flags ws;
  23460. struct nk_rect button;
  23461. button.y = bounds.y;
  23462. button.w = bounds.h;
  23463. button.h = bounds.h;
  23464. /* decrement button */
  23465. button.x = bounds.x;
  23466. if (nk_do_button_symbol(&ws, out, button, style->dec_symbol, NK_BUTTON_DEFAULT,
  23467. &style->dec_button, in, font))
  23468. val -= step;
  23469. /* increment button */
  23470. button.x = (bounds.x + bounds.w) - button.w;
  23471. if (nk_do_button_symbol(&ws, out, button, style->inc_symbol, NK_BUTTON_DEFAULT,
  23472. &style->inc_button, in, font))
  23473. val += step;
  23474. bounds.x = bounds.x + button.w + style->spacing.x;
  23475. bounds.w = bounds.w - (2 * button.w + 2 * style->spacing.x);
  23476. }
  23477. /* remove one cursor size to support visual cursor */
  23478. bounds.x += style->cursor_size.x * 0.5f;
  23479. bounds.w -= style->cursor_size.x;
  23480. /* make sure the provided values are correct */
  23481. slider_max = NK_MAX(min, max);
  23482. slider_min = NK_MIN(min, max);
  23483. slider_value = NK_CLAMP(slider_min, val, slider_max);
  23484. slider_range = slider_max - slider_min;
  23485. slider_steps = slider_range / step;
  23486. cursor_offset = (slider_value - slider_min) / step;
  23487. /* calculate cursor
  23488. Basically you have two cursors. One for visual representation and interaction
  23489. and one for updating the actual cursor value. */
  23490. logical_cursor.h = bounds.h;
  23491. logical_cursor.w = bounds.w / slider_steps;
  23492. logical_cursor.x = bounds.x + (logical_cursor.w * cursor_offset);
  23493. logical_cursor.y = bounds.y;
  23494. visual_cursor.h = style->cursor_size.y;
  23495. visual_cursor.w = style->cursor_size.x;
  23496. visual_cursor.y = (bounds.y + bounds.h * 0.5f) - visual_cursor.h * 0.5f;
  23497. visual_cursor.x = logical_cursor.x - visual_cursor.w * 0.5f;
  23498. slider_value = nk_slider_behavior(state, &logical_cursor, &visual_cursor,
  23499. in, bounds, slider_min, slider_max, slider_value, step, slider_steps);
  23500. visual_cursor.x = logical_cursor.x - visual_cursor.w * 0.5f;
  23501. /* draw slider */
  23502. if (style->draw_begin) style->draw_begin(out, style->userdata);
  23503. nk_draw_slider(out, *state, style, &bounds, &visual_cursor, slider_min, slider_value, slider_max);
  23504. if (style->draw_end) style->draw_end(out, style->userdata);
  23505. return slider_value;
  23506. }
  23507. NK_API nk_bool
  23508. nk_slider_float(struct nk_context* ctx, float min_value, float* value, float max_value,
  23509. float value_step)
  23510. {
  23511. struct nk_window* win;
  23512. struct nk_panel* layout;
  23513. struct nk_input* in;
  23514. const struct nk_style* style;
  23515. int ret = 0;
  23516. float old_value;
  23517. struct nk_rect bounds;
  23518. enum nk_widget_layout_states state;
  23519. NK_ASSERT(ctx);
  23520. NK_ASSERT(ctx->current);
  23521. NK_ASSERT(ctx->current->layout);
  23522. NK_ASSERT(value);
  23523. if (!ctx || !ctx->current || !ctx->current->layout || !value)
  23524. return ret;
  23525. win = ctx->current;
  23526. style = &ctx->style;
  23527. layout = win->layout;
  23528. state = nk_widget(&bounds, ctx);
  23529. if (!state) return ret;
  23530. in = (/*state == NK_WIDGET_ROM || */ layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  23531. old_value = *value;
  23532. *value = nk_do_slider(&ctx->last_widget_state, &win->buffer, bounds, min_value,
  23533. old_value, max_value, value_step, &style->slider, in, style->font);
  23534. return (old_value > *value || old_value < *value);
  23535. }
  23536. NK_API float
  23537. nk_slide_float(struct nk_context* ctx, float min, float val, float max, float step)
  23538. {
  23539. nk_slider_float(ctx, min, &val, max, step); return val;
  23540. }
  23541. NK_API int
  23542. nk_slide_int(struct nk_context* ctx, int min, int val, int max, int step)
  23543. {
  23544. float value = (float)val;
  23545. nk_slider_float(ctx, (float)min, &value, (float)max, (float)step);
  23546. return (int)value;
  23547. }
  23548. NK_API nk_bool
  23549. nk_slider_int(struct nk_context* ctx, int min, int* val, int max, int step)
  23550. {
  23551. int ret;
  23552. float value = (float)*val;
  23553. ret = nk_slider_float(ctx, (float)min, &value, (float)max, (float)step);
  23554. *val = (int)value;
  23555. return ret;
  23556. }
  23557. /* ===============================================================
  23558. *
  23559. * PROGRESS
  23560. *
  23561. * ===============================================================*/
  23562. NK_LIB nk_size
  23563. nk_progress_behavior(nk_flags* state, struct nk_input* in,
  23564. struct nk_rect r, struct nk_rect cursor, nk_size max, nk_size value, nk_bool modifiable)
  23565. {
  23566. int left_mouse_down = 0;
  23567. int left_mouse_click_in_cursor = 0;
  23568. nk_widget_state_reset(state);
  23569. if (!in || !modifiable) return value;
  23570. left_mouse_down = in && in->mouse.buttons[NK_BUTTON_LEFT].down;
  23571. left_mouse_click_in_cursor = in && nk_input_has_mouse_click_down_in_rect(in,
  23572. NK_BUTTON_LEFT, cursor, nk_true);
  23573. if (nk_input_is_mouse_hovering_rect(in, r))
  23574. *state = NK_WIDGET_STATE_HOVERED;
  23575. if (in && left_mouse_down && left_mouse_click_in_cursor) {
  23576. if (left_mouse_down && left_mouse_click_in_cursor) {
  23577. float ratio = NK_MAX(0, (float)(in->mouse.pos.x - cursor.x)) / (float)cursor.w;
  23578. value = (nk_size)NK_CLAMP(0, (float)max * ratio, (float)max);
  23579. in->mouse.buttons[NK_BUTTON_LEFT].clicked_pos.x = cursor.x + cursor.w / 2.0f;
  23580. *state |= NK_WIDGET_STATE_ACTIVE;
  23581. }
  23582. }
  23583. /* set progressbar widget state */
  23584. if (*state & NK_WIDGET_STATE_HOVER && !nk_input_is_mouse_prev_hovering_rect(in, r))
  23585. *state |= NK_WIDGET_STATE_ENTERED;
  23586. else if (nk_input_is_mouse_prev_hovering_rect(in, r))
  23587. *state |= NK_WIDGET_STATE_LEFT;
  23588. return value;
  23589. }
  23590. NK_LIB void
  23591. nk_draw_progress(struct nk_command_buffer* out, nk_flags state,
  23592. const struct nk_style_progress* style, const struct nk_rect* bounds,
  23593. const struct nk_rect* scursor, nk_size value, nk_size max)
  23594. {
  23595. const struct nk_style_item* background;
  23596. const struct nk_style_item* cursor;
  23597. NK_UNUSED(max);
  23598. NK_UNUSED(value);
  23599. /* select correct colors/images to draw */
  23600. if (state & NK_WIDGET_STATE_ACTIVED) {
  23601. background = &style->active;
  23602. cursor = &style->cursor_active;
  23603. }
  23604. else if (state & NK_WIDGET_STATE_HOVER) {
  23605. background = &style->hover;
  23606. cursor = &style->cursor_hover;
  23607. }
  23608. else {
  23609. background = &style->normal;
  23610. cursor = &style->cursor_normal;
  23611. }
  23612. /* draw background */
  23613. switch (background->type) {
  23614. case NK_STYLE_ITEM_IMAGE:
  23615. nk_draw_image(out, *bounds, &background->data.image, nk_white);
  23616. break;
  23617. case NK_STYLE_ITEM_NINE_SLICE:
  23618. nk_draw_nine_slice(out, *bounds, &background->data.slice, nk_white);
  23619. break;
  23620. case NK_STYLE_ITEM_COLOR:
  23621. nk_fill_rect(out, *bounds, style->rounding, background->data.color);
  23622. nk_stroke_rect(out, *bounds, style->rounding, style->border, style->border_color);
  23623. break;
  23624. }
  23625. /* draw cursor */
  23626. switch (cursor->type) {
  23627. case NK_STYLE_ITEM_IMAGE:
  23628. nk_draw_image(out, *scursor, &cursor->data.image, nk_white);
  23629. break;
  23630. case NK_STYLE_ITEM_NINE_SLICE:
  23631. nk_draw_nine_slice(out, *scursor, &cursor->data.slice, nk_white);
  23632. break;
  23633. case NK_STYLE_ITEM_COLOR:
  23634. nk_fill_rect(out, *scursor, style->rounding, cursor->data.color);
  23635. nk_stroke_rect(out, *scursor, style->rounding, style->border, style->border_color);
  23636. break;
  23637. }
  23638. }
  23639. NK_LIB nk_size
  23640. nk_do_progress(nk_flags* state,
  23641. struct nk_command_buffer* out, struct nk_rect bounds,
  23642. nk_size value, nk_size max, nk_bool modifiable,
  23643. const struct nk_style_progress* style, struct nk_input* in)
  23644. {
  23645. float prog_scale;
  23646. nk_size prog_value;
  23647. struct nk_rect cursor;
  23648. NK_ASSERT(style);
  23649. NK_ASSERT(out);
  23650. if (!out || !style) return 0;
  23651. /* calculate progressbar cursor */
  23652. cursor.w = NK_MAX(bounds.w, 2 * style->padding.x + 2 * style->border);
  23653. cursor.h = NK_MAX(bounds.h, 2 * style->padding.y + 2 * style->border);
  23654. cursor = nk_pad_rect(bounds, nk_vec2(style->padding.x + style->border, style->padding.y + style->border));
  23655. prog_scale = (float)value / (float)max;
  23656. /* update progressbar */
  23657. prog_value = NK_MIN(value, max);
  23658. prog_value = nk_progress_behavior(state, in, bounds, cursor, max, prog_value, modifiable);
  23659. cursor.w = cursor.w * prog_scale;
  23660. /* draw progressbar */
  23661. if (style->draw_begin) style->draw_begin(out, style->userdata);
  23662. nk_draw_progress(out, *state, style, &bounds, &cursor, value, max);
  23663. if (style->draw_end) style->draw_end(out, style->userdata);
  23664. return prog_value;
  23665. }
  23666. NK_API nk_bool
  23667. nk_progress(struct nk_context* ctx, nk_size* cur, nk_size max, nk_bool is_modifyable)
  23668. {
  23669. struct nk_window* win;
  23670. struct nk_panel* layout;
  23671. const struct nk_style* style;
  23672. struct nk_input* in;
  23673. struct nk_rect bounds;
  23674. enum nk_widget_layout_states state;
  23675. nk_size old_value;
  23676. NK_ASSERT(ctx);
  23677. NK_ASSERT(cur);
  23678. NK_ASSERT(ctx->current);
  23679. NK_ASSERT(ctx->current->layout);
  23680. if (!ctx || !ctx->current || !ctx->current->layout || !cur)
  23681. return 0;
  23682. win = ctx->current;
  23683. style = &ctx->style;
  23684. layout = win->layout;
  23685. state = nk_widget(&bounds, ctx);
  23686. if (!state) return 0;
  23687. in = (state == NK_WIDGET_ROM || layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  23688. old_value = *cur;
  23689. *cur = nk_do_progress(&ctx->last_widget_state, &win->buffer, bounds,
  23690. *cur, max, is_modifyable, &style->progress, in);
  23691. return (*cur != old_value);
  23692. }
  23693. NK_API nk_size
  23694. nk_prog(struct nk_context* ctx, nk_size cur, nk_size max, nk_bool modifyable)
  23695. {
  23696. nk_progress(ctx, &cur, max, modifyable);
  23697. return cur;
  23698. }
  23699. /* ===============================================================
  23700. *
  23701. * SCROLLBAR
  23702. *
  23703. * ===============================================================*/
  23704. NK_LIB float
  23705. nk_scrollbar_behavior(nk_flags* state, struct nk_input* in,
  23706. int has_scrolling, const struct nk_rect* scroll,
  23707. const struct nk_rect* cursor, const struct nk_rect* empty0,
  23708. const struct nk_rect* empty1, float scroll_offset,
  23709. float target, float scroll_step, enum nk_orientation o)
  23710. {
  23711. nk_flags ws = 0;
  23712. int left_mouse_down;
  23713. unsigned int left_mouse_clicked;
  23714. int left_mouse_click_in_cursor;
  23715. float scroll_delta;
  23716. nk_widget_state_reset(state);
  23717. if (!in) return scroll_offset;
  23718. left_mouse_down = in->mouse.buttons[NK_BUTTON_LEFT].down;
  23719. left_mouse_clicked = in->mouse.buttons[NK_BUTTON_LEFT].clicked;
  23720. left_mouse_click_in_cursor = nk_input_has_mouse_click_down_in_rect(in,
  23721. NK_BUTTON_LEFT, *cursor, nk_true);
  23722. if (nk_input_is_mouse_hovering_rect(in, *scroll))
  23723. *state = NK_WIDGET_STATE_HOVERED;
  23724. scroll_delta = (o == NK_VERTICAL) ? in->mouse.scroll_delta.y : in->mouse.scroll_delta.x;
  23725. if (left_mouse_down && left_mouse_click_in_cursor && !left_mouse_clicked) {
  23726. /* update cursor by mouse dragging */
  23727. float pixel, delta;
  23728. *state = NK_WIDGET_STATE_ACTIVE;
  23729. if (o == NK_VERTICAL) {
  23730. float cursor_y;
  23731. pixel = in->mouse.delta.y;
  23732. delta = (pixel / scroll->h) * target;
  23733. scroll_offset = NK_CLAMP(0, scroll_offset + delta, target - scroll->h);
  23734. cursor_y = scroll->y + ((scroll_offset / target) * scroll->h);
  23735. in->mouse.buttons[NK_BUTTON_LEFT].clicked_pos.y = cursor_y + cursor->h / 2.0f;
  23736. }
  23737. else {
  23738. float cursor_x;
  23739. pixel = in->mouse.delta.x;
  23740. delta = (pixel / scroll->w) * target;
  23741. scroll_offset = NK_CLAMP(0, scroll_offset + delta, target - scroll->w);
  23742. cursor_x = scroll->x + ((scroll_offset / target) * scroll->w);
  23743. in->mouse.buttons[NK_BUTTON_LEFT].clicked_pos.x = cursor_x + cursor->w / 2.0f;
  23744. }
  23745. }
  23746. else if ((nk_input_is_key_pressed(in, NK_KEY_SCROLL_UP) && o == NK_VERTICAL && has_scrolling) ||
  23747. nk_button_behavior(&ws, *empty0, in, NK_BUTTON_DEFAULT)) {
  23748. /* scroll page up by click on empty space or shortcut */
  23749. if (o == NK_VERTICAL)
  23750. scroll_offset = NK_MAX(0, scroll_offset - scroll->h);
  23751. else scroll_offset = NK_MAX(0, scroll_offset - scroll->w);
  23752. }
  23753. else if ((nk_input_is_key_pressed(in, NK_KEY_SCROLL_DOWN) && o == NK_VERTICAL && has_scrolling) ||
  23754. nk_button_behavior(&ws, *empty1, in, NK_BUTTON_DEFAULT)) {
  23755. /* scroll page down by click on empty space or shortcut */
  23756. if (o == NK_VERTICAL)
  23757. scroll_offset = NK_MIN(scroll_offset + scroll->h, target - scroll->h);
  23758. else scroll_offset = NK_MIN(scroll_offset + scroll->w, target - scroll->w);
  23759. }
  23760. else if (has_scrolling) {
  23761. if ((scroll_delta < 0 || (scroll_delta > 0))) {
  23762. /* update cursor by mouse scrolling */
  23763. scroll_offset = scroll_offset + scroll_step * (-scroll_delta);
  23764. if (o == NK_VERTICAL)
  23765. scroll_offset = NK_CLAMP(0, scroll_offset, target - scroll->h);
  23766. else scroll_offset = NK_CLAMP(0, scroll_offset, target - scroll->w);
  23767. }
  23768. else if (nk_input_is_key_pressed(in, NK_KEY_SCROLL_START)) {
  23769. /* update cursor to the beginning */
  23770. if (o == NK_VERTICAL) scroll_offset = 0;
  23771. }
  23772. else if (nk_input_is_key_pressed(in, NK_KEY_SCROLL_END)) {
  23773. /* update cursor to the end */
  23774. if (o == NK_VERTICAL) scroll_offset = target - scroll->h;
  23775. }
  23776. }
  23777. if (*state & NK_WIDGET_STATE_HOVER && !nk_input_is_mouse_prev_hovering_rect(in, *scroll))
  23778. *state |= NK_WIDGET_STATE_ENTERED;
  23779. else if (nk_input_is_mouse_prev_hovering_rect(in, *scroll))
  23780. *state |= NK_WIDGET_STATE_LEFT;
  23781. return scroll_offset;
  23782. }
  23783. NK_LIB void
  23784. nk_draw_scrollbar(struct nk_command_buffer* out, nk_flags state,
  23785. const struct nk_style_scrollbar* style, const struct nk_rect* bounds,
  23786. const struct nk_rect* scroll)
  23787. {
  23788. const struct nk_style_item* background;
  23789. const struct nk_style_item* cursor;
  23790. /* select correct colors/images to draw */
  23791. if (state & NK_WIDGET_STATE_ACTIVED) {
  23792. background = &style->active;
  23793. cursor = &style->cursor_active;
  23794. }
  23795. else if (state & NK_WIDGET_STATE_HOVER) {
  23796. background = &style->hover;
  23797. cursor = &style->cursor_hover;
  23798. }
  23799. else {
  23800. background = &style->normal;
  23801. cursor = &style->cursor_normal;
  23802. }
  23803. /* draw background */
  23804. switch (background->type) {
  23805. case NK_STYLE_ITEM_IMAGE:
  23806. nk_draw_image(out, *bounds, &background->data.image, nk_white);
  23807. break;
  23808. case NK_STYLE_ITEM_NINE_SLICE:
  23809. nk_draw_nine_slice(out, *bounds, &background->data.slice, nk_white);
  23810. break;
  23811. case NK_STYLE_ITEM_COLOR:
  23812. nk_fill_rect(out, *bounds, style->rounding, background->data.color);
  23813. nk_stroke_rect(out, *bounds, style->rounding, style->border, style->border_color);
  23814. break;
  23815. }
  23816. /* draw cursor */
  23817. switch (cursor->type) {
  23818. case NK_STYLE_ITEM_IMAGE:
  23819. nk_draw_image(out, *scroll, &cursor->data.image, nk_white);
  23820. break;
  23821. case NK_STYLE_ITEM_NINE_SLICE:
  23822. nk_draw_nine_slice(out, *scroll, &cursor->data.slice, nk_white);
  23823. break;
  23824. case NK_STYLE_ITEM_COLOR:
  23825. nk_fill_rect(out, *scroll, style->rounding_cursor, cursor->data.color);
  23826. nk_stroke_rect(out, *scroll, style->rounding_cursor, style->border_cursor, style->cursor_border_color);
  23827. break;
  23828. }
  23829. }
  23830. NK_LIB float
  23831. nk_do_scrollbarv(nk_flags* state,
  23832. struct nk_command_buffer* out, struct nk_rect scroll, int has_scrolling,
  23833. float offset, float target, float step, float button_pixel_inc,
  23834. const struct nk_style_scrollbar* style, struct nk_input* in,
  23835. const struct nk_user_font* font)
  23836. {
  23837. struct nk_rect empty_north;
  23838. struct nk_rect empty_south;
  23839. struct nk_rect cursor;
  23840. float scroll_step;
  23841. float scroll_offset;
  23842. float scroll_off;
  23843. float scroll_ratio;
  23844. NK_ASSERT(out);
  23845. NK_ASSERT(style);
  23846. NK_ASSERT(state);
  23847. if (!out || !style) return 0;
  23848. scroll.w = NK_MAX(scroll.w, 1);
  23849. scroll.h = NK_MAX(scroll.h, 0);
  23850. if (target <= scroll.h) return 0;
  23851. /* optional scrollbar buttons */
  23852. if (style->show_buttons) {
  23853. nk_flags ws;
  23854. float scroll_h;
  23855. struct nk_rect button;
  23856. button.x = scroll.x;
  23857. button.w = scroll.w;
  23858. button.h = scroll.w;
  23859. scroll_h = NK_MAX(scroll.h - 2 * button.h, 0);
  23860. scroll_step = NK_MIN(step, button_pixel_inc);
  23861. /* decrement button */
  23862. button.y = scroll.y;
  23863. if (nk_do_button_symbol(&ws, out, button, style->dec_symbol,
  23864. NK_BUTTON_REPEATER, &style->dec_button, in, font))
  23865. offset = offset - scroll_step;
  23866. /* increment button */
  23867. button.y = scroll.y + scroll.h - button.h;
  23868. if (nk_do_button_symbol(&ws, out, button, style->inc_symbol,
  23869. NK_BUTTON_REPEATER, &style->inc_button, in, font))
  23870. offset = offset + scroll_step;
  23871. scroll.y = scroll.y + button.h;
  23872. scroll.h = scroll_h;
  23873. }
  23874. /* calculate scrollbar constants */
  23875. scroll_step = NK_MIN(step, scroll.h);
  23876. scroll_offset = NK_CLAMP(0, offset, target - scroll.h);
  23877. scroll_ratio = scroll.h / target;
  23878. scroll_off = scroll_offset / target;
  23879. /* calculate scrollbar cursor bounds */
  23880. cursor.h = NK_MAX((scroll_ratio * scroll.h) - (2 * style->border + 2 * style->padding.y), 0);
  23881. cursor.y = scroll.y + (scroll_off * scroll.h) + style->border + style->padding.y;
  23882. cursor.w = scroll.w - (2 * style->border + 2 * style->padding.x);
  23883. cursor.x = scroll.x + style->border + style->padding.x;
  23884. /* calculate empty space around cursor */
  23885. empty_north.x = scroll.x;
  23886. empty_north.y = scroll.y;
  23887. empty_north.w = scroll.w;
  23888. empty_north.h = NK_MAX(cursor.y - scroll.y, 0);
  23889. empty_south.x = scroll.x;
  23890. empty_south.y = cursor.y + cursor.h;
  23891. empty_south.w = scroll.w;
  23892. empty_south.h = NK_MAX((scroll.y + scroll.h) - (cursor.y + cursor.h), 0);
  23893. /* update scrollbar */
  23894. scroll_offset = nk_scrollbar_behavior(state, in, has_scrolling, &scroll, &cursor,
  23895. &empty_north, &empty_south, scroll_offset, target, scroll_step, NK_VERTICAL);
  23896. scroll_off = scroll_offset / target;
  23897. cursor.y = scroll.y + (scroll_off * scroll.h) + style->border_cursor + style->padding.y;
  23898. /* draw scrollbar */
  23899. if (style->draw_begin) style->draw_begin(out, style->userdata);
  23900. nk_draw_scrollbar(out, *state, style, &scroll, &cursor);
  23901. if (style->draw_end) style->draw_end(out, style->userdata);
  23902. return scroll_offset;
  23903. }
  23904. NK_LIB float
  23905. nk_do_scrollbarh(nk_flags* state,
  23906. struct nk_command_buffer* out, struct nk_rect scroll, int has_scrolling,
  23907. float offset, float target, float step, float button_pixel_inc,
  23908. const struct nk_style_scrollbar* style, struct nk_input* in,
  23909. const struct nk_user_font* font)
  23910. {
  23911. struct nk_rect cursor;
  23912. struct nk_rect empty_west;
  23913. struct nk_rect empty_east;
  23914. float scroll_step;
  23915. float scroll_offset;
  23916. float scroll_off;
  23917. float scroll_ratio;
  23918. NK_ASSERT(out);
  23919. NK_ASSERT(style);
  23920. if (!out || !style) return 0;
  23921. /* scrollbar background */
  23922. scroll.h = NK_MAX(scroll.h, 1);
  23923. scroll.w = NK_MAX(scroll.w, 2 * scroll.h);
  23924. if (target <= scroll.w) return 0;
  23925. /* optional scrollbar buttons */
  23926. if (style->show_buttons) {
  23927. nk_flags ws;
  23928. float scroll_w;
  23929. struct nk_rect button;
  23930. button.y = scroll.y;
  23931. button.w = scroll.h;
  23932. button.h = scroll.h;
  23933. scroll_w = scroll.w - 2 * button.w;
  23934. scroll_step = NK_MIN(step, button_pixel_inc);
  23935. /* decrement button */
  23936. button.x = scroll.x;
  23937. if (nk_do_button_symbol(&ws, out, button, style->dec_symbol,
  23938. NK_BUTTON_REPEATER, &style->dec_button, in, font))
  23939. offset = offset - scroll_step;
  23940. /* increment button */
  23941. button.x = scroll.x + scroll.w - button.w;
  23942. if (nk_do_button_symbol(&ws, out, button, style->inc_symbol,
  23943. NK_BUTTON_REPEATER, &style->inc_button, in, font))
  23944. offset = offset + scroll_step;
  23945. scroll.x = scroll.x + button.w;
  23946. scroll.w = scroll_w;
  23947. }
  23948. /* calculate scrollbar constants */
  23949. scroll_step = NK_MIN(step, scroll.w);
  23950. scroll_offset = NK_CLAMP(0, offset, target - scroll.w);
  23951. scroll_ratio = scroll.w / target;
  23952. scroll_off = scroll_offset / target;
  23953. /* calculate cursor bounds */
  23954. cursor.w = (scroll_ratio * scroll.w) - (2 * style->border + 2 * style->padding.x);
  23955. cursor.x = scroll.x + (scroll_off * scroll.w) + style->border + style->padding.x;
  23956. cursor.h = scroll.h - (2 * style->border + 2 * style->padding.y);
  23957. cursor.y = scroll.y + style->border + style->padding.y;
  23958. /* calculate empty space around cursor */
  23959. empty_west.x = scroll.x;
  23960. empty_west.y = scroll.y;
  23961. empty_west.w = cursor.x - scroll.x;
  23962. empty_west.h = scroll.h;
  23963. empty_east.x = cursor.x + cursor.w;
  23964. empty_east.y = scroll.y;
  23965. empty_east.w = (scroll.x + scroll.w) - (cursor.x + cursor.w);
  23966. empty_east.h = scroll.h;
  23967. /* update scrollbar */
  23968. scroll_offset = nk_scrollbar_behavior(state, in, has_scrolling, &scroll, &cursor,
  23969. &empty_west, &empty_east, scroll_offset, target, scroll_step, NK_HORIZONTAL);
  23970. scroll_off = scroll_offset / target;
  23971. cursor.x = scroll.x + (scroll_off * scroll.w);
  23972. /* draw scrollbar */
  23973. if (style->draw_begin) style->draw_begin(out, style->userdata);
  23974. nk_draw_scrollbar(out, *state, style, &scroll, &cursor);
  23975. if (style->draw_end) style->draw_end(out, style->userdata);
  23976. return scroll_offset;
  23977. }
  23978. /* ===============================================================
  23979. *
  23980. * TEXT EDITOR
  23981. *
  23982. * ===============================================================*/
  23983. /* stb_textedit.h - v1.8 - public domain - Sean Barrett */
  23984. struct nk_text_find {
  23985. float x, y; /* position of n'th character */
  23986. float height; /* height of line */
  23987. int first_char, length; /* first char of row, and length */
  23988. int prev_first; /*_ first char of previous row */
  23989. };
  23990. struct nk_text_edit_row {
  23991. float x0, x1;
  23992. /* starting x location, end x location (allows for align=right, etc) */
  23993. float baseline_y_delta;
  23994. /* position of baseline relative to previous row's baseline*/
  23995. float ymin, ymax;
  23996. /* height of row above and below baseline */
  23997. int num_chars;
  23998. };
  23999. /* forward declarations */
  24000. NK_INTERN void nk_textedit_makeundo_delete(struct nk_text_edit*, int, int);
  24001. NK_INTERN void nk_textedit_makeundo_insert(struct nk_text_edit*, int, int);
  24002. NK_INTERN void nk_textedit_makeundo_replace(struct nk_text_edit*, int, int, int);
  24003. #define NK_TEXT_HAS_SELECTION(s) ((s)->select_start != (s)->select_end)
  24004. NK_INTERN float
  24005. nk_textedit_get_width(const struct nk_text_edit* edit, int line_start, int char_id,
  24006. const struct nk_user_font* font)
  24007. {
  24008. int len = 0;
  24009. nk_rune unicode = 0;
  24010. const char* str = nk_str_at_const(&edit->string, line_start + char_id, &unicode, &len);
  24011. return font->width(font->userdata, font->height, str, len);
  24012. }
  24013. NK_INTERN void
  24014. nk_textedit_layout_row(struct nk_text_edit_row* r, struct nk_text_edit* edit,
  24015. int line_start_id, float row_height, const struct nk_user_font* font)
  24016. {
  24017. int l;
  24018. int glyphs = 0;
  24019. nk_rune unicode;
  24020. const char* remaining;
  24021. int len = nk_str_len_char(&edit->string);
  24022. const char* end = nk_str_get_const(&edit->string) + len;
  24023. const char* text = nk_str_at_const(&edit->string, line_start_id, &unicode, &l);
  24024. const struct nk_vec2 size = nk_text_calculate_text_bounds(font,
  24025. text, (int)(end - text), row_height, &remaining, 0, &glyphs, NK_STOP_ON_NEW_LINE);
  24026. r->x0 = 0.0f;
  24027. r->x1 = size.x;
  24028. r->baseline_y_delta = size.y;
  24029. r->ymin = 0.0f;
  24030. r->ymax = size.y;
  24031. r->num_chars = glyphs;
  24032. }
  24033. NK_INTERN int
  24034. nk_textedit_locate_coord(struct nk_text_edit* edit, float x, float y,
  24035. const struct nk_user_font* font, float row_height)
  24036. {
  24037. struct nk_text_edit_row r;
  24038. int n = edit->string.len;
  24039. float base_y = 0, prev_x;
  24040. int i = 0, k;
  24041. r.x0 = r.x1 = 0;
  24042. r.ymin = r.ymax = 0;
  24043. r.num_chars = 0;
  24044. /* search rows to find one that straddles 'y' */
  24045. while (i < n) {
  24046. nk_textedit_layout_row(&r, edit, i, row_height, font);
  24047. if (r.num_chars <= 0)
  24048. return n;
  24049. if (i == 0 && y < base_y + r.ymin)
  24050. return 0;
  24051. if (y < base_y + r.ymax)
  24052. break;
  24053. i += r.num_chars;
  24054. base_y += r.baseline_y_delta;
  24055. }
  24056. /* below all text, return 'after' last character */
  24057. if (i >= n)
  24058. return n;
  24059. /* check if it's before the beginning of the line */
  24060. if (x < r.x0)
  24061. return i;
  24062. /* check if it's before the end of the line */
  24063. if (x < r.x1) {
  24064. /* search characters in row for one that straddles 'x' */
  24065. k = i;
  24066. prev_x = r.x0;
  24067. for (i = 0; i < r.num_chars; ++i) {
  24068. float w = nk_textedit_get_width(edit, k, i, font);
  24069. if (x < prev_x + w) {
  24070. if (x < prev_x + w / 2)
  24071. return k + i;
  24072. else return k + i + 1;
  24073. }
  24074. prev_x += w;
  24075. }
  24076. /* shouldn't happen, but if it does, fall through to end-of-line case */
  24077. }
  24078. /* if the last character is a newline, return that.
  24079. * otherwise return 'after' the last character */
  24080. if (nk_str_rune_at(&edit->string, i + r.num_chars - 1) == '\n')
  24081. return i + r.num_chars - 1;
  24082. else return i + r.num_chars;
  24083. }
  24084. NK_LIB void
  24085. nk_textedit_click(struct nk_text_edit* state, float x, float y,
  24086. const struct nk_user_font* font, float row_height)
  24087. {
  24088. /* API click: on mouse down, move the cursor to the clicked location,
  24089. * and reset the selection */
  24090. state->cursor = nk_textedit_locate_coord(state, x, y, font, row_height);
  24091. state->select_start = state->cursor;
  24092. state->select_end = state->cursor;
  24093. state->has_preferred_x = 0;
  24094. }
  24095. NK_LIB void
  24096. nk_textedit_drag(struct nk_text_edit* state, float x, float y,
  24097. const struct nk_user_font* font, float row_height)
  24098. {
  24099. /* API drag: on mouse drag, move the cursor and selection endpoint
  24100. * to the clicked location */
  24101. int p = nk_textedit_locate_coord(state, x, y, font, row_height);
  24102. if (state->select_start == state->select_end)
  24103. state->select_start = state->cursor;
  24104. state->cursor = state->select_end = p;
  24105. }
  24106. NK_INTERN void
  24107. nk_textedit_find_charpos(struct nk_text_find* find, struct nk_text_edit* state,
  24108. int n, int single_line, const struct nk_user_font* font, float row_height)
  24109. {
  24110. /* find the x/y location of a character, and remember info about the previous
  24111. * row in case we get a move-up event (for page up, we'll have to rescan) */
  24112. struct nk_text_edit_row r;
  24113. int prev_start = 0;
  24114. int z = state->string.len;
  24115. int i = 0, first;
  24116. nk_zero_struct(r);
  24117. if (n == z) {
  24118. /* if it's at the end, then find the last line -- simpler than trying to
  24119. explicitly handle this case in the regular code */
  24120. nk_textedit_layout_row(&r, state, 0, row_height, font);
  24121. if (single_line) {
  24122. find->first_char = 0;
  24123. find->length = z;
  24124. }
  24125. else {
  24126. while (i < z) {
  24127. prev_start = i;
  24128. i += r.num_chars;
  24129. nk_textedit_layout_row(&r, state, i, row_height, font);
  24130. }
  24131. find->first_char = i;
  24132. find->length = r.num_chars;
  24133. }
  24134. find->x = r.x1;
  24135. find->y = r.ymin;
  24136. find->height = r.ymax - r.ymin;
  24137. find->prev_first = prev_start;
  24138. return;
  24139. }
  24140. /* search rows to find the one that straddles character n */
  24141. find->y = 0;
  24142. for (;;) {
  24143. nk_textedit_layout_row(&r, state, i, row_height, font);
  24144. if (n < i + r.num_chars) break;
  24145. prev_start = i;
  24146. i += r.num_chars;
  24147. find->y += r.baseline_y_delta;
  24148. }
  24149. find->first_char = first = i;
  24150. find->length = r.num_chars;
  24151. find->height = r.ymax - r.ymin;
  24152. find->prev_first = prev_start;
  24153. /* now scan to find xpos */
  24154. find->x = r.x0;
  24155. for (i = 0; first + i < n; ++i)
  24156. find->x += nk_textedit_get_width(state, first, i, font);
  24157. }
  24158. NK_INTERN void
  24159. nk_textedit_clamp(struct nk_text_edit* state)
  24160. {
  24161. /* make the selection/cursor state valid if client altered the string */
  24162. int n = state->string.len;
  24163. if (NK_TEXT_HAS_SELECTION(state)) {
  24164. if (state->select_start > n) state->select_start = n;
  24165. if (state->select_end > n) state->select_end = n;
  24166. /* if clamping forced them to be equal, move the cursor to match */
  24167. if (state->select_start == state->select_end)
  24168. state->cursor = state->select_start;
  24169. }
  24170. if (state->cursor > n) state->cursor = n;
  24171. }
  24172. NK_API void
  24173. nk_textedit_delete(struct nk_text_edit* state, int where, int len)
  24174. {
  24175. /* delete characters while updating undo */
  24176. nk_textedit_makeundo_delete(state, where, len);
  24177. nk_str_delete_runes(&state->string, where, len);
  24178. state->has_preferred_x = 0;
  24179. }
  24180. NK_API void
  24181. nk_textedit_delete_selection(struct nk_text_edit* state)
  24182. {
  24183. /* delete the section */
  24184. nk_textedit_clamp(state);
  24185. if (NK_TEXT_HAS_SELECTION(state)) {
  24186. if (state->select_start < state->select_end) {
  24187. nk_textedit_delete(state, state->select_start,
  24188. state->select_end - state->select_start);
  24189. state->select_end = state->cursor = state->select_start;
  24190. }
  24191. else {
  24192. nk_textedit_delete(state, state->select_end,
  24193. state->select_start - state->select_end);
  24194. state->select_start = state->cursor = state->select_end;
  24195. }
  24196. state->has_preferred_x = 0;
  24197. }
  24198. }
  24199. NK_INTERN void
  24200. nk_textedit_sortselection(struct nk_text_edit* state)
  24201. {
  24202. /* canonicalize the selection so start <= end */
  24203. if (state->select_end < state->select_start) {
  24204. int temp = state->select_end;
  24205. state->select_end = state->select_start;
  24206. state->select_start = temp;
  24207. }
  24208. }
  24209. NK_INTERN void
  24210. nk_textedit_move_to_first(struct nk_text_edit* state)
  24211. {
  24212. /* move cursor to first character of selection */
  24213. if (NK_TEXT_HAS_SELECTION(state)) {
  24214. nk_textedit_sortselection(state);
  24215. state->cursor = state->select_start;
  24216. state->select_end = state->select_start;
  24217. state->has_preferred_x = 0;
  24218. }
  24219. }
  24220. NK_INTERN void
  24221. nk_textedit_move_to_last(struct nk_text_edit* state)
  24222. {
  24223. /* move cursor to last character of selection */
  24224. if (NK_TEXT_HAS_SELECTION(state)) {
  24225. nk_textedit_sortselection(state);
  24226. nk_textedit_clamp(state);
  24227. state->cursor = state->select_end;
  24228. state->select_start = state->select_end;
  24229. state->has_preferred_x = 0;
  24230. }
  24231. }
  24232. NK_INTERN int
  24233. nk_is_word_boundary(struct nk_text_edit* state, int idx)
  24234. {
  24235. int len;
  24236. nk_rune c;
  24237. if (idx <= 0) return 1;
  24238. if (!nk_str_at_rune(&state->string, idx, &c, &len)) return 1;
  24239. return (c == ' ' || c == '\t' || c == 0x3000 || c == ',' || c == ';' ||
  24240. c == '(' || c == ')' || c == '{' || c == '}' || c == '[' || c == ']' ||
  24241. c == '|');
  24242. }
  24243. NK_INTERN int
  24244. nk_textedit_move_to_word_previous(struct nk_text_edit* state)
  24245. {
  24246. int c = state->cursor - 1;
  24247. while (c >= 0 && !nk_is_word_boundary(state, c))
  24248. --c;
  24249. if (c < 0)
  24250. c = 0;
  24251. return c;
  24252. }
  24253. NK_INTERN int
  24254. nk_textedit_move_to_word_next(struct nk_text_edit* state)
  24255. {
  24256. const int len = state->string.len;
  24257. int c = state->cursor + 1;
  24258. while (c < len && !nk_is_word_boundary(state, c))
  24259. ++c;
  24260. if (c > len)
  24261. c = len;
  24262. return c;
  24263. }
  24264. NK_INTERN void
  24265. nk_textedit_prep_selection_at_cursor(struct nk_text_edit* state)
  24266. {
  24267. /* update selection and cursor to match each other */
  24268. if (!NK_TEXT_HAS_SELECTION(state))
  24269. state->select_start = state->select_end = state->cursor;
  24270. else state->cursor = state->select_end;
  24271. }
  24272. NK_API nk_bool
  24273. nk_textedit_cut(struct nk_text_edit* state)
  24274. {
  24275. /* API cut: delete selection */
  24276. if (state->mode == NK_TEXT_EDIT_MODE_VIEW)
  24277. return 0;
  24278. if (NK_TEXT_HAS_SELECTION(state)) {
  24279. nk_textedit_delete_selection(state); /* implicitly clamps */
  24280. state->has_preferred_x = 0;
  24281. return 1;
  24282. }
  24283. return 0;
  24284. }
  24285. NK_API nk_bool
  24286. nk_textedit_paste(struct nk_text_edit* state, char const* ctext, int len)
  24287. {
  24288. /* API paste: replace existing selection with passed-in text */
  24289. int glyphs;
  24290. const char* text = (const char*)ctext;
  24291. if (state->mode == NK_TEXT_EDIT_MODE_VIEW) return 0;
  24292. /* if there's a selection, the paste should delete it */
  24293. nk_textedit_clamp(state);
  24294. nk_textedit_delete_selection(state);
  24295. /* try to insert the characters */
  24296. glyphs = nk_utf_len(ctext, len);
  24297. if (nk_str_insert_text_char(&state->string, state->cursor, text, len)) {
  24298. nk_textedit_makeundo_insert(state, state->cursor, glyphs);
  24299. state->cursor += len;
  24300. state->has_preferred_x = 0;
  24301. return 1;
  24302. }
  24303. /* remove the undo since we didn't actually insert the characters */
  24304. if (state->undo.undo_point)
  24305. --state->undo.undo_point;
  24306. return 0;
  24307. }
  24308. NK_API void
  24309. nk_textedit_text(struct nk_text_edit* state, const char* text, int total_len)
  24310. {
  24311. nk_rune unicode;
  24312. int glyph_len;
  24313. int text_len = 0;
  24314. NK_ASSERT(state);
  24315. NK_ASSERT(text);
  24316. if (!text || !total_len || state->mode == NK_TEXT_EDIT_MODE_VIEW) return;
  24317. glyph_len = nk_utf_decode(text, &unicode, total_len);
  24318. while ((text_len < total_len) && glyph_len)
  24319. {
  24320. /* don't insert a backward delete, just process the event */
  24321. if (unicode == 127) goto next;
  24322. /* can't add newline in single-line mode */
  24323. if (unicode == '\n' && state->single_line) goto next;
  24324. /* filter incoming text */
  24325. if (state->filter && !state->filter(state, unicode)) goto next;
  24326. if (!NK_TEXT_HAS_SELECTION(state) &&
  24327. state->cursor < state->string.len)
  24328. {
  24329. if (state->mode == NK_TEXT_EDIT_MODE_REPLACE) {
  24330. nk_textedit_makeundo_replace(state, state->cursor, 1, 1);
  24331. nk_str_delete_runes(&state->string, state->cursor, 1);
  24332. }
  24333. if (nk_str_insert_text_utf8(&state->string, state->cursor,
  24334. text + text_len, 1))
  24335. {
  24336. ++state->cursor;
  24337. state->has_preferred_x = 0;
  24338. }
  24339. }
  24340. else {
  24341. nk_textedit_delete_selection(state); /* implicitly clamps */
  24342. if (nk_str_insert_text_utf8(&state->string, state->cursor,
  24343. text + text_len, 1))
  24344. {
  24345. nk_textedit_makeundo_insert(state, state->cursor, 1);
  24346. state->cursor = NK_MIN(state->cursor + 1, state->string.len);
  24347. state->has_preferred_x = 0;
  24348. }
  24349. }
  24350. next:
  24351. text_len += glyph_len;
  24352. glyph_len = nk_utf_decode(text + text_len, &unicode, total_len - text_len);
  24353. }
  24354. }
  24355. NK_LIB void
  24356. nk_textedit_key(struct nk_text_edit* state, enum nk_keys key, int shift_mod,
  24357. const struct nk_user_font* font, float row_height)
  24358. {
  24359. retry:
  24360. switch (key)
  24361. {
  24362. case NK_KEY_NONE:
  24363. case NK_KEY_CTRL:
  24364. case NK_KEY_ENTER:
  24365. case NK_KEY_SHIFT:
  24366. case NK_KEY_TAB:
  24367. case NK_KEY_COPY:
  24368. case NK_KEY_CUT:
  24369. case NK_KEY_PASTE:
  24370. case NK_KEY_MAX:
  24371. default: break;
  24372. case NK_KEY_TEXT_UNDO:
  24373. nk_textedit_undo(state);
  24374. state->has_preferred_x = 0;
  24375. break;
  24376. case NK_KEY_TEXT_REDO:
  24377. nk_textedit_redo(state);
  24378. state->has_preferred_x = 0;
  24379. break;
  24380. case NK_KEY_TEXT_SELECT_ALL:
  24381. nk_textedit_select_all(state);
  24382. state->has_preferred_x = 0;
  24383. break;
  24384. case NK_KEY_TEXT_INSERT_MODE:
  24385. if (state->mode == NK_TEXT_EDIT_MODE_VIEW)
  24386. state->mode = NK_TEXT_EDIT_MODE_INSERT;
  24387. break;
  24388. case NK_KEY_TEXT_REPLACE_MODE:
  24389. if (state->mode == NK_TEXT_EDIT_MODE_VIEW)
  24390. state->mode = NK_TEXT_EDIT_MODE_REPLACE;
  24391. break;
  24392. case NK_KEY_TEXT_RESET_MODE:
  24393. if (state->mode == NK_TEXT_EDIT_MODE_INSERT ||
  24394. state->mode == NK_TEXT_EDIT_MODE_REPLACE)
  24395. state->mode = NK_TEXT_EDIT_MODE_VIEW;
  24396. break;
  24397. case NK_KEY_LEFT:
  24398. if (shift_mod) {
  24399. nk_textedit_clamp(state);
  24400. nk_textedit_prep_selection_at_cursor(state);
  24401. /* move selection left */
  24402. if (state->select_end > 0)
  24403. --state->select_end;
  24404. state->cursor = state->select_end;
  24405. state->has_preferred_x = 0;
  24406. }
  24407. else {
  24408. /* if currently there's a selection,
  24409. * move cursor to start of selection */
  24410. if (NK_TEXT_HAS_SELECTION(state))
  24411. nk_textedit_move_to_first(state);
  24412. else if (state->cursor > 0)
  24413. --state->cursor;
  24414. state->has_preferred_x = 0;
  24415. } break;
  24416. case NK_KEY_RIGHT:
  24417. if (shift_mod) {
  24418. nk_textedit_prep_selection_at_cursor(state);
  24419. /* move selection right */
  24420. ++state->select_end;
  24421. nk_textedit_clamp(state);
  24422. state->cursor = state->select_end;
  24423. state->has_preferred_x = 0;
  24424. }
  24425. else {
  24426. /* if currently there's a selection,
  24427. * move cursor to end of selection */
  24428. if (NK_TEXT_HAS_SELECTION(state))
  24429. nk_textedit_move_to_last(state);
  24430. else ++state->cursor;
  24431. nk_textedit_clamp(state);
  24432. state->has_preferred_x = 0;
  24433. } break;
  24434. case NK_KEY_TEXT_WORD_LEFT:
  24435. if (shift_mod) {
  24436. if (!NK_TEXT_HAS_SELECTION(state))
  24437. nk_textedit_prep_selection_at_cursor(state);
  24438. state->cursor = nk_textedit_move_to_word_previous(state);
  24439. state->select_end = state->cursor;
  24440. nk_textedit_clamp(state);
  24441. }
  24442. else {
  24443. if (NK_TEXT_HAS_SELECTION(state))
  24444. nk_textedit_move_to_first(state);
  24445. else {
  24446. state->cursor = nk_textedit_move_to_word_previous(state);
  24447. nk_textedit_clamp(state);
  24448. }
  24449. } break;
  24450. case NK_KEY_TEXT_WORD_RIGHT:
  24451. if (shift_mod) {
  24452. if (!NK_TEXT_HAS_SELECTION(state))
  24453. nk_textedit_prep_selection_at_cursor(state);
  24454. state->cursor = nk_textedit_move_to_word_next(state);
  24455. state->select_end = state->cursor;
  24456. nk_textedit_clamp(state);
  24457. }
  24458. else {
  24459. if (NK_TEXT_HAS_SELECTION(state))
  24460. nk_textedit_move_to_last(state);
  24461. else {
  24462. state->cursor = nk_textedit_move_to_word_next(state);
  24463. nk_textedit_clamp(state);
  24464. }
  24465. } break;
  24466. case NK_KEY_DOWN: {
  24467. struct nk_text_find find;
  24468. struct nk_text_edit_row row;
  24469. int i, sel = shift_mod;
  24470. if (state->single_line) {
  24471. /* on windows, up&down in single-line behave like left&right */
  24472. key = NK_KEY_RIGHT;
  24473. goto retry;
  24474. }
  24475. if (sel)
  24476. nk_textedit_prep_selection_at_cursor(state);
  24477. else if (NK_TEXT_HAS_SELECTION(state))
  24478. nk_textedit_move_to_last(state);
  24479. /* compute current position of cursor point */
  24480. nk_textedit_clamp(state);
  24481. nk_textedit_find_charpos(&find, state, state->cursor, state->single_line,
  24482. font, row_height);
  24483. /* now find character position down a row */
  24484. if (find.length)
  24485. {
  24486. float x;
  24487. float goal_x = state->has_preferred_x ? state->preferred_x : find.x;
  24488. int start = find.first_char + find.length;
  24489. state->cursor = start;
  24490. nk_textedit_layout_row(&row, state, state->cursor, row_height, font);
  24491. x = row.x0;
  24492. for (i = 0; i < row.num_chars && x < row.x1; ++i) {
  24493. float dx = nk_textedit_get_width(state, start, i, font);
  24494. x += dx;
  24495. if (x > goal_x)
  24496. break;
  24497. ++state->cursor;
  24498. }
  24499. nk_textedit_clamp(state);
  24500. state->has_preferred_x = 1;
  24501. state->preferred_x = goal_x;
  24502. if (sel)
  24503. state->select_end = state->cursor;
  24504. }
  24505. } break;
  24506. case NK_KEY_UP: {
  24507. struct nk_text_find find;
  24508. struct nk_text_edit_row row;
  24509. int i, sel = shift_mod;
  24510. if (state->single_line) {
  24511. /* on windows, up&down become left&right */
  24512. key = NK_KEY_LEFT;
  24513. goto retry;
  24514. }
  24515. if (sel)
  24516. nk_textedit_prep_selection_at_cursor(state);
  24517. else if (NK_TEXT_HAS_SELECTION(state))
  24518. nk_textedit_move_to_first(state);
  24519. /* compute current position of cursor point */
  24520. nk_textedit_clamp(state);
  24521. nk_textedit_find_charpos(&find, state, state->cursor, state->single_line,
  24522. font, row_height);
  24523. /* can only go up if there's a previous row */
  24524. if (find.prev_first != find.first_char) {
  24525. /* now find character position up a row */
  24526. float x;
  24527. float goal_x = state->has_preferred_x ? state->preferred_x : find.x;
  24528. state->cursor = find.prev_first;
  24529. nk_textedit_layout_row(&row, state, state->cursor, row_height, font);
  24530. x = row.x0;
  24531. for (i = 0; i < row.num_chars && x < row.x1; ++i) {
  24532. float dx = nk_textedit_get_width(state, find.prev_first, i, font);
  24533. x += dx;
  24534. if (x > goal_x)
  24535. break;
  24536. ++state->cursor;
  24537. }
  24538. nk_textedit_clamp(state);
  24539. state->has_preferred_x = 1;
  24540. state->preferred_x = goal_x;
  24541. if (sel) state->select_end = state->cursor;
  24542. }
  24543. } break;
  24544. case NK_KEY_DEL:
  24545. if (state->mode == NK_TEXT_EDIT_MODE_VIEW)
  24546. break;
  24547. if (NK_TEXT_HAS_SELECTION(state))
  24548. nk_textedit_delete_selection(state);
  24549. else {
  24550. int n = state->string.len;
  24551. if (state->cursor < n)
  24552. nk_textedit_delete(state, state->cursor, 1);
  24553. }
  24554. state->has_preferred_x = 0;
  24555. break;
  24556. case NK_KEY_BACKSPACE:
  24557. if (state->mode == NK_TEXT_EDIT_MODE_VIEW)
  24558. break;
  24559. if (NK_TEXT_HAS_SELECTION(state))
  24560. nk_textedit_delete_selection(state);
  24561. else {
  24562. nk_textedit_clamp(state);
  24563. if (state->cursor > 0) {
  24564. nk_textedit_delete(state, state->cursor - 1, 1);
  24565. --state->cursor;
  24566. }
  24567. }
  24568. state->has_preferred_x = 0;
  24569. break;
  24570. case NK_KEY_TEXT_START:
  24571. if (shift_mod) {
  24572. nk_textedit_prep_selection_at_cursor(state);
  24573. state->cursor = state->select_end = 0;
  24574. state->has_preferred_x = 0;
  24575. }
  24576. else {
  24577. state->cursor = state->select_start = state->select_end = 0;
  24578. state->has_preferred_x = 0;
  24579. }
  24580. break;
  24581. case NK_KEY_TEXT_END:
  24582. if (shift_mod) {
  24583. nk_textedit_prep_selection_at_cursor(state);
  24584. state->cursor = state->select_end = state->string.len;
  24585. state->has_preferred_x = 0;
  24586. }
  24587. else {
  24588. state->cursor = state->string.len;
  24589. state->select_start = state->select_end = 0;
  24590. state->has_preferred_x = 0;
  24591. }
  24592. break;
  24593. case NK_KEY_TEXT_LINE_START: {
  24594. if (shift_mod) {
  24595. struct nk_text_find find;
  24596. nk_textedit_clamp(state);
  24597. nk_textedit_prep_selection_at_cursor(state);
  24598. if (state->string.len && state->cursor == state->string.len)
  24599. --state->cursor;
  24600. nk_textedit_find_charpos(&find, state, state->cursor, state->single_line,
  24601. font, row_height);
  24602. state->cursor = state->select_end = find.first_char;
  24603. state->has_preferred_x = 0;
  24604. }
  24605. else {
  24606. struct nk_text_find find;
  24607. if (state->string.len && state->cursor == state->string.len)
  24608. --state->cursor;
  24609. nk_textedit_clamp(state);
  24610. nk_textedit_move_to_first(state);
  24611. nk_textedit_find_charpos(&find, state, state->cursor, state->single_line,
  24612. font, row_height);
  24613. state->cursor = find.first_char;
  24614. state->has_preferred_x = 0;
  24615. }
  24616. } break;
  24617. case NK_KEY_TEXT_LINE_END: {
  24618. if (shift_mod) {
  24619. struct nk_text_find find;
  24620. nk_textedit_clamp(state);
  24621. nk_textedit_prep_selection_at_cursor(state);
  24622. nk_textedit_find_charpos(&find, state, state->cursor, state->single_line,
  24623. font, row_height);
  24624. state->has_preferred_x = 0;
  24625. state->cursor = find.first_char + find.length;
  24626. if (find.length > 0 && nk_str_rune_at(&state->string, state->cursor - 1) == '\n')
  24627. --state->cursor;
  24628. state->select_end = state->cursor;
  24629. }
  24630. else {
  24631. struct nk_text_find find;
  24632. nk_textedit_clamp(state);
  24633. nk_textedit_move_to_first(state);
  24634. nk_textedit_find_charpos(&find, state, state->cursor, state->single_line,
  24635. font, row_height);
  24636. state->has_preferred_x = 0;
  24637. state->cursor = find.first_char + find.length;
  24638. if (find.length > 0 && nk_str_rune_at(&state->string, state->cursor - 1) == '\n')
  24639. --state->cursor;
  24640. }} break;
  24641. }
  24642. }
  24643. NK_INTERN void
  24644. nk_textedit_flush_redo(struct nk_text_undo_state* state)
  24645. {
  24646. state->redo_point = NK_TEXTEDIT_UNDOSTATECOUNT;
  24647. state->redo_char_point = NK_TEXTEDIT_UNDOCHARCOUNT;
  24648. }
  24649. NK_INTERN void
  24650. nk_textedit_discard_undo(struct nk_text_undo_state* state)
  24651. {
  24652. /* discard the oldest entry in the undo list */
  24653. if (state->undo_point > 0) {
  24654. /* if the 0th undo state has characters, clean those up */
  24655. if (state->undo_rec[0].char_storage >= 0) {
  24656. int n = state->undo_rec[0].insert_length, i;
  24657. /* delete n characters from all other records */
  24658. state->undo_char_point = (short)(state->undo_char_point - n);
  24659. NK_MEMCPY(state->undo_char, state->undo_char + n,
  24660. (nk_size)state->undo_char_point * sizeof(nk_rune));
  24661. for (i = 0; i < state->undo_point; ++i) {
  24662. if (state->undo_rec[i].char_storage >= 0)
  24663. state->undo_rec[i].char_storage = (short)
  24664. (state->undo_rec[i].char_storage - n);
  24665. }
  24666. }
  24667. --state->undo_point;
  24668. NK_MEMCPY(state->undo_rec, state->undo_rec + 1,
  24669. (nk_size)((nk_size)state->undo_point * sizeof(state->undo_rec[0])));
  24670. }
  24671. }
  24672. NK_INTERN void
  24673. nk_textedit_discard_redo(struct nk_text_undo_state* state)
  24674. {
  24675. /* discard the oldest entry in the redo list--it's bad if this
  24676. ever happens, but because undo & redo have to store the actual
  24677. characters in different cases, the redo character buffer can
  24678. fill up even though the undo buffer didn't */
  24679. nk_size num;
  24680. int k = NK_TEXTEDIT_UNDOSTATECOUNT - 1;
  24681. if (state->redo_point <= k) {
  24682. /* if the k'th undo state has characters, clean those up */
  24683. if (state->undo_rec[k].char_storage >= 0) {
  24684. int n = state->undo_rec[k].insert_length, i;
  24685. /* delete n characters from all other records */
  24686. state->redo_char_point = (short)(state->redo_char_point + n);
  24687. num = (nk_size)(NK_TEXTEDIT_UNDOCHARCOUNT - state->redo_char_point);
  24688. NK_MEMCPY(state->undo_char + state->redo_char_point,
  24689. state->undo_char + state->redo_char_point - n, num * sizeof(char));
  24690. for (i = state->redo_point; i < k; ++i) {
  24691. if (state->undo_rec[i].char_storage >= 0) {
  24692. state->undo_rec[i].char_storage = (short)
  24693. (state->undo_rec[i].char_storage + n);
  24694. }
  24695. }
  24696. }
  24697. ++state->redo_point;
  24698. num = (nk_size)(NK_TEXTEDIT_UNDOSTATECOUNT - state->redo_point);
  24699. if (num) NK_MEMCPY(state->undo_rec + state->redo_point - 1,
  24700. state->undo_rec + state->redo_point, num * sizeof(state->undo_rec[0]));
  24701. }
  24702. }
  24703. NK_INTERN struct nk_text_undo_record*
  24704. nk_textedit_create_undo_record(struct nk_text_undo_state* state, int numchars)
  24705. {
  24706. /* any time we create a new undo record, we discard redo*/
  24707. nk_textedit_flush_redo(state);
  24708. /* if we have no free records, we have to make room,
  24709. * by sliding the existing records down */
  24710. if (state->undo_point == NK_TEXTEDIT_UNDOSTATECOUNT)
  24711. nk_textedit_discard_undo(state);
  24712. /* if the characters to store won't possibly fit in the buffer,
  24713. * we can't undo */
  24714. if (numchars > NK_TEXTEDIT_UNDOCHARCOUNT) {
  24715. state->undo_point = 0;
  24716. state->undo_char_point = 0;
  24717. return 0;
  24718. }
  24719. /* if we don't have enough free characters in the buffer,
  24720. * we have to make room */
  24721. while (state->undo_char_point + numchars > NK_TEXTEDIT_UNDOCHARCOUNT)
  24722. nk_textedit_discard_undo(state);
  24723. return &state->undo_rec[state->undo_point++];
  24724. }
  24725. NK_INTERN nk_rune*
  24726. nk_textedit_createundo(struct nk_text_undo_state* state, int pos,
  24727. int insert_len, int delete_len)
  24728. {
  24729. struct nk_text_undo_record* r = nk_textedit_create_undo_record(state, insert_len);
  24730. if (r == 0)
  24731. return 0;
  24732. r->where = pos;
  24733. r->insert_length = (short)insert_len;
  24734. r->delete_length = (short)delete_len;
  24735. if (insert_len == 0) {
  24736. r->char_storage = -1;
  24737. return 0;
  24738. }
  24739. else {
  24740. r->char_storage = state->undo_char_point;
  24741. state->undo_char_point = (short)(state->undo_char_point + insert_len);
  24742. return &state->undo_char[r->char_storage];
  24743. }
  24744. }
  24745. NK_API void
  24746. nk_textedit_undo(struct nk_text_edit* state)
  24747. {
  24748. struct nk_text_undo_state* s = &state->undo;
  24749. struct nk_text_undo_record u, * r;
  24750. if (s->undo_point == 0)
  24751. return;
  24752. /* we need to do two things: apply the undo record, and create a redo record */
  24753. u = s->undo_rec[s->undo_point - 1];
  24754. r = &s->undo_rec[s->redo_point - 1];
  24755. r->char_storage = -1;
  24756. r->insert_length = u.delete_length;
  24757. r->delete_length = u.insert_length;
  24758. r->where = u.where;
  24759. if (u.delete_length)
  24760. {
  24761. /* if the undo record says to delete characters, then the redo record will
  24762. need to re-insert the characters that get deleted, so we need to store
  24763. them.
  24764. there are three cases:
  24765. - there's enough room to store the characters
  24766. - characters stored for *redoing* don't leave room for redo
  24767. - characters stored for *undoing* don't leave room for redo
  24768. if the last is true, we have to bail */
  24769. if (s->undo_char_point + u.delete_length >= NK_TEXTEDIT_UNDOCHARCOUNT) {
  24770. /* the undo records take up too much character space; there's no space
  24771. * to store the redo characters */
  24772. r->insert_length = 0;
  24773. }
  24774. else {
  24775. int i;
  24776. /* there's definitely room to store the characters eventually */
  24777. while (s->undo_char_point + u.delete_length > s->redo_char_point) {
  24778. /* there's currently not enough room, so discard a redo record */
  24779. nk_textedit_discard_redo(s);
  24780. /* should never happen: */
  24781. if (s->redo_point == NK_TEXTEDIT_UNDOSTATECOUNT)
  24782. return;
  24783. }
  24784. r = &s->undo_rec[s->redo_point - 1];
  24785. r->char_storage = (short)(s->redo_char_point - u.delete_length);
  24786. s->redo_char_point = (short)(s->redo_char_point - u.delete_length);
  24787. /* now save the characters */
  24788. for (i = 0; i < u.delete_length; ++i)
  24789. s->undo_char[r->char_storage + i] =
  24790. nk_str_rune_at(&state->string, u.where + i);
  24791. }
  24792. /* now we can carry out the deletion */
  24793. nk_str_delete_runes(&state->string, u.where, u.delete_length);
  24794. }
  24795. /* check type of recorded action: */
  24796. if (u.insert_length) {
  24797. /* easy case: was a deletion, so we need to insert n characters */
  24798. nk_str_insert_text_runes(&state->string, u.where,
  24799. &s->undo_char[u.char_storage], u.insert_length);
  24800. s->undo_char_point = (short)(s->undo_char_point - u.insert_length);
  24801. }
  24802. state->cursor = (short)(u.where + u.insert_length);
  24803. s->undo_point--;
  24804. s->redo_point--;
  24805. }
  24806. NK_API void
  24807. nk_textedit_redo(struct nk_text_edit* state)
  24808. {
  24809. struct nk_text_undo_state* s = &state->undo;
  24810. struct nk_text_undo_record* u, r;
  24811. if (s->redo_point == NK_TEXTEDIT_UNDOSTATECOUNT)
  24812. return;
  24813. /* we need to do two things: apply the redo record, and create an undo record */
  24814. u = &s->undo_rec[s->undo_point];
  24815. r = s->undo_rec[s->redo_point];
  24816. /* we KNOW there must be room for the undo record, because the redo record
  24817. was derived from an undo record */
  24818. u->delete_length = r.insert_length;
  24819. u->insert_length = r.delete_length;
  24820. u->where = r.where;
  24821. u->char_storage = -1;
  24822. if (r.delete_length) {
  24823. /* the redo record requires us to delete characters, so the undo record
  24824. needs to store the characters */
  24825. if (s->undo_char_point + u->insert_length > s->redo_char_point) {
  24826. u->insert_length = 0;
  24827. u->delete_length = 0;
  24828. }
  24829. else {
  24830. int i;
  24831. u->char_storage = s->undo_char_point;
  24832. s->undo_char_point = (short)(s->undo_char_point + u->insert_length);
  24833. /* now save the characters */
  24834. for (i = 0; i < u->insert_length; ++i) {
  24835. s->undo_char[u->char_storage + i] =
  24836. nk_str_rune_at(&state->string, u->where + i);
  24837. }
  24838. }
  24839. nk_str_delete_runes(&state->string, r.where, r.delete_length);
  24840. }
  24841. if (r.insert_length) {
  24842. /* easy case: need to insert n characters */
  24843. nk_str_insert_text_runes(&state->string, r.where,
  24844. &s->undo_char[r.char_storage], r.insert_length);
  24845. }
  24846. state->cursor = r.where + r.insert_length;
  24847. s->undo_point++;
  24848. s->redo_point++;
  24849. }
  24850. NK_INTERN void
  24851. nk_textedit_makeundo_insert(struct nk_text_edit* state, int where, int length)
  24852. {
  24853. nk_textedit_createundo(&state->undo, where, 0, length);
  24854. }
  24855. NK_INTERN void
  24856. nk_textedit_makeundo_delete(struct nk_text_edit* state, int where, int length)
  24857. {
  24858. int i;
  24859. nk_rune* p = nk_textedit_createundo(&state->undo, where, length, 0);
  24860. if (p) {
  24861. for (i = 0; i < length; ++i)
  24862. p[i] = nk_str_rune_at(&state->string, where + i);
  24863. }
  24864. }
  24865. NK_INTERN void
  24866. nk_textedit_makeundo_replace(struct nk_text_edit* state, int where,
  24867. int old_length, int new_length)
  24868. {
  24869. int i;
  24870. nk_rune* p = nk_textedit_createundo(&state->undo, where, old_length, new_length);
  24871. if (p) {
  24872. for (i = 0; i < old_length; ++i)
  24873. p[i] = nk_str_rune_at(&state->string, where + i);
  24874. }
  24875. }
  24876. NK_LIB void
  24877. nk_textedit_clear_state(struct nk_text_edit* state, enum nk_text_edit_type type,
  24878. nk_plugin_filter filter)
  24879. {
  24880. /* reset the state to default */
  24881. state->undo.undo_point = 0;
  24882. state->undo.undo_char_point = 0;
  24883. state->undo.redo_point = NK_TEXTEDIT_UNDOSTATECOUNT;
  24884. state->undo.redo_char_point = NK_TEXTEDIT_UNDOCHARCOUNT;
  24885. state->select_end = state->select_start = 0;
  24886. state->cursor = 0;
  24887. state->has_preferred_x = 0;
  24888. state->preferred_x = 0;
  24889. state->cursor_at_end_of_line = 0;
  24890. state->initialized = 1;
  24891. state->single_line = (unsigned char)(type == NK_TEXT_EDIT_SINGLE_LINE);
  24892. state->mode = NK_TEXT_EDIT_MODE_VIEW;
  24893. state->filter = filter;
  24894. state->scrollbar = nk_vec2(0, 0);
  24895. }
  24896. NK_API void
  24897. nk_textedit_init_fixed(struct nk_text_edit* state, void* memory, nk_size size)
  24898. {
  24899. NK_ASSERT(state);
  24900. NK_ASSERT(memory);
  24901. if (!state || !memory || !size) return;
  24902. NK_MEMSET(state, 0, sizeof(struct nk_text_edit));
  24903. nk_textedit_clear_state(state, NK_TEXT_EDIT_SINGLE_LINE, 0);
  24904. nk_str_init_fixed(&state->string, memory, size);
  24905. }
  24906. NK_API void
  24907. nk_textedit_init(struct nk_text_edit* state, struct nk_allocator* alloc, nk_size size)
  24908. {
  24909. NK_ASSERT(state);
  24910. NK_ASSERT(alloc);
  24911. if (!state || !alloc) return;
  24912. NK_MEMSET(state, 0, sizeof(struct nk_text_edit));
  24913. nk_textedit_clear_state(state, NK_TEXT_EDIT_SINGLE_LINE, 0);
  24914. nk_str_init(&state->string, alloc, size);
  24915. }
  24916. #ifdef NK_INCLUDE_DEFAULT_ALLOCATOR
  24917. NK_API void
  24918. nk_textedit_init_default(struct nk_text_edit* state)
  24919. {
  24920. NK_ASSERT(state);
  24921. if (!state) return;
  24922. NK_MEMSET(state, 0, sizeof(struct nk_text_edit));
  24923. nk_textedit_clear_state(state, NK_TEXT_EDIT_SINGLE_LINE, 0);
  24924. nk_str_init_default(&state->string);
  24925. }
  24926. #endif
  24927. NK_API void
  24928. nk_textedit_select_all(struct nk_text_edit* state)
  24929. {
  24930. NK_ASSERT(state);
  24931. state->select_start = 0;
  24932. state->select_end = state->string.len;
  24933. }
  24934. NK_API void
  24935. nk_textedit_free(struct nk_text_edit* state)
  24936. {
  24937. NK_ASSERT(state);
  24938. if (!state) return;
  24939. nk_str_free(&state->string);
  24940. }
  24941. /* ===============================================================
  24942. *
  24943. * FILTER
  24944. *
  24945. * ===============================================================*/
  24946. NK_API nk_bool
  24947. nk_filter_default(const struct nk_text_edit* box, nk_rune unicode)
  24948. {
  24949. NK_UNUSED(unicode);
  24950. NK_UNUSED(box);
  24951. return nk_true;
  24952. }
  24953. NK_API nk_bool
  24954. nk_filter_ascii(const struct nk_text_edit* box, nk_rune unicode)
  24955. {
  24956. NK_UNUSED(box);
  24957. if (unicode > 128) return nk_false;
  24958. else return nk_true;
  24959. }
  24960. NK_API nk_bool
  24961. nk_filter_float(const struct nk_text_edit* box, nk_rune unicode)
  24962. {
  24963. NK_UNUSED(box);
  24964. if ((unicode < '0' || unicode > '9') && unicode != '.' && unicode != '-')
  24965. return nk_false;
  24966. else return nk_true;
  24967. }
  24968. NK_API nk_bool
  24969. nk_filter_decimal(const struct nk_text_edit* box, nk_rune unicode)
  24970. {
  24971. NK_UNUSED(box);
  24972. if ((unicode < '0' || unicode > '9') && unicode != '-')
  24973. return nk_false;
  24974. else return nk_true;
  24975. }
  24976. NK_API nk_bool
  24977. nk_filter_hex(const struct nk_text_edit* box, nk_rune unicode)
  24978. {
  24979. NK_UNUSED(box);
  24980. if ((unicode < '0' || unicode > '9') &&
  24981. (unicode < 'a' || unicode > 'f') &&
  24982. (unicode < 'A' || unicode > 'F'))
  24983. return nk_false;
  24984. else return nk_true;
  24985. }
  24986. NK_API nk_bool
  24987. nk_filter_oct(const struct nk_text_edit* box, nk_rune unicode)
  24988. {
  24989. NK_UNUSED(box);
  24990. if (unicode < '0' || unicode > '7')
  24991. return nk_false;
  24992. else return nk_true;
  24993. }
  24994. NK_API nk_bool
  24995. nk_filter_binary(const struct nk_text_edit* box, nk_rune unicode)
  24996. {
  24997. NK_UNUSED(box);
  24998. if (unicode != '0' && unicode != '1')
  24999. return nk_false;
  25000. else return nk_true;
  25001. }
  25002. /* ===============================================================
  25003. *
  25004. * EDIT
  25005. *
  25006. * ===============================================================*/
  25007. NK_LIB void
  25008. nk_edit_draw_text(struct nk_command_buffer* out,
  25009. const struct nk_style_edit* style, float pos_x, float pos_y,
  25010. float x_offset, const char* text, int byte_len, float row_height,
  25011. const struct nk_user_font* font, struct nk_color background,
  25012. struct nk_color foreground, nk_bool is_selected)
  25013. {
  25014. NK_ASSERT(out);
  25015. NK_ASSERT(font);
  25016. NK_ASSERT(style);
  25017. if (!text || !byte_len || !out || !style) return;
  25018. {int glyph_len = 0;
  25019. nk_rune unicode = 0;
  25020. int text_len = 0;
  25021. float line_width = 0;
  25022. float glyph_width;
  25023. const char* line = text;
  25024. float line_offset = 0;
  25025. int line_count = 0;
  25026. struct nk_text txt;
  25027. txt.padding = nk_vec2(0, 0);
  25028. txt.background = background;
  25029. txt.text = foreground;
  25030. glyph_len = nk_utf_decode(text + text_len, &unicode, byte_len - text_len);
  25031. if (!glyph_len) return;
  25032. while ((text_len < byte_len) && glyph_len)
  25033. {
  25034. if (unicode == '\n') {
  25035. /* new line separator so draw previous line */
  25036. struct nk_rect label;
  25037. label.y = pos_y + line_offset;
  25038. label.h = row_height;
  25039. label.w = line_width;
  25040. label.x = pos_x;
  25041. if (!line_count)
  25042. label.x += x_offset;
  25043. if (is_selected) /* selection needs to draw different background color */
  25044. nk_fill_rect(out, label, 0, background);
  25045. nk_widget_text(out, label, line, (int)((text + text_len) - line),
  25046. &txt, NK_TEXT_CENTERED, font);
  25047. text_len++;
  25048. line_count++;
  25049. line_width = 0;
  25050. line = text + text_len;
  25051. line_offset += row_height;
  25052. glyph_len = nk_utf_decode(text + text_len, &unicode, (int)(byte_len - text_len));
  25053. continue;
  25054. }
  25055. if (unicode == '\r') {
  25056. text_len++;
  25057. glyph_len = nk_utf_decode(text + text_len, &unicode, byte_len - text_len);
  25058. continue;
  25059. }
  25060. glyph_width = font->width(font->userdata, font->height, text + text_len, glyph_len);
  25061. line_width += (float)glyph_width;
  25062. text_len += glyph_len;
  25063. glyph_len = nk_utf_decode(text + text_len, &unicode, byte_len - text_len);
  25064. continue;
  25065. }
  25066. if (line_width > 0) {
  25067. /* draw last line */
  25068. struct nk_rect label;
  25069. label.y = pos_y + line_offset;
  25070. label.h = row_height;
  25071. label.w = line_width;
  25072. label.x = pos_x;
  25073. if (!line_count)
  25074. label.x += x_offset;
  25075. if (is_selected)
  25076. nk_fill_rect(out, label, 0, background);
  25077. nk_widget_text(out, label, line, (int)((text + text_len) - line),
  25078. &txt, NK_TEXT_LEFT, font);
  25079. }}
  25080. }
  25081. NK_LIB nk_flags
  25082. nk_do_edit(nk_flags* state, struct nk_command_buffer* out,
  25083. struct nk_rect bounds, nk_flags flags, nk_plugin_filter filter,
  25084. struct nk_text_edit* edit, const struct nk_style_edit* style,
  25085. struct nk_input* in, const struct nk_user_font* font)
  25086. {
  25087. struct nk_rect area;
  25088. nk_flags ret = 0;
  25089. float row_height;
  25090. char prev_state = 0;
  25091. char is_hovered = 0;
  25092. char select_all = 0;
  25093. char cursor_follow = 0;
  25094. struct nk_rect old_clip;
  25095. struct nk_rect clip;
  25096. NK_ASSERT(state);
  25097. NK_ASSERT(out);
  25098. NK_ASSERT(style);
  25099. if (!state || !out || !style)
  25100. return ret;
  25101. /* visible text area calculation */
  25102. area.x = bounds.x + style->padding.x + style->border;
  25103. area.y = bounds.y + style->padding.y + style->border;
  25104. area.w = bounds.w - (2.0f * style->padding.x + 2 * style->border);
  25105. area.h = bounds.h - (2.0f * style->padding.y + 2 * style->border);
  25106. if (flags & NK_EDIT_MULTILINE)
  25107. area.w = NK_MAX(0, area.w - style->scrollbar_size.x);
  25108. row_height = (flags & NK_EDIT_MULTILINE) ? font->height + style->row_padding : area.h;
  25109. /* calculate clipping rectangle */
  25110. old_clip = out->clip;
  25111. nk_unify(&clip, &old_clip, area.x, area.y, area.x + area.w, area.y + area.h);
  25112. /* update edit state */
  25113. prev_state = (char)edit->active;
  25114. is_hovered = (char)nk_input_is_mouse_hovering_rect(in, bounds);
  25115. if (in && in->mouse.buttons[NK_BUTTON_LEFT].clicked && in->mouse.buttons[NK_BUTTON_LEFT].down) {
  25116. edit->active = NK_INBOX(in->mouse.pos.x, in->mouse.pos.y,
  25117. bounds.x, bounds.y, bounds.w, bounds.h);
  25118. }
  25119. /* (de)activate text editor */
  25120. if (!prev_state && edit->active) {
  25121. const enum nk_text_edit_type type = (flags & NK_EDIT_MULTILINE) ?
  25122. NK_TEXT_EDIT_MULTI_LINE : NK_TEXT_EDIT_SINGLE_LINE;
  25123. /* keep scroll position when re-activating edit widget */
  25124. struct nk_vec2 oldscrollbar = edit->scrollbar;
  25125. nk_textedit_clear_state(edit, type, filter);
  25126. edit->scrollbar = oldscrollbar;
  25127. if (flags & NK_EDIT_AUTO_SELECT)
  25128. select_all = nk_true;
  25129. if (flags & NK_EDIT_GOTO_END_ON_ACTIVATE) {
  25130. edit->cursor = edit->string.len;
  25131. in = 0;
  25132. }
  25133. }
  25134. else if (!edit->active) edit->mode = NK_TEXT_EDIT_MODE_VIEW;
  25135. if (flags & NK_EDIT_READ_ONLY)
  25136. edit->mode = NK_TEXT_EDIT_MODE_VIEW;
  25137. else if (flags & NK_EDIT_ALWAYS_INSERT_MODE)
  25138. edit->mode = NK_TEXT_EDIT_MODE_INSERT;
  25139. ret = (edit->active) ? NK_EDIT_ACTIVE : NK_EDIT_INACTIVE;
  25140. if (prev_state != edit->active)
  25141. ret |= (edit->active) ? NK_EDIT_ACTIVATED : NK_EDIT_DEACTIVATED;
  25142. /* handle user input */
  25143. if (edit->active && in)
  25144. {
  25145. int shift_mod = in->keyboard.keys[NK_KEY_SHIFT].down;
  25146. const float mouse_x = (in->mouse.pos.x - area.x) + edit->scrollbar.x;
  25147. const float mouse_y = (in->mouse.pos.y - area.y) + edit->scrollbar.y;
  25148. /* mouse click handler */
  25149. is_hovered = (char)nk_input_is_mouse_hovering_rect(in, area);
  25150. if (select_all) {
  25151. nk_textedit_select_all(edit);
  25152. }
  25153. else if (is_hovered && in->mouse.buttons[NK_BUTTON_LEFT].down &&
  25154. in->mouse.buttons[NK_BUTTON_LEFT].clicked) {
  25155. nk_textedit_click(edit, mouse_x, mouse_y, font, row_height);
  25156. }
  25157. else if (is_hovered && in->mouse.buttons[NK_BUTTON_LEFT].down &&
  25158. (in->mouse.delta.x != 0.0f || in->mouse.delta.y != 0.0f)) {
  25159. nk_textedit_drag(edit, mouse_x, mouse_y, font, row_height);
  25160. cursor_follow = nk_true;
  25161. }
  25162. else if (is_hovered && in->mouse.buttons[NK_BUTTON_RIGHT].clicked &&
  25163. in->mouse.buttons[NK_BUTTON_RIGHT].down) {
  25164. nk_textedit_key(edit, NK_KEY_TEXT_WORD_LEFT, nk_false, font, row_height);
  25165. nk_textedit_key(edit, NK_KEY_TEXT_WORD_RIGHT, nk_true, font, row_height);
  25166. cursor_follow = nk_true;
  25167. }
  25168. {int i; /* keyboard input */
  25169. int old_mode = edit->mode;
  25170. for (i = 0; i < NK_KEY_MAX; ++i) {
  25171. if (i == NK_KEY_ENTER || i == NK_KEY_TAB) continue; /* special case */
  25172. if (nk_input_is_key_pressed(in, (enum nk_keys)i)) {
  25173. nk_textedit_key(edit, (enum nk_keys)i, shift_mod, font, row_height);
  25174. cursor_follow = nk_true;
  25175. }
  25176. }
  25177. if (old_mode != edit->mode) {
  25178. in->keyboard.text_len = 0;
  25179. }}
  25180. /* text input */
  25181. edit->filter = filter;
  25182. if (in->keyboard.text_len) {
  25183. nk_textedit_text(edit, in->keyboard.text, in->keyboard.text_len);
  25184. cursor_follow = nk_true;
  25185. in->keyboard.text_len = 0;
  25186. }
  25187. /* enter key handler */
  25188. if (nk_input_is_key_pressed(in, NK_KEY_ENTER)) {
  25189. cursor_follow = nk_true;
  25190. if (flags & NK_EDIT_CTRL_ENTER_NEWLINE && shift_mod)
  25191. nk_textedit_text(edit, "\n", 1);
  25192. else if (flags & NK_EDIT_SIG_ENTER)
  25193. ret |= NK_EDIT_COMMITED;
  25194. else nk_textedit_text(edit, "\n", 1);
  25195. }
  25196. /* cut & copy handler */
  25197. {int copy = nk_input_is_key_pressed(in, NK_KEY_COPY);
  25198. int cut = nk_input_is_key_pressed(in, NK_KEY_CUT);
  25199. if ((copy || cut) && (flags & NK_EDIT_CLIPBOARD))
  25200. {
  25201. int glyph_len;
  25202. nk_rune unicode;
  25203. const char* text;
  25204. int b = edit->select_start;
  25205. int e = edit->select_end;
  25206. int begin = NK_MIN(b, e);
  25207. int end = NK_MAX(b, e);
  25208. text = nk_str_at_const(&edit->string, begin, &unicode, &glyph_len);
  25209. if (edit->clip.copy)
  25210. edit->clip.copy(edit->clip.userdata, text, end - begin);
  25211. if (cut && !(flags & NK_EDIT_READ_ONLY)) {
  25212. nk_textedit_cut(edit);
  25213. cursor_follow = nk_true;
  25214. }
  25215. }}
  25216. /* paste handler */
  25217. {int paste = nk_input_is_key_pressed(in, NK_KEY_PASTE);
  25218. if (paste && (flags & NK_EDIT_CLIPBOARD) && edit->clip.paste) {
  25219. edit->clip.paste(edit->clip.userdata, edit);
  25220. cursor_follow = nk_true;
  25221. }}
  25222. /* tab handler */
  25223. {int tab = nk_input_is_key_pressed(in, NK_KEY_TAB);
  25224. if (tab && (flags & NK_EDIT_ALLOW_TAB)) {
  25225. nk_textedit_text(edit, " ", 4);
  25226. cursor_follow = nk_true;
  25227. }}
  25228. }
  25229. /* set widget state */
  25230. if (edit->active)
  25231. *state = NK_WIDGET_STATE_ACTIVE;
  25232. else nk_widget_state_reset(state);
  25233. if (is_hovered)
  25234. *state |= NK_WIDGET_STATE_HOVERED;
  25235. /* DRAW EDIT */
  25236. {const char* text = nk_str_get_const(&edit->string);
  25237. int len = nk_str_len_char(&edit->string);
  25238. {/* select background colors/images */
  25239. const struct nk_style_item* background;
  25240. if (*state & NK_WIDGET_STATE_ACTIVED)
  25241. background = &style->active;
  25242. else if (*state & NK_WIDGET_STATE_HOVER)
  25243. background = &style->hover;
  25244. else background = &style->normal;
  25245. /* draw background frame */
  25246. switch (background->type) {
  25247. case NK_STYLE_ITEM_IMAGE:
  25248. nk_draw_image(out, bounds, &background->data.image, nk_white);
  25249. break;
  25250. case NK_STYLE_ITEM_NINE_SLICE:
  25251. nk_draw_nine_slice(out, bounds, &background->data.slice, nk_white);
  25252. break;
  25253. case NK_STYLE_ITEM_COLOR:
  25254. nk_fill_rect(out, bounds, style->rounding, background->data.color);
  25255. nk_stroke_rect(out, bounds, style->rounding, style->border, style->border_color);
  25256. break;
  25257. }}
  25258. area.w = NK_MAX(0, area.w - style->cursor_size);
  25259. if (edit->active)
  25260. {
  25261. int total_lines = 1;
  25262. struct nk_vec2 text_size = nk_vec2(0, 0);
  25263. /* text pointer positions */
  25264. const char* cursor_ptr = 0;
  25265. const char* select_begin_ptr = 0;
  25266. const char* select_end_ptr = 0;
  25267. /* 2D pixel positions */
  25268. struct nk_vec2 cursor_pos = nk_vec2(0, 0);
  25269. struct nk_vec2 selection_offset_start = nk_vec2(0, 0);
  25270. struct nk_vec2 selection_offset_end = nk_vec2(0, 0);
  25271. int selection_begin = NK_MIN(edit->select_start, edit->select_end);
  25272. int selection_end = NK_MAX(edit->select_start, edit->select_end);
  25273. /* calculate total line count + total space + cursor/selection position */
  25274. float line_width = 0.0f;
  25275. if (text && len)
  25276. {
  25277. /* utf8 encoding */
  25278. float glyph_width;
  25279. int glyph_len = 0;
  25280. nk_rune unicode = 0;
  25281. int text_len = 0;
  25282. int glyphs = 0;
  25283. int row_begin = 0;
  25284. glyph_len = nk_utf_decode(text, &unicode, len);
  25285. glyph_width = font->width(font->userdata, font->height, text, glyph_len);
  25286. line_width = 0;
  25287. /* iterate all lines */
  25288. while ((text_len < len) && glyph_len)
  25289. {
  25290. /* set cursor 2D position and line */
  25291. if (!cursor_ptr && glyphs == edit->cursor)
  25292. {
  25293. int glyph_offset;
  25294. struct nk_vec2 out_offset;
  25295. struct nk_vec2 row_size;
  25296. const char* remaining;
  25297. /* calculate 2d position */
  25298. cursor_pos.y = (float)(total_lines - 1) * row_height;
  25299. row_size = nk_text_calculate_text_bounds(font, text + row_begin,
  25300. text_len - row_begin, row_height, &remaining,
  25301. &out_offset, &glyph_offset, NK_STOP_ON_NEW_LINE);
  25302. cursor_pos.x = row_size.x;
  25303. cursor_ptr = text + text_len;
  25304. }
  25305. /* set start selection 2D position and line */
  25306. if (!select_begin_ptr && edit->select_start != edit->select_end &&
  25307. glyphs == selection_begin)
  25308. {
  25309. int glyph_offset;
  25310. struct nk_vec2 out_offset;
  25311. struct nk_vec2 row_size;
  25312. const char* remaining;
  25313. /* calculate 2d position */
  25314. selection_offset_start.y = (float)(NK_MAX(total_lines - 1, 0)) * row_height;
  25315. row_size = nk_text_calculate_text_bounds(font, text + row_begin,
  25316. text_len - row_begin, row_height, &remaining,
  25317. &out_offset, &glyph_offset, NK_STOP_ON_NEW_LINE);
  25318. selection_offset_start.x = row_size.x;
  25319. select_begin_ptr = text + text_len;
  25320. }
  25321. /* set end selection 2D position and line */
  25322. if (!select_end_ptr && edit->select_start != edit->select_end &&
  25323. glyphs == selection_end)
  25324. {
  25325. int glyph_offset;
  25326. struct nk_vec2 out_offset;
  25327. struct nk_vec2 row_size;
  25328. const char* remaining;
  25329. /* calculate 2d position */
  25330. selection_offset_end.y = (float)(total_lines - 1) * row_height;
  25331. row_size = nk_text_calculate_text_bounds(font, text + row_begin,
  25332. text_len - row_begin, row_height, &remaining,
  25333. &out_offset, &glyph_offset, NK_STOP_ON_NEW_LINE);
  25334. selection_offset_end.x = row_size.x;
  25335. select_end_ptr = text + text_len;
  25336. }
  25337. if (unicode == '\n') {
  25338. text_size.x = NK_MAX(text_size.x, line_width);
  25339. total_lines++;
  25340. line_width = 0;
  25341. text_len++;
  25342. glyphs++;
  25343. row_begin = text_len;
  25344. glyph_len = nk_utf_decode(text + text_len, &unicode, len - text_len);
  25345. glyph_width = font->width(font->userdata, font->height, text + text_len, glyph_len);
  25346. continue;
  25347. }
  25348. glyphs++;
  25349. text_len += glyph_len;
  25350. line_width += (float)glyph_width;
  25351. glyph_len = nk_utf_decode(text + text_len, &unicode, len - text_len);
  25352. glyph_width = font->width(font->userdata, font->height,
  25353. text + text_len, glyph_len);
  25354. continue;
  25355. }
  25356. text_size.y = (float)total_lines * row_height;
  25357. /* handle case when cursor is at end of text buffer */
  25358. if (!cursor_ptr && edit->cursor == edit->string.len) {
  25359. cursor_pos.x = line_width;
  25360. cursor_pos.y = text_size.y - row_height;
  25361. }
  25362. }
  25363. {
  25364. /* scrollbar */
  25365. if (cursor_follow)
  25366. {
  25367. /* update scrollbar to follow cursor */
  25368. if (!(flags & NK_EDIT_NO_HORIZONTAL_SCROLL)) {
  25369. /* horizontal scroll */
  25370. const float scroll_increment = area.w * 0.25f;
  25371. if (cursor_pos.x < edit->scrollbar.x)
  25372. edit->scrollbar.x = (float)(int)NK_MAX(0.0f, cursor_pos.x - scroll_increment);
  25373. if (cursor_pos.x >= edit->scrollbar.x + area.w)
  25374. edit->scrollbar.x = (float)(int)NK_MAX(0.0f, cursor_pos.x - area.w + scroll_increment);
  25375. }
  25376. else edit->scrollbar.x = 0;
  25377. if (flags & NK_EDIT_MULTILINE) {
  25378. /* vertical scroll */
  25379. if (cursor_pos.y < edit->scrollbar.y)
  25380. edit->scrollbar.y = NK_MAX(0.0f, cursor_pos.y - row_height);
  25381. if (cursor_pos.y >= edit->scrollbar.y + row_height)
  25382. edit->scrollbar.y = edit->scrollbar.y + row_height;
  25383. }
  25384. else edit->scrollbar.y = 0;
  25385. }
  25386. /* scrollbar widget */
  25387. if (flags & NK_EDIT_MULTILINE)
  25388. {
  25389. nk_flags ws;
  25390. struct nk_rect scroll;
  25391. float scroll_target;
  25392. float scroll_offset;
  25393. float scroll_step;
  25394. float scroll_inc;
  25395. scroll = area;
  25396. scroll.x = (bounds.x + bounds.w - style->border) - style->scrollbar_size.x;
  25397. scroll.w = style->scrollbar_size.x;
  25398. scroll_offset = edit->scrollbar.y;
  25399. scroll_step = scroll.h * 0.10f;
  25400. scroll_inc = scroll.h * 0.01f;
  25401. scroll_target = text_size.y;
  25402. edit->scrollbar.y = nk_do_scrollbarv(&ws, out, scroll, 0,
  25403. scroll_offset, scroll_target, scroll_step, scroll_inc,
  25404. &style->scrollbar, in, font);
  25405. }
  25406. }
  25407. /* draw text */
  25408. {struct nk_color background_color;
  25409. struct nk_color text_color;
  25410. struct nk_color sel_background_color;
  25411. struct nk_color sel_text_color;
  25412. struct nk_color cursor_color;
  25413. struct nk_color cursor_text_color;
  25414. const struct nk_style_item* background;
  25415. nk_push_scissor(out, clip);
  25416. /* select correct colors to draw */
  25417. if (*state & NK_WIDGET_STATE_ACTIVED) {
  25418. background = &style->active;
  25419. text_color = style->text_active;
  25420. sel_text_color = style->selected_text_hover;
  25421. sel_background_color = style->selected_hover;
  25422. cursor_color = style->cursor_hover;
  25423. cursor_text_color = style->cursor_text_hover;
  25424. }
  25425. else if (*state & NK_WIDGET_STATE_HOVER) {
  25426. background = &style->hover;
  25427. text_color = style->text_hover;
  25428. sel_text_color = style->selected_text_hover;
  25429. sel_background_color = style->selected_hover;
  25430. cursor_text_color = style->cursor_text_hover;
  25431. cursor_color = style->cursor_hover;
  25432. }
  25433. else {
  25434. background = &style->normal;
  25435. text_color = style->text_normal;
  25436. sel_text_color = style->selected_text_normal;
  25437. sel_background_color = style->selected_normal;
  25438. cursor_color = style->cursor_normal;
  25439. cursor_text_color = style->cursor_text_normal;
  25440. }
  25441. if (background->type == NK_STYLE_ITEM_IMAGE)
  25442. background_color = nk_rgba(0, 0, 0, 0);
  25443. else
  25444. background_color = background->data.color;
  25445. if (edit->select_start == edit->select_end) {
  25446. /* no selection so just draw the complete text */
  25447. const char* begin = nk_str_get_const(&edit->string);
  25448. int l = nk_str_len_char(&edit->string);
  25449. nk_edit_draw_text(out, style, area.x - edit->scrollbar.x,
  25450. area.y - edit->scrollbar.y, 0, begin, l, row_height, font,
  25451. background_color, text_color, nk_false);
  25452. }
  25453. else {
  25454. /* edit has selection so draw 1-3 text chunks */
  25455. if (edit->select_start != edit->select_end && selection_begin > 0) {
  25456. /* draw unselected text before selection */
  25457. const char* begin = nk_str_get_const(&edit->string);
  25458. NK_ASSERT(select_begin_ptr);
  25459. nk_edit_draw_text(out, style, area.x - edit->scrollbar.x,
  25460. area.y - edit->scrollbar.y, 0, begin, (int)(select_begin_ptr - begin),
  25461. row_height, font, background_color, text_color, nk_false);
  25462. }
  25463. if (edit->select_start != edit->select_end) {
  25464. /* draw selected text */
  25465. NK_ASSERT(select_begin_ptr);
  25466. if (!select_end_ptr) {
  25467. const char* begin = nk_str_get_const(&edit->string);
  25468. select_end_ptr = begin + nk_str_len_char(&edit->string);
  25469. }
  25470. nk_edit_draw_text(out, style,
  25471. area.x - edit->scrollbar.x,
  25472. area.y + selection_offset_start.y - edit->scrollbar.y,
  25473. selection_offset_start.x,
  25474. select_begin_ptr, (int)(select_end_ptr - select_begin_ptr),
  25475. row_height, font, sel_background_color, sel_text_color, nk_true);
  25476. }
  25477. if ((edit->select_start != edit->select_end &&
  25478. selection_end < edit->string.len))
  25479. {
  25480. /* draw unselected text after selected text */
  25481. const char* begin = select_end_ptr;
  25482. const char* end = nk_str_get_const(&edit->string) +
  25483. nk_str_len_char(&edit->string);
  25484. NK_ASSERT(select_end_ptr);
  25485. nk_edit_draw_text(out, style,
  25486. area.x - edit->scrollbar.x,
  25487. area.y + selection_offset_end.y - edit->scrollbar.y,
  25488. selection_offset_end.x,
  25489. begin, (int)(end - begin), row_height, font,
  25490. background_color, text_color, nk_true);
  25491. }
  25492. }
  25493. /* cursor */
  25494. if (edit->select_start == edit->select_end)
  25495. {
  25496. if (edit->cursor >= nk_str_len(&edit->string) ||
  25497. (cursor_ptr && *cursor_ptr == '\n')) {
  25498. /* draw cursor at end of line */
  25499. struct nk_rect cursor;
  25500. cursor.w = style->cursor_size;
  25501. cursor.h = font->height;
  25502. cursor.x = area.x + cursor_pos.x - edit->scrollbar.x;
  25503. cursor.y = area.y + cursor_pos.y + row_height / 2.0f - cursor.h / 2.0f;
  25504. cursor.y -= edit->scrollbar.y;
  25505. nk_fill_rect(out, cursor, 0, cursor_color);
  25506. }
  25507. else {
  25508. /* draw cursor inside text */
  25509. int glyph_len;
  25510. struct nk_rect label;
  25511. struct nk_text txt;
  25512. nk_rune unicode;
  25513. NK_ASSERT(cursor_ptr);
  25514. glyph_len = nk_utf_decode(cursor_ptr, &unicode, 4);
  25515. label.x = area.x + cursor_pos.x - edit->scrollbar.x;
  25516. label.y = area.y + cursor_pos.y - edit->scrollbar.y;
  25517. label.w = font->width(font->userdata, font->height, cursor_ptr, glyph_len);
  25518. label.h = row_height;
  25519. txt.padding = nk_vec2(0, 0);
  25520. txt.background = cursor_color;;
  25521. txt.text = cursor_text_color;
  25522. nk_fill_rect(out, label, 0, cursor_color);
  25523. nk_widget_text(out, label, cursor_ptr, glyph_len, &txt, NK_TEXT_LEFT, font);
  25524. }
  25525. }}
  25526. }
  25527. else {
  25528. /* not active so just draw text */
  25529. int l = nk_str_len_char(&edit->string);
  25530. const char* begin = nk_str_get_const(&edit->string);
  25531. const struct nk_style_item* background;
  25532. struct nk_color background_color;
  25533. struct nk_color text_color;
  25534. nk_push_scissor(out, clip);
  25535. if (*state & NK_WIDGET_STATE_ACTIVED) {
  25536. background = &style->active;
  25537. text_color = style->text_active;
  25538. }
  25539. else if (*state & NK_WIDGET_STATE_HOVER) {
  25540. background = &style->hover;
  25541. text_color = style->text_hover;
  25542. }
  25543. else {
  25544. background = &style->normal;
  25545. text_color = style->text_normal;
  25546. }
  25547. if (background->type == NK_STYLE_ITEM_IMAGE)
  25548. background_color = nk_rgba(0, 0, 0, 0);
  25549. else
  25550. background_color = background->data.color;
  25551. nk_edit_draw_text(out, style, area.x - edit->scrollbar.x,
  25552. area.y - edit->scrollbar.y, 0, begin, l, row_height, font,
  25553. background_color, text_color, nk_false);
  25554. }
  25555. nk_push_scissor(out, old_clip); }
  25556. return ret;
  25557. }
  25558. NK_API void
  25559. nk_edit_focus(struct nk_context* ctx, nk_flags flags)
  25560. {
  25561. nk_hash hash;
  25562. struct nk_window* win;
  25563. NK_ASSERT(ctx);
  25564. NK_ASSERT(ctx->current);
  25565. if (!ctx || !ctx->current) return;
  25566. win = ctx->current;
  25567. hash = win->edit.seq;
  25568. win->edit.active = nk_true;
  25569. win->edit.name = hash;
  25570. if (flags & NK_EDIT_ALWAYS_INSERT_MODE)
  25571. win->edit.mode = NK_TEXT_EDIT_MODE_INSERT;
  25572. }
  25573. NK_API void
  25574. nk_edit_unfocus(struct nk_context* ctx)
  25575. {
  25576. struct nk_window* win;
  25577. NK_ASSERT(ctx);
  25578. NK_ASSERT(ctx->current);
  25579. if (!ctx || !ctx->current) return;
  25580. win = ctx->current;
  25581. win->edit.active = nk_false;
  25582. win->edit.name = 0;
  25583. }
  25584. NK_API nk_flags
  25585. nk_edit_string(struct nk_context* ctx, nk_flags flags,
  25586. char* memory, int* len, int max, nk_plugin_filter filter)
  25587. {
  25588. nk_hash hash;
  25589. nk_flags state;
  25590. struct nk_text_edit* edit;
  25591. struct nk_window* win;
  25592. NK_ASSERT(ctx);
  25593. NK_ASSERT(memory);
  25594. NK_ASSERT(len);
  25595. if (!ctx || !memory || !len)
  25596. return 0;
  25597. filter = (!filter) ? nk_filter_default : filter;
  25598. win = ctx->current;
  25599. hash = win->edit.seq;
  25600. edit = &ctx->text_edit;
  25601. nk_textedit_clear_state(&ctx->text_edit, (flags & NK_EDIT_MULTILINE) ?
  25602. NK_TEXT_EDIT_MULTI_LINE : NK_TEXT_EDIT_SINGLE_LINE, filter);
  25603. if (win->edit.active && hash == win->edit.name) {
  25604. if (flags & NK_EDIT_NO_CURSOR)
  25605. edit->cursor = nk_utf_len(memory, *len);
  25606. else edit->cursor = win->edit.cursor;
  25607. if (!(flags & NK_EDIT_SELECTABLE)) {
  25608. edit->select_start = win->edit.cursor;
  25609. edit->select_end = win->edit.cursor;
  25610. }
  25611. else {
  25612. edit->select_start = win->edit.sel_start;
  25613. edit->select_end = win->edit.sel_end;
  25614. }
  25615. edit->mode = win->edit.mode;
  25616. edit->scrollbar.x = (float)win->edit.scrollbar.x;
  25617. edit->scrollbar.y = (float)win->edit.scrollbar.y;
  25618. edit->active = nk_true;
  25619. }
  25620. else edit->active = nk_false;
  25621. max = NK_MAX(1, max);
  25622. *len = NK_MIN(*len, max - 1);
  25623. nk_str_init_fixed(&edit->string, memory, (nk_size)max);
  25624. edit->string.buffer.allocated = (nk_size)*len;
  25625. edit->string.len = nk_utf_len(memory, *len);
  25626. state = nk_edit_buffer(ctx, flags, edit, filter);
  25627. *len = (int)edit->string.buffer.allocated;
  25628. if (edit->active) {
  25629. win->edit.cursor = edit->cursor;
  25630. win->edit.sel_start = edit->select_start;
  25631. win->edit.sel_end = edit->select_end;
  25632. win->edit.mode = edit->mode;
  25633. win->edit.scrollbar.x = (nk_uint)edit->scrollbar.x;
  25634. win->edit.scrollbar.y = (nk_uint)edit->scrollbar.y;
  25635. } return state;
  25636. }
  25637. NK_API nk_flags
  25638. nk_edit_buffer(struct nk_context* ctx, nk_flags flags,
  25639. struct nk_text_edit* edit, nk_plugin_filter filter)
  25640. {
  25641. struct nk_window* win;
  25642. struct nk_style* style;
  25643. struct nk_input* in;
  25644. enum nk_widget_layout_states state;
  25645. struct nk_rect bounds;
  25646. nk_flags ret_flags = 0;
  25647. unsigned char prev_state;
  25648. nk_hash hash;
  25649. /* make sure correct values */
  25650. NK_ASSERT(ctx);
  25651. NK_ASSERT(edit);
  25652. NK_ASSERT(ctx->current);
  25653. NK_ASSERT(ctx->current->layout);
  25654. if (!ctx || !ctx->current || !ctx->current->layout)
  25655. return 0;
  25656. win = ctx->current;
  25657. style = &ctx->style;
  25658. state = nk_widget(&bounds, ctx);
  25659. if (!state) return state;
  25660. in = (win->layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  25661. /* check if edit is currently hot item */
  25662. hash = win->edit.seq++;
  25663. if (win->edit.active && hash == win->edit.name) {
  25664. if (flags & NK_EDIT_NO_CURSOR)
  25665. edit->cursor = edit->string.len;
  25666. if (!(flags & NK_EDIT_SELECTABLE)) {
  25667. edit->select_start = edit->cursor;
  25668. edit->select_end = edit->cursor;
  25669. }
  25670. if (flags & NK_EDIT_CLIPBOARD)
  25671. edit->clip = ctx->clip;
  25672. edit->active = (unsigned char)win->edit.active;
  25673. }
  25674. else edit->active = nk_false;
  25675. edit->mode = win->edit.mode;
  25676. filter = (!filter) ? nk_filter_default : filter;
  25677. prev_state = (unsigned char)edit->active;
  25678. in = (flags & NK_EDIT_READ_ONLY) ? 0 : in;
  25679. ret_flags = nk_do_edit(&ctx->last_widget_state, &win->buffer, bounds, flags,
  25680. filter, edit, &style->edit, in, style->font);
  25681. if (ctx->last_widget_state & NK_WIDGET_STATE_HOVER)
  25682. ctx->style.cursor_active = ctx->style.cursors[NK_CURSOR_TEXT];
  25683. if (edit->active && prev_state != edit->active) {
  25684. /* current edit is now hot */
  25685. win->edit.active = nk_true;
  25686. win->edit.name = hash;
  25687. }
  25688. else if (prev_state && !edit->active) {
  25689. /* current edit is now cold */
  25690. win->edit.active = nk_false;
  25691. } return ret_flags;
  25692. }
  25693. NK_API nk_flags
  25694. nk_edit_string_zero_terminated(struct nk_context* ctx, nk_flags flags,
  25695. char* buffer, int max, nk_plugin_filter filter)
  25696. {
  25697. nk_flags result;
  25698. int len = nk_strlen(buffer);
  25699. result = nk_edit_string(ctx, flags, buffer, &len, max, filter);
  25700. buffer[NK_MIN(NK_MAX(max - 1, 0), len)] = '\0';
  25701. return result;
  25702. }
  25703. /* ===============================================================
  25704. *
  25705. * PROPERTY
  25706. *
  25707. * ===============================================================*/
  25708. NK_LIB void
  25709. nk_drag_behavior(nk_flags* state, const struct nk_input* in,
  25710. struct nk_rect drag, struct nk_property_variant* variant,
  25711. float inc_per_pixel)
  25712. {
  25713. int left_mouse_down = in && in->mouse.buttons[NK_BUTTON_LEFT].down;
  25714. int left_mouse_click_in_cursor = in &&
  25715. nk_input_has_mouse_click_down_in_rect(in, NK_BUTTON_LEFT, drag, nk_true);
  25716. nk_widget_state_reset(state);
  25717. if (nk_input_is_mouse_hovering_rect(in, drag))
  25718. *state = NK_WIDGET_STATE_HOVERED;
  25719. if (left_mouse_down && left_mouse_click_in_cursor) {
  25720. float delta, pixels;
  25721. pixels = in->mouse.delta.x;
  25722. delta = pixels * inc_per_pixel;
  25723. switch (variant->kind) {
  25724. default: break;
  25725. case NK_PROPERTY_INT:
  25726. variant->value.i = variant->value.i + (int)delta;
  25727. variant->value.i = NK_CLAMP(variant->min_value.i, variant->value.i, variant->max_value.i);
  25728. break;
  25729. case NK_PROPERTY_FLOAT:
  25730. variant->value.f = variant->value.f + (float)delta;
  25731. variant->value.f = NK_CLAMP(variant->min_value.f, variant->value.f, variant->max_value.f);
  25732. break;
  25733. case NK_PROPERTY_DOUBLE:
  25734. variant->value.d = variant->value.d + (double)delta;
  25735. variant->value.d = NK_CLAMP(variant->min_value.d, variant->value.d, variant->max_value.d);
  25736. break;
  25737. }
  25738. *state = NK_WIDGET_STATE_ACTIVE;
  25739. }
  25740. if (*state & NK_WIDGET_STATE_HOVER && !nk_input_is_mouse_prev_hovering_rect(in, drag))
  25741. *state |= NK_WIDGET_STATE_ENTERED;
  25742. else if (nk_input_is_mouse_prev_hovering_rect(in, drag))
  25743. *state |= NK_WIDGET_STATE_LEFT;
  25744. }
  25745. NK_LIB void
  25746. nk_property_behavior(nk_flags* ws, const struct nk_input* in,
  25747. struct nk_rect property, struct nk_rect label, struct nk_rect edit,
  25748. struct nk_rect empty, int* state, struct nk_property_variant* variant,
  25749. float inc_per_pixel)
  25750. {
  25751. nk_widget_state_reset(ws);
  25752. if (in && *state == NK_PROPERTY_DEFAULT) {
  25753. if (nk_button_behavior(ws, edit, in, NK_BUTTON_DEFAULT))
  25754. *state = NK_PROPERTY_EDIT;
  25755. else if (nk_input_is_mouse_click_down_in_rect(in, NK_BUTTON_LEFT, label, nk_true))
  25756. *state = NK_PROPERTY_DRAG;
  25757. else if (nk_input_is_mouse_click_down_in_rect(in, NK_BUTTON_LEFT, empty, nk_true))
  25758. *state = NK_PROPERTY_DRAG;
  25759. }
  25760. if (*state == NK_PROPERTY_DRAG) {
  25761. nk_drag_behavior(ws, in, property, variant, inc_per_pixel);
  25762. if (!(*ws & NK_WIDGET_STATE_ACTIVED)) *state = NK_PROPERTY_DEFAULT;
  25763. }
  25764. }
  25765. NK_LIB void
  25766. nk_draw_property(struct nk_command_buffer* out, const struct nk_style_property* style,
  25767. const struct nk_rect* bounds, const struct nk_rect* label, nk_flags state,
  25768. const char* name, int len, const struct nk_user_font* font)
  25769. {
  25770. struct nk_text text;
  25771. const struct nk_style_item* background;
  25772. /* select correct background and text color */
  25773. if (state & NK_WIDGET_STATE_ACTIVED) {
  25774. background = &style->active;
  25775. text.text = style->label_active;
  25776. }
  25777. else if (state & NK_WIDGET_STATE_HOVER) {
  25778. background = &style->hover;
  25779. text.text = style->label_hover;
  25780. }
  25781. else {
  25782. background = &style->normal;
  25783. text.text = style->label_normal;
  25784. }
  25785. /* draw background */
  25786. switch (background->type) {
  25787. case NK_STYLE_ITEM_IMAGE:
  25788. text.background = nk_rgba(0, 0, 0, 0);
  25789. nk_draw_image(out, *bounds, &background->data.image, nk_white);
  25790. break;
  25791. case NK_STYLE_ITEM_NINE_SLICE:
  25792. text.background = nk_rgba(0, 0, 0, 0);
  25793. nk_draw_nine_slice(out, *bounds, &background->data.slice, nk_white);
  25794. break;
  25795. case NK_STYLE_ITEM_COLOR:
  25796. text.background = background->data.color;
  25797. nk_fill_rect(out, *bounds, style->rounding, background->data.color);
  25798. nk_stroke_rect(out, *bounds, style->rounding, style->border, background->data.color);
  25799. break;
  25800. }
  25801. /* draw label */
  25802. text.padding = nk_vec2(0, 0);
  25803. nk_widget_text(out, *label, name, len, &text, NK_TEXT_CENTERED, font);
  25804. }
  25805. NK_LIB void
  25806. nk_do_property(nk_flags* ws,
  25807. struct nk_command_buffer* out, struct nk_rect property,
  25808. const char* name, struct nk_property_variant* variant,
  25809. float inc_per_pixel, char* buffer, int* len,
  25810. int* state, int* cursor, int* select_begin, int* select_end,
  25811. const struct nk_style_property* style,
  25812. enum nk_property_filter filter, struct nk_input* in,
  25813. const struct nk_user_font* font, struct nk_text_edit* text_edit,
  25814. enum nk_button_behavior behavior)
  25815. {
  25816. const nk_plugin_filter filters[] = {
  25817. nk_filter_decimal,
  25818. nk_filter_float
  25819. };
  25820. nk_bool active, old;
  25821. int num_len = 0, name_len;
  25822. char string[NK_MAX_NUMBER_BUFFER];
  25823. float size;
  25824. char* dst = 0;
  25825. int* length;
  25826. struct nk_rect left;
  25827. struct nk_rect right;
  25828. struct nk_rect label;
  25829. struct nk_rect edit;
  25830. struct nk_rect empty;
  25831. /* left decrement button */
  25832. left.h = font->height / 2;
  25833. left.w = left.h;
  25834. left.x = property.x + style->border + style->padding.x;
  25835. left.y = property.y + style->border + property.h / 2.0f - left.h / 2;
  25836. /* text label */
  25837. name_len = nk_strlen(name);
  25838. size = font->width(font->userdata, font->height, name, name_len);
  25839. label.x = left.x + left.w + style->padding.x;
  25840. label.w = (float)size + 2 * style->padding.x;
  25841. label.y = property.y + style->border + style->padding.y;
  25842. label.h = property.h - (2 * style->border + 2 * style->padding.y);
  25843. /* right increment button */
  25844. right.y = left.y;
  25845. right.w = left.w;
  25846. right.h = left.h;
  25847. right.x = property.x + property.w - (right.w + style->padding.x);
  25848. /* edit */
  25849. if (*state == NK_PROPERTY_EDIT) {
  25850. size = font->width(font->userdata, font->height, buffer, *len);
  25851. size += style->edit.cursor_size;
  25852. length = len;
  25853. dst = buffer;
  25854. }
  25855. else {
  25856. switch (variant->kind) {
  25857. default: break;
  25858. case NK_PROPERTY_INT:
  25859. nk_itoa(string, variant->value.i);
  25860. num_len = nk_strlen(string);
  25861. break;
  25862. case NK_PROPERTY_FLOAT:
  25863. NK_DTOA(string, (double)variant->value.f);
  25864. num_len = nk_string_float_limit(string, NK_MAX_FLOAT_PRECISION);
  25865. break;
  25866. case NK_PROPERTY_DOUBLE:
  25867. NK_DTOA(string, variant->value.d);
  25868. num_len = nk_string_float_limit(string, NK_MAX_FLOAT_PRECISION);
  25869. break;
  25870. }
  25871. size = font->width(font->userdata, font->height, string, num_len);
  25872. dst = string;
  25873. length = &num_len;
  25874. }
  25875. edit.w = (float)size + 2 * style->padding.x;
  25876. edit.w = NK_MIN(edit.w, right.x - (label.x + label.w));
  25877. edit.x = right.x - (edit.w + style->padding.x);
  25878. edit.y = property.y + style->border;
  25879. edit.h = property.h - (2 * style->border);
  25880. /* empty left space activator */
  25881. empty.w = edit.x - (label.x + label.w);
  25882. empty.x = label.x + label.w;
  25883. empty.y = property.y;
  25884. empty.h = property.h;
  25885. /* update property */
  25886. old = (*state == NK_PROPERTY_EDIT);
  25887. nk_property_behavior(ws, in, property, label, edit, empty, state, variant, inc_per_pixel);
  25888. /* draw property */
  25889. if (style->draw_begin) style->draw_begin(out, style->userdata);
  25890. nk_draw_property(out, style, &property, &label, *ws, name, name_len, font);
  25891. if (style->draw_end) style->draw_end(out, style->userdata);
  25892. /* execute right button */
  25893. if (nk_do_button_symbol(ws, out, left, style->sym_left, behavior, &style->dec_button, in, font)) {
  25894. switch (variant->kind) {
  25895. default: break;
  25896. case NK_PROPERTY_INT:
  25897. variant->value.i = NK_CLAMP(variant->min_value.i, variant->value.i - variant->step.i, variant->max_value.i); break;
  25898. case NK_PROPERTY_FLOAT:
  25899. variant->value.f = NK_CLAMP(variant->min_value.f, variant->value.f - variant->step.f, variant->max_value.f); break;
  25900. case NK_PROPERTY_DOUBLE:
  25901. variant->value.d = NK_CLAMP(variant->min_value.d, variant->value.d - variant->step.d, variant->max_value.d); break;
  25902. }
  25903. }
  25904. /* execute left button */
  25905. if (nk_do_button_symbol(ws, out, right, style->sym_right, behavior, &style->inc_button, in, font)) {
  25906. switch (variant->kind) {
  25907. default: break;
  25908. case NK_PROPERTY_INT:
  25909. variant->value.i = NK_CLAMP(variant->min_value.i, variant->value.i + variant->step.i, variant->max_value.i); break;
  25910. case NK_PROPERTY_FLOAT:
  25911. variant->value.f = NK_CLAMP(variant->min_value.f, variant->value.f + variant->step.f, variant->max_value.f); break;
  25912. case NK_PROPERTY_DOUBLE:
  25913. variant->value.d = NK_CLAMP(variant->min_value.d, variant->value.d + variant->step.d, variant->max_value.d); break;
  25914. }
  25915. }
  25916. if (old != NK_PROPERTY_EDIT && (*state == NK_PROPERTY_EDIT)) {
  25917. /* property has been activated so setup buffer */
  25918. NK_MEMCPY(buffer, dst, (nk_size)*length);
  25919. *cursor = nk_utf_len(buffer, *length);
  25920. *len = *length;
  25921. length = len;
  25922. dst = buffer;
  25923. active = 0;
  25924. }
  25925. else active = (*state == NK_PROPERTY_EDIT);
  25926. /* execute and run text edit field */
  25927. nk_textedit_clear_state(text_edit, NK_TEXT_EDIT_SINGLE_LINE, filters[filter]);
  25928. text_edit->active = (unsigned char)active;
  25929. text_edit->string.len = *length;
  25930. text_edit->cursor = NK_CLAMP(0, *cursor, *length);
  25931. text_edit->select_start = NK_CLAMP(0, *select_begin, *length);
  25932. text_edit->select_end = NK_CLAMP(0, *select_end, *length);
  25933. text_edit->string.buffer.allocated = (nk_size)*length;
  25934. text_edit->string.buffer.memory.size = NK_MAX_NUMBER_BUFFER;
  25935. text_edit->string.buffer.memory.ptr = dst;
  25936. text_edit->string.buffer.size = NK_MAX_NUMBER_BUFFER;
  25937. text_edit->mode = NK_TEXT_EDIT_MODE_INSERT;
  25938. nk_do_edit(ws, out, edit, NK_EDIT_FIELD | NK_EDIT_AUTO_SELECT,
  25939. filters[filter], text_edit, &style->edit, (*state == NK_PROPERTY_EDIT) ? in : 0, font);
  25940. *length = text_edit->string.len;
  25941. *cursor = text_edit->cursor;
  25942. *select_begin = text_edit->select_start;
  25943. *select_end = text_edit->select_end;
  25944. if (text_edit->active && nk_input_is_key_pressed(in, NK_KEY_ENTER))
  25945. text_edit->active = nk_false;
  25946. if (active && !text_edit->active) {
  25947. /* property is now not active so convert edit text to value*/
  25948. *state = NK_PROPERTY_DEFAULT;
  25949. buffer[*len] = '\0';
  25950. switch (variant->kind) {
  25951. default: break;
  25952. case NK_PROPERTY_INT:
  25953. variant->value.i = nk_strtoi(buffer, 0);
  25954. variant->value.i = NK_CLAMP(variant->min_value.i, variant->value.i, variant->max_value.i);
  25955. break;
  25956. case NK_PROPERTY_FLOAT:
  25957. nk_string_float_limit(buffer, NK_MAX_FLOAT_PRECISION);
  25958. variant->value.f = nk_strtof(buffer, 0);
  25959. variant->value.f = NK_CLAMP(variant->min_value.f, variant->value.f, variant->max_value.f);
  25960. break;
  25961. case NK_PROPERTY_DOUBLE:
  25962. nk_string_float_limit(buffer, NK_MAX_FLOAT_PRECISION);
  25963. variant->value.d = nk_strtod(buffer, 0);
  25964. variant->value.d = NK_CLAMP(variant->min_value.d, variant->value.d, variant->max_value.d);
  25965. break;
  25966. }
  25967. }
  25968. }
  25969. NK_LIB struct nk_property_variant
  25970. nk_property_variant_int(int value, int min_value, int max_value, int step)
  25971. {
  25972. struct nk_property_variant result;
  25973. result.kind = NK_PROPERTY_INT;
  25974. result.value.i = value;
  25975. result.min_value.i = min_value;
  25976. result.max_value.i = max_value;
  25977. result.step.i = step;
  25978. return result;
  25979. }
  25980. NK_LIB struct nk_property_variant
  25981. nk_property_variant_float(float value, float min_value, float max_value, float step)
  25982. {
  25983. struct nk_property_variant result;
  25984. result.kind = NK_PROPERTY_FLOAT;
  25985. result.value.f = value;
  25986. result.min_value.f = min_value;
  25987. result.max_value.f = max_value;
  25988. result.step.f = step;
  25989. return result;
  25990. }
  25991. NK_LIB struct nk_property_variant
  25992. nk_property_variant_double(double value, double min_value, double max_value,
  25993. double step)
  25994. {
  25995. struct nk_property_variant result;
  25996. result.kind = NK_PROPERTY_DOUBLE;
  25997. result.value.d = value;
  25998. result.min_value.d = min_value;
  25999. result.max_value.d = max_value;
  26000. result.step.d = step;
  26001. return result;
  26002. }
  26003. NK_LIB void
  26004. nk_property(struct nk_context* ctx, const char* name, struct nk_property_variant* variant,
  26005. float inc_per_pixel, const enum nk_property_filter filter)
  26006. {
  26007. struct nk_window* win;
  26008. struct nk_panel* layout;
  26009. struct nk_input* in;
  26010. const struct nk_style* style;
  26011. struct nk_rect bounds;
  26012. enum nk_widget_layout_states s;
  26013. int* state = 0;
  26014. nk_hash hash = 0;
  26015. char* buffer = 0;
  26016. int* len = 0;
  26017. int* cursor = 0;
  26018. int* select_begin = 0;
  26019. int* select_end = 0;
  26020. int old_state;
  26021. char dummy_buffer[NK_MAX_NUMBER_BUFFER];
  26022. int dummy_state = NK_PROPERTY_DEFAULT;
  26023. int dummy_length = 0;
  26024. int dummy_cursor = 0;
  26025. int dummy_select_begin = 0;
  26026. int dummy_select_end = 0;
  26027. NK_ASSERT(ctx);
  26028. NK_ASSERT(ctx->current);
  26029. NK_ASSERT(ctx->current->layout);
  26030. if (!ctx || !ctx->current || !ctx->current->layout)
  26031. return;
  26032. win = ctx->current;
  26033. layout = win->layout;
  26034. style = &ctx->style;
  26035. s = nk_widget(&bounds, ctx);
  26036. if (!s) return;
  26037. /* calculate hash from name */
  26038. if (name[0] == '#') {
  26039. hash = nk_murmur_hash(name, (int)nk_strlen(name), win->property.seq++);
  26040. name++; /* special number hash */
  26041. }
  26042. else hash = nk_murmur_hash(name, (int)nk_strlen(name), 42);
  26043. /* check if property is currently hot item */
  26044. if (win->property.active && hash == win->property.name) {
  26045. buffer = win->property.buffer;
  26046. len = &win->property.length;
  26047. cursor = &win->property.cursor;
  26048. state = &win->property.state;
  26049. select_begin = &win->property.select_start;
  26050. select_end = &win->property.select_end;
  26051. }
  26052. else {
  26053. buffer = dummy_buffer;
  26054. len = &dummy_length;
  26055. cursor = &dummy_cursor;
  26056. state = &dummy_state;
  26057. select_begin = &dummy_select_begin;
  26058. select_end = &dummy_select_end;
  26059. }
  26060. /* execute property widget */
  26061. old_state = *state;
  26062. ctx->text_edit.clip = ctx->clip;
  26063. in = ((s == NK_WIDGET_ROM && !win->property.active) ||
  26064. layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  26065. nk_do_property(&ctx->last_widget_state, &win->buffer, bounds, name,
  26066. variant, inc_per_pixel, buffer, len, state, cursor, select_begin,
  26067. select_end, &style->property, filter, in, style->font, &ctx->text_edit,
  26068. ctx->button_behavior);
  26069. if (in && *state != NK_PROPERTY_DEFAULT && !win->property.active) {
  26070. /* current property is now hot */
  26071. win->property.active = 1;
  26072. NK_MEMCPY(win->property.buffer, buffer, (nk_size)*len);
  26073. win->property.length = *len;
  26074. win->property.cursor = *cursor;
  26075. win->property.state = *state;
  26076. win->property.name = hash;
  26077. win->property.select_start = *select_begin;
  26078. win->property.select_end = *select_end;
  26079. if (*state == NK_PROPERTY_DRAG) {
  26080. ctx->input.mouse.grab = nk_true;
  26081. ctx->input.mouse.grabbed = nk_true;
  26082. }
  26083. }
  26084. /* check if previously active property is now inactive */
  26085. if (*state == NK_PROPERTY_DEFAULT && old_state != NK_PROPERTY_DEFAULT) {
  26086. if (old_state == NK_PROPERTY_DRAG) {
  26087. ctx->input.mouse.grab = nk_false;
  26088. ctx->input.mouse.grabbed = nk_false;
  26089. ctx->input.mouse.ungrab = nk_true;
  26090. }
  26091. win->property.select_start = 0;
  26092. win->property.select_end = 0;
  26093. win->property.active = 0;
  26094. }
  26095. }
  26096. NK_API void
  26097. nk_property_int(struct nk_context* ctx, const char* name,
  26098. int min, int* val, int max, int step, float inc_per_pixel)
  26099. {
  26100. struct nk_property_variant variant;
  26101. NK_ASSERT(ctx);
  26102. NK_ASSERT(name);
  26103. NK_ASSERT(val);
  26104. if (!ctx || !ctx->current || !name || !val) return;
  26105. variant = nk_property_variant_int(*val, min, max, step);
  26106. nk_property(ctx, name, &variant, inc_per_pixel, NK_FILTER_INT);
  26107. *val = variant.value.i;
  26108. }
  26109. NK_API void
  26110. nk_property_float(struct nk_context* ctx, const char* name,
  26111. float min, float* val, float max, float step, float inc_per_pixel)
  26112. {
  26113. struct nk_property_variant variant;
  26114. NK_ASSERT(ctx);
  26115. NK_ASSERT(name);
  26116. NK_ASSERT(val);
  26117. if (!ctx || !ctx->current || !name || !val) return;
  26118. variant = nk_property_variant_float(*val, min, max, step);
  26119. nk_property(ctx, name, &variant, inc_per_pixel, NK_FILTER_FLOAT);
  26120. *val = variant.value.f;
  26121. }
  26122. NK_API void
  26123. nk_property_double(struct nk_context* ctx, const char* name,
  26124. double min, double* val, double max, double step, float inc_per_pixel)
  26125. {
  26126. struct nk_property_variant variant;
  26127. NK_ASSERT(ctx);
  26128. NK_ASSERT(name);
  26129. NK_ASSERT(val);
  26130. if (!ctx || !ctx->current || !name || !val) return;
  26131. variant = nk_property_variant_double(*val, min, max, step);
  26132. nk_property(ctx, name, &variant, inc_per_pixel, NK_FILTER_FLOAT);
  26133. *val = variant.value.d;
  26134. }
  26135. NK_API int
  26136. nk_propertyi(struct nk_context* ctx, const char* name, int min, int val,
  26137. int max, int step, float inc_per_pixel)
  26138. {
  26139. struct nk_property_variant variant;
  26140. NK_ASSERT(ctx);
  26141. NK_ASSERT(name);
  26142. if (!ctx || !ctx->current || !name) return val;
  26143. variant = nk_property_variant_int(val, min, max, step);
  26144. nk_property(ctx, name, &variant, inc_per_pixel, NK_FILTER_INT);
  26145. val = variant.value.i;
  26146. return val;
  26147. }
  26148. NK_API float
  26149. nk_propertyf(struct nk_context* ctx, const char* name, float min,
  26150. float val, float max, float step, float inc_per_pixel)
  26151. {
  26152. struct nk_property_variant variant;
  26153. NK_ASSERT(ctx);
  26154. NK_ASSERT(name);
  26155. if (!ctx || !ctx->current || !name) return val;
  26156. variant = nk_property_variant_float(val, min, max, step);
  26157. nk_property(ctx, name, &variant, inc_per_pixel, NK_FILTER_FLOAT);
  26158. val = variant.value.f;
  26159. return val;
  26160. }
  26161. NK_API double
  26162. nk_propertyd(struct nk_context* ctx, const char* name, double min,
  26163. double val, double max, double step, float inc_per_pixel)
  26164. {
  26165. struct nk_property_variant variant;
  26166. NK_ASSERT(ctx);
  26167. NK_ASSERT(name);
  26168. if (!ctx || !ctx->current || !name) return val;
  26169. variant = nk_property_variant_double(val, min, max, step);
  26170. nk_property(ctx, name, &variant, inc_per_pixel, NK_FILTER_FLOAT);
  26171. val = variant.value.d;
  26172. return val;
  26173. }
  26174. /* ==============================================================
  26175. *
  26176. * CHART
  26177. *
  26178. * ===============================================================*/
  26179. NK_API nk_bool
  26180. nk_chart_begin_colored(struct nk_context* ctx, enum nk_chart_type type,
  26181. struct nk_color color, struct nk_color highlight,
  26182. int count, float min_value, float max_value)
  26183. {
  26184. struct nk_window* win;
  26185. struct nk_chart* chart;
  26186. const struct nk_style* config;
  26187. const struct nk_style_chart* style;
  26188. const struct nk_style_item* background;
  26189. struct nk_rect bounds = { 0, 0, 0, 0 };
  26190. NK_ASSERT(ctx);
  26191. NK_ASSERT(ctx->current);
  26192. NK_ASSERT(ctx->current->layout);
  26193. if (!ctx || !ctx->current || !ctx->current->layout) return 0;
  26194. if (!nk_widget(&bounds, ctx)) {
  26195. chart = &ctx->current->layout->chart;
  26196. nk_zero(chart, sizeof(*chart));
  26197. return 0;
  26198. }
  26199. win = ctx->current;
  26200. config = &ctx->style;
  26201. chart = &win->layout->chart;
  26202. style = &config->chart;
  26203. /* setup basic generic chart */
  26204. nk_zero(chart, sizeof(*chart));
  26205. chart->x = bounds.x + style->padding.x;
  26206. chart->y = bounds.y + style->padding.y;
  26207. chart->w = bounds.w - 2 * style->padding.x;
  26208. chart->h = bounds.h - 2 * style->padding.y;
  26209. chart->w = NK_MAX(chart->w, 2 * style->padding.x);
  26210. chart->h = NK_MAX(chart->h, 2 * style->padding.y);
  26211. /* add first slot into chart */
  26212. {struct nk_chart_slot* slot = &chart->slots[chart->slot++];
  26213. slot->type = type;
  26214. slot->count = count;
  26215. slot->color = color;
  26216. slot->highlight = highlight;
  26217. slot->min = NK_MIN(min_value, max_value);
  26218. slot->max = NK_MAX(min_value, max_value);
  26219. slot->range = slot->max - slot->min; }
  26220. /* draw chart background */
  26221. background = &style->background;
  26222. switch (background->type) {
  26223. case NK_STYLE_ITEM_IMAGE:
  26224. nk_draw_image(&win->buffer, bounds, &background->data.image, nk_white);
  26225. break;
  26226. case NK_STYLE_ITEM_NINE_SLICE:
  26227. nk_draw_nine_slice(&win->buffer, bounds, &background->data.slice, nk_white);
  26228. break;
  26229. case NK_STYLE_ITEM_COLOR:
  26230. nk_fill_rect(&win->buffer, bounds, style->rounding, style->border_color);
  26231. nk_fill_rect(&win->buffer, nk_shrink_rect(bounds, style->border),
  26232. style->rounding, style->background.data.color);
  26233. break;
  26234. }
  26235. return 1;
  26236. }
  26237. NK_API nk_bool
  26238. nk_chart_begin(struct nk_context* ctx, const enum nk_chart_type type,
  26239. int count, float min_value, float max_value)
  26240. {
  26241. return nk_chart_begin_colored(ctx, type, ctx->style.chart.color,
  26242. ctx->style.chart.selected_color, count, min_value, max_value);
  26243. }
  26244. NK_API void
  26245. nk_chart_add_slot_colored(struct nk_context* ctx, const enum nk_chart_type type,
  26246. struct nk_color color, struct nk_color highlight,
  26247. int count, float min_value, float max_value)
  26248. {
  26249. NK_ASSERT(ctx);
  26250. NK_ASSERT(ctx->current);
  26251. NK_ASSERT(ctx->current->layout);
  26252. NK_ASSERT(ctx->current->layout->chart.slot < NK_CHART_MAX_SLOT);
  26253. if (!ctx || !ctx->current || !ctx->current->layout) return;
  26254. if (ctx->current->layout->chart.slot >= NK_CHART_MAX_SLOT) return;
  26255. /* add another slot into the graph */
  26256. {struct nk_chart* chart = &ctx->current->layout->chart;
  26257. struct nk_chart_slot* slot = &chart->slots[chart->slot++];
  26258. slot->type = type;
  26259. slot->count = count;
  26260. slot->color = color;
  26261. slot->highlight = highlight;
  26262. slot->min = NK_MIN(min_value, max_value);
  26263. slot->max = NK_MAX(min_value, max_value);
  26264. slot->range = slot->max - slot->min; }
  26265. }
  26266. NK_API void
  26267. nk_chart_add_slot(struct nk_context* ctx, const enum nk_chart_type type,
  26268. int count, float min_value, float max_value)
  26269. {
  26270. nk_chart_add_slot_colored(ctx, type, ctx->style.chart.color,
  26271. ctx->style.chart.selected_color, count, min_value, max_value);
  26272. }
  26273. NK_INTERN nk_flags
  26274. nk_chart_push_line(struct nk_context* ctx, struct nk_window* win,
  26275. struct nk_chart* g, float value, int slot)
  26276. {
  26277. struct nk_panel* layout = win->layout;
  26278. const struct nk_input* i = &ctx->input;
  26279. struct nk_command_buffer* out = &win->buffer;
  26280. nk_flags ret = 0;
  26281. struct nk_vec2 cur;
  26282. struct nk_rect bounds;
  26283. struct nk_color color;
  26284. float step;
  26285. float range;
  26286. float ratio;
  26287. NK_ASSERT(slot >= 0 && slot < NK_CHART_MAX_SLOT);
  26288. step = g->w / (float)g->slots[slot].count;
  26289. range = g->slots[slot].max - g->slots[slot].min;
  26290. ratio = (value - g->slots[slot].min) / range;
  26291. if (g->slots[slot].index == 0) {
  26292. /* first data point does not have a connection */
  26293. g->slots[slot].last.x = g->x;
  26294. g->slots[slot].last.y = (g->y + g->h) - ratio * (float)g->h;
  26295. bounds.x = g->slots[slot].last.x - 2;
  26296. bounds.y = g->slots[slot].last.y - 2;
  26297. bounds.w = bounds.h = 4;
  26298. color = g->slots[slot].color;
  26299. if (!(layout->flags & NK_WINDOW_ROM) &&
  26300. NK_INBOX(i->mouse.pos.x, i->mouse.pos.y, g->slots[slot].last.x - 3, g->slots[slot].last.y - 3, 6, 6)) {
  26301. ret = nk_input_is_mouse_hovering_rect(i, bounds) ? NK_CHART_HOVERING : 0;
  26302. ret |= (i->mouse.buttons[NK_BUTTON_LEFT].down &&
  26303. i->mouse.buttons[NK_BUTTON_LEFT].clicked) ? NK_CHART_CLICKED : 0;
  26304. color = g->slots[slot].highlight;
  26305. }
  26306. nk_fill_rect(out, bounds, 0, color);
  26307. g->slots[slot].index += 1;
  26308. return ret;
  26309. }
  26310. /* draw a line between the last data point and the new one */
  26311. color = g->slots[slot].color;
  26312. cur.x = g->x + (float)(step * (float)g->slots[slot].index);
  26313. cur.y = (g->y + g->h) - (ratio * (float)g->h);
  26314. nk_stroke_line(out, g->slots[slot].last.x, g->slots[slot].last.y, cur.x, cur.y, 1.0f, color);
  26315. bounds.x = cur.x - 3;
  26316. bounds.y = cur.y - 3;
  26317. bounds.w = bounds.h = 6;
  26318. /* user selection of current data point */
  26319. if (!(layout->flags & NK_WINDOW_ROM)) {
  26320. if (nk_input_is_mouse_hovering_rect(i, bounds)) {
  26321. ret = NK_CHART_HOVERING;
  26322. ret |= (!i->mouse.buttons[NK_BUTTON_LEFT].down &&
  26323. i->mouse.buttons[NK_BUTTON_LEFT].clicked) ? NK_CHART_CLICKED : 0;
  26324. color = g->slots[slot].highlight;
  26325. }
  26326. }
  26327. nk_fill_rect(out, nk_rect(cur.x - 2, cur.y - 2, 4, 4), 0, color);
  26328. /* save current data point position */
  26329. g->slots[slot].last.x = cur.x;
  26330. g->slots[slot].last.y = cur.y;
  26331. g->slots[slot].index += 1;
  26332. return ret;
  26333. }
  26334. NK_INTERN nk_flags
  26335. nk_chart_push_column(const struct nk_context* ctx, struct nk_window* win,
  26336. struct nk_chart* chart, float value, int slot)
  26337. {
  26338. struct nk_command_buffer* out = &win->buffer;
  26339. const struct nk_input* in = &ctx->input;
  26340. struct nk_panel* layout = win->layout;
  26341. float ratio;
  26342. nk_flags ret = 0;
  26343. struct nk_color color;
  26344. struct nk_rect item = { 0,0,0,0 };
  26345. NK_ASSERT(slot >= 0 && slot < NK_CHART_MAX_SLOT);
  26346. if (chart->slots[slot].index >= chart->slots[slot].count)
  26347. return nk_false;
  26348. if (chart->slots[slot].count) {
  26349. float padding = (float)(chart->slots[slot].count - 1);
  26350. item.w = (chart->w - padding) / (float)(chart->slots[slot].count);
  26351. }
  26352. /* calculate bounds of current bar chart entry */
  26353. color = chart->slots[slot].color;;
  26354. item.h = chart->h * NK_ABS((value / chart->slots[slot].range));
  26355. if (value >= 0) {
  26356. ratio = (value + NK_ABS(chart->slots[slot].min)) / NK_ABS(chart->slots[slot].range);
  26357. item.y = (chart->y + chart->h) - chart->h * ratio;
  26358. }
  26359. else {
  26360. ratio = (value - chart->slots[slot].max) / chart->slots[slot].range;
  26361. item.y = chart->y + (chart->h * NK_ABS(ratio)) - item.h;
  26362. }
  26363. item.x = chart->x + ((float)chart->slots[slot].index * item.w);
  26364. item.x = item.x + ((float)chart->slots[slot].index);
  26365. /* user chart bar selection */
  26366. if (!(layout->flags & NK_WINDOW_ROM) &&
  26367. NK_INBOX(in->mouse.pos.x, in->mouse.pos.y, item.x, item.y, item.w, item.h)) {
  26368. ret = NK_CHART_HOVERING;
  26369. ret |= (!in->mouse.buttons[NK_BUTTON_LEFT].down &&
  26370. in->mouse.buttons[NK_BUTTON_LEFT].clicked) ? NK_CHART_CLICKED : 0;
  26371. color = chart->slots[slot].highlight;
  26372. }
  26373. nk_fill_rect(out, item, 0, color);
  26374. chart->slots[slot].index += 1;
  26375. return ret;
  26376. }
  26377. NK_API nk_flags
  26378. nk_chart_push_slot(struct nk_context* ctx, float value, int slot)
  26379. {
  26380. nk_flags flags;
  26381. struct nk_window* win;
  26382. NK_ASSERT(ctx);
  26383. NK_ASSERT(ctx->current);
  26384. NK_ASSERT(slot >= 0 && slot < NK_CHART_MAX_SLOT);
  26385. NK_ASSERT(slot < ctx->current->layout->chart.slot);
  26386. if (!ctx || !ctx->current || slot >= NK_CHART_MAX_SLOT) return nk_false;
  26387. if (slot >= ctx->current->layout->chart.slot) return nk_false;
  26388. win = ctx->current;
  26389. if (win->layout->chart.slot < slot) return nk_false;
  26390. switch (win->layout->chart.slots[slot].type) {
  26391. case NK_CHART_LINES:
  26392. flags = nk_chart_push_line(ctx, win, &win->layout->chart, value, slot); break;
  26393. case NK_CHART_COLUMN:
  26394. flags = nk_chart_push_column(ctx, win, &win->layout->chart, value, slot); break;
  26395. default:
  26396. case NK_CHART_MAX:
  26397. flags = 0;
  26398. }
  26399. return flags;
  26400. }
  26401. NK_API nk_flags
  26402. nk_chart_push(struct nk_context* ctx, float value)
  26403. {
  26404. return nk_chart_push_slot(ctx, value, 0);
  26405. }
  26406. NK_API void
  26407. nk_chart_end(struct nk_context* ctx)
  26408. {
  26409. struct nk_window* win;
  26410. struct nk_chart* chart;
  26411. NK_ASSERT(ctx);
  26412. NK_ASSERT(ctx->current);
  26413. if (!ctx || !ctx->current)
  26414. return;
  26415. win = ctx->current;
  26416. chart = &win->layout->chart;
  26417. NK_MEMSET(chart, 0, sizeof(*chart));
  26418. return;
  26419. }
  26420. NK_API void
  26421. nk_plot(struct nk_context* ctx, enum nk_chart_type type, const float* values,
  26422. int count, int offset)
  26423. {
  26424. int i = 0;
  26425. float min_value;
  26426. float max_value;
  26427. NK_ASSERT(ctx);
  26428. NK_ASSERT(values);
  26429. if (!ctx || !values || !count) return;
  26430. min_value = values[offset];
  26431. max_value = values[offset];
  26432. for (i = 0; i < count; ++i) {
  26433. min_value = NK_MIN(values[i + offset], min_value);
  26434. max_value = NK_MAX(values[i + offset], max_value);
  26435. }
  26436. if (nk_chart_begin(ctx, type, count, min_value, max_value)) {
  26437. for (i = 0; i < count; ++i)
  26438. nk_chart_push(ctx, values[i + offset]);
  26439. nk_chart_end(ctx);
  26440. }
  26441. }
  26442. NK_API void
  26443. nk_plot_function(struct nk_context* ctx, enum nk_chart_type type, void* userdata,
  26444. float(*value_getter)(void* user, int index), int count, int offset)
  26445. {
  26446. int i = 0;
  26447. float min_value;
  26448. float max_value;
  26449. NK_ASSERT(ctx);
  26450. NK_ASSERT(value_getter);
  26451. if (!ctx || !value_getter || !count) return;
  26452. max_value = min_value = value_getter(userdata, offset);
  26453. for (i = 0; i < count; ++i) {
  26454. float value = value_getter(userdata, i + offset);
  26455. min_value = NK_MIN(value, min_value);
  26456. max_value = NK_MAX(value, max_value);
  26457. }
  26458. if (nk_chart_begin(ctx, type, count, min_value, max_value)) {
  26459. for (i = 0; i < count; ++i)
  26460. nk_chart_push(ctx, value_getter(userdata, i + offset));
  26461. nk_chart_end(ctx);
  26462. }
  26463. }
  26464. /* ==============================================================
  26465. *
  26466. * COLOR PICKER
  26467. *
  26468. * ===============================================================*/
  26469. NK_LIB nk_bool
  26470. nk_color_picker_behavior(nk_flags* state,
  26471. const struct nk_rect* bounds, const struct nk_rect* matrix,
  26472. const struct nk_rect* hue_bar, const struct nk_rect* alpha_bar,
  26473. struct nk_colorf* color, const struct nk_input* in)
  26474. {
  26475. float hsva[4];
  26476. nk_bool value_changed = 0;
  26477. nk_bool hsv_changed = 0;
  26478. NK_ASSERT(state);
  26479. NK_ASSERT(matrix);
  26480. NK_ASSERT(hue_bar);
  26481. NK_ASSERT(color);
  26482. /* color matrix */
  26483. nk_colorf_hsva_fv(hsva, *color);
  26484. if (nk_button_behavior(state, *matrix, in, NK_BUTTON_REPEATER)) {
  26485. hsva[1] = NK_SATURATE((in->mouse.pos.x - matrix->x) / (matrix->w - 1));
  26486. hsva[2] = 1.0f - NK_SATURATE((in->mouse.pos.y - matrix->y) / (matrix->h - 1));
  26487. value_changed = hsv_changed = 1;
  26488. }
  26489. /* hue bar */
  26490. if (nk_button_behavior(state, *hue_bar, in, NK_BUTTON_REPEATER)) {
  26491. hsva[0] = NK_SATURATE((in->mouse.pos.y - hue_bar->y) / (hue_bar->h - 1));
  26492. value_changed = hsv_changed = 1;
  26493. }
  26494. /* alpha bar */
  26495. if (alpha_bar) {
  26496. if (nk_button_behavior(state, *alpha_bar, in, NK_BUTTON_REPEATER)) {
  26497. hsva[3] = 1.0f - NK_SATURATE((in->mouse.pos.y - alpha_bar->y) / (alpha_bar->h - 1));
  26498. value_changed = 1;
  26499. }
  26500. }
  26501. nk_widget_state_reset(state);
  26502. if (hsv_changed) {
  26503. *color = nk_hsva_colorfv(hsva);
  26504. *state = NK_WIDGET_STATE_ACTIVE;
  26505. }
  26506. if (value_changed) {
  26507. color->a = hsva[3];
  26508. *state = NK_WIDGET_STATE_ACTIVE;
  26509. }
  26510. /* set color picker widget state */
  26511. if (nk_input_is_mouse_hovering_rect(in, *bounds))
  26512. *state = NK_WIDGET_STATE_HOVERED;
  26513. if (*state & NK_WIDGET_STATE_HOVER && !nk_input_is_mouse_prev_hovering_rect(in, *bounds))
  26514. *state |= NK_WIDGET_STATE_ENTERED;
  26515. else if (nk_input_is_mouse_prev_hovering_rect(in, *bounds))
  26516. *state |= NK_WIDGET_STATE_LEFT;
  26517. return value_changed;
  26518. }
  26519. NK_LIB void
  26520. nk_draw_color_picker(struct nk_command_buffer* o, const struct nk_rect* matrix,
  26521. const struct nk_rect* hue_bar, const struct nk_rect* alpha_bar,
  26522. struct nk_colorf col)
  26523. {
  26524. NK_STORAGE const struct nk_color black = { 0,0,0,255 };
  26525. NK_STORAGE const struct nk_color white = { 255, 255, 255, 255 };
  26526. NK_STORAGE const struct nk_color black_trans = { 0,0,0,0 };
  26527. const float crosshair_size = 7.0f;
  26528. struct nk_color temp;
  26529. float hsva[4];
  26530. float line_y;
  26531. int i;
  26532. NK_ASSERT(o);
  26533. NK_ASSERT(matrix);
  26534. NK_ASSERT(hue_bar);
  26535. /* draw hue bar */
  26536. nk_colorf_hsva_fv(hsva, col);
  26537. for (i = 0; i < 6; ++i) {
  26538. NK_GLOBAL const struct nk_color hue_colors[] = {
  26539. {255, 0, 0, 255}, {255,255,0,255}, {0,255,0,255}, {0, 255,255,255},
  26540. {0,0,255,255}, {255, 0, 255, 255}, {255, 0, 0, 255}
  26541. };
  26542. nk_fill_rect_multi_color(o,
  26543. nk_rect(hue_bar->x, hue_bar->y + (float)i * (hue_bar->h / 6.0f) + 0.5f,
  26544. hue_bar->w, (hue_bar->h / 6.0f) + 0.5f), hue_colors[i], hue_colors[i],
  26545. hue_colors[i + 1], hue_colors[i + 1]);
  26546. }
  26547. line_y = (float)(int)(hue_bar->y + hsva[0] * matrix->h + 0.5f);
  26548. nk_stroke_line(o, hue_bar->x - 1, line_y, hue_bar->x + hue_bar->w + 2,
  26549. line_y, 1, nk_rgb(255, 255, 255));
  26550. /* draw alpha bar */
  26551. if (alpha_bar) {
  26552. float alpha = NK_SATURATE(col.a);
  26553. line_y = (float)(int)(alpha_bar->y + (1.0f - alpha) * matrix->h + 0.5f);
  26554. nk_fill_rect_multi_color(o, *alpha_bar, white, white, black, black);
  26555. nk_stroke_line(o, alpha_bar->x - 1, line_y, alpha_bar->x + alpha_bar->w + 2,
  26556. line_y, 1, nk_rgb(255, 255, 255));
  26557. }
  26558. /* draw color matrix */
  26559. temp = nk_hsv_f(hsva[0], 1.0f, 1.0f);
  26560. nk_fill_rect_multi_color(o, *matrix, white, temp, temp, white);
  26561. nk_fill_rect_multi_color(o, *matrix, black_trans, black_trans, black, black);
  26562. /* draw cross-hair */
  26563. {struct nk_vec2 p; float S = hsva[1]; float V = hsva[2];
  26564. p.x = (float)(int)(matrix->x + S * matrix->w);
  26565. p.y = (float)(int)(matrix->y + (1.0f - V) * matrix->h);
  26566. nk_stroke_line(o, p.x - crosshair_size, p.y, p.x - 2, p.y, 1.0f, white);
  26567. nk_stroke_line(o, p.x + crosshair_size + 1, p.y, p.x + 3, p.y, 1.0f, white);
  26568. nk_stroke_line(o, p.x, p.y + crosshair_size + 1, p.x, p.y + 3, 1.0f, white);
  26569. nk_stroke_line(o, p.x, p.y - crosshair_size, p.x, p.y - 2, 1.0f, white); }
  26570. }
  26571. NK_LIB nk_bool
  26572. nk_do_color_picker(nk_flags* state,
  26573. struct nk_command_buffer* out, struct nk_colorf* col,
  26574. enum nk_color_format fmt, struct nk_rect bounds,
  26575. struct nk_vec2 padding, const struct nk_input* in,
  26576. const struct nk_user_font* font)
  26577. {
  26578. int ret = 0;
  26579. struct nk_rect matrix;
  26580. struct nk_rect hue_bar;
  26581. struct nk_rect alpha_bar;
  26582. float bar_w;
  26583. NK_ASSERT(out);
  26584. NK_ASSERT(col);
  26585. NK_ASSERT(state);
  26586. NK_ASSERT(font);
  26587. if (!out || !col || !state || !font)
  26588. return ret;
  26589. bar_w = font->height;
  26590. bounds.x += padding.x;
  26591. bounds.y += padding.x;
  26592. bounds.w -= 2 * padding.x;
  26593. bounds.h -= 2 * padding.y;
  26594. matrix.x = bounds.x;
  26595. matrix.y = bounds.y;
  26596. matrix.h = bounds.h;
  26597. matrix.w = bounds.w - (3 * padding.x + 2 * bar_w);
  26598. hue_bar.w = bar_w;
  26599. hue_bar.y = bounds.y;
  26600. hue_bar.h = matrix.h;
  26601. hue_bar.x = matrix.x + matrix.w + padding.x;
  26602. alpha_bar.x = hue_bar.x + hue_bar.w + padding.x;
  26603. alpha_bar.y = bounds.y;
  26604. alpha_bar.w = bar_w;
  26605. alpha_bar.h = matrix.h;
  26606. ret = nk_color_picker_behavior(state, &bounds, &matrix, &hue_bar,
  26607. (fmt == NK_RGBA) ? &alpha_bar : 0, col, in);
  26608. nk_draw_color_picker(out, &matrix, &hue_bar, (fmt == NK_RGBA) ? &alpha_bar : 0, *col);
  26609. return ret;
  26610. }
  26611. NK_API nk_bool
  26612. nk_color_pick(struct nk_context* ctx, struct nk_colorf* color,
  26613. enum nk_color_format fmt)
  26614. {
  26615. struct nk_window* win;
  26616. struct nk_panel* layout;
  26617. const struct nk_style* config;
  26618. const struct nk_input* in;
  26619. enum nk_widget_layout_states state;
  26620. struct nk_rect bounds;
  26621. NK_ASSERT(ctx);
  26622. NK_ASSERT(color);
  26623. NK_ASSERT(ctx->current);
  26624. NK_ASSERT(ctx->current->layout);
  26625. if (!ctx || !ctx->current || !ctx->current->layout || !color)
  26626. return 0;
  26627. win = ctx->current;
  26628. config = &ctx->style;
  26629. layout = win->layout;
  26630. state = nk_widget(&bounds, ctx);
  26631. if (!state) return 0;
  26632. in = (state == NK_WIDGET_ROM || layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  26633. return nk_do_color_picker(&ctx->last_widget_state, &win->buffer, color, fmt, bounds,
  26634. nk_vec2(0, 0), in, config->font);
  26635. }
  26636. NK_API struct nk_colorf
  26637. nk_color_picker(struct nk_context* ctx, struct nk_colorf color,
  26638. enum nk_color_format fmt)
  26639. {
  26640. nk_color_pick(ctx, &color, fmt);
  26641. return color;
  26642. }
  26643. /* ==============================================================
  26644. *
  26645. * COMBO
  26646. *
  26647. * ===============================================================*/
  26648. NK_INTERN nk_bool
  26649. nk_combo_begin(struct nk_context* ctx, struct nk_window* win,
  26650. struct nk_vec2 size, nk_bool is_clicked, struct nk_rect header)
  26651. {
  26652. struct nk_window* popup;
  26653. int is_open = 0;
  26654. int is_active = 0;
  26655. struct nk_rect body;
  26656. nk_hash hash;
  26657. NK_ASSERT(ctx);
  26658. NK_ASSERT(ctx->current);
  26659. NK_ASSERT(ctx->current->layout);
  26660. if (!ctx || !ctx->current || !ctx->current->layout)
  26661. return 0;
  26662. popup = win->popup.win;
  26663. body.x = header.x;
  26664. body.w = size.x;
  26665. body.y = header.y + header.h - ctx->style.window.combo_border;
  26666. body.h = size.y;
  26667. hash = win->popup.combo_count++;
  26668. is_open = (popup) ? nk_true : nk_false;
  26669. is_active = (popup && (win->popup.name == hash) && win->popup.type == NK_PANEL_COMBO);
  26670. if ((is_clicked && is_open && !is_active) || (is_open && !is_active) ||
  26671. (!is_open && !is_active && !is_clicked)) return 0;
  26672. if (!nk_nonblock_begin(ctx, 0, body,
  26673. (is_clicked && is_open) ? nk_rect(0, 0, 0, 0) : header, NK_PANEL_COMBO)) return 0;
  26674. win->popup.type = NK_PANEL_COMBO;
  26675. win->popup.name = hash;
  26676. return 1;
  26677. }
  26678. NK_API nk_bool
  26679. nk_combo_begin_text(struct nk_context* ctx, const char* selected, int len,
  26680. struct nk_vec2 size)
  26681. {
  26682. const struct nk_input* in;
  26683. struct nk_window* win;
  26684. struct nk_style* style;
  26685. enum nk_widget_layout_states s;
  26686. int is_clicked = nk_false;
  26687. struct nk_rect header;
  26688. const struct nk_style_item* background;
  26689. struct nk_text text;
  26690. NK_ASSERT(ctx);
  26691. NK_ASSERT(selected);
  26692. NK_ASSERT(ctx->current);
  26693. NK_ASSERT(ctx->current->layout);
  26694. if (!ctx || !ctx->current || !ctx->current->layout || !selected)
  26695. return 0;
  26696. win = ctx->current;
  26697. style = &ctx->style;
  26698. s = nk_widget(&header, ctx);
  26699. if (s == NK_WIDGET_INVALID)
  26700. return 0;
  26701. in = (win->layout->flags & NK_WINDOW_ROM || s == NK_WIDGET_ROM) ? 0 : &ctx->input;
  26702. if (nk_button_behavior(&ctx->last_widget_state, header, in, NK_BUTTON_DEFAULT))
  26703. is_clicked = nk_true;
  26704. /* draw combo box header background and border */
  26705. if (ctx->last_widget_state & NK_WIDGET_STATE_ACTIVED) {
  26706. background = &style->combo.active;
  26707. text.text = style->combo.label_active;
  26708. }
  26709. else if (ctx->last_widget_state & NK_WIDGET_STATE_HOVER) {
  26710. background = &style->combo.hover;
  26711. text.text = style->combo.label_hover;
  26712. }
  26713. else {
  26714. background = &style->combo.normal;
  26715. text.text = style->combo.label_normal;
  26716. }
  26717. switch (background->type) {
  26718. case NK_STYLE_ITEM_IMAGE:
  26719. text.background = nk_rgba(0, 0, 0, 0);
  26720. nk_draw_image(&win->buffer, header, &background->data.image, nk_white);
  26721. break;
  26722. case NK_STYLE_ITEM_NINE_SLICE:
  26723. text.background = nk_rgba(0, 0, 0, 0);
  26724. nk_draw_nine_slice(&win->buffer, header, &background->data.slice, nk_white);
  26725. break;
  26726. case NK_STYLE_ITEM_COLOR:
  26727. text.background = background->data.color;
  26728. nk_fill_rect(&win->buffer, header, style->combo.rounding, background->data.color);
  26729. nk_stroke_rect(&win->buffer, header, style->combo.rounding, style->combo.border, style->combo.border_color);
  26730. break;
  26731. }
  26732. {
  26733. /* print currently selected text item */
  26734. struct nk_rect label;
  26735. struct nk_rect button;
  26736. struct nk_rect content;
  26737. int draw_button_symbol;
  26738. enum nk_symbol_type sym;
  26739. if (ctx->last_widget_state & NK_WIDGET_STATE_HOVER)
  26740. sym = style->combo.sym_hover;
  26741. else if (is_clicked)
  26742. sym = style->combo.sym_active;
  26743. else
  26744. sym = style->combo.sym_normal;
  26745. /* represents whether or not the combo's button symbol should be drawn */
  26746. draw_button_symbol = sym != NK_SYMBOL_NONE;
  26747. /* calculate button */
  26748. button.w = header.h - 2 * style->combo.button_padding.y;
  26749. button.x = (header.x + header.w - header.h) - style->combo.button_padding.x;
  26750. button.y = header.y + style->combo.button_padding.y;
  26751. button.h = button.w;
  26752. content.x = button.x + style->combo.button.padding.x;
  26753. content.y = button.y + style->combo.button.padding.y;
  26754. content.w = button.w - 2 * style->combo.button.padding.x;
  26755. content.h = button.h - 2 * style->combo.button.padding.y;
  26756. /* draw selected label */
  26757. text.padding = nk_vec2(0, 0);
  26758. label.x = header.x + style->combo.content_padding.x;
  26759. label.y = header.y + style->combo.content_padding.y;
  26760. label.h = header.h - 2 * style->combo.content_padding.y;
  26761. if (draw_button_symbol)
  26762. label.w = button.x - (style->combo.content_padding.x + style->combo.spacing.x) - label.x;
  26763. else
  26764. label.w = header.w - 2 * style->combo.content_padding.x;
  26765. nk_widget_text(&win->buffer, label, selected, len, &text,
  26766. NK_TEXT_LEFT, ctx->style.font);
  26767. /* draw open/close button */
  26768. if (draw_button_symbol)
  26769. nk_draw_button_symbol(&win->buffer, &button, &content, ctx->last_widget_state,
  26770. &ctx->style.combo.button, sym, style->font);
  26771. }
  26772. return nk_combo_begin(ctx, win, size, is_clicked, header);
  26773. }
  26774. NK_API nk_bool
  26775. nk_combo_begin_label(struct nk_context* ctx, const char* selected, struct nk_vec2 size)
  26776. {
  26777. return nk_combo_begin_text(ctx, selected, nk_strlen(selected), size);
  26778. }
  26779. NK_API nk_bool
  26780. nk_combo_begin_color(struct nk_context* ctx, struct nk_color color, struct nk_vec2 size)
  26781. {
  26782. struct nk_window* win;
  26783. struct nk_style* style;
  26784. const struct nk_input* in;
  26785. struct nk_rect header;
  26786. int is_clicked = nk_false;
  26787. enum nk_widget_layout_states s;
  26788. const struct nk_style_item* background;
  26789. NK_ASSERT(ctx);
  26790. NK_ASSERT(ctx->current);
  26791. NK_ASSERT(ctx->current->layout);
  26792. if (!ctx || !ctx->current || !ctx->current->layout)
  26793. return 0;
  26794. win = ctx->current;
  26795. style = &ctx->style;
  26796. s = nk_widget(&header, ctx);
  26797. if (s == NK_WIDGET_INVALID)
  26798. return 0;
  26799. in = (win->layout->flags & NK_WINDOW_ROM || s == NK_WIDGET_ROM) ? 0 : &ctx->input;
  26800. if (nk_button_behavior(&ctx->last_widget_state, header, in, NK_BUTTON_DEFAULT))
  26801. is_clicked = nk_true;
  26802. /* draw combo box header background and border */
  26803. if (ctx->last_widget_state & NK_WIDGET_STATE_ACTIVED)
  26804. background = &style->combo.active;
  26805. else if (ctx->last_widget_state & NK_WIDGET_STATE_HOVER)
  26806. background = &style->combo.hover;
  26807. else background = &style->combo.normal;
  26808. switch (background->type) {
  26809. case NK_STYLE_ITEM_IMAGE:
  26810. nk_draw_image(&win->buffer, header, &background->data.image, nk_white);
  26811. break;
  26812. case NK_STYLE_ITEM_NINE_SLICE:
  26813. nk_draw_nine_slice(&win->buffer, header, &background->data.slice, nk_white);
  26814. break;
  26815. case NK_STYLE_ITEM_COLOR:
  26816. nk_fill_rect(&win->buffer, header, style->combo.rounding, background->data.color);
  26817. nk_stroke_rect(&win->buffer, header, style->combo.rounding, style->combo.border, style->combo.border_color);
  26818. break;
  26819. }
  26820. {
  26821. struct nk_rect content;
  26822. struct nk_rect button;
  26823. struct nk_rect bounds;
  26824. int draw_button_symbol;
  26825. enum nk_symbol_type sym;
  26826. if (ctx->last_widget_state & NK_WIDGET_STATE_HOVER)
  26827. sym = style->combo.sym_hover;
  26828. else if (is_clicked)
  26829. sym = style->combo.sym_active;
  26830. else sym = style->combo.sym_normal;
  26831. /* represents whether or not the combo's button symbol should be drawn */
  26832. draw_button_symbol = sym != NK_SYMBOL_NONE;
  26833. /* calculate button */
  26834. button.w = header.h - 2 * style->combo.button_padding.y;
  26835. button.x = (header.x + header.w - header.h) - style->combo.button_padding.x;
  26836. button.y = header.y + style->combo.button_padding.y;
  26837. button.h = button.w;
  26838. content.x = button.x + style->combo.button.padding.x;
  26839. content.y = button.y + style->combo.button.padding.y;
  26840. content.w = button.w - 2 * style->combo.button.padding.x;
  26841. content.h = button.h - 2 * style->combo.button.padding.y;
  26842. /* draw color */
  26843. bounds.h = header.h - 4 * style->combo.content_padding.y;
  26844. bounds.y = header.y + 2 * style->combo.content_padding.y;
  26845. bounds.x = header.x + 2 * style->combo.content_padding.x;
  26846. if (draw_button_symbol)
  26847. bounds.w = (button.x - (style->combo.content_padding.x + style->combo.spacing.x)) - bounds.x;
  26848. else
  26849. bounds.w = header.w - 4 * style->combo.content_padding.x;
  26850. nk_fill_rect(&win->buffer, bounds, 0, color);
  26851. /* draw open/close button */
  26852. if (draw_button_symbol)
  26853. nk_draw_button_symbol(&win->buffer, &button, &content, ctx->last_widget_state,
  26854. &ctx->style.combo.button, sym, style->font);
  26855. }
  26856. return nk_combo_begin(ctx, win, size, is_clicked, header);
  26857. }
  26858. NK_API nk_bool
  26859. nk_combo_begin_symbol(struct nk_context* ctx, enum nk_symbol_type symbol, struct nk_vec2 size)
  26860. {
  26861. struct nk_window* win;
  26862. struct nk_style* style;
  26863. const struct nk_input* in;
  26864. struct nk_rect header;
  26865. int is_clicked = nk_false;
  26866. enum nk_widget_layout_states s;
  26867. const struct nk_style_item* background;
  26868. struct nk_color sym_background;
  26869. struct nk_color symbol_color;
  26870. NK_ASSERT(ctx);
  26871. NK_ASSERT(ctx->current);
  26872. NK_ASSERT(ctx->current->layout);
  26873. if (!ctx || !ctx->current || !ctx->current->layout)
  26874. return 0;
  26875. win = ctx->current;
  26876. style = &ctx->style;
  26877. s = nk_widget(&header, ctx);
  26878. if (s == NK_WIDGET_INVALID)
  26879. return 0;
  26880. in = (win->layout->flags & NK_WINDOW_ROM || s == NK_WIDGET_ROM) ? 0 : &ctx->input;
  26881. if (nk_button_behavior(&ctx->last_widget_state, header, in, NK_BUTTON_DEFAULT))
  26882. is_clicked = nk_true;
  26883. /* draw combo box header background and border */
  26884. if (ctx->last_widget_state & NK_WIDGET_STATE_ACTIVED) {
  26885. background = &style->combo.active;
  26886. symbol_color = style->combo.symbol_active;
  26887. }
  26888. else if (ctx->last_widget_state & NK_WIDGET_STATE_HOVER) {
  26889. background = &style->combo.hover;
  26890. symbol_color = style->combo.symbol_hover;
  26891. }
  26892. else {
  26893. background = &style->combo.normal;
  26894. symbol_color = style->combo.symbol_hover;
  26895. }
  26896. switch (background->type) {
  26897. case NK_STYLE_ITEM_IMAGE:
  26898. sym_background = nk_rgba(0, 0, 0, 0);
  26899. nk_draw_image(&win->buffer, header, &background->data.image, nk_white);
  26900. break;
  26901. case NK_STYLE_ITEM_NINE_SLICE:
  26902. sym_background = nk_rgba(0, 0, 0, 0);
  26903. nk_draw_nine_slice(&win->buffer, header, &background->data.slice, nk_white);
  26904. break;
  26905. case NK_STYLE_ITEM_COLOR:
  26906. sym_background = background->data.color;
  26907. nk_fill_rect(&win->buffer, header, style->combo.rounding, background->data.color);
  26908. nk_stroke_rect(&win->buffer, header, style->combo.rounding, style->combo.border, style->combo.border_color);
  26909. break;
  26910. }
  26911. {
  26912. struct nk_rect bounds = { 0,0,0,0 };
  26913. struct nk_rect content;
  26914. struct nk_rect button;
  26915. enum nk_symbol_type sym;
  26916. if (ctx->last_widget_state & NK_WIDGET_STATE_HOVER)
  26917. sym = style->combo.sym_hover;
  26918. else if (is_clicked)
  26919. sym = style->combo.sym_active;
  26920. else sym = style->combo.sym_normal;
  26921. /* calculate button */
  26922. button.w = header.h - 2 * style->combo.button_padding.y;
  26923. button.x = (header.x + header.w - header.h) - style->combo.button_padding.y;
  26924. button.y = header.y + style->combo.button_padding.y;
  26925. button.h = button.w;
  26926. content.x = button.x + style->combo.button.padding.x;
  26927. content.y = button.y + style->combo.button.padding.y;
  26928. content.w = button.w - 2 * style->combo.button.padding.x;
  26929. content.h = button.h - 2 * style->combo.button.padding.y;
  26930. /* draw symbol */
  26931. bounds.h = header.h - 2 * style->combo.content_padding.y;
  26932. bounds.y = header.y + style->combo.content_padding.y;
  26933. bounds.x = header.x + style->combo.content_padding.x;
  26934. bounds.w = (button.x - style->combo.content_padding.y) - bounds.x;
  26935. nk_draw_symbol(&win->buffer, symbol, bounds, sym_background, symbol_color,
  26936. 1.0f, style->font);
  26937. /* draw open/close button */
  26938. nk_draw_button_symbol(&win->buffer, &bounds, &content, ctx->last_widget_state,
  26939. &ctx->style.combo.button, sym, style->font);
  26940. }
  26941. return nk_combo_begin(ctx, win, size, is_clicked, header);
  26942. }
  26943. NK_API nk_bool
  26944. nk_combo_begin_symbol_text(struct nk_context* ctx, const char* selected, int len,
  26945. enum nk_symbol_type symbol, struct nk_vec2 size)
  26946. {
  26947. struct nk_window* win;
  26948. struct nk_style* style;
  26949. struct nk_input* in;
  26950. struct nk_rect header;
  26951. int is_clicked = nk_false;
  26952. enum nk_widget_layout_states s;
  26953. const struct nk_style_item* background;
  26954. struct nk_color symbol_color;
  26955. struct nk_text text;
  26956. NK_ASSERT(ctx);
  26957. NK_ASSERT(ctx->current);
  26958. NK_ASSERT(ctx->current->layout);
  26959. if (!ctx || !ctx->current || !ctx->current->layout)
  26960. return 0;
  26961. win = ctx->current;
  26962. style = &ctx->style;
  26963. s = nk_widget(&header, ctx);
  26964. if (!s) return 0;
  26965. in = (win->layout->flags & NK_WINDOW_ROM || s == NK_WIDGET_ROM) ? 0 : &ctx->input;
  26966. if (nk_button_behavior(&ctx->last_widget_state, header, in, NK_BUTTON_DEFAULT))
  26967. is_clicked = nk_true;
  26968. /* draw combo box header background and border */
  26969. if (ctx->last_widget_state & NK_WIDGET_STATE_ACTIVED) {
  26970. background = &style->combo.active;
  26971. symbol_color = style->combo.symbol_active;
  26972. text.text = style->combo.label_active;
  26973. }
  26974. else if (ctx->last_widget_state & NK_WIDGET_STATE_HOVER) {
  26975. background = &style->combo.hover;
  26976. symbol_color = style->combo.symbol_hover;
  26977. text.text = style->combo.label_hover;
  26978. }
  26979. else {
  26980. background = &style->combo.normal;
  26981. symbol_color = style->combo.symbol_normal;
  26982. text.text = style->combo.label_normal;
  26983. }
  26984. switch (background->type) {
  26985. case NK_STYLE_ITEM_IMAGE:
  26986. text.background = nk_rgba(0, 0, 0, 0);
  26987. nk_draw_image(&win->buffer, header, &background->data.image, nk_white);
  26988. break;
  26989. case NK_STYLE_ITEM_NINE_SLICE:
  26990. text.background = nk_rgba(0, 0, 0, 0);
  26991. nk_draw_nine_slice(&win->buffer, header, &background->data.slice, nk_white);
  26992. break;
  26993. case NK_STYLE_ITEM_COLOR:
  26994. text.background = background->data.color;
  26995. nk_fill_rect(&win->buffer, header, style->combo.rounding, background->data.color);
  26996. nk_stroke_rect(&win->buffer, header, style->combo.rounding, style->combo.border, style->combo.border_color);
  26997. break;
  26998. }
  26999. {
  27000. struct nk_rect content;
  27001. struct nk_rect button;
  27002. struct nk_rect label;
  27003. struct nk_rect image;
  27004. enum nk_symbol_type sym;
  27005. if (ctx->last_widget_state & NK_WIDGET_STATE_HOVER)
  27006. sym = style->combo.sym_hover;
  27007. else if (is_clicked)
  27008. sym = style->combo.sym_active;
  27009. else sym = style->combo.sym_normal;
  27010. /* calculate button */
  27011. button.w = header.h - 2 * style->combo.button_padding.y;
  27012. button.x = (header.x + header.w - header.h) - style->combo.button_padding.x;
  27013. button.y = header.y + style->combo.button_padding.y;
  27014. button.h = button.w;
  27015. content.x = button.x + style->combo.button.padding.x;
  27016. content.y = button.y + style->combo.button.padding.y;
  27017. content.w = button.w - 2 * style->combo.button.padding.x;
  27018. content.h = button.h - 2 * style->combo.button.padding.y;
  27019. nk_draw_button_symbol(&win->buffer, &button, &content, ctx->last_widget_state,
  27020. &ctx->style.combo.button, sym, style->font);
  27021. /* draw symbol */
  27022. image.x = header.x + style->combo.content_padding.x;
  27023. image.y = header.y + style->combo.content_padding.y;
  27024. image.h = header.h - 2 * style->combo.content_padding.y;
  27025. image.w = image.h;
  27026. nk_draw_symbol(&win->buffer, symbol, image, text.background, symbol_color,
  27027. 1.0f, style->font);
  27028. /* draw label */
  27029. text.padding = nk_vec2(0, 0);
  27030. label.x = image.x + image.w + style->combo.spacing.x + style->combo.content_padding.x;
  27031. label.y = header.y + style->combo.content_padding.y;
  27032. label.w = (button.x - style->combo.content_padding.x) - label.x;
  27033. label.h = header.h - 2 * style->combo.content_padding.y;
  27034. nk_widget_text(&win->buffer, label, selected, len, &text, NK_TEXT_LEFT, style->font);
  27035. }
  27036. return nk_combo_begin(ctx, win, size, is_clicked, header);
  27037. }
  27038. NK_API nk_bool
  27039. nk_combo_begin_image(struct nk_context* ctx, struct nk_image img, struct nk_vec2 size)
  27040. {
  27041. struct nk_window* win;
  27042. struct nk_style* style;
  27043. const struct nk_input* in;
  27044. struct nk_rect header;
  27045. int is_clicked = nk_false;
  27046. enum nk_widget_layout_states s;
  27047. const struct nk_style_item* background;
  27048. NK_ASSERT(ctx);
  27049. NK_ASSERT(ctx->current);
  27050. NK_ASSERT(ctx->current->layout);
  27051. if (!ctx || !ctx->current || !ctx->current->layout)
  27052. return 0;
  27053. win = ctx->current;
  27054. style = &ctx->style;
  27055. s = nk_widget(&header, ctx);
  27056. if (s == NK_WIDGET_INVALID)
  27057. return 0;
  27058. in = (win->layout->flags & NK_WINDOW_ROM || s == NK_WIDGET_ROM) ? 0 : &ctx->input;
  27059. if (nk_button_behavior(&ctx->last_widget_state, header, in, NK_BUTTON_DEFAULT))
  27060. is_clicked = nk_true;
  27061. /* draw combo box header background and border */
  27062. if (ctx->last_widget_state & NK_WIDGET_STATE_ACTIVED)
  27063. background = &style->combo.active;
  27064. else if (ctx->last_widget_state & NK_WIDGET_STATE_HOVER)
  27065. background = &style->combo.hover;
  27066. else background = &style->combo.normal;
  27067. switch (background->type) {
  27068. case NK_STYLE_ITEM_IMAGE:
  27069. nk_draw_image(&win->buffer, header, &background->data.image, nk_white);
  27070. break;
  27071. case NK_STYLE_ITEM_NINE_SLICE:
  27072. nk_draw_nine_slice(&win->buffer, header, &background->data.slice, nk_white);
  27073. break;
  27074. case NK_STYLE_ITEM_COLOR:
  27075. nk_fill_rect(&win->buffer, header, style->combo.rounding, background->data.color);
  27076. nk_stroke_rect(&win->buffer, header, style->combo.rounding, style->combo.border, style->combo.border_color);
  27077. break;
  27078. }
  27079. {
  27080. struct nk_rect bounds = { 0,0,0,0 };
  27081. struct nk_rect content;
  27082. struct nk_rect button;
  27083. int draw_button_symbol;
  27084. enum nk_symbol_type sym;
  27085. if (ctx->last_widget_state & NK_WIDGET_STATE_HOVER)
  27086. sym = style->combo.sym_hover;
  27087. else if (is_clicked)
  27088. sym = style->combo.sym_active;
  27089. else sym = style->combo.sym_normal;
  27090. /* represents whether or not the combo's button symbol should be drawn */
  27091. draw_button_symbol = sym != NK_SYMBOL_NONE;
  27092. /* calculate button */
  27093. button.w = header.h - 2 * style->combo.button_padding.y;
  27094. button.x = (header.x + header.w - header.h) - style->combo.button_padding.y;
  27095. button.y = header.y + style->combo.button_padding.y;
  27096. button.h = button.w;
  27097. content.x = button.x + style->combo.button.padding.x;
  27098. content.y = button.y + style->combo.button.padding.y;
  27099. content.w = button.w - 2 * style->combo.button.padding.x;
  27100. content.h = button.h - 2 * style->combo.button.padding.y;
  27101. /* draw image */
  27102. bounds.h = header.h - 2 * style->combo.content_padding.y;
  27103. bounds.y = header.y + style->combo.content_padding.y;
  27104. bounds.x = header.x + style->combo.content_padding.x;
  27105. if (draw_button_symbol)
  27106. bounds.w = (button.x - style->combo.content_padding.y) - bounds.x;
  27107. else
  27108. bounds.w = header.w - 2 * style->combo.content_padding.x;
  27109. nk_draw_image(&win->buffer, bounds, &img, nk_white);
  27110. /* draw open/close button */
  27111. if (draw_button_symbol)
  27112. nk_draw_button_symbol(&win->buffer, &bounds, &content, ctx->last_widget_state,
  27113. &ctx->style.combo.button, sym, style->font);
  27114. }
  27115. return nk_combo_begin(ctx, win, size, is_clicked, header);
  27116. }
  27117. NK_API nk_bool
  27118. nk_combo_begin_image_text(struct nk_context* ctx, const char* selected, int len,
  27119. struct nk_image img, struct nk_vec2 size)
  27120. {
  27121. struct nk_window* win;
  27122. struct nk_style* style;
  27123. struct nk_input* in;
  27124. struct nk_rect header;
  27125. int is_clicked = nk_false;
  27126. enum nk_widget_layout_states s;
  27127. const struct nk_style_item* background;
  27128. struct nk_text text;
  27129. NK_ASSERT(ctx);
  27130. NK_ASSERT(ctx->current);
  27131. NK_ASSERT(ctx->current->layout);
  27132. if (!ctx || !ctx->current || !ctx->current->layout)
  27133. return 0;
  27134. win = ctx->current;
  27135. style = &ctx->style;
  27136. s = nk_widget(&header, ctx);
  27137. if (!s) return 0;
  27138. in = (win->layout->flags & NK_WINDOW_ROM || s == NK_WIDGET_ROM) ? 0 : &ctx->input;
  27139. if (nk_button_behavior(&ctx->last_widget_state, header, in, NK_BUTTON_DEFAULT))
  27140. is_clicked = nk_true;
  27141. /* draw combo box header background and border */
  27142. if (ctx->last_widget_state & NK_WIDGET_STATE_ACTIVED) {
  27143. background = &style->combo.active;
  27144. text.text = style->combo.label_active;
  27145. }
  27146. else if (ctx->last_widget_state & NK_WIDGET_STATE_HOVER) {
  27147. background = &style->combo.hover;
  27148. text.text = style->combo.label_hover;
  27149. }
  27150. else {
  27151. background = &style->combo.normal;
  27152. text.text = style->combo.label_normal;
  27153. }
  27154. switch (background->type) {
  27155. case NK_STYLE_ITEM_IMAGE:
  27156. text.background = nk_rgba(0, 0, 0, 0);
  27157. nk_draw_image(&win->buffer, header, &background->data.image, nk_white);
  27158. break;
  27159. case NK_STYLE_ITEM_NINE_SLICE:
  27160. text.background = nk_rgba(0, 0, 0, 0);
  27161. nk_draw_nine_slice(&win->buffer, header, &background->data.slice, nk_white);
  27162. break;
  27163. case NK_STYLE_ITEM_COLOR:
  27164. text.background = background->data.color;
  27165. nk_fill_rect(&win->buffer, header, style->combo.rounding, background->data.color);
  27166. nk_stroke_rect(&win->buffer, header, style->combo.rounding, style->combo.border, style->combo.border_color);
  27167. break;
  27168. }
  27169. {
  27170. struct nk_rect content;
  27171. struct nk_rect button;
  27172. struct nk_rect label;
  27173. struct nk_rect image;
  27174. int draw_button_symbol;
  27175. enum nk_symbol_type sym;
  27176. if (ctx->last_widget_state & NK_WIDGET_STATE_HOVER)
  27177. sym = style->combo.sym_hover;
  27178. else if (is_clicked)
  27179. sym = style->combo.sym_active;
  27180. else sym = style->combo.sym_normal;
  27181. /* represents whether or not the combo's button symbol should be drawn */
  27182. draw_button_symbol = sym != NK_SYMBOL_NONE;
  27183. /* calculate button */
  27184. button.w = header.h - 2 * style->combo.button_padding.y;
  27185. button.x = (header.x + header.w - header.h) - style->combo.button_padding.x;
  27186. button.y = header.y + style->combo.button_padding.y;
  27187. button.h = button.w;
  27188. content.x = button.x + style->combo.button.padding.x;
  27189. content.y = button.y + style->combo.button.padding.y;
  27190. content.w = button.w - 2 * style->combo.button.padding.x;
  27191. content.h = button.h - 2 * style->combo.button.padding.y;
  27192. if (draw_button_symbol)
  27193. nk_draw_button_symbol(&win->buffer, &button, &content, ctx->last_widget_state,
  27194. &ctx->style.combo.button, sym, style->font);
  27195. /* draw image */
  27196. image.x = header.x + style->combo.content_padding.x;
  27197. image.y = header.y + style->combo.content_padding.y;
  27198. image.h = header.h - 2 * style->combo.content_padding.y;
  27199. image.w = image.h;
  27200. nk_draw_image(&win->buffer, image, &img, nk_white);
  27201. /* draw label */
  27202. text.padding = nk_vec2(0, 0);
  27203. label.x = image.x + image.w + style->combo.spacing.x + style->combo.content_padding.x;
  27204. label.y = header.y + style->combo.content_padding.y;
  27205. label.h = header.h - 2 * style->combo.content_padding.y;
  27206. if (draw_button_symbol)
  27207. label.w = (button.x - style->combo.content_padding.x) - label.x;
  27208. else
  27209. label.w = (header.x + header.w - style->combo.content_padding.x) - label.x;
  27210. nk_widget_text(&win->buffer, label, selected, len, &text, NK_TEXT_LEFT, style->font);
  27211. }
  27212. return nk_combo_begin(ctx, win, size, is_clicked, header);
  27213. }
  27214. NK_API nk_bool
  27215. nk_combo_begin_symbol_label(struct nk_context* ctx,
  27216. const char* selected, enum nk_symbol_type type, struct nk_vec2 size)
  27217. {
  27218. return nk_combo_begin_symbol_text(ctx, selected, nk_strlen(selected), type, size);
  27219. }
  27220. NK_API nk_bool
  27221. nk_combo_begin_image_label(struct nk_context* ctx,
  27222. const char* selected, struct nk_image img, struct nk_vec2 size)
  27223. {
  27224. return nk_combo_begin_image_text(ctx, selected, nk_strlen(selected), img, size);
  27225. }
  27226. NK_API nk_bool
  27227. nk_combo_item_text(struct nk_context* ctx, const char* text, int len, nk_flags align)
  27228. {
  27229. return nk_contextual_item_text(ctx, text, len, align);
  27230. }
  27231. NK_API nk_bool
  27232. nk_combo_item_label(struct nk_context* ctx, const char* label, nk_flags align)
  27233. {
  27234. return nk_contextual_item_label(ctx, label, align);
  27235. }
  27236. NK_API nk_bool
  27237. nk_combo_item_image_text(struct nk_context* ctx, struct nk_image img, const char* text,
  27238. int len, nk_flags alignment)
  27239. {
  27240. return nk_contextual_item_image_text(ctx, img, text, len, alignment);
  27241. }
  27242. NK_API nk_bool
  27243. nk_combo_item_image_label(struct nk_context* ctx, struct nk_image img,
  27244. const char* text, nk_flags alignment)
  27245. {
  27246. return nk_contextual_item_image_label(ctx, img, text, alignment);
  27247. }
  27248. NK_API nk_bool
  27249. nk_combo_item_symbol_text(struct nk_context* ctx, enum nk_symbol_type sym,
  27250. const char* text, int len, nk_flags alignment)
  27251. {
  27252. return nk_contextual_item_symbol_text(ctx, sym, text, len, alignment);
  27253. }
  27254. NK_API nk_bool
  27255. nk_combo_item_symbol_label(struct nk_context* ctx, enum nk_symbol_type sym,
  27256. const char* label, nk_flags alignment)
  27257. {
  27258. return nk_contextual_item_symbol_label(ctx, sym, label, alignment);
  27259. }
  27260. NK_API void nk_combo_end(struct nk_context* ctx)
  27261. {
  27262. nk_contextual_end(ctx);
  27263. }
  27264. NK_API void nk_combo_close(struct nk_context* ctx)
  27265. {
  27266. nk_contextual_close(ctx);
  27267. }
  27268. NK_API int
  27269. nk_combo(struct nk_context* ctx, const char** items, int count,
  27270. int selected, int item_height, struct nk_vec2 size)
  27271. {
  27272. int i = 0;
  27273. int max_height;
  27274. struct nk_vec2 item_spacing;
  27275. struct nk_vec2 window_padding;
  27276. NK_ASSERT(ctx);
  27277. NK_ASSERT(items);
  27278. NK_ASSERT(ctx->current);
  27279. if (!ctx || !items || !count)
  27280. return selected;
  27281. item_spacing = ctx->style.window.spacing;
  27282. window_padding = nk_panel_get_padding(&ctx->style, ctx->current->layout->type);
  27283. max_height = count * item_height + count * (int)item_spacing.y;
  27284. max_height += (int)item_spacing.y * 2 + (int)window_padding.y * 2;
  27285. size.y = NK_MIN(size.y, (float)max_height);
  27286. if (nk_combo_begin_label(ctx, items[selected], size)) {
  27287. nk_layout_row_dynamic(ctx, (float)item_height, 1);
  27288. for (i = 0; i < count; ++i) {
  27289. if (nk_combo_item_label(ctx, items[i], NK_TEXT_LEFT))
  27290. selected = i;
  27291. }
  27292. nk_combo_end(ctx);
  27293. }
  27294. return selected;
  27295. }
  27296. NK_API int
  27297. nk_combo_separator(struct nk_context* ctx, const char* items_separated_by_separator,
  27298. int separator, int selected, int count, int item_height, struct nk_vec2 size)
  27299. {
  27300. int i;
  27301. int max_height;
  27302. struct nk_vec2 item_spacing;
  27303. struct nk_vec2 window_padding;
  27304. const char* current_item;
  27305. const char* iter;
  27306. int length = 0;
  27307. NK_ASSERT(ctx);
  27308. NK_ASSERT(items_separated_by_separator);
  27309. if (!ctx || !items_separated_by_separator)
  27310. return selected;
  27311. /* calculate popup window */
  27312. item_spacing = ctx->style.window.spacing;
  27313. window_padding = nk_panel_get_padding(&ctx->style, ctx->current->layout->type);
  27314. max_height = count * item_height + count * (int)item_spacing.y;
  27315. max_height += (int)item_spacing.y * 2 + (int)window_padding.y * 2;
  27316. size.y = NK_MIN(size.y, (float)max_height);
  27317. /* find selected item */
  27318. current_item = items_separated_by_separator;
  27319. for (i = 0; i < count; ++i) {
  27320. iter = current_item;
  27321. while (*iter && *iter != separator) iter++;
  27322. length = (int)(iter - current_item);
  27323. if (i == selected) break;
  27324. current_item = iter + 1;
  27325. }
  27326. if (nk_combo_begin_text(ctx, current_item, length, size)) {
  27327. current_item = items_separated_by_separator;
  27328. nk_layout_row_dynamic(ctx, (float)item_height, 1);
  27329. for (i = 0; i < count; ++i) {
  27330. iter = current_item;
  27331. while (*iter && *iter != separator) iter++;
  27332. length = (int)(iter - current_item);
  27333. if (nk_combo_item_text(ctx, current_item, length, NK_TEXT_LEFT))
  27334. selected = i;
  27335. current_item = current_item + length + 1;
  27336. }
  27337. nk_combo_end(ctx);
  27338. }
  27339. return selected;
  27340. }
  27341. NK_API int
  27342. nk_combo_string(struct nk_context* ctx, const char* items_separated_by_zeros,
  27343. int selected, int count, int item_height, struct nk_vec2 size)
  27344. {
  27345. return nk_combo_separator(ctx, items_separated_by_zeros, '\0', selected, count, item_height, size);
  27346. }
  27347. NK_API int
  27348. nk_combo_callback(struct nk_context* ctx, void(*item_getter)(void*, int, const char**),
  27349. void* userdata, int selected, int count, int item_height, struct nk_vec2 size)
  27350. {
  27351. int i;
  27352. int max_height;
  27353. struct nk_vec2 item_spacing;
  27354. struct nk_vec2 window_padding;
  27355. const char* item;
  27356. NK_ASSERT(ctx);
  27357. NK_ASSERT(item_getter);
  27358. if (!ctx || !item_getter)
  27359. return selected;
  27360. /* calculate popup window */
  27361. item_spacing = ctx->style.window.spacing;
  27362. window_padding = nk_panel_get_padding(&ctx->style, ctx->current->layout->type);
  27363. max_height = count * item_height + count * (int)item_spacing.y;
  27364. max_height += (int)item_spacing.y * 2 + (int)window_padding.y * 2;
  27365. size.y = NK_MIN(size.y, (float)max_height);
  27366. item_getter(userdata, selected, &item);
  27367. if (nk_combo_begin_label(ctx, item, size)) {
  27368. nk_layout_row_dynamic(ctx, (float)item_height, 1);
  27369. for (i = 0; i < count; ++i) {
  27370. item_getter(userdata, i, &item);
  27371. if (nk_combo_item_label(ctx, item, NK_TEXT_LEFT))
  27372. selected = i;
  27373. }
  27374. nk_combo_end(ctx);
  27375. } return selected;
  27376. }
  27377. NK_API void
  27378. nk_combobox(struct nk_context* ctx, const char** items, int count,
  27379. int* selected, int item_height, struct nk_vec2 size)
  27380. {
  27381. *selected = nk_combo(ctx, items, count, *selected, item_height, size);
  27382. }
  27383. NK_API void
  27384. nk_combobox_string(struct nk_context* ctx, const char* items_separated_by_zeros,
  27385. int* selected, int count, int item_height, struct nk_vec2 size)
  27386. {
  27387. *selected = nk_combo_string(ctx, items_separated_by_zeros, *selected, count, item_height, size);
  27388. }
  27389. NK_API void
  27390. nk_combobox_separator(struct nk_context* ctx, const char* items_separated_by_separator,
  27391. int separator, int* selected, int count, int item_height, struct nk_vec2 size)
  27392. {
  27393. *selected = nk_combo_separator(ctx, items_separated_by_separator, separator,
  27394. *selected, count, item_height, size);
  27395. }
  27396. NK_API void
  27397. nk_combobox_callback(struct nk_context* ctx,
  27398. void(*item_getter)(void* data, int id, const char** out_text),
  27399. void* userdata, int* selected, int count, int item_height, struct nk_vec2 size)
  27400. {
  27401. *selected = nk_combo_callback(ctx, item_getter, userdata, *selected, count, item_height, size);
  27402. }
  27403. /* ===============================================================
  27404. *
  27405. * TOOLTIP
  27406. *
  27407. * ===============================================================*/
  27408. NK_API nk_bool
  27409. nk_tooltip_begin(struct nk_context* ctx, float width)
  27410. {
  27411. int x, y, w, h;
  27412. struct nk_window* win;
  27413. const struct nk_input* in;
  27414. struct nk_rect bounds;
  27415. int ret;
  27416. NK_ASSERT(ctx);
  27417. NK_ASSERT(ctx->current);
  27418. NK_ASSERT(ctx->current->layout);
  27419. if (!ctx || !ctx->current || !ctx->current->layout)
  27420. return 0;
  27421. /* make sure that no nonblocking popup is currently active */
  27422. win = ctx->current;
  27423. in = &ctx->input;
  27424. if (win->popup.win && (win->popup.type & NK_PANEL_SET_NONBLOCK))
  27425. return 0;
  27426. w = nk_iceilf(width);
  27427. h = nk_iceilf(nk_null_rect.h);
  27428. x = nk_ifloorf(in->mouse.pos.x + 1) - (int)win->layout->clip.x;
  27429. y = nk_ifloorf(in->mouse.pos.y + 1) - (int)win->layout->clip.y;
  27430. bounds.x = (float)x;
  27431. bounds.y = (float)y;
  27432. bounds.w = (float)w;
  27433. bounds.h = (float)h;
  27434. ret = nk_popup_begin(ctx, NK_POPUP_DYNAMIC,
  27435. "__##Tooltip##__", NK_WINDOW_NO_SCROLLBAR | NK_WINDOW_BORDER, bounds);
  27436. if (ret) win->layout->flags &= ~(nk_flags)NK_WINDOW_ROM;
  27437. win->popup.type = NK_PANEL_TOOLTIP;
  27438. ctx->current->layout->type = NK_PANEL_TOOLTIP;
  27439. return ret;
  27440. }
  27441. NK_API void
  27442. nk_tooltip_end(struct nk_context* ctx)
  27443. {
  27444. NK_ASSERT(ctx);
  27445. NK_ASSERT(ctx->current);
  27446. if (!ctx || !ctx->current) return;
  27447. ctx->current->seq--;
  27448. nk_popup_close(ctx);
  27449. nk_popup_end(ctx);
  27450. }
  27451. NK_API void
  27452. nk_tooltip(struct nk_context* ctx, const char* text)
  27453. {
  27454. const struct nk_style* style;
  27455. struct nk_vec2 padding;
  27456. int text_len;
  27457. float text_width;
  27458. float text_height;
  27459. NK_ASSERT(ctx);
  27460. NK_ASSERT(ctx->current);
  27461. NK_ASSERT(ctx->current->layout);
  27462. NK_ASSERT(text);
  27463. if (!ctx || !ctx->current || !ctx->current->layout || !text)
  27464. return;
  27465. /* fetch configuration data */
  27466. style = &ctx->style;
  27467. padding = style->window.padding;
  27468. /* calculate size of the text and tooltip */
  27469. text_len = nk_strlen(text);
  27470. text_width = style->font->width(style->font->userdata,
  27471. style->font->height, text, text_len);
  27472. text_width += (4 * padding.x);
  27473. text_height = (style->font->height + 2 * padding.y);
  27474. /* execute tooltip and fill with text */
  27475. if (nk_tooltip_begin(ctx, (float)text_width)) {
  27476. nk_layout_row_dynamic(ctx, (float)text_height, 1);
  27477. nk_text(ctx, text, text_len, NK_TEXT_LEFT);
  27478. nk_tooltip_end(ctx);
  27479. }
  27480. }
  27481. #ifdef NK_INCLUDE_STANDARD_VARARGS
  27482. NK_API void
  27483. nk_tooltipf(struct nk_context* ctx, const char* fmt, ...)
  27484. {
  27485. va_list args;
  27486. va_start(args, fmt);
  27487. nk_tooltipfv(ctx, fmt, args);
  27488. va_end(args);
  27489. }
  27490. NK_API void
  27491. nk_tooltipfv(struct nk_context* ctx, const char* fmt, va_list args)
  27492. {
  27493. char buf[256];
  27494. nk_strfmt(buf, NK_LEN(buf), fmt, args);
  27495. nk_tooltip(ctx, buf);
  27496. }
  27497. #endif
  27498. #endif /* NK_IMPLEMENTATION */
  27499. /*
  27500. /// ## License
  27501. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~none
  27502. /// ------------------------------------------------------------------------------
  27503. /// This software is available under 2 licenses -- choose whichever you prefer.
  27504. /// ------------------------------------------------------------------------------
  27505. /// ALTERNATIVE A - MIT License
  27506. /// Copyright (c) 2016-2018 Micha Mettke
  27507. /// Permission is hereby granted, free of charge, to any person obtaining a copy of
  27508. /// this software and associated documentation files (the "Software"), to deal in
  27509. /// the Software without restriction, including without limitation the rights to
  27510. /// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
  27511. /// of the Software, and to permit persons to whom the Software is furnished to do
  27512. /// so, subject to the following conditions:
  27513. /// The above copyright notice and this permission notice shall be included in all
  27514. /// copies or substantial portions of the Software.
  27515. /// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  27516. /// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  27517. /// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  27518. /// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  27519. /// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  27520. /// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  27521. /// SOFTWARE.
  27522. /// ------------------------------------------------------------------------------
  27523. /// ALTERNATIVE B - Public Domain (www.unlicense.org)
  27524. /// This is free and unencumbered software released into the public domain.
  27525. /// Anyone is free to copy, modify, publish, use, compile, sell, or distribute this
  27526. /// software, either in source code form or as a compiled binary, for any purpose,
  27527. /// commercial or non-commercial, and by any means.
  27528. /// In jurisdictions that recognize copyright laws, the author or authors of this
  27529. /// software dedicate any and all copyright interest in the software to the public
  27530. /// domain. We make this dedication for the benefit of the public at large and to
  27531. /// the detriment of our heirs and successors. We intend this dedication to be an
  27532. /// overt act of relinquishment in perpetuity of all present and future rights to
  27533. /// this software under copyright law.
  27534. /// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  27535. /// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  27536. /// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  27537. /// AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  27538. /// ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27539. /// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27540. /// ------------------------------------------------------------------------------
  27541. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  27542. /// ## Changelog
  27543. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~none
  27544. /// [date] ([x.y.z]) - [description]
  27545. /// - [date]: date on which the change has been pushed
  27546. /// - [x.y.z]: Version string, represented in Semantic Versioning format
  27547. /// - [x]: Major version with API and library breaking changes
  27548. /// - [y]: Minor version with non-breaking API and library changes
  27549. /// - [z]: Patch version with no direct changes to the API
  27550. ///
  27551. /// - 2022/12/23 (4.10.6) - Fix incorrect glyph index in nk_font_bake()
  27552. /// - 2022/12/17 (4.10.5) - Fix nk_font_bake_pack() using TTC font offset incorrectly
  27553. /// - 2022/10/24 (4.10.4) - Fix nk_str_{append,insert}_str_utf8 always returning 0
  27554. /// - 2022/09/03 (4.10.3) - Renamed the `null` texture variable to `tex_null`
  27555. /// - 2022/08/01 (4.10.2) - Fix Apple Silicon with incorrect NK_SITE_TYPE and NK_POINTER_TYPE
  27556. /// - 2022/08/01 (4.10.1) - Fix cursor jumping back to beginning of text when typing more than
  27557. /// nk_edit_xxx limit
  27558. /// - 2022/05/27 (4.10.0) - Add nk_input_has_mouse_click_in_button_rect() to fix window move bug
  27559. /// - 2022/04/19 (4.9.8) - Added nk_rule_horizontal() widget
  27560. /// - 2022/04/18 (4.9.7) - Change button behavior when NK_BUTTON_TRIGGER_ON_RELEASE is defined to
  27561. /// only trigger when the mouse position was inside the same button on down
  27562. /// - 2022/02/03 (4.9.6) - Allow overriding the NK_INV_SQRT function, similar to NK_SIN and NK_COS
  27563. /// - 2021/12/22 (4.9.5) - Revert layout bounds not accounting for padding due to regressions
  27564. /// - 2021/12/22 (4.9.4) - Fix checking hovering when window is minimized
  27565. /// - 2021/12/22 (4.09.3) - Fix layout bounds not accounting for padding
  27566. /// - 2021/12/19 (4.09.2) - Update to stb_rect_pack.h v1.01 and stb_truetype.h v1.26
  27567. /// - 2021/12/16 (4.09.1) - Fix the majority of GCC warnings
  27568. /// - 2021/10/16 (4.09.0) - Added nk_spacer() widget
  27569. /// - 2021/09/22 (4.08.6) - Fix "may be used uninitialized" warnings in nk_widget
  27570. /// - 2021/09/22 (4.08.5) - GCC __builtin_offsetof only exists in version 4 and later
  27571. /// - 2021/09/15 (4.08.4) - Fix "'num_len' may be used uninitialized" in nk_do_property
  27572. /// - 2021/09/15 (4.08.3) - Fix "Templates cannot be declared to have 'C' Linkage"
  27573. /// - 2021/09/08 (4.08.2) - Fix warnings in C89 builds
  27574. /// - 2021/09/08 (4.08.1) - Use compiler builtins for NK_OFFSETOF when possible
  27575. /// - 2021/08/17 (4.08.0) - Implemented 9-slice scaling support for widget styles
  27576. /// - 2021/08/16 (4.07.5) - Replace usage of memset in nk_font_atlas_bake with NK_MEMSET
  27577. /// - 2021/08/15 (4.07.4) - Fix conversion and sign conversion warnings
  27578. /// - 2021/08/08 (4.07.3) - Fix crash when baking merged fonts
  27579. /// - 2021/08/08 (4.07.2) - Fix Multiline Edit wrong offset
  27580. /// - 2021/03/17 (4.07.1) - Fix warning about unused parameter
  27581. /// - 2021/03/17 (4.07.0) - Fix nk_property hover bug
  27582. /// - 2021/03/15 (4.06.4) - Change nk_propertyi back to int
  27583. /// - 2021/03/15 (4.06.3) - Update documentation for functions that now return nk_bool
  27584. /// - 2020/12/19 (4.06.2) - Fix additional C++ style comments which are not allowed in ISO C90.
  27585. /// - 2020/10/11 (4.06.1) - Fix C++ style comments which are not allowed in ISO C90.
  27586. /// - 2020/10/07 (4.06.0) - Fix nk_combo return type wrongly changed to nk_bool
  27587. /// - 2020/09/05 (4.05.0) - Use the nk_font_atlas allocator for stb_truetype memory management.
  27588. /// - 2020/09/04 (4.04.1) - Replace every boolean int by nk_bool
  27589. /// - 2020/09/04 (4.04.0) - Add nk_bool with NK_INCLUDE_STANDARD_BOOL
  27590. /// - 2020/06/13 (4.03.1) - Fix nk_pool allocation sizes.
  27591. /// - 2020/06/04 (4.03.0) - Made nk_combo header symbols optional.
  27592. /// - 2020/05/27 (4.02.5) - Fix nk_do_edit: Keep scroll position when re-activating edit widget.
  27593. /// - 2020/05/09 (4.02.4) - Fix nk_menubar height calculation bug
  27594. /// - 2020/05/08 (4.02.3) - Fix missing stdarg.h with NK_INCLUDE_STANDARD_VARARGS
  27595. /// - 2020/04/30 (4.02.2) - Fix nk_edit border drawing bug
  27596. /// - 2020/04/09 (4.02.1) - Removed unused nk_sqrt function to fix compiler warnings
  27597. /// - Fixed compiler warnings if you bring your own methods for
  27598. /// nk_cos/nk_sin/nk_strtod/nk_memset/nk_memcopy/nk_dtoa
  27599. /// - 2020/04/06 (4.01.10) - Fix bug: Do not use pool before checking for NULL
  27600. /// - 2020/03/22 (4.01.9) - Fix bug where layout state wasn't restored correctly after
  27601. /// popping a tree.
  27602. /// - 2020/03/11 (4.01.8) - Fix bug where padding is subtracted from widget
  27603. /// - 2020/03/06 (4.01.7) - Fix bug where width padding was applied twice
  27604. /// - 2020/02/06 (4.01.6) - Update stb_truetype.h and stb_rect_pack.h and separate them
  27605. /// - 2019/12/10 (4.01.5) - Fix off-by-one error in NK_INTERSECT
  27606. /// - 2019/10/09 (4.01.4) - Fix bug for autoscrolling in nk_do_edit
  27607. /// - 2019/09/20 (4.01.3) - Fixed a bug wherein combobox cannot be closed by clicking the header
  27608. /// when NK_BUTTON_TRIGGER_ON_RELEASE is defined.
  27609. /// - 2019/09/10 (4.01.2) - Fixed the nk_cos function, which deviated significantly.
  27610. /// - 2019/09/08 (4.01.1) - Fixed a bug wherein re-baking of fonts caused a segmentation
  27611. /// fault due to dst_font->glyph_count not being zeroed on subsequent
  27612. /// bakes of the same set of fonts.
  27613. /// - 2019/06/23 (4.01.0) - Added nk_***_get_scroll and nk_***_set_scroll for groups, windows, and popups.
  27614. /// - 2019/06/12 (4.00.3) - Fix panel background drawing bug.
  27615. /// - 2018/10/31 (4.00.2) - Added NK_KEYSTATE_BASED_INPUT to "fix" state based backends
  27616. /// like GLFW without breaking key repeat behavior on event based.
  27617. /// - 2018/04/01 (4.00.1) - Fixed calling `nk_convert` multiple time per single frame.
  27618. /// - 2018/04/01 (4.00.0) - BREAKING CHANGE: nk_draw_list_clear no longer tries to
  27619. /// clear provided buffers. So make sure to either free
  27620. /// or clear each passed buffer after calling nk_convert.
  27621. /// - 2018/02/23 (3.00.6) - Fixed slider dragging behavior.
  27622. /// - 2018/01/31 (3.00.5) - Fixed overcalculation of cursor data in font baking process.
  27623. /// - 2018/01/31 (3.00.4) - Removed name collision with stb_truetype.
  27624. /// - 2018/01/28 (3.00.3) - Fixed panel window border drawing bug.
  27625. /// - 2018/01/12 (3.00.2) - Added `nk_group_begin_titled` for separated group identifier and title.
  27626. /// - 2018/01/07 (3.00.1) - Started to change documentation style.
  27627. /// - 2018/01/05 (3.00.0) - BREAKING CHANGE: The previous color picker API was broken
  27628. /// because of conversions between float and byte color representation.
  27629. /// Color pickers now use floating point values to represent
  27630. /// HSV values. To get back the old behavior I added some additional
  27631. /// color conversion functions to cast between nk_color and
  27632. /// nk_colorf.
  27633. /// - 2017/12/23 (2.00.7) - Fixed small warning.
  27634. /// - 2017/12/23 (2.00.7) - Fixed `nk_edit_buffer` behavior if activated to allow input.
  27635. /// - 2017/12/23 (2.00.7) - Fixed modifyable progressbar dragging visuals and input behavior.
  27636. /// - 2017/12/04 (2.00.6) - Added formatted string tooltip widget.
  27637. /// - 2017/11/18 (2.00.5) - Fixed window becoming hidden with flag `NK_WINDOW_NO_INPUT`.
  27638. /// - 2017/11/15 (2.00.4) - Fixed font merging.
  27639. /// - 2017/11/07 (2.00.3) - Fixed window size and position modifier functions.
  27640. /// - 2017/09/14 (2.00.2) - Fixed `nk_edit_buffer` and `nk_edit_focus` behavior.
  27641. /// - 2017/09/14 (2.00.1) - Fixed window closing behavior.
  27642. /// - 2017/09/14 (2.00.0) - BREAKING CHANGE: Modifying window position and size functions now
  27643. /// require the name of the window and must happen outside the window
  27644. /// building process (between function call nk_begin and nk_end).
  27645. /// - 2017/09/11 (1.40.9) - Fixed window background flag if background window is declared last.
  27646. /// - 2017/08/27 (1.40.8) - Fixed `nk_item_is_any_active` for hidden windows.
  27647. /// - 2017/08/27 (1.40.7) - Fixed window background flag.
  27648. /// - 2017/07/07 (1.40.6) - Fixed missing clipping rect check for hovering/clicked
  27649. /// query for widgets.
  27650. /// - 2017/07/07 (1.40.5) - Fixed drawing bug for vertex output for lines and stroked
  27651. /// and filled rectangles.
  27652. /// - 2017/07/07 (1.40.4) - Fixed bug in nk_convert trying to add windows that are in
  27653. /// process of being destroyed.
  27654. /// - 2017/07/07 (1.40.3) - Fixed table internal bug caused by storing table size in
  27655. /// window instead of directly in table.
  27656. /// - 2017/06/30 (1.40.2) - Removed unneeded semicolon in C++ NK_ALIGNOF macro.
  27657. /// - 2017/06/30 (1.40.1) - Fixed drawing lines smaller or equal zero.
  27658. /// - 2017/06/08 (1.40.0) - Removed the breaking part of last commit. Auto layout now only
  27659. /// comes in effect if you pass in zero was row height argument.
  27660. /// - 2017/06/08 (1.40.0) - BREAKING CHANGE: while not directly API breaking it will change
  27661. /// how layouting works. From now there will be an internal minimum
  27662. /// row height derived from font height. If you need a row smaller than
  27663. /// that you can directly set it by `nk_layout_set_min_row_height` and
  27664. /// reset the value back by calling `nk_layout_reset_min_row_height.
  27665. /// - 2017/06/08 (1.39.1) - Fixed property text edit handling bug caused by past `nk_widget` fix.
  27666. /// - 2017/06/08 (1.39.0) - Added function to retrieve window space without calling a `nk_layout_xxx` function.
  27667. /// - 2017/06/06 (1.38.5) - Fixed `nk_convert` return flag for command buffer.
  27668. /// - 2017/05/23 (1.38.4) - Fixed activation behavior for widgets partially clipped.
  27669. /// - 2017/05/10 (1.38.3) - Fixed wrong min window size mouse scaling over boundaries.
  27670. /// - 2017/05/09 (1.38.2) - Fixed vertical scrollbar drawing with not enough space.
  27671. /// - 2017/05/09 (1.38.1) - Fixed scaler dragging behavior if window size hits minimum size.
  27672. /// - 2017/05/06 (1.38.0) - Added platform double-click support.
  27673. /// - 2017/04/20 (1.37.1) - Fixed key repeat found inside glfw demo backends.
  27674. /// - 2017/04/20 (1.37.0) - Extended properties with selection and clipboard support.
  27675. /// - 2017/04/20 (1.36.2) - Fixed #405 overlapping rows with zero padding and spacing.
  27676. /// - 2017/04/09 (1.36.1) - Fixed #403 with another widget float error.
  27677. /// - 2017/04/09 (1.36.0) - Added window `NK_WINDOW_NO_INPUT` and `NK_WINDOW_NOT_INTERACTIVE` flags.
  27678. /// - 2017/04/09 (1.35.3) - Fixed buffer heap corruption.
  27679. /// - 2017/03/25 (1.35.2) - Fixed popup overlapping for `NK_WINDOW_BACKGROUND` windows.
  27680. /// - 2017/03/25 (1.35.1) - Fixed windows closing behavior.
  27681. /// - 2017/03/18 (1.35.0) - Added horizontal scroll requested in #377.
  27682. /// - 2017/03/18 (1.34.3) - Fixed long window header titles.
  27683. /// - 2017/03/04 (1.34.2) - Fixed text edit filtering.
  27684. /// - 2017/03/04 (1.34.1) - Fixed group closable flag.
  27685. /// - 2017/02/25 (1.34.0) - Added custom draw command for better language binding support.
  27686. /// - 2017/01/24 (1.33.0) - Added programmatic way to remove edit focus.
  27687. /// - 2017/01/24 (1.32.3) - Fixed wrong define for basic type definitions for windows.
  27688. /// - 2017/01/21 (1.32.2) - Fixed input capture from hidden or closed windows.
  27689. /// - 2017/01/21 (1.32.1) - Fixed slider behavior and drawing.
  27690. /// - 2017/01/13 (1.32.0) - Added flag to put scaler into the bottom left corner.
  27691. /// - 2017/01/13 (1.31.0) - Added additional row layouting method to combine both
  27692. /// dynamic and static widgets.
  27693. /// - 2016/12/31 (1.30.0) - Extended scrollbar offset from 16-bit to 32-bit.
  27694. /// - 2016/12/31 (1.29.2) - Fixed closing window bug of minimized windows.
  27695. /// - 2016/12/03 (1.29.1) - Fixed wrapped text with no seperator and C89 error.
  27696. /// - 2016/12/03 (1.29.0) - Changed text wrapping to process words not characters.
  27697. /// - 2016/11/22 (1.28.6) - Fixed window minimized closing bug.
  27698. /// - 2016/11/19 (1.28.5) - Fixed abstract combo box closing behavior.
  27699. /// - 2016/11/19 (1.28.4) - Fixed tooltip flickering.
  27700. /// - 2016/11/19 (1.28.3) - Fixed memory leak caused by popup repeated closing.
  27701. /// - 2016/11/18 (1.28.2) - Fixed memory leak caused by popup panel allocation.
  27702. /// - 2016/11/10 (1.28.1) - Fixed some warnings and C++ error.
  27703. /// - 2016/11/10 (1.28.0) - Added additional `nk_button` versions which allows to directly
  27704. /// pass in a style struct to change buttons visual.
  27705. /// - 2016/11/10 (1.27.0) - Added additional `nk_tree` versions to support external state
  27706. /// storage. Just like last the `nk_group` commit the main
  27707. /// advantage is that you optionally can minimize nuklears runtime
  27708. /// memory consumption or handle hash collisions.
  27709. /// - 2016/11/09 (1.26.0) - Added additional `nk_group` version to support external scrollbar
  27710. /// offset storage. Main advantage is that you can externalize
  27711. /// the memory management for the offset. It could also be helpful
  27712. /// if you have a hash collision in `nk_group_begin` but really
  27713. /// want the name. In addition I added `nk_list_view` which allows
  27714. /// to draw big lists inside a group without actually having to
  27715. /// commit the whole list to nuklear (issue #269).
  27716. /// - 2016/10/30 (1.25.1) - Fixed clipping rectangle bug inside `nk_draw_list`.
  27717. /// - 2016/10/29 (1.25.0) - Pulled `nk_panel` memory management into nuklear and out of
  27718. /// the hands of the user. From now on users don't have to care
  27719. /// about panels unless they care about some information. If you
  27720. /// still need the panel just call `nk_window_get_panel`.
  27721. /// - 2016/10/21 (1.24.0) - Changed widget border drawing to stroked rectangle from filled
  27722. /// rectangle for less overdraw and widget background transparency.
  27723. /// - 2016/10/18 (1.23.0) - Added `nk_edit_focus` for manually edit widget focus control.
  27724. /// - 2016/09/29 (1.22.7) - Fixed deduction of basic type in non `<stdint.h>` compilation.
  27725. /// - 2016/09/29 (1.22.6) - Fixed edit widget UTF-8 text cursor drawing bug.
  27726. /// - 2016/09/28 (1.22.5) - Fixed edit widget UTF-8 text appending/inserting/removing.
  27727. /// - 2016/09/28 (1.22.4) - Fixed drawing bug inside edit widgets which offset all text
  27728. /// text in every edit widget if one of them is scrolled.
  27729. /// - 2016/09/28 (1.22.3) - Fixed small bug in edit widgets if not active. The wrong
  27730. /// text length is passed. It should have been in bytes but
  27731. /// was passed as glyphs.
  27732. /// - 2016/09/20 (1.22.2) - Fixed color button size calculation.
  27733. /// - 2016/09/20 (1.22.1) - Fixed some `nk_vsnprintf` behavior bugs and removed `<stdio.h>`
  27734. /// again from `NK_INCLUDE_STANDARD_VARARGS`.
  27735. /// - 2016/09/18 (1.22.0) - C89 does not support vsnprintf only C99 and newer as well
  27736. /// as C++11 and newer. In addition to use vsnprintf you have
  27737. /// to include <stdio.h>. So just defining `NK_INCLUDE_STD_VAR_ARGS`
  27738. /// is not enough. That behavior is now fixed. By default if
  27739. /// both varargs as well as stdio is selected I try to use
  27740. /// vsnprintf if not possible I will revert to vsprintf. If
  27741. /// varargs but not stdio was defined I will use my own function.
  27742. /// - 2016/09/15 (1.21.2) - Fixed panel `close` behavior for deeper panel levels.
  27743. /// - 2016/09/15 (1.21.1) - Fixed C++ errors and wrong argument to `nk_panel_get_xxxx`.
  27744. /// - 2016/09/13 (1.21.0) - !BREAKING! Fixed nonblocking popup behavior in menu, combo,
  27745. /// and contextual which prevented closing in y-direction if
  27746. /// popup did not reach max height.
  27747. /// In addition the height parameter was changed into vec2
  27748. /// for width and height to have more control over the popup size.
  27749. /// - 2016/09/13 (1.20.3) - Cleaned up and extended type selection.
  27750. /// - 2016/09/13 (1.20.2) - Fixed slider behavior hopefully for the last time. This time
  27751. /// all calculation are correct so no more hackery.
  27752. /// - 2016/09/13 (1.20.1) - Internal change to divide window/panel flags into panel flags and types.
  27753. /// Suprisinly spend years in C and still happened to confuse types
  27754. /// with flags. Probably something to take note.
  27755. /// - 2016/09/08 (1.20.0) - Added additional helper function to make it easier to just
  27756. /// take the produced buffers from `nk_convert` and unplug the
  27757. /// iteration process from `nk_context`. So now you can
  27758. /// just use the vertex,element and command buffer + two pointer
  27759. /// inside the command buffer retrieved by calls `nk__draw_begin`
  27760. /// and `nk__draw_end` and macro `nk_draw_foreach_bounded`.
  27761. /// - 2016/09/08 (1.19.0) - Added additional asserts to make sure every `nk_xxx_begin` call
  27762. /// for windows, popups, combobox, menu and contextual is guarded by
  27763. /// `if` condition and does not produce false drawing output.
  27764. /// - 2016/09/08 (1.18.0) - Changed confusing name for `NK_SYMBOL_RECT_FILLED`, `NK_SYMBOL_RECT`
  27765. /// to hopefully easier to understand `NK_SYMBOL_RECT_FILLED` and
  27766. /// `NK_SYMBOL_RECT_OUTLINE`.
  27767. /// - 2016/09/08 (1.17.0) - Changed confusing name for `NK_SYMBOL_CIRLCE_FILLED`, `NK_SYMBOL_CIRCLE`
  27768. /// to hopefully easier to understand `NK_SYMBOL_CIRCLE_FILLED` and
  27769. /// `NK_SYMBOL_CIRCLE_OUTLINE`.
  27770. /// - 2016/09/08 (1.16.0) - Added additional checks to select correct types if `NK_INCLUDE_FIXED_TYPES`
  27771. /// is not defined by supporting the biggest compiler GCC, clang and MSVC.
  27772. /// - 2016/09/07 (1.15.3) - Fixed `NK_INCLUDE_COMMAND_USERDATA` define to not cause an error.
  27773. /// - 2016/09/04 (1.15.2) - Fixed wrong combobox height calculation.
  27774. /// - 2016/09/03 (1.15.1) - Fixed gaps inside combo boxes in OpenGL.
  27775. /// - 2016/09/02 (1.15.0) - Changed nuklear to not have any default vertex layout and
  27776. /// instead made it user provided. The range of types to convert
  27777. /// to is quite limited at the moment, but I would be more than
  27778. /// happy to accept PRs to add additional.
  27779. /// - 2016/08/30 (1.14.2) - Removed unused variables.
  27780. /// - 2016/08/30 (1.14.1) - Fixed C++ build errors.
  27781. /// - 2016/08/30 (1.14.0) - Removed mouse dragging from SDL demo since it does not work correctly.
  27782. /// - 2016/08/30 (1.13.4) - Tweaked some default styling variables.
  27783. /// - 2016/08/30 (1.13.3) - Hopefully fixed drawing bug in slider, in general I would
  27784. /// refrain from using slider with a big number of steps.
  27785. /// - 2016/08/30 (1.13.2) - Fixed close and minimize button which would fire even if the
  27786. /// window was in Read Only Mode.
  27787. /// - 2016/08/30 (1.13.1) - Fixed popup panel padding handling which was previously just
  27788. /// a hack for combo box and menu.
  27789. /// - 2016/08/30 (1.13.0) - Removed `NK_WINDOW_DYNAMIC` flag from public API since
  27790. /// it is bugged and causes issues in window selection.
  27791. /// - 2016/08/30 (1.12.0) - Removed scaler size. The size of the scaler is now
  27792. /// determined by the scrollbar size.
  27793. /// - 2016/08/30 (1.11.2) - Fixed some drawing bugs caused by changes from 1.11.0.
  27794. /// - 2016/08/30 (1.11.1) - Fixed overlapping minimized window selection.
  27795. /// - 2016/08/30 (1.11.0) - Removed some internal complexity and overly complex code
  27796. /// handling panel padding and panel border.
  27797. /// - 2016/08/29 (1.10.0) - Added additional height parameter to `nk_combobox_xxx`.
  27798. /// - 2016/08/29 (1.10.0) - Fixed drawing bug in dynamic popups.
  27799. /// - 2016/08/29 (1.10.0) - Added experimental mouse scrolling to popups, menus and comboboxes.
  27800. /// - 2016/08/26 (1.10.0) - Added window name string prepresentation to account for
  27801. /// hash collisions. Currently limited to `NK_WINDOW_MAX_NAME`
  27802. /// which in term can be redefined if not big enough.
  27803. /// - 2016/08/26 (1.10.0) - Added stacks for temporary style/UI changes in code.
  27804. /// - 2016/08/25 (1.10.0) - Changed `nk_input_is_key_pressed` and 'nk_input_is_key_released'
  27805. /// to account for key press and release happening in one frame.
  27806. /// - 2016/08/25 (1.10.0) - Added additional nk_edit flag to directly jump to the end on activate.
  27807. /// - 2016/08/17 (1.09.6) - Removed invalid check for value zero in `nk_propertyx`.
  27808. /// - 2016/08/16 (1.09.5) - Fixed ROM mode for deeper levels of popup windows parents.
  27809. /// - 2016/08/15 (1.09.4) - Editbox are now still active if enter was pressed with flag
  27810. /// `NK_EDIT_SIG_ENTER`. Main reasoning is to be able to keep
  27811. /// typing after committing.
  27812. /// - 2016/08/15 (1.09.4) - Removed redundant code.
  27813. /// - 2016/08/15 (1.09.4) - Fixed negative numbers in `nk_strtoi` and remove unused variable.
  27814. /// - 2016/08/15 (1.09.3) - Fixed `NK_WINDOW_BACKGROUND` flag behavior to select a background
  27815. /// window only as selected by hovering and not by clicking.
  27816. /// - 2016/08/14 (1.09.2) - Fixed a bug in font atlas which caused wrong loading
  27817. /// of glyphs for font with multiple ranges.
  27818. /// - 2016/08/12 (1.09.1) - Added additional function to check if window is currently
  27819. /// hidden and therefore not visible.
  27820. /// - 2016/08/12 (1.09.1) - nk_window_is_closed now queries the correct flag `NK_WINDOW_CLOSED`
  27821. /// instead of the old flag `NK_WINDOW_HIDDEN`.
  27822. /// - 2016/08/09 (1.09.0) - Added additional double version to nk_property and changed
  27823. /// the underlying implementation to not cast to float and instead
  27824. /// work directly on the given values.
  27825. /// - 2016/08/09 (1.08.0) - Added additional define to overwrite library internal
  27826. /// floating pointer number to string conversion for additional
  27827. /// precision.
  27828. /// - 2016/08/09 (1.08.0) - Added additional define to overwrite library internal
  27829. /// string to floating point number conversion for additional
  27830. /// precision.
  27831. /// - 2016/08/08 (1.07.2) - Fixed compiling error without define `NK_INCLUDE_FIXED_TYPE`.
  27832. /// - 2016/08/08 (1.07.1) - Fixed possible floating point error inside `nk_widget` leading
  27833. /// to wrong wiget width calculation which results in widgets falsely
  27834. /// becoming tagged as not inside window and cannot be accessed.
  27835. /// - 2016/08/08 (1.07.0) - Nuklear now differentiates between hiding a window (NK_WINDOW_HIDDEN) and
  27836. /// closing a window (NK_WINDOW_CLOSED). A window can be hidden/shown
  27837. /// by using `nk_window_show` and closed by either clicking the close
  27838. /// icon in a window or by calling `nk_window_close`. Only closed
  27839. /// windows get removed at the end of the frame while hidden windows
  27840. /// remain.
  27841. /// - 2016/08/08 (1.06.0) - Added `nk_edit_string_zero_terminated` as a second option to
  27842. /// `nk_edit_string` which takes, edits and outputs a '\0' terminated string.
  27843. /// - 2016/08/08 (1.05.4) - Fixed scrollbar auto hiding behavior.
  27844. /// - 2016/08/08 (1.05.3) - Fixed wrong panel padding selection in `nk_layout_widget_space`.
  27845. /// - 2016/08/07 (1.05.2) - Fixed old bug in dynamic immediate mode layout API, calculating
  27846. /// wrong item spacing and panel width.
  27847. /// - 2016/08/07 (1.05.1) - Hopefully finally fixed combobox popup drawing bug.
  27848. /// - 2016/08/07 (1.05.0) - Split varargs away from `NK_INCLUDE_STANDARD_IO` into own
  27849. /// define `NK_INCLUDE_STANDARD_VARARGS` to allow more fine
  27850. /// grained controlled over library includes.
  27851. /// - 2016/08/06 (1.04.5) - Changed memset calls to `NK_MEMSET`.
  27852. /// - 2016/08/04 (1.04.4) - Fixed fast window scaling behavior.
  27853. /// - 2016/08/04 (1.04.3) - Fixed window scaling, movement bug which appears if you
  27854. /// move/scale a window and another window is behind it.
  27855. /// If you are fast enough then the window behind gets activated
  27856. /// and the operation is blocked. I now require activating
  27857. /// by hovering only if mouse is not pressed.
  27858. /// - 2016/08/04 (1.04.2) - Fixed changing fonts.
  27859. /// - 2016/08/03 (1.04.1) - Fixed `NK_WINDOW_BACKGROUND` behavior.
  27860. /// - 2016/08/03 (1.04.0) - Added color parameter to `nk_draw_image`.
  27861. /// - 2016/08/03 (1.04.0) - Added additional window padding style attributes for
  27862. /// sub windows (combo, menu, ...).
  27863. /// - 2016/08/03 (1.04.0) - Added functions to show/hide software cursor.
  27864. /// - 2016/08/03 (1.04.0) - Added `NK_WINDOW_BACKGROUND` flag to force a window
  27865. /// to be always in the background of the screen.
  27866. /// - 2016/08/03 (1.03.2) - Removed invalid assert macro for NK_RGB color picker.
  27867. /// - 2016/08/01 (1.03.1) - Added helper macros into header include guard.
  27868. /// - 2016/07/29 (1.03.0) - Moved the window/table pool into the header part to
  27869. /// simplify memory management by removing the need to
  27870. /// allocate the pool.
  27871. /// - 2016/07/29 (1.02.0) - Added auto scrollbar hiding window flag which if enabled
  27872. /// will hide the window scrollbar after NK_SCROLLBAR_HIDING_TIMEOUT
  27873. /// seconds without window interaction. To make it work
  27874. /// you have to also set a delta time inside the `nk_context`.
  27875. /// - 2016/07/25 (1.01.1) - Fixed small panel and panel border drawing bugs.
  27876. /// - 2016/07/15 (1.01.0) - Added software cursor to `nk_style` and `nk_context`.
  27877. /// - 2016/07/15 (1.01.0) - Added const correctness to `nk_buffer_push' data argument.
  27878. /// - 2016/07/15 (1.01.0) - Removed internal font baking API and simplified
  27879. /// font atlas memory management by converting pointer
  27880. /// arrays for fonts and font configurations to lists.
  27881. /// - 2016/07/15 (1.00.0) - Changed button API to use context dependent button
  27882. /// behavior instead of passing it for every function call.
  27883. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  27884. /// ## Gallery
  27885. /// ![Figure [blue]: Feature overview with blue color styling](https://cloud.githubusercontent.com/assets/8057201/13538240/acd96876-e249-11e5-9547-5ac0b19667a0.png)
  27886. /// ![Figure [red]: Feature overview with red color styling](https://cloud.githubusercontent.com/assets/8057201/13538243/b04acd4c-e249-11e5-8fd2-ad7744a5b446.png)
  27887. /// ![Figure [widgets]: Widget overview](https://cloud.githubusercontent.com/assets/8057201/11282359/3325e3c6-8eff-11e5-86cb-cf02b0596087.png)
  27888. /// ![Figure [blackwhite]: Black and white](https://cloud.githubusercontent.com/assets/8057201/11033668/59ab5d04-86e5-11e5-8091-c56f16411565.png)
  27889. /// ![Figure [filexp]: File explorer](https://cloud.githubusercontent.com/assets/8057201/10718115/02a9ba08-7b6b-11e5-950f-adacdd637739.png)
  27890. /// ![Figure [opengl]: OpenGL Editor](https://cloud.githubusercontent.com/assets/8057201/12779619/2a20d72c-ca69-11e5-95fe-4edecf820d5c.png)
  27891. /// ![Figure [nodedit]: Node Editor](https://cloud.githubusercontent.com/assets/8057201/9976995/e81ac04a-5ef7-11e5-872b-acd54fbeee03.gif)
  27892. /// ![Figure [skinning]: Using skinning in Nuklear](https://cloud.githubusercontent.com/assets/8057201/15991632/76494854-30b8-11e6-9555-a69840d0d50b.png)
  27893. /// ![Figure [bf]: Heavy modified version](https://cloud.githubusercontent.com/assets/8057201/14902576/339926a8-0d9c-11e6-9fee-a8b73af04473.png)
  27894. ///
  27895. /// ## Credits
  27896. /// Developed by Micha Mettke and every direct or indirect github contributor. <br /><br />
  27897. ///
  27898. /// Embeds [stb_texedit](https://github.com/nothings/stb/blob/master/stb_textedit.h), [stb_truetype](https://github.com/nothings/stb/blob/master/stb_truetype.h) and [stb_rectpack](https://github.com/nothings/stb/blob/master/stb_rect_pack.h) by Sean Barret (public domain) <br />
  27899. /// Uses [stddoc.c](https://github.com/r-lyeh/stddoc.c) from r-lyeh@github.com for documentation generation <br /><br />
  27900. /// Embeds ProggyClean.ttf font by Tristan Grimmer (MIT license). <br />
  27901. ///
  27902. /// Big thank you to Omar Cornut (ocornut@github) for his [imgui library](https://github.com/ocornut/imgui) and
  27903. /// giving me the inspiration for this library, Casey Muratori for handmade hero
  27904. /// and his original immediate mode graphical user interface idea and Sean
  27905. /// Barret for his amazing single header libraries which restored my faith
  27906. /// in libraries and brought me to create some of my own. Finally Apoorva Joshi
  27907. /// for his single header file packer.
  27908. */