nuklear.h 1.1 MB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910179111791217913179141791517916179171791817919179201792117922179231792417925179261792717928179291793017931179321793317934179351793617937179381793917940179411794217943179441794517946179471794817949179501795117952179531795417955179561795717958179591796017961179621796317964179651796617967179681796917970179711797217973179741797517976179771797817979179801798117982179831798417985179861798717988179891799017991179921799317994179951799617997179981799918000180011800218003180041800518006180071800818009180101801118012180131801418015180161801718018180191802018021180221802318024180251802618027180281802918030180311803218033180341803518036180371803818039180401804118042180431804418045180461804718048180491805018051180521805318054180551805618057180581805918060180611806218063180641806518066180671806818069180701807118072180731807418075180761807718078180791808018081180821808318084180851808618087180881808918090180911809218093180941809518096180971809818099181001810118102181031810418105181061810718108181091811018111181121811318114181151811618117181181811918120181211812218123181241812518126181271812818129181301813118132181331813418135181361813718138181391814018141181421814318144181451814618147181481814918150181511815218153181541815518156181571815818159181601816118162181631816418165181661816718168181691817018171181721817318174181751817618177181781817918180181811818218183181841818518186181871818818189181901819118192181931819418195181961819718198181991820018201182021820318204182051820618207182081820918210182111821218213182141821518216182171821818219182201822118222182231822418225182261822718228182291823018231182321823318234182351823618237182381823918240182411824218243182441824518246182471824818249182501825118252182531825418255182561825718258182591826018261182621826318264182651826618267182681826918270182711827218273182741827518276182771827818279182801828118282182831828418285182861828718288182891829018291182921829318294182951829618297182981829918300183011830218303183041830518306183071830818309183101831118312183131831418315183161831718318183191832018321183221832318324183251832618327183281832918330183311833218333183341833518336183371833818339183401834118342183431834418345183461834718348183491835018351183521835318354183551835618357183581835918360183611836218363183641836518366183671836818369183701837118372183731837418375183761837718378183791838018381183821838318384183851838618387183881838918390183911839218393183941839518396183971839818399184001840118402184031840418405184061840718408184091841018411184121841318414184151841618417184181841918420184211842218423184241842518426184271842818429184301843118432184331843418435184361843718438184391844018441184421844318444184451844618447184481844918450184511845218453184541845518456184571845818459184601846118462184631846418465184661846718468184691847018471184721847318474184751847618477184781847918480184811848218483184841848518486184871848818489184901849118492184931849418495184961849718498184991850018501185021850318504185051850618507185081850918510185111851218513185141851518516185171851818519185201852118522185231852418525185261852718528185291853018531185321853318534185351853618537185381853918540185411854218543185441854518546185471854818549185501855118552185531855418555185561855718558185591856018561185621856318564185651856618567185681856918570185711857218573185741857518576185771857818579185801858118582185831858418585185861858718588185891859018591185921859318594185951859618597185981859918600186011860218603186041860518606186071860818609186101861118612186131861418615186161861718618186191862018621186221862318624186251862618627186281862918630186311863218633186341863518636186371863818639186401864118642186431864418645186461864718648186491865018651186521865318654186551865618657186581865918660186611866218663186641866518666186671866818669186701867118672186731867418675186761867718678186791868018681186821868318684186851868618687186881868918690186911869218693186941869518696186971869818699187001870118702187031870418705187061870718708187091871018711187121871318714187151871618717187181871918720187211872218723187241872518726187271872818729187301873118732187331873418735187361873718738187391874018741187421874318744187451874618747187481874918750187511875218753187541875518756187571875818759187601876118762187631876418765187661876718768187691877018771187721877318774187751877618777187781877918780187811878218783187841878518786187871878818789187901879118792187931879418795187961879718798187991880018801188021880318804188051880618807188081880918810188111881218813188141881518816188171881818819188201882118822188231882418825188261882718828188291883018831188321883318834188351883618837188381883918840188411884218843188441884518846188471884818849188501885118852188531885418855188561885718858188591886018861188621886318864188651886618867188681886918870188711887218873188741887518876188771887818879188801888118882188831888418885188861888718888188891889018891188921889318894188951889618897188981889918900189011890218903189041890518906189071890818909189101891118912189131891418915189161891718918189191892018921189221892318924189251892618927189281892918930189311893218933189341893518936189371893818939189401894118942189431894418945189461894718948189491895018951189521895318954189551895618957189581895918960189611896218963189641896518966189671896818969189701897118972189731897418975189761897718978189791898018981189821898318984189851898618987189881898918990189911899218993189941899518996189971899818999190001900119002190031900419005190061900719008190091901019011190121901319014190151901619017190181901919020190211902219023190241902519026190271902819029190301903119032190331903419035190361903719038190391904019041190421904319044190451904619047190481904919050190511905219053190541905519056190571905819059190601906119062190631906419065190661906719068190691907019071190721907319074190751907619077190781907919080190811908219083190841908519086190871908819089190901909119092190931909419095190961909719098190991910019101191021910319104191051910619107191081910919110191111911219113191141911519116191171911819119191201912119122191231912419125191261912719128191291913019131191321913319134191351913619137191381913919140191411914219143191441914519146191471914819149191501915119152191531915419155191561915719158191591916019161191621916319164191651916619167191681916919170191711917219173191741917519176191771917819179191801918119182191831918419185191861918719188191891919019191191921919319194191951919619197191981919919200192011920219203192041920519206192071920819209192101921119212192131921419215192161921719218192191922019221192221922319224192251922619227192281922919230192311923219233192341923519236192371923819239192401924119242192431924419245192461924719248192491925019251192521925319254192551925619257192581925919260192611926219263192641926519266192671926819269192701927119272192731927419275192761927719278192791928019281192821928319284192851928619287192881928919290192911929219293192941929519296192971929819299193001930119302193031930419305193061930719308193091931019311193121931319314193151931619317193181931919320193211932219323193241932519326193271932819329193301933119332193331933419335193361933719338193391934019341193421934319344193451934619347193481934919350193511935219353193541935519356193571935819359193601936119362193631936419365193661936719368193691937019371193721937319374193751937619377193781937919380193811938219383193841938519386193871938819389193901939119392193931939419395193961939719398193991940019401194021940319404194051940619407194081940919410194111941219413194141941519416194171941819419194201942119422194231942419425194261942719428194291943019431194321943319434194351943619437194381943919440194411944219443194441944519446194471944819449194501945119452194531945419455194561945719458194591946019461194621946319464194651946619467194681946919470194711947219473194741947519476194771947819479194801948119482194831948419485194861948719488194891949019491194921949319494194951949619497194981949919500195011950219503195041950519506195071950819509195101951119512195131951419515195161951719518195191952019521195221952319524195251952619527195281952919530195311953219533195341953519536195371953819539195401954119542195431954419545195461954719548195491955019551195521955319554195551955619557195581955919560195611956219563195641956519566195671956819569195701957119572195731957419575195761957719578195791958019581195821958319584195851958619587195881958919590195911959219593195941959519596195971959819599196001960119602196031960419605196061960719608196091961019611196121961319614196151961619617196181961919620196211962219623196241962519626196271962819629196301963119632196331963419635196361963719638196391964019641196421964319644196451964619647196481964919650196511965219653196541965519656196571965819659196601966119662196631966419665196661966719668196691967019671196721967319674196751967619677196781967919680196811968219683196841968519686196871968819689196901969119692196931969419695196961969719698196991970019701197021970319704197051970619707197081970919710197111971219713197141971519716197171971819719197201972119722197231972419725197261972719728197291973019731197321973319734197351973619737197381973919740197411974219743197441974519746197471974819749197501975119752197531975419755197561975719758197591976019761197621976319764197651976619767197681976919770197711977219773197741977519776197771977819779197801978119782197831978419785197861978719788197891979019791197921979319794197951979619797197981979919800198011980219803198041980519806198071980819809198101981119812198131981419815198161981719818198191982019821198221982319824198251982619827198281982919830198311983219833198341983519836198371983819839198401984119842198431984419845198461984719848198491985019851198521985319854198551985619857198581985919860198611986219863198641986519866198671986819869198701987119872198731987419875198761987719878198791988019881198821988319884198851988619887198881988919890198911989219893198941989519896198971989819899199001990119902199031990419905199061990719908199091991019911199121991319914199151991619917199181991919920199211992219923199241992519926199271992819929199301993119932199331993419935199361993719938199391994019941199421994319944199451994619947199481994919950199511995219953199541995519956199571995819959199601996119962199631996419965199661996719968199691997019971199721997319974199751997619977199781997919980199811998219983199841998519986199871998819989199901999119992199931999419995199961999719998199992000020001200022000320004200052000620007200082000920010200112001220013200142001520016200172001820019200202002120022200232002420025200262002720028200292003020031200322003320034200352003620037200382003920040200412004220043200442004520046200472004820049200502005120052200532005420055200562005720058200592006020061200622006320064200652006620067200682006920070200712007220073200742007520076200772007820079200802008120082200832008420085200862008720088200892009020091200922009320094200952009620097200982009920100201012010220103201042010520106201072010820109201102011120112201132011420115201162011720118201192012020121201222012320124201252012620127201282012920130201312013220133201342013520136201372013820139201402014120142201432014420145201462014720148201492015020151201522015320154201552015620157201582015920160201612016220163201642016520166201672016820169201702017120172201732017420175201762017720178201792018020181201822018320184201852018620187201882018920190201912019220193201942019520196201972019820199202002020120202202032020420205202062020720208202092021020211202122021320214202152021620217202182021920220202212022220223202242022520226202272022820229202302023120232202332023420235202362023720238202392024020241202422024320244202452024620247202482024920250202512025220253202542025520256202572025820259202602026120262202632026420265202662026720268202692027020271202722027320274202752027620277202782027920280202812028220283202842028520286202872028820289202902029120292202932029420295202962029720298202992030020301203022030320304203052030620307203082030920310203112031220313203142031520316203172031820319203202032120322203232032420325203262032720328203292033020331203322033320334203352033620337203382033920340203412034220343203442034520346203472034820349203502035120352203532035420355203562035720358203592036020361203622036320364203652036620367203682036920370203712037220373203742037520376203772037820379203802038120382203832038420385203862038720388203892039020391203922039320394203952039620397203982039920400204012040220403204042040520406204072040820409204102041120412204132041420415204162041720418204192042020421204222042320424204252042620427204282042920430204312043220433204342043520436204372043820439204402044120442204432044420445204462044720448204492045020451204522045320454204552045620457204582045920460204612046220463204642046520466204672046820469204702047120472204732047420475204762047720478204792048020481204822048320484204852048620487204882048920490204912049220493204942049520496204972049820499205002050120502205032050420505205062050720508205092051020511205122051320514205152051620517205182051920520205212052220523205242052520526205272052820529205302053120532205332053420535205362053720538205392054020541205422054320544205452054620547205482054920550205512055220553205542055520556205572055820559205602056120562205632056420565205662056720568205692057020571205722057320574205752057620577205782057920580205812058220583205842058520586205872058820589205902059120592205932059420595205962059720598205992060020601206022060320604206052060620607206082060920610206112061220613206142061520616206172061820619206202062120622206232062420625206262062720628206292063020631206322063320634206352063620637206382063920640206412064220643206442064520646206472064820649206502065120652206532065420655206562065720658206592066020661206622066320664206652066620667206682066920670206712067220673206742067520676206772067820679206802068120682206832068420685206862068720688206892069020691206922069320694206952069620697206982069920700207012070220703207042070520706207072070820709207102071120712207132071420715207162071720718207192072020721207222072320724207252072620727207282072920730207312073220733207342073520736207372073820739207402074120742207432074420745207462074720748207492075020751207522075320754207552075620757207582075920760207612076220763207642076520766207672076820769207702077120772207732077420775207762077720778207792078020781207822078320784207852078620787207882078920790207912079220793207942079520796207972079820799208002080120802208032080420805208062080720808208092081020811208122081320814208152081620817208182081920820208212082220823208242082520826208272082820829208302083120832208332083420835208362083720838208392084020841208422084320844208452084620847208482084920850208512085220853208542085520856208572085820859208602086120862208632086420865208662086720868208692087020871208722087320874208752087620877208782087920880208812088220883208842088520886208872088820889208902089120892208932089420895208962089720898208992090020901209022090320904209052090620907209082090920910209112091220913209142091520916209172091820919209202092120922209232092420925209262092720928209292093020931209322093320934209352093620937209382093920940209412094220943209442094520946209472094820949209502095120952209532095420955209562095720958209592096020961209622096320964209652096620967209682096920970209712097220973209742097520976209772097820979209802098120982209832098420985209862098720988209892099020991209922099320994209952099620997209982099921000210012100221003210042100521006210072100821009210102101121012210132101421015210162101721018210192102021021210222102321024210252102621027210282102921030210312103221033210342103521036210372103821039210402104121042210432104421045210462104721048210492105021051210522105321054210552105621057210582105921060210612106221063210642106521066210672106821069210702107121072210732107421075210762107721078210792108021081210822108321084210852108621087210882108921090210912109221093210942109521096210972109821099211002110121102211032110421105211062110721108211092111021111211122111321114211152111621117211182111921120211212112221123211242112521126211272112821129211302113121132211332113421135211362113721138211392114021141211422114321144211452114621147211482114921150211512115221153211542115521156211572115821159211602116121162211632116421165211662116721168211692117021171211722117321174211752117621177211782117921180211812118221183211842118521186211872118821189211902119121192211932119421195211962119721198211992120021201212022120321204212052120621207212082120921210212112121221213212142121521216212172121821219212202122121222212232122421225212262122721228212292123021231212322123321234212352123621237212382123921240212412124221243212442124521246212472124821249212502125121252212532125421255212562125721258212592126021261212622126321264212652126621267212682126921270212712127221273212742127521276212772127821279212802128121282212832128421285212862128721288212892129021291212922129321294212952129621297212982129921300213012130221303213042130521306213072130821309213102131121312213132131421315213162131721318213192132021321213222132321324213252132621327213282132921330213312133221333213342133521336213372133821339213402134121342213432134421345213462134721348213492135021351213522135321354213552135621357213582135921360213612136221363213642136521366213672136821369213702137121372213732137421375213762137721378213792138021381213822138321384213852138621387213882138921390213912139221393213942139521396213972139821399214002140121402214032140421405214062140721408214092141021411214122141321414214152141621417214182141921420214212142221423214242142521426214272142821429214302143121432214332143421435214362143721438214392144021441214422144321444214452144621447214482144921450214512145221453214542145521456214572145821459214602146121462214632146421465214662146721468214692147021471214722147321474214752147621477214782147921480214812148221483214842148521486214872148821489214902149121492214932149421495214962149721498214992150021501215022150321504215052150621507215082150921510215112151221513215142151521516215172151821519215202152121522215232152421525215262152721528215292153021531215322153321534215352153621537215382153921540215412154221543215442154521546215472154821549215502155121552215532155421555215562155721558215592156021561215622156321564215652156621567215682156921570215712157221573215742157521576215772157821579215802158121582215832158421585215862158721588215892159021591215922159321594215952159621597215982159921600216012160221603216042160521606216072160821609216102161121612216132161421615216162161721618216192162021621216222162321624216252162621627216282162921630216312163221633216342163521636216372163821639216402164121642216432164421645216462164721648216492165021651216522165321654216552165621657216582165921660216612166221663216642166521666216672166821669216702167121672216732167421675216762167721678216792168021681216822168321684216852168621687216882168921690216912169221693216942169521696216972169821699217002170121702217032170421705217062170721708217092171021711217122171321714217152171621717217182171921720217212172221723217242172521726217272172821729217302173121732217332173421735217362173721738217392174021741217422174321744217452174621747217482174921750217512175221753217542175521756217572175821759217602176121762217632176421765217662176721768217692177021771217722177321774217752177621777217782177921780217812178221783217842178521786217872178821789217902179121792217932179421795217962179721798217992180021801218022180321804218052180621807218082180921810218112181221813218142181521816218172181821819218202182121822218232182421825218262182721828218292183021831218322183321834218352183621837218382183921840218412184221843218442184521846218472184821849218502185121852218532185421855218562185721858218592186021861218622186321864218652186621867218682186921870218712187221873218742187521876218772187821879218802188121882218832188421885218862188721888218892189021891218922189321894218952189621897218982189921900219012190221903219042190521906219072190821909219102191121912219132191421915219162191721918219192192021921219222192321924219252192621927219282192921930219312193221933219342193521936219372193821939219402194121942219432194421945219462194721948219492195021951219522195321954219552195621957219582195921960219612196221963219642196521966219672196821969219702197121972219732197421975219762197721978219792198021981219822198321984219852198621987219882198921990219912199221993219942199521996219972199821999220002200122002220032200422005220062200722008220092201022011220122201322014220152201622017220182201922020220212202222023220242202522026220272202822029220302203122032220332203422035220362203722038220392204022041220422204322044220452204622047220482204922050220512205222053220542205522056220572205822059220602206122062220632206422065220662206722068220692207022071220722207322074220752207622077220782207922080220812208222083220842208522086220872208822089220902209122092220932209422095220962209722098220992210022101221022210322104221052210622107221082210922110221112211222113221142211522116221172211822119221202212122122221232212422125221262212722128221292213022131221322213322134221352213622137221382213922140221412214222143221442214522146221472214822149221502215122152221532215422155221562215722158221592216022161221622216322164221652216622167221682216922170221712217222173221742217522176221772217822179221802218122182221832218422185221862218722188221892219022191221922219322194221952219622197221982219922200222012220222203222042220522206222072220822209222102221122212222132221422215222162221722218222192222022221222222222322224222252222622227222282222922230222312223222233222342223522236222372223822239222402224122242222432224422245222462224722248222492225022251222522225322254222552225622257222582225922260222612226222263222642226522266222672226822269222702227122272222732227422275222762227722278222792228022281222822228322284222852228622287222882228922290222912229222293222942229522296222972229822299223002230122302223032230422305223062230722308223092231022311223122231322314223152231622317223182231922320223212232222323223242232522326223272232822329223302233122332223332233422335223362233722338223392234022341223422234322344223452234622347223482234922350223512235222353223542235522356223572235822359223602236122362223632236422365223662236722368223692237022371223722237322374223752237622377223782237922380223812238222383223842238522386223872238822389223902239122392223932239422395223962239722398223992240022401224022240322404224052240622407224082240922410224112241222413224142241522416224172241822419224202242122422224232242422425224262242722428224292243022431224322243322434224352243622437224382243922440224412244222443224442244522446224472244822449224502245122452224532245422455224562245722458224592246022461224622246322464224652246622467224682246922470224712247222473224742247522476224772247822479224802248122482224832248422485224862248722488224892249022491224922249322494224952249622497224982249922500225012250222503225042250522506225072250822509225102251122512225132251422515225162251722518225192252022521225222252322524225252252622527225282252922530225312253222533225342253522536225372253822539225402254122542225432254422545225462254722548225492255022551225522255322554225552255622557225582255922560225612256222563225642256522566225672256822569225702257122572225732257422575225762257722578225792258022581225822258322584225852258622587225882258922590225912259222593225942259522596225972259822599226002260122602226032260422605226062260722608226092261022611226122261322614226152261622617226182261922620226212262222623226242262522626226272262822629226302263122632226332263422635226362263722638226392264022641226422264322644226452264622647226482264922650226512265222653226542265522656226572265822659226602266122662226632266422665226662266722668226692267022671226722267322674226752267622677226782267922680226812268222683226842268522686226872268822689226902269122692226932269422695226962269722698226992270022701227022270322704227052270622707227082270922710227112271222713227142271522716227172271822719227202272122722227232272422725227262272722728227292273022731227322273322734227352273622737227382273922740227412274222743227442274522746227472274822749227502275122752227532275422755227562275722758227592276022761227622276322764227652276622767227682276922770227712277222773227742277522776227772277822779227802278122782227832278422785227862278722788227892279022791227922279322794227952279622797227982279922800228012280222803228042280522806228072280822809228102281122812228132281422815228162281722818228192282022821228222282322824228252282622827228282282922830228312283222833228342283522836228372283822839228402284122842228432284422845228462284722848228492285022851228522285322854228552285622857228582285922860228612286222863228642286522866228672286822869228702287122872228732287422875228762287722878228792288022881228822288322884228852288622887228882288922890228912289222893228942289522896228972289822899229002290122902229032290422905229062290722908229092291022911229122291322914229152291622917229182291922920229212292222923229242292522926229272292822929229302293122932229332293422935229362293722938229392294022941229422294322944229452294622947229482294922950229512295222953229542295522956229572295822959229602296122962229632296422965229662296722968229692297022971229722297322974229752297622977229782297922980229812298222983229842298522986229872298822989229902299122992229932299422995229962299722998229992300023001230022300323004230052300623007230082300923010230112301223013230142301523016230172301823019230202302123022230232302423025230262302723028230292303023031230322303323034230352303623037230382303923040230412304223043230442304523046230472304823049230502305123052230532305423055230562305723058230592306023061230622306323064230652306623067230682306923070230712307223073230742307523076230772307823079230802308123082230832308423085230862308723088230892309023091230922309323094230952309623097230982309923100231012310223103231042310523106231072310823109231102311123112231132311423115231162311723118231192312023121231222312323124231252312623127231282312923130231312313223133231342313523136231372313823139231402314123142231432314423145231462314723148231492315023151231522315323154231552315623157231582315923160231612316223163231642316523166231672316823169231702317123172231732317423175231762317723178231792318023181231822318323184231852318623187231882318923190231912319223193231942319523196231972319823199232002320123202232032320423205232062320723208232092321023211232122321323214232152321623217232182321923220232212322223223232242322523226232272322823229232302323123232232332323423235232362323723238232392324023241232422324323244232452324623247232482324923250232512325223253232542325523256232572325823259232602326123262232632326423265232662326723268232692327023271232722327323274232752327623277232782327923280232812328223283232842328523286232872328823289232902329123292232932329423295232962329723298232992330023301233022330323304233052330623307233082330923310233112331223313233142331523316233172331823319233202332123322233232332423325233262332723328233292333023331233322333323334233352333623337233382333923340233412334223343233442334523346233472334823349233502335123352233532335423355233562335723358233592336023361233622336323364233652336623367233682336923370233712337223373233742337523376233772337823379233802338123382233832338423385233862338723388233892339023391233922339323394233952339623397233982339923400234012340223403234042340523406234072340823409234102341123412234132341423415234162341723418234192342023421234222342323424234252342623427234282342923430234312343223433234342343523436234372343823439234402344123442234432344423445234462344723448234492345023451234522345323454234552345623457234582345923460234612346223463234642346523466234672346823469234702347123472234732347423475234762347723478234792348023481234822348323484234852348623487234882348923490234912349223493234942349523496234972349823499235002350123502235032350423505235062350723508235092351023511235122351323514235152351623517235182351923520235212352223523235242352523526235272352823529235302353123532235332353423535235362353723538235392354023541235422354323544235452354623547235482354923550235512355223553235542355523556235572355823559235602356123562235632356423565235662356723568235692357023571235722357323574235752357623577235782357923580235812358223583235842358523586235872358823589235902359123592235932359423595235962359723598235992360023601236022360323604236052360623607236082360923610236112361223613236142361523616236172361823619236202362123622236232362423625236262362723628236292363023631236322363323634236352363623637236382363923640236412364223643236442364523646236472364823649236502365123652236532365423655236562365723658236592366023661236622366323664236652366623667236682366923670236712367223673236742367523676236772367823679236802368123682236832368423685236862368723688236892369023691236922369323694236952369623697236982369923700237012370223703237042370523706237072370823709237102371123712237132371423715237162371723718237192372023721237222372323724237252372623727237282372923730237312373223733237342373523736237372373823739237402374123742237432374423745237462374723748237492375023751237522375323754237552375623757237582375923760237612376223763237642376523766237672376823769237702377123772237732377423775237762377723778237792378023781237822378323784237852378623787237882378923790237912379223793237942379523796237972379823799238002380123802238032380423805238062380723808238092381023811238122381323814238152381623817238182381923820238212382223823238242382523826238272382823829238302383123832238332383423835238362383723838238392384023841238422384323844238452384623847238482384923850238512385223853238542385523856238572385823859238602386123862238632386423865238662386723868238692387023871238722387323874238752387623877238782387923880238812388223883238842388523886238872388823889238902389123892238932389423895238962389723898238992390023901239022390323904239052390623907239082390923910239112391223913239142391523916239172391823919239202392123922239232392423925239262392723928239292393023931239322393323934239352393623937239382393923940239412394223943239442394523946239472394823949239502395123952239532395423955239562395723958239592396023961239622396323964239652396623967239682396923970239712397223973239742397523976239772397823979239802398123982239832398423985239862398723988239892399023991239922399323994239952399623997239982399924000240012400224003240042400524006240072400824009240102401124012240132401424015240162401724018240192402024021240222402324024240252402624027240282402924030240312403224033240342403524036240372403824039240402404124042240432404424045240462404724048240492405024051240522405324054240552405624057240582405924060240612406224063240642406524066240672406824069240702407124072240732407424075240762407724078240792408024081240822408324084240852408624087240882408924090240912409224093240942409524096240972409824099241002410124102241032410424105241062410724108241092411024111241122411324114241152411624117241182411924120241212412224123241242412524126241272412824129241302413124132241332413424135241362413724138241392414024141241422414324144241452414624147241482414924150241512415224153241542415524156241572415824159241602416124162241632416424165241662416724168241692417024171241722417324174241752417624177241782417924180241812418224183241842418524186241872418824189241902419124192241932419424195241962419724198241992420024201242022420324204242052420624207242082420924210242112421224213242142421524216242172421824219242202422124222242232422424225242262422724228242292423024231242322423324234242352423624237242382423924240242412424224243242442424524246242472424824249242502425124252242532425424255242562425724258242592426024261242622426324264242652426624267242682426924270242712427224273242742427524276242772427824279242802428124282242832428424285242862428724288242892429024291242922429324294242952429624297242982429924300243012430224303243042430524306243072430824309243102431124312243132431424315243162431724318243192432024321243222432324324243252432624327243282432924330243312433224333243342433524336243372433824339243402434124342243432434424345243462434724348243492435024351243522435324354243552435624357243582435924360243612436224363243642436524366243672436824369243702437124372243732437424375243762437724378243792438024381243822438324384243852438624387243882438924390243912439224393243942439524396243972439824399244002440124402244032440424405244062440724408244092441024411244122441324414244152441624417244182441924420244212442224423244242442524426244272442824429244302443124432244332443424435244362443724438244392444024441244422444324444244452444624447244482444924450244512445224453244542445524456244572445824459244602446124462244632446424465244662446724468244692447024471244722447324474244752447624477244782447924480244812448224483244842448524486244872448824489244902449124492244932449424495244962449724498244992450024501245022450324504245052450624507245082450924510245112451224513245142451524516245172451824519245202452124522245232452424525245262452724528245292453024531245322453324534245352453624537245382453924540245412454224543245442454524546245472454824549245502455124552245532455424555245562455724558245592456024561245622456324564245652456624567245682456924570245712457224573245742457524576245772457824579245802458124582245832458424585245862458724588245892459024591245922459324594245952459624597245982459924600246012460224603246042460524606246072460824609246102461124612246132461424615246162461724618246192462024621246222462324624246252462624627246282462924630246312463224633246342463524636246372463824639246402464124642246432464424645246462464724648246492465024651246522465324654246552465624657246582465924660246612466224663246642466524666246672466824669246702467124672246732467424675246762467724678246792468024681246822468324684246852468624687246882468924690246912469224693246942469524696246972469824699247002470124702247032470424705247062470724708247092471024711247122471324714247152471624717247182471924720247212472224723247242472524726247272472824729247302473124732247332473424735247362473724738247392474024741247422474324744247452474624747247482474924750247512475224753247542475524756247572475824759247602476124762247632476424765247662476724768247692477024771247722477324774247752477624777247782477924780247812478224783247842478524786247872478824789247902479124792247932479424795247962479724798247992480024801248022480324804248052480624807248082480924810248112481224813248142481524816248172481824819248202482124822248232482424825248262482724828248292483024831248322483324834248352483624837248382483924840248412484224843248442484524846248472484824849248502485124852248532485424855248562485724858248592486024861248622486324864248652486624867248682486924870248712487224873248742487524876248772487824879248802488124882248832488424885248862488724888248892489024891248922489324894248952489624897248982489924900249012490224903249042490524906249072490824909249102491124912249132491424915249162491724918249192492024921249222492324924249252492624927249282492924930249312493224933249342493524936249372493824939249402494124942249432494424945249462494724948249492495024951249522495324954249552495624957249582495924960249612496224963249642496524966249672496824969249702497124972249732497424975249762497724978249792498024981249822498324984249852498624987249882498924990249912499224993249942499524996249972499824999250002500125002250032500425005250062500725008250092501025011250122501325014250152501625017250182501925020250212502225023250242502525026250272502825029250302503125032250332503425035250362503725038250392504025041250422504325044250452504625047250482504925050250512505225053250542505525056250572505825059250602506125062250632506425065250662506725068250692507025071250722507325074250752507625077250782507925080250812508225083250842508525086250872508825089250902509125092250932509425095250962509725098250992510025101251022510325104251052510625107251082510925110251112511225113251142511525116251172511825119251202512125122251232512425125251262512725128251292513025131251322513325134251352513625137251382513925140251412514225143251442514525146251472514825149251502515125152251532515425155251562515725158251592516025161251622516325164251652516625167251682516925170251712517225173251742517525176251772517825179251802518125182251832518425185251862518725188251892519025191251922519325194251952519625197251982519925200252012520225203252042520525206252072520825209252102521125212252132521425215252162521725218252192522025221252222522325224252252522625227252282522925230252312523225233252342523525236252372523825239252402524125242252432524425245252462524725248252492525025251252522525325254252552525625257252582525925260252612526225263252642526525266252672526825269252702527125272252732527425275252762527725278252792528025281252822528325284252852528625287252882528925290252912529225293252942529525296252972529825299253002530125302253032530425305253062530725308253092531025311253122531325314253152531625317253182531925320253212532225323253242532525326253272532825329253302533125332253332533425335253362533725338253392534025341253422534325344253452534625347253482534925350253512535225353253542535525356253572535825359253602536125362253632536425365253662536725368253692537025371253722537325374253752537625377253782537925380253812538225383253842538525386253872538825389253902539125392253932539425395253962539725398253992540025401254022540325404254052540625407254082540925410254112541225413254142541525416254172541825419254202542125422254232542425425254262542725428254292543025431254322543325434254352543625437254382543925440254412544225443254442544525446254472544825449254502545125452254532545425455254562545725458254592546025461254622546325464254652546625467254682546925470254712547225473254742547525476254772547825479254802548125482254832548425485254862548725488254892549025491254922549325494254952549625497254982549925500255012550225503255042550525506255072550825509255102551125512255132551425515255162551725518255192552025521255222552325524255252552625527255282552925530255312553225533255342553525536255372553825539255402554125542255432554425545255462554725548255492555025551255522555325554255552555625557255582555925560255612556225563255642556525566255672556825569255702557125572255732557425575255762557725578255792558025581255822558325584255852558625587255882558925590255912559225593255942559525596255972559825599256002560125602256032560425605256062560725608256092561025611256122561325614256152561625617256182561925620256212562225623256242562525626256272562825629256302563125632256332563425635256362563725638256392564025641256422564325644256452564625647256482564925650256512565225653256542565525656256572565825659256602566125662256632566425665256662566725668256692567025671256722567325674256752567625677256782567925680256812568225683256842568525686256872568825689256902569125692256932569425695256962569725698256992570025701257022570325704257052570625707257082570925710257112571225713257142571525716257172571825719257202572125722257232572425725257262572725728257292573025731257322573325734257352573625737257382573925740257412574225743257442574525746257472574825749257502575125752257532575425755257562575725758257592576025761257622576325764257652576625767257682576925770257712577225773257742577525776257772577825779257802578125782257832578425785257862578725788257892579025791257922579325794257952579625797257982579925800258012580225803258042580525806258072580825809258102581125812258132581425815258162581725818258192582025821258222582325824258252582625827258282582925830258312583225833258342583525836258372583825839258402584125842258432584425845258462584725848258492585025851258522585325854258552585625857258582585925860258612586225863258642586525866258672586825869258702587125872258732587425875258762587725878258792588025881258822588325884258852588625887258882588925890258912589225893258942589525896258972589825899259002590125902259032590425905259062590725908259092591025911259122591325914259152591625917259182591925920259212592225923259242592525926259272592825929259302593125932259332593425935259362593725938259392594025941259422594325944259452594625947259482594925950259512595225953259542595525956259572595825959259602596125962259632596425965259662596725968259692597025971259722597325974259752597625977259782597925980259812598225983259842598525986259872598825989259902599125992259932599425995259962599725998259992600026001260022600326004260052600626007260082600926010260112601226013260142601526016260172601826019260202602126022260232602426025260262602726028260292603026031260322603326034260352603626037260382603926040260412604226043260442604526046260472604826049260502605126052260532605426055260562605726058260592606026061260622606326064260652606626067260682606926070260712607226073260742607526076260772607826079260802608126082260832608426085260862608726088260892609026091260922609326094260952609626097260982609926100261012610226103261042610526106261072610826109261102611126112261132611426115261162611726118261192612026121261222612326124261252612626127261282612926130261312613226133261342613526136261372613826139261402614126142261432614426145261462614726148261492615026151261522615326154261552615626157261582615926160261612616226163261642616526166261672616826169261702617126172261732617426175261762617726178261792618026181261822618326184261852618626187261882618926190261912619226193261942619526196261972619826199262002620126202262032620426205262062620726208262092621026211262122621326214262152621626217262182621926220262212622226223262242622526226262272622826229262302623126232262332623426235262362623726238262392624026241262422624326244262452624626247262482624926250262512625226253262542625526256262572625826259262602626126262262632626426265262662626726268262692627026271262722627326274262752627626277262782627926280262812628226283262842628526286262872628826289262902629126292262932629426295262962629726298262992630026301263022630326304263052630626307263082630926310263112631226313263142631526316263172631826319263202632126322263232632426325263262632726328263292633026331263322633326334263352633626337263382633926340263412634226343263442634526346263472634826349263502635126352263532635426355263562635726358263592636026361263622636326364263652636626367263682636926370263712637226373263742637526376263772637826379263802638126382263832638426385263862638726388263892639026391263922639326394263952639626397263982639926400264012640226403264042640526406264072640826409264102641126412264132641426415264162641726418264192642026421264222642326424264252642626427264282642926430264312643226433264342643526436264372643826439264402644126442264432644426445264462644726448264492645026451264522645326454264552645626457264582645926460264612646226463264642646526466264672646826469264702647126472264732647426475264762647726478264792648026481264822648326484264852648626487264882648926490264912649226493264942649526496264972649826499265002650126502265032650426505265062650726508265092651026511265122651326514265152651626517265182651926520265212652226523265242652526526265272652826529265302653126532265332653426535265362653726538265392654026541265422654326544265452654626547265482654926550265512655226553265542655526556265572655826559265602656126562265632656426565265662656726568265692657026571265722657326574265752657626577265782657926580265812658226583265842658526586265872658826589265902659126592265932659426595265962659726598265992660026601266022660326604266052660626607266082660926610266112661226613266142661526616266172661826619266202662126622266232662426625266262662726628266292663026631266322663326634266352663626637266382663926640266412664226643266442664526646266472664826649266502665126652266532665426655266562665726658266592666026661266622666326664266652666626667266682666926670266712667226673266742667526676266772667826679266802668126682266832668426685266862668726688266892669026691266922669326694266952669626697266982669926700267012670226703267042670526706267072670826709267102671126712267132671426715267162671726718267192672026721267222672326724267252672626727267282672926730267312673226733267342673526736267372673826739267402674126742267432674426745267462674726748267492675026751267522675326754267552675626757267582675926760267612676226763267642676526766267672676826769267702677126772267732677426775267762677726778267792678026781267822678326784267852678626787267882678926790267912679226793267942679526796267972679826799268002680126802268032680426805268062680726808268092681026811268122681326814268152681626817268182681926820268212682226823268242682526826268272682826829268302683126832268332683426835268362683726838268392684026841268422684326844268452684626847268482684926850268512685226853268542685526856268572685826859268602686126862268632686426865268662686726868268692687026871268722687326874268752687626877268782687926880268812688226883268842688526886268872688826889268902689126892268932689426895268962689726898268992690026901269022690326904269052690626907269082690926910269112691226913269142691526916269172691826919269202692126922269232692426925269262692726928269292693026931269322693326934269352693626937269382693926940269412694226943269442694526946269472694826949269502695126952269532695426955269562695726958269592696026961269622696326964269652696626967269682696926970269712697226973269742697526976269772697826979269802698126982269832698426985269862698726988269892699026991269922699326994269952699626997269982699927000270012700227003270042700527006270072700827009270102701127012270132701427015270162701727018270192702027021270222702327024270252702627027270282702927030270312703227033270342703527036270372703827039270402704127042270432704427045270462704727048270492705027051270522705327054270552705627057270582705927060270612706227063270642706527066270672706827069270702707127072270732707427075270762707727078270792708027081270822708327084270852708627087270882708927090270912709227093270942709527096270972709827099271002710127102271032710427105271062710727108271092711027111271122711327114271152711627117271182711927120271212712227123271242712527126271272712827129271302713127132271332713427135271362713727138271392714027141271422714327144271452714627147271482714927150271512715227153271542715527156271572715827159271602716127162271632716427165271662716727168271692717027171271722717327174271752717627177271782717927180271812718227183271842718527186271872718827189271902719127192271932719427195271962719727198271992720027201272022720327204272052720627207272082720927210272112721227213272142721527216272172721827219272202722127222272232722427225272262722727228272292723027231272322723327234272352723627237272382723927240272412724227243272442724527246272472724827249272502725127252272532725427255272562725727258272592726027261272622726327264272652726627267272682726927270272712727227273272742727527276272772727827279272802728127282272832728427285272862728727288272892729027291272922729327294272952729627297272982729927300273012730227303273042730527306273072730827309273102731127312273132731427315273162731727318273192732027321273222732327324273252732627327273282732927330273312733227333273342733527336273372733827339273402734127342273432734427345273462734727348273492735027351273522735327354273552735627357273582735927360273612736227363273642736527366273672736827369273702737127372273732737427375273762737727378273792738027381273822738327384273852738627387273882738927390273912739227393273942739527396273972739827399274002740127402274032740427405274062740727408274092741027411274122741327414274152741627417274182741927420274212742227423274242742527426274272742827429274302743127432274332743427435274362743727438274392744027441274422744327444274452744627447274482744927450274512745227453274542745527456274572745827459274602746127462274632746427465274662746727468274692747027471274722747327474274752747627477274782747927480274812748227483274842748527486274872748827489274902749127492274932749427495274962749727498274992750027501275022750327504275052750627507275082750927510275112751227513275142751527516275172751827519275202752127522275232752427525275262752727528275292753027531275322753327534275352753627537275382753927540275412754227543275442754527546275472754827549275502755127552275532755427555275562755727558275592756027561275622756327564275652756627567275682756927570275712757227573275742757527576275772757827579275802758127582275832758427585275862758727588275892759027591275922759327594275952759627597275982759927600276012760227603276042760527606276072760827609276102761127612276132761427615276162761727618276192762027621276222762327624276252762627627276282762927630276312763227633276342763527636276372763827639276402764127642276432764427645276462764727648276492765027651276522765327654276552765627657276582765927660276612766227663276642766527666276672766827669276702767127672276732767427675276762767727678276792768027681276822768327684276852768627687276882768927690276912769227693276942769527696276972769827699277002770127702277032770427705277062770727708277092771027711277122771327714277152771627717277182771927720277212772227723277242772527726277272772827729277302773127732277332773427735277362773727738277392774027741277422774327744277452774627747277482774927750277512775227753277542775527756277572775827759277602776127762277632776427765277662776727768277692777027771277722777327774277752777627777277782777927780277812778227783277842778527786277872778827789277902779127792277932779427795277962779727798277992780027801278022780327804278052780627807278082780927810278112781227813278142781527816278172781827819278202782127822278232782427825278262782727828278292783027831278322783327834278352783627837278382783927840278412784227843278442784527846278472784827849278502785127852278532785427855278562785727858278592786027861278622786327864278652786627867278682786927870278712787227873278742787527876278772787827879278802788127882278832788427885278862788727888278892789027891278922789327894278952789627897278982789927900279012790227903279042790527906279072790827909279102791127912279132791427915279162791727918279192792027921279222792327924279252792627927279282792927930279312793227933279342793527936279372793827939279402794127942279432794427945279462794727948279492795027951279522795327954279552795627957279582795927960279612796227963279642796527966279672796827969279702797127972279732797427975279762797727978279792798027981279822798327984279852798627987279882798927990279912799227993279942799527996279972799827999280002800128002280032800428005280062800728008280092801028011280122801328014280152801628017280182801928020280212802228023280242802528026280272802828029280302803128032280332803428035280362803728038280392804028041280422804328044280452804628047280482804928050280512805228053280542805528056280572805828059280602806128062280632806428065280662806728068280692807028071280722807328074280752807628077280782807928080280812808228083280842808528086280872808828089280902809128092280932809428095280962809728098280992810028101281022810328104281052810628107281082810928110281112811228113281142811528116281172811828119281202812128122281232812428125281262812728128281292813028131281322813328134281352813628137281382813928140281412814228143281442814528146281472814828149281502815128152281532815428155281562815728158281592816028161281622816328164281652816628167281682816928170281712817228173281742817528176281772817828179281802818128182281832818428185281862818728188281892819028191281922819328194281952819628197281982819928200282012820228203282042820528206282072820828209282102821128212282132821428215282162821728218282192822028221282222822328224282252822628227282282822928230282312823228233282342823528236282372823828239282402824128242282432824428245282462824728248282492825028251282522825328254282552825628257282582825928260282612826228263282642826528266282672826828269282702827128272282732827428275282762827728278282792828028281282822828328284282852828628287282882828928290282912829228293282942829528296282972829828299283002830128302283032830428305283062830728308283092831028311283122831328314283152831628317283182831928320283212832228323283242832528326283272832828329283302833128332283332833428335283362833728338283392834028341283422834328344283452834628347283482834928350283512835228353283542835528356283572835828359283602836128362283632836428365283662836728368283692837028371283722837328374283752837628377283782837928380283812838228383283842838528386283872838828389283902839128392283932839428395283962839728398283992840028401284022840328404284052840628407284082840928410284112841228413284142841528416284172841828419284202842128422284232842428425284262842728428284292843028431284322843328434284352843628437284382843928440284412844228443284442844528446284472844828449284502845128452284532845428455284562845728458284592846028461284622846328464284652846628467284682846928470284712847228473284742847528476284772847828479284802848128482284832848428485284862848728488284892849028491284922849328494284952849628497284982849928500285012850228503285042850528506285072850828509285102851128512285132851428515285162851728518285192852028521285222852328524285252852628527285282852928530285312853228533285342853528536285372853828539285402854128542285432854428545285462854728548285492855028551285522855328554285552855628557285582855928560285612856228563285642856528566285672856828569285702857128572285732857428575285762857728578285792858028581285822858328584285852858628587285882858928590285912859228593285942859528596285972859828599286002860128602286032860428605286062860728608286092861028611286122861328614286152861628617286182861928620286212862228623286242862528626286272862828629286302863128632286332863428635286362863728638286392864028641286422864328644286452864628647286482864928650286512865228653286542865528656286572865828659286602866128662286632866428665286662866728668286692867028671286722867328674286752867628677286782867928680286812868228683286842868528686286872868828689286902869128692286932869428695286962869728698286992870028701287022870328704287052870628707287082870928710287112871228713287142871528716287172871828719287202872128722287232872428725287262872728728287292873028731287322873328734287352873628737287382873928740287412874228743287442874528746287472874828749287502875128752287532875428755287562875728758287592876028761287622876328764287652876628767287682876928770287712877228773287742877528776287772877828779287802878128782287832878428785287862878728788287892879028791287922879328794287952879628797287982879928800288012880228803288042880528806288072880828809288102881128812288132881428815288162881728818288192882028821288222882328824288252882628827288282882928830288312883228833288342883528836288372883828839288402884128842288432884428845288462884728848288492885028851288522885328854288552885628857288582885928860288612886228863288642886528866288672886828869288702887128872288732887428875288762887728878288792888028881288822888328884288852888628887288882888928890288912889228893288942889528896288972889828899289002890128902289032890428905289062890728908289092891028911289122891328914289152891628917289182891928920289212892228923289242892528926289272892828929289302893128932289332893428935289362893728938289392894028941289422894328944289452894628947289482894928950289512895228953289542895528956289572895828959289602896128962289632896428965289662896728968289692897028971289722897328974289752897628977289782897928980289812898228983289842898528986289872898828989289902899128992289932899428995289962899728998289992900029001290022900329004290052900629007290082900929010290112901229013290142901529016290172901829019290202902129022290232902429025290262902729028290292903029031290322903329034290352903629037290382903929040290412904229043290442904529046290472904829049290502905129052290532905429055290562905729058290592906029061290622906329064290652906629067290682906929070290712907229073290742907529076290772907829079290802908129082290832908429085290862908729088290892909029091290922909329094290952909629097290982909929100291012910229103291042910529106291072910829109291102911129112291132911429115291162911729118291192912029121291222912329124291252912629127291282912929130291312913229133291342913529136291372913829139291402914129142291432914429145291462914729148291492915029151291522915329154291552915629157291582915929160291612916229163291642916529166291672916829169291702917129172291732917429175291762917729178291792918029181291822918329184291852918629187291882918929190291912919229193291942919529196291972919829199292002920129202292032920429205292062920729208292092921029211292122921329214292152921629217292182921929220292212922229223292242922529226292272922829229292302923129232292332923429235292362923729238292392924029241292422924329244292452924629247292482924929250292512925229253292542925529256292572925829259292602926129262292632926429265292662926729268292692927029271292722927329274292752927629277292782927929280292812928229283292842928529286292872928829289292902929129292292932929429295292962929729298292992930029301293022930329304293052930629307293082930929310293112931229313293142931529316293172931829319293202932129322293232932429325293262932729328293292933029331293322933329334293352933629337293382933929340293412934229343293442934529346293472934829349293502935129352293532935429355293562935729358293592936029361293622936329364293652936629367293682936929370293712937229373293742937529376293772937829379293802938129382293832938429385293862938729388293892939029391293922939329394293952939629397293982939929400294012940229403294042940529406294072940829409294102941129412294132941429415294162941729418294192942029421294222942329424294252942629427294282942929430294312943229433294342943529436294372943829439294402944129442294432944429445294462944729448294492945029451294522945329454294552945629457294582945929460294612946229463294642946529466294672946829469294702947129472294732947429475294762947729478294792948029481294822948329484294852948629487294882948929490294912949229493294942949529496294972949829499295002950129502295032950429505295062950729508295092951029511295122951329514295152951629517295182951929520295212952229523295242952529526295272952829529295302953129532295332953429535295362953729538295392954029541295422954329544295452954629547295482954929550295512955229553295542955529556295572955829559295602956129562295632956429565295662956729568295692957029571295722957329574295752957629577295782957929580295812958229583295842958529586295872958829589295902959129592295932959429595295962959729598295992960029601296022960329604296052960629607296082960929610296112961229613296142961529616296172961829619296202962129622296232962429625296262962729628296292963029631296322963329634296352963629637296382963929640296412964229643296442964529646296472964829649296502965129652296532965429655296562965729658296592966029661296622966329664296652966629667296682966929670296712967229673296742967529676296772967829679296802968129682296832968429685296862968729688296892969029691296922969329694296952969629697296982969929700297012970229703297042970529706297072970829709297102971129712297132971429715297162971729718297192972029721297222972329724297252972629727297282972929730297312973229733297342973529736297372973829739297402974129742297432974429745297462974729748297492975029751297522975329754297552975629757297582975929760297612976229763297642976529766297672976829769297702977129772297732977429775297762977729778297792978029781297822978329784297852978629787297882978929790297912979229793297942979529796297972979829799298002980129802298032980429805298062980729808298092981029811298122981329814298152981629817298182981929820298212982229823298242982529826298272982829829298302983129832298332983429835298362983729838298392984029841298422984329844298452984629847298482984929850298512985229853298542985529856298572985829859298602986129862298632986429865298662986729868298692987029871298722987329874298752987629877298782987929880298812988229883298842988529886298872988829889298902989129892298932989429895298962989729898298992990029901299022990329904299052990629907299082990929910299112991229913299142991529916299172991829919299202992129922299232992429925299262992729928299292993029931299322993329934299352993629937299382993929940299412994229943299442994529946299472994829949299502995129952299532995429955299562995729958299592996029961299622996329964299652996629967299682996929970299712997229973299742997529976299772997829979299802998129982299832998429985299862998729988299892999029991299922999329994299952999629997299982999930000300013000230003300043000530006300073000830009300103001130012300133001430015300163001730018300193002030021300223002330024300253002630027300283002930030300313003230033300343003530036300373003830039300403004130042300433004430045300463004730048300493005030051300523005330054300553005630057300583005930060300613006230063300643006530066300673006830069300703007130072300733007430075300763007730078300793008030081300823008330084300853008630087300883008930090300913009230093300943009530096300973009830099301003010130102301033010430105301063010730108301093011030111301123011330114301153011630117301183011930120301213012230123301243012530126301273012830129301303013130132301333013430135301363013730138301393014030141301423014330144301453014630147301483014930150301513015230153301543015530156301573015830159301603016130162301633016430165301663016730168301693017030171301723017330174301753017630177301783017930180301813018230183301843018530186301873018830189301903019130192301933019430195301963019730198301993020030201302023020330204302053020630207302083020930210302113021230213302143021530216302173021830219302203022130222302233022430225302263022730228302293023030231302323023330234302353023630237302383023930240302413024230243302443024530246302473024830249302503025130252302533025430255302563025730258302593026030261302623026330264302653026630267302683026930270302713027230273302743027530276302773027830279302803028130282302833028430285302863028730288302893029030291302923029330294302953029630297302983029930300303013030230303303043030530306303073030830309303103031130312303133031430315303163031730318303193032030321303223032330324303253032630327303283032930330303313033230333303343033530336303373033830339303403034130342303433034430345303463034730348303493035030351303523035330354303553035630357303583035930360303613036230363303643036530366303673036830369303703037130372303733037430375303763037730378303793038030381303823038330384303853038630387303883038930390303913039230393303943039530396303973039830399304003040130402304033040430405304063040730408304093041030411304123041330414304153041630417304183041930420304213042230423304243042530426304273042830429304303043130432304333043430435304363043730438304393044030441304423044330444304453044630447304483044930450304513045230453304543045530456304573045830459304603046130462304633046430465304663046730468304693047030471304723047330474304753047630477304783047930480304813048230483304843048530486304873048830489304903049130492304933049430495304963049730498304993050030501305023050330504305053050630507305083050930510305113051230513305143051530516305173051830519305203052130522305233052430525305263052730528305293053030531305323053330534305353053630537305383053930540305413054230543305443054530546305473054830549305503055130552305533055430555305563055730558305593056030561305623056330564305653056630567305683056930570305713057230573305743057530576305773057830579305803058130582305833058430585305863058730588305893059030591305923059330594305953059630597305983059930600306013060230603306043060530606306073060830609306103061130612306133061430615306163061730618306193062030621306223062330624306253062630627306283062930630306313063230633306343063530636306373063830639306403064130642306433064430645306463064730648306493065030651306523065330654306553065630657306583065930660306613066230663306643066530666306673066830669306703067130672306733067430675306763067730678306793068030681306823068330684306853068630687306883068930690306913069230693306943069530696306973069830699307003070130702307033070430705307063070730708307093071030711307123071330714307153071630717307183071930720307213072230723307243072530726307273072830729307303073130732307333073430735307363073730738307393074030741307423074330744307453074630747307483074930750307513075230753307543075530756307573075830759307603076130762307633076430765307663076730768307693077030771307723077330774307753077630777307783077930780307813078230783307843078530786307873078830789307903079130792307933079430795307963079730798307993080030801308023080330804308053080630807308083080930810308113081230813308143081530816308173081830819308203082130822308233082430825308263082730828308293083030831308323083330834308353083630837308383083930840308413084230843308443084530846308473084830849308503085130852308533085430855308563085730858308593086030861308623086330864308653086630867308683086930870308713087230873308743087530876308773087830879308803088130882308833088430885308863088730888308893089030891308923089330894308953089630897308983089930900309013090230903309043090530906309073090830909309103091130912309133091430915309163091730918309193092030921309223092330924309253092630927309283092930930309313093230933309343093530936309373093830939309403094130942309433094430945309463094730948309493095030951309523095330954309553095630957309583095930960309613096230963309643096530966309673096830969309703097130972309733097430975309763097730978309793098030981309823098330984309853098630987309883098930990309913099230993309943099530996309973099830999310003100131002310033100431005310063100731008310093101031011310123101331014310153101631017310183101931020310213102231023310243102531026310273102831029310303103131032310333103431035310363103731038310393104031041310423104331044310453104631047310483104931050310513105231053310543105531056310573105831059310603106131062310633106431065310663106731068310693107031071310723107331074310753107631077310783107931080310813108231083310843108531086
  1. /*
  2. # Nuklear
  3. ![](https://cloud.githubusercontent.com/assets/8057201/11761525/ae06f0ca-a0c6-11e5-819d-5610b25f6ef4.gif)
  4. ## Contents
  5. 1. About section
  6. 2. Highlights section
  7. 3. Features section
  8. 4. Usage section
  9. 1. Flags section
  10. 2. Constants section
  11. 3. Dependencies section
  12. 5. Example section
  13. 6. API section
  14. 1. Context section
  15. 2. Input section
  16. 3. Drawing section
  17. 4. Window section
  18. 5. Layouting section
  19. 6. Groups section
  20. 7. Tree section
  21. 8. Properties section
  22. 7. License section
  23. 8. Changelog section
  24. 9. Gallery section
  25. 10. Credits section
  26. ## About
  27. This is a minimal state immediate mode graphical user interface toolkit
  28. written in ANSI C and licensed under public domain. It was designed as a simple
  29. embeddable user interface for application and does not have any dependencies,
  30. a default renderbackend or OS window and input handling but instead provides a very modular
  31. library approach by using simple input state for input and draw
  32. commands describing primitive shapes as output. So instead of providing a
  33. layered library that tries to abstract over a number of platform and
  34. render backends it only focuses on the actual UI.
  35. ## Highlights
  36. - Graphical user interface toolkit
  37. - Single header library
  38. - Written in C89 (a.k.a. ANSI C or ISO C90)
  39. - Small codebase (~18kLOC)
  40. - Focus on portability, efficiency and simplicity
  41. - No dependencies (not even the standard library if not wanted)
  42. - Fully skinnable and customizable
  43. - Low memory footprint with total memory control if needed or wanted
  44. - UTF-8 support
  45. - No global or hidden state
  46. - Customizable library modules (you can compile and use only what you need)
  47. - Optional font baker and vertex buffer output
  48. - [Code available on github](https://github.com/Immediate-Mode-UI/Nuklear/)
  49. ## Features
  50. - Absolutely no platform dependent code
  51. - Memory management control ranging from/to
  52. - Ease of use by allocating everything from standard library
  53. - Control every byte of memory inside the library
  54. - Font handling control ranging from/to
  55. - Use your own font implementation for everything
  56. - Use this libraries internal font baking and handling API
  57. - Drawing output control ranging from/to
  58. - Simple shapes for more high level APIs which already have drawing capabilities
  59. - Hardware accessible anti-aliased vertex buffer output
  60. - Customizable colors and properties ranging from/to
  61. - Simple changes to color by filling a simple color table
  62. - Complete control with ability to use skinning to decorate widgets
  63. - Bendable UI library with widget ranging from/to
  64. - Basic widgets like buttons, checkboxes, slider, ...
  65. - Advanced widget like abstract comboboxes, contextual menus,...
  66. - Compile time configuration to only compile what you need
  67. - Subset which can be used if you do not want to link or use the standard library
  68. - Can be easily modified to only update on user input instead of frame updates
  69. ## Usage
  70. This library is self contained in one single header file and can be used either
  71. in header only mode or in implementation mode. The header only mode is used
  72. by default when included and allows including this header in other headers
  73. and does not contain the actual implementation. <br /><br />
  74. The implementation mode requires to define the preprocessor macro
  75. NK_IMPLEMENTATION in *one* .c/.cpp file before #including this file, e.g.:
  76. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~C
  77. #define NK_IMPLEMENTATION
  78. #include "nuklear.h"
  79. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  80. Also optionally define the symbols listed in the section "OPTIONAL DEFINES"
  81. below in header and implementation mode if you want to use additional functionality
  82. or need more control over the library.
  83. !!! WARNING
  84. 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.
  85. ### Flags
  86. Flag | Description
  87. --------------------------------|------------------------------------------
  88. NK_PRIVATE | If defined declares all functions as static, so they can only be accessed inside the file that contains the implementation
  89. 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.
  90. 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.
  91. NK_INCLUDE_STANDARD_IO | If defined it will include header `<stdio.h>` and provide additional functions depending on file loading.
  92. NK_INCLUDE_STANDARD_VARARGS | If defined it will include header <stdarg.h> and provide additional functions depending on file loading.
  93. NK_INCLUDE_STANDARD_BOOL | If defined it will include header `<stdbool.h>` for nk_bool otherwise nuklear defines nk_bool as int.
  94. 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,...
  95. 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.
  96. 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
  97. 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.
  98. 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.
  99. 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.
  100. 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
  101. NK_KEYSTATE_BASED_INPUT | Define this if your backend uses key state for each frame rather than key press/release events
  102. NK_IS_WORD_BOUNDARY(c) | Define this to a function macro that takes a single nk_rune (nk_uint) and returns true if it's a word separator. If not defined, uses the default definition (see nk_is_word_boundary())
  103. !!! WARNING
  104. The following flags will pull in the standard C library:
  105. - NK_INCLUDE_DEFAULT_ALLOCATOR
  106. - NK_INCLUDE_STANDARD_IO
  107. - NK_INCLUDE_STANDARD_VARARGS
  108. !!! WARNING
  109. The following flags if defined need to be defined for both header and implementation:
  110. - NK_INCLUDE_FIXED_TYPES
  111. - NK_INCLUDE_DEFAULT_ALLOCATOR
  112. - NK_INCLUDE_STANDARD_VARARGS
  113. - NK_INCLUDE_STANDARD_BOOL
  114. - NK_INCLUDE_VERTEX_BUFFER_OUTPUT
  115. - NK_INCLUDE_FONT_BAKING
  116. - NK_INCLUDE_DEFAULT_FONT
  117. - NK_INCLUDE_STANDARD_VARARGS
  118. - NK_INCLUDE_COMMAND_USERDATA
  119. - NK_UINT_DRAW_INDEX
  120. ### Constants
  121. Define | Description
  122. --------------------------------|---------------------------------------
  123. 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.
  124. NK_MAX_NUMBER_BUFFER | Maximum buffer size for the conversion buffer between float and string Under normal circumstances this should be more than sufficient.
  125. 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.
  126. !!! WARNING
  127. The following constants if defined need to be defined for both header and implementation:
  128. - NK_MAX_NUMBER_BUFFER
  129. - NK_BUFFER_DEFAULT_INITIAL_SIZE
  130. - NK_INPUT_MAX
  131. ### Dependencies
  132. Function | Description
  133. ------------|---------------------------------------------------------------
  134. NK_ASSERT | If you don't define this, nuklear will use <assert.h> with assert().
  135. NK_MEMSET | You can define this to 'memset' or your own memset implementation replacement. If not nuklear will use its own version.
  136. NK_MEMCPY | You can define this to 'memcpy' or your own memcpy implementation replacement. If not nuklear will use its own version.
  137. 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.
  138. NK_SIN | You can define this to 'sinf' or your own sine implementation replacement. If not nuklear will use its own approximation implementation.
  139. NK_COS | You can define this to 'cosf' or your own cosine implementation replacement. If not nuklear will use its own approximation implementation.
  140. 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!).
  141. 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!).
  142. 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.
  143. !!! WARNING
  144. The following dependencies will pull in the standard C library if not redefined:
  145. - NK_ASSERT
  146. !!! WARNING
  147. The following dependencies if defined need to be defined for both header and implementation:
  148. - NK_ASSERT
  149. !!! WARNING
  150. The following dependencies if defined need to be defined only for the implementation part:
  151. - NK_MEMSET
  152. - NK_MEMCPY
  153. - NK_SQRT
  154. - NK_SIN
  155. - NK_COS
  156. - NK_STRTOD
  157. - NK_DTOA
  158. - NK_VSNPRINTF
  159. ## Example
  160. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
  161. // init gui state
  162. enum {EASY, HARD};
  163. static int op = EASY;
  164. static float value = 0.6f;
  165. static int i = 20;
  166. struct nk_context ctx;
  167. nk_init_fixed(&ctx, calloc(1, MAX_MEMORY), MAX_MEMORY, &font);
  168. if (nk_begin(&ctx, "Show", nk_rect(50, 50, 220, 220),
  169. NK_WINDOW_BORDER|NK_WINDOW_MOVABLE|NK_WINDOW_CLOSABLE)) {
  170. // fixed widget pixel width
  171. nk_layout_row_static(&ctx, 30, 80, 1);
  172. if (nk_button_label(&ctx, "button")) {
  173. // event handling
  174. }
  175. // fixed widget window ratio width
  176. nk_layout_row_dynamic(&ctx, 30, 2);
  177. if (nk_option_label(&ctx, "easy", op == EASY)) op = EASY;
  178. if (nk_option_label(&ctx, "hard", op == HARD)) op = HARD;
  179. // custom widget pixel width
  180. nk_layout_row_begin(&ctx, NK_STATIC, 30, 2);
  181. {
  182. nk_layout_row_push(&ctx, 50);
  183. nk_label(&ctx, "Volume:", NK_TEXT_LEFT);
  184. nk_layout_row_push(&ctx, 110);
  185. nk_slider_float(&ctx, 0, &value, 1.0f, 0.1f);
  186. }
  187. nk_layout_row_end(&ctx);
  188. }
  189. nk_end(&ctx);
  190. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  191. ![](https://cloud.githubusercontent.com/assets/8057201/10187981/584ecd68-675c-11e5-897c-822ef534a876.png)
  192. ## API
  193. */
  194. #ifndef NK_SINGLE_FILE
  195. #define NK_SINGLE_FILE
  196. #endif
  197. /** \file nuklear.h
  198. * \brief main API and documentation file
  199. *
  200. * \details
  201. */
  202. #ifndef NK_NUKLEAR_H_
  203. #define NK_NUKLEAR_H_
  204. #ifdef __cplusplus
  205. extern "C" {
  206. #endif
  207. /*
  208. * ==============================================================
  209. *
  210. * CONSTANTS
  211. *
  212. * ===============================================================
  213. */
  214. #define NK_UNDEFINED (-1.0f)
  215. #define NK_UTF_INVALID 0xFFFD /**< internal invalid utf8 rune */
  216. #define NK_UTF_SIZE 4 /**< describes the number of bytes a glyph consists of*/
  217. #ifndef NK_INPUT_MAX
  218. #define NK_INPUT_MAX 16
  219. #endif
  220. #ifndef NK_MAX_NUMBER_BUFFER
  221. #define NK_MAX_NUMBER_BUFFER 64
  222. #endif
  223. #ifndef NK_SCROLLBAR_HIDING_TIMEOUT
  224. #define NK_SCROLLBAR_HIDING_TIMEOUT 4.0f
  225. #endif
  226. /*
  227. * ==============================================================
  228. *
  229. * HELPER
  230. *
  231. * ===============================================================
  232. */
  233. #ifndef NK_API
  234. #ifdef NK_PRIVATE
  235. #if (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199409L))
  236. #define NK_API static inline
  237. #elif defined(__cplusplus)
  238. #define NK_API static inline
  239. #else
  240. #define NK_API static
  241. #endif
  242. #else
  243. #define NK_API extern
  244. #endif
  245. #endif
  246. #ifndef NK_LIB
  247. #ifdef NK_SINGLE_FILE
  248. #define NK_LIB static
  249. #else
  250. #define NK_LIB extern
  251. #endif
  252. #endif
  253. #define NK_INTERN static
  254. #define NK_STORAGE static
  255. #define NK_GLOBAL static
  256. #define NK_FLAG(x) (1 << (x))
  257. #define NK_STRINGIFY(x) #x
  258. #define NK_MACRO_STRINGIFY(x) NK_STRINGIFY(x)
  259. #define NK_STRING_JOIN_IMMEDIATE(arg1, arg2) arg1 ## arg2
  260. #define NK_STRING_JOIN_DELAY(arg1, arg2) NK_STRING_JOIN_IMMEDIATE(arg1, arg2)
  261. #define NK_STRING_JOIN(arg1, arg2) NK_STRING_JOIN_DELAY(arg1, arg2)
  262. #ifdef _MSC_VER
  263. #define NK_UNIQUE_NAME(name) NK_STRING_JOIN(name,__COUNTER__)
  264. #else
  265. #define NK_UNIQUE_NAME(name) NK_STRING_JOIN(name,__LINE__)
  266. #endif
  267. #ifndef NK_STATIC_ASSERT
  268. #define NK_STATIC_ASSERT(exp) typedef char NK_UNIQUE_NAME(_dummy_array)[(exp)?1:-1]
  269. #endif
  270. #ifndef NK_FILE_LINE
  271. #ifdef _MSC_VER
  272. #define NK_FILE_LINE __FILE__ ":" NK_MACRO_STRINGIFY(__COUNTER__)
  273. #else
  274. #define NK_FILE_LINE __FILE__ ":" NK_MACRO_STRINGIFY(__LINE__)
  275. #endif
  276. #endif
  277. #define NK_MIN(a,b) ((a) < (b) ? (a) : (b))
  278. #define NK_MAX(a,b) ((a) < (b) ? (b) : (a))
  279. #define NK_CLAMP(i,v,x) (NK_MAX(NK_MIN(v,x), i))
  280. #ifdef NK_INCLUDE_STANDARD_VARARGS
  281. #include <stdarg.h>
  282. #if defined(_MSC_VER) && (_MSC_VER >= 1600) /* VS 2010 and above */
  283. #include <sal.h>
  284. #define NK_PRINTF_FORMAT_STRING _Printf_format_string_
  285. #else
  286. #define NK_PRINTF_FORMAT_STRING
  287. #endif
  288. #if defined(__GNUC__)
  289. #define NK_PRINTF_VARARG_FUNC(fmtargnumber) __attribute__((format(__printf__, fmtargnumber, fmtargnumber+1)))
  290. #define NK_PRINTF_VALIST_FUNC(fmtargnumber) __attribute__((format(__printf__, fmtargnumber, 0)))
  291. #else
  292. #define NK_PRINTF_VARARG_FUNC(fmtargnumber)
  293. #define NK_PRINTF_VALIST_FUNC(fmtargnumber)
  294. #endif
  295. #endif
  296. /*
  297. * ===============================================================
  298. *
  299. * BASIC
  300. *
  301. * ===============================================================
  302. */
  303. #ifdef NK_INCLUDE_FIXED_TYPES
  304. #include <stdint.h>
  305. #define NK_INT8 int8_t
  306. #define NK_UINT8 uint8_t
  307. #define NK_INT16 int16_t
  308. #define NK_UINT16 uint16_t
  309. #define NK_INT32 int32_t
  310. #define NK_UINT32 uint32_t
  311. #define NK_SIZE_TYPE uintptr_t
  312. #define NK_POINTER_TYPE uintptr_t
  313. #else
  314. #ifndef NK_INT8
  315. #define NK_INT8 signed char
  316. #endif
  317. #ifndef NK_UINT8
  318. #define NK_UINT8 unsigned char
  319. #endif
  320. #ifndef NK_INT16
  321. #define NK_INT16 signed short
  322. #endif
  323. #ifndef NK_UINT16
  324. #define NK_UINT16 unsigned short
  325. #endif
  326. #ifndef NK_INT32
  327. #if defined(_MSC_VER)
  328. #define NK_INT32 __int32
  329. #else
  330. #define NK_INT32 signed int
  331. #endif
  332. #endif
  333. #ifndef NK_UINT32
  334. #if defined(_MSC_VER)
  335. #define NK_UINT32 unsigned __int32
  336. #else
  337. #define NK_UINT32 unsigned int
  338. #endif
  339. #endif
  340. #ifndef NK_SIZE_TYPE
  341. #if defined(_WIN64) && defined(_MSC_VER)
  342. #define NK_SIZE_TYPE unsigned __int64
  343. #elif (defined(_WIN32) || defined(WIN32)) && defined(_MSC_VER)
  344. #define NK_SIZE_TYPE unsigned __int32
  345. #elif defined(__GNUC__) || defined(__clang__)
  346. #if defined(__x86_64__) || defined(__ppc64__) || defined(__PPC64__) || defined(__aarch64__)
  347. #define NK_SIZE_TYPE unsigned long
  348. #else
  349. #define NK_SIZE_TYPE unsigned int
  350. #endif
  351. #else
  352. #define NK_SIZE_TYPE unsigned long
  353. #endif
  354. #endif
  355. #ifndef NK_POINTER_TYPE
  356. #if defined(_WIN64) && defined(_MSC_VER)
  357. #define NK_POINTER_TYPE unsigned __int64
  358. #elif (defined(_WIN32) || defined(WIN32)) && defined(_MSC_VER)
  359. #define NK_POINTER_TYPE unsigned __int32
  360. #elif defined(__GNUC__) || defined(__clang__)
  361. #if defined(__x86_64__) || defined(__ppc64__) || defined(__PPC64__) || defined(__aarch64__)
  362. #define NK_POINTER_TYPE unsigned long
  363. #else
  364. #define NK_POINTER_TYPE unsigned int
  365. #endif
  366. #else
  367. #define NK_POINTER_TYPE unsigned long
  368. #endif
  369. #endif
  370. #endif
  371. #ifndef NK_BOOL
  372. #ifdef NK_INCLUDE_STANDARD_BOOL
  373. #include <stdbool.h>
  374. #define NK_BOOL bool
  375. #else
  376. #define NK_BOOL int /**< could be char, use int for drop-in replacement backwards compatibility */
  377. #endif
  378. #endif
  379. typedef NK_INT8 nk_char;
  380. typedef NK_UINT8 nk_uchar;
  381. typedef NK_UINT8 nk_byte;
  382. typedef NK_INT16 nk_short;
  383. typedef NK_UINT16 nk_ushort;
  384. typedef NK_INT32 nk_int;
  385. typedef NK_UINT32 nk_uint;
  386. typedef NK_SIZE_TYPE nk_size;
  387. typedef NK_POINTER_TYPE nk_ptr;
  388. typedef NK_BOOL nk_bool;
  389. typedef nk_uint nk_hash;
  390. typedef nk_uint nk_flags;
  391. typedef nk_uint nk_rune;
  392. /* Make sure correct type size:
  393. * This will fire with a negative subscript error if the type sizes
  394. * are set incorrectly by the compiler, and compile out if not */
  395. NK_STATIC_ASSERT(sizeof(nk_short) == 2);
  396. NK_STATIC_ASSERT(sizeof(nk_ushort) == 2);
  397. NK_STATIC_ASSERT(sizeof(nk_uint) == 4);
  398. NK_STATIC_ASSERT(sizeof(nk_int) == 4);
  399. NK_STATIC_ASSERT(sizeof(nk_byte) == 1);
  400. NK_STATIC_ASSERT(sizeof(nk_flags) >= 4);
  401. NK_STATIC_ASSERT(sizeof(nk_rune) >= 4);
  402. NK_STATIC_ASSERT(sizeof(nk_size) >= sizeof(void*));
  403. NK_STATIC_ASSERT(sizeof(nk_ptr) >= sizeof(void*));
  404. #ifdef NK_INCLUDE_STANDARD_BOOL
  405. NK_STATIC_ASSERT(sizeof(nk_bool) == sizeof(bool));
  406. #else
  407. NK_STATIC_ASSERT(sizeof(nk_bool) >= 2);
  408. #endif
  409. /* ============================================================================
  410. *
  411. * API
  412. *
  413. * =========================================================================== */
  414. struct nk_buffer;
  415. struct nk_allocator;
  416. struct nk_command_buffer;
  417. struct nk_draw_command;
  418. struct nk_convert_config;
  419. struct nk_style_item;
  420. struct nk_text_edit;
  421. struct nk_draw_list;
  422. struct nk_user_font;
  423. struct nk_panel;
  424. struct nk_context;
  425. struct nk_draw_vertex_layout_element;
  426. struct nk_style_button;
  427. struct nk_style_toggle;
  428. struct nk_style_selectable;
  429. struct nk_style_slide;
  430. struct nk_style_progress;
  431. struct nk_style_scrollbar;
  432. struct nk_style_edit;
  433. struct nk_style_property;
  434. struct nk_style_chart;
  435. struct nk_style_combo;
  436. struct nk_style_tab;
  437. struct nk_style_window_header;
  438. struct nk_style_window;
  439. enum {nk_false, nk_true};
  440. struct nk_color {nk_byte r,g,b,a;};
  441. struct nk_colorf {float r,g,b,a;};
  442. struct nk_vec2 {float x,y;};
  443. struct nk_vec2i {short x, y;};
  444. struct nk_rect {float x,y,w,h;};
  445. struct nk_recti {short x,y,w,h;};
  446. typedef char nk_glyph[NK_UTF_SIZE];
  447. typedef union {void *ptr; int id;} nk_handle;
  448. struct nk_image {nk_handle handle; nk_ushort w, h; nk_ushort region[4];};
  449. struct nk_nine_slice {struct nk_image img; nk_ushort l, t, r, b;};
  450. struct nk_cursor {struct nk_image img; struct nk_vec2 size, offset;};
  451. struct nk_scroll {nk_uint x, y;};
  452. enum nk_heading {NK_UP, NK_RIGHT, NK_DOWN, NK_LEFT};
  453. enum nk_button_behavior {NK_BUTTON_DEFAULT, NK_BUTTON_REPEATER};
  454. enum nk_modify {NK_FIXED = nk_false, NK_MODIFIABLE = nk_true};
  455. enum nk_orientation {NK_VERTICAL, NK_HORIZONTAL};
  456. enum nk_collapse_states {NK_MINIMIZED = nk_false, NK_MAXIMIZED = nk_true};
  457. enum nk_show_states {NK_HIDDEN = nk_false, NK_SHOWN = nk_true};
  458. enum nk_chart_type {NK_CHART_LINES, NK_CHART_COLUMN, NK_CHART_MAX};
  459. enum nk_chart_event {NK_CHART_HOVERING = 0x01, NK_CHART_CLICKED = 0x02};
  460. enum nk_color_format {NK_RGB, NK_RGBA};
  461. enum nk_popup_type {NK_POPUP_STATIC, NK_POPUP_DYNAMIC};
  462. enum nk_layout_format {NK_DYNAMIC, NK_STATIC};
  463. enum nk_tree_type {NK_TREE_NODE, NK_TREE_TAB};
  464. typedef void*(*nk_plugin_alloc)(nk_handle, void *old, nk_size);
  465. typedef void (*nk_plugin_free)(nk_handle, void *old);
  466. typedef nk_bool(*nk_plugin_filter)(const struct nk_text_edit*, nk_rune unicode);
  467. typedef void(*nk_plugin_paste)(nk_handle, struct nk_text_edit*);
  468. typedef void(*nk_plugin_copy)(nk_handle, const char*, int len);
  469. struct nk_allocator {
  470. nk_handle userdata;
  471. nk_plugin_alloc alloc;
  472. nk_plugin_free free;
  473. };
  474. enum nk_symbol_type {
  475. NK_SYMBOL_NONE,
  476. NK_SYMBOL_X,
  477. NK_SYMBOL_UNDERSCORE,
  478. NK_SYMBOL_CIRCLE_SOLID,
  479. NK_SYMBOL_CIRCLE_OUTLINE,
  480. NK_SYMBOL_RECT_SOLID,
  481. NK_SYMBOL_RECT_OUTLINE,
  482. NK_SYMBOL_TRIANGLE_UP,
  483. NK_SYMBOL_TRIANGLE_DOWN,
  484. NK_SYMBOL_TRIANGLE_LEFT,
  485. NK_SYMBOL_TRIANGLE_RIGHT,
  486. NK_SYMBOL_PLUS,
  487. NK_SYMBOL_MINUS,
  488. NK_SYMBOL_TRIANGLE_UP_OUTLINE,
  489. NK_SYMBOL_TRIANGLE_DOWN_OUTLINE,
  490. NK_SYMBOL_TRIANGLE_LEFT_OUTLINE,
  491. NK_SYMBOL_TRIANGLE_RIGHT_OUTLINE,
  492. NK_SYMBOL_MAX
  493. };
  494. /* =============================================================================
  495. *
  496. * CONTEXT
  497. *
  498. * =============================================================================*/
  499. /**
  500. * \page Context
  501. * Contexts are the main entry point and the majestro of nuklear and contain all required state.
  502. * They are used for window, memory, input, style, stack, commands and time management and need
  503. * to be passed into all nuklear GUI specific functions.
  504. *
  505. * # Usage
  506. * To use a context it first has to be initialized which can be achieved by calling
  507. * one of either `nk_init_default`, `nk_init_fixed`, `nk_init`, `nk_init_custom`.
  508. * Each takes in a font handle and a specific way of handling memory. Memory control
  509. * hereby ranges from standard library to just specifying a fixed sized block of memory
  510. * which nuklear has to manage itself from.
  511. *
  512. * ```c
  513. * struct nk_context ctx;
  514. * nk_init_xxx(&ctx, ...);
  515. * while (1) {
  516. * // [...]
  517. * nk_clear(&ctx);
  518. * }
  519. * nk_free(&ctx);
  520. * ```
  521. *
  522. * # Reference
  523. * Function | Description
  524. * --------------------|-------------------------------------------------------
  525. * \ref nk_init_default | Initializes context with standard library memory allocation (malloc,free)
  526. * \ref nk_init_fixed | Initializes context from single fixed size memory block
  527. * \ref nk_init | Initializes context with memory allocator callbacks for alloc and free
  528. * \ref nk_init_custom | Initializes context from two buffers. One for draw commands the other for window/panel/table allocations
  529. * \ref nk_clear | Called at the end of the frame to reset and prepare the context for the next frame
  530. * \ref nk_free | Shutdown and free all memory allocated inside the context
  531. * \ref nk_set_user_data| Utility function to pass user data to draw command
  532. */
  533. #ifdef NK_INCLUDE_DEFAULT_ALLOCATOR
  534. /**
  535. * # nk_init_default
  536. * Initializes a `nk_context` struct with a default standard library allocator.
  537. * Should be used if you don't want to be bothered with memory management in nuklear.
  538. *
  539. * ```c
  540. * nk_bool nk_init_default(struct nk_context *ctx, const struct nk_user_font *font);
  541. * ```
  542. *
  543. * Parameter | Description
  544. * ------------|---------------------------------------------------------------
  545. * \param[in] ctx | Must point to an either stack or heap allocated `nk_context` struct
  546. * \param[in] font | Must point to a previously initialized font handle for more info look at font documentation
  547. *
  548. * \returns either `false(0)` on failure or `true(1)` on success.
  549. */
  550. NK_API nk_bool nk_init_default(struct nk_context*, const struct nk_user_font*);
  551. #endif
  552. /**
  553. * # nk_init_fixed
  554. * Initializes a `nk_context` struct from single fixed size memory block
  555. * Should be used if you want complete control over nuklear's memory management.
  556. * Especially recommended for system with little memory or systems with virtual memory.
  557. * For the later case you can just allocate for example 16MB of virtual memory
  558. * and only the required amount of memory will actually be committed.
  559. *
  560. * ```c
  561. * nk_bool nk_init_fixed(struct nk_context *ctx, void *memory, nk_size size, const struct nk_user_font *font);
  562. * ```
  563. *
  564. * !!! Warning
  565. * make sure the passed memory block is aligned correctly for `nk_draw_commands`.
  566. *
  567. * Parameter | Description
  568. * ------------|--------------------------------------------------------------
  569. * \param[in] ctx | Must point to an either stack or heap allocated `nk_context` struct
  570. * \param[in] memory | Must point to a previously allocated memory block
  571. * \param[in] size | Must contain the total size of memory
  572. * \param[in] font | Must point to a previously initialized font handle for more info look at font documentation
  573. *
  574. * \returns either `false(0)` on failure or `true(1)` on success.
  575. */
  576. NK_API nk_bool nk_init_fixed(struct nk_context*, void *memory, nk_size size, const struct nk_user_font*);
  577. /**
  578. * # nk_init
  579. * Initializes a `nk_context` struct with memory allocation callbacks for nuklear to allocate
  580. * memory from. Used internally for `nk_init_default` and provides a kitchen sink allocation
  581. * interface to nuklear. Can be useful for cases like monitoring memory consumption.
  582. *
  583. * ```c
  584. * nk_bool nk_init(struct nk_context *ctx, const struct nk_allocator *alloc, const struct nk_user_font *font);
  585. * ```
  586. *
  587. * Parameter | Description
  588. * ------------|---------------------------------------------------------------
  589. * \param[in] ctx | Must point to an either stack or heap allocated `nk_context` struct
  590. * \param[in] alloc | Must point to a previously allocated memory allocator
  591. * \param[in] font | Must point to a previously initialized font handle for more info look at font documentation
  592. *
  593. * \returns either `false(0)` on failure or `true(1)` on success.
  594. */
  595. NK_API nk_bool nk_init(struct nk_context*, const struct nk_allocator*, const struct nk_user_font*);
  596. /**
  597. * \brief Initializes a `nk_context` struct from two different either fixed or growing buffers.
  598. *
  599. * \details
  600. * The first buffer is for allocating draw commands while the second buffer is
  601. * used for allocating windows, panels and state tables.
  602. *
  603. * ```c
  604. * nk_bool nk_init_custom(struct nk_context *ctx, struct nk_buffer *cmds, struct nk_buffer *pool, const struct nk_user_font *font);
  605. * ```
  606. *
  607. * \param[in] ctx Must point to an either stack or heap allocated `nk_context` struct
  608. * \param[in] cmds Must point to a previously initialized memory buffer either fixed or dynamic to store draw commands into
  609. * \param[in] pool Must point to a previously initialized memory buffer either fixed or dynamic to store windows, panels and tables
  610. * \param[in] font Must point to a previously initialized font handle for more info look at font documentation
  611. *
  612. * \returns either `false(0)` on failure or `true(1)` on success.
  613. */
  614. NK_API nk_bool nk_init_custom(struct nk_context*, struct nk_buffer *cmds, struct nk_buffer *pool, const struct nk_user_font*);
  615. /**
  616. * \brief Resets the context state at the end of the frame.
  617. *
  618. * \details
  619. * This includes mostly garbage collector tasks like removing windows or table
  620. * not called and therefore used anymore.
  621. *
  622. * ```c
  623. * void nk_clear(struct nk_context *ctx);
  624. * ```
  625. *
  626. * \param[in] ctx Must point to a previously initialized `nk_context` struct
  627. */
  628. NK_API void nk_clear(struct nk_context*);
  629. /**
  630. * \brief Frees all memory allocated by nuklear; Not needed if context was initialized with `nk_init_fixed`.
  631. *
  632. * \details
  633. * ```c
  634. * void nk_free(struct nk_context *ctx);
  635. * ```
  636. *
  637. * \param[in] ctx Must point to a previously initialized `nk_context` struct
  638. */
  639. NK_API void nk_free(struct nk_context*);
  640. #ifdef NK_INCLUDE_COMMAND_USERDATA
  641. /**
  642. * \brief Sets the currently passed userdata passed down into each draw command.
  643. *
  644. * \details
  645. * ```c
  646. * void nk_set_user_data(struct nk_context *ctx, nk_handle data);
  647. * ```
  648. *
  649. * \param[in] ctx Must point to a previously initialized `nk_context` struct
  650. * \param[in] data Handle with either pointer or index to be passed into every draw commands
  651. */
  652. NK_API void nk_set_user_data(struct nk_context*, nk_handle handle);
  653. #endif
  654. /* =============================================================================
  655. *
  656. * INPUT
  657. *
  658. * =============================================================================*/
  659. /**
  660. * \page Input
  661. *
  662. * The input API is responsible for holding the current input state composed of
  663. * mouse, key and text input states.
  664. * It is worth noting that no direct OS or window handling is done in nuklear.
  665. * Instead all input state has to be provided by platform specific code. This on one hand
  666. * expects more work from the user and complicates usage but on the other hand
  667. * provides simple abstraction over a big number of platforms, libraries and other
  668. * already provided functionality.
  669. *
  670. * ```c
  671. * nk_input_begin(&ctx);
  672. * while (GetEvent(&evt)) {
  673. * if (evt.type == MOUSE_MOVE)
  674. * nk_input_motion(&ctx, evt.motion.x, evt.motion.y);
  675. * else if (evt.type == [...]) {
  676. * // [...]
  677. * }
  678. * } nk_input_end(&ctx);
  679. * ```
  680. *
  681. * # Usage
  682. * Input state needs to be provided to nuklear by first calling `nk_input_begin`
  683. * which resets internal state like delta mouse position and button transitions.
  684. * After `nk_input_begin` all current input state needs to be provided. This includes
  685. * mouse motion, button and key pressed and released, text input and scrolling.
  686. * Both event- or state-based input handling are supported by this API
  687. * and should work without problems. Finally after all input state has been
  688. * mirrored `nk_input_end` needs to be called to finish input process.
  689. *
  690. * ```c
  691. * struct nk_context ctx;
  692. * nk_init_xxx(&ctx, ...);
  693. * while (1) {
  694. * Event evt;
  695. * nk_input_begin(&ctx);
  696. * while (GetEvent(&evt)) {
  697. * if (evt.type == MOUSE_MOVE)
  698. * nk_input_motion(&ctx, evt.motion.x, evt.motion.y);
  699. * else if (evt.type == [...]) {
  700. * // [...]
  701. * }
  702. * }
  703. * nk_input_end(&ctx);
  704. * // [...]
  705. * nk_clear(&ctx);
  706. * } nk_free(&ctx);
  707. * ```
  708. *
  709. * # Reference
  710. * Function | Description
  711. * --------------------|-------------------------------------------------------
  712. * \ref nk_input_begin | Begins the input mirroring process. Needs to be called before all other `nk_input_xxx` calls
  713. * \ref nk_input_motion | Mirrors mouse cursor position
  714. * \ref nk_input_key | Mirrors key state with either pressed or released
  715. * \ref nk_input_button | Mirrors mouse button state with either pressed or released
  716. * \ref nk_input_scroll | Mirrors mouse scroll values
  717. * \ref nk_input_char | Adds a single ASCII text character into an internal text buffer
  718. * \ref nk_input_glyph | Adds a single multi-byte UTF-8 character into an internal text buffer
  719. * \ref nk_input_unicode| Adds a single unicode rune into an internal text buffer
  720. * \ref 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
  721. */
  722. enum nk_keys {
  723. NK_KEY_NONE,
  724. NK_KEY_SHIFT,
  725. NK_KEY_CTRL,
  726. NK_KEY_DEL,
  727. NK_KEY_ENTER,
  728. NK_KEY_TAB,
  729. NK_KEY_BACKSPACE,
  730. NK_KEY_COPY,
  731. NK_KEY_CUT,
  732. NK_KEY_PASTE,
  733. NK_KEY_UP,
  734. NK_KEY_DOWN,
  735. NK_KEY_LEFT,
  736. NK_KEY_RIGHT,
  737. /* Shortcuts: text field */
  738. NK_KEY_TEXT_INSERT_MODE,
  739. NK_KEY_TEXT_REPLACE_MODE,
  740. NK_KEY_TEXT_RESET_MODE,
  741. NK_KEY_TEXT_LINE_START,
  742. NK_KEY_TEXT_LINE_END,
  743. NK_KEY_TEXT_START,
  744. NK_KEY_TEXT_END,
  745. NK_KEY_TEXT_UNDO,
  746. NK_KEY_TEXT_REDO,
  747. NK_KEY_TEXT_SELECT_ALL,
  748. NK_KEY_TEXT_WORD_LEFT,
  749. NK_KEY_TEXT_WORD_RIGHT,
  750. /* Shortcuts: scrollbar */
  751. NK_KEY_SCROLL_START,
  752. NK_KEY_SCROLL_END,
  753. NK_KEY_SCROLL_DOWN,
  754. NK_KEY_SCROLL_UP,
  755. NK_KEY_MAX
  756. };
  757. enum nk_buttons {
  758. NK_BUTTON_LEFT,
  759. NK_BUTTON_MIDDLE,
  760. NK_BUTTON_RIGHT,
  761. NK_BUTTON_DOUBLE,
  762. NK_BUTTON_MAX
  763. };
  764. /**
  765. * \brief Begins the input mirroring process by resetting text, scroll
  766. * mouse, previous mouse position and movement as well as key state transitions.
  767. *
  768. * \details
  769. * ```c
  770. * void nk_input_begin(struct nk_context*);
  771. * ```
  772. *
  773. * \param[in] ctx Must point to a previously initialized `nk_context` struct
  774. */
  775. NK_API void nk_input_begin(struct nk_context*);
  776. /**
  777. * \brief Mirrors current mouse position to nuklear
  778. *
  779. * \details
  780. * ```c
  781. * void nk_input_motion(struct nk_context *ctx, int x, int y);
  782. * ```
  783. *
  784. * \param[in] ctx Must point to a previously initialized `nk_context` struct
  785. * \param[in] x Must hold an integer describing the current mouse cursor x-position
  786. * \param[in] y Must hold an integer describing the current mouse cursor y-position
  787. */
  788. NK_API void nk_input_motion(struct nk_context*, int x, int y);
  789. /**
  790. * \brief Mirrors the state of a specific key to nuklear
  791. *
  792. * \details
  793. * ```c
  794. * void nk_input_key(struct nk_context*, enum nk_keys key, nk_bool down);
  795. * ```
  796. *
  797. * \param[in] ctx Must point to a previously initialized `nk_context` struct
  798. * \param[in] key Must be any value specified in enum `nk_keys` that needs to be mirrored
  799. * \param[in] down Must be 0 for key is up and 1 for key is down
  800. */
  801. NK_API void nk_input_key(struct nk_context*, enum nk_keys, nk_bool down);
  802. /**
  803. * \brief Mirrors the state of a specific mouse button to nuklear
  804. *
  805. * \details
  806. * ```c
  807. * void nk_input_button(struct nk_context *ctx, enum nk_buttons btn, int x, int y, nk_bool down);
  808. * ```
  809. *
  810. * \param[in] ctx Must point to a previously initialized `nk_context` struct
  811. * \param[in] btn Must be any value specified in enum `nk_buttons` that needs to be mirrored
  812. * \param[in] x Must contain an integer describing mouse cursor x-position on click up/down
  813. * \param[in] y Must contain an integer describing mouse cursor y-position on click up/down
  814. * \param[in] down Must be 0 for key is up and 1 for key is down
  815. */
  816. NK_API void nk_input_button(struct nk_context*, enum nk_buttons, int x, int y, nk_bool down);
  817. /**
  818. * \brief Copies the last mouse scroll value to nuklear.
  819. *
  820. * \details
  821. * Is generally a scroll value. So does not have to come from mouse and could
  822. * also originate from balls, tracks, linear guide rails, or other programs.
  823. *
  824. * ```c
  825. * void nk_input_scroll(struct nk_context *ctx, struct nk_vec2 val);
  826. * ```
  827. *
  828. * \param[in] ctx | Must point to a previously initialized `nk_context` struct
  829. * \param[in] val | vector with both X- as well as Y-scroll value
  830. */
  831. NK_API void nk_input_scroll(struct nk_context*, struct nk_vec2 val);
  832. /**
  833. * \brief Copies a single ASCII character into an internal text buffer
  834. *
  835. * \details
  836. * This is basically a helper function to quickly push ASCII characters into
  837. * nuklear.
  838. *
  839. * \note
  840. * Stores up to NK_INPUT_MAX bytes between `nk_input_begin` and `nk_input_end`.
  841. *
  842. * ```c
  843. * void nk_input_char(struct nk_context *ctx, char c);
  844. * ```
  845. *
  846. * \param[in] ctx | Must point to a previously initialized `nk_context` struct
  847. * \param[in] c | Must be a single ASCII character preferable one that can be printed
  848. */
  849. NK_API void nk_input_char(struct nk_context*, char);
  850. /**
  851. * \brief Converts an encoded unicode rune into UTF-8 and copies the result into an
  852. * internal text buffer.
  853. *
  854. * \note
  855. * Stores up to NK_INPUT_MAX bytes between `nk_input_begin` and `nk_input_end`.
  856. *
  857. * ```c
  858. * void nk_input_glyph(struct nk_context *ctx, const nk_glyph g);
  859. * ```
  860. *
  861. * \param[in] ctx | Must point to a previously initialized `nk_context` struct
  862. * \param[in] g | UTF-32 unicode codepoint
  863. */
  864. NK_API void nk_input_glyph(struct nk_context*, const nk_glyph);
  865. /**
  866. * \brief Converts a unicode rune into UTF-8 and copies the result
  867. * into an internal text buffer.
  868. *
  869. * \details
  870. * \note
  871. * Stores up to NK_INPUT_MAX bytes between `nk_input_begin` and `nk_input_end`.
  872. *
  873. * ```c
  874. * void nk_input_unicode(struct nk_context*, nk_rune rune);
  875. * ```
  876. *
  877. * \param[in] ctx | Must point to a previously initialized `nk_context` struct
  878. * \param[in] rune | UTF-32 unicode codepoint
  879. */
  880. NK_API void nk_input_unicode(struct nk_context*, nk_rune);
  881. /**
  882. * \brief End the input mirroring process by resetting mouse grabbing
  883. * state to ensure the mouse cursor is not grabbed indefinitely.
  884. *
  885. * \details
  886. * ```c
  887. * void nk_input_end(struct nk_context *ctx);
  888. * ```
  889. *
  890. * \param[in] ctx | Must point to a previously initialized `nk_context` struct
  891. */
  892. NK_API void nk_input_end(struct nk_context*);
  893. /** =============================================================================
  894. *
  895. * DRAWING
  896. *
  897. * =============================================================================*/
  898. /**
  899. * \page Drawing
  900. * This library was designed to be render backend agnostic so it does
  901. * not draw anything to screen directly. Instead all drawn shapes, widgets
  902. * are made of, are buffered into memory and make up a command queue.
  903. * Each frame therefore fills the command buffer with draw commands
  904. * that then need to be executed by the user and his own render backend.
  905. * After that the command buffer needs to be cleared and a new frame can be
  906. * started. It is probably important to note that the command buffer is the main
  907. * drawing API and the optional vertex buffer API only takes this format and
  908. * converts it into a hardware accessible format.
  909. *
  910. * # Usage
  911. * To draw all draw commands accumulated over a frame you need your own render
  912. * backend able to draw a number of 2D primitives. This includes at least
  913. * filled and stroked rectangles, circles, text, lines, triangles and scissors.
  914. * As soon as this criterion is met you can iterate over each draw command
  915. * and execute each draw command in a interpreter like fashion:
  916. *
  917. * ```c
  918. * const struct nk_command *cmd = 0;
  919. * nk_foreach(cmd, &ctx) {
  920. * switch (cmd->type) {
  921. * case NK_COMMAND_LINE:
  922. * your_draw_line_function(...)
  923. * break;
  924. * case NK_COMMAND_RECT
  925. * your_draw_rect_function(...)
  926. * break;
  927. * case //...:
  928. * //[...]
  929. * }
  930. * }
  931. * ```
  932. *
  933. * In program flow context draw commands need to be executed after input has been
  934. * gathered and the complete UI with windows and their contained widgets have
  935. * been executed and before calling `nk_clear` which frees all previously
  936. * allocated draw commands.
  937. *
  938. * ```c
  939. * struct nk_context ctx;
  940. * nk_init_xxx(&ctx, ...);
  941. * while (1) {
  942. * Event evt;
  943. * nk_input_begin(&ctx);
  944. * while (GetEvent(&evt)) {
  945. * if (evt.type == MOUSE_MOVE)
  946. * nk_input_motion(&ctx, evt.motion.x, evt.motion.y);
  947. * else if (evt.type == [...]) {
  948. * [...]
  949. * }
  950. * }
  951. * nk_input_end(&ctx);
  952. * //
  953. * // [...]
  954. * //
  955. * const struct nk_command *cmd = 0;
  956. * nk_foreach(cmd, &ctx) {
  957. * switch (cmd->type) {
  958. * case NK_COMMAND_LINE:
  959. * your_draw_line_function(...)
  960. * break;
  961. * case NK_COMMAND_RECT
  962. * your_draw_rect_function(...)
  963. * break;
  964. * case ...:
  965. * // [...]
  966. * }
  967. * nk_clear(&ctx);
  968. * }
  969. * nk_free(&ctx);
  970. * ```
  971. *
  972. * You probably noticed that you have to draw all of the UI each frame which is
  973. * quite wasteful. While the actual UI updating loop is quite fast rendering
  974. * without actually needing it is not. So there are multiple things you could do.
  975. *
  976. * First is only update on input. This of course is only an option if your
  977. * application only depends on the UI and does not require any outside calculations.
  978. * If you actually only update on input make sure to update the UI two times each
  979. * frame and call `nk_clear` directly after the first pass and only draw in
  980. * the second pass. In addition it is recommended to also add additional timers
  981. * to make sure the UI is not drawn more than a fixed number of frames per second.
  982. *
  983. * ```c
  984. * struct nk_context ctx;
  985. * nk_init_xxx(&ctx, ...);
  986. * while (1) {
  987. * // [...wait for input ]
  988. * // [...do two UI passes ...]
  989. * do_ui(...)
  990. * nk_clear(&ctx);
  991. * do_ui(...)
  992. * //
  993. * // draw
  994. * const struct nk_command *cmd = 0;
  995. * nk_foreach(cmd, &ctx) {
  996. * switch (cmd->type) {
  997. * case NK_COMMAND_LINE:
  998. * your_draw_line_function(...)
  999. * break;
  1000. * case NK_COMMAND_RECT
  1001. * your_draw_rect_function(...)
  1002. * break;
  1003. * case ...:
  1004. * //[...]
  1005. * }
  1006. * nk_clear(&ctx);
  1007. * }
  1008. * nk_free(&ctx);
  1009. * ```
  1010. *
  1011. * The second probably more applicable trick is to only draw if anything changed.
  1012. * It is not really useful for applications with continuous draw loop but
  1013. * quite useful for desktop applications. To actually get nuklear to only
  1014. * draw on changes you first have to define `NK_ZERO_COMMAND_MEMORY` and
  1015. * allocate a memory buffer that will store each unique drawing output.
  1016. * After each frame you compare the draw command memory inside the library
  1017. * with your allocated buffer by memcmp. If memcmp detects differences
  1018. * you have to copy the command buffer into the allocated buffer
  1019. * and then draw like usual (this example uses fixed memory but you could
  1020. * use dynamically allocated memory).
  1021. *
  1022. * ```c
  1023. * //[... other defines ...]
  1024. * #define NK_ZERO_COMMAND_MEMORY
  1025. * #include "nuklear.h"
  1026. * //
  1027. * // setup context
  1028. * struct nk_context ctx;
  1029. * void *last = calloc(1,64*1024);
  1030. * void *buf = calloc(1,64*1024);
  1031. * nk_init_fixed(&ctx, buf, 64*1024);
  1032. * //
  1033. * // loop
  1034. * while (1) {
  1035. * // [...input...]
  1036. * // [...ui...]
  1037. * void *cmds = nk_buffer_memory(&ctx.memory);
  1038. * if (memcmp(cmds, last, ctx.memory.allocated)) {
  1039. * memcpy(last,cmds,ctx.memory.allocated);
  1040. * const struct nk_command *cmd = 0;
  1041. * nk_foreach(cmd, &ctx) {
  1042. * switch (cmd->type) {
  1043. * case NK_COMMAND_LINE:
  1044. * your_draw_line_function(...)
  1045. * break;
  1046. * case NK_COMMAND_RECT
  1047. * your_draw_rect_function(...)
  1048. * break;
  1049. * case ...:
  1050. * // [...]
  1051. * }
  1052. * }
  1053. * }
  1054. * nk_clear(&ctx);
  1055. * }
  1056. * nk_free(&ctx);
  1057. * ```
  1058. *
  1059. * Finally while using draw commands makes sense for higher abstracted platforms like
  1060. * X11 and Win32 or drawing libraries it is often desirable to use graphics
  1061. * hardware directly. Therefore it is possible to just define
  1062. * `NK_INCLUDE_VERTEX_BUFFER_OUTPUT` which includes optional vertex output.
  1063. * To access the vertex output you first have to convert all draw commands into
  1064. * vertexes by calling `nk_convert` which takes in your preferred vertex format.
  1065. * After successfully converting all draw commands just iterate over and execute all
  1066. * vertex draw commands:
  1067. *
  1068. * ```c
  1069. * // fill configuration
  1070. * struct your_vertex
  1071. * {
  1072. * float pos[2]; // important to keep it to 2 floats
  1073. * float uv[2];
  1074. * unsigned char col[4];
  1075. * };
  1076. * struct nk_convert_config cfg = {};
  1077. * static const struct nk_draw_vertex_layout_element vertex_layout[] = {
  1078. * {NK_VERTEX_POSITION, NK_FORMAT_FLOAT, NK_OFFSETOF(struct your_vertex, pos)},
  1079. * {NK_VERTEX_TEXCOORD, NK_FORMAT_FLOAT, NK_OFFSETOF(struct your_vertex, uv)},
  1080. * {NK_VERTEX_COLOR, NK_FORMAT_R8G8B8A8, NK_OFFSETOF(struct your_vertex, col)},
  1081. * {NK_VERTEX_LAYOUT_END}
  1082. * };
  1083. * cfg.shape_AA = NK_ANTI_ALIASING_ON;
  1084. * cfg.line_AA = NK_ANTI_ALIASING_ON;
  1085. * cfg.vertex_layout = vertex_layout;
  1086. * cfg.vertex_size = sizeof(struct your_vertex);
  1087. * cfg.vertex_alignment = NK_ALIGNOF(struct your_vertex);
  1088. * cfg.circle_segment_count = 22;
  1089. * cfg.curve_segment_count = 22;
  1090. * cfg.arc_segment_count = 22;
  1091. * cfg.global_alpha = 1.0f;
  1092. * cfg.tex_null = dev->tex_null;
  1093. * //
  1094. * // setup buffers and convert
  1095. * struct nk_buffer cmds, verts, idx;
  1096. * nk_buffer_init_default(&cmds);
  1097. * nk_buffer_init_default(&verts);
  1098. * nk_buffer_init_default(&idx);
  1099. * nk_convert(&ctx, &cmds, &verts, &idx, &cfg);
  1100. * //
  1101. * // draw
  1102. * nk_draw_foreach(cmd, &ctx, &cmds) {
  1103. * if (!cmd->elem_count) continue;
  1104. * //[...]
  1105. * }
  1106. * nk_buffer_free(&cms);
  1107. * nk_buffer_free(&verts);
  1108. * nk_buffer_free(&idx);
  1109. * ```
  1110. *
  1111. * # Reference
  1112. * Function | Description
  1113. * --------------------|-------------------------------------------------------
  1114. * \ref nk__begin | Returns the first draw command in the context draw command list to be drawn
  1115. * \ref nk__next | Increments the draw command iterator to the next command inside the context draw command list
  1116. * \ref nk_foreach | Iterates over each draw command inside the context draw command list
  1117. * \ref nk_convert | Converts from the abstract draw commands list into a hardware accessible vertex format
  1118. * \ref nk_draw_begin | Returns the first vertex command in the context vertex draw list to be executed
  1119. * \ref nk__draw_next | Increments the vertex command iterator to the next command inside the context vertex command list
  1120. * \ref nk__draw_end | Returns the end of the vertex draw list
  1121. * \ref nk_draw_foreach | Iterates over each vertex draw command inside the vertex draw list
  1122. */
  1123. enum nk_anti_aliasing {NK_ANTI_ALIASING_OFF, NK_ANTI_ALIASING_ON};
  1124. enum nk_convert_result {
  1125. NK_CONVERT_SUCCESS = 0,
  1126. NK_CONVERT_INVALID_PARAM = 1,
  1127. NK_CONVERT_COMMAND_BUFFER_FULL = NK_FLAG(1),
  1128. NK_CONVERT_VERTEX_BUFFER_FULL = NK_FLAG(2),
  1129. NK_CONVERT_ELEMENT_BUFFER_FULL = NK_FLAG(3)
  1130. };
  1131. struct nk_draw_null_texture {
  1132. nk_handle texture; /**!< texture handle to a texture with a white pixel */
  1133. struct nk_vec2 uv; /**!< coordinates to a white pixel in the texture */
  1134. };
  1135. struct nk_convert_config {
  1136. float global_alpha; /**!< global alpha value */
  1137. enum nk_anti_aliasing line_AA; /**!< line anti-aliasing flag can be turned off if you are tight on memory */
  1138. enum nk_anti_aliasing shape_AA; /**!< shape anti-aliasing flag can be turned off if you are tight on memory */
  1139. unsigned circle_segment_count; /**!< number of segments used for circles: default to 22 */
  1140. unsigned arc_segment_count; /**!< number of segments used for arcs: default to 22 */
  1141. unsigned curve_segment_count; /**!< number of segments used for curves: default to 22 */
  1142. struct nk_draw_null_texture tex_null; /**!< handle to texture with a white pixel for shape drawing */
  1143. const struct nk_draw_vertex_layout_element *vertex_layout; /**!< describes the vertex output format and packing */
  1144. nk_size vertex_size; /**!< sizeof one vertex for vertex packing */
  1145. nk_size vertex_alignment; /**!< vertex alignment: Can be obtained by NK_ALIGNOF */
  1146. };
  1147. /**
  1148. * \brief Returns a draw command list iterator to iterate all draw
  1149. * commands accumulated over one frame.
  1150. *
  1151. * \details
  1152. * ```c
  1153. * const struct nk_command* nk__begin(struct nk_context*);
  1154. * ```
  1155. *
  1156. * \param[in] ctx | must point to an previously initialized `nk_context` struct at the end of a frame
  1157. *
  1158. * \returns draw command pointer pointing to the first command inside the draw command list
  1159. */
  1160. NK_API const struct nk_command* nk__begin(struct nk_context*);
  1161. /**
  1162. * \brief Returns draw command pointer pointing to the next command inside the draw command list
  1163. *
  1164. * \details
  1165. * ```c
  1166. * const struct nk_command* nk__next(struct nk_context*, const struct nk_command*);
  1167. * ```
  1168. *
  1169. * \param[in] ctx | Must point to an previously initialized `nk_context` struct at the end of a frame
  1170. * \param[in] cmd | Must point to an previously a draw command either returned by `nk__begin` or `nk__next`
  1171. *
  1172. * \returns draw command pointer pointing to the next command inside the draw command list
  1173. */
  1174. NK_API const struct nk_command* nk__next(struct nk_context*, const struct nk_command*);
  1175. /**
  1176. * \brief Iterates over each draw command inside the context draw command list
  1177. *
  1178. * ```c
  1179. * #define nk_foreach(c, ctx)
  1180. * ```
  1181. *
  1182. * \param[in] ctx | Must point to an previously initialized `nk_context` struct at the end of a frame
  1183. * \param[in] cmd | Command pointer initialized to NULL
  1184. */
  1185. #define nk_foreach(c, ctx) for((c) = nk__begin(ctx); (c) != 0; (c) = nk__next(ctx,c))
  1186. #ifdef NK_INCLUDE_VERTEX_BUFFER_OUTPUT
  1187. /**
  1188. * \brief Converts all internal draw commands into vertex draw commands and fills
  1189. * three buffers with vertexes, vertex draw commands and vertex indices.
  1190. *
  1191. * \details
  1192. * The vertex format as well as some other configuration values have to be
  1193. * configured by filling out a `nk_convert_config` struct.
  1194. *
  1195. * ```c
  1196. * nk_flags nk_convert(struct nk_context *ctx, struct nk_buffer *cmds,
  1197. * struct nk_buffer *vertices, struct nk_buffer *elements, const struct nk_convert_config*);
  1198. * ```
  1199. *
  1200. * \param[in] ctx Must point to an previously initialized `nk_context` struct at the end of a frame
  1201. * \param[out] cmds Must point to a previously initialized buffer to hold converted vertex draw commands
  1202. * \param[out] vertices Must point to a previously initialized buffer to hold all produced vertices
  1203. * \param[out] elements Must point to a previously initialized buffer to hold all produced vertex indices
  1204. * \param[in] config Must point to a filled out `nk_config` struct to configure the conversion process
  1205. *
  1206. * \returns one of enum nk_convert_result error codes
  1207. *
  1208. * Parameter | Description
  1209. * --------------------------------|-----------------------------------------------------------
  1210. * NK_CONVERT_SUCCESS | Signals a successful draw command to vertex buffer conversion
  1211. * NK_CONVERT_INVALID_PARAM | An invalid argument was passed in the function call
  1212. * NK_CONVERT_COMMAND_BUFFER_FULL | The provided buffer for storing draw commands is full or failed to allocate more memory
  1213. * NK_CONVERT_VERTEX_BUFFER_FULL | The provided buffer for storing vertices is full or failed to allocate more memory
  1214. * NK_CONVERT_ELEMENT_BUFFER_FULL | The provided buffer for storing indices is full or failed to allocate more memory
  1215. */
  1216. 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*);
  1217. /**
  1218. * \brief Returns a draw vertex command buffer iterator to iterate over the vertex draw command buffer
  1219. *
  1220. * \details
  1221. * ```c
  1222. * const struct nk_draw_command* nk__draw_begin(const struct nk_context*, const struct nk_buffer*);
  1223. * ```
  1224. *
  1225. * \param[in] ctx | Must point to an previously initialized `nk_context` struct at the end of a frame
  1226. * \param[in] buf | Must point to an previously by `nk_convert` filled out vertex draw command buffer
  1227. *
  1228. * \returns vertex draw command pointer pointing to the first command inside the vertex draw command buffer
  1229. */
  1230. NK_API const struct nk_draw_command* nk__draw_begin(const struct nk_context*, const struct nk_buffer*);
  1231. /**
  1232. * # # nk__draw_end
  1233. * \returns the vertex draw command at the end of the vertex draw command buffer
  1234. *
  1235. * ```c
  1236. * const struct nk_draw_command* nk__draw_end(const struct nk_context *ctx, const struct nk_buffer *buf);
  1237. * ```
  1238. *
  1239. * Parameter | Description
  1240. * ------------|-----------------------------------------------------------
  1241. * \param[in] ctx | Must point to an previously initialized `nk_context` struct at the end of a frame
  1242. * \param[in] buf | Must point to an previously by `nk_convert` filled out vertex draw command buffer
  1243. *
  1244. * \returns vertex draw command pointer pointing to the end of the last vertex draw command inside the vertex draw command buffer
  1245. */
  1246. NK_API const struct nk_draw_command* nk__draw_end(const struct nk_context*, const struct nk_buffer*);
  1247. /**
  1248. * # # nk__draw_next
  1249. * Increments the vertex draw command buffer iterator
  1250. *
  1251. * ```c
  1252. * const struct nk_draw_command* nk__draw_next(const struct nk_draw_command*, const struct nk_buffer*, const struct nk_context*);
  1253. * ```
  1254. *
  1255. * Parameter | Description
  1256. * ------------|-----------------------------------------------------------
  1257. * \param[in] cmd | Must point to an previously either by `nk__draw_begin` or `nk__draw_next` returned vertex draw command
  1258. * \param[in] buf | Must point to an previously by `nk_convert` filled out vertex draw command buffer
  1259. * \param[in] ctx | Must point to an previously initialized `nk_context` struct at the end of a frame
  1260. *
  1261. * \returns vertex draw command pointer pointing to the end of the last vertex draw command inside the vertex draw command buffer
  1262. */
  1263. NK_API const struct nk_draw_command* nk__draw_next(const struct nk_draw_command*, const struct nk_buffer*, const struct nk_context*);
  1264. /**
  1265. * # # nk_draw_foreach
  1266. * Iterates over each vertex draw command inside a vertex draw command buffer
  1267. *
  1268. * ```c
  1269. * #define nk_draw_foreach(cmd,ctx, b)
  1270. * ```
  1271. *
  1272. * Parameter | Description
  1273. * ------------|-----------------------------------------------------------
  1274. * \param[in] cmd | `nk_draw_command`iterator set to NULL
  1275. * \param[in] buf | Must point to an previously by `nk_convert` filled out vertex draw command buffer
  1276. * \param[in] ctx | Must point to an previously initialized `nk_context` struct at the end of a frame
  1277. */
  1278. #define nk_draw_foreach(cmd,ctx, b) for((cmd)=nk__draw_begin(ctx, b); (cmd)!=0; (cmd)=nk__draw_next(cmd, b, ctx))
  1279. #endif
  1280. /** =============================================================================
  1281. *
  1282. * WINDOW
  1283. *
  1284. * =============================================================================*/
  1285. /**
  1286. * \page Window
  1287. * Windows are the main persistent state used inside nuklear and are life time
  1288. * controlled by simply "retouching" (i.e. calling) each window each frame.
  1289. * All widgets inside nuklear can only be added inside the function pair `nk_begin_xxx`
  1290. * and `nk_end`. Calling any widgets outside these two functions will result in an
  1291. * assert in debug or no state change in release mode.<br /><br />
  1292. *
  1293. * Each window holds frame persistent state like position, size, flags, state tables,
  1294. * and some garbage collected internal persistent widget state. Each window
  1295. * is linked into a window stack list which determines the drawing and overlapping
  1296. * order. The topmost window thereby is the currently active window.<br /><br />
  1297. *
  1298. * To change window position inside the stack occurs either automatically by
  1299. * user input by being clicked on or programmatically by calling `nk_window_focus`.
  1300. * Windows by default are visible unless explicitly being defined with flag
  1301. * `NK_WINDOW_HIDDEN`, the user clicked the close button on windows with flag
  1302. * `NK_WINDOW_CLOSABLE` or if a window was explicitly hidden by calling
  1303. * `nk_window_show`. To explicitly close and destroy a window call `nk_window_close`.<br /><br />
  1304. *
  1305. * # Usage
  1306. * To create and keep a window you have to call one of the two `nk_begin_xxx`
  1307. * functions to start window declarations and `nk_end` at the end. Furthermore it
  1308. * is recommended to check the return value of `nk_begin_xxx` and only process
  1309. * widgets inside the window if the value is not 0. Either way you have to call
  1310. * `nk_end` at the end of window declarations. Furthermore, do not attempt to
  1311. * nest `nk_begin_xxx` calls which will hopefully result in an assert or if not
  1312. * in a segmentation fault.
  1313. *
  1314. * ```c
  1315. * if (nk_begin_xxx(...) {
  1316. * // [... widgets ...]
  1317. * }
  1318. * nk_end(ctx);
  1319. * ```
  1320. *
  1321. * In the grand concept window and widget declarations need to occur after input
  1322. * handling and before drawing to screen. Not doing so can result in higher
  1323. * latency or at worst invalid behavior. Furthermore make sure that `nk_clear`
  1324. * is called at the end of the frame. While nuklear's default platform backends
  1325. * already call `nk_clear` for you if you write your own backend not calling
  1326. * `nk_clear` can cause asserts or even worse undefined behavior.
  1327. *
  1328. * ```c
  1329. * struct nk_context ctx;
  1330. * nk_init_xxx(&ctx, ...);
  1331. * while (1) {
  1332. * Event evt;
  1333. * nk_input_begin(&ctx);
  1334. * while (GetEvent(&evt)) {
  1335. * if (evt.type == MOUSE_MOVE)
  1336. * nk_input_motion(&ctx, evt.motion.x, evt.motion.y);
  1337. * else if (evt.type == [...]) {
  1338. * nk_input_xxx(...);
  1339. * }
  1340. * }
  1341. * nk_input_end(&ctx);
  1342. *
  1343. * if (nk_begin_xxx(...) {
  1344. * //[...]
  1345. * }
  1346. * nk_end(ctx);
  1347. *
  1348. * const struct nk_command *cmd = 0;
  1349. * nk_foreach(cmd, &ctx) {
  1350. * case NK_COMMAND_LINE:
  1351. * your_draw_line_function(...)
  1352. * break;
  1353. * case NK_COMMAND_RECT
  1354. * your_draw_rect_function(...)
  1355. * break;
  1356. * case //...:
  1357. * //[...]
  1358. * }
  1359. * nk_clear(&ctx);
  1360. * }
  1361. * nk_free(&ctx);
  1362. * ```
  1363. *
  1364. * # Reference
  1365. * Function | Description
  1366. * ------------------------------------|----------------------------------------
  1367. * \ref nk_begin | Starts a new window; needs to be called every frame for every window (unless hidden) or otherwise the window gets removed
  1368. * \ref nk_begin_titled | Extended window start with separated title and identifier to allow multiple windows with same name but not title
  1369. * \ref nk_end | Needs to be called at the end of the window building process to process scaling, scrollbars and general cleanup
  1370. *
  1371. * \ref nk_window_find | Finds and returns the window with give name
  1372. * \ref nk_window_get_bounds | Returns a rectangle with screen position and size of the currently processed window.
  1373. * \ref nk_window_get_position | Returns the position of the currently processed window
  1374. * \ref nk_window_get_size | Returns the size with width and height of the currently processed window
  1375. * \ref nk_window_get_width | Returns the width of the currently processed window
  1376. * \ref nk_window_get_height | Returns the height of the currently processed window
  1377. * \ref nk_window_get_panel | Returns the underlying panel which contains all processing state of the current window
  1378. * \ref nk_window_get_content_region | Returns the position and size of the currently visible and non-clipped space inside the currently processed window
  1379. * \ref nk_window_get_content_region_min | Returns the upper rectangle position of the currently visible and non-clipped space inside the currently processed window
  1380. * \ref nk_window_get_content_region_max | Returns the upper rectangle position of the currently visible and non-clipped space inside the currently processed window
  1381. * \ref nk_window_get_content_region_size | Returns the size of the currently visible and non-clipped space inside the currently processed window
  1382. * \ref nk_window_get_canvas | Returns the draw command buffer. Can be used to draw custom widgets
  1383. * \ref nk_window_get_scroll | Gets the scroll offset of the current window
  1384. * \ref nk_window_has_focus | Returns if the currently processed window is currently active
  1385. * \ref nk_window_is_collapsed | Returns if the window with given name is currently minimized/collapsed
  1386. * \ref nk_window_is_closed | Returns if the currently processed window was closed
  1387. * \ref nk_window_is_hidden | Returns if the currently processed window was hidden
  1388. * \ref nk_window_is_active | Same as nk_window_has_focus for some reason
  1389. * \ref nk_window_is_hovered | Returns if the currently processed window is currently being hovered by mouse
  1390. * \ref nk_window_is_any_hovered | Return if any window currently hovered
  1391. * \ref nk_item_is_any_active | Returns if any window or widgets is currently hovered or active
  1392. //
  1393. * \ref nk_window_set_bounds | Updates position and size of the currently processed window
  1394. * \ref nk_window_set_position | Updates position of the currently process window
  1395. * \ref nk_window_set_size | Updates the size of the currently processed window
  1396. * \ref nk_window_set_focus | Set the currently processed window as active window
  1397. * \ref nk_window_set_scroll | Sets the scroll offset of the current window
  1398. //
  1399. * \ref nk_window_close | Closes the window with given window name which deletes the window at the end of the frame
  1400. * \ref nk_window_collapse | Collapses the window with given window name
  1401. * \ref nk_window_collapse_if | Collapses the window with given window name if the given condition was met
  1402. * \ref nk_window_show | Hides a visible or reshows a hidden window
  1403. * \ref nk_window_show_if | Hides/shows a window depending on condition
  1404. * # nk_panel_flags
  1405. * Flag | Description
  1406. * ----------------------------|----------------------------------------
  1407. * NK_WINDOW_BORDER | Draws a border around the window to visually separate window from the background
  1408. * NK_WINDOW_MOVABLE | The movable flag indicates that a window can be moved by user input or by dragging the window header
  1409. * 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
  1410. * NK_WINDOW_CLOSABLE | Adds a closable icon into the header
  1411. * NK_WINDOW_MINIMIZABLE | Adds a minimize icon into the header
  1412. * NK_WINDOW_NO_SCROLLBAR | Removes the scrollbar from the window
  1413. * NK_WINDOW_TITLE | Forces a header at the top at the window showing the title
  1414. * 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
  1415. * NK_WINDOW_BACKGROUND | Always keep window in the background
  1416. * NK_WINDOW_SCALE_LEFT | Puts window scaler in the left-bottom corner instead right-bottom
  1417. * NK_WINDOW_NO_INPUT | Prevents window of scaling, moving or getting focus
  1418. *
  1419. * # nk_collapse_states
  1420. * State | Description
  1421. * ----------------|-----------------------------------------------------------
  1422. * NK_MINIMIZED| UI section is collapsed and not visible until maximized
  1423. * NK_MAXIMIZED| UI section is extended and visible until minimized
  1424. */
  1425. enum nk_panel_flags {
  1426. NK_WINDOW_BORDER = NK_FLAG(0),
  1427. NK_WINDOW_MOVABLE = NK_FLAG(1),
  1428. NK_WINDOW_SCALABLE = NK_FLAG(2),
  1429. NK_WINDOW_CLOSABLE = NK_FLAG(3),
  1430. NK_WINDOW_MINIMIZABLE = NK_FLAG(4),
  1431. NK_WINDOW_NO_SCROLLBAR = NK_FLAG(5),
  1432. NK_WINDOW_TITLE = NK_FLAG(6),
  1433. NK_WINDOW_SCROLL_AUTO_HIDE = NK_FLAG(7),
  1434. NK_WINDOW_BACKGROUND = NK_FLAG(8),
  1435. NK_WINDOW_SCALE_LEFT = NK_FLAG(9),
  1436. NK_WINDOW_NO_INPUT = NK_FLAG(10)
  1437. };
  1438. /**
  1439. * # # nk_begin
  1440. * Starts a new window; needs to be called every frame for every
  1441. * window (unless hidden) or otherwise the window gets removed
  1442. *
  1443. * ```c
  1444. * nk_bool nk_begin(struct nk_context *ctx, const char *title, struct nk_rect bounds, nk_flags flags);
  1445. * ```
  1446. *
  1447. * Parameter | Description
  1448. * ------------|-----------------------------------------------------------
  1449. * \param[in] ctx | Must point to an previously initialized `nk_context` struct
  1450. * \param[in] title | Window title and identifier. Needs to be persistent over frames to identify the window
  1451. * \param[in] 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
  1452. * \param[in] flags | Window flags defined in the nk_panel_flags section with a number of different window behaviors
  1453. *
  1454. * \returns `true(1)` if the window can be filled up with widgets from this point
  1455. * until `nk_end` or `false(0)` otherwise for example if minimized
  1456. */
  1457. NK_API nk_bool nk_begin(struct nk_context *ctx, const char *title, struct nk_rect bounds, nk_flags flags);
  1458. /**
  1459. * # # nk_begin_titled
  1460. * Extended window start with separated title and identifier to allow multiple
  1461. * windows with same title but not name
  1462. *
  1463. * ```c
  1464. * nk_bool nk_begin_titled(struct nk_context *ctx, const char *name, const char *title, struct nk_rect bounds, nk_flags flags);
  1465. * ```
  1466. *
  1467. * Parameter | Description
  1468. * ------------|-----------------------------------------------------------
  1469. * \param[in] ctx | Must point to an previously initialized `nk_context` struct
  1470. * \param[in] name | Window identifier. Needs to be persistent over frames to identify the window
  1471. * \param[in] title | Window title displayed inside header if flag `NK_WINDOW_TITLE` or either `NK_WINDOW_CLOSABLE` or `NK_WINDOW_MINIMIZED` was set
  1472. * \param[in] 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
  1473. * \param[in] flags | Window flags defined in the nk_panel_flags section with a number of different window behaviors
  1474. *
  1475. * \returns `true(1)` if the window can be filled up with widgets from this point
  1476. * until `nk_end` or `false(0)` otherwise for example if minimized
  1477. */
  1478. NK_API nk_bool nk_begin_titled(struct nk_context *ctx, const char *name, const char *title, struct nk_rect bounds, nk_flags flags);
  1479. /**
  1480. * # # nk_end
  1481. * Needs to be called at the end of the window building process to process scaling, scrollbars and general cleanup.
  1482. * All widget calls after this functions will result in asserts or no state changes
  1483. *
  1484. * ```c
  1485. * void nk_end(struct nk_context *ctx);
  1486. * ```
  1487. *
  1488. * Parameter | Description
  1489. * ------------|-----------------------------------------------------------
  1490. * \param[in] ctx | Must point to an previously initialized `nk_context` struct
  1491. */
  1492. NK_API void nk_end(struct nk_context *ctx);
  1493. /**
  1494. * # # nk_window_find
  1495. * Finds and returns a window from passed name
  1496. *
  1497. * ```c
  1498. * struct nk_window *nk_window_find(struct nk_context *ctx, const char *name);
  1499. * ```
  1500. *
  1501. * Parameter | Description
  1502. * ------------|-----------------------------------------------------------
  1503. * \param[in] ctx | Must point to an previously initialized `nk_context` struct
  1504. * \param[in] name | Window identifier
  1505. *
  1506. * \returns a `nk_window` struct pointing to the identified window or NULL if
  1507. * no window with the given name was found
  1508. */
  1509. NK_API struct nk_window *nk_window_find(const struct nk_context *ctx, const char *name);
  1510. /**
  1511. * # # nk_window_get_bounds
  1512. * \returns a rectangle with screen position and size of the currently processed window
  1513. *
  1514. * !!! \warning
  1515. * Only call this function between calls `nk_begin_xxx` and `nk_end`
  1516. * ```c
  1517. * struct nk_rect nk_window_get_bounds(const struct nk_context *ctx);
  1518. * ```
  1519. *
  1520. * Parameter | Description
  1521. * ------------|-----------------------------------------------------------
  1522. * \param[in] ctx | Must point to an previously initialized `nk_context` struct
  1523. *
  1524. * \returns a `nk_rect` struct with window upper left window position and size
  1525. */
  1526. NK_API struct nk_rect nk_window_get_bounds(const struct nk_context *ctx);
  1527. /**
  1528. * # # nk_window_get_position
  1529. * \returns the position of the currently processed window.
  1530. *
  1531. * !!! \warning
  1532. * Only call this function between calls `nk_begin_xxx` and `nk_end`
  1533. * ```c
  1534. * struct nk_vec2 nk_window_get_position(const struct nk_context *ctx);
  1535. * ```
  1536. *
  1537. * Parameter | Description
  1538. * ------------|-----------------------------------------------------------
  1539. * \param[in] ctx | Must point to an previously initialized `nk_context` struct
  1540. *
  1541. * \returns a `nk_vec2` struct with window upper left position
  1542. */
  1543. NK_API struct nk_vec2 nk_window_get_position(const struct nk_context *ctx);
  1544. /**
  1545. * # # nk_window_get_size
  1546. * \returns the size with width and height of the currently processed window.
  1547. *
  1548. * !!! \warning
  1549. * Only call this function between calls `nk_begin_xxx` and `nk_end`
  1550. * ```c
  1551. * struct nk_vec2 nk_window_get_size(const struct nk_context *ctx);
  1552. * ```
  1553. *
  1554. * Parameter | Description
  1555. * ------------|-----------------------------------------------------------
  1556. * \param[in] ctx | Must point to an previously initialized `nk_context` struct
  1557. *
  1558. * \returns a `nk_vec2` struct with window width and height
  1559. */
  1560. NK_API struct nk_vec2 nk_window_get_size(const struct nk_context *ctx);
  1561. /**
  1562. * nk_window_get_width
  1563. * \returns the width of the currently processed window.
  1564. *
  1565. * !!! \warning
  1566. * Only call this function between calls `nk_begin_xxx` and `nk_end`
  1567. * ```c
  1568. * float nk_window_get_width(const struct nk_context *ctx);
  1569. * ```
  1570. *
  1571. * Parameter | Description
  1572. * ------------|-----------------------------------------------------------
  1573. * \param[in] ctx | Must point to an previously initialized `nk_context` struct
  1574. *
  1575. * \returns the current window width
  1576. */
  1577. NK_API float nk_window_get_width(const struct nk_context *ctx);
  1578. /**
  1579. * # # nk_window_get_height
  1580. * \returns the height of the currently processed window.
  1581. *
  1582. * !!! \warning
  1583. * Only call this function between calls `nk_begin_xxx` and `nk_end`
  1584. * ```c
  1585. * float nk_window_get_height(const struct nk_context *ctx);
  1586. * ```
  1587. *
  1588. * Parameter | Description
  1589. * ------------|-----------------------------------------------------------
  1590. * \param[in] ctx | Must point to an previously initialized `nk_context` struct
  1591. *
  1592. * \returns the current window height
  1593. */
  1594. NK_API float nk_window_get_height(const struct nk_context* ctx);
  1595. /**
  1596. * # # nk_window_get_panel
  1597. * \returns the underlying panel which contains all processing state of the current window.
  1598. *
  1599. * !!! \warning
  1600. * Only call this function between calls `nk_begin_xxx` and `nk_end`
  1601. * !!! \warning
  1602. * Do not keep the returned panel pointer around, it is only valid until `nk_end`
  1603. * ```c
  1604. * struct nk_panel* nk_window_get_panel(struct nk_context *ctx);
  1605. * ```
  1606. *
  1607. * Parameter | Description
  1608. * ------------|-----------------------------------------------------------
  1609. * \param[in] ctx | Must point to an previously initialized `nk_context` struct
  1610. *
  1611. * \returns a pointer to window internal `nk_panel` state.
  1612. */
  1613. NK_API struct nk_panel* nk_window_get_panel(const struct nk_context* ctx);
  1614. /**
  1615. * # # nk_window_get_content_region
  1616. * \returns the position and size of the currently visible and non-clipped space
  1617. * inside the currently processed window.
  1618. *
  1619. * !!! \warning
  1620. * Only call this function between calls `nk_begin_xxx` and `nk_end`
  1621. *
  1622. * ```c
  1623. * struct nk_rect nk_window_get_content_region(struct nk_context *ctx);
  1624. * ```
  1625. *
  1626. * Parameter | Description
  1627. * ------------|-----------------------------------------------------------
  1628. * \param[in] ctx | Must point to an previously initialized `nk_context` struct
  1629. *
  1630. * \returns `nk_rect` struct with screen position and size (no scrollbar offset)
  1631. * of the visible space inside the current window
  1632. */
  1633. NK_API struct nk_rect nk_window_get_content_region(const struct nk_context* ctx);
  1634. /**
  1635. * # # nk_window_get_content_region_min
  1636. * \returns the upper left position of the currently visible and non-clipped
  1637. * space inside the currently processed window.
  1638. *
  1639. * !!! \warning
  1640. * Only call this function between calls `nk_begin_xxx` and `nk_end`
  1641. *
  1642. * ```c
  1643. * struct nk_vec2 nk_window_get_content_region_min(struct nk_context *ctx);
  1644. * ```
  1645. *
  1646. * Parameter | Description
  1647. * ------------|-----------------------------------------------------------
  1648. * \param[in] ctx | Must point to an previously initialized `nk_context` struct
  1649. *
  1650. * returns `nk_vec2` struct with upper left screen position (no scrollbar offset)
  1651. * of the visible space inside the current window
  1652. */
  1653. NK_API struct nk_vec2 nk_window_get_content_region_min(const struct nk_context *ctx);
  1654. /**
  1655. * # # nk_window_get_content_region_max
  1656. * \returns the lower right screen position of the currently visible and
  1657. * non-clipped space inside the currently processed window.
  1658. *
  1659. * !!! \warning
  1660. * Only call this function between calls `nk_begin_xxx` and `nk_end`
  1661. *
  1662. * ```c
  1663. * struct nk_vec2 nk_window_get_content_region_max(struct nk_context *ctx);
  1664. * ```
  1665. *
  1666. * Parameter | Description
  1667. * ------------|-----------------------------------------------------------
  1668. * \param[in] ctx | Must point to an previously initialized `nk_context` struct
  1669. *
  1670. * \returns `nk_vec2` struct with lower right screen position (no scrollbar offset)
  1671. * of the visible space inside the current window
  1672. */
  1673. NK_API struct nk_vec2 nk_window_get_content_region_max(const struct nk_context *ctx);
  1674. /**
  1675. * # # nk_window_get_content_region_size
  1676. * \returns the size of the currently visible and non-clipped space inside the
  1677. * currently processed window
  1678. *
  1679. * !!! \warning
  1680. * Only call this function between calls `nk_begin_xxx` and `nk_end`
  1681. *
  1682. * ```c
  1683. * struct nk_vec2 nk_window_get_content_region_size(struct nk_context *ctx);
  1684. * ```
  1685. *
  1686. * Parameter | Description
  1687. * ------------|-----------------------------------------------------------
  1688. * \param[in] ctx | Must point to an previously initialized `nk_context` struct
  1689. *
  1690. * \returns `nk_vec2` struct with size the visible space inside the current window
  1691. */
  1692. NK_API struct nk_vec2 nk_window_get_content_region_size(const struct nk_context *ctx);
  1693. /**
  1694. * # # nk_window_get_canvas
  1695. * \returns the draw command buffer. Can be used to draw custom widgets
  1696. * !!! \warning
  1697. * Only call this function between calls `nk_begin_xxx` and `nk_end`
  1698. * !!! \warning
  1699. * Do not keep the returned command buffer pointer around it is only valid until `nk_end`
  1700. *
  1701. * ```c
  1702. * struct nk_command_buffer* nk_window_get_canvas(struct nk_context *ctx);
  1703. * ```
  1704. *
  1705. * Parameter | Description
  1706. * ------------|-----------------------------------------------------------
  1707. * \param[in] ctx | Must point to an previously initialized `nk_context` struct
  1708. *
  1709. * \returns a pointer to window internal `nk_command_buffer` struct used as
  1710. * drawing canvas. Can be used to do custom drawing.
  1711. */
  1712. NK_API struct nk_command_buffer* nk_window_get_canvas(const struct nk_context* ctx);
  1713. /**
  1714. * # # nk_window_get_scroll
  1715. * Gets the scroll offset for the current window
  1716. * !!! \warning
  1717. * Only call this function between calls `nk_begin_xxx` and `nk_end`
  1718. *
  1719. * ```c
  1720. * void nk_window_get_scroll(struct nk_context *ctx, nk_uint *offset_x, nk_uint *offset_y);
  1721. * ```
  1722. *
  1723. * Parameter | Description
  1724. * -------------|-----------------------------------------------------------
  1725. * \param[in] ctx | Must point to an previously initialized `nk_context` struct
  1726. * \param[in] offset_x | A pointer to the x offset output (or NULL to ignore)
  1727. * \param[in] offset_y | A pointer to the y offset output (or NULL to ignore)
  1728. */
  1729. NK_API void nk_window_get_scroll(const struct nk_context *ctx, nk_uint *offset_x, nk_uint *offset_y);
  1730. /**
  1731. * # # nk_window_has_focus
  1732. * \returns if the currently processed window is currently active
  1733. * !!! \warning
  1734. * Only call this function between calls `nk_begin_xxx` and `nk_end`
  1735. * ```c
  1736. * nk_bool nk_window_has_focus(const struct nk_context *ctx);
  1737. * ```
  1738. *
  1739. * Parameter | Description
  1740. * ------------|-----------------------------------------------------------
  1741. * \param[in] ctx | Must point to an previously initialized `nk_context` struct
  1742. *
  1743. * \returns `false(0)` if current window is not active or `true(1)` if it is
  1744. */
  1745. NK_API nk_bool nk_window_has_focus(const struct nk_context *ctx);
  1746. /**
  1747. * # # nk_window_is_hovered
  1748. * Return if the current window is being hovered
  1749. * !!! \warning
  1750. * Only call this function between calls `nk_begin_xxx` and `nk_end`
  1751. * ```c
  1752. * nk_bool nk_window_is_hovered(struct nk_context *ctx);
  1753. * ```
  1754. *
  1755. * Parameter | Description
  1756. * ------------|-----------------------------------------------------------
  1757. * \param[in] ctx | Must point to an previously initialized `nk_context` struct
  1758. *
  1759. * \returns `true(1)` if current window is hovered or `false(0)` otherwise
  1760. */
  1761. NK_API nk_bool nk_window_is_hovered(const struct nk_context *ctx);
  1762. /**
  1763. * # # nk_window_is_collapsed
  1764. * \returns if the window with given name is currently minimized/collapsed
  1765. * ```c
  1766. * nk_bool nk_window_is_collapsed(struct nk_context *ctx, const char *name);
  1767. * ```
  1768. *
  1769. * Parameter | Description
  1770. * ------------|-----------------------------------------------------------
  1771. * \param[in] ctx | Must point to an previously initialized `nk_context` struct
  1772. * \param[in] name | Identifier of window you want to check if it is collapsed
  1773. *
  1774. * \returns `true(1)` if current window is minimized and `false(0)` if window not
  1775. * found or is not minimized
  1776. */
  1777. NK_API nk_bool nk_window_is_collapsed(const struct nk_context *ctx, const char *name);
  1778. /**
  1779. * # # nk_window_is_closed
  1780. * \returns if the window with given name was closed by calling `nk_close`
  1781. * ```c
  1782. * nk_bool nk_window_is_closed(struct nk_context *ctx, const char *name);
  1783. * ```
  1784. *
  1785. * Parameter | Description
  1786. * ------------|-----------------------------------------------------------
  1787. * \param[in] ctx | Must point to an previously initialized `nk_context` struct
  1788. * \param[in] name | Identifier of window you want to check if it is closed
  1789. *
  1790. * \returns `true(1)` if current window was closed or `false(0)` window not found or not closed
  1791. */
  1792. NK_API nk_bool nk_window_is_closed(const struct nk_context *ctx, const char* name);
  1793. /**
  1794. * # # nk_window_is_hidden
  1795. * \returns if the window with given name is hidden
  1796. * ```c
  1797. * nk_bool nk_window_is_hidden(struct nk_context *ctx, const char *name);
  1798. * ```
  1799. *
  1800. * Parameter | Description
  1801. * ------------|-----------------------------------------------------------
  1802. * \param[in] ctx | Must point to an previously initialized `nk_context` struct
  1803. * \param[in] name | Identifier of window you want to check if it is hidden
  1804. *
  1805. * \returns `true(1)` if current window is hidden or `false(0)` window not found or visible
  1806. */
  1807. NK_API nk_bool nk_window_is_hidden(const struct nk_context *ctx, const char* name);
  1808. /**
  1809. * # # nk_window_is_active
  1810. * Same as nk_window_has_focus for some reason
  1811. * ```c
  1812. * nk_bool nk_window_is_active(struct nk_context *ctx, const char *name);
  1813. * ```
  1814. *
  1815. * Parameter | Description
  1816. * ------------|-----------------------------------------------------------
  1817. * \param[in] ctx | Must point to an previously initialized `nk_context` struct
  1818. * \param[in] name | Identifier of window you want to check if it is active
  1819. *
  1820. * \returns `true(1)` if current window is active or `false(0)` window not found or not active
  1821. */
  1822. NK_API nk_bool nk_window_is_active(const struct nk_context *ctx, const char* name);
  1823. /**
  1824. * # # nk_window_is_any_hovered
  1825. * \returns if the any window is being hovered
  1826. * ```c
  1827. * nk_bool nk_window_is_any_hovered(struct nk_context*);
  1828. * ```
  1829. *
  1830. * Parameter | Description
  1831. * ------------|-----------------------------------------------------------
  1832. * \param[in] ctx | Must point to an previously initialized `nk_context` struct
  1833. *
  1834. * \returns `true(1)` if any window is hovered or `false(0)` otherwise
  1835. */
  1836. NK_API nk_bool nk_window_is_any_hovered(const struct nk_context *ctx);
  1837. /**
  1838. * # # nk_item_is_any_active
  1839. * \returns if the any window is being hovered or any widget is currently active.
  1840. * Can be used to decide if input should be processed by UI or your specific input handling.
  1841. * Example could be UI and 3D camera to move inside a 3D space.
  1842. * ```c
  1843. * nk_bool nk_item_is_any_active(struct nk_context*);
  1844. * ```
  1845. *
  1846. * Parameter | Description
  1847. * ------------|-----------------------------------------------------------
  1848. * \param[in] ctx | Must point to an previously initialized `nk_context` struct
  1849. *
  1850. * \returns `true(1)` if any window is hovered or any item is active or `false(0)` otherwise
  1851. */
  1852. NK_API nk_bool nk_item_is_any_active(const struct nk_context *ctx);
  1853. /**
  1854. * # # nk_window_set_bounds
  1855. * Updates position and size of window with passed in name
  1856. * ```c
  1857. * void nk_window_set_bounds(struct nk_context*, const char *name, struct nk_rect bounds);
  1858. * ```
  1859. *
  1860. * Parameter | Description
  1861. * ------------|-----------------------------------------------------------
  1862. * \param[in] ctx | Must point to an previously initialized `nk_context` struct
  1863. * \param[in] name | Identifier of the window to modify both position and size
  1864. * \param[in] bounds | Must point to a `nk_rect` struct with the new position and size
  1865. */
  1866. NK_API void nk_window_set_bounds(struct nk_context *ctx, const char *name, struct nk_rect bounds);
  1867. /**
  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. * \param[in] ctx | Must point to an previously initialized `nk_context` struct
  1877. * \param[in] name | Identifier of the window to modify both position
  1878. * \param[in] pos | Must point to a `nk_vec2` struct with the new position
  1879. */
  1880. NK_API void nk_window_set_position(struct nk_context *ctx, const char *name, struct nk_vec2 pos);
  1881. /**
  1882. * # # nk_window_set_size
  1883. * Updates size of window with passed in name
  1884. * ```c
  1885. * void nk_window_set_size(struct nk_context*, const char *name, struct nk_vec2);
  1886. * ```
  1887. *
  1888. * Parameter | Description
  1889. * ------------|-----------------------------------------------------------
  1890. * \param[in] ctx | Must point to an previously initialized `nk_context` struct
  1891. * \param[in] name | Identifier of the window to modify both window size
  1892. * \param[in] size | Must point to a `nk_vec2` struct with new window size
  1893. */
  1894. NK_API void nk_window_set_size(struct nk_context *ctx, const char *name, struct nk_vec2 size);
  1895. /**
  1896. * # # nk_window_set_focus
  1897. * Sets the window with given name as active
  1898. * ```c
  1899. * void nk_window_set_focus(struct nk_context*, const char *name);
  1900. * ```
  1901. *
  1902. * Parameter | Description
  1903. * ------------|-----------------------------------------------------------
  1904. * \param[in] ctx | Must point to an previously initialized `nk_context` struct
  1905. * \param[in] name | Identifier of the window to set focus on
  1906. */
  1907. NK_API void nk_window_set_focus(struct nk_context *ctx, const char *name);
  1908. /**
  1909. * # # nk_window_set_scroll
  1910. * Sets the scroll offset for the current window
  1911. * !!! \warning
  1912. * Only call this function between calls `nk_begin_xxx` and `nk_end`
  1913. *
  1914. * ```c
  1915. * void nk_window_set_scroll(struct nk_context *ctx, nk_uint offset_x, nk_uint offset_y);
  1916. * ```
  1917. *
  1918. * Parameter | Description
  1919. * -------------|-----------------------------------------------------------
  1920. * \param[in] ctx | Must point to an previously initialized `nk_context` struct
  1921. * \param[in] offset_x | The x offset to scroll to
  1922. * \param[in] offset_y | The y offset to scroll to
  1923. */
  1924. NK_API void nk_window_set_scroll(struct nk_context *ctx, nk_uint offset_x, nk_uint offset_y);
  1925. /**
  1926. * # # nk_window_close
  1927. * Closes a window and marks it for being freed at the end of the frame
  1928. * ```c
  1929. * void nk_window_close(struct nk_context *ctx, const char *name);
  1930. * ```
  1931. *
  1932. * Parameter | Description
  1933. * ------------|-----------------------------------------------------------
  1934. * \param[in] ctx | Must point to an previously initialized `nk_context` struct
  1935. * \param[in] name | Identifier of the window to close
  1936. */
  1937. NK_API void nk_window_close(struct nk_context *ctx, const char *name);
  1938. /**
  1939. * # # nk_window_collapse
  1940. * Updates collapse state of a window with given name
  1941. * ```c
  1942. * void nk_window_collapse(struct nk_context*, const char *name, enum nk_collapse_states state);
  1943. * ```
  1944. *
  1945. * Parameter | Description
  1946. * ------------|-----------------------------------------------------------
  1947. * \param[in] ctx | Must point to an previously initialized `nk_context` struct
  1948. * \param[in] name | Identifier of the window to close
  1949. * \param[in] state | value out of nk_collapse_states section
  1950. */
  1951. NK_API void nk_window_collapse(struct nk_context *ctx, const char *name, enum nk_collapse_states state);
  1952. /**
  1953. * # # nk_window_collapse_if
  1954. * Updates collapse state of a window with given name if given condition is met
  1955. * ```c
  1956. * void nk_window_collapse_if(struct nk_context*, const char *name, enum nk_collapse_states, int cond);
  1957. * ```
  1958. *
  1959. * Parameter | Description
  1960. * ------------|-----------------------------------------------------------
  1961. * \param[in] ctx | Must point to an previously initialized `nk_context` struct
  1962. * \param[in] name | Identifier of the window to either collapse or maximize
  1963. * \param[in] state | value out of nk_collapse_states section the window should be put into
  1964. * \param[in] cond | condition that has to be met to actually commit the collapse state change
  1965. */
  1966. NK_API void nk_window_collapse_if(struct nk_context *ctx, const char *name, enum nk_collapse_states state, int cond);
  1967. /**
  1968. * # # nk_window_show
  1969. * updates visibility state of a window with given name
  1970. * ```c
  1971. * void nk_window_show(struct nk_context*, const char *name, enum nk_show_states);
  1972. * ```
  1973. *
  1974. * Parameter | Description
  1975. * ------------|-----------------------------------------------------------
  1976. * \param[in] ctx | Must point to an previously initialized `nk_context` struct
  1977. * \param[in] name | Identifier of the window to either collapse or maximize
  1978. * \param[in] state | state with either visible or hidden to modify the window with
  1979. */
  1980. NK_API void nk_window_show(struct nk_context *ctx, const char *name, enum nk_show_states state);
  1981. /**
  1982. * # # nk_window_show_if
  1983. * Updates visibility state of a window with given name if a given condition is met
  1984. * ```c
  1985. * void nk_window_show_if(struct nk_context*, const char *name, enum nk_show_states, int cond);
  1986. * ```
  1987. *
  1988. * Parameter | Description
  1989. * ------------|-----------------------------------------------------------
  1990. * \param[in] ctx | Must point to an previously initialized `nk_context` struct
  1991. * \param[in] name | Identifier of the window to either hide or show
  1992. * \param[in] state | state with either visible or hidden to modify the window with
  1993. * \param[in] cond | condition that has to be met to actually commit the visibility state change
  1994. */
  1995. NK_API void nk_window_show_if(struct nk_context *ctx, const char *name, enum nk_show_states state, int cond);
  1996. /**
  1997. * # # nk_window_show_if
  1998. * Line for visual separation. Draws a line with thickness determined by the current row height.
  1999. * ```c
  2000. * void nk_rule_horizontal(struct nk_context *ctx, struct nk_color color, NK_BOOL rounding)
  2001. * ```
  2002. *
  2003. * Parameter | Description
  2004. * ----------------|-------------------------------------------------------
  2005. * \param[in] ctx | Must point to an previously initialized `nk_context` struct
  2006. * \param[in] color | Color of the horizontal line
  2007. * \param[in] rounding | Whether or not to make the line round
  2008. */
  2009. NK_API void nk_rule_horizontal(struct nk_context *ctx, struct nk_color color, nk_bool rounding);
  2010. /* =============================================================================
  2011. *
  2012. * LAYOUT
  2013. *
  2014. * =============================================================================*/
  2015. /**
  2016. * \page Layouting
  2017. * Layouting in general describes placing widget inside a window with position and size.
  2018. * While in this particular implementation there are five different APIs for layouting
  2019. * each with different trade offs between control and ease of use. <br /><br />
  2020. *
  2021. * All layouting methods in this library are based around the concept of a row.
  2022. * A row has a height the window content grows by and a number of columns and each
  2023. * layouting method specifies how each widget is placed inside the row.
  2024. * After a row has been allocated by calling a layouting functions and then
  2025. * filled with widgets will advance an internal pointer over the allocated row. <br /><br />
  2026. *
  2027. * To actually define a layout you just call the appropriate layouting function
  2028. * and each subsequent widget call will place the widget as specified. Important
  2029. * here is that if you define more widgets then columns defined inside the layout
  2030. * functions it will allocate the next row without you having to make another layouting <br /><br />
  2031. * call.
  2032. *
  2033. * Biggest limitation with using all these APIs outside the `nk_layout_space_xxx` API
  2034. * is that you have to define the row height for each. However the row height
  2035. * often depends on the height of the font. <br /><br />
  2036. *
  2037. * To fix that internally nuklear uses a minimum row height that is set to the
  2038. * height plus padding of currently active font and overwrites the row height
  2039. * value if zero. <br /><br />
  2040. *
  2041. * If you manually want to change the minimum row height then
  2042. * use nk_layout_set_min_row_height, and use nk_layout_reset_min_row_height to
  2043. * reset it back to be derived from font height. <br /><br />
  2044. *
  2045. * Also if you change the font in nuklear it will automatically change the minimum
  2046. * row height for you and. This means if you change the font but still want
  2047. * a minimum row height smaller than the font you have to repush your value. <br /><br />
  2048. *
  2049. * For actually more advanced UI I would even recommend using the `nk_layout_space_xxx`
  2050. * layouting method in combination with a cassowary constraint solver (there are
  2051. * some versions on github with permissive license model) to take over all control over widget
  2052. * layouting yourself. However for quick and dirty layouting using all the other layouting
  2053. * functions should be fine.
  2054. *
  2055. * # Usage
  2056. * 1. __nk_layout_row_dynamic__<br /><br />
  2057. * The easiest layouting function is `nk_layout_row_dynamic`. It provides each
  2058. * widgets with same horizontal space inside the row and dynamically grows
  2059. * if the owning window grows in width. So the number of columns dictates
  2060. * the size of each widget dynamically by formula:
  2061. *
  2062. * ```c
  2063. * widget_width = (window_width - padding - spacing) * (1/column_count)
  2064. * ```
  2065. *
  2066. * Just like all other layouting APIs if you define more widget than columns this
  2067. * library will allocate a new row and keep all layouting parameters previously
  2068. * defined.
  2069. *
  2070. * ```c
  2071. * if (nk_begin_xxx(...) {
  2072. * // first row with height: 30 composed of two widgets
  2073. * nk_layout_row_dynamic(&ctx, 30, 2);
  2074. * nk_widget(...);
  2075. * nk_widget(...);
  2076. * //
  2077. * // second row with same parameter as defined above
  2078. * nk_widget(...);
  2079. * nk_widget(...);
  2080. * //
  2081. * // third row uses 0 for height which will use auto layouting
  2082. * nk_layout_row_dynamic(&ctx, 0, 2);
  2083. * nk_widget(...);
  2084. * nk_widget(...);
  2085. * }
  2086. * nk_end(...);
  2087. * ```
  2088. *
  2089. * 2. __nk_layout_row_static__<br /><br />
  2090. * Another easy layouting function is `nk_layout_row_static`. It provides each
  2091. * widget with same horizontal pixel width inside the row and does not grow
  2092. * if the owning window scales smaller or bigger.
  2093. *
  2094. * ```c
  2095. * if (nk_begin_xxx(...) {
  2096. * // first row with height: 30 composed of two widgets with width: 80
  2097. * nk_layout_row_static(&ctx, 30, 80, 2);
  2098. * nk_widget(...);
  2099. * nk_widget(...);
  2100. * //
  2101. * // second row with same parameter as defined above
  2102. * nk_widget(...);
  2103. * nk_widget(...);
  2104. * //
  2105. * // third row uses 0 for height which will use auto layouting
  2106. * nk_layout_row_static(&ctx, 0, 80, 2);
  2107. * nk_widget(...);
  2108. * nk_widget(...);
  2109. * }
  2110. * nk_end(...);
  2111. * ```
  2112. *
  2113. * 3. __nk_layout_row_xxx__<br /><br />
  2114. * A little bit more advanced layouting API are functions `nk_layout_row_begin`,
  2115. * `nk_layout_row_push` and `nk_layout_row_end`. They allow to directly
  2116. * specify each column pixel or window ratio in a row. It supports either
  2117. * directly setting per column pixel width or widget window ratio but not
  2118. * both. Furthermore it is a immediate mode API so each value is directly
  2119. * pushed before calling a widget. Therefore the layout is not automatically
  2120. * repeating like the last two layouting functions.
  2121. *
  2122. * ```c
  2123. * if (nk_begin_xxx(...) {
  2124. * // first row with height: 25 composed of two widgets with width 60 and 40
  2125. * nk_layout_row_begin(ctx, NK_STATIC, 25, 2);
  2126. * nk_layout_row_push(ctx, 60);
  2127. * nk_widget(...);
  2128. * nk_layout_row_push(ctx, 40);
  2129. * nk_widget(...);
  2130. * nk_layout_row_end(ctx);
  2131. * //
  2132. * // second row with height: 25 composed of two widgets with window ratio 0.25 and 0.75
  2133. * nk_layout_row_begin(ctx, NK_DYNAMIC, 25, 2);
  2134. * nk_layout_row_push(ctx, 0.25f);
  2135. * nk_widget(...);
  2136. * nk_layout_row_push(ctx, 0.75f);
  2137. * nk_widget(...);
  2138. * nk_layout_row_end(ctx);
  2139. * //
  2140. * // third row with auto generated height: composed of two widgets with window ratio 0.25 and 0.75
  2141. * nk_layout_row_begin(ctx, NK_DYNAMIC, 0, 2);
  2142. * nk_layout_row_push(ctx, 0.25f);
  2143. * nk_widget(...);
  2144. * nk_layout_row_push(ctx, 0.75f);
  2145. * nk_widget(...);
  2146. * nk_layout_row_end(ctx);
  2147. * }
  2148. * nk_end(...);
  2149. * ```
  2150. *
  2151. * 4. __nk_layout_row__<br /><br />
  2152. * The array counterpart to API nk_layout_row_xxx is the single nk_layout_row
  2153. * functions. Instead of pushing either pixel or window ratio for every widget
  2154. * it allows to define it by array. The trade of for less control is that
  2155. * `nk_layout_row` is automatically repeating. Otherwise the behavior is the
  2156. * same.
  2157. *
  2158. * ```c
  2159. * if (nk_begin_xxx(...) {
  2160. * // two rows with height: 30 composed of two widgets with width 60 and 40
  2161. * const float ratio[] = {60,40};
  2162. * nk_layout_row(ctx, NK_STATIC, 30, 2, ratio);
  2163. * nk_widget(...);
  2164. * nk_widget(...);
  2165. * nk_widget(...);
  2166. * nk_widget(...);
  2167. * //
  2168. * // two rows with height: 30 composed of two widgets with window ratio 0.25 and 0.75
  2169. * const float ratio[] = {0.25, 0.75};
  2170. * nk_layout_row(ctx, NK_DYNAMIC, 30, 2, ratio);
  2171. * nk_widget(...);
  2172. * nk_widget(...);
  2173. * nk_widget(...);
  2174. * nk_widget(...);
  2175. * //
  2176. * // two rows with auto generated height composed of two widgets with window ratio 0.25 and 0.75
  2177. * const float ratio[] = {0.25, 0.75};
  2178. * nk_layout_row(ctx, NK_DYNAMIC, 30, 2, ratio);
  2179. * nk_widget(...);
  2180. * nk_widget(...);
  2181. * nk_widget(...);
  2182. * nk_widget(...);
  2183. * }
  2184. * nk_end(...);
  2185. * ```
  2186. *
  2187. * 5. __nk_layout_row_template_xxx__<br /><br />
  2188. * The most complex and second most flexible API is a simplified flexbox version without
  2189. * line wrapping and weights for dynamic widgets. It is an immediate mode API but
  2190. * unlike `nk_layout_row_xxx` it has auto repeat behavior and needs to be called
  2191. * before calling the templated widgets.
  2192. * The row template layout has three different per widget size specifier. The first
  2193. * one is the `nk_layout_row_template_push_static` with fixed widget pixel width.
  2194. * They do not grow if the row grows and will always stay the same.
  2195. * The second size specifier is `nk_layout_row_template_push_variable`
  2196. * which defines a minimum widget size but it also can grow if more space is available
  2197. * not taken by other widgets.
  2198. * Finally there are dynamic widgets with `nk_layout_row_template_push_dynamic`
  2199. * which are completely flexible and unlike variable widgets can even shrink
  2200. * to zero if not enough space is provided.
  2201. *
  2202. * ```c
  2203. * if (nk_begin_xxx(...) {
  2204. * // two rows with height: 30 composed of three widgets
  2205. * nk_layout_row_template_begin(ctx, 30);
  2206. * nk_layout_row_template_push_dynamic(ctx);
  2207. * nk_layout_row_template_push_variable(ctx, 80);
  2208. * nk_layout_row_template_push_static(ctx, 80);
  2209. * nk_layout_row_template_end(ctx);
  2210. * //
  2211. * // first row
  2212. * nk_widget(...); // dynamic widget can go to zero if not enough space
  2213. * nk_widget(...); // variable widget with min 80 pixel but can grow bigger if enough space
  2214. * nk_widget(...); // static widget with fixed 80 pixel width
  2215. * //
  2216. * // second row same layout
  2217. * nk_widget(...);
  2218. * nk_widget(...);
  2219. * nk_widget(...);
  2220. * }
  2221. * nk_end(...);
  2222. * ```
  2223. *
  2224. * 6. __nk_layout_space_xxx__<br /><br />
  2225. * Finally the most flexible API directly allows you to place widgets inside the
  2226. * window. The space layout API is an immediate mode API which does not support
  2227. * row auto repeat and directly sets position and size of a widget. Position
  2228. * and size hereby can be either specified as ratio of allocated space or
  2229. * allocated space local position and pixel size. Since this API is quite
  2230. * powerful there are a number of utility functions to get the available space
  2231. * and convert between local allocated space and screen space.
  2232. *
  2233. * ```c
  2234. * if (nk_begin_xxx(...) {
  2235. * // static row with height: 500 (you can set column count to INT_MAX if you don't want to be bothered)
  2236. * nk_layout_space_begin(ctx, NK_STATIC, 500, INT_MAX);
  2237. * nk_layout_space_push(ctx, nk_rect(0,0,150,200));
  2238. * nk_widget(...);
  2239. * nk_layout_space_push(ctx, nk_rect(200,200,100,200));
  2240. * nk_widget(...);
  2241. * nk_layout_space_end(ctx);
  2242. * //
  2243. * // dynamic row with height: 500 (you can set column count to INT_MAX if you don't want to be bothered)
  2244. * nk_layout_space_begin(ctx, NK_DYNAMIC, 500, INT_MAX);
  2245. * nk_layout_space_push(ctx, nk_rect(0.5,0.5,0.1,0.1));
  2246. * nk_widget(...);
  2247. * nk_layout_space_push(ctx, nk_rect(0.7,0.6,0.1,0.1));
  2248. * nk_widget(...);
  2249. * }
  2250. * nk_end(...);
  2251. * ```
  2252. *
  2253. * # Reference
  2254. * Function | Description
  2255. * ---------------------------------------------|------------------------------------
  2256. * \ref nk_layout_set_min_row_height | Set the currently used minimum row height to a specified value
  2257. * \ref nk_layout_reset_min_row_height | Resets the currently used minimum row height to font height
  2258. * \ref nk_layout_widget_bounds | Calculates current width a static layout row can fit inside a window
  2259. * \ref nk_layout_ratio_from_pixel | Utility functions to calculate window ratio from pixel size
  2260. * \ref nk_layout_row_dynamic | Current layout is divided into n same sized growing columns
  2261. * \ref nk_layout_row_static | Current layout is divided into n same fixed sized columns
  2262. * \ref nk_layout_row_begin | Starts a new row with given height and number of columns
  2263. * \ref nk_layout_row_push | Pushes another column with given size or window ratio
  2264. * \ref nk_layout_row_end | Finished previously started row
  2265. * \ref nk_layout_row | Specifies row columns in array as either window ratio or size
  2266. * \ref nk_layout_row_template_begin | Begins the row template declaration
  2267. * \ref nk_layout_row_template_push_dynamic | Adds a dynamic column that dynamically grows and can go to zero if not enough space
  2268. * \ref nk_layout_row_template_push_variable | Adds a variable column that dynamically grows but does not shrink below specified pixel width
  2269. * \ref nk_layout_row_template_push_static | Adds a static column that does not grow and will always have the same size
  2270. * \ref nk_layout_row_template_end | Marks the end of the row template
  2271. * \ref nk_layout_space_begin | Begins a new layouting space that allows to specify each widgets position and size
  2272. * \ref nk_layout_space_push | Pushes position and size of the next widget in own coordinate space either as pixel or ratio
  2273. * \ref nk_layout_space_end | Marks the end of the layouting space
  2274. * \ref nk_layout_space_bounds | Callable after nk_layout_space_begin and returns total space allocated
  2275. * \ref nk_layout_space_to_screen | Converts vector from nk_layout_space coordinate space into screen space
  2276. * \ref nk_layout_space_to_local | Converts vector from screen space into nk_layout_space coordinates
  2277. * \ref nk_layout_space_rect_to_screen | Converts rectangle from nk_layout_space coordinate space into screen space
  2278. * \ref nk_layout_space_rect_to_local | Converts rectangle from screen space into nk_layout_space coordinates
  2279. */
  2280. enum nk_widget_align {
  2281. NK_WIDGET_ALIGN_LEFT = 0x01,
  2282. NK_WIDGET_ALIGN_CENTERED = 0x02,
  2283. NK_WIDGET_ALIGN_RIGHT = 0x04,
  2284. NK_WIDGET_ALIGN_TOP = 0x08,
  2285. NK_WIDGET_ALIGN_MIDDLE = 0x10,
  2286. NK_WIDGET_ALIGN_BOTTOM = 0x20
  2287. };
  2288. enum nk_widget_alignment {
  2289. NK_WIDGET_LEFT = NK_WIDGET_ALIGN_MIDDLE|NK_WIDGET_ALIGN_LEFT,
  2290. NK_WIDGET_CENTERED = NK_WIDGET_ALIGN_MIDDLE|NK_WIDGET_ALIGN_CENTERED,
  2291. NK_WIDGET_RIGHT = NK_WIDGET_ALIGN_MIDDLE|NK_WIDGET_ALIGN_RIGHT
  2292. };
  2293. /**
  2294. * Sets the currently used minimum row height.
  2295. * !!! \warning
  2296. * The passed height needs to include both your preferred row height
  2297. * as well as padding. No internal padding is added.
  2298. *
  2299. * ```c
  2300. * void nk_layout_set_min_row_height(struct nk_context*, float height);
  2301. * ```
  2302. *
  2303. * \param[in] ctx | Must point to an previously initialized `nk_context` struct after call `nk_begin_xxx`
  2304. * \param[in] height | New minimum row height to be used for auto generating the row height
  2305. */
  2306. NK_API void nk_layout_set_min_row_height(struct nk_context*, float height);
  2307. /**
  2308. * Reset the currently used minimum row height back to `font_height + text_padding + padding`
  2309. * ```c
  2310. * void nk_layout_reset_min_row_height(struct nk_context*);
  2311. * ```
  2312. *
  2313. * \param[in] ctx | Must point to an previously initialized `nk_context` struct after call `nk_begin_xxx`
  2314. */
  2315. NK_API void nk_layout_reset_min_row_height(struct nk_context*);
  2316. /**
  2317. * \brief Returns the width of the next row allocate by one of the layouting functions
  2318. *
  2319. * \details
  2320. * ```c
  2321. * struct nk_rect nk_layout_widget_bounds(struct nk_context*);
  2322. * ```
  2323. *
  2324. * \param[in] ctx | Must point to an previously initialized `nk_context` struct after call `nk_begin_xxx`
  2325. *
  2326. * \return `nk_rect` with both position and size of the next row
  2327. */
  2328. NK_API struct nk_rect nk_layout_widget_bounds(const struct nk_context *ctx);
  2329. /**
  2330. * \brief Utility functions to calculate window ratio from pixel size
  2331. *
  2332. * \details
  2333. * ```c
  2334. * float nk_layout_ratio_from_pixel(struct nk_context*, float pixel_width);
  2335. * ```
  2336. *
  2337. * \param[in] ctx | Must point to an previously initialized `nk_context` struct after call `nk_begin_xxx`
  2338. * \param[in] pixel | Pixel_width to convert to window ratio
  2339. *
  2340. * \returns `nk_rect` with both position and size of the next row
  2341. */
  2342. NK_API float nk_layout_ratio_from_pixel(const struct nk_context *ctx, float pixel_width);
  2343. /**
  2344. * \brief Sets current row layout to share horizontal space
  2345. * between @cols number of widgets evenly. Once called all subsequent widget
  2346. * calls greater than @cols will allocate a new row with same layout.
  2347. *
  2348. * \details
  2349. * ```c
  2350. * void nk_layout_row_dynamic(struct nk_context *ctx, float height, int cols);
  2351. * ```
  2352. *
  2353. * \param[in] ctx | Must point to an previously initialized `nk_context` struct after call `nk_begin_xxx`
  2354. * \param[in] height | Holds height of each widget in row or zero for auto layouting
  2355. * \param[in] columns | Number of widget inside row
  2356. */
  2357. NK_API void nk_layout_row_dynamic(struct nk_context *ctx, float height, int cols);
  2358. /**
  2359. * \brief Sets current row layout to fill @cols number of widgets
  2360. * in row with same @item_width horizontal size. Once called all subsequent widget
  2361. * calls greater than @cols will allocate a new row with same layout.
  2362. *
  2363. * \details
  2364. * ```c
  2365. * void nk_layout_row_static(struct nk_context *ctx, float height, int item_width, int cols);
  2366. * ```
  2367. *
  2368. * \param[in] ctx | Must point to an previously initialized `nk_context` struct after call `nk_begin_xxx`
  2369. * \param[in] height | Holds height of each widget in row or zero for auto layouting
  2370. * \param[in] width | Holds pixel width of each widget in the row
  2371. * \param[in] columns | Number of widget inside row
  2372. */
  2373. NK_API void nk_layout_row_static(struct nk_context *ctx, float height, int item_width, int cols);
  2374. /**
  2375. * \brief Starts a new dynamic or fixed row with given height and columns.
  2376. *
  2377. * \details
  2378. * ```c
  2379. * void nk_layout_row_begin(struct nk_context *ctx, enum nk_layout_format fmt, float row_height, int cols);
  2380. * ```
  2381. *
  2382. * \param[in] ctx | Must point to an previously initialized `nk_context` struct after call `nk_begin_xxx`
  2383. * \param[in] fmt | either `NK_DYNAMIC` for window ratio or `NK_STATIC` for fixed size columns
  2384. * \param[in] height | holds height of each widget in row or zero for auto layouting
  2385. * \param[in] columns | Number of widget inside row
  2386. */
  2387. NK_API void nk_layout_row_begin(struct nk_context *ctx, enum nk_layout_format fmt, float row_height, int cols);
  2388. /**
  2389. * \breif Specifies either window ratio or width of a single column
  2390. *
  2391. * \details
  2392. * ```c
  2393. * void nk_layout_row_push(struct nk_context*, float value);
  2394. * ```
  2395. *
  2396. * \param[in] ctx | Must point to an previously initialized `nk_context` struct after call `nk_begin_xxx`
  2397. * \param[in] value | either a window ratio or fixed width depending on @fmt in previous `nk_layout_row_begin` call
  2398. */
  2399. NK_API void nk_layout_row_push(struct nk_context*, float value);
  2400. /**
  2401. * \brief Finished previously started row
  2402. *
  2403. * \details
  2404. * ```c
  2405. * void nk_layout_row_end(struct nk_context*);
  2406. * ```
  2407. *
  2408. * \param[in] ctx | Must point to an previously initialized `nk_context` struct after call `nk_begin_xxx`
  2409. */
  2410. NK_API void nk_layout_row_end(struct nk_context*);
  2411. /**
  2412. * \brief Specifies row columns in array as either window ratio or size
  2413. *
  2414. * \details
  2415. * ```c
  2416. * void nk_layout_row(struct nk_context*, enum nk_layout_format, float height, int cols, const float *ratio);
  2417. * ```
  2418. *
  2419. * \param[in] ctx | Must point to an previously initialized `nk_context` struct after call `nk_begin_xxx`
  2420. * \param[in] fmt | Either `NK_DYNAMIC` for window ratio or `NK_STATIC` for fixed size columns
  2421. * \param[in] height | Holds height of each widget in row or zero for auto layouting
  2422. * \param[in] columns | Number of widget inside row
  2423. */
  2424. NK_API void nk_layout_row(struct nk_context*, enum nk_layout_format, float height, int cols, const float *ratio);
  2425. /**
  2426. * # # nk_layout_row_template_begin
  2427. * Begins the row template declaration
  2428. * ```c
  2429. * void nk_layout_row_template_begin(struct nk_context*, float row_height);
  2430. * ```
  2431. *
  2432. * Parameter | Description
  2433. * ------------|-----------------------------------------------------------
  2434. * \param[in] ctx | Must point to an previously initialized `nk_context` struct after call `nk_begin_xxx`
  2435. * \param[in] height | Holds height of each widget in row or zero for auto layouting
  2436. */
  2437. NK_API void nk_layout_row_template_begin(struct nk_context*, float row_height);
  2438. /**
  2439. * # # nk_layout_row_template_push_dynamic
  2440. * Adds a dynamic column that dynamically grows and can go to zero if not enough space
  2441. * ```c
  2442. * void nk_layout_row_template_push_dynamic(struct nk_context*);
  2443. * ```
  2444. *
  2445. * Parameter | Description
  2446. * ------------|-----------------------------------------------------------
  2447. * \param[in] ctx | Must point to an previously initialized `nk_context` struct after call `nk_begin_xxx`
  2448. * \param[in] height | Holds height of each widget in row or zero for auto layouting
  2449. */
  2450. NK_API void nk_layout_row_template_push_dynamic(struct nk_context*);
  2451. /**
  2452. * # # nk_layout_row_template_push_variable
  2453. * Adds a variable column that dynamically grows but does not shrink below specified pixel width
  2454. * ```c
  2455. * void nk_layout_row_template_push_variable(struct nk_context*, float min_width);
  2456. * ```
  2457. *
  2458. * Parameter | Description
  2459. * ------------|-----------------------------------------------------------
  2460. * \param[in] ctx | Must point to an previously initialized `nk_context` struct after call `nk_begin_xxx`
  2461. * \param[in] width | Holds the minimum pixel width the next column must always be
  2462. */
  2463. NK_API void nk_layout_row_template_push_variable(struct nk_context*, float min_width);
  2464. /**
  2465. * # # nk_layout_row_template_push_static
  2466. * Adds a static column that does not grow and will always have the same size
  2467. * ```c
  2468. * void nk_layout_row_template_push_static(struct nk_context*, float width);
  2469. * ```
  2470. *
  2471. * Parameter | Description
  2472. * ------------|-----------------------------------------------------------
  2473. * \param[in] ctx | Must point to an previously initialized `nk_context` struct after call `nk_begin_xxx`
  2474. * \param[in] width | Holds the absolute pixel width value the next column must be
  2475. */
  2476. NK_API void nk_layout_row_template_push_static(struct nk_context*, float width);
  2477. /**
  2478. * # # nk_layout_row_template_end
  2479. * Marks the end of the row template
  2480. * ```c
  2481. * void nk_layout_row_template_end(struct nk_context*);
  2482. * ```
  2483. *
  2484. * Parameter | Description
  2485. * ------------|-----------------------------------------------------------
  2486. * \param[in] ctx | Must point to an previously initialized `nk_context` struct after call `nk_begin_xxx`
  2487. */
  2488. NK_API void nk_layout_row_template_end(struct nk_context*);
  2489. /**
  2490. * # # nk_layout_space_begin
  2491. * Begins a new layouting space that allows to specify each widgets position and size.
  2492. * ```c
  2493. * void nk_layout_space_begin(struct nk_context*, enum nk_layout_format, float height, int widget_count);
  2494. * ```
  2495. *
  2496. * Parameter | Description
  2497. * ------------|-----------------------------------------------------------
  2498. * \param[in] ctx | Must point to an previously initialized `nk_context` struct after call `nk_begin_xxx`
  2499. * \param[in] fmt | Either `NK_DYNAMIC` for window ratio or `NK_STATIC` for fixed size columns
  2500. * \param[in] height | Holds height of each widget in row or zero for auto layouting
  2501. * \param[in] columns | Number of widgets inside row
  2502. */
  2503. NK_API void nk_layout_space_begin(struct nk_context*, enum nk_layout_format, float height, int widget_count);
  2504. /**
  2505. * # # nk_layout_space_push
  2506. * Pushes position and size of the next widget in own coordinate space either as pixel or ratio
  2507. * ```c
  2508. * void nk_layout_space_push(struct nk_context *ctx, struct nk_rect bounds);
  2509. * ```
  2510. *
  2511. * Parameter | Description
  2512. * ------------|-----------------------------------------------------------
  2513. * \param[in] ctx | Must point to an previously initialized `nk_context` struct after call `nk_layout_space_begin`
  2514. * \param[in] bounds | Position and size in laoyut space local coordinates
  2515. */
  2516. NK_API void nk_layout_space_push(struct nk_context*, struct nk_rect bounds);
  2517. /**
  2518. * # # nk_layout_space_end
  2519. * Marks the end of the layout space
  2520. * ```c
  2521. * void nk_layout_space_end(struct nk_context*);
  2522. * ```
  2523. *
  2524. * Parameter | Description
  2525. * ------------|-----------------------------------------------------------
  2526. * \param[in] ctx | Must point to an previously initialized `nk_context` struct after call `nk_layout_space_begin`
  2527. */
  2528. NK_API void nk_layout_space_end(struct nk_context*);
  2529. /**
  2530. * # # nk_layout_space_bounds
  2531. * Utility function to calculate total space allocated for `nk_layout_space`
  2532. * ```c
  2533. * struct nk_rect nk_layout_space_bounds(struct nk_context*);
  2534. * ```
  2535. *
  2536. * Parameter | Description
  2537. * ------------|-----------------------------------------------------------
  2538. * \param[in] ctx | Must point to an previously initialized `nk_context` struct after call `nk_layout_space_begin`
  2539. *
  2540. * \returns `nk_rect` holding the total space allocated
  2541. */
  2542. NK_API struct nk_rect nk_layout_space_bounds(const struct nk_context *ctx);
  2543. /**
  2544. * # # nk_layout_space_to_screen
  2545. * Converts vector from nk_layout_space coordinate space into screen space
  2546. * ```c
  2547. * struct nk_vec2 nk_layout_space_to_screen(struct nk_context*, struct nk_vec2);
  2548. * ```
  2549. *
  2550. * Parameter | Description
  2551. * ------------|-----------------------------------------------------------
  2552. * \param[in] ctx | Must point to an previously initialized `nk_context` struct after call `nk_layout_space_begin`
  2553. * \param[in] vec | Position to convert from layout space into screen coordinate space
  2554. *
  2555. * \returns transformed `nk_vec2` in screen space coordinates
  2556. */
  2557. NK_API struct nk_vec2 nk_layout_space_to_screen(const struct nk_context* ctx, struct nk_vec2 vec);
  2558. /**
  2559. * # # nk_layout_space_to_local
  2560. * Converts vector from layout space into screen space
  2561. * ```c
  2562. * struct nk_vec2 nk_layout_space_to_local(struct nk_context*, struct nk_vec2);
  2563. * ```
  2564. *
  2565. * Parameter | Description
  2566. * ------------|-----------------------------------------------------------
  2567. * \param[in] ctx | Must point to an previously initialized `nk_context` struct after call `nk_layout_space_begin`
  2568. * \param[in] vec | Position to convert from screen space into layout coordinate space
  2569. *
  2570. * \returns transformed `nk_vec2` in layout space coordinates
  2571. */
  2572. NK_API struct nk_vec2 nk_layout_space_to_local(const struct nk_context *ctx, struct nk_vec2 vec);
  2573. /**
  2574. * # # nk_layout_space_rect_to_screen
  2575. * Converts rectangle from screen space into layout space
  2576. * ```c
  2577. * struct nk_rect nk_layout_space_rect_to_screen(struct nk_context*, struct nk_rect);
  2578. * ```
  2579. *
  2580. * Parameter | Description
  2581. * ------------|-----------------------------------------------------------
  2582. * \param[in] ctx | Must point to an previously initialized `nk_context` struct after call `nk_layout_space_begin`
  2583. * \param[in] bounds | Rectangle to convert from layout space into screen space
  2584. *
  2585. * \returns transformed `nk_rect` in screen space coordinates
  2586. */
  2587. NK_API struct nk_rect nk_layout_space_rect_to_screen(const struct nk_context *ctx, struct nk_rect bounds);
  2588. /**
  2589. * # # nk_layout_space_rect_to_local
  2590. * Converts rectangle from layout space into screen space
  2591. * ```c
  2592. * struct nk_rect nk_layout_space_rect_to_local(struct nk_context*, struct nk_rect);
  2593. * ```
  2594. *
  2595. * Parameter | Description
  2596. * ------------|-----------------------------------------------------------
  2597. * \param[in] ctx | Must point to an previously initialized `nk_context` struct after call `nk_layout_space_begin`
  2598. * \param[in] bounds | Rectangle to convert from layout space into screen space
  2599. *
  2600. * \returns transformed `nk_rect` in layout space coordinates
  2601. */
  2602. NK_API struct nk_rect nk_layout_space_rect_to_local(const struct nk_context *ctx, struct nk_rect bounds);
  2603. /**
  2604. * # # nk_spacer
  2605. * Spacer is a dummy widget that consumes space as usual but doesn't draw anything
  2606. * ```c
  2607. * void nk_spacer(struct nk_context* );
  2608. * ```
  2609. *
  2610. * Parameter | Description
  2611. * ------------|-----------------------------------------------------------
  2612. * \param[in] ctx | Must point to an previously initialized `nk_context` struct after call `nk_layout_space_begin`
  2613. *
  2614. */
  2615. NK_API void nk_spacer(struct nk_context *ctx);
  2616. /** =============================================================================
  2617. *
  2618. * GROUP
  2619. *
  2620. * =============================================================================*/
  2621. /**
  2622. * \page Groups
  2623. * Groups are basically windows inside windows. They allow to subdivide space
  2624. * in a window to layout widgets as a group. Almost all more complex widget
  2625. * layouting requirements can be solved using groups and basic layouting
  2626. * fuctionality. Groups just like windows are identified by an unique name and
  2627. * internally keep track of scrollbar offsets by default. However additional
  2628. * versions are provided to directly manage the scrollbar.
  2629. *
  2630. * # Usage
  2631. * To create a group you have to call one of the three `nk_group_begin_xxx`
  2632. * functions to start group declarations and `nk_group_end` at the end. Furthermore it
  2633. * is required to check the return value of `nk_group_begin_xxx` and only process
  2634. * widgets inside the window if the value is not 0.
  2635. * Nesting groups is possible and even encouraged since many layouting schemes
  2636. * can only be achieved by nesting. Groups, unlike windows, need `nk_group_end`
  2637. * to be only called if the corresponding `nk_group_begin_xxx` call does not return 0:
  2638. *
  2639. * ```c
  2640. * if (nk_group_begin_xxx(ctx, ...) {
  2641. * // [... widgets ...]
  2642. * nk_group_end(ctx);
  2643. * }
  2644. * ```
  2645. *
  2646. * In the grand concept groups can be called after starting a window
  2647. * with `nk_begin_xxx` and before calling `nk_end`:
  2648. *
  2649. * ```c
  2650. * struct nk_context ctx;
  2651. * nk_init_xxx(&ctx, ...);
  2652. * while (1) {
  2653. * // Input
  2654. * Event evt;
  2655. * nk_input_begin(&ctx);
  2656. * while (GetEvent(&evt)) {
  2657. * if (evt.type == MOUSE_MOVE)
  2658. * nk_input_motion(&ctx, evt.motion.x, evt.motion.y);
  2659. * else if (evt.type == [...]) {
  2660. * nk_input_xxx(...);
  2661. * }
  2662. * }
  2663. * nk_input_end(&ctx);
  2664. * //
  2665. * // Window
  2666. * if (nk_begin_xxx(...) {
  2667. * // [...widgets...]
  2668. * nk_layout_row_dynamic(...);
  2669. * if (nk_group_begin_xxx(ctx, ...) {
  2670. * //[... widgets ...]
  2671. * nk_group_end(ctx);
  2672. * }
  2673. * }
  2674. * nk_end(ctx);
  2675. * //
  2676. * // Draw
  2677. * const struct nk_command *cmd = 0;
  2678. * nk_foreach(cmd, &ctx) {
  2679. * switch (cmd->type) {
  2680. * case NK_COMMAND_LINE:
  2681. * your_draw_line_function(...)
  2682. * break;
  2683. * case NK_COMMAND_RECT
  2684. * your_draw_rect_function(...)
  2685. * break;
  2686. * case ...:
  2687. * // [...]
  2688. * }
  2689. * nk_clear(&ctx);
  2690. * }
  2691. * nk_free(&ctx);
  2692. * ```
  2693. * # Reference
  2694. * Function | Description
  2695. * --------------------------------|-------------------------------------------
  2696. * \ref nk_group_begin | Start a new group with internal scrollbar handling
  2697. * \ref nk_group_begin_titled | Start a new group with separated name and title and internal scrollbar handling
  2698. * \ref nk_group_end | Ends a group. Should only be called if nk_group_begin returned non-zero
  2699. * \ref nk_group_scrolled_offset_begin | Start a new group with manual separated handling of scrollbar x- and y-offset
  2700. * \ref nk_group_scrolled_begin | Start a new group with manual scrollbar handling
  2701. * \ref nk_group_scrolled_end | Ends a group with manual scrollbar handling. Should only be called if nk_group_begin returned non-zero
  2702. * \ref nk_group_get_scroll | Gets the scroll offset for the given group
  2703. * \ref nk_group_set_scroll | Sets the scroll offset for the given group
  2704. */
  2705. /**
  2706. * \brief Starts a new widget group. Requires a previous layouting function to specify a pos/size.
  2707. * ```c
  2708. * nk_bool nk_group_begin(struct nk_context*, const char *title, nk_flags);
  2709. * ```
  2710. *
  2711. * Parameter | Description
  2712. * ------------|-----------------------------------------------------------
  2713. * \param[in] ctx | Must point to an previously initialized `nk_context` struct
  2714. * \param[in] title | Must be an unique identifier for this group that is also used for the group header
  2715. * \param[in] flags | Window flags defined in the nk_panel_flags section with a number of different group behaviors
  2716. *
  2717. * \returns `true(1)` if visible and fillable with widgets or `false(0)` otherwise
  2718. */
  2719. NK_API nk_bool nk_group_begin(struct nk_context*, const char *title, nk_flags);
  2720. /**
  2721. * \brief Starts a new widget group. Requires a previous layouting function to specify a pos/size.
  2722. * ```c
  2723. * nk_bool nk_group_begin_titled(struct nk_context*, const char *name, const char *title, nk_flags);
  2724. * ```
  2725. *
  2726. * \param[in] ctx | Must point to an previously initialized `nk_context` struct
  2727. * \param[in] id | Must be an unique identifier for this group
  2728. * \param[in] title | Group header title
  2729. * \param[in] flags | Window flags defined in the nk_panel_flags section with a number of different group behaviors
  2730. *
  2731. * \returns `true(1)` if visible and fillable with widgets or `false(0)` otherwise
  2732. */
  2733. NK_API nk_bool nk_group_begin_titled(struct nk_context*, const char *name, const char *title, nk_flags);
  2734. /**
  2735. * # # nk_group_end
  2736. * Ends a widget group
  2737. * ```c
  2738. * void nk_group_end(struct nk_context*);
  2739. * ```
  2740. *
  2741. * Parameter | Description
  2742. * ------------|-----------------------------------------------------------
  2743. * \param[in] ctx | Must point to an previously initialized `nk_context` struct
  2744. */
  2745. NK_API void nk_group_end(struct nk_context*);
  2746. /**
  2747. * # # nk_group_scrolled_offset_begin
  2748. * starts a new widget group. requires a previous layouting function to specify
  2749. * a size. Does not keep track of scrollbar.
  2750. * ```c
  2751. * nk_bool nk_group_scrolled_offset_begin(struct nk_context*, nk_uint *x_offset, nk_uint *y_offset, const char *title, nk_flags flags);
  2752. * ```
  2753. *
  2754. * Parameter | Description
  2755. * ------------|-----------------------------------------------------------
  2756. * \param[in] ctx | Must point to an previously initialized `nk_context` struct
  2757. * \param[in] x_offset| Scrollbar x-offset to offset all widgets inside the group horizontally.
  2758. * \param[in] y_offset| Scrollbar y-offset to offset all widgets inside the group vertically
  2759. * \param[in] title | Window unique group title used to both identify and display in the group header
  2760. * \param[in] flags | Window flags from the nk_panel_flags section
  2761. *
  2762. * \returns `true(1)` if visible and fillable with widgets or `false(0)` otherwise
  2763. */
  2764. 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);
  2765. /**
  2766. * # # nk_group_scrolled_begin
  2767. * Starts a new widget group. requires a previous
  2768. * layouting function to specify a size. Does not keep track of scrollbar.
  2769. * ```c
  2770. * nk_bool nk_group_scrolled_begin(struct nk_context*, struct nk_scroll *off, const char *title, nk_flags);
  2771. * ```
  2772. *
  2773. * Parameter | Description
  2774. * ------------|-----------------------------------------------------------
  2775. * \param[in] ctx | Must point to an previously initialized `nk_context` struct
  2776. * \param[in] off | Both x- and y- scroll offset. Allows for manual scrollbar control
  2777. * \param[in] title | Window unique group title used to both identify and display in the group header
  2778. * \param[in] flags | Window flags from nk_panel_flags section
  2779. *
  2780. * \returns `true(1)` if visible and fillable with widgets or `false(0)` otherwise
  2781. */
  2782. NK_API nk_bool nk_group_scrolled_begin(struct nk_context*, struct nk_scroll *off, const char *title, nk_flags);
  2783. /**
  2784. * # # nk_group_scrolled_end
  2785. * Ends a widget group after calling nk_group_scrolled_offset_begin or nk_group_scrolled_begin.
  2786. * ```c
  2787. * void nk_group_scrolled_end(struct nk_context*);
  2788. * ```
  2789. *
  2790. * Parameter | Description
  2791. * ------------|-----------------------------------------------------------
  2792. * \param[in] ctx | Must point to an previously initialized `nk_context` struct
  2793. */
  2794. NK_API void nk_group_scrolled_end(struct nk_context*);
  2795. /**
  2796. * # # nk_group_get_scroll
  2797. * Gets the scroll position of the given group.
  2798. * ```c
  2799. * void nk_group_get_scroll(struct nk_context*, const char *id, nk_uint *x_offset, nk_uint *y_offset);
  2800. * ```
  2801. *
  2802. * Parameter | Description
  2803. * -------------|-----------------------------------------------------------
  2804. * \param[in] ctx | Must point to an previously initialized `nk_context` struct
  2805. * \param[in] id | The id of the group to get the scroll position of
  2806. * \param[in] x_offset | A pointer to the x offset output (or NULL to ignore)
  2807. * \param[in] y_offset | A pointer to the y offset output (or NULL to ignore)
  2808. */
  2809. NK_API void nk_group_get_scroll(struct nk_context*, const char *id, nk_uint *x_offset, nk_uint *y_offset);
  2810. /**
  2811. * # # nk_group_set_scroll
  2812. * Sets the scroll position of the given group.
  2813. * ```c
  2814. * void nk_group_set_scroll(struct nk_context*, const char *id, nk_uint x_offset, nk_uint y_offset);
  2815. * ```
  2816. *
  2817. * Parameter | Description
  2818. * -------------|-----------------------------------------------------------
  2819. * \param[in] ctx | Must point to an previously initialized `nk_context` struct
  2820. * \param[in] id | The id of the group to scroll
  2821. * \param[in] x_offset | The x offset to scroll to
  2822. * \param[in] y_offset | The y offset to scroll to
  2823. */
  2824. NK_API void nk_group_set_scroll(struct nk_context*, const char *id, nk_uint x_offset, nk_uint y_offset);
  2825. /** =============================================================================
  2826. *
  2827. * TREE
  2828. *
  2829. * =============================================================================*/
  2830. /**
  2831. * \page Tree
  2832. * Trees represent two different concept. First the concept of a collapsible
  2833. * UI section that can be either in a hidden or visible state. They allow the UI
  2834. * user to selectively minimize the current set of visible UI to comprehend.
  2835. * The second concept are tree widgets for visual UI representation of trees.<br /><br />
  2836. *
  2837. * Trees thereby can be nested for tree representations and multiple nested
  2838. * collapsible UI sections. All trees are started by calling of the
  2839. * `nk_tree_xxx_push_tree` functions and ended by calling one of the
  2840. * `nk_tree_xxx_pop_xxx()` functions. Each starting functions takes a title label
  2841. * and optionally an image to be displayed and the initial collapse state from
  2842. * the nk_collapse_states section.<br /><br />
  2843. *
  2844. * The runtime state of the tree is either stored outside the library by the caller
  2845. * or inside which requires a unique ID. The unique ID can either be generated
  2846. * automatically from `__FILE__` and `__LINE__` with function `nk_tree_push`,
  2847. * by `__FILE__` and a user provided ID generated for example by loop index with
  2848. * function `nk_tree_push_id` or completely provided from outside by user with
  2849. * function `nk_tree_push_hashed`.
  2850. *
  2851. * # Usage
  2852. * To create a tree you have to call one of the seven `nk_tree_xxx_push_xxx`
  2853. * functions to start a collapsible UI section and `nk_tree_xxx_pop` to mark the
  2854. * end.
  2855. * Each starting function will either return `false(0)` if the tree is collapsed
  2856. * or hidden and therefore does not need to be filled with content or `true(1)`
  2857. * if visible and required to be filled.
  2858. *
  2859. * !!! Note
  2860. * The tree header does not require and layouting function and instead
  2861. * calculates a auto height based on the currently used font size
  2862. *
  2863. * The tree ending functions only need to be called if the tree content is
  2864. * actually visible. So make sure the tree push function is guarded by `if`
  2865. * and the pop call is only taken if the tree is visible.
  2866. *
  2867. * ```c
  2868. * if (nk_tree_push(ctx, NK_TREE_TAB, "Tree", NK_MINIMIZED)) {
  2869. * nk_layout_row_dynamic(...);
  2870. * nk_widget(...);
  2871. * nk_tree_pop(ctx);
  2872. * }
  2873. * ```
  2874. *
  2875. * # Reference
  2876. * Function | Description
  2877. * ----------------------------|-------------------------------------------
  2878. * nk_tree_push | Start a collapsible UI section with internal state management
  2879. * nk_tree_push_id | Start a collapsible UI section with internal state management callable in a look
  2880. * nk_tree_push_hashed | Start a collapsible UI section with internal state management with full control over internal unique ID use to store state
  2881. * nk_tree_image_push | Start a collapsible UI section with image and label header
  2882. * nk_tree_image_push_id | Start a collapsible UI section with image and label header and internal state management callable in a look
  2883. * 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
  2884. * nk_tree_pop | Ends a collapsible UI section
  2885. * nk_tree_state_push | Start a collapsible UI section with external state management
  2886. * nk_tree_state_image_push | Start a collapsible UI section with image and label header and external state management
  2887. * nk_tree_state_pop | Ends a collapsabale UI section
  2888. *
  2889. * # nk_tree_type
  2890. * Flag | Description
  2891. * ----------------|----------------------------------------
  2892. * NK_TREE_NODE | Highlighted tree header to mark a collapsible UI section
  2893. * NK_TREE_TAB | Non-highlighted tree header closer to tree representations
  2894. */
  2895. /**
  2896. * # # nk_tree_push
  2897. * Starts a collapsible UI section with internal state management
  2898. * !!! \warning
  2899. * To keep track of the runtime tree collapsible state this function uses
  2900. * defines `__FILE__` and `__LINE__` to generate a unique ID. If you want
  2901. * to call this function in a loop please use `nk_tree_push_id` or
  2902. * `nk_tree_push_hashed` instead.
  2903. *
  2904. * ```c
  2905. * #define nk_tree_push(ctx, type, title, state)
  2906. * ```
  2907. *
  2908. * Parameter | Description
  2909. * ------------|-----------------------------------------------------------
  2910. * \param[in] ctx | Must point to an previously initialized `nk_context` struct
  2911. * \param[in] type | Value from the nk_tree_type section to visually mark a tree node header as either a collapseable UI section or tree node
  2912. * \param[in] title | Label printed in the tree header
  2913. * \param[in] state | Initial tree state value out of nk_collapse_states
  2914. *
  2915. * \returns `true(1)` if visible and fillable with widgets or `false(0)` otherwise
  2916. */
  2917. #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__)
  2918. /**
  2919. * # # nk_tree_push_id
  2920. * Starts a collapsible UI section with internal state management callable in a look
  2921. * ```c
  2922. * #define nk_tree_push_id(ctx, type, title, state, id)
  2923. * ```
  2924. *
  2925. * Parameter | Description
  2926. * ------------|-----------------------------------------------------------
  2927. * \param[in] ctx | Must point to an previously initialized `nk_context` struct
  2928. * \param[in] type | Value from the nk_tree_type section to visually mark a tree node header as either a collapseable UI section or tree node
  2929. * \param[in] title | Label printed in the tree header
  2930. * \param[in] state | Initial tree state value out of nk_collapse_states
  2931. * \param[in] id | Loop counter index if this function is called in a loop
  2932. *
  2933. * \returns `true(1)` if visible and fillable with widgets or `false(0)` otherwise
  2934. */
  2935. #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)
  2936. /**
  2937. * # # nk_tree_push_hashed
  2938. * Start a collapsible UI section with internal state management with full
  2939. * control over internal unique ID used to store state
  2940. * ```c
  2941. * 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);
  2942. * ```
  2943. *
  2944. * Parameter | Description
  2945. * ------------|-----------------------------------------------------------
  2946. * \param[in] ctx | Must point to an previously initialized `nk_context` struct
  2947. * \param[in] type | Value from the nk_tree_type section to visually mark a tree node header as either a collapseable UI section or tree node
  2948. * \param[in] title | Label printed in the tree header
  2949. * \param[in] state | Initial tree state value out of nk_collapse_states
  2950. * \param[in] hash | Memory block or string to generate the ID from
  2951. * \param[in] len | Size of passed memory block or string in __hash__
  2952. * \param[in] seed | Seeding value if this function is called in a loop or default to `0`
  2953. *
  2954. * \returns `true(1)` if visible and fillable with widgets or `false(0)` otherwise
  2955. */
  2956. 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);
  2957. /**
  2958. * # # nk_tree_image_push
  2959. * Start a collapsible UI section with image and label header
  2960. * !!! \warning
  2961. * To keep track of the runtime tree collapsible state this function uses
  2962. * defines `__FILE__` and `__LINE__` to generate a unique ID. If you want
  2963. * to call this function in a loop please use `nk_tree_image_push_id` or
  2964. * `nk_tree_image_push_hashed` instead.
  2965. *
  2966. * ```c
  2967. * #define nk_tree_image_push(ctx, type, img, title, state)
  2968. * ```
  2969. * Parameter | Description
  2970. * ------------|-----------------------------------------------------------
  2971. * \param[in] ctx | Must point to an previously initialized `nk_context` struct
  2972. * \param[in] type | Value from the nk_tree_type section to visually mark a tree node header as either a collapseable UI section or tree node
  2973. * \param[in] img | Image to display inside the header on the left of the label
  2974. * \param[in] title | Label printed in the tree header
  2975. * \param[in] state | Initial tree state value out of nk_collapse_states
  2976. *
  2977. * \returns `true(1)` if visible and fillable with widgets or `false(0)` otherwise
  2978. */
  2979. #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__)
  2980. /**
  2981. * # # nk_tree_image_push_id
  2982. * Start a collapsible UI section with image and label header and internal state
  2983. * management callable in a look
  2984. *
  2985. * ```c
  2986. * #define nk_tree_image_push_id(ctx, type, img, title, state, id)
  2987. * ```
  2988. *
  2989. * Parameter | Description
  2990. * ------------|-----------------------------------------------------------
  2991. * \param[in] ctx | Must point to an previously initialized `nk_context` struct
  2992. * \param[in] type | Value from the nk_tree_type section to visually mark a tree node header as either a collapseable UI section or tree node
  2993. * \param[in] img | Image to display inside the header on the left of the label
  2994. * \param[in] title | Label printed in the tree header
  2995. * \param[in] state | Initial tree state value out of nk_collapse_states
  2996. * \param[in] id | Loop counter index if this function is called in a loop
  2997. *
  2998. * \returns `true(1)` if visible and fillable with widgets or `false(0)` otherwise
  2999. */
  3000. #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)
  3001. /**
  3002. * # # nk_tree_image_push_hashed
  3003. * Start a collapsible UI section with internal state management with full
  3004. * control over internal unique ID used to store state
  3005. * ```c
  3006. * 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);
  3007. * ```
  3008. *
  3009. * Parameter | Description
  3010. * ------------|-----------------------------------------------------------
  3011. * \param[in] ctx | Must point to an previously initialized `nk_context` struct
  3012. * \param[in] type | Value from the nk_tree_type section to visually mark a tree node header as either a collapseable UI section or tree node
  3013. * \param[in] img | Image to display inside the header on the left of the label
  3014. * \param[in] title | Label printed in the tree header
  3015. * \param[in] state | Initial tree state value out of nk_collapse_states
  3016. * \param[in] hash | Memory block or string to generate the ID from
  3017. * \param[in] len | Size of passed memory block or string in __hash__
  3018. * \param[in] seed | Seeding value if this function is called in a loop or default to `0`
  3019. *
  3020. * \returns `true(1)` if visible and fillable with widgets or `false(0)` otherwise
  3021. */
  3022. 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);
  3023. /**
  3024. * # # nk_tree_pop
  3025. * Ends a collapsabale UI section
  3026. * ```c
  3027. * void nk_tree_pop(struct nk_context*);
  3028. * ```
  3029. *
  3030. * Parameter | Description
  3031. * ------------|-----------------------------------------------------------
  3032. * \param[in] ctx | Must point to an previously initialized `nk_context` struct after calling `nk_tree_xxx_push_xxx`
  3033. */
  3034. NK_API void nk_tree_pop(struct nk_context*);
  3035. /**
  3036. * # # nk_tree_state_push
  3037. * Start a collapsible UI section with external state management
  3038. * ```c
  3039. * nk_bool nk_tree_state_push(struct nk_context*, enum nk_tree_type, const char *title, enum nk_collapse_states *state);
  3040. * ```
  3041. *
  3042. * Parameter | Description
  3043. * ------------|-----------------------------------------------------------
  3044. * \param[in] ctx | Must point to an previously initialized `nk_context` struct after calling `nk_tree_xxx_push_xxx`
  3045. * \param[in] type | Value from the nk_tree_type section to visually mark a tree node header as either a collapseable UI section or tree node
  3046. * \param[in] title | Label printed in the tree header
  3047. * \param[in] state | Persistent state to update
  3048. *
  3049. * \returns `true(1)` if visible and fillable with widgets or `false(0)` otherwise
  3050. */
  3051. NK_API nk_bool nk_tree_state_push(struct nk_context*, enum nk_tree_type, const char *title, enum nk_collapse_states *state);
  3052. /**
  3053. * # # nk_tree_state_image_push
  3054. * Start a collapsible UI section with image and label header and external state management
  3055. * ```c
  3056. * 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);
  3057. * ```
  3058. *
  3059. * Parameter | Description
  3060. * ------------|-----------------------------------------------------------
  3061. * \param[in] ctx | Must point to an previously initialized `nk_context` struct after calling `nk_tree_xxx_push_xxx`
  3062. * \param[in] img | Image to display inside the header on the left of the label
  3063. * \param[in] type | Value from the nk_tree_type section to visually mark a tree node header as either a collapseable UI section or tree node
  3064. * \param[in] title | Label printed in the tree header
  3065. * \param[in] state | Persistent state to update
  3066. *
  3067. * \returns `true(1)` if visible and fillable with widgets or `false(0)` otherwise
  3068. */
  3069. 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);
  3070. /**
  3071. * # # nk_tree_state_pop
  3072. * Ends a collapsabale UI section
  3073. * ```c
  3074. * void nk_tree_state_pop(struct nk_context*);
  3075. * ```
  3076. *
  3077. * Parameter | Description
  3078. * ------------|-----------------------------------------------------------
  3079. * \param[in] ctx | Must point to an previously initialized `nk_context` struct after calling `nk_tree_xxx_push_xxx`
  3080. */
  3081. NK_API void nk_tree_state_pop(struct nk_context*);
  3082. #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__)
  3083. #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)
  3084. 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);
  3085. 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);
  3086. NK_API void nk_tree_element_pop(struct nk_context*);
  3087. /* =============================================================================
  3088. *
  3089. * LIST VIEW
  3090. *
  3091. * ============================================================================= */
  3092. struct nk_list_view {
  3093. /* public: */
  3094. int begin, end, count;
  3095. /* private: */
  3096. int total_height;
  3097. struct nk_context *ctx;
  3098. nk_uint *scroll_pointer;
  3099. nk_uint scroll_value;
  3100. };
  3101. 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);
  3102. NK_API void nk_list_view_end(struct nk_list_view*);
  3103. /* =============================================================================
  3104. *
  3105. * WIDGET
  3106. *
  3107. * ============================================================================= */
  3108. enum nk_widget_layout_states {
  3109. NK_WIDGET_INVALID, /**< The widget cannot be seen and is completely out of view */
  3110. NK_WIDGET_VALID, /**< The widget is completely inside the window and can be updated and drawn */
  3111. NK_WIDGET_ROM, /**< The widget is partially visible and cannot be updated */
  3112. NK_WIDGET_DISABLED /**< The widget is manually disabled and acts like NK_WIDGET_ROM */
  3113. };
  3114. enum nk_widget_states {
  3115. NK_WIDGET_STATE_MODIFIED = NK_FLAG(1),
  3116. NK_WIDGET_STATE_INACTIVE = NK_FLAG(2), /**!< widget is neither active nor hovered */
  3117. NK_WIDGET_STATE_ENTERED = NK_FLAG(3), /**!< widget has been hovered on the current frame */
  3118. NK_WIDGET_STATE_HOVER = NK_FLAG(4), /**!< widget is being hovered */
  3119. NK_WIDGET_STATE_ACTIVED = NK_FLAG(5),/**!< widget is currently activated */
  3120. NK_WIDGET_STATE_LEFT = NK_FLAG(6), /**!< widget is from this frame on not hovered anymore */
  3121. NK_WIDGET_STATE_HOVERED = NK_WIDGET_STATE_HOVER|NK_WIDGET_STATE_MODIFIED, /**!< widget is being hovered */
  3122. NK_WIDGET_STATE_ACTIVE = NK_WIDGET_STATE_ACTIVED|NK_WIDGET_STATE_MODIFIED /**!< widget is currently activated */
  3123. };
  3124. NK_API enum nk_widget_layout_states nk_widget(struct nk_rect*, const struct nk_context*);
  3125. NK_API enum nk_widget_layout_states nk_widget_fitting(struct nk_rect*, const struct nk_context*, struct nk_vec2);
  3126. NK_API struct nk_rect nk_widget_bounds(const struct nk_context*);
  3127. NK_API struct nk_vec2 nk_widget_position(const struct nk_context*);
  3128. NK_API struct nk_vec2 nk_widget_size(const struct nk_context*);
  3129. NK_API float nk_widget_width(const struct nk_context*);
  3130. NK_API float nk_widget_height(const struct nk_context*);
  3131. NK_API nk_bool nk_widget_is_hovered(const struct nk_context*);
  3132. NK_API nk_bool nk_widget_is_mouse_clicked(const struct nk_context*, enum nk_buttons);
  3133. NK_API nk_bool nk_widget_has_mouse_click_down(const struct nk_context*, enum nk_buttons, nk_bool down);
  3134. NK_API void nk_spacing(struct nk_context*, int cols);
  3135. NK_API void nk_widget_disable_begin(struct nk_context* ctx);
  3136. NK_API void nk_widget_disable_end(struct nk_context* ctx);
  3137. /* =============================================================================
  3138. *
  3139. * TEXT
  3140. *
  3141. * ============================================================================= */
  3142. enum nk_text_align {
  3143. NK_TEXT_ALIGN_LEFT = 0x01,
  3144. NK_TEXT_ALIGN_CENTERED = 0x02,
  3145. NK_TEXT_ALIGN_RIGHT = 0x04,
  3146. NK_TEXT_ALIGN_TOP = 0x08,
  3147. NK_TEXT_ALIGN_MIDDLE = 0x10,
  3148. NK_TEXT_ALIGN_BOTTOM = 0x20
  3149. };
  3150. enum nk_text_alignment {
  3151. NK_TEXT_LEFT = NK_TEXT_ALIGN_MIDDLE|NK_TEXT_ALIGN_LEFT,
  3152. NK_TEXT_CENTERED = NK_TEXT_ALIGN_MIDDLE|NK_TEXT_ALIGN_CENTERED,
  3153. NK_TEXT_RIGHT = NK_TEXT_ALIGN_MIDDLE|NK_TEXT_ALIGN_RIGHT
  3154. };
  3155. NK_API void nk_text(struct nk_context*, const char*, int, nk_flags);
  3156. NK_API void nk_text_colored(struct nk_context*, const char*, int, nk_flags, struct nk_color);
  3157. NK_API void nk_text_wrap(struct nk_context*, const char*, int);
  3158. NK_API void nk_text_wrap_colored(struct nk_context*, const char*, int, struct nk_color);
  3159. NK_API void nk_label(struct nk_context*, const char*, nk_flags align);
  3160. NK_API void nk_label_colored(struct nk_context*, const char*, nk_flags align, struct nk_color);
  3161. NK_API void nk_label_wrap(struct nk_context*, const char*);
  3162. NK_API void nk_label_colored_wrap(struct nk_context*, const char*, struct nk_color);
  3163. NK_API void nk_image(struct nk_context*, struct nk_image);
  3164. NK_API void nk_image_color(struct nk_context*, struct nk_image, struct nk_color);
  3165. #ifdef NK_INCLUDE_STANDARD_VARARGS
  3166. NK_API void nk_labelf(struct nk_context*, nk_flags, NK_PRINTF_FORMAT_STRING const char*, ...) NK_PRINTF_VARARG_FUNC(3);
  3167. 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);
  3168. NK_API void nk_labelf_wrap(struct nk_context*, NK_PRINTF_FORMAT_STRING const char*,...) NK_PRINTF_VARARG_FUNC(2);
  3169. NK_API void nk_labelf_colored_wrap(struct nk_context*, struct nk_color, NK_PRINTF_FORMAT_STRING const char*,...) NK_PRINTF_VARARG_FUNC(3);
  3170. NK_API void nk_labelfv(struct nk_context*, nk_flags, NK_PRINTF_FORMAT_STRING const char*, va_list) NK_PRINTF_VALIST_FUNC(3);
  3171. 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);
  3172. NK_API void nk_labelfv_wrap(struct nk_context*, NK_PRINTF_FORMAT_STRING const char*, va_list) NK_PRINTF_VALIST_FUNC(2);
  3173. 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);
  3174. NK_API void nk_value_bool(struct nk_context*, const char *prefix, int);
  3175. NK_API void nk_value_int(struct nk_context*, const char *prefix, int);
  3176. NK_API void nk_value_uint(struct nk_context*, const char *prefix, unsigned int);
  3177. NK_API void nk_value_float(struct nk_context*, const char *prefix, float);
  3178. NK_API void nk_value_color_byte(struct nk_context*, const char *prefix, struct nk_color);
  3179. NK_API void nk_value_color_float(struct nk_context*, const char *prefix, struct nk_color);
  3180. NK_API void nk_value_color_hex(struct nk_context*, const char *prefix, struct nk_color);
  3181. #endif
  3182. /* =============================================================================
  3183. *
  3184. * BUTTON
  3185. *
  3186. * ============================================================================= */
  3187. NK_API nk_bool nk_button_text(struct nk_context*, const char *title, int len);
  3188. NK_API nk_bool nk_button_label(struct nk_context*, const char *title);
  3189. NK_API nk_bool nk_button_color(struct nk_context*, struct nk_color);
  3190. NK_API nk_bool nk_button_symbol(struct nk_context*, enum nk_symbol_type);
  3191. NK_API nk_bool nk_button_image(struct nk_context*, struct nk_image img);
  3192. NK_API nk_bool nk_button_symbol_label(struct nk_context*, enum nk_symbol_type, const char*, nk_flags text_alignment);
  3193. NK_API nk_bool nk_button_symbol_text(struct nk_context*, enum nk_symbol_type, const char*, int, nk_flags alignment);
  3194. NK_API nk_bool nk_button_image_label(struct nk_context*, struct nk_image img, const char*, nk_flags text_alignment);
  3195. NK_API nk_bool nk_button_image_text(struct nk_context*, struct nk_image img, const char*, int, nk_flags alignment);
  3196. NK_API nk_bool nk_button_text_styled(struct nk_context*, const struct nk_style_button*, const char *title, int len);
  3197. NK_API nk_bool nk_button_label_styled(struct nk_context*, const struct nk_style_button*, const char *title);
  3198. NK_API nk_bool nk_button_symbol_styled(struct nk_context*, const struct nk_style_button*, enum nk_symbol_type);
  3199. NK_API nk_bool nk_button_image_styled(struct nk_context*, const struct nk_style_button*, struct nk_image img);
  3200. 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);
  3201. 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);
  3202. 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);
  3203. 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);
  3204. NK_API void nk_button_set_behavior(struct nk_context*, enum nk_button_behavior);
  3205. NK_API nk_bool nk_button_push_behavior(struct nk_context*, enum nk_button_behavior);
  3206. NK_API nk_bool nk_button_pop_behavior(struct nk_context*);
  3207. /* =============================================================================
  3208. *
  3209. * CHECKBOX
  3210. *
  3211. * ============================================================================= */
  3212. NK_API nk_bool nk_check_label(struct nk_context*, const char*, nk_bool active);
  3213. NK_API nk_bool nk_check_text(struct nk_context*, const char*, int, nk_bool active);
  3214. NK_API nk_bool nk_check_text_align(struct nk_context*, const char*, int, nk_bool active, nk_flags widget_alignment, nk_flags text_alignment);
  3215. NK_API unsigned nk_check_flags_label(struct nk_context*, const char*, unsigned int flags, unsigned int value);
  3216. NK_API unsigned nk_check_flags_text(struct nk_context*, const char*, int, unsigned int flags, unsigned int value);
  3217. NK_API nk_bool nk_checkbox_label(struct nk_context*, const char*, nk_bool *active);
  3218. NK_API nk_bool nk_checkbox_label_align(struct nk_context *ctx, const char *label, nk_bool *active, nk_flags widget_alignment, nk_flags text_alignment);
  3219. NK_API nk_bool nk_checkbox_text(struct nk_context*, const char*, int, nk_bool *active);
  3220. NK_API nk_bool nk_checkbox_text_align(struct nk_context *ctx, const char *text, int len, nk_bool *active, nk_flags widget_alignment, nk_flags text_alignment);
  3221. NK_API nk_bool nk_checkbox_flags_label(struct nk_context*, const char*, unsigned int *flags, unsigned int value);
  3222. NK_API nk_bool nk_checkbox_flags_text(struct nk_context*, const char*, int, unsigned int *flags, unsigned int value);
  3223. /* =============================================================================
  3224. *
  3225. * RADIO BUTTON
  3226. *
  3227. * ============================================================================= */
  3228. NK_API nk_bool nk_radio_label(struct nk_context*, const char*, nk_bool *active);
  3229. NK_API nk_bool nk_radio_label_align(struct nk_context *ctx, const char *label, nk_bool *active, nk_flags widget_alignment, nk_flags text_alignment);
  3230. NK_API nk_bool nk_radio_text(struct nk_context*, const char*, int, nk_bool *active);
  3231. NK_API nk_bool nk_radio_text_align(struct nk_context *ctx, const char *text, int len, nk_bool *active, nk_flags widget_alignment, nk_flags text_alignment);
  3232. NK_API nk_bool nk_option_label(struct nk_context*, const char*, nk_bool active);
  3233. NK_API nk_bool nk_option_label_align(struct nk_context *ctx, const char *label, nk_bool active, nk_flags widget_alignment, nk_flags text_alignment);
  3234. NK_API nk_bool nk_option_text(struct nk_context*, const char*, int, nk_bool active);
  3235. NK_API nk_bool nk_option_text_align(struct nk_context *ctx, const char *text, int len, nk_bool is_active, nk_flags widget_alignment, nk_flags text_alignment);
  3236. /* =============================================================================
  3237. *
  3238. * SELECTABLE
  3239. *
  3240. * ============================================================================= */
  3241. NK_API nk_bool nk_selectable_label(struct nk_context*, const char*, nk_flags align, nk_bool *value);
  3242. NK_API nk_bool nk_selectable_text(struct nk_context*, const char*, int, nk_flags align, nk_bool *value);
  3243. NK_API nk_bool nk_selectable_image_label(struct nk_context*,struct nk_image, const char*, nk_flags align, nk_bool *value);
  3244. NK_API nk_bool nk_selectable_image_text(struct nk_context*,struct nk_image, const char*, int, nk_flags align, nk_bool *value);
  3245. NK_API nk_bool nk_selectable_symbol_label(struct nk_context*,enum nk_symbol_type, const char*, nk_flags align, nk_bool *value);
  3246. NK_API nk_bool nk_selectable_symbol_text(struct nk_context*,enum nk_symbol_type, const char*, int, nk_flags align, nk_bool *value);
  3247. NK_API nk_bool nk_select_label(struct nk_context*, const char*, nk_flags align, nk_bool value);
  3248. NK_API nk_bool nk_select_text(struct nk_context*, const char*, int, nk_flags align, nk_bool value);
  3249. NK_API nk_bool nk_select_image_label(struct nk_context*, struct nk_image,const char*, nk_flags align, nk_bool value);
  3250. NK_API nk_bool nk_select_image_text(struct nk_context*, struct nk_image,const char*, int, nk_flags align, nk_bool value);
  3251. NK_API nk_bool nk_select_symbol_label(struct nk_context*,enum nk_symbol_type, const char*, nk_flags align, nk_bool value);
  3252. NK_API nk_bool nk_select_symbol_text(struct nk_context*,enum nk_symbol_type, const char*, int, nk_flags align, nk_bool value);
  3253. /* =============================================================================
  3254. *
  3255. * SLIDER
  3256. *
  3257. * ============================================================================= */
  3258. NK_API float nk_slide_float(struct nk_context*, float min, float val, float max, float step);
  3259. NK_API int nk_slide_int(struct nk_context*, int min, int val, int max, int step);
  3260. NK_API nk_bool nk_slider_float(struct nk_context*, float min, float *val, float max, float step);
  3261. NK_API nk_bool nk_slider_int(struct nk_context*, int min, int *val, int max, int step);
  3262. /* =============================================================================
  3263. *
  3264. * KNOB
  3265. *
  3266. * ============================================================================= */
  3267. NK_API nk_bool nk_knob_float(struct nk_context*, float min, float *val, float max, float step, enum nk_heading zero_direction, float dead_zone_degrees);
  3268. NK_API nk_bool nk_knob_int(struct nk_context*, int min, int *val, int max, int step, enum nk_heading zero_direction, float dead_zone_degrees);
  3269. /* =============================================================================
  3270. *
  3271. * PROGRESSBAR
  3272. *
  3273. * ============================================================================= */
  3274. NK_API nk_bool nk_progress(struct nk_context*, nk_size *cur, nk_size max, nk_bool modifyable);
  3275. NK_API nk_size nk_prog(struct nk_context*, nk_size cur, nk_size max, nk_bool modifyable);
  3276. /* =============================================================================
  3277. *
  3278. * COLOR PICKER
  3279. *
  3280. * ============================================================================= */
  3281. NK_API struct nk_colorf nk_color_picker(struct nk_context*, struct nk_colorf, enum nk_color_format);
  3282. NK_API nk_bool nk_color_pick(struct nk_context*, struct nk_colorf*, enum nk_color_format);
  3283. /* =============================================================================
  3284. *
  3285. * PROPERTIES
  3286. *
  3287. * =============================================================================*/
  3288. /**
  3289. * \page Properties
  3290. * Properties are the main value modification widgets in Nuklear. Changing a value
  3291. * can be achieved by dragging, adding/removing incremental steps on button click
  3292. * or by directly typing a number.
  3293. *
  3294. * # Usage
  3295. * Each property requires a unique name for identification that is also used for
  3296. * displaying a label. If you want to use the same name multiple times make sure
  3297. * add a '#' before your name. The '#' will not be shown but will generate a
  3298. * unique ID. Each property also takes in a minimum and maximum value. If you want
  3299. * to make use of the complete number range of a type just use the provided
  3300. * type limits from `limits.h`. For example `INT_MIN` and `INT_MAX` for
  3301. * `nk_property_int` and `nk_propertyi`. In additional each property takes in
  3302. * a increment value that will be added or subtracted if either the increment
  3303. * decrement button is clicked. Finally there is a value for increment per pixel
  3304. * dragged that is added or subtracted from the value.
  3305. *
  3306. * ```c
  3307. * int value = 0;
  3308. * struct nk_context ctx;
  3309. * nk_init_xxx(&ctx, ...);
  3310. * while (1) {
  3311. * // Input
  3312. * Event evt;
  3313. * nk_input_begin(&ctx);
  3314. * while (GetEvent(&evt)) {
  3315. * if (evt.type == MOUSE_MOVE)
  3316. * nk_input_motion(&ctx, evt.motion.x, evt.motion.y);
  3317. * else if (evt.type == [...]) {
  3318. * nk_input_xxx(...);
  3319. * }
  3320. * }
  3321. * nk_input_end(&ctx);
  3322. * //
  3323. * // Window
  3324. * if (nk_begin_xxx(...) {
  3325. * // Property
  3326. * nk_layout_row_dynamic(...);
  3327. * nk_property_int(ctx, "ID", INT_MIN, &value, INT_MAX, 1, 1);
  3328. * }
  3329. * nk_end(ctx);
  3330. * //
  3331. * // Draw
  3332. * const struct nk_command *cmd = 0;
  3333. * nk_foreach(cmd, &ctx) {
  3334. * switch (cmd->type) {
  3335. * case NK_COMMAND_LINE:
  3336. * your_draw_line_function(...)
  3337. * break;
  3338. * case NK_COMMAND_RECT
  3339. * your_draw_rect_function(...)
  3340. * break;
  3341. * case ...:
  3342. * // [...]
  3343. * }
  3344. * nk_clear(&ctx);
  3345. * }
  3346. * nk_free(&ctx);
  3347. * ```
  3348. *
  3349. * # Reference
  3350. * Function | Description
  3351. * --------------------|-------------------------------------------
  3352. * \ref nk_property_int | Integer property directly modifying a passed in value
  3353. * \ref nk_property_float | Float property directly modifying a passed in value
  3354. * \ref nk_property_double | Double property directly modifying a passed in value
  3355. * \ref nk_propertyi | Integer property returning the modified int value
  3356. * \ref nk_propertyf | Float property returning the modified float value
  3357. * \ref nk_propertyd | Double property returning the modified double value
  3358. *
  3359. * # # nk_property_int
  3360. * Integer property directly modifying a passed in value
  3361. * !!! \warning
  3362. * To generate a unique property ID using the same label make sure to insert
  3363. * a `#` at the beginning. It will not be shown but guarantees correct behavior.
  3364. *
  3365. * ```c
  3366. * void nk_property_int(struct nk_context *ctx, const char *name, int min, int *val, int max, int step, float inc_per_pixel);
  3367. * ```
  3368. *
  3369. * Parameter | Description
  3370. * --------------------|-----------------------------------------------------------
  3371. * \param[in] ctx | Must point to an previously initialized `nk_context` struct after calling a layouting function
  3372. * \param[in] name | String used both as a label as well as a unique identifier
  3373. * \param[in] min | Minimum value not allowed to be underflown
  3374. * \param[in] val | Integer pointer to be modified
  3375. * \param[in] max | Maximum value not allowed to be overflown
  3376. * \param[in] step | Increment added and subtracted on increment and decrement button
  3377. * \param[in] inc_per_pixel | Value per pixel added or subtracted on dragging
  3378. */
  3379. NK_API void nk_property_int(struct nk_context*, const char *name, int min, int *val, int max, int step, float inc_per_pixel);
  3380. /**
  3381. * # # nk_property_float
  3382. * Float property directly modifying a passed in value
  3383. * !!! \warning
  3384. * To generate a unique property ID using the same label make sure to insert
  3385. * a `#` at the beginning. It will not be shown but guarantees correct behavior.
  3386. *
  3387. * ```c
  3388. * void nk_property_float(struct nk_context *ctx, const char *name, float min, float *val, float max, float step, float inc_per_pixel);
  3389. * ```
  3390. *
  3391. * Parameter | Description
  3392. * --------------------|-----------------------------------------------------------
  3393. * \param[in] ctx | Must point to an previously initialized `nk_context` struct after calling a layouting function
  3394. * \param[in] name | String used both as a label as well as a unique identifier
  3395. * \param[in] min | Minimum value not allowed to be underflown
  3396. * \param[in] val | Float pointer to be modified
  3397. * \param[in] max | Maximum value not allowed to be overflown
  3398. * \param[in] step | Increment added and subtracted on increment and decrement button
  3399. * \param[in] inc_per_pixel | Value per pixel added or subtracted on dragging
  3400. */
  3401. NK_API void nk_property_float(struct nk_context*, const char *name, float min, float *val, float max, float step, float inc_per_pixel);
  3402. /**
  3403. * # # nk_property_double
  3404. * Double property directly modifying a passed in value
  3405. * !!! \warning
  3406. * To generate a unique property ID using the same label make sure to insert
  3407. * a `#` at the beginning. It will not be shown but guarantees correct behavior.
  3408. *
  3409. * ```c
  3410. * void nk_property_double(struct nk_context *ctx, const char *name, double min, double *val, double max, double step, double inc_per_pixel);
  3411. * ```
  3412. *
  3413. * Parameter | Description
  3414. * --------------------|-----------------------------------------------------------
  3415. * \param[in] ctx | Must point to an previously initialized `nk_context` struct after calling a layouting function
  3416. * \param[in] name | String used both as a label as well as a unique identifier
  3417. * \param[in] min | Minimum value not allowed to be underflown
  3418. * \param[in] val | Double pointer to be modified
  3419. * \param[in] max | Maximum value not allowed to be overflown
  3420. * \param[in] step | Increment added and subtracted on increment and decrement button
  3421. * \param[in] inc_per_pixel | Value per pixel added or subtracted on dragging
  3422. */
  3423. NK_API void nk_property_double(struct nk_context*, const char *name, double min, double *val, double max, double step, float inc_per_pixel);
  3424. /**
  3425. * # # nk_propertyi
  3426. * Integer property modifying a passed in value and returning the new value
  3427. * !!! \warning
  3428. * To generate a unique property ID using the same label make sure to insert
  3429. * a `#` at the beginning. It will not be shown but guarantees correct behavior.
  3430. *
  3431. * ```c
  3432. * int nk_propertyi(struct nk_context *ctx, const char *name, int min, int val, int max, int step, float inc_per_pixel);
  3433. * ```
  3434. *
  3435. * \param[in] ctx Must point to an previously initialized `nk_context` struct after calling a layouting function
  3436. * \param[in] name String used both as a label as well as a unique identifier
  3437. * \param[in] min Minimum value not allowed to be underflown
  3438. * \param[in] val Current integer value to be modified and returned
  3439. * \param[in] max Maximum value not allowed to be overflown
  3440. * \param[in] step Increment added and subtracted on increment and decrement button
  3441. * \param[in] inc_per_pixel Value per pixel added or subtracted on dragging
  3442. *
  3443. * \returns the new modified integer value
  3444. */
  3445. NK_API int nk_propertyi(struct nk_context*, const char *name, int min, int val, int max, int step, float inc_per_pixel);
  3446. /**
  3447. * # # nk_propertyf
  3448. * Float property modifying a passed in value and returning the new value
  3449. * !!! \warning
  3450. * To generate a unique property ID using the same label make sure to insert
  3451. * a `#` at the beginning. It will not be shown but guarantees correct behavior.
  3452. *
  3453. * ```c
  3454. * float nk_propertyf(struct nk_context *ctx, const char *name, float min, float val, float max, float step, float inc_per_pixel);
  3455. * ```
  3456. *
  3457. * \param[in] ctx Must point to an previously initialized `nk_context` struct after calling a layouting function
  3458. * \param[in] name String used both as a label as well as a unique identifier
  3459. * \param[in] min Minimum value not allowed to be underflown
  3460. * \param[in] val Current float value to be modified and returned
  3461. * \param[in] max Maximum value not allowed to be overflown
  3462. * \param[in] step Increment added and subtracted on increment and decrement button
  3463. * \param[in] inc_per_pixel Value per pixel added or subtracted on dragging
  3464. *
  3465. * \returns the new modified float value
  3466. */
  3467. NK_API float nk_propertyf(struct nk_context*, const char *name, float min, float val, float max, float step, float inc_per_pixel);
  3468. /**
  3469. * # # nk_propertyd
  3470. * Float property modifying a passed in value and returning the new value
  3471. * !!! \warning
  3472. * To generate a unique property ID using the same label make sure to insert
  3473. * a `#` at the beginning. It will not be shown but guarantees correct behavior.
  3474. *
  3475. * ```c
  3476. * float nk_propertyd(struct nk_context *ctx, const char *name, double min, double val, double max, double step, double inc_per_pixel);
  3477. * ```
  3478. *
  3479. * \param[in] ctx Must point to an previously initialized `nk_context` struct after calling a layouting function
  3480. * \param[in] name String used both as a label as well as a unique identifier
  3481. * \param[in] min Minimum value not allowed to be underflown
  3482. * \param[in] val Current double value to be modified and returned
  3483. * \param[in] max Maximum value not allowed to be overflown
  3484. * \param[in] step Increment added and subtracted on increment and decrement button
  3485. * \param[in] inc_per_pixel Value per pixel added or subtracted on dragging
  3486. *
  3487. * \returns the new modified double value
  3488. */
  3489. NK_API double nk_propertyd(struct nk_context*, const char *name, double min, double val, double max, double step, float inc_per_pixel);
  3490. /* =============================================================================
  3491. *
  3492. * TEXT EDIT
  3493. *
  3494. * ============================================================================= */
  3495. enum nk_edit_flags {
  3496. NK_EDIT_DEFAULT = 0,
  3497. NK_EDIT_READ_ONLY = NK_FLAG(0),
  3498. NK_EDIT_AUTO_SELECT = NK_FLAG(1),
  3499. NK_EDIT_SIG_ENTER = NK_FLAG(2),
  3500. NK_EDIT_ALLOW_TAB = NK_FLAG(3),
  3501. NK_EDIT_NO_CURSOR = NK_FLAG(4),
  3502. NK_EDIT_SELECTABLE = NK_FLAG(5),
  3503. NK_EDIT_CLIPBOARD = NK_FLAG(6),
  3504. NK_EDIT_CTRL_ENTER_NEWLINE = NK_FLAG(7),
  3505. NK_EDIT_NO_HORIZONTAL_SCROLL = NK_FLAG(8),
  3506. NK_EDIT_ALWAYS_INSERT_MODE = NK_FLAG(9),
  3507. NK_EDIT_MULTILINE = NK_FLAG(10),
  3508. NK_EDIT_GOTO_END_ON_ACTIVATE = NK_FLAG(11)
  3509. };
  3510. enum nk_edit_types {
  3511. NK_EDIT_SIMPLE = NK_EDIT_ALWAYS_INSERT_MODE,
  3512. NK_EDIT_FIELD = NK_EDIT_SIMPLE|NK_EDIT_SELECTABLE|NK_EDIT_CLIPBOARD,
  3513. NK_EDIT_BOX = NK_EDIT_ALWAYS_INSERT_MODE| NK_EDIT_SELECTABLE| NK_EDIT_MULTILINE|NK_EDIT_ALLOW_TAB|NK_EDIT_CLIPBOARD,
  3514. NK_EDIT_EDITOR = NK_EDIT_SELECTABLE|NK_EDIT_MULTILINE|NK_EDIT_ALLOW_TAB| NK_EDIT_CLIPBOARD
  3515. };
  3516. enum nk_edit_events {
  3517. NK_EDIT_ACTIVE = NK_FLAG(0), /**!< edit widget is currently being modified */
  3518. NK_EDIT_INACTIVE = NK_FLAG(1), /**!< edit widget is not active and is not being modified */
  3519. NK_EDIT_ACTIVATED = NK_FLAG(2), /**!< edit widget went from state inactive to state active */
  3520. NK_EDIT_DEACTIVATED = NK_FLAG(3), /**!< edit widget went from state active to state inactive */
  3521. NK_EDIT_COMMITED = NK_FLAG(4) /**!< edit widget has received an enter and lost focus */
  3522. };
  3523. NK_API nk_flags nk_edit_string(struct nk_context*, nk_flags, char *buffer, int *len, int max, nk_plugin_filter);
  3524. NK_API nk_flags nk_edit_string_zero_terminated(struct nk_context*, nk_flags, char *buffer, int max, nk_plugin_filter);
  3525. NK_API nk_flags nk_edit_buffer(struct nk_context*, nk_flags, struct nk_text_edit*, nk_plugin_filter);
  3526. NK_API void nk_edit_focus(struct nk_context*, nk_flags flags);
  3527. NK_API void nk_edit_unfocus(struct nk_context*);
  3528. /* =============================================================================
  3529. *
  3530. * CHART
  3531. *
  3532. * ============================================================================= */
  3533. NK_API nk_bool nk_chart_begin(struct nk_context*, enum nk_chart_type, int num, float min, float max);
  3534. 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);
  3535. NK_API void nk_chart_add_slot(struct nk_context *ctx, const enum nk_chart_type, int count, float min_value, float max_value);
  3536. 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);
  3537. NK_API nk_flags nk_chart_push(struct nk_context*, float);
  3538. NK_API nk_flags nk_chart_push_slot(struct nk_context*, float, int);
  3539. NK_API void nk_chart_end(struct nk_context*);
  3540. NK_API void nk_plot(struct nk_context*, enum nk_chart_type, const float *values, int count, int offset);
  3541. 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);
  3542. /* =============================================================================
  3543. *
  3544. * POPUP
  3545. *
  3546. * ============================================================================= */
  3547. NK_API nk_bool nk_popup_begin(struct nk_context*, enum nk_popup_type, const char*, nk_flags, struct nk_rect bounds);
  3548. NK_API void nk_popup_close(struct nk_context*);
  3549. NK_API void nk_popup_end(struct nk_context*);
  3550. NK_API void nk_popup_get_scroll(const struct nk_context*, nk_uint *offset_x, nk_uint *offset_y);
  3551. NK_API void nk_popup_set_scroll(struct nk_context*, nk_uint offset_x, nk_uint offset_y);
  3552. /* =============================================================================
  3553. *
  3554. * COMBOBOX
  3555. *
  3556. * ============================================================================= */
  3557. NK_API int nk_combo(struct nk_context*, const char *const *items, int count, int selected, int item_height, struct nk_vec2 size);
  3558. 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);
  3559. 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);
  3560. 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);
  3561. NK_API void nk_combobox(struct nk_context*, const char *const *items, int count, int *selected, int item_height, struct nk_vec2 size);
  3562. 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);
  3563. 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);
  3564. 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);
  3565. /* =============================================================================
  3566. *
  3567. * ABSTRACT COMBOBOX
  3568. *
  3569. * ============================================================================= */
  3570. NK_API nk_bool nk_combo_begin_text(struct nk_context*, const char *selected, int, struct nk_vec2 size);
  3571. NK_API nk_bool nk_combo_begin_label(struct nk_context*, const char *selected, struct nk_vec2 size);
  3572. NK_API nk_bool nk_combo_begin_color(struct nk_context*, struct nk_color color, struct nk_vec2 size);
  3573. NK_API nk_bool nk_combo_begin_symbol(struct nk_context*, enum nk_symbol_type, struct nk_vec2 size);
  3574. NK_API nk_bool nk_combo_begin_symbol_label(struct nk_context*, const char *selected, enum nk_symbol_type, struct nk_vec2 size);
  3575. NK_API nk_bool nk_combo_begin_symbol_text(struct nk_context*, const char *selected, int, enum nk_symbol_type, struct nk_vec2 size);
  3576. NK_API nk_bool nk_combo_begin_image(struct nk_context*, struct nk_image img, struct nk_vec2 size);
  3577. NK_API nk_bool nk_combo_begin_image_label(struct nk_context*, const char *selected, struct nk_image, struct nk_vec2 size);
  3578. NK_API nk_bool nk_combo_begin_image_text(struct nk_context*, const char *selected, int, struct nk_image, struct nk_vec2 size);
  3579. NK_API nk_bool nk_combo_item_label(struct nk_context*, const char*, nk_flags alignment);
  3580. NK_API nk_bool nk_combo_item_text(struct nk_context*, const char*,int, nk_flags alignment);
  3581. NK_API nk_bool nk_combo_item_image_label(struct nk_context*, struct nk_image, const char*, nk_flags alignment);
  3582. NK_API nk_bool nk_combo_item_image_text(struct nk_context*, struct nk_image, const char*, int,nk_flags alignment);
  3583. NK_API nk_bool nk_combo_item_symbol_label(struct nk_context*, enum nk_symbol_type, const char*, nk_flags alignment);
  3584. NK_API nk_bool nk_combo_item_symbol_text(struct nk_context*, enum nk_symbol_type, const char*, int, nk_flags alignment);
  3585. NK_API void nk_combo_close(struct nk_context*);
  3586. NK_API void nk_combo_end(struct nk_context*);
  3587. /* =============================================================================
  3588. *
  3589. * CONTEXTUAL
  3590. *
  3591. * ============================================================================= */
  3592. NK_API nk_bool nk_contextual_begin(struct nk_context*, nk_flags, struct nk_vec2, struct nk_rect trigger_bounds);
  3593. NK_API nk_bool nk_contextual_item_text(struct nk_context*, const char*, int,nk_flags align);
  3594. NK_API nk_bool nk_contextual_item_label(struct nk_context*, const char*, nk_flags align);
  3595. NK_API nk_bool nk_contextual_item_image_label(struct nk_context*, struct nk_image, const char*, nk_flags alignment);
  3596. NK_API nk_bool nk_contextual_item_image_text(struct nk_context*, struct nk_image, const char*, int len, nk_flags alignment);
  3597. NK_API nk_bool nk_contextual_item_symbol_label(struct nk_context*, enum nk_symbol_type, const char*, nk_flags alignment);
  3598. NK_API nk_bool nk_contextual_item_symbol_text(struct nk_context*, enum nk_symbol_type, const char*, int, nk_flags alignment);
  3599. NK_API void nk_contextual_close(struct nk_context*);
  3600. NK_API void nk_contextual_end(struct nk_context*);
  3601. /* =============================================================================
  3602. *
  3603. * TOOLTIP
  3604. *
  3605. * ============================================================================= */
  3606. NK_API void nk_tooltip(struct nk_context*, const char*);
  3607. #ifdef NK_INCLUDE_STANDARD_VARARGS
  3608. NK_API void nk_tooltipf(struct nk_context*, NK_PRINTF_FORMAT_STRING const char*, ...) NK_PRINTF_VARARG_FUNC(2);
  3609. NK_API void nk_tooltipfv(struct nk_context*, NK_PRINTF_FORMAT_STRING const char*, va_list) NK_PRINTF_VALIST_FUNC(2);
  3610. #endif
  3611. NK_API nk_bool nk_tooltip_begin(struct nk_context*, float width);
  3612. NK_API void nk_tooltip_end(struct nk_context*);
  3613. /* =============================================================================
  3614. *
  3615. * MENU
  3616. *
  3617. * ============================================================================= */
  3618. NK_API void nk_menubar_begin(struct nk_context*);
  3619. NK_API void nk_menubar_end(struct nk_context*);
  3620. NK_API nk_bool nk_menu_begin_text(struct nk_context*, const char* title, int title_len, nk_flags align, struct nk_vec2 size);
  3621. NK_API nk_bool nk_menu_begin_label(struct nk_context*, const char*, nk_flags align, struct nk_vec2 size);
  3622. NK_API nk_bool nk_menu_begin_image(struct nk_context*, const char*, struct nk_image, struct nk_vec2 size);
  3623. 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);
  3624. NK_API nk_bool nk_menu_begin_image_label(struct nk_context*, const char*, nk_flags align,struct nk_image, struct nk_vec2 size);
  3625. NK_API nk_bool nk_menu_begin_symbol(struct nk_context*, const char*, enum nk_symbol_type, struct nk_vec2 size);
  3626. 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);
  3627. 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);
  3628. NK_API nk_bool nk_menu_item_text(struct nk_context*, const char*, int,nk_flags align);
  3629. NK_API nk_bool nk_menu_item_label(struct nk_context*, const char*, nk_flags alignment);
  3630. NK_API nk_bool nk_menu_item_image_label(struct nk_context*, struct nk_image, const char*, nk_flags alignment);
  3631. NK_API nk_bool nk_menu_item_image_text(struct nk_context*, struct nk_image, const char*, int len, nk_flags alignment);
  3632. NK_API nk_bool nk_menu_item_symbol_text(struct nk_context*, enum nk_symbol_type, const char*, int, nk_flags alignment);
  3633. NK_API nk_bool nk_menu_item_symbol_label(struct nk_context*, enum nk_symbol_type, const char*, nk_flags alignment);
  3634. NK_API void nk_menu_close(struct nk_context*);
  3635. NK_API void nk_menu_end(struct nk_context*);
  3636. /* =============================================================================
  3637. *
  3638. * STYLE
  3639. *
  3640. * ============================================================================= */
  3641. #define NK_WIDGET_DISABLED_FACTOR 0.5f
  3642. enum nk_style_colors {
  3643. NK_COLOR_TEXT,
  3644. NK_COLOR_WINDOW,
  3645. NK_COLOR_HEADER,
  3646. NK_COLOR_BORDER,
  3647. NK_COLOR_BUTTON,
  3648. NK_COLOR_BUTTON_HOVER,
  3649. NK_COLOR_BUTTON_ACTIVE,
  3650. NK_COLOR_TOGGLE,
  3651. NK_COLOR_TOGGLE_HOVER,
  3652. NK_COLOR_TOGGLE_CURSOR,
  3653. NK_COLOR_SELECT,
  3654. NK_COLOR_SELECT_ACTIVE,
  3655. NK_COLOR_SLIDER,
  3656. NK_COLOR_SLIDER_CURSOR,
  3657. NK_COLOR_SLIDER_CURSOR_HOVER,
  3658. NK_COLOR_SLIDER_CURSOR_ACTIVE,
  3659. NK_COLOR_PROPERTY,
  3660. NK_COLOR_EDIT,
  3661. NK_COLOR_EDIT_CURSOR,
  3662. NK_COLOR_COMBO,
  3663. NK_COLOR_CHART,
  3664. NK_COLOR_CHART_COLOR,
  3665. NK_COLOR_CHART_COLOR_HIGHLIGHT,
  3666. NK_COLOR_SCROLLBAR,
  3667. NK_COLOR_SCROLLBAR_CURSOR,
  3668. NK_COLOR_SCROLLBAR_CURSOR_HOVER,
  3669. NK_COLOR_SCROLLBAR_CURSOR_ACTIVE,
  3670. NK_COLOR_TAB_HEADER,
  3671. NK_COLOR_KNOB,
  3672. NK_COLOR_KNOB_CURSOR,
  3673. NK_COLOR_KNOB_CURSOR_HOVER,
  3674. NK_COLOR_KNOB_CURSOR_ACTIVE,
  3675. NK_COLOR_COUNT
  3676. };
  3677. enum nk_style_cursor {
  3678. NK_CURSOR_ARROW,
  3679. NK_CURSOR_TEXT,
  3680. NK_CURSOR_MOVE,
  3681. NK_CURSOR_RESIZE_VERTICAL,
  3682. NK_CURSOR_RESIZE_HORIZONTAL,
  3683. NK_CURSOR_RESIZE_TOP_LEFT_DOWN_RIGHT,
  3684. NK_CURSOR_RESIZE_TOP_RIGHT_DOWN_LEFT,
  3685. NK_CURSOR_COUNT
  3686. };
  3687. NK_API void nk_style_default(struct nk_context*);
  3688. NK_API void nk_style_from_table(struct nk_context*, const struct nk_color*);
  3689. NK_API void nk_style_load_cursor(struct nk_context*, enum nk_style_cursor, const struct nk_cursor*);
  3690. NK_API void nk_style_load_all_cursors(struct nk_context*, const struct nk_cursor*);
  3691. NK_API const char* nk_style_get_color_by_name(enum nk_style_colors);
  3692. NK_API void nk_style_set_font(struct nk_context*, const struct nk_user_font*);
  3693. NK_API nk_bool nk_style_set_cursor(struct nk_context*, enum nk_style_cursor);
  3694. NK_API void nk_style_show_cursor(struct nk_context*);
  3695. NK_API void nk_style_hide_cursor(struct nk_context*);
  3696. NK_API nk_bool nk_style_push_font(struct nk_context*, const struct nk_user_font*);
  3697. NK_API nk_bool nk_style_push_float(struct nk_context*, float*, float);
  3698. NK_API nk_bool nk_style_push_vec2(struct nk_context*, struct nk_vec2*, struct nk_vec2);
  3699. NK_API nk_bool nk_style_push_style_item(struct nk_context*, struct nk_style_item*, struct nk_style_item);
  3700. NK_API nk_bool nk_style_push_flags(struct nk_context*, nk_flags*, nk_flags);
  3701. NK_API nk_bool nk_style_push_color(struct nk_context*, struct nk_color*, struct nk_color);
  3702. NK_API nk_bool nk_style_pop_font(struct nk_context*);
  3703. NK_API nk_bool nk_style_pop_float(struct nk_context*);
  3704. NK_API nk_bool nk_style_pop_vec2(struct nk_context*);
  3705. NK_API nk_bool nk_style_pop_style_item(struct nk_context*);
  3706. NK_API nk_bool nk_style_pop_flags(struct nk_context*);
  3707. NK_API nk_bool nk_style_pop_color(struct nk_context*);
  3708. /* =============================================================================
  3709. *
  3710. * COLOR
  3711. *
  3712. * ============================================================================= */
  3713. NK_API struct nk_color nk_rgb(int r, int g, int b);
  3714. NK_API struct nk_color nk_rgb_iv(const int *rgb);
  3715. NK_API struct nk_color nk_rgb_bv(const nk_byte* rgb);
  3716. NK_API struct nk_color nk_rgb_f(float r, float g, float b);
  3717. NK_API struct nk_color nk_rgb_fv(const float *rgb);
  3718. NK_API struct nk_color nk_rgb_cf(struct nk_colorf c);
  3719. NK_API struct nk_color nk_rgb_hex(const char *rgb);
  3720. NK_API struct nk_color nk_rgb_factor(struct nk_color col, float factor);
  3721. NK_API struct nk_color nk_rgba(int r, int g, int b, int a);
  3722. NK_API struct nk_color nk_rgba_u32(nk_uint);
  3723. NK_API struct nk_color nk_rgba_iv(const int *rgba);
  3724. NK_API struct nk_color nk_rgba_bv(const nk_byte *rgba);
  3725. NK_API struct nk_color nk_rgba_f(float r, float g, float b, float a);
  3726. NK_API struct nk_color nk_rgba_fv(const float *rgba);
  3727. NK_API struct nk_color nk_rgba_cf(struct nk_colorf c);
  3728. NK_API struct nk_color nk_rgba_hex(const char *rgb);
  3729. NK_API struct nk_colorf nk_hsva_colorf(float h, float s, float v, float a);
  3730. NK_API struct nk_colorf nk_hsva_colorfv(const float *c);
  3731. NK_API void nk_colorf_hsva_f(float *out_h, float *out_s, float *out_v, float *out_a, struct nk_colorf in);
  3732. NK_API void nk_colorf_hsva_fv(float *hsva, struct nk_colorf in);
  3733. NK_API struct nk_color nk_hsv(int h, int s, int v);
  3734. NK_API struct nk_color nk_hsv_iv(const int *hsv);
  3735. NK_API struct nk_color nk_hsv_bv(const nk_byte *hsv);
  3736. NK_API struct nk_color nk_hsv_f(float h, float s, float v);
  3737. NK_API struct nk_color nk_hsv_fv(const float *hsv);
  3738. NK_API struct nk_color nk_hsva(int h, int s, int v, int a);
  3739. NK_API struct nk_color nk_hsva_iv(const int *hsva);
  3740. NK_API struct nk_color nk_hsva_bv(const nk_byte *hsva);
  3741. NK_API struct nk_color nk_hsva_f(float h, float s, float v, float a);
  3742. NK_API struct nk_color nk_hsva_fv(const float *hsva);
  3743. /* color (conversion nuklear --> user) */
  3744. NK_API void nk_color_f(float *r, float *g, float *b, float *a, struct nk_color);
  3745. NK_API void nk_color_fv(float *rgba_out, struct nk_color);
  3746. NK_API struct nk_colorf nk_color_cf(struct nk_color);
  3747. NK_API void nk_color_d(double *r, double *g, double *b, double *a, struct nk_color);
  3748. NK_API void nk_color_dv(double *rgba_out, struct nk_color);
  3749. NK_API nk_uint nk_color_u32(struct nk_color);
  3750. NK_API void nk_color_hex_rgba(char *output, struct nk_color);
  3751. NK_API void nk_color_hex_rgb(char *output, struct nk_color);
  3752. NK_API void nk_color_hsv_i(int *out_h, int *out_s, int *out_v, struct nk_color);
  3753. NK_API void nk_color_hsv_b(nk_byte *out_h, nk_byte *out_s, nk_byte *out_v, struct nk_color);
  3754. NK_API void nk_color_hsv_iv(int *hsv_out, struct nk_color);
  3755. NK_API void nk_color_hsv_bv(nk_byte *hsv_out, struct nk_color);
  3756. NK_API void nk_color_hsv_f(float *out_h, float *out_s, float *out_v, struct nk_color);
  3757. NK_API void nk_color_hsv_fv(float *hsv_out, struct nk_color);
  3758. NK_API void nk_color_hsva_i(int *h, int *s, int *v, int *a, struct nk_color);
  3759. NK_API void nk_color_hsva_b(nk_byte *h, nk_byte *s, nk_byte *v, nk_byte *a, struct nk_color);
  3760. NK_API void nk_color_hsva_iv(int *hsva_out, struct nk_color);
  3761. NK_API void nk_color_hsva_bv(nk_byte *hsva_out, struct nk_color);
  3762. NK_API void nk_color_hsva_f(float *out_h, float *out_s, float *out_v, float *out_a, struct nk_color);
  3763. NK_API void nk_color_hsva_fv(float *hsva_out, struct nk_color);
  3764. /* =============================================================================
  3765. *
  3766. * IMAGE
  3767. *
  3768. * ============================================================================= */
  3769. NK_API nk_handle nk_handle_ptr(void*);
  3770. NK_API nk_handle nk_handle_id(int);
  3771. NK_API struct nk_image nk_image_handle(nk_handle);
  3772. NK_API struct nk_image nk_image_ptr(void*);
  3773. NK_API struct nk_image nk_image_id(int);
  3774. NK_API nk_bool nk_image_is_subimage(const struct nk_image* img);
  3775. NK_API struct nk_image nk_subimage_ptr(void*, nk_ushort w, nk_ushort h, struct nk_rect sub_region);
  3776. NK_API struct nk_image nk_subimage_id(int, nk_ushort w, nk_ushort h, struct nk_rect sub_region);
  3777. NK_API struct nk_image nk_subimage_handle(nk_handle, nk_ushort w, nk_ushort h, struct nk_rect sub_region);
  3778. /* =============================================================================
  3779. *
  3780. * 9-SLICE
  3781. *
  3782. * ============================================================================= */
  3783. NK_API struct nk_nine_slice nk_nine_slice_handle(nk_handle, nk_ushort l, nk_ushort t, nk_ushort r, nk_ushort b);
  3784. NK_API struct nk_nine_slice nk_nine_slice_ptr(void*, nk_ushort l, nk_ushort t, nk_ushort r, nk_ushort b);
  3785. NK_API struct nk_nine_slice nk_nine_slice_id(int, nk_ushort l, nk_ushort t, nk_ushort r, nk_ushort b);
  3786. NK_API int nk_nine_slice_is_sub9slice(const struct nk_nine_slice* img);
  3787. 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);
  3788. 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);
  3789. 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);
  3790. /* =============================================================================
  3791. *
  3792. * MATH
  3793. *
  3794. * ============================================================================= */
  3795. NK_API nk_hash nk_murmur_hash(const void *key, int len, nk_hash seed);
  3796. NK_API void nk_triangle_from_direction(struct nk_vec2 *result, struct nk_rect r, float pad_x, float pad_y, enum nk_heading);
  3797. NK_API struct nk_vec2 nk_vec2(float x, float y);
  3798. NK_API struct nk_vec2 nk_vec2i(int x, int y);
  3799. NK_API struct nk_vec2 nk_vec2v(const float *xy);
  3800. NK_API struct nk_vec2 nk_vec2iv(const int *xy);
  3801. NK_API struct nk_rect nk_get_null_rect(void);
  3802. NK_API struct nk_rect nk_rect(float x, float y, float w, float h);
  3803. NK_API struct nk_rect nk_recti(int x, int y, int w, int h);
  3804. NK_API struct nk_rect nk_recta(struct nk_vec2 pos, struct nk_vec2 size);
  3805. NK_API struct nk_rect nk_rectv(const float *xywh);
  3806. NK_API struct nk_rect nk_rectiv(const int *xywh);
  3807. NK_API struct nk_vec2 nk_rect_pos(struct nk_rect);
  3808. NK_API struct nk_vec2 nk_rect_size(struct nk_rect);
  3809. /* =============================================================================
  3810. *
  3811. * STRING
  3812. *
  3813. * ============================================================================= */
  3814. NK_API int nk_strlen(const char *str);
  3815. NK_API int nk_stricmp(const char *s1, const char *s2);
  3816. NK_API int nk_stricmpn(const char *s1, const char *s2, int n);
  3817. NK_API int nk_strtoi(const char *str, char **endptr);
  3818. NK_API float nk_strtof(const char *str, char **endptr);
  3819. #ifndef NK_STRTOD
  3820. #define NK_STRTOD nk_strtod
  3821. NK_API double nk_strtod(const char *str, char **endptr);
  3822. #endif
  3823. NK_API int nk_strfilter(const char *text, const char *regexp);
  3824. NK_API int nk_strmatch_fuzzy_string(char const *str, char const *pattern, int *out_score);
  3825. NK_API int nk_strmatch_fuzzy_text(const char *txt, int txt_len, const char *pattern, int *out_score);
  3826. /* =============================================================================
  3827. *
  3828. * UTF-8
  3829. *
  3830. * ============================================================================= */
  3831. NK_API int nk_utf_decode(const char*, nk_rune*, int);
  3832. NK_API int nk_utf_encode(nk_rune, char*, int);
  3833. NK_API int nk_utf_len(const char*, int byte_len);
  3834. NK_API const char* nk_utf_at(const char *buffer, int length, int index, nk_rune *unicode, int *len);
  3835. /* ===============================================================
  3836. *
  3837. * FONT
  3838. *
  3839. * ===============================================================*/
  3840. /**
  3841. * \page Font
  3842. * Font handling in this library was designed to be quite customizable and lets
  3843. * you decide what you want to use and what you want to provide. There are three
  3844. * different ways to use the font atlas. The first two will use your font
  3845. * handling scheme and only requires essential data to run nuklear. The next
  3846. * slightly more advanced features is font handling with vertex buffer output.
  3847. * Finally the most complex API wise is using nuklear's font baking API.
  3848. *
  3849. * # Using your own implementation without vertex buffer output
  3850. *
  3851. * So first up the easiest way to do font handling is by just providing a
  3852. * `nk_user_font` struct which only requires the height in pixel of the used
  3853. * font and a callback to calculate the width of a string. This way of handling
  3854. * fonts is best fitted for using the normal draw shape command API where you
  3855. * do all the text drawing yourself and the library does not require any kind
  3856. * of deeper knowledge about which font handling mechanism you use.
  3857. * IMPORTANT: the `nk_user_font` pointer provided to nuklear has to persist
  3858. * over the complete life time! I know this sucks but it is currently the only
  3859. * way to switch between fonts.
  3860. *
  3861. * ```c
  3862. * float your_text_width_calculation(nk_handle handle, float height, const char *text, int len)
  3863. * {
  3864. * your_font_type *type = handle.ptr;
  3865. * float text_width = ...;
  3866. * return text_width;
  3867. * }
  3868. *
  3869. * struct nk_user_font font;
  3870. * font.userdata.ptr = &your_font_class_or_struct;
  3871. * font.height = your_font_height;
  3872. * font.width = your_text_width_calculation;
  3873. *
  3874. * struct nk_context ctx;
  3875. * nk_init_default(&ctx, &font);
  3876. * ```
  3877. * # Using your own implementation with vertex buffer output
  3878. *
  3879. * While the first approach works fine if you don't want to use the optional
  3880. * vertex buffer output it is not enough if you do. To get font handling working
  3881. * for these cases you have to provide two additional parameters inside the
  3882. * `nk_user_font`. First a texture atlas handle used to draw text as subimages
  3883. * of a bigger font atlas texture and a callback to query a character's glyph
  3884. * information (offset, size, ...). So it is still possible to provide your own
  3885. * font and use the vertex buffer output.
  3886. *
  3887. * ```c
  3888. * float your_text_width_calculation(nk_handle handle, float height, const char *text, int len)
  3889. * {
  3890. * your_font_type *type = handle.ptr;
  3891. * float text_width = ...;
  3892. * return text_width;
  3893. * }
  3894. * void query_your_font_glyph(nk_handle handle, float font_height, struct nk_user_font_glyph *glyph, nk_rune codepoint, nk_rune next_codepoint)
  3895. * {
  3896. * your_font_type *type = handle.ptr;
  3897. * glyph.width = ...;
  3898. * glyph.height = ...;
  3899. * glyph.xadvance = ...;
  3900. * glyph.uv[0].x = ...;
  3901. * glyph.uv[0].y = ...;
  3902. * glyph.uv[1].x = ...;
  3903. * glyph.uv[1].y = ...;
  3904. * glyph.offset.x = ...;
  3905. * glyph.offset.y = ...;
  3906. * }
  3907. *
  3908. * struct nk_user_font font;
  3909. * font.userdata.ptr = &your_font_class_or_struct;
  3910. * font.height = your_font_height;
  3911. * font.width = your_text_width_calculation;
  3912. * font.query = query_your_font_glyph;
  3913. * font.texture.id = your_font_texture;
  3914. *
  3915. * struct nk_context ctx;
  3916. * nk_init_default(&ctx, &font);
  3917. * ```
  3918. *
  3919. * # Nuklear font baker
  3920. *
  3921. * The final approach if you do not have a font handling functionality or don't
  3922. * want to use it in this library is by using the optional font baker.
  3923. * The font baker APIs can be used to create a font plus font atlas texture
  3924. * and can be used with or without the vertex buffer output.
  3925. *
  3926. * It still uses the `nk_user_font` struct and the two different approaches
  3927. * previously stated still work. The font baker is not located inside
  3928. * `nk_context` like all other systems since it can be understood as more of
  3929. * an extension to nuklear and does not really depend on any `nk_context` state.
  3930. *
  3931. * Font baker need to be initialized first by one of the nk_font_atlas_init_xxx
  3932. * functions. If you don't care about memory just call the default version
  3933. * `nk_font_atlas_init_default` which will allocate all memory from the standard library.
  3934. * If you want to control memory allocation but you don't care if the allocated
  3935. * memory is temporary and therefore can be freed directly after the baking process
  3936. * is over or permanent you can call `nk_font_atlas_init`.
  3937. *
  3938. * After successfully initializing the font baker you can add Truetype(.ttf) fonts from
  3939. * different sources like memory or from file by calling one of the `nk_font_atlas_add_xxx`.
  3940. * functions. Adding font will permanently store each font, font config and ttf memory block(!)
  3941. * inside the font atlas and allows to reuse the font atlas. If you don't want to reuse
  3942. * the font baker by for example adding additional fonts you can call
  3943. * `nk_font_atlas_cleanup` after the baking process is over (after calling nk_font_atlas_end).
  3944. *
  3945. * As soon as you added all fonts you wanted you can now start the baking process
  3946. * for every selected glyph to image by calling `nk_font_atlas_bake`.
  3947. * The baking process returns image memory, width and height which can be used to
  3948. * either create your own image object or upload it to any graphics library.
  3949. * No matter which case you finally have to call `nk_font_atlas_end` which
  3950. * will free all temporary memory including the font atlas image so make sure
  3951. * you created our texture beforehand. `nk_font_atlas_end` requires a handle
  3952. * to your font texture or object and optionally fills a `struct nk_draw_null_texture`
  3953. * which can be used for the optional vertex output. If you don't want it just
  3954. * set the argument to `NULL`.
  3955. *
  3956. * At this point you are done and if you don't want to reuse the font atlas you
  3957. * can call `nk_font_atlas_cleanup` to free all truetype blobs and configuration
  3958. * memory. Finally if you don't use the font atlas and any of it's fonts anymore
  3959. * you need to call `nk_font_atlas_clear` to free all memory still being used.
  3960. *
  3961. * ```c
  3962. * struct nk_font_atlas atlas;
  3963. * nk_font_atlas_init_default(&atlas);
  3964. * nk_font_atlas_begin(&atlas);
  3965. * nk_font *font = nk_font_atlas_add_from_file(&atlas, "Path/To/Your/TTF_Font.ttf", 13, 0);
  3966. * nk_font *font2 = nk_font_atlas_add_from_file(&atlas, "Path/To/Your/TTF_Font2.ttf", 16, 0);
  3967. * const void* img = nk_font_atlas_bake(&atlas, &img_width, &img_height, NK_FONT_ATLAS_RGBA32);
  3968. * nk_font_atlas_end(&atlas, nk_handle_id(texture), 0);
  3969. *
  3970. * struct nk_context ctx;
  3971. * nk_init_default(&ctx, &font->handle);
  3972. * while (1) {
  3973. *
  3974. * }
  3975. * nk_font_atlas_clear(&atlas);
  3976. * ```
  3977. * The font baker API is probably the most complex API inside this library and
  3978. * I would suggest reading some of my examples `example/` to get a grip on how
  3979. * to use the font atlas. There are a number of details I left out. For example
  3980. * how to merge fonts, configure a font with `nk_font_config` to use other languages,
  3981. * use another texture coordinate format and a lot more:
  3982. *
  3983. * ```c
  3984. * struct nk_font_config cfg = nk_font_config(font_pixel_height);
  3985. * cfg.merge_mode = nk_false or nk_true;
  3986. * cfg.range = nk_font_korean_glyph_ranges();
  3987. * cfg.coord_type = NK_COORD_PIXEL;
  3988. * nk_font *font = nk_font_atlas_add_from_file(&atlas, "Path/To/Your/TTF_Font.ttf", 13, &cfg);
  3989. * ```
  3990. */
  3991. struct nk_user_font_glyph;
  3992. typedef float(*nk_text_width_f)(nk_handle, float h, const char*, int len);
  3993. typedef void(*nk_query_font_glyph_f)(nk_handle handle, float font_height,
  3994. struct nk_user_font_glyph *glyph,
  3995. nk_rune codepoint, nk_rune next_codepoint);
  3996. #if defined(NK_INCLUDE_VERTEX_BUFFER_OUTPUT) || defined(NK_INCLUDE_SOFTWARE_FONT)
  3997. struct nk_user_font_glyph {
  3998. struct nk_vec2 uv[2]; /**!< texture coordinates */
  3999. struct nk_vec2 offset; /**!< offset between top left and glyph */
  4000. float width, height; /**!< size of the glyph */
  4001. float xadvance; /**!< offset to the next glyph */
  4002. };
  4003. #endif
  4004. struct nk_user_font {
  4005. nk_handle userdata; /**!< user provided font handle */
  4006. float height; /**!< max height of the font */
  4007. nk_text_width_f width; /**!< font string width in pixel callback */
  4008. #ifdef NK_INCLUDE_VERTEX_BUFFER_OUTPUT
  4009. nk_query_font_glyph_f query; /**!< font glyph callback to query drawing info */
  4010. nk_handle texture; /**!< texture handle to the used font atlas or texture */
  4011. #endif
  4012. };
  4013. #ifdef NK_INCLUDE_FONT_BAKING
  4014. enum nk_font_coord_type {
  4015. NK_COORD_UV, /**!< texture coordinates inside font glyphs are clamped between 0-1 */
  4016. NK_COORD_PIXEL /**!< texture coordinates inside font glyphs are in absolute pixel */
  4017. };
  4018. struct nk_font;
  4019. struct nk_baked_font {
  4020. float height; /**!< height of the font */
  4021. float ascent; /**!< font glyphs ascent and descent */
  4022. float descent; /**!< font glyphs ascent and descent */
  4023. nk_rune glyph_offset; /**!< glyph array offset inside the font glyph baking output array */
  4024. nk_rune glyph_count; /**!< number of glyphs of this font inside the glyph baking array output */
  4025. const nk_rune *ranges; /**!< font codepoint ranges as pairs of (from/to) and 0 as last element */
  4026. };
  4027. struct nk_font_config {
  4028. struct nk_font_config *next; /**!< NOTE: only used internally */
  4029. void *ttf_blob; /**!< pointer to loaded TTF file memory block. * \note not needed for nk_font_atlas_add_from_memory and nk_font_atlas_add_from_file. */
  4030. nk_size ttf_size; /**!< size of the loaded TTF file memory block * \note not needed for nk_font_atlas_add_from_memory and nk_font_atlas_add_from_file. */
  4031. unsigned char ttf_data_owned_by_atlas; /**!< used inside font atlas: default to: 0*/
  4032. unsigned char merge_mode; /**!< merges this font into the last font */
  4033. unsigned char pixel_snap; /**!< align every character to pixel boundary (if true set oversample (1,1)) */
  4034. unsigned char oversample_v, oversample_h; /**!< rasterize at high quality for sub-pixel position */
  4035. unsigned char padding[3];
  4036. float size; /**!< baked pixel height of the font */
  4037. enum nk_font_coord_type coord_type; /**!< texture coordinate format with either pixel or UV coordinates */
  4038. struct nk_vec2 spacing; /**!< extra pixel spacing between glyphs */
  4039. const nk_rune *range; /**!< list of unicode ranges (2 values per range, zero terminated) */
  4040. struct nk_baked_font *font; /**!< font to setup in the baking process: NOTE: not needed for font atlas */
  4041. nk_rune fallback_glyph; /**!< fallback glyph to use if a given rune is not found */
  4042. struct nk_font_config *n;
  4043. struct nk_font_config *p;
  4044. };
  4045. struct nk_font_glyph {
  4046. nk_rune codepoint;
  4047. float xadvance;
  4048. float x0, y0, x1, y1, w, h;
  4049. float u0, v0, u1, v1;
  4050. };
  4051. struct nk_font {
  4052. struct nk_font *next;
  4053. struct nk_user_font handle;
  4054. struct nk_baked_font info;
  4055. float scale;
  4056. struct nk_font_glyph *glyphs;
  4057. const struct nk_font_glyph *fallback;
  4058. nk_rune fallback_codepoint;
  4059. nk_handle texture;
  4060. struct nk_font_config *config;
  4061. };
  4062. enum nk_font_atlas_format {
  4063. NK_FONT_ATLAS_ALPHA8,
  4064. NK_FONT_ATLAS_RGBA32
  4065. };
  4066. struct nk_font_atlas {
  4067. void *pixel;
  4068. int tex_width;
  4069. int tex_height;
  4070. struct nk_allocator permanent;
  4071. struct nk_allocator temporary;
  4072. struct nk_recti custom;
  4073. struct nk_cursor cursors[NK_CURSOR_COUNT];
  4074. int glyph_count;
  4075. struct nk_font_glyph *glyphs;
  4076. struct nk_font *default_font;
  4077. struct nk_font *fonts;
  4078. struct nk_font_config *config;
  4079. int font_num;
  4080. };
  4081. /** some language glyph codepoint ranges */
  4082. NK_API const nk_rune *nk_font_default_glyph_ranges(void);
  4083. NK_API const nk_rune *nk_font_chinese_glyph_ranges(void);
  4084. NK_API const nk_rune *nk_font_cyrillic_glyph_ranges(void);
  4085. NK_API const nk_rune *nk_font_korean_glyph_ranges(void);
  4086. #ifdef NK_INCLUDE_DEFAULT_ALLOCATOR
  4087. NK_API void nk_font_atlas_init_default(struct nk_font_atlas*);
  4088. #endif
  4089. NK_API void nk_font_atlas_init(struct nk_font_atlas*, const struct nk_allocator*);
  4090. NK_API void nk_font_atlas_init_custom(struct nk_font_atlas*, const struct nk_allocator *persistent, const struct nk_allocator *transient);
  4091. NK_API void nk_font_atlas_begin(struct nk_font_atlas*);
  4092. NK_API struct nk_font_config nk_font_config(float pixel_height);
  4093. NK_API struct nk_font *nk_font_atlas_add(struct nk_font_atlas*, const struct nk_font_config*);
  4094. #ifdef NK_INCLUDE_DEFAULT_FONT
  4095. NK_API struct nk_font* nk_font_atlas_add_default(struct nk_font_atlas*, float height, const struct nk_font_config*);
  4096. #endif
  4097. 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);
  4098. #ifdef NK_INCLUDE_STANDARD_IO
  4099. 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*);
  4100. #endif
  4101. 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*);
  4102. 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);
  4103. NK_API const void* nk_font_atlas_bake(struct nk_font_atlas*, int *width, int *height, enum nk_font_atlas_format);
  4104. NK_API void nk_font_atlas_end(struct nk_font_atlas*, nk_handle tex, struct nk_draw_null_texture*);
  4105. NK_API const struct nk_font_glyph* nk_font_find_glyph(const struct nk_font*, nk_rune unicode);
  4106. NK_API void nk_font_atlas_cleanup(struct nk_font_atlas *atlas);
  4107. NK_API void nk_font_atlas_clear(struct nk_font_atlas*);
  4108. #endif
  4109. /** ==============================================================
  4110. *
  4111. * MEMORY BUFFER
  4112. *
  4113. * ===============================================================*/
  4114. /**
  4115. * \page Memory Buffer
  4116. * A basic (double)-buffer with linear allocation and resetting as only
  4117. * freeing policy. The buffer's main purpose is to control all memory management
  4118. * inside the GUI toolkit and still leave memory control as much as possible in
  4119. * the hand of the user while also making sure the library is easy to use if
  4120. * not as much control is needed.
  4121. * In general all memory inside this library can be provided from the user in
  4122. * three different ways.
  4123. *
  4124. * The first way and the one providing most control is by just passing a fixed
  4125. * size memory block. In this case all control lies in the hand of the user
  4126. * since he can exactly control where the memory comes from and how much memory
  4127. * the library should consume. Of course using the fixed size API removes the
  4128. * ability to automatically resize a buffer if not enough memory is provided so
  4129. * you have to take over the resizing. While being a fixed sized buffer sounds
  4130. * quite limiting, it is very effective in this library since the actual memory
  4131. * consumption is quite stable and has a fixed upper bound for a lot of cases.
  4132. *
  4133. * If you don't want to think about how much memory the library should allocate
  4134. * at all time or have a very dynamic UI with unpredictable memory consumption
  4135. * habits but still want control over memory allocation you can use the dynamic
  4136. * allocator based API. The allocator consists of two callbacks for allocating
  4137. * and freeing memory and optional userdata so you can plugin your own allocator.
  4138. *
  4139. * The final and easiest way can be used by defining
  4140. * NK_INCLUDE_DEFAULT_ALLOCATOR which uses the standard library memory
  4141. * allocation functions malloc and free and takes over complete control over
  4142. * memory in this library.
  4143. */
  4144. struct nk_memory_status {
  4145. void *memory;
  4146. unsigned int type;
  4147. nk_size size;
  4148. nk_size allocated;
  4149. nk_size needed;
  4150. nk_size calls;
  4151. };
  4152. enum nk_allocation_type {
  4153. NK_BUFFER_FIXED,
  4154. NK_BUFFER_DYNAMIC
  4155. };
  4156. enum nk_buffer_allocation_type {
  4157. NK_BUFFER_FRONT,
  4158. NK_BUFFER_BACK,
  4159. NK_BUFFER_MAX
  4160. };
  4161. struct nk_buffer_marker {
  4162. nk_bool active;
  4163. nk_size offset;
  4164. };
  4165. struct nk_memory {void *ptr;nk_size size;};
  4166. struct nk_buffer {
  4167. struct nk_buffer_marker marker[NK_BUFFER_MAX]; /**!< buffer marker to free a buffer to a certain offset */
  4168. struct nk_allocator pool; /**!< allocator callback for dynamic buffers */
  4169. enum nk_allocation_type type; /**!< memory management type */
  4170. struct nk_memory memory; /**!< memory and size of the current memory block */
  4171. float grow_factor; /**!< growing factor for dynamic memory management */
  4172. nk_size allocated; /**!< total amount of memory allocated */
  4173. nk_size needed; /**!< totally consumed memory given that enough memory is present */
  4174. nk_size calls; /**!< number of allocation calls */
  4175. nk_size size; /**!< current size of the buffer */
  4176. };
  4177. #ifdef NK_INCLUDE_DEFAULT_ALLOCATOR
  4178. NK_API void nk_buffer_init_default(struct nk_buffer*);
  4179. #endif
  4180. NK_API void nk_buffer_init(struct nk_buffer*, const struct nk_allocator*, nk_size size);
  4181. NK_API void nk_buffer_init_fixed(struct nk_buffer*, void *memory, nk_size size);
  4182. NK_API void nk_buffer_info(struct nk_memory_status*, const struct nk_buffer*);
  4183. NK_API void nk_buffer_push(struct nk_buffer*, enum nk_buffer_allocation_type type, const void *memory, nk_size size, nk_size align);
  4184. NK_API void nk_buffer_mark(struct nk_buffer*, enum nk_buffer_allocation_type type);
  4185. NK_API void nk_buffer_reset(struct nk_buffer*, enum nk_buffer_allocation_type type);
  4186. NK_API void nk_buffer_clear(struct nk_buffer*);
  4187. NK_API void nk_buffer_free(struct nk_buffer*);
  4188. NK_API void *nk_buffer_memory(struct nk_buffer*);
  4189. NK_API const void *nk_buffer_memory_const(const struct nk_buffer*);
  4190. NK_API nk_size nk_buffer_total(const struct nk_buffer*);
  4191. /** ==============================================================
  4192. *
  4193. * STRING
  4194. *
  4195. * ===============================================================*/
  4196. /** Basic string buffer which is only used in context with the text editor
  4197. * to manage and manipulate dynamic or fixed size string content. This is _NOT_
  4198. * the default string handling method. The only instance you should have any contact
  4199. * with this API is if you interact with an `nk_text_edit` object inside one of the
  4200. * copy and paste functions and even there only for more advanced cases. */
  4201. struct nk_str {
  4202. struct nk_buffer buffer;
  4203. int len; /**!< in codepoints/runes/glyphs */
  4204. };
  4205. #ifdef NK_INCLUDE_DEFAULT_ALLOCATOR
  4206. NK_API void nk_str_init_default(struct nk_str*);
  4207. #endif
  4208. NK_API void nk_str_init(struct nk_str*, const struct nk_allocator*, nk_size size);
  4209. NK_API void nk_str_init_fixed(struct nk_str*, void *memory, nk_size size);
  4210. NK_API void nk_str_clear(struct nk_str*);
  4211. NK_API void nk_str_free(struct nk_str*);
  4212. NK_API int nk_str_append_text_char(struct nk_str*, const char*, int);
  4213. NK_API int nk_str_append_str_char(struct nk_str*, const char*);
  4214. NK_API int nk_str_append_text_utf8(struct nk_str*, const char*, int);
  4215. NK_API int nk_str_append_str_utf8(struct nk_str*, const char*);
  4216. NK_API int nk_str_append_text_runes(struct nk_str*, const nk_rune*, int);
  4217. NK_API int nk_str_append_str_runes(struct nk_str*, const nk_rune*);
  4218. NK_API int nk_str_insert_at_char(struct nk_str*, int pos, const char*, int);
  4219. NK_API int nk_str_insert_at_rune(struct nk_str*, int pos, const char*, int);
  4220. NK_API int nk_str_insert_text_char(struct nk_str*, int pos, const char*, int);
  4221. NK_API int nk_str_insert_str_char(struct nk_str*, int pos, const char*);
  4222. NK_API int nk_str_insert_text_utf8(struct nk_str*, int pos, const char*, int);
  4223. NK_API int nk_str_insert_str_utf8(struct nk_str*, int pos, const char*);
  4224. NK_API int nk_str_insert_text_runes(struct nk_str*, int pos, const nk_rune*, int);
  4225. NK_API int nk_str_insert_str_runes(struct nk_str*, int pos, const nk_rune*);
  4226. NK_API void nk_str_remove_chars(struct nk_str*, int len);
  4227. NK_API void nk_str_remove_runes(struct nk_str *str, int len);
  4228. NK_API void nk_str_delete_chars(struct nk_str*, int pos, int len);
  4229. NK_API void nk_str_delete_runes(struct nk_str*, int pos, int len);
  4230. NK_API char *nk_str_at_char(struct nk_str*, int pos);
  4231. NK_API char *nk_str_at_rune(struct nk_str*, int pos, nk_rune *unicode, int *len);
  4232. NK_API nk_rune nk_str_rune_at(const struct nk_str*, int pos);
  4233. NK_API const char *nk_str_at_char_const(const struct nk_str*, int pos);
  4234. NK_API const char *nk_str_at_const(const struct nk_str*, int pos, nk_rune *unicode, int *len);
  4235. NK_API char *nk_str_get(struct nk_str*);
  4236. NK_API const char *nk_str_get_const(const struct nk_str*);
  4237. NK_API int nk_str_len(const struct nk_str*);
  4238. NK_API int nk_str_len_char(const struct nk_str*);
  4239. /**===============================================================
  4240. *
  4241. * TEXT EDITOR
  4242. *
  4243. * ===============================================================*/
  4244. /**
  4245. * \page Text Editor
  4246. * Editing text in this library is handled by either `nk_edit_string` or
  4247. * `nk_edit_buffer`. But like almost everything in this library there are multiple
  4248. * ways of doing it and a balance between control and ease of use with memory
  4249. * as well as functionality controlled by flags.
  4250. *
  4251. * This library generally allows three different levels of memory control:
  4252. * First of is the most basic way of just providing a simple char array with
  4253. * string length. This method is probably the easiest way of handling simple
  4254. * user text input. Main upside is complete control over memory while the biggest
  4255. * downside in comparison with the other two approaches is missing undo/redo.
  4256. *
  4257. * For UIs that require undo/redo the second way was created. It is based on
  4258. * a fixed size nk_text_edit struct, which has an internal undo/redo stack.
  4259. * This is mainly useful if you want something more like a text editor but don't want
  4260. * to have a dynamically growing buffer.
  4261. *
  4262. * The final way is using a dynamically growing nk_text_edit struct, which
  4263. * has both a default version if you don't care where memory comes from and an
  4264. * allocator version if you do. While the text editor is quite powerful for its
  4265. * complexity I would not recommend editing gigabytes of data with it.
  4266. * It is rather designed for uses cases which make sense for a GUI library not for
  4267. * an full blown text editor.
  4268. */
  4269. #ifndef NK_TEXTEDIT_UNDOSTATECOUNT
  4270. #define NK_TEXTEDIT_UNDOSTATECOUNT 99
  4271. #endif
  4272. #ifndef NK_TEXTEDIT_UNDOCHARCOUNT
  4273. #define NK_TEXTEDIT_UNDOCHARCOUNT 999
  4274. #endif
  4275. struct nk_text_edit;
  4276. struct nk_clipboard {
  4277. nk_handle userdata;
  4278. nk_plugin_paste paste;
  4279. nk_plugin_copy copy;
  4280. };
  4281. struct nk_text_undo_record {
  4282. int where;
  4283. short insert_length;
  4284. short delete_length;
  4285. short char_storage;
  4286. };
  4287. struct nk_text_undo_state {
  4288. struct nk_text_undo_record undo_rec[NK_TEXTEDIT_UNDOSTATECOUNT];
  4289. nk_rune undo_char[NK_TEXTEDIT_UNDOCHARCOUNT];
  4290. short undo_point;
  4291. short redo_point;
  4292. short undo_char_point;
  4293. short redo_char_point;
  4294. };
  4295. enum nk_text_edit_type {
  4296. NK_TEXT_EDIT_SINGLE_LINE,
  4297. NK_TEXT_EDIT_MULTI_LINE
  4298. };
  4299. enum nk_text_edit_mode {
  4300. NK_TEXT_EDIT_MODE_VIEW,
  4301. NK_TEXT_EDIT_MODE_INSERT,
  4302. NK_TEXT_EDIT_MODE_REPLACE
  4303. };
  4304. struct nk_text_edit {
  4305. struct nk_clipboard clip;
  4306. struct nk_str string;
  4307. nk_plugin_filter filter;
  4308. struct nk_vec2 scrollbar;
  4309. int cursor;
  4310. int select_start;
  4311. int select_end;
  4312. unsigned char mode;
  4313. unsigned char cursor_at_end_of_line;
  4314. unsigned char initialized;
  4315. unsigned char has_preferred_x;
  4316. unsigned char single_line;
  4317. unsigned char active;
  4318. unsigned char padding1;
  4319. float preferred_x;
  4320. struct nk_text_undo_state undo;
  4321. };
  4322. /** filter function */
  4323. NK_API nk_bool nk_filter_default(const struct nk_text_edit*, nk_rune unicode);
  4324. NK_API nk_bool nk_filter_ascii(const struct nk_text_edit*, nk_rune unicode);
  4325. NK_API nk_bool nk_filter_float(const struct nk_text_edit*, nk_rune unicode);
  4326. NK_API nk_bool nk_filter_decimal(const struct nk_text_edit*, nk_rune unicode);
  4327. NK_API nk_bool nk_filter_hex(const struct nk_text_edit*, nk_rune unicode);
  4328. NK_API nk_bool nk_filter_oct(const struct nk_text_edit*, nk_rune unicode);
  4329. NK_API nk_bool nk_filter_binary(const struct nk_text_edit*, nk_rune unicode);
  4330. /** text editor */
  4331. #ifdef NK_INCLUDE_DEFAULT_ALLOCATOR
  4332. NK_API void nk_textedit_init_default(struct nk_text_edit*);
  4333. #endif
  4334. NK_API void nk_textedit_init(struct nk_text_edit*, const struct nk_allocator*, nk_size size);
  4335. NK_API void nk_textedit_init_fixed(struct nk_text_edit*, void *memory, nk_size size);
  4336. NK_API void nk_textedit_free(struct nk_text_edit*);
  4337. NK_API void nk_textedit_text(struct nk_text_edit*, const char*, int total_len);
  4338. NK_API void nk_textedit_delete(struct nk_text_edit*, int where, int len);
  4339. NK_API void nk_textedit_delete_selection(struct nk_text_edit*);
  4340. NK_API void nk_textedit_select_all(struct nk_text_edit*);
  4341. NK_API nk_bool nk_textedit_cut(struct nk_text_edit*);
  4342. NK_API nk_bool nk_textedit_paste(struct nk_text_edit*, char const*, int len);
  4343. NK_API void nk_textedit_undo(struct nk_text_edit*);
  4344. NK_API void nk_textedit_redo(struct nk_text_edit*);
  4345. /* ===============================================================
  4346. *
  4347. * DRAWING
  4348. *
  4349. * ===============================================================*/
  4350. /**
  4351. * \page Drawing
  4352. * This library was designed to be render backend agnostic so it does
  4353. * not draw anything to screen. Instead all drawn shapes, widgets
  4354. * are made of, are buffered into memory and make up a command queue.
  4355. * Each frame therefore fills the command buffer with draw commands
  4356. * that then need to be executed by the user and his own render backend.
  4357. * After that the command buffer needs to be cleared and a new frame can be
  4358. * started. It is probably important to note that the command buffer is the main
  4359. * drawing API and the optional vertex buffer API only takes this format and
  4360. * converts it into a hardware accessible format.
  4361. *
  4362. * To use the command queue to draw your own widgets you can access the
  4363. * command buffer of each window by calling `nk_window_get_canvas` after
  4364. * previously having called `nk_begin`:
  4365. *
  4366. * ```c
  4367. * void draw_red_rectangle_widget(struct nk_context *ctx)
  4368. * {
  4369. * struct nk_command_buffer *canvas;
  4370. * struct nk_input *input = &ctx->input;
  4371. * canvas = nk_window_get_canvas(ctx);
  4372. *
  4373. * struct nk_rect space;
  4374. * enum nk_widget_layout_states state;
  4375. * state = nk_widget(&space, ctx);
  4376. * if (!state) return;
  4377. *
  4378. * if (state != NK_WIDGET_ROM)
  4379. * update_your_widget_by_user_input(...);
  4380. * nk_fill_rect(canvas, space, 0, nk_rgb(255,0,0));
  4381. * }
  4382. *
  4383. * if (nk_begin(...)) {
  4384. * nk_layout_row_dynamic(ctx, 25, 1);
  4385. * draw_red_rectangle_widget(ctx);
  4386. * }
  4387. * nk_end(..)
  4388. *
  4389. * ```
  4390. * Important to know if you want to create your own widgets is the `nk_widget`
  4391. * call. It allocates space on the panel reserved for this widget to be used,
  4392. * but also returns the state of the widget space. If your widget is not seen and does
  4393. * not have to be updated it is '0' and you can just return. If it only has
  4394. * to be drawn the state will be `NK_WIDGET_ROM` otherwise you can do both
  4395. * update and draw your widget. The reason for separating is to only draw and
  4396. * update what is actually necessary which is crucial for performance.
  4397. */
  4398. enum nk_command_type {
  4399. NK_COMMAND_NOP,
  4400. NK_COMMAND_SCISSOR,
  4401. NK_COMMAND_LINE,
  4402. NK_COMMAND_CURVE,
  4403. NK_COMMAND_RECT,
  4404. NK_COMMAND_RECT_FILLED,
  4405. NK_COMMAND_RECT_MULTI_COLOR,
  4406. NK_COMMAND_CIRCLE,
  4407. NK_COMMAND_CIRCLE_FILLED,
  4408. NK_COMMAND_ARC,
  4409. NK_COMMAND_ARC_FILLED,
  4410. NK_COMMAND_TRIANGLE,
  4411. NK_COMMAND_TRIANGLE_FILLED,
  4412. NK_COMMAND_POLYGON,
  4413. NK_COMMAND_POLYGON_FILLED,
  4414. NK_COMMAND_POLYLINE,
  4415. NK_COMMAND_TEXT,
  4416. NK_COMMAND_IMAGE,
  4417. NK_COMMAND_CUSTOM
  4418. };
  4419. /** command base and header of every command inside the buffer */
  4420. struct nk_command {
  4421. enum nk_command_type type;
  4422. nk_size next;
  4423. #ifdef NK_INCLUDE_COMMAND_USERDATA
  4424. nk_handle userdata;
  4425. #endif
  4426. };
  4427. struct nk_command_scissor {
  4428. struct nk_command header;
  4429. short x, y;
  4430. unsigned short w, h;
  4431. };
  4432. struct nk_command_line {
  4433. struct nk_command header;
  4434. unsigned short line_thickness;
  4435. struct nk_vec2i begin;
  4436. struct nk_vec2i end;
  4437. struct nk_color color;
  4438. };
  4439. struct nk_command_curve {
  4440. struct nk_command header;
  4441. unsigned short line_thickness;
  4442. struct nk_vec2i begin;
  4443. struct nk_vec2i end;
  4444. struct nk_vec2i ctrl[2];
  4445. struct nk_color color;
  4446. };
  4447. struct nk_command_rect {
  4448. struct nk_command header;
  4449. unsigned short rounding;
  4450. unsigned short line_thickness;
  4451. short x, y;
  4452. unsigned short w, h;
  4453. struct nk_color color;
  4454. };
  4455. struct nk_command_rect_filled {
  4456. struct nk_command header;
  4457. unsigned short rounding;
  4458. short x, y;
  4459. unsigned short w, h;
  4460. struct nk_color color;
  4461. };
  4462. struct nk_command_rect_multi_color {
  4463. struct nk_command header;
  4464. short x, y;
  4465. unsigned short w, h;
  4466. struct nk_color left;
  4467. struct nk_color top;
  4468. struct nk_color bottom;
  4469. struct nk_color right;
  4470. };
  4471. struct nk_command_triangle {
  4472. struct nk_command header;
  4473. unsigned short line_thickness;
  4474. struct nk_vec2i a;
  4475. struct nk_vec2i b;
  4476. struct nk_vec2i c;
  4477. struct nk_color color;
  4478. };
  4479. struct nk_command_triangle_filled {
  4480. struct nk_command header;
  4481. struct nk_vec2i a;
  4482. struct nk_vec2i b;
  4483. struct nk_vec2i c;
  4484. struct nk_color color;
  4485. };
  4486. struct nk_command_circle {
  4487. struct nk_command header;
  4488. short x, y;
  4489. unsigned short line_thickness;
  4490. unsigned short w, h;
  4491. struct nk_color color;
  4492. };
  4493. struct nk_command_circle_filled {
  4494. struct nk_command header;
  4495. short x, y;
  4496. unsigned short w, h;
  4497. struct nk_color color;
  4498. };
  4499. struct nk_command_arc {
  4500. struct nk_command header;
  4501. short cx, cy;
  4502. unsigned short r;
  4503. unsigned short line_thickness;
  4504. float a[2];
  4505. struct nk_color color;
  4506. };
  4507. struct nk_command_arc_filled {
  4508. struct nk_command header;
  4509. short cx, cy;
  4510. unsigned short r;
  4511. float a[2];
  4512. struct nk_color color;
  4513. };
  4514. struct nk_command_polygon {
  4515. struct nk_command header;
  4516. struct nk_color color;
  4517. unsigned short line_thickness;
  4518. unsigned short point_count;
  4519. struct nk_vec2i points[1];
  4520. };
  4521. struct nk_command_polygon_filled {
  4522. struct nk_command header;
  4523. struct nk_color color;
  4524. unsigned short point_count;
  4525. struct nk_vec2i points[1];
  4526. };
  4527. struct nk_command_polyline {
  4528. struct nk_command header;
  4529. struct nk_color color;
  4530. unsigned short line_thickness;
  4531. unsigned short point_count;
  4532. struct nk_vec2i points[1];
  4533. };
  4534. struct nk_command_image {
  4535. struct nk_command header;
  4536. short x, y;
  4537. unsigned short w, h;
  4538. struct nk_image img;
  4539. struct nk_color col;
  4540. };
  4541. typedef void (*nk_command_custom_callback)(void *canvas, short x,short y,
  4542. unsigned short w, unsigned short h, nk_handle callback_data);
  4543. struct nk_command_custom {
  4544. struct nk_command header;
  4545. short x, y;
  4546. unsigned short w, h;
  4547. nk_handle callback_data;
  4548. nk_command_custom_callback callback;
  4549. };
  4550. struct nk_command_text {
  4551. struct nk_command header;
  4552. const struct nk_user_font *font;
  4553. struct nk_color background;
  4554. struct nk_color foreground;
  4555. short x, y;
  4556. unsigned short w, h;
  4557. float height;
  4558. int length;
  4559. char string[2];
  4560. };
  4561. enum nk_command_clipping {
  4562. NK_CLIPPING_OFF = nk_false,
  4563. NK_CLIPPING_ON = nk_true
  4564. };
  4565. struct nk_command_buffer {
  4566. struct nk_buffer *base;
  4567. struct nk_rect clip;
  4568. int use_clipping;
  4569. nk_handle userdata;
  4570. nk_size begin, end, last;
  4571. };
  4572. /** shape outlines */
  4573. 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);
  4574. NK_API void nk_stroke_curve(struct nk_command_buffer*, float, float, float, float, float, float, float, float, float line_thickness, struct nk_color);
  4575. NK_API void nk_stroke_rect(struct nk_command_buffer*, struct nk_rect, float rounding, float line_thickness, struct nk_color);
  4576. NK_API void nk_stroke_circle(struct nk_command_buffer*, struct nk_rect, float line_thickness, struct nk_color);
  4577. 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);
  4578. NK_API void nk_stroke_triangle(struct nk_command_buffer*, float, float, float, float, float, float, float line_thichness, struct nk_color);
  4579. NK_API void nk_stroke_polyline(struct nk_command_buffer*, const float *points, int point_count, float line_thickness, struct nk_color col);
  4580. NK_API void nk_stroke_polygon(struct nk_command_buffer*, const float *points, int point_count, float line_thickness, struct nk_color);
  4581. /** filled shades */
  4582. NK_API void nk_fill_rect(struct nk_command_buffer*, struct nk_rect, float rounding, struct nk_color);
  4583. 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);
  4584. NK_API void nk_fill_circle(struct nk_command_buffer*, struct nk_rect, struct nk_color);
  4585. 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);
  4586. 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);
  4587. NK_API void nk_fill_polygon(struct nk_command_buffer*, const float *points, int point_count, struct nk_color);
  4588. /** misc */
  4589. NK_API void nk_draw_image(struct nk_command_buffer*, struct nk_rect, const struct nk_image*, struct nk_color);
  4590. NK_API void nk_draw_nine_slice(struct nk_command_buffer*, struct nk_rect, const struct nk_nine_slice*, struct nk_color);
  4591. 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);
  4592. NK_API void nk_push_scissor(struct nk_command_buffer*, struct nk_rect);
  4593. NK_API void nk_push_custom(struct nk_command_buffer*, struct nk_rect, nk_command_custom_callback, nk_handle usr);
  4594. /* ===============================================================
  4595. *
  4596. * INPUT
  4597. *
  4598. * ===============================================================*/
  4599. struct nk_mouse_button {
  4600. nk_bool down;
  4601. unsigned int clicked;
  4602. struct nk_vec2 clicked_pos;
  4603. };
  4604. struct nk_mouse {
  4605. struct nk_mouse_button buttons[NK_BUTTON_MAX];
  4606. struct nk_vec2 pos;
  4607. #ifdef NK_BUTTON_TRIGGER_ON_RELEASE
  4608. struct nk_vec2 down_pos;
  4609. #endif
  4610. struct nk_vec2 prev;
  4611. struct nk_vec2 delta;
  4612. struct nk_vec2 scroll_delta;
  4613. unsigned char grab;
  4614. unsigned char grabbed;
  4615. unsigned char ungrab;
  4616. };
  4617. struct nk_key {
  4618. nk_bool down;
  4619. unsigned int clicked;
  4620. };
  4621. struct nk_keyboard {
  4622. struct nk_key keys[NK_KEY_MAX];
  4623. char text[NK_INPUT_MAX];
  4624. int text_len;
  4625. };
  4626. struct nk_input {
  4627. struct nk_keyboard keyboard;
  4628. struct nk_mouse mouse;
  4629. };
  4630. NK_API nk_bool nk_input_has_mouse_click(const struct nk_input*, enum nk_buttons);
  4631. NK_API nk_bool nk_input_has_mouse_click_in_rect(const struct nk_input*, enum nk_buttons, struct nk_rect);
  4632. NK_API nk_bool nk_input_has_mouse_click_in_button_rect(const struct nk_input*, enum nk_buttons, struct nk_rect);
  4633. 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);
  4634. NK_API nk_bool nk_input_is_mouse_click_in_rect(const struct nk_input*, enum nk_buttons, struct nk_rect);
  4635. 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);
  4636. NK_API nk_bool nk_input_any_mouse_click_in_rect(const struct nk_input*, struct nk_rect);
  4637. NK_API nk_bool nk_input_is_mouse_prev_hovering_rect(const struct nk_input*, struct nk_rect);
  4638. NK_API nk_bool nk_input_is_mouse_hovering_rect(const struct nk_input*, struct nk_rect);
  4639. NK_API nk_bool nk_input_mouse_clicked(const struct nk_input*, enum nk_buttons, struct nk_rect);
  4640. NK_API nk_bool nk_input_is_mouse_down(const struct nk_input*, enum nk_buttons);
  4641. NK_API nk_bool nk_input_is_mouse_pressed(const struct nk_input*, enum nk_buttons);
  4642. NK_API nk_bool nk_input_is_mouse_released(const struct nk_input*, enum nk_buttons);
  4643. NK_API nk_bool nk_input_is_key_pressed(const struct nk_input*, enum nk_keys);
  4644. NK_API nk_bool nk_input_is_key_released(const struct nk_input*, enum nk_keys);
  4645. NK_API nk_bool nk_input_is_key_down(const struct nk_input*, enum nk_keys);
  4646. /* ===============================================================
  4647. *
  4648. * DRAW LIST
  4649. *
  4650. * ===============================================================*/
  4651. #ifdef NK_INCLUDE_VERTEX_BUFFER_OUTPUT
  4652. /**
  4653. * \page "Draw List"
  4654. * The optional vertex buffer draw list provides a 2D drawing context
  4655. * with antialiasing functionality which takes basic filled or outlined shapes
  4656. * or a path and outputs vertexes, elements and draw commands.
  4657. * The actual draw list API is not required to be used directly while using this
  4658. * library since converting the default library draw command output is done by
  4659. * just calling `nk_convert` but I decided to still make this library accessible
  4660. * since it can be useful.
  4661. *
  4662. * The draw list is based on a path buffering and polygon and polyline
  4663. * rendering API which allows a lot of ways to draw 2D content to screen.
  4664. * In fact it is probably more powerful than needed but allows even more crazy
  4665. * things than this library provides by default.
  4666. */
  4667. #ifdef NK_UINT_DRAW_INDEX
  4668. typedef nk_uint nk_draw_index;
  4669. #else
  4670. typedef nk_ushort nk_draw_index;
  4671. #endif
  4672. enum nk_draw_list_stroke {
  4673. NK_STROKE_OPEN = nk_false, /***< build up path has no connection back to the beginning */
  4674. NK_STROKE_CLOSED = nk_true /***< build up path has a connection back to the beginning */
  4675. };
  4676. enum nk_draw_vertex_layout_attribute {
  4677. NK_VERTEX_POSITION,
  4678. NK_VERTEX_COLOR,
  4679. NK_VERTEX_TEXCOORD,
  4680. NK_VERTEX_ATTRIBUTE_COUNT
  4681. };
  4682. enum nk_draw_vertex_layout_format {
  4683. NK_FORMAT_SCHAR,
  4684. NK_FORMAT_SSHORT,
  4685. NK_FORMAT_SINT,
  4686. NK_FORMAT_UCHAR,
  4687. NK_FORMAT_USHORT,
  4688. NK_FORMAT_UINT,
  4689. NK_FORMAT_FLOAT,
  4690. NK_FORMAT_DOUBLE,
  4691. NK_FORMAT_COLOR_BEGIN,
  4692. NK_FORMAT_R8G8B8 = NK_FORMAT_COLOR_BEGIN,
  4693. NK_FORMAT_R16G15B16,
  4694. NK_FORMAT_R32G32B32,
  4695. NK_FORMAT_R8G8B8A8,
  4696. NK_FORMAT_B8G8R8A8,
  4697. NK_FORMAT_R16G15B16A16,
  4698. NK_FORMAT_R32G32B32A32,
  4699. NK_FORMAT_R32G32B32A32_FLOAT,
  4700. NK_FORMAT_R32G32B32A32_DOUBLE,
  4701. NK_FORMAT_RGB32,
  4702. NK_FORMAT_RGBA32,
  4703. NK_FORMAT_COLOR_END = NK_FORMAT_RGBA32,
  4704. NK_FORMAT_COUNT
  4705. };
  4706. #define NK_VERTEX_LAYOUT_END NK_VERTEX_ATTRIBUTE_COUNT,NK_FORMAT_COUNT,0
  4707. struct nk_draw_vertex_layout_element {
  4708. enum nk_draw_vertex_layout_attribute attribute;
  4709. enum nk_draw_vertex_layout_format format;
  4710. nk_size offset;
  4711. };
  4712. struct nk_draw_command {
  4713. unsigned int elem_count; /**< number of elements in the current draw batch */
  4714. struct nk_rect clip_rect; /**< current screen clipping rectangle */
  4715. nk_handle texture; /**< current texture to set */
  4716. #ifdef NK_INCLUDE_COMMAND_USERDATA
  4717. nk_handle userdata;
  4718. #endif
  4719. };
  4720. struct nk_draw_list {
  4721. struct nk_rect clip_rect;
  4722. struct nk_vec2 circle_vtx[12];
  4723. struct nk_convert_config config;
  4724. struct nk_buffer *buffer;
  4725. struct nk_buffer *vertices;
  4726. struct nk_buffer *elements;
  4727. unsigned int element_count;
  4728. unsigned int vertex_count;
  4729. unsigned int cmd_count;
  4730. nk_size cmd_offset;
  4731. unsigned int path_count;
  4732. unsigned int path_offset;
  4733. enum nk_anti_aliasing line_AA;
  4734. enum nk_anti_aliasing shape_AA;
  4735. #ifdef NK_INCLUDE_COMMAND_USERDATA
  4736. nk_handle userdata;
  4737. #endif
  4738. };
  4739. /* draw list */
  4740. NK_API void nk_draw_list_init(struct nk_draw_list*);
  4741. 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);
  4742. /* drawing */
  4743. #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))
  4744. NK_API const struct nk_draw_command* nk__draw_list_begin(const struct nk_draw_list*, const struct nk_buffer*);
  4745. 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*);
  4746. NK_API const struct nk_draw_command* nk__draw_list_end(const struct nk_draw_list*, const struct nk_buffer*);
  4747. /* path */
  4748. NK_API void nk_draw_list_path_clear(struct nk_draw_list*);
  4749. NK_API void nk_draw_list_path_line_to(struct nk_draw_list*, struct nk_vec2 pos);
  4750. 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);
  4751. 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);
  4752. NK_API void nk_draw_list_path_rect_to(struct nk_draw_list*, struct nk_vec2 a, struct nk_vec2 b, float rounding);
  4753. 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);
  4754. NK_API void nk_draw_list_path_fill(struct nk_draw_list*, struct nk_color);
  4755. NK_API void nk_draw_list_path_stroke(struct nk_draw_list*, struct nk_color, enum nk_draw_list_stroke closed, float thickness);
  4756. /* stroke */
  4757. 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);
  4758. NK_API void nk_draw_list_stroke_rect(struct nk_draw_list*, struct nk_rect rect, struct nk_color, float rounding, float thickness);
  4759. 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);
  4760. 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);
  4761. 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);
  4762. 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);
  4763. /* fill */
  4764. NK_API void nk_draw_list_fill_rect(struct nk_draw_list*, struct nk_rect rect, struct nk_color, float rounding);
  4765. 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);
  4766. 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);
  4767. 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);
  4768. 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);
  4769. /* misc */
  4770. NK_API void nk_draw_list_add_image(struct nk_draw_list*, struct nk_image texture, struct nk_rect rect, struct nk_color);
  4771. 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);
  4772. #ifdef NK_INCLUDE_COMMAND_USERDATA
  4773. NK_API void nk_draw_list_push_userdata(struct nk_draw_list*, nk_handle userdata);
  4774. #endif
  4775. #endif
  4776. /* ===============================================================
  4777. *
  4778. * GUI
  4779. *
  4780. * ===============================================================*/
  4781. enum nk_style_item_type {
  4782. NK_STYLE_ITEM_COLOR,
  4783. NK_STYLE_ITEM_IMAGE,
  4784. NK_STYLE_ITEM_NINE_SLICE
  4785. };
  4786. union nk_style_item_data {
  4787. struct nk_color color;
  4788. struct nk_image image;
  4789. struct nk_nine_slice slice;
  4790. };
  4791. struct nk_style_item {
  4792. enum nk_style_item_type type;
  4793. union nk_style_item_data data;
  4794. };
  4795. struct nk_style_text {
  4796. struct nk_color color;
  4797. struct nk_vec2 padding;
  4798. float color_factor;
  4799. float disabled_factor;
  4800. };
  4801. struct nk_style_button {
  4802. /* background */
  4803. struct nk_style_item normal;
  4804. struct nk_style_item hover;
  4805. struct nk_style_item active;
  4806. struct nk_color border_color;
  4807. float color_factor_background;
  4808. /* text */
  4809. struct nk_color text_background;
  4810. struct nk_color text_normal;
  4811. struct nk_color text_hover;
  4812. struct nk_color text_active;
  4813. nk_flags text_alignment;
  4814. float color_factor_text;
  4815. /* properties */
  4816. float border;
  4817. float rounding;
  4818. struct nk_vec2 padding;
  4819. struct nk_vec2 image_padding;
  4820. struct nk_vec2 touch_padding;
  4821. float disabled_factor;
  4822. /* optional user callbacks */
  4823. nk_handle userdata;
  4824. void(*draw_begin)(struct nk_command_buffer*, nk_handle userdata);
  4825. void(*draw_end)(struct nk_command_buffer*, nk_handle userdata);
  4826. };
  4827. struct nk_style_toggle {
  4828. /* background */
  4829. struct nk_style_item normal;
  4830. struct nk_style_item hover;
  4831. struct nk_style_item active;
  4832. struct nk_color border_color;
  4833. /* cursor */
  4834. struct nk_style_item cursor_normal;
  4835. struct nk_style_item cursor_hover;
  4836. /* text */
  4837. struct nk_color text_normal;
  4838. struct nk_color text_hover;
  4839. struct nk_color text_active;
  4840. struct nk_color text_background;
  4841. nk_flags text_alignment;
  4842. /* properties */
  4843. struct nk_vec2 padding;
  4844. struct nk_vec2 touch_padding;
  4845. float spacing;
  4846. float border;
  4847. float color_factor;
  4848. float disabled_factor;
  4849. /* optional user callbacks */
  4850. nk_handle userdata;
  4851. void(*draw_begin)(struct nk_command_buffer*, nk_handle);
  4852. void(*draw_end)(struct nk_command_buffer*, nk_handle);
  4853. };
  4854. struct nk_style_selectable {
  4855. /* background (inactive) */
  4856. struct nk_style_item normal;
  4857. struct nk_style_item hover;
  4858. struct nk_style_item pressed;
  4859. /* background (active) */
  4860. struct nk_style_item normal_active;
  4861. struct nk_style_item hover_active;
  4862. struct nk_style_item pressed_active;
  4863. /* text color (inactive) */
  4864. struct nk_color text_normal;
  4865. struct nk_color text_hover;
  4866. struct nk_color text_pressed;
  4867. /* text color (active) */
  4868. struct nk_color text_normal_active;
  4869. struct nk_color text_hover_active;
  4870. struct nk_color text_pressed_active;
  4871. struct nk_color text_background;
  4872. nk_flags text_alignment;
  4873. /* properties */
  4874. float rounding;
  4875. struct nk_vec2 padding;
  4876. struct nk_vec2 touch_padding;
  4877. struct nk_vec2 image_padding;
  4878. float color_factor;
  4879. float disabled_factor;
  4880. /* optional user callbacks */
  4881. nk_handle userdata;
  4882. void(*draw_begin)(struct nk_command_buffer*, nk_handle);
  4883. void(*draw_end)(struct nk_command_buffer*, nk_handle);
  4884. };
  4885. struct nk_style_slider {
  4886. /* background */
  4887. struct nk_style_item normal;
  4888. struct nk_style_item hover;
  4889. struct nk_style_item active;
  4890. struct nk_color border_color;
  4891. /* background bar */
  4892. struct nk_color bar_normal;
  4893. struct nk_color bar_hover;
  4894. struct nk_color bar_active;
  4895. struct nk_color bar_filled;
  4896. /* cursor */
  4897. struct nk_style_item cursor_normal;
  4898. struct nk_style_item cursor_hover;
  4899. struct nk_style_item cursor_active;
  4900. /* properties */
  4901. float border;
  4902. float rounding;
  4903. float bar_height;
  4904. struct nk_vec2 padding;
  4905. struct nk_vec2 spacing;
  4906. struct nk_vec2 cursor_size;
  4907. float color_factor;
  4908. float disabled_factor;
  4909. /* optional buttons */
  4910. int show_buttons;
  4911. struct nk_style_button inc_button;
  4912. struct nk_style_button dec_button;
  4913. enum nk_symbol_type inc_symbol;
  4914. enum nk_symbol_type dec_symbol;
  4915. /* optional user callbacks */
  4916. nk_handle userdata;
  4917. void(*draw_begin)(struct nk_command_buffer*, nk_handle);
  4918. void(*draw_end)(struct nk_command_buffer*, nk_handle);
  4919. };
  4920. struct nk_style_knob {
  4921. /* background */
  4922. struct nk_style_item normal;
  4923. struct nk_style_item hover;
  4924. struct nk_style_item active;
  4925. struct nk_color border_color;
  4926. /* knob */
  4927. struct nk_color knob_normal;
  4928. struct nk_color knob_hover;
  4929. struct nk_color knob_active;
  4930. struct nk_color knob_border_color;
  4931. /* cursor */
  4932. struct nk_color cursor_normal;
  4933. struct nk_color cursor_hover;
  4934. struct nk_color cursor_active;
  4935. /* properties */
  4936. float border;
  4937. float knob_border;
  4938. struct nk_vec2 padding;
  4939. struct nk_vec2 spacing;
  4940. float cursor_width;
  4941. float color_factor;
  4942. float disabled_factor;
  4943. /* optional user callbacks */
  4944. nk_handle userdata;
  4945. void(*draw_begin)(struct nk_command_buffer*, nk_handle);
  4946. void(*draw_end)(struct nk_command_buffer*, nk_handle);
  4947. };
  4948. struct nk_style_progress {
  4949. /* background */
  4950. struct nk_style_item normal;
  4951. struct nk_style_item hover;
  4952. struct nk_style_item active;
  4953. struct nk_color border_color;
  4954. /* cursor */
  4955. struct nk_style_item cursor_normal;
  4956. struct nk_style_item cursor_hover;
  4957. struct nk_style_item cursor_active;
  4958. struct nk_color cursor_border_color;
  4959. /* properties */
  4960. float rounding;
  4961. float border;
  4962. float cursor_border;
  4963. float cursor_rounding;
  4964. struct nk_vec2 padding;
  4965. float color_factor;
  4966. float disabled_factor;
  4967. /* optional user callbacks */
  4968. nk_handle userdata;
  4969. void(*draw_begin)(struct nk_command_buffer*, nk_handle);
  4970. void(*draw_end)(struct nk_command_buffer*, nk_handle);
  4971. };
  4972. struct nk_style_scrollbar {
  4973. /* background */
  4974. struct nk_style_item normal;
  4975. struct nk_style_item hover;
  4976. struct nk_style_item active;
  4977. struct nk_color border_color;
  4978. /* cursor */
  4979. struct nk_style_item cursor_normal;
  4980. struct nk_style_item cursor_hover;
  4981. struct nk_style_item cursor_active;
  4982. struct nk_color cursor_border_color;
  4983. /* properties */
  4984. float border;
  4985. float rounding;
  4986. float border_cursor;
  4987. float rounding_cursor;
  4988. struct nk_vec2 padding;
  4989. float color_factor;
  4990. float disabled_factor;
  4991. /* optional buttons */
  4992. int show_buttons;
  4993. struct nk_style_button inc_button;
  4994. struct nk_style_button dec_button;
  4995. enum nk_symbol_type inc_symbol;
  4996. enum nk_symbol_type dec_symbol;
  4997. /* optional user callbacks */
  4998. nk_handle userdata;
  4999. void(*draw_begin)(struct nk_command_buffer*, nk_handle);
  5000. void(*draw_end)(struct nk_command_buffer*, nk_handle);
  5001. };
  5002. struct nk_style_edit {
  5003. /* background */
  5004. struct nk_style_item normal;
  5005. struct nk_style_item hover;
  5006. struct nk_style_item active;
  5007. struct nk_color border_color;
  5008. struct nk_style_scrollbar scrollbar;
  5009. /* cursor */
  5010. struct nk_color cursor_normal;
  5011. struct nk_color cursor_hover;
  5012. struct nk_color cursor_text_normal;
  5013. struct nk_color cursor_text_hover;
  5014. /* text (unselected) */
  5015. struct nk_color text_normal;
  5016. struct nk_color text_hover;
  5017. struct nk_color text_active;
  5018. /* text (selected) */
  5019. struct nk_color selected_normal;
  5020. struct nk_color selected_hover;
  5021. struct nk_color selected_text_normal;
  5022. struct nk_color selected_text_hover;
  5023. /* properties */
  5024. float border;
  5025. float rounding;
  5026. float cursor_size;
  5027. struct nk_vec2 scrollbar_size;
  5028. struct nk_vec2 padding;
  5029. float row_padding;
  5030. float color_factor;
  5031. float disabled_factor;
  5032. };
  5033. struct nk_style_property {
  5034. /* background */
  5035. struct nk_style_item normal;
  5036. struct nk_style_item hover;
  5037. struct nk_style_item active;
  5038. struct nk_color border_color;
  5039. /* text */
  5040. struct nk_color label_normal;
  5041. struct nk_color label_hover;
  5042. struct nk_color label_active;
  5043. /* symbols */
  5044. enum nk_symbol_type sym_left;
  5045. enum nk_symbol_type sym_right;
  5046. /* properties */
  5047. float border;
  5048. float rounding;
  5049. struct nk_vec2 padding;
  5050. float color_factor;
  5051. float disabled_factor;
  5052. struct nk_style_edit edit;
  5053. struct nk_style_button inc_button;
  5054. struct nk_style_button dec_button;
  5055. /* optional user callbacks */
  5056. nk_handle userdata;
  5057. void(*draw_begin)(struct nk_command_buffer*, nk_handle);
  5058. void(*draw_end)(struct nk_command_buffer*, nk_handle);
  5059. };
  5060. struct nk_style_chart {
  5061. /* colors */
  5062. struct nk_style_item background;
  5063. struct nk_color border_color;
  5064. struct nk_color selected_color;
  5065. struct nk_color color;
  5066. /* properties */
  5067. float border;
  5068. float rounding;
  5069. struct nk_vec2 padding;
  5070. float color_factor;
  5071. float disabled_factor;
  5072. nk_bool show_markers;
  5073. };
  5074. struct nk_style_combo {
  5075. /* background */
  5076. struct nk_style_item normal;
  5077. struct nk_style_item hover;
  5078. struct nk_style_item active;
  5079. struct nk_color border_color;
  5080. /* label */
  5081. struct nk_color label_normal;
  5082. struct nk_color label_hover;
  5083. struct nk_color label_active;
  5084. /* symbol */
  5085. struct nk_color symbol_normal;
  5086. struct nk_color symbol_hover;
  5087. struct nk_color symbol_active;
  5088. /* button */
  5089. struct nk_style_button button;
  5090. enum nk_symbol_type sym_normal;
  5091. enum nk_symbol_type sym_hover;
  5092. enum nk_symbol_type sym_active;
  5093. /* properties */
  5094. float border;
  5095. float rounding;
  5096. struct nk_vec2 content_padding;
  5097. struct nk_vec2 button_padding;
  5098. struct nk_vec2 spacing;
  5099. float color_factor;
  5100. float disabled_factor;
  5101. };
  5102. struct nk_style_tab {
  5103. /* background */
  5104. struct nk_style_item background;
  5105. struct nk_color border_color;
  5106. struct nk_color text;
  5107. /* button */
  5108. struct nk_style_button tab_maximize_button;
  5109. struct nk_style_button tab_minimize_button;
  5110. struct nk_style_button node_maximize_button;
  5111. struct nk_style_button node_minimize_button;
  5112. enum nk_symbol_type sym_minimize;
  5113. enum nk_symbol_type sym_maximize;
  5114. /* properties */
  5115. float border;
  5116. float rounding;
  5117. float indent;
  5118. struct nk_vec2 padding;
  5119. struct nk_vec2 spacing;
  5120. float color_factor;
  5121. float disabled_factor;
  5122. };
  5123. enum nk_style_header_align {
  5124. NK_HEADER_LEFT,
  5125. NK_HEADER_RIGHT
  5126. };
  5127. struct nk_style_window_header {
  5128. /* background */
  5129. struct nk_style_item normal;
  5130. struct nk_style_item hover;
  5131. struct nk_style_item active;
  5132. /* button */
  5133. struct nk_style_button close_button;
  5134. struct nk_style_button minimize_button;
  5135. enum nk_symbol_type close_symbol;
  5136. enum nk_symbol_type minimize_symbol;
  5137. enum nk_symbol_type maximize_symbol;
  5138. /* title */
  5139. struct nk_color label_normal;
  5140. struct nk_color label_hover;
  5141. struct nk_color label_active;
  5142. /* properties */
  5143. enum nk_style_header_align align;
  5144. struct nk_vec2 padding;
  5145. struct nk_vec2 label_padding;
  5146. struct nk_vec2 spacing;
  5147. };
  5148. struct nk_style_window {
  5149. struct nk_style_window_header header;
  5150. struct nk_style_item fixed_background;
  5151. struct nk_color background;
  5152. struct nk_color border_color;
  5153. struct nk_color popup_border_color;
  5154. struct nk_color combo_border_color;
  5155. struct nk_color contextual_border_color;
  5156. struct nk_color menu_border_color;
  5157. struct nk_color group_border_color;
  5158. struct nk_color tooltip_border_color;
  5159. struct nk_style_item scaler;
  5160. float border;
  5161. float combo_border;
  5162. float contextual_border;
  5163. float menu_border;
  5164. float group_border;
  5165. float tooltip_border;
  5166. float popup_border;
  5167. float min_row_height_padding;
  5168. float rounding;
  5169. struct nk_vec2 spacing;
  5170. struct nk_vec2 scrollbar_size;
  5171. struct nk_vec2 min_size;
  5172. struct nk_vec2 padding;
  5173. struct nk_vec2 group_padding;
  5174. struct nk_vec2 popup_padding;
  5175. struct nk_vec2 combo_padding;
  5176. struct nk_vec2 contextual_padding;
  5177. struct nk_vec2 menu_padding;
  5178. struct nk_vec2 tooltip_padding;
  5179. };
  5180. struct nk_style {
  5181. const struct nk_user_font *font;
  5182. const struct nk_cursor *cursors[NK_CURSOR_COUNT];
  5183. const struct nk_cursor *cursor_active;
  5184. struct nk_cursor *cursor_last;
  5185. int cursor_visible;
  5186. struct nk_style_text text;
  5187. struct nk_style_button button;
  5188. struct nk_style_button contextual_button;
  5189. struct nk_style_button menu_button;
  5190. struct nk_style_toggle option;
  5191. struct nk_style_toggle checkbox;
  5192. struct nk_style_selectable selectable;
  5193. struct nk_style_slider slider;
  5194. struct nk_style_knob knob;
  5195. struct nk_style_progress progress;
  5196. struct nk_style_property property;
  5197. struct nk_style_edit edit;
  5198. struct nk_style_chart chart;
  5199. struct nk_style_scrollbar scrollh;
  5200. struct nk_style_scrollbar scrollv;
  5201. struct nk_style_tab tab;
  5202. struct nk_style_combo combo;
  5203. struct nk_style_window window;
  5204. };
  5205. NK_API struct nk_style_item nk_style_item_color(struct nk_color);
  5206. NK_API struct nk_style_item nk_style_item_image(struct nk_image img);
  5207. NK_API struct nk_style_item nk_style_item_nine_slice(struct nk_nine_slice slice);
  5208. NK_API struct nk_style_item nk_style_item_hide(void);
  5209. /*==============================================================
  5210. * PANEL
  5211. * =============================================================*/
  5212. #ifndef NK_MAX_LAYOUT_ROW_TEMPLATE_COLUMNS
  5213. #define NK_MAX_LAYOUT_ROW_TEMPLATE_COLUMNS 16
  5214. #endif
  5215. #ifndef NK_CHART_MAX_SLOT
  5216. #define NK_CHART_MAX_SLOT 4
  5217. #endif
  5218. enum nk_panel_type {
  5219. NK_PANEL_NONE = 0,
  5220. NK_PANEL_WINDOW = NK_FLAG(0),
  5221. NK_PANEL_GROUP = NK_FLAG(1),
  5222. NK_PANEL_POPUP = NK_FLAG(2),
  5223. NK_PANEL_CONTEXTUAL = NK_FLAG(4),
  5224. NK_PANEL_COMBO = NK_FLAG(5),
  5225. NK_PANEL_MENU = NK_FLAG(6),
  5226. NK_PANEL_TOOLTIP = NK_FLAG(7)
  5227. };
  5228. enum nk_panel_set {
  5229. NK_PANEL_SET_NONBLOCK = NK_PANEL_CONTEXTUAL|NK_PANEL_COMBO|NK_PANEL_MENU|NK_PANEL_TOOLTIP,
  5230. NK_PANEL_SET_POPUP = NK_PANEL_SET_NONBLOCK|NK_PANEL_POPUP,
  5231. NK_PANEL_SET_SUB = NK_PANEL_SET_POPUP|NK_PANEL_GROUP
  5232. };
  5233. struct nk_chart_slot {
  5234. enum nk_chart_type type;
  5235. struct nk_color color;
  5236. struct nk_color highlight;
  5237. float min, max, range;
  5238. int count;
  5239. struct nk_vec2 last;
  5240. int index;
  5241. nk_bool show_markers;
  5242. };
  5243. struct nk_chart {
  5244. int slot;
  5245. float x, y, w, h;
  5246. struct nk_chart_slot slots[NK_CHART_MAX_SLOT];
  5247. };
  5248. enum nk_panel_row_layout_type {
  5249. NK_LAYOUT_DYNAMIC_FIXED = 0,
  5250. NK_LAYOUT_DYNAMIC_ROW,
  5251. NK_LAYOUT_DYNAMIC_FREE,
  5252. NK_LAYOUT_DYNAMIC,
  5253. NK_LAYOUT_STATIC_FIXED,
  5254. NK_LAYOUT_STATIC_ROW,
  5255. NK_LAYOUT_STATIC_FREE,
  5256. NK_LAYOUT_STATIC,
  5257. NK_LAYOUT_TEMPLATE,
  5258. NK_LAYOUT_COUNT
  5259. };
  5260. struct nk_row_layout {
  5261. enum nk_panel_row_layout_type type;
  5262. int index;
  5263. float height;
  5264. float min_height;
  5265. int columns;
  5266. const float *ratio;
  5267. float item_width;
  5268. float item_height;
  5269. float item_offset;
  5270. float filled;
  5271. struct nk_rect item;
  5272. int tree_depth;
  5273. float templates[NK_MAX_LAYOUT_ROW_TEMPLATE_COLUMNS];
  5274. };
  5275. struct nk_popup_buffer {
  5276. nk_size begin;
  5277. nk_size parent;
  5278. nk_size last;
  5279. nk_size end;
  5280. nk_bool active;
  5281. };
  5282. struct nk_menu_state {
  5283. float x, y, w, h;
  5284. struct nk_scroll offset;
  5285. };
  5286. struct nk_panel {
  5287. enum nk_panel_type type;
  5288. nk_flags flags;
  5289. struct nk_rect bounds;
  5290. nk_uint *offset_x;
  5291. nk_uint *offset_y;
  5292. float at_x, at_y, max_x;
  5293. float footer_height;
  5294. float header_height;
  5295. float border;
  5296. unsigned int has_scrolling;
  5297. struct nk_rect clip;
  5298. struct nk_menu_state menu;
  5299. struct nk_row_layout row;
  5300. struct nk_chart chart;
  5301. struct nk_command_buffer *buffer;
  5302. struct nk_panel *parent;
  5303. };
  5304. /*==============================================================
  5305. * WINDOW
  5306. * =============================================================*/
  5307. #ifndef NK_WINDOW_MAX_NAME
  5308. #define NK_WINDOW_MAX_NAME 64
  5309. #endif
  5310. struct nk_table;
  5311. enum nk_window_flags {
  5312. NK_WINDOW_PRIVATE = NK_FLAG(11),
  5313. NK_WINDOW_DYNAMIC = NK_WINDOW_PRIVATE, /**< special window type growing up in height while being filled to a certain maximum height */
  5314. NK_WINDOW_ROM = NK_FLAG(12), /**< sets window widgets into a read only mode and does not allow input changes */
  5315. NK_WINDOW_NOT_INTERACTIVE = NK_WINDOW_ROM|NK_WINDOW_NO_INPUT, /**< prevents all interaction caused by input to either window or widgets inside */
  5316. NK_WINDOW_HIDDEN = NK_FLAG(13), /**< Hides window and stops any window interaction and drawing */
  5317. NK_WINDOW_CLOSED = NK_FLAG(14), /**< Directly closes and frees the window at the end of the frame */
  5318. NK_WINDOW_MINIMIZED = NK_FLAG(15), /**< marks the window as minimized */
  5319. NK_WINDOW_REMOVE_ROM = NK_FLAG(16) /**< Removes read only mode at the end of the window */
  5320. };
  5321. struct nk_popup_state {
  5322. struct nk_window *win;
  5323. enum nk_panel_type type;
  5324. struct nk_popup_buffer buf;
  5325. nk_hash name;
  5326. nk_bool active;
  5327. unsigned combo_count;
  5328. unsigned con_count, con_old;
  5329. unsigned active_con;
  5330. struct nk_rect header;
  5331. };
  5332. struct nk_edit_state {
  5333. nk_hash name;
  5334. unsigned int seq;
  5335. unsigned int old;
  5336. int active, prev;
  5337. int cursor;
  5338. int sel_start;
  5339. int sel_end;
  5340. struct nk_scroll scrollbar;
  5341. unsigned char mode;
  5342. unsigned char single_line;
  5343. };
  5344. struct nk_property_state {
  5345. int active, prev;
  5346. char buffer[NK_MAX_NUMBER_BUFFER];
  5347. int length;
  5348. int cursor;
  5349. int select_start;
  5350. int select_end;
  5351. nk_hash name;
  5352. unsigned int seq;
  5353. unsigned int old;
  5354. int state;
  5355. };
  5356. struct nk_window {
  5357. unsigned int seq;
  5358. nk_hash name;
  5359. char name_string[NK_WINDOW_MAX_NAME];
  5360. nk_flags flags;
  5361. struct nk_rect bounds;
  5362. struct nk_scroll scrollbar;
  5363. struct nk_command_buffer buffer;
  5364. struct nk_panel *layout;
  5365. float scrollbar_hiding_timer;
  5366. /* persistent widget state */
  5367. struct nk_property_state property;
  5368. struct nk_popup_state popup;
  5369. struct nk_edit_state edit;
  5370. unsigned int scrolled;
  5371. nk_bool widgets_disabled;
  5372. struct nk_table *tables;
  5373. unsigned int table_count;
  5374. /* window list hooks */
  5375. struct nk_window *next;
  5376. struct nk_window *prev;
  5377. struct nk_window *parent;
  5378. };
  5379. /*==============================================================
  5380. * STACK
  5381. * =============================================================*/
  5382. /**
  5383. * \page Stack
  5384. * # Stack
  5385. * The style modifier stack can be used to temporarily change a
  5386. * property inside `nk_style`. For example if you want a special
  5387. * red button you can temporarily push the old button color onto a stack
  5388. * draw the button with a red color and then you just pop the old color
  5389. * back from the stack:
  5390. *
  5391. * nk_style_push_style_item(ctx, &ctx->style.button.normal, nk_style_item_color(nk_rgb(255,0,0)));
  5392. * nk_style_push_style_item(ctx, &ctx->style.button.hover, nk_style_item_color(nk_rgb(255,0,0)));
  5393. * nk_style_push_style_item(ctx, &ctx->style.button.active, nk_style_item_color(nk_rgb(255,0,0)));
  5394. * nk_style_push_vec2(ctx, &cx->style.button.padding, nk_vec2(2,2));
  5395. *
  5396. * nk_button(...);
  5397. *
  5398. * nk_style_pop_style_item(ctx);
  5399. * nk_style_pop_style_item(ctx);
  5400. * nk_style_pop_style_item(ctx);
  5401. * nk_style_pop_vec2(ctx);
  5402. *
  5403. * Nuklear has a stack for style_items, float properties, vector properties,
  5404. * flags, colors, fonts and for button_behavior. Each has it's own fixed size stack
  5405. * which can be changed at compile time.
  5406. */
  5407. #ifndef NK_BUTTON_BEHAVIOR_STACK_SIZE
  5408. #define NK_BUTTON_BEHAVIOR_STACK_SIZE 8
  5409. #endif
  5410. #ifndef NK_FONT_STACK_SIZE
  5411. #define NK_FONT_STACK_SIZE 8
  5412. #endif
  5413. #ifndef NK_STYLE_ITEM_STACK_SIZE
  5414. #define NK_STYLE_ITEM_STACK_SIZE 16
  5415. #endif
  5416. #ifndef NK_FLOAT_STACK_SIZE
  5417. #define NK_FLOAT_STACK_SIZE 32
  5418. #endif
  5419. #ifndef NK_VECTOR_STACK_SIZE
  5420. #define NK_VECTOR_STACK_SIZE 16
  5421. #endif
  5422. #ifndef NK_FLAGS_STACK_SIZE
  5423. #define NK_FLAGS_STACK_SIZE 32
  5424. #endif
  5425. #ifndef NK_COLOR_STACK_SIZE
  5426. #define NK_COLOR_STACK_SIZE 32
  5427. #endif
  5428. #define NK_CONFIGURATION_STACK_TYPE(prefix, name, type)\
  5429. struct nk_config_stack_##name##_element {\
  5430. prefix##_##type *address;\
  5431. prefix##_##type old_value;\
  5432. }
  5433. #define NK_CONFIG_STACK(type,size)\
  5434. struct nk_config_stack_##type {\
  5435. int head;\
  5436. struct nk_config_stack_##type##_element elements[size];\
  5437. }
  5438. #define nk_float float
  5439. NK_CONFIGURATION_STACK_TYPE(struct nk, style_item, style_item);
  5440. NK_CONFIGURATION_STACK_TYPE(nk ,float, float);
  5441. NK_CONFIGURATION_STACK_TYPE(struct nk, vec2, vec2);
  5442. NK_CONFIGURATION_STACK_TYPE(nk ,flags, flags);
  5443. NK_CONFIGURATION_STACK_TYPE(struct nk, color, color);
  5444. NK_CONFIGURATION_STACK_TYPE(const struct nk, user_font, user_font*);
  5445. NK_CONFIGURATION_STACK_TYPE(enum nk, button_behavior, button_behavior);
  5446. NK_CONFIG_STACK(style_item, NK_STYLE_ITEM_STACK_SIZE);
  5447. NK_CONFIG_STACK(float, NK_FLOAT_STACK_SIZE);
  5448. NK_CONFIG_STACK(vec2, NK_VECTOR_STACK_SIZE);
  5449. NK_CONFIG_STACK(flags, NK_FLAGS_STACK_SIZE);
  5450. NK_CONFIG_STACK(color, NK_COLOR_STACK_SIZE);
  5451. NK_CONFIG_STACK(user_font, NK_FONT_STACK_SIZE);
  5452. NK_CONFIG_STACK(button_behavior, NK_BUTTON_BEHAVIOR_STACK_SIZE);
  5453. struct nk_configuration_stacks {
  5454. struct nk_config_stack_style_item style_items;
  5455. struct nk_config_stack_float floats;
  5456. struct nk_config_stack_vec2 vectors;
  5457. struct nk_config_stack_flags flags;
  5458. struct nk_config_stack_color colors;
  5459. struct nk_config_stack_user_font fonts;
  5460. struct nk_config_stack_button_behavior button_behaviors;
  5461. };
  5462. /*==============================================================
  5463. * CONTEXT
  5464. * =============================================================*/
  5465. #define NK_VALUE_PAGE_CAPACITY \
  5466. (((NK_MAX(sizeof(struct nk_window),sizeof(struct nk_panel)) / sizeof(nk_uint))) / 2)
  5467. struct nk_table {
  5468. unsigned int seq;
  5469. unsigned int size;
  5470. nk_hash keys[NK_VALUE_PAGE_CAPACITY];
  5471. nk_uint values[NK_VALUE_PAGE_CAPACITY];
  5472. struct nk_table *next, *prev;
  5473. };
  5474. union nk_page_data {
  5475. struct nk_table tbl;
  5476. struct nk_panel pan;
  5477. struct nk_window win;
  5478. };
  5479. struct nk_page_element {
  5480. union nk_page_data data;
  5481. struct nk_page_element *next;
  5482. struct nk_page_element *prev;
  5483. };
  5484. struct nk_page {
  5485. unsigned int size;
  5486. struct nk_page *next;
  5487. struct nk_page_element win[1];
  5488. };
  5489. struct nk_pool {
  5490. struct nk_allocator alloc;
  5491. enum nk_allocation_type type;
  5492. unsigned int page_count;
  5493. struct nk_page *pages;
  5494. struct nk_page_element *freelist;
  5495. unsigned capacity;
  5496. nk_size size;
  5497. nk_size cap;
  5498. };
  5499. struct nk_context {
  5500. /* public: can be accessed freely */
  5501. struct nk_input input;
  5502. struct nk_style style;
  5503. struct nk_buffer memory;
  5504. struct nk_clipboard clip;
  5505. nk_flags last_widget_state;
  5506. enum nk_button_behavior button_behavior;
  5507. struct nk_configuration_stacks stacks;
  5508. float delta_time_seconds;
  5509. /* private:
  5510. should only be accessed if you
  5511. know what you are doing */
  5512. #ifdef NK_INCLUDE_VERTEX_BUFFER_OUTPUT
  5513. struct nk_draw_list draw_list;
  5514. #endif
  5515. #ifdef NK_INCLUDE_COMMAND_USERDATA
  5516. nk_handle userdata;
  5517. #endif
  5518. /** text editor objects are quite big because of an internal
  5519. * undo/redo stack. Therefore it does not make sense to have one for
  5520. * each window for temporary use cases, so I only provide *one* instance
  5521. * for all windows. This works because the content is cleared anyway */
  5522. struct nk_text_edit text_edit;
  5523. /** draw buffer used for overlay drawing operation like cursor */
  5524. struct nk_command_buffer overlay;
  5525. /** windows */
  5526. int build;
  5527. int use_pool;
  5528. struct nk_pool pool;
  5529. struct nk_window *begin;
  5530. struct nk_window *end;
  5531. struct nk_window *active;
  5532. struct nk_window *current;
  5533. struct nk_page_element *freelist;
  5534. unsigned int count;
  5535. unsigned int seq;
  5536. };
  5537. /* ==============================================================
  5538. * MATH
  5539. * =============================================================== */
  5540. #define NK_PI 3.141592654f
  5541. #define NK_PI_HALF 1.570796326f
  5542. #define NK_UTF_INVALID 0xFFFD
  5543. #define NK_MAX_FLOAT_PRECISION 2
  5544. #define NK_UNUSED(x) ((void)(x))
  5545. #define NK_SATURATE(x) (NK_MAX(0, NK_MIN(1.0f, x)))
  5546. #define NK_LEN(a) (sizeof(a)/sizeof(a)[0])
  5547. #define NK_ABS(a) (((a) < 0) ? -(a) : (a))
  5548. #define NK_BETWEEN(x, a, b) ((a) <= (x) && (x) < (b))
  5549. #define NK_INBOX(px, py, x, y, w, h)\
  5550. (NK_BETWEEN(px,x,x+w) && NK_BETWEEN(py,y,y+h))
  5551. #define NK_INTERSECT(x0, y0, w0, h0, x1, y1, w1, h1) \
  5552. ((x1 < (x0 + w0)) && (x0 < (x1 + w1)) && \
  5553. (y1 < (y0 + h0)) && (y0 < (y1 + h1)))
  5554. #define NK_CONTAINS(x, y, w, h, bx, by, bw, bh)\
  5555. (NK_INBOX(x,y, bx, by, bw, bh) && NK_INBOX(x+w,y+h, bx, by, bw, bh))
  5556. #define nk_vec2_sub(a, b) nk_vec2((a).x - (b).x, (a).y - (b).y)
  5557. #define nk_vec2_add(a, b) nk_vec2((a).x + (b).x, (a).y + (b).y)
  5558. #define nk_vec2_len_sqr(a) ((a).x*(a).x+(a).y*(a).y)
  5559. #define nk_vec2_muls(a, t) nk_vec2((a).x * (t), (a).y * (t))
  5560. #define nk_ptr_add(t, p, i) ((t*)((void*)((nk_byte*)(p) + (i))))
  5561. #define nk_ptr_add_const(t, p, i) ((const t*)((const void*)((const nk_byte*)(p) + (i))))
  5562. #define nk_zero_struct(s) nk_zero(&s, sizeof(s))
  5563. /* ==============================================================
  5564. * ALIGNMENT
  5565. * =============================================================== */
  5566. /* Pointer to Integer type conversion for pointer alignment */
  5567. #if defined(__PTRDIFF_TYPE__) /* This case should work for GCC*/
  5568. # define NK_UINT_TO_PTR(x) ((void*)(__PTRDIFF_TYPE__)(x))
  5569. # define NK_PTR_TO_UINT(x) ((nk_size)(__PTRDIFF_TYPE__)(x))
  5570. #elif !defined(__GNUC__) /* works for compilers other than LLVM */
  5571. # define NK_UINT_TO_PTR(x) ((void*)&((char*)0)[x])
  5572. # define NK_PTR_TO_UINT(x) ((nk_size)(((char*)x)-(char*)0))
  5573. #elif defined(NK_USE_FIXED_TYPES) /* used if we have <stdint.h> */
  5574. # define NK_UINT_TO_PTR(x) ((void*)(uintptr_t)(x))
  5575. # define NK_PTR_TO_UINT(x) ((uintptr_t)(x))
  5576. #else /* generates warning but works */
  5577. # define NK_UINT_TO_PTR(x) ((void*)(x))
  5578. # define NK_PTR_TO_UINT(x) ((nk_size)(x))
  5579. #endif
  5580. #define NK_ALIGN_PTR(x, mask)\
  5581. (NK_UINT_TO_PTR((NK_PTR_TO_UINT((nk_byte*)(x) + (mask-1)) & ~(mask-1))))
  5582. #define NK_ALIGN_PTR_BACK(x, mask)\
  5583. (NK_UINT_TO_PTR((NK_PTR_TO_UINT((nk_byte*)(x)) & ~(mask-1))))
  5584. #if ((defined(__GNUC__) && __GNUC__ >= 4) || defined(__clang__)) && !defined(EMSCRIPTEN)
  5585. #define NK_OFFSETOF(st,m) (__builtin_offsetof(st,m))
  5586. #else
  5587. #define NK_OFFSETOF(st,m) ((nk_ptr)&(((st*)0)->m))
  5588. #endif
  5589. #ifdef __cplusplus
  5590. }
  5591. #endif
  5592. #ifdef __cplusplus
  5593. template<typename T> struct nk_alignof;
  5594. template<typename T, int size_diff> struct nk_helper{enum {value = size_diff};};
  5595. template<typename T> struct nk_helper<T,0>{enum {value = nk_alignof<T>::value};};
  5596. template<typename T> struct nk_alignof{struct Big {T x; char c;}; enum {
  5597. diff = sizeof(Big) - sizeof(T), value = nk_helper<Big, diff>::value};};
  5598. #define NK_ALIGNOF(t) (nk_alignof<t>::value)
  5599. #else
  5600. #define NK_ALIGNOF(t) NK_OFFSETOF(struct {char c; t _h;}, _h)
  5601. #endif
  5602. #define NK_CONTAINER_OF(ptr,type,member)\
  5603. (type*)((void*)((char*)(1 ? (ptr): &((type*)0)->member) - NK_OFFSETOF(type, member)))
  5604. #endif /* NK_NUKLEAR_H_ */
  5605. #ifdef NK_IMPLEMENTATION
  5606. #ifndef NK_INTERNAL_H
  5607. #define NK_INTERNAL_H
  5608. #ifndef NK_POOL_DEFAULT_CAPACITY
  5609. #define NK_POOL_DEFAULT_CAPACITY 16
  5610. #endif
  5611. #ifndef NK_DEFAULT_COMMAND_BUFFER_SIZE
  5612. #define NK_DEFAULT_COMMAND_BUFFER_SIZE (4*1024)
  5613. #endif
  5614. #ifndef NK_BUFFER_DEFAULT_INITIAL_SIZE
  5615. #define NK_BUFFER_DEFAULT_INITIAL_SIZE (4*1024)
  5616. #endif
  5617. /* standard library headers */
  5618. #ifdef NK_INCLUDE_DEFAULT_ALLOCATOR
  5619. #include <stdlib.h> /* malloc, free */
  5620. #endif
  5621. #ifdef NK_INCLUDE_STANDARD_IO
  5622. #include <stdio.h> /* fopen, fclose,... */
  5623. #endif
  5624. #ifdef NK_INCLUDE_STANDARD_VARARGS
  5625. #include <stdarg.h> /* valist, va_start, va_end, ... */
  5626. #endif
  5627. #ifndef NK_ASSERT
  5628. #include <assert.h>
  5629. #define NK_ASSERT(expr) assert(expr)
  5630. #endif
  5631. #define NK_DEFAULT (-1)
  5632. #ifndef NK_VSNPRINTF
  5633. /* If your compiler does support `vsnprintf` I would highly recommend
  5634. * defining this to vsnprintf instead since `vsprintf` is basically
  5635. * unbelievable unsafe and should *NEVER* be used. But I have to support
  5636. * it since C89 only provides this unsafe version. */
  5637. #if (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)) ||\
  5638. (defined(__cplusplus) && (__cplusplus >= 201103L)) || \
  5639. (defined(_POSIX_C_SOURCE) && (_POSIX_C_SOURCE >= 200112L)) ||\
  5640. (defined(_XOPEN_SOURCE) && (_XOPEN_SOURCE >= 500)) ||\
  5641. defined(_ISOC99_SOURCE) || defined(_BSD_SOURCE)
  5642. #define NK_VSNPRINTF(s,n,f,a) vsnprintf(s,n,f,a)
  5643. #else
  5644. #define NK_VSNPRINTF(s,n,f,a) vsprintf(s,f,a)
  5645. #endif
  5646. #endif
  5647. #define NK_SCHAR_MIN (-127)
  5648. #define NK_SCHAR_MAX 127
  5649. #define NK_UCHAR_MIN 0
  5650. #define NK_UCHAR_MAX 256
  5651. #define NK_SSHORT_MIN (-32767)
  5652. #define NK_SSHORT_MAX 32767
  5653. #define NK_USHORT_MIN 0
  5654. #define NK_USHORT_MAX 65535
  5655. #define NK_SINT_MIN (-2147483647)
  5656. #define NK_SINT_MAX 2147483647
  5657. #define NK_UINT_MIN 0
  5658. #define NK_UINT_MAX 4294967295u
  5659. /* Make sure correct type size:
  5660. * This will fire with a negative subscript error if the type sizes
  5661. * are set incorrectly by the compiler, and compile out if not */
  5662. NK_STATIC_ASSERT(sizeof(nk_size) >= sizeof(void*));
  5663. NK_STATIC_ASSERT(sizeof(nk_ptr) == sizeof(void*));
  5664. NK_STATIC_ASSERT(sizeof(nk_flags) >= 4);
  5665. NK_STATIC_ASSERT(sizeof(nk_rune) >= 4);
  5666. NK_STATIC_ASSERT(sizeof(nk_ushort) == 2);
  5667. NK_STATIC_ASSERT(sizeof(nk_short) == 2);
  5668. NK_STATIC_ASSERT(sizeof(nk_uint) == 4);
  5669. NK_STATIC_ASSERT(sizeof(nk_int) == 4);
  5670. NK_STATIC_ASSERT(sizeof(nk_byte) == 1);
  5671. #ifdef NK_INCLUDE_STANDARD_BOOL
  5672. NK_STATIC_ASSERT(sizeof(nk_bool) == sizeof(bool));
  5673. #else
  5674. NK_STATIC_ASSERT(sizeof(nk_bool) == 4);
  5675. #endif
  5676. NK_GLOBAL const struct nk_rect nk_null_rect = {-8192.0f, -8192.0f, 16384, 16384};
  5677. #define NK_FLOAT_PRECISION 0.00000000000001
  5678. NK_GLOBAL const struct nk_color nk_red = {255,0,0,255};
  5679. NK_GLOBAL const struct nk_color nk_green = {0,255,0,255};
  5680. NK_GLOBAL const struct nk_color nk_blue = {0,0,255,255};
  5681. NK_GLOBAL const struct nk_color nk_white = {255,255,255,255};
  5682. NK_GLOBAL const struct nk_color nk_black = {0,0,0,255};
  5683. NK_GLOBAL const struct nk_color nk_yellow = {255,255,0,255};
  5684. /* widget */
  5685. #define nk_widget_state_reset(s)\
  5686. if ((*(s)) & NK_WIDGET_STATE_MODIFIED)\
  5687. (*(s)) = NK_WIDGET_STATE_INACTIVE|NK_WIDGET_STATE_MODIFIED;\
  5688. else (*(s)) = NK_WIDGET_STATE_INACTIVE;
  5689. /* math */
  5690. #ifndef NK_INV_SQRT
  5691. NK_LIB float nk_inv_sqrt(float n);
  5692. #endif
  5693. #ifndef NK_SIN
  5694. NK_LIB float nk_sin(float x);
  5695. #endif
  5696. #ifndef NK_COS
  5697. NK_LIB float nk_cos(float x);
  5698. #endif
  5699. #ifndef NK_ATAN
  5700. NK_LIB float nk_atan(float x);
  5701. #endif
  5702. #ifndef NK_ATAN2
  5703. NK_LIB float nk_atan2(float y, float x);
  5704. #endif
  5705. NK_LIB nk_uint nk_round_up_pow2(nk_uint v);
  5706. NK_LIB struct nk_rect nk_shrink_rect(struct nk_rect r, float amount);
  5707. NK_LIB struct nk_rect nk_pad_rect(struct nk_rect r, struct nk_vec2 pad);
  5708. NK_LIB void nk_unify(struct nk_rect *clip, const struct nk_rect *a, float x0, float y0, float x1, float y1);
  5709. NK_LIB double nk_pow(double x, int n);
  5710. NK_LIB int nk_ifloord(double x);
  5711. NK_LIB int nk_ifloorf(float x);
  5712. NK_LIB int nk_iceilf(float x);
  5713. NK_LIB int nk_log10(double n);
  5714. NK_LIB float nk_roundf(float x);
  5715. /* util */
  5716. enum {NK_DO_NOT_STOP_ON_NEW_LINE, NK_STOP_ON_NEW_LINE};
  5717. NK_LIB nk_bool nk_is_lower(int c);
  5718. NK_LIB nk_bool nk_is_upper(int c);
  5719. NK_LIB int nk_to_upper(int c);
  5720. NK_LIB int nk_to_lower(int c);
  5721. #ifndef NK_MEMCPY
  5722. NK_LIB void* nk_memcopy(void *dst, const void *src, nk_size n);
  5723. #endif
  5724. #ifndef NK_MEMSET
  5725. NK_LIB void nk_memset(void *ptr, int c0, nk_size size);
  5726. #endif
  5727. NK_LIB void nk_zero(void *ptr, nk_size size);
  5728. NK_LIB char *nk_itoa(char *s, long n);
  5729. NK_LIB int nk_string_float_limit(char *string, int prec);
  5730. #ifndef NK_DTOA
  5731. NK_LIB char *nk_dtoa(char *s, double n);
  5732. #endif
  5733. 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);
  5734. 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);
  5735. #ifdef NK_INCLUDE_STANDARD_VARARGS
  5736. NK_LIB int nk_strfmt(char *buf, int buf_size, const char *fmt, va_list args);
  5737. #endif
  5738. #ifdef NK_INCLUDE_STANDARD_IO
  5739. NK_LIB char *nk_file_load(const char* path, nk_size* siz, const struct nk_allocator *alloc);
  5740. #endif
  5741. /* buffer */
  5742. #ifdef NK_INCLUDE_DEFAULT_ALLOCATOR
  5743. NK_LIB void* nk_malloc(nk_handle unused, void *old,nk_size size);
  5744. NK_LIB void nk_mfree(nk_handle unused, void *ptr);
  5745. #endif
  5746. NK_LIB void* nk_buffer_align(void *unaligned, nk_size align, nk_size *alignment, enum nk_buffer_allocation_type type);
  5747. NK_LIB void* nk_buffer_alloc(struct nk_buffer *b, enum nk_buffer_allocation_type type, nk_size size, nk_size align);
  5748. NK_LIB void* nk_buffer_realloc(struct nk_buffer *b, nk_size capacity, nk_size *size);
  5749. /* draw */
  5750. NK_LIB void nk_command_buffer_init(struct nk_command_buffer *cb, struct nk_buffer *b, enum nk_command_clipping clip);
  5751. NK_LIB void nk_command_buffer_reset(struct nk_command_buffer *b);
  5752. NK_LIB void* nk_command_buffer_push(struct nk_command_buffer* b, enum nk_command_type t, nk_size size);
  5753. 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);
  5754. /* buffering */
  5755. NK_LIB void nk_start_buffer(struct nk_context *ctx, struct nk_command_buffer *b);
  5756. NK_LIB void nk_start(struct nk_context *ctx, struct nk_window *win);
  5757. NK_LIB void nk_start_popup(struct nk_context *ctx, struct nk_window *win);
  5758. NK_LIB void nk_finish_popup(struct nk_context *ctx, struct nk_window*);
  5759. NK_LIB void nk_finish_buffer(struct nk_context *ctx, struct nk_command_buffer *b);
  5760. NK_LIB void nk_finish(struct nk_context *ctx, struct nk_window *w);
  5761. NK_LIB void nk_build(struct nk_context *ctx);
  5762. /* text editor */
  5763. NK_LIB void nk_textedit_clear_state(struct nk_text_edit *state, enum nk_text_edit_type type, nk_plugin_filter filter);
  5764. NK_LIB void nk_textedit_click(struct nk_text_edit *state, float x, float y, const struct nk_user_font *font, float row_height);
  5765. NK_LIB void nk_textedit_drag(struct nk_text_edit *state, float x, float y, const struct nk_user_font *font, float row_height);
  5766. 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);
  5767. /* window */
  5768. enum nk_window_insert_location {
  5769. NK_INSERT_BACK, /* inserts window into the back of list (front of screen) */
  5770. NK_INSERT_FRONT /* inserts window into the front of list (back of screen) */
  5771. };
  5772. NK_LIB void *nk_create_window(struct nk_context *ctx);
  5773. NK_LIB void nk_remove_window(struct nk_context*, struct nk_window*);
  5774. NK_LIB void nk_free_window(struct nk_context *ctx, struct nk_window *win);
  5775. NK_LIB struct nk_window *nk_find_window(const struct nk_context *ctx, nk_hash hash, const char *name);
  5776. NK_LIB void nk_insert_window(struct nk_context *ctx, struct nk_window *win, enum nk_window_insert_location loc);
  5777. /* pool */
  5778. NK_LIB void nk_pool_init(struct nk_pool *pool, const struct nk_allocator *alloc, unsigned int capacity);
  5779. NK_LIB void nk_pool_free(struct nk_pool *pool);
  5780. NK_LIB void nk_pool_init_fixed(struct nk_pool *pool, void *memory, nk_size size);
  5781. NK_LIB struct nk_page_element *nk_pool_alloc(struct nk_pool *pool);
  5782. /* page-element */
  5783. NK_LIB struct nk_page_element* nk_create_page_element(struct nk_context *ctx);
  5784. NK_LIB void nk_link_page_element_into_freelist(struct nk_context *ctx, struct nk_page_element *elem);
  5785. NK_LIB void nk_free_page_element(struct nk_context *ctx, struct nk_page_element *elem);
  5786. /* table */
  5787. NK_LIB struct nk_table* nk_create_table(struct nk_context *ctx);
  5788. NK_LIB void nk_remove_table(struct nk_window *win, struct nk_table *tbl);
  5789. NK_LIB void nk_free_table(struct nk_context *ctx, struct nk_table *tbl);
  5790. NK_LIB void nk_push_table(struct nk_window *win, struct nk_table *tbl);
  5791. NK_LIB nk_uint *nk_add_value(struct nk_context *ctx, struct nk_window *win, nk_hash name, nk_uint value);
  5792. NK_LIB nk_uint *nk_find_value(const struct nk_window *win, nk_hash name);
  5793. /* panel */
  5794. NK_LIB void *nk_create_panel(struct nk_context *ctx);
  5795. NK_LIB void nk_free_panel(struct nk_context*, struct nk_panel *pan);
  5796. NK_LIB nk_bool nk_panel_has_header(nk_flags flags, const char *title);
  5797. NK_LIB struct nk_vec2 nk_panel_get_padding(const struct nk_style *style, enum nk_panel_type type);
  5798. NK_LIB float nk_panel_get_border(const struct nk_style *style, nk_flags flags, enum nk_panel_type type);
  5799. NK_LIB struct nk_color nk_panel_get_border_color(const struct nk_style *style, enum nk_panel_type type);
  5800. NK_LIB nk_bool nk_panel_is_sub(enum nk_panel_type type);
  5801. NK_LIB nk_bool nk_panel_is_nonblock(enum nk_panel_type type);
  5802. NK_LIB nk_bool nk_panel_begin(struct nk_context *ctx, const char *title, enum nk_panel_type panel_type);
  5803. NK_LIB void nk_panel_end(struct nk_context *ctx);
  5804. /* layout */
  5805. NK_LIB float nk_layout_row_calculate_usable_space(const struct nk_style *style, enum nk_panel_type type, float total_space, int columns);
  5806. NK_LIB void nk_panel_layout(const struct nk_context *ctx, struct nk_window *win, float height, int cols);
  5807. NK_LIB void nk_row_layout(struct nk_context *ctx, enum nk_layout_format fmt, float height, int cols, int width);
  5808. NK_LIB void nk_panel_alloc_row(const struct nk_context *ctx, struct nk_window *win);
  5809. NK_LIB void nk_layout_widget_space(struct nk_rect *bounds, const struct nk_context *ctx, struct nk_window *win, int modify);
  5810. NK_LIB void nk_panel_alloc_space(struct nk_rect *bounds, const struct nk_context *ctx);
  5811. NK_LIB void nk_layout_peek(struct nk_rect *bounds, const struct nk_context *ctx);
  5812. /* popup */
  5813. 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);
  5814. /* text */
  5815. struct nk_text {
  5816. struct nk_vec2 padding;
  5817. struct nk_color background;
  5818. struct nk_color text;
  5819. };
  5820. 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);
  5821. 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);
  5822. /* button */
  5823. NK_LIB nk_bool nk_button_behavior(nk_flags *state, struct nk_rect r, const struct nk_input *i, enum nk_button_behavior behavior);
  5824. 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);
  5825. 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);
  5826. 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);
  5827. 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);
  5828. 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);
  5829. 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);
  5830. 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);
  5831. 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);
  5832. 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);
  5833. 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);
  5834. 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);
  5835. 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);
  5836. /* toggle */
  5837. enum nk_toggle_type {
  5838. NK_TOGGLE_CHECK,
  5839. NK_TOGGLE_OPTION
  5840. };
  5841. NK_LIB nk_bool nk_toggle_behavior(const struct nk_input *in, struct nk_rect select, nk_flags *state, nk_bool active);
  5842. 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, nk_flags text_alignment);
  5843. 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, nk_flags text_alignment);
  5844. 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, nk_flags widget_alignment, nk_flags text_alignment);
  5845. /* progress */
  5846. 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);
  5847. 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);
  5848. 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);
  5849. /* slider */
  5850. 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);
  5851. 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);
  5852. 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);
  5853. /* scrollbar */
  5854. 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);
  5855. 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);
  5856. 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);
  5857. 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);
  5858. /* selectable */
  5859. 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);
  5860. 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);
  5861. 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);
  5862. /* edit */
  5863. 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);
  5864. 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);
  5865. /* color-picker */
  5866. 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);
  5867. 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);
  5868. 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);
  5869. /* property */
  5870. enum nk_property_status {
  5871. NK_PROPERTY_DEFAULT,
  5872. NK_PROPERTY_EDIT,
  5873. NK_PROPERTY_DRAG
  5874. };
  5875. enum nk_property_filter {
  5876. NK_FILTER_INT,
  5877. NK_FILTER_FLOAT
  5878. };
  5879. enum nk_property_kind {
  5880. NK_PROPERTY_INT,
  5881. NK_PROPERTY_FLOAT,
  5882. NK_PROPERTY_DOUBLE
  5883. };
  5884. union nk_property {
  5885. int i;
  5886. float f;
  5887. double d;
  5888. };
  5889. struct nk_property_variant {
  5890. enum nk_property_kind kind;
  5891. union nk_property value;
  5892. union nk_property min_value;
  5893. union nk_property max_value;
  5894. union nk_property step;
  5895. };
  5896. NK_LIB struct nk_property_variant nk_property_variant_int(int value, int min_value, int max_value, int step);
  5897. NK_LIB struct nk_property_variant nk_property_variant_float(float value, float min_value, float max_value, float step);
  5898. NK_LIB struct nk_property_variant nk_property_variant_double(double value, double min_value, double max_value, double step);
  5899. 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);
  5900. 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);
  5901. 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);
  5902. 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);
  5903. 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);
  5904. #ifdef NK_INCLUDE_FONT_BAKING
  5905. /**
  5906. * @def NK_NO_STB_RECT_PACK_IMPLEMENTATION
  5907. *
  5908. * When defined, will avoid enabling STB_RECT_PACK_IMPLEMENTATION for when stb_rect_pack.h is already implemented elsewhere.
  5909. */
  5910. #ifndef NK_NO_STB_RECT_PACK_IMPLEMENTATION
  5911. #define STB_RECT_PACK_IMPLEMENTATION
  5912. #endif /* NK_NO_STB_RECT_PACK_IMPLEMENTATION */
  5913. /**
  5914. * @def NK_NO_STB_TRUETYPE_IMPLEMENTATION
  5915. *
  5916. * When defined, will avoid enabling STB_TRUETYPE_IMPLEMENTATION for when stb_truetype.h is already implemented elsewhere.
  5917. */
  5918. #ifndef NK_NO_STB_TRUETYPE_IMPLEMENTATION
  5919. #define STB_TRUETYPE_IMPLEMENTATION
  5920. #endif /* NK_NO_STB_TRUETYPE_IMPLEMENTATION */
  5921. /* Allow consumer to define own STBTT_malloc/STBTT_free, and use the font atlas' allocator otherwise */
  5922. #ifndef STBTT_malloc
  5923. static void*
  5924. nk_stbtt_malloc(nk_size size, void *user_data) {
  5925. struct nk_allocator *alloc = (struct nk_allocator *) user_data;
  5926. return alloc->alloc(alloc->userdata, 0, size);
  5927. }
  5928. static void
  5929. nk_stbtt_free(void *ptr, void *user_data) {
  5930. struct nk_allocator *alloc = (struct nk_allocator *) user_data;
  5931. alloc->free(alloc->userdata, ptr);
  5932. }
  5933. #define STBTT_malloc(x,u) nk_stbtt_malloc(x,u)
  5934. #define STBTT_free(x,u) nk_stbtt_free(x,u)
  5935. #endif /* STBTT_malloc */
  5936. #endif /* NK_INCLUDE_FONT_BAKING */
  5937. #endif
  5938. /* ===============================================================
  5939. *
  5940. * MATH
  5941. *
  5942. * ===============================================================*/
  5943. /*/// ### Math
  5944. /// Since nuklear is supposed to work on all systems providing floating point
  5945. /// math without any dependencies I also had to implement my own math functions
  5946. /// for sqrt, sin and cos. Since the actual highly accurate implementations for
  5947. /// the standard library functions are quite complex and I do not need high
  5948. /// precision for my use cases I use approximations.
  5949. ///
  5950. /// Sqrt
  5951. /// ----
  5952. /// For square root nuklear uses the famous fast inverse square root:
  5953. /// https://en.wikipedia.org/wiki/Fast_inverse_square_root with
  5954. /// slightly tweaked magic constant. While on today's hardware it is
  5955. /// probably not faster it is still fast and accurate enough for
  5956. /// nuklear's use cases. IMPORTANT: this requires float format IEEE 754
  5957. ///
  5958. /// Sine/Cosine
  5959. /// -----------
  5960. /// All constants inside both function are generated Remez's minimax
  5961. /// approximations for value range 0...2*PI. The reason why I decided to
  5962. /// approximate exactly that range is that nuklear only needs sine and
  5963. /// cosine to generate circles which only requires that exact range.
  5964. /// In addition I used Remez instead of Taylor for additional precision:
  5965. /// www.lolengine.net/blog/2011/12/21/better-function-approximations.
  5966. ///
  5967. /// The tool I used to generate constants for both sine and cosine
  5968. /// (it can actually approximate a lot more functions) can be
  5969. /// found here: www.lolengine.net/wiki/oss/lolremez
  5970. */
  5971. #ifndef NK_INV_SQRT
  5972. #define NK_INV_SQRT nk_inv_sqrt
  5973. NK_LIB float
  5974. nk_inv_sqrt(float n)
  5975. {
  5976. float x2;
  5977. const float threehalfs = 1.5f;
  5978. union {nk_uint i; float f;} conv = {0};
  5979. conv.f = n;
  5980. x2 = n * 0.5f;
  5981. conv.i = 0x5f375A84 - (conv.i >> 1);
  5982. conv.f = conv.f * (threehalfs - (x2 * conv.f * conv.f));
  5983. return conv.f;
  5984. }
  5985. #endif
  5986. #ifndef NK_SIN
  5987. #define NK_SIN nk_sin
  5988. NK_LIB float
  5989. nk_sin(float x)
  5990. {
  5991. NK_STORAGE const float a0 = +1.91059300966915117e-31f;
  5992. NK_STORAGE const float a1 = +1.00086760103908896f;
  5993. NK_STORAGE const float a2 = -1.21276126894734565e-2f;
  5994. NK_STORAGE const float a3 = -1.38078780785773762e-1f;
  5995. NK_STORAGE const float a4 = -2.67353392911981221e-2f;
  5996. NK_STORAGE const float a5 = +2.08026600266304389e-2f;
  5997. NK_STORAGE const float a6 = -3.03996055049204407e-3f;
  5998. NK_STORAGE const float a7 = +1.38235642404333740e-4f;
  5999. return a0 + x*(a1 + x*(a2 + x*(a3 + x*(a4 + x*(a5 + x*(a6 + x*a7))))));
  6000. }
  6001. #endif
  6002. #ifndef NK_COS
  6003. #define NK_COS nk_cos
  6004. NK_LIB float
  6005. nk_cos(float x)
  6006. {
  6007. /* New implementation. Also generated using lolremez. */
  6008. /* Old version significantly deviated from expected results. */
  6009. NK_STORAGE const float a0 = 9.9995999154986614e-1f;
  6010. NK_STORAGE const float a1 = 1.2548995793001028e-3f;
  6011. NK_STORAGE const float a2 = -5.0648546280678015e-1f;
  6012. NK_STORAGE const float a3 = 1.2942246466519995e-2f;
  6013. NK_STORAGE const float a4 = 2.8668384702547972e-2f;
  6014. NK_STORAGE const float a5 = 7.3726485210586547e-3f;
  6015. NK_STORAGE const float a6 = -3.8510875386947414e-3f;
  6016. NK_STORAGE const float a7 = 4.7196604604366623e-4f;
  6017. NK_STORAGE const float a8 = -1.8776444013090451e-5f;
  6018. return a0 + x*(a1 + x*(a2 + x*(a3 + x*(a4 + x*(a5 + x*(a6 + x*(a7 + x*a8)))))));
  6019. }
  6020. #endif
  6021. #ifndef NK_ATAN
  6022. #define NK_ATAN nk_atan
  6023. NK_LIB float
  6024. nk_atan(float x)
  6025. {
  6026. /* ./lolremez --progress --float -d 9 -r "0:pi*2" "atan(x)" */
  6027. float u = -1.0989005e-05f;
  6028. NK_ASSERT(x >= 0.0f && "TODO support negative floats");
  6029. u = u * x + 0.00034117949f;
  6030. u = u * x + -0.0044932296f;
  6031. u = u * x + 0.032596264f;
  6032. u = u * x + -0.14088021f;
  6033. u = u * x + 0.36040401f;
  6034. u = u * x + -0.47017866f;
  6035. u = u * x + 0.00050198776f;
  6036. u = u * x + 1.0077682f;
  6037. u = u * x + -0.0004765437f;
  6038. return u;
  6039. }
  6040. #endif
  6041. #ifndef NK_ATAN2
  6042. #define NK_ATAN2 nk_atan2
  6043. NK_LIB float
  6044. nk_atan2(float y, float x)
  6045. {
  6046. float ax = NK_ABS(x),
  6047. ay = NK_ABS(y);
  6048. /* 0 = +y +x 1 = -y +x
  6049. 2 = +y -x 3 = -y -x */
  6050. nk_uint signs = (y < 0) | ((x < 0) << 1);
  6051. float a;
  6052. if(y == 0.0 && x == 0.0) return 0.0f;
  6053. a = (ay > ax)
  6054. ? NK_PI_HALF - NK_ATAN(ax / ay)
  6055. : NK_ATAN(ay / ax);
  6056. switch(signs){
  6057. case 0: return a;
  6058. case 1: return -a;
  6059. case 2: return -a + NK_PI;
  6060. case 3: return a - NK_PI;
  6061. }
  6062. return 0.0f; /* prevents warning */
  6063. }
  6064. #endif
  6065. NK_LIB nk_uint
  6066. nk_round_up_pow2(nk_uint v)
  6067. {
  6068. v--;
  6069. v |= v >> 1;
  6070. v |= v >> 2;
  6071. v |= v >> 4;
  6072. v |= v >> 8;
  6073. v |= v >> 16;
  6074. v++;
  6075. return v;
  6076. }
  6077. NK_LIB double
  6078. nk_pow(double x, int n)
  6079. {
  6080. /* check the sign of n */
  6081. double r = 1;
  6082. int plus = n >= 0;
  6083. n = (plus) ? n : -n;
  6084. while (n > 0) {
  6085. if ((n & 1) == 1)
  6086. r *= x;
  6087. n /= 2;
  6088. x *= x;
  6089. }
  6090. return plus ? r : 1.0 / r;
  6091. }
  6092. NK_LIB int
  6093. nk_ifloord(double x)
  6094. {
  6095. x = (double)((int)x - ((x < 0.0) ? 1 : 0));
  6096. return (int)x;
  6097. }
  6098. NK_LIB int
  6099. nk_ifloorf(float x)
  6100. {
  6101. x = (float)((int)x - ((x < 0.0f) ? 1 : 0));
  6102. return (int)x;
  6103. }
  6104. NK_LIB int
  6105. nk_iceilf(float x)
  6106. {
  6107. if (x >= 0) {
  6108. int i = (int)x;
  6109. return (x > i) ? i+1: i;
  6110. } else {
  6111. int t = (int)x;
  6112. float r = x - (float)t;
  6113. return (r > 0.0f) ? t+1: t;
  6114. }
  6115. }
  6116. NK_LIB int
  6117. nk_log10(double n)
  6118. {
  6119. int neg;
  6120. int ret;
  6121. int exp = 0;
  6122. neg = (n < 0) ? 1 : 0;
  6123. ret = (neg) ? (int)-n : (int)n;
  6124. while ((ret / 10) > 0) {
  6125. ret /= 10;
  6126. exp++;
  6127. }
  6128. if (neg) exp = -exp;
  6129. return exp;
  6130. }
  6131. NK_LIB float
  6132. nk_roundf(float x)
  6133. {
  6134. return (x >= 0.0f) ? (float)nk_ifloorf(x + 0.5f) : (float)nk_iceilf(x - 0.5f);
  6135. }
  6136. NK_API struct nk_rect
  6137. nk_get_null_rect(void)
  6138. {
  6139. return nk_null_rect;
  6140. }
  6141. NK_API struct nk_rect
  6142. nk_rect(float x, float y, float w, float h)
  6143. {
  6144. struct nk_rect r;
  6145. r.x = x; r.y = y;
  6146. r.w = w; r.h = h;
  6147. return r;
  6148. }
  6149. NK_API struct nk_rect
  6150. nk_recti(int x, int y, int w, int h)
  6151. {
  6152. struct nk_rect r;
  6153. r.x = (float)x;
  6154. r.y = (float)y;
  6155. r.w = (float)w;
  6156. r.h = (float)h;
  6157. return r;
  6158. }
  6159. NK_API struct nk_rect
  6160. nk_recta(struct nk_vec2 pos, struct nk_vec2 size)
  6161. {
  6162. return nk_rect(pos.x, pos.y, size.x, size.y);
  6163. }
  6164. NK_API struct nk_rect
  6165. nk_rectv(const float *r)
  6166. {
  6167. return nk_rect(r[0], r[1], r[2], r[3]);
  6168. }
  6169. NK_API struct nk_rect
  6170. nk_rectiv(const int *r)
  6171. {
  6172. return nk_recti(r[0], r[1], r[2], r[3]);
  6173. }
  6174. NK_API struct nk_vec2
  6175. nk_rect_pos(struct nk_rect r)
  6176. {
  6177. struct nk_vec2 ret;
  6178. ret.x = r.x; ret.y = r.y;
  6179. return ret;
  6180. }
  6181. NK_API struct nk_vec2
  6182. nk_rect_size(struct nk_rect r)
  6183. {
  6184. struct nk_vec2 ret;
  6185. ret.x = r.w; ret.y = r.h;
  6186. return ret;
  6187. }
  6188. NK_LIB struct nk_rect
  6189. nk_shrink_rect(struct nk_rect r, float amount)
  6190. {
  6191. struct nk_rect res;
  6192. r.w = NK_MAX(r.w, 2 * amount);
  6193. r.h = NK_MAX(r.h, 2 * amount);
  6194. res.x = r.x + amount;
  6195. res.y = r.y + amount;
  6196. res.w = r.w - 2 * amount;
  6197. res.h = r.h - 2 * amount;
  6198. return res;
  6199. }
  6200. NK_LIB struct nk_rect
  6201. nk_pad_rect(struct nk_rect r, struct nk_vec2 pad)
  6202. {
  6203. r.w = NK_MAX(r.w, 2 * pad.x);
  6204. r.h = NK_MAX(r.h, 2 * pad.y);
  6205. r.x += pad.x; r.y += pad.y;
  6206. r.w -= 2 * pad.x;
  6207. r.h -= 2 * pad.y;
  6208. return r;
  6209. }
  6210. NK_API struct nk_vec2
  6211. nk_vec2(float x, float y)
  6212. {
  6213. struct nk_vec2 ret;
  6214. ret.x = x; ret.y = y;
  6215. return ret;
  6216. }
  6217. NK_API struct nk_vec2
  6218. nk_vec2i(int x, int y)
  6219. {
  6220. struct nk_vec2 ret;
  6221. ret.x = (float)x;
  6222. ret.y = (float)y;
  6223. return ret;
  6224. }
  6225. NK_API struct nk_vec2
  6226. nk_vec2v(const float *v)
  6227. {
  6228. return nk_vec2(v[0], v[1]);
  6229. }
  6230. NK_API struct nk_vec2
  6231. nk_vec2iv(const int *v)
  6232. {
  6233. return nk_vec2i(v[0], v[1]);
  6234. }
  6235. NK_LIB void
  6236. nk_unify(struct nk_rect *clip, const struct nk_rect *a, float x0, float y0,
  6237. float x1, float y1)
  6238. {
  6239. NK_ASSERT(a);
  6240. NK_ASSERT(clip);
  6241. clip->x = NK_MAX(a->x, x0);
  6242. clip->y = NK_MAX(a->y, y0);
  6243. clip->w = NK_MIN(a->x + a->w, x1) - clip->x;
  6244. clip->h = NK_MIN(a->y + a->h, y1) - clip->y;
  6245. clip->w = NK_MAX(0, clip->w);
  6246. clip->h = NK_MAX(0, clip->h);
  6247. }
  6248. NK_API void
  6249. nk_triangle_from_direction(struct nk_vec2 *result, struct nk_rect r,
  6250. float pad_x, float pad_y, enum nk_heading direction)
  6251. {
  6252. float w_half, h_half;
  6253. NK_ASSERT(result);
  6254. r.w = NK_MAX(2 * pad_x, r.w);
  6255. r.h = NK_MAX(2 * pad_y, r.h);
  6256. r.w = r.w - 2 * pad_x;
  6257. r.h = r.h - 2 * pad_y;
  6258. r.x = r.x + pad_x;
  6259. r.y = r.y + pad_y;
  6260. w_half = r.w / 2.0f;
  6261. h_half = r.h / 2.0f;
  6262. if (direction == NK_UP) {
  6263. result[0] = nk_vec2(r.x + w_half, r.y);
  6264. result[1] = nk_vec2(r.x + r.w, r.y + r.h);
  6265. result[2] = nk_vec2(r.x, r.y + r.h);
  6266. } else if (direction == NK_RIGHT) {
  6267. result[0] = nk_vec2(r.x, r.y);
  6268. result[1] = nk_vec2(r.x + r.w, r.y + h_half);
  6269. result[2] = nk_vec2(r.x, r.y + r.h);
  6270. } else if (direction == NK_DOWN) {
  6271. result[0] = nk_vec2(r.x, r.y);
  6272. result[1] = nk_vec2(r.x + r.w, r.y);
  6273. result[2] = nk_vec2(r.x + w_half, r.y + r.h);
  6274. } else {
  6275. result[0] = nk_vec2(r.x, r.y + h_half);
  6276. result[1] = nk_vec2(r.x + r.w, r.y);
  6277. result[2] = nk_vec2(r.x + r.w, r.y + r.h);
  6278. }
  6279. }
  6280. /* ===============================================================
  6281. *
  6282. * UTIL
  6283. *
  6284. * ===============================================================*/
  6285. NK_INTERN int nk_str_match_here(const char *regexp, const char *text);
  6286. NK_INTERN int nk_str_match_star(int c, const char *regexp, const char *text);
  6287. NK_LIB nk_bool nk_is_lower(int c) {return (c >= 'a' && c <= 'z') || (c >= 0xE0 && c <= 0xFF);}
  6288. NK_LIB nk_bool nk_is_upper(int c){return (c >= 'A' && c <= 'Z') || (c >= 0xC0 && c <= 0xDF);}
  6289. NK_LIB int nk_to_upper(int c) {return (c >= 'a' && c <= 'z') ? (c - ('a' - 'A')) : c;}
  6290. NK_LIB int nk_to_lower(int c) {return (c >= 'A' && c <= 'Z') ? (c - ('a' + 'A')) : c;}
  6291. #ifndef NK_MEMCPY
  6292. #define NK_MEMCPY nk_memcopy
  6293. NK_LIB void*
  6294. nk_memcopy(void *dst0, const void *src0, nk_size length)
  6295. {
  6296. nk_ptr t;
  6297. char *dst = (char*)dst0;
  6298. const char *src = (const char*)src0;
  6299. if (length == 0 || dst == src)
  6300. goto done;
  6301. #define nk_word int
  6302. #define nk_wsize sizeof(nk_word)
  6303. #define nk_wmask (nk_wsize-1)
  6304. #define NK_TLOOP(s) if (t) NK_TLOOP1(s)
  6305. #define NK_TLOOP1(s) do { s; } while (--t)
  6306. if (dst < src) {
  6307. t = (nk_ptr)src; /* only need low bits */
  6308. if ((t | (nk_ptr)dst) & nk_wmask) {
  6309. if ((t ^ (nk_ptr)dst) & nk_wmask || length < nk_wsize)
  6310. t = length;
  6311. else
  6312. t = nk_wsize - (t & nk_wmask);
  6313. length -= t;
  6314. NK_TLOOP1(*dst++ = *src++);
  6315. }
  6316. t = length / nk_wsize;
  6317. NK_TLOOP(*(nk_word*)(void*)dst = *(const nk_word*)(const void*)src;
  6318. src += nk_wsize; dst += nk_wsize);
  6319. t = length & nk_wmask;
  6320. NK_TLOOP(*dst++ = *src++);
  6321. } else {
  6322. src += length;
  6323. dst += length;
  6324. t = (nk_ptr)src;
  6325. if ((t | (nk_ptr)dst) & nk_wmask) {
  6326. if ((t ^ (nk_ptr)dst) & nk_wmask || length <= nk_wsize)
  6327. t = length;
  6328. else
  6329. t &= nk_wmask;
  6330. length -= t;
  6331. NK_TLOOP1(*--dst = *--src);
  6332. }
  6333. t = length / nk_wsize;
  6334. NK_TLOOP(src -= nk_wsize; dst -= nk_wsize;
  6335. *(nk_word*)(void*)dst = *(const nk_word*)(const void*)src);
  6336. t = length & nk_wmask;
  6337. NK_TLOOP(*--dst = *--src);
  6338. }
  6339. #undef nk_word
  6340. #undef nk_wsize
  6341. #undef nk_wmask
  6342. #undef NK_TLOOP
  6343. #undef NK_TLOOP1
  6344. done:
  6345. return (dst0);
  6346. }
  6347. #endif
  6348. #ifndef NK_MEMSET
  6349. #define NK_MEMSET nk_memset
  6350. NK_LIB void
  6351. nk_memset(void *ptr, int c0, nk_size size)
  6352. {
  6353. #define nk_word unsigned
  6354. #define nk_wsize sizeof(nk_word)
  6355. #define nk_wmask (nk_wsize - 1)
  6356. nk_byte *dst = (nk_byte*)ptr;
  6357. unsigned c = 0;
  6358. nk_size t = 0;
  6359. if ((c = (nk_byte)c0) != 0) {
  6360. c = (c << 8) | c; /* at least 16-bits */
  6361. if (sizeof(unsigned int) > 2)
  6362. c = (c << 16) | c; /* at least 32-bits*/
  6363. }
  6364. /* too small of a word count */
  6365. dst = (nk_byte*)ptr;
  6366. if (size < 3 * nk_wsize) {
  6367. while (size--) *dst++ = (nk_byte)c0;
  6368. return;
  6369. }
  6370. /* align destination */
  6371. if ((t = NK_PTR_TO_UINT(dst) & nk_wmask) != 0) {
  6372. t = nk_wsize -t;
  6373. size -= t;
  6374. do {
  6375. *dst++ = (nk_byte)c0;
  6376. } while (--t != 0);
  6377. }
  6378. /* fill word */
  6379. t = size / nk_wsize;
  6380. do {
  6381. *(nk_word*)((void*)dst) = c;
  6382. dst += nk_wsize;
  6383. } while (--t != 0);
  6384. /* fill trailing bytes */
  6385. t = (size & nk_wmask);
  6386. if (t != 0) {
  6387. do {
  6388. *dst++ = (nk_byte)c0;
  6389. } while (--t != 0);
  6390. }
  6391. #undef nk_word
  6392. #undef nk_wsize
  6393. #undef nk_wmask
  6394. }
  6395. #endif
  6396. NK_LIB void
  6397. nk_zero(void *ptr, nk_size size)
  6398. {
  6399. NK_ASSERT(ptr);
  6400. NK_MEMSET(ptr, 0, size);
  6401. }
  6402. NK_API int
  6403. nk_strlen(const char *str)
  6404. {
  6405. int siz = 0;
  6406. NK_ASSERT(str);
  6407. while (str && *str++ != '\0') siz++;
  6408. return siz;
  6409. }
  6410. NK_API int
  6411. nk_strtoi(const char *str, char **endptr)
  6412. {
  6413. int neg = 1;
  6414. const char *p = str;
  6415. int value = 0;
  6416. NK_ASSERT(str);
  6417. if (!str) return 0;
  6418. /* skip whitespace */
  6419. while (*p == ' ') p++;
  6420. if (*p == '-') {
  6421. neg = -1;
  6422. p++;
  6423. }
  6424. while (*p && *p >= '0' && *p <= '9') {
  6425. value = value * 10 + (int) (*p - '0');
  6426. p++;
  6427. }
  6428. if (endptr)
  6429. *endptr = (char *)p;
  6430. return neg*value;
  6431. }
  6432. NK_API double
  6433. nk_strtod(const char *str, char **endptr)
  6434. {
  6435. double m;
  6436. double neg = 1.0;
  6437. char *p = (char *)str;
  6438. double value = 0;
  6439. double number = 0;
  6440. NK_ASSERT(str);
  6441. if (!str) return 0;
  6442. /* skip whitespace */
  6443. while (*p == ' ') p++;
  6444. if (*p == '-') {
  6445. neg = -1.0;
  6446. p++;
  6447. }
  6448. while (*p && *p != '.' && *p != 'e') {
  6449. value = value * 10.0 + (double) (*p - '0');
  6450. p++;
  6451. }
  6452. if (*p == '.') {
  6453. p++;
  6454. for(m = 0.1; *p && *p != 'e'; p++ ) {
  6455. value = value + (double) (*p - '0') * m;
  6456. m *= 0.1;
  6457. }
  6458. }
  6459. if (*p == 'e') {
  6460. int i, pow, div;
  6461. p++;
  6462. if (*p == '-') {
  6463. div = nk_true;
  6464. p++;
  6465. } else if (*p == '+') {
  6466. div = nk_false;
  6467. p++;
  6468. } else div = nk_false;
  6469. for (pow = 0; *p; p++)
  6470. pow = pow * 10 + (int) (*p - '0');
  6471. for (m = 1.0, i = 0; i < pow; i++)
  6472. m *= 10.0;
  6473. if (div)
  6474. value /= m;
  6475. else value *= m;
  6476. }
  6477. number = value * neg;
  6478. if (endptr)
  6479. *endptr = p;
  6480. return number;
  6481. }
  6482. NK_API float
  6483. nk_strtof(const char *str, char **endptr)
  6484. {
  6485. float float_value;
  6486. double double_value;
  6487. double_value = NK_STRTOD(str, endptr);
  6488. float_value = (float)double_value;
  6489. return float_value;
  6490. }
  6491. NK_API int
  6492. nk_stricmp(const char *s1, const char *s2)
  6493. {
  6494. nk_int c1,c2,d;
  6495. do {
  6496. c1 = *s1++;
  6497. c2 = *s2++;
  6498. d = c1 - c2;
  6499. while (d) {
  6500. if (c1 <= 'Z' && c1 >= 'A') {
  6501. d += ('a' - 'A');
  6502. if (!d) break;
  6503. }
  6504. if (c2 <= 'Z' && c2 >= 'A') {
  6505. d -= ('a' - 'A');
  6506. if (!d) break;
  6507. }
  6508. return ((d >= 0) << 1) - 1;
  6509. }
  6510. } while (c1);
  6511. return 0;
  6512. }
  6513. NK_API int
  6514. nk_stricmpn(const char *s1, const char *s2, int n)
  6515. {
  6516. int c1,c2,d;
  6517. NK_ASSERT(n >= 0);
  6518. do {
  6519. c1 = *s1++;
  6520. c2 = *s2++;
  6521. if (!n--) return 0;
  6522. d = c1 - c2;
  6523. while (d) {
  6524. if (c1 <= 'Z' && c1 >= 'A') {
  6525. d += ('a' - 'A');
  6526. if (!d) break;
  6527. }
  6528. if (c2 <= 'Z' && c2 >= 'A') {
  6529. d -= ('a' - 'A');
  6530. if (!d) break;
  6531. }
  6532. return ((d >= 0) << 1) - 1;
  6533. }
  6534. } while (c1);
  6535. return 0;
  6536. }
  6537. NK_INTERN int
  6538. nk_str_match_here(const char *regexp, const char *text)
  6539. {
  6540. if (regexp[0] == '\0')
  6541. return 1;
  6542. if (regexp[1] == '*')
  6543. return nk_str_match_star(regexp[0], regexp+2, text);
  6544. if (regexp[0] == '$' && regexp[1] == '\0')
  6545. return *text == '\0';
  6546. if (*text!='\0' && (regexp[0]=='.' || regexp[0]==*text))
  6547. return nk_str_match_here(regexp+1, text+1);
  6548. return 0;
  6549. }
  6550. NK_INTERN int
  6551. nk_str_match_star(int c, const char *regexp, const char *text)
  6552. {
  6553. do {/* a '* matches zero or more instances */
  6554. if (nk_str_match_here(regexp, text))
  6555. return 1;
  6556. } while (*text != '\0' && (*text++ == c || c == '.'));
  6557. return 0;
  6558. }
  6559. NK_API int
  6560. nk_strfilter(const char *text, const char *regexp)
  6561. {
  6562. /*
  6563. c matches any literal character c
  6564. . matches any single character
  6565. ^ matches the beginning of the input string
  6566. $ matches the end of the input string
  6567. * matches zero or more occurrences of the previous character*/
  6568. if (regexp[0] == '^')
  6569. return nk_str_match_here(regexp+1, text);
  6570. do { /* must look even if string is empty */
  6571. if (nk_str_match_here(regexp, text))
  6572. return 1;
  6573. } while (*text++ != '\0');
  6574. return 0;
  6575. }
  6576. NK_API int
  6577. nk_strmatch_fuzzy_text(const char *str, int str_len,
  6578. const char *pattern, int *out_score)
  6579. {
  6580. /* Returns true if each character in pattern is found sequentially within str
  6581. * if found then out_score is also set. Score value has no intrinsic meaning.
  6582. * Range varies with pattern. Can only compare scores with same search pattern. */
  6583. /* bonus for adjacent matches */
  6584. #define NK_ADJACENCY_BONUS 5
  6585. /* bonus if match occurs after a separator */
  6586. #define NK_SEPARATOR_BONUS 10
  6587. /* bonus if match is uppercase and prev is lower */
  6588. #define NK_CAMEL_BONUS 10
  6589. /* penalty applied for every letter in str before the first match */
  6590. #define NK_LEADING_LETTER_PENALTY (-3)
  6591. /* maximum penalty for leading letters */
  6592. #define NK_MAX_LEADING_LETTER_PENALTY (-9)
  6593. /* penalty for every letter that doesn't matter */
  6594. #define NK_UNMATCHED_LETTER_PENALTY (-1)
  6595. /* loop variables */
  6596. int score = 0;
  6597. char const * pattern_iter = pattern;
  6598. int str_iter = 0;
  6599. int prev_matched = nk_false;
  6600. int prev_lower = nk_false;
  6601. /* true so if first letter match gets separator bonus*/
  6602. int prev_separator = nk_true;
  6603. /* use "best" matched letter if multiple string letters match the pattern */
  6604. char const * best_letter = 0;
  6605. int best_letter_score = 0;
  6606. /* loop over strings */
  6607. NK_ASSERT(str);
  6608. NK_ASSERT(pattern);
  6609. if (!str || !str_len || !pattern) return 0;
  6610. while (str_iter < str_len)
  6611. {
  6612. const char pattern_letter = *pattern_iter;
  6613. const char str_letter = str[str_iter];
  6614. int next_match = *pattern_iter != '\0' &&
  6615. nk_to_lower(pattern_letter) == nk_to_lower(str_letter);
  6616. int rematch = best_letter && nk_to_upper(*best_letter) == nk_to_upper(str_letter);
  6617. int advanced = next_match && best_letter;
  6618. int pattern_repeat = best_letter && *pattern_iter != '\0';
  6619. pattern_repeat = pattern_repeat &&
  6620. nk_to_lower(*best_letter) == nk_to_lower(pattern_letter);
  6621. if (advanced || pattern_repeat) {
  6622. score += best_letter_score;
  6623. best_letter = 0;
  6624. best_letter_score = 0;
  6625. }
  6626. if (next_match || rematch)
  6627. {
  6628. int new_score = 0;
  6629. /* Apply penalty for each letter before the first pattern match */
  6630. if (pattern_iter == pattern) {
  6631. int count = (int)(&str[str_iter] - str);
  6632. int penalty = NK_LEADING_LETTER_PENALTY * count;
  6633. if (penalty < NK_MAX_LEADING_LETTER_PENALTY)
  6634. penalty = NK_MAX_LEADING_LETTER_PENALTY;
  6635. score += penalty;
  6636. }
  6637. /* apply bonus for consecutive bonuses */
  6638. if (prev_matched)
  6639. new_score += NK_ADJACENCY_BONUS;
  6640. /* apply bonus for matches after a separator */
  6641. if (prev_separator)
  6642. new_score += NK_SEPARATOR_BONUS;
  6643. /* apply bonus across camel case boundaries */
  6644. if (prev_lower && nk_is_upper(str_letter))
  6645. new_score += NK_CAMEL_BONUS;
  6646. /* update pattern iter IFF the next pattern letter was matched */
  6647. if (next_match)
  6648. ++pattern_iter;
  6649. /* update best letter in str which may be for a "next" letter or a rematch */
  6650. if (new_score >= best_letter_score) {
  6651. /* apply penalty for now skipped letter */
  6652. if (best_letter != 0)
  6653. score += NK_UNMATCHED_LETTER_PENALTY;
  6654. best_letter = &str[str_iter];
  6655. best_letter_score = new_score;
  6656. }
  6657. prev_matched = nk_true;
  6658. } else {
  6659. score += NK_UNMATCHED_LETTER_PENALTY;
  6660. prev_matched = nk_false;
  6661. }
  6662. /* separators should be more easily defined */
  6663. prev_lower = nk_is_lower(str_letter) != 0;
  6664. prev_separator = str_letter == '_' || str_letter == ' ';
  6665. ++str_iter;
  6666. }
  6667. /* apply score for last match */
  6668. if (best_letter)
  6669. score += best_letter_score;
  6670. /* did not match full pattern */
  6671. if (*pattern_iter != '\0')
  6672. return nk_false;
  6673. if (out_score)
  6674. *out_score = score;
  6675. return nk_true;
  6676. }
  6677. NK_API int
  6678. nk_strmatch_fuzzy_string(char const *str, char const *pattern, int *out_score)
  6679. {
  6680. return nk_strmatch_fuzzy_text(str, nk_strlen(str), pattern, out_score);
  6681. }
  6682. NK_LIB int
  6683. nk_string_float_limit(char *string, int prec)
  6684. {
  6685. int dot = 0;
  6686. char *c = string;
  6687. while (*c) {
  6688. if (*c == '.') {
  6689. dot = 1;
  6690. c++;
  6691. continue;
  6692. }
  6693. if (dot == (prec+1)) {
  6694. *c = 0;
  6695. break;
  6696. }
  6697. if (dot > 0) dot++;
  6698. c++;
  6699. }
  6700. return (int)(c - string);
  6701. }
  6702. NK_INTERN void
  6703. nk_strrev_ascii(char *s)
  6704. {
  6705. int len = nk_strlen(s);
  6706. int end = len / 2;
  6707. int i = 0;
  6708. char t;
  6709. for (; i < end; ++i) {
  6710. t = s[i];
  6711. s[i] = s[len - 1 - i];
  6712. s[len -1 - i] = t;
  6713. }
  6714. }
  6715. NK_LIB char*
  6716. nk_itoa(char *s, long n)
  6717. {
  6718. long i = 0;
  6719. if (n == 0) {
  6720. s[i++] = '0';
  6721. s[i] = 0;
  6722. return s;
  6723. }
  6724. if (n < 0) {
  6725. s[i++] = '-';
  6726. n = -n;
  6727. }
  6728. while (n > 0) {
  6729. s[i++] = (char)('0' + (n % 10));
  6730. n /= 10;
  6731. }
  6732. s[i] = 0;
  6733. if (s[0] == '-')
  6734. ++s;
  6735. nk_strrev_ascii(s);
  6736. return s;
  6737. }
  6738. #ifndef NK_DTOA
  6739. #define NK_DTOA nk_dtoa
  6740. NK_LIB char*
  6741. nk_dtoa(char *s, double n)
  6742. {
  6743. int useExp = 0;
  6744. int digit = 0, m = 0, m1 = 0;
  6745. char *c = s;
  6746. int neg = 0;
  6747. NK_ASSERT(s);
  6748. if (!s) return 0;
  6749. if (n == 0.0) {
  6750. s[0] = '0'; s[1] = '\0';
  6751. return s;
  6752. }
  6753. neg = (n < 0);
  6754. if (neg) n = -n;
  6755. /* calculate magnitude */
  6756. m = nk_log10(n);
  6757. useExp = (m >= 14 || (neg && m >= 9) || m <= -9);
  6758. if (neg) *(c++) = '-';
  6759. /* set up for scientific notation */
  6760. if (useExp) {
  6761. if (m < 0)
  6762. m -= 1;
  6763. n = n / (double)nk_pow(10.0, m);
  6764. m1 = m;
  6765. m = 0;
  6766. }
  6767. if (m < 1.0) {
  6768. m = 0;
  6769. }
  6770. /* convert the number */
  6771. while (n > NK_FLOAT_PRECISION || m >= 0) {
  6772. double weight = nk_pow(10.0, m);
  6773. if (weight > 0) {
  6774. double t = (double)n / weight;
  6775. digit = nk_ifloord(t);
  6776. n -= ((double)digit * weight);
  6777. *(c++) = (char)('0' + (char)digit);
  6778. }
  6779. if (m == 0 && n > 0)
  6780. *(c++) = '.';
  6781. m--;
  6782. }
  6783. if (useExp) {
  6784. /* convert the exponent */
  6785. int i, j;
  6786. *(c++) = 'e';
  6787. if (m1 > 0) {
  6788. *(c++) = '+';
  6789. } else {
  6790. *(c++) = '-';
  6791. m1 = -m1;
  6792. }
  6793. m = 0;
  6794. while (m1 > 0) {
  6795. *(c++) = (char)('0' + (char)(m1 % 10));
  6796. m1 /= 10;
  6797. m++;
  6798. }
  6799. c -= m;
  6800. for (i = 0, j = m-1; i<j; i++, j--) {
  6801. /* swap without temporary */
  6802. c[i] ^= c[j];
  6803. c[j] ^= c[i];
  6804. c[i] ^= c[j];
  6805. }
  6806. c += m;
  6807. }
  6808. *(c) = '\0';
  6809. return s;
  6810. }
  6811. #endif
  6812. #ifdef NK_INCLUDE_STANDARD_VARARGS
  6813. #ifndef NK_INCLUDE_STANDARD_IO
  6814. NK_INTERN int
  6815. nk_vsnprintf(char *buf, int buf_size, const char *fmt, va_list args)
  6816. {
  6817. enum nk_arg_type {
  6818. NK_ARG_TYPE_CHAR,
  6819. NK_ARG_TYPE_SHORT,
  6820. NK_ARG_TYPE_DEFAULT,
  6821. NK_ARG_TYPE_LONG
  6822. };
  6823. enum nk_arg_flags {
  6824. NK_ARG_FLAG_LEFT = 0x01,
  6825. NK_ARG_FLAG_PLUS = 0x02,
  6826. NK_ARG_FLAG_SPACE = 0x04,
  6827. NK_ARG_FLAG_NUM = 0x10,
  6828. NK_ARG_FLAG_ZERO = 0x20
  6829. };
  6830. char number_buffer[NK_MAX_NUMBER_BUFFER];
  6831. enum nk_arg_type arg_type = NK_ARG_TYPE_DEFAULT;
  6832. int precision = NK_DEFAULT;
  6833. int width = NK_DEFAULT;
  6834. nk_flags flag = 0;
  6835. int len = 0;
  6836. int result = -1;
  6837. const char *iter = fmt;
  6838. NK_ASSERT(buf);
  6839. NK_ASSERT(buf_size);
  6840. if (!buf || !buf_size || !fmt) return 0;
  6841. for (iter = fmt; *iter && len < buf_size; iter++) {
  6842. /* copy all non-format characters */
  6843. while (*iter && (*iter != '%') && (len < buf_size))
  6844. buf[len++] = *iter++;
  6845. if (!(*iter) || len >= buf_size) break;
  6846. iter++;
  6847. /* flag arguments */
  6848. while (*iter) {
  6849. if (*iter == '-') flag |= NK_ARG_FLAG_LEFT;
  6850. else if (*iter == '+') flag |= NK_ARG_FLAG_PLUS;
  6851. else if (*iter == ' ') flag |= NK_ARG_FLAG_SPACE;
  6852. else if (*iter == '#') flag |= NK_ARG_FLAG_NUM;
  6853. else if (*iter == '0') flag |= NK_ARG_FLAG_ZERO;
  6854. else break;
  6855. iter++;
  6856. }
  6857. /* width argument */
  6858. width = NK_DEFAULT;
  6859. if (*iter >= '1' && *iter <= '9') {
  6860. char *end;
  6861. width = nk_strtoi(iter, &end);
  6862. if (end == iter)
  6863. width = -1;
  6864. else iter = end;
  6865. } else if (*iter == '*') {
  6866. width = va_arg(args, int);
  6867. iter++;
  6868. }
  6869. /* precision argument */
  6870. precision = NK_DEFAULT;
  6871. if (*iter == '.') {
  6872. iter++;
  6873. if (*iter == '*') {
  6874. precision = va_arg(args, int);
  6875. iter++;
  6876. } else {
  6877. char *end;
  6878. precision = nk_strtoi(iter, &end);
  6879. if (end == iter)
  6880. precision = -1;
  6881. else iter = end;
  6882. }
  6883. }
  6884. /* length modifier */
  6885. if (*iter == 'h') {
  6886. if (*(iter+1) == 'h') {
  6887. arg_type = NK_ARG_TYPE_CHAR;
  6888. iter++;
  6889. } else arg_type = NK_ARG_TYPE_SHORT;
  6890. iter++;
  6891. } else if (*iter == 'l') {
  6892. arg_type = NK_ARG_TYPE_LONG;
  6893. iter++;
  6894. } else arg_type = NK_ARG_TYPE_DEFAULT;
  6895. /* specifier */
  6896. if (*iter == '%') {
  6897. NK_ASSERT(arg_type == NK_ARG_TYPE_DEFAULT);
  6898. NK_ASSERT(precision == NK_DEFAULT);
  6899. NK_ASSERT(width == NK_DEFAULT);
  6900. if (len < buf_size)
  6901. buf[len++] = '%';
  6902. } else if (*iter == 's') {
  6903. /* string */
  6904. const char *str = va_arg(args, const char*);
  6905. NK_ASSERT(str != buf && "buffer and argument are not allowed to overlap!");
  6906. NK_ASSERT(arg_type == NK_ARG_TYPE_DEFAULT);
  6907. NK_ASSERT(precision == NK_DEFAULT);
  6908. NK_ASSERT(width == NK_DEFAULT);
  6909. if (str == buf) return -1;
  6910. while (str && *str && len < buf_size)
  6911. buf[len++] = *str++;
  6912. } else if (*iter == 'n') {
  6913. /* current length callback */
  6914. signed int *n = va_arg(args, int*);
  6915. NK_ASSERT(arg_type == NK_ARG_TYPE_DEFAULT);
  6916. NK_ASSERT(precision == NK_DEFAULT);
  6917. NK_ASSERT(width == NK_DEFAULT);
  6918. if (n) *n = len;
  6919. } else if (*iter == 'c' || *iter == 'i' || *iter == 'd') {
  6920. /* signed integer */
  6921. long value = 0;
  6922. const char *num_iter;
  6923. int num_len, num_print, padding;
  6924. int cur_precision = NK_MAX(precision, 1);
  6925. int cur_width = NK_MAX(width, 0);
  6926. /* retrieve correct value type */
  6927. if (arg_type == NK_ARG_TYPE_CHAR)
  6928. value = (signed char)va_arg(args, int);
  6929. else if (arg_type == NK_ARG_TYPE_SHORT)
  6930. value = (signed short)va_arg(args, int);
  6931. else if (arg_type == NK_ARG_TYPE_LONG)
  6932. value = va_arg(args, signed long);
  6933. else if (*iter == 'c')
  6934. value = (unsigned char)va_arg(args, int);
  6935. else value = va_arg(args, signed int);
  6936. /* convert number to string */
  6937. nk_itoa(number_buffer, value);
  6938. num_len = nk_strlen(number_buffer);
  6939. padding = NK_MAX(cur_width - NK_MAX(cur_precision, num_len), 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) && (precision == NK_DEFAULT))
  6946. buf[len++] = '0';
  6947. else buf[len++] = ' ';
  6948. }
  6949. }
  6950. /* copy string value representation into buffer */
  6951. if ((flag & NK_ARG_FLAG_PLUS) && value >= 0 && len < buf_size)
  6952. buf[len++] = '+';
  6953. else if ((flag & NK_ARG_FLAG_SPACE) && value >= 0 && len < buf_size)
  6954. buf[len++] = ' ';
  6955. /* fill up to precision number of digits with '0' */
  6956. num_print = NK_MAX(cur_precision, num_len);
  6957. while (precision && (num_print > num_len) && (len < buf_size)) {
  6958. buf[len++] = '0';
  6959. num_print--;
  6960. }
  6961. /* copy string value representation into buffer */
  6962. num_iter = number_buffer;
  6963. while (precision && *num_iter && len < buf_size)
  6964. buf[len++] = *num_iter++;
  6965. /* fill right padding up to width characters */
  6966. if (flag & NK_ARG_FLAG_LEFT) {
  6967. while ((padding-- > 0) && (len < buf_size))
  6968. buf[len++] = ' ';
  6969. }
  6970. } else if (*iter == 'o' || *iter == 'x' || *iter == 'X' || *iter == 'u') {
  6971. /* unsigned integer */
  6972. unsigned long value = 0;
  6973. int num_len = 0, num_print, padding = 0;
  6974. int cur_precision = NK_MAX(precision, 1);
  6975. int cur_width = NK_MAX(width, 0);
  6976. unsigned int base = (*iter == 'o') ? 8: (*iter == 'u')? 10: 16;
  6977. /* print oct/hex/dec value */
  6978. const char *upper_output_format = "0123456789ABCDEF";
  6979. const char *lower_output_format = "0123456789abcdef";
  6980. const char *output_format = (*iter == 'x') ?
  6981. lower_output_format: upper_output_format;
  6982. /* retrieve correct value type */
  6983. if (arg_type == NK_ARG_TYPE_CHAR)
  6984. value = (unsigned char)va_arg(args, int);
  6985. else if (arg_type == NK_ARG_TYPE_SHORT)
  6986. value = (unsigned short)va_arg(args, int);
  6987. else if (arg_type == NK_ARG_TYPE_LONG)
  6988. value = va_arg(args, unsigned long);
  6989. else value = va_arg(args, unsigned int);
  6990. do {
  6991. /* convert decimal number into hex/oct number */
  6992. int digit = output_format[value % base];
  6993. if (num_len < NK_MAX_NUMBER_BUFFER)
  6994. number_buffer[num_len++] = (char)digit;
  6995. value /= base;
  6996. } while (value > 0);
  6997. num_print = NK_MAX(cur_precision, num_len);
  6998. padding = NK_MAX(cur_width - NK_MAX(cur_precision, num_len), 0);
  6999. if (flag & NK_ARG_FLAG_NUM)
  7000. padding = NK_MAX(padding-1, 0);
  7001. /* fill left padding up to a total of `width` characters */
  7002. if (!(flag & NK_ARG_FLAG_LEFT)) {
  7003. while ((padding-- > 0) && (len < buf_size)) {
  7004. if ((flag & NK_ARG_FLAG_ZERO) && (precision == NK_DEFAULT))
  7005. buf[len++] = '0';
  7006. else buf[len++] = ' ';
  7007. }
  7008. }
  7009. /* fill up to precision number of digits */
  7010. if (num_print && (flag & NK_ARG_FLAG_NUM)) {
  7011. if ((*iter == 'o') && (len < buf_size)) {
  7012. buf[len++] = '0';
  7013. } else if ((*iter == 'x') && ((len+1) < buf_size)) {
  7014. buf[len++] = '0';
  7015. buf[len++] = 'x';
  7016. } else if ((*iter == 'X') && ((len+1) < buf_size)) {
  7017. buf[len++] = '0';
  7018. buf[len++] = 'X';
  7019. }
  7020. }
  7021. while (precision && (num_print > num_len) && (len < buf_size)) {
  7022. buf[len++] = '0';
  7023. num_print--;
  7024. }
  7025. /* reverse number direction */
  7026. while (num_len > 0) {
  7027. if (precision && (len < buf_size))
  7028. buf[len++] = number_buffer[num_len-1];
  7029. num_len--;
  7030. }
  7031. /* fill right padding up to width characters */
  7032. if (flag & NK_ARG_FLAG_LEFT) {
  7033. while ((padding-- > 0) && (len < buf_size))
  7034. buf[len++] = ' ';
  7035. }
  7036. } else if (*iter == 'f') {
  7037. /* floating point */
  7038. const char *num_iter;
  7039. int cur_precision = (precision < 0) ? 6: precision;
  7040. int prefix, cur_width = NK_MAX(width, 0);
  7041. double value = va_arg(args, double);
  7042. int num_len = 0, frac_len = 0, dot = 0;
  7043. int padding = 0;
  7044. NK_ASSERT(arg_type == NK_ARG_TYPE_DEFAULT);
  7045. NK_DTOA(number_buffer, value);
  7046. num_len = nk_strlen(number_buffer);
  7047. /* calculate padding */
  7048. num_iter = number_buffer;
  7049. while (*num_iter && *num_iter != '.')
  7050. num_iter++;
  7051. prefix = (*num_iter == '.')?(int)(num_iter - number_buffer)+1:0;
  7052. padding = NK_MAX(cur_width - (prefix + NK_MIN(cur_precision, num_len - prefix)) , 0);
  7053. if ((flag & NK_ARG_FLAG_PLUS) || (flag & NK_ARG_FLAG_SPACE))
  7054. padding = NK_MAX(padding-1, 0);
  7055. /* fill left padding up to a total of `width` characters */
  7056. if (!(flag & NK_ARG_FLAG_LEFT)) {
  7057. while (padding-- > 0 && (len < buf_size)) {
  7058. if (flag & NK_ARG_FLAG_ZERO)
  7059. buf[len++] = '0';
  7060. else buf[len++] = ' ';
  7061. }
  7062. }
  7063. /* copy string value representation into buffer */
  7064. num_iter = number_buffer;
  7065. if ((flag & NK_ARG_FLAG_PLUS) && (value >= 0) && (len < buf_size))
  7066. buf[len++] = '+';
  7067. else if ((flag & NK_ARG_FLAG_SPACE) && (value >= 0) && (len < buf_size))
  7068. buf[len++] = ' ';
  7069. while (*num_iter) {
  7070. if (dot) frac_len++;
  7071. if (len < buf_size)
  7072. buf[len++] = *num_iter;
  7073. if (*num_iter == '.') dot = 1;
  7074. if (frac_len >= cur_precision) break;
  7075. num_iter++;
  7076. }
  7077. /* fill number up to precision */
  7078. while (frac_len < cur_precision) {
  7079. if (!dot && len < buf_size) {
  7080. buf[len++] = '.';
  7081. dot = 1;
  7082. }
  7083. if (len < buf_size)
  7084. buf[len++] = '0';
  7085. frac_len++;
  7086. }
  7087. /* fill right padding up to width characters */
  7088. if (flag & NK_ARG_FLAG_LEFT) {
  7089. while ((padding-- > 0) && (len < buf_size))
  7090. buf[len++] = ' ';
  7091. }
  7092. } else {
  7093. /* Specifier not supported: g,G,e,E,p,z */
  7094. NK_ASSERT(0 && "specifier is not supported!");
  7095. return result;
  7096. }
  7097. }
  7098. buf[(len >= buf_size)?(buf_size-1):len] = 0;
  7099. result = (len >= buf_size)?-1:len;
  7100. return result;
  7101. }
  7102. #endif
  7103. NK_LIB int
  7104. nk_strfmt(char *buf, int buf_size, const char *fmt, va_list args)
  7105. {
  7106. int result = -1;
  7107. NK_ASSERT(buf);
  7108. NK_ASSERT(buf_size);
  7109. if (!buf || !buf_size || !fmt) return 0;
  7110. #ifdef NK_INCLUDE_STANDARD_IO
  7111. result = NK_VSNPRINTF(buf, (nk_size)buf_size, fmt, args);
  7112. result = (result >= buf_size) ? -1: result;
  7113. buf[buf_size-1] = 0;
  7114. #else
  7115. result = nk_vsnprintf(buf, buf_size, fmt, args);
  7116. #endif
  7117. return result;
  7118. }
  7119. #endif
  7120. NK_API nk_hash
  7121. nk_murmur_hash(const void * key, int len, nk_hash seed)
  7122. {
  7123. /* 32-Bit MurmurHash3: https://code.google.com/p/smhasher/wiki/MurmurHash3*/
  7124. #define NK_ROTL(x,r) ((x) << (r) | ((x) >> (32 - r)))
  7125. nk_uint h1 = seed;
  7126. nk_uint k1;
  7127. const nk_byte *data = (const nk_byte*)key;
  7128. const nk_byte *keyptr = data;
  7129. nk_byte *k1ptr;
  7130. const int bsize = sizeof(k1);
  7131. const int nblocks = len/4;
  7132. const nk_uint c1 = 0xcc9e2d51;
  7133. const nk_uint c2 = 0x1b873593;
  7134. const nk_byte *tail;
  7135. int i;
  7136. /* body */
  7137. if (!key) return 0;
  7138. for (i = 0; i < nblocks; ++i, keyptr += bsize) {
  7139. k1ptr = (nk_byte*)&k1;
  7140. k1ptr[0] = keyptr[0];
  7141. k1ptr[1] = keyptr[1];
  7142. k1ptr[2] = keyptr[2];
  7143. k1ptr[3] = keyptr[3];
  7144. k1 *= c1;
  7145. k1 = NK_ROTL(k1,15);
  7146. k1 *= c2;
  7147. h1 ^= k1;
  7148. h1 = NK_ROTL(h1,13);
  7149. h1 = h1*5+0xe6546b64;
  7150. }
  7151. /* tail */
  7152. tail = (const nk_byte*)(data + nblocks*4);
  7153. k1 = 0;
  7154. switch (len & 3) {
  7155. case 3: k1 ^= (nk_uint)(tail[2] << 16); /* fallthrough */
  7156. case 2: k1 ^= (nk_uint)(tail[1] << 8u); /* fallthrough */
  7157. case 1: k1 ^= tail[0];
  7158. k1 *= c1;
  7159. k1 = NK_ROTL(k1,15);
  7160. k1 *= c2;
  7161. h1 ^= k1;
  7162. break;
  7163. default: break;
  7164. }
  7165. /* finalization */
  7166. h1 ^= (nk_uint)len;
  7167. /* fmix32 */
  7168. h1 ^= h1 >> 16;
  7169. h1 *= 0x85ebca6b;
  7170. h1 ^= h1 >> 13;
  7171. h1 *= 0xc2b2ae35;
  7172. h1 ^= h1 >> 16;
  7173. #undef NK_ROTL
  7174. return h1;
  7175. }
  7176. #ifdef NK_INCLUDE_STANDARD_IO
  7177. NK_LIB char*
  7178. nk_file_load(const char* path, nk_size* siz, const struct nk_allocator *alloc)
  7179. {
  7180. char *buf;
  7181. FILE *fd;
  7182. long ret;
  7183. NK_ASSERT(path);
  7184. NK_ASSERT(siz);
  7185. NK_ASSERT(alloc);
  7186. if (!path || !siz || !alloc)
  7187. return 0;
  7188. fd = fopen(path, "rb");
  7189. if (!fd) return 0;
  7190. fseek(fd, 0, SEEK_END);
  7191. ret = ftell(fd);
  7192. if (ret < 0) {
  7193. fclose(fd);
  7194. return 0;
  7195. }
  7196. *siz = (nk_size)ret;
  7197. fseek(fd, 0, SEEK_SET);
  7198. buf = (char*)alloc->alloc(alloc->userdata,0, *siz);
  7199. NK_ASSERT(buf);
  7200. if (!buf) {
  7201. fclose(fd);
  7202. return 0;
  7203. }
  7204. *siz = (nk_size)fread(buf, 1,*siz, fd);
  7205. fclose(fd);
  7206. return buf;
  7207. }
  7208. #endif
  7209. NK_LIB int
  7210. nk_text_clamp(const struct nk_user_font *font, const char *text,
  7211. int text_len, float space, int *glyphs, float *text_width,
  7212. nk_rune *sep_list, int sep_count)
  7213. {
  7214. int i = 0;
  7215. int glyph_len = 0;
  7216. float last_width = 0;
  7217. nk_rune unicode = 0;
  7218. float width = 0;
  7219. int len = 0;
  7220. int g = 0;
  7221. float s;
  7222. int sep_len = 0;
  7223. int sep_g = 0;
  7224. float sep_width = 0;
  7225. sep_count = NK_MAX(sep_count,0);
  7226. glyph_len = nk_utf_decode(text, &unicode, text_len);
  7227. while (glyph_len && (width < space) && (len < text_len)) {
  7228. len += glyph_len;
  7229. s = font->width(font->userdata, font->height, text, len);
  7230. for (i = 0; i < sep_count; ++i) {
  7231. if (unicode != sep_list[i]) continue;
  7232. sep_width = last_width = width;
  7233. sep_g = g+1;
  7234. sep_len = len;
  7235. break;
  7236. }
  7237. if (i == sep_count){
  7238. last_width = sep_width = width;
  7239. sep_g = g+1;
  7240. }
  7241. width = s;
  7242. glyph_len = nk_utf_decode(&text[len], &unicode, text_len - len);
  7243. g++;
  7244. }
  7245. if (len >= text_len) {
  7246. *glyphs = g;
  7247. *text_width = last_width;
  7248. return len;
  7249. } else {
  7250. *glyphs = sep_g;
  7251. *text_width = sep_width;
  7252. return (!sep_len) ? len: sep_len;
  7253. }
  7254. }
  7255. NK_LIB struct nk_vec2
  7256. nk_text_calculate_text_bounds(const struct nk_user_font *font,
  7257. const char *begin, int byte_len, float row_height, const char **remaining,
  7258. struct nk_vec2 *out_offset, int *glyphs, int op)
  7259. {
  7260. float line_height = row_height;
  7261. struct nk_vec2 text_size = nk_vec2(0,0);
  7262. float line_width = 0.0f;
  7263. float glyph_width;
  7264. int glyph_len = 0;
  7265. nk_rune unicode = 0;
  7266. int text_len = 0;
  7267. if (!begin || byte_len <= 0 || !font)
  7268. return nk_vec2(0,row_height);
  7269. glyph_len = nk_utf_decode(begin, &unicode, byte_len);
  7270. if (!glyph_len) return text_size;
  7271. glyph_width = font->width(font->userdata, font->height, begin, glyph_len);
  7272. *glyphs = 0;
  7273. while ((text_len < byte_len) && glyph_len) {
  7274. if (unicode == '\n') {
  7275. text_size.x = NK_MAX(text_size.x, line_width);
  7276. text_size.y += line_height;
  7277. line_width = 0;
  7278. *glyphs+=1;
  7279. if (op == NK_STOP_ON_NEW_LINE)
  7280. break;
  7281. text_len++;
  7282. glyph_len = nk_utf_decode(begin + text_len, &unicode, byte_len-text_len);
  7283. continue;
  7284. }
  7285. if (unicode == '\r') {
  7286. text_len++;
  7287. *glyphs+=1;
  7288. glyph_len = nk_utf_decode(begin + text_len, &unicode, byte_len-text_len);
  7289. continue;
  7290. }
  7291. *glyphs = *glyphs + 1;
  7292. text_len += glyph_len;
  7293. line_width += (float)glyph_width;
  7294. glyph_len = nk_utf_decode(begin + text_len, &unicode, byte_len-text_len);
  7295. glyph_width = font->width(font->userdata, font->height, begin+text_len, glyph_len);
  7296. continue;
  7297. }
  7298. if (text_size.x < line_width)
  7299. text_size.x = line_width;
  7300. if (out_offset)
  7301. *out_offset = nk_vec2(line_width, text_size.y + line_height);
  7302. if (line_width > 0 || text_size.y == 0.0f)
  7303. text_size.y += line_height;
  7304. if (remaining)
  7305. *remaining = begin+text_len;
  7306. return text_size;
  7307. }
  7308. /* ==============================================================
  7309. *
  7310. * COLOR
  7311. *
  7312. * ===============================================================*/
  7313. NK_INTERN int
  7314. nk_parse_hex(const char *p, int length)
  7315. {
  7316. int i = 0;
  7317. int len = 0;
  7318. while (len < length) {
  7319. i <<= 4;
  7320. if (p[len] >= 'a' && p[len] <= 'f')
  7321. i += ((p[len] - 'a') + 10);
  7322. else if (p[len] >= 'A' && p[len] <= 'F')
  7323. i += ((p[len] - 'A') + 10);
  7324. else i += (p[len] - '0');
  7325. len++;
  7326. }
  7327. return i;
  7328. }
  7329. NK_API struct nk_color
  7330. nk_rgb_factor(struct nk_color col, float factor)
  7331. {
  7332. if (factor == 1.0f)
  7333. return col;
  7334. col.r = (nk_byte)(col.r * factor);
  7335. col.g = (nk_byte)(col.g * factor);
  7336. col.b = (nk_byte)(col.b * factor);
  7337. return col;
  7338. }
  7339. NK_API struct nk_color
  7340. nk_rgba(int r, int g, int b, int a)
  7341. {
  7342. struct nk_color ret;
  7343. ret.r = (nk_byte)NK_CLAMP(0, r, 255);
  7344. ret.g = (nk_byte)NK_CLAMP(0, g, 255);
  7345. ret.b = (nk_byte)NK_CLAMP(0, b, 255);
  7346. ret.a = (nk_byte)NK_CLAMP(0, a, 255);
  7347. return ret;
  7348. }
  7349. NK_API struct nk_color
  7350. nk_rgb_hex(const char *rgb)
  7351. {
  7352. struct nk_color col;
  7353. const char *c = rgb;
  7354. if (*c == '#') c++;
  7355. col.r = (nk_byte)nk_parse_hex(c, 2);
  7356. col.g = (nk_byte)nk_parse_hex(c+2, 2);
  7357. col.b = (nk_byte)nk_parse_hex(c+4, 2);
  7358. col.a = 255;
  7359. return col;
  7360. }
  7361. NK_API struct nk_color
  7362. nk_rgba_hex(const char *rgb)
  7363. {
  7364. struct nk_color col;
  7365. const char *c = rgb;
  7366. if (*c == '#') c++;
  7367. col.r = (nk_byte)nk_parse_hex(c, 2);
  7368. col.g = (nk_byte)nk_parse_hex(c+2, 2);
  7369. col.b = (nk_byte)nk_parse_hex(c+4, 2);
  7370. col.a = (nk_byte)nk_parse_hex(c+6, 2);
  7371. return col;
  7372. }
  7373. NK_API void
  7374. nk_color_hex_rgba(char *output, struct nk_color col)
  7375. {
  7376. #define NK_TO_HEX(i) ((i) <= 9 ? '0' + (i): 'A' - 10 + (i))
  7377. output[0] = (char)NK_TO_HEX((col.r & 0xF0) >> 4);
  7378. output[1] = (char)NK_TO_HEX((col.r & 0x0F));
  7379. output[2] = (char)NK_TO_HEX((col.g & 0xF0) >> 4);
  7380. output[3] = (char)NK_TO_HEX((col.g & 0x0F));
  7381. output[4] = (char)NK_TO_HEX((col.b & 0xF0) >> 4);
  7382. output[5] = (char)NK_TO_HEX((col.b & 0x0F));
  7383. output[6] = (char)NK_TO_HEX((col.a & 0xF0) >> 4);
  7384. output[7] = (char)NK_TO_HEX((col.a & 0x0F));
  7385. output[8] = '\0';
  7386. #undef NK_TO_HEX
  7387. }
  7388. NK_API void
  7389. nk_color_hex_rgb(char *output, struct nk_color col)
  7390. {
  7391. #define NK_TO_HEX(i) ((i) <= 9 ? '0' + (i): 'A' - 10 + (i))
  7392. output[0] = (char)NK_TO_HEX((col.r & 0xF0) >> 4);
  7393. output[1] = (char)NK_TO_HEX((col.r & 0x0F));
  7394. output[2] = (char)NK_TO_HEX((col.g & 0xF0) >> 4);
  7395. output[3] = (char)NK_TO_HEX((col.g & 0x0F));
  7396. output[4] = (char)NK_TO_HEX((col.b & 0xF0) >> 4);
  7397. output[5] = (char)NK_TO_HEX((col.b & 0x0F));
  7398. output[6] = '\0';
  7399. #undef NK_TO_HEX
  7400. }
  7401. NK_API struct nk_color
  7402. nk_rgba_iv(const int *c)
  7403. {
  7404. return nk_rgba(c[0], c[1], c[2], c[3]);
  7405. }
  7406. NK_API struct nk_color
  7407. nk_rgba_bv(const nk_byte *c)
  7408. {
  7409. return nk_rgba(c[0], c[1], c[2], c[3]);
  7410. }
  7411. NK_API struct nk_color
  7412. nk_rgb(int r, int g, int b)
  7413. {
  7414. struct nk_color ret;
  7415. ret.r = (nk_byte)NK_CLAMP(0, r, 255);
  7416. ret.g = (nk_byte)NK_CLAMP(0, g, 255);
  7417. ret.b = (nk_byte)NK_CLAMP(0, b, 255);
  7418. ret.a = (nk_byte)255;
  7419. return ret;
  7420. }
  7421. NK_API struct nk_color
  7422. nk_rgb_iv(const int *c)
  7423. {
  7424. return nk_rgb(c[0], c[1], c[2]);
  7425. }
  7426. NK_API struct nk_color
  7427. nk_rgb_bv(const nk_byte* c)
  7428. {
  7429. return nk_rgb(c[0], c[1], c[2]);
  7430. }
  7431. NK_API struct nk_color
  7432. nk_rgba_u32(nk_uint in)
  7433. {
  7434. struct nk_color ret;
  7435. ret.r = (in & 0xFF);
  7436. ret.g = ((in >> 8) & 0xFF);
  7437. ret.b = ((in >> 16) & 0xFF);
  7438. ret.a = (nk_byte)((in >> 24) & 0xFF);
  7439. return ret;
  7440. }
  7441. NK_API struct nk_color
  7442. nk_rgba_f(float r, float g, float b, float a)
  7443. {
  7444. struct nk_color ret;
  7445. ret.r = (nk_byte)(NK_SATURATE(r) * 255.0f);
  7446. ret.g = (nk_byte)(NK_SATURATE(g) * 255.0f);
  7447. ret.b = (nk_byte)(NK_SATURATE(b) * 255.0f);
  7448. ret.a = (nk_byte)(NK_SATURATE(a) * 255.0f);
  7449. return ret;
  7450. }
  7451. NK_API struct nk_color
  7452. nk_rgba_fv(const float *c)
  7453. {
  7454. return nk_rgba_f(c[0], c[1], c[2], c[3]);
  7455. }
  7456. NK_API struct nk_color
  7457. nk_rgba_cf(struct nk_colorf c)
  7458. {
  7459. return nk_rgba_f(c.r, c.g, c.b, c.a);
  7460. }
  7461. NK_API struct nk_color
  7462. nk_rgb_f(float r, float g, float b)
  7463. {
  7464. struct nk_color ret;
  7465. ret.r = (nk_byte)(NK_SATURATE(r) * 255.0f);
  7466. ret.g = (nk_byte)(NK_SATURATE(g) * 255.0f);
  7467. ret.b = (nk_byte)(NK_SATURATE(b) * 255.0f);
  7468. ret.a = 255;
  7469. return ret;
  7470. }
  7471. NK_API struct nk_color
  7472. nk_rgb_fv(const float *c)
  7473. {
  7474. return nk_rgb_f(c[0], c[1], c[2]);
  7475. }
  7476. NK_API struct nk_color
  7477. nk_rgb_cf(struct nk_colorf c)
  7478. {
  7479. return nk_rgb_f(c.r, c.g, c.b);
  7480. }
  7481. NK_API struct nk_color
  7482. nk_hsv(int h, int s, int v)
  7483. {
  7484. return nk_hsva(h, s, v, 255);
  7485. }
  7486. NK_API struct nk_color
  7487. nk_hsv_iv(const int *c)
  7488. {
  7489. return nk_hsv(c[0], c[1], c[2]);
  7490. }
  7491. NK_API struct nk_color
  7492. nk_hsv_bv(const nk_byte *c)
  7493. {
  7494. return nk_hsv(c[0], c[1], c[2]);
  7495. }
  7496. NK_API struct nk_color
  7497. nk_hsv_f(float h, float s, float v)
  7498. {
  7499. return nk_hsva_f(h, s, v, 1.0f);
  7500. }
  7501. NK_API struct nk_color
  7502. nk_hsv_fv(const float *c)
  7503. {
  7504. return nk_hsv_f(c[0], c[1], c[2]);
  7505. }
  7506. NK_API struct nk_color
  7507. nk_hsva(int h, int s, int v, int a)
  7508. {
  7509. float hf = ((float)NK_CLAMP(0, h, 255)) / 255.0f;
  7510. float sf = ((float)NK_CLAMP(0, s, 255)) / 255.0f;
  7511. float vf = ((float)NK_CLAMP(0, v, 255)) / 255.0f;
  7512. float af = ((float)NK_CLAMP(0, a, 255)) / 255.0f;
  7513. return nk_hsva_f(hf, sf, vf, af);
  7514. }
  7515. NK_API struct nk_color
  7516. nk_hsva_iv(const int *c)
  7517. {
  7518. return nk_hsva(c[0], c[1], c[2], c[3]);
  7519. }
  7520. NK_API struct nk_color
  7521. nk_hsva_bv(const nk_byte *c)
  7522. {
  7523. return nk_hsva(c[0], c[1], c[2], c[3]);
  7524. }
  7525. NK_API struct nk_colorf
  7526. nk_hsva_colorf(float h, float s, float v, float a)
  7527. {
  7528. int i;
  7529. float p, q, t, f;
  7530. struct nk_colorf out = {0,0,0,0};
  7531. if (s <= 0.0f) {
  7532. out.r = v; out.g = v; out.b = v; out.a = a;
  7533. return out;
  7534. }
  7535. h = h / (60.0f/360.0f);
  7536. i = (int)h;
  7537. f = h - (float)i;
  7538. p = v * (1.0f - s);
  7539. q = v * (1.0f - (s * f));
  7540. t = v * (1.0f - s * (1.0f - f));
  7541. switch (i) {
  7542. case 0: default: out.r = v; out.g = t; out.b = p; break;
  7543. case 1: out.r = q; out.g = v; out.b = p; break;
  7544. case 2: out.r = p; out.g = v; out.b = t; break;
  7545. case 3: out.r = p; out.g = q; out.b = v; break;
  7546. case 4: out.r = t; out.g = p; out.b = v; break;
  7547. case 5: out.r = v; out.g = p; out.b = q; break;}
  7548. out.a = a;
  7549. return out;
  7550. }
  7551. NK_API struct nk_colorf
  7552. nk_hsva_colorfv(const float *c)
  7553. {
  7554. return nk_hsva_colorf(c[0], c[1], c[2], c[3]);
  7555. }
  7556. NK_API struct nk_color
  7557. nk_hsva_f(float h, float s, float v, float a)
  7558. {
  7559. struct nk_colorf c = nk_hsva_colorf(h, s, v, a);
  7560. return nk_rgba_f(c.r, c.g, c.b, c.a);
  7561. }
  7562. NK_API struct nk_color
  7563. nk_hsva_fv(const float *c)
  7564. {
  7565. return nk_hsva_f(c[0], c[1], c[2], c[3]);
  7566. }
  7567. NK_API nk_uint
  7568. nk_color_u32(struct nk_color in)
  7569. {
  7570. nk_uint out = (nk_uint)in.r;
  7571. out |= ((nk_uint)in.g << 8);
  7572. out |= ((nk_uint)in.b << 16);
  7573. out |= ((nk_uint)in.a << 24);
  7574. return out;
  7575. }
  7576. NK_API void
  7577. nk_color_f(float *r, float *g, float *b, float *a, struct nk_color in)
  7578. {
  7579. NK_STORAGE const float s = 1.0f/255.0f;
  7580. *r = (float)in.r * s;
  7581. *g = (float)in.g * s;
  7582. *b = (float)in.b * s;
  7583. *a = (float)in.a * s;
  7584. }
  7585. NK_API void
  7586. nk_color_fv(float *c, struct nk_color in)
  7587. {
  7588. nk_color_f(&c[0], &c[1], &c[2], &c[3], in);
  7589. }
  7590. NK_API struct nk_colorf
  7591. nk_color_cf(struct nk_color in)
  7592. {
  7593. struct nk_colorf o;
  7594. nk_color_f(&o.r, &o.g, &o.b, &o.a, in);
  7595. return o;
  7596. }
  7597. NK_API void
  7598. nk_color_d(double *r, double *g, double *b, double *a, struct nk_color in)
  7599. {
  7600. NK_STORAGE const double s = 1.0/255.0;
  7601. *r = (double)in.r * s;
  7602. *g = (double)in.g * s;
  7603. *b = (double)in.b * s;
  7604. *a = (double)in.a * s;
  7605. }
  7606. NK_API void
  7607. nk_color_dv(double *c, struct nk_color in)
  7608. {
  7609. nk_color_d(&c[0], &c[1], &c[2], &c[3], in);
  7610. }
  7611. NK_API void
  7612. nk_color_hsv_f(float *out_h, float *out_s, float *out_v, struct nk_color in)
  7613. {
  7614. float a;
  7615. nk_color_hsva_f(out_h, out_s, out_v, &a, in);
  7616. }
  7617. NK_API void
  7618. nk_color_hsv_fv(float *out, struct nk_color in)
  7619. {
  7620. float a;
  7621. nk_color_hsva_f(&out[0], &out[1], &out[2], &a, in);
  7622. }
  7623. NK_API void
  7624. nk_colorf_hsva_f(float *out_h, float *out_s,
  7625. float *out_v, float *out_a, struct nk_colorf in)
  7626. {
  7627. float chroma;
  7628. float K = 0.0f;
  7629. if (in.g < in.b) {
  7630. const float t = in.g; in.g = in.b; in.b = t;
  7631. K = -1.f;
  7632. }
  7633. if (in.r < in.g) {
  7634. const float t = in.r; in.r = in.g; in.g = t;
  7635. K = -2.f/6.0f - K;
  7636. }
  7637. chroma = in.r - ((in.g < in.b) ? in.g: in.b);
  7638. *out_h = NK_ABS(K + (in.g - in.b)/(6.0f * chroma + 1e-20f));
  7639. *out_s = chroma / (in.r + 1e-20f);
  7640. *out_v = in.r;
  7641. *out_a = in.a;
  7642. }
  7643. NK_API void
  7644. nk_colorf_hsva_fv(float *hsva, struct nk_colorf in)
  7645. {
  7646. nk_colorf_hsva_f(&hsva[0], &hsva[1], &hsva[2], &hsva[3], in);
  7647. }
  7648. NK_API void
  7649. nk_color_hsva_f(float *out_h, float *out_s,
  7650. float *out_v, float *out_a, struct nk_color in)
  7651. {
  7652. struct nk_colorf col;
  7653. nk_color_f(&col.r,&col.g,&col.b,&col.a, in);
  7654. nk_colorf_hsva_f(out_h, out_s, out_v, out_a, col);
  7655. }
  7656. NK_API void
  7657. nk_color_hsva_fv(float *out, struct nk_color in)
  7658. {
  7659. nk_color_hsva_f(&out[0], &out[1], &out[2], &out[3], in);
  7660. }
  7661. NK_API void
  7662. nk_color_hsva_i(int *out_h, int *out_s, int *out_v,
  7663. int *out_a, struct nk_color in)
  7664. {
  7665. float h,s,v,a;
  7666. nk_color_hsva_f(&h, &s, &v, &a, in);
  7667. *out_h = (nk_byte)(h * 255.0f);
  7668. *out_s = (nk_byte)(s * 255.0f);
  7669. *out_v = (nk_byte)(v * 255.0f);
  7670. *out_a = (nk_byte)(a * 255.0f);
  7671. }
  7672. NK_API void
  7673. nk_color_hsva_iv(int *out, struct nk_color in)
  7674. {
  7675. nk_color_hsva_i(&out[0], &out[1], &out[2], &out[3], in);
  7676. }
  7677. NK_API void
  7678. nk_color_hsva_bv(nk_byte *out, struct nk_color in)
  7679. {
  7680. int tmp[4];
  7681. nk_color_hsva_i(&tmp[0], &tmp[1], &tmp[2], &tmp[3], in);
  7682. out[0] = (nk_byte)tmp[0];
  7683. out[1] = (nk_byte)tmp[1];
  7684. out[2] = (nk_byte)tmp[2];
  7685. out[3] = (nk_byte)tmp[3];
  7686. }
  7687. NK_API void
  7688. nk_color_hsva_b(nk_byte *h, nk_byte *s, nk_byte *v, nk_byte *a, struct nk_color in)
  7689. {
  7690. int tmp[4];
  7691. nk_color_hsva_i(&tmp[0], &tmp[1], &tmp[2], &tmp[3], in);
  7692. *h = (nk_byte)tmp[0];
  7693. *s = (nk_byte)tmp[1];
  7694. *v = (nk_byte)tmp[2];
  7695. *a = (nk_byte)tmp[3];
  7696. }
  7697. NK_API void
  7698. nk_color_hsv_i(int *out_h, int *out_s, int *out_v, struct nk_color in)
  7699. {
  7700. int a;
  7701. nk_color_hsva_i(out_h, out_s, out_v, &a, in);
  7702. }
  7703. NK_API void
  7704. nk_color_hsv_b(nk_byte *out_h, nk_byte *out_s, nk_byte *out_v, struct nk_color in)
  7705. {
  7706. int tmp[4];
  7707. nk_color_hsva_i(&tmp[0], &tmp[1], &tmp[2], &tmp[3], in);
  7708. *out_h = (nk_byte)tmp[0];
  7709. *out_s = (nk_byte)tmp[1];
  7710. *out_v = (nk_byte)tmp[2];
  7711. }
  7712. NK_API void
  7713. nk_color_hsv_iv(int *out, struct nk_color in)
  7714. {
  7715. nk_color_hsv_i(&out[0], &out[1], &out[2], in);
  7716. }
  7717. NK_API void
  7718. nk_color_hsv_bv(nk_byte *out, struct nk_color in)
  7719. {
  7720. int tmp[4];
  7721. nk_color_hsv_i(&tmp[0], &tmp[1], &tmp[2], in);
  7722. out[0] = (nk_byte)tmp[0];
  7723. out[1] = (nk_byte)tmp[1];
  7724. out[2] = (nk_byte)tmp[2];
  7725. }
  7726. /* ===============================================================
  7727. *
  7728. * UTF-8
  7729. *
  7730. * ===============================================================*/
  7731. NK_GLOBAL const nk_byte nk_utfbyte[NK_UTF_SIZE+1] = {0x80, 0, 0xC0, 0xE0, 0xF0};
  7732. NK_GLOBAL const nk_byte nk_utfmask[NK_UTF_SIZE+1] = {0xC0, 0x80, 0xE0, 0xF0, 0xF8};
  7733. NK_GLOBAL const nk_uint nk_utfmin[NK_UTF_SIZE+1] = {0, 0, 0x80, 0x800, 0x10000};
  7734. NK_GLOBAL const nk_uint nk_utfmax[NK_UTF_SIZE+1] = {0x10FFFF, 0x7F, 0x7FF, 0xFFFF, 0x10FFFF};
  7735. NK_INTERN int
  7736. nk_utf_validate(nk_rune *u, int i)
  7737. {
  7738. NK_ASSERT(u);
  7739. if (!u) return 0;
  7740. if (!NK_BETWEEN(*u, nk_utfmin[i], nk_utfmax[i]) ||
  7741. NK_BETWEEN(*u, 0xD800, 0xDFFF))
  7742. *u = NK_UTF_INVALID;
  7743. for (i = 1; *u > nk_utfmax[i]; ++i);
  7744. return i;
  7745. }
  7746. NK_INTERN nk_rune
  7747. nk_utf_decode_byte(char c, int *i)
  7748. {
  7749. NK_ASSERT(i);
  7750. if (!i) return 0;
  7751. for(*i = 0; *i < (int)NK_LEN(nk_utfmask); ++(*i)) {
  7752. if (((nk_byte)c & nk_utfmask[*i]) == nk_utfbyte[*i])
  7753. return (nk_byte)(c & ~nk_utfmask[*i]);
  7754. }
  7755. return 0;
  7756. }
  7757. NK_API int
  7758. nk_utf_decode(const char *c, nk_rune *u, int clen)
  7759. {
  7760. int i, j, len, type=0;
  7761. nk_rune udecoded;
  7762. NK_ASSERT(c);
  7763. NK_ASSERT(u);
  7764. if (!c || !u) return 0;
  7765. if (!clen) return 0;
  7766. *u = NK_UTF_INVALID;
  7767. udecoded = nk_utf_decode_byte(c[0], &len);
  7768. if (!NK_BETWEEN(len, 1, NK_UTF_SIZE))
  7769. return 1;
  7770. for (i = 1, j = 1; i < clen && j < len; ++i, ++j) {
  7771. udecoded = (udecoded << 6) | nk_utf_decode_byte(c[i], &type);
  7772. if (type != 0)
  7773. return j;
  7774. }
  7775. if (j < len)
  7776. return 0;
  7777. *u = udecoded;
  7778. nk_utf_validate(u, len);
  7779. return len;
  7780. }
  7781. NK_INTERN char
  7782. nk_utf_encode_byte(nk_rune u, int i)
  7783. {
  7784. return (char)((nk_utfbyte[i]) | ((nk_byte)u & ~nk_utfmask[i]));
  7785. }
  7786. NK_API int
  7787. nk_utf_encode(nk_rune u, char *c, int clen)
  7788. {
  7789. int len, i;
  7790. len = nk_utf_validate(&u, 0);
  7791. if (clen < len || !len || len > NK_UTF_SIZE)
  7792. return 0;
  7793. for (i = len - 1; i != 0; --i) {
  7794. c[i] = nk_utf_encode_byte(u, 0);
  7795. u >>= 6;
  7796. }
  7797. c[0] = nk_utf_encode_byte(u, len);
  7798. return len;
  7799. }
  7800. NK_API int
  7801. nk_utf_len(const char *str, int len)
  7802. {
  7803. const char *text;
  7804. int glyphs = 0;
  7805. int text_len;
  7806. int glyph_len;
  7807. int src_len = 0;
  7808. nk_rune unicode;
  7809. NK_ASSERT(str);
  7810. if (!str || !len) return 0;
  7811. text = str;
  7812. text_len = len;
  7813. glyph_len = nk_utf_decode(text, &unicode, text_len);
  7814. while (glyph_len && src_len < len) {
  7815. glyphs++;
  7816. src_len = src_len + glyph_len;
  7817. glyph_len = nk_utf_decode(text + src_len, &unicode, text_len - src_len);
  7818. }
  7819. return glyphs;
  7820. }
  7821. NK_API const char*
  7822. nk_utf_at(const char *buffer, int length, int index,
  7823. nk_rune *unicode, int *len)
  7824. {
  7825. int i = 0;
  7826. int src_len = 0;
  7827. int glyph_len = 0;
  7828. const char *text;
  7829. int text_len;
  7830. NK_ASSERT(buffer);
  7831. NK_ASSERT(unicode);
  7832. NK_ASSERT(len);
  7833. if (!buffer || !unicode || !len) return 0;
  7834. if (index < 0) {
  7835. *unicode = NK_UTF_INVALID;
  7836. *len = 0;
  7837. return 0;
  7838. }
  7839. text = buffer;
  7840. text_len = length;
  7841. glyph_len = nk_utf_decode(text, unicode, text_len);
  7842. while (glyph_len) {
  7843. if (i == index) {
  7844. *len = glyph_len;
  7845. break;
  7846. }
  7847. i++;
  7848. src_len = src_len + glyph_len;
  7849. glyph_len = nk_utf_decode(text + src_len, unicode, text_len - src_len);
  7850. }
  7851. if (i != index) return 0;
  7852. return buffer + src_len;
  7853. }
  7854. /* ==============================================================
  7855. *
  7856. * BUFFER
  7857. *
  7858. * ===============================================================*/
  7859. #ifdef NK_INCLUDE_DEFAULT_ALLOCATOR
  7860. NK_LIB void*
  7861. nk_malloc(nk_handle unused, void *old,nk_size size)
  7862. {
  7863. NK_UNUSED(unused);
  7864. NK_UNUSED(old);
  7865. return malloc(size);
  7866. }
  7867. NK_LIB void
  7868. nk_mfree(nk_handle unused, void *ptr)
  7869. {
  7870. NK_UNUSED(unused);
  7871. free(ptr);
  7872. }
  7873. NK_API void
  7874. nk_buffer_init_default(struct nk_buffer *buffer)
  7875. {
  7876. struct nk_allocator alloc;
  7877. alloc.userdata.ptr = 0;
  7878. alloc.alloc = nk_malloc;
  7879. alloc.free = nk_mfree;
  7880. nk_buffer_init(buffer, &alloc, NK_BUFFER_DEFAULT_INITIAL_SIZE);
  7881. }
  7882. #endif
  7883. NK_API void
  7884. nk_buffer_init(struct nk_buffer *b, const struct nk_allocator *a,
  7885. nk_size initial_size)
  7886. {
  7887. NK_ASSERT(b);
  7888. NK_ASSERT(a);
  7889. NK_ASSERT(initial_size);
  7890. if (!b || !a || !initial_size) return;
  7891. nk_zero(b, sizeof(*b));
  7892. b->type = NK_BUFFER_DYNAMIC;
  7893. b->memory.ptr = a->alloc(a->userdata,0, initial_size);
  7894. b->memory.size = initial_size;
  7895. b->size = initial_size;
  7896. b->grow_factor = 2.0f;
  7897. b->pool = *a;
  7898. }
  7899. NK_API void
  7900. nk_buffer_init_fixed(struct nk_buffer *b, void *m, nk_size size)
  7901. {
  7902. NK_ASSERT(b);
  7903. NK_ASSERT(m);
  7904. NK_ASSERT(size);
  7905. if (!b || !m || !size) return;
  7906. nk_zero(b, sizeof(*b));
  7907. b->type = NK_BUFFER_FIXED;
  7908. b->memory.ptr = m;
  7909. b->memory.size = size;
  7910. b->size = size;
  7911. }
  7912. NK_LIB void*
  7913. nk_buffer_align(void *unaligned,
  7914. nk_size align, nk_size *alignment,
  7915. enum nk_buffer_allocation_type type)
  7916. {
  7917. void *memory = 0;
  7918. switch (type) {
  7919. default:
  7920. case NK_BUFFER_MAX:
  7921. case NK_BUFFER_FRONT:
  7922. if (align) {
  7923. memory = NK_ALIGN_PTR(unaligned, align);
  7924. *alignment = (nk_size)((nk_byte*)memory - (nk_byte*)unaligned);
  7925. } else {
  7926. memory = unaligned;
  7927. *alignment = 0;
  7928. }
  7929. break;
  7930. case NK_BUFFER_BACK:
  7931. if (align) {
  7932. memory = NK_ALIGN_PTR_BACK(unaligned, align);
  7933. *alignment = (nk_size)((nk_byte*)unaligned - (nk_byte*)memory);
  7934. } else {
  7935. memory = unaligned;
  7936. *alignment = 0;
  7937. }
  7938. break;
  7939. }
  7940. return memory;
  7941. }
  7942. NK_LIB void*
  7943. nk_buffer_realloc(struct nk_buffer *b, nk_size capacity, nk_size *size)
  7944. {
  7945. void *temp;
  7946. nk_size buffer_size;
  7947. NK_ASSERT(b);
  7948. NK_ASSERT(size);
  7949. if (!b || !size || !b->pool.alloc || !b->pool.free)
  7950. return 0;
  7951. buffer_size = b->memory.size;
  7952. temp = b->pool.alloc(b->pool.userdata, b->memory.ptr, capacity);
  7953. NK_ASSERT(temp);
  7954. if (!temp) return 0;
  7955. *size = capacity;
  7956. if (temp != b->memory.ptr) {
  7957. NK_MEMCPY(temp, b->memory.ptr, buffer_size);
  7958. b->pool.free(b->pool.userdata, b->memory.ptr);
  7959. }
  7960. if (b->size == buffer_size) {
  7961. /* no back buffer so just set correct size */
  7962. b->size = capacity;
  7963. return temp;
  7964. } else {
  7965. /* copy back buffer to the end of the new buffer */
  7966. void *dst, *src;
  7967. nk_size back_size;
  7968. back_size = buffer_size - b->size;
  7969. dst = nk_ptr_add(void, temp, capacity - back_size);
  7970. src = nk_ptr_add(void, temp, b->size);
  7971. NK_MEMCPY(dst, src, back_size);
  7972. b->size = capacity - back_size;
  7973. }
  7974. return temp;
  7975. }
  7976. NK_LIB void*
  7977. nk_buffer_alloc(struct nk_buffer *b, enum nk_buffer_allocation_type type,
  7978. nk_size size, nk_size align)
  7979. {
  7980. int full;
  7981. nk_size alignment;
  7982. void *unaligned;
  7983. void *memory;
  7984. NK_ASSERT(b);
  7985. NK_ASSERT(size);
  7986. if (!b || !size) return 0;
  7987. b->needed += size;
  7988. /* calculate total size with needed alignment + size */
  7989. if (type == NK_BUFFER_FRONT)
  7990. unaligned = nk_ptr_add(void, b->memory.ptr, b->allocated);
  7991. else unaligned = nk_ptr_add(void, b->memory.ptr, b->size - size);
  7992. memory = nk_buffer_align(unaligned, align, &alignment, type);
  7993. /* check if buffer has enough memory*/
  7994. if (type == NK_BUFFER_FRONT)
  7995. full = ((b->allocated + size + alignment) > b->size);
  7996. else full = ((b->size - NK_MIN(b->size,(size + alignment))) <= b->allocated);
  7997. if (full) {
  7998. nk_size capacity;
  7999. if (b->type != NK_BUFFER_DYNAMIC)
  8000. return 0;
  8001. NK_ASSERT(b->pool.alloc && b->pool.free);
  8002. if (b->type != NK_BUFFER_DYNAMIC || !b->pool.alloc || !b->pool.free)
  8003. return 0;
  8004. /* buffer is full so allocate bigger buffer if dynamic */
  8005. capacity = (nk_size)((float)b->memory.size * b->grow_factor);
  8006. capacity = NK_MAX(capacity, nk_round_up_pow2((nk_uint)(b->allocated + size)));
  8007. b->memory.ptr = nk_buffer_realloc(b, capacity, &b->memory.size);
  8008. if (!b->memory.ptr) return 0;
  8009. /* align newly allocated pointer */
  8010. if (type == NK_BUFFER_FRONT)
  8011. unaligned = nk_ptr_add(void, b->memory.ptr, b->allocated);
  8012. else unaligned = nk_ptr_add(void, b->memory.ptr, b->size - size);
  8013. memory = nk_buffer_align(unaligned, align, &alignment, type);
  8014. }
  8015. if (type == NK_BUFFER_FRONT)
  8016. b->allocated += size + alignment;
  8017. else b->size -= (size + alignment);
  8018. b->needed += alignment;
  8019. b->calls++;
  8020. return memory;
  8021. }
  8022. NK_API void
  8023. nk_buffer_push(struct nk_buffer *b, enum nk_buffer_allocation_type type,
  8024. const void *memory, nk_size size, nk_size align)
  8025. {
  8026. void *mem = nk_buffer_alloc(b, type, size, align);
  8027. if (!mem) return;
  8028. NK_MEMCPY(mem, memory, size);
  8029. }
  8030. NK_API void
  8031. nk_buffer_mark(struct nk_buffer *buffer, enum nk_buffer_allocation_type type)
  8032. {
  8033. NK_ASSERT(buffer);
  8034. if (!buffer) return;
  8035. buffer->marker[type].active = nk_true;
  8036. if (type == NK_BUFFER_BACK)
  8037. buffer->marker[type].offset = buffer->size;
  8038. else buffer->marker[type].offset = buffer->allocated;
  8039. }
  8040. NK_API void
  8041. nk_buffer_reset(struct nk_buffer *buffer, enum nk_buffer_allocation_type type)
  8042. {
  8043. NK_ASSERT(buffer);
  8044. if (!buffer) return;
  8045. if (type == NK_BUFFER_BACK) {
  8046. /* reset back buffer either back to marker or empty */
  8047. buffer->needed -= (buffer->memory.size - buffer->marker[type].offset);
  8048. if (buffer->marker[type].active)
  8049. buffer->size = buffer->marker[type].offset;
  8050. else buffer->size = buffer->memory.size;
  8051. buffer->marker[type].active = nk_false;
  8052. } else {
  8053. /* reset front buffer either back to back marker or empty */
  8054. buffer->needed -= (buffer->allocated - buffer->marker[type].offset);
  8055. if (buffer->marker[type].active)
  8056. buffer->allocated = buffer->marker[type].offset;
  8057. else buffer->allocated = 0;
  8058. buffer->marker[type].active = nk_false;
  8059. }
  8060. }
  8061. NK_API void
  8062. nk_buffer_clear(struct nk_buffer *b)
  8063. {
  8064. NK_ASSERT(b);
  8065. if (!b) return;
  8066. b->allocated = 0;
  8067. b->size = b->memory.size;
  8068. b->calls = 0;
  8069. b->needed = 0;
  8070. }
  8071. NK_API void
  8072. nk_buffer_free(struct nk_buffer *b)
  8073. {
  8074. NK_ASSERT(b);
  8075. if (!b || !b->memory.ptr) return;
  8076. if (b->type == NK_BUFFER_FIXED) return;
  8077. if (!b->pool.free) return;
  8078. NK_ASSERT(b->pool.free);
  8079. b->pool.free(b->pool.userdata, b->memory.ptr);
  8080. }
  8081. NK_API void
  8082. nk_buffer_info(struct nk_memory_status *s, const struct nk_buffer *b)
  8083. {
  8084. NK_ASSERT(b);
  8085. NK_ASSERT(s);
  8086. if (!s || !b) return;
  8087. s->allocated = b->allocated;
  8088. s->size = b->memory.size;
  8089. s->needed = b->needed;
  8090. s->memory = b->memory.ptr;
  8091. s->calls = b->calls;
  8092. }
  8093. NK_API void*
  8094. nk_buffer_memory(struct nk_buffer *buffer)
  8095. {
  8096. NK_ASSERT(buffer);
  8097. if (!buffer) return 0;
  8098. return buffer->memory.ptr;
  8099. }
  8100. NK_API const void*
  8101. nk_buffer_memory_const(const struct nk_buffer *buffer)
  8102. {
  8103. NK_ASSERT(buffer);
  8104. if (!buffer) return 0;
  8105. return buffer->memory.ptr;
  8106. }
  8107. NK_API nk_size
  8108. nk_buffer_total(const struct nk_buffer *buffer)
  8109. {
  8110. NK_ASSERT(buffer);
  8111. if (!buffer) return 0;
  8112. return buffer->memory.size;
  8113. }
  8114. /* ===============================================================
  8115. *
  8116. * STRING
  8117. *
  8118. * ===============================================================*/
  8119. #ifdef NK_INCLUDE_DEFAULT_ALLOCATOR
  8120. NK_API void
  8121. nk_str_init_default(struct nk_str *str)
  8122. {
  8123. struct nk_allocator alloc;
  8124. alloc.userdata.ptr = 0;
  8125. alloc.alloc = nk_malloc;
  8126. alloc.free = nk_mfree;
  8127. nk_buffer_init(&str->buffer, &alloc, 32);
  8128. str->len = 0;
  8129. }
  8130. #endif
  8131. NK_API void
  8132. nk_str_init(struct nk_str *str, const struct nk_allocator *alloc, nk_size size)
  8133. {
  8134. nk_buffer_init(&str->buffer, alloc, size);
  8135. str->len = 0;
  8136. }
  8137. NK_API void
  8138. nk_str_init_fixed(struct nk_str *str, void *memory, nk_size size)
  8139. {
  8140. nk_buffer_init_fixed(&str->buffer, memory, size);
  8141. str->len = 0;
  8142. }
  8143. NK_API int
  8144. nk_str_append_text_char(struct nk_str *s, const char *str, int len)
  8145. {
  8146. char *mem;
  8147. NK_ASSERT(s);
  8148. NK_ASSERT(str);
  8149. if (!s || !str || !len) return 0;
  8150. mem = (char*)nk_buffer_alloc(&s->buffer, NK_BUFFER_FRONT, (nk_size)len * sizeof(char), 0);
  8151. if (!mem) return 0;
  8152. NK_MEMCPY(mem, str, (nk_size)len * sizeof(char));
  8153. s->len += nk_utf_len(str, len);
  8154. return len;
  8155. }
  8156. NK_API int
  8157. nk_str_append_str_char(struct nk_str *s, const char *str)
  8158. {
  8159. return nk_str_append_text_char(s, str, nk_strlen(str));
  8160. }
  8161. NK_API int
  8162. nk_str_append_text_utf8(struct nk_str *str, const char *text, int len)
  8163. {
  8164. int i = 0;
  8165. int byte_len = 0;
  8166. nk_rune unicode;
  8167. if (!str || !text || !len) return 0;
  8168. for (i = 0; i < len; ++i)
  8169. byte_len += nk_utf_decode(text+byte_len, &unicode, 4);
  8170. nk_str_append_text_char(str, text, byte_len);
  8171. return len;
  8172. }
  8173. NK_API int
  8174. nk_str_append_str_utf8(struct nk_str *str, const char *text)
  8175. {
  8176. int byte_len = 0;
  8177. int num_runes = 0;
  8178. int glyph_len = 0;
  8179. nk_rune unicode;
  8180. if (!str || !text) return 0;
  8181. glyph_len = byte_len = nk_utf_decode(text+byte_len, &unicode, 4);
  8182. while (unicode != '\0' && glyph_len) {
  8183. glyph_len = nk_utf_decode(text+byte_len, &unicode, 4);
  8184. byte_len += glyph_len;
  8185. num_runes++;
  8186. }
  8187. nk_str_append_text_char(str, text, byte_len);
  8188. return num_runes;
  8189. }
  8190. NK_API int
  8191. nk_str_append_text_runes(struct nk_str *str, const nk_rune *text, int len)
  8192. {
  8193. int i = 0;
  8194. int byte_len = 0;
  8195. nk_glyph glyph;
  8196. NK_ASSERT(str);
  8197. if (!str || !text || !len) return 0;
  8198. for (i = 0; i < len; ++i) {
  8199. byte_len = nk_utf_encode(text[i], glyph, NK_UTF_SIZE);
  8200. if (!byte_len) break;
  8201. nk_str_append_text_char(str, glyph, byte_len);
  8202. }
  8203. return len;
  8204. }
  8205. NK_API int
  8206. nk_str_append_str_runes(struct nk_str *str, const nk_rune *runes)
  8207. {
  8208. int i = 0;
  8209. nk_glyph glyph;
  8210. int byte_len;
  8211. NK_ASSERT(str);
  8212. if (!str || !runes) return 0;
  8213. while (runes[i] != '\0') {
  8214. byte_len = nk_utf_encode(runes[i], glyph, NK_UTF_SIZE);
  8215. nk_str_append_text_char(str, glyph, byte_len);
  8216. i++;
  8217. }
  8218. return i;
  8219. }
  8220. NK_API int
  8221. nk_str_insert_at_char(struct nk_str *s, int pos, const char *str, int len)
  8222. {
  8223. int i;
  8224. void *mem;
  8225. char *src;
  8226. char *dst;
  8227. int copylen;
  8228. NK_ASSERT(s);
  8229. NK_ASSERT(str);
  8230. NK_ASSERT(len >= 0);
  8231. if (!s || !str || !len || (nk_size)pos > s->buffer.allocated) return 0;
  8232. if ((s->buffer.allocated + (nk_size)len >= s->buffer.memory.size) &&
  8233. (s->buffer.type == NK_BUFFER_FIXED)) return 0;
  8234. copylen = (int)s->buffer.allocated - pos;
  8235. if (!copylen) {
  8236. nk_str_append_text_char(s, str, len);
  8237. return 1;
  8238. }
  8239. mem = nk_buffer_alloc(&s->buffer, NK_BUFFER_FRONT, (nk_size)len * sizeof(char), 0);
  8240. if (!mem) return 0;
  8241. /* memmove */
  8242. NK_ASSERT(((int)pos + (int)len + ((int)copylen - 1)) >= 0);
  8243. NK_ASSERT(((int)pos + ((int)copylen - 1)) >= 0);
  8244. dst = nk_ptr_add(char, s->buffer.memory.ptr, pos + len + (copylen - 1));
  8245. src = nk_ptr_add(char, s->buffer.memory.ptr, pos + (copylen-1));
  8246. for (i = 0; i < copylen; ++i) *dst-- = *src--;
  8247. mem = nk_ptr_add(void, s->buffer.memory.ptr, pos);
  8248. NK_MEMCPY(mem, str, (nk_size)len * sizeof(char));
  8249. s->len = nk_utf_len((char *)s->buffer.memory.ptr, (int)s->buffer.allocated);
  8250. return 1;
  8251. }
  8252. NK_API int
  8253. nk_str_insert_at_rune(struct nk_str *str, int pos, const char *cstr, int len)
  8254. {
  8255. int glyph_len;
  8256. nk_rune unicode;
  8257. const char *begin;
  8258. const char *buffer;
  8259. NK_ASSERT(str);
  8260. NK_ASSERT(cstr);
  8261. NK_ASSERT(len);
  8262. if (!str || !cstr || !len) return 0;
  8263. begin = nk_str_at_rune(str, pos, &unicode, &glyph_len);
  8264. if (!str->len)
  8265. return nk_str_append_text_char(str, cstr, len);
  8266. buffer = nk_str_get_const(str);
  8267. if (!begin) return 0;
  8268. return nk_str_insert_at_char(str, (int)(begin - buffer), cstr, len);
  8269. }
  8270. NK_API int
  8271. nk_str_insert_text_char(struct nk_str *str, int pos, const char *text, int len)
  8272. {
  8273. return nk_str_insert_text_utf8(str, pos, text, len);
  8274. }
  8275. NK_API int
  8276. nk_str_insert_str_char(struct nk_str *str, int pos, const char *text)
  8277. {
  8278. return nk_str_insert_text_utf8(str, pos, text, nk_strlen(text));
  8279. }
  8280. NK_API int
  8281. nk_str_insert_text_utf8(struct nk_str *str, int pos, const char *text, int len)
  8282. {
  8283. int i = 0;
  8284. int byte_len = 0;
  8285. nk_rune unicode;
  8286. NK_ASSERT(str);
  8287. NK_ASSERT(text);
  8288. if (!str || !text || !len) return 0;
  8289. for (i = 0; i < len; ++i)
  8290. byte_len += nk_utf_decode(text+byte_len, &unicode, 4);
  8291. nk_str_insert_at_rune(str, pos, text, byte_len);
  8292. return len;
  8293. }
  8294. NK_API int
  8295. nk_str_insert_str_utf8(struct nk_str *str, int pos, const char *text)
  8296. {
  8297. int byte_len = 0;
  8298. int num_runes = 0;
  8299. int glyph_len = 0;
  8300. nk_rune unicode;
  8301. if (!str || !text) return 0;
  8302. glyph_len = byte_len = nk_utf_decode(text+byte_len, &unicode, 4);
  8303. while (unicode != '\0' && glyph_len) {
  8304. glyph_len = nk_utf_decode(text+byte_len, &unicode, 4);
  8305. byte_len += glyph_len;
  8306. num_runes++;
  8307. }
  8308. nk_str_insert_at_rune(str, pos, text, byte_len);
  8309. return num_runes;
  8310. }
  8311. NK_API int
  8312. nk_str_insert_text_runes(struct nk_str *str, int pos, const nk_rune *runes, int len)
  8313. {
  8314. int i = 0;
  8315. int byte_len = 0;
  8316. nk_glyph glyph;
  8317. NK_ASSERT(str);
  8318. if (!str || !runes || !len) return 0;
  8319. for (i = 0; i < len; ++i) {
  8320. byte_len = nk_utf_encode(runes[i], glyph, NK_UTF_SIZE);
  8321. if (!byte_len) break;
  8322. nk_str_insert_at_rune(str, pos+i, glyph, byte_len);
  8323. }
  8324. return len;
  8325. }
  8326. NK_API int
  8327. nk_str_insert_str_runes(struct nk_str *str, int pos, const nk_rune *runes)
  8328. {
  8329. int i = 0;
  8330. nk_glyph glyph;
  8331. int byte_len;
  8332. NK_ASSERT(str);
  8333. if (!str || !runes) return 0;
  8334. while (runes[i] != '\0') {
  8335. byte_len = nk_utf_encode(runes[i], glyph, NK_UTF_SIZE);
  8336. nk_str_insert_at_rune(str, pos+i, glyph, byte_len);
  8337. i++;
  8338. }
  8339. return i;
  8340. }
  8341. NK_API void
  8342. nk_str_remove_chars(struct nk_str *s, int len)
  8343. {
  8344. NK_ASSERT(s);
  8345. NK_ASSERT(len >= 0);
  8346. if (!s || len < 0 || (nk_size)len > s->buffer.allocated) return;
  8347. NK_ASSERT(((int)s->buffer.allocated - (int)len) >= 0);
  8348. s->buffer.allocated -= (nk_size)len;
  8349. s->len = nk_utf_len((char *)s->buffer.memory.ptr, (int)s->buffer.allocated);
  8350. }
  8351. NK_API void
  8352. nk_str_remove_runes(struct nk_str *str, int len)
  8353. {
  8354. int index;
  8355. const char *begin;
  8356. const char *end;
  8357. nk_rune unicode;
  8358. NK_ASSERT(str);
  8359. NK_ASSERT(len >= 0);
  8360. if (!str || len < 0) return;
  8361. if (len >= str->len) {
  8362. str->len = 0;
  8363. return;
  8364. }
  8365. index = str->len - len;
  8366. begin = nk_str_at_rune(str, index, &unicode, &len);
  8367. end = (const char*)str->buffer.memory.ptr + str->buffer.allocated;
  8368. nk_str_remove_chars(str, (int)(end-begin)+1);
  8369. }
  8370. NK_API void
  8371. nk_str_delete_chars(struct nk_str *s, int pos, int len)
  8372. {
  8373. NK_ASSERT(s);
  8374. if (!s || !len || (nk_size)pos > s->buffer.allocated ||
  8375. (nk_size)(pos + len) > s->buffer.allocated) return;
  8376. if ((nk_size)(pos + len) < s->buffer.allocated) {
  8377. /* memmove */
  8378. char *dst = nk_ptr_add(char, s->buffer.memory.ptr, pos);
  8379. char *src = nk_ptr_add(char, s->buffer.memory.ptr, pos + len);
  8380. NK_MEMCPY(dst, src, s->buffer.allocated - (nk_size)(pos + len));
  8381. NK_ASSERT(((int)s->buffer.allocated - (int)len) >= 0);
  8382. s->buffer.allocated -= (nk_size)len;
  8383. } else nk_str_remove_chars(s, len);
  8384. s->len = nk_utf_len((char *)s->buffer.memory.ptr, (int)s->buffer.allocated);
  8385. }
  8386. NK_API void
  8387. nk_str_delete_runes(struct nk_str *s, int pos, int len)
  8388. {
  8389. char *temp;
  8390. nk_rune unicode;
  8391. char *begin;
  8392. char *end;
  8393. int unused;
  8394. NK_ASSERT(s);
  8395. NK_ASSERT(s->len >= pos + len);
  8396. if (s->len < pos + len)
  8397. len = NK_CLAMP(0, (s->len - pos), s->len);
  8398. if (!len) return;
  8399. temp = (char *)s->buffer.memory.ptr;
  8400. begin = nk_str_at_rune(s, pos, &unicode, &unused);
  8401. if (!begin) return;
  8402. s->buffer.memory.ptr = begin;
  8403. end = nk_str_at_rune(s, len, &unicode, &unused);
  8404. s->buffer.memory.ptr = temp;
  8405. if (!end) return;
  8406. nk_str_delete_chars(s, (int)(begin - temp), (int)(end - begin));
  8407. }
  8408. NK_API char*
  8409. nk_str_at_char(struct nk_str *s, int pos)
  8410. {
  8411. NK_ASSERT(s);
  8412. if (!s || pos > (int)s->buffer.allocated) return 0;
  8413. return nk_ptr_add(char, s->buffer.memory.ptr, pos);
  8414. }
  8415. NK_API char*
  8416. nk_str_at_rune(struct nk_str *str, int pos, nk_rune *unicode, int *len)
  8417. {
  8418. int i = 0;
  8419. int src_len = 0;
  8420. int glyph_len = 0;
  8421. char *text;
  8422. int text_len;
  8423. NK_ASSERT(str);
  8424. NK_ASSERT(unicode);
  8425. NK_ASSERT(len);
  8426. if (!str || !unicode || !len) return 0;
  8427. if (pos < 0) {
  8428. *unicode = 0;
  8429. *len = 0;
  8430. return 0;
  8431. }
  8432. text = (char*)str->buffer.memory.ptr;
  8433. text_len = (int)str->buffer.allocated;
  8434. glyph_len = nk_utf_decode(text, unicode, text_len);
  8435. while (glyph_len) {
  8436. if (i == pos) {
  8437. *len = glyph_len;
  8438. break;
  8439. }
  8440. i++;
  8441. src_len = src_len + glyph_len;
  8442. glyph_len = nk_utf_decode(text + src_len, unicode, text_len - src_len);
  8443. }
  8444. if (i != pos) return 0;
  8445. return text + src_len;
  8446. }
  8447. NK_API const char*
  8448. nk_str_at_char_const(const struct nk_str *s, int pos)
  8449. {
  8450. NK_ASSERT(s);
  8451. if (!s || pos > (int)s->buffer.allocated) return 0;
  8452. return nk_ptr_add(char, s->buffer.memory.ptr, pos);
  8453. }
  8454. NK_API const char*
  8455. nk_str_at_const(const struct nk_str *str, int pos, nk_rune *unicode, int *len)
  8456. {
  8457. int i = 0;
  8458. int src_len = 0;
  8459. int glyph_len = 0;
  8460. char *text;
  8461. int text_len;
  8462. NK_ASSERT(str);
  8463. NK_ASSERT(unicode);
  8464. NK_ASSERT(len);
  8465. if (!str || !unicode || !len) return 0;
  8466. if (pos < 0) {
  8467. *unicode = 0;
  8468. *len = 0;
  8469. return 0;
  8470. }
  8471. text = (char*)str->buffer.memory.ptr;
  8472. text_len = (int)str->buffer.allocated;
  8473. glyph_len = nk_utf_decode(text, unicode, text_len);
  8474. while (glyph_len) {
  8475. if (i == pos) {
  8476. *len = glyph_len;
  8477. break;
  8478. }
  8479. i++;
  8480. src_len = src_len + glyph_len;
  8481. glyph_len = nk_utf_decode(text + src_len, unicode, text_len - src_len);
  8482. }
  8483. if (i != pos) return 0;
  8484. return text + src_len;
  8485. }
  8486. NK_API nk_rune
  8487. nk_str_rune_at(const struct nk_str *str, int pos)
  8488. {
  8489. int len;
  8490. nk_rune unicode = 0;
  8491. nk_str_at_const(str, pos, &unicode, &len);
  8492. return unicode;
  8493. }
  8494. NK_API char*
  8495. nk_str_get(struct nk_str *s)
  8496. {
  8497. NK_ASSERT(s);
  8498. if (!s || !s->len || !s->buffer.allocated) return 0;
  8499. return (char*)s->buffer.memory.ptr;
  8500. }
  8501. NK_API const char*
  8502. nk_str_get_const(const struct nk_str *s)
  8503. {
  8504. NK_ASSERT(s);
  8505. if (!s || !s->len || !s->buffer.allocated) return 0;
  8506. return (const char*)s->buffer.memory.ptr;
  8507. }
  8508. NK_API int
  8509. nk_str_len(const struct nk_str *s)
  8510. {
  8511. NK_ASSERT(s);
  8512. if (!s || !s->len || !s->buffer.allocated) return 0;
  8513. return s->len;
  8514. }
  8515. NK_API int
  8516. nk_str_len_char(const struct nk_str *s)
  8517. {
  8518. NK_ASSERT(s);
  8519. if (!s || !s->len || !s->buffer.allocated) return 0;
  8520. return (int)s->buffer.allocated;
  8521. }
  8522. NK_API void
  8523. nk_str_clear(struct nk_str *str)
  8524. {
  8525. NK_ASSERT(str);
  8526. nk_buffer_clear(&str->buffer);
  8527. str->len = 0;
  8528. }
  8529. NK_API void
  8530. nk_str_free(struct nk_str *str)
  8531. {
  8532. NK_ASSERT(str);
  8533. nk_buffer_free(&str->buffer);
  8534. str->len = 0;
  8535. }
  8536. /* ==============================================================
  8537. *
  8538. * DRAW
  8539. *
  8540. * ===============================================================*/
  8541. NK_LIB void
  8542. nk_command_buffer_init(struct nk_command_buffer *cb,
  8543. struct nk_buffer *b, enum nk_command_clipping clip)
  8544. {
  8545. NK_ASSERT(cb);
  8546. NK_ASSERT(b);
  8547. if (!cb || !b) return;
  8548. cb->base = b;
  8549. cb->use_clipping = (int)clip;
  8550. cb->begin = b->allocated;
  8551. cb->end = b->allocated;
  8552. cb->last = b->allocated;
  8553. }
  8554. NK_LIB void
  8555. nk_command_buffer_reset(struct nk_command_buffer *b)
  8556. {
  8557. NK_ASSERT(b);
  8558. if (!b) return;
  8559. b->begin = 0;
  8560. b->end = 0;
  8561. b->last = 0;
  8562. b->clip = nk_null_rect;
  8563. #ifdef NK_INCLUDE_COMMAND_USERDATA
  8564. b->userdata.ptr = 0;
  8565. #endif
  8566. }
  8567. NK_LIB void*
  8568. nk_command_buffer_push(struct nk_command_buffer* b,
  8569. enum nk_command_type t, nk_size size)
  8570. {
  8571. NK_STORAGE const nk_size align = NK_ALIGNOF(struct nk_command);
  8572. struct nk_command *cmd;
  8573. nk_size alignment;
  8574. void *unaligned;
  8575. void *memory;
  8576. NK_ASSERT(b);
  8577. NK_ASSERT(b->base);
  8578. if (!b) return 0;
  8579. cmd = (struct nk_command*)nk_buffer_alloc(b->base,NK_BUFFER_FRONT,size,align);
  8580. if (!cmd) return 0;
  8581. /* make sure the offset to the next command is aligned */
  8582. b->last = (nk_size)((nk_byte*)cmd - (nk_byte*)b->base->memory.ptr);
  8583. unaligned = (nk_byte*)cmd + size;
  8584. memory = NK_ALIGN_PTR(unaligned, align);
  8585. alignment = (nk_size)((nk_byte*)memory - (nk_byte*)unaligned);
  8586. #ifdef NK_ZERO_COMMAND_MEMORY
  8587. NK_MEMSET(cmd, 0, size + alignment);
  8588. #endif
  8589. cmd->type = t;
  8590. cmd->next = b->base->allocated + alignment;
  8591. #ifdef NK_INCLUDE_COMMAND_USERDATA
  8592. cmd->userdata = b->userdata;
  8593. #endif
  8594. b->end = cmd->next;
  8595. return cmd;
  8596. }
  8597. NK_API void
  8598. nk_push_scissor(struct nk_command_buffer *b, struct nk_rect r)
  8599. {
  8600. struct nk_command_scissor *cmd;
  8601. NK_ASSERT(b);
  8602. if (!b) return;
  8603. b->clip.x = r.x;
  8604. b->clip.y = r.y;
  8605. b->clip.w = r.w;
  8606. b->clip.h = r.h;
  8607. cmd = (struct nk_command_scissor*)
  8608. nk_command_buffer_push(b, NK_COMMAND_SCISSOR, sizeof(*cmd));
  8609. if (!cmd) return;
  8610. cmd->x = (short)r.x;
  8611. cmd->y = (short)r.y;
  8612. cmd->w = (unsigned short)NK_MAX(0, r.w);
  8613. cmd->h = (unsigned short)NK_MAX(0, r.h);
  8614. }
  8615. NK_API void
  8616. nk_stroke_line(struct nk_command_buffer *b, float x0, float y0,
  8617. float x1, float y1, float line_thickness, struct nk_color c)
  8618. {
  8619. struct nk_command_line *cmd;
  8620. NK_ASSERT(b);
  8621. if (!b || line_thickness <= 0) return;
  8622. cmd = (struct nk_command_line*)
  8623. nk_command_buffer_push(b, NK_COMMAND_LINE, sizeof(*cmd));
  8624. if (!cmd) return;
  8625. cmd->line_thickness = (unsigned short)line_thickness;
  8626. cmd->begin.x = (short)x0;
  8627. cmd->begin.y = (short)y0;
  8628. cmd->end.x = (short)x1;
  8629. cmd->end.y = (short)y1;
  8630. cmd->color = c;
  8631. }
  8632. NK_API void
  8633. nk_stroke_curve(struct nk_command_buffer *b, float ax, float ay,
  8634. float ctrl0x, float ctrl0y, float ctrl1x, float ctrl1y,
  8635. float bx, float by, float line_thickness, struct nk_color col)
  8636. {
  8637. struct nk_command_curve *cmd;
  8638. NK_ASSERT(b);
  8639. if (!b || col.a == 0 || line_thickness <= 0) return;
  8640. cmd = (struct nk_command_curve*)
  8641. nk_command_buffer_push(b, NK_COMMAND_CURVE, sizeof(*cmd));
  8642. if (!cmd) return;
  8643. cmd->line_thickness = (unsigned short)line_thickness;
  8644. cmd->begin.x = (short)ax;
  8645. cmd->begin.y = (short)ay;
  8646. cmd->ctrl[0].x = (short)ctrl0x;
  8647. cmd->ctrl[0].y = (short)ctrl0y;
  8648. cmd->ctrl[1].x = (short)ctrl1x;
  8649. cmd->ctrl[1].y = (short)ctrl1y;
  8650. cmd->end.x = (short)bx;
  8651. cmd->end.y = (short)by;
  8652. cmd->color = col;
  8653. }
  8654. NK_API void
  8655. nk_stroke_rect(struct nk_command_buffer *b, struct nk_rect rect,
  8656. float rounding, float line_thickness, struct nk_color c)
  8657. {
  8658. struct nk_command_rect *cmd;
  8659. NK_ASSERT(b);
  8660. if (!b || c.a == 0 || rect.w == 0 || rect.h == 0 || line_thickness <= 0) return;
  8661. if (b->use_clipping) {
  8662. const struct nk_rect *clip = &b->clip;
  8663. if (!NK_INTERSECT(rect.x, rect.y, rect.w, rect.h,
  8664. clip->x, clip->y, clip->w, clip->h)) return;
  8665. }
  8666. cmd = (struct nk_command_rect*)
  8667. nk_command_buffer_push(b, NK_COMMAND_RECT, sizeof(*cmd));
  8668. if (!cmd) return;
  8669. cmd->rounding = (unsigned short)rounding;
  8670. cmd->line_thickness = (unsigned short)line_thickness;
  8671. cmd->x = (short)rect.x;
  8672. cmd->y = (short)rect.y;
  8673. cmd->w = (unsigned short)NK_MAX(0, rect.w);
  8674. cmd->h = (unsigned short)NK_MAX(0, rect.h);
  8675. cmd->color = c;
  8676. }
  8677. NK_API void
  8678. nk_fill_rect(struct nk_command_buffer *b, struct nk_rect rect,
  8679. float rounding, struct nk_color c)
  8680. {
  8681. struct nk_command_rect_filled *cmd;
  8682. NK_ASSERT(b);
  8683. if (!b || c.a == 0 || rect.w == 0 || rect.h == 0) return;
  8684. if (b->use_clipping) {
  8685. const struct nk_rect *clip = &b->clip;
  8686. if (!NK_INTERSECT(rect.x, rect.y, rect.w, rect.h,
  8687. clip->x, clip->y, clip->w, clip->h)) return;
  8688. }
  8689. cmd = (struct nk_command_rect_filled*)
  8690. nk_command_buffer_push(b, NK_COMMAND_RECT_FILLED, sizeof(*cmd));
  8691. if (!cmd) return;
  8692. cmd->rounding = (unsigned short)rounding;
  8693. cmd->x = (short)rect.x;
  8694. cmd->y = (short)rect.y;
  8695. cmd->w = (unsigned short)NK_MAX(0, rect.w);
  8696. cmd->h = (unsigned short)NK_MAX(0, rect.h);
  8697. cmd->color = c;
  8698. }
  8699. NK_API void
  8700. nk_fill_rect_multi_color(struct nk_command_buffer *b, struct nk_rect rect,
  8701. struct nk_color left, struct nk_color top, struct nk_color right,
  8702. struct nk_color bottom)
  8703. {
  8704. struct nk_command_rect_multi_color *cmd;
  8705. NK_ASSERT(b);
  8706. if (!b || rect.w == 0 || rect.h == 0) return;
  8707. if (b->use_clipping) {
  8708. const struct nk_rect *clip = &b->clip;
  8709. if (!NK_INTERSECT(rect.x, rect.y, rect.w, rect.h,
  8710. clip->x, clip->y, clip->w, clip->h)) return;
  8711. }
  8712. cmd = (struct nk_command_rect_multi_color*)
  8713. nk_command_buffer_push(b, NK_COMMAND_RECT_MULTI_COLOR, sizeof(*cmd));
  8714. if (!cmd) return;
  8715. cmd->x = (short)rect.x;
  8716. cmd->y = (short)rect.y;
  8717. cmd->w = (unsigned short)NK_MAX(0, rect.w);
  8718. cmd->h = (unsigned short)NK_MAX(0, rect.h);
  8719. cmd->left = left;
  8720. cmd->top = top;
  8721. cmd->right = right;
  8722. cmd->bottom = bottom;
  8723. }
  8724. NK_API void
  8725. nk_stroke_circle(struct nk_command_buffer *b, struct nk_rect r,
  8726. float line_thickness, struct nk_color c)
  8727. {
  8728. struct nk_command_circle *cmd;
  8729. if (!b || r.w == 0 || r.h == 0 || line_thickness <= 0) return;
  8730. if (b->use_clipping) {
  8731. const struct nk_rect *clip = &b->clip;
  8732. if (!NK_INTERSECT(r.x, r.y, r.w, r.h, clip->x, clip->y, clip->w, clip->h))
  8733. return;
  8734. }
  8735. cmd = (struct nk_command_circle*)
  8736. nk_command_buffer_push(b, NK_COMMAND_CIRCLE, sizeof(*cmd));
  8737. if (!cmd) return;
  8738. cmd->line_thickness = (unsigned short)line_thickness;
  8739. cmd->x = (short)r.x;
  8740. cmd->y = (short)r.y;
  8741. cmd->w = (unsigned short)NK_MAX(r.w, 0);
  8742. cmd->h = (unsigned short)NK_MAX(r.h, 0);
  8743. cmd->color = c;
  8744. }
  8745. NK_API void
  8746. nk_fill_circle(struct nk_command_buffer *b, struct nk_rect r, struct nk_color c)
  8747. {
  8748. struct nk_command_circle_filled *cmd;
  8749. NK_ASSERT(b);
  8750. if (!b || c.a == 0 || r.w == 0 || r.h == 0) return;
  8751. if (b->use_clipping) {
  8752. const struct nk_rect *clip = &b->clip;
  8753. if (!NK_INTERSECT(r.x, r.y, r.w, r.h, clip->x, clip->y, clip->w, clip->h))
  8754. return;
  8755. }
  8756. cmd = (struct nk_command_circle_filled*)
  8757. nk_command_buffer_push(b, NK_COMMAND_CIRCLE_FILLED, sizeof(*cmd));
  8758. if (!cmd) return;
  8759. cmd->x = (short)r.x;
  8760. cmd->y = (short)r.y;
  8761. cmd->w = (unsigned short)NK_MAX(r.w, 0);
  8762. cmd->h = (unsigned short)NK_MAX(r.h, 0);
  8763. cmd->color = c;
  8764. }
  8765. NK_API void
  8766. nk_stroke_arc(struct nk_command_buffer *b, float cx, float cy, float radius,
  8767. float a_min, float a_max, float line_thickness, struct nk_color c)
  8768. {
  8769. struct nk_command_arc *cmd;
  8770. if (!b || c.a == 0 || line_thickness <= 0) return;
  8771. cmd = (struct nk_command_arc*)
  8772. nk_command_buffer_push(b, NK_COMMAND_ARC, sizeof(*cmd));
  8773. if (!cmd) return;
  8774. cmd->line_thickness = (unsigned short)line_thickness;
  8775. cmd->cx = (short)cx;
  8776. cmd->cy = (short)cy;
  8777. cmd->r = (unsigned short)radius;
  8778. cmd->a[0] = a_min;
  8779. cmd->a[1] = a_max;
  8780. cmd->color = c;
  8781. }
  8782. NK_API void
  8783. nk_fill_arc(struct nk_command_buffer *b, float cx, float cy, float radius,
  8784. float a_min, float a_max, struct nk_color c)
  8785. {
  8786. struct nk_command_arc_filled *cmd;
  8787. NK_ASSERT(b);
  8788. if (!b || c.a == 0) return;
  8789. cmd = (struct nk_command_arc_filled*)
  8790. nk_command_buffer_push(b, NK_COMMAND_ARC_FILLED, sizeof(*cmd));
  8791. if (!cmd) return;
  8792. cmd->cx = (short)cx;
  8793. cmd->cy = (short)cy;
  8794. cmd->r = (unsigned short)radius;
  8795. cmd->a[0] = a_min;
  8796. cmd->a[1] = a_max;
  8797. cmd->color = c;
  8798. }
  8799. NK_API void
  8800. nk_stroke_triangle(struct nk_command_buffer *b, float x0, float y0, float x1,
  8801. float y1, float x2, float y2, float line_thickness, struct nk_color c)
  8802. {
  8803. struct nk_command_triangle *cmd;
  8804. NK_ASSERT(b);
  8805. if (!b || c.a == 0 || line_thickness <= 0) return;
  8806. if (b->use_clipping) {
  8807. const struct nk_rect *clip = &b->clip;
  8808. if (!NK_INBOX(x0, y0, clip->x, clip->y, clip->w, clip->h) &&
  8809. !NK_INBOX(x1, y1, clip->x, clip->y, clip->w, clip->h) &&
  8810. !NK_INBOX(x2, y2, clip->x, clip->y, clip->w, clip->h))
  8811. return;
  8812. }
  8813. cmd = (struct nk_command_triangle*)
  8814. nk_command_buffer_push(b, NK_COMMAND_TRIANGLE, sizeof(*cmd));
  8815. if (!cmd) return;
  8816. cmd->line_thickness = (unsigned short)line_thickness;
  8817. cmd->a.x = (short)x0;
  8818. cmd->a.y = (short)y0;
  8819. cmd->b.x = (short)x1;
  8820. cmd->b.y = (short)y1;
  8821. cmd->c.x = (short)x2;
  8822. cmd->c.y = (short)y2;
  8823. cmd->color = c;
  8824. }
  8825. NK_API void
  8826. nk_fill_triangle(struct nk_command_buffer *b, float x0, float y0, float x1,
  8827. float y1, float x2, float y2, struct nk_color c)
  8828. {
  8829. struct nk_command_triangle_filled *cmd;
  8830. NK_ASSERT(b);
  8831. if (!b || c.a == 0) return;
  8832. if (!b) return;
  8833. if (b->use_clipping) {
  8834. const struct nk_rect *clip = &b->clip;
  8835. if (!NK_INBOX(x0, y0, clip->x, clip->y, clip->w, clip->h) &&
  8836. !NK_INBOX(x1, y1, clip->x, clip->y, clip->w, clip->h) &&
  8837. !NK_INBOX(x2, y2, clip->x, clip->y, clip->w, clip->h))
  8838. return;
  8839. }
  8840. cmd = (struct nk_command_triangle_filled*)
  8841. nk_command_buffer_push(b, NK_COMMAND_TRIANGLE_FILLED, sizeof(*cmd));
  8842. if (!cmd) return;
  8843. cmd->a.x = (short)x0;
  8844. cmd->a.y = (short)y0;
  8845. cmd->b.x = (short)x1;
  8846. cmd->b.y = (short)y1;
  8847. cmd->c.x = (short)x2;
  8848. cmd->c.y = (short)y2;
  8849. cmd->color = c;
  8850. }
  8851. NK_API void
  8852. nk_stroke_polygon(struct nk_command_buffer *b, const float *points, int point_count,
  8853. float line_thickness, struct nk_color col)
  8854. {
  8855. int i;
  8856. nk_size size = 0;
  8857. struct nk_command_polygon *cmd;
  8858. NK_ASSERT(b);
  8859. if (!b || col.a == 0 || line_thickness <= 0) return;
  8860. size = sizeof(*cmd) + sizeof(short) * 2 * (nk_size)point_count;
  8861. cmd = (struct nk_command_polygon*) nk_command_buffer_push(b, NK_COMMAND_POLYGON, size);
  8862. if (!cmd) return;
  8863. cmd->color = col;
  8864. cmd->line_thickness = (unsigned short)line_thickness;
  8865. cmd->point_count = (unsigned short)point_count;
  8866. for (i = 0; i < point_count; ++i) {
  8867. cmd->points[i].x = (short)points[i*2];
  8868. cmd->points[i].y = (short)points[i*2+1];
  8869. }
  8870. }
  8871. NK_API void
  8872. nk_fill_polygon(struct nk_command_buffer *b, const float *points, int point_count,
  8873. struct nk_color col)
  8874. {
  8875. int i;
  8876. nk_size size = 0;
  8877. struct nk_command_polygon_filled *cmd;
  8878. NK_ASSERT(b);
  8879. if (!b || col.a == 0) return;
  8880. size = sizeof(*cmd) + sizeof(short) * 2 * (nk_size)point_count;
  8881. cmd = (struct nk_command_polygon_filled*)
  8882. nk_command_buffer_push(b, NK_COMMAND_POLYGON_FILLED, size);
  8883. if (!cmd) return;
  8884. cmd->color = col;
  8885. cmd->point_count = (unsigned short)point_count;
  8886. for (i = 0; i < point_count; ++i) {
  8887. cmd->points[i].x = (short)points[i*2+0];
  8888. cmd->points[i].y = (short)points[i*2+1];
  8889. }
  8890. }
  8891. NK_API void
  8892. nk_stroke_polyline(struct nk_command_buffer *b, const float *points, int point_count,
  8893. float line_thickness, struct nk_color col)
  8894. {
  8895. int i;
  8896. nk_size size = 0;
  8897. struct nk_command_polyline *cmd;
  8898. NK_ASSERT(b);
  8899. if (!b || col.a == 0 || line_thickness <= 0) return;
  8900. size = sizeof(*cmd) + sizeof(short) * 2 * (nk_size)point_count;
  8901. cmd = (struct nk_command_polyline*) nk_command_buffer_push(b, NK_COMMAND_POLYLINE, size);
  8902. if (!cmd) return;
  8903. cmd->color = col;
  8904. cmd->point_count = (unsigned short)point_count;
  8905. cmd->line_thickness = (unsigned short)line_thickness;
  8906. for (i = 0; i < point_count; ++i) {
  8907. cmd->points[i].x = (short)points[i*2];
  8908. cmd->points[i].y = (short)points[i*2+1];
  8909. }
  8910. }
  8911. NK_API void
  8912. nk_draw_image(struct nk_command_buffer *b, struct nk_rect r,
  8913. const struct nk_image *img, struct nk_color col)
  8914. {
  8915. struct nk_command_image *cmd;
  8916. NK_ASSERT(b);
  8917. if (!b) return;
  8918. if (b->use_clipping) {
  8919. const struct nk_rect *c = &b->clip;
  8920. 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))
  8921. return;
  8922. }
  8923. cmd = (struct nk_command_image*)
  8924. nk_command_buffer_push(b, NK_COMMAND_IMAGE, sizeof(*cmd));
  8925. if (!cmd) return;
  8926. cmd->x = (short)r.x;
  8927. cmd->y = (short)r.y;
  8928. cmd->w = (unsigned short)NK_MAX(0, r.w);
  8929. cmd->h = (unsigned short)NK_MAX(0, r.h);
  8930. cmd->img = *img;
  8931. cmd->col = col;
  8932. }
  8933. NK_API void
  8934. nk_draw_nine_slice(struct nk_command_buffer *b, struct nk_rect r,
  8935. const struct nk_nine_slice *slc, struct nk_color col)
  8936. {
  8937. struct nk_image img;
  8938. const struct nk_image *slcimg = (const struct nk_image*)slc;
  8939. nk_ushort rgnX, rgnY, rgnW, rgnH;
  8940. rgnX = slcimg->region[0];
  8941. rgnY = slcimg->region[1];
  8942. rgnW = slcimg->region[2];
  8943. rgnH = slcimg->region[3];
  8944. /* top-left */
  8945. img.handle = slcimg->handle;
  8946. img.w = slcimg->w;
  8947. img.h = slcimg->h;
  8948. img.region[0] = rgnX;
  8949. img.region[1] = rgnY;
  8950. img.region[2] = slc->l;
  8951. img.region[3] = slc->t;
  8952. nk_draw_image(b,
  8953. nk_rect(r.x, r.y, (float)slc->l, (float)slc->t),
  8954. &img, col);
  8955. #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);
  8956. /* top-center */
  8957. IMG_RGN(rgnX + slc->l, rgnY, rgnW - slc->l - slc->r, slc->t);
  8958. nk_draw_image(b,
  8959. nk_rect(r.x + (float)slc->l, r.y, (float)(r.w - slc->l - slc->r), (float)slc->t),
  8960. &img, col);
  8961. /* top-right */
  8962. IMG_RGN(rgnX + rgnW - slc->r, rgnY, slc->r, slc->t);
  8963. nk_draw_image(b,
  8964. nk_rect(r.x + r.w - (float)slc->r, r.y, (float)slc->r, (float)slc->t),
  8965. &img, col);
  8966. /* center-left */
  8967. IMG_RGN(rgnX, rgnY + slc->t, slc->l, rgnH - slc->t - slc->b);
  8968. nk_draw_image(b,
  8969. nk_rect(r.x, r.y + (float)slc->t, (float)slc->l, (float)(r.h - slc->t - slc->b)),
  8970. &img, col);
  8971. /* center */
  8972. IMG_RGN(rgnX + slc->l, rgnY + slc->t, rgnW - slc->l - slc->r, rgnH - slc->t - slc->b);
  8973. nk_draw_image(b,
  8974. 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)),
  8975. &img, col);
  8976. /* center-right */
  8977. IMG_RGN(rgnX + rgnW - slc->r, rgnY + slc->t, slc->r, rgnH - slc->t - slc->b);
  8978. nk_draw_image(b,
  8979. nk_rect(r.x + r.w - (float)slc->r, r.y + (float)slc->t, (float)slc->r, (float)(r.h - slc->t - slc->b)),
  8980. &img, col);
  8981. /* bottom-left */
  8982. IMG_RGN(rgnX, rgnY + rgnH - slc->b, slc->l, slc->b);
  8983. nk_draw_image(b,
  8984. nk_rect(r.x, r.y + r.h - (float)slc->b, (float)slc->l, (float)slc->b),
  8985. &img, col);
  8986. /* bottom-center */
  8987. IMG_RGN(rgnX + slc->l, rgnY + rgnH - slc->b, rgnW - slc->l - slc->r, slc->b);
  8988. nk_draw_image(b,
  8989. nk_rect(r.x + (float)slc->l, r.y + r.h - (float)slc->b, (float)(r.w - slc->l - slc->r), (float)slc->b),
  8990. &img, col);
  8991. /* bottom-right */
  8992. IMG_RGN(rgnX + rgnW - slc->r, rgnY + rgnH - slc->b, slc->r, slc->b);
  8993. nk_draw_image(b,
  8994. nk_rect(r.x + r.w - (float)slc->r, r.y + r.h - (float)slc->b, (float)slc->r, (float)slc->b),
  8995. &img, col);
  8996. #undef IMG_RGN
  8997. }
  8998. NK_API void
  8999. nk_push_custom(struct nk_command_buffer *b, struct nk_rect r,
  9000. nk_command_custom_callback cb, nk_handle usr)
  9001. {
  9002. struct nk_command_custom *cmd;
  9003. NK_ASSERT(b);
  9004. if (!b) return;
  9005. if (b->use_clipping) {
  9006. const struct nk_rect *c = &b->clip;
  9007. 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))
  9008. return;
  9009. }
  9010. cmd = (struct nk_command_custom*)
  9011. nk_command_buffer_push(b, NK_COMMAND_CUSTOM, sizeof(*cmd));
  9012. if (!cmd) return;
  9013. cmd->x = (short)r.x;
  9014. cmd->y = (short)r.y;
  9015. cmd->w = (unsigned short)NK_MAX(0, r.w);
  9016. cmd->h = (unsigned short)NK_MAX(0, r.h);
  9017. cmd->callback_data = usr;
  9018. cmd->callback = cb;
  9019. }
  9020. NK_API void
  9021. nk_draw_text(struct nk_command_buffer *b, struct nk_rect r,
  9022. const char *string, int length, const struct nk_user_font *font,
  9023. struct nk_color bg, struct nk_color fg)
  9024. {
  9025. float text_width = 0;
  9026. struct nk_command_text *cmd;
  9027. NK_ASSERT(b);
  9028. NK_ASSERT(font);
  9029. if (!b || !string || !length || (bg.a == 0 && fg.a == 0)) return;
  9030. if (b->use_clipping) {
  9031. const struct nk_rect *c = &b->clip;
  9032. 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))
  9033. return;
  9034. }
  9035. /* make sure text fits inside bounds */
  9036. text_width = font->width(font->userdata, font->height, string, length);
  9037. if (text_width > r.w){
  9038. int glyphs = 0;
  9039. float txt_width = (float)text_width;
  9040. length = nk_text_clamp(font, string, length, r.w, &glyphs, &txt_width, 0,0);
  9041. }
  9042. if (!length) return;
  9043. cmd = (struct nk_command_text*)
  9044. nk_command_buffer_push(b, NK_COMMAND_TEXT, sizeof(*cmd) + (nk_size)(length + 1));
  9045. if (!cmd) return;
  9046. cmd->x = (short)r.x;
  9047. cmd->y = (short)r.y;
  9048. cmd->w = (unsigned short)r.w;
  9049. cmd->h = (unsigned short)r.h;
  9050. cmd->background = bg;
  9051. cmd->foreground = fg;
  9052. cmd->font = font;
  9053. cmd->length = length;
  9054. cmd->height = font->height;
  9055. NK_MEMCPY(cmd->string, string, (nk_size)length);
  9056. cmd->string[length] = '\0';
  9057. }
  9058. /* ===============================================================
  9059. *
  9060. * VERTEX
  9061. *
  9062. * ===============================================================*/
  9063. #ifdef NK_INCLUDE_VERTEX_BUFFER_OUTPUT
  9064. NK_API void
  9065. nk_draw_list_init(struct nk_draw_list *list)
  9066. {
  9067. nk_size i = 0;
  9068. NK_ASSERT(list);
  9069. if (!list) return;
  9070. nk_zero(list, sizeof(*list));
  9071. for (i = 0; i < NK_LEN(list->circle_vtx); ++i) {
  9072. const float a = ((float)i / (float)NK_LEN(list->circle_vtx)) * 2 * NK_PI;
  9073. list->circle_vtx[i].x = (float)NK_COS(a);
  9074. list->circle_vtx[i].y = (float)NK_SIN(a);
  9075. }
  9076. }
  9077. NK_API void
  9078. nk_draw_list_setup(struct nk_draw_list *canvas, const struct nk_convert_config *config,
  9079. struct nk_buffer *cmds, struct nk_buffer *vertices, struct nk_buffer *elements,
  9080. enum nk_anti_aliasing line_aa, enum nk_anti_aliasing shape_aa)
  9081. {
  9082. NK_ASSERT(canvas);
  9083. NK_ASSERT(config);
  9084. NK_ASSERT(cmds);
  9085. NK_ASSERT(vertices);
  9086. NK_ASSERT(elements);
  9087. if (!canvas || !config || !cmds || !vertices || !elements)
  9088. return;
  9089. canvas->buffer = cmds;
  9090. canvas->config = *config;
  9091. canvas->elements = elements;
  9092. canvas->vertices = vertices;
  9093. canvas->line_AA = line_aa;
  9094. canvas->shape_AA = shape_aa;
  9095. canvas->clip_rect = nk_null_rect;
  9096. canvas->cmd_offset = 0;
  9097. canvas->element_count = 0;
  9098. canvas->vertex_count = 0;
  9099. canvas->cmd_offset = 0;
  9100. canvas->cmd_count = 0;
  9101. canvas->path_count = 0;
  9102. }
  9103. NK_API const struct nk_draw_command*
  9104. nk__draw_list_begin(const struct nk_draw_list *canvas, const struct nk_buffer *buffer)
  9105. {
  9106. nk_byte *memory;
  9107. nk_size offset;
  9108. const struct nk_draw_command *cmd;
  9109. NK_ASSERT(buffer);
  9110. if (!buffer || !buffer->size || !canvas->cmd_count)
  9111. return 0;
  9112. memory = (nk_byte*)buffer->memory.ptr;
  9113. offset = buffer->memory.size - canvas->cmd_offset;
  9114. cmd = nk_ptr_add(const struct nk_draw_command, memory, offset);
  9115. return cmd;
  9116. }
  9117. NK_API const struct nk_draw_command*
  9118. nk__draw_list_end(const struct nk_draw_list *canvas, const struct nk_buffer *buffer)
  9119. {
  9120. nk_size size;
  9121. nk_size offset;
  9122. nk_byte *memory;
  9123. const struct nk_draw_command *end;
  9124. NK_ASSERT(buffer);
  9125. NK_ASSERT(canvas);
  9126. if (!buffer || !canvas)
  9127. return 0;
  9128. memory = (nk_byte*)buffer->memory.ptr;
  9129. size = buffer->memory.size;
  9130. offset = size - canvas->cmd_offset;
  9131. end = nk_ptr_add(const struct nk_draw_command, memory, offset);
  9132. end -= (canvas->cmd_count-1);
  9133. return end;
  9134. }
  9135. NK_API const struct nk_draw_command*
  9136. nk__draw_list_next(const struct nk_draw_command *cmd,
  9137. const struct nk_buffer *buffer, const struct nk_draw_list *canvas)
  9138. {
  9139. const struct nk_draw_command *end;
  9140. NK_ASSERT(buffer);
  9141. NK_ASSERT(canvas);
  9142. if (!cmd || !buffer || !canvas)
  9143. return 0;
  9144. end = nk__draw_list_end(canvas, buffer);
  9145. if (cmd <= end) return 0;
  9146. return (cmd-1);
  9147. }
  9148. NK_INTERN struct nk_vec2*
  9149. nk_draw_list_alloc_path(struct nk_draw_list *list, int count)
  9150. {
  9151. struct nk_vec2 *points;
  9152. NK_STORAGE const nk_size point_align = NK_ALIGNOF(struct nk_vec2);
  9153. NK_STORAGE const nk_size point_size = sizeof(struct nk_vec2);
  9154. points = (struct nk_vec2*)
  9155. nk_buffer_alloc(list->buffer, NK_BUFFER_FRONT,
  9156. point_size * (nk_size)count, point_align);
  9157. if (!points) return 0;
  9158. if (!list->path_offset) {
  9159. void *memory = nk_buffer_memory(list->buffer);
  9160. list->path_offset = (unsigned int)((nk_byte*)points - (nk_byte*)memory);
  9161. }
  9162. list->path_count += (unsigned int)count;
  9163. return points;
  9164. }
  9165. NK_INTERN struct nk_vec2
  9166. nk_draw_list_path_last(struct nk_draw_list *list)
  9167. {
  9168. void *memory;
  9169. struct nk_vec2 *point;
  9170. NK_ASSERT(list->path_count);
  9171. memory = nk_buffer_memory(list->buffer);
  9172. point = nk_ptr_add(struct nk_vec2, memory, list->path_offset);
  9173. point += (list->path_count-1);
  9174. return *point;
  9175. }
  9176. NK_INTERN struct nk_draw_command*
  9177. nk_draw_list_push_command(struct nk_draw_list *list, struct nk_rect clip,
  9178. nk_handle texture)
  9179. {
  9180. NK_STORAGE const nk_size cmd_align = NK_ALIGNOF(struct nk_draw_command);
  9181. NK_STORAGE const nk_size cmd_size = sizeof(struct nk_draw_command);
  9182. struct nk_draw_command *cmd;
  9183. NK_ASSERT(list);
  9184. cmd = (struct nk_draw_command*)
  9185. nk_buffer_alloc(list->buffer, NK_BUFFER_BACK, cmd_size, cmd_align);
  9186. if (!cmd) return 0;
  9187. if (!list->cmd_count) {
  9188. nk_byte *memory = (nk_byte*)nk_buffer_memory(list->buffer);
  9189. nk_size total = nk_buffer_total(list->buffer);
  9190. memory = nk_ptr_add(nk_byte, memory, total);
  9191. list->cmd_offset = (nk_size)(memory - (nk_byte*)cmd);
  9192. }
  9193. cmd->elem_count = 0;
  9194. cmd->clip_rect = clip;
  9195. cmd->texture = texture;
  9196. #ifdef NK_INCLUDE_COMMAND_USERDATA
  9197. cmd->userdata = list->userdata;
  9198. #endif
  9199. list->cmd_count++;
  9200. list->clip_rect = clip;
  9201. return cmd;
  9202. }
  9203. NK_INTERN struct nk_draw_command*
  9204. nk_draw_list_command_last(struct nk_draw_list *list)
  9205. {
  9206. void *memory;
  9207. nk_size size;
  9208. struct nk_draw_command *cmd;
  9209. NK_ASSERT(list->cmd_count);
  9210. memory = nk_buffer_memory(list->buffer);
  9211. size = nk_buffer_total(list->buffer);
  9212. cmd = nk_ptr_add(struct nk_draw_command, memory, size - list->cmd_offset);
  9213. return (cmd - (list->cmd_count-1));
  9214. }
  9215. NK_INTERN void
  9216. nk_draw_list_add_clip(struct nk_draw_list *list, struct nk_rect rect)
  9217. {
  9218. NK_ASSERT(list);
  9219. if (!list) return;
  9220. if (!list->cmd_count) {
  9221. nk_draw_list_push_command(list, rect, list->config.tex_null.texture);
  9222. } else {
  9223. struct nk_draw_command *prev = nk_draw_list_command_last(list);
  9224. if (prev->elem_count == 0)
  9225. prev->clip_rect = rect;
  9226. nk_draw_list_push_command(list, rect, prev->texture);
  9227. }
  9228. }
  9229. NK_INTERN void
  9230. nk_draw_list_push_image(struct nk_draw_list *list, nk_handle texture)
  9231. {
  9232. NK_ASSERT(list);
  9233. if (!list) return;
  9234. if (!list->cmd_count) {
  9235. nk_draw_list_push_command(list, nk_null_rect, texture);
  9236. } else {
  9237. struct nk_draw_command *prev = nk_draw_list_command_last(list);
  9238. if (prev->elem_count == 0) {
  9239. prev->texture = texture;
  9240. #ifdef NK_INCLUDE_COMMAND_USERDATA
  9241. prev->userdata = list->userdata;
  9242. #endif
  9243. } else if (prev->texture.id != texture.id
  9244. #ifdef NK_INCLUDE_COMMAND_USERDATA
  9245. || prev->userdata.id != list->userdata.id
  9246. #endif
  9247. ) {
  9248. nk_draw_list_push_command(list, prev->clip_rect, texture);
  9249. }
  9250. }
  9251. }
  9252. #ifdef NK_INCLUDE_COMMAND_USERDATA
  9253. NK_API void
  9254. nk_draw_list_push_userdata(struct nk_draw_list *list, nk_handle userdata)
  9255. {
  9256. list->userdata = userdata;
  9257. }
  9258. #endif
  9259. NK_INTERN void*
  9260. nk_draw_list_alloc_vertices(struct nk_draw_list *list, nk_size count)
  9261. {
  9262. void *vtx;
  9263. NK_ASSERT(list);
  9264. if (!list) return 0;
  9265. vtx = nk_buffer_alloc(list->vertices, NK_BUFFER_FRONT,
  9266. list->config.vertex_size*count, list->config.vertex_alignment);
  9267. if (!vtx) return 0;
  9268. list->vertex_count += (unsigned int)count;
  9269. /* This assert triggers because your are drawing a lot of stuff and nuklear
  9270. * defined `nk_draw_index` as `nk_ushort` to safe space be default.
  9271. *
  9272. * So you reached the maximum number of indices or rather vertexes.
  9273. * To solve this issue please change typedef `nk_draw_index` to `nk_uint`
  9274. * and don't forget to specify the new element size in your drawing
  9275. * backend (OpenGL, DirectX, ...). For example in OpenGL for `glDrawElements`
  9276. * instead of specifying `GL_UNSIGNED_SHORT` you have to define `GL_UNSIGNED_INT`.
  9277. * Sorry for the inconvenience. */
  9278. if(sizeof(nk_draw_index)==2) NK_ASSERT((list->vertex_count < NK_USHORT_MAX &&
  9279. "To many vertices for 16-bit vertex indices. Please read comment above on how to solve this problem"));
  9280. return vtx;
  9281. }
  9282. NK_INTERN nk_draw_index*
  9283. nk_draw_list_alloc_elements(struct nk_draw_list *list, nk_size count)
  9284. {
  9285. nk_draw_index *ids;
  9286. struct nk_draw_command *cmd;
  9287. NK_STORAGE const nk_size elem_align = NK_ALIGNOF(nk_draw_index);
  9288. NK_STORAGE const nk_size elem_size = sizeof(nk_draw_index);
  9289. NK_ASSERT(list);
  9290. if (!list) return 0;
  9291. ids = (nk_draw_index*)
  9292. nk_buffer_alloc(list->elements, NK_BUFFER_FRONT, elem_size*count, elem_align);
  9293. if (!ids) return 0;
  9294. cmd = nk_draw_list_command_last(list);
  9295. list->element_count += (unsigned int)count;
  9296. cmd->elem_count += (unsigned int)count;
  9297. return ids;
  9298. }
  9299. NK_INTERN int
  9300. nk_draw_vertex_layout_element_is_end_of_layout(
  9301. const struct nk_draw_vertex_layout_element *element)
  9302. {
  9303. return (element->attribute == NK_VERTEX_ATTRIBUTE_COUNT ||
  9304. element->format == NK_FORMAT_COUNT);
  9305. }
  9306. NK_INTERN void
  9307. nk_draw_vertex_color(void *attr, const float *vals,
  9308. enum nk_draw_vertex_layout_format format)
  9309. {
  9310. /* if this triggers you tried to provide a value format for a color */
  9311. float val[4];
  9312. NK_ASSERT(format >= NK_FORMAT_COLOR_BEGIN);
  9313. NK_ASSERT(format <= NK_FORMAT_COLOR_END);
  9314. if (format < NK_FORMAT_COLOR_BEGIN || format > NK_FORMAT_COLOR_END) return;
  9315. val[0] = NK_SATURATE(vals[0]);
  9316. val[1] = NK_SATURATE(vals[1]);
  9317. val[2] = NK_SATURATE(vals[2]);
  9318. val[3] = NK_SATURATE(vals[3]);
  9319. switch (format) {
  9320. default: NK_ASSERT(0 && "Invalid vertex layout color format"); break;
  9321. case NK_FORMAT_R8G8B8A8:
  9322. case NK_FORMAT_R8G8B8: {
  9323. struct nk_color col = nk_rgba_fv(val);
  9324. NK_MEMCPY(attr, &col.r, sizeof(col));
  9325. } break;
  9326. case NK_FORMAT_B8G8R8A8: {
  9327. struct nk_color col = nk_rgba_fv(val);
  9328. struct nk_color bgra = nk_rgba(col.b, col.g, col.r, col.a);
  9329. NK_MEMCPY(attr, &bgra, sizeof(bgra));
  9330. } break;
  9331. case NK_FORMAT_R16G15B16: {
  9332. nk_ushort col[3];
  9333. col[0] = (nk_ushort)(val[0]*(float)NK_USHORT_MAX);
  9334. col[1] = (nk_ushort)(val[1]*(float)NK_USHORT_MAX);
  9335. col[2] = (nk_ushort)(val[2]*(float)NK_USHORT_MAX);
  9336. NK_MEMCPY(attr, col, sizeof(col));
  9337. } break;
  9338. case NK_FORMAT_R16G15B16A16: {
  9339. nk_ushort col[4];
  9340. col[0] = (nk_ushort)(val[0]*(float)NK_USHORT_MAX);
  9341. col[1] = (nk_ushort)(val[1]*(float)NK_USHORT_MAX);
  9342. col[2] = (nk_ushort)(val[2]*(float)NK_USHORT_MAX);
  9343. col[3] = (nk_ushort)(val[3]*(float)NK_USHORT_MAX);
  9344. NK_MEMCPY(attr, col, sizeof(col));
  9345. } break;
  9346. case NK_FORMAT_R32G32B32: {
  9347. nk_uint col[3];
  9348. col[0] = (nk_uint)(val[0]*(float)NK_UINT_MAX);
  9349. col[1] = (nk_uint)(val[1]*(float)NK_UINT_MAX);
  9350. col[2] = (nk_uint)(val[2]*(float)NK_UINT_MAX);
  9351. NK_MEMCPY(attr, col, sizeof(col));
  9352. } break;
  9353. case NK_FORMAT_R32G32B32A32: {
  9354. nk_uint col[4];
  9355. col[0] = (nk_uint)(val[0]*(float)NK_UINT_MAX);
  9356. col[1] = (nk_uint)(val[1]*(float)NK_UINT_MAX);
  9357. col[2] = (nk_uint)(val[2]*(float)NK_UINT_MAX);
  9358. col[3] = (nk_uint)(val[3]*(float)NK_UINT_MAX);
  9359. NK_MEMCPY(attr, col, sizeof(col));
  9360. } break;
  9361. case NK_FORMAT_R32G32B32A32_FLOAT:
  9362. NK_MEMCPY(attr, val, sizeof(float)*4);
  9363. break;
  9364. case NK_FORMAT_R32G32B32A32_DOUBLE: {
  9365. double col[4];
  9366. col[0] = (double)val[0];
  9367. col[1] = (double)val[1];
  9368. col[2] = (double)val[2];
  9369. col[3] = (double)val[3];
  9370. NK_MEMCPY(attr, col, sizeof(col));
  9371. } break;
  9372. case NK_FORMAT_RGB32:
  9373. case NK_FORMAT_RGBA32: {
  9374. struct nk_color col = nk_rgba_fv(val);
  9375. nk_uint color = nk_color_u32(col);
  9376. NK_MEMCPY(attr, &color, sizeof(color));
  9377. } break; }
  9378. }
  9379. NK_INTERN void
  9380. nk_draw_vertex_element(void *dst, const float *values, int value_count,
  9381. enum nk_draw_vertex_layout_format format)
  9382. {
  9383. int value_index;
  9384. void *attribute = dst;
  9385. /* if this triggers you tried to provide a color format for a value */
  9386. NK_ASSERT(format < NK_FORMAT_COLOR_BEGIN);
  9387. if (format >= NK_FORMAT_COLOR_BEGIN && format <= NK_FORMAT_COLOR_END) return;
  9388. for (value_index = 0; value_index < value_count; ++value_index) {
  9389. switch (format) {
  9390. default: NK_ASSERT(0 && "invalid vertex layout format"); break;
  9391. case NK_FORMAT_SCHAR: {
  9392. char value = (char)NK_CLAMP((float)NK_SCHAR_MIN, values[value_index], (float)NK_SCHAR_MAX);
  9393. NK_MEMCPY(attribute, &value, sizeof(value));
  9394. attribute = (void*)((char*)attribute + sizeof(char));
  9395. } break;
  9396. case NK_FORMAT_SSHORT: {
  9397. nk_short value = (nk_short)NK_CLAMP((float)NK_SSHORT_MIN, values[value_index], (float)NK_SSHORT_MAX);
  9398. NK_MEMCPY(attribute, &value, sizeof(value));
  9399. attribute = (void*)((char*)attribute + sizeof(value));
  9400. } break;
  9401. case NK_FORMAT_SINT: {
  9402. nk_int value = (nk_int)NK_CLAMP((float)NK_SINT_MIN, values[value_index], (float)NK_SINT_MAX);
  9403. NK_MEMCPY(attribute, &value, sizeof(value));
  9404. attribute = (void*)((char*)attribute + sizeof(nk_int));
  9405. } break;
  9406. case NK_FORMAT_UCHAR: {
  9407. unsigned char value = (unsigned char)NK_CLAMP((float)NK_UCHAR_MIN, values[value_index], (float)NK_UCHAR_MAX);
  9408. NK_MEMCPY(attribute, &value, sizeof(value));
  9409. attribute = (void*)((char*)attribute + sizeof(unsigned char));
  9410. } break;
  9411. case NK_FORMAT_USHORT: {
  9412. nk_ushort value = (nk_ushort)NK_CLAMP((float)NK_USHORT_MIN, values[value_index], (float)NK_USHORT_MAX);
  9413. NK_MEMCPY(attribute, &value, sizeof(value));
  9414. attribute = (void*)((char*)attribute + sizeof(value));
  9415. } break;
  9416. case NK_FORMAT_UINT: {
  9417. nk_uint value = (nk_uint)NK_CLAMP((float)NK_UINT_MIN, values[value_index], (float)NK_UINT_MAX);
  9418. NK_MEMCPY(attribute, &value, sizeof(value));
  9419. attribute = (void*)((char*)attribute + sizeof(nk_uint));
  9420. } break;
  9421. case NK_FORMAT_FLOAT:
  9422. NK_MEMCPY(attribute, &values[value_index], sizeof(values[value_index]));
  9423. attribute = (void*)((char*)attribute + sizeof(float));
  9424. break;
  9425. case NK_FORMAT_DOUBLE: {
  9426. double value = (double)values[value_index];
  9427. NK_MEMCPY(attribute, &value, sizeof(value));
  9428. attribute = (void*)((char*)attribute + sizeof(double));
  9429. } break;
  9430. }
  9431. }
  9432. }
  9433. NK_INTERN void*
  9434. nk_draw_vertex(void *dst, const struct nk_convert_config *config,
  9435. struct nk_vec2 pos, struct nk_vec2 uv, struct nk_colorf color)
  9436. {
  9437. void *result = (void*)((char*)dst + config->vertex_size);
  9438. const struct nk_draw_vertex_layout_element *elem_iter = config->vertex_layout;
  9439. while (!nk_draw_vertex_layout_element_is_end_of_layout(elem_iter)) {
  9440. void *address = (void*)((char*)dst + elem_iter->offset);
  9441. switch (elem_iter->attribute) {
  9442. case NK_VERTEX_ATTRIBUTE_COUNT:
  9443. default: NK_ASSERT(0 && "wrong element attribute"); break;
  9444. case NK_VERTEX_POSITION: nk_draw_vertex_element(address, &pos.x, 2, elem_iter->format); break;
  9445. case NK_VERTEX_TEXCOORD: nk_draw_vertex_element(address, &uv.x, 2, elem_iter->format); break;
  9446. case NK_VERTEX_COLOR: nk_draw_vertex_color(address, &color.r, elem_iter->format); break;
  9447. }
  9448. elem_iter++;
  9449. }
  9450. return result;
  9451. }
  9452. NK_API void
  9453. nk_draw_list_stroke_poly_line(struct nk_draw_list *list, const struct nk_vec2 *points,
  9454. const unsigned int points_count, struct nk_color color, enum nk_draw_list_stroke closed,
  9455. float thickness, enum nk_anti_aliasing aliasing)
  9456. {
  9457. nk_size count;
  9458. int thick_line;
  9459. struct nk_colorf col;
  9460. struct nk_colorf col_trans;
  9461. NK_ASSERT(list);
  9462. if (!list || points_count < 2) return;
  9463. color.a = (nk_byte)((float)color.a * list->config.global_alpha);
  9464. count = points_count;
  9465. if (!closed) count = points_count-1;
  9466. thick_line = thickness > 1.0f;
  9467. #ifdef NK_INCLUDE_COMMAND_USERDATA
  9468. nk_draw_list_push_userdata(list, list->userdata);
  9469. #endif
  9470. color.a = (nk_byte)((float)color.a * list->config.global_alpha);
  9471. nk_color_fv(&col.r, color);
  9472. col_trans = col;
  9473. col_trans.a = 0;
  9474. if (aliasing == NK_ANTI_ALIASING_ON) {
  9475. /* ANTI-ALIASED STROKE */
  9476. const float AA_SIZE = 1.0f;
  9477. NK_STORAGE const nk_size pnt_align = NK_ALIGNOF(struct nk_vec2);
  9478. NK_STORAGE const nk_size pnt_size = sizeof(struct nk_vec2);
  9479. /* allocate vertices and elements */
  9480. nk_size i1 = 0;
  9481. nk_size vertex_offset;
  9482. nk_size index = list->vertex_count;
  9483. const nk_size idx_count = (thick_line) ? (count * 18) : (count * 12);
  9484. const nk_size vtx_count = (thick_line) ? (points_count * 4): (points_count *3);
  9485. void *vtx = nk_draw_list_alloc_vertices(list, vtx_count);
  9486. nk_draw_index *ids = nk_draw_list_alloc_elements(list, idx_count);
  9487. nk_size size;
  9488. struct nk_vec2 *normals, *temp;
  9489. if (!vtx || !ids) return;
  9490. /* temporary allocate normals + points */
  9491. vertex_offset = (nk_size)((nk_byte*)vtx - (nk_byte*)list->vertices->memory.ptr);
  9492. nk_buffer_mark(list->vertices, NK_BUFFER_FRONT);
  9493. size = pnt_size * ((thick_line) ? 5 : 3) * points_count;
  9494. normals = (struct nk_vec2*) nk_buffer_alloc(list->vertices, NK_BUFFER_FRONT, size, pnt_align);
  9495. if (!normals) return;
  9496. temp = normals + points_count;
  9497. /* make sure vertex pointer is still correct */
  9498. vtx = (void*)((nk_byte*)list->vertices->memory.ptr + vertex_offset);
  9499. /* calculate normals */
  9500. for (i1 = 0; i1 < count; ++i1) {
  9501. const nk_size i2 = ((i1 + 1) == points_count) ? 0 : (i1 + 1);
  9502. struct nk_vec2 diff = nk_vec2_sub(points[i2], points[i1]);
  9503. float len;
  9504. /* vec2 inverted length */
  9505. len = nk_vec2_len_sqr(diff);
  9506. if (len != 0.0f)
  9507. len = NK_INV_SQRT(len);
  9508. else len = 1.0f;
  9509. diff = nk_vec2_muls(diff, len);
  9510. normals[i1].x = diff.y;
  9511. normals[i1].y = -diff.x;
  9512. }
  9513. if (!closed)
  9514. normals[points_count-1] = normals[points_count-2];
  9515. if (!thick_line) {
  9516. nk_size idx1, i;
  9517. if (!closed) {
  9518. struct nk_vec2 d;
  9519. temp[0] = nk_vec2_add(points[0], nk_vec2_muls(normals[0], AA_SIZE));
  9520. temp[1] = nk_vec2_sub(points[0], nk_vec2_muls(normals[0], AA_SIZE));
  9521. d = nk_vec2_muls(normals[points_count-1], AA_SIZE);
  9522. temp[(points_count-1) * 2 + 0] = nk_vec2_add(points[points_count-1], d);
  9523. temp[(points_count-1) * 2 + 1] = nk_vec2_sub(points[points_count-1], d);
  9524. }
  9525. /* fill elements */
  9526. idx1 = index;
  9527. for (i1 = 0; i1 < count; i1++) {
  9528. struct nk_vec2 dm;
  9529. float dmr2;
  9530. nk_size i2 = ((i1 + 1) == points_count) ? 0 : (i1 + 1);
  9531. nk_size idx2 = ((i1+1) == points_count) ? index: (idx1 + 3);
  9532. /* average normals */
  9533. dm = nk_vec2_muls(nk_vec2_add(normals[i1], normals[i2]), 0.5f);
  9534. dmr2 = dm.x * dm.x + dm.y* dm.y;
  9535. if (dmr2 > 0.000001f) {
  9536. float scale = 1.0f/dmr2;
  9537. scale = NK_MIN(100.0f, scale);
  9538. dm = nk_vec2_muls(dm, scale);
  9539. }
  9540. dm = nk_vec2_muls(dm, AA_SIZE);
  9541. temp[i2*2+0] = nk_vec2_add(points[i2], dm);
  9542. temp[i2*2+1] = nk_vec2_sub(points[i2], dm);
  9543. ids[0] = (nk_draw_index)(idx2 + 0); ids[1] = (nk_draw_index)(idx1+0);
  9544. ids[2] = (nk_draw_index)(idx1 + 2); ids[3] = (nk_draw_index)(idx1+2);
  9545. ids[4] = (nk_draw_index)(idx2 + 2); ids[5] = (nk_draw_index)(idx2+0);
  9546. ids[6] = (nk_draw_index)(idx2 + 1); ids[7] = (nk_draw_index)(idx1+1);
  9547. ids[8] = (nk_draw_index)(idx1 + 0); ids[9] = (nk_draw_index)(idx1+0);
  9548. ids[10]= (nk_draw_index)(idx2 + 0); ids[11]= (nk_draw_index)(idx2+1);
  9549. ids += 12;
  9550. idx1 = idx2;
  9551. }
  9552. /* fill vertices */
  9553. for (i = 0; i < points_count; ++i) {
  9554. const struct nk_vec2 uv = list->config.tex_null.uv;
  9555. vtx = nk_draw_vertex(vtx, &list->config, points[i], uv, col);
  9556. vtx = nk_draw_vertex(vtx, &list->config, temp[i*2+0], uv, col_trans);
  9557. vtx = nk_draw_vertex(vtx, &list->config, temp[i*2+1], uv, col_trans);
  9558. }
  9559. } else {
  9560. nk_size idx1, i;
  9561. const float half_inner_thickness = (thickness - AA_SIZE) * 0.5f;
  9562. if (!closed) {
  9563. struct nk_vec2 d1 = nk_vec2_muls(normals[0], half_inner_thickness + AA_SIZE);
  9564. struct nk_vec2 d2 = nk_vec2_muls(normals[0], half_inner_thickness);
  9565. temp[0] = nk_vec2_add(points[0], d1);
  9566. temp[1] = nk_vec2_add(points[0], d2);
  9567. temp[2] = nk_vec2_sub(points[0], d2);
  9568. temp[3] = nk_vec2_sub(points[0], d1);
  9569. d1 = nk_vec2_muls(normals[points_count-1], half_inner_thickness + AA_SIZE);
  9570. d2 = nk_vec2_muls(normals[points_count-1], half_inner_thickness);
  9571. temp[(points_count-1)*4+0] = nk_vec2_add(points[points_count-1], d1);
  9572. temp[(points_count-1)*4+1] = nk_vec2_add(points[points_count-1], d2);
  9573. temp[(points_count-1)*4+2] = nk_vec2_sub(points[points_count-1], d2);
  9574. temp[(points_count-1)*4+3] = nk_vec2_sub(points[points_count-1], d1);
  9575. }
  9576. /* add all elements */
  9577. idx1 = index;
  9578. for (i1 = 0; i1 < count; ++i1) {
  9579. struct nk_vec2 dm_out, dm_in;
  9580. const nk_size i2 = ((i1+1) == points_count) ? 0: (i1 + 1);
  9581. nk_size idx2 = ((i1+1) == points_count) ? index: (idx1 + 4);
  9582. /* average normals */
  9583. struct nk_vec2 dm = nk_vec2_muls(nk_vec2_add(normals[i1], normals[i2]), 0.5f);
  9584. float dmr2 = dm.x * dm.x + dm.y* dm.y;
  9585. if (dmr2 > 0.000001f) {
  9586. float scale = 1.0f/dmr2;
  9587. scale = NK_MIN(100.0f, scale);
  9588. dm = nk_vec2_muls(dm, scale);
  9589. }
  9590. dm_out = nk_vec2_muls(dm, ((half_inner_thickness) + AA_SIZE));
  9591. dm_in = nk_vec2_muls(dm, half_inner_thickness);
  9592. temp[i2*4+0] = nk_vec2_add(points[i2], dm_out);
  9593. temp[i2*4+1] = nk_vec2_add(points[i2], dm_in);
  9594. temp[i2*4+2] = nk_vec2_sub(points[i2], dm_in);
  9595. temp[i2*4+3] = nk_vec2_sub(points[i2], dm_out);
  9596. /* add indexes */
  9597. ids[0] = (nk_draw_index)(idx2 + 1); ids[1] = (nk_draw_index)(idx1+1);
  9598. ids[2] = (nk_draw_index)(idx1 + 2); ids[3] = (nk_draw_index)(idx1+2);
  9599. ids[4] = (nk_draw_index)(idx2 + 2); ids[5] = (nk_draw_index)(idx2+1);
  9600. ids[6] = (nk_draw_index)(idx2 + 1); ids[7] = (nk_draw_index)(idx1+1);
  9601. ids[8] = (nk_draw_index)(idx1 + 0); ids[9] = (nk_draw_index)(idx1+0);
  9602. ids[10]= (nk_draw_index)(idx2 + 0); ids[11] = (nk_draw_index)(idx2+1);
  9603. ids[12]= (nk_draw_index)(idx2 + 2); ids[13] = (nk_draw_index)(idx1+2);
  9604. ids[14]= (nk_draw_index)(idx1 + 3); ids[15] = (nk_draw_index)(idx1+3);
  9605. ids[16]= (nk_draw_index)(idx2 + 3); ids[17] = (nk_draw_index)(idx2+2);
  9606. ids += 18;
  9607. idx1 = idx2;
  9608. }
  9609. /* add vertices */
  9610. for (i = 0; i < points_count; ++i) {
  9611. const struct nk_vec2 uv = list->config.tex_null.uv;
  9612. vtx = nk_draw_vertex(vtx, &list->config, temp[i*4+0], uv, col_trans);
  9613. vtx = nk_draw_vertex(vtx, &list->config, temp[i*4+1], uv, col);
  9614. vtx = nk_draw_vertex(vtx, &list->config, temp[i*4+2], uv, col);
  9615. vtx = nk_draw_vertex(vtx, &list->config, temp[i*4+3], uv, col_trans);
  9616. }
  9617. }
  9618. /* free temporary normals + points */
  9619. nk_buffer_reset(list->vertices, NK_BUFFER_FRONT);
  9620. } else {
  9621. /* NON ANTI-ALIASED STROKE */
  9622. nk_size i1 = 0;
  9623. nk_size idx = list->vertex_count;
  9624. const nk_size idx_count = count * 6;
  9625. const nk_size vtx_count = count * 4;
  9626. void *vtx = nk_draw_list_alloc_vertices(list, vtx_count);
  9627. nk_draw_index *ids = nk_draw_list_alloc_elements(list, idx_count);
  9628. if (!vtx || !ids) return;
  9629. for (i1 = 0; i1 < count; ++i1) {
  9630. float dx, dy;
  9631. const struct nk_vec2 uv = list->config.tex_null.uv;
  9632. const nk_size i2 = ((i1+1) == points_count) ? 0 : i1 + 1;
  9633. const struct nk_vec2 p1 = points[i1];
  9634. const struct nk_vec2 p2 = points[i2];
  9635. struct nk_vec2 diff = nk_vec2_sub(p2, p1);
  9636. float len;
  9637. /* vec2 inverted length */
  9638. len = nk_vec2_len_sqr(diff);
  9639. if (len != 0.0f)
  9640. len = NK_INV_SQRT(len);
  9641. else len = 1.0f;
  9642. diff = nk_vec2_muls(diff, len);
  9643. /* add vertices */
  9644. dx = diff.x * (thickness * 0.5f);
  9645. dy = diff.y * (thickness * 0.5f);
  9646. vtx = nk_draw_vertex(vtx, &list->config, nk_vec2(p1.x + dy, p1.y - dx), uv, col);
  9647. vtx = nk_draw_vertex(vtx, &list->config, nk_vec2(p2.x + dy, p2.y - dx), uv, col);
  9648. vtx = nk_draw_vertex(vtx, &list->config, nk_vec2(p2.x - dy, p2.y + dx), uv, col);
  9649. vtx = nk_draw_vertex(vtx, &list->config, nk_vec2(p1.x - dy, p1.y + dx), uv, col);
  9650. ids[0] = (nk_draw_index)(idx+0); ids[1] = (nk_draw_index)(idx+1);
  9651. ids[2] = (nk_draw_index)(idx+2); ids[3] = (nk_draw_index)(idx+0);
  9652. ids[4] = (nk_draw_index)(idx+2); ids[5] = (nk_draw_index)(idx+3);
  9653. ids += 6;
  9654. idx += 4;
  9655. }
  9656. }
  9657. }
  9658. NK_API void
  9659. nk_draw_list_fill_poly_convex(struct nk_draw_list *list,
  9660. const struct nk_vec2 *points, const unsigned int points_count,
  9661. struct nk_color color, enum nk_anti_aliasing aliasing)
  9662. {
  9663. struct nk_colorf col;
  9664. struct nk_colorf col_trans;
  9665. NK_STORAGE const nk_size pnt_align = NK_ALIGNOF(struct nk_vec2);
  9666. NK_STORAGE const nk_size pnt_size = sizeof(struct nk_vec2);
  9667. NK_ASSERT(list);
  9668. if (!list || points_count < 3) return;
  9669. #ifdef NK_INCLUDE_COMMAND_USERDATA
  9670. nk_draw_list_push_userdata(list, list->userdata);
  9671. #endif
  9672. color.a = (nk_byte)((float)color.a * list->config.global_alpha);
  9673. nk_color_fv(&col.r, color);
  9674. col_trans = col;
  9675. col_trans.a = 0;
  9676. if (aliasing == NK_ANTI_ALIASING_ON) {
  9677. nk_size i = 0;
  9678. nk_size i0 = 0;
  9679. nk_size i1 = 0;
  9680. const float AA_SIZE = 1.0f;
  9681. nk_size vertex_offset = 0;
  9682. nk_size index = list->vertex_count;
  9683. const nk_size idx_count = (points_count-2)*3 + points_count*6;
  9684. const nk_size vtx_count = (points_count*2);
  9685. void *vtx = nk_draw_list_alloc_vertices(list, vtx_count);
  9686. nk_draw_index *ids = nk_draw_list_alloc_elements(list, idx_count);
  9687. nk_size size = 0;
  9688. struct nk_vec2 *normals = 0;
  9689. unsigned int vtx_inner_idx = (unsigned int)(index + 0);
  9690. unsigned int vtx_outer_idx = (unsigned int)(index + 1);
  9691. if (!vtx || !ids) return;
  9692. /* temporary allocate normals */
  9693. vertex_offset = (nk_size)((nk_byte*)vtx - (nk_byte*)list->vertices->memory.ptr);
  9694. nk_buffer_mark(list->vertices, NK_BUFFER_FRONT);
  9695. size = pnt_size * points_count;
  9696. normals = (struct nk_vec2*) nk_buffer_alloc(list->vertices, NK_BUFFER_FRONT, size, pnt_align);
  9697. if (!normals) return;
  9698. vtx = (void*)((nk_byte*)list->vertices->memory.ptr + vertex_offset);
  9699. /* add elements */
  9700. for (i = 2; i < points_count; i++) {
  9701. ids[0] = (nk_draw_index)(vtx_inner_idx);
  9702. ids[1] = (nk_draw_index)(vtx_inner_idx + ((i-1) << 1));
  9703. ids[2] = (nk_draw_index)(vtx_inner_idx + (i << 1));
  9704. ids += 3;
  9705. }
  9706. /* compute normals */
  9707. for (i0 = points_count-1, i1 = 0; i1 < points_count; i0 = i1++) {
  9708. struct nk_vec2 p0 = points[i0];
  9709. struct nk_vec2 p1 = points[i1];
  9710. struct nk_vec2 diff = nk_vec2_sub(p1, p0);
  9711. /* vec2 inverted length */
  9712. float len = nk_vec2_len_sqr(diff);
  9713. if (len != 0.0f)
  9714. len = NK_INV_SQRT(len);
  9715. else len = 1.0f;
  9716. diff = nk_vec2_muls(diff, len);
  9717. normals[i0].x = diff.y;
  9718. normals[i0].y = -diff.x;
  9719. }
  9720. /* add vertices + indexes */
  9721. for (i0 = points_count-1, i1 = 0; i1 < points_count; i0 = i1++) {
  9722. const struct nk_vec2 uv = list->config.tex_null.uv;
  9723. struct nk_vec2 n0 = normals[i0];
  9724. struct nk_vec2 n1 = normals[i1];
  9725. struct nk_vec2 dm = nk_vec2_muls(nk_vec2_add(n0, n1), 0.5f);
  9726. float dmr2 = dm.x*dm.x + dm.y*dm.y;
  9727. if (dmr2 > 0.000001f) {
  9728. float scale = 1.0f / dmr2;
  9729. scale = NK_MIN(scale, 100.0f);
  9730. dm = nk_vec2_muls(dm, scale);
  9731. }
  9732. dm = nk_vec2_muls(dm, AA_SIZE * 0.5f);
  9733. /* add vertices */
  9734. vtx = nk_draw_vertex(vtx, &list->config, nk_vec2_sub(points[i1], dm), uv, col);
  9735. vtx = nk_draw_vertex(vtx, &list->config, nk_vec2_add(points[i1], dm), uv, col_trans);
  9736. /* add indexes */
  9737. ids[0] = (nk_draw_index)(vtx_inner_idx+(i1<<1));
  9738. ids[1] = (nk_draw_index)(vtx_inner_idx+(i0<<1));
  9739. ids[2] = (nk_draw_index)(vtx_outer_idx+(i0<<1));
  9740. ids[3] = (nk_draw_index)(vtx_outer_idx+(i0<<1));
  9741. ids[4] = (nk_draw_index)(vtx_outer_idx+(i1<<1));
  9742. ids[5] = (nk_draw_index)(vtx_inner_idx+(i1<<1));
  9743. ids += 6;
  9744. }
  9745. /* free temporary normals + points */
  9746. nk_buffer_reset(list->vertices, NK_BUFFER_FRONT);
  9747. } else {
  9748. nk_size i = 0;
  9749. nk_size index = list->vertex_count;
  9750. const nk_size idx_count = (points_count-2)*3;
  9751. const nk_size vtx_count = points_count;
  9752. void *vtx = nk_draw_list_alloc_vertices(list, vtx_count);
  9753. nk_draw_index *ids = nk_draw_list_alloc_elements(list, idx_count);
  9754. if (!vtx || !ids) return;
  9755. for (i = 0; i < vtx_count; ++i)
  9756. vtx = nk_draw_vertex(vtx, &list->config, points[i], list->config.tex_null.uv, col);
  9757. for (i = 2; i < points_count; ++i) {
  9758. ids[0] = (nk_draw_index)index;
  9759. ids[1] = (nk_draw_index)(index+ i - 1);
  9760. ids[2] = (nk_draw_index)(index+i);
  9761. ids += 3;
  9762. }
  9763. }
  9764. }
  9765. NK_API void
  9766. nk_draw_list_path_clear(struct nk_draw_list *list)
  9767. {
  9768. NK_ASSERT(list);
  9769. if (!list) return;
  9770. nk_buffer_reset(list->buffer, NK_BUFFER_FRONT);
  9771. list->path_count = 0;
  9772. list->path_offset = 0;
  9773. }
  9774. NK_API void
  9775. nk_draw_list_path_line_to(struct nk_draw_list *list, struct nk_vec2 pos)
  9776. {
  9777. struct nk_vec2 *points = 0;
  9778. struct nk_draw_command *cmd = 0;
  9779. NK_ASSERT(list);
  9780. if (!list) return;
  9781. if (!list->cmd_count)
  9782. nk_draw_list_add_clip(list, nk_null_rect);
  9783. cmd = nk_draw_list_command_last(list);
  9784. if (cmd && cmd->texture.ptr != list->config.tex_null.texture.ptr)
  9785. nk_draw_list_push_image(list, list->config.tex_null.texture);
  9786. points = nk_draw_list_alloc_path(list, 1);
  9787. if (!points) return;
  9788. points[0] = pos;
  9789. }
  9790. NK_API void
  9791. nk_draw_list_path_arc_to_fast(struct nk_draw_list *list, struct nk_vec2 center,
  9792. float radius, int a_min, int a_max)
  9793. {
  9794. int a = 0;
  9795. NK_ASSERT(list);
  9796. if (!list) return;
  9797. if (a_min <= a_max) {
  9798. for (a = a_min; a <= a_max; a++) {
  9799. const struct nk_vec2 c = list->circle_vtx[(nk_size)a % NK_LEN(list->circle_vtx)];
  9800. const float x = center.x + c.x * radius;
  9801. const float y = center.y + c.y * radius;
  9802. nk_draw_list_path_line_to(list, nk_vec2(x, y));
  9803. }
  9804. }
  9805. }
  9806. NK_API void
  9807. nk_draw_list_path_arc_to(struct nk_draw_list *list, struct nk_vec2 center,
  9808. float radius, float a_min, float a_max, unsigned int segments)
  9809. {
  9810. unsigned int i = 0;
  9811. NK_ASSERT(list);
  9812. if (!list) return;
  9813. if (radius == 0.0f) return;
  9814. /* This algorithm for arc drawing relies on these two trigonometric identities[1]:
  9815. sin(a + b) = sin(a) * cos(b) + cos(a) * sin(b)
  9816. cos(a + b) = cos(a) * cos(b) - sin(a) * sin(b)
  9817. Two coordinates (x, y) of a point on a circle centered on
  9818. the origin can be written in polar form as:
  9819. x = r * cos(a)
  9820. y = r * sin(a)
  9821. where r is the radius of the circle,
  9822. a is the angle between (x, y) and the origin.
  9823. This allows us to rotate the coordinates around the
  9824. origin by an angle b using the following transformation:
  9825. x' = r * cos(a + b) = x * cos(b) - y * sin(b)
  9826. y' = r * sin(a + b) = y * cos(b) + x * sin(b)
  9827. [1] https://en.wikipedia.org/wiki/List_of_trigonometric_identities#Angle_sum_and_difference_identities
  9828. */
  9829. {const float d_angle = (a_max - a_min) / (float)segments;
  9830. const float sin_d = (float)NK_SIN(d_angle);
  9831. const float cos_d = (float)NK_COS(d_angle);
  9832. float cx = (float)NK_COS(a_min) * radius;
  9833. float cy = (float)NK_SIN(a_min) * radius;
  9834. for(i = 0; i <= segments; ++i) {
  9835. float new_cx, new_cy;
  9836. const float x = center.x + cx;
  9837. const float y = center.y + cy;
  9838. nk_draw_list_path_line_to(list, nk_vec2(x, y));
  9839. new_cx = cx * cos_d - cy * sin_d;
  9840. new_cy = cy * cos_d + cx * sin_d;
  9841. cx = new_cx;
  9842. cy = new_cy;
  9843. }}
  9844. }
  9845. NK_API void
  9846. nk_draw_list_path_rect_to(struct nk_draw_list *list, struct nk_vec2 a,
  9847. struct nk_vec2 b, float rounding)
  9848. {
  9849. float r;
  9850. NK_ASSERT(list);
  9851. if (!list) return;
  9852. r = rounding;
  9853. r = NK_MIN(r, ((b.x-a.x) < 0) ? -(b.x-a.x): (b.x-a.x));
  9854. r = NK_MIN(r, ((b.y-a.y) < 0) ? -(b.y-a.y): (b.y-a.y));
  9855. if (r == 0.0f) {
  9856. nk_draw_list_path_line_to(list, a);
  9857. nk_draw_list_path_line_to(list, nk_vec2(b.x,a.y));
  9858. nk_draw_list_path_line_to(list, b);
  9859. nk_draw_list_path_line_to(list, nk_vec2(a.x,b.y));
  9860. } else {
  9861. nk_draw_list_path_arc_to_fast(list, nk_vec2(a.x + r, a.y + r), r, 6, 9);
  9862. nk_draw_list_path_arc_to_fast(list, nk_vec2(b.x - r, a.y + r), r, 9, 12);
  9863. nk_draw_list_path_arc_to_fast(list, nk_vec2(b.x - r, b.y - r), r, 0, 3);
  9864. nk_draw_list_path_arc_to_fast(list, nk_vec2(a.x + r, b.y - r), r, 3, 6);
  9865. }
  9866. }
  9867. NK_API void
  9868. nk_draw_list_path_curve_to(struct nk_draw_list *list, struct nk_vec2 p2,
  9869. struct nk_vec2 p3, struct nk_vec2 p4, unsigned int num_segments)
  9870. {
  9871. float t_step;
  9872. unsigned int i_step;
  9873. struct nk_vec2 p1;
  9874. NK_ASSERT(list);
  9875. NK_ASSERT(list->path_count);
  9876. if (!list || !list->path_count) return;
  9877. num_segments = NK_MAX(num_segments, 1);
  9878. p1 = nk_draw_list_path_last(list);
  9879. t_step = 1.0f/(float)num_segments;
  9880. for (i_step = 1; i_step <= num_segments; ++i_step) {
  9881. float t = t_step * (float)i_step;
  9882. float u = 1.0f - t;
  9883. float w1 = u*u*u;
  9884. float w2 = 3*u*u*t;
  9885. float w3 = 3*u*t*t;
  9886. float w4 = t * t *t;
  9887. float x = w1 * p1.x + w2 * p2.x + w3 * p3.x + w4 * p4.x;
  9888. float y = w1 * p1.y + w2 * p2.y + w3 * p3.y + w4 * p4.y;
  9889. nk_draw_list_path_line_to(list, nk_vec2(x,y));
  9890. }
  9891. }
  9892. NK_API void
  9893. nk_draw_list_path_fill(struct nk_draw_list *list, struct nk_color color)
  9894. {
  9895. struct nk_vec2 *points;
  9896. NK_ASSERT(list);
  9897. if (!list) return;
  9898. points = (struct nk_vec2*)nk_buffer_memory(list->buffer);
  9899. nk_draw_list_fill_poly_convex(list, points, list->path_count, color, list->config.shape_AA);
  9900. nk_draw_list_path_clear(list);
  9901. }
  9902. NK_API void
  9903. nk_draw_list_path_stroke(struct nk_draw_list *list, struct nk_color color,
  9904. enum nk_draw_list_stroke closed, float thickness)
  9905. {
  9906. struct nk_vec2 *points;
  9907. NK_ASSERT(list);
  9908. if (!list) return;
  9909. points = (struct nk_vec2*)nk_buffer_memory(list->buffer);
  9910. nk_draw_list_stroke_poly_line(list, points, list->path_count, color,
  9911. closed, thickness, list->config.line_AA);
  9912. nk_draw_list_path_clear(list);
  9913. }
  9914. NK_API void
  9915. nk_draw_list_stroke_line(struct nk_draw_list *list, struct nk_vec2 a,
  9916. struct nk_vec2 b, struct nk_color col, float thickness)
  9917. {
  9918. NK_ASSERT(list);
  9919. if (!list || !col.a) return;
  9920. if (list->line_AA == NK_ANTI_ALIASING_ON) {
  9921. nk_draw_list_path_line_to(list, a);
  9922. nk_draw_list_path_line_to(list, b);
  9923. } else {
  9924. nk_draw_list_path_line_to(list, nk_vec2_sub(a,nk_vec2(0.5f,0.5f)));
  9925. nk_draw_list_path_line_to(list, nk_vec2_sub(b,nk_vec2(0.5f,0.5f)));
  9926. }
  9927. nk_draw_list_path_stroke(list, col, NK_STROKE_OPEN, thickness);
  9928. }
  9929. NK_API void
  9930. nk_draw_list_fill_rect(struct nk_draw_list *list, struct nk_rect rect,
  9931. struct nk_color col, float rounding)
  9932. {
  9933. NK_ASSERT(list);
  9934. if (!list || !col.a) return;
  9935. if (list->line_AA == NK_ANTI_ALIASING_ON) {
  9936. nk_draw_list_path_rect_to(list, nk_vec2(rect.x, rect.y),
  9937. nk_vec2(rect.x + rect.w, rect.y + rect.h), rounding);
  9938. } else {
  9939. nk_draw_list_path_rect_to(list, nk_vec2(rect.x-0.5f, rect.y-0.5f),
  9940. nk_vec2(rect.x + rect.w, rect.y + rect.h), rounding);
  9941. } nk_draw_list_path_fill(list, col);
  9942. }
  9943. NK_API void
  9944. nk_draw_list_stroke_rect(struct nk_draw_list *list, struct nk_rect rect,
  9945. struct nk_color col, float rounding, float thickness)
  9946. {
  9947. NK_ASSERT(list);
  9948. if (!list || !col.a) return;
  9949. if (list->line_AA == NK_ANTI_ALIASING_ON) {
  9950. nk_draw_list_path_rect_to(list, nk_vec2(rect.x, rect.y),
  9951. nk_vec2(rect.x + rect.w, rect.y + rect.h), rounding);
  9952. } else {
  9953. nk_draw_list_path_rect_to(list, nk_vec2(rect.x-0.5f, rect.y-0.5f),
  9954. nk_vec2(rect.x + rect.w, rect.y + rect.h), rounding);
  9955. } nk_draw_list_path_stroke(list, col, NK_STROKE_CLOSED, thickness);
  9956. }
  9957. NK_API void
  9958. nk_draw_list_fill_rect_multi_color(struct nk_draw_list *list, struct nk_rect rect,
  9959. struct nk_color left, struct nk_color top, struct nk_color right,
  9960. struct nk_color bottom)
  9961. {
  9962. void *vtx;
  9963. struct nk_colorf col_left, col_top;
  9964. struct nk_colorf col_right, col_bottom;
  9965. nk_draw_index *idx;
  9966. nk_draw_index index;
  9967. nk_color_fv(&col_left.r, left);
  9968. nk_color_fv(&col_right.r, right);
  9969. nk_color_fv(&col_top.r, top);
  9970. nk_color_fv(&col_bottom.r, bottom);
  9971. NK_ASSERT(list);
  9972. if (!list) return;
  9973. nk_draw_list_push_image(list, list->config.tex_null.texture);
  9974. index = (nk_draw_index)list->vertex_count;
  9975. vtx = nk_draw_list_alloc_vertices(list, 4);
  9976. idx = nk_draw_list_alloc_elements(list, 6);
  9977. if (!vtx || !idx) return;
  9978. idx[0] = (nk_draw_index)(index+0); idx[1] = (nk_draw_index)(index+1);
  9979. idx[2] = (nk_draw_index)(index+2); idx[3] = (nk_draw_index)(index+0);
  9980. idx[4] = (nk_draw_index)(index+2); idx[5] = (nk_draw_index)(index+3);
  9981. vtx = nk_draw_vertex(vtx, &list->config, nk_vec2(rect.x, rect.y), list->config.tex_null.uv, col_left);
  9982. vtx = nk_draw_vertex(vtx, &list->config, nk_vec2(rect.x + rect.w, rect.y), list->config.tex_null.uv, col_top);
  9983. vtx = nk_draw_vertex(vtx, &list->config, nk_vec2(rect.x + rect.w, rect.y + rect.h), list->config.tex_null.uv, col_right);
  9984. vtx = nk_draw_vertex(vtx, &list->config, nk_vec2(rect.x, rect.y + rect.h), list->config.tex_null.uv, col_bottom);
  9985. }
  9986. NK_API void
  9987. nk_draw_list_fill_triangle(struct nk_draw_list *list, struct nk_vec2 a,
  9988. struct nk_vec2 b, struct nk_vec2 c, struct nk_color col)
  9989. {
  9990. NK_ASSERT(list);
  9991. if (!list || !col.a) return;
  9992. nk_draw_list_path_line_to(list, a);
  9993. nk_draw_list_path_line_to(list, b);
  9994. nk_draw_list_path_line_to(list, c);
  9995. nk_draw_list_path_fill(list, col);
  9996. }
  9997. NK_API void
  9998. nk_draw_list_stroke_triangle(struct nk_draw_list *list, struct nk_vec2 a,
  9999. struct nk_vec2 b, struct nk_vec2 c, struct nk_color col, float thickness)
  10000. {
  10001. NK_ASSERT(list);
  10002. if (!list || !col.a) return;
  10003. nk_draw_list_path_line_to(list, a);
  10004. nk_draw_list_path_line_to(list, b);
  10005. nk_draw_list_path_line_to(list, c);
  10006. nk_draw_list_path_stroke(list, col, NK_STROKE_CLOSED, thickness);
  10007. }
  10008. NK_API void
  10009. nk_draw_list_fill_circle(struct nk_draw_list *list, struct nk_vec2 center,
  10010. float radius, struct nk_color col, unsigned int segs)
  10011. {
  10012. float a_max;
  10013. NK_ASSERT(list);
  10014. if (!list || !col.a) return;
  10015. a_max = NK_PI * 2.0f * ((float)segs - 1.0f) / (float)segs;
  10016. nk_draw_list_path_arc_to(list, center, radius, 0.0f, a_max, segs);
  10017. nk_draw_list_path_fill(list, col);
  10018. }
  10019. NK_API void
  10020. nk_draw_list_stroke_circle(struct nk_draw_list *list, struct nk_vec2 center,
  10021. float radius, struct nk_color col, unsigned int segs, float thickness)
  10022. {
  10023. float a_max;
  10024. NK_ASSERT(list);
  10025. if (!list || !col.a) return;
  10026. a_max = NK_PI * 2.0f * ((float)segs - 1.0f) / (float)segs;
  10027. nk_draw_list_path_arc_to(list, center, radius, 0.0f, a_max, segs);
  10028. nk_draw_list_path_stroke(list, col, NK_STROKE_CLOSED, thickness);
  10029. }
  10030. NK_API void
  10031. nk_draw_list_stroke_curve(struct nk_draw_list *list, struct nk_vec2 p0,
  10032. struct nk_vec2 cp0, struct nk_vec2 cp1, struct nk_vec2 p1,
  10033. struct nk_color col, unsigned int segments, float thickness)
  10034. {
  10035. NK_ASSERT(list);
  10036. if (!list || !col.a) return;
  10037. nk_draw_list_path_line_to(list, p0);
  10038. nk_draw_list_path_curve_to(list, cp0, cp1, p1, segments);
  10039. nk_draw_list_path_stroke(list, col, NK_STROKE_OPEN, thickness);
  10040. }
  10041. NK_INTERN void
  10042. nk_draw_list_push_rect_uv(struct nk_draw_list *list, struct nk_vec2 a,
  10043. struct nk_vec2 c, struct nk_vec2 uva, struct nk_vec2 uvc,
  10044. struct nk_color color)
  10045. {
  10046. void *vtx;
  10047. struct nk_vec2 uvb;
  10048. struct nk_vec2 uvd;
  10049. struct nk_vec2 b;
  10050. struct nk_vec2 d;
  10051. struct nk_colorf col;
  10052. nk_draw_index *idx;
  10053. nk_draw_index index;
  10054. NK_ASSERT(list);
  10055. if (!list) return;
  10056. nk_color_fv(&col.r, color);
  10057. uvb = nk_vec2(uvc.x, uva.y);
  10058. uvd = nk_vec2(uva.x, uvc.y);
  10059. b = nk_vec2(c.x, a.y);
  10060. d = nk_vec2(a.x, c.y);
  10061. index = (nk_draw_index)list->vertex_count;
  10062. vtx = nk_draw_list_alloc_vertices(list, 4);
  10063. idx = nk_draw_list_alloc_elements(list, 6);
  10064. if (!vtx || !idx) return;
  10065. idx[0] = (nk_draw_index)(index+0); idx[1] = (nk_draw_index)(index+1);
  10066. idx[2] = (nk_draw_index)(index+2); idx[3] = (nk_draw_index)(index+0);
  10067. idx[4] = (nk_draw_index)(index+2); idx[5] = (nk_draw_index)(index+3);
  10068. vtx = nk_draw_vertex(vtx, &list->config, a, uva, col);
  10069. vtx = nk_draw_vertex(vtx, &list->config, b, uvb, col);
  10070. vtx = nk_draw_vertex(vtx, &list->config, c, uvc, col);
  10071. vtx = nk_draw_vertex(vtx, &list->config, d, uvd, col);
  10072. }
  10073. NK_API void
  10074. nk_draw_list_add_image(struct nk_draw_list *list, struct nk_image texture,
  10075. struct nk_rect rect, struct nk_color color)
  10076. {
  10077. NK_ASSERT(list);
  10078. if (!list) return;
  10079. /* push new command with given texture */
  10080. nk_draw_list_push_image(list, texture.handle);
  10081. if (nk_image_is_subimage(&texture)) {
  10082. /* add region inside of the texture */
  10083. struct nk_vec2 uv[2];
  10084. uv[0].x = (float)texture.region[0]/(float)texture.w;
  10085. uv[0].y = (float)texture.region[1]/(float)texture.h;
  10086. uv[1].x = (float)(texture.region[0] + texture.region[2])/(float)texture.w;
  10087. uv[1].y = (float)(texture.region[1] + texture.region[3])/(float)texture.h;
  10088. nk_draw_list_push_rect_uv(list, nk_vec2(rect.x, rect.y),
  10089. nk_vec2(rect.x + rect.w, rect.y + rect.h), uv[0], uv[1], color);
  10090. } else nk_draw_list_push_rect_uv(list, nk_vec2(rect.x, rect.y),
  10091. nk_vec2(rect.x + rect.w, rect.y + rect.h),
  10092. nk_vec2(0.0f, 0.0f), nk_vec2(1.0f, 1.0f),color);
  10093. }
  10094. NK_API void
  10095. nk_draw_list_add_text(struct nk_draw_list *list, const struct nk_user_font *font,
  10096. struct nk_rect rect, const char *text, int len, float font_height,
  10097. struct nk_color fg)
  10098. {
  10099. float x = 0;
  10100. int text_len = 0;
  10101. nk_rune unicode = 0;
  10102. nk_rune next = 0;
  10103. int glyph_len = 0;
  10104. int next_glyph_len = 0;
  10105. struct nk_user_font_glyph g;
  10106. NK_ASSERT(list);
  10107. if (!list || !len || !text) return;
  10108. if (!NK_INTERSECT(rect.x, rect.y, rect.w, rect.h,
  10109. list->clip_rect.x, list->clip_rect.y, list->clip_rect.w, list->clip_rect.h)) return;
  10110. nk_draw_list_push_image(list, font->texture);
  10111. x = rect.x;
  10112. glyph_len = nk_utf_decode(text, &unicode, len);
  10113. if (!glyph_len) return;
  10114. /* draw every glyph image */
  10115. fg.a = (nk_byte)((float)fg.a * list->config.global_alpha);
  10116. while (text_len < len && glyph_len) {
  10117. float gx, gy, gh, gw;
  10118. float char_width = 0;
  10119. if (unicode == NK_UTF_INVALID) break;
  10120. /* query currently drawn glyph information */
  10121. next_glyph_len = nk_utf_decode(text + text_len + glyph_len, &next, (int)len - text_len);
  10122. font->query(font->userdata, font_height, &g, unicode,
  10123. (next == NK_UTF_INVALID) ? '\0' : next);
  10124. /* calculate and draw glyph drawing rectangle and image */
  10125. gx = x + g.offset.x;
  10126. gy = rect.y + g.offset.y;
  10127. gw = g.width; gh = g.height;
  10128. char_width = g.xadvance;
  10129. nk_draw_list_push_rect_uv(list, nk_vec2(gx,gy), nk_vec2(gx + gw, gy+ gh),
  10130. g.uv[0], g.uv[1], fg);
  10131. /* offset next glyph */
  10132. text_len += glyph_len;
  10133. x += char_width;
  10134. glyph_len = next_glyph_len;
  10135. unicode = next;
  10136. }
  10137. }
  10138. NK_API nk_flags
  10139. nk_convert(struct nk_context *ctx, struct nk_buffer *cmds,
  10140. struct nk_buffer *vertices, struct nk_buffer *elements,
  10141. const struct nk_convert_config *config)
  10142. {
  10143. nk_flags res = NK_CONVERT_SUCCESS;
  10144. const struct nk_command *cmd;
  10145. NK_ASSERT(ctx);
  10146. NK_ASSERT(cmds);
  10147. NK_ASSERT(vertices);
  10148. NK_ASSERT(elements);
  10149. NK_ASSERT(config);
  10150. NK_ASSERT(config->vertex_layout);
  10151. NK_ASSERT(config->vertex_size);
  10152. if (!ctx || !cmds || !vertices || !elements || !config || !config->vertex_layout)
  10153. return NK_CONVERT_INVALID_PARAM;
  10154. nk_draw_list_setup(&ctx->draw_list, config, cmds, vertices, elements,
  10155. config->line_AA, config->shape_AA);
  10156. nk_foreach(cmd, ctx)
  10157. {
  10158. #ifdef NK_INCLUDE_COMMAND_USERDATA
  10159. ctx->draw_list.userdata = cmd->userdata;
  10160. #endif
  10161. switch (cmd->type) {
  10162. case NK_COMMAND_NOP: break;
  10163. case NK_COMMAND_SCISSOR: {
  10164. const struct nk_command_scissor *s = (const struct nk_command_scissor*)cmd;
  10165. nk_draw_list_add_clip(&ctx->draw_list, nk_rect(s->x, s->y, s->w, s->h));
  10166. } break;
  10167. case NK_COMMAND_LINE: {
  10168. const struct nk_command_line *l = (const struct nk_command_line*)cmd;
  10169. nk_draw_list_stroke_line(&ctx->draw_list, nk_vec2(l->begin.x, l->begin.y),
  10170. nk_vec2(l->end.x, l->end.y), l->color, l->line_thickness);
  10171. } break;
  10172. case NK_COMMAND_CURVE: {
  10173. const struct nk_command_curve *q = (const struct nk_command_curve*)cmd;
  10174. nk_draw_list_stroke_curve(&ctx->draw_list, nk_vec2(q->begin.x, q->begin.y),
  10175. nk_vec2(q->ctrl[0].x, q->ctrl[0].y), nk_vec2(q->ctrl[1].x,
  10176. q->ctrl[1].y), nk_vec2(q->end.x, q->end.y), q->color,
  10177. config->curve_segment_count, q->line_thickness);
  10178. } break;
  10179. case NK_COMMAND_RECT: {
  10180. const struct nk_command_rect *r = (const struct nk_command_rect*)cmd;
  10181. nk_draw_list_stroke_rect(&ctx->draw_list, nk_rect(r->x, r->y, r->w, r->h),
  10182. r->color, (float)r->rounding, r->line_thickness);
  10183. } break;
  10184. case NK_COMMAND_RECT_FILLED: {
  10185. const struct nk_command_rect_filled *r = (const struct nk_command_rect_filled*)cmd;
  10186. nk_draw_list_fill_rect(&ctx->draw_list, nk_rect(r->x, r->y, r->w, r->h),
  10187. r->color, (float)r->rounding);
  10188. } break;
  10189. case NK_COMMAND_RECT_MULTI_COLOR: {
  10190. const struct nk_command_rect_multi_color *r = (const struct nk_command_rect_multi_color*)cmd;
  10191. nk_draw_list_fill_rect_multi_color(&ctx->draw_list, nk_rect(r->x, r->y, r->w, r->h),
  10192. r->left, r->top, r->right, r->bottom);
  10193. } break;
  10194. case NK_COMMAND_CIRCLE: {
  10195. const struct nk_command_circle *c = (const struct nk_command_circle*)cmd;
  10196. nk_draw_list_stroke_circle(&ctx->draw_list, nk_vec2((float)c->x + (float)c->w/2,
  10197. (float)c->y + (float)c->h/2), (float)c->w/2, c->color,
  10198. config->circle_segment_count, c->line_thickness);
  10199. } break;
  10200. case NK_COMMAND_CIRCLE_FILLED: {
  10201. const struct nk_command_circle_filled *c = (const struct nk_command_circle_filled *)cmd;
  10202. nk_draw_list_fill_circle(&ctx->draw_list, nk_vec2((float)c->x + (float)c->w/2,
  10203. (float)c->y + (float)c->h/2), (float)c->w/2, c->color,
  10204. config->circle_segment_count);
  10205. } break;
  10206. case NK_COMMAND_ARC: {
  10207. const struct nk_command_arc *c = (const struct nk_command_arc*)cmd;
  10208. nk_draw_list_path_line_to(&ctx->draw_list, nk_vec2(c->cx, c->cy));
  10209. nk_draw_list_path_arc_to(&ctx->draw_list, nk_vec2(c->cx, c->cy), c->r,
  10210. c->a[0], c->a[1], config->arc_segment_count);
  10211. nk_draw_list_path_stroke(&ctx->draw_list, c->color, NK_STROKE_CLOSED, c->line_thickness);
  10212. } break;
  10213. case NK_COMMAND_ARC_FILLED: {
  10214. const struct nk_command_arc_filled *c = (const struct nk_command_arc_filled*)cmd;
  10215. nk_draw_list_path_line_to(&ctx->draw_list, nk_vec2(c->cx, c->cy));
  10216. nk_draw_list_path_arc_to(&ctx->draw_list, nk_vec2(c->cx, c->cy), c->r,
  10217. c->a[0], c->a[1], config->arc_segment_count);
  10218. nk_draw_list_path_fill(&ctx->draw_list, c->color);
  10219. } break;
  10220. case NK_COMMAND_TRIANGLE: {
  10221. const struct nk_command_triangle *t = (const struct nk_command_triangle*)cmd;
  10222. nk_draw_list_stroke_triangle(&ctx->draw_list, nk_vec2(t->a.x, t->a.y),
  10223. nk_vec2(t->b.x, t->b.y), nk_vec2(t->c.x, t->c.y), t->color,
  10224. t->line_thickness);
  10225. } break;
  10226. case NK_COMMAND_TRIANGLE_FILLED: {
  10227. const struct nk_command_triangle_filled *t = (const struct nk_command_triangle_filled*)cmd;
  10228. nk_draw_list_fill_triangle(&ctx->draw_list, nk_vec2(t->a.x, t->a.y),
  10229. nk_vec2(t->b.x, t->b.y), nk_vec2(t->c.x, t->c.y), t->color);
  10230. } break;
  10231. case NK_COMMAND_POLYGON: {
  10232. int i;
  10233. const struct nk_command_polygon*p = (const struct nk_command_polygon*)cmd;
  10234. for (i = 0; i < p->point_count; ++i) {
  10235. struct nk_vec2 pnt = nk_vec2((float)p->points[i].x, (float)p->points[i].y);
  10236. nk_draw_list_path_line_to(&ctx->draw_list, pnt);
  10237. }
  10238. nk_draw_list_path_stroke(&ctx->draw_list, p->color, NK_STROKE_CLOSED, p->line_thickness);
  10239. } break;
  10240. case NK_COMMAND_POLYGON_FILLED: {
  10241. int i;
  10242. const struct nk_command_polygon_filled *p = (const struct nk_command_polygon_filled*)cmd;
  10243. for (i = 0; i < p->point_count; ++i) {
  10244. struct nk_vec2 pnt = nk_vec2((float)p->points[i].x, (float)p->points[i].y);
  10245. nk_draw_list_path_line_to(&ctx->draw_list, pnt);
  10246. }
  10247. nk_draw_list_path_fill(&ctx->draw_list, p->color);
  10248. } break;
  10249. case NK_COMMAND_POLYLINE: {
  10250. int i;
  10251. const struct nk_command_polyline *p = (const struct nk_command_polyline*)cmd;
  10252. for (i = 0; i < p->point_count; ++i) {
  10253. struct nk_vec2 pnt = nk_vec2((float)p->points[i].x, (float)p->points[i].y);
  10254. nk_draw_list_path_line_to(&ctx->draw_list, pnt);
  10255. }
  10256. nk_draw_list_path_stroke(&ctx->draw_list, p->color, NK_STROKE_OPEN, p->line_thickness);
  10257. } break;
  10258. case NK_COMMAND_TEXT: {
  10259. const struct nk_command_text *t = (const struct nk_command_text*)cmd;
  10260. nk_draw_list_add_text(&ctx->draw_list, t->font, nk_rect(t->x, t->y, t->w, t->h),
  10261. t->string, t->length, t->height, t->foreground);
  10262. } break;
  10263. case NK_COMMAND_IMAGE: {
  10264. const struct nk_command_image *i = (const struct nk_command_image*)cmd;
  10265. nk_draw_list_add_image(&ctx->draw_list, i->img, nk_rect(i->x, i->y, i->w, i->h), i->col);
  10266. } break;
  10267. case NK_COMMAND_CUSTOM: {
  10268. const struct nk_command_custom *c = (const struct nk_command_custom*)cmd;
  10269. c->callback(&ctx->draw_list, c->x, c->y, c->w, c->h, c->callback_data);
  10270. } break;
  10271. default: break;
  10272. }
  10273. }
  10274. res |= (cmds->needed > cmds->allocated + (cmds->memory.size - cmds->size)) ? NK_CONVERT_COMMAND_BUFFER_FULL: 0;
  10275. res |= (vertices->needed > vertices->allocated) ? NK_CONVERT_VERTEX_BUFFER_FULL: 0;
  10276. res |= (elements->needed > elements->allocated) ? NK_CONVERT_ELEMENT_BUFFER_FULL: 0;
  10277. return res;
  10278. }
  10279. NK_API const struct nk_draw_command*
  10280. nk__draw_begin(const struct nk_context *ctx,
  10281. const struct nk_buffer *buffer)
  10282. {
  10283. return nk__draw_list_begin(&ctx->draw_list, buffer);
  10284. }
  10285. NK_API const struct nk_draw_command*
  10286. nk__draw_end(const struct nk_context *ctx, const struct nk_buffer *buffer)
  10287. {
  10288. return nk__draw_list_end(&ctx->draw_list, buffer);
  10289. }
  10290. NK_API const struct nk_draw_command*
  10291. nk__draw_next(const struct nk_draw_command *cmd,
  10292. const struct nk_buffer *buffer, const struct nk_context *ctx)
  10293. {
  10294. return nk__draw_list_next(cmd, buffer, &ctx->draw_list);
  10295. }
  10296. #endif
  10297. /* stb_rect_pack.h - v1.01 - public domain - rectangle packing */
  10298. /* Sean Barrett 2014 */
  10299. /* */
  10300. /* Useful for e.g. packing rectangular textures into an atlas. */
  10301. /* Does not do rotation. */
  10302. /* */
  10303. /* Before #including, */
  10304. /* */
  10305. /* #define STB_RECT_PACK_IMPLEMENTATION */
  10306. /* */
  10307. /* in the file that you want to have the implementation. */
  10308. /* */
  10309. /* Not necessarily the awesomest packing method, but better than */
  10310. /* the totally naive one in stb_truetype (which is primarily what */
  10311. /* this is meant to replace). */
  10312. /* */
  10313. /* Has only had a few tests run, may have issues. */
  10314. /* */
  10315. /* More docs to come. */
  10316. /* */
  10317. /* No memory allocations; uses qsort() and assert() from stdlib. */
  10318. /* Can override those by defining STBRP_SORT and STBRP_ASSERT. */
  10319. /* */
  10320. /* This library currently uses the Skyline Bottom-Left algorithm. */
  10321. /* */
  10322. /* Please note: better rectangle packers are welcome! Please */
  10323. /* implement them to the same API, but with a different init */
  10324. /* function. */
  10325. /* */
  10326. /* Credits */
  10327. /* */
  10328. /* Library */
  10329. /* Sean Barrett */
  10330. /* Minor features */
  10331. /* Martins Mozeiko */
  10332. /* github:IntellectualKitty */
  10333. /* */
  10334. /* Bugfixes / warning fixes */
  10335. /* Jeremy Jaussaud */
  10336. /* Fabian Giesen */
  10337. /* */
  10338. /* Version history: */
  10339. /* */
  10340. /* 1.01 (2021-07-11) always use large rect mode, expose STBRP__MAXVAL in public section */
  10341. /* 1.00 (2019-02-25) avoid small space waste; gracefully fail too-wide rectangles */
  10342. /* 0.99 (2019-02-07) warning fixes */
  10343. /* 0.11 (2017-03-03) return packing success/fail result */
  10344. /* 0.10 (2016-10-25) remove cast-away-const to avoid warnings */
  10345. /* 0.09 (2016-08-27) fix compiler warnings */
  10346. /* 0.08 (2015-09-13) really fix bug with empty rects (w=0 or h=0) */
  10347. /* 0.07 (2015-09-13) fix bug with empty rects (w=0 or h=0) */
  10348. /* 0.06 (2015-04-15) added STBRP_SORT to allow replacing qsort */
  10349. /* 0.05: added STBRP_ASSERT to allow replacing assert */
  10350. /* 0.04: fixed minor bug in STBRP_LARGE_RECTS support */
  10351. /* 0.01: initial release */
  10352. /* */
  10353. /* LICENSE */
  10354. /* */
  10355. /* See end of file for license information. */
  10356. /* //////////////////////////////////////////////////////////////////////////// */
  10357. /* */
  10358. /* INCLUDE SECTION */
  10359. /* */
  10360. #ifndef STB_INCLUDE_STB_RECT_PACK_H
  10361. #define STB_INCLUDE_STB_RECT_PACK_H
  10362. #define STB_RECT_PACK_VERSION 1
  10363. #ifdef STBRP_STATIC
  10364. #define STBRP_DEF static
  10365. #else
  10366. #define STBRP_DEF extern
  10367. #endif
  10368. #ifdef __cplusplus
  10369. extern "C" {
  10370. #endif
  10371. typedef struct stbrp_context stbrp_context;
  10372. typedef struct stbrp_node stbrp_node;
  10373. typedef struct stbrp_rect stbrp_rect;
  10374. typedef int stbrp_coord;
  10375. #define STBRP__MAXVAL 0x7fffffff
  10376. /* Mostly for internal use, but this is the maximum supported coordinate value. */
  10377. STBRP_DEF int stbrp_pack_rects (stbrp_context *context, stbrp_rect *rects, int num_rects);
  10378. /* Assign packed locations to rectangles. The rectangles are of type */
  10379. /* 'stbrp_rect' defined below, stored in the array 'rects', and there */
  10380. /* are 'num_rects' many of them. */
  10381. /* */
  10382. /* Rectangles which are successfully packed have the 'was_packed' flag */
  10383. /* set to a non-zero value and 'x' and 'y' store the minimum location */
  10384. /* on each axis (i.e. bottom-left in cartesian coordinates, top-left */
  10385. /* if you imagine y increasing downwards). Rectangles which do not fit */
  10386. /* have the 'was_packed' flag set to 0. */
  10387. /* */
  10388. /* You should not try to access the 'rects' array from another thread */
  10389. /* while this function is running, as the function temporarily reorders */
  10390. /* the array while it executes. */
  10391. /* */
  10392. /* To pack into another rectangle, you need to call stbrp_init_target */
  10393. /* again. To continue packing into the same rectangle, you can call */
  10394. /* this function again. Calling this multiple times with multiple rect */
  10395. /* arrays will probably produce worse packing results than calling it */
  10396. /* a single time with the full rectangle array, but the option is */
  10397. /* available. */
  10398. /* */
  10399. /* The function returns 1 if all of the rectangles were successfully */
  10400. /* packed and 0 otherwise. */
  10401. struct stbrp_rect
  10402. {
  10403. /* reserved for your use: */
  10404. int id;
  10405. /* input: */
  10406. stbrp_coord w, h;
  10407. /* output: */
  10408. stbrp_coord x, y;
  10409. int was_packed; /* non-zero if valid packing */
  10410. }; /* 16 bytes, nominally */
  10411. STBRP_DEF void stbrp_init_target (stbrp_context *context, int width, int height, stbrp_node *nodes, int num_nodes);
  10412. /* Initialize a rectangle packer to: */
  10413. /* pack a rectangle that is 'width' by 'height' in dimensions */
  10414. /* using temporary storage provided by the array 'nodes', which is 'num_nodes' long */
  10415. /* */
  10416. /* You must call this function every time you start packing into a new target. */
  10417. /* */
  10418. /* There is no "shutdown" function. The 'nodes' memory must stay valid for */
  10419. /* the following stbrp_pack_rects() call (or calls), but can be freed after */
  10420. /* the call (or calls) finish. */
  10421. /* */
  10422. /* Note: to guarantee best results, either: */
  10423. /* 1. make sure 'num_nodes' >= 'width' */
  10424. /* or 2. call stbrp_allow_out_of_mem() defined below with 'allow_out_of_mem = 1' */
  10425. /* */
  10426. /* If you don't do either of the above things, widths will be quantized to multiples */
  10427. /* of small integers to guarantee the algorithm doesn't run out of temporary storage. */
  10428. /* */
  10429. /* If you do #2, then the non-quantized algorithm will be used, but the algorithm */
  10430. /* may run out of temporary storage and be unable to pack some rectangles. */
  10431. STBRP_DEF void stbrp_setup_allow_out_of_mem (stbrp_context *context, int allow_out_of_mem);
  10432. /* Optionally call this function after init but before doing any packing to */
  10433. /* change the handling of the out-of-temp-memory scenario, described above. */
  10434. /* If you call init again, this will be reset to the default (false). */
  10435. STBRP_DEF void stbrp_setup_heuristic (stbrp_context *context, int heuristic);
  10436. /* Optionally select which packing heuristic the library should use. Different */
  10437. /* heuristics will produce better/worse results for different data sets. */
  10438. /* If you call init again, this will be reset to the default. */
  10439. enum
  10440. {
  10441. STBRP_HEURISTIC_Skyline_default=0,
  10442. STBRP_HEURISTIC_Skyline_BL_sortHeight = STBRP_HEURISTIC_Skyline_default,
  10443. STBRP_HEURISTIC_Skyline_BF_sortHeight
  10444. };
  10445. /* //////////////////////////////////////////////////////////////////////////// */
  10446. /* */
  10447. /* the details of the following structures don't matter to you, but they must */
  10448. /* be visible so you can handle the memory allocations for them */
  10449. struct stbrp_node
  10450. {
  10451. stbrp_coord x,y;
  10452. stbrp_node *next;
  10453. };
  10454. struct stbrp_context
  10455. {
  10456. int width;
  10457. int height;
  10458. int align;
  10459. int init_mode;
  10460. int heuristic;
  10461. int num_nodes;
  10462. stbrp_node *active_head;
  10463. stbrp_node *free_head;
  10464. stbrp_node extra[2]; /* we allocate two extra nodes so optimal user-node-count is 'width' not 'width+2' */
  10465. };
  10466. #ifdef __cplusplus
  10467. }
  10468. #endif
  10469. #endif
  10470. /* //////////////////////////////////////////////////////////////////////////// */
  10471. /* */
  10472. /* IMPLEMENTATION SECTION */
  10473. /* */
  10474. #ifdef STB_RECT_PACK_IMPLEMENTATION
  10475. #ifndef STBRP_SORT
  10476. #include <stdlib.h>
  10477. #define STBRP_SORT qsort
  10478. #endif
  10479. #ifndef STBRP_ASSERT
  10480. #include <assert.h>
  10481. #define STBRP_ASSERT assert
  10482. #endif
  10483. #ifdef _MSC_VER
  10484. #define STBRP__NOTUSED(v) (void)(v)
  10485. #define STBRP__CDECL __cdecl
  10486. #else
  10487. #define STBRP__NOTUSED(v) (void)sizeof(v)
  10488. #define STBRP__CDECL
  10489. #endif
  10490. enum
  10491. {
  10492. STBRP__INIT_skyline = 1
  10493. };
  10494. STBRP_DEF void stbrp_setup_heuristic(stbrp_context *context, int heuristic)
  10495. {
  10496. switch (context->init_mode) {
  10497. case STBRP__INIT_skyline:
  10498. STBRP_ASSERT(heuristic == STBRP_HEURISTIC_Skyline_BL_sortHeight || heuristic == STBRP_HEURISTIC_Skyline_BF_sortHeight);
  10499. context->heuristic = heuristic;
  10500. break;
  10501. default:
  10502. STBRP_ASSERT(0);
  10503. }
  10504. }
  10505. STBRP_DEF void stbrp_setup_allow_out_of_mem(stbrp_context *context, int allow_out_of_mem)
  10506. {
  10507. if (allow_out_of_mem)
  10508. /* if it's ok to run out of memory, then don't bother aligning them; */
  10509. /* this gives better packing, but may fail due to OOM (even though */
  10510. /* the rectangles easily fit). @TODO a smarter approach would be to only */
  10511. /* quantize once we've hit OOM, then we could get rid of this parameter. */
  10512. context->align = 1;
  10513. else {
  10514. /* if it's not ok to run out of memory, then quantize the widths */
  10515. /* so that num_nodes is always enough nodes. */
  10516. /* */
  10517. /* I.e. num_nodes * align >= width */
  10518. /* align >= width / num_nodes */
  10519. /* align = ceil(width/num_nodes) */
  10520. context->align = (context->width + context->num_nodes-1) / context->num_nodes;
  10521. }
  10522. }
  10523. STBRP_DEF void stbrp_init_target(stbrp_context *context, int width, int height, stbrp_node *nodes, int num_nodes)
  10524. {
  10525. int i;
  10526. for (i=0; i < num_nodes-1; ++i)
  10527. nodes[i].next = &nodes[i+1];
  10528. nodes[i].next = NULL;
  10529. context->init_mode = STBRP__INIT_skyline;
  10530. context->heuristic = STBRP_HEURISTIC_Skyline_default;
  10531. context->free_head = &nodes[0];
  10532. context->active_head = &context->extra[0];
  10533. context->width = width;
  10534. context->height = height;
  10535. context->num_nodes = num_nodes;
  10536. stbrp_setup_allow_out_of_mem(context, 0);
  10537. /* node 0 is the full width, node 1 is the sentinel (lets us not store width explicitly) */
  10538. context->extra[0].x = 0;
  10539. context->extra[0].y = 0;
  10540. context->extra[0].next = &context->extra[1];
  10541. context->extra[1].x = (stbrp_coord) width;
  10542. context->extra[1].y = (1<<30);
  10543. context->extra[1].next = NULL;
  10544. }
  10545. /* find minimum y position if it starts at x1 */
  10546. static int stbrp__skyline_find_min_y(stbrp_context *c, stbrp_node *first, int x0, int width, int *pwaste)
  10547. {
  10548. stbrp_node *node = first;
  10549. int x1 = x0 + width;
  10550. int min_y, visited_width, waste_area;
  10551. STBRP__NOTUSED(c);
  10552. STBRP_ASSERT(first->x <= x0);
  10553. #if 0
  10554. /* skip in case we're past the node */
  10555. while (node->next->x <= x0)
  10556. ++node;
  10557. #else
  10558. STBRP_ASSERT(node->next->x > x0); /* we ended up handling this in the caller for efficiency */
  10559. #endif
  10560. STBRP_ASSERT(node->x <= x0);
  10561. min_y = 0;
  10562. waste_area = 0;
  10563. visited_width = 0;
  10564. while (node->x < x1) {
  10565. if (node->y > min_y) {
  10566. /* raise min_y higher. */
  10567. /* we've accounted for all waste up to min_y, */
  10568. /* but we'll now add more waste for everything we've visited */
  10569. waste_area += visited_width * (node->y - min_y);
  10570. min_y = node->y;
  10571. /* the first time through, visited_width might be reduced */
  10572. if (node->x < x0)
  10573. visited_width += node->next->x - x0;
  10574. else
  10575. visited_width += node->next->x - node->x;
  10576. } else {
  10577. /* add waste area */
  10578. int under_width = node->next->x - node->x;
  10579. if (under_width + visited_width > width)
  10580. under_width = width - visited_width;
  10581. waste_area += under_width * (min_y - node->y);
  10582. visited_width += under_width;
  10583. }
  10584. node = node->next;
  10585. }
  10586. *pwaste = waste_area;
  10587. return min_y;
  10588. }
  10589. typedef struct
  10590. {
  10591. int x,y;
  10592. stbrp_node **prev_link;
  10593. } stbrp__findresult;
  10594. static stbrp__findresult stbrp__skyline_find_best_pos(stbrp_context *c, int width, int height)
  10595. {
  10596. int best_waste = (1<<30), best_x, best_y = (1 << 30);
  10597. stbrp__findresult fr;
  10598. stbrp_node **prev, *node, *tail, **best = NULL;
  10599. /* align to multiple of c->align */
  10600. width = (width + c->align - 1);
  10601. width -= width % c->align;
  10602. STBRP_ASSERT(width % c->align == 0);
  10603. /* if it can't possibly fit, bail immediately */
  10604. if (width > c->width || height > c->height) {
  10605. fr.prev_link = NULL;
  10606. fr.x = fr.y = 0;
  10607. return fr;
  10608. }
  10609. node = c->active_head;
  10610. prev = &c->active_head;
  10611. while (node->x + width <= c->width) {
  10612. int y,waste;
  10613. y = stbrp__skyline_find_min_y(c, node, node->x, width, &waste);
  10614. if (c->heuristic == STBRP_HEURISTIC_Skyline_BL_sortHeight) { /* actually just want to test BL */
  10615. /* bottom left */
  10616. if (y < best_y) {
  10617. best_y = y;
  10618. best = prev;
  10619. }
  10620. } else {
  10621. /* best-fit */
  10622. if (y + height <= c->height) {
  10623. /* can only use it if it first vertically */
  10624. if (y < best_y || (y == best_y && waste < best_waste)) {
  10625. best_y = y;
  10626. best_waste = waste;
  10627. best = prev;
  10628. }
  10629. }
  10630. }
  10631. prev = &node->next;
  10632. node = node->next;
  10633. }
  10634. best_x = (best == NULL) ? 0 : (*best)->x;
  10635. /* if doing best-fit (BF), we also have to try aligning right edge to each node position */
  10636. /* */
  10637. /* e.g, if fitting */
  10638. /* */
  10639. /* ____________________ */
  10640. /* |____________________| */
  10641. /* */
  10642. /* into */
  10643. /* */
  10644. /* | | */
  10645. /* | ____________| */
  10646. /* |____________| */
  10647. /* */
  10648. /* then right-aligned reduces waste, but bottom-left BL is always chooses left-aligned */
  10649. /* */
  10650. /* This makes BF take about 2x the time */
  10651. if (c->heuristic == STBRP_HEURISTIC_Skyline_BF_sortHeight) {
  10652. tail = c->active_head;
  10653. node = c->active_head;
  10654. prev = &c->active_head;
  10655. /* find first node that's admissible */
  10656. while (tail->x < width)
  10657. tail = tail->next;
  10658. while (tail) {
  10659. int xpos = tail->x - width;
  10660. int y,waste;
  10661. STBRP_ASSERT(xpos >= 0);
  10662. /* find the left position that matches this */
  10663. while (node->next->x <= xpos) {
  10664. prev = &node->next;
  10665. node = node->next;
  10666. }
  10667. STBRP_ASSERT(node->next->x > xpos && node->x <= xpos);
  10668. y = stbrp__skyline_find_min_y(c, node, xpos, width, &waste);
  10669. if (y + height <= c->height) {
  10670. if (y <= best_y) {
  10671. if (y < best_y || waste < best_waste || (waste==best_waste && xpos < best_x)) {
  10672. best_x = xpos;
  10673. STBRP_ASSERT(y <= best_y);
  10674. best_y = y;
  10675. best_waste = waste;
  10676. best = prev;
  10677. }
  10678. }
  10679. }
  10680. tail = tail->next;
  10681. }
  10682. }
  10683. fr.prev_link = best;
  10684. fr.x = best_x;
  10685. fr.y = best_y;
  10686. return fr;
  10687. }
  10688. static stbrp__findresult stbrp__skyline_pack_rectangle(stbrp_context *context, int width, int height)
  10689. {
  10690. /* find best position according to heuristic */
  10691. stbrp__findresult res = stbrp__skyline_find_best_pos(context, width, height);
  10692. stbrp_node *node, *cur;
  10693. /* bail if: */
  10694. /* 1. it failed */
  10695. /* 2. the best node doesn't fit (we don't always check this) */
  10696. /* 3. we're out of memory */
  10697. if (res.prev_link == NULL || res.y + height > context->height || context->free_head == NULL) {
  10698. res.prev_link = NULL;
  10699. return res;
  10700. }
  10701. /* on success, create new node */
  10702. node = context->free_head;
  10703. node->x = (stbrp_coord) res.x;
  10704. node->y = (stbrp_coord) (res.y + height);
  10705. context->free_head = node->next;
  10706. /* insert the new node into the right starting point, and */
  10707. /* let 'cur' point to the remaining nodes needing to be */
  10708. /* stitched back in */
  10709. cur = *res.prev_link;
  10710. if (cur->x < res.x) {
  10711. /* preserve the existing one, so start testing with the next one */
  10712. stbrp_node *next = cur->next;
  10713. cur->next = node;
  10714. cur = next;
  10715. } else {
  10716. *res.prev_link = node;
  10717. }
  10718. /* from here, traverse cur and free the nodes, until we get to one */
  10719. /* that shouldn't be freed */
  10720. while (cur->next && cur->next->x <= res.x + width) {
  10721. stbrp_node *next = cur->next;
  10722. /* move the current node to the free list */
  10723. cur->next = context->free_head;
  10724. context->free_head = cur;
  10725. cur = next;
  10726. }
  10727. /* stitch the list back in */
  10728. node->next = cur;
  10729. if (cur->x < res.x + width)
  10730. cur->x = (stbrp_coord) (res.x + width);
  10731. #ifdef _DEBUG
  10732. cur = context->active_head;
  10733. while (cur->x < context->width) {
  10734. STBRP_ASSERT(cur->x < cur->next->x);
  10735. cur = cur->next;
  10736. }
  10737. STBRP_ASSERT(cur->next == NULL);
  10738. {
  10739. int count=0;
  10740. cur = context->active_head;
  10741. while (cur) {
  10742. cur = cur->next;
  10743. ++count;
  10744. }
  10745. cur = context->free_head;
  10746. while (cur) {
  10747. cur = cur->next;
  10748. ++count;
  10749. }
  10750. STBRP_ASSERT(count == context->num_nodes+2);
  10751. }
  10752. #endif
  10753. return res;
  10754. }
  10755. static int STBRP__CDECL rect_height_compare(const void *a, const void *b)
  10756. {
  10757. const stbrp_rect *p = (const stbrp_rect *) a;
  10758. const stbrp_rect *q = (const stbrp_rect *) b;
  10759. if (p->h > q->h)
  10760. return -1;
  10761. if (p->h < q->h)
  10762. return 1;
  10763. return (p->w > q->w) ? -1 : (p->w < q->w);
  10764. }
  10765. static int STBRP__CDECL rect_original_order(const void *a, const void *b)
  10766. {
  10767. const stbrp_rect *p = (const stbrp_rect *) a;
  10768. const stbrp_rect *q = (const stbrp_rect *) b;
  10769. return (p->was_packed < q->was_packed) ? -1 : (p->was_packed > q->was_packed);
  10770. }
  10771. STBRP_DEF int stbrp_pack_rects(stbrp_context *context, stbrp_rect *rects, int num_rects)
  10772. {
  10773. int i, all_rects_packed = 1;
  10774. /* we use the 'was_packed' field internally to allow sorting/unsorting */
  10775. for (i=0; i < num_rects; ++i) {
  10776. rects[i].was_packed = i;
  10777. }
  10778. /* sort according to heuristic */
  10779. STBRP_SORT(rects, num_rects, sizeof(rects[0]), rect_height_compare);
  10780. for (i=0; i < num_rects; ++i) {
  10781. if (rects[i].w == 0 || rects[i].h == 0) {
  10782. rects[i].x = rects[i].y = 0; /* empty rect needs no space */
  10783. } else {
  10784. stbrp__findresult fr = stbrp__skyline_pack_rectangle(context, rects[i].w, rects[i].h);
  10785. if (fr.prev_link) {
  10786. rects[i].x = (stbrp_coord) fr.x;
  10787. rects[i].y = (stbrp_coord) fr.y;
  10788. } else {
  10789. rects[i].x = rects[i].y = STBRP__MAXVAL;
  10790. }
  10791. }
  10792. }
  10793. /* unsort */
  10794. STBRP_SORT(rects, num_rects, sizeof(rects[0]), rect_original_order);
  10795. /* set was_packed flags and all_rects_packed status */
  10796. for (i=0; i < num_rects; ++i) {
  10797. rects[i].was_packed = !(rects[i].x == STBRP__MAXVAL && rects[i].y == STBRP__MAXVAL);
  10798. if (!rects[i].was_packed)
  10799. all_rects_packed = 0;
  10800. }
  10801. /* return the all_rects_packed status */
  10802. return all_rects_packed;
  10803. }
  10804. #endif
  10805. /*
  10806. ------------------------------------------------------------------------------
  10807. This software is available under 2 licenses -- choose whichever you prefer.
  10808. ------------------------------------------------------------------------------
  10809. ALTERNATIVE A - MIT License
  10810. Copyright (c) 2017 Sean Barrett
  10811. Permission is hereby granted, free of charge, to any person obtaining a copy of
  10812. this software and associated documentation files (the "Software"), to deal in
  10813. the Software without restriction, including without limitation the rights to
  10814. use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
  10815. of the Software, and to permit persons to whom the Software is furnished to do
  10816. so, subject to the following conditions:
  10817. The above copyright notice and this permission notice shall be included in all
  10818. copies or substantial portions of the Software.
  10819. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  10820. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  10821. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  10822. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  10823. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  10824. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  10825. SOFTWARE.
  10826. ------------------------------------------------------------------------------
  10827. ALTERNATIVE B - Public Domain (www.unlicense.org)
  10828. This is free and unencumbered software released into the public domain.
  10829. Anyone is free to copy, modify, publish, use, compile, sell, or distribute this
  10830. software, either in source code form or as a compiled binary, for any purpose,
  10831. commercial or non-commercial, and by any means.
  10832. In jurisdictions that recognize copyright laws, the author or authors of this
  10833. software dedicate any and all copyright interest in the software to the public
  10834. domain. We make this dedication for the benefit of the public at large and to
  10835. the detriment of our heirs and successors. We intend this dedication to be an
  10836. overt act of relinquishment in perpetuity of all present and future rights to
  10837. this software under copyright law.
  10838. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  10839. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  10840. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  10841. AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  10842. ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  10843. WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  10844. ------------------------------------------------------------------------------
  10845. */
  10846. /* stb_truetype.h - v1.26 - public domain */
  10847. /* authored from 2009-2021 by Sean Barrett / RAD Game Tools */
  10848. /* */
  10849. /* ======================================================================= */
  10850. /* */
  10851. /* NO SECURITY GUARANTEE -- DO NOT USE THIS ON UNTRUSTED FONT FILES */
  10852. /* */
  10853. /* This library does no range checking of the offsets found in the file, */
  10854. /* meaning an attacker can use it to read arbitrary memory. */
  10855. /* */
  10856. /* ======================================================================= */
  10857. /* */
  10858. /* This library processes TrueType files: */
  10859. /* parse files */
  10860. /* extract glyph metrics */
  10861. /* extract glyph shapes */
  10862. /* render glyphs to one-channel bitmaps with antialiasing (box filter) */
  10863. /* render glyphs to one-channel SDF bitmaps (signed-distance field/function) */
  10864. /* */
  10865. /* Todo: */
  10866. /* non-MS cmaps */
  10867. /* crashproof on bad data */
  10868. /* hinting? (no longer patented) */
  10869. /* cleartype-style AA? */
  10870. /* optimize: use simple memory allocator for intermediates */
  10871. /* optimize: build edge-list directly from curves */
  10872. /* optimize: rasterize directly from curves? */
  10873. /* */
  10874. /* ADDITIONAL CONTRIBUTORS */
  10875. /* */
  10876. /* Mikko Mononen: compound shape support, more cmap formats */
  10877. /* Tor Andersson: kerning, subpixel rendering */
  10878. /* Dougall Johnson: OpenType / Type 2 font handling */
  10879. /* Daniel Ribeiro Maciel: basic GPOS-based kerning */
  10880. /* */
  10881. /* Misc other: */
  10882. /* Ryan Gordon */
  10883. /* Simon Glass */
  10884. /* github:IntellectualKitty */
  10885. /* Imanol Celaya */
  10886. /* Daniel Ribeiro Maciel */
  10887. /* */
  10888. /* Bug/warning reports/fixes: */
  10889. /* "Zer" on mollyrocket Fabian "ryg" Giesen github:NiLuJe */
  10890. /* Cass Everitt Martins Mozeiko github:aloucks */
  10891. /* stoiko (Haemimont Games) Cap Petschulat github:oyvindjam */
  10892. /* Brian Hook Omar Cornut github:vassvik */
  10893. /* Walter van Niftrik Ryan Griege */
  10894. /* David Gow Peter LaValle */
  10895. /* David Given Sergey Popov */
  10896. /* Ivan-Assen Ivanov Giumo X. Clanjor */
  10897. /* Anthony Pesch Higor Euripedes */
  10898. /* Johan Duparc Thomas Fields */
  10899. /* Hou Qiming Derek Vinyard */
  10900. /* Rob Loach Cort Stratton */
  10901. /* Kenney Phillis Jr. Brian Costabile */
  10902. /* Ken Voskuil (kaesve) */
  10903. /* */
  10904. /* VERSION HISTORY */
  10905. /* */
  10906. /* 1.26 (2021-08-28) fix broken rasterizer */
  10907. /* 1.25 (2021-07-11) many fixes */
  10908. /* 1.24 (2020-02-05) fix warning */
  10909. /* 1.23 (2020-02-02) query SVG data for glyphs; query whole kerning table (but only kern not GPOS) */
  10910. /* 1.22 (2019-08-11) minimize missing-glyph duplication; fix kerning if both 'GPOS' and 'kern' are defined */
  10911. /* 1.21 (2019-02-25) fix warning */
  10912. /* 1.20 (2019-02-07) PackFontRange skips missing codepoints; GetScaleFontVMetrics() */
  10913. /* 1.19 (2018-02-11) GPOS kerning, STBTT_fmod */
  10914. /* 1.18 (2018-01-29) add missing function */
  10915. /* 1.17 (2017-07-23) make more arguments const; doc fix */
  10916. /* 1.16 (2017-07-12) SDF support */
  10917. /* 1.15 (2017-03-03) make more arguments const */
  10918. /* 1.14 (2017-01-16) num-fonts-in-TTC function */
  10919. /* 1.13 (2017-01-02) support OpenType fonts, certain Apple fonts */
  10920. /* 1.12 (2016-10-25) suppress warnings about casting away const with -Wcast-qual */
  10921. /* 1.11 (2016-04-02) fix unused-variable warning */
  10922. /* 1.10 (2016-04-02) user-defined fabs(); rare memory leak; remove duplicate typedef */
  10923. /* 1.09 (2016-01-16) warning fix; avoid crash on outofmem; use allocation userdata properly */
  10924. /* 1.08 (2015-09-13) document stbtt_Rasterize(); fixes for vertical & horizontal edges */
  10925. /* 1.07 (2015-08-01) allow PackFontRanges to accept arrays of sparse codepoints; */
  10926. /* variant PackFontRanges to pack and render in separate phases; */
  10927. /* fix stbtt_GetFontOFfsetForIndex (never worked for non-0 input?); */
  10928. /* fixed an assert() bug in the new rasterizer */
  10929. /* replace assert() with STBTT_assert() in new rasterizer */
  10930. /* */
  10931. /* Full history can be found at the end of this file. */
  10932. /* */
  10933. /* LICENSE */
  10934. /* */
  10935. /* See end of file for license information. */
  10936. /* */
  10937. /* USAGE */
  10938. /* */
  10939. /* Include this file in whatever places need to refer to it. In ONE C/C++ */
  10940. /* file, write: */
  10941. /* #define STB_TRUETYPE_IMPLEMENTATION */
  10942. /* before the #include of this file. This expands out the actual */
  10943. /* implementation into that C/C++ file. */
  10944. /* */
  10945. /* To make the implementation private to the file that generates the implementation, */
  10946. /* #define STBTT_STATIC */
  10947. /* */
  10948. /* Simple 3D API (don't ship this, but it's fine for tools and quick start) */
  10949. /* stbtt_BakeFontBitmap() -- bake a font to a bitmap for use as texture */
  10950. /* stbtt_GetBakedQuad() -- compute quad to draw for a given char */
  10951. /* */
  10952. /* Improved 3D API (more shippable): */
  10953. /* #include "stb_rect_pack.h" -- optional, but you really want it */
  10954. /* stbtt_PackBegin() */
  10955. /* stbtt_PackSetOversampling() -- for improved quality on small fonts */
  10956. /* stbtt_PackFontRanges() -- pack and renders */
  10957. /* stbtt_PackEnd() */
  10958. /* stbtt_GetPackedQuad() */
  10959. /* */
  10960. /* "Load" a font file from a memory buffer (you have to keep the buffer loaded) */
  10961. /* stbtt_InitFont() */
  10962. /* stbtt_GetFontOffsetForIndex() -- indexing for TTC font collections */
  10963. /* stbtt_GetNumberOfFonts() -- number of fonts for TTC font collections */
  10964. /* */
  10965. /* Render a unicode codepoint to a bitmap */
  10966. /* stbtt_GetCodepointBitmap() -- allocates and returns a bitmap */
  10967. /* stbtt_MakeCodepointBitmap() -- renders into bitmap you provide */
  10968. /* stbtt_GetCodepointBitmapBox() -- how big the bitmap must be */
  10969. /* */
  10970. /* Character advance/positioning */
  10971. /* stbtt_GetCodepointHMetrics() */
  10972. /* stbtt_GetFontVMetrics() */
  10973. /* stbtt_GetFontVMetricsOS2() */
  10974. /* stbtt_GetCodepointKernAdvance() */
  10975. /* */
  10976. /* Starting with version 1.06, the rasterizer was replaced with a new, */
  10977. /* faster and generally-more-precise rasterizer. The new rasterizer more */
  10978. /* accurately measures pixel coverage for anti-aliasing, except in the case */
  10979. /* where multiple shapes overlap, in which case it overestimates the AA pixel */
  10980. /* coverage. Thus, anti-aliasing of intersecting shapes may look wrong. If */
  10981. /* this turns out to be a problem, you can re-enable the old rasterizer with */
  10982. /* #define STBTT_RASTERIZER_VERSION 1 */
  10983. /* which will incur about a 15% speed hit. */
  10984. /* */
  10985. /* ADDITIONAL DOCUMENTATION */
  10986. /* */
  10987. /* Immediately after this block comment are a series of sample programs. */
  10988. /* */
  10989. /* After the sample programs is the "header file" section. This section */
  10990. /* includes documentation for each API function. */
  10991. /* */
  10992. /* Some important concepts to understand to use this library: */
  10993. /* */
  10994. /* Codepoint */
  10995. /* Characters are defined by unicode codepoints, e.g. 65 is */
  10996. /* uppercase A, 231 is lowercase c with a cedilla, 0x7e30 is */
  10997. /* the hiragana for "ma". */
  10998. /* */
  10999. /* Glyph */
  11000. /* A visual character shape (every codepoint is rendered as */
  11001. /* some glyph) */
  11002. /* */
  11003. /* Glyph index */
  11004. /* A font-specific integer ID representing a glyph */
  11005. /* */
  11006. /* Baseline */
  11007. /* Glyph shapes are defined relative to a baseline, which is the */
  11008. /* bottom of uppercase characters. Characters extend both above */
  11009. /* and below the baseline. */
  11010. /* */
  11011. /* Current Point */
  11012. /* As you draw text to the screen, you keep track of a "current point" */
  11013. /* which is the origin of each character. The current point's vertical */
  11014. /* position is the baseline. Even "baked fonts" use this model. */
  11015. /* */
  11016. /* Vertical Font Metrics */
  11017. /* The vertical qualities of the font, used to vertically position */
  11018. /* and space the characters. See docs for stbtt_GetFontVMetrics. */
  11019. /* */
  11020. /* Font Size in Pixels or Points */
  11021. /* The preferred interface for specifying font sizes in stb_truetype */
  11022. /* is to specify how tall the font's vertical extent should be in pixels. */
  11023. /* If that sounds good enough, skip the next paragraph. */
  11024. /* */
  11025. /* Most font APIs instead use "points", which are a common typographic */
  11026. /* measurement for describing font size, defined as 72 points per inch. */
  11027. /* stb_truetype provides a point API for compatibility. However, true */
  11028. /* "per inch" conventions don't make much sense on computer displays */
  11029. /* since different monitors have different number of pixels per */
  11030. /* inch. For example, Windows traditionally uses a convention that */
  11031. /* there are 96 pixels per inch, thus making 'inch' measurements have */
  11032. /* nothing to do with inches, and thus effectively defining a point to */
  11033. /* be 1.333 pixels. Additionally, the TrueType font data provides */
  11034. /* an explicit scale factor to scale a given font's glyphs to points, */
  11035. /* but the author has observed that this scale factor is often wrong */
  11036. /* for non-commercial fonts, thus making fonts scaled in points */
  11037. /* according to the TrueType spec incoherently sized in practice. */
  11038. /* */
  11039. /* DETAILED USAGE: */
  11040. /* */
  11041. /* Scale: */
  11042. /* Select how high you want the font to be, in points or pixels. */
  11043. /* Call ScaleForPixelHeight or ScaleForMappingEmToPixels to compute */
  11044. /* a scale factor SF that will be used by all other functions. */
  11045. /* */
  11046. /* Baseline: */
  11047. /* You need to select a y-coordinate that is the baseline of where */
  11048. /* your text will appear. Call GetFontBoundingBox to get the baseline-relative */
  11049. /* bounding box for all characters. SF*-y0 will be the distance in pixels */
  11050. /* that the worst-case character could extend above the baseline, so if */
  11051. /* you want the top edge of characters to appear at the top of the */
  11052. /* screen where y=0, then you would set the baseline to SF*-y0. */
  11053. /* */
  11054. /* Current point: */
  11055. /* Set the current point where the first character will appear. The */
  11056. /* first character could extend left of the current point; this is font */
  11057. /* dependent. You can either choose a current point that is the leftmost */
  11058. /* point and hope, or add some padding, or check the bounding box or */
  11059. /* left-side-bearing of the first character to be displayed and set */
  11060. /* the current point based on that. */
  11061. /* */
  11062. /* Displaying a character: */
  11063. /* Compute the bounding box of the character. It will contain signed values */
  11064. /* relative to <current_point, baseline>. I.e. if it returns x0,y0,x1,y1, */
  11065. /* then the character should be displayed in the rectangle from */
  11066. /* <current_point+SF*x0, baseline+SF*y0> to <current_point+SF*x1,baseline+SF*y1). */
  11067. /* */
  11068. /* Advancing for the next character: */
  11069. /* Call GlyphHMetrics, and compute 'current_point += SF * advance'. */
  11070. /* */
  11071. /* */
  11072. /* ADVANCED USAGE */
  11073. /* */
  11074. /* Quality: */
  11075. /* */
  11076. /* - Use the functions with Subpixel at the end to allow your characters */
  11077. /* to have subpixel positioning. Since the font is anti-aliased, not */
  11078. /* hinted, this is very import for quality. (This is not possible with */
  11079. /* baked fonts.) */
  11080. /* */
  11081. /* - Kerning is now supported, and if you're supporting subpixel rendering */
  11082. /* then kerning is worth using to give your text a polished look. */
  11083. /* */
  11084. /* Performance: */
  11085. /* */
  11086. /* - Convert Unicode codepoints to glyph indexes and operate on the glyphs; */
  11087. /* if you don't do this, stb_truetype is forced to do the conversion on */
  11088. /* every call. */
  11089. /* */
  11090. /* - There are a lot of memory allocations. We should modify it to take */
  11091. /* a temp buffer and allocate from the temp buffer (without freeing), */
  11092. /* should help performance a lot. */
  11093. /* */
  11094. /* NOTES */
  11095. /* */
  11096. /* The system uses the raw data found in the .ttf file without changing it */
  11097. /* and without building auxiliary data structures. This is a bit inefficient */
  11098. /* on little-endian systems (the data is big-endian), but assuming you're */
  11099. /* caching the bitmaps or glyph shapes this shouldn't be a big deal. */
  11100. /* */
  11101. /* It appears to be very hard to programmatically determine what font a */
  11102. /* given file is in a general way. I provide an API for this, but I don't */
  11103. /* recommend it. */
  11104. /* */
  11105. /* */
  11106. /* PERFORMANCE MEASUREMENTS FOR 1.06: */
  11107. /* */
  11108. /* 32-bit 64-bit */
  11109. /* Previous release: 8.83 s 7.68 s */
  11110. /* Pool allocations: 7.72 s 6.34 s */
  11111. /* Inline sort : 6.54 s 5.65 s */
  11112. /* New rasterizer : 5.63 s 5.00 s */
  11113. /* //////////////////////////////////////////////////////////////////////////// */
  11114. /* //////////////////////////////////////////////////////////////////////////// */
  11115. /* // */
  11116. /* // SAMPLE PROGRAMS */
  11117. /* // */
  11118. /* */
  11119. /* Incomplete text-in-3d-api example, which draws quads properly aligned to be lossless. */
  11120. /* See "tests/truetype_demo_win32.c" for a complete version. */
  11121. #if 0
  11122. #define STB_TRUETYPE_IMPLEMENTATION /* force following include to generate implementation */
  11123. #include "stb_truetype.h"
  11124. unsigned char ttf_buffer[1<<20];
  11125. unsigned char temp_bitmap[512*512];
  11126. stbtt_bakedchar cdata[96]; /* ASCII 32..126 is 95 glyphs */
  11127. GLuint ftex;
  11128. void my_stbtt_initfont(void)
  11129. {
  11130. fread(ttf_buffer, 1, 1<<20, fopen("c:/windows/fonts/times.ttf", "rb"));
  11131. stbtt_BakeFontBitmap(ttf_buffer,0, 32.0, temp_bitmap,512,512, 32,96, cdata); /* no guarantee this fits! */
  11132. /* can free ttf_buffer at this point */
  11133. glGenTextures(1, &ftex);
  11134. glBindTexture(GL_TEXTURE_2D, ftex);
  11135. glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, 512,512, 0, GL_ALPHA, GL_UNSIGNED_BYTE, temp_bitmap);
  11136. /* can free temp_bitmap at this point */
  11137. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  11138. }
  11139. void my_stbtt_print(float x, float y, char *text)
  11140. {
  11141. /* assume orthographic projection with units = screen pixels, origin at top left */
  11142. glEnable(GL_BLEND);
  11143. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  11144. glEnable(GL_TEXTURE_2D);
  11145. glBindTexture(GL_TEXTURE_2D, ftex);
  11146. glBegin(GL_QUADS);
  11147. while (*text) {
  11148. if (*text >= 32 && *text < 128) {
  11149. stbtt_aligned_quad q;
  11150. stbtt_GetBakedQuad(cdata, 512,512, *text-32, &x,&y,&q,1);/* 1=opengl & d3d10+,0=d3d9 */
  11151. glTexCoord2f(q.s0,q.t0); glVertex2f(q.x0,q.y0);
  11152. glTexCoord2f(q.s1,q.t0); glVertex2f(q.x1,q.y0);
  11153. glTexCoord2f(q.s1,q.t1); glVertex2f(q.x1,q.y1);
  11154. glTexCoord2f(q.s0,q.t1); glVertex2f(q.x0,q.y1);
  11155. }
  11156. ++text;
  11157. }
  11158. glEnd();
  11159. }
  11160. #endif
  11161. /* */
  11162. /* */
  11163. /* //////////////////////////////////////////////////////////////////////////// */
  11164. /* */
  11165. /* Complete program (this compiles): get a single bitmap, print as ASCII art */
  11166. /* */
  11167. #if 0
  11168. #include <stdio.h>
  11169. #define STB_TRUETYPE_IMPLEMENTATION /* force following include to generate implementation */
  11170. #include "stb_truetype.h"
  11171. char ttf_buffer[1<<25];
  11172. int main(int argc, char **argv)
  11173. {
  11174. stbtt_fontinfo font;
  11175. unsigned char *bitmap;
  11176. int w,h,i,j,c = (argc > 1 ? atoi(argv[1]) : 'a'), s = (argc > 2 ? atoi(argv[2]) : 20);
  11177. fread(ttf_buffer, 1, 1<<25, fopen(argc > 3 ? argv[3] : "c:/windows/fonts/arialbd.ttf", "rb"));
  11178. stbtt_InitFont(&font, ttf_buffer, stbtt_GetFontOffsetForIndex(ttf_buffer,0));
  11179. bitmap = stbtt_GetCodepointBitmap(&font, 0,stbtt_ScaleForPixelHeight(&font, s), c, &w, &h, 0,0);
  11180. for (j=0; j < h; ++j) {
  11181. for (i=0; i < w; ++i)
  11182. putchar(" .:ioVM@"[bitmap[j*w+i]>>5]);
  11183. putchar('\n');
  11184. }
  11185. return 0;
  11186. }
  11187. #endif
  11188. /* */
  11189. /* Output: */
  11190. /* */
  11191. /* .ii. */
  11192. /* @@@@@@. */
  11193. /* V@Mio@@o */
  11194. /* :i. V@V */
  11195. /* :oM@@M */
  11196. /* :@@@MM@M */
  11197. /* @@o o@M */
  11198. /* :@@. M@M */
  11199. /* @@@o@@@@ */
  11200. /* :M@@V:@@. */
  11201. /* */
  11202. /* //////////////////////////////////////////////////////////////////////////// */
  11203. /* */
  11204. /* Complete program: print "Hello World!" banner, with bugs */
  11205. /* */
  11206. #if 0
  11207. char buffer[24<<20];
  11208. unsigned char screen[20][79];
  11209. int main(int arg, char **argv)
  11210. {
  11211. stbtt_fontinfo font;
  11212. int i,j,ascent,baseline,ch=0;
  11213. float scale, xpos=2; /* leave a little padding in case the character extends left */
  11214. char *text = "Heljo World!"; /* intentionally misspelled to show 'lj' brokenness */
  11215. fread(buffer, 1, 1000000, fopen("c:/windows/fonts/arialbd.ttf", "rb"));
  11216. stbtt_InitFont(&font, buffer, 0);
  11217. scale = stbtt_ScaleForPixelHeight(&font, 15);
  11218. stbtt_GetFontVMetrics(&font, &ascent,0,0);
  11219. baseline = (int) (ascent*scale);
  11220. while (text[ch]) {
  11221. int advance,lsb,x0,y0,x1,y1;
  11222. float x_shift = xpos - (float) floor(xpos);
  11223. stbtt_GetCodepointHMetrics(&font, text[ch], &advance, &lsb);
  11224. stbtt_GetCodepointBitmapBoxSubpixel(&font, text[ch], scale,scale,x_shift,0, &x0,&y0,&x1,&y1);
  11225. stbtt_MakeCodepointBitmapSubpixel(&font, &screen[baseline + y0][(int) xpos + x0], x1-x0,y1-y0, 79, scale,scale,x_shift,0, text[ch]);
  11226. /* note that this stomps the old data, so where character boxes overlap (e.g. 'lj') it's wrong */
  11227. /* because this API is really for baking character bitmaps into textures. if you want to render */
  11228. /* a sequence of characters, you really need to render each bitmap to a temp buffer, then */
  11229. /* "alpha blend" that into the working buffer */
  11230. xpos += (advance * scale);
  11231. if (text[ch+1])
  11232. xpos += scale*stbtt_GetCodepointKernAdvance(&font, text[ch],text[ch+1]);
  11233. ++ch;
  11234. }
  11235. for (j=0; j < 20; ++j) {
  11236. for (i=0; i < 78; ++i)
  11237. putchar(" .:ioVM@"[screen[j][i]>>5]);
  11238. putchar('\n');
  11239. }
  11240. return 0;
  11241. }
  11242. #endif
  11243. /* //////////////////////////////////////////////////////////////////////////// */
  11244. /* //////////////////////////////////////////////////////////////////////////// */
  11245. /* // */
  11246. /* // INTEGRATION WITH YOUR CODEBASE */
  11247. /* // */
  11248. /* // The following sections allow you to supply alternate definitions */
  11249. /* // of C library functions used by stb_truetype, e.g. if you don't */
  11250. /* // link with the C runtime library. */
  11251. #ifdef STB_TRUETYPE_IMPLEMENTATION
  11252. /* #define your own (u)stbtt_int8/16/32 before including to override this */
  11253. #ifndef stbtt_uint8
  11254. typedef unsigned char stbtt_uint8;
  11255. typedef signed char stbtt_int8;
  11256. typedef unsigned short stbtt_uint16;
  11257. typedef signed short stbtt_int16;
  11258. typedef unsigned int stbtt_uint32;
  11259. typedef signed int stbtt_int32;
  11260. #endif
  11261. typedef char stbtt__check_size32[sizeof(stbtt_int32)==4 ? 1 : -1];
  11262. typedef char stbtt__check_size16[sizeof(stbtt_int16)==2 ? 1 : -1];
  11263. /* e.g. #define your own STBTT_ifloor/STBTT_iceil() to avoid math.h */
  11264. #ifndef STBTT_ifloor
  11265. #include <math.h>
  11266. #define STBTT_ifloor(x) ((int) floor(x))
  11267. #define STBTT_iceil(x) ((int) ceil(x))
  11268. #endif
  11269. #ifndef STBTT_sqrt
  11270. #include <math.h>
  11271. #define STBTT_sqrt(x) sqrt(x)
  11272. #define STBTT_pow(x,y) pow(x,y)
  11273. #endif
  11274. #ifndef STBTT_fmod
  11275. #include <math.h>
  11276. #define STBTT_fmod(x,y) fmod(x,y)
  11277. #endif
  11278. #ifndef STBTT_cos
  11279. #include <math.h>
  11280. #define STBTT_cos(x) cos(x)
  11281. #define STBTT_acos(x) acos(x)
  11282. #endif
  11283. #ifndef STBTT_fabs
  11284. #include <math.h>
  11285. #define STBTT_fabs(x) fabs(x)
  11286. #endif
  11287. /* #define your own functions "STBTT_malloc" / "STBTT_free" to avoid malloc.h */
  11288. #ifndef STBTT_malloc
  11289. #include <stdlib.h>
  11290. #define STBTT_malloc(x,u) ((void)(u),malloc(x))
  11291. #define STBTT_free(x,u) ((void)(u),free(x))
  11292. #endif
  11293. #ifndef STBTT_assert
  11294. #include <assert.h>
  11295. #define STBTT_assert(x) assert(x)
  11296. #endif
  11297. #ifndef STBTT_strlen
  11298. #include <string.h>
  11299. #define STBTT_strlen(x) strlen(x)
  11300. #endif
  11301. #ifndef STBTT_memcpy
  11302. #include <string.h>
  11303. #define STBTT_memcpy memcpy
  11304. #define STBTT_memset memset
  11305. #endif
  11306. #endif
  11307. /* ///////////////////////////////////////////////////////////////////////////// */
  11308. /* ///////////////////////////////////////////////////////////////////////////// */
  11309. /* // */
  11310. /* // INTERFACE */
  11311. /* // */
  11312. /* // */
  11313. #ifndef __STB_INCLUDE_STB_TRUETYPE_H__
  11314. #define __STB_INCLUDE_STB_TRUETYPE_H__
  11315. #ifdef STBTT_STATIC
  11316. #define STBTT_DEF static
  11317. #else
  11318. #define STBTT_DEF extern
  11319. #endif
  11320. #ifdef __cplusplus
  11321. extern "C" {
  11322. #endif
  11323. /* private structure */
  11324. typedef struct
  11325. {
  11326. unsigned char *data;
  11327. int cursor;
  11328. int size;
  11329. } stbtt__buf;
  11330. /* //////////////////////////////////////////////////////////////////////////// */
  11331. /* */
  11332. /* TEXTURE BAKING API */
  11333. /* */
  11334. /* If you use this API, you only have to call two functions ever. */
  11335. /* */
  11336. typedef struct
  11337. {
  11338. unsigned short x0,y0,x1,y1; /* coordinates of bbox in bitmap */
  11339. float xoff,yoff,xadvance;
  11340. } stbtt_bakedchar;
  11341. STBTT_DEF int stbtt_BakeFontBitmap(const unsigned char *data, int offset, /* font location (use offset=0 for plain .ttf) */
  11342. float pixel_height, /* height of font in pixels */
  11343. unsigned char *pixels, int pw, int ph, /* bitmap to be filled in */
  11344. int first_char, int num_chars, /* characters to bake */
  11345. stbtt_bakedchar *chardata); /* you allocate this, it's num_chars long */
  11346. /* if return is positive, the first unused row of the bitmap */
  11347. /* if return is negative, returns the negative of the number of characters that fit */
  11348. /* if return is 0, no characters fit and no rows were used */
  11349. /* This uses a very crappy packing. */
  11350. typedef struct
  11351. {
  11352. float x0,y0,s0,t0; /* top-left */
  11353. float x1,y1,s1,t1; /* bottom-right */
  11354. } stbtt_aligned_quad;
  11355. STBTT_DEF void stbtt_GetBakedQuad(const stbtt_bakedchar *chardata, int pw, int ph, /* same data as above */
  11356. int char_index, /* character to display */
  11357. float *xpos, float *ypos, /* pointers to current position in screen pixel space */
  11358. stbtt_aligned_quad *q, /* output: quad to draw */
  11359. int opengl_fillrule); /* true if opengl fill rule; false if DX9 or earlier */
  11360. /* Call GetBakedQuad with char_index = 'character - first_char', and it */
  11361. /* creates the quad you need to draw and advances the current position. */
  11362. /* */
  11363. /* The coordinate system used assumes y increases downwards. */
  11364. /* */
  11365. /* Characters will extend both above and below the current position; */
  11366. /* see discussion of "BASELINE" above. */
  11367. /* */
  11368. /* It's inefficient; you might want to c&p it and optimize it. */
  11369. STBTT_DEF void stbtt_GetScaledFontVMetrics(const unsigned char *fontdata, int index, float size, float *ascent, float *descent, float *lineGap);
  11370. /* Query the font vertical metrics without having to create a font first. */
  11371. /* //////////////////////////////////////////////////////////////////////////// */
  11372. /* */
  11373. /* NEW TEXTURE BAKING API */
  11374. /* */
  11375. /* This provides options for packing multiple fonts into one atlas, not */
  11376. /* perfectly but better than nothing. */
  11377. typedef struct
  11378. {
  11379. unsigned short x0,y0,x1,y1; /* coordinates of bbox in bitmap */
  11380. float xoff,yoff,xadvance;
  11381. float xoff2,yoff2;
  11382. } stbtt_packedchar;
  11383. typedef struct stbtt_pack_context stbtt_pack_context;
  11384. typedef struct stbtt_fontinfo stbtt_fontinfo;
  11385. #ifndef STB_RECT_PACK_VERSION
  11386. typedef struct stbrp_rect stbrp_rect;
  11387. #endif
  11388. 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);
  11389. /* Initializes a packing context stored in the passed-in stbtt_pack_context. */
  11390. /* Future calls using this context will pack characters into the bitmap passed */
  11391. /* in here: a 1-channel bitmap that is width * height. stride_in_bytes is */
  11392. /* the distance from one row to the next (or 0 to mean they are packed tightly */
  11393. /* together). "padding" is the amount of padding to leave between each */
  11394. /* character (normally you want '1' for bitmaps you'll use as textures with */
  11395. /* bilinear filtering). */
  11396. /* */
  11397. /* Returns 0 on failure, 1 on success. */
  11398. STBTT_DEF void stbtt_PackEnd (stbtt_pack_context *spc);
  11399. /* Cleans up the packing context and frees all memory. */
  11400. #define STBTT_POINT_SIZE(x) (-(x))
  11401. STBTT_DEF int stbtt_PackFontRange(stbtt_pack_context *spc, const unsigned char *fontdata, int font_index, float font_size,
  11402. int first_unicode_char_in_range, int num_chars_in_range, stbtt_packedchar *chardata_for_range);
  11403. /* Creates character bitmaps from the font_index'th font found in fontdata (use */
  11404. /* font_index=0 if you don't know what that is). It creates num_chars_in_range */
  11405. /* bitmaps for characters with unicode values starting at first_unicode_char_in_range */
  11406. /* and increasing. Data for how to render them is stored in chardata_for_range; */
  11407. /* pass these to stbtt_GetPackedQuad to get back renderable quads. */
  11408. /* */
  11409. /* font_size is the full height of the character from ascender to descender, */
  11410. /* as computed by stbtt_ScaleForPixelHeight. To use a point size as computed */
  11411. /* by stbtt_ScaleForMappingEmToPixels, wrap the point size in STBTT_POINT_SIZE() */
  11412. /* and pass that result as 'font_size': */
  11413. /* ..., 20 , ... // font max minus min y is 20 pixels tall */
  11414. /* ..., STBTT_POINT_SIZE(20), ... // 'M' is 20 pixels tall */
  11415. typedef struct
  11416. {
  11417. float font_size;
  11418. int first_unicode_codepoint_in_range; /* if non-zero, then the chars are continuous, and this is the first codepoint */
  11419. int *array_of_unicode_codepoints; /* if non-zero, then this is an array of unicode codepoints */
  11420. int num_chars;
  11421. stbtt_packedchar *chardata_for_range; /* output */
  11422. unsigned char h_oversample, v_oversample; /* don't set these, they're used internally */
  11423. } stbtt_pack_range;
  11424. STBTT_DEF int stbtt_PackFontRanges(stbtt_pack_context *spc, const unsigned char *fontdata, int font_index, stbtt_pack_range *ranges, int num_ranges);
  11425. /* Creates character bitmaps from multiple ranges of characters stored in */
  11426. /* ranges. This will usually create a better-packed bitmap than multiple */
  11427. /* calls to stbtt_PackFontRange. Note that you can call this multiple */
  11428. /* times within a single PackBegin/PackEnd. */
  11429. STBTT_DEF void stbtt_PackSetOversampling(stbtt_pack_context *spc, unsigned int h_oversample, unsigned int v_oversample);
  11430. /* Oversampling a font increases the quality by allowing higher-quality subpixel */
  11431. /* positioning, and is especially valuable at smaller text sizes. */
  11432. /* */
  11433. /* This function sets the amount of oversampling for all following calls to */
  11434. /* stbtt_PackFontRange(s) or stbtt_PackFontRangesGatherRects for a given */
  11435. /* pack context. The default (no oversampling) is achieved by h_oversample=1 */
  11436. /* and v_oversample=1. The total number of pixels required is */
  11437. /* h_oversample*v_oversample larger than the default; for example, 2x2 */
  11438. /* oversampling requires 4x the storage of 1x1. For best results, render */
  11439. /* oversampled textures with bilinear filtering. Look at the readme in */
  11440. /* stb/tests/oversample for information about oversampled fonts */
  11441. /* */
  11442. /* To use with PackFontRangesGather etc., you must set it before calls */
  11443. /* call to PackFontRangesGatherRects. */
  11444. STBTT_DEF void stbtt_PackSetSkipMissingCodepoints(stbtt_pack_context *spc, int skip);
  11445. /* If skip != 0, this tells stb_truetype to skip any codepoints for which */
  11446. /* there is no corresponding glyph. If skip=0, which is the default, then */
  11447. /* codepoints without a glyph recived the font's "missing character" glyph, */
  11448. /* typically an empty box by convention. */
  11449. STBTT_DEF void stbtt_GetPackedQuad(const stbtt_packedchar *chardata, int pw, int ph, /* same data as above */
  11450. int char_index, /* character to display */
  11451. float *xpos, float *ypos, /* pointers to current position in screen pixel space */
  11452. stbtt_aligned_quad *q, /* output: quad to draw */
  11453. int align_to_integer);
  11454. STBTT_DEF int stbtt_PackFontRangesGatherRects(stbtt_pack_context *spc, const stbtt_fontinfo *info, stbtt_pack_range *ranges, int num_ranges, stbrp_rect *rects);
  11455. STBTT_DEF void stbtt_PackFontRangesPackRects(stbtt_pack_context *spc, stbrp_rect *rects, int num_rects);
  11456. STBTT_DEF int stbtt_PackFontRangesRenderIntoRects(stbtt_pack_context *spc, const stbtt_fontinfo *info, stbtt_pack_range *ranges, int num_ranges, stbrp_rect *rects);
  11457. /* Calling these functions in sequence is roughly equivalent to calling */
  11458. /* stbtt_PackFontRanges(). If you more control over the packing of multiple */
  11459. /* fonts, or if you want to pack custom data into a font texture, take a look */
  11460. /* at the source to of stbtt_PackFontRanges() and create a custom version */
  11461. /* using these functions, e.g. call GatherRects multiple times, */
  11462. /* building up a single array of rects, then call PackRects once, */
  11463. /* then call RenderIntoRects repeatedly. This may result in a */
  11464. /* better packing than calling PackFontRanges multiple times */
  11465. /* (or it may not). */
  11466. /* this is an opaque structure that you shouldn't mess with which holds */
  11467. /* all the context needed from PackBegin to PackEnd. */
  11468. struct stbtt_pack_context {
  11469. void *user_allocator_context;
  11470. void *pack_info;
  11471. int width;
  11472. int height;
  11473. int stride_in_bytes;
  11474. int padding;
  11475. int skip_missing;
  11476. unsigned int h_oversample, v_oversample;
  11477. unsigned char *pixels;
  11478. void *nodes;
  11479. };
  11480. /* //////////////////////////////////////////////////////////////////////////// */
  11481. /* */
  11482. /* FONT LOADING */
  11483. /* */
  11484. /* */
  11485. STBTT_DEF int stbtt_GetNumberOfFonts(const unsigned char *data);
  11486. /* This function will determine the number of fonts in a font file. TrueType */
  11487. /* collection (.ttc) files may contain multiple fonts, while TrueType font */
  11488. /* (.ttf) files only contain one font. The number of fonts can be used for */
  11489. /* indexing with the previous function where the index is between zero and one */
  11490. /* less than the total fonts. If an error occurs, -1 is returned. */
  11491. STBTT_DEF int stbtt_GetFontOffsetForIndex(const unsigned char *data, int index);
  11492. /* Each .ttf/.ttc file may have more than one font. Each font has a sequential */
  11493. /* index number starting from 0. Call this function to get the font offset for */
  11494. /* a given index; it returns -1 if the index is out of range. A regular .ttf */
  11495. /* file will only define one font and it always be at offset 0, so it will */
  11496. /* return '0' for index 0, and -1 for all other indices. */
  11497. /* The following structure is defined publicly so you can declare one on */
  11498. /* the stack or as a global or etc, but you should treat it as opaque. */
  11499. struct stbtt_fontinfo
  11500. {
  11501. void * userdata;
  11502. unsigned char * data; /* pointer to .ttf file */
  11503. int fontstart; /* offset of start of font */
  11504. int numGlyphs; /* number of glyphs, needed for range checking */
  11505. int loca,head,glyf,hhea,hmtx,kern,gpos,svg; /* table locations as offset from start of .ttf */
  11506. int index_map; /* a cmap mapping for our chosen character encoding */
  11507. int indexToLocFormat; /* format needed to map from glyph index to glyph */
  11508. stbtt__buf cff; /* cff font data */
  11509. stbtt__buf charstrings; /* the charstring index */
  11510. stbtt__buf gsubrs; /* global charstring subroutines index */
  11511. stbtt__buf subrs; /* private charstring subroutines index */
  11512. stbtt__buf fontdicts; /* array of font dicts */
  11513. stbtt__buf fdselect; /* map from glyph to fontdict */
  11514. };
  11515. STBTT_DEF int stbtt_InitFont(stbtt_fontinfo *info, const unsigned char *data, int offset);
  11516. /* Given an offset into the file that defines a font, this function builds */
  11517. /* the necessary cached info for the rest of the system. You must allocate */
  11518. /* the stbtt_fontinfo yourself, and stbtt_InitFont will fill it out. You don't */
  11519. /* need to do anything special to free it, because the contents are pure */
  11520. /* value data with no additional data structures. Returns 0 on failure. */
  11521. /* //////////////////////////////////////////////////////////////////////////// */
  11522. /* */
  11523. /* CHARACTER TO GLYPH-INDEX CONVERSIOn */
  11524. STBTT_DEF int stbtt_FindGlyphIndex(const stbtt_fontinfo *info, int unicode_codepoint);
  11525. /* If you're going to perform multiple operations on the same character */
  11526. /* and you want a speed-up, call this function with the character you're */
  11527. /* going to process, then use glyph-based functions instead of the */
  11528. /* codepoint-based functions. */
  11529. /* Returns 0 if the character codepoint is not defined in the font. */
  11530. /* //////////////////////////////////////////////////////////////////////////// */
  11531. /* */
  11532. /* CHARACTER PROPERTIES */
  11533. /* */
  11534. STBTT_DEF float stbtt_ScaleForPixelHeight(const stbtt_fontinfo *info, float pixels);
  11535. /* computes a scale factor to produce a font whose "height" is 'pixels' tall. */
  11536. /* Height is measured as the distance from the highest ascender to the lowest */
  11537. /* descender; in other words, it's equivalent to calling stbtt_GetFontVMetrics */
  11538. /* and computing: */
  11539. /* scale = pixels / (ascent - descent) */
  11540. /* so if you prefer to measure height by the ascent only, use a similar calculation. */
  11541. STBTT_DEF float stbtt_ScaleForMappingEmToPixels(const stbtt_fontinfo *info, float pixels);
  11542. /* computes a scale factor to produce a font whose EM size is mapped to */
  11543. /* 'pixels' tall. This is probably what traditional APIs compute, but */
  11544. /* I'm not positive. */
  11545. STBTT_DEF void stbtt_GetFontVMetrics(const stbtt_fontinfo *info, int *ascent, int *descent, int *lineGap);
  11546. /* ascent is the coordinate above the baseline the font extends; descent */
  11547. /* is the coordinate below the baseline the font extends (i.e. it is typically negative) */
  11548. /* lineGap is the spacing between one row's descent and the next row's ascent... */
  11549. /* so you should advance the vertical position by "*ascent - *descent + *lineGap" */
  11550. /* these are expressed in unscaled coordinates, so you must multiply by */
  11551. /* the scale factor for a given size */
  11552. STBTT_DEF int stbtt_GetFontVMetricsOS2(const stbtt_fontinfo *info, int *typoAscent, int *typoDescent, int *typoLineGap);
  11553. /* analogous to GetFontVMetrics, but returns the "typographic" values from the OS/2 */
  11554. /* table (specific to MS/Windows TTF files). */
  11555. /* */
  11556. /* Returns 1 on success (table present), 0 on failure. */
  11557. STBTT_DEF void stbtt_GetFontBoundingBox(const stbtt_fontinfo *info, int *x0, int *y0, int *x1, int *y1);
  11558. /* the bounding box around all possible characters */
  11559. STBTT_DEF void stbtt_GetCodepointHMetrics(const stbtt_fontinfo *info, int codepoint, int *advanceWidth, int *leftSideBearing);
  11560. /* leftSideBearing is the offset from the current horizontal position to the left edge of the character */
  11561. /* advanceWidth is the offset from the current horizontal position to the next horizontal position */
  11562. /* these are expressed in unscaled coordinates */
  11563. STBTT_DEF int stbtt_GetCodepointKernAdvance(const stbtt_fontinfo *info, int ch1, int ch2);
  11564. /* an additional amount to add to the 'advance' value between ch1 and ch2 */
  11565. STBTT_DEF int stbtt_GetCodepointBox(const stbtt_fontinfo *info, int codepoint, int *x0, int *y0, int *x1, int *y1);
  11566. /* Gets the bounding box of the visible part of the glyph, in unscaled coordinates */
  11567. STBTT_DEF void stbtt_GetGlyphHMetrics(const stbtt_fontinfo *info, int glyph_index, int *advanceWidth, int *leftSideBearing);
  11568. STBTT_DEF int stbtt_GetGlyphKernAdvance(const stbtt_fontinfo *info, int glyph1, int glyph2);
  11569. STBTT_DEF int stbtt_GetGlyphBox(const stbtt_fontinfo *info, int glyph_index, int *x0, int *y0, int *x1, int *y1);
  11570. /* as above, but takes one or more glyph indices for greater efficiency */
  11571. typedef struct stbtt_kerningentry
  11572. {
  11573. int glyph1; /* use stbtt_FindGlyphIndex */
  11574. int glyph2;
  11575. int advance;
  11576. } stbtt_kerningentry;
  11577. STBTT_DEF int stbtt_GetKerningTableLength(const stbtt_fontinfo *info);
  11578. STBTT_DEF int stbtt_GetKerningTable(const stbtt_fontinfo *info, stbtt_kerningentry* table, int table_length);
  11579. /* Retrieves a complete list of all of the kerning pairs provided by the font */
  11580. /* stbtt_GetKerningTable never writes more than table_length entries and returns how many entries it did write. */
  11581. /* The table will be sorted by (a.glyph1 == b.glyph1)?(a.glyph2 < b.glyph2):(a.glyph1 < b.glyph1) */
  11582. /* //////////////////////////////////////////////////////////////////////////// */
  11583. /* */
  11584. /* GLYPH SHAPES (you probably don't need these, but they have to go before */
  11585. /* the bitmaps for C declaration-order reasons) */
  11586. /* */
  11587. #ifndef STBTT_vmove /* you can predefine these to use different values (but why?) */
  11588. enum {
  11589. STBTT_vmove=1,
  11590. STBTT_vline,
  11591. STBTT_vcurve,
  11592. STBTT_vcubic
  11593. };
  11594. #endif
  11595. #ifndef stbtt_vertex /* you can predefine this to use different values */
  11596. /* (we share this with other code at RAD) */
  11597. #define stbtt_vertex_type short /* can't use stbtt_int16 because that's not visible in the header file */
  11598. typedef struct
  11599. {
  11600. stbtt_vertex_type x,y,cx,cy,cx1,cy1;
  11601. unsigned char type,padding;
  11602. } stbtt_vertex;
  11603. #endif
  11604. STBTT_DEF int stbtt_IsGlyphEmpty(const stbtt_fontinfo *info, int glyph_index);
  11605. /* returns non-zero if nothing is drawn for this glyph */
  11606. STBTT_DEF int stbtt_GetCodepointShape(const stbtt_fontinfo *info, int unicode_codepoint, stbtt_vertex **vertices);
  11607. STBTT_DEF int stbtt_GetGlyphShape(const stbtt_fontinfo *info, int glyph_index, stbtt_vertex **vertices);
  11608. /* returns # of vertices and fills *vertices with the pointer to them */
  11609. /* these are expressed in "unscaled" coordinates */
  11610. /* */
  11611. /* The shape is a series of contours. Each one starts with */
  11612. /* a STBTT_moveto, then consists of a series of mixed */
  11613. /* STBTT_lineto and STBTT_curveto segments. A lineto */
  11614. /* draws a line from previous endpoint to its x,y; a curveto */
  11615. /* draws a quadratic bezier from previous endpoint to */
  11616. /* its x,y, using cx,cy as the bezier control point. */
  11617. STBTT_DEF void stbtt_FreeShape(const stbtt_fontinfo *info, stbtt_vertex *vertices);
  11618. /* frees the data allocated above */
  11619. STBTT_DEF unsigned char *stbtt_FindSVGDoc(const stbtt_fontinfo *info, int gl);
  11620. STBTT_DEF int stbtt_GetCodepointSVG(const stbtt_fontinfo *info, int unicode_codepoint, const char **svg);
  11621. STBTT_DEF int stbtt_GetGlyphSVG(const stbtt_fontinfo *info, int gl, const char **svg);
  11622. /* fills svg with the character's SVG data. */
  11623. /* returns data size or 0 if SVG not found. */
  11624. /* //////////////////////////////////////////////////////////////////////////// */
  11625. /* */
  11626. /* BITMAP RENDERING */
  11627. /* */
  11628. STBTT_DEF void stbtt_FreeBitmap(unsigned char *bitmap, void *userdata);
  11629. /* frees the bitmap allocated below */
  11630. 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);
  11631. /* allocates a large-enough single-channel 8bpp bitmap and renders the */
  11632. /* specified character/glyph at the specified scale into it, with */
  11633. /* antialiasing. 0 is no coverage (transparent), 255 is fully covered (opaque). */
  11634. /* *width & *height are filled out with the width & height of the bitmap, */
  11635. /* which is stored left-to-right, top-to-bottom. */
  11636. /* */
  11637. /* xoff/yoff are the offset it pixel space from the glyph origin to the top-left of the bitmap */
  11638. 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);
  11639. /* the same as stbtt_GetCodepoitnBitmap, but you can specify a subpixel */
  11640. /* shift for the character */
  11641. 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);
  11642. /* the same as stbtt_GetCodepointBitmap, but you pass in storage for the bitmap */
  11643. /* in the form of 'output', with row spacing of 'out_stride' bytes. the bitmap */
  11644. /* is clipped to out_w/out_h bytes. Call stbtt_GetCodepointBitmapBox to get the */
  11645. /* width and height and positioning info for it first. */
  11646. 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);
  11647. /* same as stbtt_MakeCodepointBitmap, but you can specify a subpixel */
  11648. /* shift for the character */
  11649. 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);
  11650. /* same as stbtt_MakeCodepointBitmapSubpixel, but prefiltering */
  11651. /* is performed (see stbtt_PackSetOversampling) */
  11652. 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);
  11653. /* get the bbox of the bitmap centered around the glyph origin; so the */
  11654. /* bitmap width is ix1-ix0, height is iy1-iy0, and location to place */
  11655. /* the bitmap top left is (leftSideBearing*scale,iy0). */
  11656. /* (Note that the bitmap uses y-increases-down, but the shape uses */
  11657. /* y-increases-up, so CodepointBitmapBox and CodepointBox are inverted.) */
  11658. 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);
  11659. /* same as stbtt_GetCodepointBitmapBox, but you can specify a subpixel */
  11660. /* shift for the character */
  11661. /* the following functions are equivalent to the above functions, but operate */
  11662. /* on glyph indices instead of Unicode codepoints (for efficiency) */
  11663. 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);
  11664. 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);
  11665. 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);
  11666. 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);
  11667. 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);
  11668. 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);
  11669. 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);
  11670. /* @TODO: don't expose this structure */
  11671. typedef struct
  11672. {
  11673. int w,h,stride;
  11674. unsigned char *pixels;
  11675. } stbtt__bitmap;
  11676. /* rasterize a shape with quadratic beziers into a bitmap */
  11677. STBTT_DEF void stbtt_Rasterize(stbtt__bitmap *result, /* 1-channel bitmap to draw into */
  11678. float flatness_in_pixels, /* allowable error of curve in pixels */
  11679. stbtt_vertex *vertices, /* array of vertices defining shape */
  11680. int num_verts, /* number of vertices in above array */
  11681. float scale_x, float scale_y, /* scale applied to input vertices */
  11682. float shift_x, float shift_y, /* translation applied to input vertices */
  11683. int x_off, int y_off, /* another translation applied to input */
  11684. int invert, /* if non-zero, vertically flip shape */
  11685. void *userdata); /* context for to STBTT_MALLOC */
  11686. /* //////////////////////////////////////////////////////////////////////////// */
  11687. /* */
  11688. /* Signed Distance Function (or Field) rendering */
  11689. STBTT_DEF void stbtt_FreeSDF(unsigned char *bitmap, void *userdata);
  11690. /* frees the SDF bitmap allocated below */
  11691. 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);
  11692. 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);
  11693. /* These functions compute a discretized SDF field for a single character, suitable for storing */
  11694. /* in a single-channel texture, sampling with bilinear filtering, and testing against */
  11695. /* larger than some threshold to produce scalable fonts. */
  11696. /* info -- the font */
  11697. /* scale -- controls the size of the resulting SDF bitmap, same as it would be creating a regular bitmap */
  11698. /* glyph/codepoint -- the character to generate the SDF for */
  11699. /* padding -- extra "pixels" around the character which are filled with the distance to the character (not 0), */
  11700. /* which allows effects like bit outlines */
  11701. /* onedge_value -- value 0-255 to test the SDF against to reconstruct the character (i.e. the isocontour of the character) */
  11702. /* pixel_dist_scale -- what value the SDF should increase by when moving one SDF "pixel" away from the edge (on the 0..255 scale) */
  11703. /* if positive, > onedge_value is inside; if negative, < onedge_value is inside */
  11704. /* width,height -- output height & width of the SDF bitmap (including padding) */
  11705. /* xoff,yoff -- output origin of the character */
  11706. /* return value -- a 2D array of bytes 0..255, width*height in size */
  11707. /* */
  11708. /* pixel_dist_scale & onedge_value are a scale & bias that allows you to make */
  11709. /* optimal use of the limited 0..255 for your application, trading off precision */
  11710. /* and special effects. SDF values outside the range 0..255 are clamped to 0..255. */
  11711. /* */
  11712. /* Example: */
  11713. /* scale = stbtt_ScaleForPixelHeight(22) */
  11714. /* padding = 5 */
  11715. /* onedge_value = 180 */
  11716. /* pixel_dist_scale = 180/5.0 = 36.0 */
  11717. /* */
  11718. /* This will create an SDF bitmap in which the character is about 22 pixels */
  11719. /* high but the whole bitmap is about 22+5+5=32 pixels high. To produce a filled */
  11720. /* shape, sample the SDF at each pixel and fill the pixel if the SDF value */
  11721. /* is greater than or equal to 180/255. (You'll actually want to antialias, */
  11722. /* which is beyond the scope of this example.) Additionally, you can compute */
  11723. /* offset outlines (e.g. to stroke the character border inside & outside, */
  11724. /* or only outside). For example, to fill outside the character up to 3 SDF */
  11725. /* pixels, you would compare against (180-36.0*3)/255 = 72/255. The above */
  11726. /* choice of variables maps a range from 5 pixels outside the shape to */
  11727. /* 2 pixels inside the shape to 0..255; this is intended primarily for apply */
  11728. /* outside effects only (the interior range is needed to allow proper */
  11729. /* antialiasing of the font at *smaller* sizes) */
  11730. /* */
  11731. /* The function computes the SDF analytically at each SDF pixel, not by e.g. */
  11732. /* building a higher-res bitmap and approximating it. In theory the quality */
  11733. /* should be as high as possible for an SDF of this size & representation, but */
  11734. /* unclear if this is true in practice (perhaps building a higher-res bitmap */
  11735. /* and computing from that can allow drop-out prevention). */
  11736. /* */
  11737. /* The algorithm has not been optimized at all, so expect it to be slow */
  11738. /* if computing lots of characters or very large sizes. */
  11739. /* //////////////////////////////////////////////////////////////////////////// */
  11740. /* */
  11741. /* Finding the right font... */
  11742. /* */
  11743. /* You should really just solve this offline, keep your own tables */
  11744. /* of what font is what, and don't try to get it out of the .ttf file. */
  11745. /* That's because getting it out of the .ttf file is really hard, because */
  11746. /* the names in the file can appear in many possible encodings, in many */
  11747. /* possible languages, and e.g. if you need a case-insensitive comparison, */
  11748. /* the details of that depend on the encoding & language in a complex way */
  11749. /* (actually underspecified in truetype, but also gigantic). */
  11750. /* */
  11751. /* But you can use the provided functions in two possible ways: */
  11752. /* stbtt_FindMatchingFont() will use *case-sensitive* comparisons on */
  11753. /* unicode-encoded names to try to find the font you want; */
  11754. /* you can run this before calling stbtt_InitFont() */
  11755. /* */
  11756. /* stbtt_GetFontNameString() lets you get any of the various strings */
  11757. /* from the file yourself and do your own comparisons on them. */
  11758. /* You have to have called stbtt_InitFont() first. */
  11759. STBTT_DEF int stbtt_FindMatchingFont(const unsigned char *fontdata, const char *name, int flags);
  11760. /* returns the offset (not index) of the font that matches, or -1 if none */
  11761. /* if you use STBTT_MACSTYLE_DONTCARE, use a font name like "Arial Bold". */
  11762. /* if you use any other flag, use a font name like "Arial"; this checks */
  11763. /* the 'macStyle' header field; i don't know if fonts set this consistently */
  11764. #define STBTT_MACSTYLE_DONTCARE 0
  11765. #define STBTT_MACSTYLE_BOLD 1
  11766. #define STBTT_MACSTYLE_ITALIC 2
  11767. #define STBTT_MACSTYLE_UNDERSCORE 4
  11768. #define STBTT_MACSTYLE_NONE 8 /* <= not same as 0, this makes us check the bitfield is 0 */
  11769. STBTT_DEF int stbtt_CompareUTF8toUTF16_bigendian(const char *s1, int len1, const char *s2, int len2);
  11770. /* returns 1/0 whether the first string interpreted as utf8 is identical to */
  11771. /* the second string interpreted as big-endian utf16... useful for strings from next func */
  11772. STBTT_DEF const char *stbtt_GetFontNameString(const stbtt_fontinfo *font, int *length, int platformID, int encodingID, int languageID, int nameID);
  11773. /* returns the string (which may be big-endian double byte, e.g. for unicode) */
  11774. /* and puts the length in bytes in *length. */
  11775. /* */
  11776. /* some of the values for the IDs are below; for more see the truetype spec: */
  11777. /* http://developer.apple.com/textfonts/TTRefMan/RM06/Chap6name.html */
  11778. /* http://www.microsoft.com/typography/otspec/name.htm */
  11779. enum { /* platformID */
  11780. STBTT_PLATFORM_ID_UNICODE =0,
  11781. STBTT_PLATFORM_ID_MAC =1,
  11782. STBTT_PLATFORM_ID_ISO =2,
  11783. STBTT_PLATFORM_ID_MICROSOFT =3
  11784. };
  11785. enum { /* encodingID for STBTT_PLATFORM_ID_UNICODE */
  11786. STBTT_UNICODE_EID_UNICODE_1_0 =0,
  11787. STBTT_UNICODE_EID_UNICODE_1_1 =1,
  11788. STBTT_UNICODE_EID_ISO_10646 =2,
  11789. STBTT_UNICODE_EID_UNICODE_2_0_BMP=3,
  11790. STBTT_UNICODE_EID_UNICODE_2_0_FULL=4
  11791. };
  11792. enum { /* encodingID for STBTT_PLATFORM_ID_MICROSOFT */
  11793. STBTT_MS_EID_SYMBOL =0,
  11794. STBTT_MS_EID_UNICODE_BMP =1,
  11795. STBTT_MS_EID_SHIFTJIS =2,
  11796. STBTT_MS_EID_UNICODE_FULL =10
  11797. };
  11798. enum { /* encodingID for STBTT_PLATFORM_ID_MAC; same as Script Manager codes */
  11799. STBTT_MAC_EID_ROMAN =0, STBTT_MAC_EID_ARABIC =4,
  11800. STBTT_MAC_EID_JAPANESE =1, STBTT_MAC_EID_HEBREW =5,
  11801. STBTT_MAC_EID_CHINESE_TRAD =2, STBTT_MAC_EID_GREEK =6,
  11802. STBTT_MAC_EID_KOREAN =3, STBTT_MAC_EID_RUSSIAN =7
  11803. };
  11804. enum { /* languageID for STBTT_PLATFORM_ID_MICROSOFT; same as LCID... */
  11805. /* problematic because there are e.g. 16 english LCIDs and 16 arabic LCIDs */
  11806. STBTT_MS_LANG_ENGLISH =0x0409, STBTT_MS_LANG_ITALIAN =0x0410,
  11807. STBTT_MS_LANG_CHINESE =0x0804, STBTT_MS_LANG_JAPANESE =0x0411,
  11808. STBTT_MS_LANG_DUTCH =0x0413, STBTT_MS_LANG_KOREAN =0x0412,
  11809. STBTT_MS_LANG_FRENCH =0x040c, STBTT_MS_LANG_RUSSIAN =0x0419,
  11810. STBTT_MS_LANG_GERMAN =0x0407, STBTT_MS_LANG_SPANISH =0x0409,
  11811. STBTT_MS_LANG_HEBREW =0x040d, STBTT_MS_LANG_SWEDISH =0x041D
  11812. };
  11813. enum { /* languageID for STBTT_PLATFORM_ID_MAC */
  11814. STBTT_MAC_LANG_ENGLISH =0 , STBTT_MAC_LANG_JAPANESE =11,
  11815. STBTT_MAC_LANG_ARABIC =12, STBTT_MAC_LANG_KOREAN =23,
  11816. STBTT_MAC_LANG_DUTCH =4 , STBTT_MAC_LANG_RUSSIAN =32,
  11817. STBTT_MAC_LANG_FRENCH =1 , STBTT_MAC_LANG_SPANISH =6 ,
  11818. STBTT_MAC_LANG_GERMAN =2 , STBTT_MAC_LANG_SWEDISH =5 ,
  11819. STBTT_MAC_LANG_HEBREW =10, STBTT_MAC_LANG_CHINESE_SIMPLIFIED =33,
  11820. STBTT_MAC_LANG_ITALIAN =3 , STBTT_MAC_LANG_CHINESE_TRAD =19
  11821. };
  11822. #ifdef __cplusplus
  11823. }
  11824. #endif
  11825. #endif /* __STB_INCLUDE_STB_TRUETYPE_H__ */
  11826. /* ///////////////////////////////////////////////////////////////////////////// */
  11827. /* ///////////////////////////////////////////////////////////////////////////// */
  11828. /* // */
  11829. /* // IMPLEMENTATION */
  11830. /* // */
  11831. /* // */
  11832. #ifdef STB_TRUETYPE_IMPLEMENTATION
  11833. #ifndef STBTT_MAX_OVERSAMPLE
  11834. #define STBTT_MAX_OVERSAMPLE 8
  11835. #endif
  11836. #if STBTT_MAX_OVERSAMPLE > 255
  11837. #error "STBTT_MAX_OVERSAMPLE cannot be > 255"
  11838. #endif
  11839. typedef int stbtt__test_oversample_pow2[(STBTT_MAX_OVERSAMPLE & (STBTT_MAX_OVERSAMPLE-1)) == 0 ? 1 : -1];
  11840. #ifndef STBTT_RASTERIZER_VERSION
  11841. #define STBTT_RASTERIZER_VERSION 2
  11842. #endif
  11843. #ifdef _MSC_VER
  11844. #define STBTT__NOTUSED(v) (void)(v)
  11845. #else
  11846. #define STBTT__NOTUSED(v) (void)sizeof(v)
  11847. #endif
  11848. /* //////////////////////////////////////////////////////////////////////// */
  11849. /* */
  11850. /* stbtt__buf helpers to parse data from file */
  11851. /* */
  11852. static stbtt_uint8 stbtt__buf_get8(stbtt__buf *b)
  11853. {
  11854. if (b->cursor >= b->size)
  11855. return 0;
  11856. return b->data[b->cursor++];
  11857. }
  11858. static stbtt_uint8 stbtt__buf_peek8(stbtt__buf *b)
  11859. {
  11860. if (b->cursor >= b->size)
  11861. return 0;
  11862. return b->data[b->cursor];
  11863. }
  11864. static void stbtt__buf_seek(stbtt__buf *b, int o)
  11865. {
  11866. STBTT_assert(!(o > b->size || o < 0));
  11867. b->cursor = (o > b->size || o < 0) ? b->size : o;
  11868. }
  11869. static void stbtt__buf_skip(stbtt__buf *b, int o)
  11870. {
  11871. stbtt__buf_seek(b, b->cursor + o);
  11872. }
  11873. static stbtt_uint32 stbtt__buf_get(stbtt__buf *b, int n)
  11874. {
  11875. stbtt_uint32 v = 0;
  11876. int i;
  11877. STBTT_assert(n >= 1 && n <= 4);
  11878. for (i = 0; i < n; i++)
  11879. v = (v << 8) | stbtt__buf_get8(b);
  11880. return v;
  11881. }
  11882. static stbtt__buf stbtt__new_buf(const void *p, size_t size)
  11883. {
  11884. stbtt__buf r;
  11885. STBTT_assert(size < 0x40000000);
  11886. r.data = (stbtt_uint8*) p;
  11887. r.size = (int) size;
  11888. r.cursor = 0;
  11889. return r;
  11890. }
  11891. #define stbtt__buf_get16(b) stbtt__buf_get((b), 2)
  11892. #define stbtt__buf_get32(b) stbtt__buf_get((b), 4)
  11893. static stbtt__buf stbtt__buf_range(const stbtt__buf *b, int o, int s)
  11894. {
  11895. stbtt__buf r = stbtt__new_buf(NULL, 0);
  11896. if (o < 0 || s < 0 || o > b->size || s > b->size - o) return r;
  11897. r.data = b->data + o;
  11898. r.size = s;
  11899. return r;
  11900. }
  11901. static stbtt__buf stbtt__cff_get_index(stbtt__buf *b)
  11902. {
  11903. int count, start, offsize;
  11904. start = b->cursor;
  11905. count = stbtt__buf_get16(b);
  11906. if (count) {
  11907. offsize = stbtt__buf_get8(b);
  11908. STBTT_assert(offsize >= 1 && offsize <= 4);
  11909. stbtt__buf_skip(b, offsize * count);
  11910. stbtt__buf_skip(b, stbtt__buf_get(b, offsize) - 1);
  11911. }
  11912. return stbtt__buf_range(b, start, b->cursor - start);
  11913. }
  11914. static stbtt_uint32 stbtt__cff_int(stbtt__buf *b)
  11915. {
  11916. int b0 = stbtt__buf_get8(b);
  11917. if (b0 >= 32 && b0 <= 246) return b0 - 139;
  11918. else if (b0 >= 247 && b0 <= 250) return (b0 - 247)*256 + stbtt__buf_get8(b) + 108;
  11919. else if (b0 >= 251 && b0 <= 254) return -(b0 - 251)*256 - stbtt__buf_get8(b) - 108;
  11920. else if (b0 == 28) return stbtt__buf_get16(b);
  11921. else if (b0 == 29) return stbtt__buf_get32(b);
  11922. STBTT_assert(0);
  11923. return 0;
  11924. }
  11925. static void stbtt__cff_skip_operand(stbtt__buf *b) {
  11926. int v, b0 = stbtt__buf_peek8(b);
  11927. STBTT_assert(b0 >= 28);
  11928. if (b0 == 30) {
  11929. stbtt__buf_skip(b, 1);
  11930. while (b->cursor < b->size) {
  11931. v = stbtt__buf_get8(b);
  11932. if ((v & 0xF) == 0xF || (v >> 4) == 0xF)
  11933. break;
  11934. }
  11935. } else {
  11936. stbtt__cff_int(b);
  11937. }
  11938. }
  11939. static stbtt__buf stbtt__dict_get(stbtt__buf *b, int key)
  11940. {
  11941. stbtt__buf_seek(b, 0);
  11942. while (b->cursor < b->size) {
  11943. int start = b->cursor, end, op;
  11944. while (stbtt__buf_peek8(b) >= 28)
  11945. stbtt__cff_skip_operand(b);
  11946. end = b->cursor;
  11947. op = stbtt__buf_get8(b);
  11948. if (op == 12) op = stbtt__buf_get8(b) | 0x100;
  11949. if (op == key) return stbtt__buf_range(b, start, end-start);
  11950. }
  11951. return stbtt__buf_range(b, 0, 0);
  11952. }
  11953. static void stbtt__dict_get_ints(stbtt__buf *b, int key, int outcount, stbtt_uint32 *out)
  11954. {
  11955. int i;
  11956. stbtt__buf operands = stbtt__dict_get(b, key);
  11957. for (i = 0; i < outcount && operands.cursor < operands.size; i++)
  11958. out[i] = stbtt__cff_int(&operands);
  11959. }
  11960. static int stbtt__cff_index_count(stbtt__buf *b)
  11961. {
  11962. stbtt__buf_seek(b, 0);
  11963. return stbtt__buf_get16(b);
  11964. }
  11965. static stbtt__buf stbtt__cff_index_get(stbtt__buf b, int i)
  11966. {
  11967. int count, offsize, start, end;
  11968. stbtt__buf_seek(&b, 0);
  11969. count = stbtt__buf_get16(&b);
  11970. offsize = stbtt__buf_get8(&b);
  11971. STBTT_assert(i >= 0 && i < count);
  11972. STBTT_assert(offsize >= 1 && offsize <= 4);
  11973. stbtt__buf_skip(&b, i*offsize);
  11974. start = stbtt__buf_get(&b, offsize);
  11975. end = stbtt__buf_get(&b, offsize);
  11976. return stbtt__buf_range(&b, 2+(count+1)*offsize+start, end - start);
  11977. }
  11978. /* //////////////////////////////////////////////////////////////////////// */
  11979. /* */
  11980. /* accessors to parse data from file */
  11981. /* */
  11982. /* on platforms that don't allow misaligned reads, if we want to allow */
  11983. /* truetype fonts that aren't padded to alignment, define ALLOW_UNALIGNED_TRUETYPE */
  11984. #define ttBYTE(p) (* (stbtt_uint8 *) (p))
  11985. #define ttCHAR(p) (* (stbtt_int8 *) (p))
  11986. #define ttFixed(p) ttLONG(p)
  11987. static stbtt_uint16 ttUSHORT(stbtt_uint8 *p) { return p[0]*256 + p[1]; }
  11988. static stbtt_int16 ttSHORT(stbtt_uint8 *p) { return p[0]*256 + p[1]; }
  11989. static stbtt_uint32 ttULONG(stbtt_uint8 *p) { return (p[0]<<24) + (p[1]<<16) + (p[2]<<8) + p[3]; }
  11990. static stbtt_int32 ttLONG(stbtt_uint8 *p) { return (p[0]<<24) + (p[1]<<16) + (p[2]<<8) + p[3]; }
  11991. #define stbtt_tag4(p,c0,c1,c2,c3) ((p)[0] == (c0) && (p)[1] == (c1) && (p)[2] == (c2) && (p)[3] == (c3))
  11992. #define stbtt_tag(p,str) stbtt_tag4(p,str[0],str[1],str[2],str[3])
  11993. static int stbtt__isfont(stbtt_uint8 *font)
  11994. {
  11995. /* check the version number */
  11996. if (stbtt_tag4(font, '1',0,0,0)) return 1; /* TrueType 1 */
  11997. if (stbtt_tag(font, "typ1")) return 1; /* TrueType with type 1 font -- we don't support this! */
  11998. if (stbtt_tag(font, "OTTO")) return 1; /* OpenType with CFF */
  11999. if (stbtt_tag4(font, 0,1,0,0)) return 1; /* OpenType 1.0 */
  12000. if (stbtt_tag(font, "true")) return 1; /* Apple specification for TrueType fonts */
  12001. return 0;
  12002. }
  12003. /* @OPTIMIZE: binary search */
  12004. static stbtt_uint32 stbtt__find_table(stbtt_uint8 *data, stbtt_uint32 fontstart, const char *tag)
  12005. {
  12006. stbtt_int32 num_tables = ttUSHORT(data+fontstart+4);
  12007. stbtt_uint32 tabledir = fontstart + 12;
  12008. stbtt_int32 i;
  12009. for (i=0; i < num_tables; ++i) {
  12010. stbtt_uint32 loc = tabledir + 16*i;
  12011. if (stbtt_tag(data+loc+0, tag))
  12012. return ttULONG(data+loc+8);
  12013. }
  12014. return 0;
  12015. }
  12016. static int stbtt_GetFontOffsetForIndex_internal(unsigned char *font_collection, int index)
  12017. {
  12018. /* if it's just a font, there's only one valid index */
  12019. if (stbtt__isfont(font_collection))
  12020. return index == 0 ? 0 : -1;
  12021. /* check if it's a TTC */
  12022. if (stbtt_tag(font_collection, "ttcf")) {
  12023. /* version 1? */
  12024. if (ttULONG(font_collection+4) == 0x00010000 || ttULONG(font_collection+4) == 0x00020000) {
  12025. stbtt_int32 n = ttLONG(font_collection+8);
  12026. if (index >= n)
  12027. return -1;
  12028. return ttULONG(font_collection+12+index*4);
  12029. }
  12030. }
  12031. return -1;
  12032. }
  12033. static int stbtt_GetNumberOfFonts_internal(unsigned char *font_collection)
  12034. {
  12035. /* if it's just a font, there's only one valid font */
  12036. if (stbtt__isfont(font_collection))
  12037. return 1;
  12038. /* check if it's a TTC */
  12039. if (stbtt_tag(font_collection, "ttcf")) {
  12040. /* version 1? */
  12041. if (ttULONG(font_collection+4) == 0x00010000 || ttULONG(font_collection+4) == 0x00020000) {
  12042. return ttLONG(font_collection+8);
  12043. }
  12044. }
  12045. return 0;
  12046. }
  12047. static stbtt__buf stbtt__get_subrs(stbtt__buf cff, stbtt__buf fontdict)
  12048. {
  12049. stbtt_uint32 subrsoff = 0, private_loc[2] = { 0, 0 };
  12050. stbtt__buf pdict;
  12051. stbtt__dict_get_ints(&fontdict, 18, 2, private_loc);
  12052. if (!private_loc[1] || !private_loc[0]) return stbtt__new_buf(NULL, 0);
  12053. pdict = stbtt__buf_range(&cff, private_loc[1], private_loc[0]);
  12054. stbtt__dict_get_ints(&pdict, 19, 1, &subrsoff);
  12055. if (!subrsoff) return stbtt__new_buf(NULL, 0);
  12056. stbtt__buf_seek(&cff, private_loc[1]+subrsoff);
  12057. return stbtt__cff_get_index(&cff);
  12058. }
  12059. /* since most people won't use this, find this table the first time it's needed */
  12060. static int stbtt__get_svg(stbtt_fontinfo *info)
  12061. {
  12062. stbtt_uint32 t;
  12063. if (info->svg < 0) {
  12064. t = stbtt__find_table(info->data, info->fontstart, "SVG ");
  12065. if (t) {
  12066. stbtt_uint32 offset = ttULONG(info->data + t + 2);
  12067. info->svg = t + offset;
  12068. } else {
  12069. info->svg = 0;
  12070. }
  12071. }
  12072. return info->svg;
  12073. }
  12074. static int stbtt_InitFont_internal(stbtt_fontinfo *info, unsigned char *data, int fontstart)
  12075. {
  12076. stbtt_uint32 cmap, t;
  12077. stbtt_int32 i,numTables;
  12078. info->data = data;
  12079. info->fontstart = fontstart;
  12080. info->cff = stbtt__new_buf(NULL, 0);
  12081. cmap = stbtt__find_table(data, fontstart, "cmap"); /* required */
  12082. info->loca = stbtt__find_table(data, fontstart, "loca"); /* required */
  12083. info->head = stbtt__find_table(data, fontstart, "head"); /* required */
  12084. info->glyf = stbtt__find_table(data, fontstart, "glyf"); /* required */
  12085. info->hhea = stbtt__find_table(data, fontstart, "hhea"); /* required */
  12086. info->hmtx = stbtt__find_table(data, fontstart, "hmtx"); /* required */
  12087. info->kern = stbtt__find_table(data, fontstart, "kern"); /* not required */
  12088. info->gpos = stbtt__find_table(data, fontstart, "GPOS"); /* not required */
  12089. if (!cmap || !info->head || !info->hhea || !info->hmtx)
  12090. return 0;
  12091. if (info->glyf) {
  12092. /* required for truetype */
  12093. if (!info->loca) return 0;
  12094. } else {
  12095. /* initialization for CFF / Type2 fonts (OTF) */
  12096. stbtt__buf b, topdict, topdictidx;
  12097. stbtt_uint32 cstype = 2, charstrings = 0, fdarrayoff = 0, fdselectoff = 0;
  12098. stbtt_uint32 cff;
  12099. cff = stbtt__find_table(data, fontstart, "CFF ");
  12100. if (!cff) return 0;
  12101. info->fontdicts = stbtt__new_buf(NULL, 0);
  12102. info->fdselect = stbtt__new_buf(NULL, 0);
  12103. /* @TODO this should use size from table (not 512MB) */
  12104. info->cff = stbtt__new_buf(data+cff, 512*1024*1024);
  12105. b = info->cff;
  12106. /* read the header */
  12107. stbtt__buf_skip(&b, 2);
  12108. stbtt__buf_seek(&b, stbtt__buf_get8(&b)); /* hdrsize */
  12109. /* @TODO the name INDEX could list multiple fonts, */
  12110. /* but we just use the first one. */
  12111. stbtt__cff_get_index(&b); /* name INDEX */
  12112. topdictidx = stbtt__cff_get_index(&b);
  12113. topdict = stbtt__cff_index_get(topdictidx, 0);
  12114. stbtt__cff_get_index(&b); /* string INDEX */
  12115. info->gsubrs = stbtt__cff_get_index(&b);
  12116. stbtt__dict_get_ints(&topdict, 17, 1, &charstrings);
  12117. stbtt__dict_get_ints(&topdict, 0x100 | 6, 1, &cstype);
  12118. stbtt__dict_get_ints(&topdict, 0x100 | 36, 1, &fdarrayoff);
  12119. stbtt__dict_get_ints(&topdict, 0x100 | 37, 1, &fdselectoff);
  12120. info->subrs = stbtt__get_subrs(b, topdict);
  12121. /* we only support Type 2 charstrings */
  12122. if (cstype != 2) return 0;
  12123. if (charstrings == 0) return 0;
  12124. if (fdarrayoff) {
  12125. /* looks like a CID font */
  12126. if (!fdselectoff) return 0;
  12127. stbtt__buf_seek(&b, fdarrayoff);
  12128. info->fontdicts = stbtt__cff_get_index(&b);
  12129. info->fdselect = stbtt__buf_range(&b, fdselectoff, b.size-fdselectoff);
  12130. }
  12131. stbtt__buf_seek(&b, charstrings);
  12132. info->charstrings = stbtt__cff_get_index(&b);
  12133. }
  12134. t = stbtt__find_table(data, fontstart, "maxp");
  12135. if (t)
  12136. info->numGlyphs = ttUSHORT(data+t+4);
  12137. else
  12138. info->numGlyphs = 0xffff;
  12139. info->svg = -1;
  12140. /* find a cmap encoding table we understand *now* to avoid searching */
  12141. /* later. (todo: could make this installable) */
  12142. /* the same regardless of glyph. */
  12143. numTables = ttUSHORT(data + cmap + 2);
  12144. info->index_map = 0;
  12145. for (i=0; i < numTables; ++i) {
  12146. stbtt_uint32 encoding_record = cmap + 4 + 8 * i;
  12147. /* find an encoding we understand: */
  12148. switch(ttUSHORT(data+encoding_record)) {
  12149. case STBTT_PLATFORM_ID_MICROSOFT:
  12150. switch (ttUSHORT(data+encoding_record+2)) {
  12151. case STBTT_MS_EID_UNICODE_BMP:
  12152. case STBTT_MS_EID_UNICODE_FULL:
  12153. /* MS/Unicode */
  12154. info->index_map = cmap + ttULONG(data+encoding_record+4);
  12155. break;
  12156. }
  12157. break;
  12158. case STBTT_PLATFORM_ID_UNICODE:
  12159. /* Mac/iOS has these */
  12160. /* all the encodingIDs are unicode, so we don't bother to check it */
  12161. info->index_map = cmap + ttULONG(data+encoding_record+4);
  12162. break;
  12163. }
  12164. }
  12165. if (info->index_map == 0)
  12166. return 0;
  12167. info->indexToLocFormat = ttUSHORT(data+info->head + 50);
  12168. return 1;
  12169. }
  12170. STBTT_DEF int stbtt_FindGlyphIndex(const stbtt_fontinfo *info, int unicode_codepoint)
  12171. {
  12172. stbtt_uint8 *data = info->data;
  12173. stbtt_uint32 index_map = info->index_map;
  12174. stbtt_uint16 format = ttUSHORT(data + index_map + 0);
  12175. if (format == 0) { /* apple byte encoding */
  12176. stbtt_int32 bytes = ttUSHORT(data + index_map + 2);
  12177. if (unicode_codepoint < bytes-6)
  12178. return ttBYTE(data + index_map + 6 + unicode_codepoint);
  12179. return 0;
  12180. } else if (format == 6) {
  12181. stbtt_uint32 first = ttUSHORT(data + index_map + 6);
  12182. stbtt_uint32 count = ttUSHORT(data + index_map + 8);
  12183. if ((stbtt_uint32) unicode_codepoint >= first && (stbtt_uint32) unicode_codepoint < first+count)
  12184. return ttUSHORT(data + index_map + 10 + (unicode_codepoint - first)*2);
  12185. return 0;
  12186. } else if (format == 2) {
  12187. STBTT_assert(0); /* @TODO: high-byte mapping for japanese/chinese/korean */
  12188. return 0;
  12189. } else if (format == 4) { /* standard mapping for windows fonts: binary search collection of ranges */
  12190. stbtt_uint16 segcount = ttUSHORT(data+index_map+6) >> 1;
  12191. stbtt_uint16 searchRange = ttUSHORT(data+index_map+8) >> 1;
  12192. stbtt_uint16 entrySelector = ttUSHORT(data+index_map+10);
  12193. stbtt_uint16 rangeShift = ttUSHORT(data+index_map+12) >> 1;
  12194. /* do a binary search of the segments */
  12195. stbtt_uint32 endCount = index_map + 14;
  12196. stbtt_uint32 search = endCount;
  12197. if (unicode_codepoint > 0xffff)
  12198. return 0;
  12199. /* they lie from endCount .. endCount + segCount */
  12200. /* but searchRange is the nearest power of two, so... */
  12201. if (unicode_codepoint >= ttUSHORT(data + search + rangeShift*2))
  12202. search += rangeShift*2;
  12203. /* now decrement to bias correctly to find smallest */
  12204. search -= 2;
  12205. while (entrySelector) {
  12206. stbtt_uint16 end;
  12207. searchRange >>= 1;
  12208. end = ttUSHORT(data + search + searchRange*2);
  12209. if (unicode_codepoint > end)
  12210. search += searchRange*2;
  12211. --entrySelector;
  12212. }
  12213. search += 2;
  12214. {
  12215. stbtt_uint16 offset, start, last;
  12216. stbtt_uint16 item = (stbtt_uint16) ((search - endCount) >> 1);
  12217. start = ttUSHORT(data + index_map + 14 + segcount*2 + 2 + 2*item);
  12218. last = ttUSHORT(data + endCount + 2*item);
  12219. if (unicode_codepoint < start || unicode_codepoint > last)
  12220. return 0;
  12221. offset = ttUSHORT(data + index_map + 14 + segcount*6 + 2 + 2*item);
  12222. if (offset == 0)
  12223. return (stbtt_uint16) (unicode_codepoint + ttSHORT(data + index_map + 14 + segcount*4 + 2 + 2*item));
  12224. return ttUSHORT(data + offset + (unicode_codepoint-start)*2 + index_map + 14 + segcount*6 + 2 + 2*item);
  12225. }
  12226. } else if (format == 12 || format == 13) {
  12227. stbtt_uint32 ngroups = ttULONG(data+index_map+12);
  12228. stbtt_int32 low,high;
  12229. low = 0; high = (stbtt_int32)ngroups;
  12230. /* Binary search the right group. */
  12231. while (low < high) {
  12232. stbtt_int32 mid = low + ((high-low) >> 1); /* rounds down, so low <= mid < high */
  12233. stbtt_uint32 start_char = ttULONG(data+index_map+16+mid*12);
  12234. stbtt_uint32 end_char = ttULONG(data+index_map+16+mid*12+4);
  12235. if ((stbtt_uint32) unicode_codepoint < start_char)
  12236. high = mid;
  12237. else if ((stbtt_uint32) unicode_codepoint > end_char)
  12238. low = mid+1;
  12239. else {
  12240. stbtt_uint32 start_glyph = ttULONG(data+index_map+16+mid*12+8);
  12241. if (format == 12)
  12242. return start_glyph + unicode_codepoint-start_char;
  12243. else /* format == 13 */
  12244. return start_glyph;
  12245. }
  12246. }
  12247. return 0; /* not found */
  12248. }
  12249. /* @TODO */
  12250. STBTT_assert(0);
  12251. return 0;
  12252. }
  12253. STBTT_DEF int stbtt_GetCodepointShape(const stbtt_fontinfo *info, int unicode_codepoint, stbtt_vertex **vertices)
  12254. {
  12255. return stbtt_GetGlyphShape(info, stbtt_FindGlyphIndex(info, unicode_codepoint), vertices);
  12256. }
  12257. static void stbtt_setvertex(stbtt_vertex *v, stbtt_uint8 type, stbtt_int32 x, stbtt_int32 y, stbtt_int32 cx, stbtt_int32 cy)
  12258. {
  12259. v->type = type;
  12260. v->x = (stbtt_int16) x;
  12261. v->y = (stbtt_int16) y;
  12262. v->cx = (stbtt_int16) cx;
  12263. v->cy = (stbtt_int16) cy;
  12264. }
  12265. static int stbtt__GetGlyfOffset(const stbtt_fontinfo *info, int glyph_index)
  12266. {
  12267. int g1,g2;
  12268. STBTT_assert(!info->cff.size);
  12269. if (glyph_index >= info->numGlyphs) return -1; /* glyph index out of range */
  12270. if (info->indexToLocFormat >= 2) return -1; /* unknown index->glyph map format */
  12271. if (info->indexToLocFormat == 0) {
  12272. g1 = info->glyf + ttUSHORT(info->data + info->loca + glyph_index * 2) * 2;
  12273. g2 = info->glyf + ttUSHORT(info->data + info->loca + glyph_index * 2 + 2) * 2;
  12274. } else {
  12275. g1 = info->glyf + ttULONG (info->data + info->loca + glyph_index * 4);
  12276. g2 = info->glyf + ttULONG (info->data + info->loca + glyph_index * 4 + 4);
  12277. }
  12278. return g1==g2 ? -1 : g1; /* if length is 0, return -1 */
  12279. }
  12280. static int stbtt__GetGlyphInfoT2(const stbtt_fontinfo *info, int glyph_index, int *x0, int *y0, int *x1, int *y1);
  12281. STBTT_DEF int stbtt_GetGlyphBox(const stbtt_fontinfo *info, int glyph_index, int *x0, int *y0, int *x1, int *y1)
  12282. {
  12283. if (info->cff.size) {
  12284. stbtt__GetGlyphInfoT2(info, glyph_index, x0, y0, x1, y1);
  12285. } else {
  12286. int g = stbtt__GetGlyfOffset(info, glyph_index);
  12287. if (g < 0) return 0;
  12288. if (x0) *x0 = ttSHORT(info->data + g + 2);
  12289. if (y0) *y0 = ttSHORT(info->data + g + 4);
  12290. if (x1) *x1 = ttSHORT(info->data + g + 6);
  12291. if (y1) *y1 = ttSHORT(info->data + g + 8);
  12292. }
  12293. return 1;
  12294. }
  12295. STBTT_DEF int stbtt_GetCodepointBox(const stbtt_fontinfo *info, int codepoint, int *x0, int *y0, int *x1, int *y1)
  12296. {
  12297. return stbtt_GetGlyphBox(info, stbtt_FindGlyphIndex(info,codepoint), x0,y0,x1,y1);
  12298. }
  12299. STBTT_DEF int stbtt_IsGlyphEmpty(const stbtt_fontinfo *info, int glyph_index)
  12300. {
  12301. stbtt_int16 numberOfContours;
  12302. int g;
  12303. if (info->cff.size)
  12304. return stbtt__GetGlyphInfoT2(info, glyph_index, NULL, NULL, NULL, NULL) == 0;
  12305. g = stbtt__GetGlyfOffset(info, glyph_index);
  12306. if (g < 0) return 1;
  12307. numberOfContours = ttSHORT(info->data + g);
  12308. return numberOfContours == 0;
  12309. }
  12310. static int stbtt__close_shape(stbtt_vertex *vertices, int num_vertices, int was_off, int start_off,
  12311. stbtt_int32 sx, stbtt_int32 sy, stbtt_int32 scx, stbtt_int32 scy, stbtt_int32 cx, stbtt_int32 cy)
  12312. {
  12313. if (start_off) {
  12314. if (was_off)
  12315. stbtt_setvertex(&vertices[num_vertices++], STBTT_vcurve, (cx+scx)>>1, (cy+scy)>>1, cx,cy);
  12316. stbtt_setvertex(&vertices[num_vertices++], STBTT_vcurve, sx,sy,scx,scy);
  12317. } else {
  12318. if (was_off)
  12319. stbtt_setvertex(&vertices[num_vertices++], STBTT_vcurve,sx,sy,cx,cy);
  12320. else
  12321. stbtt_setvertex(&vertices[num_vertices++], STBTT_vline,sx,sy,0,0);
  12322. }
  12323. return num_vertices;
  12324. }
  12325. static int stbtt__GetGlyphShapeTT(const stbtt_fontinfo *info, int glyph_index, stbtt_vertex **pvertices)
  12326. {
  12327. stbtt_int16 numberOfContours;
  12328. stbtt_uint8 *endPtsOfContours;
  12329. stbtt_uint8 *data = info->data;
  12330. stbtt_vertex *vertices=0;
  12331. int num_vertices=0;
  12332. int g = stbtt__GetGlyfOffset(info, glyph_index);
  12333. *pvertices = NULL;
  12334. if (g < 0) return 0;
  12335. numberOfContours = ttSHORT(data + g);
  12336. if (numberOfContours > 0) {
  12337. stbtt_uint8 flags=0,flagcount;
  12338. stbtt_int32 ins, i,j=0,m,n, next_move, was_off=0, off, start_off=0;
  12339. stbtt_int32 x,y,cx,cy,sx,sy, scx,scy;
  12340. stbtt_uint8 *points;
  12341. endPtsOfContours = (data + g + 10);
  12342. ins = ttUSHORT(data + g + 10 + numberOfContours * 2);
  12343. points = data + g + 10 + numberOfContours * 2 + 2 + ins;
  12344. n = 1+ttUSHORT(endPtsOfContours + numberOfContours*2-2);
  12345. m = n + 2*numberOfContours; /* a loose bound on how many vertices we might need */
  12346. vertices = (stbtt_vertex *) STBTT_malloc(m * sizeof(vertices[0]), info->userdata);
  12347. if (vertices == 0)
  12348. return 0;
  12349. next_move = 0;
  12350. flagcount=0;
  12351. /* in first pass, we load uninterpreted data into the allocated array */
  12352. /* above, shifted to the end of the array so we won't overwrite it when */
  12353. /* we create our final data starting from the front */
  12354. off = m - n; /* starting offset for uninterpreted data, regardless of how m ends up being calculated */
  12355. /* first load flags */
  12356. for (i=0; i < n; ++i) {
  12357. if (flagcount == 0) {
  12358. flags = *points++;
  12359. if (flags & 8)
  12360. flagcount = *points++;
  12361. } else
  12362. --flagcount;
  12363. vertices[off+i].type = flags;
  12364. }
  12365. /* now load x coordinates */
  12366. x=0;
  12367. for (i=0; i < n; ++i) {
  12368. flags = vertices[off+i].type;
  12369. if (flags & 2) {
  12370. stbtt_int16 dx = *points++;
  12371. x += (flags & 16) ? dx : -dx; /* ??? */
  12372. } else {
  12373. if (!(flags & 16)) {
  12374. x = x + (stbtt_int16) (points[0]*256 + points[1]);
  12375. points += 2;
  12376. }
  12377. }
  12378. vertices[off+i].x = (stbtt_int16) x;
  12379. }
  12380. /* now load y coordinates */
  12381. y=0;
  12382. for (i=0; i < n; ++i) {
  12383. flags = vertices[off+i].type;
  12384. if (flags & 4) {
  12385. stbtt_int16 dy = *points++;
  12386. y += (flags & 32) ? dy : -dy; /* ??? */
  12387. } else {
  12388. if (!(flags & 32)) {
  12389. y = y + (stbtt_int16) (points[0]*256 + points[1]);
  12390. points += 2;
  12391. }
  12392. }
  12393. vertices[off+i].y = (stbtt_int16) y;
  12394. }
  12395. /* now convert them to our format */
  12396. num_vertices=0;
  12397. sx = sy = cx = cy = scx = scy = 0;
  12398. for (i=0; i < n; ++i) {
  12399. flags = vertices[off+i].type;
  12400. x = (stbtt_int16) vertices[off+i].x;
  12401. y = (stbtt_int16) vertices[off+i].y;
  12402. if (next_move == i) {
  12403. if (i != 0)
  12404. num_vertices = stbtt__close_shape(vertices, num_vertices, was_off, start_off, sx,sy,scx,scy,cx,cy);
  12405. /* now start the new one */
  12406. start_off = !(flags & 1);
  12407. if (start_off) {
  12408. /* if we start off with an off-curve point, then when we need to find a point on the curve */
  12409. /* where we can start, and we need to save some state for when we wraparound. */
  12410. scx = x;
  12411. scy = y;
  12412. if (!(vertices[off+i+1].type & 1)) {
  12413. /* next point is also a curve point, so interpolate an on-point curve */
  12414. sx = (x + (stbtt_int32) vertices[off+i+1].x) >> 1;
  12415. sy = (y + (stbtt_int32) vertices[off+i+1].y) >> 1;
  12416. } else {
  12417. /* otherwise just use the next point as our start point */
  12418. sx = (stbtt_int32) vertices[off+i+1].x;
  12419. sy = (stbtt_int32) vertices[off+i+1].y;
  12420. ++i; /* we're using point i+1 as the starting point, so skip it */
  12421. }
  12422. } else {
  12423. sx = x;
  12424. sy = y;
  12425. }
  12426. stbtt_setvertex(&vertices[num_vertices++], STBTT_vmove,sx,sy,0,0);
  12427. was_off = 0;
  12428. next_move = 1 + ttUSHORT(endPtsOfContours+j*2);
  12429. ++j;
  12430. } else {
  12431. if (!(flags & 1)) { /* if it's a curve */
  12432. if (was_off) /* two off-curve control points in a row means interpolate an on-curve midpoint */
  12433. stbtt_setvertex(&vertices[num_vertices++], STBTT_vcurve, (cx+x)>>1, (cy+y)>>1, cx, cy);
  12434. cx = x;
  12435. cy = y;
  12436. was_off = 1;
  12437. } else {
  12438. if (was_off)
  12439. stbtt_setvertex(&vertices[num_vertices++], STBTT_vcurve, x,y, cx, cy);
  12440. else
  12441. stbtt_setvertex(&vertices[num_vertices++], STBTT_vline, x,y,0,0);
  12442. was_off = 0;
  12443. }
  12444. }
  12445. }
  12446. num_vertices = stbtt__close_shape(vertices, num_vertices, was_off, start_off, sx,sy,scx,scy,cx,cy);
  12447. } else if (numberOfContours < 0) {
  12448. /* Compound shapes. */
  12449. int more = 1;
  12450. stbtt_uint8 *comp = data + g + 10;
  12451. num_vertices = 0;
  12452. vertices = 0;
  12453. while (more) {
  12454. stbtt_uint16 flags, gidx;
  12455. int comp_num_verts = 0, i;
  12456. stbtt_vertex *comp_verts = 0, *tmp = 0;
  12457. float mtx[6] = {1,0,0,1,0,0}, m, n;
  12458. flags = ttSHORT(comp); comp+=2;
  12459. gidx = ttSHORT(comp); comp+=2;
  12460. if (flags & 2) { /* XY values */
  12461. if (flags & 1) { /* shorts */
  12462. mtx[4] = ttSHORT(comp); comp+=2;
  12463. mtx[5] = ttSHORT(comp); comp+=2;
  12464. } else {
  12465. mtx[4] = ttCHAR(comp); comp+=1;
  12466. mtx[5] = ttCHAR(comp); comp+=1;
  12467. }
  12468. }
  12469. else {
  12470. /* @TODO handle matching point */
  12471. STBTT_assert(0);
  12472. }
  12473. if (flags & (1<<3)) { /* WE_HAVE_A_SCALE */
  12474. mtx[0] = mtx[3] = ttSHORT(comp)/16384.0f; comp+=2;
  12475. mtx[1] = mtx[2] = 0;
  12476. } else if (flags & (1<<6)) { /* WE_HAVE_AN_X_AND_YSCALE */
  12477. mtx[0] = ttSHORT(comp)/16384.0f; comp+=2;
  12478. mtx[1] = mtx[2] = 0;
  12479. mtx[3] = ttSHORT(comp)/16384.0f; comp+=2;
  12480. } else if (flags & (1<<7)) { /* WE_HAVE_A_TWO_BY_TWO */
  12481. mtx[0] = ttSHORT(comp)/16384.0f; comp+=2;
  12482. mtx[1] = ttSHORT(comp)/16384.0f; comp+=2;
  12483. mtx[2] = ttSHORT(comp)/16384.0f; comp+=2;
  12484. mtx[3] = ttSHORT(comp)/16384.0f; comp+=2;
  12485. }
  12486. /* Find transformation scales. */
  12487. m = (float) STBTT_sqrt(mtx[0]*mtx[0] + mtx[1]*mtx[1]);
  12488. n = (float) STBTT_sqrt(mtx[2]*mtx[2] + mtx[3]*mtx[3]);
  12489. /* Get indexed glyph. */
  12490. comp_num_verts = stbtt_GetGlyphShape(info, gidx, &comp_verts);
  12491. if (comp_num_verts > 0) {
  12492. /* Transform vertices. */
  12493. for (i = 0; i < comp_num_verts; ++i) {
  12494. stbtt_vertex* v = &comp_verts[i];
  12495. stbtt_vertex_type x,y;
  12496. x=v->x; y=v->y;
  12497. v->x = (stbtt_vertex_type)(m * (mtx[0]*x + mtx[2]*y + mtx[4]));
  12498. v->y = (stbtt_vertex_type)(n * (mtx[1]*x + mtx[3]*y + mtx[5]));
  12499. x=v->cx; y=v->cy;
  12500. v->cx = (stbtt_vertex_type)(m * (mtx[0]*x + mtx[2]*y + mtx[4]));
  12501. v->cy = (stbtt_vertex_type)(n * (mtx[1]*x + mtx[3]*y + mtx[5]));
  12502. }
  12503. /* Append vertices. */
  12504. tmp = (stbtt_vertex*)STBTT_malloc((num_vertices+comp_num_verts)*sizeof(stbtt_vertex), info->userdata);
  12505. if (!tmp) {
  12506. if (vertices) STBTT_free(vertices, info->userdata);
  12507. if (comp_verts) STBTT_free(comp_verts, info->userdata);
  12508. return 0;
  12509. }
  12510. if (num_vertices > 0 && vertices) STBTT_memcpy(tmp, vertices, num_vertices*sizeof(stbtt_vertex));
  12511. STBTT_memcpy(tmp+num_vertices, comp_verts, comp_num_verts*sizeof(stbtt_vertex));
  12512. if (vertices) STBTT_free(vertices, info->userdata);
  12513. vertices = tmp;
  12514. STBTT_free(comp_verts, info->userdata);
  12515. num_vertices += comp_num_verts;
  12516. }
  12517. /* More components ? */
  12518. more = flags & (1<<5);
  12519. }
  12520. } else {
  12521. /* numberOfCounters == 0, do nothing */
  12522. }
  12523. *pvertices = vertices;
  12524. return num_vertices;
  12525. }
  12526. typedef struct
  12527. {
  12528. int bounds;
  12529. int started;
  12530. float first_x, first_y;
  12531. float x, y;
  12532. stbtt_int32 min_x, max_x, min_y, max_y;
  12533. stbtt_vertex *pvertices;
  12534. int num_vertices;
  12535. } stbtt__csctx;
  12536. #define STBTT__CSCTX_INIT(bounds) {bounds,0, 0,0, 0,0, 0,0,0,0, NULL, 0}
  12537. static void stbtt__track_vertex(stbtt__csctx *c, stbtt_int32 x, stbtt_int32 y)
  12538. {
  12539. if (x > c->max_x || !c->started) c->max_x = x;
  12540. if (y > c->max_y || !c->started) c->max_y = y;
  12541. if (x < c->min_x || !c->started) c->min_x = x;
  12542. if (y < c->min_y || !c->started) c->min_y = y;
  12543. c->started = 1;
  12544. }
  12545. 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)
  12546. {
  12547. if (c->bounds) {
  12548. stbtt__track_vertex(c, x, y);
  12549. if (type == STBTT_vcubic) {
  12550. stbtt__track_vertex(c, cx, cy);
  12551. stbtt__track_vertex(c, cx1, cy1);
  12552. }
  12553. } else {
  12554. stbtt_setvertex(&c->pvertices[c->num_vertices], type, x, y, cx, cy);
  12555. c->pvertices[c->num_vertices].cx1 = (stbtt_int16) cx1;
  12556. c->pvertices[c->num_vertices].cy1 = (stbtt_int16) cy1;
  12557. }
  12558. c->num_vertices++;
  12559. }
  12560. static void stbtt__csctx_close_shape(stbtt__csctx *ctx)
  12561. {
  12562. if (ctx->first_x != ctx->x || ctx->first_y != ctx->y)
  12563. stbtt__csctx_v(ctx, STBTT_vline, (int)ctx->first_x, (int)ctx->first_y, 0, 0, 0, 0);
  12564. }
  12565. static void stbtt__csctx_rmove_to(stbtt__csctx *ctx, float dx, float dy)
  12566. {
  12567. stbtt__csctx_close_shape(ctx);
  12568. ctx->first_x = ctx->x = ctx->x + dx;
  12569. ctx->first_y = ctx->y = ctx->y + dy;
  12570. stbtt__csctx_v(ctx, STBTT_vmove, (int)ctx->x, (int)ctx->y, 0, 0, 0, 0);
  12571. }
  12572. static void stbtt__csctx_rline_to(stbtt__csctx *ctx, float dx, float dy)
  12573. {
  12574. ctx->x += dx;
  12575. ctx->y += dy;
  12576. stbtt__csctx_v(ctx, STBTT_vline, (int)ctx->x, (int)ctx->y, 0, 0, 0, 0);
  12577. }
  12578. static void stbtt__csctx_rccurve_to(stbtt__csctx *ctx, float dx1, float dy1, float dx2, float dy2, float dx3, float dy3)
  12579. {
  12580. float cx1 = ctx->x + dx1;
  12581. float cy1 = ctx->y + dy1;
  12582. float cx2 = cx1 + dx2;
  12583. float cy2 = cy1 + dy2;
  12584. ctx->x = cx2 + dx3;
  12585. ctx->y = cy2 + dy3;
  12586. stbtt__csctx_v(ctx, STBTT_vcubic, (int)ctx->x, (int)ctx->y, (int)cx1, (int)cy1, (int)cx2, (int)cy2);
  12587. }
  12588. static stbtt__buf stbtt__get_subr(stbtt__buf idx, int n)
  12589. {
  12590. int count = stbtt__cff_index_count(&idx);
  12591. int bias = 107;
  12592. if (count >= 33900)
  12593. bias = 32768;
  12594. else if (count >= 1240)
  12595. bias = 1131;
  12596. n += bias;
  12597. if (n < 0 || n >= count)
  12598. return stbtt__new_buf(NULL, 0);
  12599. return stbtt__cff_index_get(idx, n);
  12600. }
  12601. static stbtt__buf stbtt__cid_get_glyph_subrs(const stbtt_fontinfo *info, int glyph_index)
  12602. {
  12603. stbtt__buf fdselect = info->fdselect;
  12604. int nranges, start, end, v, fmt, fdselector = -1, i;
  12605. stbtt__buf_seek(&fdselect, 0);
  12606. fmt = stbtt__buf_get8(&fdselect);
  12607. if (fmt == 0) {
  12608. /* untested */
  12609. stbtt__buf_skip(&fdselect, glyph_index);
  12610. fdselector = stbtt__buf_get8(&fdselect);
  12611. } else if (fmt == 3) {
  12612. nranges = stbtt__buf_get16(&fdselect);
  12613. start = stbtt__buf_get16(&fdselect);
  12614. for (i = 0; i < nranges; i++) {
  12615. v = stbtt__buf_get8(&fdselect);
  12616. end = stbtt__buf_get16(&fdselect);
  12617. if (glyph_index >= start && glyph_index < end) {
  12618. fdselector = v;
  12619. break;
  12620. }
  12621. start = end;
  12622. }
  12623. }
  12624. if (fdselector == -1) stbtt__new_buf(NULL, 0);
  12625. return stbtt__get_subrs(info->cff, stbtt__cff_index_get(info->fontdicts, fdselector));
  12626. }
  12627. static int stbtt__run_charstring(const stbtt_fontinfo *info, int glyph_index, stbtt__csctx *c)
  12628. {
  12629. int in_header = 1, maskbits = 0, subr_stack_height = 0, sp = 0, v, i, b0;
  12630. int has_subrs = 0, clear_stack;
  12631. float s[48];
  12632. stbtt__buf subr_stack[10], subrs = info->subrs, b;
  12633. float f;
  12634. #define STBTT__CSERR(s) (0)
  12635. /* this currently ignores the initial width value, which isn't needed if we have hmtx */
  12636. b = stbtt__cff_index_get(info->charstrings, glyph_index);
  12637. while (b.cursor < b.size) {
  12638. i = 0;
  12639. clear_stack = 1;
  12640. b0 = stbtt__buf_get8(&b);
  12641. switch (b0) {
  12642. /* @TODO implement hinting */
  12643. case 0x13: /* hintmask */
  12644. case 0x14: /* cntrmask */
  12645. if (in_header)
  12646. maskbits += (sp / 2); /* implicit "vstem" */
  12647. in_header = 0;
  12648. stbtt__buf_skip(&b, (maskbits + 7) / 8);
  12649. break;
  12650. case 0x01: /* hstem */
  12651. case 0x03: /* vstem */
  12652. case 0x12: /* hstemhm */
  12653. case 0x17: /* vstemhm */
  12654. maskbits += (sp / 2);
  12655. break;
  12656. case 0x15: /* rmoveto */
  12657. in_header = 0;
  12658. if (sp < 2) return STBTT__CSERR("rmoveto stack");
  12659. stbtt__csctx_rmove_to(c, s[sp-2], s[sp-1]);
  12660. break;
  12661. case 0x04: /* vmoveto */
  12662. in_header = 0;
  12663. if (sp < 1) return STBTT__CSERR("vmoveto stack");
  12664. stbtt__csctx_rmove_to(c, 0, s[sp-1]);
  12665. break;
  12666. case 0x16: /* hmoveto */
  12667. in_header = 0;
  12668. if (sp < 1) return STBTT__CSERR("hmoveto stack");
  12669. stbtt__csctx_rmove_to(c, s[sp-1], 0);
  12670. break;
  12671. case 0x05: /* rlineto */
  12672. if (sp < 2) return STBTT__CSERR("rlineto stack");
  12673. for (; i + 1 < sp; i += 2)
  12674. stbtt__csctx_rline_to(c, s[i], s[i+1]);
  12675. break;
  12676. /* hlineto/vlineto and vhcurveto/hvcurveto alternate horizontal and vertical */
  12677. /* starting from a different place. */
  12678. case 0x07: /* vlineto */
  12679. if (sp < 1) return STBTT__CSERR("vlineto stack");
  12680. goto vlineto;
  12681. case 0x06: /* hlineto */
  12682. if (sp < 1) return STBTT__CSERR("hlineto stack");
  12683. for (;;) {
  12684. if (i >= sp) break;
  12685. stbtt__csctx_rline_to(c, s[i], 0);
  12686. i++;
  12687. vlineto:
  12688. if (i >= sp) break;
  12689. stbtt__csctx_rline_to(c, 0, s[i]);
  12690. i++;
  12691. }
  12692. break;
  12693. case 0x1F: /* hvcurveto */
  12694. if (sp < 4) return STBTT__CSERR("hvcurveto stack");
  12695. goto hvcurveto;
  12696. case 0x1E: /* vhcurveto */
  12697. if (sp < 4) return STBTT__CSERR("vhcurveto stack");
  12698. for (;;) {
  12699. if (i + 3 >= sp) break;
  12700. 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);
  12701. i += 4;
  12702. hvcurveto:
  12703. if (i + 3 >= sp) break;
  12704. 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]);
  12705. i += 4;
  12706. }
  12707. break;
  12708. case 0x08: /* rrcurveto */
  12709. if (sp < 6) return STBTT__CSERR("rcurveline stack");
  12710. for (; i + 5 < sp; i += 6)
  12711. stbtt__csctx_rccurve_to(c, s[i], s[i+1], s[i+2], s[i+3], s[i+4], s[i+5]);
  12712. break;
  12713. case 0x18: /* rcurveline */
  12714. if (sp < 8) return STBTT__CSERR("rcurveline stack");
  12715. for (; i + 5 < sp - 2; i += 6)
  12716. stbtt__csctx_rccurve_to(c, s[i], s[i+1], s[i+2], s[i+3], s[i+4], s[i+5]);
  12717. if (i + 1 >= sp) return STBTT__CSERR("rcurveline stack");
  12718. stbtt__csctx_rline_to(c, s[i], s[i+1]);
  12719. break;
  12720. case 0x19: /* rlinecurve */
  12721. if (sp < 8) return STBTT__CSERR("rlinecurve stack");
  12722. for (; i + 1 < sp - 6; i += 2)
  12723. stbtt__csctx_rline_to(c, s[i], s[i+1]);
  12724. if (i + 5 >= sp) return STBTT__CSERR("rlinecurve stack");
  12725. stbtt__csctx_rccurve_to(c, s[i], s[i+1], s[i+2], s[i+3], s[i+4], s[i+5]);
  12726. break;
  12727. case 0x1A: /* vvcurveto */
  12728. case 0x1B: /* hhcurveto */
  12729. if (sp < 4) return STBTT__CSERR("(vv|hh)curveto stack");
  12730. f = 0.0;
  12731. if (sp & 1) { f = s[i]; i++; }
  12732. for (; i + 3 < sp; i += 4) {
  12733. if (b0 == 0x1B)
  12734. stbtt__csctx_rccurve_to(c, s[i], f, s[i+1], s[i+2], s[i+3], 0.0);
  12735. else
  12736. stbtt__csctx_rccurve_to(c, f, s[i], s[i+1], s[i+2], 0.0, s[i+3]);
  12737. f = 0.0;
  12738. }
  12739. break;
  12740. case 0x0A: /* callsubr */
  12741. if (!has_subrs) {
  12742. if (info->fdselect.size)
  12743. subrs = stbtt__cid_get_glyph_subrs(info, glyph_index);
  12744. has_subrs = 1;
  12745. }
  12746. /* FALLTHROUGH */
  12747. case 0x1D: /* callgsubr */
  12748. if (sp < 1) return STBTT__CSERR("call(g|)subr stack");
  12749. v = (int) s[--sp];
  12750. if (subr_stack_height >= 10) return STBTT__CSERR("recursion limit");
  12751. subr_stack[subr_stack_height++] = b;
  12752. b = stbtt__get_subr(b0 == 0x0A ? subrs : info->gsubrs, v);
  12753. if (b.size == 0) return STBTT__CSERR("subr not found");
  12754. b.cursor = 0;
  12755. clear_stack = 0;
  12756. break;
  12757. case 0x0B: /* return */
  12758. if (subr_stack_height <= 0) return STBTT__CSERR("return outside subr");
  12759. b = subr_stack[--subr_stack_height];
  12760. clear_stack = 0;
  12761. break;
  12762. case 0x0E: /* endchar */
  12763. stbtt__csctx_close_shape(c);
  12764. return 1;
  12765. case 0x0C: { /* two-byte escape */
  12766. float dx1, dx2, dx3, dx4, dx5, dx6, dy1, dy2, dy3, dy4, dy5, dy6;
  12767. float dx, dy;
  12768. int b1 = stbtt__buf_get8(&b);
  12769. switch (b1) {
  12770. /* @TODO These "flex" implementations ignore the flex-depth and resolution, */
  12771. /* and always draw beziers. */
  12772. case 0x22: /* hflex */
  12773. if (sp < 7) return STBTT__CSERR("hflex stack");
  12774. dx1 = s[0];
  12775. dx2 = s[1];
  12776. dy2 = s[2];
  12777. dx3 = s[3];
  12778. dx4 = s[4];
  12779. dx5 = s[5];
  12780. dx6 = s[6];
  12781. stbtt__csctx_rccurve_to(c, dx1, 0, dx2, dy2, dx3, 0);
  12782. stbtt__csctx_rccurve_to(c, dx4, 0, dx5, -dy2, dx6, 0);
  12783. break;
  12784. case 0x23: /* flex */
  12785. if (sp < 13) return STBTT__CSERR("flex stack");
  12786. dx1 = s[0];
  12787. dy1 = s[1];
  12788. dx2 = s[2];
  12789. dy2 = s[3];
  12790. dx3 = s[4];
  12791. dy3 = s[5];
  12792. dx4 = s[6];
  12793. dy4 = s[7];
  12794. dx5 = s[8];
  12795. dy5 = s[9];
  12796. dx6 = s[10];
  12797. dy6 = s[11];
  12798. /* fd is s[12] */
  12799. stbtt__csctx_rccurve_to(c, dx1, dy1, dx2, dy2, dx3, dy3);
  12800. stbtt__csctx_rccurve_to(c, dx4, dy4, dx5, dy5, dx6, dy6);
  12801. break;
  12802. case 0x24: /* hflex1 */
  12803. if (sp < 9) return STBTT__CSERR("hflex1 stack");
  12804. dx1 = s[0];
  12805. dy1 = s[1];
  12806. dx2 = s[2];
  12807. dy2 = s[3];
  12808. dx3 = s[4];
  12809. dx4 = s[5];
  12810. dx5 = s[6];
  12811. dy5 = s[7];
  12812. dx6 = s[8];
  12813. stbtt__csctx_rccurve_to(c, dx1, dy1, dx2, dy2, dx3, 0);
  12814. stbtt__csctx_rccurve_to(c, dx4, 0, dx5, dy5, dx6, -(dy1+dy2+dy5));
  12815. break;
  12816. case 0x25: /* flex1 */
  12817. if (sp < 11) return STBTT__CSERR("flex1 stack");
  12818. dx1 = s[0];
  12819. dy1 = s[1];
  12820. dx2 = s[2];
  12821. dy2 = s[3];
  12822. dx3 = s[4];
  12823. dy3 = s[5];
  12824. dx4 = s[6];
  12825. dy4 = s[7];
  12826. dx5 = s[8];
  12827. dy5 = s[9];
  12828. dx6 = dy6 = s[10];
  12829. dx = dx1+dx2+dx3+dx4+dx5;
  12830. dy = dy1+dy2+dy3+dy4+dy5;
  12831. if (STBTT_fabs(dx) > STBTT_fabs(dy))
  12832. dy6 = -dy;
  12833. else
  12834. dx6 = -dx;
  12835. stbtt__csctx_rccurve_to(c, dx1, dy1, dx2, dy2, dx3, dy3);
  12836. stbtt__csctx_rccurve_to(c, dx4, dy4, dx5, dy5, dx6, dy6);
  12837. break;
  12838. default:
  12839. return STBTT__CSERR("unimplemented");
  12840. }
  12841. } break;
  12842. default:
  12843. if (b0 != 255 && b0 != 28 && b0 < 32)
  12844. return STBTT__CSERR("reserved operator");
  12845. /* push immediate */
  12846. if (b0 == 255) {
  12847. f = (float)(stbtt_int32)stbtt__buf_get32(&b) / 0x10000;
  12848. } else {
  12849. stbtt__buf_skip(&b, -1);
  12850. f = (float)(stbtt_int16)stbtt__cff_int(&b);
  12851. }
  12852. if (sp >= 48) return STBTT__CSERR("push stack overflow");
  12853. s[sp++] = f;
  12854. clear_stack = 0;
  12855. break;
  12856. }
  12857. if (clear_stack) sp = 0;
  12858. }
  12859. return STBTT__CSERR("no endchar");
  12860. #undef STBTT__CSERR
  12861. }
  12862. static int stbtt__GetGlyphShapeT2(const stbtt_fontinfo *info, int glyph_index, stbtt_vertex **pvertices)
  12863. {
  12864. /* runs the charstring twice, once to count and once to output (to avoid realloc) */
  12865. stbtt__csctx count_ctx = STBTT__CSCTX_INIT(1);
  12866. stbtt__csctx output_ctx = STBTT__CSCTX_INIT(0);
  12867. if (stbtt__run_charstring(info, glyph_index, &count_ctx)) {
  12868. *pvertices = (stbtt_vertex*)STBTT_malloc(count_ctx.num_vertices*sizeof(stbtt_vertex), info->userdata);
  12869. output_ctx.pvertices = *pvertices;
  12870. if (stbtt__run_charstring(info, glyph_index, &output_ctx)) {
  12871. STBTT_assert(output_ctx.num_vertices == count_ctx.num_vertices);
  12872. return output_ctx.num_vertices;
  12873. }
  12874. }
  12875. *pvertices = NULL;
  12876. return 0;
  12877. }
  12878. static int stbtt__GetGlyphInfoT2(const stbtt_fontinfo *info, int glyph_index, int *x0, int *y0, int *x1, int *y1)
  12879. {
  12880. stbtt__csctx c = STBTT__CSCTX_INIT(1);
  12881. int r = stbtt__run_charstring(info, glyph_index, &c);
  12882. if (x0) *x0 = r ? c.min_x : 0;
  12883. if (y0) *y0 = r ? c.min_y : 0;
  12884. if (x1) *x1 = r ? c.max_x : 0;
  12885. if (y1) *y1 = r ? c.max_y : 0;
  12886. return r ? c.num_vertices : 0;
  12887. }
  12888. STBTT_DEF int stbtt_GetGlyphShape(const stbtt_fontinfo *info, int glyph_index, stbtt_vertex **pvertices)
  12889. {
  12890. if (!info->cff.size)
  12891. return stbtt__GetGlyphShapeTT(info, glyph_index, pvertices);
  12892. else
  12893. return stbtt__GetGlyphShapeT2(info, glyph_index, pvertices);
  12894. }
  12895. STBTT_DEF void stbtt_GetGlyphHMetrics(const stbtt_fontinfo *info, int glyph_index, int *advanceWidth, int *leftSideBearing)
  12896. {
  12897. stbtt_uint16 numOfLongHorMetrics = ttUSHORT(info->data+info->hhea + 34);
  12898. if (glyph_index < numOfLongHorMetrics) {
  12899. if (advanceWidth) *advanceWidth = ttSHORT(info->data + info->hmtx + 4*glyph_index);
  12900. if (leftSideBearing) *leftSideBearing = ttSHORT(info->data + info->hmtx + 4*glyph_index + 2);
  12901. } else {
  12902. if (advanceWidth) *advanceWidth = ttSHORT(info->data + info->hmtx + 4*(numOfLongHorMetrics-1));
  12903. if (leftSideBearing) *leftSideBearing = ttSHORT(info->data + info->hmtx + 4*numOfLongHorMetrics + 2*(glyph_index - numOfLongHorMetrics));
  12904. }
  12905. }
  12906. STBTT_DEF int stbtt_GetKerningTableLength(const stbtt_fontinfo *info)
  12907. {
  12908. stbtt_uint8 *data = info->data + info->kern;
  12909. /* we only look at the first table. it must be 'horizontal' and format 0. */
  12910. if (!info->kern)
  12911. return 0;
  12912. if (ttUSHORT(data+2) < 1) /* number of tables, need at least 1 */
  12913. return 0;
  12914. if (ttUSHORT(data+8) != 1) /* horizontal flag must be set in format */
  12915. return 0;
  12916. return ttUSHORT(data+10);
  12917. }
  12918. STBTT_DEF int stbtt_GetKerningTable(const stbtt_fontinfo *info, stbtt_kerningentry* table, int table_length)
  12919. {
  12920. stbtt_uint8 *data = info->data + info->kern;
  12921. int k, length;
  12922. /* we only look at the first table. it must be 'horizontal' and format 0. */
  12923. if (!info->kern)
  12924. return 0;
  12925. if (ttUSHORT(data+2) < 1) /* number of tables, need at least 1 */
  12926. return 0;
  12927. if (ttUSHORT(data+8) != 1) /* horizontal flag must be set in format */
  12928. return 0;
  12929. length = ttUSHORT(data+10);
  12930. if (table_length < length)
  12931. length = table_length;
  12932. for (k = 0; k < length; k++)
  12933. {
  12934. table[k].glyph1 = ttUSHORT(data+18+(k*6));
  12935. table[k].glyph2 = ttUSHORT(data+20+(k*6));
  12936. table[k].advance = ttSHORT(data+22+(k*6));
  12937. }
  12938. return length;
  12939. }
  12940. static int stbtt__GetGlyphKernInfoAdvance(const stbtt_fontinfo *info, int glyph1, int glyph2)
  12941. {
  12942. stbtt_uint8 *data = info->data + info->kern;
  12943. stbtt_uint32 needle, straw;
  12944. int l, r, m;
  12945. /* we only look at the first table. it must be 'horizontal' and format 0. */
  12946. if (!info->kern)
  12947. return 0;
  12948. if (ttUSHORT(data+2) < 1) /* number of tables, need at least 1 */
  12949. return 0;
  12950. if (ttUSHORT(data+8) != 1) /* horizontal flag must be set in format */
  12951. return 0;
  12952. l = 0;
  12953. r = ttUSHORT(data+10) - 1;
  12954. needle = glyph1 << 16 | glyph2;
  12955. while (l <= r) {
  12956. m = (l + r) >> 1;
  12957. straw = ttULONG(data+18+(m*6)); /* note: unaligned read */
  12958. if (needle < straw)
  12959. r = m - 1;
  12960. else if (needle > straw)
  12961. l = m + 1;
  12962. else
  12963. return ttSHORT(data+22+(m*6));
  12964. }
  12965. return 0;
  12966. }
  12967. static stbtt_int32 stbtt__GetCoverageIndex(stbtt_uint8 *coverageTable, int glyph)
  12968. {
  12969. stbtt_uint16 coverageFormat = ttUSHORT(coverageTable);
  12970. switch (coverageFormat) {
  12971. case 1: {
  12972. stbtt_uint16 glyphCount = ttUSHORT(coverageTable + 2);
  12973. /* Binary search. */
  12974. stbtt_int32 l=0, r=glyphCount-1, m;
  12975. int straw, needle=glyph;
  12976. while (l <= r) {
  12977. stbtt_uint8 *glyphArray = coverageTable + 4;
  12978. stbtt_uint16 glyphID;
  12979. m = (l + r) >> 1;
  12980. glyphID = ttUSHORT(glyphArray + 2 * m);
  12981. straw = glyphID;
  12982. if (needle < straw)
  12983. r = m - 1;
  12984. else if (needle > straw)
  12985. l = m + 1;
  12986. else {
  12987. return m;
  12988. }
  12989. }
  12990. break;
  12991. }
  12992. case 2: {
  12993. stbtt_uint16 rangeCount = ttUSHORT(coverageTable + 2);
  12994. stbtt_uint8 *rangeArray = coverageTable + 4;
  12995. /* Binary search. */
  12996. stbtt_int32 l=0, r=rangeCount-1, m;
  12997. int strawStart, strawEnd, needle=glyph;
  12998. while (l <= r) {
  12999. stbtt_uint8 *rangeRecord;
  13000. m = (l + r) >> 1;
  13001. rangeRecord = rangeArray + 6 * m;
  13002. strawStart = ttUSHORT(rangeRecord);
  13003. strawEnd = ttUSHORT(rangeRecord + 2);
  13004. if (needle < strawStart)
  13005. r = m - 1;
  13006. else if (needle > strawEnd)
  13007. l = m + 1;
  13008. else {
  13009. stbtt_uint16 startCoverageIndex = ttUSHORT(rangeRecord + 4);
  13010. return startCoverageIndex + glyph - strawStart;
  13011. }
  13012. }
  13013. break;
  13014. }
  13015. default: return -1; /* unsupported */
  13016. }
  13017. return -1;
  13018. }
  13019. static stbtt_int32 stbtt__GetGlyphClass(stbtt_uint8 *classDefTable, int glyph)
  13020. {
  13021. stbtt_uint16 classDefFormat = ttUSHORT(classDefTable);
  13022. switch (classDefFormat)
  13023. {
  13024. case 1: {
  13025. stbtt_uint16 startGlyphID = ttUSHORT(classDefTable + 2);
  13026. stbtt_uint16 glyphCount = ttUSHORT(classDefTable + 4);
  13027. stbtt_uint8 *classDef1ValueArray = classDefTable + 6;
  13028. if (glyph >= startGlyphID && glyph < startGlyphID + glyphCount)
  13029. return (stbtt_int32)ttUSHORT(classDef1ValueArray + 2 * (glyph - startGlyphID));
  13030. break;
  13031. }
  13032. case 2: {
  13033. stbtt_uint16 classRangeCount = ttUSHORT(classDefTable + 2);
  13034. stbtt_uint8 *classRangeRecords = classDefTable + 4;
  13035. /* Binary search. */
  13036. stbtt_int32 l=0, r=classRangeCount-1, m;
  13037. int strawStart, strawEnd, needle=glyph;
  13038. while (l <= r) {
  13039. stbtt_uint8 *classRangeRecord;
  13040. m = (l + r) >> 1;
  13041. classRangeRecord = classRangeRecords + 6 * m;
  13042. strawStart = ttUSHORT(classRangeRecord);
  13043. strawEnd = ttUSHORT(classRangeRecord + 2);
  13044. if (needle < strawStart)
  13045. r = m - 1;
  13046. else if (needle > strawEnd)
  13047. l = m + 1;
  13048. else
  13049. return (stbtt_int32)ttUSHORT(classRangeRecord + 4);
  13050. }
  13051. break;
  13052. }
  13053. default:
  13054. return -1; /* Unsupported definition type, return an error. */
  13055. }
  13056. /* "All glyphs not assigned to a class fall into class 0". (OpenType spec) */
  13057. return 0;
  13058. }
  13059. /* Define to STBTT_assert(x) if you want to break on unimplemented formats. */
  13060. #define STBTT_GPOS_TODO_assert(x)
  13061. static stbtt_int32 stbtt__GetGlyphGPOSInfoAdvance(const stbtt_fontinfo *info, int glyph1, int glyph2)
  13062. {
  13063. stbtt_uint16 lookupListOffset;
  13064. stbtt_uint8 *lookupList;
  13065. stbtt_uint16 lookupCount;
  13066. stbtt_uint8 *data;
  13067. stbtt_int32 i, sti;
  13068. if (!info->gpos) return 0;
  13069. data = info->data + info->gpos;
  13070. if (ttUSHORT(data+0) != 1) return 0; /* Major version 1 */
  13071. if (ttUSHORT(data+2) != 0) return 0; /* Minor version 0 */
  13072. lookupListOffset = ttUSHORT(data+8);
  13073. lookupList = data + lookupListOffset;
  13074. lookupCount = ttUSHORT(lookupList);
  13075. for (i=0; i<lookupCount; ++i) {
  13076. stbtt_uint16 lookupOffset = ttUSHORT(lookupList + 2 + 2 * i);
  13077. stbtt_uint8 *lookupTable = lookupList + lookupOffset;
  13078. stbtt_uint16 lookupType = ttUSHORT(lookupTable);
  13079. stbtt_uint16 subTableCount = ttUSHORT(lookupTable + 4);
  13080. stbtt_uint8 *subTableOffsets = lookupTable + 6;
  13081. if (lookupType != 2) /* Pair Adjustment Positioning Subtable */
  13082. continue;
  13083. for (sti=0; sti<subTableCount; sti++) {
  13084. stbtt_uint16 subtableOffset = ttUSHORT(subTableOffsets + 2 * sti);
  13085. stbtt_uint8 *table = lookupTable + subtableOffset;
  13086. stbtt_uint16 posFormat = ttUSHORT(table);
  13087. stbtt_uint16 coverageOffset = ttUSHORT(table + 2);
  13088. stbtt_int32 coverageIndex = stbtt__GetCoverageIndex(table + coverageOffset, glyph1);
  13089. if (coverageIndex == -1) continue;
  13090. switch (posFormat) {
  13091. case 1: {
  13092. stbtt_int32 l, r, m;
  13093. int straw, needle;
  13094. stbtt_uint16 valueFormat1 = ttUSHORT(table + 4);
  13095. stbtt_uint16 valueFormat2 = ttUSHORT(table + 6);
  13096. if (valueFormat1 == 4 && valueFormat2 == 0) { /* Support more formats? */
  13097. stbtt_int32 valueRecordPairSizeInBytes = 2;
  13098. stbtt_uint16 pairSetCount = ttUSHORT(table + 8);
  13099. stbtt_uint16 pairPosOffset = ttUSHORT(table + 10 + 2 * coverageIndex);
  13100. stbtt_uint8 *pairValueTable = table + pairPosOffset;
  13101. stbtt_uint16 pairValueCount = ttUSHORT(pairValueTable);
  13102. stbtt_uint8 *pairValueArray = pairValueTable + 2;
  13103. if (coverageIndex >= pairSetCount) return 0;
  13104. needle=glyph2;
  13105. r=pairValueCount-1;
  13106. l=0;
  13107. /* Binary search. */
  13108. while (l <= r) {
  13109. stbtt_uint16 secondGlyph;
  13110. stbtt_uint8 *pairValue;
  13111. m = (l + r) >> 1;
  13112. pairValue = pairValueArray + (2 + valueRecordPairSizeInBytes) * m;
  13113. secondGlyph = ttUSHORT(pairValue);
  13114. straw = secondGlyph;
  13115. if (needle < straw)
  13116. r = m - 1;
  13117. else if (needle > straw)
  13118. l = m + 1;
  13119. else {
  13120. stbtt_int16 xAdvance = ttSHORT(pairValue + 2);
  13121. return xAdvance;
  13122. }
  13123. }
  13124. } else
  13125. return 0;
  13126. break;
  13127. }
  13128. case 2: {
  13129. stbtt_uint16 valueFormat1 = ttUSHORT(table + 4);
  13130. stbtt_uint16 valueFormat2 = ttUSHORT(table + 6);
  13131. if (valueFormat1 == 4 && valueFormat2 == 0) { /* Support more formats? */
  13132. stbtt_uint16 classDef1Offset = ttUSHORT(table + 8);
  13133. stbtt_uint16 classDef2Offset = ttUSHORT(table + 10);
  13134. int glyph1class = stbtt__GetGlyphClass(table + classDef1Offset, glyph1);
  13135. int glyph2class = stbtt__GetGlyphClass(table + classDef2Offset, glyph2);
  13136. stbtt_uint16 class1Count = ttUSHORT(table + 12);
  13137. stbtt_uint16 class2Count = ttUSHORT(table + 14);
  13138. stbtt_uint8 *class1Records, *class2Records;
  13139. stbtt_int16 xAdvance;
  13140. if (glyph1class < 0 || glyph1class >= class1Count) return 0; /* malformed */
  13141. if (glyph2class < 0 || glyph2class >= class2Count) return 0; /* malformed */
  13142. class1Records = table + 16;
  13143. class2Records = class1Records + 2 * (glyph1class * class2Count);
  13144. xAdvance = ttSHORT(class2Records + 2 * glyph2class);
  13145. return xAdvance;
  13146. } else
  13147. return 0;
  13148. break;
  13149. }
  13150. default:
  13151. return 0; /* Unsupported position format */
  13152. }
  13153. }
  13154. }
  13155. return 0;
  13156. }
  13157. STBTT_DEF int stbtt_GetGlyphKernAdvance(const stbtt_fontinfo *info, int g1, int g2)
  13158. {
  13159. int xAdvance = 0;
  13160. if (info->gpos)
  13161. xAdvance += stbtt__GetGlyphGPOSInfoAdvance(info, g1, g2);
  13162. else if (info->kern)
  13163. xAdvance += stbtt__GetGlyphKernInfoAdvance(info, g1, g2);
  13164. return xAdvance;
  13165. }
  13166. STBTT_DEF int stbtt_GetCodepointKernAdvance(const stbtt_fontinfo *info, int ch1, int ch2)
  13167. {
  13168. if (!info->kern && !info->gpos) /* if no kerning table, don't waste time looking up both codepoint->glyphs */
  13169. return 0;
  13170. return stbtt_GetGlyphKernAdvance(info, stbtt_FindGlyphIndex(info,ch1), stbtt_FindGlyphIndex(info,ch2));
  13171. }
  13172. STBTT_DEF void stbtt_GetCodepointHMetrics(const stbtt_fontinfo *info, int codepoint, int *advanceWidth, int *leftSideBearing)
  13173. {
  13174. stbtt_GetGlyphHMetrics(info, stbtt_FindGlyphIndex(info,codepoint), advanceWidth, leftSideBearing);
  13175. }
  13176. STBTT_DEF void stbtt_GetFontVMetrics(const stbtt_fontinfo *info, int *ascent, int *descent, int *lineGap)
  13177. {
  13178. if (ascent ) *ascent = ttSHORT(info->data+info->hhea + 4);
  13179. if (descent) *descent = ttSHORT(info->data+info->hhea + 6);
  13180. if (lineGap) *lineGap = ttSHORT(info->data+info->hhea + 8);
  13181. }
  13182. STBTT_DEF int stbtt_GetFontVMetricsOS2(const stbtt_fontinfo *info, int *typoAscent, int *typoDescent, int *typoLineGap)
  13183. {
  13184. int tab = stbtt__find_table(info->data, info->fontstart, "OS/2");
  13185. if (!tab)
  13186. return 0;
  13187. if (typoAscent ) *typoAscent = ttSHORT(info->data+tab + 68);
  13188. if (typoDescent) *typoDescent = ttSHORT(info->data+tab + 70);
  13189. if (typoLineGap) *typoLineGap = ttSHORT(info->data+tab + 72);
  13190. return 1;
  13191. }
  13192. STBTT_DEF void stbtt_GetFontBoundingBox(const stbtt_fontinfo *info, int *x0, int *y0, int *x1, int *y1)
  13193. {
  13194. *x0 = ttSHORT(info->data + info->head + 36);
  13195. *y0 = ttSHORT(info->data + info->head + 38);
  13196. *x1 = ttSHORT(info->data + info->head + 40);
  13197. *y1 = ttSHORT(info->data + info->head + 42);
  13198. }
  13199. STBTT_DEF float stbtt_ScaleForPixelHeight(const stbtt_fontinfo *info, float height)
  13200. {
  13201. int fheight = ttSHORT(info->data + info->hhea + 4) - ttSHORT(info->data + info->hhea + 6);
  13202. return (float) height / fheight;
  13203. }
  13204. STBTT_DEF float stbtt_ScaleForMappingEmToPixels(const stbtt_fontinfo *info, float pixels)
  13205. {
  13206. int unitsPerEm = ttUSHORT(info->data + info->head + 18);
  13207. return pixels / unitsPerEm;
  13208. }
  13209. STBTT_DEF void stbtt_FreeShape(const stbtt_fontinfo *info, stbtt_vertex *v)
  13210. {
  13211. STBTT_free(v, info->userdata);
  13212. }
  13213. STBTT_DEF stbtt_uint8 *stbtt_FindSVGDoc(const stbtt_fontinfo *info, int gl)
  13214. {
  13215. int i;
  13216. stbtt_uint8 *data = info->data;
  13217. stbtt_uint8 *svg_doc_list = data + stbtt__get_svg((stbtt_fontinfo *) info);
  13218. int numEntries = ttUSHORT(svg_doc_list);
  13219. stbtt_uint8 *svg_docs = svg_doc_list + 2;
  13220. for(i=0; i<numEntries; i++) {
  13221. stbtt_uint8 *svg_doc = svg_docs + (12 * i);
  13222. if ((gl >= ttUSHORT(svg_doc)) && (gl <= ttUSHORT(svg_doc + 2)))
  13223. return svg_doc;
  13224. }
  13225. return 0;
  13226. }
  13227. STBTT_DEF int stbtt_GetGlyphSVG(const stbtt_fontinfo *info, int gl, const char **svg)
  13228. {
  13229. stbtt_uint8 *data = info->data;
  13230. stbtt_uint8 *svg_doc;
  13231. if (info->svg == 0)
  13232. return 0;
  13233. svg_doc = stbtt_FindSVGDoc(info, gl);
  13234. if (svg_doc != NULL) {
  13235. *svg = (char *) data + info->svg + ttULONG(svg_doc + 4);
  13236. return ttULONG(svg_doc + 8);
  13237. } else {
  13238. return 0;
  13239. }
  13240. }
  13241. STBTT_DEF int stbtt_GetCodepointSVG(const stbtt_fontinfo *info, int unicode_codepoint, const char **svg)
  13242. {
  13243. return stbtt_GetGlyphSVG(info, stbtt_FindGlyphIndex(info, unicode_codepoint), svg);
  13244. }
  13245. /* //////////////////////////////////////////////////////////////////////////// */
  13246. /* */
  13247. /* antialiasing software rasterizer */
  13248. /* */
  13249. 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)
  13250. {
  13251. int x0=0,y0=0,x1,y1; /* =0 suppresses compiler warning */
  13252. if (!stbtt_GetGlyphBox(font, glyph, &x0,&y0,&x1,&y1)) {
  13253. /* e.g. space character */
  13254. if (ix0) *ix0 = 0;
  13255. if (iy0) *iy0 = 0;
  13256. if (ix1) *ix1 = 0;
  13257. if (iy1) *iy1 = 0;
  13258. } else {
  13259. /* move to integral bboxes (treating pixels as little squares, what pixels get touched)? */
  13260. if (ix0) *ix0 = STBTT_ifloor( x0 * scale_x + shift_x);
  13261. if (iy0) *iy0 = STBTT_ifloor(-y1 * scale_y + shift_y);
  13262. if (ix1) *ix1 = STBTT_iceil ( x1 * scale_x + shift_x);
  13263. if (iy1) *iy1 = STBTT_iceil (-y0 * scale_y + shift_y);
  13264. }
  13265. }
  13266. 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)
  13267. {
  13268. stbtt_GetGlyphBitmapBoxSubpixel(font, glyph, scale_x, scale_y,0.0f,0.0f, ix0, iy0, ix1, iy1);
  13269. }
  13270. 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)
  13271. {
  13272. stbtt_GetGlyphBitmapBoxSubpixel(font, stbtt_FindGlyphIndex(font,codepoint), scale_x, scale_y,shift_x,shift_y, ix0,iy0,ix1,iy1);
  13273. }
  13274. 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)
  13275. {
  13276. stbtt_GetCodepointBitmapBoxSubpixel(font, codepoint, scale_x, scale_y,0.0f,0.0f, ix0,iy0,ix1,iy1);
  13277. }
  13278. /* //////////////////////////////////////////////////////////////////////////// */
  13279. /* */
  13280. /* Rasterizer */
  13281. typedef struct stbtt__hheap_chunk
  13282. {
  13283. struct stbtt__hheap_chunk *next;
  13284. } stbtt__hheap_chunk;
  13285. typedef struct stbtt__hheap
  13286. {
  13287. struct stbtt__hheap_chunk *head;
  13288. void *first_free;
  13289. int num_remaining_in_head_chunk;
  13290. } stbtt__hheap;
  13291. static void *stbtt__hheap_alloc(stbtt__hheap *hh, size_t size, void *userdata)
  13292. {
  13293. if (hh->first_free) {
  13294. void *p = hh->first_free;
  13295. hh->first_free = * (void **) p;
  13296. return p;
  13297. } else {
  13298. if (hh->num_remaining_in_head_chunk == 0) {
  13299. int count = (size < 32 ? 2000 : size < 128 ? 800 : 100);
  13300. stbtt__hheap_chunk *c = (stbtt__hheap_chunk *) STBTT_malloc(sizeof(stbtt__hheap_chunk) + size * count, userdata);
  13301. if (c == NULL)
  13302. return NULL;
  13303. c->next = hh->head;
  13304. hh->head = c;
  13305. hh->num_remaining_in_head_chunk = count;
  13306. }
  13307. --hh->num_remaining_in_head_chunk;
  13308. return (char *) (hh->head) + sizeof(stbtt__hheap_chunk) + size * hh->num_remaining_in_head_chunk;
  13309. }
  13310. }
  13311. static void stbtt__hheap_free(stbtt__hheap *hh, void *p)
  13312. {
  13313. *(void **) p = hh->first_free;
  13314. hh->first_free = p;
  13315. }
  13316. static void stbtt__hheap_cleanup(stbtt__hheap *hh, void *userdata)
  13317. {
  13318. stbtt__hheap_chunk *c = hh->head;
  13319. while (c) {
  13320. stbtt__hheap_chunk *n = c->next;
  13321. STBTT_free(c, userdata);
  13322. c = n;
  13323. }
  13324. }
  13325. typedef struct stbtt__edge {
  13326. float x0,y0, x1,y1;
  13327. int invert;
  13328. } stbtt__edge;
  13329. typedef struct stbtt__active_edge
  13330. {
  13331. struct stbtt__active_edge *next;
  13332. #if STBTT_RASTERIZER_VERSION==1
  13333. int x,dx;
  13334. float ey;
  13335. int direction;
  13336. #elif STBTT_RASTERIZER_VERSION==2
  13337. float fx,fdx,fdy;
  13338. float direction;
  13339. float sy;
  13340. float ey;
  13341. #else
  13342. #error "Unrecognized value of STBTT_RASTERIZER_VERSION"
  13343. #endif
  13344. } stbtt__active_edge;
  13345. #if STBTT_RASTERIZER_VERSION == 1
  13346. #define STBTT_FIXSHIFT 10
  13347. #define STBTT_FIX (1 << STBTT_FIXSHIFT)
  13348. #define STBTT_FIXMASK (STBTT_FIX-1)
  13349. static stbtt__active_edge *stbtt__new_active(stbtt__hheap *hh, stbtt__edge *e, int off_x, float start_point, void *userdata)
  13350. {
  13351. stbtt__active_edge *z = (stbtt__active_edge *) stbtt__hheap_alloc(hh, sizeof(*z), userdata);
  13352. float dxdy = (e->x1 - e->x0) / (e->y1 - e->y0);
  13353. STBTT_assert(z != NULL);
  13354. if (!z) return z;
  13355. /* round dx down to avoid overshooting */
  13356. if (dxdy < 0)
  13357. z->dx = -STBTT_ifloor(STBTT_FIX * -dxdy);
  13358. else
  13359. z->dx = STBTT_ifloor(STBTT_FIX * dxdy);
  13360. 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 */
  13361. z->x -= off_x * STBTT_FIX;
  13362. z->ey = e->y1;
  13363. z->next = 0;
  13364. z->direction = e->invert ? 1 : -1;
  13365. return z;
  13366. }
  13367. #elif STBTT_RASTERIZER_VERSION == 2
  13368. static stbtt__active_edge *stbtt__new_active(stbtt__hheap *hh, stbtt__edge *e, int off_x, float start_point, void *userdata)
  13369. {
  13370. stbtt__active_edge *z = (stbtt__active_edge *) stbtt__hheap_alloc(hh, sizeof(*z), userdata);
  13371. float dxdy = (e->x1 - e->x0) / (e->y1 - e->y0);
  13372. STBTT_assert(z != NULL);
  13373. /* STBTT_assert(e->y0 <= start_point); */
  13374. if (!z) return z;
  13375. z->fdx = dxdy;
  13376. z->fdy = dxdy != 0.0f ? (1.0f/dxdy) : 0.0f;
  13377. z->fx = e->x0 + dxdy * (start_point - e->y0);
  13378. z->fx -= off_x;
  13379. z->direction = e->invert ? 1.0f : -1.0f;
  13380. z->sy = e->y0;
  13381. z->ey = e->y1;
  13382. z->next = 0;
  13383. return z;
  13384. }
  13385. #else
  13386. #error "Unrecognized value of STBTT_RASTERIZER_VERSION"
  13387. #endif
  13388. #if STBTT_RASTERIZER_VERSION == 1
  13389. /* note: this routine clips fills that extend off the edges... ideally this */
  13390. /* wouldn't happen, but it could happen if the truetype glyph bounding boxes */
  13391. /* are wrong, or if the user supplies a too-small bitmap */
  13392. static void stbtt__fill_active_edges(unsigned char *scanline, int len, stbtt__active_edge *e, int max_weight)
  13393. {
  13394. /* non-zero winding fill */
  13395. int x0=0, w=0;
  13396. while (e) {
  13397. if (w == 0) {
  13398. /* if we're currently at zero, we need to record the edge start point */
  13399. x0 = e->x; w += e->direction;
  13400. } else {
  13401. int x1 = e->x; w += e->direction;
  13402. /* if we went to zero, we need to draw */
  13403. if (w == 0) {
  13404. int i = x0 >> STBTT_FIXSHIFT;
  13405. int j = x1 >> STBTT_FIXSHIFT;
  13406. if (i < len && j >= 0) {
  13407. if (i == j) {
  13408. /* x0,x1 are the same pixel, so compute combined coverage */
  13409. scanline[i] = scanline[i] + (stbtt_uint8) ((x1 - x0) * max_weight >> STBTT_FIXSHIFT);
  13410. } else {
  13411. if (i >= 0) /* add antialiasing for x0 */
  13412. scanline[i] = scanline[i] + (stbtt_uint8) (((STBTT_FIX - (x0 & STBTT_FIXMASK)) * max_weight) >> STBTT_FIXSHIFT);
  13413. else
  13414. i = -1; /* clip */
  13415. if (j < len) /* add antialiasing for x1 */
  13416. scanline[j] = scanline[j] + (stbtt_uint8) (((x1 & STBTT_FIXMASK) * max_weight) >> STBTT_FIXSHIFT);
  13417. else
  13418. j = len; /* clip */
  13419. for (++i; i < j; ++i) /* fill pixels between x0 and x1 */
  13420. scanline[i] = scanline[i] + (stbtt_uint8) max_weight;
  13421. }
  13422. }
  13423. }
  13424. }
  13425. e = e->next;
  13426. }
  13427. }
  13428. static void stbtt__rasterize_sorted_edges(stbtt__bitmap *result, stbtt__edge *e, int n, int vsubsample, int off_x, int off_y, void *userdata)
  13429. {
  13430. stbtt__hheap hh = { 0, 0, 0 };
  13431. stbtt__active_edge *active = NULL;
  13432. int y,j=0;
  13433. int max_weight = (255 / vsubsample); /* weight per vertical scanline */
  13434. int s; /* vertical subsample index */
  13435. unsigned char scanline_data[512], *scanline;
  13436. if (result->w > 512)
  13437. scanline = (unsigned char *) STBTT_malloc(result->w, userdata);
  13438. else
  13439. scanline = scanline_data;
  13440. y = off_y * vsubsample;
  13441. e[n].y0 = (off_y + result->h) * (float) vsubsample + 1;
  13442. while (j < result->h) {
  13443. STBTT_memset(scanline, 0, result->w);
  13444. for (s=0; s < vsubsample; ++s) {
  13445. /* find center of pixel for this scanline */
  13446. float scan_y = y + 0.5f;
  13447. stbtt__active_edge **step = &active;
  13448. /* update all active edges; */
  13449. /* remove all active edges that terminate before the center of this scanline */
  13450. while (*step) {
  13451. stbtt__active_edge * z = *step;
  13452. if (z->ey <= scan_y) {
  13453. *step = z->next; /* delete from list */
  13454. STBTT_assert(z->direction);
  13455. z->direction = 0;
  13456. stbtt__hheap_free(&hh, z);
  13457. } else {
  13458. z->x += z->dx; /* advance to position for current scanline */
  13459. step = &((*step)->next); /* advance through list */
  13460. }
  13461. }
  13462. /* resort the list if needed */
  13463. for(;;) {
  13464. int changed=0;
  13465. step = &active;
  13466. while (*step && (*step)->next) {
  13467. if ((*step)->x > (*step)->next->x) {
  13468. stbtt__active_edge *t = *step;
  13469. stbtt__active_edge *q = t->next;
  13470. t->next = q->next;
  13471. q->next = t;
  13472. *step = q;
  13473. changed = 1;
  13474. }
  13475. step = &(*step)->next;
  13476. }
  13477. if (!changed) break;
  13478. }
  13479. /* insert all edges that start before the center of this scanline -- omit ones that also end on this scanline */
  13480. while (e->y0 <= scan_y) {
  13481. if (e->y1 > scan_y) {
  13482. stbtt__active_edge *z = stbtt__new_active(&hh, e, off_x, scan_y, userdata);
  13483. if (z != NULL) {
  13484. /* find insertion point */
  13485. if (active == NULL)
  13486. active = z;
  13487. else if (z->x < active->x) {
  13488. /* insert at front */
  13489. z->next = active;
  13490. active = z;
  13491. } else {
  13492. /* find thing to insert AFTER */
  13493. stbtt__active_edge *p = active;
  13494. while (p->next && p->next->x < z->x)
  13495. p = p->next;
  13496. /* at this point, p->next->x is NOT < z->x */
  13497. z->next = p->next;
  13498. p->next = z;
  13499. }
  13500. }
  13501. }
  13502. ++e;
  13503. }
  13504. /* now process all active edges in XOR fashion */
  13505. if (active)
  13506. stbtt__fill_active_edges(scanline, result->w, active, max_weight);
  13507. ++y;
  13508. }
  13509. STBTT_memcpy(result->pixels + j * result->stride, scanline, result->w);
  13510. ++j;
  13511. }
  13512. stbtt__hheap_cleanup(&hh, userdata);
  13513. if (scanline != scanline_data)
  13514. STBTT_free(scanline, userdata);
  13515. }
  13516. #elif STBTT_RASTERIZER_VERSION == 2
  13517. /* the edge passed in here does not cross the vertical line at x or the vertical line at x+1 */
  13518. /* (i.e. it has already been clipped to those) */
  13519. static void stbtt__handle_clipped_edge(float *scanline, int x, stbtt__active_edge *e, float x0, float y0, float x1, float y1)
  13520. {
  13521. if (y0 == y1) return;
  13522. STBTT_assert(y0 < y1);
  13523. STBTT_assert(e->sy <= e->ey);
  13524. if (y0 > e->ey) return;
  13525. if (y1 < e->sy) return;
  13526. if (y0 < e->sy) {
  13527. x0 += (x1-x0) * (e->sy - y0) / (y1-y0);
  13528. y0 = e->sy;
  13529. }
  13530. if (y1 > e->ey) {
  13531. x1 += (x1-x0) * (e->ey - y1) / (y1-y0);
  13532. y1 = e->ey;
  13533. }
  13534. if (x0 == x)
  13535. STBTT_assert(x1 <= x+1);
  13536. else if (x0 == x+1)
  13537. STBTT_assert(x1 >= x);
  13538. else if (x0 <= x)
  13539. STBTT_assert(x1 <= x);
  13540. else if (x0 >= x+1)
  13541. STBTT_assert(x1 >= x+1);
  13542. else
  13543. STBTT_assert(x1 >= x && x1 <= x+1);
  13544. if (x0 <= x && x1 <= x)
  13545. scanline[x] += e->direction * (y1-y0);
  13546. else if (x0 >= x+1 && x1 >= x+1)
  13547. ;
  13548. else {
  13549. STBTT_assert(x0 >= x && x0 <= x+1 && x1 >= x && x1 <= x+1);
  13550. scanline[x] += e->direction * (y1-y0) * (1-((x0-x)+(x1-x))/2); /* coverage = 1 - average x position */
  13551. }
  13552. }
  13553. static float stbtt__sized_trapezoid_area(float height, float top_width, float bottom_width)
  13554. {
  13555. STBTT_assert(top_width >= 0);
  13556. STBTT_assert(bottom_width >= 0);
  13557. return (top_width + bottom_width) / 2.0f * height;
  13558. }
  13559. static float stbtt__position_trapezoid_area(float height, float tx0, float tx1, float bx0, float bx1)
  13560. {
  13561. return stbtt__sized_trapezoid_area(height, tx1 - tx0, bx1 - bx0);
  13562. }
  13563. static float stbtt__sized_triangle_area(float height, float width)
  13564. {
  13565. return height * width / 2;
  13566. }
  13567. static void stbtt__fill_active_edges_new(float *scanline, float *scanline_fill, int len, stbtt__active_edge *e, float y_top)
  13568. {
  13569. float y_bottom = y_top+1;
  13570. while (e) {
  13571. /* brute force every pixel */
  13572. /* compute intersection points with top & bottom */
  13573. STBTT_assert(e->ey >= y_top);
  13574. if (e->fdx == 0) {
  13575. float x0 = e->fx;
  13576. if (x0 < len) {
  13577. if (x0 >= 0) {
  13578. stbtt__handle_clipped_edge(scanline,(int) x0,e, x0,y_top, x0,y_bottom);
  13579. stbtt__handle_clipped_edge(scanline_fill-1,(int) x0+1,e, x0,y_top, x0,y_bottom);
  13580. } else {
  13581. stbtt__handle_clipped_edge(scanline_fill-1,0,e, x0,y_top, x0,y_bottom);
  13582. }
  13583. }
  13584. } else {
  13585. float x0 = e->fx;
  13586. float dx = e->fdx;
  13587. float xb = x0 + dx;
  13588. float x_top, x_bottom;
  13589. float sy0,sy1;
  13590. float dy = e->fdy;
  13591. STBTT_assert(e->sy <= y_bottom && e->ey >= y_top);
  13592. /* compute endpoints of line segment clipped to this scanline (if the */
  13593. /* line segment starts on this scanline. x0 is the intersection of the */
  13594. /* line with y_top, but that may be off the line segment. */
  13595. if (e->sy > y_top) {
  13596. x_top = x0 + dx * (e->sy - y_top);
  13597. sy0 = e->sy;
  13598. } else {
  13599. x_top = x0;
  13600. sy0 = y_top;
  13601. }
  13602. if (e->ey < y_bottom) {
  13603. x_bottom = x0 + dx * (e->ey - y_top);
  13604. sy1 = e->ey;
  13605. } else {
  13606. x_bottom = xb;
  13607. sy1 = y_bottom;
  13608. }
  13609. if (x_top >= 0 && x_bottom >= 0 && x_top < len && x_bottom < len) {
  13610. /* from here on, we don't have to range check x values */
  13611. if ((int) x_top == (int) x_bottom) {
  13612. float height;
  13613. /* simple case, only spans one pixel */
  13614. int x = (int) x_top;
  13615. height = (sy1 - sy0) * e->direction;
  13616. STBTT_assert(x >= 0 && x < len);
  13617. scanline[x] += stbtt__position_trapezoid_area(height, x_top, x+1.0f, x_bottom, x+1.0f);
  13618. scanline_fill[x] += height; /* everything right of this pixel is filled */
  13619. } else {
  13620. int x,x1,x2;
  13621. float y_crossing, y_final, step, sign, area;
  13622. /* covers 2+ pixels */
  13623. if (x_top > x_bottom) {
  13624. /* flip scanline vertically; signed area is the same */
  13625. float t;
  13626. sy0 = y_bottom - (sy0 - y_top);
  13627. sy1 = y_bottom - (sy1 - y_top);
  13628. t = sy0, sy0 = sy1, sy1 = t;
  13629. t = x_bottom, x_bottom = x_top, x_top = t;
  13630. dx = -dx;
  13631. dy = -dy;
  13632. t = x0, x0 = xb, xb = t;
  13633. }
  13634. STBTT_assert(dy >= 0);
  13635. STBTT_assert(dx >= 0);
  13636. x1 = (int) x_top;
  13637. x2 = (int) x_bottom;
  13638. /* compute intersection with y axis at x1+1 */
  13639. y_crossing = y_top + dy * (x1+1 - x0);
  13640. /* compute intersection with y axis at x2 */
  13641. y_final = y_top + dy * (x2 - x0);
  13642. /* x1 x_top x2 x_bottom */
  13643. /* y_top +------|-----+------------+------------+--------|---+------------+ */
  13644. /* | | | | | | */
  13645. /* | | | | | | */
  13646. /* sy0 | Txxxxx|............|............|............|............| */
  13647. /* y_crossing | *xxxxx.......|............|............|............| */
  13648. /* | | xxxxx..|............|............|............| */
  13649. /* | | /- xx*xxxx........|............|............| */
  13650. /* | | dy < | xxxxxx..|............|............| */
  13651. /* y_final | | \- | xx*xxx.........|............| */
  13652. /* sy1 | | | | xxxxxB...|............| */
  13653. /* | | | | | | */
  13654. /* | | | | | | */
  13655. /* y_bottom +------------+------------+------------+------------+------------+ */
  13656. /* */
  13657. /* goal is to measure the area covered by '.' in each pixel */
  13658. /* if x2 is right at the right edge of x1, y_crossing can blow up, github #1057 */
  13659. /* @TODO: maybe test against sy1 rather than y_bottom? */
  13660. if (y_crossing > y_bottom)
  13661. y_crossing = y_bottom;
  13662. sign = e->direction;
  13663. /* area of the rectangle covered from sy0..y_crossing */
  13664. area = sign * (y_crossing-sy0);
  13665. /* area of the triangle (x_top,sy0), (x1+1,sy0), (x1+1,y_crossing) */
  13666. scanline[x1] += stbtt__sized_triangle_area(area, x1+1 - x_top);
  13667. /* check if final y_crossing is blown up; no test case for this */
  13668. if (y_final > y_bottom) {
  13669. y_final = y_bottom;
  13670. dy = (y_final - y_crossing ) / (x2 - (x1+1)); /* if denom=0, y_final = y_crossing, so y_final <= y_bottom */
  13671. }
  13672. /* in second pixel, area covered by line segment found in first pixel */
  13673. /* is always a rectangle 1 wide * the height of that line segment; this */
  13674. /* is exactly what the variable 'area' stores. it also gets a contribution */
  13675. /* from the line segment within it. the THIRD pixel will get the first */
  13676. /* pixel's rectangle contribution, the second pixel's rectangle contribution, */
  13677. /* and its own contribution. the 'own contribution' is the same in every pixel except */
  13678. /* the leftmost and rightmost, a trapezoid that slides down in each pixel. */
  13679. /* the second pixel's contribution to the third pixel will be the */
  13680. /* rectangle 1 wide times the height change in the second pixel, which is dy. */
  13681. step = sign * dy * 1; /* dy is dy/dx, change in y for every 1 change in x, */
  13682. /* which multiplied by 1-pixel-width is how much pixel area changes for each step in x */
  13683. /* so the area advances by 'step' every time */
  13684. for (x = x1+1; x < x2; ++x) {
  13685. scanline[x] += area + step/2; /* area of trapezoid is 1*step/2 */
  13686. area += step;
  13687. }
  13688. STBTT_assert(STBTT_fabs(area) <= 1.01f); /* accumulated error from area += step unless we round step down */
  13689. STBTT_assert(sy1 > y_final-0.01f);
  13690. /* area covered in the last pixel is the rectangle from all the pixels to the left, */
  13691. /* plus the trapezoid filled by the line segment in this pixel all the way to the right edge */
  13692. scanline[x2] += area + sign * stbtt__position_trapezoid_area(sy1-y_final, (float) x2, x2+1.0f, x_bottom, x2+1.0f);
  13693. /* the rest of the line is filled based on the total height of the line segment in this pixel */
  13694. scanline_fill[x2] += sign * (sy1-sy0);
  13695. }
  13696. } else {
  13697. /* if edge goes outside of box we're drawing, we require */
  13698. /* clipping logic. since this does not match the intended use */
  13699. /* of this library, we use a different, very slow brute */
  13700. /* force implementation */
  13701. /* note though that this does happen some of the time because */
  13702. /* x_top and x_bottom can be extrapolated at the top & bottom of */
  13703. /* the shape and actually lie outside the bounding box */
  13704. int x;
  13705. for (x=0; x < len; ++x) {
  13706. /* cases: */
  13707. /* */
  13708. /* there can be up to two intersections with the pixel. any intersection */
  13709. /* with left or right edges can be handled by splitting into two (or three) */
  13710. /* regions. intersections with top & bottom do not necessitate case-wise logic. */
  13711. /* */
  13712. /* the old way of doing this found the intersections with the left & right edges, */
  13713. /* then used some simple logic to produce up to three segments in sorted order */
  13714. /* from top-to-bottom. however, this had a problem: if an x edge was epsilon */
  13715. /* across the x border, then the corresponding y position might not be distinct */
  13716. /* from the other y segment, and it might ignored as an empty segment. to avoid */
  13717. /* that, we need to explicitly produce segments based on x positions. */
  13718. /* rename variables to clearly-defined pairs */
  13719. float y0 = y_top;
  13720. float x1 = (float) (x);
  13721. float x2 = (float) (x+1);
  13722. float x3 = xb;
  13723. float y3 = y_bottom;
  13724. /* x = e->x + e->dx * (y-y_top) */
  13725. /* (y-y_top) = (x - e->x) / e->dx */
  13726. /* y = (x - e->x) / e->dx + y_top */
  13727. float y1 = (x - x0) / dx + y_top;
  13728. float y2 = (x+1 - x0) / dx + y_top;
  13729. if (x0 < x1 && x3 > x2) { /* three segments descending down-right */
  13730. stbtt__handle_clipped_edge(scanline,x,e, x0,y0, x1,y1);
  13731. stbtt__handle_clipped_edge(scanline,x,e, x1,y1, x2,y2);
  13732. stbtt__handle_clipped_edge(scanline,x,e, x2,y2, x3,y3);
  13733. } else if (x3 < x1 && x0 > x2) { /* three segments descending down-left */
  13734. stbtt__handle_clipped_edge(scanline,x,e, x0,y0, x2,y2);
  13735. stbtt__handle_clipped_edge(scanline,x,e, x2,y2, x1,y1);
  13736. stbtt__handle_clipped_edge(scanline,x,e, x1,y1, x3,y3);
  13737. } else if (x0 < x1 && x3 > x1) { /* two segments across x, down-right */
  13738. stbtt__handle_clipped_edge(scanline,x,e, x0,y0, x1,y1);
  13739. stbtt__handle_clipped_edge(scanline,x,e, x1,y1, x3,y3);
  13740. } else if (x3 < x1 && x0 > x1) { /* two segments across x, down-left */
  13741. stbtt__handle_clipped_edge(scanline,x,e, x0,y0, x1,y1);
  13742. stbtt__handle_clipped_edge(scanline,x,e, x1,y1, x3,y3);
  13743. } else if (x0 < x2 && x3 > x2) { /* two segments across x+1, down-right */
  13744. stbtt__handle_clipped_edge(scanline,x,e, x0,y0, x2,y2);
  13745. stbtt__handle_clipped_edge(scanline,x,e, x2,y2, x3,y3);
  13746. } else if (x3 < x2 && x0 > x2) { /* two segments across x+1, down-left */
  13747. stbtt__handle_clipped_edge(scanline,x,e, x0,y0, x2,y2);
  13748. stbtt__handle_clipped_edge(scanline,x,e, x2,y2, x3,y3);
  13749. } else { /* one segment */
  13750. stbtt__handle_clipped_edge(scanline,x,e, x0,y0, x3,y3);
  13751. }
  13752. }
  13753. }
  13754. }
  13755. e = e->next;
  13756. }
  13757. }
  13758. /* directly AA rasterize edges w/o supersampling */
  13759. static void stbtt__rasterize_sorted_edges(stbtt__bitmap *result, stbtt__edge *e, int n, int vsubsample, int off_x, int off_y, void *userdata)
  13760. {
  13761. stbtt__hheap hh = { 0, 0, 0 };
  13762. stbtt__active_edge *active = NULL;
  13763. int y,j=0, i;
  13764. float scanline_data[129], *scanline, *scanline2;
  13765. STBTT__NOTUSED(vsubsample);
  13766. if (result->w > 64)
  13767. scanline = (float *) STBTT_malloc((result->w*2+1) * sizeof(float), userdata);
  13768. else
  13769. scanline = scanline_data;
  13770. scanline2 = scanline + result->w;
  13771. y = off_y;
  13772. e[n].y0 = (float) (off_y + result->h) + 1;
  13773. while (j < result->h) {
  13774. /* find center of pixel for this scanline */
  13775. float scan_y_top = y + 0.0f;
  13776. float scan_y_bottom = y + 1.0f;
  13777. stbtt__active_edge **step = &active;
  13778. STBTT_memset(scanline , 0, result->w*sizeof(scanline[0]));
  13779. STBTT_memset(scanline2, 0, (result->w+1)*sizeof(scanline[0]));
  13780. /* update all active edges; */
  13781. /* remove all active edges that terminate before the top of this scanline */
  13782. while (*step) {
  13783. stbtt__active_edge * z = *step;
  13784. if (z->ey <= scan_y_top) {
  13785. *step = z->next; /* delete from list */
  13786. STBTT_assert(z->direction);
  13787. z->direction = 0;
  13788. stbtt__hheap_free(&hh, z);
  13789. } else {
  13790. step = &((*step)->next); /* advance through list */
  13791. }
  13792. }
  13793. /* insert all edges that start before the bottom of this scanline */
  13794. while (e->y0 <= scan_y_bottom) {
  13795. if (e->y0 != e->y1) {
  13796. stbtt__active_edge *z = stbtt__new_active(&hh, e, off_x, scan_y_top, userdata);
  13797. if (z != NULL) {
  13798. if (j == 0 && off_y != 0) {
  13799. if (z->ey < scan_y_top) {
  13800. /* this can happen due to subpixel positioning and some kind of fp rounding error i think */
  13801. z->ey = scan_y_top;
  13802. }
  13803. }
  13804. STBTT_assert(z->ey >= scan_y_top); /* if we get really unlucky a tiny bit of an edge can be out of bounds */
  13805. /* insert at front */
  13806. z->next = active;
  13807. active = z;
  13808. }
  13809. }
  13810. ++e;
  13811. }
  13812. /* now process all active edges */
  13813. if (active)
  13814. stbtt__fill_active_edges_new(scanline, scanline2+1, result->w, active, scan_y_top);
  13815. {
  13816. float sum = 0;
  13817. for (i=0; i < result->w; ++i) {
  13818. float k;
  13819. int m;
  13820. sum += scanline2[i];
  13821. k = scanline[i] + sum;
  13822. k = (float) STBTT_fabs(k)*255 + 0.5f;
  13823. m = (int) k;
  13824. if (m > 255) m = 255;
  13825. result->pixels[j*result->stride + i] = (unsigned char) m;
  13826. }
  13827. }
  13828. /* advance all the edges */
  13829. step = &active;
  13830. while (*step) {
  13831. stbtt__active_edge *z = *step;
  13832. z->fx += z->fdx; /* advance to position for current scanline */
  13833. step = &((*step)->next); /* advance through list */
  13834. }
  13835. ++y;
  13836. ++j;
  13837. }
  13838. stbtt__hheap_cleanup(&hh, userdata);
  13839. if (scanline != scanline_data)
  13840. STBTT_free(scanline, userdata);
  13841. }
  13842. #else
  13843. #error "Unrecognized value of STBTT_RASTERIZER_VERSION"
  13844. #endif
  13845. #define STBTT__COMPARE(a,b) ((a)->y0 < (b)->y0)
  13846. static void stbtt__sort_edges_ins_sort(stbtt__edge *p, int n)
  13847. {
  13848. int i,j;
  13849. for (i=1; i < n; ++i) {
  13850. stbtt__edge t = p[i], *a = &t;
  13851. j = i;
  13852. while (j > 0) {
  13853. stbtt__edge *b = &p[j-1];
  13854. int c = STBTT__COMPARE(a,b);
  13855. if (!c) break;
  13856. p[j] = p[j-1];
  13857. --j;
  13858. }
  13859. if (i != j)
  13860. p[j] = t;
  13861. }
  13862. }
  13863. static void stbtt__sort_edges_quicksort(stbtt__edge *p, int n)
  13864. {
  13865. /* threshold for transitioning to insertion sort */
  13866. while (n > 12) {
  13867. stbtt__edge t;
  13868. int c01,c12,c,m,i,j;
  13869. /* compute median of three */
  13870. m = n >> 1;
  13871. c01 = STBTT__COMPARE(&p[0],&p[m]);
  13872. c12 = STBTT__COMPARE(&p[m],&p[n-1]);
  13873. /* if 0 >= mid >= end, or 0 < mid < end, then use mid */
  13874. if (c01 != c12) {
  13875. /* otherwise, we'll need to swap something else to middle */
  13876. int z;
  13877. c = STBTT__COMPARE(&p[0],&p[n-1]);
  13878. /* 0>mid && mid<n: 0>n => n; 0<n => 0 */
  13879. /* 0<mid && mid>n: 0>n => 0; 0<n => n */
  13880. z = (c == c12) ? 0 : n-1;
  13881. t = p[z];
  13882. p[z] = p[m];
  13883. p[m] = t;
  13884. }
  13885. /* now p[m] is the median-of-three */
  13886. /* swap it to the beginning so it won't move around */
  13887. t = p[0];
  13888. p[0] = p[m];
  13889. p[m] = t;
  13890. /* partition loop */
  13891. i=1;
  13892. j=n-1;
  13893. for(;;) {
  13894. /* handling of equality is crucial here */
  13895. /* for sentinels & efficiency with duplicates */
  13896. for (;;++i) {
  13897. if (!STBTT__COMPARE(&p[i], &p[0])) break;
  13898. }
  13899. for (;;--j) {
  13900. if (!STBTT__COMPARE(&p[0], &p[j])) break;
  13901. }
  13902. /* make sure we haven't crossed */
  13903. if (i >= j) break;
  13904. t = p[i];
  13905. p[i] = p[j];
  13906. p[j] = t;
  13907. ++i;
  13908. --j;
  13909. }
  13910. /* recurse on smaller side, iterate on larger */
  13911. if (j < (n-i)) {
  13912. stbtt__sort_edges_quicksort(p,j);
  13913. p = p+i;
  13914. n = n-i;
  13915. } else {
  13916. stbtt__sort_edges_quicksort(p+i, n-i);
  13917. n = j;
  13918. }
  13919. }
  13920. }
  13921. static void stbtt__sort_edges(stbtt__edge *p, int n)
  13922. {
  13923. stbtt__sort_edges_quicksort(p, n);
  13924. stbtt__sort_edges_ins_sort(p, n);
  13925. }
  13926. typedef struct
  13927. {
  13928. float x,y;
  13929. } stbtt__point;
  13930. 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)
  13931. {
  13932. float y_scale_inv = invert ? -scale_y : scale_y;
  13933. stbtt__edge *e;
  13934. int n,i,j,k,m;
  13935. #if STBTT_RASTERIZER_VERSION == 1
  13936. int vsubsample = result->h < 8 ? 15 : 5;
  13937. #elif STBTT_RASTERIZER_VERSION == 2
  13938. int vsubsample = 1;
  13939. #else
  13940. #error "Unrecognized value of STBTT_RASTERIZER_VERSION"
  13941. #endif
  13942. /* vsubsample should divide 255 evenly; otherwise we won't reach full opacity */
  13943. /* now we have to blow out the windings into explicit edge lists */
  13944. n = 0;
  13945. for (i=0; i < windings; ++i)
  13946. n += wcount[i];
  13947. e = (stbtt__edge *) STBTT_malloc(sizeof(*e) * (n+1), userdata); /* add an extra one as a sentinel */
  13948. if (e == 0) return;
  13949. n = 0;
  13950. m=0;
  13951. for (i=0; i < windings; ++i) {
  13952. stbtt__point *p = pts + m;
  13953. m += wcount[i];
  13954. j = wcount[i]-1;
  13955. for (k=0; k < wcount[i]; j=k++) {
  13956. int a=k,b=j;
  13957. /* skip the edge if horizontal */
  13958. if (p[j].y == p[k].y)
  13959. continue;
  13960. /* add edge from j to k to the list */
  13961. e[n].invert = 0;
  13962. if (invert ? p[j].y > p[k].y : p[j].y < p[k].y) {
  13963. e[n].invert = 1;
  13964. a=j,b=k;
  13965. }
  13966. e[n].x0 = p[a].x * scale_x + shift_x;
  13967. e[n].y0 = (p[a].y * y_scale_inv + shift_y) * vsubsample;
  13968. e[n].x1 = p[b].x * scale_x + shift_x;
  13969. e[n].y1 = (p[b].y * y_scale_inv + shift_y) * vsubsample;
  13970. ++n;
  13971. }
  13972. }
  13973. /* now sort the edges by their highest point (should snap to integer, and then by x) */
  13974. /* STBTT_sort(e, n, sizeof(e[0]), stbtt__edge_compare); */
  13975. stbtt__sort_edges(e, n);
  13976. /* now, traverse the scanlines and find the intersections on each scanline, use xor winding rule */
  13977. stbtt__rasterize_sorted_edges(result, e, n, vsubsample, off_x, off_y, userdata);
  13978. STBTT_free(e, userdata);
  13979. }
  13980. static void stbtt__add_point(stbtt__point *points, int n, float x, float y)
  13981. {
  13982. if (!points) return; /* during first pass, it's unallocated */
  13983. points[n].x = x;
  13984. points[n].y = y;
  13985. }
  13986. /* tessellate until threshold p is happy... @TODO warped to compensate for non-linear stretching */
  13987. 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)
  13988. {
  13989. /* midpoint */
  13990. float mx = (x0 + 2*x1 + x2)/4;
  13991. float my = (y0 + 2*y1 + y2)/4;
  13992. /* versus directly drawn line */
  13993. float dx = (x0+x2)/2 - mx;
  13994. float dy = (y0+y2)/2 - my;
  13995. if (n > 16) /* 65536 segments on one curve better be enough! */
  13996. return 1;
  13997. if (dx*dx+dy*dy > objspace_flatness_squared) { /* half-pixel error allowed... need to be smaller if AA */
  13998. stbtt__tesselate_curve(points, num_points, x0,y0, (x0+x1)/2.0f,(y0+y1)/2.0f, mx,my, objspace_flatness_squared,n+1);
  13999. stbtt__tesselate_curve(points, num_points, mx,my, (x1+x2)/2.0f,(y1+y2)/2.0f, x2,y2, objspace_flatness_squared,n+1);
  14000. } else {
  14001. stbtt__add_point(points, *num_points,x2,y2);
  14002. *num_points = *num_points+1;
  14003. }
  14004. return 1;
  14005. }
  14006. 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)
  14007. {
  14008. /* @TODO this "flatness" calculation is just made-up nonsense that seems to work well enough */
  14009. float dx0 = x1-x0;
  14010. float dy0 = y1-y0;
  14011. float dx1 = x2-x1;
  14012. float dy1 = y2-y1;
  14013. float dx2 = x3-x2;
  14014. float dy2 = y3-y2;
  14015. float dx = x3-x0;
  14016. float dy = y3-y0;
  14017. float longlen = (float) (STBTT_sqrt(dx0*dx0+dy0*dy0)+STBTT_sqrt(dx1*dx1+dy1*dy1)+STBTT_sqrt(dx2*dx2+dy2*dy2));
  14018. float shortlen = (float) STBTT_sqrt(dx*dx+dy*dy);
  14019. float flatness_squared = longlen*longlen-shortlen*shortlen;
  14020. if (n > 16) /* 65536 segments on one curve better be enough! */
  14021. return;
  14022. if (flatness_squared > objspace_flatness_squared) {
  14023. float x01 = (x0+x1)/2;
  14024. float y01 = (y0+y1)/2;
  14025. float x12 = (x1+x2)/2;
  14026. float y12 = (y1+y2)/2;
  14027. float x23 = (x2+x3)/2;
  14028. float y23 = (y2+y3)/2;
  14029. float xa = (x01+x12)/2;
  14030. float ya = (y01+y12)/2;
  14031. float xb = (x12+x23)/2;
  14032. float yb = (y12+y23)/2;
  14033. float mx = (xa+xb)/2;
  14034. float my = (ya+yb)/2;
  14035. stbtt__tesselate_cubic(points, num_points, x0,y0, x01,y01, xa,ya, mx,my, objspace_flatness_squared,n+1);
  14036. stbtt__tesselate_cubic(points, num_points, mx,my, xb,yb, x23,y23, x3,y3, objspace_flatness_squared,n+1);
  14037. } else {
  14038. stbtt__add_point(points, *num_points,x3,y3);
  14039. *num_points = *num_points+1;
  14040. }
  14041. }
  14042. /* returns number of contours */
  14043. static stbtt__point *stbtt_FlattenCurves(stbtt_vertex *vertices, int num_verts, float objspace_flatness, int **contour_lengths, int *num_contours, void *userdata)
  14044. {
  14045. stbtt__point *points=0;
  14046. int num_points=0;
  14047. float objspace_flatness_squared = objspace_flatness * objspace_flatness;
  14048. int i,n=0,start=0, pass;
  14049. /* count how many "moves" there are to get the contour count */
  14050. for (i=0; i < num_verts; ++i)
  14051. if (vertices[i].type == STBTT_vmove)
  14052. ++n;
  14053. *num_contours = n;
  14054. if (n == 0) return 0;
  14055. *contour_lengths = (int *) STBTT_malloc(sizeof(**contour_lengths) * n, userdata);
  14056. if (*contour_lengths == 0) {
  14057. *num_contours = 0;
  14058. return 0;
  14059. }
  14060. /* make two passes through the points so we don't need to realloc */
  14061. for (pass=0; pass < 2; ++pass) {
  14062. float x=0,y=0;
  14063. if (pass == 1) {
  14064. points = (stbtt__point *) STBTT_malloc(num_points * sizeof(points[0]), userdata);
  14065. if (points == NULL) goto error;
  14066. }
  14067. num_points = 0;
  14068. n= -1;
  14069. for (i=0; i < num_verts; ++i) {
  14070. switch (vertices[i].type) {
  14071. case STBTT_vmove:
  14072. /* start the next contour */
  14073. if (n >= 0)
  14074. (*contour_lengths)[n] = num_points - start;
  14075. ++n;
  14076. start = num_points;
  14077. x = vertices[i].x, y = vertices[i].y;
  14078. stbtt__add_point(points, num_points++, x,y);
  14079. break;
  14080. case STBTT_vline:
  14081. x = vertices[i].x, y = vertices[i].y;
  14082. stbtt__add_point(points, num_points++, x, y);
  14083. break;
  14084. case STBTT_vcurve:
  14085. stbtt__tesselate_curve(points, &num_points, x,y,
  14086. vertices[i].cx, vertices[i].cy,
  14087. vertices[i].x, vertices[i].y,
  14088. objspace_flatness_squared, 0);
  14089. x = vertices[i].x, y = vertices[i].y;
  14090. break;
  14091. case STBTT_vcubic:
  14092. stbtt__tesselate_cubic(points, &num_points, x,y,
  14093. vertices[i].cx, vertices[i].cy,
  14094. vertices[i].cx1, vertices[i].cy1,
  14095. vertices[i].x, vertices[i].y,
  14096. objspace_flatness_squared, 0);
  14097. x = vertices[i].x, y = vertices[i].y;
  14098. break;
  14099. }
  14100. }
  14101. (*contour_lengths)[n] = num_points - start;
  14102. }
  14103. return points;
  14104. error:
  14105. STBTT_free(points, userdata);
  14106. STBTT_free(*contour_lengths, userdata);
  14107. *contour_lengths = 0;
  14108. *num_contours = 0;
  14109. return NULL;
  14110. }
  14111. 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)
  14112. {
  14113. float scale = scale_x > scale_y ? scale_y : scale_x;
  14114. int winding_count = 0;
  14115. int *winding_lengths = NULL;
  14116. stbtt__point *windings = stbtt_FlattenCurves(vertices, num_verts, flatness_in_pixels / scale, &winding_lengths, &winding_count, userdata);
  14117. if (windings) {
  14118. stbtt__rasterize(result, windings, winding_lengths, winding_count, scale_x, scale_y, shift_x, shift_y, x_off, y_off, invert, userdata);
  14119. STBTT_free(winding_lengths, userdata);
  14120. STBTT_free(windings, userdata);
  14121. }
  14122. }
  14123. STBTT_DEF void stbtt_FreeBitmap(unsigned char *bitmap, void *userdata)
  14124. {
  14125. STBTT_free(bitmap, userdata);
  14126. }
  14127. 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)
  14128. {
  14129. int ix0,iy0,ix1,iy1;
  14130. stbtt__bitmap gbm;
  14131. stbtt_vertex *vertices;
  14132. int num_verts = stbtt_GetGlyphShape(info, glyph, &vertices);
  14133. if (scale_x == 0) scale_x = scale_y;
  14134. if (scale_y == 0) {
  14135. if (scale_x == 0) {
  14136. STBTT_free(vertices, info->userdata);
  14137. return NULL;
  14138. }
  14139. scale_y = scale_x;
  14140. }
  14141. stbtt_GetGlyphBitmapBoxSubpixel(info, glyph, scale_x, scale_y, shift_x, shift_y, &ix0,&iy0,&ix1,&iy1);
  14142. /* now we get the size */
  14143. gbm.w = (ix1 - ix0);
  14144. gbm.h = (iy1 - iy0);
  14145. gbm.pixels = NULL; /* in case we error */
  14146. if (width ) *width = gbm.w;
  14147. if (height) *height = gbm.h;
  14148. if (xoff ) *xoff = ix0;
  14149. if (yoff ) *yoff = iy0;
  14150. if (gbm.w && gbm.h) {
  14151. gbm.pixels = (unsigned char *) STBTT_malloc(gbm.w * gbm.h, info->userdata);
  14152. if (gbm.pixels) {
  14153. gbm.stride = gbm.w;
  14154. stbtt_Rasterize(&gbm, 0.35f, vertices, num_verts, scale_x, scale_y, shift_x, shift_y, ix0, iy0, 1, info->userdata);
  14155. }
  14156. }
  14157. STBTT_free(vertices, info->userdata);
  14158. return gbm.pixels;
  14159. }
  14160. 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)
  14161. {
  14162. return stbtt_GetGlyphBitmapSubpixel(info, scale_x, scale_y, 0.0f, 0.0f, glyph, width, height, xoff, yoff);
  14163. }
  14164. 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)
  14165. {
  14166. int ix0,iy0;
  14167. stbtt_vertex *vertices;
  14168. int num_verts = stbtt_GetGlyphShape(info, glyph, &vertices);
  14169. stbtt__bitmap gbm;
  14170. stbtt_GetGlyphBitmapBoxSubpixel(info, glyph, scale_x, scale_y, shift_x, shift_y, &ix0,&iy0,0,0);
  14171. gbm.pixels = output;
  14172. gbm.w = out_w;
  14173. gbm.h = out_h;
  14174. gbm.stride = out_stride;
  14175. if (gbm.w && gbm.h)
  14176. stbtt_Rasterize(&gbm, 0.35f, vertices, num_verts, scale_x, scale_y, shift_x, shift_y, ix0,iy0, 1, info->userdata);
  14177. STBTT_free(vertices, info->userdata);
  14178. }
  14179. 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)
  14180. {
  14181. stbtt_MakeGlyphBitmapSubpixel(info, output, out_w, out_h, out_stride, scale_x, scale_y, 0.0f,0.0f, glyph);
  14182. }
  14183. 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)
  14184. {
  14185. return stbtt_GetGlyphBitmapSubpixel(info, scale_x, scale_y,shift_x,shift_y, stbtt_FindGlyphIndex(info,codepoint), width,height,xoff,yoff);
  14186. }
  14187. 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)
  14188. {
  14189. 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));
  14190. }
  14191. 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)
  14192. {
  14193. stbtt_MakeGlyphBitmapSubpixel(info, output, out_w, out_h, out_stride, scale_x, scale_y, shift_x, shift_y, stbtt_FindGlyphIndex(info,codepoint));
  14194. }
  14195. 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)
  14196. {
  14197. return stbtt_GetCodepointBitmapSubpixel(info, scale_x, scale_y, 0.0f,0.0f, codepoint, width,height,xoff,yoff);
  14198. }
  14199. 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)
  14200. {
  14201. stbtt_MakeCodepointBitmapSubpixel(info, output, out_w, out_h, out_stride, scale_x, scale_y, 0.0f,0.0f, codepoint);
  14202. }
  14203. /* //////////////////////////////////////////////////////////////////////////// */
  14204. /* */
  14205. /* bitmap baking */
  14206. /* */
  14207. /* This is SUPER-CRAPPY packing to keep source code small */
  14208. static int stbtt_BakeFontBitmap_internal(unsigned char *data, int offset, /* font location (use offset=0 for plain .ttf) */
  14209. float pixel_height, /* height of font in pixels */
  14210. unsigned char *pixels, int pw, int ph, /* bitmap to be filled in */
  14211. int first_char, int num_chars, /* characters to bake */
  14212. stbtt_bakedchar *chardata)
  14213. {
  14214. float scale;
  14215. int x,y,bottom_y, i;
  14216. stbtt_fontinfo f;
  14217. f.userdata = NULL;
  14218. if (!stbtt_InitFont(&f, data, offset))
  14219. return -1;
  14220. STBTT_memset(pixels, 0, pw*ph); /* background of 0 around pixels */
  14221. x=y=1;
  14222. bottom_y = 1;
  14223. scale = stbtt_ScaleForPixelHeight(&f, pixel_height);
  14224. for (i=0; i < num_chars; ++i) {
  14225. int advance, lsb, x0,y0,x1,y1,gw,gh;
  14226. int g = stbtt_FindGlyphIndex(&f, first_char + i);
  14227. stbtt_GetGlyphHMetrics(&f, g, &advance, &lsb);
  14228. stbtt_GetGlyphBitmapBox(&f, g, scale,scale, &x0,&y0,&x1,&y1);
  14229. gw = x1-x0;
  14230. gh = y1-y0;
  14231. if (x + gw + 1 >= pw)
  14232. y = bottom_y, x = 1; /* advance to next row */
  14233. if (y + gh + 1 >= ph) /* check if it fits vertically AFTER potentially moving to next row */
  14234. return -i;
  14235. STBTT_assert(x+gw < pw);
  14236. STBTT_assert(y+gh < ph);
  14237. stbtt_MakeGlyphBitmap(&f, pixels+x+y*pw, gw,gh,pw, scale,scale, g);
  14238. chardata[i].x0 = (stbtt_int16) x;
  14239. chardata[i].y0 = (stbtt_int16) y;
  14240. chardata[i].x1 = (stbtt_int16) (x + gw);
  14241. chardata[i].y1 = (stbtt_int16) (y + gh);
  14242. chardata[i].xadvance = scale * advance;
  14243. chardata[i].xoff = (float) x0;
  14244. chardata[i].yoff = (float) y0;
  14245. x = x + gw + 1;
  14246. if (y+gh+1 > bottom_y)
  14247. bottom_y = y+gh+1;
  14248. }
  14249. return bottom_y;
  14250. }
  14251. 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)
  14252. {
  14253. float d3d_bias = opengl_fillrule ? 0 : -0.5f;
  14254. float ipw = 1.0f / pw, iph = 1.0f / ph;
  14255. const stbtt_bakedchar *b = chardata + char_index;
  14256. int round_x = STBTT_ifloor((*xpos + b->xoff) + 0.5f);
  14257. int round_y = STBTT_ifloor((*ypos + b->yoff) + 0.5f);
  14258. q->x0 = round_x + d3d_bias;
  14259. q->y0 = round_y + d3d_bias;
  14260. q->x1 = round_x + b->x1 - b->x0 + d3d_bias;
  14261. q->y1 = round_y + b->y1 - b->y0 + d3d_bias;
  14262. q->s0 = b->x0 * ipw;
  14263. q->t0 = b->y0 * iph;
  14264. q->s1 = b->x1 * ipw;
  14265. q->t1 = b->y1 * iph;
  14266. *xpos += b->xadvance;
  14267. }
  14268. /* //////////////////////////////////////////////////////////////////////////// */
  14269. /* */
  14270. /* rectangle packing replacement routines if you don't have stb_rect_pack.h */
  14271. /* */
  14272. #ifndef STB_RECT_PACK_VERSION
  14273. typedef int stbrp_coord;
  14274. /* ////////////////////////////////////////////////////////////////////////////////// */
  14275. /* // */
  14276. /* // */
  14277. /* COMPILER WARNING ?!?!? // */
  14278. /* // */
  14279. /* // */
  14280. /* if you get a compile warning due to these symbols being defined more than // */
  14281. /* once, move #include "stb_rect_pack.h" before #include "stb_truetype.h" // */
  14282. /* // */
  14283. /* ////////////////////////////////////////////////////////////////////////////////// */
  14284. typedef struct
  14285. {
  14286. int width,height;
  14287. int x,y,bottom_y;
  14288. } stbrp_context;
  14289. typedef struct
  14290. {
  14291. unsigned char x;
  14292. } stbrp_node;
  14293. struct stbrp_rect
  14294. {
  14295. stbrp_coord x,y;
  14296. int id,w,h,was_packed;
  14297. };
  14298. static void stbrp_init_target(stbrp_context *con, int pw, int ph, stbrp_node *nodes, int num_nodes)
  14299. {
  14300. con->width = pw;
  14301. con->height = ph;
  14302. con->x = 0;
  14303. con->y = 0;
  14304. con->bottom_y = 0;
  14305. STBTT__NOTUSED(nodes);
  14306. STBTT__NOTUSED(num_nodes);
  14307. }
  14308. static void stbrp_pack_rects(stbrp_context *con, stbrp_rect *rects, int num_rects)
  14309. {
  14310. int i;
  14311. for (i=0; i < num_rects; ++i) {
  14312. if (con->x + rects[i].w > con->width) {
  14313. con->x = 0;
  14314. con->y = con->bottom_y;
  14315. }
  14316. if (con->y + rects[i].h > con->height)
  14317. break;
  14318. rects[i].x = con->x;
  14319. rects[i].y = con->y;
  14320. rects[i].was_packed = 1;
  14321. con->x += rects[i].w;
  14322. if (con->y + rects[i].h > con->bottom_y)
  14323. con->bottom_y = con->y + rects[i].h;
  14324. }
  14325. for ( ; i < num_rects; ++i)
  14326. rects[i].was_packed = 0;
  14327. }
  14328. #endif
  14329. /* //////////////////////////////////////////////////////////////////////////// */
  14330. /* */
  14331. /* bitmap baking */
  14332. /* */
  14333. /* This is SUPER-AWESOME (tm Ryan Gordon) packing using stb_rect_pack.h. If */
  14334. /* stb_rect_pack.h isn't available, it uses the BakeFontBitmap strategy. */
  14335. 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)
  14336. {
  14337. stbrp_context *context = (stbrp_context *) STBTT_malloc(sizeof(*context) ,alloc_context);
  14338. int num_nodes = pw - padding;
  14339. stbrp_node *nodes = (stbrp_node *) STBTT_malloc(sizeof(*nodes ) * num_nodes,alloc_context);
  14340. if (context == NULL || nodes == NULL) {
  14341. if (context != NULL) STBTT_free(context, alloc_context);
  14342. if (nodes != NULL) STBTT_free(nodes , alloc_context);
  14343. return 0;
  14344. }
  14345. spc->user_allocator_context = alloc_context;
  14346. spc->width = pw;
  14347. spc->height = ph;
  14348. spc->pixels = pixels;
  14349. spc->pack_info = context;
  14350. spc->nodes = nodes;
  14351. spc->padding = padding;
  14352. spc->stride_in_bytes = stride_in_bytes != 0 ? stride_in_bytes : pw;
  14353. spc->h_oversample = 1;
  14354. spc->v_oversample = 1;
  14355. spc->skip_missing = 0;
  14356. stbrp_init_target(context, pw-padding, ph-padding, nodes, num_nodes);
  14357. if (pixels)
  14358. STBTT_memset(pixels, 0, pw*ph); /* background of 0 around pixels */
  14359. return 1;
  14360. }
  14361. STBTT_DEF void stbtt_PackEnd (stbtt_pack_context *spc)
  14362. {
  14363. STBTT_free(spc->nodes , spc->user_allocator_context);
  14364. STBTT_free(spc->pack_info, spc->user_allocator_context);
  14365. }
  14366. STBTT_DEF void stbtt_PackSetOversampling(stbtt_pack_context *spc, unsigned int h_oversample, unsigned int v_oversample)
  14367. {
  14368. STBTT_assert(h_oversample <= STBTT_MAX_OVERSAMPLE);
  14369. STBTT_assert(v_oversample <= STBTT_MAX_OVERSAMPLE);
  14370. if (h_oversample <= STBTT_MAX_OVERSAMPLE)
  14371. spc->h_oversample = h_oversample;
  14372. if (v_oversample <= STBTT_MAX_OVERSAMPLE)
  14373. spc->v_oversample = v_oversample;
  14374. }
  14375. STBTT_DEF void stbtt_PackSetSkipMissingCodepoints(stbtt_pack_context *spc, int skip)
  14376. {
  14377. spc->skip_missing = skip;
  14378. }
  14379. #define STBTT__OVER_MASK (STBTT_MAX_OVERSAMPLE-1)
  14380. static void stbtt__h_prefilter(unsigned char *pixels, int w, int h, int stride_in_bytes, unsigned int kernel_width)
  14381. {
  14382. unsigned char buffer[STBTT_MAX_OVERSAMPLE];
  14383. int safe_w = w - kernel_width;
  14384. int j;
  14385. STBTT_memset(buffer, 0, STBTT_MAX_OVERSAMPLE); /* suppress bogus warning from VS2013 -analyze */
  14386. for (j=0; j < h; ++j) {
  14387. int i;
  14388. unsigned int total;
  14389. STBTT_memset(buffer, 0, kernel_width);
  14390. total = 0;
  14391. /* make kernel_width a constant in common cases so compiler can optimize out the divide */
  14392. switch (kernel_width) {
  14393. case 2:
  14394. for (i=0; i <= safe_w; ++i) {
  14395. total += pixels[i] - buffer[i & STBTT__OVER_MASK];
  14396. buffer[(i+kernel_width) & STBTT__OVER_MASK] = pixels[i];
  14397. pixels[i] = (unsigned char) (total / 2);
  14398. }
  14399. break;
  14400. case 3:
  14401. for (i=0; i <= safe_w; ++i) {
  14402. total += pixels[i] - buffer[i & STBTT__OVER_MASK];
  14403. buffer[(i+kernel_width) & STBTT__OVER_MASK] = pixels[i];
  14404. pixels[i] = (unsigned char) (total / 3);
  14405. }
  14406. break;
  14407. case 4:
  14408. for (i=0; i <= safe_w; ++i) {
  14409. total += pixels[i] - buffer[i & STBTT__OVER_MASK];
  14410. buffer[(i+kernel_width) & STBTT__OVER_MASK] = pixels[i];
  14411. pixels[i] = (unsigned char) (total / 4);
  14412. }
  14413. break;
  14414. case 5:
  14415. for (i=0; i <= safe_w; ++i) {
  14416. total += pixels[i] - buffer[i & STBTT__OVER_MASK];
  14417. buffer[(i+kernel_width) & STBTT__OVER_MASK] = pixels[i];
  14418. pixels[i] = (unsigned char) (total / 5);
  14419. }
  14420. break;
  14421. default:
  14422. for (i=0; i <= safe_w; ++i) {
  14423. total += pixels[i] - buffer[i & STBTT__OVER_MASK];
  14424. buffer[(i+kernel_width) & STBTT__OVER_MASK] = pixels[i];
  14425. pixels[i] = (unsigned char) (total / kernel_width);
  14426. }
  14427. break;
  14428. }
  14429. for (; i < w; ++i) {
  14430. STBTT_assert(pixels[i] == 0);
  14431. total -= buffer[i & STBTT__OVER_MASK];
  14432. pixels[i] = (unsigned char) (total / kernel_width);
  14433. }
  14434. pixels += stride_in_bytes;
  14435. }
  14436. }
  14437. static void stbtt__v_prefilter(unsigned char *pixels, int w, int h, int stride_in_bytes, unsigned int kernel_width)
  14438. {
  14439. unsigned char buffer[STBTT_MAX_OVERSAMPLE];
  14440. int safe_h = h - kernel_width;
  14441. int j;
  14442. STBTT_memset(buffer, 0, STBTT_MAX_OVERSAMPLE); /* suppress bogus warning from VS2013 -analyze */
  14443. for (j=0; j < w; ++j) {
  14444. int i;
  14445. unsigned int total;
  14446. STBTT_memset(buffer, 0, kernel_width);
  14447. total = 0;
  14448. /* make kernel_width a constant in common cases so compiler can optimize out the divide */
  14449. switch (kernel_width) {
  14450. case 2:
  14451. for (i=0; i <= safe_h; ++i) {
  14452. total += pixels[i*stride_in_bytes] - buffer[i & STBTT__OVER_MASK];
  14453. buffer[(i+kernel_width) & STBTT__OVER_MASK] = pixels[i*stride_in_bytes];
  14454. pixels[i*stride_in_bytes] = (unsigned char) (total / 2);
  14455. }
  14456. break;
  14457. case 3:
  14458. for (i=0; i <= safe_h; ++i) {
  14459. total += pixels[i*stride_in_bytes] - buffer[i & STBTT__OVER_MASK];
  14460. buffer[(i+kernel_width) & STBTT__OVER_MASK] = pixels[i*stride_in_bytes];
  14461. pixels[i*stride_in_bytes] = (unsigned char) (total / 3);
  14462. }
  14463. break;
  14464. case 4:
  14465. for (i=0; i <= safe_h; ++i) {
  14466. total += pixels[i*stride_in_bytes] - buffer[i & STBTT__OVER_MASK];
  14467. buffer[(i+kernel_width) & STBTT__OVER_MASK] = pixels[i*stride_in_bytes];
  14468. pixels[i*stride_in_bytes] = (unsigned char) (total / 4);
  14469. }
  14470. break;
  14471. case 5:
  14472. for (i=0; i <= safe_h; ++i) {
  14473. total += pixels[i*stride_in_bytes] - buffer[i & STBTT__OVER_MASK];
  14474. buffer[(i+kernel_width) & STBTT__OVER_MASK] = pixels[i*stride_in_bytes];
  14475. pixels[i*stride_in_bytes] = (unsigned char) (total / 5);
  14476. }
  14477. break;
  14478. default:
  14479. for (i=0; i <= safe_h; ++i) {
  14480. total += pixels[i*stride_in_bytes] - buffer[i & STBTT__OVER_MASK];
  14481. buffer[(i+kernel_width) & STBTT__OVER_MASK] = pixels[i*stride_in_bytes];
  14482. pixels[i*stride_in_bytes] = (unsigned char) (total / kernel_width);
  14483. }
  14484. break;
  14485. }
  14486. for (; i < h; ++i) {
  14487. STBTT_assert(pixels[i*stride_in_bytes] == 0);
  14488. total -= buffer[i & STBTT__OVER_MASK];
  14489. pixels[i*stride_in_bytes] = (unsigned char) (total / kernel_width);
  14490. }
  14491. pixels += 1;
  14492. }
  14493. }
  14494. static float stbtt__oversample_shift(int oversample)
  14495. {
  14496. if (!oversample)
  14497. return 0.0f;
  14498. /* The prefilter is a box filter of width "oversample", */
  14499. /* which shifts phase by (oversample - 1)/2 pixels in */
  14500. /* oversampled space. We want to shift in the opposite */
  14501. /* direction to counter this. */
  14502. return (float)-(oversample - 1) / (2.0f * (float)oversample);
  14503. }
  14504. /* rects array must be big enough to accommodate all characters in the given ranges */
  14505. STBTT_DEF int stbtt_PackFontRangesGatherRects(stbtt_pack_context *spc, const stbtt_fontinfo *info, stbtt_pack_range *ranges, int num_ranges, stbrp_rect *rects)
  14506. {
  14507. int i,j,k;
  14508. int missing_glyph_added = 0;
  14509. k=0;
  14510. for (i=0; i < num_ranges; ++i) {
  14511. float fh = ranges[i].font_size;
  14512. float scale = fh > 0 ? stbtt_ScaleForPixelHeight(info, fh) : stbtt_ScaleForMappingEmToPixels(info, -fh);
  14513. ranges[i].h_oversample = (unsigned char) spc->h_oversample;
  14514. ranges[i].v_oversample = (unsigned char) spc->v_oversample;
  14515. for (j=0; j < ranges[i].num_chars; ++j) {
  14516. int x0,y0,x1,y1;
  14517. int codepoint = ranges[i].array_of_unicode_codepoints == NULL ? ranges[i].first_unicode_codepoint_in_range + j : ranges[i].array_of_unicode_codepoints[j];
  14518. int glyph = stbtt_FindGlyphIndex(info, codepoint);
  14519. if (glyph == 0 && (spc->skip_missing || missing_glyph_added)) {
  14520. rects[k].w = rects[k].h = 0;
  14521. } else {
  14522. stbtt_GetGlyphBitmapBoxSubpixel(info,glyph,
  14523. scale * spc->h_oversample,
  14524. scale * spc->v_oversample,
  14525. 0,0,
  14526. &x0,&y0,&x1,&y1);
  14527. rects[k].w = (stbrp_coord) (x1-x0 + spc->padding + spc->h_oversample-1);
  14528. rects[k].h = (stbrp_coord) (y1-y0 + spc->padding + spc->v_oversample-1);
  14529. if (glyph == 0)
  14530. missing_glyph_added = 1;
  14531. }
  14532. ++k;
  14533. }
  14534. }
  14535. return k;
  14536. }
  14537. 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)
  14538. {
  14539. stbtt_MakeGlyphBitmapSubpixel(info,
  14540. output,
  14541. out_w - (prefilter_x - 1),
  14542. out_h - (prefilter_y - 1),
  14543. out_stride,
  14544. scale_x,
  14545. scale_y,
  14546. shift_x,
  14547. shift_y,
  14548. glyph);
  14549. if (prefilter_x > 1)
  14550. stbtt__h_prefilter(output, out_w, out_h, out_stride, prefilter_x);
  14551. if (prefilter_y > 1)
  14552. stbtt__v_prefilter(output, out_w, out_h, out_stride, prefilter_y);
  14553. *sub_x = stbtt__oversample_shift(prefilter_x);
  14554. *sub_y = stbtt__oversample_shift(prefilter_y);
  14555. }
  14556. /* rects array must be big enough to accommodate all characters in the given ranges */
  14557. STBTT_DEF int stbtt_PackFontRangesRenderIntoRects(stbtt_pack_context *spc, const stbtt_fontinfo *info, stbtt_pack_range *ranges, int num_ranges, stbrp_rect *rects)
  14558. {
  14559. int i,j,k, missing_glyph = -1, return_value = 1;
  14560. /* save current values */
  14561. int old_h_over = spc->h_oversample;
  14562. int old_v_over = spc->v_oversample;
  14563. k = 0;
  14564. for (i=0; i < num_ranges; ++i) {
  14565. float fh = ranges[i].font_size;
  14566. float scale = fh > 0 ? stbtt_ScaleForPixelHeight(info, fh) : stbtt_ScaleForMappingEmToPixels(info, -fh);
  14567. float recip_h,recip_v,sub_x,sub_y;
  14568. spc->h_oversample = ranges[i].h_oversample;
  14569. spc->v_oversample = ranges[i].v_oversample;
  14570. recip_h = 1.0f / spc->h_oversample;
  14571. recip_v = 1.0f / spc->v_oversample;
  14572. sub_x = stbtt__oversample_shift(spc->h_oversample);
  14573. sub_y = stbtt__oversample_shift(spc->v_oversample);
  14574. for (j=0; j < ranges[i].num_chars; ++j) {
  14575. stbrp_rect *r = &rects[k];
  14576. if (r->was_packed && r->w != 0 && r->h != 0) {
  14577. stbtt_packedchar *bc = &ranges[i].chardata_for_range[j];
  14578. int advance, lsb, x0,y0,x1,y1;
  14579. int codepoint = ranges[i].array_of_unicode_codepoints == NULL ? ranges[i].first_unicode_codepoint_in_range + j : ranges[i].array_of_unicode_codepoints[j];
  14580. int glyph = stbtt_FindGlyphIndex(info, codepoint);
  14581. stbrp_coord pad = (stbrp_coord) spc->padding;
  14582. /* pad on left and top */
  14583. r->x += pad;
  14584. r->y += pad;
  14585. r->w -= pad;
  14586. r->h -= pad;
  14587. stbtt_GetGlyphHMetrics(info, glyph, &advance, &lsb);
  14588. stbtt_GetGlyphBitmapBox(info, glyph,
  14589. scale * spc->h_oversample,
  14590. scale * spc->v_oversample,
  14591. &x0,&y0,&x1,&y1);
  14592. stbtt_MakeGlyphBitmapSubpixel(info,
  14593. spc->pixels + r->x + r->y*spc->stride_in_bytes,
  14594. r->w - spc->h_oversample+1,
  14595. r->h - spc->v_oversample+1,
  14596. spc->stride_in_bytes,
  14597. scale * spc->h_oversample,
  14598. scale * spc->v_oversample,
  14599. 0,0,
  14600. glyph);
  14601. if (spc->h_oversample > 1)
  14602. stbtt__h_prefilter(spc->pixels + r->x + r->y*spc->stride_in_bytes,
  14603. r->w, r->h, spc->stride_in_bytes,
  14604. spc->h_oversample);
  14605. if (spc->v_oversample > 1)
  14606. stbtt__v_prefilter(spc->pixels + r->x + r->y*spc->stride_in_bytes,
  14607. r->w, r->h, spc->stride_in_bytes,
  14608. spc->v_oversample);
  14609. bc->x0 = (stbtt_int16) r->x;
  14610. bc->y0 = (stbtt_int16) r->y;
  14611. bc->x1 = (stbtt_int16) (r->x + r->w);
  14612. bc->y1 = (stbtt_int16) (r->y + r->h);
  14613. bc->xadvance = scale * advance;
  14614. bc->xoff = (float) x0 * recip_h + sub_x;
  14615. bc->yoff = (float) y0 * recip_v + sub_y;
  14616. bc->xoff2 = (x0 + r->w) * recip_h + sub_x;
  14617. bc->yoff2 = (y0 + r->h) * recip_v + sub_y;
  14618. if (glyph == 0)
  14619. missing_glyph = j;
  14620. } else if (spc->skip_missing) {
  14621. return_value = 0;
  14622. } else if (r->was_packed && r->w == 0 && r->h == 0 && missing_glyph >= 0) {
  14623. ranges[i].chardata_for_range[j] = ranges[i].chardata_for_range[missing_glyph];
  14624. } else {
  14625. return_value = 0; /* if any fail, report failure */
  14626. }
  14627. ++k;
  14628. }
  14629. }
  14630. /* restore original values */
  14631. spc->h_oversample = old_h_over;
  14632. spc->v_oversample = old_v_over;
  14633. return return_value;
  14634. }
  14635. STBTT_DEF void stbtt_PackFontRangesPackRects(stbtt_pack_context *spc, stbrp_rect *rects, int num_rects)
  14636. {
  14637. stbrp_pack_rects((stbrp_context *) spc->pack_info, rects, num_rects);
  14638. }
  14639. STBTT_DEF int stbtt_PackFontRanges(stbtt_pack_context *spc, const unsigned char *fontdata, int font_index, stbtt_pack_range *ranges, int num_ranges)
  14640. {
  14641. stbtt_fontinfo info;
  14642. int i,j,n, return_value = 1;
  14643. /* stbrp_context *context = (stbrp_context *) spc->pack_info; */
  14644. stbrp_rect *rects;
  14645. /* flag all characters as NOT packed */
  14646. for (i=0; i < num_ranges; ++i)
  14647. for (j=0; j < ranges[i].num_chars; ++j)
  14648. ranges[i].chardata_for_range[j].x0 =
  14649. ranges[i].chardata_for_range[j].y0 =
  14650. ranges[i].chardata_for_range[j].x1 =
  14651. ranges[i].chardata_for_range[j].y1 = 0;
  14652. n = 0;
  14653. for (i=0; i < num_ranges; ++i)
  14654. n += ranges[i].num_chars;
  14655. rects = (stbrp_rect *) STBTT_malloc(sizeof(*rects) * n, spc->user_allocator_context);
  14656. if (rects == NULL)
  14657. return 0;
  14658. info.userdata = spc->user_allocator_context;
  14659. stbtt_InitFont(&info, fontdata, stbtt_GetFontOffsetForIndex(fontdata,font_index));
  14660. n = stbtt_PackFontRangesGatherRects(spc, &info, ranges, num_ranges, rects);
  14661. stbtt_PackFontRangesPackRects(spc, rects, n);
  14662. return_value = stbtt_PackFontRangesRenderIntoRects(spc, &info, ranges, num_ranges, rects);
  14663. STBTT_free(rects, spc->user_allocator_context);
  14664. return return_value;
  14665. }
  14666. STBTT_DEF int stbtt_PackFontRange(stbtt_pack_context *spc, const unsigned char *fontdata, int font_index, float font_size,
  14667. int first_unicode_codepoint_in_range, int num_chars_in_range, stbtt_packedchar *chardata_for_range)
  14668. {
  14669. stbtt_pack_range range;
  14670. range.first_unicode_codepoint_in_range = first_unicode_codepoint_in_range;
  14671. range.array_of_unicode_codepoints = NULL;
  14672. range.num_chars = num_chars_in_range;
  14673. range.chardata_for_range = chardata_for_range;
  14674. range.font_size = font_size;
  14675. return stbtt_PackFontRanges(spc, fontdata, font_index, &range, 1);
  14676. }
  14677. STBTT_DEF void stbtt_GetScaledFontVMetrics(const unsigned char *fontdata, int index, float size, float *ascent, float *descent, float *lineGap)
  14678. {
  14679. int i_ascent, i_descent, i_lineGap;
  14680. float scale;
  14681. stbtt_fontinfo info;
  14682. stbtt_InitFont(&info, fontdata, stbtt_GetFontOffsetForIndex(fontdata, index));
  14683. scale = size > 0 ? stbtt_ScaleForPixelHeight(&info, size) : stbtt_ScaleForMappingEmToPixels(&info, -size);
  14684. stbtt_GetFontVMetrics(&info, &i_ascent, &i_descent, &i_lineGap);
  14685. *ascent = (float) i_ascent * scale;
  14686. *descent = (float) i_descent * scale;
  14687. *lineGap = (float) i_lineGap * scale;
  14688. }
  14689. 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)
  14690. {
  14691. float ipw = 1.0f / pw, iph = 1.0f / ph;
  14692. const stbtt_packedchar *b = chardata + char_index;
  14693. if (align_to_integer) {
  14694. float x = (float) STBTT_ifloor((*xpos + b->xoff) + 0.5f);
  14695. float y = (float) STBTT_ifloor((*ypos + b->yoff) + 0.5f);
  14696. q->x0 = x;
  14697. q->y0 = y;
  14698. q->x1 = x + b->xoff2 - b->xoff;
  14699. q->y1 = y + b->yoff2 - b->yoff;
  14700. } else {
  14701. q->x0 = *xpos + b->xoff;
  14702. q->y0 = *ypos + b->yoff;
  14703. q->x1 = *xpos + b->xoff2;
  14704. q->y1 = *ypos + b->yoff2;
  14705. }
  14706. q->s0 = b->x0 * ipw;
  14707. q->t0 = b->y0 * iph;
  14708. q->s1 = b->x1 * ipw;
  14709. q->t1 = b->y1 * iph;
  14710. *xpos += b->xadvance;
  14711. }
  14712. /* //////////////////////////////////////////////////////////////////////////// */
  14713. /* */
  14714. /* sdf computation */
  14715. /* */
  14716. #define STBTT_min(a,b) ((a) < (b) ? (a) : (b))
  14717. #define STBTT_max(a,b) ((a) < (b) ? (b) : (a))
  14718. 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])
  14719. {
  14720. float q0perp = q0[1]*ray[0] - q0[0]*ray[1];
  14721. float q1perp = q1[1]*ray[0] - q1[0]*ray[1];
  14722. float q2perp = q2[1]*ray[0] - q2[0]*ray[1];
  14723. float roperp = orig[1]*ray[0] - orig[0]*ray[1];
  14724. float a = q0perp - 2*q1perp + q2perp;
  14725. float b = q1perp - q0perp;
  14726. float c = q0perp - roperp;
  14727. float s0 = 0., s1 = 0.;
  14728. int num_s = 0;
  14729. if (a != 0.0) {
  14730. float discr = b*b - a*c;
  14731. if (discr > 0.0) {
  14732. float rcpna = -1 / a;
  14733. float d = (float) STBTT_sqrt(discr);
  14734. s0 = (b+d) * rcpna;
  14735. s1 = (b-d) * rcpna;
  14736. if (s0 >= 0.0 && s0 <= 1.0)
  14737. num_s = 1;
  14738. if (d > 0.0 && s1 >= 0.0 && s1 <= 1.0) {
  14739. if (num_s == 0) s0 = s1;
  14740. ++num_s;
  14741. }
  14742. }
  14743. } else {
  14744. /* 2*b*s + c = 0 */
  14745. /* s = -c / (2*b) */
  14746. s0 = c / (-2 * b);
  14747. if (s0 >= 0.0 && s0 <= 1.0)
  14748. num_s = 1;
  14749. }
  14750. if (num_s == 0)
  14751. return 0;
  14752. else {
  14753. float rcp_len2 = 1 / (ray[0]*ray[0] + ray[1]*ray[1]);
  14754. float rayn_x = ray[0] * rcp_len2, rayn_y = ray[1] * rcp_len2;
  14755. float q0d = q0[0]*rayn_x + q0[1]*rayn_y;
  14756. float q1d = q1[0]*rayn_x + q1[1]*rayn_y;
  14757. float q2d = q2[0]*rayn_x + q2[1]*rayn_y;
  14758. float rod = orig[0]*rayn_x + orig[1]*rayn_y;
  14759. float q10d = q1d - q0d;
  14760. float q20d = q2d - q0d;
  14761. float q0rd = q0d - rod;
  14762. hits[0][0] = q0rd + s0*(2.0f - 2.0f*s0)*q10d + s0*s0*q20d;
  14763. hits[0][1] = a*s0+b;
  14764. if (num_s > 1) {
  14765. hits[1][0] = q0rd + s1*(2.0f - 2.0f*s1)*q10d + s1*s1*q20d;
  14766. hits[1][1] = a*s1+b;
  14767. return 2;
  14768. } else {
  14769. return 1;
  14770. }
  14771. }
  14772. }
  14773. static int equal(float *a, float *b)
  14774. {
  14775. return (a[0] == b[0] && a[1] == b[1]);
  14776. }
  14777. static int stbtt__compute_crossings_x(float x, float y, int nverts, stbtt_vertex *verts)
  14778. {
  14779. int i;
  14780. float orig[2], ray[2] = { 1, 0 };
  14781. float y_frac;
  14782. int winding = 0;
  14783. /* make sure y never passes through a vertex of the shape */
  14784. y_frac = (float) STBTT_fmod(y, 1.0f);
  14785. if (y_frac < 0.01f)
  14786. y += 0.01f;
  14787. else if (y_frac > 0.99f)
  14788. y -= 0.01f;
  14789. orig[0] = x;
  14790. orig[1] = y;
  14791. /* test a ray from (-infinity,y) to (x,y) */
  14792. for (i=0; i < nverts; ++i) {
  14793. if (verts[i].type == STBTT_vline) {
  14794. int x0 = (int) verts[i-1].x, y0 = (int) verts[i-1].y;
  14795. int x1 = (int) verts[i ].x, y1 = (int) verts[i ].y;
  14796. if (y > STBTT_min(y0,y1) && y < STBTT_max(y0,y1) && x > STBTT_min(x0,x1)) {
  14797. float x_inter = (y - y0) / (y1 - y0) * (x1-x0) + x0;
  14798. if (x_inter < x)
  14799. winding += (y0 < y1) ? 1 : -1;
  14800. }
  14801. }
  14802. if (verts[i].type == STBTT_vcurve) {
  14803. int x0 = (int) verts[i-1].x , y0 = (int) verts[i-1].y ;
  14804. int x1 = (int) verts[i ].cx, y1 = (int) verts[i ].cy;
  14805. int x2 = (int) verts[i ].x , y2 = (int) verts[i ].y ;
  14806. int ax = STBTT_min(x0,STBTT_min(x1,x2)), ay = STBTT_min(y0,STBTT_min(y1,y2));
  14807. int by = STBTT_max(y0,STBTT_max(y1,y2));
  14808. if (y > ay && y < by && x > ax) {
  14809. float q0[2],q1[2],q2[2];
  14810. float hits[2][2];
  14811. q0[0] = (float)x0;
  14812. q0[1] = (float)y0;
  14813. q1[0] = (float)x1;
  14814. q1[1] = (float)y1;
  14815. q2[0] = (float)x2;
  14816. q2[1] = (float)y2;
  14817. if (equal(q0,q1) || equal(q1,q2)) {
  14818. x0 = (int)verts[i-1].x;
  14819. y0 = (int)verts[i-1].y;
  14820. x1 = (int)verts[i ].x;
  14821. y1 = (int)verts[i ].y;
  14822. if (y > STBTT_min(y0,y1) && y < STBTT_max(y0,y1) && x > STBTT_min(x0,x1)) {
  14823. float x_inter = (y - y0) / (y1 - y0) * (x1-x0) + x0;
  14824. if (x_inter < x)
  14825. winding += (y0 < y1) ? 1 : -1;
  14826. }
  14827. } else {
  14828. int num_hits = stbtt__ray_intersect_bezier(orig, ray, q0, q1, q2, hits);
  14829. if (num_hits >= 1)
  14830. if (hits[0][0] < 0)
  14831. winding += (hits[0][1] < 0 ? -1 : 1);
  14832. if (num_hits >= 2)
  14833. if (hits[1][0] < 0)
  14834. winding += (hits[1][1] < 0 ? -1 : 1);
  14835. }
  14836. }
  14837. }
  14838. }
  14839. return winding;
  14840. }
  14841. static float stbtt__cuberoot( float x )
  14842. {
  14843. if (x<0)
  14844. return -(float) STBTT_pow(-x,1.0f/3.0f);
  14845. else
  14846. return (float) STBTT_pow( x,1.0f/3.0f);
  14847. }
  14848. /* x^3 + a*x^2 + b*x + c = 0 */
  14849. static int stbtt__solve_cubic(float a, float b, float c, float* r)
  14850. {
  14851. float s = -a / 3;
  14852. float p = b - a*a / 3;
  14853. float q = a * (2*a*a - 9*b) / 27 + c;
  14854. float p3 = p*p*p;
  14855. float d = q*q + 4*p3 / 27;
  14856. if (d >= 0) {
  14857. float z = (float) STBTT_sqrt(d);
  14858. float u = (-q + z) / 2;
  14859. float v = (-q - z) / 2;
  14860. u = stbtt__cuberoot(u);
  14861. v = stbtt__cuberoot(v);
  14862. r[0] = s + u + v;
  14863. return 1;
  14864. } else {
  14865. float u = (float) STBTT_sqrt(-p/3);
  14866. float v = (float) STBTT_acos(-STBTT_sqrt(-27/p3) * q / 2) / 3; /* p3 must be negative, since d is negative */
  14867. float m = (float) STBTT_cos(v);
  14868. float n = (float) STBTT_cos(v-3.141592/2)*1.732050808f;
  14869. r[0] = s + u * 2 * m;
  14870. r[1] = s - u * (m + n);
  14871. r[2] = s - u * (m - n);
  14872. /* 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? */
  14873. /* STBTT_assert( STBTT_fabs(((r[1]+a)*r[1]+b)*r[1]+c) < 0.05f); */
  14874. /* STBTT_assert( STBTT_fabs(((r[2]+a)*r[2]+b)*r[2]+c) < 0.05f); */
  14875. return 3;
  14876. }
  14877. }
  14878. 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)
  14879. {
  14880. float scale_x = scale, scale_y = scale;
  14881. int ix0,iy0,ix1,iy1;
  14882. int w,h;
  14883. unsigned char *data;
  14884. if (scale == 0) return NULL;
  14885. stbtt_GetGlyphBitmapBoxSubpixel(info, glyph, scale, scale, 0.0f,0.0f, &ix0,&iy0,&ix1,&iy1);
  14886. /* if empty, return NULL */
  14887. if (ix0 == ix1 || iy0 == iy1)
  14888. return NULL;
  14889. ix0 -= padding;
  14890. iy0 -= padding;
  14891. ix1 += padding;
  14892. iy1 += padding;
  14893. w = (ix1 - ix0);
  14894. h = (iy1 - iy0);
  14895. if (width ) *width = w;
  14896. if (height) *height = h;
  14897. if (xoff ) *xoff = ix0;
  14898. if (yoff ) *yoff = iy0;
  14899. /* invert for y-downwards bitmaps */
  14900. scale_y = -scale_y;
  14901. {
  14902. int x,y,i,j;
  14903. float *precompute;
  14904. stbtt_vertex *verts;
  14905. int num_verts = stbtt_GetGlyphShape(info, glyph, &verts);
  14906. data = (unsigned char *) STBTT_malloc(w * h, info->userdata);
  14907. precompute = (float *) STBTT_malloc(num_verts * sizeof(float), info->userdata);
  14908. for (i=0,j=num_verts-1; i < num_verts; j=i++) {
  14909. if (verts[i].type == STBTT_vline) {
  14910. float x0 = verts[i].x*scale_x, y0 = verts[i].y*scale_y;
  14911. float x1 = verts[j].x*scale_x, y1 = verts[j].y*scale_y;
  14912. float dist = (float) STBTT_sqrt((x1-x0)*(x1-x0) + (y1-y0)*(y1-y0));
  14913. precompute[i] = (dist == 0) ? 0.0f : 1.0f / dist;
  14914. } else if (verts[i].type == STBTT_vcurve) {
  14915. float x2 = verts[j].x *scale_x, y2 = verts[j].y *scale_y;
  14916. float x1 = verts[i].cx*scale_x, y1 = verts[i].cy*scale_y;
  14917. float x0 = verts[i].x *scale_x, y0 = verts[i].y *scale_y;
  14918. float bx = x0 - 2*x1 + x2, by = y0 - 2*y1 + y2;
  14919. float len2 = bx*bx + by*by;
  14920. if (len2 != 0.0f)
  14921. precompute[i] = 1.0f / (bx*bx + by*by);
  14922. else
  14923. precompute[i] = 0.0f;
  14924. } else
  14925. precompute[i] = 0.0f;
  14926. }
  14927. for (y=iy0; y < iy1; ++y) {
  14928. for (x=ix0; x < ix1; ++x) {
  14929. float val;
  14930. float min_dist = 999999.0f;
  14931. float sx = (float) x + 0.5f;
  14932. float sy = (float) y + 0.5f;
  14933. float x_gspace = (sx / scale_x);
  14934. float y_gspace = (sy / scale_y);
  14935. 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 */
  14936. for (i=0; i < num_verts; ++i) {
  14937. float x0 = verts[i].x*scale_x, y0 = verts[i].y*scale_y;
  14938. if (verts[i].type == STBTT_vline && precompute[i] != 0.0f) {
  14939. float x1 = verts[i-1].x*scale_x, y1 = verts[i-1].y*scale_y;
  14940. float dist,dist2 = (x0-sx)*(x0-sx) + (y0-sy)*(y0-sy);
  14941. if (dist2 < min_dist*min_dist)
  14942. min_dist = (float) STBTT_sqrt(dist2);
  14943. /* coarse culling against bbox */
  14944. /* if (sx > STBTT_min(x0,x1)-min_dist && sx < STBTT_max(x0,x1)+min_dist && */
  14945. /* sy > STBTT_min(y0,y1)-min_dist && sy < STBTT_max(y0,y1)+min_dist) */
  14946. dist = (float) STBTT_fabs((x1-x0)*(y0-sy) - (y1-y0)*(x0-sx)) * precompute[i];
  14947. STBTT_assert(i != 0);
  14948. if (dist < min_dist) {
  14949. /* check position along line */
  14950. /* x' = x0 + t*(x1-x0), y' = y0 + t*(y1-y0) */
  14951. /* minimize (x'-sx)*(x'-sx)+(y'-sy)*(y'-sy) */
  14952. float dx = x1-x0, dy = y1-y0;
  14953. float px = x0-sx, py = y0-sy;
  14954. /* 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 */
  14955. /* derivative: 2*px*dx + 2*py*dy + (2*dx*dx+2*dy*dy)*t, set to 0 and solve */
  14956. float t = -(px*dx + py*dy) / (dx*dx + dy*dy);
  14957. if (t >= 0.0f && t <= 1.0f)
  14958. min_dist = dist;
  14959. }
  14960. } else if (verts[i].type == STBTT_vcurve) {
  14961. float x2 = verts[i-1].x *scale_x, y2 = verts[i-1].y *scale_y;
  14962. float x1 = verts[i ].cx*scale_x, y1 = verts[i ].cy*scale_y;
  14963. float box_x0 = STBTT_min(STBTT_min(x0,x1),x2);
  14964. float box_y0 = STBTT_min(STBTT_min(y0,y1),y2);
  14965. float box_x1 = STBTT_max(STBTT_max(x0,x1),x2);
  14966. float box_y1 = STBTT_max(STBTT_max(y0,y1),y2);
  14967. /* coarse culling against bbox to avoid computing cubic unnecessarily */
  14968. if (sx > box_x0-min_dist && sx < box_x1+min_dist && sy > box_y0-min_dist && sy < box_y1+min_dist) {
  14969. int num=0;
  14970. float ax = x1-x0, ay = y1-y0;
  14971. float bx = x0 - 2*x1 + x2, by = y0 - 2*y1 + y2;
  14972. float mx = x0 - sx, my = y0 - sy;
  14973. float res[3] = {0.f,0.f,0.f};
  14974. float px,py,t,it,dist2;
  14975. float a_inv = precompute[i];
  14976. if (a_inv == 0.0) { /* if a_inv is 0, it's 2nd degree so use quadratic formula */
  14977. float a = 3*(ax*bx + ay*by);
  14978. float b = 2*(ax*ax + ay*ay) + (mx*bx+my*by);
  14979. float c = mx*ax+my*ay;
  14980. if (a == 0.0) { /* if a is 0, it's linear */
  14981. if (b != 0.0) {
  14982. res[num++] = -c/b;
  14983. }
  14984. } else {
  14985. float discriminant = b*b - 4*a*c;
  14986. if (discriminant < 0)
  14987. num = 0;
  14988. else {
  14989. float root = (float) STBTT_sqrt(discriminant);
  14990. res[0] = (-b - root)/(2*a);
  14991. res[1] = (-b + root)/(2*a);
  14992. num = 2; /* don't bother distinguishing 1-solution case, as code below will still work */
  14993. }
  14994. }
  14995. } else {
  14996. float b = 3*(ax*bx + ay*by) * a_inv; /* could precompute this as it doesn't depend on sample point */
  14997. float c = (2*(ax*ax + ay*ay) + (mx*bx+my*by)) * a_inv;
  14998. float d = (mx*ax+my*ay) * a_inv;
  14999. num = stbtt__solve_cubic(b, c, d, res);
  15000. }
  15001. dist2 = (x0-sx)*(x0-sx) + (y0-sy)*(y0-sy);
  15002. if (dist2 < min_dist*min_dist)
  15003. min_dist = (float) STBTT_sqrt(dist2);
  15004. if (num >= 1 && res[0] >= 0.0f && res[0] <= 1.0f) {
  15005. t = res[0], it = 1.0f - t;
  15006. px = it*it*x0 + 2*t*it*x1 + t*t*x2;
  15007. py = it*it*y0 + 2*t*it*y1 + t*t*y2;
  15008. dist2 = (px-sx)*(px-sx) + (py-sy)*(py-sy);
  15009. if (dist2 < min_dist * min_dist)
  15010. min_dist = (float) STBTT_sqrt(dist2);
  15011. }
  15012. if (num >= 2 && res[1] >= 0.0f && res[1] <= 1.0f) {
  15013. t = res[1], it = 1.0f - t;
  15014. px = it*it*x0 + 2*t*it*x1 + t*t*x2;
  15015. py = it*it*y0 + 2*t*it*y1 + t*t*y2;
  15016. dist2 = (px-sx)*(px-sx) + (py-sy)*(py-sy);
  15017. if (dist2 < min_dist * min_dist)
  15018. min_dist = (float) STBTT_sqrt(dist2);
  15019. }
  15020. if (num >= 3 && res[2] >= 0.0f && res[2] <= 1.0f) {
  15021. t = res[2], it = 1.0f - t;
  15022. px = it*it*x0 + 2*t*it*x1 + t*t*x2;
  15023. py = it*it*y0 + 2*t*it*y1 + t*t*y2;
  15024. dist2 = (px-sx)*(px-sx) + (py-sy)*(py-sy);
  15025. if (dist2 < min_dist * min_dist)
  15026. min_dist = (float) STBTT_sqrt(dist2);
  15027. }
  15028. }
  15029. }
  15030. }
  15031. if (winding == 0)
  15032. min_dist = -min_dist; /* if outside the shape, value is negative */
  15033. val = onedge_value + pixel_dist_scale * min_dist;
  15034. if (val < 0)
  15035. val = 0;
  15036. else if (val > 255)
  15037. val = 255;
  15038. data[(y-iy0)*w+(x-ix0)] = (unsigned char) val;
  15039. }
  15040. }
  15041. STBTT_free(precompute, info->userdata);
  15042. STBTT_free(verts, info->userdata);
  15043. }
  15044. return data;
  15045. }
  15046. 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)
  15047. {
  15048. return stbtt_GetGlyphSDF(info, scale, stbtt_FindGlyphIndex(info, codepoint), padding, onedge_value, pixel_dist_scale, width, height, xoff, yoff);
  15049. }
  15050. STBTT_DEF void stbtt_FreeSDF(unsigned char *bitmap, void *userdata)
  15051. {
  15052. STBTT_free(bitmap, userdata);
  15053. }
  15054. /* //////////////////////////////////////////////////////////////////////////// */
  15055. /* */
  15056. /* font name matching -- recommended not to use this */
  15057. /* */
  15058. /* check if a utf8 string contains a prefix which is the utf16 string; if so return length of matching utf8 string */
  15059. static stbtt_int32 stbtt__CompareUTF8toUTF16_bigendian_prefix(stbtt_uint8 *s1, stbtt_int32 len1, stbtt_uint8 *s2, stbtt_int32 len2)
  15060. {
  15061. stbtt_int32 i=0;
  15062. /* convert utf16 to utf8 and compare the results while converting */
  15063. while (len2) {
  15064. stbtt_uint16 ch = s2[0]*256 + s2[1];
  15065. if (ch < 0x80) {
  15066. if (i >= len1) return -1;
  15067. if (s1[i++] != ch) return -1;
  15068. } else if (ch < 0x800) {
  15069. if (i+1 >= len1) return -1;
  15070. if (s1[i++] != 0xc0 + (ch >> 6)) return -1;
  15071. if (s1[i++] != 0x80 + (ch & 0x3f)) return -1;
  15072. } else if (ch >= 0xd800 && ch < 0xdc00) {
  15073. stbtt_uint32 c;
  15074. stbtt_uint16 ch2 = s2[2]*256 + s2[3];
  15075. if (i+3 >= len1) return -1;
  15076. c = ((ch - 0xd800) << 10) + (ch2 - 0xdc00) + 0x10000;
  15077. if (s1[i++] != 0xf0 + (c >> 18)) return -1;
  15078. if (s1[i++] != 0x80 + ((c >> 12) & 0x3f)) return -1;
  15079. if (s1[i++] != 0x80 + ((c >> 6) & 0x3f)) return -1;
  15080. if (s1[i++] != 0x80 + ((c ) & 0x3f)) return -1;
  15081. s2 += 2; /* plus another 2 below */
  15082. len2 -= 2;
  15083. } else if (ch >= 0xdc00 && ch < 0xe000) {
  15084. return -1;
  15085. } else {
  15086. if (i+2 >= len1) return -1;
  15087. if (s1[i++] != 0xe0 + (ch >> 12)) return -1;
  15088. if (s1[i++] != 0x80 + ((ch >> 6) & 0x3f)) return -1;
  15089. if (s1[i++] != 0x80 + ((ch ) & 0x3f)) return -1;
  15090. }
  15091. s2 += 2;
  15092. len2 -= 2;
  15093. }
  15094. return i;
  15095. }
  15096. static int stbtt_CompareUTF8toUTF16_bigendian_internal(char *s1, int len1, char *s2, int len2)
  15097. {
  15098. return len1 == stbtt__CompareUTF8toUTF16_bigendian_prefix((stbtt_uint8*) s1, len1, (stbtt_uint8*) s2, len2);
  15099. }
  15100. /* returns results in whatever encoding you request... but note that 2-byte encodings */
  15101. /* will be BIG-ENDIAN... use stbtt_CompareUTF8toUTF16_bigendian() to compare */
  15102. STBTT_DEF const char *stbtt_GetFontNameString(const stbtt_fontinfo *font, int *length, int platformID, int encodingID, int languageID, int nameID)
  15103. {
  15104. stbtt_int32 i,count,stringOffset;
  15105. stbtt_uint8 *fc = font->data;
  15106. stbtt_uint32 offset = font->fontstart;
  15107. stbtt_uint32 nm = stbtt__find_table(fc, offset, "name");
  15108. if (!nm) return NULL;
  15109. count = ttUSHORT(fc+nm+2);
  15110. stringOffset = nm + ttUSHORT(fc+nm+4);
  15111. for (i=0; i < count; ++i) {
  15112. stbtt_uint32 loc = nm + 6 + 12 * i;
  15113. if (platformID == ttUSHORT(fc+loc+0) && encodingID == ttUSHORT(fc+loc+2)
  15114. && languageID == ttUSHORT(fc+loc+4) && nameID == ttUSHORT(fc+loc+6)) {
  15115. *length = ttUSHORT(fc+loc+8);
  15116. return (const char *) (fc+stringOffset+ttUSHORT(fc+loc+10));
  15117. }
  15118. }
  15119. return NULL;
  15120. }
  15121. static int stbtt__matchpair(stbtt_uint8 *fc, stbtt_uint32 nm, stbtt_uint8 *name, stbtt_int32 nlen, stbtt_int32 target_id, stbtt_int32 next_id)
  15122. {
  15123. stbtt_int32 i;
  15124. stbtt_int32 count = ttUSHORT(fc+nm+2);
  15125. stbtt_int32 stringOffset = nm + ttUSHORT(fc+nm+4);
  15126. for (i=0; i < count; ++i) {
  15127. stbtt_uint32 loc = nm + 6 + 12 * i;
  15128. stbtt_int32 id = ttUSHORT(fc+loc+6);
  15129. if (id == target_id) {
  15130. /* find the encoding */
  15131. stbtt_int32 platform = ttUSHORT(fc+loc+0), encoding = ttUSHORT(fc+loc+2), language = ttUSHORT(fc+loc+4);
  15132. /* is this a Unicode encoding? */
  15133. if (platform == 0 || (platform == 3 && encoding == 1) || (platform == 3 && encoding == 10)) {
  15134. stbtt_int32 slen = ttUSHORT(fc+loc+8);
  15135. stbtt_int32 off = ttUSHORT(fc+loc+10);
  15136. /* check if there's a prefix match */
  15137. stbtt_int32 matchlen = stbtt__CompareUTF8toUTF16_bigendian_prefix(name, nlen, fc+stringOffset+off,slen);
  15138. if (matchlen >= 0) {
  15139. /* check for target_id+1 immediately following, with same encoding & language */
  15140. 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) {
  15141. slen = ttUSHORT(fc+loc+12+8);
  15142. off = ttUSHORT(fc+loc+12+10);
  15143. if (slen == 0) {
  15144. if (matchlen == nlen)
  15145. return 1;
  15146. } else if (matchlen < nlen && name[matchlen] == ' ') {
  15147. ++matchlen;
  15148. if (stbtt_CompareUTF8toUTF16_bigendian_internal((char*) (name+matchlen), nlen-matchlen, (char*)(fc+stringOffset+off),slen))
  15149. return 1;
  15150. }
  15151. } else {
  15152. /* if nothing immediately following */
  15153. if (matchlen == nlen)
  15154. return 1;
  15155. }
  15156. }
  15157. }
  15158. /* @TODO handle other encodings */
  15159. }
  15160. }
  15161. return 0;
  15162. }
  15163. static int stbtt__matches(stbtt_uint8 *fc, stbtt_uint32 offset, stbtt_uint8 *name, stbtt_int32 flags)
  15164. {
  15165. stbtt_int32 nlen = (stbtt_int32) STBTT_strlen((char *) name);
  15166. stbtt_uint32 nm,hd;
  15167. if (!stbtt__isfont(fc+offset)) return 0;
  15168. /* check italics/bold/underline flags in macStyle... */
  15169. if (flags) {
  15170. hd = stbtt__find_table(fc, offset, "head");
  15171. if ((ttUSHORT(fc+hd+44) & 7) != (flags & 7)) return 0;
  15172. }
  15173. nm = stbtt__find_table(fc, offset, "name");
  15174. if (!nm) return 0;
  15175. if (flags) {
  15176. /* if we checked the macStyle flags, then just check the family and ignore the subfamily */
  15177. if (stbtt__matchpair(fc, nm, name, nlen, 16, -1)) return 1;
  15178. if (stbtt__matchpair(fc, nm, name, nlen, 1, -1)) return 1;
  15179. if (stbtt__matchpair(fc, nm, name, nlen, 3, -1)) return 1;
  15180. } else {
  15181. if (stbtt__matchpair(fc, nm, name, nlen, 16, 17)) return 1;
  15182. if (stbtt__matchpair(fc, nm, name, nlen, 1, 2)) return 1;
  15183. if (stbtt__matchpair(fc, nm, name, nlen, 3, -1)) return 1;
  15184. }
  15185. return 0;
  15186. }
  15187. static int stbtt_FindMatchingFont_internal(unsigned char *font_collection, char *name_utf8, stbtt_int32 flags)
  15188. {
  15189. stbtt_int32 i;
  15190. for (i=0;;++i) {
  15191. stbtt_int32 off = stbtt_GetFontOffsetForIndex(font_collection, i);
  15192. if (off < 0) return off;
  15193. if (stbtt__matches((stbtt_uint8 *) font_collection, off, (stbtt_uint8*) name_utf8, flags))
  15194. return off;
  15195. }
  15196. }
  15197. #if defined(__GNUC__) || defined(__clang__)
  15198. #pragma GCC diagnostic push
  15199. #pragma GCC diagnostic ignored "-Wcast-qual"
  15200. #endif
  15201. STBTT_DEF int stbtt_BakeFontBitmap(const unsigned char *data, int offset,
  15202. float pixel_height, unsigned char *pixels, int pw, int ph,
  15203. int first_char, int num_chars, stbtt_bakedchar *chardata)
  15204. {
  15205. return stbtt_BakeFontBitmap_internal((unsigned char *) data, offset, pixel_height, pixels, pw, ph, first_char, num_chars, chardata);
  15206. }
  15207. STBTT_DEF int stbtt_GetFontOffsetForIndex(const unsigned char *data, int index)
  15208. {
  15209. return stbtt_GetFontOffsetForIndex_internal((unsigned char *) data, index);
  15210. }
  15211. STBTT_DEF int stbtt_GetNumberOfFonts(const unsigned char *data)
  15212. {
  15213. return stbtt_GetNumberOfFonts_internal((unsigned char *) data);
  15214. }
  15215. STBTT_DEF int stbtt_InitFont(stbtt_fontinfo *info, const unsigned char *data, int offset)
  15216. {
  15217. return stbtt_InitFont_internal(info, (unsigned char *) data, offset);
  15218. }
  15219. STBTT_DEF int stbtt_FindMatchingFont(const unsigned char *fontdata, const char *name, int flags)
  15220. {
  15221. return stbtt_FindMatchingFont_internal((unsigned char *) fontdata, (char *) name, flags);
  15222. }
  15223. STBTT_DEF int stbtt_CompareUTF8toUTF16_bigendian(const char *s1, int len1, const char *s2, int len2)
  15224. {
  15225. return stbtt_CompareUTF8toUTF16_bigendian_internal((char *) s1, len1, (char *) s2, len2);
  15226. }
  15227. #if defined(__GNUC__) || defined(__clang__)
  15228. #pragma GCC diagnostic pop
  15229. #endif
  15230. #endif /* STB_TRUETYPE_IMPLEMENTATION */
  15231. /* FULL VERSION HISTORY */
  15232. /* */
  15233. /* 1.25 (2021-07-11) many fixes */
  15234. /* 1.24 (2020-02-05) fix warning */
  15235. /* 1.23 (2020-02-02) query SVG data for glyphs; query whole kerning table (but only kern not GPOS) */
  15236. /* 1.22 (2019-08-11) minimize missing-glyph duplication; fix kerning if both 'GPOS' and 'kern' are defined */
  15237. /* 1.21 (2019-02-25) fix warning */
  15238. /* 1.20 (2019-02-07) PackFontRange skips missing codepoints; GetScaleFontVMetrics() */
  15239. /* 1.19 (2018-02-11) OpenType GPOS kerning (horizontal only), STBTT_fmod */
  15240. /* 1.18 (2018-01-29) add missing function */
  15241. /* 1.17 (2017-07-23) make more arguments const; doc fix */
  15242. /* 1.16 (2017-07-12) SDF support */
  15243. /* 1.15 (2017-03-03) make more arguments const */
  15244. /* 1.14 (2017-01-16) num-fonts-in-TTC function */
  15245. /* 1.13 (2017-01-02) support OpenType fonts, certain Apple fonts */
  15246. /* 1.12 (2016-10-25) suppress warnings about casting away const with -Wcast-qual */
  15247. /* 1.11 (2016-04-02) fix unused-variable warning */
  15248. /* 1.10 (2016-04-02) allow user-defined fabs() replacement */
  15249. /* fix memory leak if fontsize=0.0 */
  15250. /* fix warning from duplicate typedef */
  15251. /* 1.09 (2016-01-16) warning fix; avoid crash on outofmem; use alloc userdata for PackFontRanges */
  15252. /* 1.08 (2015-09-13) document stbtt_Rasterize(); fixes for vertical & horizontal edges */
  15253. /* 1.07 (2015-08-01) allow PackFontRanges to accept arrays of sparse codepoints; */
  15254. /* allow PackFontRanges to pack and render in separate phases; */
  15255. /* fix stbtt_GetFontOFfsetForIndex (never worked for non-0 input?); */
  15256. /* fixed an assert() bug in the new rasterizer */
  15257. /* replace assert() with STBTT_assert() in new rasterizer */
  15258. /* 1.06 (2015-07-14) performance improvements (~35% faster on x86 and x64 on test machine) */
  15259. /* also more precise AA rasterizer, except if shapes overlap */
  15260. /* remove need for STBTT_sort */
  15261. /* 1.05 (2015-04-15) fix misplaced definitions for STBTT_STATIC */
  15262. /* 1.04 (2015-04-15) typo in example */
  15263. /* 1.03 (2015-04-12) STBTT_STATIC, fix memory leak in new packing, various fixes */
  15264. /* 1.02 (2014-12-10) fix various warnings & compile issues w/ stb_rect_pack, C++ */
  15265. /* 1.01 (2014-12-08) fix subpixel position when oversampling to exactly match */
  15266. /* non-oversampled; STBTT_POINT_SIZE for packed case only */
  15267. /* 1.00 (2014-12-06) add new PackBegin etc. API, w/ support for oversampling */
  15268. /* 0.99 (2014-09-18) fix multiple bugs with subpixel rendering (ryg) */
  15269. /* 0.9 (2014-08-07) support certain mac/iOS fonts without an MS platformID */
  15270. /* 0.8b (2014-07-07) fix a warning */
  15271. /* 0.8 (2014-05-25) fix a few more warnings */
  15272. /* 0.7 (2013-09-25) bugfix: subpixel glyph bug fixed in 0.5 had come back */
  15273. /* 0.6c (2012-07-24) improve documentation */
  15274. /* 0.6b (2012-07-20) fix a few more warnings */
  15275. /* 0.6 (2012-07-17) fix warnings; added stbtt_ScaleForMappingEmToPixels, */
  15276. /* stbtt_GetFontBoundingBox, stbtt_IsGlyphEmpty */
  15277. /* 0.5 (2011-12-09) bugfixes: */
  15278. /* subpixel glyph renderer computed wrong bounding box */
  15279. /* first vertex of shape can be off-curve (FreeSans) */
  15280. /* 0.4b (2011-12-03) fixed an error in the font baking example */
  15281. /* 0.4 (2011-12-01) kerning, subpixel rendering (tor) */
  15282. /* bugfixes for: */
  15283. /* codepoint-to-glyph conversion using table fmt=12 */
  15284. /* codepoint-to-glyph conversion using table fmt=4 */
  15285. /* stbtt_GetBakedQuad with non-square texture (Zer) */
  15286. /* updated Hello World! sample to use kerning and subpixel */
  15287. /* fixed some warnings */
  15288. /* 0.3 (2009-06-24) cmap fmt=12, compound shapes (MM) */
  15289. /* userdata, malloc-from-userdata, non-zero fill (stb) */
  15290. /* 0.2 (2009-03-11) Fix unsigned/signed char warnings */
  15291. /* 0.1 (2009-03-09) First public release */
  15292. /* */
  15293. /*
  15294. ------------------------------------------------------------------------------
  15295. This software is available under 2 licenses -- choose whichever you prefer.
  15296. ------------------------------------------------------------------------------
  15297. ALTERNATIVE A - MIT License
  15298. Copyright (c) 2017 Sean Barrett
  15299. Permission is hereby granted, free of charge, to any person obtaining a copy of
  15300. this software and associated documentation files (the "Software"), to deal in
  15301. the Software without restriction, including without limitation the rights to
  15302. use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
  15303. of the Software, and to permit persons to whom the Software is furnished to do
  15304. so, subject to the following conditions:
  15305. The above copyright notice and this permission notice shall be included in all
  15306. copies or substantial portions of the Software.
  15307. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15308. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15309. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15310. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15311. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  15312. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  15313. SOFTWARE.
  15314. ------------------------------------------------------------------------------
  15315. ALTERNATIVE B - Public Domain (www.unlicense.org)
  15316. This is free and unencumbered software released into the public domain.
  15317. Anyone is free to copy, modify, publish, use, compile, sell, or distribute this
  15318. software, either in source code form or as a compiled binary, for any purpose,
  15319. commercial or non-commercial, and by any means.
  15320. In jurisdictions that recognize copyright laws, the author or authors of this
  15321. software dedicate any and all copyright interest in the software to the public
  15322. domain. We make this dedication for the benefit of the public at large and to
  15323. the detriment of our heirs and successors. We intend this dedication to be an
  15324. overt act of relinquishment in perpetuity of all present and future rights to
  15325. this software under copyright law.
  15326. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15327. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15328. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15329. AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  15330. ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  15331. WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  15332. ------------------------------------------------------------------------------
  15333. */
  15334. #ifdef NK_INCLUDE_FONT_BAKING
  15335. /* -------------------------------------------------------------
  15336. *
  15337. * RECT PACK
  15338. *
  15339. * --------------------------------------------------------------*/
  15340. /*
  15341. * ==============================================================
  15342. *
  15343. * TRUETYPE
  15344. *
  15345. * ===============================================================
  15346. */
  15347. #define STBTT_MAX_OVERSAMPLE 8
  15348. /* -------------------------------------------------------------
  15349. *
  15350. * FONT BAKING
  15351. *
  15352. * --------------------------------------------------------------*/
  15353. struct nk_font_bake_data {
  15354. struct stbtt_fontinfo info;
  15355. struct stbrp_rect *rects;
  15356. stbtt_pack_range *ranges;
  15357. nk_rune range_count;
  15358. };
  15359. struct nk_font_baker {
  15360. struct nk_allocator alloc;
  15361. struct stbtt_pack_context spc;
  15362. struct nk_font_bake_data *build;
  15363. stbtt_packedchar *packed_chars;
  15364. struct stbrp_rect *rects;
  15365. stbtt_pack_range *ranges;
  15366. };
  15367. NK_GLOBAL const nk_size nk_rect_align = NK_ALIGNOF(struct stbrp_rect);
  15368. NK_GLOBAL const nk_size nk_range_align = NK_ALIGNOF(stbtt_pack_range);
  15369. NK_GLOBAL const nk_size nk_char_align = NK_ALIGNOF(stbtt_packedchar);
  15370. NK_GLOBAL const nk_size nk_build_align = NK_ALIGNOF(struct nk_font_bake_data);
  15371. NK_GLOBAL const nk_size nk_baker_align = NK_ALIGNOF(struct nk_font_baker);
  15372. NK_INTERN int
  15373. nk_range_count(const nk_rune *range)
  15374. {
  15375. const nk_rune *iter = range;
  15376. NK_ASSERT(range);
  15377. if (!range) return 0;
  15378. while (*(iter++) != 0);
  15379. return (iter == range) ? 0 : (int)((iter - range)/2);
  15380. }
  15381. NK_INTERN int
  15382. nk_range_glyph_count(const nk_rune *range, int count)
  15383. {
  15384. int i = 0;
  15385. int total_glyphs = 0;
  15386. for (i = 0; i < count; ++i) {
  15387. int diff;
  15388. nk_rune f = range[(i*2)+0];
  15389. nk_rune t = range[(i*2)+1];
  15390. NK_ASSERT(t >= f);
  15391. diff = (int)((t - f) + 1);
  15392. total_glyphs += diff;
  15393. }
  15394. return total_glyphs;
  15395. }
  15396. NK_API const nk_rune*
  15397. nk_font_default_glyph_ranges(void)
  15398. {
  15399. NK_STORAGE const nk_rune ranges[] = {0x0020, 0x00FF, 0};
  15400. return ranges;
  15401. }
  15402. NK_API const nk_rune*
  15403. nk_font_chinese_glyph_ranges(void)
  15404. {
  15405. NK_STORAGE const nk_rune ranges[] = {
  15406. 0x0020, 0x00FF,
  15407. 0x3000, 0x30FF,
  15408. 0x31F0, 0x31FF,
  15409. 0xFF00, 0xFFEF,
  15410. 0x4E00, 0x9FAF,
  15411. 0
  15412. };
  15413. return ranges;
  15414. }
  15415. NK_API const nk_rune*
  15416. nk_font_cyrillic_glyph_ranges(void)
  15417. {
  15418. NK_STORAGE const nk_rune ranges[] = {
  15419. 0x0020, 0x00FF,
  15420. 0x0400, 0x052F,
  15421. 0x2DE0, 0x2DFF,
  15422. 0xA640, 0xA69F,
  15423. 0
  15424. };
  15425. return ranges;
  15426. }
  15427. NK_API const nk_rune*
  15428. nk_font_korean_glyph_ranges(void)
  15429. {
  15430. NK_STORAGE const nk_rune ranges[] = {
  15431. 0x0020, 0x00FF,
  15432. 0x3131, 0x3163,
  15433. 0xAC00, 0xD79D,
  15434. 0
  15435. };
  15436. return ranges;
  15437. }
  15438. NK_INTERN void
  15439. nk_font_baker_memory(nk_size *temp, int *glyph_count,
  15440. struct nk_font_config *config_list, int count)
  15441. {
  15442. int range_count = 0;
  15443. int total_range_count = 0;
  15444. struct nk_font_config *iter, *i;
  15445. NK_ASSERT(config_list);
  15446. NK_ASSERT(glyph_count);
  15447. if (!config_list) {
  15448. *temp = 0;
  15449. *glyph_count = 0;
  15450. return;
  15451. }
  15452. *glyph_count = 0;
  15453. for (iter = config_list; iter; iter = iter->next) {
  15454. i = iter;
  15455. do {if (!i->range) iter->range = nk_font_default_glyph_ranges();
  15456. range_count = nk_range_count(i->range);
  15457. total_range_count += range_count;
  15458. *glyph_count += nk_range_glyph_count(i->range, range_count);
  15459. } while ((i = i->n) != iter);
  15460. }
  15461. *temp = (nk_size)*glyph_count * sizeof(struct stbrp_rect);
  15462. *temp += (nk_size)total_range_count * sizeof(stbtt_pack_range);
  15463. *temp += (nk_size)*glyph_count * sizeof(stbtt_packedchar);
  15464. *temp += (nk_size)count * sizeof(struct nk_font_bake_data);
  15465. *temp += sizeof(struct nk_font_baker);
  15466. *temp += nk_rect_align + nk_range_align + nk_char_align;
  15467. *temp += nk_build_align + nk_baker_align;
  15468. }
  15469. NK_INTERN struct nk_font_baker*
  15470. nk_font_baker(void *memory, int glyph_count, int count, const struct nk_allocator *alloc)
  15471. {
  15472. struct nk_font_baker *baker;
  15473. if (!memory) return 0;
  15474. /* setup baker inside a memory block */
  15475. baker = (struct nk_font_baker*)NK_ALIGN_PTR(memory, nk_baker_align);
  15476. baker->build = (struct nk_font_bake_data*)NK_ALIGN_PTR((baker + 1), nk_build_align);
  15477. baker->packed_chars = (stbtt_packedchar*)NK_ALIGN_PTR((baker->build + count), nk_char_align);
  15478. baker->rects = (struct stbrp_rect*)NK_ALIGN_PTR((baker->packed_chars + glyph_count), nk_rect_align);
  15479. baker->ranges = (stbtt_pack_range*)NK_ALIGN_PTR((baker->rects + glyph_count), nk_range_align);
  15480. baker->alloc = *alloc;
  15481. return baker;
  15482. }
  15483. NK_INTERN int
  15484. nk_font_bake_pack(struct nk_font_baker *baker,
  15485. nk_size *image_memory, int *width, int *height, struct nk_recti *custom,
  15486. const struct nk_font_config *config_list, int count,
  15487. const struct nk_allocator *alloc)
  15488. {
  15489. NK_STORAGE const nk_size max_height = 1024 * 32;
  15490. const struct nk_font_config *config_iter, *it;
  15491. int total_glyph_count = 0;
  15492. int total_range_count = 0;
  15493. int range_count = 0;
  15494. int i = 0;
  15495. NK_ASSERT(image_memory);
  15496. NK_ASSERT(width);
  15497. NK_ASSERT(height);
  15498. NK_ASSERT(config_list);
  15499. NK_ASSERT(count);
  15500. NK_ASSERT(alloc);
  15501. if (!image_memory || !width || !height || !config_list || !count) return nk_false;
  15502. for (config_iter = config_list; config_iter; config_iter = config_iter->next) {
  15503. it = config_iter;
  15504. do {range_count = nk_range_count(it->range);
  15505. total_range_count += range_count;
  15506. total_glyph_count += nk_range_glyph_count(it->range, range_count);
  15507. } while ((it = it->n) != config_iter);
  15508. }
  15509. /* setup font baker from temporary memory */
  15510. for (config_iter = config_list; config_iter; config_iter = config_iter->next) {
  15511. it = config_iter;
  15512. do {
  15513. struct stbtt_fontinfo *font_info = &baker->build[i++].info;
  15514. font_info->userdata = (void*)alloc;
  15515. if (!stbtt_InitFont(font_info, (const unsigned char*)it->ttf_blob, stbtt_GetFontOffsetForIndex((const unsigned char*)it->ttf_blob, 0)))
  15516. return nk_false;
  15517. } while ((it = it->n) != config_iter);
  15518. }
  15519. *height = 0;
  15520. *width = (total_glyph_count > 1000) ? 1024 : 512;
  15521. stbtt_PackBegin(&baker->spc, 0, (int)*width, (int)max_height, 0, 1, (void*)alloc);
  15522. {
  15523. int input_i = 0;
  15524. int range_n = 0;
  15525. int rect_n = 0;
  15526. int char_n = 0;
  15527. if (custom) {
  15528. /* pack custom user data first so it will be in the upper left corner*/
  15529. struct stbrp_rect custom_space;
  15530. nk_zero(&custom_space, sizeof(custom_space));
  15531. custom_space.w = (stbrp_coord)(custom->w);
  15532. custom_space.h = (stbrp_coord)(custom->h);
  15533. stbtt_PackSetOversampling(&baker->spc, 1, 1);
  15534. stbrp_pack_rects((struct stbrp_context*)baker->spc.pack_info, &custom_space, 1);
  15535. *height = NK_MAX(*height, (int)(custom_space.y + custom_space.h));
  15536. custom->x = (short)custom_space.x;
  15537. custom->y = (short)custom_space.y;
  15538. custom->w = (short)custom_space.w;
  15539. custom->h = (short)custom_space.h;
  15540. }
  15541. /* first font pass: pack all glyphs */
  15542. for (input_i = 0, config_iter = config_list; input_i < count && config_iter;
  15543. config_iter = config_iter->next) {
  15544. it = config_iter;
  15545. do {int n = 0;
  15546. int glyph_count;
  15547. const nk_rune *in_range;
  15548. const struct nk_font_config *cfg = it;
  15549. struct nk_font_bake_data *tmp = &baker->build[input_i++];
  15550. /* count glyphs + ranges in current font */
  15551. glyph_count = 0; range_count = 0;
  15552. for (in_range = cfg->range; in_range[0] && in_range[1]; in_range += 2) {
  15553. glyph_count += (int)(in_range[1] - in_range[0]) + 1;
  15554. range_count++;
  15555. }
  15556. /* setup ranges */
  15557. tmp->ranges = baker->ranges + range_n;
  15558. tmp->range_count = (nk_rune)range_count;
  15559. range_n += range_count;
  15560. for (i = 0; i < range_count; ++i) {
  15561. in_range = &cfg->range[i * 2];
  15562. tmp->ranges[i].font_size = cfg->size;
  15563. tmp->ranges[i].first_unicode_codepoint_in_range = (int)in_range[0];
  15564. tmp->ranges[i].num_chars = (int)(in_range[1]- in_range[0]) + 1;
  15565. tmp->ranges[i].chardata_for_range = baker->packed_chars + char_n;
  15566. char_n += tmp->ranges[i].num_chars;
  15567. }
  15568. /* pack */
  15569. tmp->rects = baker->rects + rect_n;
  15570. rect_n += glyph_count;
  15571. stbtt_PackSetOversampling(&baker->spc, cfg->oversample_h, cfg->oversample_v);
  15572. n = stbtt_PackFontRangesGatherRects(&baker->spc, &tmp->info,
  15573. tmp->ranges, (int)tmp->range_count, tmp->rects);
  15574. stbrp_pack_rects((struct stbrp_context*)baker->spc.pack_info, tmp->rects, (int)n);
  15575. /* texture height */
  15576. for (i = 0; i < n; ++i) {
  15577. if (tmp->rects[i].was_packed)
  15578. *height = NK_MAX(*height, tmp->rects[i].y + tmp->rects[i].h);
  15579. }
  15580. } while ((it = it->n) != config_iter);
  15581. }
  15582. NK_ASSERT(rect_n == total_glyph_count);
  15583. NK_ASSERT(char_n == total_glyph_count);
  15584. NK_ASSERT(range_n == total_range_count);
  15585. }
  15586. *height = (int)nk_round_up_pow2((nk_uint)*height);
  15587. *image_memory = (nk_size)(*width) * (nk_size)(*height);
  15588. return nk_true;
  15589. }
  15590. NK_INTERN void
  15591. nk_font_bake(struct nk_font_baker *baker, void *image_memory, int width, int height,
  15592. struct nk_font_glyph *glyphs, int glyphs_count,
  15593. const struct nk_font_config *config_list, int font_count)
  15594. {
  15595. int input_i = 0;
  15596. nk_rune glyph_n = 0;
  15597. const struct nk_font_config *config_iter;
  15598. const struct nk_font_config *it;
  15599. NK_ASSERT(image_memory);
  15600. NK_ASSERT(width);
  15601. NK_ASSERT(height);
  15602. NK_ASSERT(config_list);
  15603. NK_ASSERT(baker);
  15604. NK_ASSERT(font_count);
  15605. NK_ASSERT(glyphs_count);
  15606. if (!image_memory || !width || !height || !config_list ||
  15607. !font_count || !glyphs || !glyphs_count)
  15608. return;
  15609. /* second font pass: render glyphs */
  15610. nk_zero(image_memory, (nk_size)((nk_size)width * (nk_size)height));
  15611. baker->spc.pixels = (unsigned char*)image_memory;
  15612. baker->spc.height = (int)height;
  15613. for (input_i = 0, config_iter = config_list; input_i < font_count && config_iter;
  15614. config_iter = config_iter->next) {
  15615. it = config_iter;
  15616. do {const struct nk_font_config *cfg = it;
  15617. struct nk_font_bake_data *tmp = &baker->build[input_i++];
  15618. stbtt_PackSetOversampling(&baker->spc, cfg->oversample_h, cfg->oversample_v);
  15619. stbtt_PackFontRangesRenderIntoRects(&baker->spc, &tmp->info, tmp->ranges, (int)tmp->range_count, tmp->rects);
  15620. } while ((it = it->n) != config_iter);
  15621. } stbtt_PackEnd(&baker->spc);
  15622. /* third pass: setup font and glyphs */
  15623. for (input_i = 0, config_iter = config_list; input_i < font_count && config_iter;
  15624. config_iter = config_iter->next) {
  15625. it = config_iter;
  15626. do {nk_size i = 0;
  15627. int char_idx = 0;
  15628. nk_rune glyph_count = 0;
  15629. const struct nk_font_config *cfg = it;
  15630. struct nk_font_bake_data *tmp = &baker->build[input_i++];
  15631. struct nk_baked_font *dst_font = cfg->font;
  15632. float font_scale = stbtt_ScaleForPixelHeight(&tmp->info, cfg->size);
  15633. int unscaled_ascent, unscaled_descent, unscaled_line_gap;
  15634. stbtt_GetFontVMetrics(&tmp->info, &unscaled_ascent, &unscaled_descent,
  15635. &unscaled_line_gap);
  15636. /* fill baked font */
  15637. if (!cfg->merge_mode) {
  15638. dst_font->ranges = cfg->range;
  15639. dst_font->height = cfg->size;
  15640. dst_font->ascent = ((float)unscaled_ascent * font_scale);
  15641. dst_font->descent = ((float)unscaled_descent * font_scale);
  15642. dst_font->glyph_offset = glyph_n;
  15643. /*
  15644. Need to zero this, or it will carry over from a previous
  15645. bake, and cause a segfault when accessing glyphs[].
  15646. */
  15647. dst_font->glyph_count = 0;
  15648. }
  15649. /* fill own baked font glyph array */
  15650. for (i = 0; i < tmp->range_count; ++i) {
  15651. stbtt_pack_range *range = &tmp->ranges[i];
  15652. for (char_idx = 0; char_idx < range->num_chars; char_idx++)
  15653. {
  15654. nk_rune codepoint = 0;
  15655. float dummy_x = 0, dummy_y = 0;
  15656. stbtt_aligned_quad q;
  15657. struct nk_font_glyph *glyph;
  15658. /* query glyph bounds from stb_truetype */
  15659. const stbtt_packedchar *pc = &range->chardata_for_range[char_idx];
  15660. codepoint = (nk_rune)(range->first_unicode_codepoint_in_range + char_idx);
  15661. stbtt_GetPackedQuad(range->chardata_for_range, (int)width,
  15662. (int)height, char_idx, &dummy_x, &dummy_y, &q, 0);
  15663. /* fill own glyph type with data */
  15664. glyph = &glyphs[dst_font->glyph_offset + dst_font->glyph_count + (unsigned int)glyph_count];
  15665. glyph->codepoint = codepoint;
  15666. glyph->x0 = q.x0; glyph->y0 = q.y0;
  15667. glyph->x1 = q.x1; glyph->y1 = q.y1;
  15668. glyph->y0 += (dst_font->ascent + 0.5f);
  15669. glyph->y1 += (dst_font->ascent + 0.5f);
  15670. glyph->w = glyph->x1 - glyph->x0 + 0.5f;
  15671. glyph->h = glyph->y1 - glyph->y0;
  15672. if (cfg->coord_type == NK_COORD_PIXEL) {
  15673. glyph->u0 = q.s0 * (float)width;
  15674. glyph->v0 = q.t0 * (float)height;
  15675. glyph->u1 = q.s1 * (float)width;
  15676. glyph->v1 = q.t1 * (float)height;
  15677. } else {
  15678. glyph->u0 = q.s0;
  15679. glyph->v0 = q.t0;
  15680. glyph->u1 = q.s1;
  15681. glyph->v1 = q.t1;
  15682. }
  15683. glyph->xadvance = (pc->xadvance + cfg->spacing.x);
  15684. if (cfg->pixel_snap)
  15685. glyph->xadvance = (float)(int)(glyph->xadvance + 0.5f);
  15686. glyph_count++;
  15687. }
  15688. }
  15689. dst_font->glyph_count += glyph_count;
  15690. glyph_n += glyph_count;
  15691. } while ((it = it->n) != config_iter);
  15692. }
  15693. }
  15694. NK_INTERN void
  15695. nk_font_bake_custom_data(void *img_memory, int img_width, int img_height,
  15696. struct nk_recti img_dst, const char *texture_data_mask, int tex_width,
  15697. int tex_height, char white, char black)
  15698. {
  15699. nk_byte *pixels;
  15700. int y = 0;
  15701. int x = 0;
  15702. int n = 0;
  15703. NK_ASSERT(img_memory);
  15704. NK_ASSERT(img_width);
  15705. NK_ASSERT(img_height);
  15706. NK_ASSERT(texture_data_mask);
  15707. NK_UNUSED(tex_height);
  15708. if (!img_memory || !img_width || !img_height || !texture_data_mask)
  15709. return;
  15710. pixels = (nk_byte*)img_memory;
  15711. for (y = 0, n = 0; y < tex_height; ++y) {
  15712. for (x = 0; x < tex_width; ++x, ++n) {
  15713. const int off0 = ((img_dst.x + x) + (img_dst.y + y) * img_width);
  15714. const int off1 = off0 + 1 + tex_width;
  15715. pixels[off0] = (texture_data_mask[n] == white) ? 0xFF : 0x00;
  15716. pixels[off1] = (texture_data_mask[n] == black) ? 0xFF : 0x00;
  15717. }
  15718. }
  15719. }
  15720. NK_INTERN void
  15721. nk_font_bake_convert(void *out_memory, int img_width, int img_height,
  15722. const void *in_memory)
  15723. {
  15724. int n = 0;
  15725. nk_rune *dst;
  15726. const nk_byte *src;
  15727. NK_ASSERT(out_memory);
  15728. NK_ASSERT(in_memory);
  15729. NK_ASSERT(img_width);
  15730. NK_ASSERT(img_height);
  15731. if (!out_memory || !in_memory || !img_height || !img_width) return;
  15732. dst = (nk_rune*)out_memory;
  15733. src = (const nk_byte*)in_memory;
  15734. for (n = (int)(img_width * img_height); n > 0; n--)
  15735. *dst++ = ((nk_rune)(*src++) << 24) | 0x00FFFFFF;
  15736. }
  15737. /* -------------------------------------------------------------
  15738. *
  15739. * FONT
  15740. *
  15741. * --------------------------------------------------------------*/
  15742. NK_INTERN float
  15743. nk_font_text_width(nk_handle handle, float height, const char *text, int len)
  15744. {
  15745. nk_rune unicode;
  15746. int text_len = 0;
  15747. float text_width = 0;
  15748. int glyph_len = 0;
  15749. float scale = 0;
  15750. struct nk_font *font = (struct nk_font*)handle.ptr;
  15751. NK_ASSERT(font);
  15752. NK_ASSERT(font->glyphs);
  15753. if (!font || !text || !len)
  15754. return 0;
  15755. scale = height/font->info.height;
  15756. glyph_len = text_len = nk_utf_decode(text, &unicode, (int)len);
  15757. if (!glyph_len) return 0;
  15758. while (text_len <= (int)len && glyph_len) {
  15759. const struct nk_font_glyph *g;
  15760. if (unicode == NK_UTF_INVALID) break;
  15761. /* query currently drawn glyph information */
  15762. g = nk_font_find_glyph(font, unicode);
  15763. text_width += g->xadvance * scale;
  15764. /* offset next glyph */
  15765. glyph_len = nk_utf_decode(text + text_len, &unicode, (int)len - text_len);
  15766. text_len += glyph_len;
  15767. }
  15768. return text_width;
  15769. }
  15770. #ifdef NK_INCLUDE_VERTEX_BUFFER_OUTPUT
  15771. NK_INTERN void
  15772. nk_font_query_font_glyph(nk_handle handle, float height,
  15773. struct nk_user_font_glyph *glyph, nk_rune codepoint, nk_rune next_codepoint)
  15774. {
  15775. float scale;
  15776. const struct nk_font_glyph *g;
  15777. struct nk_font *font;
  15778. NK_ASSERT(glyph);
  15779. NK_UNUSED(next_codepoint);
  15780. font = (struct nk_font*)handle.ptr;
  15781. NK_ASSERT(font);
  15782. NK_ASSERT(font->glyphs);
  15783. if (!font || !glyph)
  15784. return;
  15785. scale = height/font->info.height;
  15786. g = nk_font_find_glyph(font, codepoint);
  15787. glyph->width = (g->x1 - g->x0) * scale;
  15788. glyph->height = (g->y1 - g->y0) * scale;
  15789. glyph->offset = nk_vec2(g->x0 * scale, g->y0 * scale);
  15790. glyph->xadvance = (g->xadvance * scale);
  15791. glyph->uv[0] = nk_vec2(g->u0, g->v0);
  15792. glyph->uv[1] = nk_vec2(g->u1, g->v1);
  15793. }
  15794. #endif
  15795. NK_API const struct nk_font_glyph*
  15796. nk_font_find_glyph(const struct nk_font *font, nk_rune unicode)
  15797. {
  15798. int i = 0;
  15799. int count;
  15800. int total_glyphs = 0;
  15801. const struct nk_font_glyph *glyph = 0;
  15802. const struct nk_font_config *iter = 0;
  15803. NK_ASSERT(font);
  15804. NK_ASSERT(font->glyphs);
  15805. NK_ASSERT(font->info.ranges);
  15806. if (!font || !font->glyphs) return 0;
  15807. glyph = font->fallback;
  15808. iter = font->config;
  15809. do {count = nk_range_count(iter->range);
  15810. for (i = 0; i < count; ++i) {
  15811. nk_rune f = iter->range[(i*2)+0];
  15812. nk_rune t = iter->range[(i*2)+1];
  15813. int diff = (int)((t - f) + 1);
  15814. if (unicode >= f && unicode <= t)
  15815. return &font->glyphs[((nk_rune)total_glyphs + (unicode - f))];
  15816. total_glyphs += diff;
  15817. }
  15818. } while ((iter = iter->n) != font->config);
  15819. return glyph;
  15820. }
  15821. NK_INTERN void
  15822. nk_font_init(struct nk_font *font, float pixel_height,
  15823. nk_rune fallback_codepoint, struct nk_font_glyph *glyphs,
  15824. const struct nk_baked_font *baked_font, nk_handle atlas)
  15825. {
  15826. struct nk_baked_font baked;
  15827. NK_ASSERT(font);
  15828. NK_ASSERT(glyphs);
  15829. NK_ASSERT(baked_font);
  15830. if (!font || !glyphs || !baked_font)
  15831. return;
  15832. baked = *baked_font;
  15833. font->fallback = 0;
  15834. font->info = baked;
  15835. font->scale = (float)pixel_height / (float)font->info.height;
  15836. font->glyphs = &glyphs[baked_font->glyph_offset];
  15837. font->texture = atlas;
  15838. font->fallback_codepoint = fallback_codepoint;
  15839. font->fallback = nk_font_find_glyph(font, fallback_codepoint);
  15840. font->handle.height = font->info.height * font->scale;
  15841. font->handle.width = nk_font_text_width;
  15842. font->handle.userdata.ptr = font;
  15843. #ifdef NK_INCLUDE_VERTEX_BUFFER_OUTPUT
  15844. font->handle.query = nk_font_query_font_glyph;
  15845. font->handle.texture = font->texture;
  15846. #endif
  15847. }
  15848. /* ---------------------------------------------------------------------------
  15849. *
  15850. * DEFAULT FONT
  15851. *
  15852. * ProggyClean.ttf
  15853. * Copyright (c) 2004, 2005 Tristan Grimmer
  15854. * MIT license https://github.com/bluescan/proggyfonts/blob/master/LICENSE
  15855. * Download and more information at https://github.com/bluescan/proggyfonts
  15856. *-----------------------------------------------------------------------------*/
  15857. #ifdef __clang__
  15858. #pragma clang diagnostic push
  15859. #pragma clang diagnostic ignored "-Woverlength-strings"
  15860. #elif defined(__GNUC__) || defined(__GNUG__)
  15861. #pragma GCC diagnostic push
  15862. #pragma GCC diagnostic ignored "-Woverlength-strings"
  15863. #endif
  15864. #ifdef NK_INCLUDE_DEFAULT_FONT
  15865. NK_GLOBAL const char nk_proggy_clean_ttf_compressed_data_base85[11980+1] =
  15866. "7])#######hV0qs'/###[),##/l:$#Q6>##5[n42>c-TH`->>#/e>11NNV=Bv(*:.F?uu#(gRU.o0XGH`$vhLG1hxt9?W`#,5LsCp#-i>.r$<$6pD>Lb';9Crc6tgXmKVeU2cD4Eo3R/"
  15867. "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#"
  15868. "`8ND>Qo#t'X#(v#Y9w0#1D$CIf;W'#pWUPXOuxXuU(H9M(1<q-UE31#^-V'8IRUo7Qf./L>=Ke$$'5F%)]0^#[email protected]<r:QLtFsLcL6##lOj)#.Y5<-R&KgLwqJfLgN&;Q?gI^#DY2uL"
  15869. "i@^rMl9t=cWq6##weg>$FBjVQTSDgEKnIS7EM9>ZY9w0#L;>>#Mx&4Mvt//L[MkA#[email protected]'[0#7RL_&#w+F%HtG9M#XL`N&.,GM4Pg;-<nLENhvx>-VsM.M0rJfLH2eTM`*oJMHRC`N"
  15870. "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"
  15871. "*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)"
  15872. "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"
  15873. "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."
  15874. "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"
  15875. "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(,)"
  15876. "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#"
  15877. "'/###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"
  15878. "_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"
  15879. "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/"
  15880. "/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"
  15881. "%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&#"
  15882. "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)("
  15883. "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"
  15884. "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"
  15885. "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^-"
  15886. "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-"
  15887. "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"
  15888. "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%"
  15889. "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]"
  15890. "%(?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"
  15891. "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:"
  15892. "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("
  15893. "$/V,;(kXZejWO`<[5?\?ewY(*9=%wDc;,u<'9t3W-(H1th3+G]ucQ]kLs7df($/*JL]@*t7Bu_G3_7mp7<[email protected];x3B0lqp7Hf,^Ze7-##@/c58Mo(3;knp0%)A7?-W+eI'o8)b<"
  15894. "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'?"
  15895. "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/;"
  15896. ")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"
  15897. "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("
  15898. "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"
  15899. "bIu)'Z,*[>br5fX^:FPAWr-m2KgL<LUN098kTF&#lvo58=/vjDo;.;)Ka*hLR#/k=rKbxuV`>Q_nN6'8uTG&#1T5g)uLv:873UpTLgH+#FgpH'_o1780Ph8KmxQJ8#H72L4@768@Tm&Q"
  15900. "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<-"
  15901. "V8J'(1)G][68hW$5'q[GC&5j`TE?m'esFGNRM)j,ffZ?-qx8;->g4t*:CIP/[Qap7/9'#(1sao7w-.qNUdkJ)tCF&#B^;[email protected]$m%#QvQS8U@)2Z+3K:AKM5i"
  15902. "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"
  15903. ".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@@"
  15904. "$&)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*"
  15905. "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"
  15906. "@-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&#"
  15907. "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#"
  15908. "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"
  15909. "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"
  15910. "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&#"
  15911. "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"
  15912. ":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+"
  15913. "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*"
  15914. "$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"
  15915. ":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"
  15916. "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"
  15917. "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"
  15918. "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"
  15919. ":^#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@&]>"
  15920. "_>@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%"
  15921. "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;"
  15922. "^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:"
  15923. "+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%"
  15924. "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-"
  15925. "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*"
  15926. "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"
  15927. "8kD2)2fU/M#$e.)T4,_=8hLim[&);?UkK'-x?'(:siIfL<$pFM`i<?%W(mGDHM%>iWP,##P`%/L<eXi:@Z9C.7o=@(pXdAO/NLQ8lPl+HPOQa8wD8=^GlPa8TKI1CjhsCTSLJM'/Wl>-"
  15928. "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`"
  15929. "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]%/"
  15930. "+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"
  15931. "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"
  15932. "?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"
  15933. "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"
  15934. ">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'>"
  15935. "[%$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"
  15936. "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#"
  15937. "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$"
  15938. "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,)"
  15939. "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"
  15940. "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"
  15941. "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"
  15942. "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(#"
  15943. ";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&>"
  15944. "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#"
  15945. "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"
  15946. "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#"
  15947. "/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-#"
  15948. "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#"
  15949. "TmD<#%JSMFove:CTBEXI:<eh2g)B,3h2^G3i;#d3jD>)4kMYD4lVu`4m`:&5niUA5@(A5BA1]PBB:xlBCC=2CDLXMCEUtiCf&0g2'tN?PGT4CPGT4CPGT4CPGT4CPGT4CPGT4CPGT4CP"
  15950. "GT4CPGT4CPGT4CPGT4CPGT4CPGT4CP-qekC`.9kEg^+F$kwViFJTB&5KTB&5KTB&5KTB&5KTB&5KTB&5KTB&5KTB&5KTB&5KTB&5KTB&5KTB&5KTB&5KTB&5KTB&5o,^<-28ZI'O?;xp"
  15951. "O?;xpO?;xpO?;xpO?;xpO?;xpO?;xpO?;xpO?;xpO?;xpO?;xpO?;xpO?;xpO?;xp;7q-#lLYI:xvD=#";
  15952. #endif /* NK_INCLUDE_DEFAULT_FONT */
  15953. #define NK_CURSOR_DATA_W 90
  15954. #define NK_CURSOR_DATA_H 27
  15955. NK_GLOBAL const char nk_custom_cursor_data[NK_CURSOR_DATA_W * NK_CURSOR_DATA_H + 1] =
  15956. {
  15957. "..- -XXXXXXX- X - X -XXXXXXX - XXXXXXX"
  15958. "..- -X.....X- X.X - X.X -X.....X - X.....X"
  15959. "--- -XXX.XXX- X...X - X...X -X....X - X....X"
  15960. "X - X.X - X.....X - X.....X -X...X - X...X"
  15961. "XX - X.X -X.......X- X.......X -X..X.X - X.X..X"
  15962. "X.X - X.X -XXXX.XXXX- XXXX.XXXX -X.X X.X - X.X X.X"
  15963. "X..X - X.X - X.X - X.X -XX X.X - X.X XX"
  15964. "X...X - X.X - X.X - XX X.X XX - X.X - X.X "
  15965. "X....X - X.X - X.X - X.X X.X X.X - X.X - X.X "
  15966. "X.....X - X.X - X.X - X..X X.X X..X - X.X - X.X "
  15967. "X......X - X.X - X.X - X...XXXXXX.XXXXXX...X - X.X XX-XX X.X "
  15968. "X.......X - X.X - X.X -X.....................X- X.X X.X-X.X X.X "
  15969. "X........X - X.X - X.X - X...XXXXXX.XXXXXX...X - X.X..X-X..X.X "
  15970. "X.........X -XXX.XXX- X.X - X..X X.X X..X - X...X-X...X "
  15971. "X..........X-X.....X- X.X - X.X X.X X.X - X....X-X....X "
  15972. "X......XXXXX-XXXXXXX- X.X - XX X.X XX - X.....X-X.....X "
  15973. "X...X..X --------- X.X - X.X - XXXXXXX-XXXXXXX "
  15974. "X..X X..X - -XXXX.XXXX- XXXX.XXXX ------------------------------------"
  15975. "X.X X..X - -X.......X- X.......X - XX XX - "
  15976. "XX X..X - - X.....X - X.....X - X.X X.X - "
  15977. " X..X - X...X - X...X - X..X X..X - "
  15978. " XX - X.X - X.X - X...XXXXXXXXXXXXX...X - "
  15979. "------------ - X - X -X.....................X- "
  15980. " ----------------------------------- X...XXXXXXXXXXXXX...X - "
  15981. " - X..X X..X - "
  15982. " - X.X X.X - "
  15983. " - XX XX - "
  15984. };
  15985. #ifdef __clang__
  15986. #pragma clang diagnostic pop
  15987. #elif defined(__GNUC__) || defined(__GNUG__)
  15988. #pragma GCC diagnostic pop
  15989. #endif
  15990. NK_GLOBAL unsigned char *nk__barrier;
  15991. NK_GLOBAL unsigned char *nk__barrier2;
  15992. NK_GLOBAL unsigned char *nk__barrier3;
  15993. NK_GLOBAL unsigned char *nk__barrier4;
  15994. NK_GLOBAL unsigned char *nk__dout;
  15995. NK_INTERN unsigned int
  15996. nk_decompress_length(unsigned char *input)
  15997. {
  15998. return (unsigned int)((input[8] << 24) + (input[9] << 16) + (input[10] << 8) + input[11]);
  15999. }
  16000. NK_INTERN void
  16001. nk__match(unsigned char *data, unsigned int length)
  16002. {
  16003. /* INVERSE of memmove... write each byte before copying the next...*/
  16004. NK_ASSERT (nk__dout + length <= nk__barrier);
  16005. if (nk__dout + length > nk__barrier) { nk__dout += length; return; }
  16006. if (data < nk__barrier4) { nk__dout = nk__barrier+1; return; }
  16007. while (length--) *nk__dout++ = *data++;
  16008. }
  16009. NK_INTERN void
  16010. nk__lit(unsigned char *data, unsigned int length)
  16011. {
  16012. NK_ASSERT (nk__dout + length <= nk__barrier);
  16013. if (nk__dout + length > nk__barrier) { nk__dout += length; return; }
  16014. if (data < nk__barrier2) { nk__dout = nk__barrier+1; return; }
  16015. NK_MEMCPY(nk__dout, data, length);
  16016. nk__dout += length;
  16017. }
  16018. NK_INTERN unsigned char*
  16019. nk_decompress_token(unsigned char *i)
  16020. {
  16021. #define nk__in2(x) ((i[x] << 8) + i[(x)+1])
  16022. #define nk__in3(x) ((i[x] << 16) + nk__in2((x)+1))
  16023. #define nk__in4(x) ((i[x] << 24) + nk__in3((x)+1))
  16024. if (*i >= 0x20) { /* use fewer if's for cases that expand small */
  16025. if (*i >= 0x80) nk__match(nk__dout-i[1]-1, (unsigned int)i[0] - 0x80 + 1), i += 2;
  16026. else if (*i >= 0x40) nk__match(nk__dout-(nk__in2(0) - 0x4000 + 1), (unsigned int)i[2]+1), i += 3;
  16027. else /* *i >= 0x20 */ nk__lit(i+1, (unsigned int)i[0] - 0x20 + 1), i += 1 + (i[0] - 0x20 + 1);
  16028. } else { /* more ifs for cases that expand large, since overhead is amortized */
  16029. if (*i >= 0x18) nk__match(nk__dout-(unsigned int)(nk__in3(0) - 0x180000 + 1), (unsigned int)i[3]+1), i += 4;
  16030. else if (*i >= 0x10) nk__match(nk__dout-(unsigned int)(nk__in3(0) - 0x100000 + 1), (unsigned int)nk__in2(3)+1), i += 5;
  16031. else if (*i >= 0x08) nk__lit(i+2, (unsigned int)nk__in2(0) - 0x0800 + 1), i += 2 + (nk__in2(0) - 0x0800 + 1);
  16032. else if (*i == 0x07) nk__lit(i+3, (unsigned int)nk__in2(1) + 1), i += 3 + (nk__in2(1) + 1);
  16033. else if (*i == 0x06) nk__match(nk__dout-(unsigned int)(nk__in3(1)+1), i[4]+1u), i += 5;
  16034. else if (*i == 0x04) nk__match(nk__dout-(unsigned int)(nk__in3(1)+1), (unsigned int)nk__in2(4)+1u), i += 6;
  16035. }
  16036. return i;
  16037. }
  16038. NK_INTERN unsigned int
  16039. nk_adler32(unsigned int adler32, unsigned char *buffer, unsigned int buflen)
  16040. {
  16041. const unsigned long ADLER_MOD = 65521;
  16042. unsigned long s1 = adler32 & 0xffff, s2 = adler32 >> 16;
  16043. unsigned long blocklen, i;
  16044. blocklen = buflen % 5552;
  16045. while (buflen) {
  16046. for (i=0; i + 7 < blocklen; i += 8) {
  16047. s1 += buffer[0]; s2 += s1;
  16048. s1 += buffer[1]; s2 += s1;
  16049. s1 += buffer[2]; s2 += s1;
  16050. s1 += buffer[3]; s2 += s1;
  16051. s1 += buffer[4]; s2 += s1;
  16052. s1 += buffer[5]; s2 += s1;
  16053. s1 += buffer[6]; s2 += s1;
  16054. s1 += buffer[7]; s2 += s1;
  16055. buffer += 8;
  16056. }
  16057. for (; i < blocklen; ++i) {
  16058. s1 += *buffer++; s2 += s1;
  16059. }
  16060. s1 %= ADLER_MOD; s2 %= ADLER_MOD;
  16061. buflen -= (unsigned int)blocklen;
  16062. blocklen = 5552;
  16063. }
  16064. return (unsigned int)(s2 << 16) + (unsigned int)s1;
  16065. }
  16066. NK_INTERN unsigned int
  16067. nk_decompress(unsigned char *output, unsigned char *i, unsigned int length)
  16068. {
  16069. unsigned int olen;
  16070. if (nk__in4(0) != 0x57bC0000) return 0;
  16071. if (nk__in4(4) != 0) return 0; /* error! stream is > 4GB */
  16072. olen = nk_decompress_length(i);
  16073. nk__barrier2 = i;
  16074. nk__barrier3 = i+length;
  16075. nk__barrier = output + olen;
  16076. nk__barrier4 = output;
  16077. i += 16;
  16078. nk__dout = output;
  16079. for (;;) {
  16080. unsigned char *old_i = i;
  16081. i = nk_decompress_token(i);
  16082. if (i == old_i) {
  16083. if (*i == 0x05 && i[1] == 0xfa) {
  16084. NK_ASSERT(nk__dout == output + olen);
  16085. if (nk__dout != output + olen) return 0;
  16086. if (nk_adler32(1, output, olen) != (unsigned int) nk__in4(2))
  16087. return 0;
  16088. return olen;
  16089. } else {
  16090. NK_ASSERT(0); /* NOTREACHED */
  16091. return 0;
  16092. }
  16093. }
  16094. NK_ASSERT(nk__dout <= output + olen);
  16095. if (nk__dout > output + olen)
  16096. return 0;
  16097. }
  16098. }
  16099. NK_INTERN unsigned int
  16100. nk_decode_85_byte(char c)
  16101. {
  16102. return (unsigned int)((c >= '\\') ? c-36 : c-35);
  16103. }
  16104. NK_INTERN void
  16105. nk_decode_85(unsigned char* dst, const unsigned char* src)
  16106. {
  16107. while (*src)
  16108. {
  16109. unsigned int tmp =
  16110. nk_decode_85_byte((char)src[0]) +
  16111. 85 * (nk_decode_85_byte((char)src[1]) +
  16112. 85 * (nk_decode_85_byte((char)src[2]) +
  16113. 85 * (nk_decode_85_byte((char)src[3]) +
  16114. 85 * nk_decode_85_byte((char)src[4]))));
  16115. /* we can't assume little-endianess. */
  16116. dst[0] = (unsigned char)((tmp >> 0) & 0xFF);
  16117. dst[1] = (unsigned char)((tmp >> 8) & 0xFF);
  16118. dst[2] = (unsigned char)((tmp >> 16) & 0xFF);
  16119. dst[3] = (unsigned char)((tmp >> 24) & 0xFF);
  16120. src += 5;
  16121. dst += 4;
  16122. }
  16123. }
  16124. /* -------------------------------------------------------------
  16125. *
  16126. * FONT ATLAS
  16127. *
  16128. * --------------------------------------------------------------*/
  16129. NK_API struct nk_font_config
  16130. nk_font_config(float pixel_height)
  16131. {
  16132. struct nk_font_config cfg;
  16133. nk_zero_struct(cfg);
  16134. cfg.ttf_blob = 0;
  16135. cfg.ttf_size = 0;
  16136. cfg.ttf_data_owned_by_atlas = 0;
  16137. cfg.size = pixel_height;
  16138. cfg.oversample_h = 3;
  16139. cfg.oversample_v = 1;
  16140. cfg.pixel_snap = 0;
  16141. cfg.coord_type = NK_COORD_UV;
  16142. cfg.spacing = nk_vec2(0,0);
  16143. cfg.range = nk_font_default_glyph_ranges();
  16144. cfg.merge_mode = 0;
  16145. cfg.fallback_glyph = '?';
  16146. cfg.font = 0;
  16147. cfg.n = 0;
  16148. return cfg;
  16149. }
  16150. #ifdef NK_INCLUDE_DEFAULT_ALLOCATOR
  16151. NK_API void
  16152. nk_font_atlas_init_default(struct nk_font_atlas *atlas)
  16153. {
  16154. NK_ASSERT(atlas);
  16155. if (!atlas) return;
  16156. nk_zero_struct(*atlas);
  16157. atlas->temporary.userdata.ptr = 0;
  16158. atlas->temporary.alloc = nk_malloc;
  16159. atlas->temporary.free = nk_mfree;
  16160. atlas->permanent.userdata.ptr = 0;
  16161. atlas->permanent.alloc = nk_malloc;
  16162. atlas->permanent.free = nk_mfree;
  16163. }
  16164. #endif
  16165. NK_API void
  16166. nk_font_atlas_init(struct nk_font_atlas *atlas, const struct nk_allocator *alloc)
  16167. {
  16168. NK_ASSERT(atlas);
  16169. NK_ASSERT(alloc);
  16170. if (!atlas || !alloc) return;
  16171. nk_zero_struct(*atlas);
  16172. atlas->permanent = *alloc;
  16173. atlas->temporary = *alloc;
  16174. }
  16175. NK_API void
  16176. nk_font_atlas_init_custom(struct nk_font_atlas *atlas,
  16177. const struct nk_allocator *permanent, const struct nk_allocator *temporary)
  16178. {
  16179. NK_ASSERT(atlas);
  16180. NK_ASSERT(permanent);
  16181. NK_ASSERT(temporary);
  16182. if (!atlas || !permanent || !temporary) return;
  16183. nk_zero_struct(*atlas);
  16184. atlas->permanent = *permanent;
  16185. atlas->temporary = *temporary;
  16186. }
  16187. NK_API void
  16188. nk_font_atlas_begin(struct nk_font_atlas *atlas)
  16189. {
  16190. NK_ASSERT(atlas);
  16191. NK_ASSERT(atlas->temporary.alloc && atlas->temporary.free);
  16192. NK_ASSERT(atlas->permanent.alloc && atlas->permanent.free);
  16193. if (!atlas || !atlas->permanent.alloc || !atlas->permanent.free ||
  16194. !atlas->temporary.alloc || !atlas->temporary.free) return;
  16195. if (atlas->glyphs) {
  16196. atlas->permanent.free(atlas->permanent.userdata, atlas->glyphs);
  16197. atlas->glyphs = 0;
  16198. }
  16199. if (atlas->pixel) {
  16200. atlas->permanent.free(atlas->permanent.userdata, atlas->pixel);
  16201. atlas->pixel = 0;
  16202. }
  16203. }
  16204. NK_API struct nk_font*
  16205. nk_font_atlas_add(struct nk_font_atlas *atlas, const struct nk_font_config *config)
  16206. {
  16207. struct nk_font *font = 0;
  16208. struct nk_font_config *cfg;
  16209. NK_ASSERT(atlas);
  16210. NK_ASSERT(atlas->permanent.alloc);
  16211. NK_ASSERT(atlas->permanent.free);
  16212. NK_ASSERT(atlas->temporary.alloc);
  16213. NK_ASSERT(atlas->temporary.free);
  16214. NK_ASSERT(config);
  16215. NK_ASSERT(config->ttf_blob);
  16216. NK_ASSERT(config->ttf_size);
  16217. NK_ASSERT(config->size > 0.0f);
  16218. if (!atlas || !config || !config->ttf_blob || !config->ttf_size || config->size <= 0.0f||
  16219. !atlas->permanent.alloc || !atlas->permanent.free ||
  16220. !atlas->temporary.alloc || !atlas->temporary.free)
  16221. return 0;
  16222. /* allocate font config */
  16223. cfg = (struct nk_font_config*)
  16224. atlas->permanent.alloc(atlas->permanent.userdata,0, sizeof(struct nk_font_config));
  16225. NK_MEMCPY(cfg, config, sizeof(*config));
  16226. cfg->n = cfg;
  16227. cfg->p = cfg;
  16228. if (!config->merge_mode) {
  16229. /* insert font config into list */
  16230. if (!atlas->config) {
  16231. atlas->config = cfg;
  16232. cfg->next = 0;
  16233. } else {
  16234. struct nk_font_config *i = atlas->config;
  16235. while (i->next) i = i->next;
  16236. i->next = cfg;
  16237. cfg->next = 0;
  16238. }
  16239. /* allocate new font */
  16240. font = (struct nk_font*)
  16241. atlas->permanent.alloc(atlas->permanent.userdata,0, sizeof(struct nk_font));
  16242. NK_ASSERT(font);
  16243. nk_zero(font, sizeof(*font));
  16244. if (!font) return 0;
  16245. font->config = cfg;
  16246. /* insert font into list */
  16247. if (!atlas->fonts) {
  16248. atlas->fonts = font;
  16249. font->next = 0;
  16250. } else {
  16251. struct nk_font *i = atlas->fonts;
  16252. while (i->next) i = i->next;
  16253. i->next = font;
  16254. font->next = 0;
  16255. }
  16256. cfg->font = &font->info;
  16257. } else {
  16258. /* extend previously added font */
  16259. struct nk_font *f = 0;
  16260. struct nk_font_config *c = 0;
  16261. NK_ASSERT(atlas->font_num);
  16262. f = atlas->fonts;
  16263. c = f->config;
  16264. cfg->font = &f->info;
  16265. cfg->n = c;
  16266. cfg->p = c->p;
  16267. c->p->n = cfg;
  16268. c->p = cfg;
  16269. }
  16270. /* create own copy of .TTF font blob */
  16271. if (!config->ttf_data_owned_by_atlas) {
  16272. cfg->ttf_blob = atlas->permanent.alloc(atlas->permanent.userdata,0, cfg->ttf_size);
  16273. NK_ASSERT(cfg->ttf_blob);
  16274. if (!cfg->ttf_blob) {
  16275. atlas->font_num++;
  16276. return 0;
  16277. }
  16278. NK_MEMCPY(cfg->ttf_blob, config->ttf_blob, cfg->ttf_size);
  16279. cfg->ttf_data_owned_by_atlas = 1;
  16280. }
  16281. atlas->font_num++;
  16282. return font;
  16283. }
  16284. NK_API struct nk_font*
  16285. nk_font_atlas_add_from_memory(struct nk_font_atlas *atlas, void *memory,
  16286. nk_size size, float height, const struct nk_font_config *config)
  16287. {
  16288. struct nk_font_config cfg;
  16289. NK_ASSERT(memory);
  16290. NK_ASSERT(size);
  16291. NK_ASSERT(atlas);
  16292. NK_ASSERT(atlas->temporary.alloc);
  16293. NK_ASSERT(atlas->temporary.free);
  16294. NK_ASSERT(atlas->permanent.alloc);
  16295. NK_ASSERT(atlas->permanent.free);
  16296. if (!atlas || !atlas->temporary.alloc || !atlas->temporary.free || !memory || !size ||
  16297. !atlas->permanent.alloc || !atlas->permanent.free)
  16298. return 0;
  16299. cfg = (config) ? *config: nk_font_config(height);
  16300. cfg.ttf_blob = memory;
  16301. cfg.ttf_size = size;
  16302. cfg.size = height;
  16303. cfg.ttf_data_owned_by_atlas = 0;
  16304. return nk_font_atlas_add(atlas, &cfg);
  16305. }
  16306. #ifdef NK_INCLUDE_STANDARD_IO
  16307. NK_API struct nk_font*
  16308. nk_font_atlas_add_from_file(struct nk_font_atlas *atlas, const char *file_path,
  16309. float height, const struct nk_font_config *config)
  16310. {
  16311. nk_size size;
  16312. char *memory;
  16313. struct nk_font_config cfg;
  16314. NK_ASSERT(atlas);
  16315. NK_ASSERT(atlas->temporary.alloc);
  16316. NK_ASSERT(atlas->temporary.free);
  16317. NK_ASSERT(atlas->permanent.alloc);
  16318. NK_ASSERT(atlas->permanent.free);
  16319. if (!atlas || !file_path) return 0;
  16320. memory = nk_file_load(file_path, &size, &atlas->permanent);
  16321. if (!memory) return 0;
  16322. cfg = (config) ? *config: nk_font_config(height);
  16323. cfg.ttf_blob = memory;
  16324. cfg.ttf_size = size;
  16325. cfg.size = height;
  16326. cfg.ttf_data_owned_by_atlas = 1;
  16327. return nk_font_atlas_add(atlas, &cfg);
  16328. }
  16329. #endif
  16330. NK_API struct nk_font*
  16331. nk_font_atlas_add_compressed(struct nk_font_atlas *atlas,
  16332. void *compressed_data, nk_size compressed_size, float height,
  16333. const struct nk_font_config *config)
  16334. {
  16335. unsigned int decompressed_size;
  16336. void *decompressed_data;
  16337. struct nk_font_config cfg;
  16338. NK_ASSERT(atlas);
  16339. NK_ASSERT(atlas->temporary.alloc);
  16340. NK_ASSERT(atlas->temporary.free);
  16341. NK_ASSERT(atlas->permanent.alloc);
  16342. NK_ASSERT(atlas->permanent.free);
  16343. NK_ASSERT(compressed_data);
  16344. NK_ASSERT(compressed_size);
  16345. if (!atlas || !compressed_data || !atlas->temporary.alloc || !atlas->temporary.free ||
  16346. !atlas->permanent.alloc || !atlas->permanent.free)
  16347. return 0;
  16348. decompressed_size = nk_decompress_length((unsigned char*)compressed_data);
  16349. decompressed_data = atlas->permanent.alloc(atlas->permanent.userdata,0,decompressed_size);
  16350. NK_ASSERT(decompressed_data);
  16351. if (!decompressed_data) return 0;
  16352. nk_decompress((unsigned char*)decompressed_data, (unsigned char*)compressed_data,
  16353. (unsigned int)compressed_size);
  16354. cfg = (config) ? *config: nk_font_config(height);
  16355. cfg.ttf_blob = decompressed_data;
  16356. cfg.ttf_size = decompressed_size;
  16357. cfg.size = height;
  16358. cfg.ttf_data_owned_by_atlas = 1;
  16359. return nk_font_atlas_add(atlas, &cfg);
  16360. }
  16361. NK_API struct nk_font*
  16362. nk_font_atlas_add_compressed_base85(struct nk_font_atlas *atlas,
  16363. const char *data_base85, float height, const struct nk_font_config *config)
  16364. {
  16365. int compressed_size;
  16366. void *compressed_data;
  16367. struct nk_font *font;
  16368. NK_ASSERT(atlas);
  16369. NK_ASSERT(atlas->temporary.alloc);
  16370. NK_ASSERT(atlas->temporary.free);
  16371. NK_ASSERT(atlas->permanent.alloc);
  16372. NK_ASSERT(atlas->permanent.free);
  16373. NK_ASSERT(data_base85);
  16374. if (!atlas || !data_base85 || !atlas->temporary.alloc || !atlas->temporary.free ||
  16375. !atlas->permanent.alloc || !atlas->permanent.free)
  16376. return 0;
  16377. compressed_size = (((int)nk_strlen(data_base85) + 4) / 5) * 4;
  16378. compressed_data = atlas->temporary.alloc(atlas->temporary.userdata,0, (nk_size)compressed_size);
  16379. NK_ASSERT(compressed_data);
  16380. if (!compressed_data) return 0;
  16381. nk_decode_85((unsigned char*)compressed_data, (const unsigned char*)data_base85);
  16382. font = nk_font_atlas_add_compressed(atlas, compressed_data,
  16383. (nk_size)compressed_size, height, config);
  16384. atlas->temporary.free(atlas->temporary.userdata, compressed_data);
  16385. return font;
  16386. }
  16387. #ifdef NK_INCLUDE_DEFAULT_FONT
  16388. NK_API struct nk_font*
  16389. nk_font_atlas_add_default(struct nk_font_atlas *atlas,
  16390. float pixel_height, const struct nk_font_config *config)
  16391. {
  16392. NK_ASSERT(atlas);
  16393. NK_ASSERT(atlas->temporary.alloc);
  16394. NK_ASSERT(atlas->temporary.free);
  16395. NK_ASSERT(atlas->permanent.alloc);
  16396. NK_ASSERT(atlas->permanent.free);
  16397. return nk_font_atlas_add_compressed_base85(atlas,
  16398. nk_proggy_clean_ttf_compressed_data_base85, pixel_height, config);
  16399. }
  16400. #endif
  16401. NK_API const void*
  16402. nk_font_atlas_bake(struct nk_font_atlas *atlas, int *width, int *height,
  16403. enum nk_font_atlas_format fmt)
  16404. {
  16405. int i = 0;
  16406. void *tmp = 0;
  16407. nk_size tmp_size, img_size;
  16408. struct nk_font *font_iter;
  16409. struct nk_font_baker *baker;
  16410. NK_ASSERT(atlas);
  16411. NK_ASSERT(atlas->temporary.alloc);
  16412. NK_ASSERT(atlas->temporary.free);
  16413. NK_ASSERT(atlas->permanent.alloc);
  16414. NK_ASSERT(atlas->permanent.free);
  16415. NK_ASSERT(width);
  16416. NK_ASSERT(height);
  16417. if (!atlas || !width || !height ||
  16418. !atlas->temporary.alloc || !atlas->temporary.free ||
  16419. !atlas->permanent.alloc || !atlas->permanent.free)
  16420. return 0;
  16421. #ifdef NK_INCLUDE_DEFAULT_FONT
  16422. /* no font added so just use default font */
  16423. if (!atlas->font_num)
  16424. atlas->default_font = nk_font_atlas_add_default(atlas, 13.0f, 0);
  16425. #endif
  16426. NK_ASSERT(atlas->font_num);
  16427. if (!atlas->font_num) return 0;
  16428. /* allocate temporary baker memory required for the baking process */
  16429. nk_font_baker_memory(&tmp_size, &atlas->glyph_count, atlas->config, atlas->font_num);
  16430. tmp = atlas->temporary.alloc(atlas->temporary.userdata,0, tmp_size);
  16431. NK_ASSERT(tmp);
  16432. if (!tmp) goto failed;
  16433. NK_MEMSET(tmp,0,tmp_size);
  16434. /* allocate glyph memory for all fonts */
  16435. baker = nk_font_baker(tmp, atlas->glyph_count, atlas->font_num, &atlas->temporary);
  16436. atlas->glyphs = (struct nk_font_glyph*)atlas->permanent.alloc(
  16437. atlas->permanent.userdata,0, sizeof(struct nk_font_glyph)*(nk_size)atlas->glyph_count);
  16438. NK_ASSERT(atlas->glyphs);
  16439. if (!atlas->glyphs)
  16440. goto failed;
  16441. /* pack all glyphs into a tight fit space */
  16442. atlas->custom.w = (NK_CURSOR_DATA_W*2)+1;
  16443. atlas->custom.h = NK_CURSOR_DATA_H + 1;
  16444. if (!nk_font_bake_pack(baker, &img_size, width, height, &atlas->custom,
  16445. atlas->config, atlas->font_num, &atlas->temporary))
  16446. goto failed;
  16447. /* allocate memory for the baked image font atlas */
  16448. atlas->pixel = atlas->temporary.alloc(atlas->temporary.userdata,0, img_size);
  16449. NK_ASSERT(atlas->pixel);
  16450. if (!atlas->pixel)
  16451. goto failed;
  16452. /* bake glyphs and custom white pixel into image */
  16453. nk_font_bake(baker, atlas->pixel, *width, *height,
  16454. atlas->glyphs, atlas->glyph_count, atlas->config, atlas->font_num);
  16455. nk_font_bake_custom_data(atlas->pixel, *width, *height, atlas->custom,
  16456. nk_custom_cursor_data, NK_CURSOR_DATA_W, NK_CURSOR_DATA_H, '.', 'X');
  16457. if (fmt == NK_FONT_ATLAS_RGBA32) {
  16458. /* convert alpha8 image into rgba32 image */
  16459. void *img_rgba = atlas->temporary.alloc(atlas->temporary.userdata,0,
  16460. (nk_size)(*width * *height * 4));
  16461. NK_ASSERT(img_rgba);
  16462. if (!img_rgba) goto failed;
  16463. nk_font_bake_convert(img_rgba, *width, *height, atlas->pixel);
  16464. atlas->temporary.free(atlas->temporary.userdata, atlas->pixel);
  16465. atlas->pixel = img_rgba;
  16466. }
  16467. atlas->tex_width = *width;
  16468. atlas->tex_height = *height;
  16469. /* initialize each font */
  16470. for (font_iter = atlas->fonts; font_iter; font_iter = font_iter->next) {
  16471. struct nk_font *font = font_iter;
  16472. struct nk_font_config *config = font->config;
  16473. nk_font_init(font, config->size, config->fallback_glyph, atlas->glyphs,
  16474. config->font, nk_handle_ptr(0));
  16475. }
  16476. /* initialize each cursor */
  16477. {NK_STORAGE const struct nk_vec2 nk_cursor_data[NK_CURSOR_COUNT][3] = {
  16478. /* Pos Size Offset */
  16479. {{ 0, 3}, {12,19}, { 0, 0}},
  16480. {{13, 0}, { 7,16}, { 4, 8}},
  16481. {{31, 0}, {23,23}, {11,11}},
  16482. {{21, 0}, { 9, 23}, { 5,11}},
  16483. {{55,18}, {23, 9}, {11, 5}},
  16484. {{73, 0}, {17,17}, { 9, 9}},
  16485. {{55, 0}, {17,17}, { 9, 9}}
  16486. };
  16487. for (i = 0; i < NK_CURSOR_COUNT; ++i) {
  16488. struct nk_cursor *cursor = &atlas->cursors[i];
  16489. cursor->img.w = (unsigned short)*width;
  16490. cursor->img.h = (unsigned short)*height;
  16491. cursor->img.region[0] = (unsigned short)(atlas->custom.x + nk_cursor_data[i][0].x);
  16492. cursor->img.region[1] = (unsigned short)(atlas->custom.y + nk_cursor_data[i][0].y);
  16493. cursor->img.region[2] = (unsigned short)nk_cursor_data[i][1].x;
  16494. cursor->img.region[3] = (unsigned short)nk_cursor_data[i][1].y;
  16495. cursor->size = nk_cursor_data[i][1];
  16496. cursor->offset = nk_cursor_data[i][2];
  16497. }}
  16498. /* free temporary memory */
  16499. atlas->temporary.free(atlas->temporary.userdata, tmp);
  16500. return atlas->pixel;
  16501. failed:
  16502. /* error so cleanup all memory */
  16503. if (tmp) atlas->temporary.free(atlas->temporary.userdata, tmp);
  16504. if (atlas->glyphs) {
  16505. atlas->permanent.free(atlas->permanent.userdata, atlas->glyphs);
  16506. atlas->glyphs = 0;
  16507. }
  16508. if (atlas->pixel) {
  16509. atlas->temporary.free(atlas->temporary.userdata, atlas->pixel);
  16510. atlas->pixel = 0;
  16511. }
  16512. return 0;
  16513. }
  16514. NK_API void
  16515. nk_font_atlas_end(struct nk_font_atlas *atlas, nk_handle texture,
  16516. struct nk_draw_null_texture *tex_null)
  16517. {
  16518. int i = 0;
  16519. struct nk_font *font_iter;
  16520. NK_ASSERT(atlas);
  16521. if (!atlas) {
  16522. if (!tex_null) return;
  16523. tex_null->texture = texture;
  16524. tex_null->uv = nk_vec2(0.5f,0.5f);
  16525. }
  16526. if (tex_null) {
  16527. tex_null->texture = texture;
  16528. tex_null->uv.x = (atlas->custom.x + 0.5f)/(float)atlas->tex_width;
  16529. tex_null->uv.y = (atlas->custom.y + 0.5f)/(float)atlas->tex_height;
  16530. }
  16531. for (font_iter = atlas->fonts; font_iter; font_iter = font_iter->next) {
  16532. font_iter->texture = texture;
  16533. #ifdef NK_INCLUDE_VERTEX_BUFFER_OUTPUT
  16534. font_iter->handle.texture = texture;
  16535. #endif
  16536. }
  16537. for (i = 0; i < NK_CURSOR_COUNT; ++i)
  16538. atlas->cursors[i].img.handle = texture;
  16539. atlas->temporary.free(atlas->temporary.userdata, atlas->pixel);
  16540. atlas->pixel = 0;
  16541. atlas->tex_width = 0;
  16542. atlas->tex_height = 0;
  16543. atlas->custom.x = 0;
  16544. atlas->custom.y = 0;
  16545. atlas->custom.w = 0;
  16546. atlas->custom.h = 0;
  16547. }
  16548. NK_API void
  16549. nk_font_atlas_cleanup(struct nk_font_atlas *atlas)
  16550. {
  16551. NK_ASSERT(atlas);
  16552. NK_ASSERT(atlas->temporary.alloc);
  16553. NK_ASSERT(atlas->temporary.free);
  16554. NK_ASSERT(atlas->permanent.alloc);
  16555. NK_ASSERT(atlas->permanent.free);
  16556. if (!atlas || !atlas->permanent.alloc || !atlas->permanent.free) return;
  16557. if (atlas->config) {
  16558. struct nk_font_config *iter;
  16559. for (iter = atlas->config; iter; iter = iter->next) {
  16560. struct nk_font_config *i;
  16561. for (i = iter->n; i != iter; i = i->n) {
  16562. atlas->permanent.free(atlas->permanent.userdata, i->ttf_blob);
  16563. i->ttf_blob = 0;
  16564. }
  16565. atlas->permanent.free(atlas->permanent.userdata, iter->ttf_blob);
  16566. iter->ttf_blob = 0;
  16567. }
  16568. }
  16569. }
  16570. NK_API void
  16571. nk_font_atlas_clear(struct nk_font_atlas *atlas)
  16572. {
  16573. NK_ASSERT(atlas);
  16574. NK_ASSERT(atlas->temporary.alloc);
  16575. NK_ASSERT(atlas->temporary.free);
  16576. NK_ASSERT(atlas->permanent.alloc);
  16577. NK_ASSERT(atlas->permanent.free);
  16578. if (!atlas || !atlas->permanent.alloc || !atlas->permanent.free) return;
  16579. if (atlas->config) {
  16580. struct nk_font_config *iter, *next;
  16581. for (iter = atlas->config; iter; iter = next) {
  16582. struct nk_font_config *i, *n;
  16583. for (i = iter->n; i != iter; i = n) {
  16584. n = i->n;
  16585. if (i->ttf_blob)
  16586. atlas->permanent.free(atlas->permanent.userdata, i->ttf_blob);
  16587. atlas->permanent.free(atlas->permanent.userdata, i);
  16588. }
  16589. next = iter->next;
  16590. if (i->ttf_blob)
  16591. atlas->permanent.free(atlas->permanent.userdata, iter->ttf_blob);
  16592. atlas->permanent.free(atlas->permanent.userdata, iter);
  16593. }
  16594. atlas->config = 0;
  16595. }
  16596. if (atlas->fonts) {
  16597. struct nk_font *iter, *next;
  16598. for (iter = atlas->fonts; iter; iter = next) {
  16599. next = iter->next;
  16600. atlas->permanent.free(atlas->permanent.userdata, iter);
  16601. }
  16602. atlas->fonts = 0;
  16603. }
  16604. if (atlas->glyphs)
  16605. atlas->permanent.free(atlas->permanent.userdata, atlas->glyphs);
  16606. nk_zero_struct(*atlas);
  16607. }
  16608. #endif
  16609. /* ===============================================================
  16610. *
  16611. * INPUT
  16612. *
  16613. * ===============================================================*/
  16614. NK_API void
  16615. nk_input_begin(struct nk_context *ctx)
  16616. {
  16617. int i;
  16618. struct nk_input *in;
  16619. NK_ASSERT(ctx);
  16620. if (!ctx) return;
  16621. in = &ctx->input;
  16622. for (i = 0; i < NK_BUTTON_MAX; ++i)
  16623. in->mouse.buttons[i].clicked = 0;
  16624. in->keyboard.text_len = 0;
  16625. in->mouse.scroll_delta = nk_vec2(0,0);
  16626. in->mouse.prev.x = in->mouse.pos.x;
  16627. in->mouse.prev.y = in->mouse.pos.y;
  16628. in->mouse.delta.x = 0;
  16629. in->mouse.delta.y = 0;
  16630. for (i = 0; i < NK_KEY_MAX; i++)
  16631. in->keyboard.keys[i].clicked = 0;
  16632. }
  16633. NK_API void
  16634. nk_input_end(struct nk_context *ctx)
  16635. {
  16636. struct nk_input *in;
  16637. NK_ASSERT(ctx);
  16638. if (!ctx) return;
  16639. in = &ctx->input;
  16640. if (in->mouse.grab)
  16641. in->mouse.grab = 0;
  16642. if (in->mouse.ungrab) {
  16643. in->mouse.grabbed = 0;
  16644. in->mouse.ungrab = 0;
  16645. in->mouse.grab = 0;
  16646. }
  16647. }
  16648. NK_API void
  16649. nk_input_motion(struct nk_context *ctx, int x, int y)
  16650. {
  16651. struct nk_input *in;
  16652. NK_ASSERT(ctx);
  16653. if (!ctx) return;
  16654. in = &ctx->input;
  16655. in->mouse.pos.x = (float)x;
  16656. in->mouse.pos.y = (float)y;
  16657. in->mouse.delta.x = in->mouse.pos.x - in->mouse.prev.x;
  16658. in->mouse.delta.y = in->mouse.pos.y - in->mouse.prev.y;
  16659. }
  16660. NK_API void
  16661. nk_input_key(struct nk_context *ctx, enum nk_keys key, nk_bool down)
  16662. {
  16663. struct nk_input *in;
  16664. NK_ASSERT(ctx);
  16665. if (!ctx) return;
  16666. in = &ctx->input;
  16667. #ifdef NK_KEYSTATE_BASED_INPUT
  16668. if (in->keyboard.keys[key].down != down)
  16669. in->keyboard.keys[key].clicked++;
  16670. #else
  16671. in->keyboard.keys[key].clicked++;
  16672. #endif
  16673. in->keyboard.keys[key].down = down;
  16674. }
  16675. NK_API void
  16676. nk_input_button(struct nk_context *ctx, enum nk_buttons id, int x, int y, nk_bool down)
  16677. {
  16678. struct nk_mouse_button *btn;
  16679. struct nk_input *in;
  16680. NK_ASSERT(ctx);
  16681. if (!ctx) return;
  16682. in = &ctx->input;
  16683. if (in->mouse.buttons[id].down == down) return;
  16684. btn = &in->mouse.buttons[id];
  16685. btn->clicked_pos.x = (float)x;
  16686. btn->clicked_pos.y = (float)y;
  16687. btn->down = down;
  16688. btn->clicked++;
  16689. /* Fix Click-Drag for touch events. */
  16690. in->mouse.delta.x = 0;
  16691. in->mouse.delta.y = 0;
  16692. #ifdef NK_BUTTON_TRIGGER_ON_RELEASE
  16693. if (down == 1 && id == NK_BUTTON_LEFT)
  16694. {
  16695. in->mouse.down_pos.x = btn->clicked_pos.x;
  16696. in->mouse.down_pos.y = btn->clicked_pos.y;
  16697. }
  16698. #endif
  16699. }
  16700. NK_API void
  16701. nk_input_scroll(struct nk_context *ctx, struct nk_vec2 val)
  16702. {
  16703. NK_ASSERT(ctx);
  16704. if (!ctx) return;
  16705. ctx->input.mouse.scroll_delta.x += val.x;
  16706. ctx->input.mouse.scroll_delta.y += val.y;
  16707. }
  16708. NK_API void
  16709. nk_input_glyph(struct nk_context *ctx, const nk_glyph glyph)
  16710. {
  16711. int len = 0;
  16712. nk_rune unicode;
  16713. struct nk_input *in;
  16714. NK_ASSERT(ctx);
  16715. if (!ctx) return;
  16716. in = &ctx->input;
  16717. len = nk_utf_decode(glyph, &unicode, NK_UTF_SIZE);
  16718. if (len && ((in->keyboard.text_len + len) < NK_INPUT_MAX)) {
  16719. nk_utf_encode(unicode, &in->keyboard.text[in->keyboard.text_len],
  16720. NK_INPUT_MAX - in->keyboard.text_len);
  16721. in->keyboard.text_len += len;
  16722. }
  16723. }
  16724. NK_API void
  16725. nk_input_char(struct nk_context *ctx, char c)
  16726. {
  16727. nk_glyph glyph = {0};
  16728. NK_ASSERT(ctx);
  16729. if (!ctx) return;
  16730. glyph[0] = c;
  16731. nk_input_glyph(ctx, glyph);
  16732. }
  16733. NK_API void
  16734. nk_input_unicode(struct nk_context *ctx, nk_rune unicode)
  16735. {
  16736. nk_glyph rune;
  16737. NK_ASSERT(ctx);
  16738. if (!ctx) return;
  16739. nk_utf_encode(unicode, rune, NK_UTF_SIZE);
  16740. nk_input_glyph(ctx, rune);
  16741. }
  16742. NK_API nk_bool
  16743. nk_input_has_mouse_click(const struct nk_input *i, enum nk_buttons id)
  16744. {
  16745. const struct nk_mouse_button *btn;
  16746. if (!i) return nk_false;
  16747. btn = &i->mouse.buttons[id];
  16748. return (btn->clicked && btn->down == nk_false) ? nk_true : nk_false;
  16749. }
  16750. NK_API nk_bool
  16751. nk_input_has_mouse_click_in_rect(const struct nk_input *i, enum nk_buttons id,
  16752. struct nk_rect b)
  16753. {
  16754. const struct nk_mouse_button *btn;
  16755. if (!i) return nk_false;
  16756. btn = &i->mouse.buttons[id];
  16757. if (!NK_INBOX(btn->clicked_pos.x,btn->clicked_pos.y,b.x,b.y,b.w,b.h))
  16758. return nk_false;
  16759. return nk_true;
  16760. }
  16761. NK_API nk_bool
  16762. nk_input_has_mouse_click_in_button_rect(const struct nk_input *i, enum nk_buttons id,
  16763. struct nk_rect b)
  16764. {
  16765. const struct nk_mouse_button *btn;
  16766. if (!i) return nk_false;
  16767. btn = &i->mouse.buttons[id];
  16768. #ifdef NK_BUTTON_TRIGGER_ON_RELEASE
  16769. if (!NK_INBOX(btn->clicked_pos.x,btn->clicked_pos.y,b.x,b.y,b.w,b.h)
  16770. || !NK_INBOX(i->mouse.down_pos.x,i->mouse.down_pos.y,b.x,b.y,b.w,b.h))
  16771. #else
  16772. if (!NK_INBOX(btn->clicked_pos.x,btn->clicked_pos.y,b.x,b.y,b.w,b.h))
  16773. #endif
  16774. return nk_false;
  16775. return nk_true;
  16776. }
  16777. NK_API nk_bool
  16778. nk_input_has_mouse_click_down_in_rect(const struct nk_input *i, enum nk_buttons id,
  16779. struct nk_rect b, nk_bool down)
  16780. {
  16781. const struct nk_mouse_button *btn;
  16782. if (!i) return nk_false;
  16783. btn = &i->mouse.buttons[id];
  16784. return nk_input_has_mouse_click_in_rect(i, id, b) && (btn->down == down);
  16785. }
  16786. NK_API nk_bool
  16787. nk_input_is_mouse_click_in_rect(const struct nk_input *i, enum nk_buttons id,
  16788. struct nk_rect b)
  16789. {
  16790. const struct nk_mouse_button *btn;
  16791. if (!i) return nk_false;
  16792. btn = &i->mouse.buttons[id];
  16793. return (nk_input_has_mouse_click_down_in_rect(i, id, b, nk_false) &&
  16794. btn->clicked) ? nk_true : nk_false;
  16795. }
  16796. NK_API nk_bool
  16797. nk_input_is_mouse_click_down_in_rect(const struct nk_input *i, enum nk_buttons id,
  16798. struct nk_rect b, nk_bool down)
  16799. {
  16800. const struct nk_mouse_button *btn;
  16801. if (!i) return nk_false;
  16802. btn = &i->mouse.buttons[id];
  16803. return (nk_input_has_mouse_click_down_in_rect(i, id, b, down) &&
  16804. btn->clicked) ? nk_true : nk_false;
  16805. }
  16806. NK_API nk_bool
  16807. nk_input_any_mouse_click_in_rect(const struct nk_input *in, struct nk_rect b)
  16808. {
  16809. int i, down = 0;
  16810. for (i = 0; i < NK_BUTTON_MAX; ++i)
  16811. down = down || nk_input_is_mouse_click_in_rect(in, (enum nk_buttons)i, b);
  16812. return down;
  16813. }
  16814. NK_API nk_bool
  16815. nk_input_is_mouse_hovering_rect(const struct nk_input *i, struct nk_rect rect)
  16816. {
  16817. if (!i) return nk_false;
  16818. return NK_INBOX(i->mouse.pos.x, i->mouse.pos.y, rect.x, rect.y, rect.w, rect.h);
  16819. }
  16820. NK_API nk_bool
  16821. nk_input_is_mouse_prev_hovering_rect(const struct nk_input *i, struct nk_rect rect)
  16822. {
  16823. if (!i) return nk_false;
  16824. return NK_INBOX(i->mouse.prev.x, i->mouse.prev.y, rect.x, rect.y, rect.w, rect.h);
  16825. }
  16826. NK_API nk_bool
  16827. nk_input_mouse_clicked(const struct nk_input *i, enum nk_buttons id, struct nk_rect rect)
  16828. {
  16829. if (!i) return nk_false;
  16830. if (!nk_input_is_mouse_hovering_rect(i, rect)) return nk_false;
  16831. return nk_input_is_mouse_click_in_rect(i, id, rect);
  16832. }
  16833. NK_API nk_bool
  16834. nk_input_is_mouse_down(const struct nk_input *i, enum nk_buttons id)
  16835. {
  16836. if (!i) return nk_false;
  16837. return i->mouse.buttons[id].down;
  16838. }
  16839. NK_API nk_bool
  16840. nk_input_is_mouse_pressed(const struct nk_input *i, enum nk_buttons id)
  16841. {
  16842. const struct nk_mouse_button *b;
  16843. if (!i) return nk_false;
  16844. b = &i->mouse.buttons[id];
  16845. if (b->down && b->clicked)
  16846. return nk_true;
  16847. return nk_false;
  16848. }
  16849. NK_API nk_bool
  16850. nk_input_is_mouse_released(const struct nk_input *i, enum nk_buttons id)
  16851. {
  16852. if (!i) return nk_false;
  16853. return (!i->mouse.buttons[id].down && i->mouse.buttons[id].clicked);
  16854. }
  16855. NK_API nk_bool
  16856. nk_input_is_key_pressed(const struct nk_input *i, enum nk_keys key)
  16857. {
  16858. const struct nk_key *k;
  16859. if (!i) return nk_false;
  16860. k = &i->keyboard.keys[key];
  16861. if ((k->down && k->clicked) || (!k->down && k->clicked >= 2))
  16862. return nk_true;
  16863. return nk_false;
  16864. }
  16865. NK_API nk_bool
  16866. nk_input_is_key_released(const struct nk_input *i, enum nk_keys key)
  16867. {
  16868. const struct nk_key *k;
  16869. if (!i) return nk_false;
  16870. k = &i->keyboard.keys[key];
  16871. if ((!k->down && k->clicked) || (k->down && k->clicked >= 2))
  16872. return nk_true;
  16873. return nk_false;
  16874. }
  16875. NK_API nk_bool
  16876. nk_input_is_key_down(const struct nk_input *i, enum nk_keys key)
  16877. {
  16878. const struct nk_key *k;
  16879. if (!i) return nk_false;
  16880. k = &i->keyboard.keys[key];
  16881. if (k->down) return nk_true;
  16882. return nk_false;
  16883. }
  16884. /* ===============================================================
  16885. *
  16886. * STYLE
  16887. *
  16888. * ===============================================================*/
  16889. NK_API void nk_style_default(struct nk_context *ctx){nk_style_from_table(ctx, 0);}
  16890. #define NK_COLOR_MAP(NK_COLOR)\
  16891. NK_COLOR(NK_COLOR_TEXT, 175,175,175,255) \
  16892. NK_COLOR(NK_COLOR_WINDOW, 45, 45, 45, 255) \
  16893. NK_COLOR(NK_COLOR_HEADER, 40, 40, 40, 255) \
  16894. NK_COLOR(NK_COLOR_BORDER, 65, 65, 65, 255) \
  16895. NK_COLOR(NK_COLOR_BUTTON, 50, 50, 50, 255) \
  16896. NK_COLOR(NK_COLOR_BUTTON_HOVER, 40, 40, 40, 255) \
  16897. NK_COLOR(NK_COLOR_BUTTON_ACTIVE, 35, 35, 35, 255) \
  16898. NK_COLOR(NK_COLOR_TOGGLE, 100,100,100,255) \
  16899. NK_COLOR(NK_COLOR_TOGGLE_HOVER, 120,120,120,255) \
  16900. NK_COLOR(NK_COLOR_TOGGLE_CURSOR, 45, 45, 45, 255) \
  16901. NK_COLOR(NK_COLOR_SELECT, 45, 45, 45, 255) \
  16902. NK_COLOR(NK_COLOR_SELECT_ACTIVE, 35, 35, 35,255) \
  16903. NK_COLOR(NK_COLOR_SLIDER, 38, 38, 38, 255) \
  16904. NK_COLOR(NK_COLOR_SLIDER_CURSOR, 100,100,100,255) \
  16905. NK_COLOR(NK_COLOR_SLIDER_CURSOR_HOVER, 120,120,120,255) \
  16906. NK_COLOR(NK_COLOR_SLIDER_CURSOR_ACTIVE, 150,150,150,255) \
  16907. NK_COLOR(NK_COLOR_PROPERTY, 38, 38, 38, 255) \
  16908. NK_COLOR(NK_COLOR_EDIT, 38, 38, 38, 255) \
  16909. NK_COLOR(NK_COLOR_EDIT_CURSOR, 175,175,175,255) \
  16910. NK_COLOR(NK_COLOR_COMBO, 45, 45, 45, 255) \
  16911. NK_COLOR(NK_COLOR_CHART, 120,120,120,255) \
  16912. NK_COLOR(NK_COLOR_CHART_COLOR, 45, 45, 45, 255) \
  16913. NK_COLOR(NK_COLOR_CHART_COLOR_HIGHLIGHT, 255, 0, 0, 255) \
  16914. NK_COLOR(NK_COLOR_SCROLLBAR, 40, 40, 40, 255) \
  16915. NK_COLOR(NK_COLOR_SCROLLBAR_CURSOR, 100,100,100,255) \
  16916. NK_COLOR(NK_COLOR_SCROLLBAR_CURSOR_HOVER, 120,120,120,255) \
  16917. NK_COLOR(NK_COLOR_SCROLLBAR_CURSOR_ACTIVE, 150,150,150,255) \
  16918. NK_COLOR(NK_COLOR_TAB_HEADER, 40, 40, 40,255) \
  16919. NK_COLOR(NK_COLOR_KNOB, 38, 38, 38, 255) \
  16920. NK_COLOR(NK_COLOR_KNOB_CURSOR, 100,100,100,255) \
  16921. NK_COLOR(NK_COLOR_KNOB_CURSOR_HOVER, 120,120,120,255) \
  16922. NK_COLOR(NK_COLOR_KNOB_CURSOR_ACTIVE, 150,150,150,255)
  16923. NK_GLOBAL const struct nk_color
  16924. nk_default_color_style[NK_COLOR_COUNT] = {
  16925. #define NK_COLOR(a,b,c,d,e) {b,c,d,e},
  16926. NK_COLOR_MAP(NK_COLOR)
  16927. #undef NK_COLOR
  16928. };
  16929. NK_GLOBAL const char *nk_color_names[NK_COLOR_COUNT] = {
  16930. #define NK_COLOR(a,b,c,d,e) #a,
  16931. NK_COLOR_MAP(NK_COLOR)
  16932. #undef NK_COLOR
  16933. };
  16934. NK_API const char*
  16935. nk_style_get_color_by_name(enum nk_style_colors c)
  16936. {
  16937. return nk_color_names[c];
  16938. }
  16939. NK_API struct nk_style_item
  16940. nk_style_item_color(struct nk_color col)
  16941. {
  16942. struct nk_style_item i;
  16943. i.type = NK_STYLE_ITEM_COLOR;
  16944. i.data.color = col;
  16945. return i;
  16946. }
  16947. NK_API struct nk_style_item
  16948. nk_style_item_image(struct nk_image img)
  16949. {
  16950. struct nk_style_item i;
  16951. i.type = NK_STYLE_ITEM_IMAGE;
  16952. i.data.image = img;
  16953. return i;
  16954. }
  16955. NK_API struct nk_style_item
  16956. nk_style_item_nine_slice(struct nk_nine_slice slice)
  16957. {
  16958. struct nk_style_item i;
  16959. i.type = NK_STYLE_ITEM_NINE_SLICE;
  16960. i.data.slice = slice;
  16961. return i;
  16962. }
  16963. NK_API struct nk_style_item
  16964. nk_style_item_hide(void)
  16965. {
  16966. struct nk_style_item i;
  16967. i.type = NK_STYLE_ITEM_COLOR;
  16968. i.data.color = nk_rgba(0,0,0,0);
  16969. return i;
  16970. }
  16971. NK_API void
  16972. nk_style_from_table(struct nk_context *ctx, const struct nk_color *table)
  16973. {
  16974. struct nk_style *style;
  16975. struct nk_style_text *text;
  16976. struct nk_style_button *button;
  16977. struct nk_style_toggle *toggle;
  16978. struct nk_style_selectable *select;
  16979. struct nk_style_slider *slider;
  16980. struct nk_style_knob *knob;
  16981. struct nk_style_progress *prog;
  16982. struct nk_style_scrollbar *scroll;
  16983. struct nk_style_edit *edit;
  16984. struct nk_style_property *property;
  16985. struct nk_style_combo *combo;
  16986. struct nk_style_chart *chart;
  16987. struct nk_style_tab *tab;
  16988. struct nk_style_window *win;
  16989. NK_ASSERT(ctx);
  16990. if (!ctx) return;
  16991. style = &ctx->style;
  16992. table = (!table) ? nk_default_color_style: table;
  16993. /* default text */
  16994. text = &style->text;
  16995. text->color = table[NK_COLOR_TEXT];
  16996. text->padding = nk_vec2(0,0);
  16997. text->color_factor = 1.0f;
  16998. text->disabled_factor = NK_WIDGET_DISABLED_FACTOR;
  16999. /* default button */
  17000. button = &style->button;
  17001. nk_zero_struct(*button);
  17002. button->normal = nk_style_item_color(table[NK_COLOR_BUTTON]);
  17003. button->hover = nk_style_item_color(table[NK_COLOR_BUTTON_HOVER]);
  17004. button->active = nk_style_item_color(table[NK_COLOR_BUTTON_ACTIVE]);
  17005. button->border_color = table[NK_COLOR_BORDER];
  17006. button->text_background = table[NK_COLOR_BUTTON];
  17007. button->text_normal = table[NK_COLOR_TEXT];
  17008. button->text_hover = table[NK_COLOR_TEXT];
  17009. button->text_active = table[NK_COLOR_TEXT];
  17010. button->padding = nk_vec2(2.0f,2.0f);
  17011. button->image_padding = nk_vec2(0.0f,0.0f);
  17012. button->touch_padding = nk_vec2(0.0f, 0.0f);
  17013. button->userdata = nk_handle_ptr(0);
  17014. button->text_alignment = NK_TEXT_CENTERED;
  17015. button->border = 1.0f;
  17016. button->rounding = 4.0f;
  17017. button->color_factor_text = 1.0f;
  17018. button->color_factor_background = 1.0f;
  17019. button->disabled_factor = NK_WIDGET_DISABLED_FACTOR;
  17020. button->draw_begin = 0;
  17021. button->draw_end = 0;
  17022. /* contextual button */
  17023. button = &style->contextual_button;
  17024. nk_zero_struct(*button);
  17025. button->normal = nk_style_item_color(table[NK_COLOR_WINDOW]);
  17026. button->hover = nk_style_item_color(table[NK_COLOR_BUTTON_HOVER]);
  17027. button->active = nk_style_item_color(table[NK_COLOR_BUTTON_ACTIVE]);
  17028. button->border_color = table[NK_COLOR_WINDOW];
  17029. button->text_background = table[NK_COLOR_WINDOW];
  17030. button->text_normal = table[NK_COLOR_TEXT];
  17031. button->text_hover = table[NK_COLOR_TEXT];
  17032. button->text_active = table[NK_COLOR_TEXT];
  17033. button->padding = nk_vec2(2.0f,2.0f);
  17034. button->touch_padding = nk_vec2(0.0f,0.0f);
  17035. button->userdata = nk_handle_ptr(0);
  17036. button->text_alignment = NK_TEXT_CENTERED;
  17037. button->border = 0.0f;
  17038. button->rounding = 0.0f;
  17039. button->color_factor_text = 1.0f;
  17040. button->color_factor_background = 1.0f;
  17041. button->disabled_factor = NK_WIDGET_DISABLED_FACTOR;
  17042. button->draw_begin = 0;
  17043. button->draw_end = 0;
  17044. /* menu button */
  17045. button = &style->menu_button;
  17046. nk_zero_struct(*button);
  17047. button->normal = nk_style_item_color(table[NK_COLOR_WINDOW]);
  17048. button->hover = nk_style_item_color(table[NK_COLOR_WINDOW]);
  17049. button->active = nk_style_item_color(table[NK_COLOR_WINDOW]);
  17050. button->border_color = table[NK_COLOR_WINDOW];
  17051. button->text_background = table[NK_COLOR_WINDOW];
  17052. button->text_normal = table[NK_COLOR_TEXT];
  17053. button->text_hover = table[NK_COLOR_TEXT];
  17054. button->text_active = table[NK_COLOR_TEXT];
  17055. button->padding = nk_vec2(2.0f,2.0f);
  17056. button->touch_padding = nk_vec2(0.0f,0.0f);
  17057. button->userdata = nk_handle_ptr(0);
  17058. button->text_alignment = NK_TEXT_CENTERED;
  17059. button->border = 0.0f;
  17060. button->rounding = 1.0f;
  17061. button->color_factor_text = 1.0f;
  17062. button->color_factor_background = 1.0f;
  17063. button->disabled_factor = NK_WIDGET_DISABLED_FACTOR;
  17064. button->draw_begin = 0;
  17065. button->draw_end = 0;
  17066. /* checkbox toggle */
  17067. toggle = &style->checkbox;
  17068. nk_zero_struct(*toggle);
  17069. toggle->normal = nk_style_item_color(table[NK_COLOR_TOGGLE]);
  17070. toggle->hover = nk_style_item_color(table[NK_COLOR_TOGGLE_HOVER]);
  17071. toggle->active = nk_style_item_color(table[NK_COLOR_TOGGLE_HOVER]);
  17072. toggle->cursor_normal = nk_style_item_color(table[NK_COLOR_TOGGLE_CURSOR]);
  17073. toggle->cursor_hover = nk_style_item_color(table[NK_COLOR_TOGGLE_CURSOR]);
  17074. toggle->userdata = nk_handle_ptr(0);
  17075. toggle->text_background = table[NK_COLOR_WINDOW];
  17076. toggle->text_normal = table[NK_COLOR_TEXT];
  17077. toggle->text_hover = table[NK_COLOR_TEXT];
  17078. toggle->text_active = table[NK_COLOR_TEXT];
  17079. toggle->padding = nk_vec2(2.0f, 2.0f);
  17080. toggle->touch_padding = nk_vec2(0,0);
  17081. toggle->border_color = nk_rgba(0,0,0,0);
  17082. toggle->border = 0.0f;
  17083. toggle->spacing = 4;
  17084. toggle->color_factor = 1.0f;
  17085. toggle->disabled_factor = NK_WIDGET_DISABLED_FACTOR;
  17086. /* option toggle */
  17087. toggle = &style->option;
  17088. nk_zero_struct(*toggle);
  17089. toggle->normal = nk_style_item_color(table[NK_COLOR_TOGGLE]);
  17090. toggle->hover = nk_style_item_color(table[NK_COLOR_TOGGLE_HOVER]);
  17091. toggle->active = nk_style_item_color(table[NK_COLOR_TOGGLE_HOVER]);
  17092. toggle->cursor_normal = nk_style_item_color(table[NK_COLOR_TOGGLE_CURSOR]);
  17093. toggle->cursor_hover = nk_style_item_color(table[NK_COLOR_TOGGLE_CURSOR]);
  17094. toggle->userdata = nk_handle_ptr(0);
  17095. toggle->text_background = table[NK_COLOR_WINDOW];
  17096. toggle->text_normal = table[NK_COLOR_TEXT];
  17097. toggle->text_hover = table[NK_COLOR_TEXT];
  17098. toggle->text_active = table[NK_COLOR_TEXT];
  17099. toggle->padding = nk_vec2(3.0f, 3.0f);
  17100. toggle->touch_padding = nk_vec2(0,0);
  17101. toggle->border_color = nk_rgba(0,0,0,0);
  17102. toggle->border = 0.0f;
  17103. toggle->spacing = 4;
  17104. toggle->color_factor = 1.0f;
  17105. toggle->disabled_factor = NK_WIDGET_DISABLED_FACTOR;
  17106. /* selectable */
  17107. select = &style->selectable;
  17108. nk_zero_struct(*select);
  17109. select->normal = nk_style_item_color(table[NK_COLOR_SELECT]);
  17110. select->hover = nk_style_item_color(table[NK_COLOR_SELECT]);
  17111. select->pressed = nk_style_item_color(table[NK_COLOR_SELECT]);
  17112. select->normal_active = nk_style_item_color(table[NK_COLOR_SELECT_ACTIVE]);
  17113. select->hover_active = nk_style_item_color(table[NK_COLOR_SELECT_ACTIVE]);
  17114. select->pressed_active = nk_style_item_color(table[NK_COLOR_SELECT_ACTIVE]);
  17115. select->text_normal = table[NK_COLOR_TEXT];
  17116. select->text_hover = table[NK_COLOR_TEXT];
  17117. select->text_pressed = table[NK_COLOR_TEXT];
  17118. select->text_normal_active = table[NK_COLOR_TEXT];
  17119. select->text_hover_active = table[NK_COLOR_TEXT];
  17120. select->text_pressed_active = table[NK_COLOR_TEXT];
  17121. select->padding = nk_vec2(2.0f,2.0f);
  17122. select->image_padding = nk_vec2(2.0f,2.0f);
  17123. select->touch_padding = nk_vec2(0,0);
  17124. select->userdata = nk_handle_ptr(0);
  17125. select->rounding = 0.0f;
  17126. select->color_factor = 1.0f;
  17127. select->disabled_factor = NK_WIDGET_DISABLED_FACTOR;
  17128. select->draw_begin = 0;
  17129. select->draw_end = 0;
  17130. /* slider */
  17131. slider = &style->slider;
  17132. nk_zero_struct(*slider);
  17133. slider->normal = nk_style_item_hide();
  17134. slider->hover = nk_style_item_hide();
  17135. slider->active = nk_style_item_hide();
  17136. slider->bar_normal = table[NK_COLOR_SLIDER];
  17137. slider->bar_hover = table[NK_COLOR_SLIDER];
  17138. slider->bar_active = table[NK_COLOR_SLIDER];
  17139. slider->bar_filled = table[NK_COLOR_SLIDER_CURSOR];
  17140. slider->cursor_normal = nk_style_item_color(table[NK_COLOR_SLIDER_CURSOR]);
  17141. slider->cursor_hover = nk_style_item_color(table[NK_COLOR_SLIDER_CURSOR_HOVER]);
  17142. slider->cursor_active = nk_style_item_color(table[NK_COLOR_SLIDER_CURSOR_ACTIVE]);
  17143. slider->inc_symbol = NK_SYMBOL_TRIANGLE_RIGHT;
  17144. slider->dec_symbol = NK_SYMBOL_TRIANGLE_LEFT;
  17145. slider->cursor_size = nk_vec2(16,16);
  17146. slider->padding = nk_vec2(2,2);
  17147. slider->spacing = nk_vec2(2,2);
  17148. slider->userdata = nk_handle_ptr(0);
  17149. slider->show_buttons = nk_false;
  17150. slider->bar_height = 8;
  17151. slider->rounding = 0;
  17152. slider->color_factor = 1.0f;
  17153. slider->disabled_factor = NK_WIDGET_DISABLED_FACTOR;
  17154. slider->draw_begin = 0;
  17155. slider->draw_end = 0;
  17156. /* slider buttons */
  17157. button = &style->slider.inc_button;
  17158. button->normal = nk_style_item_color(nk_rgb(40,40,40));
  17159. button->hover = nk_style_item_color(nk_rgb(42,42,42));
  17160. button->active = nk_style_item_color(nk_rgb(44,44,44));
  17161. button->border_color = nk_rgb(65,65,65);
  17162. button->text_background = nk_rgb(40,40,40);
  17163. button->text_normal = nk_rgb(175,175,175);
  17164. button->text_hover = nk_rgb(175,175,175);
  17165. button->text_active = nk_rgb(175,175,175);
  17166. button->padding = nk_vec2(8.0f,8.0f);
  17167. button->touch_padding = nk_vec2(0.0f,0.0f);
  17168. button->userdata = nk_handle_ptr(0);
  17169. button->text_alignment = NK_TEXT_CENTERED;
  17170. button->border = 1.0f;
  17171. button->rounding = 0.0f;
  17172. button->color_factor_text = 1.0f;
  17173. button->color_factor_background = 1.0f;
  17174. button->disabled_factor = NK_WIDGET_DISABLED_FACTOR;
  17175. button->draw_begin = 0;
  17176. button->draw_end = 0;
  17177. style->slider.dec_button = style->slider.inc_button;
  17178. /* knob */
  17179. knob = &style->knob;
  17180. nk_zero_struct(*knob);
  17181. knob->normal = nk_style_item_hide();
  17182. knob->hover = nk_style_item_hide();
  17183. knob->active = nk_style_item_hide();
  17184. knob->knob_normal = table[NK_COLOR_KNOB];
  17185. knob->knob_hover = table[NK_COLOR_KNOB];
  17186. knob->knob_active = table[NK_COLOR_KNOB];
  17187. knob->cursor_normal = table[NK_COLOR_KNOB_CURSOR];
  17188. knob->cursor_hover = table[NK_COLOR_KNOB_CURSOR_HOVER];
  17189. knob->cursor_active = table[NK_COLOR_KNOB_CURSOR_ACTIVE];
  17190. knob->knob_border_color = table[NK_COLOR_BORDER];
  17191. knob->knob_border = 1.0f;
  17192. knob->padding = nk_vec2(2,2);
  17193. knob->spacing = nk_vec2(2,2);
  17194. knob->cursor_width = 2;
  17195. knob->color_factor = 1.0f;
  17196. knob->disabled_factor = NK_WIDGET_DISABLED_FACTOR;
  17197. knob->userdata = nk_handle_ptr(0);
  17198. knob->draw_begin = 0;
  17199. knob->draw_end = 0;
  17200. /* progressbar */
  17201. prog = &style->progress;
  17202. nk_zero_struct(*prog);
  17203. prog->normal = nk_style_item_color(table[NK_COLOR_SLIDER]);
  17204. prog->hover = nk_style_item_color(table[NK_COLOR_SLIDER]);
  17205. prog->active = nk_style_item_color(table[NK_COLOR_SLIDER]);
  17206. prog->cursor_normal = nk_style_item_color(table[NK_COLOR_SLIDER_CURSOR]);
  17207. prog->cursor_hover = nk_style_item_color(table[NK_COLOR_SLIDER_CURSOR_HOVER]);
  17208. prog->cursor_active = nk_style_item_color(table[NK_COLOR_SLIDER_CURSOR_ACTIVE]);
  17209. prog->border_color = nk_rgba(0,0,0,0);
  17210. prog->cursor_border_color = nk_rgba(0,0,0,0);
  17211. prog->userdata = nk_handle_ptr(0);
  17212. prog->padding = nk_vec2(4,4);
  17213. prog->rounding = 0;
  17214. prog->border = 0;
  17215. prog->cursor_rounding = 0;
  17216. prog->cursor_border = 0;
  17217. prog->color_factor = 1.0f;
  17218. prog->disabled_factor = NK_WIDGET_DISABLED_FACTOR;
  17219. prog->draw_begin = 0;
  17220. prog->draw_end = 0;
  17221. /* scrollbars */
  17222. scroll = &style->scrollh;
  17223. nk_zero_struct(*scroll);
  17224. scroll->normal = nk_style_item_color(table[NK_COLOR_SCROLLBAR]);
  17225. scroll->hover = nk_style_item_color(table[NK_COLOR_SCROLLBAR]);
  17226. scroll->active = nk_style_item_color(table[NK_COLOR_SCROLLBAR]);
  17227. scroll->cursor_normal = nk_style_item_color(table[NK_COLOR_SCROLLBAR_CURSOR]);
  17228. scroll->cursor_hover = nk_style_item_color(table[NK_COLOR_SCROLLBAR_CURSOR_HOVER]);
  17229. scroll->cursor_active = nk_style_item_color(table[NK_COLOR_SCROLLBAR_CURSOR_ACTIVE]);
  17230. scroll->dec_symbol = NK_SYMBOL_CIRCLE_SOLID;
  17231. scroll->inc_symbol = NK_SYMBOL_CIRCLE_SOLID;
  17232. scroll->userdata = nk_handle_ptr(0);
  17233. scroll->border_color = table[NK_COLOR_SCROLLBAR];
  17234. scroll->cursor_border_color = table[NK_COLOR_SCROLLBAR];
  17235. scroll->padding = nk_vec2(0,0);
  17236. scroll->show_buttons = nk_false;
  17237. scroll->border = 0;
  17238. scroll->rounding = 0;
  17239. scroll->border_cursor = 0;
  17240. scroll->rounding_cursor = 0;
  17241. scroll->color_factor = 1.0f;
  17242. scroll->disabled_factor = NK_WIDGET_DISABLED_FACTOR;
  17243. scroll->draw_begin = 0;
  17244. scroll->draw_end = 0;
  17245. style->scrollv = style->scrollh;
  17246. /* scrollbars buttons */
  17247. button = &style->scrollh.inc_button;
  17248. button->normal = nk_style_item_color(nk_rgb(40,40,40));
  17249. button->hover = nk_style_item_color(nk_rgb(42,42,42));
  17250. button->active = nk_style_item_color(nk_rgb(44,44,44));
  17251. button->border_color = nk_rgb(65,65,65);
  17252. button->text_background = nk_rgb(40,40,40);
  17253. button->text_normal = nk_rgb(175,175,175);
  17254. button->text_hover = nk_rgb(175,175,175);
  17255. button->text_active = nk_rgb(175,175,175);
  17256. button->padding = nk_vec2(4.0f,4.0f);
  17257. button->touch_padding = nk_vec2(0.0f,0.0f);
  17258. button->userdata = nk_handle_ptr(0);
  17259. button->text_alignment = NK_TEXT_CENTERED;
  17260. button->border = 1.0f;
  17261. button->rounding = 0.0f;
  17262. button->color_factor_text = 1.0f;
  17263. button->color_factor_background = 1.0f;
  17264. button->disabled_factor = NK_WIDGET_DISABLED_FACTOR;
  17265. button->draw_begin = 0;
  17266. button->draw_end = 0;
  17267. style->scrollh.dec_button = style->scrollh.inc_button;
  17268. style->scrollv.inc_button = style->scrollh.inc_button;
  17269. style->scrollv.dec_button = style->scrollh.inc_button;
  17270. /* edit */
  17271. edit = &style->edit;
  17272. nk_zero_struct(*edit);
  17273. edit->normal = nk_style_item_color(table[NK_COLOR_EDIT]);
  17274. edit->hover = nk_style_item_color(table[NK_COLOR_EDIT]);
  17275. edit->active = nk_style_item_color(table[NK_COLOR_EDIT]);
  17276. edit->cursor_normal = table[NK_COLOR_TEXT];
  17277. edit->cursor_hover = table[NK_COLOR_TEXT];
  17278. edit->cursor_text_normal= table[NK_COLOR_EDIT];
  17279. edit->cursor_text_hover = table[NK_COLOR_EDIT];
  17280. edit->border_color = table[NK_COLOR_BORDER];
  17281. edit->text_normal = table[NK_COLOR_TEXT];
  17282. edit->text_hover = table[NK_COLOR_TEXT];
  17283. edit->text_active = table[NK_COLOR_TEXT];
  17284. edit->selected_normal = table[NK_COLOR_TEXT];
  17285. edit->selected_hover = table[NK_COLOR_TEXT];
  17286. edit->selected_text_normal = table[NK_COLOR_EDIT];
  17287. edit->selected_text_hover = table[NK_COLOR_EDIT];
  17288. edit->scrollbar_size = nk_vec2(10,10);
  17289. edit->scrollbar = style->scrollv;
  17290. edit->padding = nk_vec2(4,4);
  17291. edit->row_padding = 2;
  17292. edit->cursor_size = 4;
  17293. edit->border = 1;
  17294. edit->rounding = 0;
  17295. edit->color_factor = 1.0f;
  17296. edit->disabled_factor = NK_WIDGET_DISABLED_FACTOR;
  17297. /* property */
  17298. property = &style->property;
  17299. nk_zero_struct(*property);
  17300. property->normal = nk_style_item_color(table[NK_COLOR_PROPERTY]);
  17301. property->hover = nk_style_item_color(table[NK_COLOR_PROPERTY]);
  17302. property->active = nk_style_item_color(table[NK_COLOR_PROPERTY]);
  17303. property->border_color = table[NK_COLOR_BORDER];
  17304. property->label_normal = table[NK_COLOR_TEXT];
  17305. property->label_hover = table[NK_COLOR_TEXT];
  17306. property->label_active = table[NK_COLOR_TEXT];
  17307. property->sym_left = NK_SYMBOL_TRIANGLE_LEFT;
  17308. property->sym_right = NK_SYMBOL_TRIANGLE_RIGHT;
  17309. property->userdata = nk_handle_ptr(0);
  17310. property->padding = nk_vec2(4,4);
  17311. property->border = 1;
  17312. property->rounding = 10;
  17313. property->draw_begin = 0;
  17314. property->draw_end = 0;
  17315. property->color_factor = 1.0f;
  17316. property->disabled_factor = NK_WIDGET_DISABLED_FACTOR;
  17317. /* property buttons */
  17318. button = &style->property.dec_button;
  17319. nk_zero_struct(*button);
  17320. button->normal = nk_style_item_color(table[NK_COLOR_PROPERTY]);
  17321. button->hover = nk_style_item_color(table[NK_COLOR_PROPERTY]);
  17322. button->active = nk_style_item_color(table[NK_COLOR_PROPERTY]);
  17323. button->border_color = nk_rgba(0,0,0,0);
  17324. button->text_background = table[NK_COLOR_PROPERTY];
  17325. button->text_normal = table[NK_COLOR_TEXT];
  17326. button->text_hover = table[NK_COLOR_TEXT];
  17327. button->text_active = table[NK_COLOR_TEXT];
  17328. button->padding = nk_vec2(0.0f,0.0f);
  17329. button->touch_padding = nk_vec2(0.0f,0.0f);
  17330. button->userdata = nk_handle_ptr(0);
  17331. button->text_alignment = NK_TEXT_CENTERED;
  17332. button->border = 0.0f;
  17333. button->rounding = 0.0f;
  17334. button->color_factor_text = 1.0f;
  17335. button->color_factor_background = 1.0f;
  17336. button->disabled_factor = NK_WIDGET_DISABLED_FACTOR;
  17337. button->draw_begin = 0;
  17338. button->draw_end = 0;
  17339. style->property.inc_button = style->property.dec_button;
  17340. /* property edit */
  17341. edit = &style->property.edit;
  17342. nk_zero_struct(*edit);
  17343. edit->normal = nk_style_item_color(table[NK_COLOR_PROPERTY]);
  17344. edit->hover = nk_style_item_color(table[NK_COLOR_PROPERTY]);
  17345. edit->active = nk_style_item_color(table[NK_COLOR_PROPERTY]);
  17346. edit->border_color = nk_rgba(0,0,0,0);
  17347. edit->cursor_normal = table[NK_COLOR_TEXT];
  17348. edit->cursor_hover = table[NK_COLOR_TEXT];
  17349. edit->cursor_text_normal= table[NK_COLOR_EDIT];
  17350. edit->cursor_text_hover = table[NK_COLOR_EDIT];
  17351. edit->text_normal = table[NK_COLOR_TEXT];
  17352. edit->text_hover = table[NK_COLOR_TEXT];
  17353. edit->text_active = table[NK_COLOR_TEXT];
  17354. edit->selected_normal = table[NK_COLOR_TEXT];
  17355. edit->selected_hover = table[NK_COLOR_TEXT];
  17356. edit->selected_text_normal = table[NK_COLOR_EDIT];
  17357. edit->selected_text_hover = table[NK_COLOR_EDIT];
  17358. edit->padding = nk_vec2(0,0);
  17359. edit->cursor_size = 8;
  17360. edit->border = 0;
  17361. edit->rounding = 0;
  17362. edit->color_factor = 1.0f;
  17363. edit->disabled_factor = NK_WIDGET_DISABLED_FACTOR;
  17364. /* chart */
  17365. chart = &style->chart;
  17366. nk_zero_struct(*chart);
  17367. chart->background = nk_style_item_color(table[NK_COLOR_CHART]);
  17368. chart->border_color = table[NK_COLOR_BORDER];
  17369. chart->selected_color = table[NK_COLOR_CHART_COLOR_HIGHLIGHT];
  17370. chart->color = table[NK_COLOR_CHART_COLOR];
  17371. chart->padding = nk_vec2(4,4);
  17372. chart->border = 0;
  17373. chart->rounding = 0;
  17374. chart->color_factor = 1.0f;
  17375. chart->disabled_factor = NK_WIDGET_DISABLED_FACTOR;
  17376. chart->show_markers = nk_true;
  17377. /* combo */
  17378. combo = &style->combo;
  17379. combo->normal = nk_style_item_color(table[NK_COLOR_COMBO]);
  17380. combo->hover = nk_style_item_color(table[NK_COLOR_COMBO]);
  17381. combo->active = nk_style_item_color(table[NK_COLOR_COMBO]);
  17382. combo->border_color = table[NK_COLOR_BORDER];
  17383. combo->label_normal = table[NK_COLOR_TEXT];
  17384. combo->label_hover = table[NK_COLOR_TEXT];
  17385. combo->label_active = table[NK_COLOR_TEXT];
  17386. combo->sym_normal = NK_SYMBOL_TRIANGLE_DOWN;
  17387. combo->sym_hover = NK_SYMBOL_TRIANGLE_DOWN;
  17388. combo->sym_active = NK_SYMBOL_TRIANGLE_DOWN;
  17389. combo->content_padding = nk_vec2(4,4);
  17390. combo->button_padding = nk_vec2(0,4);
  17391. combo->spacing = nk_vec2(4,0);
  17392. combo->border = 1;
  17393. combo->rounding = 0;
  17394. combo->color_factor = 1.0f;
  17395. combo->disabled_factor = NK_WIDGET_DISABLED_FACTOR;
  17396. /* combo button */
  17397. button = &style->combo.button;
  17398. nk_zero_struct(*button);
  17399. button->normal = nk_style_item_color(table[NK_COLOR_COMBO]);
  17400. button->hover = nk_style_item_color(table[NK_COLOR_COMBO]);
  17401. button->active = nk_style_item_color(table[NK_COLOR_COMBO]);
  17402. button->border_color = nk_rgba(0,0,0,0);
  17403. button->text_background = table[NK_COLOR_COMBO];
  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(2.0f,2.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->color_factor_text = 1.0f;
  17414. button->color_factor_background = 1.0f;
  17415. button->disabled_factor = NK_WIDGET_DISABLED_FACTOR;
  17416. button->draw_begin = 0;
  17417. button->draw_end = 0;
  17418. /* tab */
  17419. tab = &style->tab;
  17420. tab->background = nk_style_item_color(table[NK_COLOR_TAB_HEADER]);
  17421. tab->border_color = table[NK_COLOR_BORDER];
  17422. tab->text = table[NK_COLOR_TEXT];
  17423. tab->sym_minimize = NK_SYMBOL_TRIANGLE_RIGHT;
  17424. tab->sym_maximize = NK_SYMBOL_TRIANGLE_DOWN;
  17425. tab->padding = nk_vec2(4,4);
  17426. tab->spacing = nk_vec2(4,4);
  17427. tab->indent = 10.0f;
  17428. tab->border = 1;
  17429. tab->rounding = 0;
  17430. tab->color_factor = 1.0f;
  17431. tab->disabled_factor = NK_WIDGET_DISABLED_FACTOR;
  17432. /* tab button */
  17433. button = &style->tab.tab_minimize_button;
  17434. nk_zero_struct(*button);
  17435. button->normal = nk_style_item_color(table[NK_COLOR_TAB_HEADER]);
  17436. button->hover = nk_style_item_color(table[NK_COLOR_TAB_HEADER]);
  17437. button->active = nk_style_item_color(table[NK_COLOR_TAB_HEADER]);
  17438. button->border_color = nk_rgba(0,0,0,0);
  17439. button->text_background = table[NK_COLOR_TAB_HEADER];
  17440. button->text_normal = table[NK_COLOR_TEXT];
  17441. button->text_hover = table[NK_COLOR_TEXT];
  17442. button->text_active = table[NK_COLOR_TEXT];
  17443. button->padding = nk_vec2(2.0f,2.0f);
  17444. button->touch_padding = nk_vec2(0.0f,0.0f);
  17445. button->userdata = nk_handle_ptr(0);
  17446. button->text_alignment = NK_TEXT_CENTERED;
  17447. button->border = 0.0f;
  17448. button->rounding = 0.0f;
  17449. button->color_factor_text = 1.0f;
  17450. button->color_factor_background = 1.0f;
  17451. button->disabled_factor = NK_WIDGET_DISABLED_FACTOR;
  17452. button->draw_begin = 0;
  17453. button->draw_end = 0;
  17454. style->tab.tab_maximize_button =*button;
  17455. /* node button */
  17456. button = &style->tab.node_minimize_button;
  17457. nk_zero_struct(*button);
  17458. button->normal = nk_style_item_color(table[NK_COLOR_WINDOW]);
  17459. button->hover = nk_style_item_color(table[NK_COLOR_WINDOW]);
  17460. button->active = nk_style_item_color(table[NK_COLOR_WINDOW]);
  17461. button->border_color = nk_rgba(0,0,0,0);
  17462. button->text_background = table[NK_COLOR_TAB_HEADER];
  17463. button->text_normal = table[NK_COLOR_TEXT];
  17464. button->text_hover = table[NK_COLOR_TEXT];
  17465. button->text_active = table[NK_COLOR_TEXT];
  17466. button->padding = nk_vec2(2.0f,2.0f);
  17467. button->touch_padding = nk_vec2(0.0f,0.0f);
  17468. button->userdata = nk_handle_ptr(0);
  17469. button->text_alignment = NK_TEXT_CENTERED;
  17470. button->border = 0.0f;
  17471. button->rounding = 0.0f;
  17472. button->color_factor_text = 1.0f;
  17473. button->color_factor_background = 1.0f;
  17474. button->disabled_factor = NK_WIDGET_DISABLED_FACTOR;
  17475. button->draw_begin = 0;
  17476. button->draw_end = 0;
  17477. style->tab.node_maximize_button =*button;
  17478. /* window header */
  17479. win = &style->window;
  17480. win->header.align = NK_HEADER_RIGHT;
  17481. win->header.close_symbol = NK_SYMBOL_X;
  17482. win->header.minimize_symbol = NK_SYMBOL_MINUS;
  17483. win->header.maximize_symbol = NK_SYMBOL_PLUS;
  17484. win->header.normal = nk_style_item_color(table[NK_COLOR_HEADER]);
  17485. win->header.hover = nk_style_item_color(table[NK_COLOR_HEADER]);
  17486. win->header.active = nk_style_item_color(table[NK_COLOR_HEADER]);
  17487. win->header.label_normal = table[NK_COLOR_TEXT];
  17488. win->header.label_hover = table[NK_COLOR_TEXT];
  17489. win->header.label_active = table[NK_COLOR_TEXT];
  17490. win->header.label_padding = nk_vec2(4,4);
  17491. win->header.padding = nk_vec2(4,4);
  17492. win->header.spacing = nk_vec2(0,0);
  17493. /* window header close button */
  17494. button = &style->window.header.close_button;
  17495. nk_zero_struct(*button);
  17496. button->normal = nk_style_item_color(table[NK_COLOR_HEADER]);
  17497. button->hover = nk_style_item_color(table[NK_COLOR_HEADER]);
  17498. button->active = nk_style_item_color(table[NK_COLOR_HEADER]);
  17499. button->border_color = nk_rgba(0,0,0,0);
  17500. button->text_background = table[NK_COLOR_HEADER];
  17501. button->text_normal = table[NK_COLOR_TEXT];
  17502. button->text_hover = table[NK_COLOR_TEXT];
  17503. button->text_active = table[NK_COLOR_TEXT];
  17504. button->padding = nk_vec2(0.0f,0.0f);
  17505. button->touch_padding = nk_vec2(0.0f,0.0f);
  17506. button->userdata = nk_handle_ptr(0);
  17507. button->text_alignment = NK_TEXT_CENTERED;
  17508. button->border = 0.0f;
  17509. button->rounding = 0.0f;
  17510. button->color_factor_text = 1.0f;
  17511. button->color_factor_background = 1.0f;
  17512. button->disabled_factor = NK_WIDGET_DISABLED_FACTOR;
  17513. button->draw_begin = 0;
  17514. button->draw_end = 0;
  17515. /* window header minimize button */
  17516. button = &style->window.header.minimize_button;
  17517. nk_zero_struct(*button);
  17518. button->normal = nk_style_item_color(table[NK_COLOR_HEADER]);
  17519. button->hover = nk_style_item_color(table[NK_COLOR_HEADER]);
  17520. button->active = nk_style_item_color(table[NK_COLOR_HEADER]);
  17521. button->border_color = nk_rgba(0,0,0,0);
  17522. button->text_background = table[NK_COLOR_HEADER];
  17523. button->text_normal = table[NK_COLOR_TEXT];
  17524. button->text_hover = table[NK_COLOR_TEXT];
  17525. button->text_active = table[NK_COLOR_TEXT];
  17526. button->padding = nk_vec2(0.0f,0.0f);
  17527. button->touch_padding = nk_vec2(0.0f,0.0f);
  17528. button->userdata = nk_handle_ptr(0);
  17529. button->text_alignment = NK_TEXT_CENTERED;
  17530. button->border = 0.0f;
  17531. button->rounding = 0.0f;
  17532. button->color_factor_text = 1.0f;
  17533. button->color_factor_background = 1.0f;
  17534. button->disabled_factor = NK_WIDGET_DISABLED_FACTOR;
  17535. button->draw_begin = 0;
  17536. button->draw_end = 0;
  17537. /* window */
  17538. win->background = table[NK_COLOR_WINDOW];
  17539. win->fixed_background = nk_style_item_color(table[NK_COLOR_WINDOW]);
  17540. win->border_color = table[NK_COLOR_BORDER];
  17541. win->popup_border_color = table[NK_COLOR_BORDER];
  17542. win->combo_border_color = table[NK_COLOR_BORDER];
  17543. win->contextual_border_color = table[NK_COLOR_BORDER];
  17544. win->menu_border_color = table[NK_COLOR_BORDER];
  17545. win->group_border_color = table[NK_COLOR_BORDER];
  17546. win->tooltip_border_color = table[NK_COLOR_BORDER];
  17547. win->scaler = nk_style_item_color(table[NK_COLOR_TEXT]);
  17548. win->rounding = 0.0f;
  17549. win->spacing = nk_vec2(4,4);
  17550. win->scrollbar_size = nk_vec2(10,10);
  17551. win->min_size = nk_vec2(64,64);
  17552. win->combo_border = 1.0f;
  17553. win->contextual_border = 1.0f;
  17554. win->menu_border = 1.0f;
  17555. win->group_border = 1.0f;
  17556. win->tooltip_border = 1.0f;
  17557. win->popup_border = 1.0f;
  17558. win->border = 2.0f;
  17559. win->min_row_height_padding = 8;
  17560. win->padding = nk_vec2(4,4);
  17561. win->group_padding = nk_vec2(4,4);
  17562. win->popup_padding = nk_vec2(4,4);
  17563. win->combo_padding = nk_vec2(4,4);
  17564. win->contextual_padding = nk_vec2(4,4);
  17565. win->menu_padding = nk_vec2(4,4);
  17566. win->tooltip_padding = nk_vec2(4,4);
  17567. }
  17568. NK_API void
  17569. nk_style_set_font(struct nk_context *ctx, const struct nk_user_font *font)
  17570. {
  17571. struct nk_style *style;
  17572. NK_ASSERT(ctx);
  17573. if (!ctx) return;
  17574. style = &ctx->style;
  17575. style->font = font;
  17576. ctx->stacks.fonts.head = 0;
  17577. if (ctx->current)
  17578. nk_layout_reset_min_row_height(ctx);
  17579. }
  17580. NK_API nk_bool
  17581. nk_style_push_font(struct nk_context *ctx, const struct nk_user_font *font)
  17582. {
  17583. struct nk_config_stack_user_font *font_stack;
  17584. struct nk_config_stack_user_font_element *element;
  17585. NK_ASSERT(ctx);
  17586. if (!ctx) return 0;
  17587. font_stack = &ctx->stacks.fonts;
  17588. NK_ASSERT(font_stack->head < (int)NK_LEN(font_stack->elements));
  17589. if (font_stack->head >= (int)NK_LEN(font_stack->elements))
  17590. return 0;
  17591. element = &font_stack->elements[font_stack->head++];
  17592. element->address = &ctx->style.font;
  17593. element->old_value = ctx->style.font;
  17594. ctx->style.font = font;
  17595. return 1;
  17596. }
  17597. NK_API nk_bool
  17598. nk_style_pop_font(struct nk_context *ctx)
  17599. {
  17600. struct nk_config_stack_user_font *font_stack;
  17601. struct nk_config_stack_user_font_element *element;
  17602. NK_ASSERT(ctx);
  17603. if (!ctx) return 0;
  17604. font_stack = &ctx->stacks.fonts;
  17605. NK_ASSERT(font_stack->head > 0);
  17606. if (font_stack->head < 1)
  17607. return 0;
  17608. element = &font_stack->elements[--font_stack->head];
  17609. *element->address = element->old_value;
  17610. return 1;
  17611. }
  17612. #define NK_STYLE_PUSH_IMPLEMENATION(prefix, type, stack) \
  17613. nk_style_push_##type(struct nk_context *ctx, prefix##_##type *address, prefix##_##type value)\
  17614. {\
  17615. struct nk_config_stack_##type * type_stack;\
  17616. struct nk_config_stack_##type##_element *element;\
  17617. NK_ASSERT(ctx);\
  17618. if (!ctx) return 0;\
  17619. type_stack = &ctx->stacks.stack;\
  17620. NK_ASSERT(type_stack->head < (int)NK_LEN(type_stack->elements));\
  17621. if (type_stack->head >= (int)NK_LEN(type_stack->elements))\
  17622. return 0;\
  17623. element = &type_stack->elements[type_stack->head++];\
  17624. element->address = address;\
  17625. element->old_value = *address;\
  17626. *address = value;\
  17627. return 1;\
  17628. }
  17629. #define NK_STYLE_POP_IMPLEMENATION(type, stack) \
  17630. nk_style_pop_##type(struct nk_context *ctx)\
  17631. {\
  17632. struct nk_config_stack_##type *type_stack;\
  17633. struct nk_config_stack_##type##_element *element;\
  17634. NK_ASSERT(ctx);\
  17635. if (!ctx) return 0;\
  17636. type_stack = &ctx->stacks.stack;\
  17637. NK_ASSERT(type_stack->head > 0);\
  17638. if (type_stack->head < 1)\
  17639. return 0;\
  17640. element = &type_stack->elements[--type_stack->head];\
  17641. *element->address = element->old_value;\
  17642. return 1;\
  17643. }
  17644. NK_API nk_bool NK_STYLE_PUSH_IMPLEMENATION(struct nk, style_item, style_items)
  17645. NK_API nk_bool NK_STYLE_PUSH_IMPLEMENATION(nk,float, floats)
  17646. NK_API nk_bool NK_STYLE_PUSH_IMPLEMENATION(struct nk, vec2, vectors)
  17647. NK_API nk_bool NK_STYLE_PUSH_IMPLEMENATION(nk,flags, flags)
  17648. NK_API nk_bool NK_STYLE_PUSH_IMPLEMENATION(struct nk,color, colors)
  17649. NK_API nk_bool NK_STYLE_POP_IMPLEMENATION(style_item, style_items)
  17650. NK_API nk_bool NK_STYLE_POP_IMPLEMENATION(float,floats)
  17651. NK_API nk_bool NK_STYLE_POP_IMPLEMENATION(vec2, vectors)
  17652. NK_API nk_bool NK_STYLE_POP_IMPLEMENATION(flags,flags)
  17653. NK_API nk_bool NK_STYLE_POP_IMPLEMENATION(color,colors)
  17654. NK_API nk_bool
  17655. nk_style_set_cursor(struct nk_context *ctx, enum nk_style_cursor c)
  17656. {
  17657. struct nk_style *style;
  17658. NK_ASSERT(ctx);
  17659. if (!ctx) return 0;
  17660. style = &ctx->style;
  17661. if (style->cursors[c]) {
  17662. style->cursor_active = style->cursors[c];
  17663. return 1;
  17664. }
  17665. return 0;
  17666. }
  17667. NK_API void
  17668. nk_style_show_cursor(struct nk_context *ctx)
  17669. {
  17670. ctx->style.cursor_visible = nk_true;
  17671. }
  17672. NK_API void
  17673. nk_style_hide_cursor(struct nk_context *ctx)
  17674. {
  17675. ctx->style.cursor_visible = nk_false;
  17676. }
  17677. NK_API void
  17678. nk_style_load_cursor(struct nk_context *ctx, enum nk_style_cursor cursor,
  17679. const struct nk_cursor *c)
  17680. {
  17681. struct nk_style *style;
  17682. NK_ASSERT(ctx);
  17683. if (!ctx) return;
  17684. style = &ctx->style;
  17685. style->cursors[cursor] = c;
  17686. }
  17687. NK_API void
  17688. nk_style_load_all_cursors(struct nk_context *ctx, const struct nk_cursor *cursors)
  17689. {
  17690. int i = 0;
  17691. struct nk_style *style;
  17692. NK_ASSERT(ctx);
  17693. if (!ctx) return;
  17694. style = &ctx->style;
  17695. for (i = 0; i < NK_CURSOR_COUNT; ++i)
  17696. style->cursors[i] = &cursors[i];
  17697. style->cursor_visible = nk_true;
  17698. }
  17699. /* ==============================================================
  17700. *
  17701. * CONTEXT
  17702. *
  17703. * ===============================================================*/
  17704. NK_INTERN void
  17705. nk_setup(struct nk_context *ctx, const struct nk_user_font *font)
  17706. {
  17707. NK_ASSERT(ctx);
  17708. if (!ctx) return;
  17709. nk_zero_struct(*ctx);
  17710. nk_style_default(ctx);
  17711. ctx->seq = 1;
  17712. if (font) ctx->style.font = font;
  17713. #ifdef NK_INCLUDE_VERTEX_BUFFER_OUTPUT
  17714. nk_draw_list_init(&ctx->draw_list);
  17715. #endif
  17716. }
  17717. #ifdef NK_INCLUDE_DEFAULT_ALLOCATOR
  17718. NK_API nk_bool
  17719. nk_init_default(struct nk_context *ctx, const struct nk_user_font *font)
  17720. {
  17721. struct nk_allocator alloc;
  17722. alloc.userdata.ptr = 0;
  17723. alloc.alloc = nk_malloc;
  17724. alloc.free = nk_mfree;
  17725. return nk_init(ctx, &alloc, font);
  17726. }
  17727. #endif
  17728. NK_API nk_bool
  17729. nk_init_fixed(struct nk_context *ctx, void *memory, nk_size size,
  17730. const struct nk_user_font *font)
  17731. {
  17732. NK_ASSERT(memory);
  17733. if (!memory) return 0;
  17734. nk_setup(ctx, font);
  17735. nk_buffer_init_fixed(&ctx->memory, memory, size);
  17736. ctx->use_pool = nk_false;
  17737. return 1;
  17738. }
  17739. NK_API nk_bool
  17740. nk_init_custom(struct nk_context *ctx, struct nk_buffer *cmds,
  17741. struct nk_buffer *pool, const struct nk_user_font *font)
  17742. {
  17743. NK_ASSERT(cmds);
  17744. NK_ASSERT(pool);
  17745. if (!cmds || !pool) return 0;
  17746. nk_setup(ctx, font);
  17747. ctx->memory = *cmds;
  17748. if (pool->type == NK_BUFFER_FIXED) {
  17749. /* take memory from buffer and alloc fixed pool */
  17750. nk_pool_init_fixed(&ctx->pool, pool->memory.ptr, pool->memory.size);
  17751. } else {
  17752. /* create dynamic pool from buffer allocator */
  17753. struct nk_allocator *alloc = &pool->pool;
  17754. nk_pool_init(&ctx->pool, alloc, NK_POOL_DEFAULT_CAPACITY);
  17755. }
  17756. ctx->use_pool = nk_true;
  17757. return 1;
  17758. }
  17759. NK_API nk_bool
  17760. nk_init(struct nk_context *ctx, const struct nk_allocator *alloc,
  17761. const struct nk_user_font *font)
  17762. {
  17763. NK_ASSERT(alloc);
  17764. if (!alloc) return 0;
  17765. nk_setup(ctx, font);
  17766. nk_buffer_init(&ctx->memory, alloc, NK_DEFAULT_COMMAND_BUFFER_SIZE);
  17767. nk_pool_init(&ctx->pool, alloc, NK_POOL_DEFAULT_CAPACITY);
  17768. ctx->use_pool = nk_true;
  17769. return 1;
  17770. }
  17771. #ifdef NK_INCLUDE_COMMAND_USERDATA
  17772. NK_API void
  17773. nk_set_user_data(struct nk_context *ctx, nk_handle handle)
  17774. {
  17775. if (!ctx) return;
  17776. ctx->userdata = handle;
  17777. if (ctx->current)
  17778. ctx->current->buffer.userdata = handle;
  17779. }
  17780. #endif
  17781. NK_API void
  17782. nk_free(struct nk_context *ctx)
  17783. {
  17784. NK_ASSERT(ctx);
  17785. if (!ctx) return;
  17786. nk_buffer_free(&ctx->memory);
  17787. if (ctx->use_pool)
  17788. nk_pool_free(&ctx->pool);
  17789. nk_zero(&ctx->input, sizeof(ctx->input));
  17790. nk_zero(&ctx->style, sizeof(ctx->style));
  17791. nk_zero(&ctx->memory, sizeof(ctx->memory));
  17792. ctx->seq = 0;
  17793. ctx->build = 0;
  17794. ctx->begin = 0;
  17795. ctx->end = 0;
  17796. ctx->active = 0;
  17797. ctx->current = 0;
  17798. ctx->freelist = 0;
  17799. ctx->count = 0;
  17800. }
  17801. NK_API void
  17802. nk_clear(struct nk_context *ctx)
  17803. {
  17804. struct nk_window *iter;
  17805. struct nk_window *next;
  17806. NK_ASSERT(ctx);
  17807. if (!ctx) return;
  17808. if (ctx->use_pool)
  17809. nk_buffer_clear(&ctx->memory);
  17810. else nk_buffer_reset(&ctx->memory, NK_BUFFER_FRONT);
  17811. ctx->build = 0;
  17812. ctx->memory.calls = 0;
  17813. ctx->last_widget_state = 0;
  17814. ctx->style.cursor_active = ctx->style.cursors[NK_CURSOR_ARROW];
  17815. NK_MEMSET(&ctx->overlay, 0, sizeof(ctx->overlay));
  17816. /* garbage collector */
  17817. iter = ctx->begin;
  17818. while (iter) {
  17819. /* make sure valid minimized windows do not get removed */
  17820. if ((iter->flags & NK_WINDOW_MINIMIZED) &&
  17821. !(iter->flags & NK_WINDOW_CLOSED) &&
  17822. iter->seq == ctx->seq) {
  17823. iter = iter->next;
  17824. continue;
  17825. }
  17826. /* remove hotness from hidden or closed windows*/
  17827. if (((iter->flags & NK_WINDOW_HIDDEN) ||
  17828. (iter->flags & NK_WINDOW_CLOSED)) &&
  17829. iter == ctx->active) {
  17830. ctx->active = iter->prev;
  17831. ctx->end = iter->prev;
  17832. if (!ctx->end)
  17833. ctx->begin = 0;
  17834. if (ctx->active)
  17835. ctx->active->flags &= ~(unsigned)NK_WINDOW_ROM;
  17836. }
  17837. /* free unused popup windows */
  17838. if (iter->popup.win && iter->popup.win->seq != ctx->seq) {
  17839. nk_free_window(ctx, iter->popup.win);
  17840. iter->popup.win = 0;
  17841. }
  17842. /* remove unused window state tables */
  17843. {struct nk_table *n, *it = iter->tables;
  17844. while (it) {
  17845. n = it->next;
  17846. if (it->seq != ctx->seq) {
  17847. nk_remove_table(iter, it);
  17848. nk_zero(it, sizeof(union nk_page_data));
  17849. nk_free_table(ctx, it);
  17850. if (it == iter->tables)
  17851. iter->tables = n;
  17852. } it = n;
  17853. }}
  17854. /* window itself is not used anymore so free */
  17855. if (iter->seq != ctx->seq || iter->flags & NK_WINDOW_CLOSED) {
  17856. next = iter->next;
  17857. nk_remove_window(ctx, iter);
  17858. nk_free_window(ctx, iter);
  17859. iter = next;
  17860. } else iter = iter->next;
  17861. }
  17862. ctx->seq++;
  17863. }
  17864. NK_LIB void
  17865. nk_start_buffer(struct nk_context *ctx, struct nk_command_buffer *buffer)
  17866. {
  17867. NK_ASSERT(ctx);
  17868. NK_ASSERT(buffer);
  17869. if (!ctx || !buffer) return;
  17870. buffer->begin = ctx->memory.allocated;
  17871. buffer->end = buffer->begin;
  17872. buffer->last = buffer->begin;
  17873. buffer->clip = nk_null_rect;
  17874. }
  17875. NK_LIB void
  17876. nk_start(struct nk_context *ctx, struct nk_window *win)
  17877. {
  17878. NK_ASSERT(ctx);
  17879. NK_ASSERT(win);
  17880. nk_start_buffer(ctx, &win->buffer);
  17881. }
  17882. NK_LIB void
  17883. nk_start_popup(struct nk_context *ctx, struct nk_window *win)
  17884. {
  17885. struct nk_popup_buffer *buf;
  17886. NK_ASSERT(ctx);
  17887. NK_ASSERT(win);
  17888. if (!ctx || !win) return;
  17889. /* save buffer fill state for popup */
  17890. buf = &win->popup.buf;
  17891. buf->begin = win->buffer.end;
  17892. buf->end = win->buffer.end;
  17893. buf->parent = win->buffer.last;
  17894. buf->last = buf->begin;
  17895. buf->active = nk_true;
  17896. }
  17897. NK_LIB void
  17898. nk_finish_popup(struct nk_context *ctx, struct nk_window *win)
  17899. {
  17900. struct nk_popup_buffer *buf;
  17901. NK_ASSERT(ctx);
  17902. NK_ASSERT(win);
  17903. if (!ctx || !win) return;
  17904. buf = &win->popup.buf;
  17905. buf->last = win->buffer.last;
  17906. buf->end = win->buffer.end;
  17907. }
  17908. NK_LIB void
  17909. nk_finish_buffer(struct nk_context *ctx, struct nk_command_buffer *buffer)
  17910. {
  17911. NK_ASSERT(ctx);
  17912. NK_ASSERT(buffer);
  17913. if (!ctx || !buffer) return;
  17914. buffer->end = ctx->memory.allocated;
  17915. }
  17916. NK_LIB void
  17917. nk_finish(struct nk_context *ctx, struct nk_window *win)
  17918. {
  17919. struct nk_popup_buffer *buf;
  17920. struct nk_command *parent_last;
  17921. void *memory;
  17922. NK_ASSERT(ctx);
  17923. NK_ASSERT(win);
  17924. if (!ctx || !win) return;
  17925. nk_finish_buffer(ctx, &win->buffer);
  17926. if (!win->popup.buf.active) return;
  17927. buf = &win->popup.buf;
  17928. memory = ctx->memory.memory.ptr;
  17929. parent_last = nk_ptr_add(struct nk_command, memory, buf->parent);
  17930. parent_last->next = buf->end;
  17931. }
  17932. NK_LIB void
  17933. nk_build(struct nk_context *ctx)
  17934. {
  17935. struct nk_window *it = 0;
  17936. struct nk_command *cmd = 0;
  17937. nk_byte *buffer = 0;
  17938. /* draw cursor overlay */
  17939. if (!ctx->style.cursor_active)
  17940. ctx->style.cursor_active = ctx->style.cursors[NK_CURSOR_ARROW];
  17941. if (ctx->style.cursor_active && !ctx->input.mouse.grabbed && ctx->style.cursor_visible) {
  17942. struct nk_rect mouse_bounds;
  17943. const struct nk_cursor *cursor = ctx->style.cursor_active;
  17944. nk_command_buffer_init(&ctx->overlay, &ctx->memory, NK_CLIPPING_OFF);
  17945. nk_start_buffer(ctx, &ctx->overlay);
  17946. mouse_bounds.x = ctx->input.mouse.pos.x - cursor->offset.x;
  17947. mouse_bounds.y = ctx->input.mouse.pos.y - cursor->offset.y;
  17948. mouse_bounds.w = cursor->size.x;
  17949. mouse_bounds.h = cursor->size.y;
  17950. nk_draw_image(&ctx->overlay, mouse_bounds, &cursor->img, nk_white);
  17951. nk_finish_buffer(ctx, &ctx->overlay);
  17952. }
  17953. /* build one big draw command list out of all window buffers */
  17954. it = ctx->begin;
  17955. buffer = (nk_byte*)ctx->memory.memory.ptr;
  17956. while (it != 0) {
  17957. struct nk_window *next = it->next;
  17958. if (it->buffer.last == it->buffer.begin || (it->flags & NK_WINDOW_HIDDEN)||
  17959. it->seq != ctx->seq)
  17960. goto cont;
  17961. cmd = nk_ptr_add(struct nk_command, buffer, it->buffer.last);
  17962. while (next && ((next->buffer.last == next->buffer.begin) ||
  17963. (next->flags & NK_WINDOW_HIDDEN) || next->seq != ctx->seq))
  17964. next = next->next; /* skip empty command buffers */
  17965. if (next) cmd->next = next->buffer.begin;
  17966. cont: it = next;
  17967. }
  17968. /* append all popup draw commands into lists */
  17969. it = ctx->begin;
  17970. while (it != 0) {
  17971. struct nk_window *next = it->next;
  17972. struct nk_popup_buffer *buf;
  17973. if (!it->popup.buf.active)
  17974. goto skip;
  17975. buf = &it->popup.buf;
  17976. cmd->next = buf->begin;
  17977. cmd = nk_ptr_add(struct nk_command, buffer, buf->last);
  17978. buf->active = nk_false;
  17979. skip: it = next;
  17980. }
  17981. if (cmd) {
  17982. /* append overlay commands */
  17983. if (ctx->overlay.end != ctx->overlay.begin)
  17984. cmd->next = ctx->overlay.begin;
  17985. else cmd->next = ctx->memory.allocated;
  17986. }
  17987. }
  17988. NK_API const struct nk_command*
  17989. nk__begin(struct nk_context *ctx)
  17990. {
  17991. struct nk_window *iter;
  17992. nk_byte *buffer;
  17993. NK_ASSERT(ctx);
  17994. if (!ctx) return 0;
  17995. if (!ctx->count) return 0;
  17996. buffer = (nk_byte*)ctx->memory.memory.ptr;
  17997. if (!ctx->build) {
  17998. nk_build(ctx);
  17999. ctx->build = nk_true;
  18000. }
  18001. iter = ctx->begin;
  18002. while (iter && ((iter->buffer.begin == iter->buffer.end) ||
  18003. (iter->flags & NK_WINDOW_HIDDEN) || iter->seq != ctx->seq))
  18004. iter = iter->next;
  18005. if (!iter) return 0;
  18006. return nk_ptr_add_const(struct nk_command, buffer, iter->buffer.begin);
  18007. }
  18008. NK_API const struct nk_command*
  18009. nk__next(struct nk_context *ctx, const struct nk_command *cmd)
  18010. {
  18011. nk_byte *buffer;
  18012. const struct nk_command *next;
  18013. NK_ASSERT(ctx);
  18014. if (!ctx || !cmd || !ctx->count) return 0;
  18015. if (cmd->next >= ctx->memory.allocated) return 0;
  18016. buffer = (nk_byte*)ctx->memory.memory.ptr;
  18017. next = nk_ptr_add_const(struct nk_command, buffer, cmd->next);
  18018. return next;
  18019. }
  18020. /* ===============================================================
  18021. *
  18022. * POOL
  18023. *
  18024. * ===============================================================*/
  18025. NK_LIB void
  18026. nk_pool_init(struct nk_pool *pool, const struct nk_allocator *alloc,
  18027. unsigned int capacity)
  18028. {
  18029. NK_ASSERT(capacity >= 1);
  18030. nk_zero(pool, sizeof(*pool));
  18031. pool->alloc = *alloc;
  18032. pool->capacity = capacity;
  18033. pool->type = NK_BUFFER_DYNAMIC;
  18034. pool->pages = 0;
  18035. }
  18036. NK_LIB void
  18037. nk_pool_free(struct nk_pool *pool)
  18038. {
  18039. struct nk_page *iter;
  18040. if (!pool) return;
  18041. iter = pool->pages;
  18042. if (pool->type == NK_BUFFER_FIXED) return;
  18043. while (iter) {
  18044. struct nk_page *next = iter->next;
  18045. pool->alloc.free(pool->alloc.userdata, iter);
  18046. iter = next;
  18047. }
  18048. }
  18049. NK_LIB void
  18050. nk_pool_init_fixed(struct nk_pool *pool, void *memory, nk_size size)
  18051. {
  18052. nk_zero(pool, sizeof(*pool));
  18053. NK_ASSERT(size >= sizeof(struct nk_page));
  18054. if (size < sizeof(struct nk_page)) return;
  18055. /* first nk_page_element is embedded in nk_page, additional elements follow in adjacent space */
  18056. pool->capacity = (unsigned)(1 + (size - sizeof(struct nk_page)) / sizeof(struct nk_page_element));
  18057. pool->pages = (struct nk_page*)memory;
  18058. pool->type = NK_BUFFER_FIXED;
  18059. pool->size = size;
  18060. }
  18061. NK_LIB struct nk_page_element*
  18062. nk_pool_alloc(struct nk_pool *pool)
  18063. {
  18064. if (!pool->pages || pool->pages->size >= pool->capacity) {
  18065. /* allocate new page */
  18066. struct nk_page *page;
  18067. if (pool->type == NK_BUFFER_FIXED) {
  18068. NK_ASSERT(pool->pages);
  18069. if (!pool->pages) return 0;
  18070. NK_ASSERT(pool->pages->size < pool->capacity);
  18071. return 0;
  18072. } else {
  18073. nk_size size = sizeof(struct nk_page);
  18074. size += (pool->capacity - 1) * sizeof(struct nk_page_element);
  18075. page = (struct nk_page*)pool->alloc.alloc(pool->alloc.userdata,0, size);
  18076. page->next = pool->pages;
  18077. pool->pages = page;
  18078. page->size = 0;
  18079. }
  18080. } return &pool->pages->win[pool->pages->size++];
  18081. }
  18082. /* ===============================================================
  18083. *
  18084. * PAGE ELEMENT
  18085. *
  18086. * ===============================================================*/
  18087. NK_LIB struct nk_page_element*
  18088. nk_create_page_element(struct nk_context *ctx)
  18089. {
  18090. struct nk_page_element *elem;
  18091. if (ctx->freelist) {
  18092. /* unlink page element from free list */
  18093. elem = ctx->freelist;
  18094. ctx->freelist = elem->next;
  18095. } else if (ctx->use_pool) {
  18096. /* allocate page element from memory pool */
  18097. elem = nk_pool_alloc(&ctx->pool);
  18098. NK_ASSERT(elem);
  18099. if (!elem) return 0;
  18100. } else {
  18101. /* allocate new page element from back of fixed size memory buffer */
  18102. NK_STORAGE const nk_size size = sizeof(struct nk_page_element);
  18103. NK_STORAGE const nk_size align = NK_ALIGNOF(struct nk_page_element);
  18104. elem = (struct nk_page_element*)nk_buffer_alloc(&ctx->memory, NK_BUFFER_BACK, size, align);
  18105. NK_ASSERT(elem);
  18106. if (!elem) return 0;
  18107. }
  18108. nk_zero_struct(*elem);
  18109. elem->next = 0;
  18110. elem->prev = 0;
  18111. return elem;
  18112. }
  18113. NK_LIB void
  18114. nk_link_page_element_into_freelist(struct nk_context *ctx,
  18115. struct nk_page_element *elem)
  18116. {
  18117. /* link table into freelist */
  18118. if (!ctx->freelist) {
  18119. ctx->freelist = elem;
  18120. } else {
  18121. elem->next = ctx->freelist;
  18122. ctx->freelist = elem;
  18123. }
  18124. }
  18125. NK_LIB void
  18126. nk_free_page_element(struct nk_context *ctx, struct nk_page_element *elem)
  18127. {
  18128. /* we have a pool so just add to free list */
  18129. if (ctx->use_pool) {
  18130. nk_link_page_element_into_freelist(ctx, elem);
  18131. return;
  18132. }
  18133. /* if possible remove last element from back of fixed memory buffer */
  18134. {void *elem_end = (void*)(elem + 1);
  18135. void *buffer_end = (nk_byte*)ctx->memory.memory.ptr + ctx->memory.size;
  18136. if (elem_end == buffer_end)
  18137. ctx->memory.size -= sizeof(struct nk_page_element);
  18138. else nk_link_page_element_into_freelist(ctx, elem);}
  18139. }
  18140. /* ===============================================================
  18141. *
  18142. * TABLE
  18143. *
  18144. * ===============================================================*/
  18145. NK_LIB struct nk_table*
  18146. nk_create_table(struct nk_context *ctx)
  18147. {
  18148. struct nk_page_element *elem;
  18149. elem = nk_create_page_element(ctx);
  18150. if (!elem) return 0;
  18151. nk_zero_struct(*elem);
  18152. return &elem->data.tbl;
  18153. }
  18154. NK_LIB void
  18155. nk_free_table(struct nk_context *ctx, struct nk_table *tbl)
  18156. {
  18157. union nk_page_data *pd = NK_CONTAINER_OF(tbl, union nk_page_data, tbl);
  18158. struct nk_page_element *pe = NK_CONTAINER_OF(pd, struct nk_page_element, data);
  18159. nk_free_page_element(ctx, pe);
  18160. }
  18161. NK_LIB void
  18162. nk_push_table(struct nk_window *win, struct nk_table *tbl)
  18163. {
  18164. if (!win->tables) {
  18165. win->tables = tbl;
  18166. tbl->next = 0;
  18167. tbl->prev = 0;
  18168. tbl->size = 0;
  18169. win->table_count = 1;
  18170. return;
  18171. }
  18172. win->tables->prev = tbl;
  18173. tbl->next = win->tables;
  18174. tbl->prev = 0;
  18175. tbl->size = 0;
  18176. win->tables = tbl;
  18177. win->table_count++;
  18178. }
  18179. NK_LIB void
  18180. nk_remove_table(struct nk_window *win, struct nk_table *tbl)
  18181. {
  18182. if (win->tables == tbl)
  18183. win->tables = tbl->next;
  18184. if (tbl->next)
  18185. tbl->next->prev = tbl->prev;
  18186. if (tbl->prev)
  18187. tbl->prev->next = tbl->next;
  18188. tbl->next = 0;
  18189. tbl->prev = 0;
  18190. }
  18191. NK_LIB nk_uint*
  18192. nk_add_value(struct nk_context *ctx, struct nk_window *win,
  18193. nk_hash name, nk_uint value)
  18194. {
  18195. NK_ASSERT(ctx);
  18196. NK_ASSERT(win);
  18197. if (!win || !ctx) return 0;
  18198. if (!win->tables || win->tables->size >= NK_VALUE_PAGE_CAPACITY) {
  18199. struct nk_table *tbl = nk_create_table(ctx);
  18200. NK_ASSERT(tbl);
  18201. if (!tbl) return 0;
  18202. nk_push_table(win, tbl);
  18203. }
  18204. win->tables->seq = win->seq;
  18205. win->tables->keys[win->tables->size] = name;
  18206. win->tables->values[win->tables->size] = value;
  18207. return &win->tables->values[win->tables->size++];
  18208. }
  18209. NK_LIB nk_uint*
  18210. nk_find_value(const struct nk_window *win, nk_hash name)
  18211. {
  18212. struct nk_table *iter = win->tables;
  18213. while (iter) {
  18214. unsigned int i = 0;
  18215. unsigned int size = iter->size;
  18216. for (i = 0; i < size; ++i) {
  18217. if (iter->keys[i] == name) {
  18218. iter->seq = win->seq;
  18219. return &iter->values[i];
  18220. }
  18221. } size = NK_VALUE_PAGE_CAPACITY;
  18222. iter = iter->next;
  18223. }
  18224. return 0;
  18225. }
  18226. /* ===============================================================
  18227. *
  18228. * PANEL
  18229. *
  18230. * ===============================================================*/
  18231. NK_LIB void*
  18232. nk_create_panel(struct nk_context *ctx)
  18233. {
  18234. struct nk_page_element *elem;
  18235. elem = nk_create_page_element(ctx);
  18236. if (!elem) return 0;
  18237. nk_zero_struct(*elem);
  18238. return &elem->data.pan;
  18239. }
  18240. NK_LIB void
  18241. nk_free_panel(struct nk_context *ctx, struct nk_panel *pan)
  18242. {
  18243. union nk_page_data *pd = NK_CONTAINER_OF(pan, union nk_page_data, pan);
  18244. struct nk_page_element *pe = NK_CONTAINER_OF(pd, struct nk_page_element, data);
  18245. nk_free_page_element(ctx, pe);
  18246. }
  18247. NK_LIB nk_bool
  18248. nk_panel_has_header(nk_flags flags, const char *title)
  18249. {
  18250. nk_bool active = 0;
  18251. active = (flags & (NK_WINDOW_CLOSABLE|NK_WINDOW_MINIMIZABLE));
  18252. active = active || (flags & NK_WINDOW_TITLE);
  18253. active = active && !(flags & NK_WINDOW_HIDDEN) && title;
  18254. return active;
  18255. }
  18256. NK_LIB struct nk_vec2
  18257. nk_panel_get_padding(const struct nk_style *style, enum nk_panel_type type)
  18258. {
  18259. switch (type) {
  18260. default:
  18261. case NK_PANEL_WINDOW: return style->window.padding;
  18262. case NK_PANEL_GROUP: return style->window.group_padding;
  18263. case NK_PANEL_POPUP: return style->window.popup_padding;
  18264. case NK_PANEL_CONTEXTUAL: return style->window.contextual_padding;
  18265. case NK_PANEL_COMBO: return style->window.combo_padding;
  18266. case NK_PANEL_MENU: return style->window.menu_padding;
  18267. case NK_PANEL_TOOLTIP: return style->window.menu_padding;}
  18268. }
  18269. NK_LIB float
  18270. nk_panel_get_border(const struct nk_style *style, nk_flags flags,
  18271. enum nk_panel_type type)
  18272. {
  18273. if (flags & NK_WINDOW_BORDER) {
  18274. switch (type) {
  18275. default:
  18276. case NK_PANEL_WINDOW: return style->window.border;
  18277. case NK_PANEL_GROUP: return style->window.group_border;
  18278. case NK_PANEL_POPUP: return style->window.popup_border;
  18279. case NK_PANEL_CONTEXTUAL: return style->window.contextual_border;
  18280. case NK_PANEL_COMBO: return style->window.combo_border;
  18281. case NK_PANEL_MENU: return style->window.menu_border;
  18282. case NK_PANEL_TOOLTIP: return style->window.menu_border;
  18283. }} else return 0;
  18284. }
  18285. NK_LIB struct nk_color
  18286. nk_panel_get_border_color(const struct nk_style *style, enum nk_panel_type type)
  18287. {
  18288. switch (type) {
  18289. default:
  18290. case NK_PANEL_WINDOW: return style->window.border_color;
  18291. case NK_PANEL_GROUP: return style->window.group_border_color;
  18292. case NK_PANEL_POPUP: return style->window.popup_border_color;
  18293. case NK_PANEL_CONTEXTUAL: return style->window.contextual_border_color;
  18294. case NK_PANEL_COMBO: return style->window.combo_border_color;
  18295. case NK_PANEL_MENU: return style->window.menu_border_color;
  18296. case NK_PANEL_TOOLTIP: return style->window.menu_border_color;}
  18297. }
  18298. NK_LIB nk_bool
  18299. nk_panel_is_sub(enum nk_panel_type type)
  18300. {
  18301. return ((int)type & (int)NK_PANEL_SET_SUB)?1:0;
  18302. }
  18303. NK_LIB nk_bool
  18304. nk_panel_is_nonblock(enum nk_panel_type type)
  18305. {
  18306. return ((int)type & (int)NK_PANEL_SET_NONBLOCK)?1:0;
  18307. }
  18308. NK_LIB nk_bool
  18309. nk_panel_begin(struct nk_context *ctx, const char *title, enum nk_panel_type panel_type)
  18310. {
  18311. struct nk_input *in;
  18312. struct nk_window *win;
  18313. struct nk_panel *layout;
  18314. struct nk_command_buffer *out;
  18315. const struct nk_style *style;
  18316. const struct nk_user_font *font;
  18317. struct nk_vec2 scrollbar_size;
  18318. struct nk_vec2 panel_padding;
  18319. NK_ASSERT(ctx);
  18320. NK_ASSERT(ctx->current);
  18321. NK_ASSERT(ctx->current->layout);
  18322. if (!ctx || !ctx->current || !ctx->current->layout) return 0;
  18323. nk_zero(ctx->current->layout, sizeof(*ctx->current->layout));
  18324. if ((ctx->current->flags & NK_WINDOW_HIDDEN) || (ctx->current->flags & NK_WINDOW_CLOSED)) {
  18325. nk_zero(ctx->current->layout, sizeof(struct nk_panel));
  18326. ctx->current->layout->type = panel_type;
  18327. return 0;
  18328. }
  18329. /* pull state into local stack */
  18330. style = &ctx->style;
  18331. font = style->font;
  18332. win = ctx->current;
  18333. layout = win->layout;
  18334. out = &win->buffer;
  18335. in = (win->flags & NK_WINDOW_NO_INPUT) ? 0: &ctx->input;
  18336. #ifdef NK_INCLUDE_COMMAND_USERDATA
  18337. win->buffer.userdata = ctx->userdata;
  18338. #endif
  18339. /* pull style configuration into local stack */
  18340. scrollbar_size = style->window.scrollbar_size;
  18341. panel_padding = nk_panel_get_padding(style, panel_type);
  18342. /* window movement */
  18343. if ((win->flags & NK_WINDOW_MOVABLE) && !(win->flags & NK_WINDOW_ROM)) {
  18344. nk_bool left_mouse_down;
  18345. unsigned int left_mouse_clicked;
  18346. int left_mouse_click_in_cursor;
  18347. /* calculate draggable window space */
  18348. struct nk_rect header;
  18349. header.x = win->bounds.x;
  18350. header.y = win->bounds.y;
  18351. header.w = win->bounds.w;
  18352. if (nk_panel_has_header(win->flags, title)) {
  18353. header.h = font->height + 2.0f * style->window.header.padding.y;
  18354. header.h += 2.0f * style->window.header.label_padding.y;
  18355. } else header.h = panel_padding.y;
  18356. /* window movement by dragging */
  18357. left_mouse_down = in->mouse.buttons[NK_BUTTON_LEFT].down;
  18358. left_mouse_clicked = in->mouse.buttons[NK_BUTTON_LEFT].clicked;
  18359. left_mouse_click_in_cursor = nk_input_has_mouse_click_down_in_rect(in,
  18360. NK_BUTTON_LEFT, header, nk_true);
  18361. if (left_mouse_down && left_mouse_click_in_cursor && !left_mouse_clicked) {
  18362. win->bounds.x = win->bounds.x + in->mouse.delta.x;
  18363. win->bounds.y = win->bounds.y + in->mouse.delta.y;
  18364. in->mouse.buttons[NK_BUTTON_LEFT].clicked_pos.x += in->mouse.delta.x;
  18365. in->mouse.buttons[NK_BUTTON_LEFT].clicked_pos.y += in->mouse.delta.y;
  18366. ctx->style.cursor_active = ctx->style.cursors[NK_CURSOR_MOVE];
  18367. }
  18368. }
  18369. /* setup panel */
  18370. layout->type = panel_type;
  18371. layout->flags = win->flags;
  18372. layout->bounds = win->bounds;
  18373. layout->bounds.x += panel_padding.x;
  18374. layout->bounds.w -= 2*panel_padding.x;
  18375. if (win->flags & NK_WINDOW_BORDER) {
  18376. layout->border = nk_panel_get_border(style, win->flags, panel_type);
  18377. layout->bounds = nk_shrink_rect(layout->bounds, layout->border);
  18378. } else layout->border = 0;
  18379. layout->at_y = layout->bounds.y;
  18380. layout->at_x = layout->bounds.x;
  18381. layout->max_x = 0;
  18382. layout->header_height = 0;
  18383. layout->footer_height = 0;
  18384. nk_layout_reset_min_row_height(ctx);
  18385. layout->row.index = 0;
  18386. layout->row.columns = 0;
  18387. layout->row.ratio = 0;
  18388. layout->row.item_width = 0;
  18389. layout->row.tree_depth = 0;
  18390. layout->row.height = panel_padding.y;
  18391. layout->has_scrolling = nk_true;
  18392. if (!(win->flags & NK_WINDOW_NO_SCROLLBAR))
  18393. layout->bounds.w -= scrollbar_size.x;
  18394. if (!nk_panel_is_nonblock(panel_type)) {
  18395. layout->footer_height = 0;
  18396. if (!(win->flags & NK_WINDOW_NO_SCROLLBAR) || win->flags & NK_WINDOW_SCALABLE)
  18397. layout->footer_height = scrollbar_size.y;
  18398. layout->bounds.h -= layout->footer_height;
  18399. }
  18400. /* panel header */
  18401. if (nk_panel_has_header(win->flags, title))
  18402. {
  18403. struct nk_text text;
  18404. struct nk_rect header;
  18405. const struct nk_style_item *background = 0;
  18406. /* calculate header bounds */
  18407. header.x = win->bounds.x;
  18408. header.y = win->bounds.y;
  18409. header.w = win->bounds.w;
  18410. header.h = font->height + 2.0f * style->window.header.padding.y;
  18411. header.h += (2.0f * style->window.header.label_padding.y);
  18412. /* shrink panel by header */
  18413. layout->header_height = header.h;
  18414. layout->bounds.y += header.h;
  18415. layout->bounds.h -= header.h;
  18416. layout->at_y += header.h;
  18417. /* select correct header background and text color */
  18418. if (ctx->active == win) {
  18419. background = &style->window.header.active;
  18420. text.text = style->window.header.label_active;
  18421. } else if (nk_input_is_mouse_hovering_rect(&ctx->input, header)) {
  18422. background = &style->window.header.hover;
  18423. text.text = style->window.header.label_hover;
  18424. } else {
  18425. background = &style->window.header.normal;
  18426. text.text = style->window.header.label_normal;
  18427. }
  18428. /* draw header background */
  18429. header.h += 1.0f;
  18430. switch(background->type) {
  18431. case NK_STYLE_ITEM_IMAGE:
  18432. text.background = nk_rgba(0,0,0,0);
  18433. nk_draw_image(&win->buffer, header, &background->data.image, nk_white);
  18434. break;
  18435. case NK_STYLE_ITEM_NINE_SLICE:
  18436. text.background = nk_rgba(0, 0, 0, 0);
  18437. nk_draw_nine_slice(&win->buffer, header, &background->data.slice, nk_white);
  18438. break;
  18439. case NK_STYLE_ITEM_COLOR:
  18440. text.background = background->data.color;
  18441. nk_fill_rect(out, header, 0, background->data.color);
  18442. break;
  18443. }
  18444. /* window close button */
  18445. {struct nk_rect button;
  18446. button.y = header.y + style->window.header.padding.y;
  18447. button.h = header.h - 2 * style->window.header.padding.y;
  18448. button.w = button.h;
  18449. if (win->flags & NK_WINDOW_CLOSABLE) {
  18450. nk_flags ws = 0;
  18451. if (style->window.header.align == NK_HEADER_RIGHT) {
  18452. button.x = (header.w + header.x) - (button.w + style->window.header.padding.x);
  18453. header.w -= button.w + style->window.header.spacing.x + style->window.header.padding.x;
  18454. } else {
  18455. button.x = header.x + style->window.header.padding.x;
  18456. header.x += button.w + style->window.header.spacing.x + style->window.header.padding.x;
  18457. }
  18458. if (nk_do_button_symbol(&ws, &win->buffer, button,
  18459. style->window.header.close_symbol, NK_BUTTON_DEFAULT,
  18460. &style->window.header.close_button, in, style->font) && !(win->flags & NK_WINDOW_ROM))
  18461. {
  18462. layout->flags |= NK_WINDOW_HIDDEN;
  18463. layout->flags &= (nk_flags)~NK_WINDOW_MINIMIZED;
  18464. }
  18465. }
  18466. /* window minimize button */
  18467. if (win->flags & NK_WINDOW_MINIMIZABLE) {
  18468. nk_flags ws = 0;
  18469. if (style->window.header.align == NK_HEADER_RIGHT) {
  18470. button.x = (header.w + header.x) - button.w;
  18471. if (!(win->flags & NK_WINDOW_CLOSABLE)) {
  18472. button.x -= style->window.header.padding.x;
  18473. header.w -= style->window.header.padding.x;
  18474. }
  18475. header.w -= button.w + style->window.header.spacing.x;
  18476. } else {
  18477. button.x = header.x;
  18478. header.x += button.w + style->window.header.spacing.x + style->window.header.padding.x;
  18479. }
  18480. if (nk_do_button_symbol(&ws, &win->buffer, button, (layout->flags & NK_WINDOW_MINIMIZED)?
  18481. style->window.header.maximize_symbol: style->window.header.minimize_symbol,
  18482. NK_BUTTON_DEFAULT, &style->window.header.minimize_button, in, style->font) && !(win->flags & NK_WINDOW_ROM))
  18483. layout->flags = (layout->flags & NK_WINDOW_MINIMIZED) ?
  18484. layout->flags & (nk_flags)~NK_WINDOW_MINIMIZED:
  18485. layout->flags | NK_WINDOW_MINIMIZED;
  18486. }}
  18487. {/* window header title */
  18488. int text_len = nk_strlen(title);
  18489. struct nk_rect label = {0,0,0,0};
  18490. float t = font->width(font->userdata, font->height, title, text_len);
  18491. text.padding = nk_vec2(0,0);
  18492. label.x = header.x + style->window.header.padding.x;
  18493. label.x += style->window.header.label_padding.x;
  18494. label.y = header.y + style->window.header.label_padding.y;
  18495. label.h = font->height + 2 * style->window.header.label_padding.y;
  18496. label.w = t + 2 * style->window.header.spacing.x;
  18497. label.w = NK_CLAMP(0, label.w, header.x + header.w - label.x);
  18498. nk_widget_text(out, label, (const char*)title, text_len, &text, NK_TEXT_LEFT, font);}
  18499. }
  18500. /* draw window background */
  18501. if (!(layout->flags & NK_WINDOW_MINIMIZED) && !(layout->flags & NK_WINDOW_DYNAMIC)) {
  18502. struct nk_rect body;
  18503. body.x = win->bounds.x;
  18504. body.w = win->bounds.w;
  18505. body.y = (win->bounds.y + layout->header_height);
  18506. body.h = (win->bounds.h - layout->header_height);
  18507. switch(style->window.fixed_background.type) {
  18508. case NK_STYLE_ITEM_IMAGE:
  18509. nk_draw_image(out, body, &style->window.fixed_background.data.image, nk_white);
  18510. break;
  18511. case NK_STYLE_ITEM_NINE_SLICE:
  18512. nk_draw_nine_slice(out, body, &style->window.fixed_background.data.slice, nk_white);
  18513. break;
  18514. case NK_STYLE_ITEM_COLOR:
  18515. nk_fill_rect(out, body, style->window.rounding, style->window.fixed_background.data.color);
  18516. break;
  18517. }
  18518. }
  18519. /* set clipping rectangle */
  18520. {struct nk_rect clip;
  18521. layout->clip = layout->bounds;
  18522. nk_unify(&clip, &win->buffer.clip, layout->clip.x, layout->clip.y,
  18523. layout->clip.x + layout->clip.w, layout->clip.y + layout->clip.h);
  18524. nk_push_scissor(out, clip);
  18525. layout->clip = clip;}
  18526. return !(layout->flags & NK_WINDOW_HIDDEN) && !(layout->flags & NK_WINDOW_MINIMIZED);
  18527. }
  18528. NK_LIB void
  18529. nk_panel_end(struct nk_context *ctx)
  18530. {
  18531. struct nk_input *in;
  18532. struct nk_window *window;
  18533. struct nk_panel *layout;
  18534. const struct nk_style *style;
  18535. struct nk_command_buffer *out;
  18536. struct nk_vec2 scrollbar_size;
  18537. struct nk_vec2 panel_padding;
  18538. NK_ASSERT(ctx);
  18539. NK_ASSERT(ctx->current);
  18540. NK_ASSERT(ctx->current->layout);
  18541. if (!ctx || !ctx->current || !ctx->current->layout)
  18542. return;
  18543. window = ctx->current;
  18544. layout = window->layout;
  18545. style = &ctx->style;
  18546. out = &window->buffer;
  18547. in = (layout->flags & NK_WINDOW_ROM || layout->flags & NK_WINDOW_NO_INPUT) ? 0 :&ctx->input;
  18548. if (!nk_panel_is_sub(layout->type))
  18549. nk_push_scissor(out, nk_null_rect);
  18550. /* cache configuration data */
  18551. scrollbar_size = style->window.scrollbar_size;
  18552. panel_padding = nk_panel_get_padding(style, layout->type);
  18553. /* update the current cursor Y-position to point over the last added widget */
  18554. layout->at_y += layout->row.height;
  18555. /* dynamic panels */
  18556. if (layout->flags & NK_WINDOW_DYNAMIC && !(layout->flags & NK_WINDOW_MINIMIZED))
  18557. {
  18558. /* update panel height to fit dynamic growth */
  18559. struct nk_rect empty_space;
  18560. if (layout->at_y < (layout->bounds.y + layout->bounds.h))
  18561. layout->bounds.h = layout->at_y - layout->bounds.y;
  18562. /* fill top empty space */
  18563. empty_space.x = window->bounds.x;
  18564. empty_space.y = layout->bounds.y;
  18565. empty_space.h = panel_padding.y;
  18566. empty_space.w = window->bounds.w;
  18567. nk_fill_rect(out, empty_space, 0, style->window.background);
  18568. /* fill left empty space */
  18569. empty_space.x = window->bounds.x;
  18570. empty_space.y = layout->bounds.y;
  18571. empty_space.w = panel_padding.x + layout->border;
  18572. empty_space.h = layout->bounds.h;
  18573. nk_fill_rect(out, empty_space, 0, style->window.background);
  18574. /* fill right empty space */
  18575. empty_space.x = layout->bounds.x + layout->bounds.w;
  18576. empty_space.y = layout->bounds.y;
  18577. empty_space.w = panel_padding.x + layout->border;
  18578. empty_space.h = layout->bounds.h;
  18579. if (*layout->offset_y == 0 && !(layout->flags & NK_WINDOW_NO_SCROLLBAR))
  18580. empty_space.w += scrollbar_size.x;
  18581. nk_fill_rect(out, empty_space, 0, style->window.background);
  18582. /* fill bottom empty space */
  18583. if (layout->footer_height > 0) {
  18584. empty_space.x = window->bounds.x;
  18585. empty_space.y = layout->bounds.y + layout->bounds.h;
  18586. empty_space.w = window->bounds.w;
  18587. empty_space.h = layout->footer_height;
  18588. nk_fill_rect(out, empty_space, 0, style->window.background);
  18589. }
  18590. }
  18591. /* scrollbars */
  18592. if (!(layout->flags & NK_WINDOW_NO_SCROLLBAR) &&
  18593. !(layout->flags & NK_WINDOW_MINIMIZED) &&
  18594. window->scrollbar_hiding_timer < NK_SCROLLBAR_HIDING_TIMEOUT)
  18595. {
  18596. struct nk_rect scroll;
  18597. int scroll_has_scrolling;
  18598. float scroll_target;
  18599. float scroll_offset;
  18600. float scroll_step;
  18601. float scroll_inc;
  18602. /* mouse wheel scrolling */
  18603. if (nk_panel_is_sub(layout->type))
  18604. {
  18605. /* sub-window mouse wheel scrolling */
  18606. struct nk_window *root_window = window;
  18607. struct nk_panel *root_panel = window->layout;
  18608. while (root_panel->parent)
  18609. root_panel = root_panel->parent;
  18610. while (root_window->parent)
  18611. root_window = root_window->parent;
  18612. /* only allow scrolling if parent window is active */
  18613. scroll_has_scrolling = nk_false;
  18614. if ((root_window == ctx->active) && layout->has_scrolling) {
  18615. /* and panel is being hovered and inside clip rect*/
  18616. if (nk_input_is_mouse_hovering_rect(in, layout->bounds) &&
  18617. NK_INTERSECT(layout->bounds.x, layout->bounds.y, layout->bounds.w, layout->bounds.h,
  18618. root_panel->clip.x, root_panel->clip.y, root_panel->clip.w, root_panel->clip.h))
  18619. {
  18620. /* deactivate all parent scrolling */
  18621. root_panel = window->layout;
  18622. while (root_panel->parent) {
  18623. root_panel->has_scrolling = nk_false;
  18624. root_panel = root_panel->parent;
  18625. }
  18626. root_panel->has_scrolling = nk_false;
  18627. scroll_has_scrolling = nk_true;
  18628. }
  18629. }
  18630. } else {
  18631. /* window mouse wheel scrolling */
  18632. scroll_has_scrolling = (window == ctx->active) && layout->has_scrolling;
  18633. if (in && (in->mouse.scroll_delta.y > 0 || in->mouse.scroll_delta.x > 0) && scroll_has_scrolling)
  18634. window->scrolled = nk_true;
  18635. else window->scrolled = nk_false;
  18636. }
  18637. {
  18638. /* vertical scrollbar */
  18639. nk_flags state = 0;
  18640. scroll.x = layout->bounds.x + layout->bounds.w + panel_padding.x;
  18641. scroll.y = layout->bounds.y;
  18642. scroll.w = scrollbar_size.x;
  18643. scroll.h = layout->bounds.h;
  18644. scroll_offset = (float)*layout->offset_y;
  18645. scroll_step = scroll.h * 0.10f;
  18646. scroll_inc = scroll.h * 0.01f;
  18647. scroll_target = (float)(int)(layout->at_y - scroll.y);
  18648. scroll_offset = nk_do_scrollbarv(&state, out, scroll, scroll_has_scrolling,
  18649. scroll_offset, scroll_target, scroll_step, scroll_inc,
  18650. &ctx->style.scrollv, in, style->font);
  18651. *layout->offset_y = (nk_uint)scroll_offset;
  18652. if (in && scroll_has_scrolling)
  18653. in->mouse.scroll_delta.y = 0;
  18654. }
  18655. {
  18656. /* horizontal scrollbar */
  18657. nk_flags state = 0;
  18658. scroll.x = layout->bounds.x;
  18659. scroll.y = layout->bounds.y + layout->bounds.h;
  18660. scroll.w = layout->bounds.w;
  18661. scroll.h = scrollbar_size.y;
  18662. scroll_offset = (float)*layout->offset_x;
  18663. scroll_target = (float)(int)(layout->max_x - scroll.x);
  18664. scroll_step = layout->max_x * 0.05f;
  18665. scroll_inc = layout->max_x * 0.005f;
  18666. scroll_offset = nk_do_scrollbarh(&state, out, scroll, scroll_has_scrolling,
  18667. scroll_offset, scroll_target, scroll_step, scroll_inc,
  18668. &ctx->style.scrollh, in, style->font);
  18669. *layout->offset_x = (nk_uint)scroll_offset;
  18670. }
  18671. }
  18672. /* hide scroll if no user input */
  18673. if (window->flags & NK_WINDOW_SCROLL_AUTO_HIDE) {
  18674. int has_input = ctx->input.mouse.delta.x != 0 || ctx->input.mouse.delta.y != 0 || ctx->input.mouse.scroll_delta.y != 0;
  18675. int is_window_hovered = nk_window_is_hovered(ctx);
  18676. int any_item_active = (ctx->last_widget_state & NK_WIDGET_STATE_MODIFIED);
  18677. if ((!has_input && is_window_hovered) || (!is_window_hovered && !any_item_active))
  18678. window->scrollbar_hiding_timer += ctx->delta_time_seconds;
  18679. else window->scrollbar_hiding_timer = 0;
  18680. } else window->scrollbar_hiding_timer = 0;
  18681. /* window border */
  18682. if (layout->flags & NK_WINDOW_BORDER)
  18683. {
  18684. struct nk_color border_color = nk_panel_get_border_color(style, layout->type);
  18685. const float padding_y = (layout->flags & NK_WINDOW_MINIMIZED)
  18686. ? (style->window.border + window->bounds.y + layout->header_height)
  18687. : ((layout->flags & NK_WINDOW_DYNAMIC)
  18688. ? (layout->bounds.y + layout->bounds.h + layout->footer_height)
  18689. : (window->bounds.y + window->bounds.h));
  18690. struct nk_rect b = window->bounds;
  18691. b.h = padding_y - window->bounds.y;
  18692. nk_stroke_rect(out, b, style->window.rounding, layout->border, border_color);
  18693. }
  18694. /* scaler */
  18695. if ((layout->flags & NK_WINDOW_SCALABLE) && in && !(layout->flags & NK_WINDOW_MINIMIZED))
  18696. {
  18697. /* calculate scaler bounds */
  18698. struct nk_rect scaler;
  18699. scaler.w = scrollbar_size.x;
  18700. scaler.h = scrollbar_size.y;
  18701. scaler.y = layout->bounds.y + layout->bounds.h;
  18702. if (layout->flags & NK_WINDOW_SCALE_LEFT)
  18703. scaler.x = layout->bounds.x - panel_padding.x * 0.5f;
  18704. else scaler.x = layout->bounds.x + layout->bounds.w + panel_padding.x;
  18705. if (layout->flags & NK_WINDOW_NO_SCROLLBAR)
  18706. scaler.x -= scaler.w;
  18707. /* draw scaler */
  18708. {const struct nk_style_item *item = &style->window.scaler;
  18709. if (item->type == NK_STYLE_ITEM_IMAGE)
  18710. nk_draw_image(out, scaler, &item->data.image, nk_white);
  18711. else {
  18712. if (layout->flags & NK_WINDOW_SCALE_LEFT) {
  18713. nk_fill_triangle(out, scaler.x, scaler.y, scaler.x,
  18714. scaler.y + scaler.h, scaler.x + scaler.w,
  18715. scaler.y + scaler.h, item->data.color);
  18716. } else {
  18717. nk_fill_triangle(out, scaler.x + scaler.w, scaler.y, scaler.x + scaler.w,
  18718. scaler.y + scaler.h, scaler.x, scaler.y + scaler.h, item->data.color);
  18719. }
  18720. }}
  18721. /* do window scaling */
  18722. if (!(window->flags & NK_WINDOW_ROM)) {
  18723. struct nk_vec2 window_size = style->window.min_size;
  18724. int left_mouse_down = in->mouse.buttons[NK_BUTTON_LEFT].down;
  18725. int left_mouse_click_in_scaler = nk_input_has_mouse_click_down_in_rect(in,
  18726. NK_BUTTON_LEFT, scaler, nk_true);
  18727. if (left_mouse_down && left_mouse_click_in_scaler) {
  18728. float delta_x = in->mouse.delta.x;
  18729. if (layout->flags & NK_WINDOW_SCALE_LEFT) {
  18730. delta_x = -delta_x;
  18731. window->bounds.x += in->mouse.delta.x;
  18732. }
  18733. /* dragging in x-direction */
  18734. if (window->bounds.w + delta_x >= window_size.x) {
  18735. if ((delta_x < 0) || (delta_x > 0 && in->mouse.pos.x >= scaler.x)) {
  18736. window->bounds.w = window->bounds.w + delta_x;
  18737. scaler.x += in->mouse.delta.x;
  18738. }
  18739. }
  18740. /* dragging in y-direction (only possible if static window) */
  18741. if (!(layout->flags & NK_WINDOW_DYNAMIC)) {
  18742. if (window_size.y < window->bounds.h + in->mouse.delta.y) {
  18743. if ((in->mouse.delta.y < 0) || (in->mouse.delta.y > 0 && in->mouse.pos.y >= scaler.y)) {
  18744. window->bounds.h = window->bounds.h + in->mouse.delta.y;
  18745. scaler.y += in->mouse.delta.y;
  18746. }
  18747. }
  18748. }
  18749. ctx->style.cursor_active = ctx->style.cursors[NK_CURSOR_RESIZE_TOP_RIGHT_DOWN_LEFT];
  18750. in->mouse.buttons[NK_BUTTON_LEFT].clicked_pos.x = scaler.x + scaler.w/2.0f;
  18751. in->mouse.buttons[NK_BUTTON_LEFT].clicked_pos.y = scaler.y + scaler.h/2.0f;
  18752. }
  18753. }
  18754. }
  18755. if (!nk_panel_is_sub(layout->type)) {
  18756. /* window is hidden so clear command buffer */
  18757. if (layout->flags & NK_WINDOW_HIDDEN)
  18758. nk_command_buffer_reset(&window->buffer);
  18759. /* window is visible and not tab */
  18760. else nk_finish(ctx, window);
  18761. }
  18762. /* NK_WINDOW_REMOVE_ROM flag was set so remove NK_WINDOW_ROM */
  18763. if (layout->flags & NK_WINDOW_REMOVE_ROM) {
  18764. layout->flags &= ~(nk_flags)NK_WINDOW_ROM;
  18765. layout->flags &= ~(nk_flags)NK_WINDOW_REMOVE_ROM;
  18766. }
  18767. window->flags = layout->flags;
  18768. /* property garbage collector */
  18769. if (window->property.active && window->property.old != window->property.seq &&
  18770. window->property.active == window->property.prev) {
  18771. nk_zero(&window->property, sizeof(window->property));
  18772. } else {
  18773. window->property.old = window->property.seq;
  18774. window->property.prev = window->property.active;
  18775. window->property.seq = 0;
  18776. }
  18777. /* edit garbage collector */
  18778. if (window->edit.active && window->edit.old != window->edit.seq &&
  18779. window->edit.active == window->edit.prev) {
  18780. nk_zero(&window->edit, sizeof(window->edit));
  18781. } else {
  18782. window->edit.old = window->edit.seq;
  18783. window->edit.prev = window->edit.active;
  18784. window->edit.seq = 0;
  18785. }
  18786. /* contextual garbage collector */
  18787. if (window->popup.active_con && window->popup.con_old != window->popup.con_count) {
  18788. window->popup.con_count = 0;
  18789. window->popup.con_old = 0;
  18790. window->popup.active_con = 0;
  18791. } else {
  18792. window->popup.con_old = window->popup.con_count;
  18793. window->popup.con_count = 0;
  18794. }
  18795. window->popup.combo_count = 0;
  18796. /* helper to make sure you have a 'nk_tree_push' for every 'nk_tree_pop' */
  18797. NK_ASSERT(!layout->row.tree_depth);
  18798. }
  18799. /* ===============================================================
  18800. *
  18801. * WINDOW
  18802. *
  18803. * ===============================================================*/
  18804. NK_LIB void*
  18805. nk_create_window(struct nk_context *ctx)
  18806. {
  18807. struct nk_page_element *elem;
  18808. elem = nk_create_page_element(ctx);
  18809. if (!elem) return 0;
  18810. elem->data.win.seq = ctx->seq;
  18811. return &elem->data.win;
  18812. }
  18813. NK_LIB void
  18814. nk_free_window(struct nk_context *ctx, struct nk_window *win)
  18815. {
  18816. /* unlink windows from list */
  18817. struct nk_table *it = win->tables;
  18818. if (win->popup.win) {
  18819. nk_free_window(ctx, win->popup.win);
  18820. win->popup.win = 0;
  18821. }
  18822. win->next = 0;
  18823. win->prev = 0;
  18824. while (it) {
  18825. /*free window state tables */
  18826. struct nk_table *n = it->next;
  18827. nk_remove_table(win, it);
  18828. nk_free_table(ctx, it);
  18829. if (it == win->tables)
  18830. win->tables = n;
  18831. it = n;
  18832. }
  18833. /* link windows into freelist */
  18834. {union nk_page_data *pd = NK_CONTAINER_OF(win, union nk_page_data, win);
  18835. struct nk_page_element *pe = NK_CONTAINER_OF(pd, struct nk_page_element, data);
  18836. nk_free_page_element(ctx, pe);}
  18837. }
  18838. NK_LIB struct nk_window*
  18839. nk_find_window(const struct nk_context *ctx, nk_hash hash, const char *name)
  18840. {
  18841. struct nk_window *iter;
  18842. iter = ctx->begin;
  18843. while (iter) {
  18844. NK_ASSERT(iter != iter->next);
  18845. if (iter->name == hash) {
  18846. int max_len = nk_strlen(iter->name_string);
  18847. if (!nk_stricmpn(iter->name_string, name, max_len))
  18848. return iter;
  18849. }
  18850. iter = iter->next;
  18851. }
  18852. return 0;
  18853. }
  18854. NK_LIB void
  18855. nk_insert_window(struct nk_context *ctx, struct nk_window *win,
  18856. enum nk_window_insert_location loc)
  18857. {
  18858. const struct nk_window *iter;
  18859. NK_ASSERT(ctx);
  18860. NK_ASSERT(win);
  18861. if (!win || !ctx) return;
  18862. iter = ctx->begin;
  18863. while (iter) {
  18864. NK_ASSERT(iter != iter->next);
  18865. NK_ASSERT(iter != win);
  18866. if (iter == win) return;
  18867. iter = iter->next;
  18868. }
  18869. if (!ctx->begin) {
  18870. win->next = 0;
  18871. win->prev = 0;
  18872. ctx->begin = win;
  18873. ctx->end = win;
  18874. ctx->count = 1;
  18875. return;
  18876. }
  18877. if (loc == NK_INSERT_BACK) {
  18878. struct nk_window *end;
  18879. end = ctx->end;
  18880. end->flags |= NK_WINDOW_ROM;
  18881. end->next = win;
  18882. win->prev = ctx->end;
  18883. win->next = 0;
  18884. ctx->end = win;
  18885. ctx->active = ctx->end;
  18886. ctx->end->flags &= ~(nk_flags)NK_WINDOW_ROM;
  18887. } else {
  18888. /*ctx->end->flags |= NK_WINDOW_ROM;*/
  18889. ctx->begin->prev = win;
  18890. win->next = ctx->begin;
  18891. win->prev = 0;
  18892. ctx->begin = win;
  18893. ctx->begin->flags &= ~(nk_flags)NK_WINDOW_ROM;
  18894. }
  18895. ctx->count++;
  18896. }
  18897. NK_LIB void
  18898. nk_remove_window(struct nk_context *ctx, struct nk_window *win)
  18899. {
  18900. if (win == ctx->begin || win == ctx->end) {
  18901. if (win == ctx->begin) {
  18902. ctx->begin = win->next;
  18903. if (win->next)
  18904. win->next->prev = 0;
  18905. }
  18906. if (win == ctx->end) {
  18907. ctx->end = win->prev;
  18908. if (win->prev)
  18909. win->prev->next = 0;
  18910. }
  18911. } else {
  18912. if (win->next)
  18913. win->next->prev = win->prev;
  18914. if (win->prev)
  18915. win->prev->next = win->next;
  18916. }
  18917. if (win == ctx->active || !ctx->active) {
  18918. ctx->active = ctx->end;
  18919. if (ctx->end)
  18920. ctx->end->flags &= ~(nk_flags)NK_WINDOW_ROM;
  18921. }
  18922. win->next = 0;
  18923. win->prev = 0;
  18924. ctx->count--;
  18925. }
  18926. NK_API nk_bool
  18927. nk_begin(struct nk_context *ctx, const char *title,
  18928. struct nk_rect bounds, nk_flags flags)
  18929. {
  18930. return nk_begin_titled(ctx, title, title, bounds, flags);
  18931. }
  18932. NK_API nk_bool
  18933. nk_begin_titled(struct nk_context *ctx, const char *name, const char *title,
  18934. struct nk_rect bounds, nk_flags flags)
  18935. {
  18936. struct nk_window *win;
  18937. struct nk_style *style;
  18938. nk_hash name_hash;
  18939. int name_len;
  18940. int ret = 0;
  18941. NK_ASSERT(ctx);
  18942. NK_ASSERT(name);
  18943. NK_ASSERT(title);
  18944. NK_ASSERT(ctx->style.font && ctx->style.font->width && "if this triggers you forgot to add a font");
  18945. NK_ASSERT(!ctx->current && "if this triggers you missed a `nk_end` call");
  18946. if (!ctx || ctx->current || !title || !name)
  18947. return 0;
  18948. /* find or create window */
  18949. style = &ctx->style;
  18950. name_len = (int)nk_strlen(name);
  18951. name_hash = nk_murmur_hash(name, (int)name_len, NK_WINDOW_TITLE);
  18952. win = nk_find_window(ctx, name_hash, name);
  18953. if (!win) {
  18954. /* create new window */
  18955. nk_size name_length = (nk_size)name_len;
  18956. win = (struct nk_window*)nk_create_window(ctx);
  18957. NK_ASSERT(win);
  18958. if (!win) return 0;
  18959. if (flags & NK_WINDOW_BACKGROUND)
  18960. nk_insert_window(ctx, win, NK_INSERT_FRONT);
  18961. else nk_insert_window(ctx, win, NK_INSERT_BACK);
  18962. nk_command_buffer_init(&win->buffer, &ctx->memory, NK_CLIPPING_ON);
  18963. win->flags = flags;
  18964. win->bounds = bounds;
  18965. win->name = name_hash;
  18966. name_length = NK_MIN(name_length, NK_WINDOW_MAX_NAME-1);
  18967. NK_MEMCPY(win->name_string, name, name_length);
  18968. win->name_string[name_length] = 0;
  18969. win->popup.win = 0;
  18970. win->widgets_disabled = nk_false;
  18971. if (!ctx->active)
  18972. ctx->active = win;
  18973. } else {
  18974. /* update window */
  18975. win->flags &= ~(nk_flags)(NK_WINDOW_PRIVATE-1);
  18976. win->flags |= flags;
  18977. if (!(win->flags & (NK_WINDOW_MOVABLE | NK_WINDOW_SCALABLE)))
  18978. win->bounds = bounds;
  18979. /* If this assert triggers you either:
  18980. *
  18981. * I.) Have more than one window with the same name or
  18982. * II.) You forgot to actually draw the window.
  18983. * More specific you did not call `nk_clear` (nk_clear will be
  18984. * automatically called for you if you are using one of the
  18985. * provided demo backends). */
  18986. NK_ASSERT(win->seq != ctx->seq);
  18987. win->seq = ctx->seq;
  18988. if (!ctx->active && !(win->flags & NK_WINDOW_HIDDEN)) {
  18989. ctx->active = win;
  18990. ctx->end = win;
  18991. }
  18992. }
  18993. if (win->flags & NK_WINDOW_HIDDEN) {
  18994. ctx->current = win;
  18995. win->layout = 0;
  18996. return 0;
  18997. } else nk_start(ctx, win);
  18998. /* window overlapping */
  18999. if (!(win->flags & NK_WINDOW_HIDDEN) && !(win->flags & NK_WINDOW_NO_INPUT))
  19000. {
  19001. int inpanel, ishovered;
  19002. struct nk_window *iter = win;
  19003. float h = ctx->style.font->height + 2.0f * style->window.header.padding.y +
  19004. (2.0f * style->window.header.label_padding.y);
  19005. struct nk_rect win_bounds = (!(win->flags & NK_WINDOW_MINIMIZED))?
  19006. win->bounds: nk_rect(win->bounds.x, win->bounds.y, win->bounds.w, h);
  19007. /* activate window if hovered and no other window is overlapping this window */
  19008. inpanel = nk_input_has_mouse_click_down_in_rect(&ctx->input, NK_BUTTON_LEFT, win_bounds, nk_true);
  19009. inpanel = inpanel && ctx->input.mouse.buttons[NK_BUTTON_LEFT].clicked;
  19010. ishovered = nk_input_is_mouse_hovering_rect(&ctx->input, win_bounds);
  19011. if ((win != ctx->active) && ishovered && !ctx->input.mouse.buttons[NK_BUTTON_LEFT].down) {
  19012. iter = win->next;
  19013. while (iter) {
  19014. struct nk_rect iter_bounds = (!(iter->flags & NK_WINDOW_MINIMIZED))?
  19015. iter->bounds: nk_rect(iter->bounds.x, iter->bounds.y, iter->bounds.w, h);
  19016. if (NK_INTERSECT(win_bounds.x, win_bounds.y, win_bounds.w, win_bounds.h,
  19017. iter_bounds.x, iter_bounds.y, iter_bounds.w, iter_bounds.h) &&
  19018. (!(iter->flags & NK_WINDOW_HIDDEN)))
  19019. break;
  19020. if (iter->popup.win && iter->popup.active && !(iter->flags & NK_WINDOW_HIDDEN) &&
  19021. NK_INTERSECT(win->bounds.x, win_bounds.y, win_bounds.w, win_bounds.h,
  19022. iter->popup.win->bounds.x, iter->popup.win->bounds.y,
  19023. iter->popup.win->bounds.w, iter->popup.win->bounds.h))
  19024. break;
  19025. iter = iter->next;
  19026. }
  19027. }
  19028. /* activate window if clicked */
  19029. if (iter && inpanel && (win != ctx->end)) {
  19030. iter = win->next;
  19031. while (iter) {
  19032. /* try to find a panel with higher priority in the same position */
  19033. struct nk_rect iter_bounds = (!(iter->flags & NK_WINDOW_MINIMIZED))?
  19034. iter->bounds: nk_rect(iter->bounds.x, iter->bounds.y, iter->bounds.w, h);
  19035. if (NK_INBOX(ctx->input.mouse.pos.x, ctx->input.mouse.pos.y,
  19036. iter_bounds.x, iter_bounds.y, iter_bounds.w, iter_bounds.h) &&
  19037. !(iter->flags & NK_WINDOW_HIDDEN))
  19038. break;
  19039. if (iter->popup.win && iter->popup.active && !(iter->flags & NK_WINDOW_HIDDEN) &&
  19040. NK_INTERSECT(win_bounds.x, win_bounds.y, win_bounds.w, win_bounds.h,
  19041. iter->popup.win->bounds.x, iter->popup.win->bounds.y,
  19042. iter->popup.win->bounds.w, iter->popup.win->bounds.h))
  19043. break;
  19044. iter = iter->next;
  19045. }
  19046. }
  19047. if (iter && !(win->flags & NK_WINDOW_ROM) && (win->flags & NK_WINDOW_BACKGROUND)) {
  19048. win->flags |= (nk_flags)NK_WINDOW_ROM;
  19049. iter->flags &= ~(nk_flags)NK_WINDOW_ROM;
  19050. ctx->active = iter;
  19051. if (!(iter->flags & NK_WINDOW_BACKGROUND)) {
  19052. /* current window is active in that position so transfer to top
  19053. * at the highest priority in stack */
  19054. nk_remove_window(ctx, iter);
  19055. nk_insert_window(ctx, iter, NK_INSERT_BACK);
  19056. }
  19057. } else {
  19058. if (!iter && ctx->end != win) {
  19059. if (!(win->flags & NK_WINDOW_BACKGROUND)) {
  19060. /* current window is active in that position so transfer to top
  19061. * at the highest priority in stack */
  19062. nk_remove_window(ctx, win);
  19063. nk_insert_window(ctx, win, NK_INSERT_BACK);
  19064. }
  19065. win->flags &= ~(nk_flags)NK_WINDOW_ROM;
  19066. ctx->active = win;
  19067. }
  19068. if (ctx->end != win && !(win->flags & NK_WINDOW_BACKGROUND))
  19069. win->flags |= NK_WINDOW_ROM;
  19070. }
  19071. }
  19072. win->layout = (struct nk_panel*)nk_create_panel(ctx);
  19073. ctx->current = win;
  19074. ret = nk_panel_begin(ctx, title, NK_PANEL_WINDOW);
  19075. win->layout->offset_x = &win->scrollbar.x;
  19076. win->layout->offset_y = &win->scrollbar.y;
  19077. return ret;
  19078. }
  19079. NK_API void
  19080. nk_end(struct nk_context *ctx)
  19081. {
  19082. struct nk_panel *layout;
  19083. NK_ASSERT(ctx);
  19084. NK_ASSERT(ctx->current && "if this triggers you forgot to call `nk_begin`");
  19085. if (!ctx || !ctx->current)
  19086. return;
  19087. layout = ctx->current->layout;
  19088. if (!layout || (layout->type == NK_PANEL_WINDOW && (ctx->current->flags & NK_WINDOW_HIDDEN))) {
  19089. ctx->current = 0;
  19090. return;
  19091. }
  19092. nk_panel_end(ctx);
  19093. nk_free_panel(ctx, ctx->current->layout);
  19094. ctx->current = 0;
  19095. }
  19096. NK_API struct nk_rect
  19097. nk_window_get_bounds(const struct nk_context *ctx)
  19098. {
  19099. NK_ASSERT(ctx);
  19100. NK_ASSERT(ctx->current);
  19101. if (!ctx || !ctx->current) return nk_rect(0,0,0,0);
  19102. return ctx->current->bounds;
  19103. }
  19104. NK_API struct nk_vec2
  19105. nk_window_get_position(const struct nk_context *ctx)
  19106. {
  19107. NK_ASSERT(ctx);
  19108. NK_ASSERT(ctx->current);
  19109. if (!ctx || !ctx->current) return nk_vec2(0,0);
  19110. return nk_vec2(ctx->current->bounds.x, ctx->current->bounds.y);
  19111. }
  19112. NK_API struct nk_vec2
  19113. nk_window_get_size(const struct nk_context *ctx)
  19114. {
  19115. NK_ASSERT(ctx);
  19116. NK_ASSERT(ctx->current);
  19117. if (!ctx || !ctx->current) return nk_vec2(0,0);
  19118. return nk_vec2(ctx->current->bounds.w, ctx->current->bounds.h);
  19119. }
  19120. NK_API float
  19121. nk_window_get_width(const struct nk_context *ctx)
  19122. {
  19123. NK_ASSERT(ctx);
  19124. NK_ASSERT(ctx->current);
  19125. if (!ctx || !ctx->current) return 0;
  19126. return ctx->current->bounds.w;
  19127. }
  19128. NK_API float
  19129. nk_window_get_height(const struct nk_context *ctx)
  19130. {
  19131. NK_ASSERT(ctx);
  19132. NK_ASSERT(ctx->current);
  19133. if (!ctx || !ctx->current) return 0;
  19134. return ctx->current->bounds.h;
  19135. }
  19136. NK_API struct nk_rect
  19137. nk_window_get_content_region(const struct nk_context *ctx)
  19138. {
  19139. NK_ASSERT(ctx);
  19140. NK_ASSERT(ctx->current);
  19141. if (!ctx || !ctx->current) return nk_rect(0,0,0,0);
  19142. return ctx->current->layout->clip;
  19143. }
  19144. NK_API struct nk_vec2
  19145. nk_window_get_content_region_min(const struct nk_context *ctx)
  19146. {
  19147. NK_ASSERT(ctx);
  19148. NK_ASSERT(ctx->current);
  19149. NK_ASSERT(ctx->current->layout);
  19150. if (!ctx || !ctx->current) return nk_vec2(0,0);
  19151. return nk_vec2(ctx->current->layout->clip.x, ctx->current->layout->clip.y);
  19152. }
  19153. NK_API struct nk_vec2
  19154. nk_window_get_content_region_max(const struct nk_context *ctx)
  19155. {
  19156. NK_ASSERT(ctx);
  19157. NK_ASSERT(ctx->current);
  19158. NK_ASSERT(ctx->current->layout);
  19159. if (!ctx || !ctx->current) return nk_vec2(0,0);
  19160. return nk_vec2(ctx->current->layout->clip.x + ctx->current->layout->clip.w,
  19161. ctx->current->layout->clip.y + ctx->current->layout->clip.h);
  19162. }
  19163. NK_API struct nk_vec2
  19164. nk_window_get_content_region_size(const struct nk_context *ctx)
  19165. {
  19166. NK_ASSERT(ctx);
  19167. NK_ASSERT(ctx->current);
  19168. NK_ASSERT(ctx->current->layout);
  19169. if (!ctx || !ctx->current) return nk_vec2(0,0);
  19170. return nk_vec2(ctx->current->layout->clip.w, ctx->current->layout->clip.h);
  19171. }
  19172. NK_API struct nk_command_buffer*
  19173. nk_window_get_canvas(const struct nk_context *ctx)
  19174. {
  19175. NK_ASSERT(ctx);
  19176. NK_ASSERT(ctx->current);
  19177. NK_ASSERT(ctx->current->layout);
  19178. if (!ctx || !ctx->current) return 0;
  19179. return &ctx->current->buffer;
  19180. }
  19181. NK_API struct nk_panel*
  19182. nk_window_get_panel(const struct nk_context *ctx)
  19183. {
  19184. NK_ASSERT(ctx);
  19185. NK_ASSERT(ctx->current);
  19186. if (!ctx || !ctx->current) return 0;
  19187. return ctx->current->layout;
  19188. }
  19189. NK_API void
  19190. nk_window_get_scroll(const struct nk_context *ctx, nk_uint *offset_x, nk_uint *offset_y)
  19191. {
  19192. struct nk_window *win;
  19193. NK_ASSERT(ctx);
  19194. NK_ASSERT(ctx->current);
  19195. if (!ctx || !ctx->current)
  19196. return ;
  19197. win = ctx->current;
  19198. if (offset_x)
  19199. *offset_x = win->scrollbar.x;
  19200. if (offset_y)
  19201. *offset_y = win->scrollbar.y;
  19202. }
  19203. NK_API nk_bool
  19204. nk_window_has_focus(const struct nk_context *ctx)
  19205. {
  19206. NK_ASSERT(ctx);
  19207. NK_ASSERT(ctx->current);
  19208. NK_ASSERT(ctx->current->layout);
  19209. if (!ctx || !ctx->current) return 0;
  19210. return ctx->current == ctx->active;
  19211. }
  19212. NK_API nk_bool
  19213. nk_window_is_hovered(const struct nk_context *ctx)
  19214. {
  19215. NK_ASSERT(ctx);
  19216. NK_ASSERT(ctx->current);
  19217. if (!ctx || !ctx->current || (ctx->current->flags & NK_WINDOW_HIDDEN))
  19218. return 0;
  19219. else {
  19220. struct nk_rect actual_bounds = ctx->current->bounds;
  19221. if (ctx->begin->flags & NK_WINDOW_MINIMIZED) {
  19222. actual_bounds.h = ctx->current->layout->header_height;
  19223. }
  19224. return nk_input_is_mouse_hovering_rect(&ctx->input, actual_bounds);
  19225. }
  19226. }
  19227. NK_API nk_bool
  19228. nk_window_is_any_hovered(const struct nk_context *ctx)
  19229. {
  19230. struct nk_window *iter;
  19231. NK_ASSERT(ctx);
  19232. if (!ctx) return 0;
  19233. iter = ctx->begin;
  19234. while (iter) {
  19235. /* check if window is being hovered */
  19236. if(!(iter->flags & NK_WINDOW_HIDDEN)) {
  19237. /* check if window popup is being hovered */
  19238. if (iter->popup.active && iter->popup.win && nk_input_is_mouse_hovering_rect(&ctx->input, iter->popup.win->bounds))
  19239. return 1;
  19240. if (iter->flags & NK_WINDOW_MINIMIZED) {
  19241. struct nk_rect header = iter->bounds;
  19242. header.h = ctx->style.font->height + 2 * ctx->style.window.header.padding.y;
  19243. if (nk_input_is_mouse_hovering_rect(&ctx->input, header))
  19244. return 1;
  19245. } else if (nk_input_is_mouse_hovering_rect(&ctx->input, iter->bounds)) {
  19246. return 1;
  19247. }
  19248. }
  19249. iter = iter->next;
  19250. }
  19251. return 0;
  19252. }
  19253. NK_API nk_bool
  19254. nk_item_is_any_active(const struct nk_context *ctx)
  19255. {
  19256. int any_hovered = nk_window_is_any_hovered(ctx);
  19257. int any_active = (ctx->last_widget_state & NK_WIDGET_STATE_MODIFIED);
  19258. return any_hovered || any_active;
  19259. }
  19260. NK_API nk_bool
  19261. nk_window_is_collapsed(const struct nk_context *ctx, const char *name)
  19262. {
  19263. int title_len;
  19264. nk_hash title_hash;
  19265. struct nk_window *win;
  19266. NK_ASSERT(ctx);
  19267. if (!ctx) return 0;
  19268. title_len = (int)nk_strlen(name);
  19269. title_hash = nk_murmur_hash(name, (int)title_len, NK_WINDOW_TITLE);
  19270. win = nk_find_window(ctx, title_hash, name);
  19271. if (!win) return 0;
  19272. return win->flags & NK_WINDOW_MINIMIZED;
  19273. }
  19274. NK_API nk_bool
  19275. nk_window_is_closed(const struct nk_context *ctx, const char *name)
  19276. {
  19277. int title_len;
  19278. nk_hash title_hash;
  19279. struct nk_window *win;
  19280. NK_ASSERT(ctx);
  19281. if (!ctx) return 1;
  19282. title_len = (int)nk_strlen(name);
  19283. title_hash = nk_murmur_hash(name, (int)title_len, NK_WINDOW_TITLE);
  19284. win = nk_find_window(ctx, title_hash, name);
  19285. if (!win) return 1;
  19286. return (win->flags & NK_WINDOW_CLOSED);
  19287. }
  19288. NK_API nk_bool
  19289. nk_window_is_hidden(const struct nk_context *ctx, const char *name)
  19290. {
  19291. int title_len;
  19292. nk_hash title_hash;
  19293. struct nk_window *win;
  19294. NK_ASSERT(ctx);
  19295. if (!ctx) return 1;
  19296. title_len = (int)nk_strlen(name);
  19297. title_hash = nk_murmur_hash(name, (int)title_len, NK_WINDOW_TITLE);
  19298. win = nk_find_window(ctx, title_hash, name);
  19299. if (!win) return 1;
  19300. return (win->flags & NK_WINDOW_HIDDEN);
  19301. }
  19302. NK_API nk_bool
  19303. nk_window_is_active(const struct nk_context *ctx, const char *name)
  19304. {
  19305. int title_len;
  19306. nk_hash title_hash;
  19307. struct nk_window *win;
  19308. NK_ASSERT(ctx);
  19309. if (!ctx) return 0;
  19310. title_len = (int)nk_strlen(name);
  19311. title_hash = nk_murmur_hash(name, (int)title_len, NK_WINDOW_TITLE);
  19312. win = nk_find_window(ctx, title_hash, name);
  19313. if (!win) return 0;
  19314. return win == ctx->active;
  19315. }
  19316. NK_API struct nk_window*
  19317. nk_window_find(const struct nk_context *ctx, const char *name)
  19318. {
  19319. int title_len;
  19320. nk_hash title_hash;
  19321. title_len = (int)nk_strlen(name);
  19322. title_hash = nk_murmur_hash(name, (int)title_len, NK_WINDOW_TITLE);
  19323. return nk_find_window(ctx, title_hash, name);
  19324. }
  19325. NK_API void
  19326. nk_window_close(struct nk_context *ctx, const char *name)
  19327. {
  19328. struct nk_window *win;
  19329. NK_ASSERT(ctx);
  19330. if (!ctx) return;
  19331. win = nk_window_find(ctx, name);
  19332. if (!win) return;
  19333. NK_ASSERT(ctx->current != win && "You cannot close a currently active window");
  19334. if (ctx->current == win) return;
  19335. win->flags |= NK_WINDOW_HIDDEN;
  19336. win->flags |= NK_WINDOW_CLOSED;
  19337. }
  19338. NK_API void
  19339. nk_window_set_bounds(struct nk_context *ctx,
  19340. const char *name, struct nk_rect bounds)
  19341. {
  19342. struct nk_window *win;
  19343. NK_ASSERT(ctx);
  19344. if (!ctx) return;
  19345. win = nk_window_find(ctx, name);
  19346. if (!win) return;
  19347. win->bounds = bounds;
  19348. }
  19349. NK_API void
  19350. nk_window_set_position(struct nk_context *ctx,
  19351. const char *name, struct nk_vec2 pos)
  19352. {
  19353. struct nk_window *win = nk_window_find(ctx, name);
  19354. if (!win) return;
  19355. win->bounds.x = pos.x;
  19356. win->bounds.y = pos.y;
  19357. }
  19358. NK_API void
  19359. nk_window_set_size(struct nk_context *ctx,
  19360. const char *name, struct nk_vec2 size)
  19361. {
  19362. struct nk_window *win = nk_window_find(ctx, name);
  19363. if (!win) return;
  19364. win->bounds.w = size.x;
  19365. win->bounds.h = size.y;
  19366. }
  19367. NK_API void
  19368. nk_window_set_scroll(struct nk_context *ctx, nk_uint offset_x, nk_uint offset_y)
  19369. {
  19370. struct nk_window *win;
  19371. NK_ASSERT(ctx);
  19372. NK_ASSERT(ctx->current);
  19373. if (!ctx || !ctx->current)
  19374. return;
  19375. win = ctx->current;
  19376. win->scrollbar.x = offset_x;
  19377. win->scrollbar.y = offset_y;
  19378. }
  19379. NK_API void
  19380. nk_window_collapse(struct nk_context *ctx, const char *name,
  19381. enum nk_collapse_states c)
  19382. {
  19383. int title_len;
  19384. nk_hash title_hash;
  19385. struct nk_window *win;
  19386. NK_ASSERT(ctx);
  19387. if (!ctx) return;
  19388. title_len = (int)nk_strlen(name);
  19389. title_hash = nk_murmur_hash(name, (int)title_len, NK_WINDOW_TITLE);
  19390. win = nk_find_window(ctx, title_hash, name);
  19391. if (!win) return;
  19392. if (c == NK_MINIMIZED)
  19393. win->flags |= NK_WINDOW_MINIMIZED;
  19394. else win->flags &= ~(nk_flags)NK_WINDOW_MINIMIZED;
  19395. }
  19396. NK_API void
  19397. nk_window_collapse_if(struct nk_context *ctx, const char *name,
  19398. enum nk_collapse_states c, int cond)
  19399. {
  19400. NK_ASSERT(ctx);
  19401. if (!ctx || !cond) return;
  19402. nk_window_collapse(ctx, name, c);
  19403. }
  19404. NK_API void
  19405. nk_window_show(struct nk_context *ctx, const char *name, enum nk_show_states s)
  19406. {
  19407. int title_len;
  19408. nk_hash title_hash;
  19409. struct nk_window *win;
  19410. NK_ASSERT(ctx);
  19411. if (!ctx) return;
  19412. title_len = (int)nk_strlen(name);
  19413. title_hash = nk_murmur_hash(name, (int)title_len, NK_WINDOW_TITLE);
  19414. win = nk_find_window(ctx, title_hash, name);
  19415. if (!win) return;
  19416. if (s == NK_HIDDEN) {
  19417. win->flags |= NK_WINDOW_HIDDEN;
  19418. } else win->flags &= ~(nk_flags)NK_WINDOW_HIDDEN;
  19419. }
  19420. NK_API void
  19421. nk_window_show_if(struct nk_context *ctx, const char *name,
  19422. enum nk_show_states s, int cond)
  19423. {
  19424. NK_ASSERT(ctx);
  19425. if (!ctx || !cond) return;
  19426. nk_window_show(ctx, name, s);
  19427. }
  19428. NK_API void
  19429. nk_window_set_focus(struct nk_context *ctx, const char *name)
  19430. {
  19431. int title_len;
  19432. nk_hash title_hash;
  19433. struct nk_window *win;
  19434. NK_ASSERT(ctx);
  19435. if (!ctx) return;
  19436. title_len = (int)nk_strlen(name);
  19437. title_hash = nk_murmur_hash(name, (int)title_len, NK_WINDOW_TITLE);
  19438. win = nk_find_window(ctx, title_hash, name);
  19439. if (win && ctx->end != win) {
  19440. nk_remove_window(ctx, win);
  19441. nk_insert_window(ctx, win, NK_INSERT_BACK);
  19442. }
  19443. ctx->active = win;
  19444. }
  19445. NK_API void
  19446. nk_rule_horizontal(struct nk_context *ctx, struct nk_color color, nk_bool rounding)
  19447. {
  19448. struct nk_rect space;
  19449. enum nk_widget_layout_states state = nk_widget(&space, ctx);
  19450. struct nk_command_buffer *canvas = nk_window_get_canvas(ctx);
  19451. if (!state) return;
  19452. nk_fill_rect(canvas, space, rounding && space.h > 1.5f ? space.h / 2.0f : 0, color);
  19453. }
  19454. /* ===============================================================
  19455. *
  19456. * POPUP
  19457. *
  19458. * ===============================================================*/
  19459. NK_API nk_bool
  19460. nk_popup_begin(struct nk_context *ctx, enum nk_popup_type type,
  19461. const char *title, nk_flags flags, struct nk_rect rect)
  19462. {
  19463. struct nk_window *popup;
  19464. struct nk_window *win;
  19465. struct nk_panel *panel;
  19466. int title_len;
  19467. nk_hash title_hash;
  19468. nk_size allocated;
  19469. NK_ASSERT(ctx);
  19470. NK_ASSERT(title);
  19471. NK_ASSERT(ctx->current);
  19472. NK_ASSERT(ctx->current->layout);
  19473. if (!ctx || !ctx->current || !ctx->current->layout)
  19474. return 0;
  19475. win = ctx->current;
  19476. panel = win->layout;
  19477. NK_ASSERT(!((int)panel->type & (int)NK_PANEL_SET_POPUP) && "popups are not allowed to have popups");
  19478. (void)panel;
  19479. title_len = (int)nk_strlen(title);
  19480. title_hash = nk_murmur_hash(title, (int)title_len, NK_PANEL_POPUP);
  19481. popup = win->popup.win;
  19482. if (!popup) {
  19483. popup = (struct nk_window*)nk_create_window(ctx);
  19484. popup->parent = win;
  19485. win->popup.win = popup;
  19486. win->popup.active = 0;
  19487. win->popup.type = NK_PANEL_POPUP;
  19488. }
  19489. /* make sure we have correct popup */
  19490. if (win->popup.name != title_hash) {
  19491. if (!win->popup.active) {
  19492. nk_zero(popup, sizeof(*popup));
  19493. win->popup.name = title_hash;
  19494. win->popup.active = 1;
  19495. win->popup.type = NK_PANEL_POPUP;
  19496. } else return 0;
  19497. }
  19498. /* popup position is local to window */
  19499. ctx->current = popup;
  19500. rect.x += win->layout->clip.x;
  19501. rect.y += win->layout->clip.y;
  19502. /* setup popup data */
  19503. popup->parent = win;
  19504. popup->bounds = rect;
  19505. popup->seq = ctx->seq;
  19506. popup->layout = (struct nk_panel*)nk_create_panel(ctx);
  19507. popup->flags = flags;
  19508. popup->flags |= NK_WINDOW_BORDER;
  19509. if (type == NK_POPUP_DYNAMIC)
  19510. popup->flags |= NK_WINDOW_DYNAMIC;
  19511. popup->buffer = win->buffer;
  19512. nk_start_popup(ctx, win);
  19513. allocated = ctx->memory.allocated;
  19514. nk_push_scissor(&popup->buffer, nk_null_rect);
  19515. if (nk_panel_begin(ctx, title, NK_PANEL_POPUP)) {
  19516. /* popup is running therefore invalidate parent panels */
  19517. struct nk_panel *root;
  19518. root = win->layout;
  19519. while (root) {
  19520. root->flags |= NK_WINDOW_ROM;
  19521. root->flags &= ~(nk_flags)NK_WINDOW_REMOVE_ROM;
  19522. root = root->parent;
  19523. }
  19524. win->popup.active = 1;
  19525. popup->layout->offset_x = &popup->scrollbar.x;
  19526. popup->layout->offset_y = &popup->scrollbar.y;
  19527. popup->layout->parent = win->layout;
  19528. return 1;
  19529. } else {
  19530. /* popup was closed/is invalid so cleanup */
  19531. struct nk_panel *root;
  19532. root = win->layout;
  19533. while (root) {
  19534. root->flags |= NK_WINDOW_REMOVE_ROM;
  19535. root = root->parent;
  19536. }
  19537. win->popup.buf.active = 0;
  19538. win->popup.active = 0;
  19539. ctx->memory.allocated = allocated;
  19540. ctx->current = win;
  19541. nk_free_panel(ctx, popup->layout);
  19542. popup->layout = 0;
  19543. return 0;
  19544. }
  19545. }
  19546. NK_LIB nk_bool
  19547. nk_nonblock_begin(struct nk_context *ctx,
  19548. nk_flags flags, struct nk_rect body, struct nk_rect header,
  19549. enum nk_panel_type panel_type)
  19550. {
  19551. struct nk_window *popup;
  19552. struct nk_window *win;
  19553. struct nk_panel *panel;
  19554. int is_active = nk_true;
  19555. NK_ASSERT(ctx);
  19556. NK_ASSERT(ctx->current);
  19557. NK_ASSERT(ctx->current->layout);
  19558. if (!ctx || !ctx->current || !ctx->current->layout)
  19559. return 0;
  19560. /* popups cannot have popups */
  19561. win = ctx->current;
  19562. panel = win->layout;
  19563. NK_ASSERT(!((int)panel->type & (int)NK_PANEL_SET_POPUP));
  19564. (void)panel;
  19565. popup = win->popup.win;
  19566. if (!popup) {
  19567. /* create window for nonblocking popup */
  19568. popup = (struct nk_window*)nk_create_window(ctx);
  19569. popup->parent = win;
  19570. win->popup.win = popup;
  19571. win->popup.type = panel_type;
  19572. nk_command_buffer_init(&popup->buffer, &ctx->memory, NK_CLIPPING_ON);
  19573. } else {
  19574. /* close the popup if user pressed outside or in the header */
  19575. int pressed, in_body, in_header;
  19576. #ifdef NK_BUTTON_TRIGGER_ON_RELEASE
  19577. pressed = nk_input_is_mouse_released(&ctx->input, NK_BUTTON_LEFT);
  19578. #else
  19579. pressed = nk_input_is_mouse_pressed(&ctx->input, NK_BUTTON_LEFT);
  19580. #endif
  19581. in_body = nk_input_is_mouse_hovering_rect(&ctx->input, body);
  19582. in_header = nk_input_is_mouse_hovering_rect(&ctx->input, header);
  19583. if (pressed && (!in_body || in_header))
  19584. is_active = nk_false;
  19585. }
  19586. win->popup.header = header;
  19587. if (!is_active) {
  19588. /* remove read only mode from all parent panels */
  19589. struct nk_panel *root = win->layout;
  19590. while (root) {
  19591. root->flags |= NK_WINDOW_REMOVE_ROM;
  19592. root = root->parent;
  19593. }
  19594. return is_active;
  19595. }
  19596. popup->bounds = body;
  19597. popup->parent = win;
  19598. popup->layout = (struct nk_panel*)nk_create_panel(ctx);
  19599. popup->flags = flags;
  19600. popup->flags |= NK_WINDOW_BORDER;
  19601. popup->flags |= NK_WINDOW_DYNAMIC;
  19602. popup->seq = ctx->seq;
  19603. win->popup.active = 1;
  19604. NK_ASSERT(popup->layout);
  19605. nk_start_popup(ctx, win);
  19606. popup->buffer = win->buffer;
  19607. nk_push_scissor(&popup->buffer, nk_null_rect);
  19608. ctx->current = popup;
  19609. nk_panel_begin(ctx, 0, panel_type);
  19610. win->buffer = popup->buffer;
  19611. popup->layout->parent = win->layout;
  19612. popup->layout->offset_x = &popup->scrollbar.x;
  19613. popup->layout->offset_y = &popup->scrollbar.y;
  19614. /* set read only mode to all parent panels */
  19615. {struct nk_panel *root;
  19616. root = win->layout;
  19617. while (root) {
  19618. root->flags |= NK_WINDOW_ROM;
  19619. root = root->parent;
  19620. }}
  19621. return is_active;
  19622. }
  19623. NK_API void
  19624. nk_popup_close(struct nk_context *ctx)
  19625. {
  19626. struct nk_window *popup;
  19627. NK_ASSERT(ctx);
  19628. if (!ctx || !ctx->current) return;
  19629. popup = ctx->current;
  19630. NK_ASSERT(popup->parent);
  19631. NK_ASSERT((int)popup->layout->type & (int)NK_PANEL_SET_POPUP);
  19632. popup->flags |= NK_WINDOW_HIDDEN;
  19633. }
  19634. NK_API void
  19635. nk_popup_end(struct nk_context *ctx)
  19636. {
  19637. struct nk_window *win;
  19638. struct nk_window *popup;
  19639. NK_ASSERT(ctx);
  19640. NK_ASSERT(ctx->current);
  19641. NK_ASSERT(ctx->current->layout);
  19642. if (!ctx || !ctx->current || !ctx->current->layout)
  19643. return;
  19644. popup = ctx->current;
  19645. if (!popup->parent) return;
  19646. win = popup->parent;
  19647. if (popup->flags & NK_WINDOW_HIDDEN) {
  19648. struct nk_panel *root;
  19649. root = win->layout;
  19650. while (root) {
  19651. root->flags |= NK_WINDOW_REMOVE_ROM;
  19652. root = root->parent;
  19653. }
  19654. win->popup.active = 0;
  19655. }
  19656. nk_push_scissor(&popup->buffer, nk_null_rect);
  19657. nk_end(ctx);
  19658. win->buffer = popup->buffer;
  19659. nk_finish_popup(ctx, win);
  19660. ctx->current = win;
  19661. nk_push_scissor(&win->buffer, win->layout->clip);
  19662. }
  19663. NK_API void
  19664. nk_popup_get_scroll(const struct nk_context *ctx, nk_uint *offset_x, nk_uint *offset_y)
  19665. {
  19666. struct nk_window *popup;
  19667. NK_ASSERT(ctx);
  19668. NK_ASSERT(ctx->current);
  19669. NK_ASSERT(ctx->current->layout);
  19670. if (!ctx || !ctx->current || !ctx->current->layout)
  19671. return;
  19672. popup = ctx->current;
  19673. if (offset_x)
  19674. *offset_x = popup->scrollbar.x;
  19675. if (offset_y)
  19676. *offset_y = popup->scrollbar.y;
  19677. }
  19678. NK_API void
  19679. nk_popup_set_scroll(struct nk_context *ctx, nk_uint offset_x, nk_uint offset_y)
  19680. {
  19681. struct nk_window *popup;
  19682. NK_ASSERT(ctx);
  19683. NK_ASSERT(ctx->current);
  19684. NK_ASSERT(ctx->current->layout);
  19685. if (!ctx || !ctx->current || !ctx->current->layout)
  19686. return;
  19687. popup = ctx->current;
  19688. popup->scrollbar.x = offset_x;
  19689. popup->scrollbar.y = offset_y;
  19690. }
  19691. /* ==============================================================
  19692. *
  19693. * CONTEXTUAL
  19694. *
  19695. * ===============================================================*/
  19696. NK_API nk_bool
  19697. nk_contextual_begin(struct nk_context *ctx, nk_flags flags, struct nk_vec2 size,
  19698. struct nk_rect trigger_bounds)
  19699. {
  19700. struct nk_window *win;
  19701. struct nk_window *popup;
  19702. struct nk_rect body;
  19703. struct nk_input* in;
  19704. NK_STORAGE const struct nk_rect null_rect = {-1,-1,0,0};
  19705. int is_clicked = 0;
  19706. int is_open = 0;
  19707. int ret = 0;
  19708. NK_ASSERT(ctx);
  19709. NK_ASSERT(ctx->current);
  19710. NK_ASSERT(ctx->current->layout);
  19711. if (!ctx || !ctx->current || !ctx->current->layout)
  19712. return 0;
  19713. win = ctx->current;
  19714. ++win->popup.con_count;
  19715. if (ctx->current != ctx->active)
  19716. return 0;
  19717. /* check if currently active contextual is active */
  19718. popup = win->popup.win;
  19719. is_open = (popup && win->popup.type == NK_PANEL_CONTEXTUAL);
  19720. in = win->widgets_disabled ? 0 : &ctx->input;
  19721. if (in) {
  19722. is_clicked = nk_input_mouse_clicked(in, NK_BUTTON_RIGHT, trigger_bounds);
  19723. if (win->popup.active_con && win->popup.con_count != win->popup.active_con)
  19724. return 0;
  19725. if (!is_open && win->popup.active_con)
  19726. win->popup.active_con = 0;
  19727. if ((!is_open && !is_clicked))
  19728. return 0;
  19729. /* calculate contextual position on click */
  19730. win->popup.active_con = win->popup.con_count;
  19731. if (is_clicked) {
  19732. body.x = in->mouse.pos.x;
  19733. body.y = in->mouse.pos.y;
  19734. } else {
  19735. body.x = popup->bounds.x;
  19736. body.y = popup->bounds.y;
  19737. }
  19738. body.w = size.x;
  19739. body.h = size.y;
  19740. /* start nonblocking contextual popup */
  19741. ret = nk_nonblock_begin(ctx, flags | NK_WINDOW_NO_SCROLLBAR, body,
  19742. null_rect, NK_PANEL_CONTEXTUAL);
  19743. if (ret) win->popup.type = NK_PANEL_CONTEXTUAL;
  19744. else {
  19745. win->popup.active_con = 0;
  19746. win->popup.type = NK_PANEL_NONE;
  19747. if (win->popup.win)
  19748. win->popup.win->flags = 0;
  19749. }
  19750. }
  19751. return ret;
  19752. }
  19753. NK_API nk_bool
  19754. nk_contextual_item_text(struct nk_context *ctx, const char *text, int len,
  19755. nk_flags alignment)
  19756. {
  19757. struct nk_window *win;
  19758. const struct nk_input *in;
  19759. const struct nk_style *style;
  19760. struct nk_rect bounds;
  19761. enum nk_widget_layout_states state;
  19762. NK_ASSERT(ctx);
  19763. NK_ASSERT(ctx->current);
  19764. NK_ASSERT(ctx->current->layout);
  19765. if (!ctx || !ctx->current || !ctx->current->layout)
  19766. return 0;
  19767. win = ctx->current;
  19768. style = &ctx->style;
  19769. state = nk_widget_fitting(&bounds, ctx, style->contextual_button.padding);
  19770. if (!state) return nk_false;
  19771. in = (state == NK_WIDGET_ROM || win->layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  19772. if (nk_do_button_text(&ctx->last_widget_state, &win->buffer, bounds,
  19773. text, len, alignment, NK_BUTTON_DEFAULT, &style->contextual_button, in, style->font)) {
  19774. nk_contextual_close(ctx);
  19775. return nk_true;
  19776. }
  19777. return nk_false;
  19778. }
  19779. NK_API nk_bool
  19780. nk_contextual_item_label(struct nk_context *ctx, const char *label, nk_flags align)
  19781. {
  19782. return nk_contextual_item_text(ctx, label, nk_strlen(label), align);
  19783. }
  19784. NK_API nk_bool
  19785. nk_contextual_item_image_text(struct nk_context *ctx, struct nk_image img,
  19786. const char *text, int len, nk_flags align)
  19787. {
  19788. struct nk_window *win;
  19789. const struct nk_input *in;
  19790. const struct nk_style *style;
  19791. struct nk_rect bounds;
  19792. enum nk_widget_layout_states state;
  19793. NK_ASSERT(ctx);
  19794. NK_ASSERT(ctx->current);
  19795. NK_ASSERT(ctx->current->layout);
  19796. if (!ctx || !ctx->current || !ctx->current->layout)
  19797. return 0;
  19798. win = ctx->current;
  19799. style = &ctx->style;
  19800. state = nk_widget_fitting(&bounds, ctx, style->contextual_button.padding);
  19801. if (!state) return nk_false;
  19802. in = (state == NK_WIDGET_ROM || win->layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  19803. if (nk_do_button_text_image(&ctx->last_widget_state, &win->buffer, bounds,
  19804. img, text, len, align, NK_BUTTON_DEFAULT, &style->contextual_button, style->font, in)){
  19805. nk_contextual_close(ctx);
  19806. return nk_true;
  19807. }
  19808. return nk_false;
  19809. }
  19810. NK_API nk_bool
  19811. nk_contextual_item_image_label(struct nk_context *ctx, struct nk_image img,
  19812. const char *label, nk_flags align)
  19813. {
  19814. return nk_contextual_item_image_text(ctx, img, label, nk_strlen(label), align);
  19815. }
  19816. NK_API nk_bool
  19817. nk_contextual_item_symbol_text(struct nk_context *ctx, enum nk_symbol_type symbol,
  19818. const char *text, int len, nk_flags align)
  19819. {
  19820. struct nk_window *win;
  19821. const struct nk_input *in;
  19822. const struct nk_style *style;
  19823. struct nk_rect bounds;
  19824. enum nk_widget_layout_states state;
  19825. NK_ASSERT(ctx);
  19826. NK_ASSERT(ctx->current);
  19827. NK_ASSERT(ctx->current->layout);
  19828. if (!ctx || !ctx->current || !ctx->current->layout)
  19829. return 0;
  19830. win = ctx->current;
  19831. style = &ctx->style;
  19832. state = nk_widget_fitting(&bounds, ctx, style->contextual_button.padding);
  19833. if (!state) return nk_false;
  19834. in = (state == NK_WIDGET_ROM || win->layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  19835. if (nk_do_button_text_symbol(&ctx->last_widget_state, &win->buffer, bounds,
  19836. symbol, text, len, align, NK_BUTTON_DEFAULT, &style->contextual_button, style->font, in)) {
  19837. nk_contextual_close(ctx);
  19838. return nk_true;
  19839. }
  19840. return nk_false;
  19841. }
  19842. NK_API nk_bool
  19843. nk_contextual_item_symbol_label(struct nk_context *ctx, enum nk_symbol_type symbol,
  19844. const char *text, nk_flags align)
  19845. {
  19846. return nk_contextual_item_symbol_text(ctx, symbol, text, nk_strlen(text), align);
  19847. }
  19848. NK_API void
  19849. nk_contextual_close(struct nk_context *ctx)
  19850. {
  19851. NK_ASSERT(ctx);
  19852. NK_ASSERT(ctx->current);
  19853. NK_ASSERT(ctx->current->layout);
  19854. if (!ctx || !ctx->current || !ctx->current->layout) return;
  19855. nk_popup_close(ctx);
  19856. }
  19857. NK_API void
  19858. nk_contextual_end(struct nk_context *ctx)
  19859. {
  19860. struct nk_window *popup;
  19861. struct nk_panel *panel;
  19862. NK_ASSERT(ctx);
  19863. NK_ASSERT(ctx->current);
  19864. if (!ctx || !ctx->current) return;
  19865. popup = ctx->current;
  19866. panel = popup->layout;
  19867. NK_ASSERT(popup->parent);
  19868. NK_ASSERT((int)panel->type & (int)NK_PANEL_SET_POPUP);
  19869. if (panel->flags & NK_WINDOW_DYNAMIC) {
  19870. /* Close behavior
  19871. This is a bit of a hack solution since we do not know before we end our popup
  19872. how big it will be. We therefore do not directly know when a
  19873. click outside the non-blocking popup must close it at that direct frame.
  19874. Instead it will be closed in the next frame.*/
  19875. struct nk_rect body = {0,0,0,0};
  19876. if (panel->at_y < (panel->bounds.y + panel->bounds.h)) {
  19877. struct nk_vec2 padding = nk_panel_get_padding(&ctx->style, panel->type);
  19878. body = panel->bounds;
  19879. body.y = (panel->at_y + panel->footer_height + panel->border + padding.y + panel->row.height);
  19880. body.h = (panel->bounds.y + panel->bounds.h) - body.y;
  19881. }
  19882. {int pressed = nk_input_is_mouse_pressed(&ctx->input, NK_BUTTON_LEFT);
  19883. int in_body = nk_input_is_mouse_hovering_rect(&ctx->input, body);
  19884. if (pressed && in_body)
  19885. popup->flags |= NK_WINDOW_HIDDEN;
  19886. }
  19887. }
  19888. if (popup->flags & NK_WINDOW_HIDDEN)
  19889. popup->seq = 0;
  19890. nk_popup_end(ctx);
  19891. return;
  19892. }
  19893. /* ===============================================================
  19894. *
  19895. * MENU
  19896. *
  19897. * ===============================================================*/
  19898. NK_API void
  19899. nk_menubar_begin(struct nk_context *ctx)
  19900. {
  19901. struct nk_panel *layout;
  19902. NK_ASSERT(ctx);
  19903. NK_ASSERT(ctx->current);
  19904. NK_ASSERT(ctx->current->layout);
  19905. if (!ctx || !ctx->current || !ctx->current->layout)
  19906. return;
  19907. layout = ctx->current->layout;
  19908. NK_ASSERT(layout->at_y == layout->bounds.y);
  19909. /* if this assert triggers you allocated space between nk_begin and nk_menubar_begin.
  19910. If you want a menubar the first nuklear function after `nk_begin` has to be a
  19911. `nk_menubar_begin` call. Inside the menubar you then have to allocate space for
  19912. widgets (also supports multiple rows).
  19913. Example:
  19914. if (nk_begin(...)) {
  19915. nk_menubar_begin(...);
  19916. nk_layout_xxxx(...);
  19917. nk_button(...);
  19918. nk_layout_xxxx(...);
  19919. nk_button(...);
  19920. nk_menubar_end(...);
  19921. }
  19922. nk_end(...);
  19923. */
  19924. if (layout->flags & NK_WINDOW_HIDDEN || layout->flags & NK_WINDOW_MINIMIZED)
  19925. return;
  19926. layout->menu.x = layout->at_x;
  19927. layout->menu.y = layout->at_y + layout->row.height;
  19928. layout->menu.w = layout->bounds.w;
  19929. layout->menu.offset.x = *layout->offset_x;
  19930. layout->menu.offset.y = *layout->offset_y;
  19931. *layout->offset_y = 0;
  19932. }
  19933. NK_API void
  19934. nk_menubar_end(struct nk_context *ctx)
  19935. {
  19936. struct nk_window *win;
  19937. struct nk_panel *layout;
  19938. struct nk_command_buffer *out;
  19939. NK_ASSERT(ctx);
  19940. NK_ASSERT(ctx->current);
  19941. NK_ASSERT(ctx->current->layout);
  19942. if (!ctx || !ctx->current || !ctx->current->layout)
  19943. return;
  19944. win = ctx->current;
  19945. out = &win->buffer;
  19946. layout = win->layout;
  19947. if (layout->flags & NK_WINDOW_HIDDEN || layout->flags & NK_WINDOW_MINIMIZED)
  19948. return;
  19949. layout->menu.h = layout->at_y - layout->menu.y;
  19950. layout->menu.h += layout->row.height + ctx->style.window.spacing.y;
  19951. layout->bounds.y += layout->menu.h;
  19952. layout->bounds.h -= layout->menu.h;
  19953. *layout->offset_x = layout->menu.offset.x;
  19954. *layout->offset_y = layout->menu.offset.y;
  19955. layout->at_y = layout->bounds.y - layout->row.height;
  19956. layout->clip.y = layout->bounds.y;
  19957. layout->clip.h = layout->bounds.h;
  19958. nk_push_scissor(out, layout->clip);
  19959. }
  19960. NK_INTERN int
  19961. nk_menu_begin(struct nk_context *ctx, struct nk_window *win,
  19962. const char *id, int is_clicked, struct nk_rect header, struct nk_vec2 size)
  19963. {
  19964. int is_open = 0;
  19965. int is_active = 0;
  19966. struct nk_rect body;
  19967. struct nk_window *popup;
  19968. nk_hash hash = nk_murmur_hash(id, (int)nk_strlen(id), NK_PANEL_MENU);
  19969. NK_ASSERT(ctx);
  19970. NK_ASSERT(ctx->current);
  19971. NK_ASSERT(ctx->current->layout);
  19972. if (!ctx || !ctx->current || !ctx->current->layout)
  19973. return 0;
  19974. body.x = header.x;
  19975. body.w = size.x;
  19976. body.y = header.y + header.h;
  19977. body.h = size.y;
  19978. popup = win->popup.win;
  19979. is_open = popup ? nk_true : nk_false;
  19980. is_active = (popup && (win->popup.name == hash) && win->popup.type == NK_PANEL_MENU);
  19981. if ((is_clicked && is_open && !is_active) || (is_open && !is_active) ||
  19982. (!is_open && !is_active && !is_clicked)) return 0;
  19983. if (!nk_nonblock_begin(ctx, NK_WINDOW_NO_SCROLLBAR, body, header, NK_PANEL_MENU))
  19984. return 0;
  19985. win->popup.type = NK_PANEL_MENU;
  19986. win->popup.name = hash;
  19987. return 1;
  19988. }
  19989. NK_API nk_bool
  19990. nk_menu_begin_text(struct nk_context *ctx, const char *title, int len,
  19991. nk_flags align, struct nk_vec2 size)
  19992. {
  19993. struct nk_window *win;
  19994. const struct nk_input *in;
  19995. struct nk_rect header;
  19996. int is_clicked = nk_false;
  19997. nk_flags state;
  19998. NK_ASSERT(ctx);
  19999. NK_ASSERT(ctx->current);
  20000. NK_ASSERT(ctx->current->layout);
  20001. if (!ctx || !ctx->current || !ctx->current->layout)
  20002. return 0;
  20003. win = ctx->current;
  20004. state = nk_widget(&header, ctx);
  20005. if (!state) return 0;
  20006. in = (state == NK_WIDGET_ROM || state == NK_WIDGET_DISABLED || win->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  20007. if (nk_do_button_text(&ctx->last_widget_state, &win->buffer, header,
  20008. title, len, align, NK_BUTTON_DEFAULT, &ctx->style.menu_button, in, ctx->style.font))
  20009. is_clicked = nk_true;
  20010. return nk_menu_begin(ctx, win, title, is_clicked, header, size);
  20011. }
  20012. NK_API nk_bool nk_menu_begin_label(struct nk_context *ctx,
  20013. const char *text, nk_flags align, struct nk_vec2 size)
  20014. {
  20015. return nk_menu_begin_text(ctx, text, nk_strlen(text), align, size);
  20016. }
  20017. NK_API nk_bool
  20018. nk_menu_begin_image(struct nk_context *ctx, const char *id, struct nk_image img,
  20019. struct nk_vec2 size)
  20020. {
  20021. struct nk_window *win;
  20022. struct nk_rect header;
  20023. const struct nk_input *in;
  20024. int is_clicked = nk_false;
  20025. nk_flags state;
  20026. NK_ASSERT(ctx);
  20027. NK_ASSERT(ctx->current);
  20028. NK_ASSERT(ctx->current->layout);
  20029. if (!ctx || !ctx->current || !ctx->current->layout)
  20030. return 0;
  20031. win = ctx->current;
  20032. state = nk_widget(&header, ctx);
  20033. if (!state) return 0;
  20034. in = (state == NK_WIDGET_ROM || state == NK_WIDGET_DISABLED || win->layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  20035. if (nk_do_button_image(&ctx->last_widget_state, &win->buffer, header,
  20036. img, NK_BUTTON_DEFAULT, &ctx->style.menu_button, in))
  20037. is_clicked = nk_true;
  20038. return nk_menu_begin(ctx, win, id, is_clicked, header, size);
  20039. }
  20040. NK_API nk_bool
  20041. nk_menu_begin_symbol(struct nk_context *ctx, const char *id,
  20042. enum nk_symbol_type sym, struct nk_vec2 size)
  20043. {
  20044. struct nk_window *win;
  20045. const struct nk_input *in;
  20046. struct nk_rect header;
  20047. int is_clicked = nk_false;
  20048. nk_flags state;
  20049. NK_ASSERT(ctx);
  20050. NK_ASSERT(ctx->current);
  20051. NK_ASSERT(ctx->current->layout);
  20052. if (!ctx || !ctx->current || !ctx->current->layout)
  20053. return 0;
  20054. win = ctx->current;
  20055. state = nk_widget(&header, ctx);
  20056. if (!state) return 0;
  20057. in = (state == NK_WIDGET_ROM || state == NK_WIDGET_DISABLED || win->layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  20058. if (nk_do_button_symbol(&ctx->last_widget_state, &win->buffer, header,
  20059. sym, NK_BUTTON_DEFAULT, &ctx->style.menu_button, in, ctx->style.font))
  20060. is_clicked = nk_true;
  20061. return nk_menu_begin(ctx, win, id, is_clicked, header, size);
  20062. }
  20063. NK_API nk_bool
  20064. nk_menu_begin_image_text(struct nk_context *ctx, const char *title, int len,
  20065. nk_flags align, struct nk_image img, struct nk_vec2 size)
  20066. {
  20067. struct nk_window *win;
  20068. struct nk_rect header;
  20069. const struct nk_input *in;
  20070. int is_clicked = nk_false;
  20071. nk_flags state;
  20072. NK_ASSERT(ctx);
  20073. NK_ASSERT(ctx->current);
  20074. NK_ASSERT(ctx->current->layout);
  20075. if (!ctx || !ctx->current || !ctx->current->layout)
  20076. return 0;
  20077. win = ctx->current;
  20078. state = nk_widget(&header, ctx);
  20079. if (!state) return 0;
  20080. in = (state == NK_WIDGET_ROM || state == NK_WIDGET_DISABLED || win->layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  20081. if (nk_do_button_text_image(&ctx->last_widget_state, &win->buffer,
  20082. header, img, title, len, align, NK_BUTTON_DEFAULT, &ctx->style.menu_button,
  20083. ctx->style.font, in))
  20084. is_clicked = nk_true;
  20085. return nk_menu_begin(ctx, win, title, is_clicked, header, size);
  20086. }
  20087. NK_API nk_bool
  20088. nk_menu_begin_image_label(struct nk_context *ctx,
  20089. const char *title, nk_flags align, struct nk_image img, struct nk_vec2 size)
  20090. {
  20091. return nk_menu_begin_image_text(ctx, title, nk_strlen(title), align, img, size);
  20092. }
  20093. NK_API nk_bool
  20094. nk_menu_begin_symbol_text(struct nk_context *ctx, const char *title, int len,
  20095. nk_flags align, enum nk_symbol_type sym, struct nk_vec2 size)
  20096. {
  20097. struct nk_window *win;
  20098. struct nk_rect header;
  20099. const struct nk_input *in;
  20100. int is_clicked = nk_false;
  20101. nk_flags state;
  20102. NK_ASSERT(ctx);
  20103. NK_ASSERT(ctx->current);
  20104. NK_ASSERT(ctx->current->layout);
  20105. if (!ctx || !ctx->current || !ctx->current->layout)
  20106. return 0;
  20107. win = ctx->current;
  20108. state = nk_widget(&header, ctx);
  20109. if (!state) return 0;
  20110. in = (state == NK_WIDGET_ROM || state == NK_WIDGET_DISABLED || win->layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  20111. if (nk_do_button_text_symbol(&ctx->last_widget_state, &win->buffer,
  20112. header, sym, title, len, align, NK_BUTTON_DEFAULT, &ctx->style.menu_button,
  20113. ctx->style.font, in)) is_clicked = nk_true;
  20114. return nk_menu_begin(ctx, win, title, is_clicked, header, size);
  20115. }
  20116. NK_API nk_bool
  20117. nk_menu_begin_symbol_label(struct nk_context *ctx,
  20118. const char *title, nk_flags align, enum nk_symbol_type sym, struct nk_vec2 size )
  20119. {
  20120. return nk_menu_begin_symbol_text(ctx, title, nk_strlen(title), align,sym,size);
  20121. }
  20122. NK_API nk_bool
  20123. nk_menu_item_text(struct nk_context *ctx, const char *title, int len, nk_flags align)
  20124. {
  20125. return nk_contextual_item_text(ctx, title, len, align);
  20126. }
  20127. NK_API nk_bool
  20128. nk_menu_item_label(struct nk_context *ctx, const char *label, nk_flags align)
  20129. {
  20130. return nk_contextual_item_label(ctx, label, align);
  20131. }
  20132. NK_API nk_bool
  20133. nk_menu_item_image_label(struct nk_context *ctx, struct nk_image img,
  20134. const char *label, nk_flags align)
  20135. {
  20136. return nk_contextual_item_image_label(ctx, img, label, align);
  20137. }
  20138. NK_API nk_bool
  20139. nk_menu_item_image_text(struct nk_context *ctx, struct nk_image img,
  20140. const char *text, int len, nk_flags align)
  20141. {
  20142. return nk_contextual_item_image_text(ctx, img, text, len, align);
  20143. }
  20144. NK_API nk_bool nk_menu_item_symbol_text(struct nk_context *ctx, enum nk_symbol_type sym,
  20145. const char *text, int len, nk_flags align)
  20146. {
  20147. return nk_contextual_item_symbol_text(ctx, sym, text, len, align);
  20148. }
  20149. NK_API nk_bool nk_menu_item_symbol_label(struct nk_context *ctx, enum nk_symbol_type sym,
  20150. const char *label, nk_flags align)
  20151. {
  20152. return nk_contextual_item_symbol_label(ctx, sym, label, align);
  20153. }
  20154. NK_API void nk_menu_close(struct nk_context *ctx)
  20155. {
  20156. nk_contextual_close(ctx);
  20157. }
  20158. NK_API void
  20159. nk_menu_end(struct nk_context *ctx)
  20160. {
  20161. nk_contextual_end(ctx);
  20162. }
  20163. /* ===============================================================
  20164. *
  20165. * LAYOUT
  20166. *
  20167. * ===============================================================*/
  20168. NK_API void
  20169. nk_layout_set_min_row_height(struct nk_context *ctx, float height)
  20170. {
  20171. struct nk_window *win;
  20172. struct nk_panel *layout;
  20173. NK_ASSERT(ctx);
  20174. NK_ASSERT(ctx->current);
  20175. NK_ASSERT(ctx->current->layout);
  20176. if (!ctx || !ctx->current || !ctx->current->layout)
  20177. return;
  20178. win = ctx->current;
  20179. layout = win->layout;
  20180. layout->row.min_height = height;
  20181. }
  20182. NK_API void
  20183. nk_layout_reset_min_row_height(struct nk_context *ctx)
  20184. {
  20185. struct nk_window *win;
  20186. struct nk_panel *layout;
  20187. NK_ASSERT(ctx);
  20188. NK_ASSERT(ctx->current);
  20189. NK_ASSERT(ctx->current->layout);
  20190. if (!ctx || !ctx->current || !ctx->current->layout)
  20191. return;
  20192. win = ctx->current;
  20193. layout = win->layout;
  20194. layout->row.min_height = ctx->style.font->height;
  20195. layout->row.min_height += ctx->style.text.padding.y*2;
  20196. layout->row.min_height += ctx->style.window.min_row_height_padding*2;
  20197. }
  20198. NK_LIB float
  20199. nk_layout_row_calculate_usable_space(const struct nk_style *style, enum nk_panel_type type,
  20200. float total_space, int columns)
  20201. {
  20202. float panel_spacing;
  20203. float panel_space;
  20204. struct nk_vec2 spacing;
  20205. NK_UNUSED(type);
  20206. spacing = style->window.spacing;
  20207. /* calculate the usable panel space */
  20208. panel_spacing = (float)NK_MAX(columns - 1, 0) * spacing.x;
  20209. panel_space = total_space - panel_spacing;
  20210. return panel_space;
  20211. }
  20212. NK_LIB void
  20213. nk_panel_layout(const struct nk_context *ctx, struct nk_window *win,
  20214. float height, int cols)
  20215. {
  20216. struct nk_panel *layout;
  20217. const struct nk_style *style;
  20218. struct nk_command_buffer *out;
  20219. struct nk_vec2 item_spacing;
  20220. struct nk_color color;
  20221. NK_ASSERT(ctx);
  20222. NK_ASSERT(ctx->current);
  20223. NK_ASSERT(ctx->current->layout);
  20224. if (!ctx || !ctx->current || !ctx->current->layout)
  20225. return;
  20226. /* prefetch some configuration data */
  20227. layout = win->layout;
  20228. style = &ctx->style;
  20229. out = &win->buffer;
  20230. color = style->window.background;
  20231. item_spacing = style->window.spacing;
  20232. /* if one of these triggers you forgot to add an `if` condition around either
  20233. a window, group, popup, combobox or contextual menu `begin` and `end` block.
  20234. Example:
  20235. if (nk_begin(...) {...} nk_end(...); or
  20236. if (nk_group_begin(...) { nk_group_end(...);} */
  20237. NK_ASSERT(!(layout->flags & NK_WINDOW_MINIMIZED));
  20238. NK_ASSERT(!(layout->flags & NK_WINDOW_HIDDEN));
  20239. NK_ASSERT(!(layout->flags & NK_WINDOW_CLOSED));
  20240. /* update the current row and set the current row layout */
  20241. layout->row.index = 0;
  20242. layout->at_y += layout->row.height;
  20243. layout->row.columns = cols;
  20244. if (height == 0.0f)
  20245. layout->row.height = NK_MAX(height, layout->row.min_height) + item_spacing.y;
  20246. else layout->row.height = height + item_spacing.y;
  20247. layout->row.item_offset = 0;
  20248. if (layout->flags & NK_WINDOW_DYNAMIC) {
  20249. /* draw background for dynamic panels */
  20250. struct nk_rect background;
  20251. background.x = win->bounds.x;
  20252. background.w = win->bounds.w;
  20253. background.y = layout->at_y - 1.0f;
  20254. background.h = layout->row.height + 1.0f;
  20255. nk_fill_rect(out, background, 0, color);
  20256. }
  20257. }
  20258. NK_LIB void
  20259. nk_row_layout(struct nk_context *ctx, enum nk_layout_format fmt,
  20260. float height, int cols, int width)
  20261. {
  20262. /* update the current row and set the current row layout */
  20263. struct nk_window *win;
  20264. NK_ASSERT(ctx);
  20265. NK_ASSERT(ctx->current);
  20266. NK_ASSERT(ctx->current->layout);
  20267. if (!ctx || !ctx->current || !ctx->current->layout)
  20268. return;
  20269. win = ctx->current;
  20270. nk_panel_layout(ctx, win, height, cols);
  20271. if (fmt == NK_DYNAMIC)
  20272. win->layout->row.type = NK_LAYOUT_DYNAMIC_FIXED;
  20273. else win->layout->row.type = NK_LAYOUT_STATIC_FIXED;
  20274. win->layout->row.ratio = 0;
  20275. win->layout->row.filled = 0;
  20276. win->layout->row.item_offset = 0;
  20277. win->layout->row.item_width = (float)width;
  20278. }
  20279. NK_API float
  20280. nk_layout_ratio_from_pixel(const struct nk_context *ctx, float pixel_width)
  20281. {
  20282. struct nk_window *win;
  20283. NK_ASSERT(ctx);
  20284. NK_ASSERT(pixel_width);
  20285. if (!ctx || !ctx->current || !ctx->current->layout) return 0;
  20286. win = ctx->current;
  20287. return NK_CLAMP(0.0f, pixel_width/win->bounds.x, 1.0f);
  20288. }
  20289. NK_API void
  20290. nk_layout_row_dynamic(struct nk_context *ctx, float height, int cols)
  20291. {
  20292. nk_row_layout(ctx, NK_DYNAMIC, height, cols, 0);
  20293. }
  20294. NK_API void
  20295. nk_layout_row_static(struct nk_context *ctx, float height, int item_width, int cols)
  20296. {
  20297. nk_row_layout(ctx, NK_STATIC, height, cols, item_width);
  20298. }
  20299. NK_API void
  20300. nk_layout_row_begin(struct nk_context *ctx, enum nk_layout_format fmt,
  20301. float row_height, int cols)
  20302. {
  20303. struct nk_window *win;
  20304. struct nk_panel *layout;
  20305. NK_ASSERT(ctx);
  20306. NK_ASSERT(ctx->current);
  20307. NK_ASSERT(ctx->current->layout);
  20308. if (!ctx || !ctx->current || !ctx->current->layout)
  20309. return;
  20310. win = ctx->current;
  20311. layout = win->layout;
  20312. nk_panel_layout(ctx, win, row_height, cols);
  20313. if (fmt == NK_DYNAMIC)
  20314. layout->row.type = NK_LAYOUT_DYNAMIC_ROW;
  20315. else layout->row.type = NK_LAYOUT_STATIC_ROW;
  20316. layout->row.ratio = 0;
  20317. layout->row.filled = 0;
  20318. layout->row.item_width = 0;
  20319. layout->row.item_offset = 0;
  20320. layout->row.columns = cols;
  20321. }
  20322. NK_API void
  20323. nk_layout_row_push(struct nk_context *ctx, float ratio_or_width)
  20324. {
  20325. struct nk_window *win;
  20326. struct nk_panel *layout;
  20327. NK_ASSERT(ctx);
  20328. NK_ASSERT(ctx->current);
  20329. NK_ASSERT(ctx->current->layout);
  20330. if (!ctx || !ctx->current || !ctx->current->layout)
  20331. return;
  20332. win = ctx->current;
  20333. layout = win->layout;
  20334. NK_ASSERT(layout->row.type == NK_LAYOUT_STATIC_ROW || layout->row.type == NK_LAYOUT_DYNAMIC_ROW);
  20335. if (layout->row.type != NK_LAYOUT_STATIC_ROW && layout->row.type != NK_LAYOUT_DYNAMIC_ROW)
  20336. return;
  20337. if (layout->row.type == NK_LAYOUT_DYNAMIC_ROW) {
  20338. float ratio = ratio_or_width;
  20339. if ((ratio + layout->row.filled) > 1.0f) return;
  20340. if (ratio > 0.0f)
  20341. layout->row.item_width = NK_SATURATE(ratio);
  20342. else layout->row.item_width = 1.0f - layout->row.filled;
  20343. } else layout->row.item_width = ratio_or_width;
  20344. }
  20345. NK_API void
  20346. nk_layout_row_end(struct nk_context *ctx)
  20347. {
  20348. struct nk_window *win;
  20349. struct nk_panel *layout;
  20350. NK_ASSERT(ctx);
  20351. NK_ASSERT(ctx->current);
  20352. NK_ASSERT(ctx->current->layout);
  20353. if (!ctx || !ctx->current || !ctx->current->layout)
  20354. return;
  20355. win = ctx->current;
  20356. layout = win->layout;
  20357. NK_ASSERT(layout->row.type == NK_LAYOUT_STATIC_ROW || layout->row.type == NK_LAYOUT_DYNAMIC_ROW);
  20358. if (layout->row.type != NK_LAYOUT_STATIC_ROW && layout->row.type != NK_LAYOUT_DYNAMIC_ROW)
  20359. return;
  20360. layout->row.item_width = 0;
  20361. layout->row.item_offset = 0;
  20362. }
  20363. NK_API void
  20364. nk_layout_row(struct nk_context *ctx, enum nk_layout_format fmt,
  20365. float height, int cols, const float *ratio)
  20366. {
  20367. int i;
  20368. int n_undef = 0;
  20369. struct nk_window *win;
  20370. struct nk_panel *layout;
  20371. NK_ASSERT(ctx);
  20372. NK_ASSERT(ctx->current);
  20373. NK_ASSERT(ctx->current->layout);
  20374. if (!ctx || !ctx->current || !ctx->current->layout)
  20375. return;
  20376. win = ctx->current;
  20377. layout = win->layout;
  20378. nk_panel_layout(ctx, win, height, cols);
  20379. if (fmt == NK_DYNAMIC) {
  20380. /* calculate width of undefined widget ratios */
  20381. float r = 0;
  20382. layout->row.ratio = ratio;
  20383. for (i = 0; i < cols; ++i) {
  20384. if (ratio[i] < 0.0f)
  20385. n_undef++;
  20386. else r += ratio[i];
  20387. }
  20388. r = NK_SATURATE(1.0f - r);
  20389. layout->row.type = NK_LAYOUT_DYNAMIC;
  20390. layout->row.item_width = (r > 0 && n_undef > 0) ? (r / (float)n_undef):0;
  20391. } else {
  20392. layout->row.ratio = ratio;
  20393. layout->row.type = NK_LAYOUT_STATIC;
  20394. layout->row.item_width = 0;
  20395. layout->row.item_offset = 0;
  20396. }
  20397. layout->row.item_offset = 0;
  20398. layout->row.filled = 0;
  20399. }
  20400. NK_API void
  20401. nk_layout_row_template_begin(struct nk_context *ctx, float height)
  20402. {
  20403. struct nk_window *win;
  20404. struct nk_panel *layout;
  20405. NK_ASSERT(ctx);
  20406. NK_ASSERT(ctx->current);
  20407. NK_ASSERT(ctx->current->layout);
  20408. if (!ctx || !ctx->current || !ctx->current->layout)
  20409. return;
  20410. win = ctx->current;
  20411. layout = win->layout;
  20412. nk_panel_layout(ctx, win, height, 1);
  20413. layout->row.type = NK_LAYOUT_TEMPLATE;
  20414. layout->row.columns = 0;
  20415. layout->row.ratio = 0;
  20416. layout->row.item_width = 0;
  20417. layout->row.item_height = 0;
  20418. layout->row.item_offset = 0;
  20419. layout->row.filled = 0;
  20420. layout->row.item.x = 0;
  20421. layout->row.item.y = 0;
  20422. layout->row.item.w = 0;
  20423. layout->row.item.h = 0;
  20424. }
  20425. NK_API void
  20426. nk_layout_row_template_push_dynamic(struct nk_context *ctx)
  20427. {
  20428. struct nk_window *win;
  20429. struct nk_panel *layout;
  20430. NK_ASSERT(ctx);
  20431. NK_ASSERT(ctx->current);
  20432. NK_ASSERT(ctx->current->layout);
  20433. if (!ctx || !ctx->current || !ctx->current->layout)
  20434. return;
  20435. win = ctx->current;
  20436. layout = win->layout;
  20437. NK_ASSERT(layout->row.type == NK_LAYOUT_TEMPLATE);
  20438. NK_ASSERT(layout->row.columns < NK_MAX_LAYOUT_ROW_TEMPLATE_COLUMNS);
  20439. if (layout->row.type != NK_LAYOUT_TEMPLATE) return;
  20440. if (layout->row.columns >= NK_MAX_LAYOUT_ROW_TEMPLATE_COLUMNS) return;
  20441. layout->row.templates[layout->row.columns++] = -1.0f;
  20442. }
  20443. NK_API void
  20444. nk_layout_row_template_push_variable(struct nk_context *ctx, float min_width)
  20445. {
  20446. struct nk_window *win;
  20447. struct nk_panel *layout;
  20448. NK_ASSERT(ctx);
  20449. NK_ASSERT(ctx->current);
  20450. NK_ASSERT(ctx->current->layout);
  20451. if (!ctx || !ctx->current || !ctx->current->layout)
  20452. return;
  20453. win = ctx->current;
  20454. layout = win->layout;
  20455. NK_ASSERT(layout->row.type == NK_LAYOUT_TEMPLATE);
  20456. NK_ASSERT(layout->row.columns < NK_MAX_LAYOUT_ROW_TEMPLATE_COLUMNS);
  20457. if (layout->row.type != NK_LAYOUT_TEMPLATE) return;
  20458. if (layout->row.columns >= NK_MAX_LAYOUT_ROW_TEMPLATE_COLUMNS) return;
  20459. layout->row.templates[layout->row.columns++] = -min_width;
  20460. }
  20461. NK_API void
  20462. nk_layout_row_template_push_static(struct nk_context *ctx, float width)
  20463. {
  20464. struct nk_window *win;
  20465. struct nk_panel *layout;
  20466. NK_ASSERT(ctx);
  20467. NK_ASSERT(ctx->current);
  20468. NK_ASSERT(ctx->current->layout);
  20469. if (!ctx || !ctx->current || !ctx->current->layout)
  20470. return;
  20471. win = ctx->current;
  20472. layout = win->layout;
  20473. NK_ASSERT(layout->row.type == NK_LAYOUT_TEMPLATE);
  20474. NK_ASSERT(layout->row.columns < NK_MAX_LAYOUT_ROW_TEMPLATE_COLUMNS);
  20475. if (layout->row.type != NK_LAYOUT_TEMPLATE) return;
  20476. if (layout->row.columns >= NK_MAX_LAYOUT_ROW_TEMPLATE_COLUMNS) return;
  20477. layout->row.templates[layout->row.columns++] = width;
  20478. }
  20479. NK_API void
  20480. nk_layout_row_template_end(struct nk_context *ctx)
  20481. {
  20482. struct nk_window *win;
  20483. struct nk_panel *layout;
  20484. int i = 0;
  20485. int variable_count = 0;
  20486. int min_variable_count = 0;
  20487. float min_fixed_width = 0.0f;
  20488. float total_fixed_width = 0.0f;
  20489. float max_variable_width = 0.0f;
  20490. NK_ASSERT(ctx);
  20491. NK_ASSERT(ctx->current);
  20492. NK_ASSERT(ctx->current->layout);
  20493. if (!ctx || !ctx->current || !ctx->current->layout)
  20494. return;
  20495. win = ctx->current;
  20496. layout = win->layout;
  20497. NK_ASSERT(layout->row.type == NK_LAYOUT_TEMPLATE);
  20498. if (layout->row.type != NK_LAYOUT_TEMPLATE) return;
  20499. for (i = 0; i < layout->row.columns; ++i) {
  20500. float width = layout->row.templates[i];
  20501. if (width >= 0.0f) {
  20502. total_fixed_width += width;
  20503. min_fixed_width += width;
  20504. } else if (width < -1.0f) {
  20505. width = -width;
  20506. total_fixed_width += width;
  20507. max_variable_width = NK_MAX(max_variable_width, width);
  20508. variable_count++;
  20509. } else {
  20510. min_variable_count++;
  20511. variable_count++;
  20512. }
  20513. }
  20514. if (variable_count) {
  20515. float space = nk_layout_row_calculate_usable_space(&ctx->style, layout->type,
  20516. layout->bounds.w, layout->row.columns);
  20517. float var_width = (NK_MAX(space-min_fixed_width,0.0f)) / (float)variable_count;
  20518. int enough_space = var_width >= max_variable_width;
  20519. if (!enough_space)
  20520. var_width = (NK_MAX(space-total_fixed_width,0)) / (float)min_variable_count;
  20521. for (i = 0; i < layout->row.columns; ++i) {
  20522. float *width = &layout->row.templates[i];
  20523. *width = (*width >= 0.0f)? *width: (*width < -1.0f && !enough_space)? -(*width): var_width;
  20524. }
  20525. }
  20526. }
  20527. NK_API void
  20528. nk_layout_space_begin(struct nk_context *ctx, enum nk_layout_format fmt,
  20529. float height, int widget_count)
  20530. {
  20531. struct nk_window *win;
  20532. struct nk_panel *layout;
  20533. NK_ASSERT(ctx);
  20534. NK_ASSERT(ctx->current);
  20535. NK_ASSERT(ctx->current->layout);
  20536. if (!ctx || !ctx->current || !ctx->current->layout)
  20537. return;
  20538. win = ctx->current;
  20539. layout = win->layout;
  20540. nk_panel_layout(ctx, win, height, widget_count);
  20541. if (fmt == NK_STATIC)
  20542. layout->row.type = NK_LAYOUT_STATIC_FREE;
  20543. else layout->row.type = NK_LAYOUT_DYNAMIC_FREE;
  20544. layout->row.ratio = 0;
  20545. layout->row.filled = 0;
  20546. layout->row.item_width = 0;
  20547. layout->row.item_offset = 0;
  20548. }
  20549. NK_API void
  20550. nk_layout_space_end(struct nk_context *ctx)
  20551. {
  20552. struct nk_window *win;
  20553. struct nk_panel *layout;
  20554. NK_ASSERT(ctx);
  20555. NK_ASSERT(ctx->current);
  20556. NK_ASSERT(ctx->current->layout);
  20557. if (!ctx || !ctx->current || !ctx->current->layout)
  20558. return;
  20559. win = ctx->current;
  20560. layout = win->layout;
  20561. layout->row.item_width = 0;
  20562. layout->row.item_height = 0;
  20563. layout->row.item_offset = 0;
  20564. nk_zero(&layout->row.item, sizeof(layout->row.item));
  20565. }
  20566. NK_API void
  20567. nk_layout_space_push(struct nk_context *ctx, struct nk_rect rect)
  20568. {
  20569. struct nk_window *win;
  20570. struct nk_panel *layout;
  20571. NK_ASSERT(ctx);
  20572. NK_ASSERT(ctx->current);
  20573. NK_ASSERT(ctx->current->layout);
  20574. if (!ctx || !ctx->current || !ctx->current->layout)
  20575. return;
  20576. win = ctx->current;
  20577. layout = win->layout;
  20578. layout->row.item = rect;
  20579. }
  20580. NK_API struct nk_rect
  20581. nk_layout_space_bounds(const struct nk_context *ctx)
  20582. {
  20583. struct nk_rect ret;
  20584. struct nk_window *win;
  20585. struct nk_panel *layout;
  20586. NK_ASSERT(ctx);
  20587. NK_ASSERT(ctx->current);
  20588. NK_ASSERT(ctx->current->layout);
  20589. win = ctx->current;
  20590. layout = win->layout;
  20591. ret.x = layout->clip.x;
  20592. ret.y = layout->clip.y;
  20593. ret.w = layout->clip.w;
  20594. ret.h = layout->row.height;
  20595. return ret;
  20596. }
  20597. NK_API struct nk_rect
  20598. nk_layout_widget_bounds(const struct nk_context *ctx)
  20599. {
  20600. struct nk_rect ret;
  20601. struct nk_window *win;
  20602. struct nk_panel *layout;
  20603. NK_ASSERT(ctx);
  20604. NK_ASSERT(ctx->current);
  20605. NK_ASSERT(ctx->current->layout);
  20606. win = ctx->current;
  20607. layout = win->layout;
  20608. ret.x = layout->at_x;
  20609. ret.y = layout->at_y;
  20610. ret.w = layout->bounds.w - NK_MAX(layout->at_x - layout->bounds.x,0);
  20611. ret.h = layout->row.height;
  20612. return ret;
  20613. }
  20614. NK_API struct nk_vec2
  20615. nk_layout_space_to_screen(const struct nk_context *ctx, struct nk_vec2 ret)
  20616. {
  20617. struct nk_window *win;
  20618. struct nk_panel *layout;
  20619. NK_ASSERT(ctx);
  20620. NK_ASSERT(ctx->current);
  20621. NK_ASSERT(ctx->current->layout);
  20622. win = ctx->current;
  20623. layout = win->layout;
  20624. ret.x += layout->at_x - (float)*layout->offset_x;
  20625. ret.y += layout->at_y - (float)*layout->offset_y;
  20626. return ret;
  20627. }
  20628. NK_API struct nk_vec2
  20629. nk_layout_space_to_local(const struct nk_context *ctx, struct nk_vec2 ret)
  20630. {
  20631. struct nk_window *win;
  20632. struct nk_panel *layout;
  20633. NK_ASSERT(ctx);
  20634. NK_ASSERT(ctx->current);
  20635. NK_ASSERT(ctx->current->layout);
  20636. win = ctx->current;
  20637. layout = win->layout;
  20638. ret.x += -layout->at_x + (float)*layout->offset_x;
  20639. ret.y += -layout->at_y + (float)*layout->offset_y;
  20640. return ret;
  20641. }
  20642. NK_API struct nk_rect
  20643. nk_layout_space_rect_to_screen(const struct nk_context *ctx, struct nk_rect ret)
  20644. {
  20645. struct nk_window *win;
  20646. struct nk_panel *layout;
  20647. NK_ASSERT(ctx);
  20648. NK_ASSERT(ctx->current);
  20649. NK_ASSERT(ctx->current->layout);
  20650. win = ctx->current;
  20651. layout = win->layout;
  20652. ret.x += layout->at_x - (float)*layout->offset_x;
  20653. ret.y += layout->at_y - (float)*layout->offset_y;
  20654. return ret;
  20655. }
  20656. NK_API struct nk_rect
  20657. nk_layout_space_rect_to_local(const struct nk_context *ctx, struct nk_rect ret)
  20658. {
  20659. struct nk_window *win;
  20660. struct nk_panel *layout;
  20661. NK_ASSERT(ctx);
  20662. NK_ASSERT(ctx->current);
  20663. NK_ASSERT(ctx->current->layout);
  20664. win = ctx->current;
  20665. layout = win->layout;
  20666. ret.x += -layout->at_x + (float)*layout->offset_x;
  20667. ret.y += -layout->at_y + (float)*layout->offset_y;
  20668. return ret;
  20669. }
  20670. NK_LIB void
  20671. nk_panel_alloc_row(const struct nk_context *ctx, struct nk_window *win)
  20672. {
  20673. struct nk_panel *layout = win->layout;
  20674. struct nk_vec2 spacing = ctx->style.window.spacing;
  20675. const float row_height = layout->row.height - spacing.y;
  20676. nk_panel_layout(ctx, win, row_height, layout->row.columns);
  20677. }
  20678. NK_LIB void
  20679. nk_layout_widget_space(struct nk_rect *bounds, const struct nk_context *ctx,
  20680. struct nk_window *win, int modify)
  20681. {
  20682. struct nk_panel *layout;
  20683. const struct nk_style *style;
  20684. struct nk_vec2 spacing;
  20685. float item_offset = 0;
  20686. float item_width = 0;
  20687. float item_spacing = 0;
  20688. float panel_space = 0;
  20689. NK_ASSERT(ctx);
  20690. NK_ASSERT(ctx->current);
  20691. NK_ASSERT(ctx->current->layout);
  20692. if (!ctx || !ctx->current || !ctx->current->layout)
  20693. return;
  20694. win = ctx->current;
  20695. layout = win->layout;
  20696. style = &ctx->style;
  20697. NK_ASSERT(bounds);
  20698. spacing = style->window.spacing;
  20699. panel_space = nk_layout_row_calculate_usable_space(&ctx->style, layout->type,
  20700. layout->bounds.w, layout->row.columns);
  20701. #define NK_FRAC(x) (x - (float)(int)nk_roundf(x)) /* will be used to remove fookin gaps */
  20702. /* calculate the width of one item inside the current layout space */
  20703. switch (layout->row.type) {
  20704. case NK_LAYOUT_DYNAMIC_FIXED: {
  20705. /* scaling fixed size widgets item width */
  20706. float w = NK_MAX(1.0f,panel_space) / (float)layout->row.columns;
  20707. item_offset = (float)layout->row.index * w;
  20708. item_width = w + NK_FRAC(item_offset);
  20709. item_spacing = (float)layout->row.index * spacing.x;
  20710. } break;
  20711. case NK_LAYOUT_DYNAMIC_ROW: {
  20712. /* scaling single ratio widget width */
  20713. float w = layout->row.item_width * panel_space;
  20714. item_offset = layout->row.item_offset;
  20715. item_width = w + NK_FRAC(item_offset);
  20716. item_spacing = 0;
  20717. if (modify) {
  20718. layout->row.item_offset += w + spacing.x;
  20719. layout->row.filled += layout->row.item_width;
  20720. layout->row.index = 0;
  20721. }
  20722. } break;
  20723. case NK_LAYOUT_DYNAMIC_FREE: {
  20724. /* panel width depended free widget placing */
  20725. bounds->x = layout->at_x + (layout->bounds.w * layout->row.item.x);
  20726. bounds->x -= (float)*layout->offset_x;
  20727. bounds->y = layout->at_y + (layout->row.height * layout->row.item.y);
  20728. bounds->y -= (float)*layout->offset_y;
  20729. bounds->w = layout->bounds.w * layout->row.item.w + NK_FRAC(bounds->x);
  20730. bounds->h = layout->row.height * layout->row.item.h + NK_FRAC(bounds->y);
  20731. return;
  20732. }
  20733. case NK_LAYOUT_DYNAMIC: {
  20734. /* scaling arrays of panel width ratios for every widget */
  20735. float ratio, w;
  20736. NK_ASSERT(layout->row.ratio);
  20737. ratio = (layout->row.ratio[layout->row.index] < 0) ?
  20738. layout->row.item_width : layout->row.ratio[layout->row.index];
  20739. w = (ratio * panel_space);
  20740. item_spacing = (float)layout->row.index * spacing.x;
  20741. item_offset = layout->row.item_offset;
  20742. item_width = w + NK_FRAC(item_offset);
  20743. if (modify) {
  20744. layout->row.item_offset += w;
  20745. layout->row.filled += ratio;
  20746. }
  20747. } break;
  20748. case NK_LAYOUT_STATIC_FIXED: {
  20749. /* non-scaling fixed widgets item width */
  20750. item_width = layout->row.item_width;
  20751. item_offset = (float)layout->row.index * item_width;
  20752. item_spacing = (float)layout->row.index * spacing.x;
  20753. } break;
  20754. case NK_LAYOUT_STATIC_ROW: {
  20755. /* scaling single ratio widget width */
  20756. item_width = layout->row.item_width;
  20757. item_offset = layout->row.item_offset;
  20758. item_spacing = (float)layout->row.index * spacing.x;
  20759. if (modify) layout->row.item_offset += item_width;
  20760. } break;
  20761. case NK_LAYOUT_STATIC_FREE: {
  20762. /* free widget placing */
  20763. bounds->x = layout->at_x + layout->row.item.x;
  20764. bounds->w = layout->row.item.w;
  20765. if (((bounds->x + bounds->w) > layout->max_x) && modify)
  20766. layout->max_x = (bounds->x + bounds->w);
  20767. bounds->x -= (float)*layout->offset_x;
  20768. bounds->y = layout->at_y + layout->row.item.y;
  20769. bounds->y -= (float)*layout->offset_y;
  20770. bounds->h = layout->row.item.h;
  20771. return;
  20772. }
  20773. case NK_LAYOUT_STATIC: {
  20774. /* non-scaling array of panel pixel width for every widget */
  20775. item_spacing = (float)layout->row.index * spacing.x;
  20776. item_width = layout->row.ratio[layout->row.index];
  20777. item_offset = layout->row.item_offset;
  20778. if (modify) layout->row.item_offset += item_width;
  20779. } break;
  20780. case NK_LAYOUT_TEMPLATE: {
  20781. /* stretchy row layout with combined dynamic/static widget width*/
  20782. float w;
  20783. NK_ASSERT(layout->row.index < layout->row.columns);
  20784. NK_ASSERT(layout->row.index < NK_MAX_LAYOUT_ROW_TEMPLATE_COLUMNS);
  20785. w = layout->row.templates[layout->row.index];
  20786. item_offset = layout->row.item_offset;
  20787. item_width = w + NK_FRAC(item_offset);
  20788. item_spacing = (float)layout->row.index * spacing.x;
  20789. if (modify) layout->row.item_offset += w;
  20790. } break;
  20791. #undef NK_FRAC
  20792. default: NK_ASSERT(0); break;
  20793. };
  20794. /* set the bounds of the newly allocated widget */
  20795. bounds->w = item_width;
  20796. bounds->h = layout->row.height - spacing.y;
  20797. bounds->y = layout->at_y - (float)*layout->offset_y;
  20798. bounds->x = layout->at_x + item_offset + item_spacing;
  20799. if (((bounds->x + bounds->w) > layout->max_x) && modify)
  20800. layout->max_x = bounds->x + bounds->w;
  20801. bounds->x -= (float)*layout->offset_x;
  20802. }
  20803. NK_LIB void
  20804. nk_panel_alloc_space(struct nk_rect *bounds, const struct nk_context *ctx)
  20805. {
  20806. struct nk_window *win;
  20807. struct nk_panel *layout;
  20808. NK_ASSERT(ctx);
  20809. NK_ASSERT(ctx->current);
  20810. NK_ASSERT(ctx->current->layout);
  20811. if (!ctx || !ctx->current || !ctx->current->layout)
  20812. return;
  20813. /* check if the end of the row has been hit and begin new row if so */
  20814. win = ctx->current;
  20815. layout = win->layout;
  20816. if (layout->row.index >= layout->row.columns)
  20817. nk_panel_alloc_row(ctx, win);
  20818. /* calculate widget position and size */
  20819. nk_layout_widget_space(bounds, ctx, win, nk_true);
  20820. layout->row.index++;
  20821. }
  20822. NK_LIB void
  20823. nk_layout_peek(struct nk_rect *bounds, const struct nk_context *ctx)
  20824. {
  20825. float y;
  20826. int index;
  20827. struct nk_window *win;
  20828. struct nk_panel *layout;
  20829. NK_ASSERT(ctx);
  20830. NK_ASSERT(ctx->current);
  20831. NK_ASSERT(ctx->current->layout);
  20832. if (!ctx || !ctx->current || !ctx->current->layout) {
  20833. *bounds = nk_rect(0,0,0,0);
  20834. return;
  20835. }
  20836. win = ctx->current;
  20837. layout = win->layout;
  20838. y = layout->at_y;
  20839. index = layout->row.index;
  20840. if (layout->row.index >= layout->row.columns) {
  20841. layout->at_y += layout->row.height;
  20842. layout->row.index = 0;
  20843. }
  20844. nk_layout_widget_space(bounds, ctx, win, nk_false);
  20845. if (!layout->row.index) {
  20846. bounds->x -= layout->row.item_offset;
  20847. }
  20848. layout->at_y = y;
  20849. layout->row.index = index;
  20850. }
  20851. NK_API void
  20852. nk_spacer(struct nk_context *ctx )
  20853. {
  20854. struct nk_rect dummy_rect = { 0, 0, 0, 0 };
  20855. nk_panel_alloc_space( &dummy_rect, ctx );
  20856. }
  20857. /* ===============================================================
  20858. *
  20859. * TREE
  20860. *
  20861. * ===============================================================*/
  20862. NK_INTERN int
  20863. nk_tree_state_base(struct nk_context *ctx, enum nk_tree_type type,
  20864. struct nk_image *img, const char *title, enum nk_collapse_states *state)
  20865. {
  20866. struct nk_window *win;
  20867. struct nk_panel *layout;
  20868. const struct nk_style *style;
  20869. struct nk_command_buffer *out;
  20870. const struct nk_input *in;
  20871. const struct nk_style_button *button;
  20872. enum nk_symbol_type symbol;
  20873. float row_height;
  20874. struct nk_vec2 item_spacing;
  20875. struct nk_rect header = {0,0,0,0};
  20876. struct nk_rect sym = {0,0,0,0};
  20877. struct nk_text text;
  20878. nk_flags ws = 0;
  20879. enum nk_widget_layout_states widget_state;
  20880. NK_ASSERT(ctx);
  20881. NK_ASSERT(ctx->current);
  20882. NK_ASSERT(ctx->current->layout);
  20883. if (!ctx || !ctx->current || !ctx->current->layout)
  20884. return 0;
  20885. /* cache some data */
  20886. win = ctx->current;
  20887. layout = win->layout;
  20888. out = &win->buffer;
  20889. style = &ctx->style;
  20890. item_spacing = style->window.spacing;
  20891. /* calculate header bounds and draw background */
  20892. row_height = style->font->height + 2 * style->tab.padding.y;
  20893. nk_layout_set_min_row_height(ctx, row_height);
  20894. nk_layout_row_dynamic(ctx, row_height, 1);
  20895. nk_layout_reset_min_row_height(ctx);
  20896. widget_state = nk_widget(&header, ctx);
  20897. if (type == NK_TREE_TAB) {
  20898. const struct nk_style_item *background = &style->tab.background;
  20899. switch(background->type) {
  20900. case NK_STYLE_ITEM_IMAGE:
  20901. nk_draw_image(out, header, &background->data.image, nk_rgb_factor(nk_white, style->tab.color_factor));
  20902. break;
  20903. case NK_STYLE_ITEM_NINE_SLICE:
  20904. nk_draw_nine_slice(out, header, &background->data.slice, nk_rgb_factor(nk_white, style->tab.color_factor));
  20905. break;
  20906. case NK_STYLE_ITEM_COLOR:
  20907. nk_fill_rect(out, header, 0, nk_rgb_factor(style->tab.border_color, style->tab.color_factor));
  20908. nk_fill_rect(out, nk_shrink_rect(header, style->tab.border),
  20909. style->tab.rounding, nk_rgb_factor(background->data.color, style->tab.color_factor));
  20910. break;
  20911. }
  20912. } else text.background = style->window.background;
  20913. /* update node state */
  20914. in = (!(layout->flags & NK_WINDOW_ROM)) ? &ctx->input: 0;
  20915. in = (in && widget_state == NK_WIDGET_VALID) ? &ctx->input : 0;
  20916. if (nk_button_behavior(&ws, header, in, NK_BUTTON_DEFAULT))
  20917. *state = (*state == NK_MAXIMIZED) ? NK_MINIMIZED : NK_MAXIMIZED;
  20918. /* select correct button style */
  20919. if (*state == NK_MAXIMIZED) {
  20920. symbol = style->tab.sym_maximize;
  20921. if (type == NK_TREE_TAB)
  20922. button = &style->tab.tab_maximize_button;
  20923. else button = &style->tab.node_maximize_button;
  20924. } else {
  20925. symbol = style->tab.sym_minimize;
  20926. if (type == NK_TREE_TAB)
  20927. button = &style->tab.tab_minimize_button;
  20928. else button = &style->tab.node_minimize_button;
  20929. }
  20930. {/* draw triangle button */
  20931. sym.w = sym.h = style->font->height;
  20932. sym.y = header.y + style->tab.padding.y;
  20933. sym.x = header.x + style->tab.padding.x;
  20934. nk_do_button_symbol(&ws, &win->buffer, sym, symbol, NK_BUTTON_DEFAULT,
  20935. button, 0, style->font);
  20936. if (img) {
  20937. /* draw optional image icon */
  20938. sym.x = sym.x + sym.w + 4 * item_spacing.x;
  20939. nk_draw_image(&win->buffer, sym, img, nk_white);
  20940. sym.w = style->font->height + style->tab.spacing.x;}
  20941. }
  20942. {/* draw label */
  20943. struct nk_rect label;
  20944. header.w = NK_MAX(header.w, sym.w + item_spacing.x);
  20945. label.x = sym.x + sym.w + item_spacing.x;
  20946. label.y = sym.y;
  20947. label.w = header.w - (sym.w + item_spacing.y + style->tab.indent);
  20948. label.h = style->font->height;
  20949. text.text = nk_rgb_factor(style->tab.text, style->tab.color_factor);
  20950. text.padding = nk_vec2(0,0);
  20951. nk_widget_text(out, label, title, nk_strlen(title), &text,
  20952. NK_TEXT_LEFT, style->font);}
  20953. /* increase x-axis cursor widget position pointer */
  20954. if (*state == NK_MAXIMIZED) {
  20955. layout->at_x = header.x + (float)*layout->offset_x + style->tab.indent;
  20956. layout->bounds.w = NK_MAX(layout->bounds.w, style->tab.indent);
  20957. layout->bounds.w -= (style->tab.indent + style->window.padding.x);
  20958. layout->row.tree_depth++;
  20959. return nk_true;
  20960. } else return nk_false;
  20961. }
  20962. NK_INTERN int
  20963. nk_tree_base(struct nk_context *ctx, enum nk_tree_type type,
  20964. struct nk_image *img, const char *title, enum nk_collapse_states initial_state,
  20965. const char *hash, int len, int line)
  20966. {
  20967. struct nk_window *win = ctx->current;
  20968. int title_len = 0;
  20969. nk_hash tree_hash = 0;
  20970. nk_uint *state = 0;
  20971. /* retrieve tree state from internal widget state tables */
  20972. if (!hash) {
  20973. title_len = (int)nk_strlen(title);
  20974. tree_hash = nk_murmur_hash(title, (int)title_len, (nk_hash)line);
  20975. } else tree_hash = nk_murmur_hash(hash, len, (nk_hash)line);
  20976. state = nk_find_value(win, tree_hash);
  20977. if (!state) {
  20978. state = nk_add_value(ctx, win, tree_hash, 0);
  20979. *state = initial_state;
  20980. }
  20981. return nk_tree_state_base(ctx, type, img, title, (enum nk_collapse_states*)state);
  20982. }
  20983. NK_API nk_bool
  20984. nk_tree_state_push(struct nk_context *ctx, enum nk_tree_type type,
  20985. const char *title, enum nk_collapse_states *state)
  20986. {
  20987. return nk_tree_state_base(ctx, type, 0, title, state);
  20988. }
  20989. NK_API nk_bool
  20990. nk_tree_state_image_push(struct nk_context *ctx, enum nk_tree_type type,
  20991. struct nk_image img, const char *title, enum nk_collapse_states *state)
  20992. {
  20993. return nk_tree_state_base(ctx, type, &img, title, state);
  20994. }
  20995. NK_API void
  20996. nk_tree_state_pop(struct nk_context *ctx)
  20997. {
  20998. struct nk_window *win = 0;
  20999. struct nk_panel *layout = 0;
  21000. NK_ASSERT(ctx);
  21001. NK_ASSERT(ctx->current);
  21002. NK_ASSERT(ctx->current->layout);
  21003. if (!ctx || !ctx->current || !ctx->current->layout)
  21004. return;
  21005. win = ctx->current;
  21006. layout = win->layout;
  21007. layout->at_x -= ctx->style.tab.indent + (float)*layout->offset_x;
  21008. layout->bounds.w += ctx->style.tab.indent + ctx->style.window.padding.x;
  21009. NK_ASSERT(layout->row.tree_depth);
  21010. layout->row.tree_depth--;
  21011. }
  21012. NK_API nk_bool
  21013. nk_tree_push_hashed(struct nk_context *ctx, enum nk_tree_type type,
  21014. const char *title, enum nk_collapse_states initial_state,
  21015. const char *hash, int len, int line)
  21016. {
  21017. return nk_tree_base(ctx, type, 0, title, initial_state, hash, len, line);
  21018. }
  21019. NK_API nk_bool
  21020. nk_tree_image_push_hashed(struct nk_context *ctx, enum nk_tree_type type,
  21021. struct nk_image img, const char *title, enum nk_collapse_states initial_state,
  21022. const char *hash, int len,int seed)
  21023. {
  21024. return nk_tree_base(ctx, type, &img, title, initial_state, hash, len, seed);
  21025. }
  21026. NK_API void
  21027. nk_tree_pop(struct nk_context *ctx)
  21028. {
  21029. nk_tree_state_pop(ctx);
  21030. }
  21031. NK_INTERN int
  21032. nk_tree_element_image_push_hashed_base(struct nk_context *ctx, enum nk_tree_type type,
  21033. struct nk_image *img, const char *title, int title_len,
  21034. enum nk_collapse_states *state, nk_bool *selected)
  21035. {
  21036. struct nk_window *win;
  21037. struct nk_panel *layout;
  21038. const struct nk_style *style;
  21039. struct nk_command_buffer *out;
  21040. const struct nk_input *in;
  21041. const struct nk_style_button *button;
  21042. enum nk_symbol_type symbol;
  21043. float row_height;
  21044. struct nk_vec2 padding;
  21045. int text_len;
  21046. float text_width;
  21047. struct nk_vec2 item_spacing;
  21048. struct nk_rect header = {0,0,0,0};
  21049. struct nk_rect sym = {0,0,0,0};
  21050. nk_flags ws = 0;
  21051. enum nk_widget_layout_states widget_state;
  21052. NK_ASSERT(ctx);
  21053. NK_ASSERT(ctx->current);
  21054. NK_ASSERT(ctx->current->layout);
  21055. if (!ctx || !ctx->current || !ctx->current->layout)
  21056. return 0;
  21057. /* cache some data */
  21058. win = ctx->current;
  21059. layout = win->layout;
  21060. out = &win->buffer;
  21061. style = &ctx->style;
  21062. item_spacing = style->window.spacing;
  21063. padding = style->selectable.padding;
  21064. /* calculate header bounds and draw background */
  21065. row_height = style->font->height + 2 * style->tab.padding.y;
  21066. nk_layout_set_min_row_height(ctx, row_height);
  21067. nk_layout_row_dynamic(ctx, row_height, 1);
  21068. nk_layout_reset_min_row_height(ctx);
  21069. widget_state = nk_widget(&header, ctx);
  21070. if (type == NK_TREE_TAB) {
  21071. const struct nk_style_item *background = &style->tab.background;
  21072. switch (background->type) {
  21073. case NK_STYLE_ITEM_IMAGE:
  21074. nk_draw_image(out, header, &background->data.image, nk_rgb_factor(nk_white, style->tab.color_factor));
  21075. break;
  21076. case NK_STYLE_ITEM_NINE_SLICE:
  21077. nk_draw_nine_slice(out, header, &background->data.slice, nk_rgb_factor(nk_white, style->tab.color_factor));
  21078. break;
  21079. case NK_STYLE_ITEM_COLOR:
  21080. nk_fill_rect(out, header, 0, nk_rgb_factor(style->tab.border_color, style->tab.color_factor));
  21081. nk_fill_rect(out, nk_shrink_rect(header, style->tab.border),
  21082. style->tab.rounding, nk_rgb_factor(background->data.color, style->tab.color_factor));
  21083. break;
  21084. }
  21085. }
  21086. in = (!(layout->flags & NK_WINDOW_ROM)) ? &ctx->input: 0;
  21087. in = (in && widget_state == NK_WIDGET_VALID) ? &ctx->input : 0;
  21088. /* select correct button style */
  21089. if (*state == NK_MAXIMIZED) {
  21090. symbol = style->tab.sym_maximize;
  21091. if (type == NK_TREE_TAB)
  21092. button = &style->tab.tab_maximize_button;
  21093. else button = &style->tab.node_maximize_button;
  21094. } else {
  21095. symbol = style->tab.sym_minimize;
  21096. if (type == NK_TREE_TAB)
  21097. button = &style->tab.tab_minimize_button;
  21098. else button = &style->tab.node_minimize_button;
  21099. }
  21100. {/* draw triangle button */
  21101. sym.w = sym.h = style->font->height;
  21102. sym.y = header.y + style->tab.padding.y;
  21103. sym.x = header.x + style->tab.padding.x;
  21104. if (nk_do_button_symbol(&ws, &win->buffer, sym, symbol, NK_BUTTON_DEFAULT, button, in, style->font))
  21105. *state = (*state == NK_MAXIMIZED) ? NK_MINIMIZED : NK_MAXIMIZED;}
  21106. /* draw label */
  21107. {nk_flags dummy = 0;
  21108. struct nk_rect label;
  21109. /* calculate size of the text and tooltip */
  21110. text_len = nk_strlen(title);
  21111. text_width = style->font->width(style->font->userdata, style->font->height, title, text_len);
  21112. text_width += (4 * padding.x);
  21113. header.w = NK_MAX(header.w, sym.w + item_spacing.x);
  21114. label.x = sym.x + sym.w + item_spacing.x;
  21115. label.y = sym.y;
  21116. label.w = NK_MIN(header.w - (sym.w + item_spacing.y + style->tab.indent), text_width);
  21117. label.h = style->font->height;
  21118. if (img) {
  21119. nk_do_selectable_image(&dummy, &win->buffer, label, title, title_len, NK_TEXT_LEFT,
  21120. selected, img, &style->selectable, in, style->font);
  21121. } else nk_do_selectable(&dummy, &win->buffer, label, title, title_len, NK_TEXT_LEFT,
  21122. selected, &style->selectable, in, style->font);
  21123. }
  21124. /* increase x-axis cursor widget position pointer */
  21125. if (*state == NK_MAXIMIZED) {
  21126. layout->at_x = header.x + (float)*layout->offset_x + style->tab.indent;
  21127. layout->bounds.w = NK_MAX(layout->bounds.w, style->tab.indent);
  21128. layout->bounds.w -= (style->tab.indent + style->window.padding.x);
  21129. layout->row.tree_depth++;
  21130. return nk_true;
  21131. } else return nk_false;
  21132. }
  21133. NK_INTERN int
  21134. nk_tree_element_base(struct nk_context *ctx, enum nk_tree_type type,
  21135. struct nk_image *img, const char *title, enum nk_collapse_states initial_state,
  21136. nk_bool *selected, const char *hash, int len, int line)
  21137. {
  21138. struct nk_window *win = ctx->current;
  21139. int title_len = 0;
  21140. nk_hash tree_hash = 0;
  21141. nk_uint *state = 0;
  21142. /* retrieve tree state from internal widget state tables */
  21143. if (!hash) {
  21144. title_len = (int)nk_strlen(title);
  21145. tree_hash = nk_murmur_hash(title, (int)title_len, (nk_hash)line);
  21146. } else tree_hash = nk_murmur_hash(hash, len, (nk_hash)line);
  21147. state = nk_find_value(win, tree_hash);
  21148. if (!state) {
  21149. state = nk_add_value(ctx, win, tree_hash, 0);
  21150. *state = initial_state;
  21151. } return nk_tree_element_image_push_hashed_base(ctx, type, img, title,
  21152. nk_strlen(title), (enum nk_collapse_states*)state, selected);
  21153. }
  21154. NK_API nk_bool
  21155. nk_tree_element_push_hashed(struct nk_context *ctx, enum nk_tree_type type,
  21156. const char *title, enum nk_collapse_states initial_state,
  21157. nk_bool *selected, const char *hash, int len, int seed)
  21158. {
  21159. return nk_tree_element_base(ctx, type, 0, title, initial_state, selected, hash, len, seed);
  21160. }
  21161. NK_API nk_bool
  21162. nk_tree_element_image_push_hashed(struct nk_context *ctx, enum nk_tree_type type,
  21163. struct nk_image img, const char *title, enum nk_collapse_states initial_state,
  21164. nk_bool *selected, const char *hash, int len,int seed)
  21165. {
  21166. return nk_tree_element_base(ctx, type, &img, title, initial_state, selected, hash, len, seed);
  21167. }
  21168. NK_API void
  21169. nk_tree_element_pop(struct nk_context *ctx)
  21170. {
  21171. nk_tree_state_pop(ctx);
  21172. }
  21173. /* ===============================================================
  21174. *
  21175. * GROUP
  21176. *
  21177. * ===============================================================*/
  21178. NK_API nk_bool
  21179. nk_group_scrolled_offset_begin(struct nk_context *ctx,
  21180. nk_uint *x_offset, nk_uint *y_offset, const char *title, nk_flags flags)
  21181. {
  21182. struct nk_rect bounds;
  21183. struct nk_window panel;
  21184. struct nk_window *win;
  21185. win = ctx->current;
  21186. nk_panel_alloc_space(&bounds, ctx);
  21187. {const struct nk_rect *c = &win->layout->clip;
  21188. if (!NK_INTERSECT(c->x, c->y, c->w, c->h, bounds.x, bounds.y, bounds.w, bounds.h) &&
  21189. !(flags & NK_WINDOW_MOVABLE)) {
  21190. return 0;
  21191. }}
  21192. if (win->flags & NK_WINDOW_ROM)
  21193. flags |= NK_WINDOW_ROM;
  21194. /* initialize a fake window to create the panel from */
  21195. nk_zero(&panel, sizeof(panel));
  21196. panel.bounds = bounds;
  21197. panel.flags = flags;
  21198. panel.scrollbar.x = *x_offset;
  21199. panel.scrollbar.y = *y_offset;
  21200. panel.buffer = win->buffer;
  21201. panel.layout = (struct nk_panel*)nk_create_panel(ctx);
  21202. ctx->current = &panel;
  21203. nk_panel_begin(ctx, (flags & NK_WINDOW_TITLE) ? title: 0, NK_PANEL_GROUP);
  21204. win->buffer = panel.buffer;
  21205. win->buffer.clip = panel.layout->clip;
  21206. panel.layout->offset_x = x_offset;
  21207. panel.layout->offset_y = y_offset;
  21208. panel.layout->parent = win->layout;
  21209. win->layout = panel.layout;
  21210. ctx->current = win;
  21211. if ((panel.layout->flags & NK_WINDOW_CLOSED) ||
  21212. (panel.layout->flags & NK_WINDOW_MINIMIZED))
  21213. {
  21214. nk_flags f = panel.layout->flags;
  21215. nk_group_scrolled_end(ctx);
  21216. if (f & NK_WINDOW_CLOSED)
  21217. return NK_WINDOW_CLOSED;
  21218. if (f & NK_WINDOW_MINIMIZED)
  21219. return NK_WINDOW_MINIMIZED;
  21220. }
  21221. return 1;
  21222. }
  21223. NK_API void
  21224. nk_group_scrolled_end(struct nk_context *ctx)
  21225. {
  21226. struct nk_window *win;
  21227. struct nk_panel *parent;
  21228. struct nk_panel *g;
  21229. struct nk_rect clip;
  21230. struct nk_window pan;
  21231. struct nk_vec2 panel_padding;
  21232. NK_ASSERT(ctx);
  21233. NK_ASSERT(ctx->current);
  21234. if (!ctx || !ctx->current)
  21235. return;
  21236. /* make sure nk_group_begin was called correctly */
  21237. NK_ASSERT(ctx->current);
  21238. win = ctx->current;
  21239. NK_ASSERT(win->layout);
  21240. g = win->layout;
  21241. NK_ASSERT(g->parent);
  21242. parent = g->parent;
  21243. /* dummy window */
  21244. nk_zero_struct(pan);
  21245. panel_padding = nk_panel_get_padding(&ctx->style, NK_PANEL_GROUP);
  21246. pan.bounds.y = g->bounds.y - (g->header_height + g->menu.h);
  21247. pan.bounds.x = g->bounds.x - panel_padding.x;
  21248. pan.bounds.w = g->bounds.w + 2 * panel_padding.x;
  21249. pan.bounds.h = g->bounds.h + g->header_height + g->menu.h;
  21250. if (g->flags & NK_WINDOW_BORDER) {
  21251. pan.bounds.x -= g->border;
  21252. pan.bounds.y -= g->border;
  21253. pan.bounds.w += 2*g->border;
  21254. pan.bounds.h += 2*g->border;
  21255. }
  21256. if (!(g->flags & NK_WINDOW_NO_SCROLLBAR)) {
  21257. pan.bounds.w += ctx->style.window.scrollbar_size.x;
  21258. pan.bounds.h += ctx->style.window.scrollbar_size.y;
  21259. }
  21260. pan.scrollbar.x = *g->offset_x;
  21261. pan.scrollbar.y = *g->offset_y;
  21262. pan.flags = g->flags;
  21263. pan.buffer = win->buffer;
  21264. pan.layout = g;
  21265. pan.parent = win;
  21266. ctx->current = &pan;
  21267. /* make sure group has correct clipping rectangle */
  21268. nk_unify(&clip, &parent->clip, pan.bounds.x, pan.bounds.y,
  21269. pan.bounds.x + pan.bounds.w, pan.bounds.y + pan.bounds.h + panel_padding.x);
  21270. nk_push_scissor(&pan.buffer, clip);
  21271. nk_end(ctx);
  21272. win->buffer = pan.buffer;
  21273. nk_push_scissor(&win->buffer, parent->clip);
  21274. ctx->current = win;
  21275. win->layout = parent;
  21276. g->bounds = pan.bounds;
  21277. return;
  21278. }
  21279. NK_API nk_bool
  21280. nk_group_scrolled_begin(struct nk_context *ctx,
  21281. struct nk_scroll *scroll, const char *title, nk_flags flags)
  21282. {
  21283. return nk_group_scrolled_offset_begin(ctx, &scroll->x, &scroll->y, title, flags);
  21284. }
  21285. NK_API nk_bool
  21286. nk_group_begin_titled(struct nk_context *ctx, const char *id,
  21287. const char *title, nk_flags flags)
  21288. {
  21289. int id_len;
  21290. nk_hash id_hash;
  21291. struct nk_window *win;
  21292. nk_uint *x_offset;
  21293. nk_uint *y_offset;
  21294. NK_ASSERT(ctx);
  21295. NK_ASSERT(id);
  21296. NK_ASSERT(ctx->current);
  21297. NK_ASSERT(ctx->current->layout);
  21298. if (!ctx || !ctx->current || !ctx->current->layout || !id)
  21299. return 0;
  21300. /* find persistent group scrollbar value */
  21301. win = ctx->current;
  21302. id_len = (int)nk_strlen(id);
  21303. id_hash = nk_murmur_hash(id, (int)id_len, NK_PANEL_GROUP);
  21304. x_offset = nk_find_value(win, id_hash);
  21305. if (!x_offset) {
  21306. x_offset = nk_add_value(ctx, win, id_hash, 0);
  21307. y_offset = nk_add_value(ctx, win, id_hash+1, 0);
  21308. NK_ASSERT(x_offset);
  21309. NK_ASSERT(y_offset);
  21310. if (!x_offset || !y_offset) return 0;
  21311. *x_offset = *y_offset = 0;
  21312. } else y_offset = nk_find_value(win, id_hash+1);
  21313. return nk_group_scrolled_offset_begin(ctx, x_offset, y_offset, title, flags);
  21314. }
  21315. NK_API nk_bool
  21316. nk_group_begin(struct nk_context *ctx, const char *title, nk_flags flags)
  21317. {
  21318. return nk_group_begin_titled(ctx, title, title, flags);
  21319. }
  21320. NK_API void
  21321. nk_group_end(struct nk_context *ctx)
  21322. {
  21323. nk_group_scrolled_end(ctx);
  21324. }
  21325. NK_API void
  21326. nk_group_get_scroll(struct nk_context *ctx, const char *id, nk_uint *x_offset, nk_uint *y_offset)
  21327. {
  21328. int id_len;
  21329. nk_hash id_hash;
  21330. struct nk_window *win;
  21331. nk_uint *x_offset_ptr;
  21332. nk_uint *y_offset_ptr;
  21333. NK_ASSERT(ctx);
  21334. NK_ASSERT(id);
  21335. NK_ASSERT(ctx->current);
  21336. NK_ASSERT(ctx->current->layout);
  21337. if (!ctx || !ctx->current || !ctx->current->layout || !id)
  21338. return;
  21339. /* find persistent group scrollbar value */
  21340. win = ctx->current;
  21341. id_len = (int)nk_strlen(id);
  21342. id_hash = nk_murmur_hash(id, (int)id_len, NK_PANEL_GROUP);
  21343. x_offset_ptr = nk_find_value(win, id_hash);
  21344. if (!x_offset_ptr) {
  21345. x_offset_ptr = nk_add_value(ctx, win, id_hash, 0);
  21346. y_offset_ptr = nk_add_value(ctx, win, id_hash+1, 0);
  21347. NK_ASSERT(x_offset_ptr);
  21348. NK_ASSERT(y_offset_ptr);
  21349. if (!x_offset_ptr || !y_offset_ptr) return;
  21350. *x_offset_ptr = *y_offset_ptr = 0;
  21351. } else y_offset_ptr = nk_find_value(win, id_hash+1);
  21352. if (x_offset)
  21353. *x_offset = *x_offset_ptr;
  21354. if (y_offset)
  21355. *y_offset = *y_offset_ptr;
  21356. }
  21357. NK_API void
  21358. nk_group_set_scroll(struct nk_context *ctx, const char *id, nk_uint x_offset, nk_uint y_offset)
  21359. {
  21360. int id_len;
  21361. nk_hash id_hash;
  21362. struct nk_window *win;
  21363. nk_uint *x_offset_ptr;
  21364. nk_uint *y_offset_ptr;
  21365. NK_ASSERT(ctx);
  21366. NK_ASSERT(id);
  21367. NK_ASSERT(ctx->current);
  21368. NK_ASSERT(ctx->current->layout);
  21369. if (!ctx || !ctx->current || !ctx->current->layout || !id)
  21370. return;
  21371. /* find persistent group scrollbar value */
  21372. win = ctx->current;
  21373. id_len = (int)nk_strlen(id);
  21374. id_hash = nk_murmur_hash(id, (int)id_len, NK_PANEL_GROUP);
  21375. x_offset_ptr = nk_find_value(win, id_hash);
  21376. if (!x_offset_ptr) {
  21377. x_offset_ptr = nk_add_value(ctx, win, id_hash, 0);
  21378. y_offset_ptr = nk_add_value(ctx, win, id_hash+1, 0);
  21379. NK_ASSERT(x_offset_ptr);
  21380. NK_ASSERT(y_offset_ptr);
  21381. if (!x_offset_ptr || !y_offset_ptr) return;
  21382. *x_offset_ptr = *y_offset_ptr = 0;
  21383. } else y_offset_ptr = nk_find_value(win, id_hash+1);
  21384. *x_offset_ptr = x_offset;
  21385. *y_offset_ptr = y_offset;
  21386. }
  21387. /* ===============================================================
  21388. *
  21389. * LIST VIEW
  21390. *
  21391. * ===============================================================*/
  21392. NK_API nk_bool
  21393. nk_list_view_begin(struct nk_context *ctx, struct nk_list_view *view,
  21394. const char *title, nk_flags flags, int row_height, int row_count)
  21395. {
  21396. int title_len;
  21397. nk_hash title_hash;
  21398. nk_uint *x_offset;
  21399. nk_uint *y_offset;
  21400. int result;
  21401. struct nk_window *win;
  21402. struct nk_panel *layout;
  21403. const struct nk_style *style;
  21404. struct nk_vec2 item_spacing;
  21405. NK_ASSERT(ctx);
  21406. NK_ASSERT(view);
  21407. NK_ASSERT(title);
  21408. if (!ctx || !view || !title) return 0;
  21409. win = ctx->current;
  21410. style = &ctx->style;
  21411. item_spacing = style->window.spacing;
  21412. row_height += NK_MAX(0, (int)item_spacing.y);
  21413. /* find persistent list view scrollbar offset */
  21414. title_len = (int)nk_strlen(title);
  21415. title_hash = nk_murmur_hash(title, (int)title_len, NK_PANEL_GROUP);
  21416. x_offset = nk_find_value(win, title_hash);
  21417. if (!x_offset) {
  21418. x_offset = nk_add_value(ctx, win, title_hash, 0);
  21419. y_offset = nk_add_value(ctx, win, title_hash+1, 0);
  21420. NK_ASSERT(x_offset);
  21421. NK_ASSERT(y_offset);
  21422. if (!x_offset || !y_offset) return 0;
  21423. *x_offset = *y_offset = 0;
  21424. } else y_offset = nk_find_value(win, title_hash+1);
  21425. view->scroll_value = *y_offset;
  21426. view->scroll_pointer = y_offset;
  21427. *y_offset = 0;
  21428. result = nk_group_scrolled_offset_begin(ctx, x_offset, y_offset, title, flags);
  21429. win = ctx->current;
  21430. layout = win->layout;
  21431. view->total_height = row_height * NK_MAX(row_count,1);
  21432. view->begin = (int)NK_MAX(((float)view->scroll_value / (float)row_height), 0.0f);
  21433. view->count = (int)NK_MAX(nk_iceilf((layout->clip.h)/(float)row_height),0);
  21434. view->count = NK_MIN(view->count, row_count - view->begin);
  21435. view->end = view->begin + view->count;
  21436. view->ctx = ctx;
  21437. return result;
  21438. }
  21439. NK_API void
  21440. nk_list_view_end(struct nk_list_view *view)
  21441. {
  21442. struct nk_context *ctx;
  21443. struct nk_window *win;
  21444. struct nk_panel *layout;
  21445. NK_ASSERT(view);
  21446. NK_ASSERT(view->ctx);
  21447. NK_ASSERT(view->scroll_pointer);
  21448. if (!view || !view->ctx) return;
  21449. ctx = view->ctx;
  21450. win = ctx->current;
  21451. layout = win->layout;
  21452. layout->at_y = layout->bounds.y + (float)view->total_height;
  21453. *view->scroll_pointer = *view->scroll_pointer + view->scroll_value;
  21454. nk_group_end(view->ctx);
  21455. }
  21456. /* ===============================================================
  21457. *
  21458. * WIDGET
  21459. *
  21460. * ===============================================================*/
  21461. NK_API struct nk_rect
  21462. nk_widget_bounds(const struct nk_context *ctx)
  21463. {
  21464. struct nk_rect bounds;
  21465. NK_ASSERT(ctx);
  21466. NK_ASSERT(ctx->current);
  21467. if (!ctx || !ctx->current)
  21468. return nk_rect(0,0,0,0);
  21469. nk_layout_peek(&bounds, ctx);
  21470. return bounds;
  21471. }
  21472. NK_API struct nk_vec2
  21473. nk_widget_position(const struct nk_context *ctx)
  21474. {
  21475. struct nk_rect bounds;
  21476. NK_ASSERT(ctx);
  21477. NK_ASSERT(ctx->current);
  21478. if (!ctx || !ctx->current)
  21479. return nk_vec2(0,0);
  21480. nk_layout_peek(&bounds, ctx);
  21481. return nk_vec2(bounds.x, bounds.y);
  21482. }
  21483. NK_API struct nk_vec2
  21484. nk_widget_size(const struct nk_context *ctx)
  21485. {
  21486. struct nk_rect bounds;
  21487. NK_ASSERT(ctx);
  21488. NK_ASSERT(ctx->current);
  21489. if (!ctx || !ctx->current)
  21490. return nk_vec2(0,0);
  21491. nk_layout_peek(&bounds, ctx);
  21492. return nk_vec2(bounds.w, bounds.h);
  21493. }
  21494. NK_API float
  21495. nk_widget_width(const struct nk_context *ctx)
  21496. {
  21497. struct nk_rect bounds;
  21498. NK_ASSERT(ctx);
  21499. NK_ASSERT(ctx->current);
  21500. if (!ctx || !ctx->current)
  21501. return 0;
  21502. nk_layout_peek(&bounds, ctx);
  21503. return bounds.w;
  21504. }
  21505. NK_API float
  21506. nk_widget_height(const struct nk_context *ctx)
  21507. {
  21508. struct nk_rect bounds;
  21509. NK_ASSERT(ctx);
  21510. NK_ASSERT(ctx->current);
  21511. if (!ctx || !ctx->current)
  21512. return 0;
  21513. nk_layout_peek(&bounds, ctx);
  21514. return bounds.h;
  21515. }
  21516. NK_API nk_bool
  21517. nk_widget_is_hovered(const struct nk_context *ctx)
  21518. {
  21519. struct nk_rect c, v;
  21520. struct nk_rect bounds;
  21521. NK_ASSERT(ctx);
  21522. NK_ASSERT(ctx->current);
  21523. if (!ctx || !ctx->current || ctx->active != ctx->current)
  21524. return 0;
  21525. c = ctx->current->layout->clip;
  21526. c.x = (float)((int)c.x);
  21527. c.y = (float)((int)c.y);
  21528. c.w = (float)((int)c.w);
  21529. c.h = (float)((int)c.h);
  21530. nk_layout_peek(&bounds, ctx);
  21531. nk_unify(&v, &c, bounds.x, bounds.y, bounds.x + bounds.w, bounds.y + bounds.h);
  21532. if (!NK_INTERSECT(c.x, c.y, c.w, c.h, bounds.x, bounds.y, bounds.w, bounds.h))
  21533. return 0;
  21534. return nk_input_is_mouse_hovering_rect(&ctx->input, bounds);
  21535. }
  21536. NK_API nk_bool
  21537. nk_widget_is_mouse_clicked(const struct nk_context *ctx, enum nk_buttons btn)
  21538. {
  21539. struct nk_rect c, v;
  21540. struct nk_rect bounds;
  21541. NK_ASSERT(ctx);
  21542. NK_ASSERT(ctx->current);
  21543. if (!ctx || !ctx->current || ctx->active != ctx->current)
  21544. return 0;
  21545. c = ctx->current->layout->clip;
  21546. c.x = (float)((int)c.x);
  21547. c.y = (float)((int)c.y);
  21548. c.w = (float)((int)c.w);
  21549. c.h = (float)((int)c.h);
  21550. nk_layout_peek(&bounds, ctx);
  21551. nk_unify(&v, &c, bounds.x, bounds.y, bounds.x + bounds.w, bounds.y + bounds.h);
  21552. if (!NK_INTERSECT(c.x, c.y, c.w, c.h, bounds.x, bounds.y, bounds.w, bounds.h))
  21553. return 0;
  21554. return nk_input_mouse_clicked(&ctx->input, btn, bounds);
  21555. }
  21556. NK_API nk_bool
  21557. nk_widget_has_mouse_click_down(const struct nk_context *ctx, enum nk_buttons btn, nk_bool down)
  21558. {
  21559. struct nk_rect c, v;
  21560. struct nk_rect bounds;
  21561. NK_ASSERT(ctx);
  21562. NK_ASSERT(ctx->current);
  21563. if (!ctx || !ctx->current || ctx->active != ctx->current)
  21564. return 0;
  21565. c = ctx->current->layout->clip;
  21566. c.x = (float)((int)c.x);
  21567. c.y = (float)((int)c.y);
  21568. c.w = (float)((int)c.w);
  21569. c.h = (float)((int)c.h);
  21570. nk_layout_peek(&bounds, ctx);
  21571. nk_unify(&v, &c, bounds.x, bounds.y, bounds.x + bounds.w, bounds.y + bounds.h);
  21572. if (!NK_INTERSECT(c.x, c.y, c.w, c.h, bounds.x, bounds.y, bounds.w, bounds.h))
  21573. return 0;
  21574. return nk_input_has_mouse_click_down_in_rect(&ctx->input, btn, bounds, down);
  21575. }
  21576. NK_API enum nk_widget_layout_states
  21577. nk_widget(struct nk_rect *bounds, const struct nk_context *ctx)
  21578. {
  21579. struct nk_rect c, v;
  21580. struct nk_window *win;
  21581. struct nk_panel *layout;
  21582. const struct nk_input *in;
  21583. NK_ASSERT(ctx);
  21584. NK_ASSERT(ctx->current);
  21585. NK_ASSERT(ctx->current->layout);
  21586. if (!ctx || !ctx->current || !ctx->current->layout)
  21587. return NK_WIDGET_INVALID;
  21588. /* allocate space and check if the widget needs to be updated and drawn */
  21589. nk_panel_alloc_space(bounds, ctx);
  21590. win = ctx->current;
  21591. layout = win->layout;
  21592. in = &ctx->input;
  21593. c = layout->clip;
  21594. /* if one of these triggers you forgot to add an `if` condition around either
  21595. a window, group, popup, combobox or contextual menu `begin` and `end` block.
  21596. Example:
  21597. if (nk_begin(...) {...} nk_end(...); or
  21598. if (nk_group_begin(...) { nk_group_end(...);} */
  21599. NK_ASSERT(!(layout->flags & NK_WINDOW_MINIMIZED));
  21600. NK_ASSERT(!(layout->flags & NK_WINDOW_HIDDEN));
  21601. NK_ASSERT(!(layout->flags & NK_WINDOW_CLOSED));
  21602. /* need to convert to int here to remove floating point errors */
  21603. bounds->x = (float)((int)bounds->x);
  21604. bounds->y = (float)((int)bounds->y);
  21605. bounds->w = (float)((int)bounds->w);
  21606. bounds->h = (float)((int)bounds->h);
  21607. c.x = (float)((int)c.x);
  21608. c.y = (float)((int)c.y);
  21609. c.w = (float)((int)c.w);
  21610. c.h = (float)((int)c.h);
  21611. nk_unify(&v, &c, bounds->x, bounds->y, bounds->x + bounds->w, bounds->y + bounds->h);
  21612. if (!NK_INTERSECT(c.x, c.y, c.w, c.h, bounds->x, bounds->y, bounds->w, bounds->h))
  21613. return NK_WIDGET_INVALID;
  21614. if (win->widgets_disabled)
  21615. return NK_WIDGET_DISABLED;
  21616. if (!NK_INBOX(in->mouse.pos.x, in->mouse.pos.y, v.x, v.y, v.w, v.h))
  21617. return NK_WIDGET_ROM;
  21618. return NK_WIDGET_VALID;
  21619. }
  21620. NK_API enum nk_widget_layout_states
  21621. nk_widget_fitting(struct nk_rect *bounds, const struct nk_context *ctx,
  21622. struct nk_vec2 item_padding)
  21623. {
  21624. /* update the bounds to stand without padding */
  21625. enum nk_widget_layout_states state;
  21626. NK_UNUSED(item_padding);
  21627. NK_ASSERT(ctx);
  21628. NK_ASSERT(ctx->current);
  21629. NK_ASSERT(ctx->current->layout);
  21630. if (!ctx || !ctx->current || !ctx->current->layout)
  21631. return NK_WIDGET_INVALID;
  21632. state = nk_widget(bounds, ctx);
  21633. return state;
  21634. }
  21635. NK_API void
  21636. nk_spacing(struct nk_context *ctx, int cols)
  21637. {
  21638. struct nk_window *win;
  21639. struct nk_panel *layout;
  21640. struct nk_rect none;
  21641. int i, index, rows;
  21642. NK_ASSERT(ctx);
  21643. NK_ASSERT(ctx->current);
  21644. NK_ASSERT(ctx->current->layout);
  21645. if (!ctx || !ctx->current || !ctx->current->layout)
  21646. return;
  21647. /* spacing over row boundaries */
  21648. win = ctx->current;
  21649. layout = win->layout;
  21650. index = (layout->row.index + cols) % layout->row.columns;
  21651. rows = (layout->row.index + cols) / layout->row.columns;
  21652. if (rows) {
  21653. for (i = 0; i < rows; ++i)
  21654. nk_panel_alloc_row(ctx, win);
  21655. cols = index;
  21656. }
  21657. /* non table layout need to allocate space */
  21658. if (layout->row.type != NK_LAYOUT_DYNAMIC_FIXED &&
  21659. layout->row.type != NK_LAYOUT_STATIC_FIXED) {
  21660. for (i = 0; i < cols; ++i)
  21661. nk_panel_alloc_space(&none, ctx);
  21662. } layout->row.index = index;
  21663. }
  21664. NK_API void
  21665. nk_widget_disable_begin(struct nk_context* ctx)
  21666. {
  21667. struct nk_window* win;
  21668. struct nk_style* style;
  21669. NK_ASSERT(ctx);
  21670. NK_ASSERT(ctx->current);
  21671. if (!ctx || !ctx->current)
  21672. return;
  21673. win = ctx->current;
  21674. style = &ctx->style;
  21675. win->widgets_disabled = nk_true;
  21676. style->button.color_factor_text = style->button.disabled_factor;
  21677. style->button.color_factor_background = style->button.disabled_factor;
  21678. style->chart.color_factor = style->chart.disabled_factor;
  21679. style->checkbox.color_factor = style->checkbox.disabled_factor;
  21680. style->combo.color_factor = style->combo.disabled_factor;
  21681. style->combo.button.color_factor_text = style->combo.button.disabled_factor;
  21682. style->combo.button.color_factor_background = style->combo.button.disabled_factor;
  21683. style->contextual_button.color_factor_text = style->contextual_button.disabled_factor;
  21684. style->contextual_button.color_factor_background = style->contextual_button.disabled_factor;
  21685. style->edit.color_factor = style->edit.disabled_factor;
  21686. style->edit.scrollbar.color_factor = style->edit.scrollbar.disabled_factor;
  21687. style->menu_button.color_factor_text = style->menu_button.disabled_factor;
  21688. style->menu_button.color_factor_background = style->menu_button.disabled_factor;
  21689. style->option.color_factor = style->option.disabled_factor;
  21690. style->progress.color_factor = style->progress.disabled_factor;
  21691. style->property.color_factor = style->property.disabled_factor;
  21692. style->property.inc_button.color_factor_text = style->property.inc_button.disabled_factor;
  21693. style->property.inc_button.color_factor_background = style->property.inc_button.disabled_factor;
  21694. style->property.dec_button.color_factor_text = style->property.dec_button.disabled_factor;
  21695. style->property.dec_button.color_factor_background = style->property.dec_button.disabled_factor;
  21696. style->property.edit.color_factor = style->property.edit.disabled_factor;
  21697. style->scrollh.color_factor = style->scrollh.disabled_factor;
  21698. style->scrollh.inc_button.color_factor_text = style->scrollh.inc_button.disabled_factor;
  21699. style->scrollh.inc_button.color_factor_background = style->scrollh.inc_button.disabled_factor;
  21700. style->scrollh.dec_button.color_factor_text = style->scrollh.dec_button.disabled_factor;
  21701. style->scrollh.dec_button.color_factor_background = style->scrollh.dec_button.disabled_factor;
  21702. style->scrollv.color_factor = style->scrollv.disabled_factor;
  21703. style->scrollv.inc_button.color_factor_text = style->scrollv.inc_button.disabled_factor;
  21704. style->scrollv.inc_button.color_factor_background = style->scrollv.inc_button.disabled_factor;
  21705. style->scrollv.dec_button.color_factor_text = style->scrollv.dec_button.disabled_factor;
  21706. style->scrollv.dec_button.color_factor_background = style->scrollv.dec_button.disabled_factor;
  21707. style->selectable.color_factor = style->selectable.disabled_factor;
  21708. style->slider.color_factor = style->slider.disabled_factor;
  21709. style->slider.inc_button.color_factor_text = style->slider.inc_button.disabled_factor;
  21710. style->slider.inc_button.color_factor_background = style->slider.inc_button.disabled_factor;
  21711. style->slider.dec_button.color_factor_text = style->slider.dec_button.disabled_factor;
  21712. style->slider.dec_button.color_factor_background = style->slider.dec_button.disabled_factor;
  21713. style->tab.color_factor = style->tab.disabled_factor;
  21714. style->tab.node_maximize_button.color_factor_text = style->tab.node_maximize_button.disabled_factor;
  21715. style->tab.node_minimize_button.color_factor_text = style->tab.node_minimize_button.disabled_factor;
  21716. style->tab.tab_maximize_button.color_factor_text = style->tab.tab_maximize_button.disabled_factor;
  21717. style->tab.tab_maximize_button.color_factor_background = style->tab.tab_maximize_button.disabled_factor;
  21718. style->tab.tab_minimize_button.color_factor_text = style->tab.tab_minimize_button.disabled_factor;
  21719. style->tab.tab_minimize_button.color_factor_background = style->tab.tab_minimize_button.disabled_factor;
  21720. style->text.color_factor = style->text.disabled_factor;
  21721. }
  21722. NK_API void
  21723. nk_widget_disable_end(struct nk_context* ctx)
  21724. {
  21725. struct nk_window* win;
  21726. struct nk_style* style;
  21727. NK_ASSERT(ctx);
  21728. NK_ASSERT(ctx->current);
  21729. if (!ctx || !ctx->current)
  21730. return;
  21731. win = ctx->current;
  21732. style = &ctx->style;
  21733. win->widgets_disabled = nk_false;
  21734. style->button.color_factor_text = 1.0f;
  21735. style->button.color_factor_background = 1.0f;
  21736. style->chart.color_factor = 1.0f;
  21737. style->checkbox.color_factor = 1.0f;
  21738. style->combo.color_factor = 1.0f;
  21739. style->combo.button.color_factor_text = 1.0f;
  21740. style->combo.button.color_factor_background = 1.0f;
  21741. style->contextual_button.color_factor_text = 1.0f;
  21742. style->contextual_button.color_factor_background = 1.0f;
  21743. style->edit.color_factor = 1.0f;
  21744. style->edit.scrollbar.color_factor = 1.0f;
  21745. style->menu_button.color_factor_text = 1.0f;
  21746. style->menu_button.color_factor_background = 1.0f;
  21747. style->option.color_factor = 1.0f;
  21748. style->progress.color_factor = 1.0f;
  21749. style->property.color_factor = 1.0f;
  21750. style->property.inc_button.color_factor_text = 1.0f;
  21751. style->property.inc_button.color_factor_background = 1.0f;
  21752. style->property.dec_button.color_factor_text = 1.0f;
  21753. style->property.dec_button.color_factor_background = 1.0f;
  21754. style->property.edit.color_factor = 1.0f;
  21755. style->scrollh.color_factor = 1.0f;
  21756. style->scrollh.inc_button.color_factor_text = 1.0f;
  21757. style->scrollh.inc_button.color_factor_background = 1.0f;
  21758. style->scrollh.dec_button.color_factor_text = 1.0f;
  21759. style->scrollh.dec_button.color_factor_background = 1.0f;
  21760. style->scrollv.color_factor = 1.0f;
  21761. style->scrollv.inc_button.color_factor_text = 1.0f;
  21762. style->scrollv.inc_button.color_factor_background = 1.0f;
  21763. style->scrollv.dec_button.color_factor_text = 1.0f;
  21764. style->scrollv.dec_button.color_factor_background = 1.0f;
  21765. style->selectable.color_factor = 1.0f;
  21766. style->slider.color_factor = 1.0f;
  21767. style->slider.inc_button.color_factor_text = 1.0f;
  21768. style->slider.inc_button.color_factor_background = 1.0f;
  21769. style->slider.dec_button.color_factor_text = 1.0f;
  21770. style->slider.dec_button.color_factor_background = 1.0f;
  21771. style->tab.color_factor = 1.0f;
  21772. style->tab.node_maximize_button.color_factor_text = 1.0f;
  21773. style->tab.node_minimize_button.color_factor_text = 1.0f;
  21774. style->tab.tab_maximize_button.color_factor_text = 1.0f;
  21775. style->tab.tab_maximize_button.color_factor_background = 1.0f;
  21776. style->tab.tab_minimize_button.color_factor_text = 1.0f;
  21777. style->tab.tab_minimize_button.color_factor_background = 1.0f;
  21778. style->text.color_factor = 1.0f;
  21779. }
  21780. /* ===============================================================
  21781. *
  21782. * TEXT
  21783. *
  21784. * ===============================================================*/
  21785. NK_LIB void
  21786. nk_widget_text(struct nk_command_buffer *o, struct nk_rect b,
  21787. const char *string, int len, const struct nk_text *t,
  21788. nk_flags a, const struct nk_user_font *f)
  21789. {
  21790. struct nk_rect label;
  21791. float text_width;
  21792. NK_ASSERT(o);
  21793. NK_ASSERT(t);
  21794. if (!o || !t) return;
  21795. b.h = NK_MAX(b.h, 2 * t->padding.y);
  21796. label.x = 0; label.w = 0;
  21797. label.y = b.y + t->padding.y;
  21798. label.h = NK_MIN(f->height, b.h - 2 * t->padding.y);
  21799. text_width = f->width(f->userdata, f->height, (const char*)string, len);
  21800. text_width += (2.0f * t->padding.x);
  21801. /* align in x-axis */
  21802. if (a & NK_TEXT_ALIGN_LEFT) {
  21803. label.x = b.x + t->padding.x;
  21804. label.w = NK_MAX(0, b.w - 2 * t->padding.x);
  21805. } else if (a & NK_TEXT_ALIGN_CENTERED) {
  21806. label.w = NK_MAX(1, 2 * t->padding.x + (float)text_width);
  21807. label.x = (b.x + t->padding.x + ((b.w - 2 * t->padding.x) - label.w) / 2);
  21808. label.x = NK_MAX(b.x + t->padding.x, label.x);
  21809. label.w = NK_MIN(b.x + b.w, label.x + label.w);
  21810. if (label.w >= label.x) label.w -= label.x;
  21811. } else if (a & NK_TEXT_ALIGN_RIGHT) {
  21812. label.x = NK_MAX(b.x + t->padding.x, (b.x + b.w) - (2 * t->padding.x + (float)text_width));
  21813. label.w = (float)text_width + 2 * t->padding.x;
  21814. } else return;
  21815. /* align in y-axis */
  21816. if (a & NK_TEXT_ALIGN_MIDDLE) {
  21817. label.y = b.y + b.h/2.0f - (float)f->height/2.0f;
  21818. label.h = NK_MAX(b.h/2.0f, b.h - (b.h/2.0f + f->height/2.0f));
  21819. } else if (a & NK_TEXT_ALIGN_BOTTOM) {
  21820. label.y = b.y + b.h - f->height;
  21821. label.h = f->height;
  21822. }
  21823. nk_draw_text(o, label, (const char*)string, len, f, t->background, t->text);
  21824. }
  21825. NK_LIB void
  21826. nk_widget_text_wrap(struct nk_command_buffer *o, struct nk_rect b,
  21827. const char *string, int len, const struct nk_text *t,
  21828. const struct nk_user_font *f)
  21829. {
  21830. float width;
  21831. int glyphs = 0;
  21832. int fitting = 0;
  21833. int done = 0;
  21834. struct nk_rect line;
  21835. struct nk_text text;
  21836. NK_INTERN nk_rune seperator[] = {' '};
  21837. NK_ASSERT(o);
  21838. NK_ASSERT(t);
  21839. if (!o || !t) return;
  21840. text.padding = nk_vec2(0,0);
  21841. text.background = t->background;
  21842. text.text = t->text;
  21843. b.w = NK_MAX(b.w, 2 * t->padding.x);
  21844. b.h = NK_MAX(b.h, 2 * t->padding.y);
  21845. b.h = b.h - 2 * t->padding.y;
  21846. line.x = b.x + t->padding.x;
  21847. line.y = b.y + t->padding.y;
  21848. line.w = b.w - 2 * t->padding.x;
  21849. line.h = 2 * t->padding.y + f->height;
  21850. fitting = nk_text_clamp(f, string, len, line.w, &glyphs, &width, seperator,NK_LEN(seperator));
  21851. while (done < len) {
  21852. if (!fitting || line.y + line.h >= (b.y + b.h)) break;
  21853. nk_widget_text(o, line, &string[done], fitting, &text, NK_TEXT_LEFT, f);
  21854. done += fitting;
  21855. line.y += f->height + 2 * t->padding.y;
  21856. fitting = nk_text_clamp(f, &string[done], len - done, line.w, &glyphs, &width, seperator,NK_LEN(seperator));
  21857. }
  21858. }
  21859. NK_API void
  21860. nk_text_colored(struct nk_context *ctx, const char *str, int len,
  21861. nk_flags alignment, struct nk_color color)
  21862. {
  21863. struct nk_window *win;
  21864. const struct nk_style *style;
  21865. struct nk_vec2 item_padding;
  21866. struct nk_rect bounds;
  21867. struct nk_text text;
  21868. NK_ASSERT(ctx);
  21869. NK_ASSERT(ctx->current);
  21870. NK_ASSERT(ctx->current->layout);
  21871. if (!ctx || !ctx->current || !ctx->current->layout) return;
  21872. win = ctx->current;
  21873. style = &ctx->style;
  21874. nk_panel_alloc_space(&bounds, ctx);
  21875. item_padding = style->text.padding;
  21876. text.padding.x = item_padding.x;
  21877. text.padding.y = item_padding.y;
  21878. text.background = style->window.background;
  21879. text.text = nk_rgb_factor(color, style->text.color_factor);
  21880. nk_widget_text(&win->buffer, bounds, str, len, &text, alignment, style->font);
  21881. }
  21882. NK_API void
  21883. nk_text_wrap_colored(struct nk_context *ctx, const char *str,
  21884. int len, struct nk_color color)
  21885. {
  21886. struct nk_window *win;
  21887. const struct nk_style *style;
  21888. struct nk_vec2 item_padding;
  21889. struct nk_rect bounds;
  21890. struct nk_text text;
  21891. NK_ASSERT(ctx);
  21892. NK_ASSERT(ctx->current);
  21893. NK_ASSERT(ctx->current->layout);
  21894. if (!ctx || !ctx->current || !ctx->current->layout) return;
  21895. win = ctx->current;
  21896. style = &ctx->style;
  21897. nk_panel_alloc_space(&bounds, ctx);
  21898. item_padding = style->text.padding;
  21899. text.padding.x = item_padding.x;
  21900. text.padding.y = item_padding.y;
  21901. text.background = style->window.background;
  21902. text.text = nk_rgb_factor(color, style->text.color_factor);
  21903. nk_widget_text_wrap(&win->buffer, bounds, str, len, &text, style->font);
  21904. }
  21905. #ifdef NK_INCLUDE_STANDARD_VARARGS
  21906. NK_API void
  21907. nk_labelf_colored(struct nk_context *ctx, nk_flags flags,
  21908. struct nk_color color, const char *fmt, ...)
  21909. {
  21910. va_list args;
  21911. va_start(args, fmt);
  21912. nk_labelfv_colored(ctx, flags, color, fmt, args);
  21913. va_end(args);
  21914. }
  21915. NK_API void
  21916. nk_labelf_colored_wrap(struct nk_context *ctx, struct nk_color color,
  21917. const char *fmt, ...)
  21918. {
  21919. va_list args;
  21920. va_start(args, fmt);
  21921. nk_labelfv_colored_wrap(ctx, color, fmt, args);
  21922. va_end(args);
  21923. }
  21924. NK_API void
  21925. nk_labelf(struct nk_context *ctx, nk_flags flags, const char *fmt, ...)
  21926. {
  21927. va_list args;
  21928. va_start(args, fmt);
  21929. nk_labelfv(ctx, flags, fmt, args);
  21930. va_end(args);
  21931. }
  21932. NK_API void
  21933. nk_labelf_wrap(struct nk_context *ctx, const char *fmt,...)
  21934. {
  21935. va_list args;
  21936. va_start(args, fmt);
  21937. nk_labelfv_wrap(ctx, fmt, args);
  21938. va_end(args);
  21939. }
  21940. NK_API void
  21941. nk_labelfv_colored(struct nk_context *ctx, nk_flags flags,
  21942. struct nk_color color, const char *fmt, va_list args)
  21943. {
  21944. char buf[256];
  21945. nk_strfmt(buf, NK_LEN(buf), fmt, args);
  21946. nk_label_colored(ctx, buf, flags, color);
  21947. }
  21948. NK_API void
  21949. nk_labelfv_colored_wrap(struct nk_context *ctx, struct nk_color color,
  21950. const char *fmt, va_list args)
  21951. {
  21952. char buf[256];
  21953. nk_strfmt(buf, NK_LEN(buf), fmt, args);
  21954. nk_label_colored_wrap(ctx, buf, color);
  21955. }
  21956. NK_API void
  21957. nk_labelfv(struct nk_context *ctx, nk_flags flags, const char *fmt, va_list args)
  21958. {
  21959. char buf[256];
  21960. nk_strfmt(buf, NK_LEN(buf), fmt, args);
  21961. nk_label(ctx, buf, flags);
  21962. }
  21963. NK_API void
  21964. nk_labelfv_wrap(struct nk_context *ctx, const char *fmt, va_list args)
  21965. {
  21966. char buf[256];
  21967. nk_strfmt(buf, NK_LEN(buf), fmt, args);
  21968. nk_label_wrap(ctx, buf);
  21969. }
  21970. NK_API void
  21971. nk_value_bool(struct nk_context *ctx, const char *prefix, int value)
  21972. {
  21973. nk_labelf(ctx, NK_TEXT_LEFT, "%s: %s", prefix, ((value) ? "true": "false"));
  21974. }
  21975. NK_API void
  21976. nk_value_int(struct nk_context *ctx, const char *prefix, int value)
  21977. {
  21978. nk_labelf(ctx, NK_TEXT_LEFT, "%s: %d", prefix, value);
  21979. }
  21980. NK_API void
  21981. nk_value_uint(struct nk_context *ctx, const char *prefix, unsigned int value)
  21982. {
  21983. nk_labelf(ctx, NK_TEXT_LEFT, "%s: %u", prefix, value);
  21984. }
  21985. NK_API void
  21986. nk_value_float(struct nk_context *ctx, const char *prefix, float value)
  21987. {
  21988. double double_value = (double)value;
  21989. nk_labelf(ctx, NK_TEXT_LEFT, "%s: %.3f", prefix, double_value);
  21990. }
  21991. NK_API void
  21992. nk_value_color_byte(struct nk_context *ctx, const char *p, struct nk_color c)
  21993. {
  21994. nk_labelf(ctx, NK_TEXT_LEFT, "%s: (%d, %d, %d, %d)", p, c.r, c.g, c.b, c.a);
  21995. }
  21996. NK_API void
  21997. nk_value_color_float(struct nk_context *ctx, const char *p, struct nk_color color)
  21998. {
  21999. double c[4]; nk_color_dv(c, color);
  22000. nk_labelf(ctx, NK_TEXT_LEFT, "%s: (%.2f, %.2f, %.2f, %.2f)",
  22001. p, c[0], c[1], c[2], c[3]);
  22002. }
  22003. NK_API void
  22004. nk_value_color_hex(struct nk_context *ctx, const char *prefix, struct nk_color color)
  22005. {
  22006. char hex[16];
  22007. nk_color_hex_rgba(hex, color);
  22008. nk_labelf(ctx, NK_TEXT_LEFT, "%s: %s", prefix, hex);
  22009. }
  22010. #endif
  22011. NK_API void
  22012. nk_text(struct nk_context *ctx, const char *str, int len, nk_flags alignment)
  22013. {
  22014. NK_ASSERT(ctx);
  22015. if (!ctx) return;
  22016. nk_text_colored(ctx, str, len, alignment, ctx->style.text.color);
  22017. }
  22018. NK_API void
  22019. nk_text_wrap(struct nk_context *ctx, const char *str, int len)
  22020. {
  22021. NK_ASSERT(ctx);
  22022. if (!ctx) return;
  22023. nk_text_wrap_colored(ctx, str, len, ctx->style.text.color);
  22024. }
  22025. NK_API void
  22026. nk_label(struct nk_context *ctx, const char *str, nk_flags alignment)
  22027. {
  22028. nk_text(ctx, str, nk_strlen(str), alignment);
  22029. }
  22030. NK_API void
  22031. nk_label_colored(struct nk_context *ctx, const char *str, nk_flags align,
  22032. struct nk_color color)
  22033. {
  22034. nk_text_colored(ctx, str, nk_strlen(str), align, color);
  22035. }
  22036. NK_API void
  22037. nk_label_wrap(struct nk_context *ctx, const char *str)
  22038. {
  22039. nk_text_wrap(ctx, str, nk_strlen(str));
  22040. }
  22041. NK_API void
  22042. nk_label_colored_wrap(struct nk_context *ctx, const char *str, struct nk_color color)
  22043. {
  22044. nk_text_wrap_colored(ctx, str, nk_strlen(str), color);
  22045. }
  22046. /* ===============================================================
  22047. *
  22048. * IMAGE
  22049. *
  22050. * ===============================================================*/
  22051. NK_API nk_handle
  22052. nk_handle_ptr(void *ptr)
  22053. {
  22054. nk_handle handle = {0};
  22055. handle.ptr = ptr;
  22056. return handle;
  22057. }
  22058. NK_API nk_handle
  22059. nk_handle_id(int id)
  22060. {
  22061. nk_handle handle;
  22062. nk_zero_struct(handle);
  22063. handle.id = id;
  22064. return handle;
  22065. }
  22066. NK_API struct nk_image
  22067. nk_subimage_ptr(void *ptr, nk_ushort w, nk_ushort h, struct nk_rect r)
  22068. {
  22069. struct nk_image s;
  22070. nk_zero(&s, sizeof(s));
  22071. s.handle.ptr = ptr;
  22072. s.w = w; s.h = h;
  22073. s.region[0] = (nk_ushort)r.x;
  22074. s.region[1] = (nk_ushort)r.y;
  22075. s.region[2] = (nk_ushort)r.w;
  22076. s.region[3] = (nk_ushort)r.h;
  22077. return s;
  22078. }
  22079. NK_API struct nk_image
  22080. nk_subimage_id(int id, nk_ushort w, nk_ushort h, struct nk_rect r)
  22081. {
  22082. struct nk_image s;
  22083. nk_zero(&s, sizeof(s));
  22084. s.handle.id = id;
  22085. s.w = w; s.h = h;
  22086. s.region[0] = (nk_ushort)r.x;
  22087. s.region[1] = (nk_ushort)r.y;
  22088. s.region[2] = (nk_ushort)r.w;
  22089. s.region[3] = (nk_ushort)r.h;
  22090. return s;
  22091. }
  22092. NK_API struct nk_image
  22093. nk_subimage_handle(nk_handle handle, nk_ushort w, nk_ushort h, struct nk_rect r)
  22094. {
  22095. struct nk_image s;
  22096. nk_zero(&s, sizeof(s));
  22097. s.handle = handle;
  22098. s.w = w; s.h = h;
  22099. s.region[0] = (nk_ushort)r.x;
  22100. s.region[1] = (nk_ushort)r.y;
  22101. s.region[2] = (nk_ushort)r.w;
  22102. s.region[3] = (nk_ushort)r.h;
  22103. return s;
  22104. }
  22105. NK_API struct nk_image
  22106. nk_image_handle(nk_handle handle)
  22107. {
  22108. struct nk_image s;
  22109. nk_zero(&s, sizeof(s));
  22110. s.handle = handle;
  22111. s.w = 0; s.h = 0;
  22112. s.region[0] = 0;
  22113. s.region[1] = 0;
  22114. s.region[2] = 0;
  22115. s.region[3] = 0;
  22116. return s;
  22117. }
  22118. NK_API struct nk_image
  22119. nk_image_ptr(void *ptr)
  22120. {
  22121. struct nk_image s;
  22122. nk_zero(&s, sizeof(s));
  22123. NK_ASSERT(ptr);
  22124. s.handle.ptr = ptr;
  22125. s.w = 0; s.h = 0;
  22126. s.region[0] = 0;
  22127. s.region[1] = 0;
  22128. s.region[2] = 0;
  22129. s.region[3] = 0;
  22130. return s;
  22131. }
  22132. NK_API struct nk_image
  22133. nk_image_id(int id)
  22134. {
  22135. struct nk_image s;
  22136. nk_zero(&s, sizeof(s));
  22137. s.handle.id = id;
  22138. s.w = 0; s.h = 0;
  22139. s.region[0] = 0;
  22140. s.region[1] = 0;
  22141. s.region[2] = 0;
  22142. s.region[3] = 0;
  22143. return s;
  22144. }
  22145. NK_API nk_bool
  22146. nk_image_is_subimage(const struct nk_image* img)
  22147. {
  22148. NK_ASSERT(img);
  22149. return !(img->w == 0 && img->h == 0);
  22150. }
  22151. NK_API void
  22152. nk_image(struct nk_context *ctx, struct nk_image img)
  22153. {
  22154. struct nk_window *win;
  22155. struct nk_rect bounds;
  22156. NK_ASSERT(ctx);
  22157. NK_ASSERT(ctx->current);
  22158. NK_ASSERT(ctx->current->layout);
  22159. if (!ctx || !ctx->current || !ctx->current->layout) return;
  22160. win = ctx->current;
  22161. if (!nk_widget(&bounds, ctx)) return;
  22162. nk_draw_image(&win->buffer, bounds, &img, nk_white);
  22163. }
  22164. NK_API void
  22165. nk_image_color(struct nk_context *ctx, struct nk_image img, struct nk_color col)
  22166. {
  22167. struct nk_window *win;
  22168. struct nk_rect bounds;
  22169. NK_ASSERT(ctx);
  22170. NK_ASSERT(ctx->current);
  22171. NK_ASSERT(ctx->current->layout);
  22172. if (!ctx || !ctx->current || !ctx->current->layout) return;
  22173. win = ctx->current;
  22174. if (!nk_widget(&bounds, ctx)) return;
  22175. nk_draw_image(&win->buffer, bounds, &img, col);
  22176. }
  22177. /* ===============================================================
  22178. *
  22179. * 9-SLICE
  22180. *
  22181. * ===============================================================*/
  22182. NK_API struct nk_nine_slice
  22183. 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)
  22184. {
  22185. struct nk_nine_slice s;
  22186. struct nk_image *i = &s.img;
  22187. nk_zero(&s, sizeof(s));
  22188. i->handle.ptr = ptr;
  22189. i->w = w; i->h = h;
  22190. i->region[0] = (nk_ushort)rgn.x;
  22191. i->region[1] = (nk_ushort)rgn.y;
  22192. i->region[2] = (nk_ushort)rgn.w;
  22193. i->region[3] = (nk_ushort)rgn.h;
  22194. s.l = l; s.t = t; s.r = r; s.b = b;
  22195. return s;
  22196. }
  22197. NK_API struct nk_nine_slice
  22198. 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)
  22199. {
  22200. struct nk_nine_slice s;
  22201. struct nk_image *i = &s.img;
  22202. nk_zero(&s, sizeof(s));
  22203. i->handle.id = id;
  22204. i->w = w; i->h = h;
  22205. i->region[0] = (nk_ushort)rgn.x;
  22206. i->region[1] = (nk_ushort)rgn.y;
  22207. i->region[2] = (nk_ushort)rgn.w;
  22208. i->region[3] = (nk_ushort)rgn.h;
  22209. s.l = l; s.t = t; s.r = r; s.b = b;
  22210. return s;
  22211. }
  22212. NK_API struct nk_nine_slice
  22213. 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)
  22214. {
  22215. struct nk_nine_slice s;
  22216. struct nk_image *i = &s.img;
  22217. nk_zero(&s, sizeof(s));
  22218. i->handle = handle;
  22219. i->w = w; i->h = h;
  22220. i->region[0] = (nk_ushort)rgn.x;
  22221. i->region[1] = (nk_ushort)rgn.y;
  22222. i->region[2] = (nk_ushort)rgn.w;
  22223. i->region[3] = (nk_ushort)rgn.h;
  22224. s.l = l; s.t = t; s.r = r; s.b = b;
  22225. return s;
  22226. }
  22227. NK_API struct nk_nine_slice
  22228. nk_nine_slice_handle(nk_handle handle, nk_ushort l, nk_ushort t, nk_ushort r, nk_ushort b)
  22229. {
  22230. struct nk_nine_slice s;
  22231. struct nk_image *i = &s.img;
  22232. nk_zero(&s, sizeof(s));
  22233. i->handle = handle;
  22234. i->w = 0; i->h = 0;
  22235. i->region[0] = 0;
  22236. i->region[1] = 0;
  22237. i->region[2] = 0;
  22238. i->region[3] = 0;
  22239. s.l = l; s.t = t; s.r = r; s.b = b;
  22240. return s;
  22241. }
  22242. NK_API struct nk_nine_slice
  22243. nk_nine_slice_ptr(void *ptr, nk_ushort l, nk_ushort t, nk_ushort r, nk_ushort b)
  22244. {
  22245. struct nk_nine_slice s;
  22246. struct nk_image *i = &s.img;
  22247. nk_zero(&s, sizeof(s));
  22248. NK_ASSERT(ptr);
  22249. i->handle.ptr = ptr;
  22250. i->w = 0; i->h = 0;
  22251. i->region[0] = 0;
  22252. i->region[1] = 0;
  22253. i->region[2] = 0;
  22254. i->region[3] = 0;
  22255. s.l = l; s.t = t; s.r = r; s.b = b;
  22256. return s;
  22257. }
  22258. NK_API struct nk_nine_slice
  22259. nk_nine_slice_id(int id, nk_ushort l, nk_ushort t, nk_ushort r, nk_ushort b)
  22260. {
  22261. struct nk_nine_slice s;
  22262. struct nk_image *i = &s.img;
  22263. nk_zero(&s, sizeof(s));
  22264. i->handle.id = id;
  22265. i->w = 0; i->h = 0;
  22266. i->region[0] = 0;
  22267. i->region[1] = 0;
  22268. i->region[2] = 0;
  22269. i->region[3] = 0;
  22270. s.l = l; s.t = t; s.r = r; s.b = b;
  22271. return s;
  22272. }
  22273. NK_API int
  22274. nk_nine_slice_is_sub9slice(const struct nk_nine_slice* slice)
  22275. {
  22276. NK_ASSERT(slice);
  22277. return !(slice->img.w == 0 && slice->img.h == 0);
  22278. }
  22279. /* ==============================================================
  22280. *
  22281. * BUTTON
  22282. *
  22283. * ===============================================================*/
  22284. NK_LIB void
  22285. nk_draw_symbol(struct nk_command_buffer *out, enum nk_symbol_type type,
  22286. struct nk_rect content, struct nk_color background, struct nk_color foreground,
  22287. float border_width, const struct nk_user_font *font)
  22288. {
  22289. switch (type) {
  22290. case NK_SYMBOL_X:
  22291. case NK_SYMBOL_UNDERSCORE:
  22292. case NK_SYMBOL_PLUS:
  22293. case NK_SYMBOL_MINUS: {
  22294. /* single character text symbol */
  22295. const char *X = (type == NK_SYMBOL_X) ? "x":
  22296. (type == NK_SYMBOL_UNDERSCORE) ? "_":
  22297. (type == NK_SYMBOL_PLUS) ? "+": "-";
  22298. struct nk_text text;
  22299. text.padding = nk_vec2(0,0);
  22300. text.background = background;
  22301. text.text = foreground;
  22302. nk_widget_text(out, content, X, 1, &text, NK_TEXT_CENTERED, font);
  22303. } break;
  22304. case NK_SYMBOL_CIRCLE_SOLID:
  22305. case NK_SYMBOL_CIRCLE_OUTLINE:
  22306. case NK_SYMBOL_RECT_SOLID:
  22307. case NK_SYMBOL_RECT_OUTLINE: {
  22308. /* simple empty/filled shapes */
  22309. if (type == NK_SYMBOL_RECT_SOLID || type == NK_SYMBOL_RECT_OUTLINE) {
  22310. nk_fill_rect(out, content, 0, foreground);
  22311. if (type == NK_SYMBOL_RECT_OUTLINE)
  22312. nk_fill_rect(out, nk_shrink_rect(content, border_width), 0, background);
  22313. } else {
  22314. nk_fill_circle(out, content, foreground);
  22315. if (type == NK_SYMBOL_CIRCLE_OUTLINE)
  22316. nk_fill_circle(out, nk_shrink_rect(content, 1), background);
  22317. }
  22318. } break;
  22319. case NK_SYMBOL_TRIANGLE_UP:
  22320. case NK_SYMBOL_TRIANGLE_DOWN:
  22321. case NK_SYMBOL_TRIANGLE_LEFT:
  22322. case NK_SYMBOL_TRIANGLE_RIGHT: {
  22323. enum nk_heading heading;
  22324. struct nk_vec2 points[3];
  22325. heading = (type == NK_SYMBOL_TRIANGLE_RIGHT) ? NK_RIGHT :
  22326. (type == NK_SYMBOL_TRIANGLE_LEFT) ? NK_LEFT:
  22327. (type == NK_SYMBOL_TRIANGLE_UP) ? NK_UP: NK_DOWN;
  22328. nk_triangle_from_direction(points, content, 0, 0, heading);
  22329. nk_fill_triangle(out, points[0].x, points[0].y, points[1].x, points[1].y,
  22330. points[2].x, points[2].y, foreground);
  22331. } break;
  22332. case NK_SYMBOL_TRIANGLE_UP_OUTLINE:
  22333. case NK_SYMBOL_TRIANGLE_DOWN_OUTLINE:
  22334. case NK_SYMBOL_TRIANGLE_LEFT_OUTLINE:
  22335. case NK_SYMBOL_TRIANGLE_RIGHT_OUTLINE: {
  22336. enum nk_heading heading;
  22337. struct nk_vec2 points[3];
  22338. heading = (type == NK_SYMBOL_TRIANGLE_RIGHT_OUTLINE) ? NK_RIGHT :
  22339. (type == NK_SYMBOL_TRIANGLE_LEFT_OUTLINE) ? NK_LEFT:
  22340. (type == NK_SYMBOL_TRIANGLE_UP_OUTLINE) ? NK_UP: NK_DOWN;
  22341. nk_triangle_from_direction(points, content, 0, 0, heading);
  22342. nk_stroke_triangle(out, points[0].x, points[0].y, points[1].x, points[1].y,
  22343. points[2].x, points[2].y, border_width, foreground);
  22344. } break;
  22345. default:
  22346. case NK_SYMBOL_NONE:
  22347. case NK_SYMBOL_MAX: break;
  22348. }
  22349. }
  22350. NK_LIB nk_bool
  22351. nk_button_behavior(nk_flags *state, struct nk_rect r,
  22352. const struct nk_input *i, enum nk_button_behavior behavior)
  22353. {
  22354. int ret = 0;
  22355. nk_widget_state_reset(state);
  22356. if (!i) return 0;
  22357. if (nk_input_is_mouse_hovering_rect(i, r)) {
  22358. *state = NK_WIDGET_STATE_HOVERED;
  22359. if (nk_input_is_mouse_down(i, NK_BUTTON_LEFT))
  22360. *state = NK_WIDGET_STATE_ACTIVE;
  22361. if (nk_input_has_mouse_click_in_button_rect(i, NK_BUTTON_LEFT, r)) {
  22362. ret = (behavior != NK_BUTTON_DEFAULT) ?
  22363. nk_input_is_mouse_down(i, NK_BUTTON_LEFT):
  22364. #ifdef NK_BUTTON_TRIGGER_ON_RELEASE
  22365. nk_input_is_mouse_released(i, NK_BUTTON_LEFT);
  22366. #else
  22367. nk_input_is_mouse_pressed(i, NK_BUTTON_LEFT);
  22368. #endif
  22369. }
  22370. }
  22371. if (*state & NK_WIDGET_STATE_HOVER && !nk_input_is_mouse_prev_hovering_rect(i, r))
  22372. *state |= NK_WIDGET_STATE_ENTERED;
  22373. else if (nk_input_is_mouse_prev_hovering_rect(i, r))
  22374. *state |= NK_WIDGET_STATE_LEFT;
  22375. return ret;
  22376. }
  22377. NK_LIB const struct nk_style_item*
  22378. nk_draw_button(struct nk_command_buffer *out,
  22379. const struct nk_rect *bounds, nk_flags state,
  22380. const struct nk_style_button *style)
  22381. {
  22382. const struct nk_style_item *background;
  22383. if (state & NK_WIDGET_STATE_HOVER)
  22384. background = &style->hover;
  22385. else if (state & NK_WIDGET_STATE_ACTIVED)
  22386. background = &style->active;
  22387. else background = &style->normal;
  22388. switch (background->type) {
  22389. case NK_STYLE_ITEM_IMAGE:
  22390. nk_draw_image(out, *bounds, &background->data.image, nk_rgb_factor(nk_white, style->color_factor_background));
  22391. break;
  22392. case NK_STYLE_ITEM_NINE_SLICE:
  22393. nk_draw_nine_slice(out, *bounds, &background->data.slice, nk_rgb_factor(nk_white, style->color_factor_background));
  22394. break;
  22395. case NK_STYLE_ITEM_COLOR:
  22396. nk_fill_rect(out, *bounds, style->rounding, nk_rgb_factor(background->data.color, style->color_factor_background));
  22397. nk_stroke_rect(out, *bounds, style->rounding, style->border, nk_rgb_factor(style->border_color, style->color_factor_background));
  22398. break;
  22399. }
  22400. return background;
  22401. }
  22402. NK_LIB nk_bool
  22403. nk_do_button(nk_flags *state, struct nk_command_buffer *out, struct nk_rect r,
  22404. const struct nk_style_button *style, const struct nk_input *in,
  22405. enum nk_button_behavior behavior, struct nk_rect *content)
  22406. {
  22407. struct nk_rect bounds;
  22408. NK_ASSERT(style);
  22409. NK_ASSERT(state);
  22410. NK_ASSERT(out);
  22411. if (!out || !style)
  22412. return nk_false;
  22413. /* calculate button content space */
  22414. content->x = r.x + style->padding.x + style->border + style->rounding;
  22415. content->y = r.y + style->padding.y + style->border + style->rounding;
  22416. content->w = r.w - (2 * (style->padding.x + style->border + style->rounding));
  22417. content->h = r.h - (2 * (style->padding.y + style->border + style->rounding));
  22418. /* execute button behavior */
  22419. bounds.x = r.x - style->touch_padding.x;
  22420. bounds.y = r.y - style->touch_padding.y;
  22421. bounds.w = r.w + 2 * style->touch_padding.x;
  22422. bounds.h = r.h + 2 * style->touch_padding.y;
  22423. return nk_button_behavior(state, bounds, in, behavior);
  22424. }
  22425. NK_LIB void
  22426. nk_draw_button_text(struct nk_command_buffer *out,
  22427. const struct nk_rect *bounds, const struct nk_rect *content, nk_flags state,
  22428. const struct nk_style_button *style, const char *txt, int len,
  22429. nk_flags text_alignment, const struct nk_user_font *font)
  22430. {
  22431. struct nk_text text;
  22432. const struct nk_style_item *background;
  22433. background = nk_draw_button(out, bounds, state, style);
  22434. /* select correct colors/images */
  22435. if (background->type == NK_STYLE_ITEM_COLOR)
  22436. text.background = background->data.color;
  22437. else text.background = style->text_background;
  22438. if (state & NK_WIDGET_STATE_HOVER)
  22439. text.text = style->text_hover;
  22440. else if (state & NK_WIDGET_STATE_ACTIVED)
  22441. text.text = style->text_active;
  22442. else text.text = style->text_normal;
  22443. text.text = nk_rgb_factor(text.text, style->color_factor_text);
  22444. text.padding = nk_vec2(0,0);
  22445. nk_widget_text(out, *content, txt, len, &text, text_alignment, font);
  22446. }
  22447. NK_LIB nk_bool
  22448. nk_do_button_text(nk_flags *state,
  22449. struct nk_command_buffer *out, struct nk_rect bounds,
  22450. const char *string, int len, nk_flags align, enum nk_button_behavior behavior,
  22451. const struct nk_style_button *style, const struct nk_input *in,
  22452. const struct nk_user_font *font)
  22453. {
  22454. struct nk_rect content;
  22455. int ret = nk_false;
  22456. NK_ASSERT(state);
  22457. NK_ASSERT(style);
  22458. NK_ASSERT(out);
  22459. NK_ASSERT(string);
  22460. NK_ASSERT(font);
  22461. if (!out || !style || !font || !string)
  22462. return nk_false;
  22463. ret = nk_do_button(state, out, bounds, style, in, behavior, &content);
  22464. if (style->draw_begin) style->draw_begin(out, style->userdata);
  22465. nk_draw_button_text(out, &bounds, &content, *state, style, string, len, align, font);
  22466. if (style->draw_end) style->draw_end(out, style->userdata);
  22467. return ret;
  22468. }
  22469. NK_LIB void
  22470. nk_draw_button_symbol(struct nk_command_buffer *out,
  22471. const struct nk_rect *bounds, const struct nk_rect *content,
  22472. nk_flags state, const struct nk_style_button *style,
  22473. enum nk_symbol_type type, const struct nk_user_font *font)
  22474. {
  22475. struct nk_color sym, bg;
  22476. const struct nk_style_item *background;
  22477. /* select correct colors/images */
  22478. background = nk_draw_button(out, bounds, state, style);
  22479. if (background->type == NK_STYLE_ITEM_COLOR)
  22480. bg = background->data.color;
  22481. else bg = style->text_background;
  22482. if (state & NK_WIDGET_STATE_HOVER)
  22483. sym = style->text_hover;
  22484. else if (state & NK_WIDGET_STATE_ACTIVED)
  22485. sym = style->text_active;
  22486. else sym = style->text_normal;
  22487. sym = nk_rgb_factor(sym, style->color_factor_text);
  22488. nk_draw_symbol(out, type, *content, bg, sym, 1, font);
  22489. }
  22490. NK_LIB nk_bool
  22491. nk_do_button_symbol(nk_flags *state,
  22492. struct nk_command_buffer *out, struct nk_rect bounds,
  22493. enum nk_symbol_type symbol, enum nk_button_behavior behavior,
  22494. const struct nk_style_button *style, const struct nk_input *in,
  22495. const struct nk_user_font *font)
  22496. {
  22497. int ret;
  22498. struct nk_rect content;
  22499. NK_ASSERT(state);
  22500. NK_ASSERT(style);
  22501. NK_ASSERT(font);
  22502. NK_ASSERT(out);
  22503. if (!out || !style || !font || !state)
  22504. return nk_false;
  22505. ret = nk_do_button(state, out, bounds, style, in, behavior, &content);
  22506. if (style->draw_begin) style->draw_begin(out, style->userdata);
  22507. nk_draw_button_symbol(out, &bounds, &content, *state, style, symbol, font);
  22508. if (style->draw_end) style->draw_end(out, style->userdata);
  22509. return ret;
  22510. }
  22511. NK_LIB void
  22512. nk_draw_button_image(struct nk_command_buffer *out,
  22513. const struct nk_rect *bounds, const struct nk_rect *content,
  22514. nk_flags state, const struct nk_style_button *style, const struct nk_image *img)
  22515. {
  22516. nk_draw_button(out, bounds, state, style);
  22517. nk_draw_image(out, *content, img, nk_rgb_factor(nk_white, style->color_factor_background));
  22518. }
  22519. NK_LIB nk_bool
  22520. nk_do_button_image(nk_flags *state,
  22521. struct nk_command_buffer *out, struct nk_rect bounds,
  22522. struct nk_image img, enum nk_button_behavior b,
  22523. const struct nk_style_button *style, const struct nk_input *in)
  22524. {
  22525. int ret;
  22526. struct nk_rect content;
  22527. NK_ASSERT(state);
  22528. NK_ASSERT(style);
  22529. NK_ASSERT(out);
  22530. if (!out || !style || !state)
  22531. return nk_false;
  22532. ret = nk_do_button(state, out, bounds, style, in, b, &content);
  22533. content.x += style->image_padding.x;
  22534. content.y += style->image_padding.y;
  22535. content.w -= 2 * style->image_padding.x;
  22536. content.h -= 2 * style->image_padding.y;
  22537. if (style->draw_begin) style->draw_begin(out, style->userdata);
  22538. nk_draw_button_image(out, &bounds, &content, *state, style, &img);
  22539. if (style->draw_end) style->draw_end(out, style->userdata);
  22540. return ret;
  22541. }
  22542. NK_LIB void
  22543. nk_draw_button_text_symbol(struct nk_command_buffer *out,
  22544. const struct nk_rect *bounds, const struct nk_rect *label,
  22545. const struct nk_rect *symbol, nk_flags state, const struct nk_style_button *style,
  22546. const char *str, int len, enum nk_symbol_type type,
  22547. const struct nk_user_font *font)
  22548. {
  22549. struct nk_color sym;
  22550. struct nk_text text;
  22551. const struct nk_style_item *background;
  22552. /* select correct background colors/images */
  22553. background = nk_draw_button(out, bounds, state, style);
  22554. if (background->type == NK_STYLE_ITEM_COLOR)
  22555. text.background = background->data.color;
  22556. else text.background = style->text_background;
  22557. /* select correct text colors */
  22558. if (state & NK_WIDGET_STATE_HOVER) {
  22559. sym = style->text_hover;
  22560. text.text = style->text_hover;
  22561. } else if (state & NK_WIDGET_STATE_ACTIVED) {
  22562. sym = style->text_active;
  22563. text.text = style->text_active;
  22564. } else {
  22565. sym = style->text_normal;
  22566. text.text = style->text_normal;
  22567. }
  22568. sym = nk_rgb_factor(sym, style->color_factor_text);
  22569. text.text = nk_rgb_factor(text.text, style->color_factor_text);
  22570. text.padding = nk_vec2(0,0);
  22571. nk_draw_symbol(out, type, *symbol, style->text_background, sym, 0, font);
  22572. nk_widget_text(out, *label, str, len, &text, NK_TEXT_CENTERED, font);
  22573. }
  22574. NK_LIB nk_bool
  22575. nk_do_button_text_symbol(nk_flags *state,
  22576. struct nk_command_buffer *out, struct nk_rect bounds,
  22577. enum nk_symbol_type symbol, const char *str, int len, nk_flags align,
  22578. enum nk_button_behavior behavior, const struct nk_style_button *style,
  22579. const struct nk_user_font *font, const struct nk_input *in)
  22580. {
  22581. int ret;
  22582. struct nk_rect tri = {0,0,0,0};
  22583. struct nk_rect content;
  22584. NK_ASSERT(style);
  22585. NK_ASSERT(out);
  22586. NK_ASSERT(font);
  22587. if (!out || !style || !font)
  22588. return nk_false;
  22589. ret = nk_do_button(state, out, bounds, style, in, behavior, &content);
  22590. tri.y = content.y + (content.h/2) - font->height/2;
  22591. tri.w = font->height; tri.h = font->height;
  22592. if (align & NK_TEXT_ALIGN_LEFT) {
  22593. tri.x = (content.x + content.w) - (2 * style->padding.x + tri.w);
  22594. tri.x = NK_MAX(tri.x, 0);
  22595. } else tri.x = content.x + 2 * style->padding.x;
  22596. /* draw button */
  22597. if (style->draw_begin) style->draw_begin(out, style->userdata);
  22598. nk_draw_button_text_symbol(out, &bounds, &content, &tri,
  22599. *state, style, str, len, symbol, font);
  22600. if (style->draw_end) style->draw_end(out, style->userdata);
  22601. return ret;
  22602. }
  22603. NK_LIB void
  22604. nk_draw_button_text_image(struct nk_command_buffer *out,
  22605. const struct nk_rect *bounds, const struct nk_rect *label,
  22606. const struct nk_rect *image, nk_flags state, const struct nk_style_button *style,
  22607. const char *str, int len, const struct nk_user_font *font,
  22608. const struct nk_image *img)
  22609. {
  22610. struct nk_text text;
  22611. const struct nk_style_item *background;
  22612. background = nk_draw_button(out, bounds, state, style);
  22613. /* select correct colors */
  22614. if (background->type == NK_STYLE_ITEM_COLOR)
  22615. text.background = background->data.color;
  22616. else text.background = style->text_background;
  22617. if (state & NK_WIDGET_STATE_HOVER)
  22618. text.text = style->text_hover;
  22619. else if (state & NK_WIDGET_STATE_ACTIVED)
  22620. text.text = style->text_active;
  22621. else text.text = style->text_normal;
  22622. text.text = nk_rgb_factor(text.text, style->color_factor_text);
  22623. text.padding = nk_vec2(0, 0);
  22624. nk_widget_text(out, *label, str, len, &text, NK_TEXT_CENTERED, font);
  22625. nk_draw_image(out, *image, img, nk_rgb_factor(nk_white, style->color_factor_background));
  22626. }
  22627. NK_LIB nk_bool
  22628. nk_do_button_text_image(nk_flags *state,
  22629. struct nk_command_buffer *out, struct nk_rect bounds,
  22630. struct nk_image img, const char* str, int len, nk_flags align,
  22631. enum nk_button_behavior behavior, const struct nk_style_button *style,
  22632. const struct nk_user_font *font, const struct nk_input *in)
  22633. {
  22634. int ret;
  22635. struct nk_rect icon;
  22636. struct nk_rect content;
  22637. NK_ASSERT(style);
  22638. NK_ASSERT(state);
  22639. NK_ASSERT(font);
  22640. NK_ASSERT(out);
  22641. if (!out || !font || !style || !str)
  22642. return nk_false;
  22643. ret = nk_do_button(state, out, bounds, style, in, behavior, &content);
  22644. icon.y = bounds.y + style->padding.y;
  22645. icon.w = icon.h = bounds.h - 2 * style->padding.y;
  22646. if (align & NK_TEXT_ALIGN_LEFT) {
  22647. icon.x = (bounds.x + bounds.w) - (2 * style->padding.x + icon.w);
  22648. icon.x = NK_MAX(icon.x, 0);
  22649. } else icon.x = bounds.x + 2 * style->padding.x;
  22650. icon.x += style->image_padding.x;
  22651. icon.y += style->image_padding.y;
  22652. icon.w -= 2 * style->image_padding.x;
  22653. icon.h -= 2 * style->image_padding.y;
  22654. if (style->draw_begin) style->draw_begin(out, style->userdata);
  22655. nk_draw_button_text_image(out, &bounds, &content, &icon, *state, style, str, len, font, &img);
  22656. if (style->draw_end) style->draw_end(out, style->userdata);
  22657. return ret;
  22658. }
  22659. NK_API void
  22660. nk_button_set_behavior(struct nk_context *ctx, enum nk_button_behavior behavior)
  22661. {
  22662. NK_ASSERT(ctx);
  22663. if (!ctx) return;
  22664. ctx->button_behavior = behavior;
  22665. }
  22666. NK_API nk_bool
  22667. nk_button_push_behavior(struct nk_context *ctx, enum nk_button_behavior behavior)
  22668. {
  22669. struct nk_config_stack_button_behavior *button_stack;
  22670. struct nk_config_stack_button_behavior_element *element;
  22671. NK_ASSERT(ctx);
  22672. if (!ctx) return 0;
  22673. button_stack = &ctx->stacks.button_behaviors;
  22674. NK_ASSERT(button_stack->head < (int)NK_LEN(button_stack->elements));
  22675. if (button_stack->head >= (int)NK_LEN(button_stack->elements))
  22676. return 0;
  22677. element = &button_stack->elements[button_stack->head++];
  22678. element->address = &ctx->button_behavior;
  22679. element->old_value = ctx->button_behavior;
  22680. ctx->button_behavior = behavior;
  22681. return 1;
  22682. }
  22683. NK_API nk_bool
  22684. nk_button_pop_behavior(struct nk_context *ctx)
  22685. {
  22686. struct nk_config_stack_button_behavior *button_stack;
  22687. struct nk_config_stack_button_behavior_element *element;
  22688. NK_ASSERT(ctx);
  22689. if (!ctx) return 0;
  22690. button_stack = &ctx->stacks.button_behaviors;
  22691. NK_ASSERT(button_stack->head > 0);
  22692. if (button_stack->head < 1)
  22693. return 0;
  22694. element = &button_stack->elements[--button_stack->head];
  22695. *element->address = element->old_value;
  22696. return 1;
  22697. }
  22698. NK_API nk_bool
  22699. nk_button_text_styled(struct nk_context *ctx,
  22700. const struct nk_style_button *style, const char *title, int len)
  22701. {
  22702. struct nk_window *win;
  22703. struct nk_panel *layout;
  22704. const struct nk_input *in;
  22705. struct nk_rect bounds;
  22706. enum nk_widget_layout_states state;
  22707. NK_ASSERT(ctx);
  22708. NK_ASSERT(style);
  22709. NK_ASSERT(ctx->current);
  22710. NK_ASSERT(ctx->current->layout);
  22711. if (!style || !ctx || !ctx->current || !ctx->current->layout) return 0;
  22712. win = ctx->current;
  22713. layout = win->layout;
  22714. state = nk_widget(&bounds, ctx);
  22715. if (!state) return 0;
  22716. in = (state == NK_WIDGET_ROM || state == NK_WIDGET_DISABLED || layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  22717. return nk_do_button_text(&ctx->last_widget_state, &win->buffer, bounds,
  22718. title, len, style->text_alignment, ctx->button_behavior,
  22719. style, in, ctx->style.font);
  22720. }
  22721. NK_API nk_bool
  22722. nk_button_text(struct nk_context *ctx, const char *title, int len)
  22723. {
  22724. NK_ASSERT(ctx);
  22725. if (!ctx) return 0;
  22726. return nk_button_text_styled(ctx, &ctx->style.button, title, len);
  22727. }
  22728. NK_API nk_bool nk_button_label_styled(struct nk_context *ctx,
  22729. const struct nk_style_button *style, const char *title)
  22730. {
  22731. return nk_button_text_styled(ctx, style, title, nk_strlen(title));
  22732. }
  22733. NK_API nk_bool nk_button_label(struct nk_context *ctx, const char *title)
  22734. {
  22735. return nk_button_text(ctx, title, nk_strlen(title));
  22736. }
  22737. NK_API nk_bool
  22738. nk_button_color(struct nk_context *ctx, struct nk_color color)
  22739. {
  22740. struct nk_window *win;
  22741. struct nk_panel *layout;
  22742. const struct nk_input *in;
  22743. struct nk_style_button button;
  22744. int ret = 0;
  22745. struct nk_rect bounds;
  22746. struct nk_rect content;
  22747. enum nk_widget_layout_states state;
  22748. NK_ASSERT(ctx);
  22749. NK_ASSERT(ctx->current);
  22750. NK_ASSERT(ctx->current->layout);
  22751. if (!ctx || !ctx->current || !ctx->current->layout)
  22752. return 0;
  22753. win = ctx->current;
  22754. layout = win->layout;
  22755. state = nk_widget(&bounds, ctx);
  22756. if (!state) return 0;
  22757. in = (state == NK_WIDGET_ROM || state == NK_WIDGET_DISABLED || layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  22758. button = ctx->style.button;
  22759. button.normal = nk_style_item_color(color);
  22760. button.hover = nk_style_item_color(color);
  22761. button.active = nk_style_item_color(color);
  22762. ret = nk_do_button(&ctx->last_widget_state, &win->buffer, bounds,
  22763. &button, in, ctx->button_behavior, &content);
  22764. nk_draw_button(&win->buffer, &bounds, ctx->last_widget_state, &button);
  22765. return ret;
  22766. }
  22767. NK_API nk_bool
  22768. nk_button_symbol_styled(struct nk_context *ctx,
  22769. const struct nk_style_button *style, enum nk_symbol_type symbol)
  22770. {
  22771. struct nk_window *win;
  22772. struct nk_panel *layout;
  22773. const struct nk_input *in;
  22774. struct nk_rect bounds;
  22775. enum nk_widget_layout_states state;
  22776. NK_ASSERT(ctx);
  22777. NK_ASSERT(ctx->current);
  22778. NK_ASSERT(ctx->current->layout);
  22779. if (!ctx || !ctx->current || !ctx->current->layout)
  22780. return 0;
  22781. win = ctx->current;
  22782. layout = win->layout;
  22783. state = nk_widget(&bounds, ctx);
  22784. if (!state) return 0;
  22785. in = (state == NK_WIDGET_ROM || state == NK_WIDGET_DISABLED || layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  22786. return nk_do_button_symbol(&ctx->last_widget_state, &win->buffer, bounds,
  22787. symbol, ctx->button_behavior, style, in, ctx->style.font);
  22788. }
  22789. NK_API nk_bool
  22790. nk_button_symbol(struct nk_context *ctx, enum nk_symbol_type symbol)
  22791. {
  22792. NK_ASSERT(ctx);
  22793. if (!ctx) return 0;
  22794. return nk_button_symbol_styled(ctx, &ctx->style.button, symbol);
  22795. }
  22796. NK_API nk_bool
  22797. nk_button_image_styled(struct nk_context *ctx, const struct nk_style_button *style,
  22798. struct nk_image img)
  22799. {
  22800. struct nk_window *win;
  22801. struct nk_panel *layout;
  22802. const struct nk_input *in;
  22803. struct nk_rect bounds;
  22804. enum nk_widget_layout_states state;
  22805. NK_ASSERT(ctx);
  22806. NK_ASSERT(ctx->current);
  22807. NK_ASSERT(ctx->current->layout);
  22808. if (!ctx || !ctx->current || !ctx->current->layout)
  22809. return 0;
  22810. win = ctx->current;
  22811. layout = win->layout;
  22812. state = nk_widget(&bounds, ctx);
  22813. if (!state) return 0;
  22814. in = (state == NK_WIDGET_ROM || state == NK_WIDGET_DISABLED || layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  22815. return nk_do_button_image(&ctx->last_widget_state, &win->buffer, bounds,
  22816. img, ctx->button_behavior, style, in);
  22817. }
  22818. NK_API nk_bool
  22819. nk_button_image(struct nk_context *ctx, struct nk_image img)
  22820. {
  22821. NK_ASSERT(ctx);
  22822. if (!ctx) return 0;
  22823. return nk_button_image_styled(ctx, &ctx->style.button, img);
  22824. }
  22825. NK_API nk_bool
  22826. nk_button_symbol_text_styled(struct nk_context *ctx,
  22827. const struct nk_style_button *style, enum nk_symbol_type symbol,
  22828. const char *text, int len, nk_flags align)
  22829. {
  22830. struct nk_window *win;
  22831. struct nk_panel *layout;
  22832. const struct nk_input *in;
  22833. struct nk_rect bounds;
  22834. enum nk_widget_layout_states state;
  22835. NK_ASSERT(ctx);
  22836. NK_ASSERT(ctx->current);
  22837. NK_ASSERT(ctx->current->layout);
  22838. if (!ctx || !ctx->current || !ctx->current->layout)
  22839. return 0;
  22840. win = ctx->current;
  22841. layout = win->layout;
  22842. state = nk_widget(&bounds, ctx);
  22843. if (!state) return 0;
  22844. in = (state == NK_WIDGET_ROM || state == NK_WIDGET_DISABLED || layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  22845. return nk_do_button_text_symbol(&ctx->last_widget_state, &win->buffer, bounds,
  22846. symbol, text, len, align, ctx->button_behavior,
  22847. style, ctx->style.font, in);
  22848. }
  22849. NK_API nk_bool
  22850. nk_button_symbol_text(struct nk_context *ctx, enum nk_symbol_type symbol,
  22851. const char* text, int len, nk_flags align)
  22852. {
  22853. NK_ASSERT(ctx);
  22854. if (!ctx) return 0;
  22855. return nk_button_symbol_text_styled(ctx, &ctx->style.button, symbol, text, len, align);
  22856. }
  22857. NK_API nk_bool nk_button_symbol_label(struct nk_context *ctx, enum nk_symbol_type symbol,
  22858. const char *label, nk_flags align)
  22859. {
  22860. return nk_button_symbol_text(ctx, symbol, label, nk_strlen(label), align);
  22861. }
  22862. NK_API nk_bool nk_button_symbol_label_styled(struct nk_context *ctx,
  22863. const struct nk_style_button *style, enum nk_symbol_type symbol,
  22864. const char *title, nk_flags align)
  22865. {
  22866. return nk_button_symbol_text_styled(ctx, style, symbol, title, nk_strlen(title), align);
  22867. }
  22868. NK_API nk_bool
  22869. nk_button_image_text_styled(struct nk_context *ctx,
  22870. const struct nk_style_button *style, struct nk_image img, const char *text,
  22871. int len, nk_flags align)
  22872. {
  22873. struct nk_window *win;
  22874. struct nk_panel *layout;
  22875. const struct nk_input *in;
  22876. struct nk_rect bounds;
  22877. enum nk_widget_layout_states state;
  22878. NK_ASSERT(ctx);
  22879. NK_ASSERT(ctx->current);
  22880. NK_ASSERT(ctx->current->layout);
  22881. if (!ctx || !ctx->current || !ctx->current->layout)
  22882. return 0;
  22883. win = ctx->current;
  22884. layout = win->layout;
  22885. state = nk_widget(&bounds, ctx);
  22886. if (!state) return 0;
  22887. in = (state == NK_WIDGET_ROM || state == NK_WIDGET_DISABLED || layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  22888. return nk_do_button_text_image(&ctx->last_widget_state, &win->buffer,
  22889. bounds, img, text, len, align, ctx->button_behavior,
  22890. style, ctx->style.font, in);
  22891. }
  22892. NK_API nk_bool
  22893. nk_button_image_text(struct nk_context *ctx, struct nk_image img,
  22894. const char *text, int len, nk_flags align)
  22895. {
  22896. return nk_button_image_text_styled(ctx, &ctx->style.button,img, text, len, align);
  22897. }
  22898. NK_API nk_bool nk_button_image_label(struct nk_context *ctx, struct nk_image img,
  22899. const char *label, nk_flags align)
  22900. {
  22901. return nk_button_image_text(ctx, img, label, nk_strlen(label), align);
  22902. }
  22903. NK_API nk_bool nk_button_image_label_styled(struct nk_context *ctx,
  22904. const struct nk_style_button *style, struct nk_image img,
  22905. const char *label, nk_flags text_alignment)
  22906. {
  22907. return nk_button_image_text_styled(ctx, style, img, label, nk_strlen(label), text_alignment);
  22908. }
  22909. /* ===============================================================
  22910. *
  22911. * TOGGLE
  22912. *
  22913. * ===============================================================*/
  22914. NK_LIB nk_bool
  22915. nk_toggle_behavior(const struct nk_input *in, struct nk_rect select,
  22916. nk_flags *state, nk_bool active)
  22917. {
  22918. nk_widget_state_reset(state);
  22919. if (nk_button_behavior(state, select, in, NK_BUTTON_DEFAULT)) {
  22920. *state = NK_WIDGET_STATE_ACTIVE;
  22921. active = !active;
  22922. }
  22923. if (*state & NK_WIDGET_STATE_HOVER && !nk_input_is_mouse_prev_hovering_rect(in, select))
  22924. *state |= NK_WIDGET_STATE_ENTERED;
  22925. else if (nk_input_is_mouse_prev_hovering_rect(in, select))
  22926. *state |= NK_WIDGET_STATE_LEFT;
  22927. return active;
  22928. }
  22929. NK_LIB void
  22930. nk_draw_checkbox(struct nk_command_buffer *out,
  22931. nk_flags state, const struct nk_style_toggle *style, nk_bool active,
  22932. const struct nk_rect *label, const struct nk_rect *selector,
  22933. const struct nk_rect *cursors, const char *string, int len,
  22934. const struct nk_user_font *font, nk_flags text_alignment)
  22935. {
  22936. const struct nk_style_item *background;
  22937. const struct nk_style_item *cursor;
  22938. struct nk_text text;
  22939. /* select correct colors/images */
  22940. if (state & NK_WIDGET_STATE_HOVER) {
  22941. background = &style->hover;
  22942. cursor = &style->cursor_hover;
  22943. text.text = style->text_hover;
  22944. } else if (state & NK_WIDGET_STATE_ACTIVED) {
  22945. background = &style->hover;
  22946. cursor = &style->cursor_hover;
  22947. text.text = style->text_active;
  22948. } else {
  22949. background = &style->normal;
  22950. cursor = &style->cursor_normal;
  22951. text.text = style->text_normal;
  22952. }
  22953. text.text = nk_rgb_factor(text.text, style->color_factor);
  22954. text.padding.x = 0;
  22955. text.padding.y = 0;
  22956. text.background = style->text_background;
  22957. nk_widget_text(out, *label, string, len, &text, text_alignment, font);
  22958. /* draw background and cursor */
  22959. if (background->type == NK_STYLE_ITEM_COLOR) {
  22960. nk_fill_rect(out, *selector, 0, nk_rgb_factor(style->border_color, style->color_factor));
  22961. nk_fill_rect(out, nk_shrink_rect(*selector, style->border), 0, nk_rgb_factor(background->data.color, style->color_factor));
  22962. } else nk_draw_image(out, *selector, &background->data.image, nk_rgb_factor(nk_white, style->color_factor));
  22963. if (active) {
  22964. if (cursor->type == NK_STYLE_ITEM_IMAGE)
  22965. nk_draw_image(out, *cursors, &cursor->data.image, nk_rgb_factor(nk_white, style->color_factor));
  22966. else nk_fill_rect(out, *cursors, 0, cursor->data.color);
  22967. }
  22968. }
  22969. NK_LIB void
  22970. nk_draw_option(struct nk_command_buffer *out,
  22971. nk_flags state, const struct nk_style_toggle *style, nk_bool active,
  22972. const struct nk_rect *label, const struct nk_rect *selector,
  22973. const struct nk_rect *cursors, const char *string, int len,
  22974. const struct nk_user_font *font, nk_flags text_alignment)
  22975. {
  22976. const struct nk_style_item *background;
  22977. const struct nk_style_item *cursor;
  22978. struct nk_text text;
  22979. /* select correct colors/images */
  22980. if (state & NK_WIDGET_STATE_HOVER) {
  22981. background = &style->hover;
  22982. cursor = &style->cursor_hover;
  22983. text.text = style->text_hover;
  22984. } else if (state & NK_WIDGET_STATE_ACTIVED) {
  22985. background = &style->hover;
  22986. cursor = &style->cursor_hover;
  22987. text.text = style->text_active;
  22988. } else {
  22989. background = &style->normal;
  22990. cursor = &style->cursor_normal;
  22991. text.text = style->text_normal;
  22992. }
  22993. text.text = nk_rgb_factor(text.text, style->color_factor);
  22994. text.padding.x = 0;
  22995. text.padding.y = 0;
  22996. text.background = style->text_background;
  22997. nk_widget_text(out, *label, string, len, &text, text_alignment, font);
  22998. /* draw background and cursor */
  22999. if (background->type == NK_STYLE_ITEM_COLOR) {
  23000. nk_fill_circle(out, *selector, nk_rgb_factor(style->border_color, style->color_factor));
  23001. nk_fill_circle(out, nk_shrink_rect(*selector, style->border), nk_rgb_factor(background->data.color, style->color_factor));
  23002. } else nk_draw_image(out, *selector, &background->data.image, nk_rgb_factor(nk_white, style->color_factor));
  23003. if (active) {
  23004. if (cursor->type == NK_STYLE_ITEM_IMAGE)
  23005. nk_draw_image(out, *cursors, &cursor->data.image, nk_rgb_factor(nk_white, style->color_factor));
  23006. else nk_fill_circle(out, *cursors, cursor->data.color);
  23007. }
  23008. }
  23009. NK_LIB nk_bool
  23010. nk_do_toggle(nk_flags *state,
  23011. struct nk_command_buffer *out, struct nk_rect r,
  23012. nk_bool *active, const char *str, int len, enum nk_toggle_type type,
  23013. const struct nk_style_toggle *style, const struct nk_input *in,
  23014. const struct nk_user_font *font, nk_flags widget_alignment, nk_flags text_alignment)
  23015. {
  23016. int was_active;
  23017. struct nk_rect bounds;
  23018. struct nk_rect select;
  23019. struct nk_rect cursor;
  23020. struct nk_rect label;
  23021. NK_ASSERT(style);
  23022. NK_ASSERT(out);
  23023. NK_ASSERT(font);
  23024. if (!out || !style || !font || !active)
  23025. return 0;
  23026. r.w = NK_MAX(r.w, font->height + 2 * style->padding.x);
  23027. r.h = NK_MAX(r.h, font->height + 2 * style->padding.y);
  23028. /* add additional touch padding for touch screen devices */
  23029. bounds.x = r.x - style->touch_padding.x;
  23030. bounds.y = r.y - style->touch_padding.y;
  23031. bounds.w = r.w + 2 * style->touch_padding.x;
  23032. bounds.h = r.h + 2 * style->touch_padding.y;
  23033. /* calculate the selector space */
  23034. select.w = font->height;
  23035. select.h = select.w;
  23036. if (widget_alignment & NK_WIDGET_ALIGN_RIGHT) {
  23037. select.x = r.x + r.w - font->height;
  23038. /* label in front of the selector */
  23039. label.x = r.x;
  23040. label.w = r.w - select.w - style->spacing * 2;
  23041. } else if (widget_alignment & NK_WIDGET_ALIGN_CENTERED) {
  23042. select.x = r.x + (r.w - select.w) / 2;
  23043. /* label in front of selector */
  23044. label.x = r.x;
  23045. label.w = (r.w - select.w - style->spacing * 2) / 2;
  23046. } else { /* Default: NK_WIDGET_ALIGN_LEFT */
  23047. select.x = r.x;
  23048. /* label behind the selector */
  23049. label.x = select.x + select.w + style->spacing;
  23050. label.w = NK_MAX(r.x + r.w, label.x) - label.x;
  23051. }
  23052. if (widget_alignment & NK_WIDGET_ALIGN_TOP) {
  23053. select.y = r.y;
  23054. } else if (widget_alignment & NK_WIDGET_ALIGN_BOTTOM) {
  23055. select.y = r.y + r.h - select.h - 2 * style->padding.y;
  23056. } else { /* Default: NK_WIDGET_ALIGN_MIDDLE */
  23057. select.y = r.y + r.h/2.0f - select.h/2.0f;
  23058. }
  23059. label.y = select.y;
  23060. label.h = select.w;
  23061. /* calculate the bounds of the cursor inside the selector */
  23062. cursor.x = select.x + style->padding.x + style->border;
  23063. cursor.y = select.y + style->padding.y + style->border;
  23064. cursor.w = select.w - (2 * style->padding.x + 2 * style->border);
  23065. cursor.h = select.h - (2 * style->padding.y + 2 * style->border);
  23066. /* update selector */
  23067. was_active = *active;
  23068. *active = nk_toggle_behavior(in, bounds, state, *active);
  23069. /* draw selector */
  23070. if (style->draw_begin)
  23071. style->draw_begin(out, style->userdata);
  23072. if (type == NK_TOGGLE_CHECK) {
  23073. nk_draw_checkbox(out, *state, style, *active, &label, &select, &cursor, str, len, font, text_alignment);
  23074. } else {
  23075. nk_draw_option(out, *state, style, *active, &label, &select, &cursor, str, len, font, text_alignment);
  23076. }
  23077. if (style->draw_end)
  23078. style->draw_end(out, style->userdata);
  23079. return (was_active != *active);
  23080. }
  23081. /*----------------------------------------------------------------
  23082. *
  23083. * CHECKBOX
  23084. *
  23085. * --------------------------------------------------------------*/
  23086. NK_API nk_bool
  23087. nk_check_text(struct nk_context *ctx, const char *text, int len, nk_bool active)
  23088. {
  23089. struct nk_window *win;
  23090. struct nk_panel *layout;
  23091. const struct nk_input *in;
  23092. const struct nk_style *style;
  23093. struct nk_rect bounds;
  23094. enum nk_widget_layout_states state;
  23095. NK_ASSERT(ctx);
  23096. NK_ASSERT(ctx->current);
  23097. NK_ASSERT(ctx->current->layout);
  23098. if (!ctx || !ctx->current || !ctx->current->layout)
  23099. return active;
  23100. win = ctx->current;
  23101. style = &ctx->style;
  23102. layout = win->layout;
  23103. state = nk_widget(&bounds, ctx);
  23104. if (!state) return active;
  23105. in = (state == NK_WIDGET_ROM || state == NK_WIDGET_DISABLED || layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  23106. nk_do_toggle(&ctx->last_widget_state, &win->buffer, bounds, &active,
  23107. text, len, NK_TOGGLE_CHECK, &style->checkbox, in, style->font, NK_WIDGET_LEFT, NK_TEXT_LEFT);
  23108. return active;
  23109. }
  23110. NK_API nk_bool
  23111. nk_check_text_align(struct nk_context *ctx, const char *text, int len, nk_bool active, nk_flags widget_alignment, nk_flags text_alignment)
  23112. {
  23113. struct nk_window *win;
  23114. struct nk_panel *layout;
  23115. const struct nk_input *in;
  23116. const struct nk_style *style;
  23117. struct nk_rect bounds;
  23118. enum nk_widget_layout_states state;
  23119. NK_ASSERT(ctx);
  23120. NK_ASSERT(ctx->current);
  23121. NK_ASSERT(ctx->current->layout);
  23122. if (!ctx || !ctx->current || !ctx->current->layout)
  23123. return active;
  23124. win = ctx->current;
  23125. style = &ctx->style;
  23126. layout = win->layout;
  23127. state = nk_widget(&bounds, ctx);
  23128. if (!state) return active;
  23129. in = (state == NK_WIDGET_ROM || state == NK_WIDGET_DISABLED || layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  23130. nk_do_toggle(&ctx->last_widget_state, &win->buffer, bounds, &active,
  23131. text, len, NK_TOGGLE_CHECK, &style->checkbox, in, style->font, widget_alignment, text_alignment);
  23132. return active;
  23133. }
  23134. NK_API unsigned int
  23135. nk_check_flags_text(struct nk_context *ctx, const char *text, int len,
  23136. unsigned int flags, unsigned int value)
  23137. {
  23138. int old_active;
  23139. NK_ASSERT(ctx);
  23140. NK_ASSERT(text);
  23141. if (!ctx || !text) return flags;
  23142. old_active = (int)((flags & value) & value);
  23143. if (nk_check_text(ctx, text, len, old_active))
  23144. flags |= value;
  23145. else flags &= ~value;
  23146. return flags;
  23147. }
  23148. NK_API nk_bool
  23149. nk_checkbox_text(struct nk_context *ctx, const char *text, int len, nk_bool *active)
  23150. {
  23151. int old_val;
  23152. NK_ASSERT(ctx);
  23153. NK_ASSERT(text);
  23154. NK_ASSERT(active);
  23155. if (!ctx || !text || !active) return 0;
  23156. old_val = *active;
  23157. *active = nk_check_text(ctx, text, len, *active);
  23158. return old_val != *active;
  23159. }
  23160. NK_API nk_bool
  23161. nk_checkbox_text_align(struct nk_context *ctx, const char *text, int len, nk_bool *active, nk_flags widget_alignment, nk_flags text_alignment)
  23162. {
  23163. int old_val;
  23164. NK_ASSERT(ctx);
  23165. NK_ASSERT(text);
  23166. NK_ASSERT(active);
  23167. if (!ctx || !text || !active) return 0;
  23168. old_val = *active;
  23169. *active = nk_check_text_align(ctx, text, len, *active, widget_alignment, text_alignment);
  23170. return old_val != *active;
  23171. }
  23172. NK_API nk_bool
  23173. nk_checkbox_flags_text(struct nk_context *ctx, const char *text, int len,
  23174. unsigned int *flags, unsigned int value)
  23175. {
  23176. nk_bool active;
  23177. NK_ASSERT(ctx);
  23178. NK_ASSERT(text);
  23179. NK_ASSERT(flags);
  23180. if (!ctx || !text || !flags) return 0;
  23181. active = (int)((*flags & value) & value);
  23182. if (nk_checkbox_text(ctx, text, len, &active)) {
  23183. if (active) *flags |= value;
  23184. else *flags &= ~value;
  23185. return 1;
  23186. }
  23187. return 0;
  23188. }
  23189. NK_API nk_bool nk_check_label(struct nk_context *ctx, const char *label, nk_bool active)
  23190. {
  23191. return nk_check_text(ctx, label, nk_strlen(label), active);
  23192. }
  23193. NK_API unsigned int nk_check_flags_label(struct nk_context *ctx, const char *label,
  23194. unsigned int flags, unsigned int value)
  23195. {
  23196. return nk_check_flags_text(ctx, label, nk_strlen(label), flags, value);
  23197. }
  23198. NK_API nk_bool nk_checkbox_label(struct nk_context *ctx, const char *label, nk_bool *active)
  23199. {
  23200. return nk_checkbox_text(ctx, label, nk_strlen(label), active);
  23201. }
  23202. NK_API nk_bool nk_checkbox_label_align(struct nk_context *ctx, const char *label, nk_bool *active, nk_flags widget_alignment, nk_flags text_alignment)
  23203. {
  23204. return nk_checkbox_text_align(ctx, label, nk_strlen(label), active, widget_alignment, text_alignment);
  23205. }
  23206. NK_API nk_bool nk_checkbox_flags_label(struct nk_context *ctx, const char *label,
  23207. unsigned int *flags, unsigned int value)
  23208. {
  23209. return nk_checkbox_flags_text(ctx, label, nk_strlen(label), flags, value);
  23210. }
  23211. /*----------------------------------------------------------------
  23212. *
  23213. * OPTION
  23214. *
  23215. * --------------------------------------------------------------*/
  23216. NK_API nk_bool
  23217. nk_option_text(struct nk_context *ctx, const char *text, int len, nk_bool is_active)
  23218. {
  23219. struct nk_window *win;
  23220. struct nk_panel *layout;
  23221. const struct nk_input *in;
  23222. const struct nk_style *style;
  23223. struct nk_rect bounds;
  23224. enum nk_widget_layout_states state;
  23225. NK_ASSERT(ctx);
  23226. NK_ASSERT(ctx->current);
  23227. NK_ASSERT(ctx->current->layout);
  23228. if (!ctx || !ctx->current || !ctx->current->layout)
  23229. return is_active;
  23230. win = ctx->current;
  23231. style = &ctx->style;
  23232. layout = win->layout;
  23233. state = nk_widget(&bounds, ctx);
  23234. if (!state) return (int)state;
  23235. in = (state == NK_WIDGET_ROM || state == NK_WIDGET_DISABLED || layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  23236. nk_do_toggle(&ctx->last_widget_state, &win->buffer, bounds, &is_active,
  23237. text, len, NK_TOGGLE_OPTION, &style->option, in, style->font, NK_WIDGET_LEFT, NK_TEXT_LEFT);
  23238. return is_active;
  23239. }
  23240. NK_API nk_bool
  23241. nk_option_text_align(struct nk_context *ctx, const char *text, int len, nk_bool is_active, nk_flags widget_alignment, nk_flags text_alignment)
  23242. {
  23243. struct nk_window *win;
  23244. struct nk_panel *layout;
  23245. const struct nk_input *in;
  23246. const struct nk_style *style;
  23247. struct nk_rect bounds;
  23248. enum nk_widget_layout_states state;
  23249. NK_ASSERT(ctx);
  23250. NK_ASSERT(ctx->current);
  23251. NK_ASSERT(ctx->current->layout);
  23252. if (!ctx || !ctx->current || !ctx->current->layout)
  23253. return is_active;
  23254. win = ctx->current;
  23255. style = &ctx->style;
  23256. layout = win->layout;
  23257. state = nk_widget(&bounds, ctx);
  23258. if (!state) return (int)state;
  23259. in = (state == NK_WIDGET_ROM || state == NK_WIDGET_DISABLED || layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  23260. nk_do_toggle(&ctx->last_widget_state, &win->buffer, bounds, &is_active,
  23261. text, len, NK_TOGGLE_OPTION, &style->option, in, style->font, widget_alignment, text_alignment);
  23262. return is_active;
  23263. }
  23264. NK_API nk_bool
  23265. nk_radio_text(struct nk_context *ctx, const char *text, int len, nk_bool *active)
  23266. {
  23267. int old_value;
  23268. NK_ASSERT(ctx);
  23269. NK_ASSERT(text);
  23270. NK_ASSERT(active);
  23271. if (!ctx || !text || !active) return 0;
  23272. old_value = *active;
  23273. *active = nk_option_text(ctx, text, len, old_value);
  23274. return old_value != *active;
  23275. }
  23276. NK_API nk_bool
  23277. nk_radio_text_align(struct nk_context *ctx, const char *text, int len, nk_bool *active, nk_flags widget_alignment, nk_flags text_alignment)
  23278. {
  23279. int old_value;
  23280. NK_ASSERT(ctx);
  23281. NK_ASSERT(text);
  23282. NK_ASSERT(active);
  23283. if (!ctx || !text || !active) return 0;
  23284. old_value = *active;
  23285. *active = nk_option_text_align(ctx, text, len, old_value, widget_alignment, text_alignment);
  23286. return old_value != *active;
  23287. }
  23288. NK_API nk_bool
  23289. nk_option_label(struct nk_context *ctx, const char *label, nk_bool active)
  23290. {
  23291. return nk_option_text(ctx, label, nk_strlen(label), active);
  23292. }
  23293. NK_API nk_bool
  23294. nk_option_label_align(struct nk_context *ctx, const char *label, nk_bool active, nk_flags widget_alignment, nk_flags text_alignment)
  23295. {
  23296. return nk_option_text_align(ctx, label, nk_strlen(label), active, widget_alignment, text_alignment);
  23297. }
  23298. NK_API nk_bool
  23299. nk_radio_label(struct nk_context *ctx, const char *label, nk_bool *active)
  23300. {
  23301. return nk_radio_text(ctx, label, nk_strlen(label), active);
  23302. }
  23303. NK_API nk_bool
  23304. nk_radio_label_align(struct nk_context *ctx, const char *label, nk_bool *active, nk_flags widget_alignment, nk_flags text_alignment)
  23305. {
  23306. return nk_radio_text_align(ctx, label, nk_strlen(label), active, widget_alignment, text_alignment);
  23307. }
  23308. /* ===============================================================
  23309. *
  23310. * SELECTABLE
  23311. *
  23312. * ===============================================================*/
  23313. NK_LIB void
  23314. nk_draw_selectable(struct nk_command_buffer *out,
  23315. nk_flags state, const struct nk_style_selectable *style, nk_bool active,
  23316. const struct nk_rect *bounds,
  23317. const struct nk_rect *icon, const struct nk_image *img, enum nk_symbol_type sym,
  23318. const char *string, int len, nk_flags align, const struct nk_user_font *font)
  23319. {
  23320. const struct nk_style_item *background;
  23321. struct nk_text text;
  23322. text.padding = style->padding;
  23323. /* select correct colors/images */
  23324. if (!active) {
  23325. if (state & NK_WIDGET_STATE_ACTIVED) {
  23326. background = &style->pressed;
  23327. text.text = style->text_pressed;
  23328. } else if (state & NK_WIDGET_STATE_HOVER) {
  23329. background = &style->hover;
  23330. text.text = style->text_hover;
  23331. } else {
  23332. background = &style->normal;
  23333. text.text = style->text_normal;
  23334. }
  23335. } else {
  23336. if (state & NK_WIDGET_STATE_ACTIVED) {
  23337. background = &style->pressed_active;
  23338. text.text = style->text_pressed_active;
  23339. } else if (state & NK_WIDGET_STATE_HOVER) {
  23340. background = &style->hover_active;
  23341. text.text = style->text_hover_active;
  23342. } else {
  23343. background = &style->normal_active;
  23344. text.text = style->text_normal_active;
  23345. }
  23346. }
  23347. text.text = nk_rgb_factor(text.text, style->color_factor);
  23348. /* draw selectable background and text */
  23349. switch (background->type) {
  23350. case NK_STYLE_ITEM_IMAGE:
  23351. text.background = nk_rgba(0, 0, 0, 0);
  23352. nk_draw_image(out, *bounds, &background->data.image, nk_rgb_factor(nk_white, style->color_factor));
  23353. break;
  23354. case NK_STYLE_ITEM_NINE_SLICE:
  23355. text.background = nk_rgba(0, 0, 0, 0);
  23356. nk_draw_nine_slice(out, *bounds, &background->data.slice, nk_rgb_factor(nk_white, style->color_factor));
  23357. break;
  23358. case NK_STYLE_ITEM_COLOR:
  23359. text.background = background->data.color;
  23360. nk_fill_rect(out, *bounds, style->rounding, background->data.color);
  23361. break;
  23362. }
  23363. if (icon) {
  23364. if (img) nk_draw_image(out, *icon, img, nk_rgb_factor(nk_white, style->color_factor));
  23365. else nk_draw_symbol(out, sym, *icon, text.background, text.text, 1, font);
  23366. }
  23367. nk_widget_text(out, *bounds, string, len, &text, align, font);
  23368. }
  23369. NK_LIB nk_bool
  23370. nk_do_selectable(nk_flags *state, struct nk_command_buffer *out,
  23371. struct nk_rect bounds, const char *str, int len, nk_flags align, nk_bool *value,
  23372. const struct nk_style_selectable *style, const struct nk_input *in,
  23373. const struct nk_user_font *font)
  23374. {
  23375. int old_value;
  23376. struct nk_rect touch;
  23377. NK_ASSERT(state);
  23378. NK_ASSERT(out);
  23379. NK_ASSERT(str);
  23380. NK_ASSERT(len);
  23381. NK_ASSERT(value);
  23382. NK_ASSERT(style);
  23383. NK_ASSERT(font);
  23384. if (!state || !out || !str || !len || !value || !style || !font) return 0;
  23385. old_value = *value;
  23386. /* remove padding */
  23387. touch.x = bounds.x - style->touch_padding.x;
  23388. touch.y = bounds.y - style->touch_padding.y;
  23389. touch.w = bounds.w + style->touch_padding.x * 2;
  23390. touch.h = bounds.h + style->touch_padding.y * 2;
  23391. /* update button */
  23392. if (nk_button_behavior(state, touch, in, NK_BUTTON_DEFAULT))
  23393. *value = !(*value);
  23394. /* draw selectable */
  23395. if (style->draw_begin) style->draw_begin(out, style->userdata);
  23396. nk_draw_selectable(out, *state, style, *value, &bounds, 0,0,NK_SYMBOL_NONE, str, len, align, font);
  23397. if (style->draw_end) style->draw_end(out, style->userdata);
  23398. return old_value != *value;
  23399. }
  23400. NK_LIB nk_bool
  23401. nk_do_selectable_image(nk_flags *state, struct nk_command_buffer *out,
  23402. struct nk_rect bounds, const char *str, int len, nk_flags align, nk_bool *value,
  23403. const struct nk_image *img, const struct nk_style_selectable *style,
  23404. const struct nk_input *in, const struct nk_user_font *font)
  23405. {
  23406. nk_bool old_value;
  23407. struct nk_rect touch;
  23408. struct nk_rect icon;
  23409. NK_ASSERT(state);
  23410. NK_ASSERT(out);
  23411. NK_ASSERT(str);
  23412. NK_ASSERT(len);
  23413. NK_ASSERT(value);
  23414. NK_ASSERT(style);
  23415. NK_ASSERT(font);
  23416. if (!state || !out || !str || !len || !value || !style || !font) return 0;
  23417. old_value = *value;
  23418. /* toggle behavior */
  23419. touch.x = bounds.x - style->touch_padding.x;
  23420. touch.y = bounds.y - style->touch_padding.y;
  23421. touch.w = bounds.w + style->touch_padding.x * 2;
  23422. touch.h = bounds.h + style->touch_padding.y * 2;
  23423. if (nk_button_behavior(state, touch, in, NK_BUTTON_DEFAULT))
  23424. *value = !(*value);
  23425. icon.y = bounds.y + style->padding.y;
  23426. icon.w = icon.h = bounds.h - 2 * style->padding.y;
  23427. if (align & NK_TEXT_ALIGN_LEFT) {
  23428. icon.x = (bounds.x + bounds.w) - (2 * style->padding.x + icon.w);
  23429. icon.x = NK_MAX(icon.x, 0);
  23430. } else icon.x = bounds.x + 2 * style->padding.x;
  23431. icon.x += style->image_padding.x;
  23432. icon.y += style->image_padding.y;
  23433. icon.w -= 2 * style->image_padding.x;
  23434. icon.h -= 2 * style->image_padding.y;
  23435. /* draw selectable */
  23436. if (style->draw_begin) style->draw_begin(out, style->userdata);
  23437. nk_draw_selectable(out, *state, style, *value, &bounds, &icon, img, NK_SYMBOL_NONE, str, len, align, font);
  23438. if (style->draw_end) style->draw_end(out, style->userdata);
  23439. return old_value != *value;
  23440. }
  23441. NK_LIB nk_bool
  23442. nk_do_selectable_symbol(nk_flags *state, struct nk_command_buffer *out,
  23443. struct nk_rect bounds, const char *str, int len, nk_flags align, nk_bool *value,
  23444. enum nk_symbol_type sym, const struct nk_style_selectable *style,
  23445. const struct nk_input *in, const struct nk_user_font *font)
  23446. {
  23447. int old_value;
  23448. struct nk_rect touch;
  23449. struct nk_rect icon;
  23450. NK_ASSERT(state);
  23451. NK_ASSERT(out);
  23452. NK_ASSERT(str);
  23453. NK_ASSERT(len);
  23454. NK_ASSERT(value);
  23455. NK_ASSERT(style);
  23456. NK_ASSERT(font);
  23457. if (!state || !out || !str || !len || !value || !style || !font) return 0;
  23458. old_value = *value;
  23459. /* toggle behavior */
  23460. touch.x = bounds.x - style->touch_padding.x;
  23461. touch.y = bounds.y - style->touch_padding.y;
  23462. touch.w = bounds.w + style->touch_padding.x * 2;
  23463. touch.h = bounds.h + style->touch_padding.y * 2;
  23464. if (nk_button_behavior(state, touch, in, NK_BUTTON_DEFAULT))
  23465. *value = !(*value);
  23466. icon.y = bounds.y + style->padding.y;
  23467. icon.w = icon.h = bounds.h - 2 * style->padding.y;
  23468. if (align & NK_TEXT_ALIGN_LEFT) {
  23469. icon.x = (bounds.x + bounds.w) - (2 * style->padding.x + icon.w);
  23470. icon.x = NK_MAX(icon.x, 0);
  23471. } else icon.x = bounds.x + 2 * style->padding.x;
  23472. icon.x += style->image_padding.x;
  23473. icon.y += style->image_padding.y;
  23474. icon.w -= 2 * style->image_padding.x;
  23475. icon.h -= 2 * style->image_padding.y;
  23476. /* draw selectable */
  23477. if (style->draw_begin) style->draw_begin(out, style->userdata);
  23478. nk_draw_selectable(out, *state, style, *value, &bounds, &icon, 0, sym, str, len, align, font);
  23479. if (style->draw_end) style->draw_end(out, style->userdata);
  23480. return old_value != *value;
  23481. }
  23482. NK_API nk_bool
  23483. nk_selectable_text(struct nk_context *ctx, const char *str, int len,
  23484. nk_flags align, nk_bool *value)
  23485. {
  23486. struct nk_window *win;
  23487. struct nk_panel *layout;
  23488. const struct nk_input *in;
  23489. const struct nk_style *style;
  23490. enum nk_widget_layout_states state;
  23491. struct nk_rect bounds;
  23492. NK_ASSERT(ctx);
  23493. NK_ASSERT(value);
  23494. NK_ASSERT(ctx->current);
  23495. NK_ASSERT(ctx->current->layout);
  23496. if (!ctx || !ctx->current || !ctx->current->layout || !value)
  23497. return 0;
  23498. win = ctx->current;
  23499. layout = win->layout;
  23500. style = &ctx->style;
  23501. state = nk_widget(&bounds, ctx);
  23502. if (!state) return 0;
  23503. in = (state == NK_WIDGET_ROM || state == NK_WIDGET_DISABLED || layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  23504. return nk_do_selectable(&ctx->last_widget_state, &win->buffer, bounds,
  23505. str, len, align, value, &style->selectable, in, style->font);
  23506. }
  23507. NK_API nk_bool
  23508. nk_selectable_image_text(struct nk_context *ctx, struct nk_image img,
  23509. const char *str, int len, nk_flags align, nk_bool *value)
  23510. {
  23511. struct nk_window *win;
  23512. struct nk_panel *layout;
  23513. const struct nk_input *in;
  23514. const struct nk_style *style;
  23515. enum nk_widget_layout_states state;
  23516. struct nk_rect bounds;
  23517. NK_ASSERT(ctx);
  23518. NK_ASSERT(value);
  23519. NK_ASSERT(ctx->current);
  23520. NK_ASSERT(ctx->current->layout);
  23521. if (!ctx || !ctx->current || !ctx->current->layout || !value)
  23522. return 0;
  23523. win = ctx->current;
  23524. layout = win->layout;
  23525. style = &ctx->style;
  23526. state = nk_widget(&bounds, ctx);
  23527. if (!state) return 0;
  23528. in = (state == NK_WIDGET_ROM || state == NK_WIDGET_DISABLED || layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  23529. return nk_do_selectable_image(&ctx->last_widget_state, &win->buffer, bounds,
  23530. str, len, align, value, &img, &style->selectable, in, style->font);
  23531. }
  23532. NK_API nk_bool
  23533. nk_selectable_symbol_text(struct nk_context *ctx, enum nk_symbol_type sym,
  23534. const char *str, int len, nk_flags align, nk_bool *value)
  23535. {
  23536. struct nk_window *win;
  23537. struct nk_panel *layout;
  23538. const struct nk_input *in;
  23539. const struct nk_style *style;
  23540. enum nk_widget_layout_states state;
  23541. struct nk_rect bounds;
  23542. NK_ASSERT(ctx);
  23543. NK_ASSERT(value);
  23544. NK_ASSERT(ctx->current);
  23545. NK_ASSERT(ctx->current->layout);
  23546. if (!ctx || !ctx->current || !ctx->current->layout || !value)
  23547. return 0;
  23548. win = ctx->current;
  23549. layout = win->layout;
  23550. style = &ctx->style;
  23551. state = nk_widget(&bounds, ctx);
  23552. if (!state) return 0;
  23553. in = (state == NK_WIDGET_ROM || state == NK_WIDGET_DISABLED || layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  23554. return nk_do_selectable_symbol(&ctx->last_widget_state, &win->buffer, bounds,
  23555. str, len, align, value, sym, &style->selectable, in, style->font);
  23556. }
  23557. NK_API nk_bool
  23558. nk_selectable_symbol_label(struct nk_context *ctx, enum nk_symbol_type sym,
  23559. const char *title, nk_flags align, nk_bool *value)
  23560. {
  23561. return nk_selectable_symbol_text(ctx, sym, title, nk_strlen(title), align, value);
  23562. }
  23563. NK_API nk_bool nk_select_text(struct nk_context *ctx, const char *str, int len,
  23564. nk_flags align, nk_bool value)
  23565. {
  23566. nk_selectable_text(ctx, str, len, align, &value);return value;
  23567. }
  23568. NK_API nk_bool nk_selectable_label(struct nk_context *ctx, const char *str, nk_flags align, nk_bool *value)
  23569. {
  23570. return nk_selectable_text(ctx, str, nk_strlen(str), align, value);
  23571. }
  23572. NK_API nk_bool nk_selectable_image_label(struct nk_context *ctx,struct nk_image img,
  23573. const char *str, nk_flags align, nk_bool *value)
  23574. {
  23575. return nk_selectable_image_text(ctx, img, str, nk_strlen(str), align, value);
  23576. }
  23577. NK_API nk_bool nk_select_label(struct nk_context *ctx, const char *str, nk_flags align, nk_bool value)
  23578. {
  23579. nk_selectable_text(ctx, str, nk_strlen(str), align, &value);return value;
  23580. }
  23581. NK_API nk_bool nk_select_image_label(struct nk_context *ctx, struct nk_image img,
  23582. const char *str, nk_flags align, nk_bool value)
  23583. {
  23584. nk_selectable_image_text(ctx, img, str, nk_strlen(str), align, &value);return value;
  23585. }
  23586. NK_API nk_bool nk_select_image_text(struct nk_context *ctx, struct nk_image img,
  23587. const char *str, int len, nk_flags align, nk_bool value)
  23588. {
  23589. nk_selectable_image_text(ctx, img, str, len, align, &value);return value;
  23590. }
  23591. NK_API nk_bool
  23592. nk_select_symbol_text(struct nk_context *ctx, enum nk_symbol_type sym,
  23593. const char *title, int title_len, nk_flags align, nk_bool value)
  23594. {
  23595. nk_selectable_symbol_text(ctx, sym, title, title_len, align, &value);return value;
  23596. }
  23597. NK_API nk_bool
  23598. nk_select_symbol_label(struct nk_context *ctx, enum nk_symbol_type sym,
  23599. const char *title, nk_flags align, nk_bool value)
  23600. {
  23601. return nk_select_symbol_text(ctx, sym, title, nk_strlen(title), align, value);
  23602. }
  23603. /* ===============================================================
  23604. *
  23605. * SLIDER
  23606. *
  23607. * ===============================================================*/
  23608. NK_LIB float
  23609. nk_slider_behavior(nk_flags *state, struct nk_rect *logical_cursor,
  23610. struct nk_rect *visual_cursor, struct nk_input *in,
  23611. struct nk_rect bounds, float slider_min, float slider_max, float slider_value,
  23612. float slider_step, float slider_steps)
  23613. {
  23614. int left_mouse_down;
  23615. int left_mouse_click_in_cursor;
  23616. /* check if visual cursor is being dragged */
  23617. nk_widget_state_reset(state);
  23618. left_mouse_down = in && in->mouse.buttons[NK_BUTTON_LEFT].down;
  23619. left_mouse_click_in_cursor = in && nk_input_has_mouse_click_down_in_rect(in,
  23620. NK_BUTTON_LEFT, *visual_cursor, nk_true);
  23621. if (left_mouse_down && left_mouse_click_in_cursor) {
  23622. float ratio = 0;
  23623. const float d = in->mouse.pos.x - (visual_cursor->x+visual_cursor->w*0.5f);
  23624. const float pxstep = bounds.w / slider_steps;
  23625. /* only update value if the next slider step is reached */
  23626. *state = NK_WIDGET_STATE_ACTIVE;
  23627. if (NK_ABS(d) >= pxstep) {
  23628. const float steps = (float)((int)(NK_ABS(d) / pxstep));
  23629. slider_value += (d > 0) ? (slider_step*steps) : -(slider_step*steps);
  23630. slider_value = NK_CLAMP(slider_min, slider_value, slider_max);
  23631. ratio = (slider_value - slider_min)/slider_step;
  23632. logical_cursor->x = bounds.x + (logical_cursor->w * ratio);
  23633. in->mouse.buttons[NK_BUTTON_LEFT].clicked_pos.x = logical_cursor->x;
  23634. }
  23635. }
  23636. /* slider widget state */
  23637. if (nk_input_is_mouse_hovering_rect(in, bounds))
  23638. *state = NK_WIDGET_STATE_HOVERED;
  23639. if (*state & NK_WIDGET_STATE_HOVER &&
  23640. !nk_input_is_mouse_prev_hovering_rect(in, bounds))
  23641. *state |= NK_WIDGET_STATE_ENTERED;
  23642. else if (nk_input_is_mouse_prev_hovering_rect(in, bounds))
  23643. *state |= NK_WIDGET_STATE_LEFT;
  23644. return slider_value;
  23645. }
  23646. NK_LIB void
  23647. nk_draw_slider(struct nk_command_buffer *out, nk_flags state,
  23648. const struct nk_style_slider *style, const struct nk_rect *bounds,
  23649. const struct nk_rect *visual_cursor, float min, float value, float max)
  23650. {
  23651. struct nk_rect fill;
  23652. struct nk_rect bar;
  23653. const struct nk_style_item *background;
  23654. /* select correct slider images/colors */
  23655. struct nk_color bar_color;
  23656. const struct nk_style_item *cursor;
  23657. NK_UNUSED(min);
  23658. NK_UNUSED(max);
  23659. NK_UNUSED(value);
  23660. if (state & NK_WIDGET_STATE_ACTIVED) {
  23661. background = &style->active;
  23662. bar_color = style->bar_active;
  23663. cursor = &style->cursor_active;
  23664. } else if (state & NK_WIDGET_STATE_HOVER) {
  23665. background = &style->hover;
  23666. bar_color = style->bar_hover;
  23667. cursor = &style->cursor_hover;
  23668. } else {
  23669. background = &style->normal;
  23670. bar_color = style->bar_normal;
  23671. cursor = &style->cursor_normal;
  23672. }
  23673. /* calculate slider background bar */
  23674. bar.x = bounds->x;
  23675. bar.y = (visual_cursor->y + visual_cursor->h/2) - bounds->h/12;
  23676. bar.w = bounds->w;
  23677. bar.h = bounds->h/6;
  23678. /* filled background bar style */
  23679. fill.w = (visual_cursor->x + (visual_cursor->w/2.0f)) - bar.x;
  23680. fill.x = bar.x;
  23681. fill.y = bar.y;
  23682. fill.h = bar.h;
  23683. /* draw background */
  23684. switch(background->type) {
  23685. case NK_STYLE_ITEM_IMAGE:
  23686. nk_draw_image(out, *bounds, &background->data.image, nk_rgb_factor(nk_white, style->color_factor));
  23687. break;
  23688. case NK_STYLE_ITEM_NINE_SLICE:
  23689. nk_draw_nine_slice(out, *bounds, &background->data.slice, nk_rgb_factor(nk_white, style->color_factor));
  23690. break;
  23691. case NK_STYLE_ITEM_COLOR:
  23692. nk_fill_rect(out, *bounds, style->rounding, nk_rgb_factor(background->data.color, style->color_factor));
  23693. nk_stroke_rect(out, *bounds, style->rounding, style->border, nk_rgb_factor(style->border_color, style->color_factor));
  23694. break;
  23695. }
  23696. /* draw slider bar */
  23697. nk_fill_rect(out, bar, style->rounding, nk_rgb_factor(bar_color, style->color_factor));
  23698. nk_fill_rect(out, fill, style->rounding, nk_rgb_factor(style->bar_filled, style->color_factor));
  23699. /* draw cursor */
  23700. if (cursor->type == NK_STYLE_ITEM_IMAGE)
  23701. nk_draw_image(out, *visual_cursor, &cursor->data.image, nk_rgb_factor(nk_white, style->color_factor));
  23702. else
  23703. nk_fill_circle(out, *visual_cursor, nk_rgb_factor(cursor->data.color, style->color_factor));
  23704. }
  23705. NK_LIB float
  23706. nk_do_slider(nk_flags *state,
  23707. struct nk_command_buffer *out, struct nk_rect bounds,
  23708. float min, float val, float max, float step,
  23709. const struct nk_style_slider *style, struct nk_input *in,
  23710. const struct nk_user_font *font)
  23711. {
  23712. float slider_range;
  23713. float slider_min;
  23714. float slider_max;
  23715. float slider_value;
  23716. float slider_steps;
  23717. float cursor_offset;
  23718. struct nk_rect visual_cursor;
  23719. struct nk_rect logical_cursor;
  23720. NK_ASSERT(style);
  23721. NK_ASSERT(out);
  23722. if (!out || !style)
  23723. return 0;
  23724. /* remove padding from slider bounds */
  23725. bounds.x = bounds.x + style->padding.x;
  23726. bounds.y = bounds.y + style->padding.y;
  23727. bounds.h = NK_MAX(bounds.h, 2*style->padding.y);
  23728. bounds.w = NK_MAX(bounds.w, 2*style->padding.x + style->cursor_size.x);
  23729. bounds.w -= 2 * style->padding.x;
  23730. bounds.h -= 2 * style->padding.y;
  23731. /* optional buttons */
  23732. if (style->show_buttons) {
  23733. nk_flags ws;
  23734. struct nk_rect button;
  23735. button.y = bounds.y;
  23736. button.w = bounds.h;
  23737. button.h = bounds.h;
  23738. /* decrement button */
  23739. button.x = bounds.x;
  23740. if (nk_do_button_symbol(&ws, out, button, style->dec_symbol, NK_BUTTON_DEFAULT,
  23741. &style->dec_button, in, font))
  23742. val -= step;
  23743. /* increment button */
  23744. button.x = (bounds.x + bounds.w) - button.w;
  23745. if (nk_do_button_symbol(&ws, out, button, style->inc_symbol, NK_BUTTON_DEFAULT,
  23746. &style->inc_button, in, font))
  23747. val += step;
  23748. bounds.x = bounds.x + button.w + style->spacing.x;
  23749. bounds.w = bounds.w - (2*button.w + 2*style->spacing.x);
  23750. }
  23751. /* remove one cursor size to support visual cursor */
  23752. bounds.x += style->cursor_size.x*0.5f;
  23753. bounds.w -= style->cursor_size.x;
  23754. /* make sure the provided values are correct */
  23755. slider_max = NK_MAX(min, max);
  23756. slider_min = NK_MIN(min, max);
  23757. slider_value = NK_CLAMP(slider_min, val, slider_max);
  23758. slider_range = slider_max - slider_min;
  23759. slider_steps = slider_range / step;
  23760. cursor_offset = (slider_value - slider_min) / step;
  23761. /* calculate cursor
  23762. Basically you have two cursors. One for visual representation and interaction
  23763. and one for updating the actual cursor value. */
  23764. logical_cursor.h = bounds.h;
  23765. logical_cursor.w = bounds.w / slider_steps;
  23766. logical_cursor.x = bounds.x + (logical_cursor.w * cursor_offset);
  23767. logical_cursor.y = bounds.y;
  23768. visual_cursor.h = style->cursor_size.y;
  23769. visual_cursor.w = style->cursor_size.x;
  23770. visual_cursor.y = (bounds.y + bounds.h*0.5f) - visual_cursor.h*0.5f;
  23771. visual_cursor.x = logical_cursor.x - visual_cursor.w*0.5f;
  23772. slider_value = nk_slider_behavior(state, &logical_cursor, &visual_cursor,
  23773. in, bounds, slider_min, slider_max, slider_value, step, slider_steps);
  23774. visual_cursor.x = logical_cursor.x - visual_cursor.w*0.5f;
  23775. /* draw slider */
  23776. if (style->draw_begin) style->draw_begin(out, style->userdata);
  23777. nk_draw_slider(out, *state, style, &bounds, &visual_cursor, slider_min, slider_value, slider_max);
  23778. if (style->draw_end) style->draw_end(out, style->userdata);
  23779. return slider_value;
  23780. }
  23781. NK_API nk_bool
  23782. nk_slider_float(struct nk_context *ctx, float min_value, float *value, float max_value,
  23783. float value_step)
  23784. {
  23785. struct nk_window *win;
  23786. struct nk_panel *layout;
  23787. struct nk_input *in;
  23788. const struct nk_style *style;
  23789. int ret = 0;
  23790. float old_value;
  23791. struct nk_rect bounds;
  23792. enum nk_widget_layout_states state;
  23793. NK_ASSERT(ctx);
  23794. NK_ASSERT(ctx->current);
  23795. NK_ASSERT(ctx->current->layout);
  23796. NK_ASSERT(value);
  23797. if (!ctx || !ctx->current || !ctx->current->layout || !value)
  23798. return ret;
  23799. win = ctx->current;
  23800. style = &ctx->style;
  23801. layout = win->layout;
  23802. state = nk_widget(&bounds, ctx);
  23803. if (!state) return ret;
  23804. in = (/*state == NK_WIDGET_ROM || */ state == NK_WIDGET_DISABLED || layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  23805. old_value = *value;
  23806. *value = nk_do_slider(&ctx->last_widget_state, &win->buffer, bounds, min_value,
  23807. old_value, max_value, value_step, &style->slider, in, style->font);
  23808. return (old_value > *value || old_value < *value);
  23809. }
  23810. NK_API float
  23811. nk_slide_float(struct nk_context *ctx, float min, float val, float max, float step)
  23812. {
  23813. nk_slider_float(ctx, min, &val, max, step); return val;
  23814. }
  23815. NK_API int
  23816. nk_slide_int(struct nk_context *ctx, int min, int val, int max, int step)
  23817. {
  23818. float value = (float)val;
  23819. nk_slider_float(ctx, (float)min, &value, (float)max, (float)step);
  23820. return (int)value;
  23821. }
  23822. NK_API nk_bool
  23823. nk_slider_int(struct nk_context *ctx, int min, int *val, int max, int step)
  23824. {
  23825. int ret;
  23826. float value = (float)*val;
  23827. ret = nk_slider_float(ctx, (float)min, &value, (float)max, (float)step);
  23828. *val = (int)value;
  23829. return ret;
  23830. }
  23831. /* ===============================================================
  23832. *
  23833. * KNOB
  23834. *
  23835. * ===============================================================*/
  23836. NK_LIB float
  23837. nk_knob_behavior(nk_flags *state, struct nk_input *in,
  23838. struct nk_rect bounds, float knob_min, float knob_max, float knob_value,
  23839. float knob_step, float knob_steps,
  23840. enum nk_heading zero_direction, float dead_zone_percent)
  23841. {
  23842. struct nk_vec2 origin;
  23843. float angle = 0.0f;
  23844. origin.x = bounds.x + (bounds.w / 2);
  23845. origin.y = bounds.y + (bounds.h / 2);
  23846. nk_widget_state_reset(state);
  23847. /* handle click and drag input */
  23848. if(in &&
  23849. in->mouse.buttons[NK_BUTTON_LEFT].down &&
  23850. nk_input_has_mouse_click_down_in_rect(in, NK_BUTTON_LEFT, bounds, nk_true)){
  23851. /* calculate angle from origin and rotate */
  23852. const float direction_rads[4] = {
  23853. NK_PI * 2.5f, /* 90 NK_UP */
  23854. NK_PI * 2.0f, /* 0 NK_RIGHT */
  23855. NK_PI * 1.5f, /* 270 NK_DOWN */
  23856. NK_PI, /* 180 NK_LEFT */
  23857. };
  23858. *state = NK_WIDGET_STATE_ACTIVE;
  23859. angle = NK_ATAN2(in->mouse.pos.y - origin.y, in->mouse.pos.x - origin.x) + direction_rads[zero_direction];
  23860. angle -= (angle > NK_PI * 2) ? NK_PI * 3 : NK_PI;
  23861. /* account for dead space applied when drawing */
  23862. angle *= 1.0f / (1.0f - dead_zone_percent);
  23863. angle = NK_CLAMP(-NK_PI, angle, NK_PI);
  23864. /* convert -pi -> pi range to 0.0 -> 1.0 */
  23865. angle = (angle + NK_PI) / (NK_PI * 2);
  23866. /* click to closest step */
  23867. knob_value = knob_min + ( (int)(angle * knob_steps + (knob_step / 2)) ) * knob_step;
  23868. knob_value = NK_CLAMP(knob_min, knob_value, knob_max);
  23869. }
  23870. /* knob widget state */
  23871. if (nk_input_is_mouse_hovering_rect(in, bounds)){
  23872. *state = NK_WIDGET_STATE_HOVERED;
  23873. /* handle scroll and arrow inputs */
  23874. if (in->mouse.scroll_delta.y > 0 ||
  23875. (in->keyboard.keys[NK_KEY_UP].down && in->keyboard.keys[NK_KEY_UP].clicked)) {
  23876. knob_value += knob_step;
  23877. }
  23878. if (in->mouse.scroll_delta.y < 0 ||
  23879. (in->keyboard.keys[NK_KEY_DOWN].down && in->keyboard.keys[NK_KEY_DOWN].clicked)) {
  23880. knob_value -= knob_step;
  23881. }
  23882. /* easiest way to disable scrolling of parent panels..knob eats scrolling */
  23883. in->mouse.scroll_delta.y = 0;
  23884. knob_value = NK_CLAMP(knob_min, knob_value, knob_max);
  23885. }
  23886. if (*state & NK_WIDGET_STATE_HOVER &&
  23887. !nk_input_is_mouse_prev_hovering_rect(in, bounds))
  23888. *state |= NK_WIDGET_STATE_ENTERED;
  23889. else if (nk_input_is_mouse_prev_hovering_rect(in, bounds))
  23890. *state |= NK_WIDGET_STATE_LEFT;
  23891. return knob_value;
  23892. }
  23893. NK_LIB void
  23894. nk_draw_knob(struct nk_command_buffer *out, nk_flags state,
  23895. const struct nk_style_knob *style, const struct nk_rect *bounds, float min, float value, float max,
  23896. enum nk_heading zero_direction, float dead_zone_percent)
  23897. {
  23898. const struct nk_style_item *background;
  23899. struct nk_color knob_color, cursor;
  23900. NK_UNUSED(min);
  23901. NK_UNUSED(max);
  23902. NK_UNUSED(value);
  23903. if (state & NK_WIDGET_STATE_ACTIVED) {
  23904. background = &style->active;
  23905. knob_color = style->knob_active;
  23906. cursor = style->cursor_active;
  23907. } else if (state & NK_WIDGET_STATE_HOVER) {
  23908. background = &style->hover;
  23909. knob_color = style->knob_hover;
  23910. cursor = style->cursor_hover;
  23911. } else {
  23912. background = &style->normal;
  23913. knob_color = style->knob_normal;
  23914. cursor = style->cursor_normal;
  23915. }
  23916. /* draw background */
  23917. switch(background->type) {
  23918. case NK_STYLE_ITEM_IMAGE:
  23919. nk_draw_image(out, *bounds, &background->data.image, nk_rgb_factor(nk_white, style->color_factor));
  23920. break;
  23921. case NK_STYLE_ITEM_NINE_SLICE:
  23922. nk_draw_nine_slice(out, *bounds, &background->data.slice, nk_rgb_factor(nk_white, style->color_factor));
  23923. break;
  23924. case NK_STYLE_ITEM_COLOR:
  23925. nk_fill_rect(out, *bounds, 0, nk_rgb_factor(background->data.color, style->color_factor));
  23926. nk_stroke_rect(out, *bounds, 0, style->border, nk_rgb_factor(style->border_color, style->color_factor));
  23927. break;
  23928. }
  23929. /* draw knob */
  23930. nk_fill_circle(out, *bounds, nk_rgb_factor(knob_color, style->color_factor));
  23931. if(style->knob_border > 0){
  23932. struct nk_rect border_bounds = *bounds;
  23933. border_bounds.x += style->knob_border / 2;
  23934. border_bounds.y += style->knob_border / 2;
  23935. border_bounds.w -= style->knob_border;
  23936. border_bounds.h -= style->knob_border;
  23937. nk_stroke_circle(out, border_bounds, style->knob_border, nk_rgb_factor(style->knob_border_color, style->color_factor));
  23938. }
  23939. { /* calculate cursor line cords */
  23940. float half_circle_size = (bounds->w / 2);
  23941. float angle = (value - min) / (max - min);
  23942. float alive_zone = 1.0f - dead_zone_percent;
  23943. struct nk_vec2 cursor_start, cursor_end;
  23944. const float direction_rads[4] = {
  23945. NK_PI * 1.5f, /* 90 NK_UP */
  23946. 0.0f, /* 0 NK_RIGHT */
  23947. NK_PI * 0.5f, /* 270 NK_DOWN */
  23948. NK_PI, /* 180 NK_LEFT */
  23949. };
  23950. /* calculate + apply dead zone */
  23951. angle = (angle * alive_zone) + (dead_zone_percent / 2);
  23952. /* percentage 0.0 -> 1.0 to radians, rads are 0.0 to (2*pi) NOT -pi to pi */
  23953. angle *= NK_PI * 2;
  23954. /* apply zero angle */
  23955. angle += direction_rads[zero_direction];
  23956. if(angle > NK_PI * 2)
  23957. angle -= NK_PI * 2;
  23958. cursor_start.x = bounds->x + half_circle_size + (angle > NK_PI);
  23959. cursor_start.y = bounds->y + half_circle_size + (angle < NK_PI_HALF || angle > (NK_PI * 1.5f));
  23960. cursor_end.x = cursor_start.x + (half_circle_size * NK_COS(angle));
  23961. cursor_end.y = cursor_start.y + (half_circle_size * NK_SIN(angle));
  23962. /* cut off half of the cursor */
  23963. cursor_start.x = (cursor_start.x + cursor_end.x) / 2;
  23964. cursor_start.y = (cursor_start.y + cursor_end.y) / 2;
  23965. /* draw cursor */
  23966. nk_stroke_line(out, cursor_start.x, cursor_start.y, cursor_end.x, cursor_end.y, 2, nk_rgb_factor(cursor, style->color_factor));
  23967. }
  23968. }
  23969. NK_LIB float
  23970. nk_do_knob(nk_flags *state,
  23971. struct nk_command_buffer *out, struct nk_rect bounds,
  23972. float min, float val, float max, float step,
  23973. enum nk_heading zero_direction, float dead_zone_percent,
  23974. const struct nk_style_knob *style, struct nk_input *in)
  23975. {
  23976. float knob_range;
  23977. float knob_min;
  23978. float knob_max;
  23979. float knob_value;
  23980. float knob_steps;
  23981. NK_ASSERT(style);
  23982. NK_ASSERT(out);
  23983. if (!out || !style)
  23984. return 0;
  23985. /* remove padding from knob bounds */
  23986. bounds.y = bounds.y + style->padding.y;
  23987. bounds.x = bounds.x + style->padding.x;
  23988. bounds.h = NK_MAX(bounds.h, 2*style->padding.y);
  23989. bounds.w = NK_MAX(bounds.w, 2*style->padding.x);
  23990. bounds.w -= 2 * style->padding.x;
  23991. bounds.h -= 2 * style->padding.y;
  23992. if(bounds.h < bounds.w){
  23993. bounds.x += (bounds.w - bounds.h) / 2;
  23994. bounds.w = bounds.h;
  23995. }
  23996. /* make sure the provided values are correct */
  23997. knob_max = NK_MAX(min, max);
  23998. knob_min = NK_MIN(min, max);
  23999. knob_value = NK_CLAMP(knob_min, val, knob_max);
  24000. knob_range = knob_max - knob_min;
  24001. knob_steps = knob_range / step;
  24002. knob_value = nk_knob_behavior(state, in, bounds, knob_min, knob_max, knob_value, step, knob_steps, zero_direction, dead_zone_percent);
  24003. /* draw knob */
  24004. if (style->draw_begin) style->draw_begin(out, style->userdata);
  24005. nk_draw_knob(out, *state, style, &bounds, knob_min, knob_value, knob_max, zero_direction, dead_zone_percent);
  24006. if (style->draw_end) style->draw_end(out, style->userdata);
  24007. return knob_value;
  24008. }
  24009. NK_API nk_bool
  24010. nk_knob_float(struct nk_context *ctx, float min_value, float *value, float max_value,
  24011. float value_step, enum nk_heading zero_direction, float dead_zone_degrees)
  24012. {
  24013. struct nk_window *win;
  24014. struct nk_panel *layout;
  24015. struct nk_input *in;
  24016. const struct nk_style *style;
  24017. int ret = 0;
  24018. float old_value;
  24019. struct nk_rect bounds;
  24020. enum nk_widget_layout_states state;
  24021. NK_ASSERT(ctx);
  24022. NK_ASSERT(ctx->current);
  24023. NK_ASSERT(ctx->current->layout);
  24024. NK_ASSERT(value);
  24025. NK_ASSERT(NK_BETWEEN(dead_zone_degrees, 0.0f, 360.0f));
  24026. if (!ctx || !ctx->current || !ctx->current->layout || !value)
  24027. return ret;
  24028. win = ctx->current;
  24029. style = &ctx->style;
  24030. layout = win->layout;
  24031. state = nk_widget(&bounds, ctx);
  24032. if (!state) return ret;
  24033. in = (state == NK_WIDGET_DISABLED || layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  24034. old_value = *value;
  24035. *value = nk_do_knob(&ctx->last_widget_state, &win->buffer, bounds, min_value,
  24036. old_value, max_value, value_step, zero_direction, dead_zone_degrees / 360.0f, &style->knob, in);
  24037. return (old_value > *value || old_value < *value);
  24038. }
  24039. NK_API nk_bool
  24040. nk_knob_int(struct nk_context *ctx, int min, int *val, int max, int step,
  24041. enum nk_heading zero_direction, float dead_zone_degrees)
  24042. {
  24043. int ret;
  24044. float value = (float)*val;
  24045. ret = nk_knob_float(ctx, (float)min, &value, (float)max, (float)step, zero_direction, dead_zone_degrees);
  24046. *val = (int)value;
  24047. return ret;
  24048. }
  24049. /* ===============================================================
  24050. *
  24051. * PROGRESS
  24052. *
  24053. * ===============================================================*/
  24054. NK_LIB nk_size
  24055. nk_progress_behavior(nk_flags *state, struct nk_input *in,
  24056. struct nk_rect r, struct nk_rect cursor, nk_size max, nk_size value, nk_bool modifiable)
  24057. {
  24058. int left_mouse_down = 0;
  24059. int left_mouse_click_in_cursor = 0;
  24060. nk_widget_state_reset(state);
  24061. if (!in || !modifiable) return value;
  24062. left_mouse_down = in && in->mouse.buttons[NK_BUTTON_LEFT].down;
  24063. left_mouse_click_in_cursor = in && nk_input_has_mouse_click_down_in_rect(in,
  24064. NK_BUTTON_LEFT, cursor, nk_true);
  24065. if (nk_input_is_mouse_hovering_rect(in, r))
  24066. *state = NK_WIDGET_STATE_HOVERED;
  24067. if (in && left_mouse_down && left_mouse_click_in_cursor) {
  24068. if (left_mouse_down && left_mouse_click_in_cursor) {
  24069. float ratio = NK_MAX(0, (float)(in->mouse.pos.x - cursor.x)) / (float)cursor.w;
  24070. value = (nk_size)NK_CLAMP(0, (float)max * ratio, (float)max);
  24071. in->mouse.buttons[NK_BUTTON_LEFT].clicked_pos.x = cursor.x + cursor.w/2.0f;
  24072. *state |= NK_WIDGET_STATE_ACTIVE;
  24073. }
  24074. }
  24075. /* set progressbar widget state */
  24076. if (*state & NK_WIDGET_STATE_HOVER && !nk_input_is_mouse_prev_hovering_rect(in, r))
  24077. *state |= NK_WIDGET_STATE_ENTERED;
  24078. else if (nk_input_is_mouse_prev_hovering_rect(in, r))
  24079. *state |= NK_WIDGET_STATE_LEFT;
  24080. return value;
  24081. }
  24082. NK_LIB void
  24083. nk_draw_progress(struct nk_command_buffer *out, nk_flags state,
  24084. const struct nk_style_progress *style, const struct nk_rect *bounds,
  24085. const struct nk_rect *scursor, nk_size value, nk_size max)
  24086. {
  24087. const struct nk_style_item *background;
  24088. const struct nk_style_item *cursor;
  24089. NK_UNUSED(max);
  24090. NK_UNUSED(value);
  24091. /* select correct colors/images to draw */
  24092. if (state & NK_WIDGET_STATE_ACTIVED) {
  24093. background = &style->active;
  24094. cursor = &style->cursor_active;
  24095. } else if (state & NK_WIDGET_STATE_HOVER){
  24096. background = &style->hover;
  24097. cursor = &style->cursor_hover;
  24098. } else {
  24099. background = &style->normal;
  24100. cursor = &style->cursor_normal;
  24101. }
  24102. /* draw background */
  24103. switch(background->type) {
  24104. case NK_STYLE_ITEM_IMAGE:
  24105. nk_draw_image(out, *bounds, &background->data.image, nk_rgb_factor(nk_white, style->color_factor));
  24106. break;
  24107. case NK_STYLE_ITEM_NINE_SLICE:
  24108. nk_draw_nine_slice(out, *bounds, &background->data.slice, nk_rgb_factor(nk_white, style->color_factor));
  24109. break;
  24110. case NK_STYLE_ITEM_COLOR:
  24111. nk_fill_rect(out, *bounds, style->rounding, nk_rgb_factor(background->data.color, style->color_factor));
  24112. nk_stroke_rect(out, *bounds, style->rounding, style->border, nk_rgb_factor(style->border_color, style->color_factor));
  24113. break;
  24114. }
  24115. /* draw cursor */
  24116. switch(cursor->type) {
  24117. case NK_STYLE_ITEM_IMAGE:
  24118. nk_draw_image(out, *scursor, &cursor->data.image, nk_rgb_factor(nk_white, style->color_factor));
  24119. break;
  24120. case NK_STYLE_ITEM_NINE_SLICE:
  24121. nk_draw_nine_slice(out, *scursor, &cursor->data.slice, nk_rgb_factor(nk_white, style->color_factor));
  24122. break;
  24123. case NK_STYLE_ITEM_COLOR:
  24124. nk_fill_rect(out, *scursor, style->rounding, nk_rgb_factor(cursor->data.color, style->color_factor));
  24125. nk_stroke_rect(out, *scursor, style->rounding, style->border, nk_rgb_factor(style->border_color, style->color_factor));
  24126. break;
  24127. }
  24128. }
  24129. NK_LIB nk_size
  24130. nk_do_progress(nk_flags *state,
  24131. struct nk_command_buffer *out, struct nk_rect bounds,
  24132. nk_size value, nk_size max, nk_bool modifiable,
  24133. const struct nk_style_progress *style, struct nk_input *in)
  24134. {
  24135. float prog_scale;
  24136. nk_size prog_value;
  24137. struct nk_rect cursor;
  24138. NK_ASSERT(style);
  24139. NK_ASSERT(out);
  24140. if (!out || !style) return 0;
  24141. /* calculate progressbar cursor */
  24142. cursor.w = NK_MAX(bounds.w, 2 * style->padding.x + 2 * style->border);
  24143. cursor.h = NK_MAX(bounds.h, 2 * style->padding.y + 2 * style->border);
  24144. cursor = nk_pad_rect(bounds, nk_vec2(style->padding.x + style->border, style->padding.y + style->border));
  24145. prog_scale = (float)value / (float)max;
  24146. /* update progressbar */
  24147. prog_value = NK_MIN(value, max);
  24148. prog_value = nk_progress_behavior(state, in, bounds, cursor,max, prog_value, modifiable);
  24149. cursor.w = cursor.w * prog_scale;
  24150. /* draw progressbar */
  24151. if (style->draw_begin) style->draw_begin(out, style->userdata);
  24152. nk_draw_progress(out, *state, style, &bounds, &cursor, value, max);
  24153. if (style->draw_end) style->draw_end(out, style->userdata);
  24154. return prog_value;
  24155. }
  24156. NK_API nk_bool
  24157. nk_progress(struct nk_context *ctx, nk_size *cur, nk_size max, nk_bool is_modifyable)
  24158. {
  24159. struct nk_window *win;
  24160. struct nk_panel *layout;
  24161. const struct nk_style *style;
  24162. struct nk_input *in;
  24163. struct nk_rect bounds;
  24164. enum nk_widget_layout_states state;
  24165. nk_size old_value;
  24166. NK_ASSERT(ctx);
  24167. NK_ASSERT(cur);
  24168. NK_ASSERT(ctx->current);
  24169. NK_ASSERT(ctx->current->layout);
  24170. if (!ctx || !ctx->current || !ctx->current->layout || !cur)
  24171. return 0;
  24172. win = ctx->current;
  24173. style = &ctx->style;
  24174. layout = win->layout;
  24175. state = nk_widget(&bounds, ctx);
  24176. if (!state) return 0;
  24177. in = (state == NK_WIDGET_ROM || state == NK_WIDGET_DISABLED || layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  24178. old_value = *cur;
  24179. *cur = nk_do_progress(&ctx->last_widget_state, &win->buffer, bounds,
  24180. *cur, max, is_modifyable, &style->progress, in);
  24181. return (*cur != old_value);
  24182. }
  24183. NK_API nk_size
  24184. nk_prog(struct nk_context *ctx, nk_size cur, nk_size max, nk_bool modifyable)
  24185. {
  24186. nk_progress(ctx, &cur, max, modifyable);
  24187. return cur;
  24188. }
  24189. /* ===============================================================
  24190. *
  24191. * SCROLLBAR
  24192. *
  24193. * ===============================================================*/
  24194. NK_LIB float
  24195. nk_scrollbar_behavior(nk_flags *state, struct nk_input *in,
  24196. int has_scrolling, const struct nk_rect *scroll,
  24197. const struct nk_rect *cursor, const struct nk_rect *empty0,
  24198. const struct nk_rect *empty1, float scroll_offset,
  24199. float target, float scroll_step, enum nk_orientation o)
  24200. {
  24201. nk_flags ws = 0;
  24202. int left_mouse_down;
  24203. unsigned int left_mouse_clicked;
  24204. int left_mouse_click_in_cursor;
  24205. float scroll_delta;
  24206. nk_widget_state_reset(state);
  24207. if (!in) return scroll_offset;
  24208. left_mouse_down = in->mouse.buttons[NK_BUTTON_LEFT].down;
  24209. left_mouse_clicked = in->mouse.buttons[NK_BUTTON_LEFT].clicked;
  24210. left_mouse_click_in_cursor = nk_input_has_mouse_click_down_in_rect(in,
  24211. NK_BUTTON_LEFT, *cursor, nk_true);
  24212. if (nk_input_is_mouse_hovering_rect(in, *scroll))
  24213. *state = NK_WIDGET_STATE_HOVERED;
  24214. scroll_delta = (o == NK_VERTICAL) ? in->mouse.scroll_delta.y: in->mouse.scroll_delta.x;
  24215. if (left_mouse_down && left_mouse_click_in_cursor && !left_mouse_clicked) {
  24216. /* update cursor by mouse dragging */
  24217. float pixel, delta;
  24218. *state = NK_WIDGET_STATE_ACTIVE;
  24219. if (o == NK_VERTICAL) {
  24220. float cursor_y;
  24221. pixel = in->mouse.delta.y;
  24222. delta = (pixel / scroll->h) * target;
  24223. scroll_offset = NK_CLAMP(0, scroll_offset + delta, target - scroll->h);
  24224. cursor_y = scroll->y + ((scroll_offset/target) * scroll->h);
  24225. in->mouse.buttons[NK_BUTTON_LEFT].clicked_pos.y = cursor_y + cursor->h/2.0f;
  24226. } else {
  24227. float cursor_x;
  24228. pixel = in->mouse.delta.x;
  24229. delta = (pixel / scroll->w) * target;
  24230. scroll_offset = NK_CLAMP(0, scroll_offset + delta, target - scroll->w);
  24231. cursor_x = scroll->x + ((scroll_offset/target) * scroll->w);
  24232. in->mouse.buttons[NK_BUTTON_LEFT].clicked_pos.x = cursor_x + cursor->w/2.0f;
  24233. }
  24234. } else if ((nk_input_is_key_pressed(in, NK_KEY_SCROLL_UP) && o == NK_VERTICAL && has_scrolling)||
  24235. nk_button_behavior(&ws, *empty0, in, NK_BUTTON_DEFAULT)) {
  24236. /* scroll page up by click on empty space or shortcut */
  24237. if (o == NK_VERTICAL)
  24238. scroll_offset = NK_MAX(0, scroll_offset - scroll->h);
  24239. else scroll_offset = NK_MAX(0, scroll_offset - scroll->w);
  24240. } else if ((nk_input_is_key_pressed(in, NK_KEY_SCROLL_DOWN) && o == NK_VERTICAL && has_scrolling) ||
  24241. nk_button_behavior(&ws, *empty1, in, NK_BUTTON_DEFAULT)) {
  24242. /* scroll page down by click on empty space or shortcut */
  24243. if (o == NK_VERTICAL)
  24244. scroll_offset = NK_MIN(scroll_offset + scroll->h, target - scroll->h);
  24245. else scroll_offset = NK_MIN(scroll_offset + scroll->w, target - scroll->w);
  24246. } else if (has_scrolling) {
  24247. if ((scroll_delta < 0 || (scroll_delta > 0))) {
  24248. /* update cursor by mouse scrolling */
  24249. scroll_offset = scroll_offset + scroll_step * (-scroll_delta);
  24250. if (o == NK_VERTICAL)
  24251. scroll_offset = NK_CLAMP(0, scroll_offset, target - scroll->h);
  24252. else scroll_offset = NK_CLAMP(0, scroll_offset, target - scroll->w);
  24253. } else if (nk_input_is_key_pressed(in, NK_KEY_SCROLL_START)) {
  24254. /* update cursor to the beginning */
  24255. if (o == NK_VERTICAL) scroll_offset = 0;
  24256. } else if (nk_input_is_key_pressed(in, NK_KEY_SCROLL_END)) {
  24257. /* update cursor to the end */
  24258. if (o == NK_VERTICAL) scroll_offset = target - scroll->h;
  24259. }
  24260. }
  24261. if (*state & NK_WIDGET_STATE_HOVER && !nk_input_is_mouse_prev_hovering_rect(in, *scroll))
  24262. *state |= NK_WIDGET_STATE_ENTERED;
  24263. else if (nk_input_is_mouse_prev_hovering_rect(in, *scroll))
  24264. *state |= NK_WIDGET_STATE_LEFT;
  24265. return scroll_offset;
  24266. }
  24267. NK_LIB void
  24268. nk_draw_scrollbar(struct nk_command_buffer *out, nk_flags state,
  24269. const struct nk_style_scrollbar *style, const struct nk_rect *bounds,
  24270. const struct nk_rect *scroll)
  24271. {
  24272. const struct nk_style_item *background;
  24273. const struct nk_style_item *cursor;
  24274. /* select correct colors/images to draw */
  24275. if (state & NK_WIDGET_STATE_ACTIVED) {
  24276. background = &style->active;
  24277. cursor = &style->cursor_active;
  24278. } else if (state & NK_WIDGET_STATE_HOVER) {
  24279. background = &style->hover;
  24280. cursor = &style->cursor_hover;
  24281. } else {
  24282. background = &style->normal;
  24283. cursor = &style->cursor_normal;
  24284. }
  24285. /* draw background */
  24286. switch (background->type) {
  24287. case NK_STYLE_ITEM_IMAGE:
  24288. nk_draw_image(out, *bounds, &background->data.image, nk_white);
  24289. break;
  24290. case NK_STYLE_ITEM_NINE_SLICE:
  24291. nk_draw_nine_slice(out, *bounds, &background->data.slice, nk_white);
  24292. break;
  24293. case NK_STYLE_ITEM_COLOR:
  24294. nk_fill_rect(out, *bounds, style->rounding, background->data.color);
  24295. nk_stroke_rect(out, *bounds, style->rounding, style->border, style->border_color);
  24296. break;
  24297. }
  24298. /* draw cursor */
  24299. switch (cursor->type) {
  24300. case NK_STYLE_ITEM_IMAGE:
  24301. nk_draw_image(out, *scroll, &cursor->data.image, nk_white);
  24302. break;
  24303. case NK_STYLE_ITEM_NINE_SLICE:
  24304. nk_draw_nine_slice(out, *scroll, &cursor->data.slice, nk_white);
  24305. break;
  24306. case NK_STYLE_ITEM_COLOR:
  24307. nk_fill_rect(out, *scroll, style->rounding_cursor, cursor->data.color);
  24308. nk_stroke_rect(out, *scroll, style->rounding_cursor, style->border_cursor, style->cursor_border_color);
  24309. break;
  24310. }
  24311. }
  24312. NK_LIB float
  24313. nk_do_scrollbarv(nk_flags *state,
  24314. struct nk_command_buffer *out, struct nk_rect scroll, int has_scrolling,
  24315. float offset, float target, float step, float button_pixel_inc,
  24316. const struct nk_style_scrollbar *style, struct nk_input *in,
  24317. const struct nk_user_font *font)
  24318. {
  24319. struct nk_rect empty_north;
  24320. struct nk_rect empty_south;
  24321. struct nk_rect cursor;
  24322. float scroll_step;
  24323. float scroll_offset;
  24324. float scroll_off;
  24325. float scroll_ratio;
  24326. NK_ASSERT(out);
  24327. NK_ASSERT(style);
  24328. NK_ASSERT(state);
  24329. if (!out || !style) return 0;
  24330. scroll.w = NK_MAX(scroll.w, 1);
  24331. scroll.h = NK_MAX(scroll.h, 0);
  24332. if (target <= scroll.h) return 0;
  24333. /* optional scrollbar buttons */
  24334. if (style->show_buttons) {
  24335. nk_flags ws;
  24336. float scroll_h;
  24337. struct nk_rect button;
  24338. button.x = scroll.x;
  24339. button.w = scroll.w;
  24340. button.h = scroll.w;
  24341. scroll_h = NK_MAX(scroll.h - 2 * button.h,0);
  24342. scroll_step = NK_MIN(step, button_pixel_inc);
  24343. /* decrement button */
  24344. button.y = scroll.y;
  24345. if (nk_do_button_symbol(&ws, out, button, style->dec_symbol,
  24346. NK_BUTTON_REPEATER, &style->dec_button, in, font))
  24347. offset = offset - scroll_step;
  24348. /* increment button */
  24349. button.y = scroll.y + scroll.h - button.h;
  24350. if (nk_do_button_symbol(&ws, out, button, style->inc_symbol,
  24351. NK_BUTTON_REPEATER, &style->inc_button, in, font))
  24352. offset = offset + scroll_step;
  24353. scroll.y = scroll.y + button.h;
  24354. scroll.h = scroll_h;
  24355. }
  24356. /* calculate scrollbar constants */
  24357. scroll_step = NK_MIN(step, scroll.h);
  24358. scroll_offset = NK_CLAMP(0, offset, target - scroll.h);
  24359. scroll_ratio = scroll.h / target;
  24360. scroll_off = scroll_offset / target;
  24361. /* calculate scrollbar cursor bounds */
  24362. cursor.h = NK_MAX((scroll_ratio * scroll.h) - (2*style->border + 2*style->padding.y), 0);
  24363. cursor.y = scroll.y + (scroll_off * scroll.h) + style->border + style->padding.y;
  24364. cursor.w = scroll.w - (2 * style->border + 2 * style->padding.x);
  24365. cursor.x = scroll.x + style->border + style->padding.x;
  24366. /* calculate empty space around cursor */
  24367. empty_north.x = scroll.x;
  24368. empty_north.y = scroll.y;
  24369. empty_north.w = scroll.w;
  24370. empty_north.h = NK_MAX(cursor.y - scroll.y, 0);
  24371. empty_south.x = scroll.x;
  24372. empty_south.y = cursor.y + cursor.h;
  24373. empty_south.w = scroll.w;
  24374. empty_south.h = NK_MAX((scroll.y + scroll.h) - (cursor.y + cursor.h), 0);
  24375. /* update scrollbar */
  24376. scroll_offset = nk_scrollbar_behavior(state, in, has_scrolling, &scroll, &cursor,
  24377. &empty_north, &empty_south, scroll_offset, target, scroll_step, NK_VERTICAL);
  24378. scroll_off = scroll_offset / target;
  24379. cursor.y = scroll.y + (scroll_off * scroll.h) + style->border_cursor + style->padding.y;
  24380. /* draw scrollbar */
  24381. if (style->draw_begin) style->draw_begin(out, style->userdata);
  24382. nk_draw_scrollbar(out, *state, style, &scroll, &cursor);
  24383. if (style->draw_end) style->draw_end(out, style->userdata);
  24384. return scroll_offset;
  24385. }
  24386. NK_LIB float
  24387. nk_do_scrollbarh(nk_flags *state,
  24388. struct nk_command_buffer *out, struct nk_rect scroll, int has_scrolling,
  24389. float offset, float target, float step, float button_pixel_inc,
  24390. const struct nk_style_scrollbar *style, struct nk_input *in,
  24391. const struct nk_user_font *font)
  24392. {
  24393. struct nk_rect cursor;
  24394. struct nk_rect empty_west;
  24395. struct nk_rect empty_east;
  24396. float scroll_step;
  24397. float scroll_offset;
  24398. float scroll_off;
  24399. float scroll_ratio;
  24400. NK_ASSERT(out);
  24401. NK_ASSERT(style);
  24402. if (!out || !style) return 0;
  24403. /* scrollbar background */
  24404. scroll.h = NK_MAX(scroll.h, 1);
  24405. scroll.w = NK_MAX(scroll.w, 2 * scroll.h);
  24406. if (target <= scroll.w) return 0;
  24407. /* optional scrollbar buttons */
  24408. if (style->show_buttons) {
  24409. nk_flags ws;
  24410. float scroll_w;
  24411. struct nk_rect button;
  24412. button.y = scroll.y;
  24413. button.w = scroll.h;
  24414. button.h = scroll.h;
  24415. scroll_w = scroll.w - 2 * button.w;
  24416. scroll_step = NK_MIN(step, button_pixel_inc);
  24417. /* decrement button */
  24418. button.x = scroll.x;
  24419. if (nk_do_button_symbol(&ws, out, button, style->dec_symbol,
  24420. NK_BUTTON_REPEATER, &style->dec_button, in, font))
  24421. offset = offset - scroll_step;
  24422. /* increment button */
  24423. button.x = scroll.x + scroll.w - button.w;
  24424. if (nk_do_button_symbol(&ws, out, button, style->inc_symbol,
  24425. NK_BUTTON_REPEATER, &style->inc_button, in, font))
  24426. offset = offset + scroll_step;
  24427. scroll.x = scroll.x + button.w;
  24428. scroll.w = scroll_w;
  24429. }
  24430. /* calculate scrollbar constants */
  24431. scroll_step = NK_MIN(step, scroll.w);
  24432. scroll_offset = NK_CLAMP(0, offset, target - scroll.w);
  24433. scroll_ratio = scroll.w / target;
  24434. scroll_off = scroll_offset / target;
  24435. /* calculate cursor bounds */
  24436. cursor.w = (scroll_ratio * scroll.w) - (2*style->border + 2*style->padding.x);
  24437. cursor.x = scroll.x + (scroll_off * scroll.w) + style->border + style->padding.x;
  24438. cursor.h = scroll.h - (2 * style->border + 2 * style->padding.y);
  24439. cursor.y = scroll.y + style->border + style->padding.y;
  24440. /* calculate empty space around cursor */
  24441. empty_west.x = scroll.x;
  24442. empty_west.y = scroll.y;
  24443. empty_west.w = cursor.x - scroll.x;
  24444. empty_west.h = scroll.h;
  24445. empty_east.x = cursor.x + cursor.w;
  24446. empty_east.y = scroll.y;
  24447. empty_east.w = (scroll.x + scroll.w) - (cursor.x + cursor.w);
  24448. empty_east.h = scroll.h;
  24449. /* update scrollbar */
  24450. scroll_offset = nk_scrollbar_behavior(state, in, has_scrolling, &scroll, &cursor,
  24451. &empty_west, &empty_east, scroll_offset, target, scroll_step, NK_HORIZONTAL);
  24452. scroll_off = scroll_offset / target;
  24453. cursor.x = scroll.x + (scroll_off * scroll.w);
  24454. /* draw scrollbar */
  24455. if (style->draw_begin) style->draw_begin(out, style->userdata);
  24456. nk_draw_scrollbar(out, *state, style, &scroll, &cursor);
  24457. if (style->draw_end) style->draw_end(out, style->userdata);
  24458. return scroll_offset;
  24459. }
  24460. /* ===============================================================
  24461. *
  24462. * TEXT EDITOR
  24463. *
  24464. * ===============================================================*/
  24465. /* stb_textedit.h - v1.8 - public domain - Sean Barrett */
  24466. struct nk_text_find {
  24467. float x,y; /* position of n'th character */
  24468. float height; /* height of line */
  24469. int first_char, length; /* first char of row, and length */
  24470. int prev_first; /*_ first char of previous row */
  24471. };
  24472. struct nk_text_edit_row {
  24473. float x0,x1;
  24474. /* starting x location, end x location (allows for align=right, etc) */
  24475. float baseline_y_delta;
  24476. /* position of baseline relative to previous row's baseline*/
  24477. float ymin,ymax;
  24478. /* height of row above and below baseline */
  24479. int num_chars;
  24480. };
  24481. /* forward declarations */
  24482. NK_INTERN void nk_textedit_makeundo_delete(struct nk_text_edit*, int, int);
  24483. NK_INTERN void nk_textedit_makeundo_insert(struct nk_text_edit*, int, int);
  24484. NK_INTERN void nk_textedit_makeundo_replace(struct nk_text_edit*, int, int, int);
  24485. #define NK_TEXT_HAS_SELECTION(s) ((s)->select_start != (s)->select_end)
  24486. NK_INTERN float
  24487. nk_textedit_get_width(const struct nk_text_edit *edit, int line_start, int char_id,
  24488. const struct nk_user_font *font)
  24489. {
  24490. int len = 0;
  24491. nk_rune unicode = 0;
  24492. const char *str = nk_str_at_const(&edit->string, line_start + char_id, &unicode, &len);
  24493. return font->width(font->userdata, font->height, str, len);
  24494. }
  24495. NK_INTERN void
  24496. nk_textedit_layout_row(struct nk_text_edit_row *r, struct nk_text_edit *edit,
  24497. int line_start_id, float row_height, const struct nk_user_font *font)
  24498. {
  24499. int l;
  24500. int glyphs = 0;
  24501. nk_rune unicode;
  24502. const char *remaining;
  24503. int len = nk_str_len_char(&edit->string);
  24504. const char *end = nk_str_get_const(&edit->string) + len;
  24505. const char *text = nk_str_at_const(&edit->string, line_start_id, &unicode, &l);
  24506. const struct nk_vec2 size = nk_text_calculate_text_bounds(font,
  24507. text, (int)(end - text), row_height, &remaining, 0, &glyphs, NK_STOP_ON_NEW_LINE);
  24508. r->x0 = 0.0f;
  24509. r->x1 = size.x;
  24510. r->baseline_y_delta = size.y;
  24511. r->ymin = 0.0f;
  24512. r->ymax = size.y;
  24513. r->num_chars = glyphs;
  24514. }
  24515. NK_INTERN int
  24516. nk_textedit_locate_coord(struct nk_text_edit *edit, float x, float y,
  24517. const struct nk_user_font *font, float row_height)
  24518. {
  24519. struct nk_text_edit_row r;
  24520. int n = edit->string.len;
  24521. float base_y = 0, prev_x;
  24522. int i=0, k;
  24523. r.x0 = r.x1 = 0;
  24524. r.ymin = r.ymax = 0;
  24525. r.num_chars = 0;
  24526. /* search rows to find one that straddles 'y' */
  24527. while (i < n) {
  24528. nk_textedit_layout_row(&r, edit, i, row_height, font);
  24529. if (r.num_chars <= 0)
  24530. return n;
  24531. if (i==0 && y < base_y + r.ymin)
  24532. return 0;
  24533. if (y < base_y + r.ymax)
  24534. break;
  24535. i += r.num_chars;
  24536. base_y += r.baseline_y_delta;
  24537. }
  24538. /* below all text, return 'after' last character */
  24539. if (i >= n)
  24540. return n;
  24541. /* check if it's before the beginning of the line */
  24542. if (x < r.x0)
  24543. return i;
  24544. /* check if it's before the end of the line */
  24545. if (x < r.x1) {
  24546. /* search characters in row for one that straddles 'x' */
  24547. k = i;
  24548. prev_x = r.x0;
  24549. for (i=0; i < r.num_chars; ++i) {
  24550. float w = nk_textedit_get_width(edit, k, i, font);
  24551. if (x < prev_x+w) {
  24552. if (x < prev_x+w/2)
  24553. return k+i;
  24554. else return k+i+1;
  24555. }
  24556. prev_x += w;
  24557. }
  24558. /* shouldn't happen, but if it does, fall through to end-of-line case */
  24559. }
  24560. /* if the last character is a newline, return that.
  24561. * otherwise return 'after' the last character */
  24562. if (nk_str_rune_at(&edit->string, i+r.num_chars-1) == '\n')
  24563. return i+r.num_chars-1;
  24564. else return i+r.num_chars;
  24565. }
  24566. NK_LIB void
  24567. nk_textedit_click(struct nk_text_edit *state, float x, float y,
  24568. const struct nk_user_font *font, float row_height)
  24569. {
  24570. /* API click: on mouse down, move the cursor to the clicked location,
  24571. * and reset the selection */
  24572. state->cursor = nk_textedit_locate_coord(state, x, y, font, row_height);
  24573. state->select_start = state->cursor;
  24574. state->select_end = state->cursor;
  24575. state->has_preferred_x = 0;
  24576. }
  24577. NK_LIB void
  24578. nk_textedit_drag(struct nk_text_edit *state, float x, float y,
  24579. const struct nk_user_font *font, float row_height)
  24580. {
  24581. /* API drag: on mouse drag, move the cursor and selection endpoint
  24582. * to the clicked location */
  24583. int p = nk_textedit_locate_coord(state, x, y, font, row_height);
  24584. if (state->select_start == state->select_end)
  24585. state->select_start = state->cursor;
  24586. state->cursor = state->select_end = p;
  24587. }
  24588. NK_INTERN void
  24589. nk_textedit_find_charpos(struct nk_text_find *find, struct nk_text_edit *state,
  24590. int n, int single_line, const struct nk_user_font *font, float row_height)
  24591. {
  24592. /* find the x/y location of a character, and remember info about the previous
  24593. * row in case we get a move-up event (for page up, we'll have to rescan) */
  24594. struct nk_text_edit_row r;
  24595. int prev_start = 0;
  24596. int z = state->string.len;
  24597. int i=0, first;
  24598. nk_zero_struct(r);
  24599. if (n == z) {
  24600. /* if it's at the end, then find the last line -- simpler than trying to
  24601. explicitly handle this case in the regular code */
  24602. nk_textedit_layout_row(&r, state, 0, row_height, font);
  24603. if (single_line) {
  24604. find->first_char = 0;
  24605. find->length = z;
  24606. } else {
  24607. while (i < z) {
  24608. prev_start = i;
  24609. i += r.num_chars;
  24610. nk_textedit_layout_row(&r, state, i, row_height, font);
  24611. }
  24612. find->first_char = i;
  24613. find->length = r.num_chars;
  24614. }
  24615. find->x = r.x1;
  24616. find->y = r.ymin;
  24617. find->height = r.ymax - r.ymin;
  24618. find->prev_first = prev_start;
  24619. return;
  24620. }
  24621. /* search rows to find the one that straddles character n */
  24622. find->y = 0;
  24623. for(;;) {
  24624. nk_textedit_layout_row(&r, state, i, row_height, font);
  24625. if (n < i + r.num_chars) break;
  24626. prev_start = i;
  24627. i += r.num_chars;
  24628. find->y += r.baseline_y_delta;
  24629. }
  24630. find->first_char = first = i;
  24631. find->length = r.num_chars;
  24632. find->height = r.ymax - r.ymin;
  24633. find->prev_first = prev_start;
  24634. /* now scan to find xpos */
  24635. find->x = r.x0;
  24636. for (i=0; first+i < n; ++i)
  24637. find->x += nk_textedit_get_width(state, first, i, font);
  24638. }
  24639. NK_INTERN void
  24640. nk_textedit_clamp(struct nk_text_edit *state)
  24641. {
  24642. /* make the selection/cursor state valid if client altered the string */
  24643. int n = state->string.len;
  24644. if (NK_TEXT_HAS_SELECTION(state)) {
  24645. if (state->select_start > n) state->select_start = n;
  24646. if (state->select_end > n) state->select_end = n;
  24647. /* if clamping forced them to be equal, move the cursor to match */
  24648. if (state->select_start == state->select_end)
  24649. state->cursor = state->select_start;
  24650. }
  24651. if (state->cursor > n) state->cursor = n;
  24652. }
  24653. NK_API void
  24654. nk_textedit_delete(struct nk_text_edit *state, int where, int len)
  24655. {
  24656. /* delete characters while updating undo */
  24657. nk_textedit_makeundo_delete(state, where, len);
  24658. nk_str_delete_runes(&state->string, where, len);
  24659. state->has_preferred_x = 0;
  24660. }
  24661. NK_API void
  24662. nk_textedit_delete_selection(struct nk_text_edit *state)
  24663. {
  24664. /* delete the section */
  24665. nk_textedit_clamp(state);
  24666. if (NK_TEXT_HAS_SELECTION(state)) {
  24667. if (state->select_start < state->select_end) {
  24668. nk_textedit_delete(state, state->select_start,
  24669. state->select_end - state->select_start);
  24670. state->select_end = state->cursor = state->select_start;
  24671. } else {
  24672. nk_textedit_delete(state, state->select_end,
  24673. state->select_start - state->select_end);
  24674. state->select_start = state->cursor = state->select_end;
  24675. }
  24676. state->has_preferred_x = 0;
  24677. }
  24678. }
  24679. NK_INTERN void
  24680. nk_textedit_sortselection(struct nk_text_edit *state)
  24681. {
  24682. /* canonicalize the selection so start <= end */
  24683. if (state->select_end < state->select_start) {
  24684. int temp = state->select_end;
  24685. state->select_end = state->select_start;
  24686. state->select_start = temp;
  24687. }
  24688. }
  24689. NK_INTERN void
  24690. nk_textedit_move_to_first(struct nk_text_edit *state)
  24691. {
  24692. /* move cursor to first character of selection */
  24693. if (NK_TEXT_HAS_SELECTION(state)) {
  24694. nk_textedit_sortselection(state);
  24695. state->cursor = state->select_start;
  24696. state->select_end = state->select_start;
  24697. state->has_preferred_x = 0;
  24698. }
  24699. }
  24700. NK_INTERN void
  24701. nk_textedit_move_to_last(struct nk_text_edit *state)
  24702. {
  24703. /* move cursor to last character of selection */
  24704. if (NK_TEXT_HAS_SELECTION(state)) {
  24705. nk_textedit_sortselection(state);
  24706. nk_textedit_clamp(state);
  24707. state->cursor = state->select_end;
  24708. state->select_start = state->select_end;
  24709. state->has_preferred_x = 0;
  24710. }
  24711. }
  24712. NK_INTERN int
  24713. nk_is_word_boundary( struct nk_text_edit *state, int idx)
  24714. {
  24715. int len;
  24716. nk_rune c;
  24717. if (idx < 0) return 1;
  24718. if (!nk_str_at_rune(&state->string, idx, &c, &len)) return 1;
  24719. #ifndef NK_IS_WORD_BOUNDARY
  24720. return (c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '\f' ||
  24721. c == '\v' || c == 0x3000);
  24722. #else
  24723. return NK_IS_WORD_BOUNDARY(c);
  24724. #endif
  24725. }
  24726. NK_INTERN int
  24727. nk_textedit_move_to_word_previous(struct nk_text_edit *state)
  24728. {
  24729. int c = state->cursor - 1;
  24730. if (c > 0) {
  24731. if (nk_is_word_boundary(state, c)) {
  24732. while (c > 0 && nk_is_word_boundary(state, --c));
  24733. }
  24734. while (!nk_is_word_boundary(state, --c));
  24735. c++;
  24736. } else {
  24737. return 0;
  24738. }
  24739. return c;
  24740. }
  24741. NK_INTERN int
  24742. nk_textedit_move_to_word_next(struct nk_text_edit *state)
  24743. {
  24744. const int len = state->string.len;
  24745. int c = state->cursor;
  24746. if (c < len) {
  24747. if (!nk_is_word_boundary(state, c)) {
  24748. while (c < len && !nk_is_word_boundary(state, ++c));
  24749. }
  24750. while (c < len && nk_is_word_boundary(state, ++c));
  24751. } else {
  24752. return len;
  24753. }
  24754. return c;
  24755. }
  24756. NK_INTERN void
  24757. nk_textedit_prep_selection_at_cursor(struct nk_text_edit *state)
  24758. {
  24759. /* update selection and cursor to match each other */
  24760. if (!NK_TEXT_HAS_SELECTION(state))
  24761. state->select_start = state->select_end = state->cursor;
  24762. else state->cursor = state->select_end;
  24763. }
  24764. NK_API nk_bool
  24765. nk_textedit_cut(struct nk_text_edit *state)
  24766. {
  24767. /* API cut: delete selection */
  24768. if (state->mode == NK_TEXT_EDIT_MODE_VIEW)
  24769. return 0;
  24770. if (NK_TEXT_HAS_SELECTION(state)) {
  24771. nk_textedit_delete_selection(state); /* implicitly clamps */
  24772. state->has_preferred_x = 0;
  24773. return 1;
  24774. }
  24775. return 0;
  24776. }
  24777. NK_API nk_bool
  24778. nk_textedit_paste(struct nk_text_edit *state, char const *ctext, int len)
  24779. {
  24780. /* API paste: replace existing selection with passed-in text */
  24781. int glyphs;
  24782. const char *text = (const char *) ctext;
  24783. if (state->mode == NK_TEXT_EDIT_MODE_VIEW) return 0;
  24784. /* if there's a selection, the paste should delete it */
  24785. nk_textedit_clamp(state);
  24786. nk_textedit_delete_selection(state);
  24787. /* try to insert the characters */
  24788. glyphs = nk_utf_len(ctext, len);
  24789. if (nk_str_insert_text_char(&state->string, state->cursor, text, len)) {
  24790. nk_textedit_makeundo_insert(state, state->cursor, glyphs);
  24791. state->cursor += len;
  24792. state->has_preferred_x = 0;
  24793. return 1;
  24794. }
  24795. /* remove the undo since we didn't actually insert the characters */
  24796. if (state->undo.undo_point)
  24797. --state->undo.undo_point;
  24798. return 0;
  24799. }
  24800. NK_API void
  24801. nk_textedit_text(struct nk_text_edit *state, const char *text, int total_len)
  24802. {
  24803. nk_rune unicode;
  24804. int glyph_len;
  24805. int text_len = 0;
  24806. NK_ASSERT(state);
  24807. NK_ASSERT(text);
  24808. if (!text || !total_len || state->mode == NK_TEXT_EDIT_MODE_VIEW) return;
  24809. glyph_len = nk_utf_decode(text, &unicode, total_len);
  24810. while ((text_len < total_len) && glyph_len)
  24811. {
  24812. /* don't insert a backward delete, just process the event */
  24813. if (unicode == 127) goto next;
  24814. /* can't add newline in single-line mode */
  24815. if (unicode == '\n' && state->single_line) goto next;
  24816. /* filter incoming text */
  24817. if (state->filter && !state->filter(state, unicode)) goto next;
  24818. if (!NK_TEXT_HAS_SELECTION(state) &&
  24819. state->cursor < state->string.len)
  24820. {
  24821. if (state->mode == NK_TEXT_EDIT_MODE_REPLACE) {
  24822. nk_textedit_makeundo_replace(state, state->cursor, 1, 1);
  24823. nk_str_delete_runes(&state->string, state->cursor, 1);
  24824. }
  24825. if (nk_str_insert_text_utf8(&state->string, state->cursor,
  24826. text+text_len, 1))
  24827. {
  24828. ++state->cursor;
  24829. state->has_preferred_x = 0;
  24830. }
  24831. } else {
  24832. nk_textedit_delete_selection(state); /* implicitly clamps */
  24833. if (nk_str_insert_text_utf8(&state->string, state->cursor,
  24834. text+text_len, 1))
  24835. {
  24836. nk_textedit_makeundo_insert(state, state->cursor, 1);
  24837. state->cursor = NK_MIN(state->cursor + 1, state->string.len);
  24838. state->has_preferred_x = 0;
  24839. }
  24840. }
  24841. next:
  24842. text_len += glyph_len;
  24843. glyph_len = nk_utf_decode(text + text_len, &unicode, total_len-text_len);
  24844. }
  24845. }
  24846. NK_LIB void
  24847. nk_textedit_key(struct nk_text_edit *state, enum nk_keys key, int shift_mod,
  24848. const struct nk_user_font *font, float row_height)
  24849. {
  24850. retry:
  24851. switch (key)
  24852. {
  24853. case NK_KEY_NONE:
  24854. case NK_KEY_CTRL:
  24855. case NK_KEY_ENTER:
  24856. case NK_KEY_SHIFT:
  24857. case NK_KEY_TAB:
  24858. case NK_KEY_COPY:
  24859. case NK_KEY_CUT:
  24860. case NK_KEY_PASTE:
  24861. case NK_KEY_MAX:
  24862. default: break;
  24863. case NK_KEY_TEXT_UNDO:
  24864. nk_textedit_undo(state);
  24865. state->has_preferred_x = 0;
  24866. break;
  24867. case NK_KEY_TEXT_REDO:
  24868. nk_textedit_redo(state);
  24869. state->has_preferred_x = 0;
  24870. break;
  24871. case NK_KEY_TEXT_SELECT_ALL:
  24872. nk_textedit_select_all(state);
  24873. state->has_preferred_x = 0;
  24874. break;
  24875. case NK_KEY_TEXT_INSERT_MODE:
  24876. if (state->mode == NK_TEXT_EDIT_MODE_VIEW)
  24877. state->mode = NK_TEXT_EDIT_MODE_INSERT;
  24878. break;
  24879. case NK_KEY_TEXT_REPLACE_MODE:
  24880. if (state->mode == NK_TEXT_EDIT_MODE_VIEW)
  24881. state->mode = NK_TEXT_EDIT_MODE_REPLACE;
  24882. break;
  24883. case NK_KEY_TEXT_RESET_MODE:
  24884. if (state->mode == NK_TEXT_EDIT_MODE_INSERT ||
  24885. state->mode == NK_TEXT_EDIT_MODE_REPLACE)
  24886. state->mode = NK_TEXT_EDIT_MODE_VIEW;
  24887. break;
  24888. case NK_KEY_LEFT:
  24889. if (shift_mod) {
  24890. nk_textedit_clamp(state);
  24891. nk_textedit_prep_selection_at_cursor(state);
  24892. /* move selection left */
  24893. if (state->select_end > 0)
  24894. --state->select_end;
  24895. state->cursor = state->select_end;
  24896. state->has_preferred_x = 0;
  24897. } else {
  24898. /* if currently there's a selection,
  24899. * move cursor to start of selection */
  24900. if (NK_TEXT_HAS_SELECTION(state))
  24901. nk_textedit_move_to_first(state);
  24902. else if (state->cursor > 0)
  24903. --state->cursor;
  24904. state->has_preferred_x = 0;
  24905. } break;
  24906. case NK_KEY_RIGHT:
  24907. if (shift_mod) {
  24908. nk_textedit_prep_selection_at_cursor(state);
  24909. /* move selection right */
  24910. ++state->select_end;
  24911. nk_textedit_clamp(state);
  24912. state->cursor = state->select_end;
  24913. state->has_preferred_x = 0;
  24914. } else {
  24915. /* if currently there's a selection,
  24916. * move cursor to end of selection */
  24917. if (NK_TEXT_HAS_SELECTION(state))
  24918. nk_textedit_move_to_last(state);
  24919. else ++state->cursor;
  24920. nk_textedit_clamp(state);
  24921. state->has_preferred_x = 0;
  24922. } break;
  24923. case NK_KEY_TEXT_WORD_LEFT:
  24924. if (shift_mod) {
  24925. if( !NK_TEXT_HAS_SELECTION( state ) )
  24926. nk_textedit_prep_selection_at_cursor(state);
  24927. state->cursor = nk_textedit_move_to_word_previous(state);
  24928. state->select_end = state->cursor;
  24929. nk_textedit_clamp(state );
  24930. } else {
  24931. if (NK_TEXT_HAS_SELECTION(state))
  24932. nk_textedit_move_to_first(state);
  24933. else {
  24934. state->cursor = nk_textedit_move_to_word_previous(state);
  24935. nk_textedit_clamp(state );
  24936. }
  24937. } break;
  24938. case NK_KEY_TEXT_WORD_RIGHT:
  24939. if (shift_mod) {
  24940. if( !NK_TEXT_HAS_SELECTION( state ) )
  24941. nk_textedit_prep_selection_at_cursor(state);
  24942. state->cursor = nk_textedit_move_to_word_next(state);
  24943. state->select_end = state->cursor;
  24944. nk_textedit_clamp(state);
  24945. } else {
  24946. if (NK_TEXT_HAS_SELECTION(state))
  24947. nk_textedit_move_to_last(state);
  24948. else {
  24949. state->cursor = nk_textedit_move_to_word_next(state);
  24950. nk_textedit_clamp(state );
  24951. }
  24952. } break;
  24953. case NK_KEY_DOWN: {
  24954. struct nk_text_find find;
  24955. struct nk_text_edit_row row;
  24956. int i, sel = shift_mod;
  24957. if (state->single_line) {
  24958. /* on windows, up&down in single-line behave like left&right */
  24959. key = NK_KEY_RIGHT;
  24960. goto retry;
  24961. }
  24962. if (sel)
  24963. nk_textedit_prep_selection_at_cursor(state);
  24964. else if (NK_TEXT_HAS_SELECTION(state))
  24965. nk_textedit_move_to_last(state);
  24966. /* compute current position of cursor point */
  24967. nk_textedit_clamp(state);
  24968. nk_textedit_find_charpos(&find, state, state->cursor, state->single_line,
  24969. font, row_height);
  24970. /* now find character position down a row */
  24971. if (find.length)
  24972. {
  24973. float x;
  24974. float goal_x = state->has_preferred_x ? state->preferred_x : find.x;
  24975. int start = find.first_char + find.length;
  24976. state->cursor = start;
  24977. nk_textedit_layout_row(&row, state, state->cursor, row_height, font);
  24978. x = row.x0;
  24979. for (i=0; i < row.num_chars && x < row.x1; ++i) {
  24980. float dx = nk_textedit_get_width(state, start, i, font);
  24981. x += dx;
  24982. if (x > goal_x)
  24983. break;
  24984. ++state->cursor;
  24985. }
  24986. nk_textedit_clamp(state);
  24987. state->has_preferred_x = 1;
  24988. state->preferred_x = goal_x;
  24989. if (sel)
  24990. state->select_end = state->cursor;
  24991. }
  24992. } break;
  24993. case NK_KEY_UP: {
  24994. struct nk_text_find find;
  24995. struct nk_text_edit_row row;
  24996. int i, sel = shift_mod;
  24997. if (state->single_line) {
  24998. /* on windows, up&down become left&right */
  24999. key = NK_KEY_LEFT;
  25000. goto retry;
  25001. }
  25002. if (sel)
  25003. nk_textedit_prep_selection_at_cursor(state);
  25004. else if (NK_TEXT_HAS_SELECTION(state))
  25005. nk_textedit_move_to_first(state);
  25006. /* compute current position of cursor point */
  25007. nk_textedit_clamp(state);
  25008. nk_textedit_find_charpos(&find, state, state->cursor, state->single_line,
  25009. font, row_height);
  25010. /* can only go up if there's a previous row */
  25011. if (find.prev_first != find.first_char) {
  25012. /* now find character position up a row */
  25013. float x;
  25014. float goal_x = state->has_preferred_x ? state->preferred_x : find.x;
  25015. state->cursor = find.prev_first;
  25016. nk_textedit_layout_row(&row, state, state->cursor, row_height, font);
  25017. x = row.x0;
  25018. for (i=0; i < row.num_chars && x < row.x1; ++i) {
  25019. float dx = nk_textedit_get_width(state, find.prev_first, i, font);
  25020. x += dx;
  25021. if (x > goal_x)
  25022. break;
  25023. ++state->cursor;
  25024. }
  25025. nk_textedit_clamp(state);
  25026. state->has_preferred_x = 1;
  25027. state->preferred_x = goal_x;
  25028. if (sel) state->select_end = state->cursor;
  25029. }
  25030. } break;
  25031. case NK_KEY_DEL:
  25032. if (state->mode == NK_TEXT_EDIT_MODE_VIEW)
  25033. break;
  25034. if (NK_TEXT_HAS_SELECTION(state))
  25035. nk_textedit_delete_selection(state);
  25036. else {
  25037. int n = state->string.len;
  25038. if (state->cursor < n)
  25039. nk_textedit_delete(state, state->cursor, 1);
  25040. }
  25041. state->has_preferred_x = 0;
  25042. break;
  25043. case NK_KEY_BACKSPACE:
  25044. if (state->mode == NK_TEXT_EDIT_MODE_VIEW)
  25045. break;
  25046. if (NK_TEXT_HAS_SELECTION(state))
  25047. nk_textedit_delete_selection(state);
  25048. else {
  25049. nk_textedit_clamp(state);
  25050. if (state->cursor > 0) {
  25051. nk_textedit_delete(state, state->cursor-1, 1);
  25052. --state->cursor;
  25053. }
  25054. }
  25055. state->has_preferred_x = 0;
  25056. break;
  25057. case NK_KEY_TEXT_START:
  25058. if (shift_mod) {
  25059. nk_textedit_prep_selection_at_cursor(state);
  25060. state->cursor = state->select_end = 0;
  25061. state->has_preferred_x = 0;
  25062. } else {
  25063. state->cursor = state->select_start = state->select_end = 0;
  25064. state->has_preferred_x = 0;
  25065. }
  25066. break;
  25067. case NK_KEY_TEXT_END:
  25068. if (shift_mod) {
  25069. nk_textedit_prep_selection_at_cursor(state);
  25070. state->cursor = state->select_end = state->string.len;
  25071. state->has_preferred_x = 0;
  25072. } else {
  25073. state->cursor = state->string.len;
  25074. state->select_start = state->select_end = 0;
  25075. state->has_preferred_x = 0;
  25076. }
  25077. break;
  25078. case NK_KEY_TEXT_LINE_START: {
  25079. if (shift_mod) {
  25080. struct nk_text_find find;
  25081. nk_textedit_clamp(state);
  25082. nk_textedit_prep_selection_at_cursor(state);
  25083. if (state->string.len && state->cursor == state->string.len)
  25084. --state->cursor;
  25085. nk_textedit_find_charpos(&find, state,state->cursor, state->single_line,
  25086. font, row_height);
  25087. state->cursor = state->select_end = find.first_char;
  25088. state->has_preferred_x = 0;
  25089. } else {
  25090. struct nk_text_find find;
  25091. if (state->string.len && state->cursor == state->string.len)
  25092. --state->cursor;
  25093. nk_textedit_clamp(state);
  25094. nk_textedit_move_to_first(state);
  25095. nk_textedit_find_charpos(&find, state, state->cursor, state->single_line,
  25096. font, row_height);
  25097. state->cursor = find.first_char;
  25098. state->has_preferred_x = 0;
  25099. }
  25100. } break;
  25101. case NK_KEY_TEXT_LINE_END: {
  25102. if (shift_mod) {
  25103. struct nk_text_find find;
  25104. nk_textedit_clamp(state);
  25105. nk_textedit_prep_selection_at_cursor(state);
  25106. nk_textedit_find_charpos(&find, state, state->cursor, state->single_line,
  25107. font, row_height);
  25108. state->has_preferred_x = 0;
  25109. state->cursor = find.first_char + find.length;
  25110. if (find.length > 0 && nk_str_rune_at(&state->string, state->cursor-1) == '\n')
  25111. --state->cursor;
  25112. state->select_end = state->cursor;
  25113. } else {
  25114. struct nk_text_find find;
  25115. nk_textedit_clamp(state);
  25116. nk_textedit_move_to_first(state);
  25117. nk_textedit_find_charpos(&find, state, state->cursor, state->single_line,
  25118. font, row_height);
  25119. state->has_preferred_x = 0;
  25120. state->cursor = find.first_char + find.length;
  25121. if (find.length > 0 && nk_str_rune_at(&state->string, state->cursor-1) == '\n')
  25122. --state->cursor;
  25123. }} break;
  25124. }
  25125. }
  25126. NK_INTERN void
  25127. nk_textedit_flush_redo(struct nk_text_undo_state *state)
  25128. {
  25129. state->redo_point = NK_TEXTEDIT_UNDOSTATECOUNT;
  25130. state->redo_char_point = NK_TEXTEDIT_UNDOCHARCOUNT;
  25131. }
  25132. NK_INTERN void
  25133. nk_textedit_discard_undo(struct nk_text_undo_state *state)
  25134. {
  25135. /* discard the oldest entry in the undo list */
  25136. if (state->undo_point > 0) {
  25137. /* if the 0th undo state has characters, clean those up */
  25138. if (state->undo_rec[0].char_storage >= 0) {
  25139. int n = state->undo_rec[0].insert_length, i;
  25140. /* delete n characters from all other records */
  25141. state->undo_char_point = (short)(state->undo_char_point - n);
  25142. NK_MEMCPY(state->undo_char, state->undo_char + n,
  25143. (nk_size)state->undo_char_point*sizeof(nk_rune));
  25144. for (i=0; i < state->undo_point; ++i) {
  25145. if (state->undo_rec[i].char_storage >= 0)
  25146. state->undo_rec[i].char_storage = (short)
  25147. (state->undo_rec[i].char_storage - n);
  25148. }
  25149. }
  25150. --state->undo_point;
  25151. NK_MEMCPY(state->undo_rec, state->undo_rec+1,
  25152. (nk_size)((nk_size)state->undo_point * sizeof(state->undo_rec[0])));
  25153. }
  25154. }
  25155. NK_INTERN void
  25156. nk_textedit_discard_redo(struct nk_text_undo_state *state)
  25157. {
  25158. /* discard the oldest entry in the redo list--it's bad if this
  25159. ever happens, but because undo & redo have to store the actual
  25160. characters in different cases, the redo character buffer can
  25161. fill up even though the undo buffer didn't */
  25162. nk_size num;
  25163. int k = NK_TEXTEDIT_UNDOSTATECOUNT-1;
  25164. if (state->redo_point <= k) {
  25165. /* if the k'th undo state has characters, clean those up */
  25166. if (state->undo_rec[k].char_storage >= 0) {
  25167. int n = state->undo_rec[k].insert_length, i;
  25168. /* delete n characters from all other records */
  25169. state->redo_char_point = (short)(state->redo_char_point + n);
  25170. num = (nk_size)(NK_TEXTEDIT_UNDOCHARCOUNT - state->redo_char_point);
  25171. NK_MEMCPY(state->undo_char + state->redo_char_point,
  25172. state->undo_char + state->redo_char_point-n, num * sizeof(char));
  25173. for (i = state->redo_point; i < k; ++i) {
  25174. if (state->undo_rec[i].char_storage >= 0) {
  25175. state->undo_rec[i].char_storage = (short)
  25176. (state->undo_rec[i].char_storage + n);
  25177. }
  25178. }
  25179. }
  25180. ++state->redo_point;
  25181. num = (nk_size)(NK_TEXTEDIT_UNDOSTATECOUNT - state->redo_point);
  25182. if (num) NK_MEMCPY(state->undo_rec + state->redo_point-1,
  25183. state->undo_rec + state->redo_point, num * sizeof(state->undo_rec[0]));
  25184. }
  25185. }
  25186. NK_INTERN struct nk_text_undo_record*
  25187. nk_textedit_create_undo_record(struct nk_text_undo_state *state, int numchars)
  25188. {
  25189. /* any time we create a new undo record, we discard redo*/
  25190. nk_textedit_flush_redo(state);
  25191. /* if we have no free records, we have to make room,
  25192. * by sliding the existing records down */
  25193. if (state->undo_point == NK_TEXTEDIT_UNDOSTATECOUNT)
  25194. nk_textedit_discard_undo(state);
  25195. /* if the characters to store won't possibly fit in the buffer,
  25196. * we can't undo */
  25197. if (numchars > NK_TEXTEDIT_UNDOCHARCOUNT) {
  25198. state->undo_point = 0;
  25199. state->undo_char_point = 0;
  25200. return 0;
  25201. }
  25202. /* if we don't have enough free characters in the buffer,
  25203. * we have to make room */
  25204. while (state->undo_char_point + numchars > NK_TEXTEDIT_UNDOCHARCOUNT)
  25205. nk_textedit_discard_undo(state);
  25206. return &state->undo_rec[state->undo_point++];
  25207. }
  25208. NK_INTERN nk_rune*
  25209. nk_textedit_createundo(struct nk_text_undo_state *state, int pos,
  25210. int insert_len, int delete_len)
  25211. {
  25212. struct nk_text_undo_record *r = nk_textedit_create_undo_record(state, insert_len);
  25213. if (r == 0)
  25214. return 0;
  25215. r->where = pos;
  25216. r->insert_length = (short) insert_len;
  25217. r->delete_length = (short) delete_len;
  25218. if (insert_len == 0) {
  25219. r->char_storage = -1;
  25220. return 0;
  25221. } else {
  25222. r->char_storage = state->undo_char_point;
  25223. state->undo_char_point = (short)(state->undo_char_point + insert_len);
  25224. return &state->undo_char[r->char_storage];
  25225. }
  25226. }
  25227. NK_API void
  25228. nk_textedit_undo(struct nk_text_edit *state)
  25229. {
  25230. struct nk_text_undo_state *s = &state->undo;
  25231. struct nk_text_undo_record u, *r;
  25232. if (s->undo_point == 0)
  25233. return;
  25234. /* we need to do two things: apply the undo record, and create a redo record */
  25235. u = s->undo_rec[s->undo_point-1];
  25236. r = &s->undo_rec[s->redo_point-1];
  25237. r->char_storage = -1;
  25238. r->insert_length = u.delete_length;
  25239. r->delete_length = u.insert_length;
  25240. r->where = u.where;
  25241. if (u.delete_length)
  25242. {
  25243. /* if the undo record says to delete characters, then the redo record will
  25244. need to re-insert the characters that get deleted, so we need to store
  25245. them.
  25246. there are three cases:
  25247. - there's enough room to store the characters
  25248. - characters stored for *redoing* don't leave room for redo
  25249. - characters stored for *undoing* don't leave room for redo
  25250. if the last is true, we have to bail */
  25251. if (s->undo_char_point + u.delete_length >= NK_TEXTEDIT_UNDOCHARCOUNT) {
  25252. /* the undo records take up too much character space; there's no space
  25253. * to store the redo characters */
  25254. r->insert_length = 0;
  25255. } else {
  25256. int i;
  25257. /* there's definitely room to store the characters eventually */
  25258. while (s->undo_char_point + u.delete_length > s->redo_char_point) {
  25259. /* there's currently not enough room, so discard a redo record */
  25260. nk_textedit_discard_redo(s);
  25261. /* should never happen: */
  25262. if (s->redo_point == NK_TEXTEDIT_UNDOSTATECOUNT)
  25263. return;
  25264. }
  25265. r = &s->undo_rec[s->redo_point-1];
  25266. r->char_storage = (short)(s->redo_char_point - u.delete_length);
  25267. s->redo_char_point = (short)(s->redo_char_point - u.delete_length);
  25268. /* now save the characters */
  25269. for (i=0; i < u.delete_length; ++i)
  25270. s->undo_char[r->char_storage + i] =
  25271. nk_str_rune_at(&state->string, u.where + i);
  25272. }
  25273. /* now we can carry out the deletion */
  25274. nk_str_delete_runes(&state->string, u.where, u.delete_length);
  25275. }
  25276. /* check type of recorded action: */
  25277. if (u.insert_length) {
  25278. /* easy case: was a deletion, so we need to insert n characters */
  25279. nk_str_insert_text_runes(&state->string, u.where,
  25280. &s->undo_char[u.char_storage], u.insert_length);
  25281. s->undo_char_point = (short)(s->undo_char_point - u.insert_length);
  25282. }
  25283. state->cursor = (short)(u.where + u.insert_length);
  25284. s->undo_point--;
  25285. s->redo_point--;
  25286. }
  25287. NK_API void
  25288. nk_textedit_redo(struct nk_text_edit *state)
  25289. {
  25290. struct nk_text_undo_state *s = &state->undo;
  25291. struct nk_text_undo_record *u, r;
  25292. if (s->redo_point == NK_TEXTEDIT_UNDOSTATECOUNT)
  25293. return;
  25294. /* we need to do two things: apply the redo record, and create an undo record */
  25295. u = &s->undo_rec[s->undo_point];
  25296. r = s->undo_rec[s->redo_point];
  25297. /* we KNOW there must be room for the undo record, because the redo record
  25298. was derived from an undo record */
  25299. u->delete_length = r.insert_length;
  25300. u->insert_length = r.delete_length;
  25301. u->where = r.where;
  25302. u->char_storage = -1;
  25303. if (r.delete_length) {
  25304. /* the redo record requires us to delete characters, so the undo record
  25305. needs to store the characters */
  25306. if (s->undo_char_point + u->insert_length > s->redo_char_point) {
  25307. u->insert_length = 0;
  25308. u->delete_length = 0;
  25309. } else {
  25310. int i;
  25311. u->char_storage = s->undo_char_point;
  25312. s->undo_char_point = (short)(s->undo_char_point + u->insert_length);
  25313. /* now save the characters */
  25314. for (i=0; i < u->insert_length; ++i) {
  25315. s->undo_char[u->char_storage + i] =
  25316. nk_str_rune_at(&state->string, u->where + i);
  25317. }
  25318. }
  25319. nk_str_delete_runes(&state->string, r.where, r.delete_length);
  25320. }
  25321. if (r.insert_length) {
  25322. /* easy case: need to insert n characters */
  25323. nk_str_insert_text_runes(&state->string, r.where,
  25324. &s->undo_char[r.char_storage], r.insert_length);
  25325. }
  25326. state->cursor = r.where + r.insert_length;
  25327. s->undo_point++;
  25328. s->redo_point++;
  25329. }
  25330. NK_INTERN void
  25331. nk_textedit_makeundo_insert(struct nk_text_edit *state, int where, int length)
  25332. {
  25333. nk_textedit_createundo(&state->undo, where, 0, length);
  25334. }
  25335. NK_INTERN void
  25336. nk_textedit_makeundo_delete(struct nk_text_edit *state, int where, int length)
  25337. {
  25338. int i;
  25339. nk_rune *p = nk_textedit_createundo(&state->undo, where, length, 0);
  25340. if (p) {
  25341. for (i=0; i < length; ++i)
  25342. p[i] = nk_str_rune_at(&state->string, where+i);
  25343. }
  25344. }
  25345. NK_INTERN void
  25346. nk_textedit_makeundo_replace(struct nk_text_edit *state, int where,
  25347. int old_length, int new_length)
  25348. {
  25349. int i;
  25350. nk_rune *p = nk_textedit_createundo(&state->undo, where, old_length, new_length);
  25351. if (p) {
  25352. for (i=0; i < old_length; ++i)
  25353. p[i] = nk_str_rune_at(&state->string, where+i);
  25354. }
  25355. }
  25356. NK_LIB void
  25357. nk_textedit_clear_state(struct nk_text_edit *state, enum nk_text_edit_type type,
  25358. nk_plugin_filter filter)
  25359. {
  25360. /* reset the state to default */
  25361. state->undo.undo_point = 0;
  25362. state->undo.undo_char_point = 0;
  25363. state->undo.redo_point = NK_TEXTEDIT_UNDOSTATECOUNT;
  25364. state->undo.redo_char_point = NK_TEXTEDIT_UNDOCHARCOUNT;
  25365. state->select_end = state->select_start = 0;
  25366. state->cursor = 0;
  25367. state->has_preferred_x = 0;
  25368. state->preferred_x = 0;
  25369. state->cursor_at_end_of_line = 0;
  25370. state->initialized = 1;
  25371. state->single_line = (unsigned char)(type == NK_TEXT_EDIT_SINGLE_LINE);
  25372. state->mode = NK_TEXT_EDIT_MODE_VIEW;
  25373. state->filter = filter;
  25374. state->scrollbar = nk_vec2(0,0);
  25375. }
  25376. NK_API void
  25377. nk_textedit_init_fixed(struct nk_text_edit *state, void *memory, nk_size size)
  25378. {
  25379. NK_ASSERT(state);
  25380. NK_ASSERT(memory);
  25381. if (!state || !memory || !size) return;
  25382. NK_MEMSET(state, 0, sizeof(struct nk_text_edit));
  25383. nk_textedit_clear_state(state, NK_TEXT_EDIT_SINGLE_LINE, 0);
  25384. nk_str_init_fixed(&state->string, memory, size);
  25385. }
  25386. NK_API void
  25387. nk_textedit_init(struct nk_text_edit *state, const struct nk_allocator *alloc, nk_size size)
  25388. {
  25389. NK_ASSERT(state);
  25390. NK_ASSERT(alloc);
  25391. if (!state || !alloc) return;
  25392. NK_MEMSET(state, 0, sizeof(struct nk_text_edit));
  25393. nk_textedit_clear_state(state, NK_TEXT_EDIT_SINGLE_LINE, 0);
  25394. nk_str_init(&state->string, alloc, size);
  25395. }
  25396. #ifdef NK_INCLUDE_DEFAULT_ALLOCATOR
  25397. NK_API void
  25398. nk_textedit_init_default(struct nk_text_edit *state)
  25399. {
  25400. NK_ASSERT(state);
  25401. if (!state) return;
  25402. NK_MEMSET(state, 0, sizeof(struct nk_text_edit));
  25403. nk_textedit_clear_state(state, NK_TEXT_EDIT_SINGLE_LINE, 0);
  25404. nk_str_init_default(&state->string);
  25405. }
  25406. #endif
  25407. NK_API void
  25408. nk_textedit_select_all(struct nk_text_edit *state)
  25409. {
  25410. NK_ASSERT(state);
  25411. state->select_start = 0;
  25412. state->select_end = state->string.len;
  25413. }
  25414. NK_API void
  25415. nk_textedit_free(struct nk_text_edit *state)
  25416. {
  25417. NK_ASSERT(state);
  25418. if (!state) return;
  25419. nk_str_free(&state->string);
  25420. }
  25421. /* ===============================================================
  25422. *
  25423. * FILTER
  25424. *
  25425. * ===============================================================*/
  25426. NK_API nk_bool
  25427. nk_filter_default(const struct nk_text_edit *box, nk_rune unicode)
  25428. {
  25429. NK_UNUSED(unicode);
  25430. NK_UNUSED(box);
  25431. return nk_true;
  25432. }
  25433. NK_API nk_bool
  25434. nk_filter_ascii(const struct nk_text_edit *box, nk_rune unicode)
  25435. {
  25436. NK_UNUSED(box);
  25437. if (unicode > 128) return nk_false;
  25438. else return nk_true;
  25439. }
  25440. NK_API nk_bool
  25441. nk_filter_float(const struct nk_text_edit *box, nk_rune unicode)
  25442. {
  25443. NK_UNUSED(box);
  25444. if ((unicode < '0' || unicode > '9') && unicode != '.' && unicode != '-')
  25445. return nk_false;
  25446. else return nk_true;
  25447. }
  25448. NK_API nk_bool
  25449. nk_filter_decimal(const struct nk_text_edit *box, nk_rune unicode)
  25450. {
  25451. NK_UNUSED(box);
  25452. if ((unicode < '0' || unicode > '9') && unicode != '-')
  25453. return nk_false;
  25454. else return nk_true;
  25455. }
  25456. NK_API nk_bool
  25457. nk_filter_hex(const struct nk_text_edit *box, nk_rune unicode)
  25458. {
  25459. NK_UNUSED(box);
  25460. if ((unicode < '0' || unicode > '9') &&
  25461. (unicode < 'a' || unicode > 'f') &&
  25462. (unicode < 'A' || unicode > 'F'))
  25463. return nk_false;
  25464. else return nk_true;
  25465. }
  25466. NK_API nk_bool
  25467. nk_filter_oct(const struct nk_text_edit *box, nk_rune unicode)
  25468. {
  25469. NK_UNUSED(box);
  25470. if (unicode < '0' || unicode > '7')
  25471. return nk_false;
  25472. else return nk_true;
  25473. }
  25474. NK_API nk_bool
  25475. nk_filter_binary(const struct nk_text_edit *box, nk_rune unicode)
  25476. {
  25477. NK_UNUSED(box);
  25478. if (unicode != '0' && unicode != '1')
  25479. return nk_false;
  25480. else return nk_true;
  25481. }
  25482. /* ===============================================================
  25483. *
  25484. * EDIT
  25485. *
  25486. * ===============================================================*/
  25487. NK_LIB void
  25488. nk_edit_draw_text(struct nk_command_buffer *out,
  25489. const struct nk_style_edit *style, float pos_x, float pos_y,
  25490. float x_offset, const char *text, int byte_len, float row_height,
  25491. const struct nk_user_font *font, struct nk_color background,
  25492. struct nk_color foreground, nk_bool is_selected)
  25493. {
  25494. NK_ASSERT(out);
  25495. NK_ASSERT(font);
  25496. NK_ASSERT(style);
  25497. if (!text || !byte_len || !out || !style) return;
  25498. {int glyph_len = 0;
  25499. nk_rune unicode = 0;
  25500. int text_len = 0;
  25501. float line_width = 0;
  25502. float glyph_width;
  25503. const char *line = text;
  25504. float line_offset = 0;
  25505. int line_count = 0;
  25506. struct nk_text txt;
  25507. txt.padding = nk_vec2(0,0);
  25508. txt.background = background;
  25509. txt.text = foreground;
  25510. foreground = nk_rgb_factor(foreground, style->color_factor);
  25511. background = nk_rgb_factor(background, style->color_factor);
  25512. glyph_len = nk_utf_decode(text+text_len, &unicode, byte_len-text_len);
  25513. if (!glyph_len) return;
  25514. while ((text_len < byte_len) && glyph_len)
  25515. {
  25516. if (unicode == '\n') {
  25517. /* new line separator so draw previous line */
  25518. struct nk_rect label;
  25519. label.y = pos_y + line_offset;
  25520. label.h = row_height;
  25521. label.w = line_width;
  25522. label.x = pos_x;
  25523. if (!line_count)
  25524. label.x += x_offset;
  25525. if (is_selected) /* selection needs to draw different background color */
  25526. nk_fill_rect(out, label, 0, background);
  25527. nk_widget_text(out, label, line, (int)((text + text_len) - line),
  25528. &txt, NK_TEXT_CENTERED, font);
  25529. text_len++;
  25530. line_count++;
  25531. line_width = 0;
  25532. line = text + text_len;
  25533. line_offset += row_height;
  25534. glyph_len = nk_utf_decode(text + text_len, &unicode, (int)(byte_len-text_len));
  25535. continue;
  25536. }
  25537. if (unicode == '\r') {
  25538. text_len++;
  25539. glyph_len = nk_utf_decode(text + text_len, &unicode, byte_len-text_len);
  25540. continue;
  25541. }
  25542. glyph_width = font->width(font->userdata, font->height, text+text_len, glyph_len);
  25543. line_width += (float)glyph_width;
  25544. text_len += glyph_len;
  25545. glyph_len = nk_utf_decode(text + text_len, &unicode, byte_len-text_len);
  25546. continue;
  25547. }
  25548. if (line_width > 0) {
  25549. /* draw last line */
  25550. struct nk_rect label;
  25551. label.y = pos_y + line_offset;
  25552. label.h = row_height;
  25553. label.w = line_width;
  25554. label.x = pos_x;
  25555. if (!line_count)
  25556. label.x += x_offset;
  25557. if (is_selected)
  25558. nk_fill_rect(out, label, 0, background);
  25559. nk_widget_text(out, label, line, (int)((text + text_len) - line),
  25560. &txt, NK_TEXT_LEFT, font);
  25561. }}
  25562. }
  25563. NK_LIB nk_flags
  25564. nk_do_edit(nk_flags *state, struct nk_command_buffer *out,
  25565. struct nk_rect bounds, nk_flags flags, nk_plugin_filter filter,
  25566. struct nk_text_edit *edit, const struct nk_style_edit *style,
  25567. struct nk_input *in, const struct nk_user_font *font)
  25568. {
  25569. struct nk_rect area;
  25570. nk_flags ret = 0;
  25571. float row_height;
  25572. char prev_state = 0;
  25573. char is_hovered = 0;
  25574. char select_all = 0;
  25575. char cursor_follow = 0;
  25576. struct nk_rect old_clip;
  25577. struct nk_rect clip;
  25578. NK_ASSERT(state);
  25579. NK_ASSERT(out);
  25580. NK_ASSERT(style);
  25581. if (!state || !out || !style)
  25582. return ret;
  25583. /* visible text area calculation */
  25584. area.x = bounds.x + style->padding.x + style->border;
  25585. area.y = bounds.y + style->padding.y + style->border;
  25586. area.w = bounds.w - (2.0f * style->padding.x + 2 * style->border);
  25587. area.h = bounds.h - (2.0f * style->padding.y + 2 * style->border);
  25588. if (flags & NK_EDIT_MULTILINE)
  25589. area.w = NK_MAX(0, area.w - style->scrollbar_size.x);
  25590. row_height = (flags & NK_EDIT_MULTILINE)? font->height + style->row_padding: area.h;
  25591. /* calculate clipping rectangle */
  25592. old_clip = out->clip;
  25593. nk_unify(&clip, &old_clip, area.x, area.y, area.x + area.w, area.y + area.h);
  25594. /* update edit state */
  25595. prev_state = (char)edit->active;
  25596. if (in && in->mouse.buttons[NK_BUTTON_LEFT].clicked && in->mouse.buttons[NK_BUTTON_LEFT].down) {
  25597. edit->active = NK_INBOX(in->mouse.pos.x, in->mouse.pos.y,
  25598. bounds.x, bounds.y, bounds.w, bounds.h);
  25599. }
  25600. /* (de)activate text editor */
  25601. if (!prev_state && edit->active) {
  25602. const enum nk_text_edit_type type = (flags & NK_EDIT_MULTILINE) ?
  25603. NK_TEXT_EDIT_MULTI_LINE: NK_TEXT_EDIT_SINGLE_LINE;
  25604. /* keep scroll position when re-activating edit widget */
  25605. struct nk_vec2 oldscrollbar = edit->scrollbar;
  25606. nk_textedit_clear_state(edit, type, filter);
  25607. edit->scrollbar = oldscrollbar;
  25608. if (flags & NK_EDIT_AUTO_SELECT)
  25609. select_all = nk_true;
  25610. if (flags & NK_EDIT_GOTO_END_ON_ACTIVATE) {
  25611. edit->cursor = edit->string.len;
  25612. in = 0;
  25613. }
  25614. } else if (!edit->active) edit->mode = NK_TEXT_EDIT_MODE_VIEW;
  25615. if (flags & NK_EDIT_READ_ONLY)
  25616. edit->mode = NK_TEXT_EDIT_MODE_VIEW;
  25617. else if (flags & NK_EDIT_ALWAYS_INSERT_MODE)
  25618. edit->mode = NK_TEXT_EDIT_MODE_INSERT;
  25619. ret = (edit->active) ? NK_EDIT_ACTIVE: NK_EDIT_INACTIVE;
  25620. if (prev_state != edit->active)
  25621. ret |= (edit->active) ? NK_EDIT_ACTIVATED: NK_EDIT_DEACTIVATED;
  25622. /* handle user input */
  25623. if (edit->active && in)
  25624. {
  25625. int shift_mod = in->keyboard.keys[NK_KEY_SHIFT].down;
  25626. const float mouse_x = (in->mouse.pos.x - area.x) + edit->scrollbar.x;
  25627. const float mouse_y = (in->mouse.pos.y - area.y) + edit->scrollbar.y;
  25628. /* mouse click handler */
  25629. is_hovered = (char)nk_input_is_mouse_hovering_rect(in, area);
  25630. if (select_all) {
  25631. nk_textedit_select_all(edit);
  25632. } else if (is_hovered && in->mouse.buttons[NK_BUTTON_LEFT].down &&
  25633. in->mouse.buttons[NK_BUTTON_LEFT].clicked) {
  25634. nk_textedit_click(edit, mouse_x, mouse_y, font, row_height);
  25635. } else if (is_hovered && in->mouse.buttons[NK_BUTTON_LEFT].down &&
  25636. (in->mouse.delta.x != 0.0f || in->mouse.delta.y != 0.0f)) {
  25637. nk_textedit_drag(edit, mouse_x, mouse_y, font, row_height);
  25638. cursor_follow = nk_true;
  25639. } else if (is_hovered && in->mouse.buttons[NK_BUTTON_RIGHT].clicked &&
  25640. in->mouse.buttons[NK_BUTTON_RIGHT].down) {
  25641. nk_textedit_key(edit, NK_KEY_TEXT_WORD_LEFT, nk_false, font, row_height);
  25642. nk_textedit_key(edit, NK_KEY_TEXT_WORD_RIGHT, nk_true, font, row_height);
  25643. cursor_follow = nk_true;
  25644. }
  25645. {int i; /* keyboard input */
  25646. int old_mode = edit->mode;
  25647. for (i = 0; i < NK_KEY_MAX; ++i) {
  25648. if (i == NK_KEY_ENTER || i == NK_KEY_TAB) continue; /* special case */
  25649. if (nk_input_is_key_pressed(in, (enum nk_keys)i)) {
  25650. nk_textedit_key(edit, (enum nk_keys)i, shift_mod, font, row_height);
  25651. cursor_follow = nk_true;
  25652. }
  25653. }
  25654. if (old_mode != edit->mode) {
  25655. in->keyboard.text_len = 0;
  25656. }}
  25657. /* text input */
  25658. edit->filter = filter;
  25659. if (in->keyboard.text_len) {
  25660. nk_textedit_text(edit, in->keyboard.text, in->keyboard.text_len);
  25661. cursor_follow = nk_true;
  25662. in->keyboard.text_len = 0;
  25663. }
  25664. /* enter key handler */
  25665. if (nk_input_is_key_pressed(in, NK_KEY_ENTER)) {
  25666. cursor_follow = nk_true;
  25667. if (flags & NK_EDIT_CTRL_ENTER_NEWLINE && shift_mod)
  25668. nk_textedit_text(edit, "\n", 1);
  25669. else if (flags & NK_EDIT_SIG_ENTER)
  25670. ret |= NK_EDIT_COMMITED;
  25671. else nk_textedit_text(edit, "\n", 1);
  25672. }
  25673. /* cut & copy handler */
  25674. {int copy= nk_input_is_key_pressed(in, NK_KEY_COPY);
  25675. int cut = nk_input_is_key_pressed(in, NK_KEY_CUT);
  25676. if ((copy || cut) && (flags & NK_EDIT_CLIPBOARD))
  25677. {
  25678. int glyph_len;
  25679. nk_rune unicode;
  25680. const char *text;
  25681. int b = edit->select_start;
  25682. int e = edit->select_end;
  25683. int begin = NK_MIN(b, e);
  25684. int end = NK_MAX(b, e);
  25685. text = nk_str_at_const(&edit->string, begin, &unicode, &glyph_len);
  25686. if (edit->clip.copy)
  25687. edit->clip.copy(edit->clip.userdata, text, end - begin);
  25688. if (cut && !(flags & NK_EDIT_READ_ONLY)){
  25689. nk_textedit_cut(edit);
  25690. cursor_follow = nk_true;
  25691. }
  25692. }}
  25693. /* paste handler */
  25694. {int paste = nk_input_is_key_pressed(in, NK_KEY_PASTE);
  25695. if (paste && (flags & NK_EDIT_CLIPBOARD) && edit->clip.paste) {
  25696. edit->clip.paste(edit->clip.userdata, edit);
  25697. cursor_follow = nk_true;
  25698. }}
  25699. /* tab handler */
  25700. {int tab = nk_input_is_key_pressed(in, NK_KEY_TAB);
  25701. if (tab && (flags & NK_EDIT_ALLOW_TAB)) {
  25702. nk_textedit_text(edit, " ", 4);
  25703. cursor_follow = nk_true;
  25704. }}
  25705. }
  25706. /* set widget state */
  25707. if (edit->active)
  25708. *state = NK_WIDGET_STATE_ACTIVE;
  25709. else nk_widget_state_reset(state);
  25710. if (is_hovered)
  25711. *state |= NK_WIDGET_STATE_HOVERED;
  25712. /* DRAW EDIT */
  25713. {const char *text = nk_str_get_const(&edit->string);
  25714. int len = nk_str_len_char(&edit->string);
  25715. {/* select background colors/images */
  25716. const struct nk_style_item *background;
  25717. if (*state & NK_WIDGET_STATE_ACTIVED)
  25718. background = &style->active;
  25719. else if (*state & NK_WIDGET_STATE_HOVER)
  25720. background = &style->hover;
  25721. else background = &style->normal;
  25722. /* draw background frame */
  25723. switch(background->type) {
  25724. case NK_STYLE_ITEM_IMAGE:
  25725. nk_draw_image(out, bounds, &background->data.image, nk_rgb_factor(nk_white, style->color_factor));
  25726. break;
  25727. case NK_STYLE_ITEM_NINE_SLICE:
  25728. nk_draw_nine_slice(out, bounds, &background->data.slice, nk_rgb_factor(nk_white, style->color_factor));
  25729. break;
  25730. case NK_STYLE_ITEM_COLOR:
  25731. nk_fill_rect(out, bounds, style->rounding, nk_rgb_factor(background->data.color, style->color_factor));
  25732. nk_stroke_rect(out, bounds, style->rounding, style->border, nk_rgb_factor(style->border_color, style->color_factor));
  25733. break;
  25734. }}
  25735. area.w = NK_MAX(0, area.w - style->cursor_size);
  25736. if (edit->active)
  25737. {
  25738. int total_lines = 1;
  25739. struct nk_vec2 text_size = nk_vec2(0,0);
  25740. /* text pointer positions */
  25741. const char *cursor_ptr = 0;
  25742. const char *select_begin_ptr = 0;
  25743. const char *select_end_ptr = 0;
  25744. /* 2D pixel positions */
  25745. struct nk_vec2 cursor_pos = nk_vec2(0,0);
  25746. struct nk_vec2 selection_offset_start = nk_vec2(0,0);
  25747. struct nk_vec2 selection_offset_end = nk_vec2(0,0);
  25748. int selection_begin = NK_MIN(edit->select_start, edit->select_end);
  25749. int selection_end = NK_MAX(edit->select_start, edit->select_end);
  25750. /* calculate total line count + total space + cursor/selection position */
  25751. float line_width = 0.0f;
  25752. if (text && len)
  25753. {
  25754. /* utf8 encoding */
  25755. float glyph_width;
  25756. int glyph_len = 0;
  25757. nk_rune unicode = 0;
  25758. int text_len = 0;
  25759. int glyphs = 0;
  25760. int row_begin = 0;
  25761. glyph_len = nk_utf_decode(text, &unicode, len);
  25762. glyph_width = font->width(font->userdata, font->height, text, glyph_len);
  25763. line_width = 0;
  25764. /* iterate all lines */
  25765. while ((text_len < len) && glyph_len)
  25766. {
  25767. /* set cursor 2D position and line */
  25768. if (!cursor_ptr && glyphs == edit->cursor)
  25769. {
  25770. int glyph_offset;
  25771. struct nk_vec2 out_offset;
  25772. struct nk_vec2 row_size;
  25773. const char *remaining;
  25774. /* calculate 2d position */
  25775. cursor_pos.y = (float)(total_lines-1) * row_height;
  25776. row_size = nk_text_calculate_text_bounds(font, text+row_begin,
  25777. text_len-row_begin, row_height, &remaining,
  25778. &out_offset, &glyph_offset, NK_STOP_ON_NEW_LINE);
  25779. cursor_pos.x = row_size.x;
  25780. cursor_ptr = text + text_len;
  25781. }
  25782. /* set start selection 2D position and line */
  25783. if (!select_begin_ptr && edit->select_start != edit->select_end &&
  25784. glyphs == selection_begin)
  25785. {
  25786. int glyph_offset;
  25787. struct nk_vec2 out_offset;
  25788. struct nk_vec2 row_size;
  25789. const char *remaining;
  25790. /* calculate 2d position */
  25791. selection_offset_start.y = (float)(NK_MAX(total_lines-1,0)) * row_height;
  25792. row_size = nk_text_calculate_text_bounds(font, text+row_begin,
  25793. text_len-row_begin, row_height, &remaining,
  25794. &out_offset, &glyph_offset, NK_STOP_ON_NEW_LINE);
  25795. selection_offset_start.x = row_size.x;
  25796. select_begin_ptr = text + text_len;
  25797. }
  25798. /* set end selection 2D position and line */
  25799. if (!select_end_ptr && edit->select_start != edit->select_end &&
  25800. glyphs == selection_end)
  25801. {
  25802. int glyph_offset;
  25803. struct nk_vec2 out_offset;
  25804. struct nk_vec2 row_size;
  25805. const char *remaining;
  25806. /* calculate 2d position */
  25807. selection_offset_end.y = (float)(total_lines-1) * row_height;
  25808. row_size = nk_text_calculate_text_bounds(font, text+row_begin,
  25809. text_len-row_begin, row_height, &remaining,
  25810. &out_offset, &glyph_offset, NK_STOP_ON_NEW_LINE);
  25811. selection_offset_end.x = row_size.x;
  25812. select_end_ptr = text + text_len;
  25813. }
  25814. if (unicode == '\n') {
  25815. text_size.x = NK_MAX(text_size.x, line_width);
  25816. total_lines++;
  25817. line_width = 0;
  25818. text_len++;
  25819. glyphs++;
  25820. row_begin = text_len;
  25821. glyph_len = nk_utf_decode(text + text_len, &unicode, len-text_len);
  25822. glyph_width = font->width(font->userdata, font->height, text+text_len, glyph_len);
  25823. continue;
  25824. }
  25825. glyphs++;
  25826. text_len += glyph_len;
  25827. line_width += (float)glyph_width;
  25828. glyph_len = nk_utf_decode(text + text_len, &unicode, len-text_len);
  25829. glyph_width = font->width(font->userdata, font->height,
  25830. text+text_len, glyph_len);
  25831. continue;
  25832. }
  25833. text_size.y = (float)total_lines * row_height;
  25834. /* handle case when cursor is at end of text buffer */
  25835. if (!cursor_ptr && edit->cursor == edit->string.len) {
  25836. cursor_pos.x = line_width;
  25837. cursor_pos.y = text_size.y - row_height;
  25838. }
  25839. }
  25840. {
  25841. /* scrollbar */
  25842. if (cursor_follow)
  25843. {
  25844. /* update scrollbar to follow cursor */
  25845. if (!(flags & NK_EDIT_NO_HORIZONTAL_SCROLL)) {
  25846. /* horizontal scroll */
  25847. const float scroll_increment = area.w * 0.25f;
  25848. if (cursor_pos.x < edit->scrollbar.x)
  25849. edit->scrollbar.x = (float)(int)NK_MAX(0.0f, cursor_pos.x - scroll_increment);
  25850. if (cursor_pos.x >= edit->scrollbar.x + area.w)
  25851. edit->scrollbar.x = (float)(int)NK_MAX(0.0f, cursor_pos.x - area.w + scroll_increment);
  25852. } else edit->scrollbar.x = 0;
  25853. if (flags & NK_EDIT_MULTILINE) {
  25854. /* vertical scroll: like horizontal, it only adjusts if the
  25855. * cursor leaves the visible area, and then only just enough
  25856. * to keep it visible */
  25857. if (cursor_pos.y < edit->scrollbar.y)
  25858. edit->scrollbar.y = NK_MAX(0.0f, cursor_pos.y);
  25859. if (cursor_pos.y > edit->scrollbar.y + area.h - row_height)
  25860. edit->scrollbar.y = edit->scrollbar.y + row_height;
  25861. } else edit->scrollbar.y = 0;
  25862. }
  25863. /* scrollbar widget */
  25864. if (flags & NK_EDIT_MULTILINE)
  25865. {
  25866. nk_flags ws;
  25867. struct nk_rect scroll;
  25868. float scroll_target;
  25869. float scroll_offset;
  25870. float scroll_step;
  25871. float scroll_inc;
  25872. scroll = area;
  25873. scroll.x = (bounds.x + bounds.w - style->border) - style->scrollbar_size.x;
  25874. scroll.w = style->scrollbar_size.x;
  25875. scroll_offset = edit->scrollbar.y;
  25876. scroll_step = scroll.h * 0.10f;
  25877. scroll_inc = scroll.h * 0.01f;
  25878. scroll_target = text_size.y;
  25879. edit->scrollbar.y = nk_do_scrollbarv(&ws, out, scroll, is_hovered,
  25880. scroll_offset, scroll_target, scroll_step, scroll_inc,
  25881. &style->scrollbar, in, font);
  25882. /* Eat mouse scroll if we're active */
  25883. if (is_hovered && in->mouse.scroll_delta.y) {
  25884. in->mouse.scroll_delta.y = 0;
  25885. }
  25886. }
  25887. }
  25888. /* draw text */
  25889. {struct nk_color background_color;
  25890. struct nk_color text_color;
  25891. struct nk_color sel_background_color;
  25892. struct nk_color sel_text_color;
  25893. struct nk_color cursor_color;
  25894. struct nk_color cursor_text_color;
  25895. const struct nk_style_item *background;
  25896. nk_push_scissor(out, clip);
  25897. /* select correct colors to draw */
  25898. if (*state & NK_WIDGET_STATE_ACTIVED) {
  25899. background = &style->active;
  25900. text_color = style->text_active;
  25901. sel_text_color = style->selected_text_hover;
  25902. sel_background_color = style->selected_hover;
  25903. cursor_color = style->cursor_hover;
  25904. cursor_text_color = style->cursor_text_hover;
  25905. } else if (*state & NK_WIDGET_STATE_HOVER) {
  25906. background = &style->hover;
  25907. text_color = style->text_hover;
  25908. sel_text_color = style->selected_text_hover;
  25909. sel_background_color = style->selected_hover;
  25910. cursor_text_color = style->cursor_text_hover;
  25911. cursor_color = style->cursor_hover;
  25912. } else {
  25913. background = &style->normal;
  25914. text_color = style->text_normal;
  25915. sel_text_color = style->selected_text_normal;
  25916. sel_background_color = style->selected_normal;
  25917. cursor_color = style->cursor_normal;
  25918. cursor_text_color = style->cursor_text_normal;
  25919. }
  25920. if (background->type == NK_STYLE_ITEM_IMAGE)
  25921. background_color = nk_rgba(0,0,0,0);
  25922. else
  25923. background_color = background->data.color;
  25924. cursor_color = nk_rgb_factor(cursor_color, style->color_factor);
  25925. cursor_text_color = nk_rgb_factor(cursor_text_color, style->color_factor);
  25926. if (edit->select_start == edit->select_end) {
  25927. /* no selection so just draw the complete text */
  25928. const char *begin = nk_str_get_const(&edit->string);
  25929. int l = nk_str_len_char(&edit->string);
  25930. nk_edit_draw_text(out, style, area.x - edit->scrollbar.x,
  25931. area.y - edit->scrollbar.y, 0, begin, l, row_height, font,
  25932. background_color, text_color, nk_false);
  25933. } else {
  25934. /* edit has selection so draw 1-3 text chunks */
  25935. if (edit->select_start != edit->select_end && selection_begin > 0){
  25936. /* draw unselected text before selection */
  25937. const char *begin = nk_str_get_const(&edit->string);
  25938. NK_ASSERT(select_begin_ptr);
  25939. nk_edit_draw_text(out, style, area.x - edit->scrollbar.x,
  25940. area.y - edit->scrollbar.y, 0, begin, (int)(select_begin_ptr - begin),
  25941. row_height, font, background_color, text_color, nk_false);
  25942. }
  25943. if (edit->select_start != edit->select_end) {
  25944. /* draw selected text */
  25945. NK_ASSERT(select_begin_ptr);
  25946. if (!select_end_ptr) {
  25947. const char *begin = nk_str_get_const(&edit->string);
  25948. select_end_ptr = begin + nk_str_len_char(&edit->string);
  25949. }
  25950. nk_edit_draw_text(out, style,
  25951. area.x - edit->scrollbar.x,
  25952. area.y + selection_offset_start.y - edit->scrollbar.y,
  25953. selection_offset_start.x,
  25954. select_begin_ptr, (int)(select_end_ptr - select_begin_ptr),
  25955. row_height, font, sel_background_color, sel_text_color, nk_true);
  25956. }
  25957. if ((edit->select_start != edit->select_end &&
  25958. selection_end < edit->string.len))
  25959. {
  25960. /* draw unselected text after selected text */
  25961. const char *begin = select_end_ptr;
  25962. const char *end = nk_str_get_const(&edit->string) +
  25963. nk_str_len_char(&edit->string);
  25964. NK_ASSERT(select_end_ptr);
  25965. nk_edit_draw_text(out, style,
  25966. area.x - edit->scrollbar.x,
  25967. area.y + selection_offset_end.y - edit->scrollbar.y,
  25968. selection_offset_end.x,
  25969. begin, (int)(end - begin), row_height, font,
  25970. background_color, text_color, nk_true);
  25971. }
  25972. }
  25973. /* cursor */
  25974. if (edit->select_start == edit->select_end)
  25975. {
  25976. if (edit->cursor >= nk_str_len(&edit->string) ||
  25977. (cursor_ptr && *cursor_ptr == '\n')) {
  25978. /* draw cursor at end of line */
  25979. struct nk_rect cursor;
  25980. cursor.w = style->cursor_size;
  25981. cursor.h = font->height;
  25982. cursor.x = area.x + cursor_pos.x - edit->scrollbar.x;
  25983. cursor.y = area.y + cursor_pos.y + row_height/2.0f - cursor.h/2.0f;
  25984. cursor.y -= edit->scrollbar.y;
  25985. nk_fill_rect(out, cursor, 0, cursor_color);
  25986. } else {
  25987. /* draw cursor inside text */
  25988. int glyph_len;
  25989. struct nk_rect label;
  25990. struct nk_text txt;
  25991. nk_rune unicode;
  25992. NK_ASSERT(cursor_ptr);
  25993. glyph_len = nk_utf_decode(cursor_ptr, &unicode, 4);
  25994. label.x = area.x + cursor_pos.x - edit->scrollbar.x;
  25995. label.y = area.y + cursor_pos.y - edit->scrollbar.y;
  25996. label.w = font->width(font->userdata, font->height, cursor_ptr, glyph_len);
  25997. label.h = row_height;
  25998. txt.padding = nk_vec2(0,0);
  25999. txt.background = cursor_color;;
  26000. txt.text = cursor_text_color;
  26001. nk_fill_rect(out, label, 0, cursor_color);
  26002. nk_widget_text(out, label, cursor_ptr, glyph_len, &txt, NK_TEXT_LEFT, font);
  26003. }
  26004. }}
  26005. } else {
  26006. /* not active so just draw text */
  26007. int l = nk_str_len_char(&edit->string);
  26008. const char *begin = nk_str_get_const(&edit->string);
  26009. const struct nk_style_item *background;
  26010. struct nk_color background_color;
  26011. struct nk_color text_color;
  26012. nk_push_scissor(out, clip);
  26013. if (*state & NK_WIDGET_STATE_ACTIVED) {
  26014. background = &style->active;
  26015. text_color = style->text_active;
  26016. } else if (*state & NK_WIDGET_STATE_HOVER) {
  26017. background = &style->hover;
  26018. text_color = style->text_hover;
  26019. } else {
  26020. background = &style->normal;
  26021. text_color = style->text_normal;
  26022. }
  26023. if (background->type == NK_STYLE_ITEM_IMAGE)
  26024. background_color = nk_rgba(0,0,0,0);
  26025. else
  26026. background_color = background->data.color;
  26027. background_color = nk_rgb_factor(background_color, style->color_factor);
  26028. text_color = nk_rgb_factor(text_color, style->color_factor);
  26029. nk_edit_draw_text(out, style, area.x - edit->scrollbar.x,
  26030. area.y - edit->scrollbar.y, 0, begin, l, row_height, font,
  26031. background_color, text_color, nk_false);
  26032. }
  26033. nk_push_scissor(out, old_clip);}
  26034. return ret;
  26035. }
  26036. NK_API void
  26037. nk_edit_focus(struct nk_context *ctx, nk_flags flags)
  26038. {
  26039. nk_hash hash;
  26040. struct nk_window *win;
  26041. NK_ASSERT(ctx);
  26042. NK_ASSERT(ctx->current);
  26043. if (!ctx || !ctx->current) return;
  26044. win = ctx->current;
  26045. hash = win->edit.seq;
  26046. win->edit.active = nk_true;
  26047. win->edit.name = hash;
  26048. if (flags & NK_EDIT_ALWAYS_INSERT_MODE)
  26049. win->edit.mode = NK_TEXT_EDIT_MODE_INSERT;
  26050. }
  26051. NK_API void
  26052. nk_edit_unfocus(struct nk_context *ctx)
  26053. {
  26054. struct nk_window *win;
  26055. NK_ASSERT(ctx);
  26056. NK_ASSERT(ctx->current);
  26057. if (!ctx || !ctx->current) return;
  26058. win = ctx->current;
  26059. win->edit.active = nk_false;
  26060. win->edit.name = 0;
  26061. }
  26062. NK_API nk_flags
  26063. nk_edit_string(struct nk_context *ctx, nk_flags flags,
  26064. char *memory, int *len, int max, nk_plugin_filter filter)
  26065. {
  26066. nk_hash hash;
  26067. nk_flags state;
  26068. struct nk_text_edit *edit;
  26069. struct nk_window *win;
  26070. NK_ASSERT(ctx);
  26071. NK_ASSERT(memory);
  26072. NK_ASSERT(len);
  26073. if (!ctx || !memory || !len)
  26074. return 0;
  26075. filter = (!filter) ? nk_filter_default: filter;
  26076. win = ctx->current;
  26077. hash = win->edit.seq;
  26078. edit = &ctx->text_edit;
  26079. nk_textedit_clear_state(&ctx->text_edit, (flags & NK_EDIT_MULTILINE)?
  26080. NK_TEXT_EDIT_MULTI_LINE: NK_TEXT_EDIT_SINGLE_LINE, filter);
  26081. if (win->edit.active && hash == win->edit.name) {
  26082. if (flags & NK_EDIT_NO_CURSOR)
  26083. edit->cursor = nk_utf_len(memory, *len);
  26084. else edit->cursor = win->edit.cursor;
  26085. if (!(flags & NK_EDIT_SELECTABLE)) {
  26086. edit->select_start = win->edit.cursor;
  26087. edit->select_end = win->edit.cursor;
  26088. } else {
  26089. edit->select_start = win->edit.sel_start;
  26090. edit->select_end = win->edit.sel_end;
  26091. }
  26092. edit->mode = win->edit.mode;
  26093. edit->scrollbar.x = (float)win->edit.scrollbar.x;
  26094. edit->scrollbar.y = (float)win->edit.scrollbar.y;
  26095. edit->active = nk_true;
  26096. } else edit->active = nk_false;
  26097. max = NK_MAX(1, max);
  26098. *len = NK_MIN(*len, max-1);
  26099. nk_str_init_fixed(&edit->string, memory, (nk_size)max);
  26100. edit->string.buffer.allocated = (nk_size)*len;
  26101. edit->string.len = nk_utf_len(memory, *len);
  26102. state = nk_edit_buffer(ctx, flags, edit, filter);
  26103. *len = (int)edit->string.buffer.allocated;
  26104. if (edit->active) {
  26105. win->edit.cursor = edit->cursor;
  26106. win->edit.sel_start = edit->select_start;
  26107. win->edit.sel_end = edit->select_end;
  26108. win->edit.mode = edit->mode;
  26109. win->edit.scrollbar.x = (nk_uint)edit->scrollbar.x;
  26110. win->edit.scrollbar.y = (nk_uint)edit->scrollbar.y;
  26111. } return state;
  26112. }
  26113. NK_API nk_flags
  26114. nk_edit_buffer(struct nk_context *ctx, nk_flags flags,
  26115. struct nk_text_edit *edit, nk_plugin_filter filter)
  26116. {
  26117. struct nk_window *win;
  26118. struct nk_style *style;
  26119. struct nk_input *in;
  26120. enum nk_widget_layout_states state;
  26121. struct nk_rect bounds;
  26122. nk_flags ret_flags = 0;
  26123. unsigned char prev_state;
  26124. nk_hash hash;
  26125. /* make sure correct values */
  26126. NK_ASSERT(ctx);
  26127. NK_ASSERT(edit);
  26128. NK_ASSERT(ctx->current);
  26129. NK_ASSERT(ctx->current->layout);
  26130. if (!ctx || !ctx->current || !ctx->current->layout)
  26131. return 0;
  26132. win = ctx->current;
  26133. style = &ctx->style;
  26134. state = nk_widget(&bounds, ctx);
  26135. if (!state) return state;
  26136. else if (state == NK_WIDGET_DISABLED)
  26137. flags |= NK_EDIT_READ_ONLY;
  26138. in = (win->layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  26139. /* check if edit is currently hot item */
  26140. hash = win->edit.seq++;
  26141. if (win->edit.active && hash == win->edit.name) {
  26142. if (flags & NK_EDIT_NO_CURSOR)
  26143. edit->cursor = edit->string.len;
  26144. if (!(flags & NK_EDIT_SELECTABLE)) {
  26145. edit->select_start = edit->cursor;
  26146. edit->select_end = edit->cursor;
  26147. }
  26148. if (flags & NK_EDIT_CLIPBOARD)
  26149. edit->clip = ctx->clip;
  26150. edit->active = (unsigned char)win->edit.active;
  26151. } else edit->active = nk_false;
  26152. edit->mode = win->edit.mode;
  26153. filter = (!filter) ? nk_filter_default: filter;
  26154. prev_state = (unsigned char)edit->active;
  26155. in = (flags & NK_EDIT_READ_ONLY) ? 0: in;
  26156. ret_flags = nk_do_edit(&ctx->last_widget_state, &win->buffer, bounds, flags,
  26157. filter, edit, &style->edit, in, style->font);
  26158. if (ctx->last_widget_state & NK_WIDGET_STATE_HOVER)
  26159. ctx->style.cursor_active = ctx->style.cursors[NK_CURSOR_TEXT];
  26160. if (edit->active && prev_state != edit->active) {
  26161. /* current edit is now hot */
  26162. win->edit.active = nk_true;
  26163. win->edit.name = hash;
  26164. } else if (prev_state && !edit->active) {
  26165. /* current edit is now cold */
  26166. win->edit.active = nk_false;
  26167. } return ret_flags;
  26168. }
  26169. NK_API nk_flags
  26170. nk_edit_string_zero_terminated(struct nk_context *ctx, nk_flags flags,
  26171. char *buffer, int max, nk_plugin_filter filter)
  26172. {
  26173. nk_flags result;
  26174. int len = nk_strlen(buffer);
  26175. result = nk_edit_string(ctx, flags, buffer, &len, max, filter);
  26176. buffer[NK_MIN(NK_MAX(max-1,0), len)] = '\0';
  26177. return result;
  26178. }
  26179. /* ===============================================================
  26180. *
  26181. * PROPERTY
  26182. *
  26183. * ===============================================================*/
  26184. NK_LIB void
  26185. nk_drag_behavior(nk_flags *state, const struct nk_input *in,
  26186. struct nk_rect drag, struct nk_property_variant *variant,
  26187. float inc_per_pixel)
  26188. {
  26189. int left_mouse_down = in && in->mouse.buttons[NK_BUTTON_LEFT].down;
  26190. int left_mouse_click_in_cursor = in &&
  26191. nk_input_has_mouse_click_down_in_rect(in, NK_BUTTON_LEFT, drag, nk_true);
  26192. nk_widget_state_reset(state);
  26193. if (nk_input_is_mouse_hovering_rect(in, drag))
  26194. *state = NK_WIDGET_STATE_HOVERED;
  26195. if (left_mouse_down && left_mouse_click_in_cursor) {
  26196. float delta, pixels;
  26197. pixels = in->mouse.delta.x;
  26198. delta = pixels * inc_per_pixel;
  26199. switch (variant->kind) {
  26200. default: break;
  26201. case NK_PROPERTY_INT:
  26202. variant->value.i = variant->value.i + (int)delta;
  26203. variant->value.i = NK_CLAMP(variant->min_value.i, variant->value.i, variant->max_value.i);
  26204. break;
  26205. case NK_PROPERTY_FLOAT:
  26206. variant->value.f = variant->value.f + (float)delta;
  26207. variant->value.f = NK_CLAMP(variant->min_value.f, variant->value.f, variant->max_value.f);
  26208. break;
  26209. case NK_PROPERTY_DOUBLE:
  26210. variant->value.d = variant->value.d + (double)delta;
  26211. variant->value.d = NK_CLAMP(variant->min_value.d, variant->value.d, variant->max_value.d);
  26212. break;
  26213. }
  26214. *state = NK_WIDGET_STATE_ACTIVE;
  26215. }
  26216. if (*state & NK_WIDGET_STATE_HOVER && !nk_input_is_mouse_prev_hovering_rect(in, drag))
  26217. *state |= NK_WIDGET_STATE_ENTERED;
  26218. else if (nk_input_is_mouse_prev_hovering_rect(in, drag))
  26219. *state |= NK_WIDGET_STATE_LEFT;
  26220. }
  26221. NK_LIB void
  26222. nk_property_behavior(nk_flags *ws, const struct nk_input *in,
  26223. struct nk_rect property, struct nk_rect label, struct nk_rect edit,
  26224. struct nk_rect empty, int *state, struct nk_property_variant *variant,
  26225. float inc_per_pixel)
  26226. {
  26227. nk_widget_state_reset(ws);
  26228. if (in && *state == NK_PROPERTY_DEFAULT) {
  26229. if (nk_button_behavior(ws, edit, in, NK_BUTTON_DEFAULT))
  26230. *state = NK_PROPERTY_EDIT;
  26231. else if (nk_input_is_mouse_click_down_in_rect(in, NK_BUTTON_LEFT, label, nk_true))
  26232. *state = NK_PROPERTY_DRAG;
  26233. else if (nk_input_is_mouse_click_down_in_rect(in, NK_BUTTON_LEFT, empty, nk_true))
  26234. *state = NK_PROPERTY_DRAG;
  26235. }
  26236. if (*state == NK_PROPERTY_DRAG) {
  26237. nk_drag_behavior(ws, in, property, variant, inc_per_pixel);
  26238. if (!(*ws & NK_WIDGET_STATE_ACTIVED)) *state = NK_PROPERTY_DEFAULT;
  26239. }
  26240. }
  26241. NK_LIB void
  26242. nk_draw_property(struct nk_command_buffer *out, const struct nk_style_property *style,
  26243. const struct nk_rect *bounds, const struct nk_rect *label, nk_flags state,
  26244. const char *name, int len, const struct nk_user_font *font)
  26245. {
  26246. struct nk_text text;
  26247. const struct nk_style_item *background;
  26248. /* select correct background and text color */
  26249. if (state & NK_WIDGET_STATE_ACTIVED) {
  26250. background = &style->active;
  26251. text.text = style->label_active;
  26252. } else if (state & NK_WIDGET_STATE_HOVER) {
  26253. background = &style->hover;
  26254. text.text = style->label_hover;
  26255. } else {
  26256. background = &style->normal;
  26257. text.text = style->label_normal;
  26258. }
  26259. text.text = nk_rgb_factor(text.text, style->color_factor);
  26260. /* draw background */
  26261. switch(background->type) {
  26262. case NK_STYLE_ITEM_IMAGE:
  26263. text.background = nk_rgba(0, 0, 0, 0);
  26264. nk_draw_image(out, *bounds, &background->data.image, nk_rgb_factor(nk_white, style->color_factor));
  26265. break;
  26266. case NK_STYLE_ITEM_NINE_SLICE:
  26267. text.background = nk_rgba(0, 0, 0, 0);
  26268. nk_draw_nine_slice(out, *bounds, &background->data.slice, nk_rgb_factor(nk_white, style->color_factor));
  26269. break;
  26270. case NK_STYLE_ITEM_COLOR:
  26271. text.background = background->data.color;
  26272. nk_fill_rect(out, *bounds, style->rounding, nk_rgb_factor(background->data.color, style->color_factor));
  26273. nk_stroke_rect(out, *bounds, style->rounding, style->border, nk_rgb_factor(style->border_color, style->color_factor));
  26274. break;
  26275. }
  26276. /* draw label */
  26277. text.padding = nk_vec2(0,0);
  26278. if (name && name[0] != '#') {
  26279. nk_widget_text(out, *label, name, len, &text, NK_TEXT_CENTERED, font);
  26280. }
  26281. }
  26282. NK_LIB void
  26283. nk_do_property(nk_flags *ws,
  26284. struct nk_command_buffer *out, struct nk_rect property,
  26285. const char *name, struct nk_property_variant *variant,
  26286. float inc_per_pixel, char *buffer, int *len,
  26287. int *state, int *cursor, int *select_begin, int *select_end,
  26288. const struct nk_style_property *style,
  26289. enum nk_property_filter filter, struct nk_input *in,
  26290. const struct nk_user_font *font, struct nk_text_edit *text_edit,
  26291. enum nk_button_behavior behavior)
  26292. {
  26293. const nk_plugin_filter filters[] = {
  26294. nk_filter_decimal,
  26295. nk_filter_float
  26296. };
  26297. nk_bool active, old;
  26298. int num_len = 0, name_len = 0;
  26299. char string[NK_MAX_NUMBER_BUFFER];
  26300. float size;
  26301. char *dst = 0;
  26302. int *length;
  26303. struct nk_rect left;
  26304. struct nk_rect right;
  26305. struct nk_rect label;
  26306. struct nk_rect edit;
  26307. struct nk_rect empty;
  26308. /* left decrement button */
  26309. left.h = font->height/2;
  26310. left.w = left.h;
  26311. left.x = property.x + style->border + style->padding.x;
  26312. left.y = property.y + style->border + property.h/2.0f - left.h/2;
  26313. /* text label */
  26314. if (name && name[0] != '#') {
  26315. name_len = nk_strlen(name);
  26316. }
  26317. size = font->width(font->userdata, font->height, name, name_len);
  26318. label.x = left.x + left.w + style->padding.x;
  26319. label.w = (float)size + 2 * style->padding.x;
  26320. label.y = property.y + style->border + style->padding.y;
  26321. label.h = property.h - (2 * style->border + 2 * style->padding.y);
  26322. /* right increment button */
  26323. right.y = left.y;
  26324. right.w = left.w;
  26325. right.h = left.h;
  26326. right.x = property.x + property.w - (right.w + style->padding.x);
  26327. /* edit */
  26328. if (*state == NK_PROPERTY_EDIT) {
  26329. size = font->width(font->userdata, font->height, buffer, *len);
  26330. size += style->edit.cursor_size;
  26331. length = len;
  26332. dst = buffer;
  26333. } else {
  26334. switch (variant->kind) {
  26335. default: break;
  26336. case NK_PROPERTY_INT:
  26337. nk_itoa(string, variant->value.i);
  26338. num_len = nk_strlen(string);
  26339. break;
  26340. case NK_PROPERTY_FLOAT:
  26341. NK_DTOA(string, (double)variant->value.f);
  26342. num_len = nk_string_float_limit(string, NK_MAX_FLOAT_PRECISION);
  26343. break;
  26344. case NK_PROPERTY_DOUBLE:
  26345. NK_DTOA(string, variant->value.d);
  26346. num_len = nk_string_float_limit(string, NK_MAX_FLOAT_PRECISION);
  26347. break;
  26348. }
  26349. size = font->width(font->userdata, font->height, string, num_len);
  26350. dst = string;
  26351. length = &num_len;
  26352. }
  26353. edit.w = (float)size + 2 * style->padding.x;
  26354. edit.w = NK_MIN(edit.w, right.x - (label.x + label.w));
  26355. edit.x = right.x - (edit.w + style->padding.x);
  26356. edit.y = property.y + style->border;
  26357. edit.h = property.h - (2 * style->border);
  26358. /* empty left space activator */
  26359. empty.w = edit.x - (label.x + label.w);
  26360. empty.x = label.x + label.w;
  26361. empty.y = property.y;
  26362. empty.h = property.h;
  26363. /* update property */
  26364. old = (*state == NK_PROPERTY_EDIT);
  26365. nk_property_behavior(ws, in, property, label, edit, empty, state, variant, inc_per_pixel);
  26366. /* draw property */
  26367. if (style->draw_begin) style->draw_begin(out, style->userdata);
  26368. nk_draw_property(out, style, &property, &label, *ws, name, name_len, font);
  26369. if (style->draw_end) style->draw_end(out, style->userdata);
  26370. /* execute right button */
  26371. if (nk_do_button_symbol(ws, out, left, style->sym_left, behavior, &style->dec_button, in, font)) {
  26372. switch (variant->kind) {
  26373. default: break;
  26374. case NK_PROPERTY_INT:
  26375. variant->value.i = NK_CLAMP(variant->min_value.i, variant->value.i - variant->step.i, variant->max_value.i); break;
  26376. case NK_PROPERTY_FLOAT:
  26377. variant->value.f = NK_CLAMP(variant->min_value.f, variant->value.f - variant->step.f, variant->max_value.f); break;
  26378. case NK_PROPERTY_DOUBLE:
  26379. variant->value.d = NK_CLAMP(variant->min_value.d, variant->value.d - variant->step.d, variant->max_value.d); break;
  26380. }
  26381. }
  26382. /* execute left button */
  26383. if (nk_do_button_symbol(ws, out, right, style->sym_right, behavior, &style->inc_button, in, font)) {
  26384. switch (variant->kind) {
  26385. default: break;
  26386. case NK_PROPERTY_INT:
  26387. variant->value.i = NK_CLAMP(variant->min_value.i, variant->value.i + variant->step.i, variant->max_value.i); break;
  26388. case NK_PROPERTY_FLOAT:
  26389. variant->value.f = NK_CLAMP(variant->min_value.f, variant->value.f + variant->step.f, variant->max_value.f); break;
  26390. case NK_PROPERTY_DOUBLE:
  26391. variant->value.d = NK_CLAMP(variant->min_value.d, variant->value.d + variant->step.d, variant->max_value.d); break;
  26392. }
  26393. }
  26394. if (old != NK_PROPERTY_EDIT && (*state == NK_PROPERTY_EDIT)) {
  26395. /* property has been activated so setup buffer */
  26396. NK_MEMCPY(buffer, dst, (nk_size)*length);
  26397. *cursor = nk_utf_len(buffer, *length);
  26398. *len = *length;
  26399. length = len;
  26400. dst = buffer;
  26401. active = 0;
  26402. } else active = (*state == NK_PROPERTY_EDIT);
  26403. /* execute and run text edit field */
  26404. nk_textedit_clear_state(text_edit, NK_TEXT_EDIT_SINGLE_LINE, filters[filter]);
  26405. text_edit->active = (unsigned char)active;
  26406. text_edit->string.len = *length;
  26407. text_edit->cursor = NK_CLAMP(0, *cursor, *length);
  26408. text_edit->select_start = NK_CLAMP(0,*select_begin, *length);
  26409. text_edit->select_end = NK_CLAMP(0,*select_end, *length);
  26410. text_edit->string.buffer.allocated = (nk_size)*length;
  26411. text_edit->string.buffer.memory.size = NK_MAX_NUMBER_BUFFER;
  26412. text_edit->string.buffer.memory.ptr = dst;
  26413. text_edit->string.buffer.size = NK_MAX_NUMBER_BUFFER;
  26414. text_edit->mode = NK_TEXT_EDIT_MODE_INSERT;
  26415. nk_do_edit(ws, out, edit, (int)NK_EDIT_FIELD|(int)NK_EDIT_AUTO_SELECT,
  26416. filters[filter], text_edit, &style->edit, (*state == NK_PROPERTY_EDIT) ? in: 0, font);
  26417. *length = text_edit->string.len;
  26418. *cursor = text_edit->cursor;
  26419. *select_begin = text_edit->select_start;
  26420. *select_end = text_edit->select_end;
  26421. if (text_edit->active && nk_input_is_key_pressed(in, NK_KEY_ENTER))
  26422. text_edit->active = nk_false;
  26423. if (active && !text_edit->active) {
  26424. /* property is now not active so convert edit text to value*/
  26425. *state = NK_PROPERTY_DEFAULT;
  26426. buffer[*len] = '\0';
  26427. switch (variant->kind) {
  26428. default: break;
  26429. case NK_PROPERTY_INT:
  26430. variant->value.i = nk_strtoi(buffer, 0);
  26431. variant->value.i = NK_CLAMP(variant->min_value.i, variant->value.i, variant->max_value.i);
  26432. break;
  26433. case NK_PROPERTY_FLOAT:
  26434. nk_string_float_limit(buffer, NK_MAX_FLOAT_PRECISION);
  26435. variant->value.f = nk_strtof(buffer, 0);
  26436. variant->value.f = NK_CLAMP(variant->min_value.f, variant->value.f, variant->max_value.f);
  26437. break;
  26438. case NK_PROPERTY_DOUBLE:
  26439. nk_string_float_limit(buffer, NK_MAX_FLOAT_PRECISION);
  26440. variant->value.d = nk_strtod(buffer, 0);
  26441. variant->value.d = NK_CLAMP(variant->min_value.d, variant->value.d, variant->max_value.d);
  26442. break;
  26443. }
  26444. }
  26445. }
  26446. NK_LIB struct nk_property_variant
  26447. nk_property_variant_int(int value, int min_value, int max_value, int step)
  26448. {
  26449. struct nk_property_variant result;
  26450. result.kind = NK_PROPERTY_INT;
  26451. result.value.i = value;
  26452. result.min_value.i = min_value;
  26453. result.max_value.i = max_value;
  26454. result.step.i = step;
  26455. return result;
  26456. }
  26457. NK_LIB struct nk_property_variant
  26458. nk_property_variant_float(float value, float min_value, float max_value, float step)
  26459. {
  26460. struct nk_property_variant result;
  26461. result.kind = NK_PROPERTY_FLOAT;
  26462. result.value.f = value;
  26463. result.min_value.f = min_value;
  26464. result.max_value.f = max_value;
  26465. result.step.f = step;
  26466. return result;
  26467. }
  26468. NK_LIB struct nk_property_variant
  26469. nk_property_variant_double(double value, double min_value, double max_value,
  26470. double step)
  26471. {
  26472. struct nk_property_variant result;
  26473. result.kind = NK_PROPERTY_DOUBLE;
  26474. result.value.d = value;
  26475. result.min_value.d = min_value;
  26476. result.max_value.d = max_value;
  26477. result.step.d = step;
  26478. return result;
  26479. }
  26480. NK_LIB void
  26481. nk_property(struct nk_context *ctx, const char *name, struct nk_property_variant *variant,
  26482. float inc_per_pixel, const enum nk_property_filter filter)
  26483. {
  26484. struct nk_window *win;
  26485. struct nk_panel *layout;
  26486. struct nk_input *in;
  26487. const struct nk_style *style;
  26488. struct nk_rect bounds;
  26489. enum nk_widget_layout_states s;
  26490. int *state = 0;
  26491. nk_hash hash = 0;
  26492. char *buffer = 0;
  26493. int *len = 0;
  26494. int *cursor = 0;
  26495. int *select_begin = 0;
  26496. int *select_end = 0;
  26497. int old_state;
  26498. char dummy_buffer[NK_MAX_NUMBER_BUFFER];
  26499. int dummy_state = NK_PROPERTY_DEFAULT;
  26500. int dummy_length = 0;
  26501. int dummy_cursor = 0;
  26502. int dummy_select_begin = 0;
  26503. int dummy_select_end = 0;
  26504. NK_ASSERT(ctx);
  26505. NK_ASSERT(ctx->current);
  26506. NK_ASSERT(ctx->current->layout);
  26507. if (!ctx || !ctx->current || !ctx->current->layout)
  26508. return;
  26509. win = ctx->current;
  26510. layout = win->layout;
  26511. style = &ctx->style;
  26512. s = nk_widget(&bounds, ctx);
  26513. if (!s) return;
  26514. /* calculate hash from name */
  26515. if (name[0] == '#') {
  26516. hash = nk_murmur_hash(name, (int)nk_strlen(name), win->property.seq++);
  26517. name++; /* special number hash */
  26518. } else hash = nk_murmur_hash(name, (int)nk_strlen(name), 42);
  26519. /* check if property is currently hot item */
  26520. if (win->property.active && hash == win->property.name) {
  26521. buffer = win->property.buffer;
  26522. len = &win->property.length;
  26523. cursor = &win->property.cursor;
  26524. state = &win->property.state;
  26525. select_begin = &win->property.select_start;
  26526. select_end = &win->property.select_end;
  26527. } else {
  26528. buffer = dummy_buffer;
  26529. len = &dummy_length;
  26530. cursor = &dummy_cursor;
  26531. state = &dummy_state;
  26532. select_begin = &dummy_select_begin;
  26533. select_end = &dummy_select_end;
  26534. }
  26535. /* execute property widget */
  26536. old_state = *state;
  26537. ctx->text_edit.clip = ctx->clip;
  26538. in = ((s == NK_WIDGET_ROM && !win->property.active) ||
  26539. layout->flags & NK_WINDOW_ROM || s == NK_WIDGET_DISABLED) ? 0 : &ctx->input;
  26540. nk_do_property(&ctx->last_widget_state, &win->buffer, bounds, name,
  26541. variant, inc_per_pixel, buffer, len, state, cursor, select_begin,
  26542. select_end, &style->property, filter, in, style->font, &ctx->text_edit,
  26543. ctx->button_behavior);
  26544. if (in && *state != NK_PROPERTY_DEFAULT && !win->property.active) {
  26545. /* current property is now hot */
  26546. win->property.active = 1;
  26547. NK_MEMCPY(win->property.buffer, buffer, (nk_size)*len);
  26548. win->property.length = *len;
  26549. win->property.cursor = *cursor;
  26550. win->property.state = *state;
  26551. win->property.name = hash;
  26552. win->property.select_start = *select_begin;
  26553. win->property.select_end = *select_end;
  26554. if (*state == NK_PROPERTY_DRAG) {
  26555. ctx->input.mouse.grab = nk_true;
  26556. ctx->input.mouse.grabbed = nk_true;
  26557. }
  26558. }
  26559. /* check if previously active property is now inactive */
  26560. if (*state == NK_PROPERTY_DEFAULT && old_state != NK_PROPERTY_DEFAULT) {
  26561. if (old_state == NK_PROPERTY_DRAG) {
  26562. ctx->input.mouse.grab = nk_false;
  26563. ctx->input.mouse.grabbed = nk_false;
  26564. ctx->input.mouse.ungrab = nk_true;
  26565. }
  26566. win->property.select_start = 0;
  26567. win->property.select_end = 0;
  26568. win->property.active = 0;
  26569. }
  26570. }
  26571. NK_API void
  26572. nk_property_int(struct nk_context *ctx, const char *name,
  26573. int min, int *val, int max, int step, float inc_per_pixel)
  26574. {
  26575. struct nk_property_variant variant;
  26576. NK_ASSERT(ctx);
  26577. NK_ASSERT(name);
  26578. NK_ASSERT(val);
  26579. if (!ctx || !ctx->current || !name || !val) return;
  26580. variant = nk_property_variant_int(*val, min, max, step);
  26581. nk_property(ctx, name, &variant, inc_per_pixel, NK_FILTER_INT);
  26582. *val = variant.value.i;
  26583. }
  26584. NK_API void
  26585. nk_property_float(struct nk_context *ctx, const char *name,
  26586. float min, float *val, float max, float step, float inc_per_pixel)
  26587. {
  26588. struct nk_property_variant variant;
  26589. NK_ASSERT(ctx);
  26590. NK_ASSERT(name);
  26591. NK_ASSERT(val);
  26592. if (!ctx || !ctx->current || !name || !val) return;
  26593. variant = nk_property_variant_float(*val, min, max, step);
  26594. nk_property(ctx, name, &variant, inc_per_pixel, NK_FILTER_FLOAT);
  26595. *val = variant.value.f;
  26596. }
  26597. NK_API void
  26598. nk_property_double(struct nk_context *ctx, const char *name,
  26599. double min, double *val, double max, double step, float inc_per_pixel)
  26600. {
  26601. struct nk_property_variant variant;
  26602. NK_ASSERT(ctx);
  26603. NK_ASSERT(name);
  26604. NK_ASSERT(val);
  26605. if (!ctx || !ctx->current || !name || !val) return;
  26606. variant = nk_property_variant_double(*val, min, max, step);
  26607. nk_property(ctx, name, &variant, inc_per_pixel, NK_FILTER_FLOAT);
  26608. *val = variant.value.d;
  26609. }
  26610. NK_API int
  26611. nk_propertyi(struct nk_context *ctx, const char *name, int min, int val,
  26612. int max, int step, float inc_per_pixel)
  26613. {
  26614. struct nk_property_variant variant;
  26615. NK_ASSERT(ctx);
  26616. NK_ASSERT(name);
  26617. if (!ctx || !ctx->current || !name) return val;
  26618. variant = nk_property_variant_int(val, min, max, step);
  26619. nk_property(ctx, name, &variant, inc_per_pixel, NK_FILTER_INT);
  26620. val = variant.value.i;
  26621. return val;
  26622. }
  26623. NK_API float
  26624. nk_propertyf(struct nk_context *ctx, const char *name, float min,
  26625. float val, float max, float step, float inc_per_pixel)
  26626. {
  26627. struct nk_property_variant variant;
  26628. NK_ASSERT(ctx);
  26629. NK_ASSERT(name);
  26630. if (!ctx || !ctx->current || !name) return val;
  26631. variant = nk_property_variant_float(val, min, max, step);
  26632. nk_property(ctx, name, &variant, inc_per_pixel, NK_FILTER_FLOAT);
  26633. val = variant.value.f;
  26634. return val;
  26635. }
  26636. NK_API double
  26637. nk_propertyd(struct nk_context *ctx, const char *name, double min,
  26638. double val, double max, double step, float inc_per_pixel)
  26639. {
  26640. struct nk_property_variant variant;
  26641. NK_ASSERT(ctx);
  26642. NK_ASSERT(name);
  26643. if (!ctx || !ctx->current || !name) return val;
  26644. variant = nk_property_variant_double(val, min, max, step);
  26645. nk_property(ctx, name, &variant, inc_per_pixel, NK_FILTER_FLOAT);
  26646. val = variant.value.d;
  26647. return val;
  26648. }
  26649. /* ==============================================================
  26650. *
  26651. * CHART
  26652. *
  26653. * ===============================================================*/
  26654. NK_API nk_bool
  26655. nk_chart_begin_colored(struct nk_context *ctx, enum nk_chart_type type,
  26656. struct nk_color color, struct nk_color highlight,
  26657. int count, float min_value, float max_value)
  26658. {
  26659. struct nk_window *win;
  26660. struct nk_chart *chart;
  26661. const struct nk_style *config;
  26662. const struct nk_style_chart *style;
  26663. const struct nk_style_item *background;
  26664. struct nk_rect bounds = {0, 0, 0, 0};
  26665. NK_ASSERT(ctx);
  26666. NK_ASSERT(ctx->current);
  26667. NK_ASSERT(ctx->current->layout);
  26668. if (!ctx || !ctx->current || !ctx->current->layout) return 0;
  26669. if (!nk_widget(&bounds, ctx)) {
  26670. chart = &ctx->current->layout->chart;
  26671. nk_zero(chart, sizeof(*chart));
  26672. return 0;
  26673. }
  26674. win = ctx->current;
  26675. config = &ctx->style;
  26676. chart = &win->layout->chart;
  26677. style = &config->chart;
  26678. /* setup basic generic chart */
  26679. nk_zero(chart, sizeof(*chart));
  26680. chart->x = bounds.x + style->padding.x;
  26681. chart->y = bounds.y + style->padding.y;
  26682. chart->w = bounds.w - 2 * style->padding.x;
  26683. chart->h = bounds.h - 2 * style->padding.y;
  26684. chart->w = NK_MAX(chart->w, 2 * style->padding.x);
  26685. chart->h = NK_MAX(chart->h, 2 * style->padding.y);
  26686. /* add first slot into chart */
  26687. {struct nk_chart_slot *slot = &chart->slots[chart->slot++];
  26688. slot->type = type;
  26689. slot->count = count;
  26690. slot->color = nk_rgb_factor(color, style->color_factor);
  26691. slot->highlight = highlight;
  26692. slot->min = NK_MIN(min_value, max_value);
  26693. slot->max = NK_MAX(min_value, max_value);
  26694. slot->range = slot->max - slot->min;
  26695. slot->show_markers = style->show_markers;}
  26696. /* draw chart background */
  26697. background = &style->background;
  26698. switch(background->type) {
  26699. case NK_STYLE_ITEM_IMAGE:
  26700. nk_draw_image(&win->buffer, bounds, &background->data.image, nk_rgb_factor(nk_white, style->color_factor));
  26701. break;
  26702. case NK_STYLE_ITEM_NINE_SLICE:
  26703. nk_draw_nine_slice(&win->buffer, bounds, &background->data.slice, nk_rgb_factor(nk_white, style->color_factor));
  26704. break;
  26705. case NK_STYLE_ITEM_COLOR:
  26706. nk_fill_rect(&win->buffer, bounds, style->rounding, nk_rgb_factor(style->border_color, style->color_factor));
  26707. nk_fill_rect(&win->buffer, nk_shrink_rect(bounds, style->border),
  26708. style->rounding, nk_rgb_factor(style->background.data.color, style->color_factor));
  26709. break;
  26710. }
  26711. return 1;
  26712. }
  26713. NK_API nk_bool
  26714. nk_chart_begin(struct nk_context *ctx, const enum nk_chart_type type,
  26715. int count, float min_value, float max_value)
  26716. {
  26717. return nk_chart_begin_colored(ctx, type, ctx->style.chart.color,
  26718. ctx->style.chart.selected_color, count, min_value, max_value);
  26719. }
  26720. NK_API void
  26721. nk_chart_add_slot_colored(struct nk_context *ctx, const enum nk_chart_type type,
  26722. struct nk_color color, struct nk_color highlight,
  26723. int count, float min_value, float max_value)
  26724. {
  26725. const struct nk_style_chart* style;
  26726. NK_ASSERT(ctx);
  26727. NK_ASSERT(ctx->current);
  26728. NK_ASSERT(ctx->current->layout);
  26729. NK_ASSERT(ctx->current->layout->chart.slot < NK_CHART_MAX_SLOT);
  26730. if (!ctx || !ctx->current || !ctx->current->layout) return;
  26731. if (ctx->current->layout->chart.slot >= NK_CHART_MAX_SLOT) return;
  26732. style = &ctx->style.chart;
  26733. /* add another slot into the graph */
  26734. {struct nk_chart *chart = &ctx->current->layout->chart;
  26735. struct nk_chart_slot *slot = &chart->slots[chart->slot++];
  26736. slot->type = type;
  26737. slot->count = count;
  26738. slot->color = nk_rgb_factor(color, style->color_factor);
  26739. slot->highlight = highlight;
  26740. slot->min = NK_MIN(min_value, max_value);
  26741. slot->max = NK_MAX(min_value, max_value);
  26742. slot->range = slot->max - slot->min;
  26743. slot->show_markers = style->show_markers;}
  26744. }
  26745. NK_API void
  26746. nk_chart_add_slot(struct nk_context *ctx, const enum nk_chart_type type,
  26747. int count, float min_value, float max_value)
  26748. {
  26749. nk_chart_add_slot_colored(ctx, type, ctx->style.chart.color,
  26750. ctx->style.chart.selected_color, count, min_value, max_value);
  26751. }
  26752. NK_INTERN nk_flags
  26753. nk_chart_push_line(struct nk_context *ctx, struct nk_window *win,
  26754. struct nk_chart *g, float value, int slot)
  26755. {
  26756. struct nk_panel *layout = win->layout;
  26757. const struct nk_input *i = ctx->current->widgets_disabled ? 0 : &ctx->input;
  26758. struct nk_command_buffer *out = &win->buffer;
  26759. nk_flags ret = 0;
  26760. struct nk_vec2 cur;
  26761. struct nk_rect bounds;
  26762. struct nk_color color;
  26763. float step;
  26764. float range;
  26765. float ratio;
  26766. NK_ASSERT(slot >= 0 && slot < NK_CHART_MAX_SLOT);
  26767. step = g->w / (float)g->slots[slot].count;
  26768. range = g->slots[slot].max - g->slots[slot].min;
  26769. ratio = (value - g->slots[slot].min) / range;
  26770. if (g->slots[slot].index == 0) {
  26771. /* first data point does not have a connection */
  26772. g->slots[slot].last.x = g->x;
  26773. g->slots[slot].last.y = (g->y + g->h) - ratio * (float)g->h;
  26774. bounds.x = g->slots[slot].last.x - 2;
  26775. bounds.y = g->slots[slot].last.y - 2;
  26776. bounds.w = bounds.h = 4;
  26777. color = g->slots[slot].color;
  26778. if (!(layout->flags & NK_WINDOW_ROM) && i &&
  26779. NK_INBOX(i->mouse.pos.x,i->mouse.pos.y, g->slots[slot].last.x-3, g->slots[slot].last.y-3, 6, 6)){
  26780. ret = nk_input_is_mouse_hovering_rect(i, bounds) ? NK_CHART_HOVERING : 0;
  26781. ret |= (i->mouse.buttons[NK_BUTTON_LEFT].down &&
  26782. i->mouse.buttons[NK_BUTTON_LEFT].clicked) ? NK_CHART_CLICKED: 0;
  26783. color = g->slots[slot].highlight;
  26784. }
  26785. if (g->slots[slot].show_markers) {
  26786. nk_fill_rect(out, bounds, 0, color);
  26787. }
  26788. g->slots[slot].index += 1;
  26789. return ret;
  26790. }
  26791. /* draw a line between the last data point and the new one */
  26792. color = g->slots[slot].color;
  26793. cur.x = g->x + (float)(step * (float)g->slots[slot].index);
  26794. cur.y = (g->y + g->h) - (ratio * (float)g->h);
  26795. nk_stroke_line(out, g->slots[slot].last.x, g->slots[slot].last.y, cur.x, cur.y, 1.0f, color);
  26796. bounds.x = cur.x - 3;
  26797. bounds.y = cur.y - 3;
  26798. bounds.w = bounds.h = 6;
  26799. /* user selection of current data point */
  26800. if (!(layout->flags & NK_WINDOW_ROM)) {
  26801. if (nk_input_is_mouse_hovering_rect(i, bounds)) {
  26802. ret = NK_CHART_HOVERING;
  26803. ret |= (!i->mouse.buttons[NK_BUTTON_LEFT].down &&
  26804. i->mouse.buttons[NK_BUTTON_LEFT].clicked) ? NK_CHART_CLICKED: 0;
  26805. color = g->slots[slot].highlight;
  26806. }
  26807. }
  26808. if (g->slots[slot].show_markers) {
  26809. nk_fill_rect(out, nk_rect(cur.x - 2, cur.y - 2, 4, 4), 0, color);
  26810. }
  26811. /* save current data point position */
  26812. g->slots[slot].last.x = cur.x;
  26813. g->slots[slot].last.y = cur.y;
  26814. g->slots[slot].index += 1;
  26815. return ret;
  26816. }
  26817. NK_INTERN nk_flags
  26818. nk_chart_push_column(const struct nk_context *ctx, struct nk_window *win,
  26819. struct nk_chart *chart, float value, int slot)
  26820. {
  26821. struct nk_command_buffer *out = &win->buffer;
  26822. const struct nk_input *in = ctx->current->widgets_disabled ? 0 : &ctx->input;
  26823. struct nk_panel *layout = win->layout;
  26824. float ratio;
  26825. nk_flags ret = 0;
  26826. struct nk_color color;
  26827. struct nk_rect item = {0,0,0,0};
  26828. NK_ASSERT(slot >= 0 && slot < NK_CHART_MAX_SLOT);
  26829. if (chart->slots[slot].index >= chart->slots[slot].count)
  26830. return nk_false;
  26831. if (chart->slots[slot].count) {
  26832. float padding = (float)(chart->slots[slot].count-1);
  26833. item.w = (chart->w - padding) / (float)(chart->slots[slot].count);
  26834. }
  26835. /* calculate bounds of current bar chart entry */
  26836. color = chart->slots[slot].color;;
  26837. item.h = chart->h * NK_ABS((value/chart->slots[slot].range));
  26838. if (value >= 0) {
  26839. ratio = (value + NK_ABS(chart->slots[slot].min)) / NK_ABS(chart->slots[slot].range);
  26840. item.y = (chart->y + chart->h) - chart->h * ratio;
  26841. } else {
  26842. ratio = (value - chart->slots[slot].max) / chart->slots[slot].range;
  26843. item.y = chart->y + (chart->h * NK_ABS(ratio)) - item.h;
  26844. }
  26845. item.x = chart->x + ((float)chart->slots[slot].index * item.w);
  26846. item.x = item.x + ((float)chart->slots[slot].index);
  26847. /* user chart bar selection */
  26848. if (!(layout->flags & NK_WINDOW_ROM) && in &&
  26849. NK_INBOX(in->mouse.pos.x,in->mouse.pos.y,item.x,item.y,item.w,item.h)) {
  26850. ret = NK_CHART_HOVERING;
  26851. ret |= (!in->mouse.buttons[NK_BUTTON_LEFT].down &&
  26852. in->mouse.buttons[NK_BUTTON_LEFT].clicked) ? NK_CHART_CLICKED: 0;
  26853. color = chart->slots[slot].highlight;
  26854. }
  26855. nk_fill_rect(out, item, 0, color);
  26856. chart->slots[slot].index += 1;
  26857. return ret;
  26858. }
  26859. NK_API nk_flags
  26860. nk_chart_push_slot(struct nk_context *ctx, float value, int slot)
  26861. {
  26862. nk_flags flags;
  26863. struct nk_window *win;
  26864. NK_ASSERT(ctx);
  26865. NK_ASSERT(ctx->current);
  26866. NK_ASSERT(slot >= 0 && slot < NK_CHART_MAX_SLOT);
  26867. NK_ASSERT(slot < ctx->current->layout->chart.slot);
  26868. if (!ctx || !ctx->current || slot >= NK_CHART_MAX_SLOT) return nk_false;
  26869. if (slot >= ctx->current->layout->chart.slot) return nk_false;
  26870. win = ctx->current;
  26871. if (win->layout->chart.slot < slot) return nk_false;
  26872. switch (win->layout->chart.slots[slot].type) {
  26873. case NK_CHART_LINES:
  26874. flags = nk_chart_push_line(ctx, win, &win->layout->chart, value, slot); break;
  26875. case NK_CHART_COLUMN:
  26876. flags = nk_chart_push_column(ctx, win, &win->layout->chart, value, slot); break;
  26877. default:
  26878. case NK_CHART_MAX:
  26879. flags = 0;
  26880. }
  26881. return flags;
  26882. }
  26883. NK_API nk_flags
  26884. nk_chart_push(struct nk_context *ctx, float value)
  26885. {
  26886. return nk_chart_push_slot(ctx, value, 0);
  26887. }
  26888. NK_API void
  26889. nk_chart_end(struct nk_context *ctx)
  26890. {
  26891. struct nk_window *win;
  26892. struct nk_chart *chart;
  26893. NK_ASSERT(ctx);
  26894. NK_ASSERT(ctx->current);
  26895. if (!ctx || !ctx->current)
  26896. return;
  26897. win = ctx->current;
  26898. chart = &win->layout->chart;
  26899. NK_MEMSET(chart, 0, sizeof(*chart));
  26900. return;
  26901. }
  26902. NK_API void
  26903. nk_plot(struct nk_context *ctx, enum nk_chart_type type, const float *values,
  26904. int count, int offset)
  26905. {
  26906. int i = 0;
  26907. float min_value;
  26908. float max_value;
  26909. NK_ASSERT(ctx);
  26910. NK_ASSERT(values);
  26911. if (!ctx || !values || !count) return;
  26912. min_value = values[offset];
  26913. max_value = values[offset];
  26914. for (i = 0; i < count; ++i) {
  26915. min_value = NK_MIN(values[i + offset], min_value);
  26916. max_value = NK_MAX(values[i + offset], max_value);
  26917. }
  26918. if (nk_chart_begin(ctx, type, count, min_value, max_value)) {
  26919. for (i = 0; i < count; ++i)
  26920. nk_chart_push(ctx, values[i + offset]);
  26921. nk_chart_end(ctx);
  26922. }
  26923. }
  26924. NK_API void
  26925. nk_plot_function(struct nk_context *ctx, enum nk_chart_type type, void *userdata,
  26926. float(*value_getter)(void* user, int index), int count, int offset)
  26927. {
  26928. int i = 0;
  26929. float min_value;
  26930. float max_value;
  26931. NK_ASSERT(ctx);
  26932. NK_ASSERT(value_getter);
  26933. if (!ctx || !value_getter || !count) return;
  26934. max_value = min_value = value_getter(userdata, offset);
  26935. for (i = 0; i < count; ++i) {
  26936. float value = value_getter(userdata, i + offset);
  26937. min_value = NK_MIN(value, min_value);
  26938. max_value = NK_MAX(value, max_value);
  26939. }
  26940. if (nk_chart_begin(ctx, type, count, min_value, max_value)) {
  26941. for (i = 0; i < count; ++i)
  26942. nk_chart_push(ctx, value_getter(userdata, i + offset));
  26943. nk_chart_end(ctx);
  26944. }
  26945. }
  26946. /* ==============================================================
  26947. *
  26948. * COLOR PICKER
  26949. *
  26950. * ===============================================================*/
  26951. NK_LIB nk_bool
  26952. nk_color_picker_behavior(nk_flags *state,
  26953. const struct nk_rect *bounds, const struct nk_rect *matrix,
  26954. const struct nk_rect *hue_bar, const struct nk_rect *alpha_bar,
  26955. struct nk_colorf *color, const struct nk_input *in)
  26956. {
  26957. float hsva[4];
  26958. nk_bool value_changed = 0;
  26959. nk_bool hsv_changed = 0;
  26960. NK_ASSERT(state);
  26961. NK_ASSERT(matrix);
  26962. NK_ASSERT(hue_bar);
  26963. NK_ASSERT(color);
  26964. /* color matrix */
  26965. nk_colorf_hsva_fv(hsva, *color);
  26966. if (nk_button_behavior(state, *matrix, in, NK_BUTTON_REPEATER)) {
  26967. hsva[1] = NK_SATURATE((in->mouse.pos.x - matrix->x) / (matrix->w-1));
  26968. hsva[2] = 1.0f - NK_SATURATE((in->mouse.pos.y - matrix->y) / (matrix->h-1));
  26969. value_changed = hsv_changed = 1;
  26970. }
  26971. /* hue bar */
  26972. if (nk_button_behavior(state, *hue_bar, in, NK_BUTTON_REPEATER)) {
  26973. hsva[0] = NK_SATURATE((in->mouse.pos.y - hue_bar->y) / (hue_bar->h-1));
  26974. value_changed = hsv_changed = 1;
  26975. }
  26976. /* alpha bar */
  26977. if (alpha_bar) {
  26978. if (nk_button_behavior(state, *alpha_bar, in, NK_BUTTON_REPEATER)) {
  26979. hsva[3] = 1.0f - NK_SATURATE((in->mouse.pos.y - alpha_bar->y) / (alpha_bar->h-1));
  26980. value_changed = 1;
  26981. }
  26982. }
  26983. nk_widget_state_reset(state);
  26984. if (hsv_changed) {
  26985. *color = nk_hsva_colorfv(hsva);
  26986. *state = NK_WIDGET_STATE_ACTIVE;
  26987. }
  26988. if (value_changed) {
  26989. color->a = hsva[3];
  26990. *state = NK_WIDGET_STATE_ACTIVE;
  26991. }
  26992. /* set color picker widget state */
  26993. if (nk_input_is_mouse_hovering_rect(in, *bounds))
  26994. *state = NK_WIDGET_STATE_HOVERED;
  26995. if (*state & NK_WIDGET_STATE_HOVER && !nk_input_is_mouse_prev_hovering_rect(in, *bounds))
  26996. *state |= NK_WIDGET_STATE_ENTERED;
  26997. else if (nk_input_is_mouse_prev_hovering_rect(in, *bounds))
  26998. *state |= NK_WIDGET_STATE_LEFT;
  26999. return value_changed;
  27000. }
  27001. NK_LIB void
  27002. nk_draw_color_picker(struct nk_command_buffer *o, const struct nk_rect *matrix,
  27003. const struct nk_rect *hue_bar, const struct nk_rect *alpha_bar,
  27004. struct nk_colorf col)
  27005. {
  27006. NK_STORAGE const struct nk_color black = {0,0,0,255};
  27007. NK_STORAGE const struct nk_color white = {255, 255, 255, 255};
  27008. NK_STORAGE const struct nk_color black_trans = {0,0,0,0};
  27009. const float crosshair_size = 7.0f;
  27010. struct nk_color temp;
  27011. float hsva[4];
  27012. float line_y;
  27013. int i;
  27014. NK_ASSERT(o);
  27015. NK_ASSERT(matrix);
  27016. NK_ASSERT(hue_bar);
  27017. /* draw hue bar */
  27018. nk_colorf_hsva_fv(hsva, col);
  27019. for (i = 0; i < 6; ++i) {
  27020. NK_GLOBAL const struct nk_color hue_colors[] = {
  27021. {255, 0, 0, 255}, {255,255,0,255}, {0,255,0,255}, {0, 255,255,255},
  27022. {0,0,255,255}, {255, 0, 255, 255}, {255, 0, 0, 255}
  27023. };
  27024. nk_fill_rect_multi_color(o,
  27025. nk_rect(hue_bar->x, hue_bar->y + (float)i * (hue_bar->h/6.0f) + 0.5f,
  27026. hue_bar->w, (hue_bar->h/6.0f) + 0.5f), hue_colors[i], hue_colors[i],
  27027. hue_colors[i+1], hue_colors[i+1]);
  27028. }
  27029. line_y = (float)(int)(hue_bar->y + hsva[0] * matrix->h + 0.5f);
  27030. nk_stroke_line(o, hue_bar->x-1, line_y, hue_bar->x + hue_bar->w + 2,
  27031. line_y, 1, nk_rgb(255,255,255));
  27032. /* draw alpha bar */
  27033. if (alpha_bar) {
  27034. float alpha = NK_SATURATE(col.a);
  27035. line_y = (float)(int)(alpha_bar->y + (1.0f - alpha) * matrix->h + 0.5f);
  27036. nk_fill_rect_multi_color(o, *alpha_bar, white, white, black, black);
  27037. nk_stroke_line(o, alpha_bar->x-1, line_y, alpha_bar->x + alpha_bar->w + 2,
  27038. line_y, 1, nk_rgb(255,255,255));
  27039. }
  27040. /* draw color matrix */
  27041. temp = nk_hsv_f(hsva[0], 1.0f, 1.0f);
  27042. nk_fill_rect_multi_color(o, *matrix, white, temp, temp, white);
  27043. nk_fill_rect_multi_color(o, *matrix, black_trans, black_trans, black, black);
  27044. /* draw cross-hair */
  27045. {struct nk_vec2 p; float S = hsva[1]; float V = hsva[2];
  27046. p.x = (float)(int)(matrix->x + S * matrix->w);
  27047. p.y = (float)(int)(matrix->y + (1.0f - V) * matrix->h);
  27048. nk_stroke_line(o, p.x - crosshair_size, p.y, p.x-2, p.y, 1.0f, white);
  27049. nk_stroke_line(o, p.x + crosshair_size + 1, p.y, p.x+3, p.y, 1.0f, white);
  27050. nk_stroke_line(o, p.x, p.y + crosshair_size + 1, p.x, p.y+3, 1.0f, white);
  27051. nk_stroke_line(o, p.x, p.y - crosshair_size, p.x, p.y-2, 1.0f, white);}
  27052. }
  27053. NK_LIB nk_bool
  27054. nk_do_color_picker(nk_flags *state,
  27055. struct nk_command_buffer *out, struct nk_colorf *col,
  27056. enum nk_color_format fmt, struct nk_rect bounds,
  27057. struct nk_vec2 padding, const struct nk_input *in,
  27058. const struct nk_user_font *font)
  27059. {
  27060. int ret = 0;
  27061. struct nk_rect matrix;
  27062. struct nk_rect hue_bar;
  27063. struct nk_rect alpha_bar;
  27064. float bar_w;
  27065. NK_ASSERT(out);
  27066. NK_ASSERT(col);
  27067. NK_ASSERT(state);
  27068. NK_ASSERT(font);
  27069. if (!out || !col || !state || !font)
  27070. return ret;
  27071. bar_w = font->height;
  27072. bounds.x += padding.x;
  27073. bounds.y += padding.x;
  27074. bounds.w -= 2 * padding.x;
  27075. bounds.h -= 2 * padding.y;
  27076. matrix.x = bounds.x;
  27077. matrix.y = bounds.y;
  27078. matrix.h = bounds.h;
  27079. matrix.w = bounds.w - (3 * padding.x + 2 * bar_w);
  27080. hue_bar.w = bar_w;
  27081. hue_bar.y = bounds.y;
  27082. hue_bar.h = matrix.h;
  27083. hue_bar.x = matrix.x + matrix.w + padding.x;
  27084. alpha_bar.x = hue_bar.x + hue_bar.w + padding.x;
  27085. alpha_bar.y = bounds.y;
  27086. alpha_bar.w = bar_w;
  27087. alpha_bar.h = matrix.h;
  27088. ret = nk_color_picker_behavior(state, &bounds, &matrix, &hue_bar,
  27089. (fmt == NK_RGBA) ? &alpha_bar:0, col, in);
  27090. nk_draw_color_picker(out, &matrix, &hue_bar, (fmt == NK_RGBA) ? &alpha_bar:0, *col);
  27091. return ret;
  27092. }
  27093. NK_API nk_bool
  27094. nk_color_pick(struct nk_context * ctx, struct nk_colorf *color,
  27095. enum nk_color_format fmt)
  27096. {
  27097. struct nk_window *win;
  27098. struct nk_panel *layout;
  27099. const struct nk_style *config;
  27100. const struct nk_input *in;
  27101. enum nk_widget_layout_states state;
  27102. struct nk_rect bounds;
  27103. NK_ASSERT(ctx);
  27104. NK_ASSERT(color);
  27105. NK_ASSERT(ctx->current);
  27106. NK_ASSERT(ctx->current->layout);
  27107. if (!ctx || !ctx->current || !ctx->current->layout || !color)
  27108. return 0;
  27109. win = ctx->current;
  27110. config = &ctx->style;
  27111. layout = win->layout;
  27112. state = nk_widget(&bounds, ctx);
  27113. if (!state) return 0;
  27114. in = (state == NK_WIDGET_ROM || state == NK_WIDGET_DISABLED || layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  27115. return nk_do_color_picker(&ctx->last_widget_state, &win->buffer, color, fmt, bounds,
  27116. nk_vec2(0,0), in, config->font);
  27117. }
  27118. NK_API struct nk_colorf
  27119. nk_color_picker(struct nk_context *ctx, struct nk_colorf color,
  27120. enum nk_color_format fmt)
  27121. {
  27122. nk_color_pick(ctx, &color, fmt);
  27123. return color;
  27124. }
  27125. /* ==============================================================
  27126. *
  27127. * COMBO
  27128. *
  27129. * ===============================================================*/
  27130. NK_INTERN nk_bool
  27131. nk_combo_begin(struct nk_context *ctx, struct nk_window *win,
  27132. struct nk_vec2 size, nk_bool is_clicked, struct nk_rect header)
  27133. {
  27134. struct nk_window *popup;
  27135. int is_open = 0;
  27136. int is_active = 0;
  27137. struct nk_rect body;
  27138. nk_hash hash;
  27139. NK_ASSERT(ctx);
  27140. NK_ASSERT(ctx->current);
  27141. NK_ASSERT(ctx->current->layout);
  27142. if (!ctx || !ctx->current || !ctx->current->layout)
  27143. return 0;
  27144. popup = win->popup.win;
  27145. body.x = header.x;
  27146. body.w = size.x;
  27147. body.y = header.y + header.h-ctx->style.window.combo_border;
  27148. body.h = size.y;
  27149. hash = win->popup.combo_count++;
  27150. is_open = (popup) ? nk_true:nk_false;
  27151. is_active = (popup && (win->popup.name == hash) && win->popup.type == NK_PANEL_COMBO);
  27152. if ((is_clicked && is_open && !is_active) || (is_open && !is_active) ||
  27153. (!is_open && !is_active && !is_clicked)) return 0;
  27154. if (!nk_nonblock_begin(ctx, 0, body,
  27155. (is_clicked && is_open)?nk_rect(0,0,0,0):header, NK_PANEL_COMBO)) return 0;
  27156. win->popup.type = NK_PANEL_COMBO;
  27157. win->popup.name = hash;
  27158. return 1;
  27159. }
  27160. NK_API nk_bool
  27161. nk_combo_begin_text(struct nk_context *ctx, const char *selected, int len,
  27162. struct nk_vec2 size)
  27163. {
  27164. const struct nk_input *in;
  27165. struct nk_window *win;
  27166. struct nk_style *style;
  27167. enum nk_widget_layout_states s;
  27168. int is_clicked = nk_false;
  27169. struct nk_rect header;
  27170. const struct nk_style_item *background;
  27171. struct nk_text text;
  27172. NK_ASSERT(ctx);
  27173. NK_ASSERT(selected);
  27174. NK_ASSERT(ctx->current);
  27175. NK_ASSERT(ctx->current->layout);
  27176. if (!ctx || !ctx->current || !ctx->current->layout || !selected)
  27177. return 0;
  27178. win = ctx->current;
  27179. style = &ctx->style;
  27180. s = nk_widget(&header, ctx);
  27181. if (s == NK_WIDGET_INVALID)
  27182. return 0;
  27183. in = (win->layout->flags & NK_WINDOW_ROM || s == NK_WIDGET_DISABLED || s == NK_WIDGET_ROM)? 0: &ctx->input;
  27184. if (nk_button_behavior(&ctx->last_widget_state, header, in, NK_BUTTON_DEFAULT))
  27185. is_clicked = nk_true;
  27186. /* draw combo box header background and border */
  27187. if (ctx->last_widget_state & NK_WIDGET_STATE_ACTIVED) {
  27188. background = &style->combo.active;
  27189. text.text = style->combo.label_active;
  27190. } else if (ctx->last_widget_state & NK_WIDGET_STATE_HOVER) {
  27191. background = &style->combo.hover;
  27192. text.text = style->combo.label_hover;
  27193. } else {
  27194. background = &style->combo.normal;
  27195. text.text = style->combo.label_normal;
  27196. }
  27197. text.text = nk_rgb_factor(text.text, style->combo.color_factor);
  27198. switch(background->type) {
  27199. case NK_STYLE_ITEM_IMAGE:
  27200. text.background = nk_rgba(0, 0, 0, 0);
  27201. nk_draw_image(&win->buffer, header, &background->data.image, nk_rgb_factor(nk_white, style->combo.color_factor));
  27202. break;
  27203. case NK_STYLE_ITEM_NINE_SLICE:
  27204. text.background = nk_rgba(0, 0, 0, 0);
  27205. nk_draw_nine_slice(&win->buffer, header, &background->data.slice, nk_rgb_factor(nk_white, style->combo.color_factor));
  27206. break;
  27207. case NK_STYLE_ITEM_COLOR:
  27208. text.background = background->data.color;
  27209. nk_fill_rect(&win->buffer, header, style->combo.rounding, nk_rgb_factor(background->data.color, style->combo.color_factor));
  27210. nk_stroke_rect(&win->buffer, header, style->combo.rounding, style->combo.border, nk_rgb_factor(style->combo.border_color, style->combo.color_factor));
  27211. break;
  27212. }
  27213. {
  27214. /* print currently selected text item */
  27215. struct nk_rect label;
  27216. struct nk_rect button;
  27217. struct nk_rect content;
  27218. int draw_button_symbol;
  27219. enum nk_symbol_type sym;
  27220. if (ctx->last_widget_state & NK_WIDGET_STATE_HOVER)
  27221. sym = style->combo.sym_hover;
  27222. else if (is_clicked)
  27223. sym = style->combo.sym_active;
  27224. else
  27225. sym = style->combo.sym_normal;
  27226. /* represents whether or not the combo's button symbol should be drawn */
  27227. draw_button_symbol = sym != NK_SYMBOL_NONE;
  27228. /* calculate button */
  27229. button.w = header.h - 2 * style->combo.button_padding.y;
  27230. button.x = (header.x + header.w - header.h) - style->combo.button_padding.x;
  27231. button.y = header.y + style->combo.button_padding.y;
  27232. button.h = button.w;
  27233. content.x = button.x + style->combo.button.padding.x;
  27234. content.y = button.y + style->combo.button.padding.y;
  27235. content.w = button.w - 2 * style->combo.button.padding.x;
  27236. content.h = button.h - 2 * style->combo.button.padding.y;
  27237. /* draw selected label */
  27238. text.padding = nk_vec2(0,0);
  27239. label.x = header.x + style->combo.content_padding.x;
  27240. label.y = header.y + style->combo.content_padding.y;
  27241. label.h = header.h - 2 * style->combo.content_padding.y;
  27242. if (draw_button_symbol)
  27243. label.w = button.x - (style->combo.content_padding.x + style->combo.spacing.x) - label.x;
  27244. else
  27245. label.w = header.w - 2 * style->combo.content_padding.x;
  27246. nk_widget_text(&win->buffer, label, selected, len, &text,
  27247. NK_TEXT_LEFT, ctx->style.font);
  27248. /* draw open/close button */
  27249. if (draw_button_symbol)
  27250. nk_draw_button_symbol(&win->buffer, &button, &content, ctx->last_widget_state,
  27251. &ctx->style.combo.button, sym, style->font);
  27252. }
  27253. return nk_combo_begin(ctx, win, size, is_clicked, header);
  27254. }
  27255. NK_API nk_bool
  27256. nk_combo_begin_label(struct nk_context *ctx, const char *selected, struct nk_vec2 size)
  27257. {
  27258. return nk_combo_begin_text(ctx, selected, nk_strlen(selected), size);
  27259. }
  27260. NK_API nk_bool
  27261. nk_combo_begin_color(struct nk_context *ctx, struct nk_color color, struct nk_vec2 size)
  27262. {
  27263. struct nk_window *win;
  27264. struct nk_style *style;
  27265. const struct nk_input *in;
  27266. struct nk_rect header;
  27267. int is_clicked = nk_false;
  27268. enum nk_widget_layout_states s;
  27269. const struct nk_style_item *background;
  27270. NK_ASSERT(ctx);
  27271. NK_ASSERT(ctx->current);
  27272. NK_ASSERT(ctx->current->layout);
  27273. if (!ctx || !ctx->current || !ctx->current->layout)
  27274. return 0;
  27275. win = ctx->current;
  27276. style = &ctx->style;
  27277. s = nk_widget(&header, ctx);
  27278. if (s == NK_WIDGET_INVALID)
  27279. return 0;
  27280. in = (win->layout->flags & NK_WINDOW_ROM || s == NK_WIDGET_DISABLED || s == NK_WIDGET_ROM)? 0: &ctx->input;
  27281. if (nk_button_behavior(&ctx->last_widget_state, header, in, NK_BUTTON_DEFAULT))
  27282. is_clicked = nk_true;
  27283. /* draw combo box header background and border */
  27284. if (ctx->last_widget_state & NK_WIDGET_STATE_ACTIVED)
  27285. background = &style->combo.active;
  27286. else if (ctx->last_widget_state & NK_WIDGET_STATE_HOVER)
  27287. background = &style->combo.hover;
  27288. else background = &style->combo.normal;
  27289. switch(background->type) {
  27290. case NK_STYLE_ITEM_IMAGE:
  27291. nk_draw_image(&win->buffer, header, &background->data.image, nk_rgb_factor(nk_white, style->combo.color_factor));
  27292. break;
  27293. case NK_STYLE_ITEM_NINE_SLICE:
  27294. nk_draw_nine_slice(&win->buffer, header, &background->data.slice, nk_rgb_factor(nk_white, style->combo.color_factor));
  27295. break;
  27296. case NK_STYLE_ITEM_COLOR:
  27297. nk_fill_rect(&win->buffer, header, style->combo.rounding, nk_rgb_factor(background->data.color, style->combo.color_factor));
  27298. nk_stroke_rect(&win->buffer, header, style->combo.rounding, style->combo.border, nk_rgb_factor(style->combo.border_color, style->combo.color_factor));
  27299. break;
  27300. }
  27301. {
  27302. struct nk_rect content;
  27303. struct nk_rect button;
  27304. struct nk_rect bounds;
  27305. int draw_button_symbol;
  27306. enum nk_symbol_type sym;
  27307. if (ctx->last_widget_state & NK_WIDGET_STATE_HOVER)
  27308. sym = style->combo.sym_hover;
  27309. else if (is_clicked)
  27310. sym = style->combo.sym_active;
  27311. else sym = style->combo.sym_normal;
  27312. /* represents whether or not the combo's button symbol should be drawn */
  27313. draw_button_symbol = sym != NK_SYMBOL_NONE;
  27314. /* calculate button */
  27315. button.w = header.h - 2 * style->combo.button_padding.y;
  27316. button.x = (header.x + header.w - header.h) - style->combo.button_padding.x;
  27317. button.y = header.y + style->combo.button_padding.y;
  27318. button.h = button.w;
  27319. content.x = button.x + style->combo.button.padding.x;
  27320. content.y = button.y + style->combo.button.padding.y;
  27321. content.w = button.w - 2 * style->combo.button.padding.x;
  27322. content.h = button.h - 2 * style->combo.button.padding.y;
  27323. /* draw color */
  27324. bounds.h = header.h - 4 * style->combo.content_padding.y;
  27325. bounds.y = header.y + 2 * style->combo.content_padding.y;
  27326. bounds.x = header.x + 2 * style->combo.content_padding.x;
  27327. if (draw_button_symbol)
  27328. bounds.w = (button.x - (style->combo.content_padding.x + style->combo.spacing.x)) - bounds.x;
  27329. else
  27330. bounds.w = header.w - 4 * style->combo.content_padding.x;
  27331. nk_fill_rect(&win->buffer, bounds, 0, nk_rgb_factor(color, style->combo.color_factor));
  27332. /* draw open/close button */
  27333. if (draw_button_symbol)
  27334. nk_draw_button_symbol(&win->buffer, &button, &content, ctx->last_widget_state,
  27335. &ctx->style.combo.button, sym, style->font);
  27336. }
  27337. return nk_combo_begin(ctx, win, size, is_clicked, header);
  27338. }
  27339. NK_API nk_bool
  27340. nk_combo_begin_symbol(struct nk_context *ctx, enum nk_symbol_type symbol, struct nk_vec2 size)
  27341. {
  27342. struct nk_window *win;
  27343. struct nk_style *style;
  27344. const struct nk_input *in;
  27345. struct nk_rect header;
  27346. int is_clicked = nk_false;
  27347. enum nk_widget_layout_states s;
  27348. const struct nk_style_item *background;
  27349. struct nk_color sym_background;
  27350. struct nk_color symbol_color;
  27351. NK_ASSERT(ctx);
  27352. NK_ASSERT(ctx->current);
  27353. NK_ASSERT(ctx->current->layout);
  27354. if (!ctx || !ctx->current || !ctx->current->layout)
  27355. return 0;
  27356. win = ctx->current;
  27357. style = &ctx->style;
  27358. s = nk_widget(&header, ctx);
  27359. if (s == NK_WIDGET_INVALID)
  27360. return 0;
  27361. in = (win->layout->flags & NK_WINDOW_ROM || s == NK_WIDGET_DISABLED || s == NK_WIDGET_ROM)? 0: &ctx->input;
  27362. if (nk_button_behavior(&ctx->last_widget_state, header, in, NK_BUTTON_DEFAULT))
  27363. is_clicked = nk_true;
  27364. /* draw combo box header background and border */
  27365. if (ctx->last_widget_state & NK_WIDGET_STATE_ACTIVED) {
  27366. background = &style->combo.active;
  27367. symbol_color = style->combo.symbol_active;
  27368. } else if (ctx->last_widget_state & NK_WIDGET_STATE_HOVER) {
  27369. background = &style->combo.hover;
  27370. symbol_color = style->combo.symbol_hover;
  27371. } else {
  27372. background = &style->combo.normal;
  27373. symbol_color = style->combo.symbol_hover;
  27374. }
  27375. symbol_color = nk_rgb_factor(symbol_color, style->combo.color_factor);
  27376. switch(background->type) {
  27377. case NK_STYLE_ITEM_IMAGE:
  27378. sym_background = nk_rgba(0, 0, 0, 0);
  27379. nk_draw_image(&win->buffer, header, &background->data.image, nk_rgb_factor(nk_white, style->combo.color_factor));
  27380. break;
  27381. case NK_STYLE_ITEM_NINE_SLICE:
  27382. sym_background = nk_rgba(0, 0, 0, 0);
  27383. nk_draw_nine_slice(&win->buffer, header, &background->data.slice, nk_rgb_factor(nk_white, style->combo.color_factor));
  27384. break;
  27385. case NK_STYLE_ITEM_COLOR:
  27386. sym_background = background->data.color;
  27387. nk_fill_rect(&win->buffer, header, style->combo.rounding, nk_rgb_factor(background->data.color, style->combo.color_factor));
  27388. nk_stroke_rect(&win->buffer, header, style->combo.rounding, style->combo.border, nk_rgb_factor(style->combo.border_color, style->combo.color_factor));
  27389. break;
  27390. }
  27391. {
  27392. struct nk_rect bounds = {0,0,0,0};
  27393. struct nk_rect content;
  27394. struct nk_rect button;
  27395. enum nk_symbol_type sym;
  27396. if (ctx->last_widget_state & NK_WIDGET_STATE_HOVER)
  27397. sym = style->combo.sym_hover;
  27398. else if (is_clicked)
  27399. sym = style->combo.sym_active;
  27400. else sym = style->combo.sym_normal;
  27401. /* calculate button */
  27402. button.w = header.h - 2 * style->combo.button_padding.y;
  27403. button.x = (header.x + header.w - header.h) - style->combo.button_padding.y;
  27404. button.y = header.y + style->combo.button_padding.y;
  27405. button.h = button.w;
  27406. content.x = button.x + style->combo.button.padding.x;
  27407. content.y = button.y + style->combo.button.padding.y;
  27408. content.w = button.w - 2 * style->combo.button.padding.x;
  27409. content.h = button.h - 2 * style->combo.button.padding.y;
  27410. /* draw symbol */
  27411. bounds.h = header.h - 2 * style->combo.content_padding.y;
  27412. bounds.y = header.y + style->combo.content_padding.y;
  27413. bounds.x = header.x + style->combo.content_padding.x;
  27414. bounds.w = (button.x - style->combo.content_padding.y) - bounds.x;
  27415. nk_draw_symbol(&win->buffer, symbol, bounds, sym_background, symbol_color,
  27416. 1.0f, style->font);
  27417. /* draw open/close button */
  27418. nk_draw_button_symbol(&win->buffer, &bounds, &content, ctx->last_widget_state,
  27419. &ctx->style.combo.button, sym, style->font);
  27420. }
  27421. return nk_combo_begin(ctx, win, size, is_clicked, header);
  27422. }
  27423. NK_API nk_bool
  27424. nk_combo_begin_symbol_text(struct nk_context *ctx, const char *selected, int len,
  27425. enum nk_symbol_type symbol, struct nk_vec2 size)
  27426. {
  27427. struct nk_window *win;
  27428. struct nk_style *style;
  27429. struct nk_input *in;
  27430. struct nk_rect header;
  27431. int is_clicked = nk_false;
  27432. enum nk_widget_layout_states s;
  27433. const struct nk_style_item *background;
  27434. struct nk_color symbol_color;
  27435. struct nk_text text;
  27436. NK_ASSERT(ctx);
  27437. NK_ASSERT(ctx->current);
  27438. NK_ASSERT(ctx->current->layout);
  27439. if (!ctx || !ctx->current || !ctx->current->layout)
  27440. return 0;
  27441. win = ctx->current;
  27442. style = &ctx->style;
  27443. s = nk_widget(&header, ctx);
  27444. if (!s) return 0;
  27445. in = (win->layout->flags & NK_WINDOW_ROM || s == NK_WIDGET_DISABLED || s == NK_WIDGET_ROM)? 0: &ctx->input;
  27446. if (nk_button_behavior(&ctx->last_widget_state, header, in, NK_BUTTON_DEFAULT))
  27447. is_clicked = nk_true;
  27448. /* draw combo box header background and border */
  27449. if (ctx->last_widget_state & NK_WIDGET_STATE_ACTIVED) {
  27450. background = &style->combo.active;
  27451. symbol_color = style->combo.symbol_active;
  27452. text.text = style->combo.label_active;
  27453. } else if (ctx->last_widget_state & NK_WIDGET_STATE_HOVER) {
  27454. background = &style->combo.hover;
  27455. symbol_color = style->combo.symbol_hover;
  27456. text.text = style->combo.label_hover;
  27457. } else {
  27458. background = &style->combo.normal;
  27459. symbol_color = style->combo.symbol_normal;
  27460. text.text = style->combo.label_normal;
  27461. }
  27462. text.text = nk_rgb_factor(text.text, style->combo.color_factor);
  27463. symbol_color = nk_rgb_factor(symbol_color, style->combo.color_factor);
  27464. switch(background->type) {
  27465. case NK_STYLE_ITEM_IMAGE:
  27466. text.background = nk_rgba(0, 0, 0, 0);
  27467. nk_draw_image(&win->buffer, header, &background->data.image, nk_rgb_factor(nk_white, style->combo.color_factor));
  27468. break;
  27469. case NK_STYLE_ITEM_NINE_SLICE:
  27470. text.background = nk_rgba(0, 0, 0, 0);
  27471. nk_draw_nine_slice(&win->buffer, header, &background->data.slice, nk_rgb_factor(nk_white, style->combo.color_factor));
  27472. break;
  27473. case NK_STYLE_ITEM_COLOR:
  27474. text.background = background->data.color;
  27475. nk_fill_rect(&win->buffer, header, style->combo.rounding, nk_rgb_factor(background->data.color, style->combo.color_factor));
  27476. nk_stroke_rect(&win->buffer, header, style->combo.rounding, style->combo.border, nk_rgb_factor(style->combo.border_color, style->combo.color_factor));
  27477. break;
  27478. }
  27479. {
  27480. struct nk_rect content;
  27481. struct nk_rect button;
  27482. struct nk_rect label;
  27483. struct nk_rect image;
  27484. enum nk_symbol_type sym;
  27485. if (ctx->last_widget_state & NK_WIDGET_STATE_HOVER)
  27486. sym = style->combo.sym_hover;
  27487. else if (is_clicked)
  27488. sym = style->combo.sym_active;
  27489. else sym = style->combo.sym_normal;
  27490. /* calculate button */
  27491. button.w = header.h - 2 * style->combo.button_padding.y;
  27492. button.x = (header.x + header.w - header.h) - style->combo.button_padding.x;
  27493. button.y = header.y + style->combo.button_padding.y;
  27494. button.h = button.w;
  27495. content.x = button.x + style->combo.button.padding.x;
  27496. content.y = button.y + style->combo.button.padding.y;
  27497. content.w = button.w - 2 * style->combo.button.padding.x;
  27498. content.h = button.h - 2 * style->combo.button.padding.y;
  27499. nk_draw_button_symbol(&win->buffer, &button, &content, ctx->last_widget_state,
  27500. &ctx->style.combo.button, sym, style->font);
  27501. /* draw symbol */
  27502. image.x = header.x + style->combo.content_padding.x;
  27503. image.y = header.y + style->combo.content_padding.y;
  27504. image.h = header.h - 2 * style->combo.content_padding.y;
  27505. image.w = image.h;
  27506. nk_draw_symbol(&win->buffer, symbol, image, text.background, symbol_color,
  27507. 1.0f, style->font);
  27508. /* draw label */
  27509. text.padding = nk_vec2(0,0);
  27510. label.x = image.x + image.w + style->combo.spacing.x + style->combo.content_padding.x;
  27511. label.y = header.y + style->combo.content_padding.y;
  27512. label.w = (button.x - style->combo.content_padding.x) - label.x;
  27513. label.h = header.h - 2 * style->combo.content_padding.y;
  27514. nk_widget_text(&win->buffer, label, selected, len, &text, NK_TEXT_LEFT, style->font);
  27515. }
  27516. return nk_combo_begin(ctx, win, size, is_clicked, header);
  27517. }
  27518. NK_API nk_bool
  27519. nk_combo_begin_image(struct nk_context *ctx, struct nk_image img, struct nk_vec2 size)
  27520. {
  27521. struct nk_window *win;
  27522. struct nk_style *style;
  27523. const struct nk_input *in;
  27524. struct nk_rect header;
  27525. int is_clicked = nk_false;
  27526. enum nk_widget_layout_states s;
  27527. const struct nk_style_item *background;
  27528. NK_ASSERT(ctx);
  27529. NK_ASSERT(ctx->current);
  27530. NK_ASSERT(ctx->current->layout);
  27531. if (!ctx || !ctx->current || !ctx->current->layout)
  27532. return 0;
  27533. win = ctx->current;
  27534. style = &ctx->style;
  27535. s = nk_widget(&header, ctx);
  27536. if (s == NK_WIDGET_INVALID)
  27537. return 0;
  27538. in = (win->layout->flags & NK_WINDOW_ROM || s == NK_WIDGET_DISABLED || s == NK_WIDGET_ROM)? 0: &ctx->input;
  27539. if (nk_button_behavior(&ctx->last_widget_state, header, in, NK_BUTTON_DEFAULT))
  27540. is_clicked = nk_true;
  27541. /* draw combo box header background and border */
  27542. if (ctx->last_widget_state & NK_WIDGET_STATE_ACTIVED)
  27543. background = &style->combo.active;
  27544. else if (ctx->last_widget_state & NK_WIDGET_STATE_HOVER)
  27545. background = &style->combo.hover;
  27546. else background = &style->combo.normal;
  27547. switch (background->type) {
  27548. case NK_STYLE_ITEM_IMAGE:
  27549. nk_draw_image(&win->buffer, header, &background->data.image, nk_rgb_factor(nk_white, style->combo.color_factor));
  27550. break;
  27551. case NK_STYLE_ITEM_NINE_SLICE:
  27552. nk_draw_nine_slice(&win->buffer, header, &background->data.slice, nk_rgb_factor(nk_white, style->combo.color_factor));
  27553. break;
  27554. case NK_STYLE_ITEM_COLOR:
  27555. nk_fill_rect(&win->buffer, header, style->combo.rounding, nk_rgb_factor(background->data.color, style->combo.color_factor));
  27556. nk_stroke_rect(&win->buffer, header, style->combo.rounding, style->combo.border, nk_rgb_factor(style->combo.border_color, style->combo.color_factor));
  27557. break;
  27558. }
  27559. {
  27560. struct nk_rect bounds = {0,0,0,0};
  27561. struct nk_rect content;
  27562. struct nk_rect button;
  27563. int draw_button_symbol;
  27564. enum nk_symbol_type sym;
  27565. if (ctx->last_widget_state & NK_WIDGET_STATE_HOVER)
  27566. sym = style->combo.sym_hover;
  27567. else if (is_clicked)
  27568. sym = style->combo.sym_active;
  27569. else sym = style->combo.sym_normal;
  27570. /* represents whether or not the combo's button symbol should be drawn */
  27571. draw_button_symbol = sym != NK_SYMBOL_NONE;
  27572. /* calculate button */
  27573. button.w = header.h - 2 * style->combo.button_padding.y;
  27574. button.x = (header.x + header.w - header.h) - style->combo.button_padding.y;
  27575. button.y = header.y + style->combo.button_padding.y;
  27576. button.h = button.w;
  27577. content.x = button.x + style->combo.button.padding.x;
  27578. content.y = button.y + style->combo.button.padding.y;
  27579. content.w = button.w - 2 * style->combo.button.padding.x;
  27580. content.h = button.h - 2 * style->combo.button.padding.y;
  27581. /* draw image */
  27582. bounds.h = header.h - 2 * style->combo.content_padding.y;
  27583. bounds.y = header.y + style->combo.content_padding.y;
  27584. bounds.x = header.x + style->combo.content_padding.x;
  27585. if (draw_button_symbol)
  27586. bounds.w = (button.x - style->combo.content_padding.y) - bounds.x;
  27587. else
  27588. bounds.w = header.w - 2 * style->combo.content_padding.x;
  27589. nk_draw_image(&win->buffer, bounds, &img, nk_rgb_factor(nk_white, style->combo.color_factor));
  27590. /* draw open/close button */
  27591. if (draw_button_symbol)
  27592. nk_draw_button_symbol(&win->buffer, &bounds, &content, ctx->last_widget_state,
  27593. &ctx->style.combo.button, sym, style->font);
  27594. }
  27595. return nk_combo_begin(ctx, win, size, is_clicked, header);
  27596. }
  27597. NK_API nk_bool
  27598. nk_combo_begin_image_text(struct nk_context *ctx, const char *selected, int len,
  27599. struct nk_image img, struct nk_vec2 size)
  27600. {
  27601. struct nk_window *win;
  27602. struct nk_style *style;
  27603. struct nk_input *in;
  27604. struct nk_rect header;
  27605. int is_clicked = nk_false;
  27606. enum nk_widget_layout_states s;
  27607. const struct nk_style_item *background;
  27608. struct nk_text text;
  27609. NK_ASSERT(ctx);
  27610. NK_ASSERT(ctx->current);
  27611. NK_ASSERT(ctx->current->layout);
  27612. if (!ctx || !ctx->current || !ctx->current->layout)
  27613. return 0;
  27614. win = ctx->current;
  27615. style = &ctx->style;
  27616. s = nk_widget(&header, ctx);
  27617. if (!s) return 0;
  27618. in = (win->layout->flags & NK_WINDOW_ROM || s == NK_WIDGET_DISABLED || s == NK_WIDGET_ROM)? 0: &ctx->input;
  27619. if (nk_button_behavior(&ctx->last_widget_state, header, in, NK_BUTTON_DEFAULT))
  27620. is_clicked = nk_true;
  27621. /* draw combo box header background and border */
  27622. if (ctx->last_widget_state & NK_WIDGET_STATE_ACTIVED) {
  27623. background = &style->combo.active;
  27624. text.text = style->combo.label_active;
  27625. } else if (ctx->last_widget_state & NK_WIDGET_STATE_HOVER) {
  27626. background = &style->combo.hover;
  27627. text.text = style->combo.label_hover;
  27628. } else {
  27629. background = &style->combo.normal;
  27630. text.text = style->combo.label_normal;
  27631. }
  27632. text.text = nk_rgb_factor(text.text, style->combo.color_factor);
  27633. switch(background->type) {
  27634. case NK_STYLE_ITEM_IMAGE:
  27635. text.background = nk_rgba(0, 0, 0, 0);
  27636. nk_draw_image(&win->buffer, header, &background->data.image, nk_rgb_factor(nk_white, style->combo.color_factor));
  27637. break;
  27638. case NK_STYLE_ITEM_NINE_SLICE:
  27639. text.background = nk_rgba(0, 0, 0, 0);
  27640. nk_draw_nine_slice(&win->buffer, header, &background->data.slice, nk_rgb_factor(nk_white, style->combo.color_factor));
  27641. break;
  27642. case NK_STYLE_ITEM_COLOR:
  27643. text.background = background->data.color;
  27644. nk_fill_rect(&win->buffer, header, style->combo.rounding, nk_rgb_factor(background->data.color, style->combo.color_factor));
  27645. nk_stroke_rect(&win->buffer, header, style->combo.rounding, style->combo.border, nk_rgb_factor(style->combo.border_color, style->combo.color_factor));
  27646. break;
  27647. }
  27648. {
  27649. struct nk_rect content;
  27650. struct nk_rect button;
  27651. struct nk_rect label;
  27652. struct nk_rect image;
  27653. int draw_button_symbol;
  27654. enum nk_symbol_type sym;
  27655. if (ctx->last_widget_state & NK_WIDGET_STATE_HOVER)
  27656. sym = style->combo.sym_hover;
  27657. else if (is_clicked)
  27658. sym = style->combo.sym_active;
  27659. else sym = style->combo.sym_normal;
  27660. /* represents whether or not the combo's button symbol should be drawn */
  27661. draw_button_symbol = sym != NK_SYMBOL_NONE;
  27662. /* calculate button */
  27663. button.w = header.h - 2 * style->combo.button_padding.y;
  27664. button.x = (header.x + header.w - header.h) - style->combo.button_padding.x;
  27665. button.y = header.y + style->combo.button_padding.y;
  27666. button.h = button.w;
  27667. content.x = button.x + style->combo.button.padding.x;
  27668. content.y = button.y + style->combo.button.padding.y;
  27669. content.w = button.w - 2 * style->combo.button.padding.x;
  27670. content.h = button.h - 2 * style->combo.button.padding.y;
  27671. if (draw_button_symbol)
  27672. nk_draw_button_symbol(&win->buffer, &button, &content, ctx->last_widget_state,
  27673. &ctx->style.combo.button, sym, style->font);
  27674. /* draw image */
  27675. image.x = header.x + style->combo.content_padding.x;
  27676. image.y = header.y + style->combo.content_padding.y;
  27677. image.h = header.h - 2 * style->combo.content_padding.y;
  27678. image.w = image.h;
  27679. nk_draw_image(&win->buffer, image, &img, nk_rgb_factor(nk_white, style->combo.color_factor));
  27680. /* draw label */
  27681. text.padding = nk_vec2(0,0);
  27682. label.x = image.x + image.w + style->combo.spacing.x + style->combo.content_padding.x;
  27683. label.y = header.y + style->combo.content_padding.y;
  27684. label.h = header.h - 2 * style->combo.content_padding.y;
  27685. if (draw_button_symbol)
  27686. label.w = (button.x - style->combo.content_padding.x) - label.x;
  27687. else
  27688. label.w = (header.x + header.w - style->combo.content_padding.x) - label.x;
  27689. nk_widget_text(&win->buffer, label, selected, len, &text, NK_TEXT_LEFT, style->font);
  27690. }
  27691. return nk_combo_begin(ctx, win, size, is_clicked, header);
  27692. }
  27693. NK_API nk_bool
  27694. nk_combo_begin_symbol_label(struct nk_context *ctx,
  27695. const char *selected, enum nk_symbol_type type, struct nk_vec2 size)
  27696. {
  27697. return nk_combo_begin_symbol_text(ctx, selected, nk_strlen(selected), type, size);
  27698. }
  27699. NK_API nk_bool
  27700. nk_combo_begin_image_label(struct nk_context *ctx,
  27701. const char *selected, struct nk_image img, struct nk_vec2 size)
  27702. {
  27703. return nk_combo_begin_image_text(ctx, selected, nk_strlen(selected), img, size);
  27704. }
  27705. NK_API nk_bool
  27706. nk_combo_item_text(struct nk_context *ctx, const char *text, int len,nk_flags align)
  27707. {
  27708. return nk_contextual_item_text(ctx, text, len, align);
  27709. }
  27710. NK_API nk_bool
  27711. nk_combo_item_label(struct nk_context *ctx, const char *label, nk_flags align)
  27712. {
  27713. return nk_contextual_item_label(ctx, label, align);
  27714. }
  27715. NK_API nk_bool
  27716. nk_combo_item_image_text(struct nk_context *ctx, struct nk_image img, const char *text,
  27717. int len, nk_flags alignment)
  27718. {
  27719. return nk_contextual_item_image_text(ctx, img, text, len, alignment);
  27720. }
  27721. NK_API nk_bool
  27722. nk_combo_item_image_label(struct nk_context *ctx, struct nk_image img,
  27723. const char *text, nk_flags alignment)
  27724. {
  27725. return nk_contextual_item_image_label(ctx, img, text, alignment);
  27726. }
  27727. NK_API nk_bool
  27728. nk_combo_item_symbol_text(struct nk_context *ctx, enum nk_symbol_type sym,
  27729. const char *text, int len, nk_flags alignment)
  27730. {
  27731. return nk_contextual_item_symbol_text(ctx, sym, text, len, alignment);
  27732. }
  27733. NK_API nk_bool
  27734. nk_combo_item_symbol_label(struct nk_context *ctx, enum nk_symbol_type sym,
  27735. const char *label, nk_flags alignment)
  27736. {
  27737. return nk_contextual_item_symbol_label(ctx, sym, label, alignment);
  27738. }
  27739. NK_API void nk_combo_end(struct nk_context *ctx)
  27740. {
  27741. nk_contextual_end(ctx);
  27742. }
  27743. NK_API void nk_combo_close(struct nk_context *ctx)
  27744. {
  27745. nk_contextual_close(ctx);
  27746. }
  27747. NK_API int
  27748. nk_combo(struct nk_context *ctx, const char *const *items, int count,
  27749. int selected, int item_height, struct nk_vec2 size)
  27750. {
  27751. int i = 0;
  27752. int max_height;
  27753. struct nk_vec2 item_spacing;
  27754. struct nk_vec2 window_padding;
  27755. NK_ASSERT(ctx);
  27756. NK_ASSERT(items);
  27757. NK_ASSERT(ctx->current);
  27758. if (!ctx || !items ||!count)
  27759. return selected;
  27760. item_spacing = ctx->style.window.spacing;
  27761. window_padding = nk_panel_get_padding(&ctx->style, ctx->current->layout->type);
  27762. max_height = count * item_height + count * (int)item_spacing.y;
  27763. max_height += (int)item_spacing.y * 2 + (int)window_padding.y * 2;
  27764. size.y = NK_MIN(size.y, (float)max_height);
  27765. if (nk_combo_begin_label(ctx, items[selected], size)) {
  27766. nk_layout_row_dynamic(ctx, (float)item_height, 1);
  27767. for (i = 0; i < count; ++i) {
  27768. if (nk_combo_item_label(ctx, items[i], NK_TEXT_LEFT))
  27769. selected = i;
  27770. }
  27771. nk_combo_end(ctx);
  27772. }
  27773. return selected;
  27774. }
  27775. NK_API int
  27776. nk_combo_separator(struct nk_context *ctx, const char *items_separated_by_separator,
  27777. int separator, int selected, int count, int item_height, struct nk_vec2 size)
  27778. {
  27779. int i;
  27780. int max_height;
  27781. struct nk_vec2 item_spacing;
  27782. struct nk_vec2 window_padding;
  27783. const char *current_item;
  27784. const char *iter;
  27785. int length = 0;
  27786. NK_ASSERT(ctx);
  27787. NK_ASSERT(items_separated_by_separator);
  27788. if (!ctx || !items_separated_by_separator)
  27789. return selected;
  27790. /* calculate popup window */
  27791. item_spacing = ctx->style.window.spacing;
  27792. window_padding = nk_panel_get_padding(&ctx->style, ctx->current->layout->type);
  27793. max_height = count * item_height + count * (int)item_spacing.y;
  27794. max_height += (int)item_spacing.y * 2 + (int)window_padding.y * 2;
  27795. size.y = NK_MIN(size.y, (float)max_height);
  27796. /* find selected item */
  27797. current_item = items_separated_by_separator;
  27798. for (i = 0; i < count; ++i) {
  27799. iter = current_item;
  27800. while (*iter && *iter != separator) iter++;
  27801. length = (int)(iter - current_item);
  27802. if (i == selected) break;
  27803. current_item = iter + 1;
  27804. }
  27805. if (nk_combo_begin_text(ctx, current_item, length, size)) {
  27806. current_item = items_separated_by_separator;
  27807. nk_layout_row_dynamic(ctx, (float)item_height, 1);
  27808. for (i = 0; i < count; ++i) {
  27809. iter = current_item;
  27810. while (*iter && *iter != separator) iter++;
  27811. length = (int)(iter - current_item);
  27812. if (nk_combo_item_text(ctx, current_item, length, NK_TEXT_LEFT))
  27813. selected = i;
  27814. current_item = current_item + length + 1;
  27815. }
  27816. nk_combo_end(ctx);
  27817. }
  27818. return selected;
  27819. }
  27820. NK_API int
  27821. nk_combo_string(struct nk_context *ctx, const char *items_separated_by_zeros,
  27822. int selected, int count, int item_height, struct nk_vec2 size)
  27823. {
  27824. return nk_combo_separator(ctx, items_separated_by_zeros, '\0', selected, count, item_height, size);
  27825. }
  27826. NK_API int
  27827. nk_combo_callback(struct nk_context *ctx, void(*item_getter)(void*, int, const char**),
  27828. void *userdata, int selected, int count, int item_height, struct nk_vec2 size)
  27829. {
  27830. int i;
  27831. int max_height;
  27832. struct nk_vec2 item_spacing;
  27833. struct nk_vec2 window_padding;
  27834. const char *item;
  27835. NK_ASSERT(ctx);
  27836. NK_ASSERT(item_getter);
  27837. if (!ctx || !item_getter)
  27838. return selected;
  27839. /* calculate popup window */
  27840. item_spacing = ctx->style.window.spacing;
  27841. window_padding = nk_panel_get_padding(&ctx->style, ctx->current->layout->type);
  27842. max_height = count * item_height + count * (int)item_spacing.y;
  27843. max_height += (int)item_spacing.y * 2 + (int)window_padding.y * 2;
  27844. size.y = NK_MIN(size.y, (float)max_height);
  27845. item_getter(userdata, selected, &item);
  27846. if (nk_combo_begin_label(ctx, item, size)) {
  27847. nk_layout_row_dynamic(ctx, (float)item_height, 1);
  27848. for (i = 0; i < count; ++i) {
  27849. item_getter(userdata, i, &item);
  27850. if (nk_combo_item_label(ctx, item, NK_TEXT_LEFT))
  27851. selected = i;
  27852. }
  27853. nk_combo_end(ctx);
  27854. } return selected;
  27855. }
  27856. NK_API void
  27857. nk_combobox(struct nk_context *ctx, const char *const *items, int count,
  27858. int *selected, int item_height, struct nk_vec2 size)
  27859. {
  27860. *selected = nk_combo(ctx, items, count, *selected, item_height, size);
  27861. }
  27862. NK_API void
  27863. nk_combobox_string(struct nk_context *ctx, const char *items_separated_by_zeros,
  27864. int *selected, int count, int item_height, struct nk_vec2 size)
  27865. {
  27866. *selected = nk_combo_string(ctx, items_separated_by_zeros, *selected, count, item_height, size);
  27867. }
  27868. NK_API void
  27869. nk_combobox_separator(struct nk_context *ctx, const char *items_separated_by_separator,
  27870. int separator, int *selected, int count, int item_height, struct nk_vec2 size)
  27871. {
  27872. *selected = nk_combo_separator(ctx, items_separated_by_separator, separator,
  27873. *selected, count, item_height, size);
  27874. }
  27875. NK_API void
  27876. nk_combobox_callback(struct nk_context *ctx,
  27877. void(*item_getter)(void* data, int id, const char **out_text),
  27878. void *userdata, int *selected, int count, int item_height, struct nk_vec2 size)
  27879. {
  27880. *selected = nk_combo_callback(ctx, item_getter, userdata, *selected, count, item_height, size);
  27881. }
  27882. /* ===============================================================
  27883. *
  27884. * TOOLTIP
  27885. *
  27886. * ===============================================================*/
  27887. NK_API nk_bool
  27888. nk_tooltip_begin(struct nk_context *ctx, float width)
  27889. {
  27890. int x,y,w,h;
  27891. struct nk_window *win;
  27892. const struct nk_input *in;
  27893. struct nk_rect bounds;
  27894. int ret;
  27895. NK_ASSERT(ctx);
  27896. NK_ASSERT(ctx->current);
  27897. NK_ASSERT(ctx->current->layout);
  27898. if (!ctx || !ctx->current || !ctx->current->layout)
  27899. return 0;
  27900. /* make sure that no nonblocking popup is currently active */
  27901. win = ctx->current;
  27902. in = &ctx->input;
  27903. if (win->popup.win && ((int)win->popup.type & (int)NK_PANEL_SET_NONBLOCK))
  27904. return 0;
  27905. w = nk_iceilf(width);
  27906. h = nk_iceilf(nk_null_rect.h);
  27907. x = nk_ifloorf(in->mouse.pos.x + 1) - (int)win->layout->clip.x;
  27908. y = nk_ifloorf(in->mouse.pos.y + 1) - (int)win->layout->clip.y;
  27909. bounds.x = (float)x;
  27910. bounds.y = (float)y;
  27911. bounds.w = (float)w;
  27912. bounds.h = (float)h;
  27913. ret = nk_popup_begin(ctx, NK_POPUP_DYNAMIC,
  27914. "__##Tooltip##__", NK_WINDOW_NO_SCROLLBAR|NK_WINDOW_BORDER, bounds);
  27915. if (ret) win->layout->flags &= ~(nk_flags)NK_WINDOW_ROM;
  27916. win->popup.type = NK_PANEL_TOOLTIP;
  27917. ctx->current->layout->type = NK_PANEL_TOOLTIP;
  27918. return ret;
  27919. }
  27920. NK_API void
  27921. nk_tooltip_end(struct nk_context *ctx)
  27922. {
  27923. NK_ASSERT(ctx);
  27924. NK_ASSERT(ctx->current);
  27925. if (!ctx || !ctx->current) return;
  27926. ctx->current->seq--;
  27927. nk_popup_close(ctx);
  27928. nk_popup_end(ctx);
  27929. }
  27930. NK_API void
  27931. nk_tooltip(struct nk_context *ctx, const char *text)
  27932. {
  27933. const struct nk_style *style;
  27934. struct nk_vec2 padding;
  27935. int text_len;
  27936. float text_width;
  27937. float text_height;
  27938. NK_ASSERT(ctx);
  27939. NK_ASSERT(ctx->current);
  27940. NK_ASSERT(ctx->current->layout);
  27941. NK_ASSERT(text);
  27942. if (!ctx || !ctx->current || !ctx->current->layout || !text)
  27943. return;
  27944. /* fetch configuration data */
  27945. style = &ctx->style;
  27946. padding = style->window.padding;
  27947. /* calculate size of the text and tooltip */
  27948. text_len = nk_strlen(text);
  27949. text_width = style->font->width(style->font->userdata,
  27950. style->font->height, text, text_len);
  27951. text_width += (4 * padding.x);
  27952. text_height = (style->font->height + 2 * padding.y);
  27953. /* execute tooltip and fill with text */
  27954. if (nk_tooltip_begin(ctx, (float)text_width)) {
  27955. nk_layout_row_dynamic(ctx, (float)text_height, 1);
  27956. nk_text(ctx, text, text_len, NK_TEXT_LEFT);
  27957. nk_tooltip_end(ctx);
  27958. }
  27959. }
  27960. #ifdef NK_INCLUDE_STANDARD_VARARGS
  27961. NK_API void
  27962. nk_tooltipf(struct nk_context *ctx, const char *fmt, ...)
  27963. {
  27964. va_list args;
  27965. va_start(args, fmt);
  27966. nk_tooltipfv(ctx, fmt, args);
  27967. va_end(args);
  27968. }
  27969. NK_API void
  27970. nk_tooltipfv(struct nk_context *ctx, const char *fmt, va_list args)
  27971. {
  27972. char buf[256];
  27973. nk_strfmt(buf, NK_LEN(buf), fmt, args);
  27974. nk_tooltip(ctx, buf);
  27975. }
  27976. #endif
  27977. #endif /* NK_IMPLEMENTATION */
  27978. /*
  27979. /// ## License
  27980. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~none
  27981. /// ------------------------------------------------------------------------------
  27982. /// This software is available under 2 licenses -- choose whichever you prefer.
  27983. /// ------------------------------------------------------------------------------
  27984. /// ALTERNATIVE A - MIT License
  27985. /// Copyright (c) 2016-2018 Micha Mettke
  27986. /// Permission is hereby granted, free of charge, to any person obtaining a copy of
  27987. /// this software and associated documentation files (the "Software"), to deal in
  27988. /// the Software without restriction, including without limitation the rights to
  27989. /// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
  27990. /// of the Software, and to permit persons to whom the Software is furnished to do
  27991. /// so, subject to the following conditions:
  27992. /// The above copyright notice and this permission notice shall be included in all
  27993. /// copies or substantial portions of the Software.
  27994. /// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  27995. /// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  27996. /// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  27997. /// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  27998. /// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  27999. /// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  28000. /// SOFTWARE.
  28001. /// ------------------------------------------------------------------------------
  28002. /// ALTERNATIVE B - Public Domain (www.unlicense.org)
  28003. /// This is free and unencumbered software released into the public domain.
  28004. /// Anyone is free to copy, modify, publish, use, compile, sell, or distribute this
  28005. /// software, either in source code form or as a compiled binary, for any purpose,
  28006. /// commercial or non-commercial, and by any means.
  28007. /// In jurisdictions that recognize copyright laws, the author or authors of this
  28008. /// software dedicate any and all copyright interest in the software to the public
  28009. /// domain. We make this dedication for the benefit of the public at large and to
  28010. /// the detriment of our heirs and successors. We intend this dedication to be an
  28011. /// overt act of relinquishment in perpetuity of all present and future rights to
  28012. /// this software under copyright law.
  28013. /// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  28014. /// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  28015. /// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  28016. /// AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  28017. /// ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28018. /// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28019. /// ------------------------------------------------------------------------------
  28020. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  28021. /// ## Changelog
  28022. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~none
  28023. /// [date] ([x.y.z]) - [description]
  28024. /// - [date]: date on which the change has been pushed
  28025. /// - [x.y.z]: Version string, represented in Semantic Versioning format
  28026. /// - [x]: Major version with API and library breaking changes
  28027. /// - [y]: Minor version with non-breaking API and library changes
  28028. /// - [z]: Patch version with no direct changes to the API
  28029. ///
  28030. /// - 2025/04/06 (4.12.7) - Fix text input navigation and mouse scrolling
  28031. /// - 2025/03/29 (4.12.6) - Fix unitialized data in nk_input_char
  28032. /// - 2025/03/05 (4.12.5) - Fix scrolling knob also scrolling parent window, remove dead code
  28033. /// - 2024/12/11 (4.12.4) - Fix array subscript [0, 0] is outside array bounds of ‘char[1]’
  28034. /// - 2024/12/11 (4.12.3) - Fix border color for property widgets
  28035. /// - 2024/11/20 (4.12.2) - Fix int/float type conversion warnings in `nk_roundf`
  28036. /// - 2024/03/07 (4.12.1) - Fix bitwise operations warnings in C++20
  28037. /// - 2023/11/26 (4.12.0) - Added an alignment option to checkboxes and radio buttons.
  28038. /// - 2023/10/11 (4.11.0) - Added nk_widget_disable_begin() and nk_widget_disable_end()
  28039. /// - 2022/12/23 (4.10.6) - Fix incorrect glyph index in nk_font_bake()
  28040. /// - 2022/12/17 (4.10.5) - Fix nk_font_bake_pack() using TTC font offset incorrectly
  28041. /// - 2022/10/24 (4.10.4) - Fix nk_str_{append,insert}_str_utf8 always returning 0
  28042. /// - 2022/09/03 (4.10.3) - Renamed the `null` texture variable to `tex_null`
  28043. /// - 2022/08/01 (4.10.2) - Fix Apple Silicon with incorrect NK_SITE_TYPE and NK_POINTER_TYPE
  28044. /// - 2022/08/01 (4.10.1) - Fix cursor jumping back to beginning of text when typing more than
  28045. /// nk_edit_xxx limit
  28046. /// - 2022/05/27 (4.10.0) - Add nk_input_has_mouse_click_in_button_rect() to fix window move bug
  28047. /// - 2022/04/19 (4.9.8) - Added nk_rule_horizontal() widget
  28048. /// - 2022/04/18 (4.9.7) - Change button behavior when NK_BUTTON_TRIGGER_ON_RELEASE is defined to
  28049. /// only trigger when the mouse position was inside the same button on down
  28050. /// - 2022/02/03 (4.9.6) - Allow overriding the NK_INV_SQRT function, similar to NK_SIN and NK_COS
  28051. /// - 2021/12/22 (4.9.5) - Revert layout bounds not accounting for padding due to regressions
  28052. /// - 2021/12/22 (4.9.4) - Fix checking hovering when window is minimized
  28053. /// - 2021/12/22 (4.09.3) - Fix layout bounds not accounting for padding
  28054. /// - 2021/12/19 (4.09.2) - Update to stb_rect_pack.h v1.01 and stb_truetype.h v1.26
  28055. /// - 2021/12/16 (4.09.1) - Fix the majority of GCC warnings
  28056. /// - 2021/10/16 (4.09.0) - Added nk_spacer() widget
  28057. /// - 2021/09/22 (4.08.6) - Fix "may be used uninitialized" warnings in nk_widget
  28058. /// - 2021/09/22 (4.08.5) - GCC __builtin_offsetof only exists in version 4 and later
  28059. /// - 2021/09/15 (4.08.4) - Fix "'num_len' may be used uninitialized" in nk_do_property
  28060. /// - 2021/09/15 (4.08.3) - Fix "Templates cannot be declared to have 'C' Linkage"
  28061. /// - 2021/09/08 (4.08.2) - Fix warnings in C89 builds
  28062. /// - 2021/09/08 (4.08.1) - Use compiler builtins for NK_OFFSETOF when possible
  28063. /// - 2021/08/17 (4.08.0) - Implemented 9-slice scaling support for widget styles
  28064. /// - 2021/08/16 (4.07.5) - Replace usage of memset in nk_font_atlas_bake with NK_MEMSET
  28065. /// - 2021/08/15 (4.07.4) - Fix conversion and sign conversion warnings
  28066. /// - 2021/08/08 (4.07.3) - Fix crash when baking merged fonts
  28067. /// - 2021/08/08 (4.07.2) - Fix Multiline Edit wrong offset
  28068. /// - 2021/03/17 (4.07.1) - Fix warning about unused parameter
  28069. /// - 2021/03/17 (4.07.0) - Fix nk_property hover bug
  28070. /// - 2021/03/15 (4.06.4) - Change nk_propertyi back to int
  28071. /// - 2021/03/15 (4.06.3) - Update documentation for functions that now return nk_bool
  28072. /// - 2020/12/19 (4.06.2) - Fix additional C++ style comments which are not allowed in ISO C90.
  28073. /// - 2020/10/11 (4.06.1) - Fix C++ style comments which are not allowed in ISO C90.
  28074. /// - 2020/10/07 (4.06.0) - Fix nk_combo return type wrongly changed to nk_bool
  28075. /// - 2020/09/05 (4.05.0) - Use the nk_font_atlas allocator for stb_truetype memory management.
  28076. /// - 2020/09/04 (4.04.1) - Replace every boolean int by nk_bool
  28077. /// - 2020/09/04 (4.04.0) - Add nk_bool with NK_INCLUDE_STANDARD_BOOL
  28078. /// - 2020/06/13 (4.03.1) - Fix nk_pool allocation sizes.
  28079. /// - 2020/06/04 (4.03.0) - Made nk_combo header symbols optional.
  28080. /// - 2020/05/27 (4.02.5) - Fix nk_do_edit: Keep scroll position when re-activating edit widget.
  28081. /// - 2020/05/09 (4.02.4) - Fix nk_menubar height calculation bug
  28082. /// - 2020/05/08 (4.02.3) - Fix missing stdarg.h with NK_INCLUDE_STANDARD_VARARGS
  28083. /// - 2020/04/30 (4.02.2) - Fix nk_edit border drawing bug
  28084. /// - 2020/04/09 (4.02.1) - Removed unused nk_sqrt function to fix compiler warnings
  28085. /// - Fixed compiler warnings if you bring your own methods for
  28086. /// nk_cos/nk_sin/nk_strtod/nk_memset/nk_memcopy/nk_dtoa
  28087. /// - 2020/04/06 (4.01.10) - Fix bug: Do not use pool before checking for NULL
  28088. /// - 2020/03/22 (4.01.9) - Fix bug where layout state wasn't restored correctly after
  28089. /// popping a tree.
  28090. /// - 2020/03/11 (4.01.8) - Fix bug where padding is subtracted from widget
  28091. /// - 2020/03/06 (4.01.7) - Fix bug where width padding was applied twice
  28092. /// - 2020/02/06 (4.01.6) - Update stb_truetype.h and stb_rect_pack.h and separate them
  28093. /// - 2019/12/10 (4.01.5) - Fix off-by-one error in NK_INTERSECT
  28094. /// - 2019/10/09 (4.01.4) - Fix bug for autoscrolling in nk_do_edit
  28095. /// - 2019/09/20 (4.01.3) - Fixed a bug wherein combobox cannot be closed by clicking the header
  28096. /// when NK_BUTTON_TRIGGER_ON_RELEASE is defined.
  28097. /// - 2019/09/10 (4.01.2) - Fixed the nk_cos function, which deviated significantly.
  28098. /// - 2019/09/08 (4.01.1) - Fixed a bug wherein re-baking of fonts caused a segmentation
  28099. /// fault due to dst_font->glyph_count not being zeroed on subsequent
  28100. /// bakes of the same set of fonts.
  28101. /// - 2019/06/23 (4.01.0) - Added nk_***_get_scroll and nk_***_set_scroll for groups, windows, and popups.
  28102. /// - 2019/06/12 (4.00.3) - Fix panel background drawing bug.
  28103. /// - 2018/10/31 (4.00.2) - Added NK_KEYSTATE_BASED_INPUT to "fix" state based backends
  28104. /// like GLFW without breaking key repeat behavior on event based.
  28105. /// - 2018/04/01 (4.00.1) - Fixed calling `nk_convert` multiple time per single frame.
  28106. /// - 2018/04/01 (4.00.0) - BREAKING CHANGE: nk_draw_list_clear no longer tries to
  28107. /// clear provided buffers. So make sure to either free
  28108. /// or clear each passed buffer after calling nk_convert.
  28109. /// - 2018/02/23 (3.00.6) - Fixed slider dragging behavior.
  28110. /// - 2018/01/31 (3.00.5) - Fixed overcalculation of cursor data in font baking process.
  28111. /// - 2018/01/31 (3.00.4) - Removed name collision with stb_truetype.
  28112. /// - 2018/01/28 (3.00.3) - Fixed panel window border drawing bug.
  28113. /// - 2018/01/12 (3.00.2) - Added `nk_group_begin_titled` for separated group identifier and title.
  28114. /// - 2018/01/07 (3.00.1) - Started to change documentation style.
  28115. /// - 2018/01/05 (3.00.0) - BREAKING CHANGE: The previous color picker API was broken
  28116. /// because of conversions between float and byte color representation.
  28117. /// Color pickers now use floating point values to represent
  28118. /// HSV values. To get back the old behavior I added some additional
  28119. /// color conversion functions to cast between nk_color and
  28120. /// nk_colorf.
  28121. /// - 2017/12/23 (2.00.7) - Fixed small warning.
  28122. /// - 2017/12/23 (2.00.7) - Fixed `nk_edit_buffer` behavior if activated to allow input.
  28123. /// - 2017/12/23 (2.00.7) - Fixed modifyable progressbar dragging visuals and input behavior.
  28124. /// - 2017/12/04 (2.00.6) - Added formatted string tooltip widget.
  28125. /// - 2017/11/18 (2.00.5) - Fixed window becoming hidden with flag `NK_WINDOW_NO_INPUT`.
  28126. /// - 2017/11/15 (2.00.4) - Fixed font merging.
  28127. /// - 2017/11/07 (2.00.3) - Fixed window size and position modifier functions.
  28128. /// - 2017/09/14 (2.00.2) - Fixed `nk_edit_buffer` and `nk_edit_focus` behavior.
  28129. /// - 2017/09/14 (2.00.1) - Fixed window closing behavior.
  28130. /// - 2017/09/14 (2.00.0) - BREAKING CHANGE: Modifying window position and size functions now
  28131. /// require the name of the window and must happen outside the window
  28132. /// building process (between function call nk_begin and nk_end).
  28133. /// - 2017/09/11 (1.40.9) - Fixed window background flag if background window is declared last.
  28134. /// - 2017/08/27 (1.40.8) - Fixed `nk_item_is_any_active` for hidden windows.
  28135. /// - 2017/08/27 (1.40.7) - Fixed window background flag.
  28136. /// - 2017/07/07 (1.40.6) - Fixed missing clipping rect check for hovering/clicked
  28137. /// query for widgets.
  28138. /// - 2017/07/07 (1.40.5) - Fixed drawing bug for vertex output for lines and stroked
  28139. /// and filled rectangles.
  28140. /// - 2017/07/07 (1.40.4) - Fixed bug in nk_convert trying to add windows that are in
  28141. /// process of being destroyed.
  28142. /// - 2017/07/07 (1.40.3) - Fixed table internal bug caused by storing table size in
  28143. /// window instead of directly in table.
  28144. /// - 2017/06/30 (1.40.2) - Removed unneeded semicolon in C++ NK_ALIGNOF macro.
  28145. /// - 2017/06/30 (1.40.1) - Fixed drawing lines smaller or equal zero.
  28146. /// - 2017/06/08 (1.40.0) - Removed the breaking part of last commit. Auto layout now only
  28147. /// comes in effect if you pass in zero was row height argument.
  28148. /// - 2017/06/08 (1.40.0) - BREAKING CHANGE: while not directly API breaking it will change
  28149. /// how layouting works. From now there will be an internal minimum
  28150. /// row height derived from font height. If you need a row smaller than
  28151. /// that you can directly set it by `nk_layout_set_min_row_height` and
  28152. /// reset the value back by calling `nk_layout_reset_min_row_height.
  28153. /// - 2017/06/08 (1.39.1) - Fixed property text edit handling bug caused by past `nk_widget` fix.
  28154. /// - 2017/06/08 (1.39.0) - Added function to retrieve window space without calling a `nk_layout_xxx` function.
  28155. /// - 2017/06/06 (1.38.5) - Fixed `nk_convert` return flag for command buffer.
  28156. /// - 2017/05/23 (1.38.4) - Fixed activation behavior for widgets partially clipped.
  28157. /// - 2017/05/10 (1.38.3) - Fixed wrong min window size mouse scaling over boundaries.
  28158. /// - 2017/05/09 (1.38.2) - Fixed vertical scrollbar drawing with not enough space.
  28159. /// - 2017/05/09 (1.38.1) - Fixed scaler dragging behavior if window size hits minimum size.
  28160. /// - 2017/05/06 (1.38.0) - Added platform double-click support.
  28161. /// - 2017/04/20 (1.37.1) - Fixed key repeat found inside glfw demo backends.
  28162. /// - 2017/04/20 (1.37.0) - Extended properties with selection and clipboard support.
  28163. /// - 2017/04/20 (1.36.2) - Fixed #405 overlapping rows with zero padding and spacing.
  28164. /// - 2017/04/09 (1.36.1) - Fixed #403 with another widget float error.
  28165. /// - 2017/04/09 (1.36.0) - Added window `NK_WINDOW_NO_INPUT` and `NK_WINDOW_NOT_INTERACTIVE` flags.
  28166. /// - 2017/04/09 (1.35.3) - Fixed buffer heap corruption.
  28167. /// - 2017/03/25 (1.35.2) - Fixed popup overlapping for `NK_WINDOW_BACKGROUND` windows.
  28168. /// - 2017/03/25 (1.35.1) - Fixed windows closing behavior.
  28169. /// - 2017/03/18 (1.35.0) - Added horizontal scroll requested in #377.
  28170. /// - 2017/03/18 (1.34.3) - Fixed long window header titles.
  28171. /// - 2017/03/04 (1.34.2) - Fixed text edit filtering.
  28172. /// - 2017/03/04 (1.34.1) - Fixed group closable flag.
  28173. /// - 2017/02/25 (1.34.0) - Added custom draw command for better language binding support.
  28174. /// - 2017/01/24 (1.33.0) - Added programmatic way to remove edit focus.
  28175. /// - 2017/01/24 (1.32.3) - Fixed wrong define for basic type definitions for windows.
  28176. /// - 2017/01/21 (1.32.2) - Fixed input capture from hidden or closed windows.
  28177. /// - 2017/01/21 (1.32.1) - Fixed slider behavior and drawing.
  28178. /// - 2017/01/13 (1.32.0) - Added flag to put scaler into the bottom left corner.
  28179. /// - 2017/01/13 (1.31.0) - Added additional row layouting method to combine both
  28180. /// dynamic and static widgets.
  28181. /// - 2016/12/31 (1.30.0) - Extended scrollbar offset from 16-bit to 32-bit.
  28182. /// - 2016/12/31 (1.29.2) - Fixed closing window bug of minimized windows.
  28183. /// - 2016/12/03 (1.29.1) - Fixed wrapped text with no separator and C89 error.
  28184. /// - 2016/12/03 (1.29.0) - Changed text wrapping to process words not characters.
  28185. /// - 2016/11/22 (1.28.6) - Fixed window minimized closing bug.
  28186. /// - 2016/11/19 (1.28.5) - Fixed abstract combo box closing behavior.
  28187. /// - 2016/11/19 (1.28.4) - Fixed tooltip flickering.
  28188. /// - 2016/11/19 (1.28.3) - Fixed memory leak caused by popup repeated closing.
  28189. /// - 2016/11/18 (1.28.2) - Fixed memory leak caused by popup panel allocation.
  28190. /// - 2016/11/10 (1.28.1) - Fixed some warnings and C++ error.
  28191. /// - 2016/11/10 (1.28.0) - Added additional `nk_button` versions which allows to directly
  28192. /// pass in a style struct to change buttons visual.
  28193. /// - 2016/11/10 (1.27.0) - Added additional `nk_tree` versions to support external state
  28194. /// storage. Just like last the `nk_group` commit the main
  28195. /// advantage is that you optionally can minimize nuklears runtime
  28196. /// memory consumption or handle hash collisions.
  28197. /// - 2016/11/09 (1.26.0) - Added additional `nk_group` version to support external scrollbar
  28198. /// offset storage. Main advantage is that you can externalize
  28199. /// the memory management for the offset. It could also be helpful
  28200. /// if you have a hash collision in `nk_group_begin` but really
  28201. /// want the name. In addition I added `nk_list_view` which allows
  28202. /// to draw big lists inside a group without actually having to
  28203. /// commit the whole list to nuklear (issue #269).
  28204. /// - 2016/10/30 (1.25.1) - Fixed clipping rectangle bug inside `nk_draw_list`.
  28205. /// - 2016/10/29 (1.25.0) - Pulled `nk_panel` memory management into nuklear and out of
  28206. /// the hands of the user. From now on users don't have to care
  28207. /// about panels unless they care about some information. If you
  28208. /// still need the panel just call `nk_window_get_panel`.
  28209. /// - 2016/10/21 (1.24.0) - Changed widget border drawing to stroked rectangle from filled
  28210. /// rectangle for less overdraw and widget background transparency.
  28211. /// - 2016/10/18 (1.23.0) - Added `nk_edit_focus` for manually edit widget focus control.
  28212. /// - 2016/09/29 (1.22.7) - Fixed deduction of basic type in non `<stdint.h>` compilation.
  28213. /// - 2016/09/29 (1.22.6) - Fixed edit widget UTF-8 text cursor drawing bug.
  28214. /// - 2016/09/28 (1.22.5) - Fixed edit widget UTF-8 text appending/inserting/removing.
  28215. /// - 2016/09/28 (1.22.4) - Fixed drawing bug inside edit widgets which offset all text
  28216. /// text in every edit widget if one of them is scrolled.
  28217. /// - 2016/09/28 (1.22.3) - Fixed small bug in edit widgets if not active. The wrong
  28218. /// text length is passed. It should have been in bytes but
  28219. /// was passed as glyphs.
  28220. /// - 2016/09/20 (1.22.2) - Fixed color button size calculation.
  28221. /// - 2016/09/20 (1.22.1) - Fixed some `nk_vsnprintf` behavior bugs and removed `<stdio.h>`
  28222. /// again from `NK_INCLUDE_STANDARD_VARARGS`.
  28223. /// - 2016/09/18 (1.22.0) - C89 does not support vsnprintf only C99 and newer as well
  28224. /// as C++11 and newer. In addition to use vsnprintf you have
  28225. /// to include <stdio.h>. So just defining `NK_INCLUDE_STD_VAR_ARGS`
  28226. /// is not enough. That behavior is now fixed. By default if
  28227. /// both varargs as well as stdio is selected I try to use
  28228. /// vsnprintf if not possible I will revert to vsprintf. If
  28229. /// varargs but not stdio was defined I will use my own function.
  28230. /// - 2016/09/15 (1.21.2) - Fixed panel `close` behavior for deeper panel levels.
  28231. /// - 2016/09/15 (1.21.1) - Fixed C++ errors and wrong argument to `nk_panel_get_xxxx`.
  28232. /// - 2016/09/13 (1.21.0) - !BREAKING! Fixed nonblocking popup behavior in menu, combo,
  28233. /// and contextual which prevented closing in y-direction if
  28234. /// popup did not reach max height.
  28235. /// In addition the height parameter was changed into vec2
  28236. /// for width and height to have more control over the popup size.
  28237. /// - 2016/09/13 (1.20.3) - Cleaned up and extended type selection.
  28238. /// - 2016/09/13 (1.20.2) - Fixed slider behavior hopefully for the last time. This time
  28239. /// all calculation are correct so no more hackery.
  28240. /// - 2016/09/13 (1.20.1) - Internal change to divide window/panel flags into panel flags and types.
  28241. /// Suprisinly spend years in C and still happened to confuse types
  28242. /// with flags. Probably something to take note.
  28243. /// - 2016/09/08 (1.20.0) - Added additional helper function to make it easier to just
  28244. /// take the produced buffers from `nk_convert` and unplug the
  28245. /// iteration process from `nk_context`. So now you can
  28246. /// just use the vertex,element and command buffer + two pointer
  28247. /// inside the command buffer retrieved by calls `nk__draw_begin`
  28248. /// and `nk__draw_end` and macro `nk_draw_foreach_bounded`.
  28249. /// - 2016/09/08 (1.19.0) - Added additional asserts to make sure every `nk_xxx_begin` call
  28250. /// for windows, popups, combobox, menu and contextual is guarded by
  28251. /// `if` condition and does not produce false drawing output.
  28252. /// - 2016/09/08 (1.18.0) - Changed confusing name for `NK_SYMBOL_RECT_FILLED`, `NK_SYMBOL_RECT`
  28253. /// to hopefully easier to understand `NK_SYMBOL_RECT_FILLED` and
  28254. /// `NK_SYMBOL_RECT_OUTLINE`.
  28255. /// - 2016/09/08 (1.17.0) - Changed confusing name for `NK_SYMBOL_CIRLCE_FILLED`, `NK_SYMBOL_CIRCLE`
  28256. /// to hopefully easier to understand `NK_SYMBOL_CIRCLE_FILLED` and
  28257. /// `NK_SYMBOL_CIRCLE_OUTLINE`.
  28258. /// - 2016/09/08 (1.16.0) - Added additional checks to select correct types if `NK_INCLUDE_FIXED_TYPES`
  28259. /// is not defined by supporting the biggest compiler GCC, clang and MSVC.
  28260. /// - 2016/09/07 (1.15.3) - Fixed `NK_INCLUDE_COMMAND_USERDATA` define to not cause an error.
  28261. /// - 2016/09/04 (1.15.2) - Fixed wrong combobox height calculation.
  28262. /// - 2016/09/03 (1.15.1) - Fixed gaps inside combo boxes in OpenGL.
  28263. /// - 2016/09/02 (1.15.0) - Changed nuklear to not have any default vertex layout and
  28264. /// instead made it user provided. The range of types to convert
  28265. /// to is quite limited at the moment, but I would be more than
  28266. /// happy to accept PRs to add additional.
  28267. /// - 2016/08/30 (1.14.2) - Removed unused variables.
  28268. /// - 2016/08/30 (1.14.1) - Fixed C++ build errors.
  28269. /// - 2016/08/30 (1.14.0) - Removed mouse dragging from SDL demo since it does not work correctly.
  28270. /// - 2016/08/30 (1.13.4) - Tweaked some default styling variables.
  28271. /// - 2016/08/30 (1.13.3) - Hopefully fixed drawing bug in slider, in general I would
  28272. /// refrain from using slider with a big number of steps.
  28273. /// - 2016/08/30 (1.13.2) - Fixed close and minimize button which would fire even if the
  28274. /// window was in Read Only Mode.
  28275. /// - 2016/08/30 (1.13.1) - Fixed popup panel padding handling which was previously just
  28276. /// a hack for combo box and menu.
  28277. /// - 2016/08/30 (1.13.0) - Removed `NK_WINDOW_DYNAMIC` flag from public API since
  28278. /// it is bugged and causes issues in window selection.
  28279. /// - 2016/08/30 (1.12.0) - Removed scaler size. The size of the scaler is now
  28280. /// determined by the scrollbar size.
  28281. /// - 2016/08/30 (1.11.2) - Fixed some drawing bugs caused by changes from 1.11.0.
  28282. /// - 2016/08/30 (1.11.1) - Fixed overlapping minimized window selection.
  28283. /// - 2016/08/30 (1.11.0) - Removed some internal complexity and overly complex code
  28284. /// handling panel padding and panel border.
  28285. /// - 2016/08/29 (1.10.0) - Added additional height parameter to `nk_combobox_xxx`.
  28286. /// - 2016/08/29 (1.10.0) - Fixed drawing bug in dynamic popups.
  28287. /// - 2016/08/29 (1.10.0) - Added experimental mouse scrolling to popups, menus and comboboxes.
  28288. /// - 2016/08/26 (1.10.0) - Added window name string prepresentation to account for
  28289. /// hash collisions. Currently limited to `NK_WINDOW_MAX_NAME`
  28290. /// which in term can be redefined if not big enough.
  28291. /// - 2016/08/26 (1.10.0) - Added stacks for temporary style/UI changes in code.
  28292. /// - 2016/08/25 (1.10.0) - Changed `nk_input_is_key_pressed` and 'nk_input_is_key_released'
  28293. /// to account for key press and release happening in one frame.
  28294. /// - 2016/08/25 (1.10.0) - Added additional nk_edit flag to directly jump to the end on activate.
  28295. /// - 2016/08/17 (1.09.6) - Removed invalid check for value zero in `nk_propertyx`.
  28296. /// - 2016/08/16 (1.09.5) - Fixed ROM mode for deeper levels of popup windows parents.
  28297. /// - 2016/08/15 (1.09.4) - Editbox are now still active if enter was pressed with flag
  28298. /// `NK_EDIT_SIG_ENTER`. Main reasoning is to be able to keep
  28299. /// typing after committing.
  28300. /// - 2016/08/15 (1.09.4) - Removed redundant code.
  28301. /// - 2016/08/15 (1.09.4) - Fixed negative numbers in `nk_strtoi` and remove unused variable.
  28302. /// - 2016/08/15 (1.09.3) - Fixed `NK_WINDOW_BACKGROUND` flag behavior to select a background
  28303. /// window only as selected by hovering and not by clicking.
  28304. /// - 2016/08/14 (1.09.2) - Fixed a bug in font atlas which caused wrong loading
  28305. /// of glyphs for font with multiple ranges.
  28306. /// - 2016/08/12 (1.09.1) - Added additional function to check if window is currently
  28307. /// hidden and therefore not visible.
  28308. /// - 2016/08/12 (1.09.1) - nk_window_is_closed now queries the correct flag `NK_WINDOW_CLOSED`
  28309. /// instead of the old flag `NK_WINDOW_HIDDEN`.
  28310. /// - 2016/08/09 (1.09.0) - Added additional double version to nk_property and changed
  28311. /// the underlying implementation to not cast to float and instead
  28312. /// work directly on the given values.
  28313. /// - 2016/08/09 (1.08.0) - Added additional define to overwrite library internal
  28314. /// floating pointer number to string conversion for additional
  28315. /// precision.
  28316. /// - 2016/08/09 (1.08.0) - Added additional define to overwrite library internal
  28317. /// string to floating point number conversion for additional
  28318. /// precision.
  28319. /// - 2016/08/08 (1.07.2) - Fixed compiling error without define `NK_INCLUDE_FIXED_TYPE`.
  28320. /// - 2016/08/08 (1.07.1) - Fixed possible floating point error inside `nk_widget` leading
  28321. /// to wrong widget width calculation which results in widgets falsely
  28322. /// becoming tagged as not inside window and cannot be accessed.
  28323. /// - 2016/08/08 (1.07.0) - Nuklear now differentiates between hiding a window (NK_WINDOW_HIDDEN) and
  28324. /// closing a window (NK_WINDOW_CLOSED). A window can be hidden/shown
  28325. /// by using `nk_window_show` and closed by either clicking the close
  28326. /// icon in a window or by calling `nk_window_close`. Only closed
  28327. /// windows get removed at the end of the frame while hidden windows
  28328. /// remain.
  28329. /// - 2016/08/08 (1.06.0) - Added `nk_edit_string_zero_terminated` as a second option to
  28330. /// `nk_edit_string` which takes, edits and outputs a '\0' terminated string.
  28331. /// - 2016/08/08 (1.05.4) - Fixed scrollbar auto hiding behavior.
  28332. /// - 2016/08/08 (1.05.3) - Fixed wrong panel padding selection in `nk_layout_widget_space`.
  28333. /// - 2016/08/07 (1.05.2) - Fixed old bug in dynamic immediate mode layout API, calculating
  28334. /// wrong item spacing and panel width.
  28335. /// - 2016/08/07 (1.05.1) - Hopefully finally fixed combobox popup drawing bug.
  28336. /// - 2016/08/07 (1.05.0) - Split varargs away from `NK_INCLUDE_STANDARD_IO` into own
  28337. /// define `NK_INCLUDE_STANDARD_VARARGS` to allow more fine
  28338. /// grained controlled over library includes.
  28339. /// - 2016/08/06 (1.04.5) - Changed memset calls to `NK_MEMSET`.
  28340. /// - 2016/08/04 (1.04.4) - Fixed fast window scaling behavior.
  28341. /// - 2016/08/04 (1.04.3) - Fixed window scaling, movement bug which appears if you
  28342. /// move/scale a window and another window is behind it.
  28343. /// If you are fast enough then the window behind gets activated
  28344. /// and the operation is blocked. I now require activating
  28345. /// by hovering only if mouse is not pressed.
  28346. /// - 2016/08/04 (1.04.2) - Fixed changing fonts.
  28347. /// - 2016/08/03 (1.04.1) - Fixed `NK_WINDOW_BACKGROUND` behavior.
  28348. /// - 2016/08/03 (1.04.0) - Added color parameter to `nk_draw_image`.
  28349. /// - 2016/08/03 (1.04.0) - Added additional window padding style attributes for
  28350. /// sub windows (combo, menu, ...).
  28351. /// - 2016/08/03 (1.04.0) - Added functions to show/hide software cursor.
  28352. /// - 2016/08/03 (1.04.0) - Added `NK_WINDOW_BACKGROUND` flag to force a window
  28353. /// to be always in the background of the screen.
  28354. /// - 2016/08/03 (1.03.2) - Removed invalid assert macro for NK_RGB color picker.
  28355. /// - 2016/08/01 (1.03.1) - Added helper macros into header include guard.
  28356. /// - 2016/07/29 (1.03.0) - Moved the window/table pool into the header part to
  28357. /// simplify memory management by removing the need to
  28358. /// allocate the pool.
  28359. /// - 2016/07/29 (1.02.0) - Added auto scrollbar hiding window flag which if enabled
  28360. /// will hide the window scrollbar after NK_SCROLLBAR_HIDING_TIMEOUT
  28361. /// seconds without window interaction. To make it work
  28362. /// you have to also set a delta time inside the `nk_context`.
  28363. /// - 2016/07/25 (1.01.1) - Fixed small panel and panel border drawing bugs.
  28364. /// - 2016/07/15 (1.01.0) - Added software cursor to `nk_style` and `nk_context`.
  28365. /// - 2016/07/15 (1.01.0) - Added const correctness to `nk_buffer_push' data argument.
  28366. /// - 2016/07/15 (1.01.0) - Removed internal font baking API and simplified
  28367. /// font atlas memory management by converting pointer
  28368. /// arrays for fonts and font configurations to lists.
  28369. /// - 2016/07/15 (1.00.0) - Changed button API to use context dependent button
  28370. /// behavior instead of passing it for every function call.
  28371. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  28372. /// ## Gallery
  28373. /// ![Figure [blue]: Feature overview with blue color styling](https://cloud.githubusercontent.com/assets/8057201/13538240/acd96876-e249-11e5-9547-5ac0b19667a0.png)
  28374. /// ![Figure [red]: Feature overview with red color styling](https://cloud.githubusercontent.com/assets/8057201/13538243/b04acd4c-e249-11e5-8fd2-ad7744a5b446.png)
  28375. /// ![Figure [widgets]: Widget overview](https://cloud.githubusercontent.com/assets/8057201/11282359/3325e3c6-8eff-11e5-86cb-cf02b0596087.png)
  28376. /// ![Figure [blackwhite]: Black and white](https://cloud.githubusercontent.com/assets/8057201/11033668/59ab5d04-86e5-11e5-8091-c56f16411565.png)
  28377. /// ![Figure [filexp]: File explorer](https://cloud.githubusercontent.com/assets/8057201/10718115/02a9ba08-7b6b-11e5-950f-adacdd637739.png)
  28378. /// ![Figure [opengl]: OpenGL Editor](https://cloud.githubusercontent.com/assets/8057201/12779619/2a20d72c-ca69-11e5-95fe-4edecf820d5c.png)
  28379. /// ![Figure [nodedit]: Node Editor](https://cloud.githubusercontent.com/assets/8057201/9976995/e81ac04a-5ef7-11e5-872b-acd54fbeee03.gif)
  28380. /// ![Figure [skinning]: Using skinning in Nuklear](https://cloud.githubusercontent.com/assets/8057201/15991632/76494854-30b8-11e6-9555-a69840d0d50b.png)
  28381. /// ![Figure [bf]: Heavy modified version](https://cloud.githubusercontent.com/assets/8057201/14902576/339926a8-0d9c-11e6-9fee-a8b73af04473.png)
  28382. ///
  28383. /// ## Credits
  28384. /// Developed by Micha Mettke and every direct or indirect github contributor. <br /><br />
  28385. ///
  28386. /// 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 />
  28387. /// Uses [stddoc.c](https://github.com/r-lyeh/stddoc.c) from [email protected] for documentation generation <br /><br />
  28388. /// Embeds ProggyClean.ttf font by Tristan Grimmer (MIT license). <br />
  28389. ///
  28390. /// Big thank you to Omar Cornut (ocornut@github) for his [imgui library](https://github.com/ocornut/imgui) and
  28391. /// giving me the inspiration for this library, Casey Muratori for handmade hero
  28392. /// and his original immediate mode graphical user interface idea and Sean
  28393. /// Barret for his amazing single header libraries which restored my faith
  28394. /// in libraries and brought me to create some of my own. Finally Apoorva Joshi
  28395. /// for his single header file packer.
  28396. */