text_server_adv.cpp 224 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231
  1. /*************************************************************************/
  2. /* text_server_adv.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "text_server_adv.h"
  31. #ifdef GDEXTENSION
  32. // Headers for building as GDExtension plug-in.
  33. #include <godot_cpp/classes/file.hpp>
  34. #include <godot_cpp/classes/project_settings.hpp>
  35. #include <godot_cpp/classes/rendering_server.hpp>
  36. #include <godot_cpp/classes/translation_server.hpp>
  37. #include <godot_cpp/core/error_macros.hpp>
  38. using namespace godot;
  39. #else
  40. // Headers for building as built-in module.
  41. #include "core/config/project_settings.h"
  42. #include "core/core_bind.h"
  43. #include "core/error/error_macros.h"
  44. #include "core/object/worker_thread_pool.h"
  45. #include "core/string/print_string.h"
  46. #include "core/string/translation.h"
  47. #include "modules/modules_enabled.gen.h" // For freetype, msdfgen.
  48. using namespace core_bind;
  49. #endif
  50. // Built-in ICU data.
  51. #ifdef ICU_STATIC_DATA
  52. #include "icudata.gen.h"
  53. #endif
  54. // Thirdparty headers.
  55. #ifdef MODULE_MSDFGEN_ENABLED
  56. #include "core/ShapeDistanceFinder.h"
  57. #include "core/contour-combiners.h"
  58. #include "core/edge-selectors.h"
  59. #include "msdfgen.h"
  60. #endif
  61. /*************************************************************************/
  62. /* bmp_font_t HarfBuzz Bitmap font interface */
  63. /*************************************************************************/
  64. hb_font_funcs_t *TextServerAdvanced::funcs = nullptr;
  65. TextServerAdvanced::bmp_font_t *TextServerAdvanced::_bmp_font_create(TextServerAdvanced::FontForSizeAdvanced *p_face, bool p_unref) {
  66. bmp_font_t *bm_font = memnew(bmp_font_t);
  67. if (!bm_font) {
  68. return nullptr;
  69. }
  70. bm_font->face = p_face;
  71. bm_font->unref = p_unref;
  72. return bm_font;
  73. }
  74. void TextServerAdvanced::_bmp_font_destroy(void *p_data) {
  75. bmp_font_t *bm_font = static_cast<bmp_font_t *>(p_data);
  76. memdelete(bm_font);
  77. }
  78. hb_bool_t TextServerAdvanced::_bmp_get_nominal_glyph(hb_font_t *p_font, void *p_font_data, hb_codepoint_t p_unicode, hb_codepoint_t *r_glyph, void *p_user_data) {
  79. const bmp_font_t *bm_font = static_cast<const bmp_font_t *>(p_font_data);
  80. if (!bm_font->face) {
  81. return false;
  82. }
  83. if (!bm_font->face->glyph_map.has(p_unicode)) {
  84. if (bm_font->face->glyph_map.has(0xf000u + p_unicode)) {
  85. *r_glyph = 0xf000u + p_unicode;
  86. return true;
  87. } else {
  88. return false;
  89. }
  90. }
  91. *r_glyph = p_unicode;
  92. return true;
  93. }
  94. hb_position_t TextServerAdvanced::_bmp_get_glyph_h_advance(hb_font_t *p_font, void *p_font_data, hb_codepoint_t p_glyph, void *p_user_data) {
  95. const bmp_font_t *bm_font = static_cast<const bmp_font_t *>(p_font_data);
  96. if (!bm_font->face) {
  97. return 0;
  98. }
  99. if (!bm_font->face->glyph_map.has(p_glyph)) {
  100. return 0;
  101. }
  102. return bm_font->face->glyph_map[p_glyph].advance.x * 64;
  103. }
  104. hb_position_t TextServerAdvanced::_bmp_get_glyph_v_advance(hb_font_t *p_font, void *p_font_data, hb_codepoint_t p_glyph, void *p_user_data) {
  105. const bmp_font_t *bm_font = static_cast<const bmp_font_t *>(p_font_data);
  106. if (!bm_font->face) {
  107. return 0;
  108. }
  109. if (!bm_font->face->glyph_map.has(p_glyph)) {
  110. return 0;
  111. }
  112. return -bm_font->face->glyph_map[p_glyph].advance.y * 64;
  113. }
  114. hb_position_t TextServerAdvanced::_bmp_get_glyph_h_kerning(hb_font_t *p_font, void *p_font_data, hb_codepoint_t p_left_glyph, hb_codepoint_t p_right_glyph, void *p_user_data) {
  115. const bmp_font_t *bm_font = static_cast<const bmp_font_t *>(p_font_data);
  116. if (!bm_font->face) {
  117. return 0;
  118. }
  119. if (!bm_font->face->kerning_map.has(Vector2i(p_left_glyph, p_right_glyph))) {
  120. return 0;
  121. }
  122. return bm_font->face->kerning_map[Vector2i(p_left_glyph, p_right_glyph)].x * 64;
  123. }
  124. hb_bool_t TextServerAdvanced::_bmp_get_glyph_v_origin(hb_font_t *p_font, void *p_font_data, hb_codepoint_t p_glyph, hb_position_t *r_x, hb_position_t *r_y, void *p_user_data) {
  125. const bmp_font_t *bm_font = static_cast<const bmp_font_t *>(p_font_data);
  126. if (!bm_font->face) {
  127. return false;
  128. }
  129. if (!bm_font->face->glyph_map.has(p_glyph)) {
  130. return false;
  131. }
  132. *r_x = bm_font->face->glyph_map[p_glyph].advance.x * 32;
  133. *r_y = -bm_font->face->ascent * 64;
  134. return true;
  135. }
  136. hb_bool_t TextServerAdvanced::_bmp_get_glyph_extents(hb_font_t *p_font, void *p_font_data, hb_codepoint_t p_glyph, hb_glyph_extents_t *r_extents, void *p_user_data) {
  137. const bmp_font_t *bm_font = static_cast<const bmp_font_t *>(p_font_data);
  138. if (!bm_font->face) {
  139. return false;
  140. }
  141. if (!bm_font->face->glyph_map.has(p_glyph)) {
  142. return false;
  143. }
  144. r_extents->x_bearing = 0;
  145. r_extents->y_bearing = 0;
  146. r_extents->width = bm_font->face->glyph_map[p_glyph].rect.size.x * 64;
  147. r_extents->height = bm_font->face->glyph_map[p_glyph].rect.size.y * 64;
  148. return true;
  149. }
  150. hb_bool_t TextServerAdvanced::_bmp_get_font_h_extents(hb_font_t *p_font, void *p_font_data, hb_font_extents_t *r_metrics, void *p_user_data) {
  151. const bmp_font_t *bm_font = static_cast<const bmp_font_t *>(p_font_data);
  152. if (!bm_font->face) {
  153. return false;
  154. }
  155. r_metrics->ascender = bm_font->face->ascent;
  156. r_metrics->descender = bm_font->face->descent;
  157. r_metrics->line_gap = 0;
  158. return true;
  159. }
  160. void TextServerAdvanced::_bmp_create_font_funcs() {
  161. if (funcs == nullptr) {
  162. funcs = hb_font_funcs_create();
  163. hb_font_funcs_set_font_h_extents_func(funcs, _bmp_get_font_h_extents, nullptr, nullptr);
  164. hb_font_funcs_set_nominal_glyph_func(funcs, _bmp_get_nominal_glyph, nullptr, nullptr);
  165. hb_font_funcs_set_glyph_h_advance_func(funcs, _bmp_get_glyph_h_advance, nullptr, nullptr);
  166. hb_font_funcs_set_glyph_v_advance_func(funcs, _bmp_get_glyph_v_advance, nullptr, nullptr);
  167. hb_font_funcs_set_glyph_v_origin_func(funcs, _bmp_get_glyph_v_origin, nullptr, nullptr);
  168. hb_font_funcs_set_glyph_h_kerning_func(funcs, _bmp_get_glyph_h_kerning, nullptr, nullptr);
  169. hb_font_funcs_set_glyph_extents_func(funcs, _bmp_get_glyph_extents, nullptr, nullptr);
  170. hb_font_funcs_make_immutable(funcs);
  171. }
  172. }
  173. void TextServerAdvanced::_bmp_free_font_funcs() {
  174. if (funcs != nullptr) {
  175. hb_font_funcs_destroy(funcs);
  176. funcs = nullptr;
  177. }
  178. }
  179. void TextServerAdvanced::_bmp_font_set_funcs(hb_font_t *p_font, TextServerAdvanced::FontForSizeAdvanced *p_face, bool p_unref) {
  180. hb_font_set_funcs(p_font, funcs, _bmp_font_create(p_face, p_unref), _bmp_font_destroy);
  181. }
  182. hb_font_t *TextServerAdvanced::_bmp_font_create(TextServerAdvanced::FontForSizeAdvanced *p_face, hb_destroy_func_t p_destroy) {
  183. hb_font_t *font;
  184. hb_face_t *face = hb_face_create(nullptr, 0);
  185. font = hb_font_create(face);
  186. hb_face_destroy(face);
  187. _bmp_font_set_funcs(font, p_face, false);
  188. return font;
  189. }
  190. /*************************************************************************/
  191. /* Character properties. */
  192. /*************************************************************************/
  193. _FORCE_INLINE_ bool is_ain(char32_t p_chr) {
  194. return u_getIntPropertyValue(p_chr, UCHAR_JOINING_GROUP) == U_JG_AIN;
  195. }
  196. _FORCE_INLINE_ bool is_alef(char32_t p_chr) {
  197. return u_getIntPropertyValue(p_chr, UCHAR_JOINING_GROUP) == U_JG_ALEF;
  198. }
  199. _FORCE_INLINE_ bool is_beh(char32_t p_chr) {
  200. int32_t prop = u_getIntPropertyValue(p_chr, UCHAR_JOINING_GROUP);
  201. return (prop == U_JG_BEH) || (prop == U_JG_NOON) || (prop == U_JG_AFRICAN_NOON) || (prop == U_JG_NYA) || (prop == U_JG_YEH) || (prop == U_JG_FARSI_YEH);
  202. }
  203. _FORCE_INLINE_ bool is_dal(char32_t p_chr) {
  204. return u_getIntPropertyValue(p_chr, UCHAR_JOINING_GROUP) == U_JG_DAL;
  205. }
  206. _FORCE_INLINE_ bool is_feh(char32_t p_chr) {
  207. return (u_getIntPropertyValue(p_chr, UCHAR_JOINING_GROUP) == U_JG_FEH) || (u_getIntPropertyValue(p_chr, UCHAR_JOINING_GROUP) == U_JG_AFRICAN_FEH);
  208. }
  209. _FORCE_INLINE_ bool is_gaf(char32_t p_chr) {
  210. return u_getIntPropertyValue(p_chr, UCHAR_JOINING_GROUP) == U_JG_GAF;
  211. }
  212. _FORCE_INLINE_ bool is_heh(char32_t p_chr) {
  213. return u_getIntPropertyValue(p_chr, UCHAR_JOINING_GROUP) == U_JG_HEH;
  214. }
  215. _FORCE_INLINE_ bool is_kaf(char32_t p_chr) {
  216. return u_getIntPropertyValue(p_chr, UCHAR_JOINING_GROUP) == U_JG_KAF;
  217. }
  218. _FORCE_INLINE_ bool is_lam(char32_t p_chr) {
  219. return u_getIntPropertyValue(p_chr, UCHAR_JOINING_GROUP) == U_JG_LAM;
  220. }
  221. _FORCE_INLINE_ bool is_qaf(char32_t p_chr) {
  222. return (u_getIntPropertyValue(p_chr, UCHAR_JOINING_GROUP) == U_JG_QAF) || (u_getIntPropertyValue(p_chr, UCHAR_JOINING_GROUP) == U_JG_AFRICAN_QAF);
  223. }
  224. _FORCE_INLINE_ bool is_reh(char32_t p_chr) {
  225. return u_getIntPropertyValue(p_chr, UCHAR_JOINING_GROUP) == U_JG_REH;
  226. }
  227. _FORCE_INLINE_ bool is_seen_sad(char32_t p_chr) {
  228. return (u_getIntPropertyValue(p_chr, UCHAR_JOINING_GROUP) == U_JG_SAD) || (u_getIntPropertyValue(p_chr, UCHAR_JOINING_GROUP) == U_JG_SEEN);
  229. }
  230. _FORCE_INLINE_ bool is_tah(char32_t p_chr) {
  231. return u_getIntPropertyValue(p_chr, UCHAR_JOINING_GROUP) == U_JG_TAH;
  232. }
  233. _FORCE_INLINE_ bool is_teh_marbuta(char32_t p_chr) {
  234. return u_getIntPropertyValue(p_chr, UCHAR_JOINING_GROUP) == U_JG_TEH_MARBUTA;
  235. }
  236. _FORCE_INLINE_ bool is_yeh(char32_t p_chr) {
  237. int32_t prop = u_getIntPropertyValue(p_chr, UCHAR_JOINING_GROUP);
  238. return (prop == U_JG_YEH) || (prop == U_JG_FARSI_YEH) || (prop == U_JG_YEH_BARREE) || (prop == U_JG_BURUSHASKI_YEH_BARREE) || (prop == U_JG_YEH_WITH_TAIL);
  239. }
  240. _FORCE_INLINE_ bool is_waw(char32_t p_chr) {
  241. return u_getIntPropertyValue(p_chr, UCHAR_JOINING_GROUP) == U_JG_WAW;
  242. }
  243. _FORCE_INLINE_ bool is_transparent(char32_t p_chr) {
  244. return u_getIntPropertyValue(p_chr, UCHAR_JOINING_TYPE) == U_JT_TRANSPARENT;
  245. }
  246. _FORCE_INLINE_ bool is_ligature(char32_t p_chr, char32_t p_nchr) {
  247. return (is_lam(p_chr) && is_alef(p_nchr));
  248. }
  249. _FORCE_INLINE_ bool is_connected_to_prev(char32_t p_chr, char32_t p_pchr) {
  250. int32_t prop = u_getIntPropertyValue(p_pchr, UCHAR_JOINING_TYPE);
  251. return (prop != U_JT_RIGHT_JOINING) && (prop != U_JT_NON_JOINING) ? !is_ligature(p_pchr, p_chr) : false;
  252. }
  253. /*************************************************************************/
  254. bool TextServerAdvanced::has_feature(Feature p_feature) const {
  255. switch (p_feature) {
  256. case FEATURE_SIMPLE_LAYOUT:
  257. case FEATURE_BIDI_LAYOUT:
  258. case FEATURE_VERTICAL_LAYOUT:
  259. case FEATURE_SHAPING:
  260. case FEATURE_KASHIDA_JUSTIFICATION:
  261. case FEATURE_BREAK_ITERATORS:
  262. case FEATURE_FONT_BITMAP:
  263. #ifdef MODULE_FREETYPE_ENABLED
  264. case FEATURE_FONT_DYNAMIC:
  265. #endif
  266. #ifdef MODULE_MSDFGEN_ENABLED
  267. case FEATURE_FONT_MSDF:
  268. #endif
  269. case FEATURE_FONT_VARIABLE:
  270. case FEATURE_CONTEXT_SENSITIVE_CASE_CONVERSION:
  271. case FEATURE_USE_SUPPORT_DATA:
  272. case FEATURE_UNICODE_IDENTIFIERS:
  273. case FEATURE_UNICODE_SECURITY:
  274. return true;
  275. default: {
  276. }
  277. }
  278. return false;
  279. }
  280. String TextServerAdvanced::get_name() const {
  281. #ifdef GDEXTENSION
  282. return "ICU / HarfBuzz / Graphite (GDExtension)";
  283. #else
  284. return "ICU / HarfBuzz / Graphite (Built-in)";
  285. #endif
  286. }
  287. int64_t TextServerAdvanced::get_features() const {
  288. int64_t interface_features = FEATURE_SIMPLE_LAYOUT | FEATURE_BIDI_LAYOUT | FEATURE_VERTICAL_LAYOUT | FEATURE_SHAPING | FEATURE_KASHIDA_JUSTIFICATION | FEATURE_BREAK_ITERATORS | FEATURE_FONT_BITMAP | FEATURE_FONT_VARIABLE | FEATURE_CONTEXT_SENSITIVE_CASE_CONVERSION | FEATURE_USE_SUPPORT_DATA;
  289. #ifdef MODULE_FREETYPE_ENABLED
  290. interface_features |= FEATURE_FONT_DYNAMIC;
  291. #endif
  292. #ifdef MODULE_MSDFGEN_ENABLED
  293. interface_features |= FEATURE_FONT_MSDF;
  294. #endif
  295. return interface_features;
  296. }
  297. void TextServerAdvanced::free_rid(const RID &p_rid) {
  298. _THREAD_SAFE_METHOD_
  299. if (font_owner.owns(p_rid)) {
  300. FontAdvanced *fd = font_owner.get_or_null(p_rid);
  301. font_owner.free(p_rid);
  302. memdelete(fd);
  303. } else if (shaped_owner.owns(p_rid)) {
  304. ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_rid);
  305. shaped_owner.free(p_rid);
  306. memdelete(sd);
  307. }
  308. }
  309. bool TextServerAdvanced::has(const RID &p_rid) {
  310. _THREAD_SAFE_METHOD_
  311. return font_owner.owns(p_rid) || shaped_owner.owns(p_rid);
  312. }
  313. bool TextServerAdvanced::load_support_data(const String &p_filename) {
  314. _THREAD_SAFE_METHOD_
  315. #ifdef ICU_STATIC_DATA
  316. if (!icu_data_loaded) {
  317. UErrorCode err = U_ZERO_ERROR;
  318. u_init(&err); // Do not check for errors, since we only load part of the data.
  319. icu_data_loaded = true;
  320. }
  321. #else
  322. if (!icu_data_loaded) {
  323. String filename = (p_filename.is_empty()) ? String("res://") + _MKSTR(ICU_DATA_NAME) : p_filename;
  324. Ref<File> f;
  325. f.instantiate();
  326. if (f->open(filename, File::READ) != OK) {
  327. return false;
  328. }
  329. uint64_t len = f->get_length();
  330. PackedByteArray icu_data = f->get_buffer(len);
  331. UErrorCode err = U_ZERO_ERROR;
  332. udata_setCommonData(icu_data.ptr(), &err);
  333. if (U_FAILURE(err)) {
  334. ERR_FAIL_V_MSG(false, u_errorName(err));
  335. }
  336. err = U_ZERO_ERROR;
  337. u_init(&err);
  338. if (U_FAILURE(err)) {
  339. ERR_FAIL_V_MSG(false, u_errorName(err));
  340. }
  341. icu_data_loaded = true;
  342. }
  343. #endif
  344. return true;
  345. }
  346. String TextServerAdvanced::get_support_data_filename() const {
  347. #ifdef ICU_STATIC_DATA
  348. return _MKSTR(ICU_DATA_NAME);
  349. #else
  350. return String();
  351. #endif
  352. }
  353. String TextServerAdvanced::get_support_data_info() const {
  354. #ifdef ICU_STATIC_DATA
  355. return String("ICU break iteration data (") + _MKSTR(ICU_DATA_NAME) + String(").");
  356. #else
  357. return String();
  358. #endif
  359. }
  360. bool TextServerAdvanced::save_support_data(const String &p_filename) const {
  361. _THREAD_SAFE_METHOD_
  362. #ifdef ICU_STATIC_DATA
  363. // Store data to the res file if it's available.
  364. Ref<File> f;
  365. f.instantiate();
  366. if (f->open(p_filename, File::WRITE) != OK) {
  367. return false;
  368. }
  369. PackedByteArray icu_data;
  370. icu_data.resize(U_ICUDATA_SIZE);
  371. memcpy(icu_data.ptrw(), U_ICUDATA_ENTRY_POINT, U_ICUDATA_SIZE);
  372. f->store_buffer(icu_data);
  373. return true;
  374. #else
  375. return false;
  376. #endif
  377. }
  378. bool TextServerAdvanced::is_locale_right_to_left(const String &p_locale) const {
  379. String l = p_locale.get_slicec('_', 0);
  380. if ((l == "ar") || (l == "dv") || (l == "he") || (l == "fa") || (l == "ff") || (l == "ku") || (l == "ur")) {
  381. return true;
  382. } else {
  383. return false;
  384. }
  385. }
  386. _FORCE_INLINE_ void TextServerAdvanced::_insert_feature(const StringName &p_name, int32_t p_tag, Variant::Type p_vtype, bool p_hidden) {
  387. FeatureInfo fi;
  388. fi.name = p_name;
  389. fi.vtype = p_vtype;
  390. fi.hidden = p_hidden;
  391. feature_sets.insert(p_name, p_tag);
  392. feature_sets_inv.insert(p_tag, fi);
  393. }
  394. void TextServerAdvanced::_insert_feature_sets() {
  395. // Registered OpenType feature tags.
  396. // Name, Tag, Data Type, Hidden
  397. _insert_feature("access_all_alternates", HB_TAG('a', 'a', 'l', 't'), Variant::Type::INT, false);
  398. _insert_feature("above_base_forms", HB_TAG('a', 'b', 'v', 'f'), Variant::Type::INT, true);
  399. _insert_feature("above_base_mark_positioning", HB_TAG('a', 'b', 'v', 'm'), Variant::Type::INT, true);
  400. _insert_feature("above_base_substitutions", HB_TAG('a', 'b', 'v', 's'), Variant::Type::INT, true);
  401. _insert_feature("alternative_fractions", HB_TAG('a', 'f', 'r', 'c'), Variant::Type::INT, false);
  402. _insert_feature("akhands", HB_TAG('a', 'k', 'h', 'n'), Variant::Type::INT, true);
  403. _insert_feature("below_base_forms", HB_TAG('b', 'l', 'w', 'f'), Variant::Type::INT, true);
  404. _insert_feature("below_base_mark_positioning", HB_TAG('b', 'l', 'w', 'm'), Variant::Type::INT, true);
  405. _insert_feature("below_base_substitutions", HB_TAG('b', 'l', 'w', 's'), Variant::Type::INT, true);
  406. _insert_feature("contextual_alternates", HB_TAG('c', 'a', 'l', 't'), Variant::Type::BOOL, false);
  407. _insert_feature("case_sensitive_forms", HB_TAG('c', 'a', 's', 'e'), Variant::Type::BOOL, false);
  408. _insert_feature("glyph_composition", HB_TAG('c', 'c', 'm', 'p'), Variant::Type::INT, true);
  409. _insert_feature("conjunct_form_after_ro", HB_TAG('c', 'f', 'a', 'r'), Variant::Type::INT, true);
  410. _insert_feature("contextual_half_width_spacing", HB_TAG('c', 'h', 'w', 's'), Variant::Type::INT, true);
  411. _insert_feature("conjunct_forms", HB_TAG('c', 'j', 'c', 't'), Variant::Type::INT, true);
  412. _insert_feature("contextual_ligatures", HB_TAG('c', 'l', 'i', 'g'), Variant::Type::BOOL, false);
  413. _insert_feature("centered_cjk_punctuation", HB_TAG('c', 'p', 'c', 't'), Variant::Type::BOOL, false);
  414. _insert_feature("capital_spacing", HB_TAG('c', 'p', 's', 'p'), Variant::Type::BOOL, false);
  415. _insert_feature("contextual_swash", HB_TAG('c', 's', 'w', 'h'), Variant::Type::INT, false);
  416. _insert_feature("cursive_positioning", HB_TAG('c', 'u', 'r', 's'), Variant::Type::INT, true);
  417. _insert_feature("character_variant_01", HB_TAG('c', 'v', '0', '1'), Variant::Type::BOOL, false);
  418. _insert_feature("character_variant_02", HB_TAG('c', 'v', '0', '2'), Variant::Type::BOOL, false);
  419. _insert_feature("character_variant_03", HB_TAG('c', 'v', '0', '3'), Variant::Type::BOOL, false);
  420. _insert_feature("character_variant_04", HB_TAG('c', 'v', '0', '4'), Variant::Type::BOOL, false);
  421. _insert_feature("character_variant_05", HB_TAG('c', 'v', '0', '5'), Variant::Type::BOOL, false);
  422. _insert_feature("character_variant_06", HB_TAG('c', 'v', '0', '6'), Variant::Type::BOOL, false);
  423. _insert_feature("character_variant_07", HB_TAG('c', 'v', '0', '7'), Variant::Type::BOOL, false);
  424. _insert_feature("character_variant_08", HB_TAG('c', 'v', '0', '8'), Variant::Type::BOOL, false);
  425. _insert_feature("character_variant_09", HB_TAG('c', 'v', '0', '9'), Variant::Type::BOOL, false);
  426. _insert_feature("character_variant_10", HB_TAG('c', 'v', '1', '0'), Variant::Type::BOOL, false);
  427. _insert_feature("character_variant_11", HB_TAG('c', 'v', '1', '1'), Variant::Type::BOOL, false);
  428. _insert_feature("character_variant_12", HB_TAG('c', 'v', '1', '2'), Variant::Type::BOOL, false);
  429. _insert_feature("character_variant_13", HB_TAG('c', 'v', '1', '3'), Variant::Type::BOOL, false);
  430. _insert_feature("character_variant_14", HB_TAG('c', 'v', '1', '4'), Variant::Type::BOOL, false);
  431. _insert_feature("character_variant_15", HB_TAG('c', 'v', '1', '5'), Variant::Type::BOOL, false);
  432. _insert_feature("character_variant_16", HB_TAG('c', 'v', '1', '6'), Variant::Type::BOOL, false);
  433. _insert_feature("character_variant_17", HB_TAG('c', 'v', '1', '7'), Variant::Type::BOOL, false);
  434. _insert_feature("character_variant_18", HB_TAG('c', 'v', '1', '8'), Variant::Type::BOOL, false);
  435. _insert_feature("character_variant_19", HB_TAG('c', 'v', '1', '9'), Variant::Type::BOOL, false);
  436. _insert_feature("character_variant_20", HB_TAG('c', 'v', '2', '0'), Variant::Type::BOOL, false);
  437. _insert_feature("character_variant_21", HB_TAG('c', 'v', '2', '1'), Variant::Type::BOOL, false);
  438. _insert_feature("character_variant_22", HB_TAG('c', 'v', '2', '2'), Variant::Type::BOOL, false);
  439. _insert_feature("character_variant_23", HB_TAG('c', 'v', '2', '3'), Variant::Type::BOOL, false);
  440. _insert_feature("character_variant_24", HB_TAG('c', 'v', '2', '4'), Variant::Type::BOOL, false);
  441. _insert_feature("character_variant_25", HB_TAG('c', 'v', '2', '5'), Variant::Type::BOOL, false);
  442. _insert_feature("character_variant_26", HB_TAG('c', 'v', '2', '6'), Variant::Type::BOOL, false);
  443. _insert_feature("character_variant_27", HB_TAG('c', 'v', '2', '7'), Variant::Type::BOOL, false);
  444. _insert_feature("character_variant_28", HB_TAG('c', 'v', '2', '8'), Variant::Type::BOOL, false);
  445. _insert_feature("character_variant_29", HB_TAG('c', 'v', '2', '9'), Variant::Type::BOOL, false);
  446. _insert_feature("character_variant_30", HB_TAG('c', 'v', '3', '0'), Variant::Type::BOOL, false);
  447. _insert_feature("character_variant_31", HB_TAG('c', 'v', '3', '1'), Variant::Type::BOOL, false);
  448. _insert_feature("character_variant_32", HB_TAG('c', 'v', '3', '2'), Variant::Type::BOOL, false);
  449. _insert_feature("character_variant_33", HB_TAG('c', 'v', '3', '3'), Variant::Type::BOOL, false);
  450. _insert_feature("character_variant_34", HB_TAG('c', 'v', '3', '4'), Variant::Type::BOOL, false);
  451. _insert_feature("character_variant_35", HB_TAG('c', 'v', '3', '5'), Variant::Type::BOOL, false);
  452. _insert_feature("character_variant_36", HB_TAG('c', 'v', '3', '6'), Variant::Type::BOOL, false);
  453. _insert_feature("character_variant_37", HB_TAG('c', 'v', '3', '7'), Variant::Type::BOOL, false);
  454. _insert_feature("character_variant_38", HB_TAG('c', 'v', '3', '8'), Variant::Type::BOOL, false);
  455. _insert_feature("character_variant_39", HB_TAG('c', 'v', '3', '9'), Variant::Type::BOOL, false);
  456. _insert_feature("character_variant_40", HB_TAG('c', 'v', '4', '0'), Variant::Type::BOOL, false);
  457. _insert_feature("character_variant_41", HB_TAG('c', 'v', '4', '1'), Variant::Type::BOOL, false);
  458. _insert_feature("character_variant_42", HB_TAG('c', 'v', '4', '2'), Variant::Type::BOOL, false);
  459. _insert_feature("character_variant_43", HB_TAG('c', 'v', '4', '3'), Variant::Type::BOOL, false);
  460. _insert_feature("character_variant_44", HB_TAG('c', 'v', '4', '4'), Variant::Type::BOOL, false);
  461. _insert_feature("character_variant_45", HB_TAG('c', 'v', '4', '5'), Variant::Type::BOOL, false);
  462. _insert_feature("character_variant_46", HB_TAG('c', 'v', '4', '6'), Variant::Type::BOOL, false);
  463. _insert_feature("character_variant_47", HB_TAG('c', 'v', '4', '7'), Variant::Type::BOOL, false);
  464. _insert_feature("character_variant_48", HB_TAG('c', 'v', '4', '8'), Variant::Type::BOOL, false);
  465. _insert_feature("character_variant_49", HB_TAG('c', 'v', '4', '9'), Variant::Type::BOOL, false);
  466. _insert_feature("character_variant_50", HB_TAG('c', 'v', '5', '0'), Variant::Type::BOOL, false);
  467. _insert_feature("character_variant_51", HB_TAG('c', 'v', '5', '1'), Variant::Type::BOOL, false);
  468. _insert_feature("character_variant_52", HB_TAG('c', 'v', '5', '2'), Variant::Type::BOOL, false);
  469. _insert_feature("character_variant_53", HB_TAG('c', 'v', '5', '3'), Variant::Type::BOOL, false);
  470. _insert_feature("character_variant_54", HB_TAG('c', 'v', '5', '4'), Variant::Type::BOOL, false);
  471. _insert_feature("character_variant_55", HB_TAG('c', 'v', '5', '5'), Variant::Type::BOOL, false);
  472. _insert_feature("character_variant_56", HB_TAG('c', 'v', '5', '6'), Variant::Type::BOOL, false);
  473. _insert_feature("character_variant_57", HB_TAG('c', 'v', '5', '7'), Variant::Type::BOOL, false);
  474. _insert_feature("character_variant_58", HB_TAG('c', 'v', '5', '8'), Variant::Type::BOOL, false);
  475. _insert_feature("character_variant_59", HB_TAG('c', 'v', '5', '9'), Variant::Type::BOOL, false);
  476. _insert_feature("character_variant_60", HB_TAG('c', 'v', '6', '0'), Variant::Type::BOOL, false);
  477. _insert_feature("character_variant_61", HB_TAG('c', 'v', '6', '1'), Variant::Type::BOOL, false);
  478. _insert_feature("character_variant_62", HB_TAG('c', 'v', '6', '2'), Variant::Type::BOOL, false);
  479. _insert_feature("character_variant_63", HB_TAG('c', 'v', '6', '3'), Variant::Type::BOOL, false);
  480. _insert_feature("character_variant_64", HB_TAG('c', 'v', '6', '4'), Variant::Type::BOOL, false);
  481. _insert_feature("character_variant_65", HB_TAG('c', 'v', '6', '5'), Variant::Type::BOOL, false);
  482. _insert_feature("character_variant_66", HB_TAG('c', 'v', '6', '6'), Variant::Type::BOOL, false);
  483. _insert_feature("character_variant_67", HB_TAG('c', 'v', '6', '7'), Variant::Type::BOOL, false);
  484. _insert_feature("character_variant_68", HB_TAG('c', 'v', '6', '8'), Variant::Type::BOOL, false);
  485. _insert_feature("character_variant_69", HB_TAG('c', 'v', '6', '9'), Variant::Type::BOOL, false);
  486. _insert_feature("character_variant_70", HB_TAG('c', 'v', '7', '0'), Variant::Type::BOOL, false);
  487. _insert_feature("character_variant_71", HB_TAG('c', 'v', '7', '1'), Variant::Type::BOOL, false);
  488. _insert_feature("character_variant_72", HB_TAG('c', 'v', '7', '2'), Variant::Type::BOOL, false);
  489. _insert_feature("character_variant_73", HB_TAG('c', 'v', '7', '3'), Variant::Type::BOOL, false);
  490. _insert_feature("character_variant_74", HB_TAG('c', 'v', '7', '4'), Variant::Type::BOOL, false);
  491. _insert_feature("character_variant_75", HB_TAG('c', 'v', '7', '5'), Variant::Type::BOOL, false);
  492. _insert_feature("character_variant_76", HB_TAG('c', 'v', '7', '6'), Variant::Type::BOOL, false);
  493. _insert_feature("character_variant_77", HB_TAG('c', 'v', '7', '7'), Variant::Type::BOOL, false);
  494. _insert_feature("character_variant_78", HB_TAG('c', 'v', '7', '8'), Variant::Type::BOOL, false);
  495. _insert_feature("character_variant_79", HB_TAG('c', 'v', '7', '9'), Variant::Type::BOOL, false);
  496. _insert_feature("character_variant_80", HB_TAG('c', 'v', '8', '0'), Variant::Type::BOOL, false);
  497. _insert_feature("character_variant_81", HB_TAG('c', 'v', '8', '1'), Variant::Type::BOOL, false);
  498. _insert_feature("character_variant_82", HB_TAG('c', 'v', '8', '2'), Variant::Type::BOOL, false);
  499. _insert_feature("character_variant_83", HB_TAG('c', 'v', '8', '3'), Variant::Type::BOOL, false);
  500. _insert_feature("character_variant_84", HB_TAG('c', 'v', '8', '4'), Variant::Type::BOOL, false);
  501. _insert_feature("character_variant_85", HB_TAG('c', 'v', '8', '5'), Variant::Type::BOOL, false);
  502. _insert_feature("character_variant_86", HB_TAG('c', 'v', '8', '6'), Variant::Type::BOOL, false);
  503. _insert_feature("character_variant_87", HB_TAG('c', 'v', '8', '7'), Variant::Type::BOOL, false);
  504. _insert_feature("character_variant_88", HB_TAG('c', 'v', '8', '8'), Variant::Type::BOOL, false);
  505. _insert_feature("character_variant_89", HB_TAG('c', 'v', '8', '9'), Variant::Type::BOOL, false);
  506. _insert_feature("character_variant_90", HB_TAG('c', 'v', '9', '0'), Variant::Type::BOOL, false);
  507. _insert_feature("character_variant_91", HB_TAG('c', 'v', '9', '1'), Variant::Type::BOOL, false);
  508. _insert_feature("character_variant_92", HB_TAG('c', 'v', '9', '2'), Variant::Type::BOOL, false);
  509. _insert_feature("character_variant_93", HB_TAG('c', 'v', '9', '3'), Variant::Type::BOOL, false);
  510. _insert_feature("character_variant_94", HB_TAG('c', 'v', '9', '4'), Variant::Type::BOOL, false);
  511. _insert_feature("character_variant_95", HB_TAG('c', 'v', '9', '5'), Variant::Type::BOOL, false);
  512. _insert_feature("character_variant_96", HB_TAG('c', 'v', '9', '6'), Variant::Type::BOOL, false);
  513. _insert_feature("character_variant_97", HB_TAG('c', 'v', '9', '7'), Variant::Type::BOOL, false);
  514. _insert_feature("character_variant_98", HB_TAG('c', 'v', '9', '8'), Variant::Type::BOOL, false);
  515. _insert_feature("character_variant_99", HB_TAG('c', 'v', '9', '9'), Variant::Type::BOOL, false);
  516. _insert_feature("petite_capitals_from_capitals", HB_TAG('c', '2', 'p', 'c'), Variant::Type::BOOL, false);
  517. _insert_feature("small_capitals_from_capitals", HB_TAG('c', '2', 's', 'c'), Variant::Type::BOOL, false);
  518. _insert_feature("distances", HB_TAG('d', 'i', 's', 't'), Variant::Type::INT, true);
  519. _insert_feature("discretionary_ligatures", HB_TAG('d', 'l', 'i', 'g'), Variant::Type::BOOL, false);
  520. _insert_feature("denominators", HB_TAG('d', 'n', 'o', 'm'), Variant::Type::BOOL, false);
  521. _insert_feature("dotless_forms", HB_TAG('d', 't', 'l', 's'), Variant::Type::INT, true);
  522. _insert_feature("expert_forms", HB_TAG('e', 'x', 'p', 't'), Variant::Type::BOOL, true);
  523. _insert_feature("final_glyph_on_line_alternates", HB_TAG('f', 'a', 'l', 't'), Variant::Type::INT, false);
  524. _insert_feature("terminal_forms_2", HB_TAG('f', 'i', 'n', '2'), Variant::Type::INT, true);
  525. _insert_feature("terminal_forms_3", HB_TAG('f', 'i', 'n', '3'), Variant::Type::INT, true);
  526. _insert_feature("terminal_forms", HB_TAG('f', 'i', 'n', 'a'), Variant::Type::INT, true);
  527. _insert_feature("flattened_accent_forms", HB_TAG('f', 'l', 'a', 'c'), Variant::Type::INT, true);
  528. _insert_feature("fractions", HB_TAG('f', 'r', 'a', 'c'), Variant::Type::BOOL, false);
  529. _insert_feature("full_widths", HB_TAG('f', 'w', 'i', 'd'), Variant::Type::BOOL, false);
  530. _insert_feature("half_forms", HB_TAG('h', 'a', 'l', 'f'), Variant::Type::INT, true);
  531. _insert_feature("halant_forms", HB_TAG('h', 'a', 'l', 'n'), Variant::Type::INT, true);
  532. _insert_feature("alternate_half_widths", HB_TAG('h', 'a', 'l', 't'), Variant::Type::BOOL, false);
  533. _insert_feature("historical_forms", HB_TAG('h', 'i', 's', 't'), Variant::Type::INT, false);
  534. _insert_feature("horizontal_kana_alternates", HB_TAG('h', 'k', 'n', 'a'), Variant::Type::BOOL, false);
  535. _insert_feature("historical_ligatures", HB_TAG('h', 'l', 'i', 'g'), Variant::Type::BOOL, false);
  536. _insert_feature("hangul", HB_TAG('h', 'n', 'g', 'l'), Variant::Type::INT, false);
  537. _insert_feature("hojo_kanji_forms", HB_TAG('h', 'o', 'j', 'o'), Variant::Type::INT, false);
  538. _insert_feature("half_widths", HB_TAG('h', 'w', 'i', 'd'), Variant::Type::BOOL, false);
  539. _insert_feature("initial_forms", HB_TAG('i', 'n', 'i', 't'), Variant::Type::INT, true);
  540. _insert_feature("isolated_forms", HB_TAG('i', 's', 'o', 'l'), Variant::Type::INT, true);
  541. _insert_feature("italics", HB_TAG('i', 't', 'a', 'l'), Variant::Type::INT, false);
  542. _insert_feature("justification_alternates", HB_TAG('j', 'a', 'l', 't'), Variant::Type::INT, false);
  543. _insert_feature("jis78_forms", HB_TAG('j', 'p', '7', '8'), Variant::Type::INT, false);
  544. _insert_feature("jis83_forms", HB_TAG('j', 'p', '8', '3'), Variant::Type::INT, false);
  545. _insert_feature("jis90_forms", HB_TAG('j', 'p', '9', '0'), Variant::Type::INT, false);
  546. _insert_feature("jis2004_forms", HB_TAG('j', 'p', '0', '4'), Variant::Type::INT, false);
  547. _insert_feature("kerning", HB_TAG('k', 'e', 'r', 'n'), Variant::Type::BOOL, false);
  548. _insert_feature("left_bounds", HB_TAG('l', 'f', 'b', 'd'), Variant::Type::INT, false);
  549. _insert_feature("standard_ligatures", HB_TAG('l', 'i', 'g', 'a'), Variant::Type::BOOL, false);
  550. _insert_feature("leading_jamo_forms", HB_TAG('l', 'j', 'm', 'o'), Variant::Type::INT, true);
  551. _insert_feature("lining_figures", HB_TAG('l', 'n', 'u', 'm'), Variant::Type::INT, false);
  552. _insert_feature("localized_forms", HB_TAG('l', 'o', 'c', 'l'), Variant::Type::INT, true);
  553. _insert_feature("left_to_right_alternates", HB_TAG('l', 't', 'r', 'a'), Variant::Type::INT, true);
  554. _insert_feature("left_to_right_mirrored_forms", HB_TAG('l', 't', 'r', 'm'), Variant::Type::INT, true);
  555. _insert_feature("mark_positioning", HB_TAG('m', 'a', 'r', 'k'), Variant::Type::INT, true);
  556. _insert_feature("medial_forms_2", HB_TAG('m', 'e', 'd', '2'), Variant::Type::INT, true);
  557. _insert_feature("medial_forms", HB_TAG('m', 'e', 'd', 'i'), Variant::Type::INT, true);
  558. _insert_feature("mathematical_greek", HB_TAG('m', 'g', 'r', 'k'), Variant::Type::BOOL, false);
  559. _insert_feature("mark_to_mark_positioning", HB_TAG('m', 'k', 'm', 'k'), Variant::Type::INT, true);
  560. _insert_feature("mark_positioning_via_substitution", HB_TAG('m', 's', 'e', 't'), Variant::Type::INT, true);
  561. _insert_feature("alternate_annotation_forms", HB_TAG('n', 'a', 'l', 't'), Variant::Type::INT, false);
  562. _insert_feature("nlc_kanji_forms", HB_TAG('n', 'l', 'c', 'k'), Variant::Type::INT, false);
  563. _insert_feature("nukta_forms", HB_TAG('n', 'u', 'k', 't'), Variant::Type::INT, true);
  564. _insert_feature("numerators", HB_TAG('n', 'u', 'm', 'r'), Variant::Type::BOOL, false);
  565. _insert_feature("oldstyle_figures", HB_TAG('o', 'n', 'u', 'm'), Variant::Type::INT, false);
  566. _insert_feature("optical_bounds", HB_TAG('o', 'p', 'b', 'd'), Variant::Type::INT, true);
  567. _insert_feature("ordinals", HB_TAG('o', 'r', 'd', 'n'), Variant::Type::BOOL, false);
  568. _insert_feature("ornaments", HB_TAG('o', 'r', 'n', 'm'), Variant::Type::INT, false);
  569. _insert_feature("proportional_alternate_widths", HB_TAG('p', 'a', 'l', 't'), Variant::Type::BOOL, false);
  570. _insert_feature("petite_capitals", HB_TAG('p', 'c', 'a', 'p'), Variant::Type::BOOL, false);
  571. _insert_feature("proportional_kana", HB_TAG('p', 'k', 'n', 'a'), Variant::Type::BOOL, false);
  572. _insert_feature("proportional_figures", HB_TAG('p', 'n', 'u', 'm'), Variant::Type::BOOL, false);
  573. _insert_feature("pre_base_forms", HB_TAG('p', 'r', 'e', 'f'), Variant::Type::INT, true);
  574. _insert_feature("pre_base_substitutions", HB_TAG('p', 'r', 'e', 's'), Variant::Type::INT, true);
  575. _insert_feature("post_base_forms", HB_TAG('p', 's', 't', 'f'), Variant::Type::INT, true);
  576. _insert_feature("post_base_substitutions", HB_TAG('p', 's', 't', 's'), Variant::Type::INT, true);
  577. _insert_feature("proportional_widths", HB_TAG('p', 'w', 'i', 'd'), Variant::Type::BOOL, false);
  578. _insert_feature("quarter_widths", HB_TAG('q', 'w', 'i', 'd'), Variant::Type::BOOL, false);
  579. _insert_feature("randomize", HB_TAG('r', 'a', 'n', 'd'), Variant::Type::INT, false);
  580. _insert_feature("required_contextual_alternates", HB_TAG('r', 'c', 'l', 't'), Variant::Type::BOOL, true);
  581. _insert_feature("rakar_forms", HB_TAG('r', 'k', 'r', 'f'), Variant::Type::INT, true);
  582. _insert_feature("required_ligatures", HB_TAG('r', 'l', 'i', 'g'), Variant::Type::BOOL, true);
  583. _insert_feature("reph_forms", HB_TAG('r', 'p', 'h', 'f'), Variant::Type::INT, true);
  584. _insert_feature("right_bounds", HB_TAG('r', 't', 'b', 'd'), Variant::Type::INT, false);
  585. _insert_feature("right_to_left_alternates", HB_TAG('r', 't', 'l', 'a'), Variant::Type::INT, true);
  586. _insert_feature("right_to_left_mirrored_forms", HB_TAG('r', 't', 'l', 'm'), Variant::Type::INT, true);
  587. _insert_feature("ruby_notation_forms", HB_TAG('r', 'u', 'b', 'y'), Variant::Type::INT, false);
  588. _insert_feature("required_variation_alternates", HB_TAG('r', 'v', 'r', 'n'), Variant::Type::INT, true);
  589. _insert_feature("stylistic_alternates", HB_TAG('s', 'a', 'l', 't'), Variant::Type::INT, false);
  590. _insert_feature("scientific_inferiors", HB_TAG('s', 'i', 'n', 'f'), Variant::Type::BOOL, false);
  591. _insert_feature("optical_size", HB_TAG('s', 'i', 'z', 'e'), Variant::Type::INT, false);
  592. _insert_feature("small_capitals", HB_TAG('s', 'm', 'c', 'p'), Variant::Type::BOOL, false);
  593. _insert_feature("simplified_forms", HB_TAG('s', 'm', 'p', 'l'), Variant::Type::INT, false);
  594. _insert_feature("stylistic_set_01", HB_TAG('s', 's', '0', '1'), Variant::Type::BOOL, false);
  595. _insert_feature("stylistic_set_02", HB_TAG('s', 's', '0', '2'), Variant::Type::BOOL, false);
  596. _insert_feature("stylistic_set_03", HB_TAG('s', 's', '0', '3'), Variant::Type::BOOL, false);
  597. _insert_feature("stylistic_set_04", HB_TAG('s', 's', '0', '4'), Variant::Type::BOOL, false);
  598. _insert_feature("stylistic_set_05", HB_TAG('s', 's', '0', '5'), Variant::Type::BOOL, false);
  599. _insert_feature("stylistic_set_06", HB_TAG('s', 's', '0', '6'), Variant::Type::BOOL, false);
  600. _insert_feature("stylistic_set_07", HB_TAG('s', 's', '0', '7'), Variant::Type::BOOL, false);
  601. _insert_feature("stylistic_set_08", HB_TAG('s', 's', '0', '8'), Variant::Type::BOOL, false);
  602. _insert_feature("stylistic_set_09", HB_TAG('s', 's', '0', '9'), Variant::Type::BOOL, false);
  603. _insert_feature("stylistic_set_10", HB_TAG('s', 's', '1', '0'), Variant::Type::BOOL, false);
  604. _insert_feature("stylistic_set_11", HB_TAG('s', 's', '1', '1'), Variant::Type::BOOL, false);
  605. _insert_feature("stylistic_set_12", HB_TAG('s', 's', '1', '2'), Variant::Type::BOOL, false);
  606. _insert_feature("stylistic_set_13", HB_TAG('s', 's', '1', '3'), Variant::Type::BOOL, false);
  607. _insert_feature("stylistic_set_14", HB_TAG('s', 's', '1', '4'), Variant::Type::BOOL, false);
  608. _insert_feature("stylistic_set_15", HB_TAG('s', 's', '1', '5'), Variant::Type::BOOL, false);
  609. _insert_feature("stylistic_set_16", HB_TAG('s', 's', '1', '6'), Variant::Type::BOOL, false);
  610. _insert_feature("stylistic_set_17", HB_TAG('s', 's', '1', '7'), Variant::Type::BOOL, false);
  611. _insert_feature("stylistic_set_18", HB_TAG('s', 's', '1', '8'), Variant::Type::BOOL, false);
  612. _insert_feature("stylistic_set_19", HB_TAG('s', 's', '1', '9'), Variant::Type::BOOL, false);
  613. _insert_feature("stylistic_set_20", HB_TAG('s', 's', '2', '0'), Variant::Type::BOOL, false);
  614. _insert_feature("math_script_style_alternates", HB_TAG('s', 's', 't', 'y'), Variant::Type::INT, true);
  615. _insert_feature("stretching_glyph_decomposition", HB_TAG('s', 't', 'c', 'h'), Variant::Type::INT, true);
  616. _insert_feature("subscript", HB_TAG('s', 'u', 'b', 's'), Variant::Type::BOOL, false);
  617. _insert_feature("superscript", HB_TAG('s', 'u', 'p', 's'), Variant::Type::BOOL, false);
  618. _insert_feature("swash", HB_TAG('s', 'w', 's', 'h'), Variant::Type::INT, false);
  619. _insert_feature("titling", HB_TAG('t', 'i', 't', 'l'), Variant::Type::BOOL, false);
  620. _insert_feature("trailing_jamo_forms", HB_TAG('t', 'j', 'm', 'o'), Variant::Type::INT, true);
  621. _insert_feature("traditional_name_forms", HB_TAG('t', 'n', 'a', 'm'), Variant::Type::INT, false);
  622. _insert_feature("tabular_figures", HB_TAG('t', 'n', 'u', 'm'), Variant::Type::BOOL, false);
  623. _insert_feature("traditional_forms", HB_TAG('t', 'r', 'a', 'd'), Variant::Type::INT, false);
  624. _insert_feature("third_widths", HB_TAG('t', 'w', 'i', 'd'), Variant::Type::BOOL, false);
  625. _insert_feature("unicase", HB_TAG('u', 'n', 'i', 'c'), Variant::Type::BOOL, false);
  626. _insert_feature("alternate_vertical_metrics", HB_TAG('v', 'a', 'l', 't'), Variant::Type::INT, false);
  627. _insert_feature("vattu_variants", HB_TAG('v', 'a', 't', 'u'), Variant::Type::INT, true);
  628. _insert_feature("vertical_contextual_half_width_spacing", HB_TAG('v', 'c', 'h', 'w'), Variant::Type::BOOL, false);
  629. _insert_feature("vertical_alternates", HB_TAG('v', 'e', 'r', 't'), Variant::Type::INT, false);
  630. _insert_feature("alternate_vertical_half_metrics", HB_TAG('v', 'h', 'a', 'l'), Variant::Type::BOOL, false);
  631. _insert_feature("vowel_jamo_forms", HB_TAG('v', 'j', 'm', 'o'), Variant::Type::INT, true);
  632. _insert_feature("vertical_kana_alternates", HB_TAG('v', 'k', 'n', 'a'), Variant::Type::INT, false);
  633. _insert_feature("vertical_kerning", HB_TAG('v', 'k', 'r', 'n'), Variant::Type::BOOL, false);
  634. _insert_feature("proportional_alternate_vertical_metrics", HB_TAG('v', 'p', 'a', 'l'), Variant::Type::BOOL, false);
  635. _insert_feature("vertical_alternates_and_rotation", HB_TAG('v', 'r', 't', '2'), Variant::Type::INT, false);
  636. _insert_feature("vertical_alternates_for_rotation", HB_TAG('v', 'r', 't', 'r'), Variant::Type::INT, false);
  637. _insert_feature("slashed_zero", HB_TAG('z', 'e', 'r', 'o'), Variant::Type::BOOL, false);
  638. // Registered OpenType variation tag.
  639. _insert_feature("italic", HB_TAG('i', 't', 'a', 'l'), Variant::Type::INT, false);
  640. _insert_feature("optical_size", HB_TAG('o', 'p', 's', 'z'), Variant::Type::INT, false);
  641. _insert_feature("slant", HB_TAG('s', 'l', 'n', 't'), Variant::Type::INT, false);
  642. _insert_feature("width", HB_TAG('w', 'd', 't', 'h'), Variant::Type::INT, false);
  643. _insert_feature("weight", HB_TAG('w', 'g', 'h', 't'), Variant::Type::INT, false);
  644. }
  645. int64_t TextServerAdvanced::name_to_tag(const String &p_name) const {
  646. if (feature_sets.has(p_name)) {
  647. return feature_sets[p_name];
  648. }
  649. // No readable name, use tag string.
  650. return hb_tag_from_string(p_name.replace("custom_", "").ascii().get_data(), -1);
  651. }
  652. Variant::Type TextServerAdvanced::_get_tag_type(int64_t p_tag) const {
  653. if (feature_sets_inv.has(p_tag)) {
  654. return feature_sets_inv[p_tag].vtype;
  655. }
  656. return Variant::Type::INT;
  657. }
  658. bool TextServerAdvanced::_get_tag_hidden(int64_t p_tag) const {
  659. if (feature_sets_inv.has(p_tag)) {
  660. return feature_sets_inv[p_tag].hidden;
  661. }
  662. return false;
  663. }
  664. String TextServerAdvanced::tag_to_name(int64_t p_tag) const {
  665. if (feature_sets_inv.has(p_tag)) {
  666. return feature_sets_inv[p_tag].name;
  667. }
  668. // No readable name, use tag string.
  669. char name[5];
  670. memset(name, 0, 5);
  671. hb_tag_to_string(p_tag, name);
  672. return String("custom_") + String(name);
  673. }
  674. /*************************************************************************/
  675. /* Font Glyph Rendering */
  676. /*************************************************************************/
  677. _FORCE_INLINE_ TextServerAdvanced::FontTexturePosition TextServerAdvanced::find_texture_pos_for_glyph(FontForSizeAdvanced *p_data, int p_color_size, Image::Format p_image_format, int p_width, int p_height, bool p_msdf) const {
  678. FontTexturePosition ret;
  679. ret.index = -1;
  680. int mw = p_width;
  681. int mh = p_height;
  682. for (int i = 0; i < p_data->textures.size(); i++) {
  683. const FontTexture &ct = p_data->textures[i];
  684. if (p_image_format != ct.format) {
  685. continue;
  686. }
  687. if (mw > ct.texture_w || mh > ct.texture_h) { // Too big for this texture.
  688. continue;
  689. }
  690. if (ct.offsets.size() < ct.texture_w) {
  691. continue;
  692. }
  693. ret.y = 0x7fffffff;
  694. ret.x = 0;
  695. for (int j = 0; j < ct.texture_w - mw; j++) {
  696. int max_y = 0;
  697. for (int k = j; k < j + mw; k++) {
  698. int y = ct.offsets[k];
  699. if (y > max_y) {
  700. max_y = y;
  701. }
  702. }
  703. if (max_y < ret.y) {
  704. ret.y = max_y;
  705. ret.x = j;
  706. }
  707. }
  708. if (ret.y == 0x7fffffff || ret.y + mh > ct.texture_h) {
  709. continue; // Fail, could not fit it here.
  710. }
  711. ret.index = i;
  712. break;
  713. }
  714. if (ret.index == -1) {
  715. // Could not find texture to fit, create one.
  716. ret.x = 0;
  717. ret.y = 0;
  718. int texsize = MAX(p_data->size.x * p_data->oversampling * 8, 256);
  719. #ifdef GDEXTENSION
  720. texsize = Math::next_power_of_2(texsize);
  721. #else
  722. texsize = next_power_of_2(texsize);
  723. #endif
  724. if (p_msdf) {
  725. texsize = MIN(texsize, 2048);
  726. } else {
  727. texsize = MIN(texsize, 1024);
  728. }
  729. if (mw > texsize) { // Special case, adapt to it?
  730. #ifdef GDEXTENSION
  731. texsize = Math::next_power_of_2(mw);
  732. #else
  733. texsize = next_power_of_2(mw);
  734. #endif
  735. }
  736. if (mh > texsize) { // Special case, adapt to it?
  737. #ifdef GDEXTENSION
  738. texsize = Math::next_power_of_2(mh);
  739. #else
  740. texsize = next_power_of_2(mh);
  741. #endif
  742. }
  743. FontTexture tex;
  744. tex.texture_w = texsize;
  745. tex.texture_h = texsize;
  746. tex.format = p_image_format;
  747. tex.imgdata.resize(texsize * texsize * p_color_size);
  748. {
  749. // Zero texture.
  750. uint8_t *w = tex.imgdata.ptrw();
  751. ERR_FAIL_COND_V(texsize * texsize * p_color_size > tex.imgdata.size(), ret);
  752. // Initialize the texture to all-white pixels to prevent artifacts when the
  753. // font is displayed at a non-default scale with filtering enabled.
  754. if (p_color_size == 2) {
  755. for (int i = 0; i < texsize * texsize * p_color_size; i += 2) { // FORMAT_LA8, BW font.
  756. w[i + 0] = 255;
  757. w[i + 1] = 0;
  758. }
  759. } else if (p_color_size == 4) {
  760. for (int i = 0; i < texsize * texsize * p_color_size; i += 4) { // FORMAT_RGBA8, Color font, Multichannel(+True) SDF.
  761. w[i + 0] = 255;
  762. w[i + 1] = 255;
  763. w[i + 2] = 255;
  764. w[i + 3] = 0;
  765. }
  766. } else {
  767. ERR_FAIL_V(ret);
  768. }
  769. }
  770. tex.offsets.resize(texsize);
  771. int32_t *offw = tex.offsets.ptrw();
  772. for (int i = 0; i < texsize; i++) { // Zero offsets.
  773. offw[i] = 0;
  774. }
  775. p_data->textures.push_back(tex);
  776. ret.index = p_data->textures.size() - 1;
  777. }
  778. return ret;
  779. }
  780. #ifdef MODULE_MSDFGEN_ENABLED
  781. struct MSContext {
  782. msdfgen::Point2 position;
  783. msdfgen::Shape *shape = nullptr;
  784. msdfgen::Contour *contour = nullptr;
  785. };
  786. class DistancePixelConversion {
  787. double invRange;
  788. public:
  789. _FORCE_INLINE_ explicit DistancePixelConversion(double range) :
  790. invRange(1 / range) {}
  791. _FORCE_INLINE_ void operator()(float *pixels, const msdfgen::MultiAndTrueDistance &distance) const {
  792. pixels[0] = float(invRange * distance.r + .5);
  793. pixels[1] = float(invRange * distance.g + .5);
  794. pixels[2] = float(invRange * distance.b + .5);
  795. pixels[3] = float(invRange * distance.a + .5);
  796. }
  797. };
  798. struct MSDFThreadData {
  799. msdfgen::Bitmap<float, 4> *output;
  800. msdfgen::Shape *shape;
  801. msdfgen::Projection *projection;
  802. DistancePixelConversion *distancePixelConversion;
  803. };
  804. static msdfgen::Point2 ft_point2(const FT_Vector &vector) {
  805. return msdfgen::Point2(vector.x / 60.0f, vector.y / 60.0f);
  806. }
  807. static int ft_move_to(const FT_Vector *to, void *user) {
  808. MSContext *context = static_cast<MSContext *>(user);
  809. if (!(context->contour && context->contour->edges.empty())) {
  810. context->contour = &context->shape->addContour();
  811. }
  812. context->position = ft_point2(*to);
  813. return 0;
  814. }
  815. static int ft_line_to(const FT_Vector *to, void *user) {
  816. MSContext *context = static_cast<MSContext *>(user);
  817. msdfgen::Point2 endpoint = ft_point2(*to);
  818. if (endpoint != context->position) {
  819. context->contour->addEdge(new msdfgen::LinearSegment(context->position, endpoint));
  820. context->position = endpoint;
  821. }
  822. return 0;
  823. }
  824. static int ft_conic_to(const FT_Vector *control, const FT_Vector *to, void *user) {
  825. MSContext *context = static_cast<MSContext *>(user);
  826. context->contour->addEdge(new msdfgen::QuadraticSegment(context->position, ft_point2(*control), ft_point2(*to)));
  827. context->position = ft_point2(*to);
  828. return 0;
  829. }
  830. static int ft_cubic_to(const FT_Vector *control1, const FT_Vector *control2, const FT_Vector *to, void *user) {
  831. MSContext *context = static_cast<MSContext *>(user);
  832. context->contour->addEdge(new msdfgen::CubicSegment(context->position, ft_point2(*control1), ft_point2(*control2), ft_point2(*to)));
  833. context->position = ft_point2(*to);
  834. return 0;
  835. }
  836. void TextServerAdvanced::_generateMTSDF_threaded(uint32_t y, void *p_td) const {
  837. MSDFThreadData *td = static_cast<MSDFThreadData *>(p_td);
  838. msdfgen::ShapeDistanceFinder<msdfgen::OverlappingContourCombiner<msdfgen::MultiAndTrueDistanceSelector>> distanceFinder(*td->shape);
  839. int row = td->shape->inverseYAxis ? td->output->height() - y - 1 : y;
  840. for (int col = 0; col < td->output->width(); ++col) {
  841. int x = (y % 2) ? td->output->width() - col - 1 : col;
  842. msdfgen::Point2 p = td->projection->unproject(msdfgen::Point2(x + .5, y + .5));
  843. msdfgen::MultiAndTrueDistance distance = distanceFinder.distance(p);
  844. td->distancePixelConversion->operator()(td->output->operator()(x, row), distance);
  845. }
  846. }
  847. _FORCE_INLINE_ TextServerAdvanced::FontGlyph TextServerAdvanced::rasterize_msdf(FontAdvanced *p_font_data, FontForSizeAdvanced *p_data, int p_pixel_range, int p_rect_margin, FT_Outline *outline, const Vector2 &advance) const {
  848. msdfgen::Shape shape;
  849. shape.contours.clear();
  850. shape.inverseYAxis = false;
  851. MSContext context = {};
  852. context.shape = &shape;
  853. FT_Outline_Funcs ft_functions;
  854. ft_functions.move_to = &ft_move_to;
  855. ft_functions.line_to = &ft_line_to;
  856. ft_functions.conic_to = &ft_conic_to;
  857. ft_functions.cubic_to = &ft_cubic_to;
  858. ft_functions.shift = 0;
  859. ft_functions.delta = 0;
  860. int error = FT_Outline_Decompose(outline, &ft_functions, &context);
  861. ERR_FAIL_COND_V_MSG(error, FontGlyph(), "FreeType: Outline decomposition error: '" + String(FT_Error_String(error)) + "'.");
  862. if (!shape.contours.empty() && shape.contours.back().edges.empty()) {
  863. shape.contours.pop_back();
  864. }
  865. if (FT_Outline_Get_Orientation(outline) == 1) {
  866. for (int i = 0; i < (int)shape.contours.size(); ++i) {
  867. shape.contours[i].reverse();
  868. }
  869. }
  870. shape.inverseYAxis = true;
  871. shape.normalize();
  872. msdfgen::Shape::Bounds bounds = shape.getBounds(p_pixel_range);
  873. FontGlyph chr;
  874. chr.found = true;
  875. chr.advance = advance;
  876. if (shape.validate() && shape.contours.size() > 0) {
  877. int w = (bounds.r - bounds.l);
  878. int h = (bounds.t - bounds.b);
  879. int mw = w + p_rect_margin * 4;
  880. int mh = h + p_rect_margin * 4;
  881. ERR_FAIL_COND_V(mw > 4096, FontGlyph());
  882. ERR_FAIL_COND_V(mh > 4096, FontGlyph());
  883. FontTexturePosition tex_pos = find_texture_pos_for_glyph(p_data, 4, Image::FORMAT_RGBA8, mw, mh, true);
  884. ERR_FAIL_COND_V(tex_pos.index < 0, FontGlyph());
  885. FontTexture &tex = p_data->textures.write[tex_pos.index];
  886. edgeColoringSimple(shape, 3.0); // Max. angle.
  887. msdfgen::Bitmap<float, 4> image(w, h); // Texture size.
  888. DistancePixelConversion distancePixelConversion(p_pixel_range);
  889. msdfgen::Projection projection(msdfgen::Vector2(1.0, 1.0), msdfgen::Vector2(-bounds.l, -bounds.b));
  890. msdfgen::MSDFGeneratorConfig config(true, msdfgen::ErrorCorrectionConfig());
  891. MSDFThreadData td;
  892. td.output = &image;
  893. td.shape = &shape;
  894. td.projection = &projection;
  895. td.distancePixelConversion = &distancePixelConversion;
  896. WorkerThreadPool::GroupID group_task = WorkerThreadPool::get_singleton()->add_template_group_task(this, &TextServerAdvanced::_generateMTSDF_threaded, &td, h, -1, true, SNAME("FontServerRasterizeMSDF"));
  897. WorkerThreadPool::get_singleton()->wait_for_group_task_completion(group_task);
  898. msdfgen::msdfErrorCorrection(image, shape, projection, p_pixel_range, config);
  899. {
  900. uint8_t *wr = tex.imgdata.ptrw();
  901. for (int i = 0; i < h; i++) {
  902. for (int j = 0; j < w; j++) {
  903. int ofs = ((i + tex_pos.y + p_rect_margin * 2) * tex.texture_w + j + tex_pos.x + p_rect_margin * 2) * 4;
  904. ERR_FAIL_COND_V(ofs >= tex.imgdata.size(), FontGlyph());
  905. wr[ofs + 0] = (uint8_t)(CLAMP(image(j, i)[0] * 256.f, 0.f, 255.f));
  906. wr[ofs + 1] = (uint8_t)(CLAMP(image(j, i)[1] * 256.f, 0.f, 255.f));
  907. wr[ofs + 2] = (uint8_t)(CLAMP(image(j, i)[2] * 256.f, 0.f, 255.f));
  908. wr[ofs + 3] = (uint8_t)(CLAMP(image(j, i)[3] * 256.f, 0.f, 255.f));
  909. }
  910. }
  911. }
  912. tex.dirty = true;
  913. // Update height array.
  914. int32_t *offw = tex.offsets.ptrw();
  915. for (int k = tex_pos.x; k < tex_pos.x + mw; k++) {
  916. offw[k] = tex_pos.y + mh;
  917. }
  918. chr.texture_idx = tex_pos.index;
  919. chr.uv_rect = Rect2(tex_pos.x + p_rect_margin, tex_pos.y + p_rect_margin, w + p_rect_margin * 2, h + p_rect_margin * 2);
  920. chr.rect.position = Vector2(bounds.l - p_rect_margin, -bounds.t - p_rect_margin);
  921. chr.rect.size = chr.uv_rect.size;
  922. }
  923. return chr;
  924. }
  925. #endif
  926. #ifdef MODULE_FREETYPE_ENABLED
  927. _FORCE_INLINE_ TextServerAdvanced::FontGlyph TextServerAdvanced::rasterize_bitmap(FontForSizeAdvanced *p_data, int p_rect_margin, FT_Bitmap bitmap, int yofs, int xofs, const Vector2 &advance, bool p_bgra) const {
  928. int w = bitmap.width;
  929. int h = bitmap.rows;
  930. int color_size = 2;
  931. switch (bitmap.pixel_mode) {
  932. case FT_PIXEL_MODE_MONO:
  933. case FT_PIXEL_MODE_GRAY: {
  934. color_size = 2;
  935. } break;
  936. case FT_PIXEL_MODE_BGRA: {
  937. color_size = 4;
  938. } break;
  939. case FT_PIXEL_MODE_LCD: {
  940. color_size = 4;
  941. w /= 3;
  942. } break;
  943. case FT_PIXEL_MODE_LCD_V: {
  944. color_size = 4;
  945. h /= 3;
  946. } break;
  947. }
  948. int mw = w + p_rect_margin * 4;
  949. int mh = h + p_rect_margin * 4;
  950. ERR_FAIL_COND_V(mw > 4096, FontGlyph());
  951. ERR_FAIL_COND_V(mh > 4096, FontGlyph());
  952. Image::Format require_format = color_size == 4 ? Image::FORMAT_RGBA8 : Image::FORMAT_LA8;
  953. FontTexturePosition tex_pos = find_texture_pos_for_glyph(p_data, color_size, require_format, mw, mh, false);
  954. ERR_FAIL_COND_V(tex_pos.index < 0, FontGlyph());
  955. // Fit character in char texture.
  956. FontTexture &tex = p_data->textures.write[tex_pos.index];
  957. {
  958. uint8_t *wr = tex.imgdata.ptrw();
  959. for (int i = 0; i < h; i++) {
  960. for (int j = 0; j < w; j++) {
  961. int ofs = ((i + tex_pos.y + p_rect_margin * 2) * tex.texture_w + j + tex_pos.x + p_rect_margin * 2) * color_size;
  962. ERR_FAIL_COND_V(ofs >= tex.imgdata.size(), FontGlyph());
  963. switch (bitmap.pixel_mode) {
  964. case FT_PIXEL_MODE_MONO: {
  965. int byte = i * bitmap.pitch + (j >> 3);
  966. int bit = 1 << (7 - (j % 8));
  967. wr[ofs + 0] = 255; // grayscale as 1
  968. wr[ofs + 1] = (bitmap.buffer[byte] & bit) ? 255 : 0;
  969. } break;
  970. case FT_PIXEL_MODE_GRAY:
  971. wr[ofs + 0] = 255; // grayscale as 1
  972. wr[ofs + 1] = bitmap.buffer[i * bitmap.pitch + j];
  973. break;
  974. case FT_PIXEL_MODE_BGRA: {
  975. int ofs_color = i * bitmap.pitch + (j << 2);
  976. wr[ofs + 2] = bitmap.buffer[ofs_color + 0];
  977. wr[ofs + 1] = bitmap.buffer[ofs_color + 1];
  978. wr[ofs + 0] = bitmap.buffer[ofs_color + 2];
  979. wr[ofs + 3] = bitmap.buffer[ofs_color + 3];
  980. } break;
  981. case FT_PIXEL_MODE_LCD: {
  982. int ofs_color = i * bitmap.pitch + (j * 3);
  983. if (p_bgra) {
  984. wr[ofs + 0] = bitmap.buffer[ofs_color + 0];
  985. wr[ofs + 1] = bitmap.buffer[ofs_color + 1];
  986. wr[ofs + 2] = bitmap.buffer[ofs_color + 2];
  987. wr[ofs + 3] = 255;
  988. } else {
  989. wr[ofs + 0] = bitmap.buffer[ofs_color + 2];
  990. wr[ofs + 1] = bitmap.buffer[ofs_color + 1];
  991. wr[ofs + 2] = bitmap.buffer[ofs_color + 0];
  992. wr[ofs + 3] = 255;
  993. }
  994. } break;
  995. case FT_PIXEL_MODE_LCD_V: {
  996. int ofs_color = i * bitmap.pitch * 3 + j;
  997. if (p_bgra) {
  998. wr[ofs + 0] = bitmap.buffer[ofs_color + bitmap.pitch * 2];
  999. wr[ofs + 1] = bitmap.buffer[ofs_color + bitmap.pitch];
  1000. wr[ofs + 2] = bitmap.buffer[ofs_color + 0];
  1001. wr[ofs + 3] = 255;
  1002. } else {
  1003. wr[ofs + 0] = bitmap.buffer[ofs_color + 0];
  1004. wr[ofs + 1] = bitmap.buffer[ofs_color + bitmap.pitch];
  1005. wr[ofs + 2] = bitmap.buffer[ofs_color + bitmap.pitch * 2];
  1006. wr[ofs + 3] = 255;
  1007. }
  1008. } break;
  1009. default:
  1010. ERR_FAIL_V_MSG(FontGlyph(), "Font uses unsupported pixel format: " + String::num_int64(bitmap.pixel_mode) + ".");
  1011. break;
  1012. }
  1013. }
  1014. }
  1015. }
  1016. tex.dirty = true;
  1017. // Update height array.
  1018. int32_t *offw = tex.offsets.ptrw();
  1019. for (int k = tex_pos.x; k < tex_pos.x + mw; k++) {
  1020. offw[k] = tex_pos.y + mh;
  1021. }
  1022. FontGlyph chr;
  1023. chr.advance = advance * p_data->scale / p_data->oversampling;
  1024. chr.texture_idx = tex_pos.index;
  1025. chr.found = true;
  1026. chr.uv_rect = Rect2(tex_pos.x + p_rect_margin, tex_pos.y + p_rect_margin, w + p_rect_margin * 2, h + p_rect_margin * 2);
  1027. chr.rect.position = Vector2(xofs - p_rect_margin, -yofs - p_rect_margin) * p_data->scale / p_data->oversampling;
  1028. chr.rect.size = chr.uv_rect.size * p_data->scale / p_data->oversampling;
  1029. return chr;
  1030. }
  1031. #endif
  1032. /*************************************************************************/
  1033. /* Font Cache */
  1034. /*************************************************************************/
  1035. _FORCE_INLINE_ bool TextServerAdvanced::_ensure_glyph(FontAdvanced *p_font_data, const Vector2i &p_size, int32_t p_glyph) const {
  1036. ERR_FAIL_COND_V(!_ensure_cache_for_size(p_font_data, p_size), false);
  1037. int32_t glyph_index = p_glyph & 0xffffff; // Remove subpixel shifts.
  1038. FontForSizeAdvanced *fd = p_font_data->cache[p_size];
  1039. if (fd->glyph_map.has(p_glyph)) {
  1040. return fd->glyph_map[p_glyph].found;
  1041. }
  1042. if (glyph_index == 0) { // Non graphical or invalid glyph, do not render.
  1043. fd->glyph_map[p_glyph] = FontGlyph();
  1044. return true;
  1045. }
  1046. #ifdef MODULE_FREETYPE_ENABLED
  1047. FontGlyph gl;
  1048. if (fd->face) {
  1049. FT_Int32 flags = FT_LOAD_DEFAULT;
  1050. bool outline = p_size.y > 0;
  1051. switch (p_font_data->hinting) {
  1052. case TextServer::HINTING_NONE:
  1053. flags |= FT_LOAD_NO_HINTING;
  1054. break;
  1055. case TextServer::HINTING_LIGHT:
  1056. flags |= FT_LOAD_TARGET_LIGHT;
  1057. break;
  1058. default:
  1059. flags |= FT_LOAD_TARGET_NORMAL;
  1060. break;
  1061. }
  1062. if (p_font_data->force_autohinter) {
  1063. flags |= FT_LOAD_FORCE_AUTOHINT;
  1064. }
  1065. if (outline) {
  1066. flags |= FT_LOAD_NO_BITMAP;
  1067. } else if (FT_HAS_COLOR(fd->face)) {
  1068. flags |= FT_LOAD_COLOR;
  1069. }
  1070. FT_Fixed v, h;
  1071. FT_Get_Advance(fd->face, glyph_index, flags, &h);
  1072. FT_Get_Advance(fd->face, glyph_index, flags | FT_LOAD_VERTICAL_LAYOUT, &v);
  1073. int error = FT_Load_Glyph(fd->face, glyph_index, flags);
  1074. if (error) {
  1075. fd->glyph_map[p_glyph] = FontGlyph();
  1076. return false;
  1077. }
  1078. if (!p_font_data->msdf) {
  1079. if ((p_font_data->subpixel_positioning == SUBPIXEL_POSITIONING_ONE_QUARTER) || (p_font_data->subpixel_positioning == SUBPIXEL_POSITIONING_AUTO && p_size.x <= SUBPIXEL_POSITIONING_ONE_QUARTER_MAX_SIZE)) {
  1080. FT_Pos xshift = (int)((p_glyph >> 27) & 3) << 4;
  1081. FT_Outline_Translate(&fd->face->glyph->outline, xshift, 0);
  1082. } else if ((p_font_data->subpixel_positioning == SUBPIXEL_POSITIONING_ONE_HALF) || (p_font_data->subpixel_positioning == SUBPIXEL_POSITIONING_AUTO && p_size.x <= SUBPIXEL_POSITIONING_ONE_HALF_MAX_SIZE)) {
  1083. FT_Pos xshift = (int)((p_glyph >> 27) & 3) << 5;
  1084. FT_Outline_Translate(&fd->face->glyph->outline, xshift, 0);
  1085. }
  1086. }
  1087. if (p_font_data->embolden != 0.f) {
  1088. FT_Pos strength = p_font_data->embolden * p_size.x * 4; // 26.6 fractional units (1 / 64).
  1089. FT_Outline_Embolden(&fd->face->glyph->outline, strength);
  1090. }
  1091. if (p_font_data->transform != Transform2D()) {
  1092. FT_Matrix mat = { FT_Fixed(p_font_data->transform[0][0] * 65536), FT_Fixed(p_font_data->transform[0][1] * 65536), FT_Fixed(p_font_data->transform[1][0] * 65536), FT_Fixed(p_font_data->transform[1][1] * 65536) }; // 16.16 fractional units (1 / 65536).
  1093. FT_Outline_Transform(&fd->face->glyph->outline, &mat);
  1094. }
  1095. FT_Render_Mode aa_mode = FT_RENDER_MODE_NORMAL;
  1096. bool bgra = false;
  1097. switch (p_font_data->antialiasing) {
  1098. case FONT_ANTIALIASING_NONE: {
  1099. aa_mode = FT_RENDER_MODE_MONO;
  1100. } break;
  1101. case FONT_ANTIALIASING_GRAY: {
  1102. aa_mode = FT_RENDER_MODE_NORMAL;
  1103. } break;
  1104. case FONT_ANTIALIASING_LCD: {
  1105. int aa_layout = (int)((p_glyph >> 24) & 7);
  1106. switch (aa_layout) {
  1107. case FONT_LCD_SUBPIXEL_LAYOUT_HRGB: {
  1108. aa_mode = FT_RENDER_MODE_LCD;
  1109. bgra = false;
  1110. } break;
  1111. case FONT_LCD_SUBPIXEL_LAYOUT_HBGR: {
  1112. aa_mode = FT_RENDER_MODE_LCD;
  1113. bgra = true;
  1114. } break;
  1115. case FONT_LCD_SUBPIXEL_LAYOUT_VRGB: {
  1116. aa_mode = FT_RENDER_MODE_LCD_V;
  1117. bgra = false;
  1118. } break;
  1119. case FONT_LCD_SUBPIXEL_LAYOUT_VBGR: {
  1120. aa_mode = FT_RENDER_MODE_LCD_V;
  1121. bgra = true;
  1122. } break;
  1123. default: {
  1124. aa_mode = FT_RENDER_MODE_NORMAL;
  1125. } break;
  1126. }
  1127. } break;
  1128. }
  1129. if (!outline) {
  1130. if (!p_font_data->msdf) {
  1131. error = FT_Render_Glyph(fd->face->glyph, aa_mode);
  1132. }
  1133. FT_GlyphSlot slot = fd->face->glyph;
  1134. if (!error) {
  1135. if (p_font_data->msdf) {
  1136. #ifdef MODULE_MSDFGEN_ENABLED
  1137. gl = rasterize_msdf(p_font_data, fd, p_font_data->msdf_range, rect_range, &slot->outline, Vector2((h + (1 << 9)) >> 10, (v + (1 << 9)) >> 10) / 64.0);
  1138. #else
  1139. fd->glyph_map[p_glyph] = FontGlyph();
  1140. ERR_FAIL_V_MSG(false, "Compiled without MSDFGEN support!");
  1141. #endif
  1142. } else {
  1143. gl = rasterize_bitmap(fd, rect_range, slot->bitmap, slot->bitmap_top, slot->bitmap_left, Vector2((h + (1 << 9)) >> 10, (v + (1 << 9)) >> 10) / 64.0, bgra);
  1144. }
  1145. }
  1146. } else {
  1147. FT_Stroker stroker;
  1148. if (FT_Stroker_New(ft_library, &stroker) != 0) {
  1149. fd->glyph_map[p_glyph] = FontGlyph();
  1150. ERR_FAIL_V_MSG(false, "FreeType: Failed to load glyph stroker.");
  1151. }
  1152. FT_Stroker_Set(stroker, (int)(fd->size.y * fd->oversampling * 16.0), FT_STROKER_LINECAP_BUTT, FT_STROKER_LINEJOIN_ROUND, 0);
  1153. FT_Glyph glyph;
  1154. FT_BitmapGlyph glyph_bitmap;
  1155. if (FT_Get_Glyph(fd->face->glyph, &glyph) != 0) {
  1156. goto cleanup_stroker;
  1157. }
  1158. if (FT_Glyph_Stroke(&glyph, stroker, 1) != 0) {
  1159. goto cleanup_glyph;
  1160. }
  1161. if (FT_Glyph_To_Bitmap(&glyph, aa_mode, nullptr, 1) != 0) {
  1162. goto cleanup_glyph;
  1163. }
  1164. glyph_bitmap = (FT_BitmapGlyph)glyph;
  1165. gl = rasterize_bitmap(fd, rect_range, glyph_bitmap->bitmap, glyph_bitmap->top, glyph_bitmap->left, Vector2(), bgra);
  1166. cleanup_glyph:
  1167. FT_Done_Glyph(glyph);
  1168. cleanup_stroker:
  1169. FT_Stroker_Done(stroker);
  1170. }
  1171. fd->glyph_map[p_glyph] = gl;
  1172. return gl.found;
  1173. }
  1174. #endif
  1175. fd->glyph_map[p_glyph] = FontGlyph();
  1176. return false;
  1177. }
  1178. _FORCE_INLINE_ bool TextServerAdvanced::_ensure_cache_for_size(FontAdvanced *p_font_data, const Vector2i &p_size) const {
  1179. ERR_FAIL_COND_V(p_size.x <= 0, false);
  1180. if (p_font_data->cache.has(p_size)) {
  1181. return true;
  1182. }
  1183. FontForSizeAdvanced *fd = memnew(FontForSizeAdvanced);
  1184. fd->size = p_size;
  1185. if (p_font_data->data_ptr && (p_font_data->data_size > 0)) {
  1186. // Init dynamic font.
  1187. #ifdef MODULE_FREETYPE_ENABLED
  1188. int error = 0;
  1189. if (!ft_library) {
  1190. error = FT_Init_FreeType(&ft_library);
  1191. ERR_FAIL_COND_V_MSG(error != 0, false, "FreeType: Error initializing library: '" + String(FT_Error_String(error)) + "'.");
  1192. }
  1193. memset(&fd->stream, 0, sizeof(FT_StreamRec));
  1194. fd->stream.base = (unsigned char *)p_font_data->data_ptr;
  1195. fd->stream.size = p_font_data->data_size;
  1196. fd->stream.pos = 0;
  1197. FT_Open_Args fargs;
  1198. memset(&fargs, 0, sizeof(FT_Open_Args));
  1199. fargs.memory_base = (unsigned char *)p_font_data->data_ptr;
  1200. fargs.memory_size = p_font_data->data_size;
  1201. fargs.flags = FT_OPEN_MEMORY;
  1202. fargs.stream = &fd->stream;
  1203. int max_index = 0;
  1204. FT_Face tmp_face = nullptr;
  1205. error = FT_Open_Face(ft_library, &fargs, -1, &tmp_face);
  1206. if (tmp_face && error == 0) {
  1207. max_index = tmp_face->num_faces - 1;
  1208. }
  1209. if (tmp_face) {
  1210. FT_Done_Face(tmp_face);
  1211. }
  1212. error = FT_Open_Face(ft_library, &fargs, CLAMP(p_font_data->face_index, 0, max_index), &fd->face);
  1213. if (error) {
  1214. FT_Done_Face(fd->face);
  1215. fd->face = nullptr;
  1216. ERR_FAIL_V_MSG(false, "FreeType: Error loading font: '" + String(FT_Error_String(error)) + "'.");
  1217. }
  1218. if (p_font_data->msdf) {
  1219. fd->oversampling = 1.0;
  1220. fd->size.x = p_font_data->msdf_source_size;
  1221. } else if (p_font_data->oversampling <= 0.0) {
  1222. fd->oversampling = font_get_global_oversampling();
  1223. } else {
  1224. fd->oversampling = p_font_data->oversampling;
  1225. }
  1226. if (FT_HAS_COLOR(fd->face) && fd->face->num_fixed_sizes > 0) {
  1227. int best_match = 0;
  1228. int diff = ABS(fd->size.x - ((int64_t)fd->face->available_sizes[0].width));
  1229. fd->scale = double(fd->size.x * fd->oversampling) / fd->face->available_sizes[0].width;
  1230. for (int i = 1; i < fd->face->num_fixed_sizes; i++) {
  1231. int ndiff = ABS(fd->size.x - ((int64_t)fd->face->available_sizes[i].width));
  1232. if (ndiff < diff) {
  1233. best_match = i;
  1234. diff = ndiff;
  1235. fd->scale = double(fd->size.x * fd->oversampling) / fd->face->available_sizes[i].width;
  1236. }
  1237. }
  1238. FT_Select_Size(fd->face, best_match);
  1239. } else {
  1240. FT_Set_Pixel_Sizes(fd->face, 0, double(fd->size.x * fd->oversampling));
  1241. fd->scale = ((double)fd->size.x * fd->oversampling) / (double)fd->face->size->metrics.y_ppem;
  1242. }
  1243. fd->hb_handle = hb_ft_font_create(fd->face, nullptr);
  1244. fd->ascent = (fd->face->size->metrics.ascender / 64.0) / fd->oversampling * fd->scale;
  1245. fd->descent = (-fd->face->size->metrics.descender / 64.0) / fd->oversampling * fd->scale;
  1246. fd->underline_position = (-FT_MulFix(fd->face->underline_position, fd->face->size->metrics.y_scale) / 64.0) / fd->oversampling * fd->scale;
  1247. fd->underline_thickness = (FT_MulFix(fd->face->underline_thickness, fd->face->size->metrics.y_scale) / 64.0) / fd->oversampling * fd->scale;
  1248. #if HB_VERSION_ATLEAST(3, 3, 0)
  1249. hb_font_set_synthetic_slant(fd->hb_handle, p_font_data->transform[0][1]);
  1250. #else
  1251. #ifndef _MSC_VER
  1252. #warning Building with HarfBuzz < 3.3.0, synthetic slant offset correction disabled.
  1253. #endif
  1254. #endif
  1255. if (!p_font_data->face_init) {
  1256. // Get style flags and name.
  1257. if (fd->face->family_name != nullptr) {
  1258. p_font_data->font_name = String::utf8((const char *)fd->face->family_name);
  1259. }
  1260. if (fd->face->style_name != nullptr) {
  1261. p_font_data->style_name = String::utf8((const char *)fd->face->style_name);
  1262. }
  1263. p_font_data->style_flags = 0;
  1264. if (fd->face->style_flags & FT_STYLE_FLAG_BOLD) {
  1265. p_font_data->style_flags.set_flag(FONT_BOLD);
  1266. }
  1267. if (fd->face->style_flags & FT_STYLE_FLAG_ITALIC) {
  1268. p_font_data->style_flags.set_flag(FONT_ITALIC);
  1269. }
  1270. if (fd->face->face_flags & FT_FACE_FLAG_FIXED_WIDTH) {
  1271. p_font_data->style_flags.set_flag(FONT_FIXED_WIDTH);
  1272. }
  1273. hb_face_t *hb_face = hb_font_get_face(fd->hb_handle);
  1274. // Get supported scripts from OpenType font data.
  1275. p_font_data->supported_scripts.clear();
  1276. unsigned int count = hb_ot_layout_table_get_script_tags(hb_face, HB_OT_TAG_GSUB, 0, nullptr, nullptr);
  1277. if (count != 0) {
  1278. hb_tag_t *script_tags = (hb_tag_t *)memalloc(count * sizeof(hb_tag_t));
  1279. hb_ot_layout_table_get_script_tags(hb_face, HB_OT_TAG_GSUB, 0, &count, script_tags);
  1280. for (unsigned int i = 0; i < count; i++) {
  1281. p_font_data->supported_scripts.insert(script_tags[i]);
  1282. }
  1283. memfree(script_tags);
  1284. }
  1285. count = hb_ot_layout_table_get_script_tags(hb_face, HB_OT_TAG_GPOS, 0, nullptr, nullptr);
  1286. if (count != 0) {
  1287. hb_tag_t *script_tags = (hb_tag_t *)memalloc(count * sizeof(hb_tag_t));
  1288. hb_ot_layout_table_get_script_tags(hb_face, HB_OT_TAG_GPOS, 0, &count, script_tags);
  1289. for (unsigned int i = 0; i < count; i++) {
  1290. p_font_data->supported_scripts.insert(script_tags[i]);
  1291. }
  1292. memfree(script_tags);
  1293. }
  1294. // Get supported scripts from OS2 table.
  1295. TT_OS2 *os2 = (TT_OS2 *)FT_Get_Sfnt_Table(fd->face, FT_SFNT_OS2);
  1296. if (os2) {
  1297. if ((os2->ulUnicodeRange1 & 1L << 4) || (os2->ulUnicodeRange1 & 1L << 5) || (os2->ulUnicodeRange1 & 1L << 6) || (os2->ulUnicodeRange1 & 1L << 31) || (os2->ulUnicodeRange2 & 1L << 0) || (os2->ulUnicodeRange2 & 1L << 1) || (os2->ulUnicodeRange2 & 1L << 2) || (os2->ulUnicodeRange2 & 1L << 3) || (os2->ulUnicodeRange2 & 1L << 4) || (os2->ulUnicodeRange2 & 1L << 5) || (os2->ulUnicodeRange2 & 1L << 6) || (os2->ulUnicodeRange2 & 1L << 7) || (os2->ulUnicodeRange2 & 1L << 8) || (os2->ulUnicodeRange2 & 1L << 9) || (os2->ulUnicodeRange2 & 1L << 10) || (os2->ulUnicodeRange2 & 1L << 11) || (os2->ulUnicodeRange2 & 1L << 12) || (os2->ulUnicodeRange2 & 1L << 13) || (os2->ulUnicodeRange2 & 1L << 14) || (os2->ulUnicodeRange2 & 1L << 15) || (os2->ulUnicodeRange2 & 1L << 30) || (os2->ulUnicodeRange3 & 1L << 0) || (os2->ulUnicodeRange3 & 1L << 1) || (os2->ulUnicodeRange3 & 1L << 2) || (os2->ulUnicodeRange3 & 1L << 4) || (os2->ulUnicodeRange3 & 1L << 5) || (os2->ulUnicodeRange3 & 1L << 18) || (os2->ulUnicodeRange3 & 1L << 24) || (os2->ulUnicodeRange3 & 1L << 25) || (os2->ulUnicodeRange3 & 1L << 26) || (os2->ulUnicodeRange3 & 1L << 27) || (os2->ulUnicodeRange3 & 1L << 28) || (os2->ulUnicodeRange4 & 1L << 3) || (os2->ulUnicodeRange4 & 1L << 6) || (os2->ulUnicodeRange4 & 1L << 15) || (os2->ulUnicodeRange4 & 1L << 23) || (os2->ulUnicodeRange4 & 1L << 24) || (os2->ulUnicodeRange4 & 1L << 26)) {
  1298. p_font_data->supported_scripts.insert(HB_SCRIPT_COMMON);
  1299. }
  1300. if ((os2->ulUnicodeRange1 & 1L << 0) || (os2->ulUnicodeRange1 & 1L << 1) || (os2->ulUnicodeRange1 & 1L << 2) || (os2->ulUnicodeRange1 & 1L << 3) || (os2->ulUnicodeRange1 & 1L << 29)) {
  1301. p_font_data->supported_scripts.insert(HB_SCRIPT_LATIN);
  1302. }
  1303. if ((os2->ulUnicodeRange1 & 1L << 7) || (os2->ulUnicodeRange1 & 1L << 30)) {
  1304. p_font_data->supported_scripts.insert(HB_SCRIPT_GREEK);
  1305. }
  1306. if (os2->ulUnicodeRange1 & 1L << 8) {
  1307. p_font_data->supported_scripts.insert(HB_SCRIPT_COPTIC);
  1308. }
  1309. if (os2->ulUnicodeRange1 & 1L << 9) {
  1310. p_font_data->supported_scripts.insert(HB_SCRIPT_CYRILLIC);
  1311. }
  1312. if (os2->ulUnicodeRange1 & 1L << 10) {
  1313. p_font_data->supported_scripts.insert(HB_SCRIPT_ARMENIAN);
  1314. }
  1315. if (os2->ulUnicodeRange1 & 1L << 11) {
  1316. p_font_data->supported_scripts.insert(HB_SCRIPT_HEBREW);
  1317. }
  1318. if (os2->ulUnicodeRange1 & 1L << 12) {
  1319. p_font_data->supported_scripts.insert(HB_SCRIPT_VAI);
  1320. }
  1321. if ((os2->ulUnicodeRange1 & 1L << 13) || (os2->ulUnicodeRange2 & 1L << 31) || (os2->ulUnicodeRange3 & 1L << 3)) {
  1322. p_font_data->supported_scripts.insert(HB_SCRIPT_ARABIC);
  1323. }
  1324. if (os2->ulUnicodeRange1 & 1L << 14) {
  1325. p_font_data->supported_scripts.insert(HB_SCRIPT_NKO);
  1326. }
  1327. if (os2->ulUnicodeRange1 & 1L << 15) {
  1328. p_font_data->supported_scripts.insert(HB_SCRIPT_DEVANAGARI);
  1329. }
  1330. if (os2->ulUnicodeRange1 & 1L << 16) {
  1331. p_font_data->supported_scripts.insert(HB_SCRIPT_BENGALI);
  1332. }
  1333. if (os2->ulUnicodeRange1 & 1L << 17) {
  1334. p_font_data->supported_scripts.insert(HB_SCRIPT_GURMUKHI);
  1335. }
  1336. if (os2->ulUnicodeRange1 & 1L << 18) {
  1337. p_font_data->supported_scripts.insert(HB_SCRIPT_GUJARATI);
  1338. }
  1339. if (os2->ulUnicodeRange1 & 1L << 19) {
  1340. p_font_data->supported_scripts.insert(HB_SCRIPT_ORIYA);
  1341. }
  1342. if (os2->ulUnicodeRange1 & 1L << 20) {
  1343. p_font_data->supported_scripts.insert(HB_SCRIPT_TAMIL);
  1344. }
  1345. if (os2->ulUnicodeRange1 & 1L << 21) {
  1346. p_font_data->supported_scripts.insert(HB_SCRIPT_TELUGU);
  1347. }
  1348. if (os2->ulUnicodeRange1 & 1L << 22) {
  1349. p_font_data->supported_scripts.insert(HB_SCRIPT_KANNADA);
  1350. }
  1351. if (os2->ulUnicodeRange1 & 1L << 23) {
  1352. p_font_data->supported_scripts.insert(HB_SCRIPT_MALAYALAM);
  1353. }
  1354. if (os2->ulUnicodeRange1 & 1L << 24) {
  1355. p_font_data->supported_scripts.insert(HB_SCRIPT_THAI);
  1356. }
  1357. if (os2->ulUnicodeRange1 & 1L << 25) {
  1358. p_font_data->supported_scripts.insert(HB_SCRIPT_LAO);
  1359. }
  1360. if (os2->ulUnicodeRange1 & 1L << 26) {
  1361. p_font_data->supported_scripts.insert(HB_SCRIPT_GEORGIAN);
  1362. }
  1363. if (os2->ulUnicodeRange1 & 1L << 27) {
  1364. p_font_data->supported_scripts.insert(HB_SCRIPT_BALINESE);
  1365. }
  1366. if ((os2->ulUnicodeRange1 & 1L << 28) || (os2->ulUnicodeRange2 & 1L << 20) || (os2->ulUnicodeRange2 & 1L << 24)) {
  1367. p_font_data->supported_scripts.insert(HB_SCRIPT_HANGUL);
  1368. }
  1369. if ((os2->ulUnicodeRange2 & 1L << 21) || (os2->ulUnicodeRange2 & 1L << 22) || (os2->ulUnicodeRange2 & 1L << 23) || (os2->ulUnicodeRange2 & 1L << 26) || (os2->ulUnicodeRange2 & 1L << 27) || (os2->ulUnicodeRange2 & 1L << 29)) {
  1370. p_font_data->supported_scripts.insert(HB_SCRIPT_HAN);
  1371. }
  1372. if (os2->ulUnicodeRange2 & 1L << 17) {
  1373. p_font_data->supported_scripts.insert(HB_SCRIPT_HIRAGANA);
  1374. }
  1375. if (os2->ulUnicodeRange2 & 1L << 18) {
  1376. p_font_data->supported_scripts.insert(HB_SCRIPT_KATAKANA);
  1377. }
  1378. if (os2->ulUnicodeRange2 & 1L << 19) {
  1379. p_font_data->supported_scripts.insert(HB_SCRIPT_BOPOMOFO);
  1380. }
  1381. if (os2->ulUnicodeRange3 & 1L << 6) {
  1382. p_font_data->supported_scripts.insert(HB_SCRIPT_TIBETAN);
  1383. }
  1384. if (os2->ulUnicodeRange3 & 1L << 7) {
  1385. p_font_data->supported_scripts.insert(HB_SCRIPT_SYRIAC);
  1386. }
  1387. if (os2->ulUnicodeRange3 & 1L << 8) {
  1388. p_font_data->supported_scripts.insert(HB_SCRIPT_THAANA);
  1389. }
  1390. if (os2->ulUnicodeRange3 & 1L << 9) {
  1391. p_font_data->supported_scripts.insert(HB_SCRIPT_SINHALA);
  1392. }
  1393. if (os2->ulUnicodeRange3 & 1L << 10) {
  1394. p_font_data->supported_scripts.insert(HB_SCRIPT_MYANMAR);
  1395. }
  1396. if (os2->ulUnicodeRange3 & 1L << 11) {
  1397. p_font_data->supported_scripts.insert(HB_SCRIPT_ETHIOPIC);
  1398. }
  1399. if (os2->ulUnicodeRange3 & 1L << 12) {
  1400. p_font_data->supported_scripts.insert(HB_SCRIPT_CHEROKEE);
  1401. }
  1402. if (os2->ulUnicodeRange3 & 1L << 13) {
  1403. p_font_data->supported_scripts.insert(HB_SCRIPT_CANADIAN_SYLLABICS);
  1404. }
  1405. if (os2->ulUnicodeRange3 & 1L << 14) {
  1406. p_font_data->supported_scripts.insert(HB_SCRIPT_OGHAM);
  1407. }
  1408. if (os2->ulUnicodeRange3 & 1L << 15) {
  1409. p_font_data->supported_scripts.insert(HB_SCRIPT_RUNIC);
  1410. }
  1411. if (os2->ulUnicodeRange3 & 1L << 16) {
  1412. p_font_data->supported_scripts.insert(HB_SCRIPT_KHMER);
  1413. }
  1414. if (os2->ulUnicodeRange3 & 1L << 17) {
  1415. p_font_data->supported_scripts.insert(HB_SCRIPT_MONGOLIAN);
  1416. }
  1417. if (os2->ulUnicodeRange3 & 1L << 19) {
  1418. p_font_data->supported_scripts.insert(HB_SCRIPT_YI);
  1419. }
  1420. if (os2->ulUnicodeRange3 & 1L << 20) {
  1421. p_font_data->supported_scripts.insert(HB_SCRIPT_HANUNOO);
  1422. p_font_data->supported_scripts.insert(HB_SCRIPT_TAGBANWA);
  1423. p_font_data->supported_scripts.insert(HB_SCRIPT_BUHID);
  1424. p_font_data->supported_scripts.insert(HB_SCRIPT_TAGALOG);
  1425. }
  1426. if (os2->ulUnicodeRange3 & 1L << 21) {
  1427. p_font_data->supported_scripts.insert(HB_SCRIPT_OLD_ITALIC);
  1428. }
  1429. if (os2->ulUnicodeRange3 & 1L << 22) {
  1430. p_font_data->supported_scripts.insert(HB_SCRIPT_GOTHIC);
  1431. }
  1432. if (os2->ulUnicodeRange3 & 1L << 23) {
  1433. p_font_data->supported_scripts.insert(HB_SCRIPT_DESERET);
  1434. }
  1435. if (os2->ulUnicodeRange3 & 1L << 29) {
  1436. p_font_data->supported_scripts.insert(HB_SCRIPT_LIMBU);
  1437. }
  1438. if (os2->ulUnicodeRange3 & 1L << 30) {
  1439. p_font_data->supported_scripts.insert(HB_SCRIPT_TAI_LE);
  1440. }
  1441. if (os2->ulUnicodeRange3 & 1L << 31) {
  1442. p_font_data->supported_scripts.insert(HB_SCRIPT_NEW_TAI_LUE);
  1443. }
  1444. if (os2->ulUnicodeRange4 & 1L << 0) {
  1445. p_font_data->supported_scripts.insert(HB_SCRIPT_BUGINESE);
  1446. }
  1447. if (os2->ulUnicodeRange4 & 1L << 1) {
  1448. p_font_data->supported_scripts.insert(HB_SCRIPT_GLAGOLITIC);
  1449. }
  1450. if (os2->ulUnicodeRange4 & 1L << 2) {
  1451. p_font_data->supported_scripts.insert(HB_SCRIPT_TIFINAGH);
  1452. }
  1453. if (os2->ulUnicodeRange4 & 1L << 4) {
  1454. p_font_data->supported_scripts.insert(HB_SCRIPT_SYLOTI_NAGRI);
  1455. }
  1456. if (os2->ulUnicodeRange4 & 1L << 5) {
  1457. p_font_data->supported_scripts.insert(HB_SCRIPT_LINEAR_B);
  1458. }
  1459. if (os2->ulUnicodeRange4 & 1L << 7) {
  1460. p_font_data->supported_scripts.insert(HB_SCRIPT_UGARITIC);
  1461. }
  1462. if (os2->ulUnicodeRange4 & 1L << 8) {
  1463. p_font_data->supported_scripts.insert(HB_SCRIPT_OLD_PERSIAN);
  1464. }
  1465. if (os2->ulUnicodeRange4 & 1L << 9) {
  1466. p_font_data->supported_scripts.insert(HB_SCRIPT_SHAVIAN);
  1467. }
  1468. if (os2->ulUnicodeRange4 & 1L << 10) {
  1469. p_font_data->supported_scripts.insert(HB_SCRIPT_OSMANYA);
  1470. }
  1471. if (os2->ulUnicodeRange4 & 1L << 11) {
  1472. p_font_data->supported_scripts.insert(HB_SCRIPT_CYPRIOT);
  1473. }
  1474. if (os2->ulUnicodeRange4 & 1L << 12) {
  1475. p_font_data->supported_scripts.insert(HB_SCRIPT_KHAROSHTHI);
  1476. }
  1477. if (os2->ulUnicodeRange4 & 1L << 13) {
  1478. p_font_data->supported_scripts.insert(HB_SCRIPT_TAI_VIET);
  1479. }
  1480. if (os2->ulUnicodeRange4 & 1L << 14) {
  1481. p_font_data->supported_scripts.insert(HB_SCRIPT_CUNEIFORM);
  1482. }
  1483. if (os2->ulUnicodeRange4 & 1L << 16) {
  1484. p_font_data->supported_scripts.insert(HB_SCRIPT_SUNDANESE);
  1485. }
  1486. if (os2->ulUnicodeRange4 & 1L << 17) {
  1487. p_font_data->supported_scripts.insert(HB_SCRIPT_LEPCHA);
  1488. }
  1489. if (os2->ulUnicodeRange4 & 1L << 18) {
  1490. p_font_data->supported_scripts.insert(HB_SCRIPT_OL_CHIKI);
  1491. }
  1492. if (os2->ulUnicodeRange4 & 1L << 19) {
  1493. p_font_data->supported_scripts.insert(HB_SCRIPT_SAURASHTRA);
  1494. }
  1495. if (os2->ulUnicodeRange4 & 1L << 20) {
  1496. p_font_data->supported_scripts.insert(HB_SCRIPT_KAYAH_LI);
  1497. }
  1498. if (os2->ulUnicodeRange4 & 1L << 21) {
  1499. p_font_data->supported_scripts.insert(HB_SCRIPT_REJANG);
  1500. }
  1501. if (os2->ulUnicodeRange4 & 1L << 22) {
  1502. p_font_data->supported_scripts.insert(HB_SCRIPT_CHAM);
  1503. }
  1504. if (os2->ulUnicodeRange4 & 1L << 25) {
  1505. p_font_data->supported_scripts.insert(HB_SCRIPT_ANATOLIAN_HIEROGLYPHS);
  1506. }
  1507. }
  1508. // Read OpenType feature tags.
  1509. p_font_data->supported_features.clear();
  1510. count = hb_ot_layout_table_get_feature_tags(hb_face, HB_OT_TAG_GSUB, 0, nullptr, nullptr);
  1511. if (count != 0) {
  1512. hb_tag_t *feature_tags = (hb_tag_t *)memalloc(count * sizeof(hb_tag_t));
  1513. hb_ot_layout_table_get_feature_tags(hb_face, HB_OT_TAG_GSUB, 0, &count, feature_tags);
  1514. for (unsigned int i = 0; i < count; i++) {
  1515. Dictionary ftr;
  1516. #if HB_VERSION_ATLEAST(2, 1, 0)
  1517. hb_ot_name_id_t lbl_id;
  1518. if (hb_ot_layout_feature_get_name_ids(hb_face, HB_OT_TAG_GSUB, i, &lbl_id, nullptr, nullptr, nullptr, nullptr)) {
  1519. PackedInt32Array lbl;
  1520. unsigned int text_size = hb_ot_name_get_utf32(hb_face, lbl_id, hb_language_from_string(TranslationServer::get_singleton()->get_tool_locale().ascii().get_data(), -1), nullptr, nullptr) + 1;
  1521. lbl.resize(text_size);
  1522. memset((uint32_t *)lbl.ptrw(), 0, sizeof(uint32_t) * text_size);
  1523. hb_ot_name_get_utf32(hb_face, lbl_id, hb_language_from_string(TranslationServer::get_singleton()->get_tool_locale().ascii().get_data(), -1), &text_size, (uint32_t *)lbl.ptrw());
  1524. ftr["label"] = String((const char32_t *)lbl.ptr());
  1525. }
  1526. #else
  1527. #ifndef _MSC_VER
  1528. #warning Building with HarfBuzz < 2.1.0, readable OpenType feature names disabled.
  1529. #endif
  1530. #endif
  1531. ftr["type"] = _get_tag_type(feature_tags[i]);
  1532. ftr["hidden"] = _get_tag_hidden(feature_tags[i]);
  1533. p_font_data->supported_features[feature_tags[i]] = ftr;
  1534. }
  1535. memfree(feature_tags);
  1536. }
  1537. count = hb_ot_layout_table_get_feature_tags(hb_face, HB_OT_TAG_GPOS, 0, nullptr, nullptr);
  1538. if (count != 0) {
  1539. hb_tag_t *feature_tags = (hb_tag_t *)memalloc(count * sizeof(hb_tag_t));
  1540. hb_ot_layout_table_get_feature_tags(hb_face, HB_OT_TAG_GPOS, 0, &count, feature_tags);
  1541. for (unsigned int i = 0; i < count; i++) {
  1542. Dictionary ftr;
  1543. #if HB_VERSION_ATLEAST(2, 1, 0)
  1544. hb_ot_name_id_t lbl_id;
  1545. if (hb_ot_layout_feature_get_name_ids(hb_face, HB_OT_TAG_GPOS, i, &lbl_id, nullptr, nullptr, nullptr, nullptr)) {
  1546. PackedInt32Array lbl;
  1547. unsigned int text_size = hb_ot_name_get_utf32(hb_face, lbl_id, hb_language_from_string(TranslationServer::get_singleton()->get_tool_locale().ascii().get_data(), -1), nullptr, nullptr) + 1;
  1548. lbl.resize(text_size);
  1549. memset((uint32_t *)lbl.ptrw(), 0, sizeof(uint32_t) * text_size);
  1550. hb_ot_name_get_utf32(hb_face, lbl_id, hb_language_from_string(TranslationServer::get_singleton()->get_tool_locale().ascii().get_data(), -1), &text_size, (uint32_t *)lbl.ptrw());
  1551. ftr["label"] = String((const char32_t *)lbl.ptr());
  1552. }
  1553. #else
  1554. #ifndef _MSC_VER
  1555. #warning Building with HarfBuzz < 2.1.0, readable OpenType feature names disabled.
  1556. #endif
  1557. #endif
  1558. ftr["type"] = _get_tag_type(feature_tags[i]);
  1559. ftr["hidden"] = _get_tag_hidden(feature_tags[i]);
  1560. p_font_data->supported_features[feature_tags[i]] = ftr;
  1561. }
  1562. memfree(feature_tags);
  1563. }
  1564. // Read OpenType variations.
  1565. p_font_data->supported_varaitions.clear();
  1566. if (fd->face->face_flags & FT_FACE_FLAG_MULTIPLE_MASTERS) {
  1567. FT_MM_Var *amaster;
  1568. FT_Get_MM_Var(fd->face, &amaster);
  1569. for (FT_UInt i = 0; i < amaster->num_axis; i++) {
  1570. p_font_data->supported_varaitions[(int32_t)amaster->axis[i].tag] = Vector3i(amaster->axis[i].minimum / 65536, amaster->axis[i].maximum / 65536, amaster->axis[i].def / 65536);
  1571. }
  1572. FT_Done_MM_Var(ft_library, amaster);
  1573. }
  1574. p_font_data->face_init = true;
  1575. }
  1576. // Write variations.
  1577. if (fd->face->face_flags & FT_FACE_FLAG_MULTIPLE_MASTERS) {
  1578. FT_MM_Var *amaster;
  1579. FT_Get_MM_Var(fd->face, &amaster);
  1580. Vector<hb_variation_t> hb_vars;
  1581. Vector<FT_Fixed> coords;
  1582. coords.resize(amaster->num_axis);
  1583. FT_Get_Var_Design_Coordinates(fd->face, coords.size(), coords.ptrw());
  1584. for (FT_UInt i = 0; i < amaster->num_axis; i++) {
  1585. hb_variation_t var;
  1586. // Reset to default.
  1587. var.tag = amaster->axis[i].tag;
  1588. var.value = (double)amaster->axis[i].def / 65536.0;
  1589. coords.write[i] = amaster->axis[i].def;
  1590. if (p_font_data->variation_coordinates.has(var.tag)) {
  1591. var.value = p_font_data->variation_coordinates[var.tag];
  1592. coords.write[i] = CLAMP(var.value * 65536.0, amaster->axis[i].minimum, amaster->axis[i].maximum);
  1593. }
  1594. if (p_font_data->variation_coordinates.has(tag_to_name(var.tag))) {
  1595. var.value = p_font_data->variation_coordinates[tag_to_name(var.tag)];
  1596. coords.write[i] = CLAMP(var.value * 65536.0, amaster->axis[i].minimum, amaster->axis[i].maximum);
  1597. }
  1598. hb_vars.push_back(var);
  1599. }
  1600. FT_Set_Var_Design_Coordinates(fd->face, coords.size(), coords.ptrw());
  1601. hb_font_set_variations(fd->hb_handle, hb_vars.is_empty() ? nullptr : &hb_vars[0], hb_vars.size());
  1602. FT_Done_MM_Var(ft_library, amaster);
  1603. }
  1604. #else
  1605. ERR_FAIL_V_MSG(false, "FreeType: Can't load dynamic font, engine is compiled without FreeType support!");
  1606. #endif
  1607. } else {
  1608. // Init bitmap font.
  1609. fd->hb_handle = _bmp_font_create(fd, nullptr);
  1610. }
  1611. p_font_data->cache[p_size] = fd;
  1612. return true;
  1613. }
  1614. _FORCE_INLINE_ void TextServerAdvanced::_font_clear_cache(FontAdvanced *p_font_data) {
  1615. for (const KeyValue<Vector2i, FontForSizeAdvanced *> &E : p_font_data->cache) {
  1616. memdelete(E.value);
  1617. }
  1618. p_font_data->cache.clear();
  1619. p_font_data->face_init = false;
  1620. p_font_data->supported_features.clear();
  1621. p_font_data->supported_varaitions.clear();
  1622. p_font_data->supported_scripts.clear();
  1623. }
  1624. hb_font_t *TextServerAdvanced::_font_get_hb_handle(const RID &p_font_rid, int64_t p_size) const {
  1625. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1626. ERR_FAIL_COND_V(!fd, nullptr);
  1627. MutexLock lock(fd->mutex);
  1628. Vector2i size = _get_size(fd, p_size);
  1629. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), nullptr);
  1630. return fd->cache[size]->hb_handle;
  1631. }
  1632. RID TextServerAdvanced::create_font() {
  1633. _THREAD_SAFE_METHOD_
  1634. FontAdvanced *fd = memnew(FontAdvanced);
  1635. return font_owner.make_rid(fd);
  1636. }
  1637. void TextServerAdvanced::font_set_data(const RID &p_font_rid, const PackedByteArray &p_data) {
  1638. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1639. ERR_FAIL_COND(!fd);
  1640. MutexLock lock(fd->mutex);
  1641. _font_clear_cache(fd);
  1642. fd->data = p_data;
  1643. fd->data_ptr = fd->data.ptr();
  1644. fd->data_size = fd->data.size();
  1645. }
  1646. void TextServerAdvanced::font_set_data_ptr(const RID &p_font_rid, const uint8_t *p_data_ptr, int64_t p_data_size) {
  1647. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1648. ERR_FAIL_COND(!fd);
  1649. MutexLock lock(fd->mutex);
  1650. _font_clear_cache(fd);
  1651. fd->data.resize(0);
  1652. fd->data_ptr = p_data_ptr;
  1653. fd->data_size = p_data_size;
  1654. }
  1655. void TextServerAdvanced::font_set_face_index(const RID &p_font_rid, int64_t p_face_index) {
  1656. ERR_FAIL_COND(p_face_index < 0);
  1657. ERR_FAIL_COND(p_face_index >= 0x7FFF);
  1658. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1659. ERR_FAIL_COND(!fd);
  1660. MutexLock lock(fd->mutex);
  1661. if (fd->face_index != p_face_index) {
  1662. fd->face_index = p_face_index;
  1663. _font_clear_cache(fd);
  1664. }
  1665. }
  1666. int64_t TextServerAdvanced::font_get_face_index(const RID &p_font_rid) const {
  1667. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1668. ERR_FAIL_COND_V(!fd, 0);
  1669. MutexLock lock(fd->mutex);
  1670. return fd->face_index;
  1671. }
  1672. int64_t TextServerAdvanced::font_get_face_count(const RID &p_font_rid) const {
  1673. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1674. ERR_FAIL_COND_V(!fd, 0);
  1675. MutexLock lock(fd->mutex);
  1676. int face_count = 0;
  1677. if (fd->data_ptr && (fd->data_size > 0)) {
  1678. // Init dynamic font.
  1679. #ifdef MODULE_FREETYPE_ENABLED
  1680. int error = 0;
  1681. if (!ft_library) {
  1682. error = FT_Init_FreeType(&ft_library);
  1683. ERR_FAIL_COND_V_MSG(error != 0, false, "FreeType: Error initializing library: '" + String(FT_Error_String(error)) + "'.");
  1684. }
  1685. FT_StreamRec stream;
  1686. memset(&stream, 0, sizeof(FT_StreamRec));
  1687. stream.base = (unsigned char *)fd->data_ptr;
  1688. stream.size = fd->data_size;
  1689. stream.pos = 0;
  1690. FT_Open_Args fargs;
  1691. memset(&fargs, 0, sizeof(FT_Open_Args));
  1692. fargs.memory_base = (unsigned char *)fd->data_ptr;
  1693. fargs.memory_size = fd->data_size;
  1694. fargs.flags = FT_OPEN_MEMORY;
  1695. fargs.stream = &stream;
  1696. FT_Face tmp_face;
  1697. error = FT_Open_Face(ft_library, &fargs, -1, &tmp_face);
  1698. if (error == 0) {
  1699. face_count = tmp_face->num_faces;
  1700. }
  1701. FT_Done_Face(tmp_face);
  1702. #endif
  1703. }
  1704. return face_count;
  1705. }
  1706. void TextServerAdvanced::font_set_style(const RID &p_font_rid, BitField<FontStyle> p_style) {
  1707. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1708. ERR_FAIL_COND(!fd);
  1709. MutexLock lock(fd->mutex);
  1710. Vector2i size = _get_size(fd, 16);
  1711. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  1712. fd->style_flags = p_style;
  1713. }
  1714. BitField<TextServer::FontStyle> TextServerAdvanced::font_get_style(const RID &p_font_rid) const {
  1715. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1716. ERR_FAIL_COND_V(!fd, 0);
  1717. MutexLock lock(fd->mutex);
  1718. Vector2i size = _get_size(fd, 16);
  1719. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), 0);
  1720. return fd->style_flags;
  1721. }
  1722. void TextServerAdvanced::font_set_style_name(const RID &p_font_rid, const String &p_name) {
  1723. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1724. ERR_FAIL_COND(!fd);
  1725. MutexLock lock(fd->mutex);
  1726. Vector2i size = _get_size(fd, 16);
  1727. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  1728. fd->style_name = p_name;
  1729. }
  1730. String TextServerAdvanced::font_get_style_name(const RID &p_font_rid) const {
  1731. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1732. ERR_FAIL_COND_V(!fd, String());
  1733. MutexLock lock(fd->mutex);
  1734. Vector2i size = _get_size(fd, 16);
  1735. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), String());
  1736. return fd->style_name;
  1737. }
  1738. void TextServerAdvanced::font_set_name(const RID &p_font_rid, const String &p_name) {
  1739. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1740. ERR_FAIL_COND(!fd);
  1741. MutexLock lock(fd->mutex);
  1742. Vector2i size = _get_size(fd, 16);
  1743. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  1744. fd->font_name = p_name;
  1745. }
  1746. String TextServerAdvanced::font_get_name(const RID &p_font_rid) const {
  1747. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1748. ERR_FAIL_COND_V(!fd, String());
  1749. MutexLock lock(fd->mutex);
  1750. Vector2i size = _get_size(fd, 16);
  1751. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), String());
  1752. return fd->font_name;
  1753. }
  1754. void TextServerAdvanced::font_set_antialiasing(RID p_font_rid, TextServer::FontAntialiasing p_antialiasing) {
  1755. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1756. ERR_FAIL_COND(!fd);
  1757. MutexLock lock(fd->mutex);
  1758. if (fd->antialiasing != p_antialiasing) {
  1759. _font_clear_cache(fd);
  1760. fd->antialiasing = p_antialiasing;
  1761. }
  1762. }
  1763. TextServer::FontAntialiasing TextServerAdvanced::font_get_antialiasing(RID p_font_rid) const {
  1764. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1765. ERR_FAIL_COND_V(!fd, TextServer::FONT_ANTIALIASING_NONE);
  1766. MutexLock lock(fd->mutex);
  1767. return fd->antialiasing;
  1768. }
  1769. void TextServerAdvanced::font_set_generate_mipmaps(const RID &p_font_rid, bool p_generate_mipmaps) {
  1770. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1771. ERR_FAIL_COND(!fd);
  1772. MutexLock lock(fd->mutex);
  1773. if (fd->mipmaps != p_generate_mipmaps) {
  1774. for (KeyValue<Vector2i, FontForSizeAdvanced *> &E : fd->cache) {
  1775. for (int i = 0; i < E.value->textures.size(); i++) {
  1776. E.value->textures.write[i].dirty = true;
  1777. }
  1778. }
  1779. fd->mipmaps = p_generate_mipmaps;
  1780. }
  1781. }
  1782. bool TextServerAdvanced::font_get_generate_mipmaps(const RID &p_font_rid) const {
  1783. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1784. ERR_FAIL_COND_V(!fd, false);
  1785. MutexLock lock(fd->mutex);
  1786. return fd->mipmaps;
  1787. }
  1788. void TextServerAdvanced::font_set_multichannel_signed_distance_field(const RID &p_font_rid, bool p_msdf) {
  1789. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1790. ERR_FAIL_COND(!fd);
  1791. MutexLock lock(fd->mutex);
  1792. if (fd->msdf != p_msdf) {
  1793. _font_clear_cache(fd);
  1794. fd->msdf = p_msdf;
  1795. }
  1796. }
  1797. bool TextServerAdvanced::font_is_multichannel_signed_distance_field(const RID &p_font_rid) const {
  1798. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1799. ERR_FAIL_COND_V(!fd, false);
  1800. MutexLock lock(fd->mutex);
  1801. return fd->msdf;
  1802. }
  1803. void TextServerAdvanced::font_set_msdf_pixel_range(const RID &p_font_rid, int64_t p_msdf_pixel_range) {
  1804. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1805. ERR_FAIL_COND(!fd);
  1806. MutexLock lock(fd->mutex);
  1807. if (fd->msdf_range != p_msdf_pixel_range) {
  1808. _font_clear_cache(fd);
  1809. fd->msdf_range = p_msdf_pixel_range;
  1810. }
  1811. }
  1812. int64_t TextServerAdvanced::font_get_msdf_pixel_range(const RID &p_font_rid) const {
  1813. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1814. ERR_FAIL_COND_V(!fd, false);
  1815. MutexLock lock(fd->mutex);
  1816. return fd->msdf_range;
  1817. }
  1818. void TextServerAdvanced::font_set_msdf_size(const RID &p_font_rid, int64_t p_msdf_size) {
  1819. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1820. ERR_FAIL_COND(!fd);
  1821. MutexLock lock(fd->mutex);
  1822. if (fd->msdf_source_size != p_msdf_size) {
  1823. _font_clear_cache(fd);
  1824. fd->msdf_source_size = p_msdf_size;
  1825. }
  1826. }
  1827. int64_t TextServerAdvanced::font_get_msdf_size(const RID &p_font_rid) const {
  1828. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1829. ERR_FAIL_COND_V(!fd, false);
  1830. MutexLock lock(fd->mutex);
  1831. return fd->msdf_source_size;
  1832. }
  1833. void TextServerAdvanced::font_set_fixed_size(const RID &p_font_rid, int64_t p_fixed_size) {
  1834. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1835. ERR_FAIL_COND(!fd);
  1836. MutexLock lock(fd->mutex);
  1837. fd->fixed_size = p_fixed_size;
  1838. }
  1839. int64_t TextServerAdvanced::font_get_fixed_size(const RID &p_font_rid) const {
  1840. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1841. ERR_FAIL_COND_V(!fd, false);
  1842. MutexLock lock(fd->mutex);
  1843. return fd->fixed_size;
  1844. }
  1845. void TextServerAdvanced::font_set_force_autohinter(const RID &p_font_rid, bool p_force_autohinter) {
  1846. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1847. ERR_FAIL_COND(!fd);
  1848. MutexLock lock(fd->mutex);
  1849. if (fd->force_autohinter != p_force_autohinter) {
  1850. _font_clear_cache(fd);
  1851. fd->force_autohinter = p_force_autohinter;
  1852. }
  1853. }
  1854. bool TextServerAdvanced::font_is_force_autohinter(const RID &p_font_rid) const {
  1855. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1856. ERR_FAIL_COND_V(!fd, false);
  1857. MutexLock lock(fd->mutex);
  1858. return fd->force_autohinter;
  1859. }
  1860. void TextServerAdvanced::font_set_hinting(const RID &p_font_rid, TextServer::Hinting p_hinting) {
  1861. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1862. ERR_FAIL_COND(!fd);
  1863. MutexLock lock(fd->mutex);
  1864. if (fd->hinting != p_hinting) {
  1865. _font_clear_cache(fd);
  1866. fd->hinting = p_hinting;
  1867. }
  1868. }
  1869. TextServer::Hinting TextServerAdvanced::font_get_hinting(const RID &p_font_rid) const {
  1870. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1871. ERR_FAIL_COND_V(!fd, HINTING_NONE);
  1872. MutexLock lock(fd->mutex);
  1873. return fd->hinting;
  1874. }
  1875. void TextServerAdvanced::font_set_subpixel_positioning(const RID &p_font_rid, TextServer::SubpixelPositioning p_subpixel) {
  1876. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1877. ERR_FAIL_COND(!fd);
  1878. MutexLock lock(fd->mutex);
  1879. fd->subpixel_positioning = p_subpixel;
  1880. }
  1881. TextServer::SubpixelPositioning TextServerAdvanced::font_get_subpixel_positioning(const RID &p_font_rid) const {
  1882. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1883. ERR_FAIL_COND_V(!fd, SUBPIXEL_POSITIONING_DISABLED);
  1884. MutexLock lock(fd->mutex);
  1885. return fd->subpixel_positioning;
  1886. }
  1887. void TextServerAdvanced::font_set_embolden(const RID &p_font_rid, double p_strength) {
  1888. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1889. ERR_FAIL_COND(!fd);
  1890. MutexLock lock(fd->mutex);
  1891. if (fd->embolden != p_strength) {
  1892. _font_clear_cache(fd);
  1893. fd->embolden = p_strength;
  1894. }
  1895. }
  1896. double TextServerAdvanced::font_get_embolden(const RID &p_font_rid) const {
  1897. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1898. ERR_FAIL_COND_V(!fd, 0.0);
  1899. MutexLock lock(fd->mutex);
  1900. return fd->embolden;
  1901. }
  1902. void TextServerAdvanced::font_set_transform(const RID &p_font_rid, const Transform2D &p_transform) {
  1903. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1904. ERR_FAIL_COND(!fd);
  1905. MutexLock lock(fd->mutex);
  1906. if (fd->transform != p_transform) {
  1907. _font_clear_cache(fd);
  1908. fd->transform = p_transform;
  1909. }
  1910. }
  1911. Transform2D TextServerAdvanced::font_get_transform(const RID &p_font_rid) const {
  1912. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1913. ERR_FAIL_COND_V(!fd, Transform2D());
  1914. MutexLock lock(fd->mutex);
  1915. return fd->transform;
  1916. }
  1917. void TextServerAdvanced::font_set_variation_coordinates(const RID &p_font_rid, const Dictionary &p_variation_coordinates) {
  1918. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1919. ERR_FAIL_COND(!fd);
  1920. MutexLock lock(fd->mutex);
  1921. if (fd->variation_coordinates != p_variation_coordinates) {
  1922. _font_clear_cache(fd);
  1923. fd->variation_coordinates = p_variation_coordinates;
  1924. }
  1925. }
  1926. Dictionary TextServerAdvanced::font_get_variation_coordinates(const RID &p_font_rid) const {
  1927. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1928. ERR_FAIL_COND_V(!fd, Dictionary());
  1929. MutexLock lock(fd->mutex);
  1930. return fd->variation_coordinates;
  1931. }
  1932. void TextServerAdvanced::font_set_oversampling(const RID &p_font_rid, double p_oversampling) {
  1933. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1934. ERR_FAIL_COND(!fd);
  1935. MutexLock lock(fd->mutex);
  1936. if (fd->oversampling != p_oversampling) {
  1937. _font_clear_cache(fd);
  1938. fd->oversampling = p_oversampling;
  1939. }
  1940. }
  1941. double TextServerAdvanced::font_get_oversampling(const RID &p_font_rid) const {
  1942. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1943. ERR_FAIL_COND_V(!fd, 0.0);
  1944. MutexLock lock(fd->mutex);
  1945. return fd->oversampling;
  1946. }
  1947. TypedArray<Vector2i> TextServerAdvanced::font_get_size_cache_list(const RID &p_font_rid) const {
  1948. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1949. ERR_FAIL_COND_V(!fd, TypedArray<Vector2i>());
  1950. MutexLock lock(fd->mutex);
  1951. TypedArray<Vector2i> ret;
  1952. for (const KeyValue<Vector2i, FontForSizeAdvanced *> &E : fd->cache) {
  1953. ret.push_back(E.key);
  1954. }
  1955. return ret;
  1956. }
  1957. void TextServerAdvanced::font_clear_size_cache(const RID &p_font_rid) {
  1958. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1959. ERR_FAIL_COND(!fd);
  1960. MutexLock lock(fd->mutex);
  1961. for (const KeyValue<Vector2i, FontForSizeAdvanced *> &E : fd->cache) {
  1962. memdelete(E.value);
  1963. }
  1964. fd->cache.clear();
  1965. }
  1966. void TextServerAdvanced::font_remove_size_cache(const RID &p_font_rid, const Vector2i &p_size) {
  1967. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1968. ERR_FAIL_COND(!fd);
  1969. MutexLock lock(fd->mutex);
  1970. if (fd->cache.has(p_size)) {
  1971. memdelete(fd->cache[p_size]);
  1972. fd->cache.erase(p_size);
  1973. }
  1974. }
  1975. void TextServerAdvanced::font_set_ascent(const RID &p_font_rid, int64_t p_size, double p_ascent) {
  1976. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1977. ERR_FAIL_COND(!fd);
  1978. MutexLock lock(fd->mutex);
  1979. Vector2i size = _get_size(fd, p_size);
  1980. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  1981. fd->cache[size]->ascent = p_ascent;
  1982. }
  1983. double TextServerAdvanced::font_get_ascent(const RID &p_font_rid, int64_t p_size) const {
  1984. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1985. ERR_FAIL_COND_V(!fd, 0.0);
  1986. MutexLock lock(fd->mutex);
  1987. Vector2i size = _get_size(fd, p_size);
  1988. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), 0.0);
  1989. if (fd->msdf) {
  1990. return fd->cache[size]->ascent * (double)p_size / (double)fd->msdf_source_size;
  1991. } else {
  1992. return fd->cache[size]->ascent;
  1993. }
  1994. }
  1995. void TextServerAdvanced::font_set_descent(const RID &p_font_rid, int64_t p_size, double p_descent) {
  1996. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1997. ERR_FAIL_COND(!fd);
  1998. Vector2i size = _get_size(fd, p_size);
  1999. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  2000. fd->cache[size]->descent = p_descent;
  2001. }
  2002. double TextServerAdvanced::font_get_descent(const RID &p_font_rid, int64_t p_size) const {
  2003. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2004. ERR_FAIL_COND_V(!fd, 0.0);
  2005. MutexLock lock(fd->mutex);
  2006. Vector2i size = _get_size(fd, p_size);
  2007. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), 0.0);
  2008. if (fd->msdf) {
  2009. return fd->cache[size]->descent * (double)p_size / (double)fd->msdf_source_size;
  2010. } else {
  2011. return fd->cache[size]->descent;
  2012. }
  2013. }
  2014. void TextServerAdvanced::font_set_underline_position(const RID &p_font_rid, int64_t p_size, double p_underline_position) {
  2015. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2016. ERR_FAIL_COND(!fd);
  2017. MutexLock lock(fd->mutex);
  2018. Vector2i size = _get_size(fd, p_size);
  2019. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  2020. fd->cache[size]->underline_position = p_underline_position;
  2021. }
  2022. double TextServerAdvanced::font_get_underline_position(const RID &p_font_rid, int64_t p_size) const {
  2023. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2024. ERR_FAIL_COND_V(!fd, 0.0);
  2025. MutexLock lock(fd->mutex);
  2026. Vector2i size = _get_size(fd, p_size);
  2027. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), 0.0);
  2028. if (fd->msdf) {
  2029. return fd->cache[size]->underline_position * (double)p_size / (double)fd->msdf_source_size;
  2030. } else {
  2031. return fd->cache[size]->underline_position;
  2032. }
  2033. }
  2034. void TextServerAdvanced::font_set_underline_thickness(const RID &p_font_rid, int64_t p_size, double p_underline_thickness) {
  2035. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2036. ERR_FAIL_COND(!fd);
  2037. MutexLock lock(fd->mutex);
  2038. Vector2i size = _get_size(fd, p_size);
  2039. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  2040. fd->cache[size]->underline_thickness = p_underline_thickness;
  2041. }
  2042. double TextServerAdvanced::font_get_underline_thickness(const RID &p_font_rid, int64_t p_size) const {
  2043. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2044. ERR_FAIL_COND_V(!fd, 0.0);
  2045. MutexLock lock(fd->mutex);
  2046. Vector2i size = _get_size(fd, p_size);
  2047. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), 0.0);
  2048. if (fd->msdf) {
  2049. return fd->cache[size]->underline_thickness * (double)p_size / (double)fd->msdf_source_size;
  2050. } else {
  2051. return fd->cache[size]->underline_thickness;
  2052. }
  2053. }
  2054. void TextServerAdvanced::font_set_scale(const RID &p_font_rid, int64_t p_size, double p_scale) {
  2055. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2056. ERR_FAIL_COND(!fd);
  2057. MutexLock lock(fd->mutex);
  2058. Vector2i size = _get_size(fd, p_size);
  2059. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  2060. #ifdef MODULE_FREETYPE_ENABLED
  2061. if (fd->cache[size]->face) {
  2062. return; // Do not override scale for dynamic fonts, it's calculated automatically.
  2063. }
  2064. #endif
  2065. fd->cache[size]->scale = p_scale;
  2066. }
  2067. double TextServerAdvanced::font_get_scale(const RID &p_font_rid, int64_t p_size) const {
  2068. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2069. ERR_FAIL_COND_V(!fd, 0.0);
  2070. MutexLock lock(fd->mutex);
  2071. Vector2i size = _get_size(fd, p_size);
  2072. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), 0.0);
  2073. if (fd->msdf) {
  2074. return fd->cache[size]->scale * (double)p_size / (double)fd->msdf_source_size;
  2075. } else {
  2076. return fd->cache[size]->scale / fd->cache[size]->oversampling;
  2077. }
  2078. }
  2079. int64_t TextServerAdvanced::font_get_texture_count(const RID &p_font_rid, const Vector2i &p_size) const {
  2080. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2081. ERR_FAIL_COND_V(!fd, 0);
  2082. MutexLock lock(fd->mutex);
  2083. Vector2i size = _get_size_outline(fd, p_size);
  2084. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), 0);
  2085. return fd->cache[size]->textures.size();
  2086. }
  2087. void TextServerAdvanced::font_clear_textures(const RID &p_font_rid, const Vector2i &p_size) {
  2088. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2089. ERR_FAIL_COND(!fd);
  2090. MutexLock lock(fd->mutex);
  2091. Vector2i size = _get_size_outline(fd, p_size);
  2092. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  2093. fd->cache[size]->textures.clear();
  2094. }
  2095. void TextServerAdvanced::font_remove_texture(const RID &p_font_rid, const Vector2i &p_size, int64_t p_texture_index) {
  2096. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2097. ERR_FAIL_COND(!fd);
  2098. MutexLock lock(fd->mutex);
  2099. Vector2i size = _get_size_outline(fd, p_size);
  2100. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  2101. ERR_FAIL_INDEX(p_texture_index, fd->cache[size]->textures.size());
  2102. fd->cache[size]->textures.remove_at(p_texture_index);
  2103. }
  2104. void TextServerAdvanced::font_set_texture_image(const RID &p_font_rid, const Vector2i &p_size, int64_t p_texture_index, const Ref<Image> &p_image) {
  2105. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2106. ERR_FAIL_COND(!fd);
  2107. ERR_FAIL_COND(p_image.is_null());
  2108. MutexLock lock(fd->mutex);
  2109. Vector2i size = _get_size_outline(fd, p_size);
  2110. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  2111. ERR_FAIL_COND(p_texture_index < 0);
  2112. if (p_texture_index >= fd->cache[size]->textures.size()) {
  2113. fd->cache[size]->textures.resize(p_texture_index + 1);
  2114. }
  2115. FontTexture &tex = fd->cache[size]->textures.write[p_texture_index];
  2116. tex.imgdata = p_image->get_data();
  2117. tex.texture_w = p_image->get_width();
  2118. tex.texture_h = p_image->get_height();
  2119. tex.format = p_image->get_format();
  2120. Ref<Image> img;
  2121. img.instantiate();
  2122. img->create_from_data(tex.texture_w, tex.texture_h, false, tex.format, tex.imgdata);
  2123. if (fd->mipmaps) {
  2124. img->generate_mipmaps();
  2125. }
  2126. tex.texture = ImageTexture::create_from_image(img);
  2127. tex.dirty = false;
  2128. }
  2129. Ref<Image> TextServerAdvanced::font_get_texture_image(const RID &p_font_rid, const Vector2i &p_size, int64_t p_texture_index) const {
  2130. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2131. ERR_FAIL_COND_V(!fd, Ref<Image>());
  2132. MutexLock lock(fd->mutex);
  2133. Vector2i size = _get_size_outline(fd, p_size);
  2134. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), Ref<Image>());
  2135. ERR_FAIL_INDEX_V(p_texture_index, fd->cache[size]->textures.size(), Ref<Image>());
  2136. const FontTexture &tex = fd->cache[size]->textures[p_texture_index];
  2137. Ref<Image> img;
  2138. img.instantiate();
  2139. img->create_from_data(tex.texture_w, tex.texture_h, false, tex.format, tex.imgdata);
  2140. return img;
  2141. }
  2142. void TextServerAdvanced::font_set_texture_offsets(const RID &p_font_rid, const Vector2i &p_size, int64_t p_texture_index, const PackedInt32Array &p_offset) {
  2143. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2144. ERR_FAIL_COND(!fd);
  2145. MutexLock lock(fd->mutex);
  2146. Vector2i size = _get_size_outline(fd, p_size);
  2147. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  2148. ERR_FAIL_COND(p_texture_index < 0);
  2149. if (p_texture_index >= fd->cache[size]->textures.size()) {
  2150. fd->cache[size]->textures.resize(p_texture_index + 1);
  2151. }
  2152. FontTexture &tex = fd->cache[size]->textures.write[p_texture_index];
  2153. tex.offsets = p_offset;
  2154. }
  2155. PackedInt32Array TextServerAdvanced::font_get_texture_offsets(const RID &p_font_rid, const Vector2i &p_size, int64_t p_texture_index) const {
  2156. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2157. ERR_FAIL_COND_V(!fd, PackedInt32Array());
  2158. MutexLock lock(fd->mutex);
  2159. Vector2i size = _get_size_outline(fd, p_size);
  2160. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), PackedInt32Array());
  2161. ERR_FAIL_INDEX_V(p_texture_index, fd->cache[size]->textures.size(), PackedInt32Array());
  2162. const FontTexture &tex = fd->cache[size]->textures[p_texture_index];
  2163. return tex.offsets;
  2164. }
  2165. PackedInt32Array TextServerAdvanced::font_get_glyph_list(const RID &p_font_rid, const Vector2i &p_size) const {
  2166. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2167. ERR_FAIL_COND_V(!fd, PackedInt32Array());
  2168. MutexLock lock(fd->mutex);
  2169. Vector2i size = _get_size_outline(fd, p_size);
  2170. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), PackedInt32Array());
  2171. PackedInt32Array ret;
  2172. const HashMap<int32_t, FontGlyph> &gl = fd->cache[size]->glyph_map;
  2173. for (const KeyValue<int32_t, FontGlyph> &E : gl) {
  2174. ret.push_back(E.key);
  2175. }
  2176. return ret;
  2177. }
  2178. void TextServerAdvanced::font_clear_glyphs(const RID &p_font_rid, const Vector2i &p_size) {
  2179. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2180. ERR_FAIL_COND(!fd);
  2181. MutexLock lock(fd->mutex);
  2182. Vector2i size = _get_size_outline(fd, p_size);
  2183. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  2184. fd->cache[size]->glyph_map.clear();
  2185. }
  2186. void TextServerAdvanced::font_remove_glyph(const RID &p_font_rid, const Vector2i &p_size, int64_t p_glyph) {
  2187. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2188. ERR_FAIL_COND(!fd);
  2189. MutexLock lock(fd->mutex);
  2190. Vector2i size = _get_size_outline(fd, p_size);
  2191. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  2192. fd->cache[size]->glyph_map.erase(p_glyph);
  2193. }
  2194. double TextServerAdvanced::_get_extra_advance(RID p_font_rid, int p_font_size) const {
  2195. const FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2196. ERR_FAIL_COND_V(!fd, 0.0);
  2197. MutexLock lock(fd->mutex);
  2198. Vector2i size = _get_size(fd, p_font_size);
  2199. if (fd->embolden != 0.0) {
  2200. return fd->embolden * double(size.x) / 64.0;
  2201. } else {
  2202. return 0.0;
  2203. }
  2204. }
  2205. Vector2 TextServerAdvanced::font_get_glyph_advance(const RID &p_font_rid, int64_t p_size, int64_t p_glyph) const {
  2206. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2207. ERR_FAIL_COND_V(!fd, Vector2());
  2208. MutexLock lock(fd->mutex);
  2209. Vector2i size = _get_size(fd, p_size);
  2210. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), Vector2());
  2211. int mod = 0;
  2212. if (fd->antialiasing == FONT_ANTIALIASING_LCD) {
  2213. TextServer::FontLCDSubpixelLayout layout = (TextServer::FontLCDSubpixelLayout)(int)GLOBAL_GET("gui/theme/lcd_subpixel_layout");
  2214. if (layout != FONT_LCD_SUBPIXEL_LAYOUT_NONE) {
  2215. mod = (layout << 24);
  2216. }
  2217. }
  2218. if (!_ensure_glyph(fd, size, p_glyph | mod)) {
  2219. return Vector2(); // Invalid or non graphicl glyph, do not display errors.
  2220. }
  2221. const HashMap<int32_t, FontGlyph> &gl = fd->cache[size]->glyph_map;
  2222. Vector2 ea;
  2223. if (fd->embolden != 0.0) {
  2224. ea.x = fd->embolden * double(size.x) / 64.0;
  2225. }
  2226. if (fd->msdf) {
  2227. return (gl[p_glyph | mod].advance + ea) * (double)p_size / (double)fd->msdf_source_size;
  2228. } else if ((fd->subpixel_positioning == SUBPIXEL_POSITIONING_DISABLED) || (fd->subpixel_positioning == SUBPIXEL_POSITIONING_AUTO && size.x > SUBPIXEL_POSITIONING_ONE_HALF_MAX_SIZE)) {
  2229. return (gl[p_glyph | mod].advance + ea).round();
  2230. } else {
  2231. return gl[p_glyph | mod].advance + ea;
  2232. }
  2233. }
  2234. void TextServerAdvanced::font_set_glyph_advance(const RID &p_font_rid, int64_t p_size, int64_t p_glyph, const Vector2 &p_advance) {
  2235. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2236. ERR_FAIL_COND(!fd);
  2237. MutexLock lock(fd->mutex);
  2238. Vector2i size = _get_size(fd, p_size);
  2239. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  2240. HashMap<int32_t, FontGlyph> &gl = fd->cache[size]->glyph_map;
  2241. gl[p_glyph].advance = p_advance;
  2242. gl[p_glyph].found = true;
  2243. }
  2244. Vector2 TextServerAdvanced::font_get_glyph_offset(const RID &p_font_rid, const Vector2i &p_size, int64_t p_glyph) const {
  2245. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2246. ERR_FAIL_COND_V(!fd, Vector2());
  2247. MutexLock lock(fd->mutex);
  2248. Vector2i size = _get_size_outline(fd, p_size);
  2249. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), Vector2());
  2250. int mod = 0;
  2251. if (fd->antialiasing == FONT_ANTIALIASING_LCD) {
  2252. TextServer::FontLCDSubpixelLayout layout = (TextServer::FontLCDSubpixelLayout)(int)GLOBAL_GET("gui/theme/lcd_subpixel_layout");
  2253. if (layout != FONT_LCD_SUBPIXEL_LAYOUT_NONE) {
  2254. mod = (layout << 24);
  2255. }
  2256. }
  2257. if (!_ensure_glyph(fd, size, p_glyph | mod)) {
  2258. return Vector2(); // Invalid or non graphicl glyph, do not display errors.
  2259. }
  2260. const HashMap<int32_t, FontGlyph> &gl = fd->cache[size]->glyph_map;
  2261. if (fd->msdf) {
  2262. return gl[p_glyph | mod].rect.position * (double)p_size.x / (double)fd->msdf_source_size;
  2263. } else {
  2264. return gl[p_glyph | mod].rect.position;
  2265. }
  2266. }
  2267. void TextServerAdvanced::font_set_glyph_offset(const RID &p_font_rid, const Vector2i &p_size, int64_t p_glyph, const Vector2 &p_offset) {
  2268. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2269. ERR_FAIL_COND(!fd);
  2270. MutexLock lock(fd->mutex);
  2271. Vector2i size = _get_size_outline(fd, p_size);
  2272. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  2273. HashMap<int32_t, FontGlyph> &gl = fd->cache[size]->glyph_map;
  2274. gl[p_glyph].rect.position = p_offset;
  2275. gl[p_glyph].found = true;
  2276. }
  2277. Vector2 TextServerAdvanced::font_get_glyph_size(const RID &p_font_rid, const Vector2i &p_size, int64_t p_glyph) const {
  2278. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2279. ERR_FAIL_COND_V(!fd, Vector2());
  2280. MutexLock lock(fd->mutex);
  2281. Vector2i size = _get_size_outline(fd, p_size);
  2282. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), Vector2());
  2283. int mod = 0;
  2284. if (fd->antialiasing == FONT_ANTIALIASING_LCD) {
  2285. TextServer::FontLCDSubpixelLayout layout = (TextServer::FontLCDSubpixelLayout)(int)GLOBAL_GET("gui/theme/lcd_subpixel_layout");
  2286. if (layout != FONT_LCD_SUBPIXEL_LAYOUT_NONE) {
  2287. mod = (layout << 24);
  2288. }
  2289. }
  2290. if (!_ensure_glyph(fd, size, p_glyph | mod)) {
  2291. return Vector2(); // Invalid or non graphicl glyph, do not display errors.
  2292. }
  2293. const HashMap<int32_t, FontGlyph> &gl = fd->cache[size]->glyph_map;
  2294. if (fd->msdf) {
  2295. return gl[p_glyph | mod].rect.size * (double)p_size.x / (double)fd->msdf_source_size;
  2296. } else {
  2297. return gl[p_glyph | mod].rect.size;
  2298. }
  2299. }
  2300. void TextServerAdvanced::font_set_glyph_size(const RID &p_font_rid, const Vector2i &p_size, int64_t p_glyph, const Vector2 &p_gl_size) {
  2301. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2302. ERR_FAIL_COND(!fd);
  2303. MutexLock lock(fd->mutex);
  2304. Vector2i size = _get_size_outline(fd, p_size);
  2305. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  2306. HashMap<int32_t, FontGlyph> &gl = fd->cache[size]->glyph_map;
  2307. gl[p_glyph].rect.size = p_gl_size;
  2308. gl[p_glyph].found = true;
  2309. }
  2310. Rect2 TextServerAdvanced::font_get_glyph_uv_rect(const RID &p_font_rid, const Vector2i &p_size, int64_t p_glyph) const {
  2311. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2312. ERR_FAIL_COND_V(!fd, Rect2());
  2313. MutexLock lock(fd->mutex);
  2314. Vector2i size = _get_size_outline(fd, p_size);
  2315. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), Rect2());
  2316. int mod = 0;
  2317. if (fd->antialiasing == FONT_ANTIALIASING_LCD) {
  2318. TextServer::FontLCDSubpixelLayout layout = (TextServer::FontLCDSubpixelLayout)(int)GLOBAL_GET("gui/theme/lcd_subpixel_layout");
  2319. if (layout != FONT_LCD_SUBPIXEL_LAYOUT_NONE) {
  2320. mod = (layout << 24);
  2321. }
  2322. }
  2323. if (!_ensure_glyph(fd, size, p_glyph | mod)) {
  2324. return Rect2(); // Invalid or non graphicl glyph, do not display errors.
  2325. }
  2326. const HashMap<int32_t, FontGlyph> &gl = fd->cache[size]->glyph_map;
  2327. return gl[p_glyph | mod].uv_rect;
  2328. }
  2329. void TextServerAdvanced::font_set_glyph_uv_rect(const RID &p_font_rid, const Vector2i &p_size, int64_t p_glyph, const Rect2 &p_uv_rect) {
  2330. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2331. ERR_FAIL_COND(!fd);
  2332. MutexLock lock(fd->mutex);
  2333. Vector2i size = _get_size_outline(fd, p_size);
  2334. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  2335. HashMap<int32_t, FontGlyph> &gl = fd->cache[size]->glyph_map;
  2336. gl[p_glyph].uv_rect = p_uv_rect;
  2337. gl[p_glyph].found = true;
  2338. }
  2339. int64_t TextServerAdvanced::font_get_glyph_texture_idx(const RID &p_font_rid, const Vector2i &p_size, int64_t p_glyph) const {
  2340. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2341. ERR_FAIL_COND_V(!fd, -1);
  2342. MutexLock lock(fd->mutex);
  2343. Vector2i size = _get_size_outline(fd, p_size);
  2344. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), -1);
  2345. int mod = 0;
  2346. if (fd->antialiasing == FONT_ANTIALIASING_LCD) {
  2347. TextServer::FontLCDSubpixelLayout layout = (TextServer::FontLCDSubpixelLayout)(int)GLOBAL_GET("gui/theme/lcd_subpixel_layout");
  2348. if (layout != FONT_LCD_SUBPIXEL_LAYOUT_NONE) {
  2349. mod = (layout << 24);
  2350. }
  2351. }
  2352. if (!_ensure_glyph(fd, size, p_glyph | mod)) {
  2353. return -1; // Invalid or non graphicl glyph, do not display errors.
  2354. }
  2355. const HashMap<int32_t, FontGlyph> &gl = fd->cache[size]->glyph_map;
  2356. return gl[p_glyph | mod].texture_idx;
  2357. }
  2358. void TextServerAdvanced::font_set_glyph_texture_idx(const RID &p_font_rid, const Vector2i &p_size, int64_t p_glyph, int64_t p_texture_idx) {
  2359. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2360. ERR_FAIL_COND(!fd);
  2361. MutexLock lock(fd->mutex);
  2362. Vector2i size = _get_size_outline(fd, p_size);
  2363. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  2364. HashMap<int32_t, FontGlyph> &gl = fd->cache[size]->glyph_map;
  2365. gl[p_glyph].texture_idx = p_texture_idx;
  2366. gl[p_glyph].found = true;
  2367. }
  2368. RID TextServerAdvanced::font_get_glyph_texture_rid(const RID &p_font_rid, const Vector2i &p_size, int64_t p_glyph) const {
  2369. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2370. ERR_FAIL_COND_V(!fd, RID());
  2371. MutexLock lock(fd->mutex);
  2372. Vector2i size = _get_size_outline(fd, p_size);
  2373. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), RID());
  2374. int mod = 0;
  2375. if (fd->antialiasing == FONT_ANTIALIASING_LCD) {
  2376. TextServer::FontLCDSubpixelLayout layout = (TextServer::FontLCDSubpixelLayout)(int)GLOBAL_GET("gui/theme/lcd_subpixel_layout");
  2377. if (layout != FONT_LCD_SUBPIXEL_LAYOUT_NONE) {
  2378. mod = (layout << 24);
  2379. }
  2380. }
  2381. if (!_ensure_glyph(fd, size, p_glyph | mod)) {
  2382. return RID(); // Invalid or non graphicl glyph, do not display errors.
  2383. }
  2384. const HashMap<int32_t, FontGlyph> &gl = fd->cache[size]->glyph_map;
  2385. ERR_FAIL_COND_V(gl[p_glyph | mod].texture_idx < -1 || gl[p_glyph | mod].texture_idx >= fd->cache[size]->textures.size(), RID());
  2386. if (RenderingServer::get_singleton() != nullptr) {
  2387. if (gl[p_glyph | mod].texture_idx != -1) {
  2388. if (fd->cache[size]->textures[gl[p_glyph | mod].texture_idx].dirty) {
  2389. FontTexture &tex = fd->cache[size]->textures.write[gl[p_glyph | mod].texture_idx];
  2390. Ref<Image> img;
  2391. img.instantiate();
  2392. img->create_from_data(tex.texture_w, tex.texture_h, false, tex.format, tex.imgdata);
  2393. if (fd->mipmaps) {
  2394. img->generate_mipmaps();
  2395. }
  2396. if (tex.texture.is_null()) {
  2397. tex.texture = ImageTexture::create_from_image(img);
  2398. } else {
  2399. tex.texture->update(img);
  2400. }
  2401. tex.dirty = false;
  2402. }
  2403. return fd->cache[size]->textures[gl[p_glyph | mod].texture_idx].texture->get_rid();
  2404. }
  2405. }
  2406. return RID();
  2407. }
  2408. Size2 TextServerAdvanced::font_get_glyph_texture_size(const RID &p_font_rid, const Vector2i &p_size, int64_t p_glyph) const {
  2409. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2410. ERR_FAIL_COND_V(!fd, Size2());
  2411. MutexLock lock(fd->mutex);
  2412. Vector2i size = _get_size_outline(fd, p_size);
  2413. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), Size2());
  2414. int mod = 0;
  2415. if (fd->antialiasing == FONT_ANTIALIASING_LCD) {
  2416. TextServer::FontLCDSubpixelLayout layout = (TextServer::FontLCDSubpixelLayout)(int)GLOBAL_GET("gui/theme/lcd_subpixel_layout");
  2417. if (layout != FONT_LCD_SUBPIXEL_LAYOUT_NONE) {
  2418. mod = (layout << 24);
  2419. }
  2420. }
  2421. if (!_ensure_glyph(fd, size, p_glyph | mod)) {
  2422. return Size2(); // Invalid or non graphicl glyph, do not display errors.
  2423. }
  2424. const HashMap<int32_t, FontGlyph> &gl = fd->cache[size]->glyph_map;
  2425. ERR_FAIL_COND_V(gl[p_glyph | mod].texture_idx < -1 || gl[p_glyph | mod].texture_idx >= fd->cache[size]->textures.size(), Size2());
  2426. if (RenderingServer::get_singleton() != nullptr) {
  2427. if (gl[p_glyph | mod].texture_idx != -1) {
  2428. if (fd->cache[size]->textures[gl[p_glyph | mod].texture_idx].dirty) {
  2429. FontTexture &tex = fd->cache[size]->textures.write[gl[p_glyph | mod].texture_idx];
  2430. Ref<Image> img;
  2431. img.instantiate();
  2432. img->create_from_data(tex.texture_w, tex.texture_h, false, tex.format, tex.imgdata);
  2433. if (fd->mipmaps) {
  2434. img->generate_mipmaps();
  2435. }
  2436. if (tex.texture.is_null()) {
  2437. tex.texture = ImageTexture::create_from_image(img);
  2438. } else {
  2439. tex.texture->update(img);
  2440. }
  2441. tex.dirty = false;
  2442. }
  2443. return fd->cache[size]->textures[gl[p_glyph | mod].texture_idx].texture->get_size();
  2444. }
  2445. }
  2446. return Size2();
  2447. }
  2448. Dictionary TextServerAdvanced::font_get_glyph_contours(const RID &p_font_rid, int64_t p_size, int64_t p_index) const {
  2449. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2450. ERR_FAIL_COND_V(!fd, Dictionary());
  2451. MutexLock lock(fd->mutex);
  2452. Vector2i size = _get_size(fd, p_size);
  2453. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), Dictionary());
  2454. #ifdef MODULE_FREETYPE_ENABLED
  2455. PackedVector3Array points;
  2456. PackedInt32Array contours;
  2457. int32_t index = p_index & 0xffffff; // Remove subpixel shifts.
  2458. int error = FT_Load_Glyph(fd->cache[size]->face, index, FT_LOAD_NO_BITMAP | (fd->force_autohinter ? FT_LOAD_FORCE_AUTOHINT : 0));
  2459. ERR_FAIL_COND_V(error, Dictionary());
  2460. if (fd->embolden != 0.f) {
  2461. FT_Pos strength = fd->embolden * p_size * 4; // 26.6 fractional units (1 / 64).
  2462. FT_Outline_Embolden(&fd->cache[size]->face->glyph->outline, strength);
  2463. }
  2464. if (fd->transform != Transform2D()) {
  2465. FT_Matrix mat = { FT_Fixed(fd->transform[0][0] * 65536), FT_Fixed(fd->transform[0][1] * 65536), FT_Fixed(fd->transform[1][0] * 65536), FT_Fixed(fd->transform[1][1] * 65536) }; // 16.16 fractional units (1 / 65536).
  2466. FT_Outline_Transform(&fd->cache[size]->face->glyph->outline, &mat);
  2467. }
  2468. double scale = (1.0 / 64.0) / fd->cache[size]->oversampling * fd->cache[size]->scale;
  2469. if (fd->msdf) {
  2470. scale = scale * (double)p_size / (double)fd->msdf_source_size;
  2471. }
  2472. for (short i = 0; i < fd->cache[size]->face->glyph->outline.n_points; i++) {
  2473. points.push_back(Vector3(fd->cache[size]->face->glyph->outline.points[i].x * scale, -fd->cache[size]->face->glyph->outline.points[i].y * scale, FT_CURVE_TAG(fd->cache[size]->face->glyph->outline.tags[i])));
  2474. }
  2475. for (short i = 0; i < fd->cache[size]->face->glyph->outline.n_contours; i++) {
  2476. contours.push_back(fd->cache[size]->face->glyph->outline.contours[i]);
  2477. }
  2478. bool orientation = (FT_Outline_Get_Orientation(&fd->cache[size]->face->glyph->outline) == FT_ORIENTATION_FILL_RIGHT);
  2479. Dictionary out;
  2480. out["points"] = points;
  2481. out["contours"] = contours;
  2482. out["orientation"] = orientation;
  2483. return out;
  2484. #else
  2485. return Dictionary();
  2486. #endif
  2487. }
  2488. TypedArray<Vector2i> TextServerAdvanced::font_get_kerning_list(const RID &p_font_rid, int64_t p_size) const {
  2489. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2490. ERR_FAIL_COND_V(!fd, TypedArray<Vector2i>());
  2491. MutexLock lock(fd->mutex);
  2492. Vector2i size = _get_size(fd, p_size);
  2493. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), TypedArray<Vector2i>());
  2494. TypedArray<Vector2i> ret;
  2495. for (const KeyValue<Vector2i, FontForSizeAdvanced *> &E : fd->cache) {
  2496. ret.push_back(E.key);
  2497. }
  2498. return ret;
  2499. }
  2500. void TextServerAdvanced::font_clear_kerning_map(const RID &p_font_rid, int64_t p_size) {
  2501. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2502. ERR_FAIL_COND(!fd);
  2503. MutexLock lock(fd->mutex);
  2504. Vector2i size = _get_size(fd, p_size);
  2505. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  2506. fd->cache[size]->kerning_map.clear();
  2507. }
  2508. void TextServerAdvanced::font_remove_kerning(const RID &p_font_rid, int64_t p_size, const Vector2i &p_glyph_pair) {
  2509. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2510. ERR_FAIL_COND(!fd);
  2511. MutexLock lock(fd->mutex);
  2512. Vector2i size = _get_size(fd, p_size);
  2513. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  2514. fd->cache[size]->kerning_map.erase(p_glyph_pair);
  2515. }
  2516. void TextServerAdvanced::font_set_kerning(const RID &p_font_rid, int64_t p_size, const Vector2i &p_glyph_pair, const Vector2 &p_kerning) {
  2517. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2518. ERR_FAIL_COND(!fd);
  2519. MutexLock lock(fd->mutex);
  2520. Vector2i size = _get_size(fd, p_size);
  2521. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  2522. fd->cache[size]->kerning_map[p_glyph_pair] = p_kerning;
  2523. }
  2524. Vector2 TextServerAdvanced::font_get_kerning(const RID &p_font_rid, int64_t p_size, const Vector2i &p_glyph_pair) const {
  2525. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2526. ERR_FAIL_COND_V(!fd, Vector2());
  2527. MutexLock lock(fd->mutex);
  2528. Vector2i size = _get_size(fd, p_size);
  2529. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), Vector2());
  2530. const HashMap<Vector2i, Vector2> &kern = fd->cache[size]->kerning_map;
  2531. if (kern.has(p_glyph_pair)) {
  2532. if (fd->msdf) {
  2533. return kern[p_glyph_pair] * (double)p_size / (double)fd->msdf_source_size;
  2534. } else {
  2535. return kern[p_glyph_pair];
  2536. }
  2537. } else {
  2538. #ifdef MODULE_FREETYPE_ENABLED
  2539. if (fd->cache[size]->face) {
  2540. FT_Vector delta;
  2541. FT_Get_Kerning(fd->cache[size]->face, p_glyph_pair.x, p_glyph_pair.y, FT_KERNING_DEFAULT, &delta);
  2542. if (fd->msdf) {
  2543. return Vector2(delta.x, delta.y) * (double)p_size / (double)fd->msdf_source_size;
  2544. } else {
  2545. return Vector2(delta.x, delta.y);
  2546. }
  2547. }
  2548. #endif
  2549. }
  2550. return Vector2();
  2551. }
  2552. int64_t TextServerAdvanced::font_get_glyph_index(const RID &p_font_rid, int64_t p_size, int64_t p_char, int64_t p_variation_selector) const {
  2553. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2554. ERR_FAIL_COND_V(!fd, 0);
  2555. ERR_FAIL_COND_V_MSG((p_char >= 0xd800 && p_char <= 0xdfff) || (p_char > 0x10ffff), 0, "Unicode parsing error: Invalid unicode codepoint " + String::num_int64(p_char, 16) + ".");
  2556. ERR_FAIL_COND_V_MSG((p_variation_selector >= 0xd800 && p_variation_selector <= 0xdfff) || (p_variation_selector > 0x10ffff), 0, "Unicode parsing error: Invalid unicode codepoint " + String::num_int64(p_variation_selector, 16) + ".");
  2557. MutexLock lock(fd->mutex);
  2558. Vector2i size = _get_size(fd, p_size);
  2559. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), 0);
  2560. #ifdef MODULE_FREETYPE_ENABLED
  2561. if (fd->cache[size]->face) {
  2562. if (p_variation_selector) {
  2563. return FT_Face_GetCharVariantIndex(fd->cache[size]->face, p_char, p_variation_selector);
  2564. } else {
  2565. return FT_Get_Char_Index(fd->cache[size]->face, p_char);
  2566. }
  2567. } else {
  2568. return (int64_t)p_char;
  2569. }
  2570. #else
  2571. return (int64_t)p_char;
  2572. #endif
  2573. }
  2574. bool TextServerAdvanced::font_has_char(const RID &p_font_rid, int64_t p_char) const {
  2575. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2576. ERR_FAIL_COND_V(!fd, false);
  2577. ERR_FAIL_COND_V_MSG((p_char >= 0xd800 && p_char <= 0xdfff) || (p_char > 0x10ffff), false, "Unicode parsing error: Invalid unicode codepoint " + String::num_int64(p_char, 16) + ".");
  2578. MutexLock lock(fd->mutex);
  2579. if (fd->cache.is_empty()) {
  2580. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, fd->msdf ? Vector2i(fd->msdf_source_size, 0) : Vector2i(16, 0)), false);
  2581. }
  2582. FontForSizeAdvanced *at_size = fd->cache.begin()->value;
  2583. #ifdef MODULE_FREETYPE_ENABLED
  2584. if (at_size && at_size->face) {
  2585. return FT_Get_Char_Index(at_size->face, p_char) != 0;
  2586. }
  2587. #endif
  2588. return (at_size) ? at_size->glyph_map.has((int32_t)p_char) : false;
  2589. }
  2590. String TextServerAdvanced::font_get_supported_chars(const RID &p_font_rid) const {
  2591. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2592. ERR_FAIL_COND_V(!fd, String());
  2593. MutexLock lock(fd->mutex);
  2594. if (fd->cache.is_empty()) {
  2595. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, fd->msdf ? Vector2i(fd->msdf_source_size, 0) : Vector2i(16, 0)), String());
  2596. }
  2597. FontForSizeAdvanced *at_size = fd->cache.begin()->value;
  2598. String chars;
  2599. #ifdef MODULE_FREETYPE_ENABLED
  2600. if (at_size && at_size->face) {
  2601. FT_UInt gindex;
  2602. FT_ULong charcode = FT_Get_First_Char(at_size->face, &gindex);
  2603. while (gindex != 0) {
  2604. if (charcode != 0) {
  2605. chars = chars + String::chr(charcode);
  2606. }
  2607. charcode = FT_Get_Next_Char(at_size->face, charcode, &gindex);
  2608. }
  2609. return chars;
  2610. }
  2611. #endif
  2612. if (at_size) {
  2613. const HashMap<int32_t, FontGlyph> &gl = at_size->glyph_map;
  2614. for (const KeyValue<int32_t, FontGlyph> &E : gl) {
  2615. chars = chars + String::chr(E.key);
  2616. }
  2617. }
  2618. return chars;
  2619. }
  2620. void TextServerAdvanced::font_render_range(const RID &p_font_rid, const Vector2i &p_size, int64_t p_start, int64_t p_end) {
  2621. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2622. ERR_FAIL_COND(!fd);
  2623. ERR_FAIL_COND_MSG((p_start >= 0xd800 && p_start <= 0xdfff) || (p_start > 0x10ffff), "Unicode parsing error: Invalid unicode codepoint " + String::num_int64(p_start, 16) + ".");
  2624. ERR_FAIL_COND_MSG((p_end >= 0xd800 && p_end <= 0xdfff) || (p_end > 0x10ffff), "Unicode parsing error: Invalid unicode codepoint " + String::num_int64(p_end, 16) + ".");
  2625. MutexLock lock(fd->mutex);
  2626. Vector2i size = _get_size_outline(fd, p_size);
  2627. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  2628. for (int64_t i = p_start; i <= p_end; i++) {
  2629. #ifdef MODULE_FREETYPE_ENABLED
  2630. int32_t idx = FT_Get_Char_Index(fd->cache[size]->face, i);
  2631. if (fd->cache[size]->face) {
  2632. if (fd->msdf) {
  2633. _ensure_glyph(fd, size, (int32_t)idx);
  2634. } else {
  2635. for (int aa = 0; aa < ((fd->antialiasing == FONT_ANTIALIASING_LCD) ? FONT_LCD_SUBPIXEL_LAYOUT_MAX : 1); aa++) {
  2636. if ((fd->subpixel_positioning == SUBPIXEL_POSITIONING_ONE_QUARTER) || (fd->subpixel_positioning == SUBPIXEL_POSITIONING_AUTO && size.x <= SUBPIXEL_POSITIONING_ONE_QUARTER_MAX_SIZE)) {
  2637. _ensure_glyph(fd, size, (int32_t)idx | (0 << 27) | (aa << 24));
  2638. _ensure_glyph(fd, size, (int32_t)idx | (1 << 27) | (aa << 24));
  2639. _ensure_glyph(fd, size, (int32_t)idx | (2 << 27) | (aa << 24));
  2640. _ensure_glyph(fd, size, (int32_t)idx | (3 << 27) | (aa << 24));
  2641. } else if ((fd->subpixel_positioning == SUBPIXEL_POSITIONING_ONE_HALF) || (fd->subpixel_positioning == SUBPIXEL_POSITIONING_AUTO && size.x <= SUBPIXEL_POSITIONING_ONE_HALF_MAX_SIZE)) {
  2642. _ensure_glyph(fd, size, (int32_t)idx | (1 << 27) | (aa << 24));
  2643. _ensure_glyph(fd, size, (int32_t)idx | (0 << 27) | (aa << 24));
  2644. } else {
  2645. _ensure_glyph(fd, size, (int32_t)idx | (aa << 24));
  2646. }
  2647. }
  2648. }
  2649. }
  2650. #endif
  2651. }
  2652. }
  2653. void TextServerAdvanced::font_render_glyph(const RID &p_font_rid, const Vector2i &p_size, int64_t p_index) {
  2654. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2655. ERR_FAIL_COND(!fd);
  2656. MutexLock lock(fd->mutex);
  2657. Vector2i size = _get_size_outline(fd, p_size);
  2658. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  2659. #ifdef MODULE_FREETYPE_ENABLED
  2660. int32_t idx = p_index & 0xffffff; // Remove subpixel shifts.
  2661. if (fd->cache[size]->face) {
  2662. if (fd->msdf) {
  2663. _ensure_glyph(fd, size, (int32_t)idx);
  2664. } else {
  2665. for (int aa = 0; aa < ((fd->antialiasing == FONT_ANTIALIASING_LCD) ? FONT_LCD_SUBPIXEL_LAYOUT_MAX : 1); aa++) {
  2666. if ((fd->subpixel_positioning == SUBPIXEL_POSITIONING_ONE_QUARTER) || (fd->subpixel_positioning == SUBPIXEL_POSITIONING_AUTO && size.x <= SUBPIXEL_POSITIONING_ONE_QUARTER_MAX_SIZE)) {
  2667. _ensure_glyph(fd, size, (int32_t)idx | (0 << 27) | (aa << 24));
  2668. _ensure_glyph(fd, size, (int32_t)idx | (1 << 27) | (aa << 24));
  2669. _ensure_glyph(fd, size, (int32_t)idx | (2 << 27) | (aa << 24));
  2670. _ensure_glyph(fd, size, (int32_t)idx | (3 << 27) | (aa << 24));
  2671. } else if ((fd->subpixel_positioning == SUBPIXEL_POSITIONING_ONE_HALF) || (fd->subpixel_positioning == SUBPIXEL_POSITIONING_AUTO && size.x <= SUBPIXEL_POSITIONING_ONE_HALF_MAX_SIZE)) {
  2672. _ensure_glyph(fd, size, (int32_t)idx | (1 << 27) | (aa << 24));
  2673. _ensure_glyph(fd, size, (int32_t)idx | (0 << 27) | (aa << 24));
  2674. } else {
  2675. _ensure_glyph(fd, size, (int32_t)idx | (aa << 24));
  2676. }
  2677. }
  2678. }
  2679. }
  2680. #endif
  2681. }
  2682. void TextServerAdvanced::font_draw_glyph(const RID &p_font_rid, const RID &p_canvas, int64_t p_size, const Vector2 &p_pos, int64_t p_index, const Color &p_color) const {
  2683. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2684. ERR_FAIL_COND(!fd);
  2685. MutexLock lock(fd->mutex);
  2686. Vector2i size = _get_size(fd, p_size);
  2687. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  2688. int32_t index = p_index & 0xffffff; // Remove subpixel shifts.
  2689. bool lcd_aa = false;
  2690. #ifdef MODULE_FREETYPE_ENABLED
  2691. if (!fd->msdf && fd->cache[size]->face) {
  2692. // LCD layout, bits 24, 25, 26
  2693. if (fd->antialiasing == FONT_ANTIALIASING_LCD) {
  2694. TextServer::FontLCDSubpixelLayout layout = (TextServer::FontLCDSubpixelLayout)(int)GLOBAL_GET("gui/theme/lcd_subpixel_layout");
  2695. if (layout != FONT_LCD_SUBPIXEL_LAYOUT_NONE) {
  2696. lcd_aa = true;
  2697. index = index | (layout << 24);
  2698. }
  2699. }
  2700. // Subpixel X-shift, bits 27, 28
  2701. if ((fd->subpixel_positioning == SUBPIXEL_POSITIONING_ONE_QUARTER) || (fd->subpixel_positioning == SUBPIXEL_POSITIONING_AUTO && size.x <= SUBPIXEL_POSITIONING_ONE_QUARTER_MAX_SIZE)) {
  2702. int xshift = (int)(Math::floor(4 * (p_pos.x + 0.125)) - 4 * Math::floor(p_pos.x + 0.125));
  2703. index = index | (xshift << 27);
  2704. } else if ((fd->subpixel_positioning == SUBPIXEL_POSITIONING_ONE_HALF) || (fd->subpixel_positioning == SUBPIXEL_POSITIONING_AUTO && size.x <= SUBPIXEL_POSITIONING_ONE_HALF_MAX_SIZE)) {
  2705. int xshift = (int)(Math::floor(2 * (p_pos.x + 0.25)) - 2 * Math::floor(p_pos.x + 0.25));
  2706. index = index | (xshift << 27);
  2707. }
  2708. }
  2709. #endif
  2710. if (!_ensure_glyph(fd, size, index)) {
  2711. return; // Invalid or non-graphical glyph, do not display errors, nothing to draw.
  2712. }
  2713. const FontGlyph &gl = fd->cache[size]->glyph_map[index];
  2714. if (gl.found) {
  2715. ERR_FAIL_COND(gl.texture_idx < -1 || gl.texture_idx >= fd->cache[size]->textures.size());
  2716. if (gl.texture_idx != -1) {
  2717. Color modulate = p_color;
  2718. #ifdef MODULE_FREETYPE_ENABLED
  2719. if (fd->cache[size]->face && (fd->cache[size]->textures[gl.texture_idx].format == Image::FORMAT_RGBA8) && !lcd_aa && !fd->msdf) {
  2720. modulate.r = modulate.g = modulate.b = 1.0;
  2721. }
  2722. #endif
  2723. if (RenderingServer::get_singleton() != nullptr) {
  2724. if (fd->cache[size]->textures[gl.texture_idx].dirty) {
  2725. FontTexture &tex = fd->cache[size]->textures.write[gl.texture_idx];
  2726. Ref<Image> img;
  2727. img.instantiate();
  2728. img->create_from_data(tex.texture_w, tex.texture_h, false, tex.format, tex.imgdata);
  2729. if (fd->mipmaps) {
  2730. img->generate_mipmaps();
  2731. }
  2732. if (tex.texture.is_null()) {
  2733. tex.texture = ImageTexture::create_from_image(img);
  2734. } else {
  2735. tex.texture->update(img);
  2736. }
  2737. tex.dirty = false;
  2738. }
  2739. RID texture = fd->cache[size]->textures[gl.texture_idx].texture->get_rid();
  2740. if (fd->msdf) {
  2741. Point2 cpos = p_pos;
  2742. cpos += gl.rect.position * (double)p_size / (double)fd->msdf_source_size;
  2743. Size2 csize = gl.rect.size * (double)p_size / (double)fd->msdf_source_size;
  2744. RenderingServer::get_singleton()->canvas_item_add_msdf_texture_rect_region(p_canvas, Rect2(cpos, csize), texture, gl.uv_rect, modulate, 0, fd->msdf_range);
  2745. } else {
  2746. Point2 cpos = p_pos;
  2747. cpos.y = Math::floor(cpos.y);
  2748. if ((fd->subpixel_positioning == SUBPIXEL_POSITIONING_ONE_QUARTER) || (fd->subpixel_positioning == SUBPIXEL_POSITIONING_AUTO && size.x <= SUBPIXEL_POSITIONING_ONE_QUARTER_MAX_SIZE)) {
  2749. cpos.x = ((int)Math::floor(cpos.x + 0.125));
  2750. } else if ((fd->subpixel_positioning == SUBPIXEL_POSITIONING_ONE_HALF) || (fd->subpixel_positioning == SUBPIXEL_POSITIONING_AUTO && size.x <= SUBPIXEL_POSITIONING_ONE_HALF_MAX_SIZE)) {
  2751. cpos.x = ((int)Math::floor(cpos.x + 0.25));
  2752. } else {
  2753. cpos.x = Math::floor(cpos.x);
  2754. }
  2755. cpos += gl.rect.position;
  2756. Size2 csize = gl.rect.size;
  2757. if (lcd_aa) {
  2758. RenderingServer::get_singleton()->canvas_item_add_lcd_texture_rect_region(p_canvas, Rect2(cpos, csize), texture, gl.uv_rect, modulate);
  2759. } else {
  2760. RenderingServer::get_singleton()->canvas_item_add_texture_rect_region(p_canvas, Rect2(cpos, csize), texture, gl.uv_rect, modulate, false, false);
  2761. }
  2762. }
  2763. }
  2764. }
  2765. }
  2766. }
  2767. void TextServerAdvanced::font_draw_glyph_outline(const RID &p_font_rid, const RID &p_canvas, int64_t p_size, int64_t p_outline_size, const Vector2 &p_pos, int64_t p_index, const Color &p_color) const {
  2768. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2769. ERR_FAIL_COND(!fd);
  2770. MutexLock lock(fd->mutex);
  2771. Vector2i size = _get_size_outline(fd, Vector2i(p_size, p_outline_size));
  2772. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  2773. int32_t index = p_index & 0xffffff; // Remove subpixel shifts.
  2774. bool lcd_aa = false;
  2775. #ifdef MODULE_FREETYPE_ENABLED
  2776. if (!fd->msdf && fd->cache[size]->face) {
  2777. // LCD layout, bits 24, 25, 26
  2778. if (fd->antialiasing == FONT_ANTIALIASING_LCD) {
  2779. TextServer::FontLCDSubpixelLayout layout = (TextServer::FontLCDSubpixelLayout)(int)GLOBAL_GET("gui/theme/lcd_subpixel_layout");
  2780. if (layout != FONT_LCD_SUBPIXEL_LAYOUT_NONE) {
  2781. lcd_aa = true;
  2782. index = index | (layout << 24);
  2783. }
  2784. }
  2785. // Subpixel X-shift, bits 27, 28
  2786. if ((fd->subpixel_positioning == SUBPIXEL_POSITIONING_ONE_QUARTER) || (fd->subpixel_positioning == SUBPIXEL_POSITIONING_AUTO && size.x <= SUBPIXEL_POSITIONING_ONE_QUARTER_MAX_SIZE)) {
  2787. int xshift = (int)(Math::floor(4 * (p_pos.x + 0.125)) - 4 * Math::floor(p_pos.x + 0.125));
  2788. index = index | (xshift << 27);
  2789. } else if ((fd->subpixel_positioning == SUBPIXEL_POSITIONING_ONE_HALF) || (fd->subpixel_positioning == SUBPIXEL_POSITIONING_AUTO && size.x <= SUBPIXEL_POSITIONING_ONE_HALF_MAX_SIZE)) {
  2790. int xshift = (int)(Math::floor(2 * (p_pos.x + 0.25)) - 2 * Math::floor(p_pos.x + 0.25));
  2791. index = index | (xshift << 27);
  2792. }
  2793. }
  2794. #endif
  2795. if (!_ensure_glyph(fd, size, index)) {
  2796. return; // Invalid or non-graphical glyph, do not display errors, nothing to draw.
  2797. }
  2798. const FontGlyph &gl = fd->cache[size]->glyph_map[index];
  2799. if (gl.found) {
  2800. ERR_FAIL_COND(gl.texture_idx < -1 || gl.texture_idx >= fd->cache[size]->textures.size());
  2801. if (gl.texture_idx != -1) {
  2802. Color modulate = p_color;
  2803. #ifdef MODULE_FREETYPE_ENABLED
  2804. if (fd->cache[size]->face && (fd->cache[size]->textures[gl.texture_idx].format == Image::FORMAT_RGBA8) && !lcd_aa && !fd->msdf) {
  2805. modulate.r = modulate.g = modulate.b = 1.0;
  2806. }
  2807. #endif
  2808. if (RenderingServer::get_singleton() != nullptr) {
  2809. if (fd->cache[size]->textures[gl.texture_idx].dirty) {
  2810. FontTexture &tex = fd->cache[size]->textures.write[gl.texture_idx];
  2811. Ref<Image> img;
  2812. img.instantiate();
  2813. img->create_from_data(tex.texture_w, tex.texture_h, false, tex.format, tex.imgdata);
  2814. if (fd->mipmaps) {
  2815. img->generate_mipmaps();
  2816. }
  2817. if (tex.texture.is_null()) {
  2818. tex.texture = ImageTexture::create_from_image(img);
  2819. } else {
  2820. tex.texture->update(img);
  2821. }
  2822. tex.dirty = false;
  2823. }
  2824. RID texture = fd->cache[size]->textures[gl.texture_idx].texture->get_rid();
  2825. if (fd->msdf) {
  2826. Point2 cpos = p_pos;
  2827. cpos += gl.rect.position * (double)p_size / (double)fd->msdf_source_size;
  2828. Size2 csize = gl.rect.size * (double)p_size / (double)fd->msdf_source_size;
  2829. RenderingServer::get_singleton()->canvas_item_add_msdf_texture_rect_region(p_canvas, Rect2(cpos, csize), texture, gl.uv_rect, modulate, p_outline_size * 2, fd->msdf_range);
  2830. } else {
  2831. Point2 cpos = p_pos;
  2832. cpos.y = Math::floor(cpos.y);
  2833. if ((fd->subpixel_positioning == SUBPIXEL_POSITIONING_ONE_QUARTER) || (fd->subpixel_positioning == SUBPIXEL_POSITIONING_AUTO && size.x <= SUBPIXEL_POSITIONING_ONE_QUARTER_MAX_SIZE)) {
  2834. cpos.x = ((int)Math::floor(cpos.x + 0.125));
  2835. } else if ((fd->subpixel_positioning == SUBPIXEL_POSITIONING_ONE_HALF) || (fd->subpixel_positioning == SUBPIXEL_POSITIONING_AUTO && size.x <= SUBPIXEL_POSITIONING_ONE_HALF_MAX_SIZE)) {
  2836. cpos.x = ((int)Math::floor(cpos.x + 0.25));
  2837. } else {
  2838. cpos.x = Math::floor(cpos.x);
  2839. }
  2840. cpos += gl.rect.position;
  2841. Size2 csize = gl.rect.size;
  2842. if (lcd_aa) {
  2843. RenderingServer::get_singleton()->canvas_item_add_lcd_texture_rect_region(p_canvas, Rect2(cpos, csize), texture, gl.uv_rect, modulate);
  2844. } else {
  2845. RenderingServer::get_singleton()->canvas_item_add_texture_rect_region(p_canvas, Rect2(cpos, csize), texture, gl.uv_rect, modulate, false, false);
  2846. }
  2847. }
  2848. }
  2849. }
  2850. }
  2851. }
  2852. bool TextServerAdvanced::font_is_language_supported(const RID &p_font_rid, const String &p_language) const {
  2853. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2854. ERR_FAIL_COND_V(!fd, false);
  2855. MutexLock lock(fd->mutex);
  2856. if (fd->language_support_overrides.has(p_language)) {
  2857. return fd->language_support_overrides[p_language];
  2858. } else {
  2859. return true;
  2860. }
  2861. }
  2862. void TextServerAdvanced::font_set_language_support_override(const RID &p_font_rid, const String &p_language, bool p_supported) {
  2863. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2864. ERR_FAIL_COND(!fd);
  2865. MutexLock lock(fd->mutex);
  2866. fd->language_support_overrides[p_language] = p_supported;
  2867. }
  2868. bool TextServerAdvanced::font_get_language_support_override(const RID &p_font_rid, const String &p_language) {
  2869. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2870. ERR_FAIL_COND_V(!fd, false);
  2871. MutexLock lock(fd->mutex);
  2872. return fd->language_support_overrides[p_language];
  2873. }
  2874. void TextServerAdvanced::font_remove_language_support_override(const RID &p_font_rid, const String &p_language) {
  2875. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2876. ERR_FAIL_COND(!fd);
  2877. MutexLock lock(fd->mutex);
  2878. fd->language_support_overrides.erase(p_language);
  2879. }
  2880. PackedStringArray TextServerAdvanced::font_get_language_support_overrides(const RID &p_font_rid) {
  2881. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2882. ERR_FAIL_COND_V(!fd, PackedStringArray());
  2883. MutexLock lock(fd->mutex);
  2884. PackedStringArray out;
  2885. for (const KeyValue<String, bool> &E : fd->language_support_overrides) {
  2886. out.push_back(E.key);
  2887. }
  2888. return out;
  2889. }
  2890. bool TextServerAdvanced::font_is_script_supported(const RID &p_font_rid, const String &p_script) const {
  2891. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2892. ERR_FAIL_COND_V(!fd, false);
  2893. MutexLock lock(fd->mutex);
  2894. if (fd->script_support_overrides.has(p_script)) {
  2895. return fd->script_support_overrides[p_script];
  2896. } else {
  2897. Vector2i size = _get_size(fd, 16);
  2898. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), false);
  2899. return fd->supported_scripts.has(hb_tag_from_string(p_script.ascii().get_data(), -1));
  2900. }
  2901. }
  2902. void TextServerAdvanced::font_set_script_support_override(const RID &p_font_rid, const String &p_script, bool p_supported) {
  2903. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2904. ERR_FAIL_COND(!fd);
  2905. MutexLock lock(fd->mutex);
  2906. fd->script_support_overrides[p_script] = p_supported;
  2907. }
  2908. bool TextServerAdvanced::font_get_script_support_override(const RID &p_font_rid, const String &p_script) {
  2909. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2910. ERR_FAIL_COND_V(!fd, false);
  2911. MutexLock lock(fd->mutex);
  2912. return fd->script_support_overrides[p_script];
  2913. }
  2914. void TextServerAdvanced::font_remove_script_support_override(const RID &p_font_rid, const String &p_script) {
  2915. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2916. ERR_FAIL_COND(!fd);
  2917. MutexLock lock(fd->mutex);
  2918. fd->script_support_overrides.erase(p_script);
  2919. }
  2920. PackedStringArray TextServerAdvanced::font_get_script_support_overrides(const RID &p_font_rid) {
  2921. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2922. ERR_FAIL_COND_V(!fd, PackedStringArray());
  2923. MutexLock lock(fd->mutex);
  2924. PackedStringArray out;
  2925. for (const KeyValue<String, bool> &E : fd->script_support_overrides) {
  2926. out.push_back(E.key);
  2927. }
  2928. return out;
  2929. }
  2930. void TextServerAdvanced::font_set_opentype_feature_overrides(const RID &p_font_rid, const Dictionary &p_overrides) {
  2931. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2932. ERR_FAIL_COND(!fd);
  2933. MutexLock lock(fd->mutex);
  2934. Vector2i size = _get_size(fd, 16);
  2935. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  2936. fd->feature_overrides = p_overrides;
  2937. }
  2938. Dictionary TextServerAdvanced::font_get_opentype_feature_overrides(const RID &p_font_rid) const {
  2939. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2940. ERR_FAIL_COND_V(!fd, Dictionary());
  2941. MutexLock lock(fd->mutex);
  2942. return fd->feature_overrides;
  2943. }
  2944. Dictionary TextServerAdvanced::font_supported_feature_list(const RID &p_font_rid) const {
  2945. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2946. ERR_FAIL_COND_V(!fd, Dictionary());
  2947. MutexLock lock(fd->mutex);
  2948. Vector2i size = _get_size(fd, 16);
  2949. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), Dictionary());
  2950. return fd->supported_features;
  2951. }
  2952. Dictionary TextServerAdvanced::font_supported_variation_list(const RID &p_font_rid) const {
  2953. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2954. ERR_FAIL_COND_V(!fd, Dictionary());
  2955. MutexLock lock(fd->mutex);
  2956. Vector2i size = _get_size(fd, 16);
  2957. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), Dictionary());
  2958. return fd->supported_varaitions;
  2959. }
  2960. double TextServerAdvanced::font_get_global_oversampling() const {
  2961. return oversampling;
  2962. }
  2963. void TextServerAdvanced::font_set_global_oversampling(double p_oversampling) {
  2964. _THREAD_SAFE_METHOD_
  2965. if (oversampling != p_oversampling) {
  2966. oversampling = p_oversampling;
  2967. List<RID> fonts;
  2968. font_owner.get_owned_list(&fonts);
  2969. bool font_cleared = false;
  2970. for (const RID &E : fonts) {
  2971. if (!font_is_multichannel_signed_distance_field(E) && font_get_oversampling(E) <= 0) {
  2972. font_clear_size_cache(E);
  2973. font_cleared = true;
  2974. }
  2975. }
  2976. if (font_cleared) {
  2977. List<RID> text_bufs;
  2978. shaped_owner.get_owned_list(&text_bufs);
  2979. for (const RID &E : text_bufs) {
  2980. invalidate(shaped_owner.get_or_null(E), false);
  2981. }
  2982. }
  2983. }
  2984. }
  2985. /*************************************************************************/
  2986. /* Shaped text buffer interface */
  2987. /*************************************************************************/
  2988. int64_t TextServerAdvanced::_convert_pos(const String &p_utf32, const Char16String &p_utf16, int64_t p_pos) const {
  2989. int64_t limit = p_pos;
  2990. if (p_utf32.length() != p_utf16.length()) {
  2991. const UChar *data = p_utf16.get_data();
  2992. for (int i = 0; i < p_pos; i++) {
  2993. if (U16_IS_LEAD(data[i])) {
  2994. limit--;
  2995. }
  2996. }
  2997. }
  2998. return limit;
  2999. }
  3000. int64_t TextServerAdvanced::_convert_pos(const ShapedTextDataAdvanced *p_sd, int64_t p_pos) const {
  3001. int64_t limit = p_pos;
  3002. if (p_sd->text.length() != p_sd->utf16.length()) {
  3003. const UChar *data = p_sd->utf16.get_data();
  3004. for (int i = 0; i < p_pos; i++) {
  3005. if (U16_IS_LEAD(data[i])) {
  3006. limit--;
  3007. }
  3008. }
  3009. }
  3010. return limit;
  3011. }
  3012. int64_t TextServerAdvanced::_convert_pos_inv(const ShapedTextDataAdvanced *p_sd, int64_t p_pos) const {
  3013. int64_t limit = p_pos;
  3014. if (p_sd->text.length() != p_sd->utf16.length()) {
  3015. for (int i = 0; i < p_pos; i++) {
  3016. if (p_sd->text[i] > 0xffff) {
  3017. limit++;
  3018. }
  3019. }
  3020. }
  3021. return limit;
  3022. }
  3023. void TextServerAdvanced::invalidate(TextServerAdvanced::ShapedTextDataAdvanced *p_shaped, bool p_text) {
  3024. p_shaped->valid = false;
  3025. p_shaped->sort_valid = false;
  3026. p_shaped->line_breaks_valid = false;
  3027. p_shaped->justification_ops_valid = false;
  3028. p_shaped->text_trimmed = false;
  3029. p_shaped->ascent = 0.0;
  3030. p_shaped->descent = 0.0;
  3031. p_shaped->width = 0.0;
  3032. p_shaped->upos = 0.0;
  3033. p_shaped->uthk = 0.0;
  3034. p_shaped->glyphs.clear();
  3035. p_shaped->glyphs_logical.clear();
  3036. p_shaped->overrun_trim_data = TrimData();
  3037. p_shaped->utf16 = Char16String();
  3038. for (int i = 0; i < p_shaped->bidi_iter.size(); i++) {
  3039. ubidi_close(p_shaped->bidi_iter[i]);
  3040. }
  3041. p_shaped->bidi_iter.clear();
  3042. if (p_text) {
  3043. if (p_shaped->script_iter != nullptr) {
  3044. memdelete(p_shaped->script_iter);
  3045. p_shaped->script_iter = nullptr;
  3046. }
  3047. p_shaped->break_ops_valid = false;
  3048. p_shaped->js_ops_valid = false;
  3049. }
  3050. }
  3051. void TextServerAdvanced::full_copy(ShapedTextDataAdvanced *p_shaped) {
  3052. ShapedTextDataAdvanced *parent = shaped_owner.get_or_null(p_shaped->parent);
  3053. for (const KeyValue<Variant, ShapedTextDataAdvanced::EmbeddedObject> &E : parent->objects) {
  3054. if (E.value.pos >= p_shaped->start && E.value.pos < p_shaped->end) {
  3055. p_shaped->objects[E.key] = E.value;
  3056. }
  3057. }
  3058. for (int i = 0; i < parent->spans.size(); i++) {
  3059. ShapedTextDataAdvanced::Span span = parent->spans[i];
  3060. if (span.start >= p_shaped->end || span.end <= p_shaped->start) {
  3061. continue;
  3062. }
  3063. span.start = MAX(p_shaped->start, span.start);
  3064. span.end = MIN(p_shaped->end, span.end);
  3065. p_shaped->spans.push_back(span);
  3066. }
  3067. p_shaped->parent = RID();
  3068. }
  3069. RID TextServerAdvanced::create_shaped_text(TextServer::Direction p_direction, TextServer::Orientation p_orientation) {
  3070. _THREAD_SAFE_METHOD_
  3071. ShapedTextDataAdvanced *sd = memnew(ShapedTextDataAdvanced);
  3072. sd->hb_buffer = hb_buffer_create();
  3073. sd->direction = p_direction;
  3074. sd->orientation = p_orientation;
  3075. return shaped_owner.make_rid(sd);
  3076. }
  3077. void TextServerAdvanced::shaped_text_clear(const RID &p_shaped) {
  3078. ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
  3079. ERR_FAIL_COND(!sd);
  3080. MutexLock lock(sd->mutex);
  3081. sd->parent = RID();
  3082. sd->start = 0;
  3083. sd->end = 0;
  3084. sd->text = String();
  3085. sd->spans.clear();
  3086. sd->objects.clear();
  3087. sd->bidi_override.clear();
  3088. invalidate(sd, true);
  3089. }
  3090. void TextServerAdvanced::shaped_text_set_direction(const RID &p_shaped, TextServer::Direction p_direction) {
  3091. ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
  3092. ERR_FAIL_COND(!sd);
  3093. MutexLock lock(sd->mutex);
  3094. if (sd->direction != p_direction) {
  3095. if (sd->parent != RID()) {
  3096. full_copy(sd);
  3097. }
  3098. sd->direction = p_direction;
  3099. invalidate(sd, false);
  3100. }
  3101. }
  3102. TextServer::Direction TextServerAdvanced::shaped_text_get_direction(const RID &p_shaped) const {
  3103. const ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
  3104. ERR_FAIL_COND_V(!sd, TextServer::DIRECTION_LTR);
  3105. MutexLock lock(sd->mutex);
  3106. return sd->direction;
  3107. }
  3108. TextServer::Direction TextServerAdvanced::shaped_text_get_inferred_direction(const RID &p_shaped) const {
  3109. const ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
  3110. ERR_FAIL_COND_V(!sd, TextServer::DIRECTION_LTR);
  3111. MutexLock lock(sd->mutex);
  3112. return sd->para_direction;
  3113. }
  3114. void TextServerAdvanced::shaped_text_set_custom_punctuation(const RID &p_shaped, const String &p_punct) {
  3115. _THREAD_SAFE_METHOD_
  3116. ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
  3117. ERR_FAIL_COND(!sd);
  3118. if (sd->custom_punct != p_punct) {
  3119. if (sd->parent != RID()) {
  3120. full_copy(sd);
  3121. }
  3122. sd->custom_punct = p_punct;
  3123. invalidate(sd, false);
  3124. }
  3125. }
  3126. String TextServerAdvanced::shaped_text_get_custom_punctuation(const RID &p_shaped) const {
  3127. _THREAD_SAFE_METHOD_
  3128. const ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
  3129. ERR_FAIL_COND_V(!sd, String());
  3130. return sd->custom_punct;
  3131. }
  3132. void TextServerAdvanced::shaped_text_set_bidi_override(const RID &p_shaped, const Array &p_override) {
  3133. ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
  3134. ERR_FAIL_COND(!sd);
  3135. MutexLock lock(sd->mutex);
  3136. if (sd->parent != RID()) {
  3137. full_copy(sd);
  3138. }
  3139. sd->bidi_override.clear();
  3140. for (int i = 0; i < p_override.size(); i++) {
  3141. if (p_override[i].get_type() == Variant::VECTOR2I) {
  3142. sd->bidi_override.push_back(p_override[i]);
  3143. }
  3144. }
  3145. invalidate(sd, false);
  3146. }
  3147. void TextServerAdvanced::shaped_text_set_orientation(const RID &p_shaped, TextServer::Orientation p_orientation) {
  3148. ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
  3149. ERR_FAIL_COND(!sd);
  3150. MutexLock lock(sd->mutex);
  3151. if (sd->orientation != p_orientation) {
  3152. if (sd->parent != RID()) {
  3153. full_copy(sd);
  3154. }
  3155. sd->orientation = p_orientation;
  3156. invalidate(sd, false);
  3157. }
  3158. }
  3159. void TextServerAdvanced::shaped_text_set_preserve_invalid(const RID &p_shaped, bool p_enabled) {
  3160. ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
  3161. ERR_FAIL_COND(!sd);
  3162. MutexLock lock(sd->mutex);
  3163. ERR_FAIL_COND(sd->parent != RID());
  3164. if (sd->preserve_invalid != p_enabled) {
  3165. sd->preserve_invalid = p_enabled;
  3166. invalidate(sd, false);
  3167. }
  3168. }
  3169. bool TextServerAdvanced::shaped_text_get_preserve_invalid(const RID &p_shaped) const {
  3170. const ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
  3171. ERR_FAIL_COND_V(!sd, false);
  3172. MutexLock lock(sd->mutex);
  3173. return sd->preserve_invalid;
  3174. }
  3175. void TextServerAdvanced::shaped_text_set_preserve_control(const RID &p_shaped, bool p_enabled) {
  3176. ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
  3177. ERR_FAIL_COND(!sd);
  3178. MutexLock lock(sd->mutex);
  3179. if (sd->preserve_control != p_enabled) {
  3180. if (sd->parent != RID()) {
  3181. full_copy(sd);
  3182. }
  3183. sd->preserve_control = p_enabled;
  3184. invalidate(sd, false);
  3185. }
  3186. }
  3187. bool TextServerAdvanced::shaped_text_get_preserve_control(const RID &p_shaped) const {
  3188. const ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
  3189. ERR_FAIL_COND_V(!sd, false);
  3190. MutexLock lock(sd->mutex);
  3191. return sd->preserve_control;
  3192. }
  3193. void TextServerAdvanced::shaped_text_set_spacing(const RID &p_shaped, SpacingType p_spacing, int64_t p_value) {
  3194. ERR_FAIL_INDEX((int)p_spacing, 4);
  3195. ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
  3196. ERR_FAIL_COND(!sd);
  3197. MutexLock lock(sd->mutex);
  3198. if (sd->extra_spacing[p_spacing] != p_value) {
  3199. if (sd->parent != RID()) {
  3200. full_copy(sd);
  3201. }
  3202. sd->extra_spacing[p_spacing] = p_value;
  3203. invalidate(sd, false);
  3204. }
  3205. }
  3206. int64_t TextServerAdvanced::shaped_text_get_spacing(const RID &p_shaped, SpacingType p_spacing) const {
  3207. ERR_FAIL_INDEX_V((int)p_spacing, 4, 0);
  3208. const ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
  3209. ERR_FAIL_COND_V(!sd, 0);
  3210. MutexLock lock(sd->mutex);
  3211. return sd->extra_spacing[p_spacing];
  3212. }
  3213. TextServer::Orientation TextServerAdvanced::shaped_text_get_orientation(const RID &p_shaped) const {
  3214. const ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
  3215. ERR_FAIL_COND_V(!sd, TextServer::ORIENTATION_HORIZONTAL);
  3216. MutexLock lock(sd->mutex);
  3217. return sd->orientation;
  3218. }
  3219. int64_t TextServerAdvanced::shaped_get_span_count(const RID &p_shaped) const {
  3220. ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
  3221. ERR_FAIL_COND_V(!sd, 0);
  3222. return sd->spans.size();
  3223. }
  3224. Variant TextServerAdvanced::shaped_get_span_meta(const RID &p_shaped, int64_t p_index) const {
  3225. ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
  3226. ERR_FAIL_COND_V(!sd, Variant());
  3227. ERR_FAIL_INDEX_V(p_index, sd->spans.size(), Variant());
  3228. return sd->spans[p_index].meta;
  3229. }
  3230. void TextServerAdvanced::shaped_set_span_update_font(const RID &p_shaped, int64_t p_index, const Array &p_fonts, int64_t p_size, const Dictionary &p_opentype_features) {
  3231. ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
  3232. ERR_FAIL_COND(!sd);
  3233. ERR_FAIL_INDEX(p_index, sd->spans.size());
  3234. ShapedTextDataAdvanced::Span &span = sd->spans.ptrw()[p_index];
  3235. bool changed = (span.font_size != p_size) || (span.features != p_opentype_features) || (p_fonts.size() != span.fonts.size());
  3236. if (!changed) {
  3237. for (int i = 0; i < p_fonts.size(); i++) {
  3238. changed = changed || (span.fonts[i] != p_fonts[i]);
  3239. }
  3240. }
  3241. if (changed) {
  3242. span.fonts = p_fonts;
  3243. span.font_size = p_size;
  3244. span.features = p_opentype_features;
  3245. invalidate(sd, false);
  3246. }
  3247. }
  3248. bool TextServerAdvanced::shaped_text_add_string(const RID &p_shaped, const String &p_text, const Array &p_fonts, int64_t p_size, const Dictionary &p_opentype_features, const String &p_language, const Variant &p_meta) {
  3249. ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
  3250. ERR_FAIL_COND_V(!sd, false);
  3251. ERR_FAIL_COND_V(p_size <= 0, false);
  3252. MutexLock lock(sd->mutex);
  3253. for (int i = 0; i < p_fonts.size(); i++) {
  3254. ERR_FAIL_COND_V(!font_owner.get_or_null(p_fonts[i]), false);
  3255. }
  3256. if (p_text.is_empty()) {
  3257. return true;
  3258. }
  3259. if (sd->parent != RID()) {
  3260. full_copy(sd);
  3261. }
  3262. ShapedTextDataAdvanced::Span span;
  3263. span.start = sd->text.length();
  3264. span.end = span.start + p_text.length();
  3265. span.fonts = p_fonts; // Do not pre-sort, spans will be divided to subruns later.
  3266. span.font_size = p_size;
  3267. span.language = p_language;
  3268. span.features = p_opentype_features;
  3269. span.meta = p_meta;
  3270. sd->spans.push_back(span);
  3271. sd->text = sd->text + p_text;
  3272. sd->end += p_text.length();
  3273. invalidate(sd, true);
  3274. return true;
  3275. }
  3276. bool TextServerAdvanced::shaped_text_add_object(const RID &p_shaped, const Variant &p_key, const Size2 &p_size, InlineAlignment p_inline_align, int64_t p_length) {
  3277. _THREAD_SAFE_METHOD_
  3278. ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
  3279. ERR_FAIL_COND_V(!sd, false);
  3280. ERR_FAIL_COND_V(p_key == Variant(), false);
  3281. ERR_FAIL_COND_V(sd->objects.has(p_key), false);
  3282. if (sd->parent != RID()) {
  3283. full_copy(sd);
  3284. }
  3285. ShapedTextDataAdvanced::Span span;
  3286. span.start = sd->start + sd->text.length();
  3287. span.end = span.start + p_length;
  3288. span.embedded_key = p_key;
  3289. ShapedTextDataAdvanced::EmbeddedObject obj;
  3290. obj.inline_align = p_inline_align;
  3291. obj.rect.size = p_size;
  3292. obj.pos = span.start;
  3293. sd->spans.push_back(span);
  3294. sd->text = sd->text + String::chr(0xfffc).repeat(p_length);
  3295. sd->end += p_length;
  3296. sd->objects[p_key] = obj;
  3297. invalidate(sd, true);
  3298. return true;
  3299. }
  3300. bool TextServerAdvanced::shaped_text_resize_object(const RID &p_shaped, const Variant &p_key, const Size2 &p_size, InlineAlignment p_inline_align) {
  3301. ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
  3302. ERR_FAIL_COND_V(!sd, false);
  3303. MutexLock lock(sd->mutex);
  3304. ERR_FAIL_COND_V(!sd->objects.has(p_key), false);
  3305. sd->objects[p_key].rect.size = p_size;
  3306. sd->objects[p_key].inline_align = p_inline_align;
  3307. if (sd->valid) {
  3308. // Recalc string metrics.
  3309. sd->ascent = 0;
  3310. sd->descent = 0;
  3311. sd->width = 0;
  3312. sd->upos = 0;
  3313. sd->uthk = 0;
  3314. int sd_size = sd->glyphs.size();
  3315. for (int i = 0; i < sd_size; i++) {
  3316. Glyph gl = sd->glyphs[i];
  3317. Variant key;
  3318. if (gl.count == 1) {
  3319. for (const KeyValue<Variant, ShapedTextDataAdvanced::EmbeddedObject> &E : sd->objects) {
  3320. if (E.value.pos == gl.start) {
  3321. key = E.key;
  3322. break;
  3323. }
  3324. }
  3325. }
  3326. if (key != Variant()) {
  3327. if (sd->orientation == ORIENTATION_HORIZONTAL) {
  3328. sd->objects[key].rect.position.x = sd->width;
  3329. sd->width += sd->objects[key].rect.size.x;
  3330. sd->glyphs.write[i].advance = sd->objects[key].rect.size.x;
  3331. } else {
  3332. sd->objects[key].rect.position.y = sd->width;
  3333. sd->width += sd->objects[key].rect.size.y;
  3334. sd->glyphs.write[i].advance = sd->objects[key].rect.size.y;
  3335. }
  3336. } else {
  3337. if (gl.font_rid.is_valid()) {
  3338. if (sd->orientation == ORIENTATION_HORIZONTAL) {
  3339. sd->ascent = MAX(sd->ascent, MAX(font_get_ascent(gl.font_rid, gl.font_size), -gl.y_off));
  3340. sd->descent = MAX(sd->descent, MAX(font_get_descent(gl.font_rid, gl.font_size), gl.y_off));
  3341. } else {
  3342. sd->ascent = MAX(sd->ascent, Math::round(font_get_glyph_advance(gl.font_rid, gl.font_size, gl.index).x * 0.5));
  3343. sd->descent = MAX(sd->descent, Math::round(font_get_glyph_advance(gl.font_rid, gl.font_size, gl.index).x * 0.5));
  3344. }
  3345. sd->upos = MAX(sd->upos, font_get_underline_position(gl.font_rid, gl.font_size));
  3346. sd->uthk = MAX(sd->uthk, font_get_underline_thickness(gl.font_rid, gl.font_size));
  3347. } else if (sd->preserve_invalid || (sd->preserve_control && is_control(gl.index))) {
  3348. // Glyph not found, replace with hex code box.
  3349. if (sd->orientation == ORIENTATION_HORIZONTAL) {
  3350. sd->ascent = MAX(sd->ascent, get_hex_code_box_size(gl.font_size, gl.index).y);
  3351. } else {
  3352. sd->ascent = MAX(sd->ascent, Math::round(get_hex_code_box_size(gl.font_size, gl.index).x * 0.5));
  3353. sd->descent = MAX(sd->descent, Math::round(get_hex_code_box_size(gl.font_size, gl.index).x * 0.5));
  3354. }
  3355. }
  3356. sd->width += gl.advance * gl.repeat;
  3357. }
  3358. }
  3359. _realign(sd);
  3360. }
  3361. return true;
  3362. }
  3363. void TextServerAdvanced::_realign(ShapedTextDataAdvanced *p_sd) const {
  3364. // Align embedded objects to baseline.
  3365. double full_ascent = p_sd->ascent;
  3366. double full_descent = p_sd->descent;
  3367. for (KeyValue<Variant, ShapedTextDataAdvanced::EmbeddedObject> &E : p_sd->objects) {
  3368. if ((E.value.pos >= p_sd->start) && (E.value.pos < p_sd->end)) {
  3369. if (p_sd->orientation == ORIENTATION_HORIZONTAL) {
  3370. switch (E.value.inline_align & INLINE_ALIGNMENT_TEXT_MASK) {
  3371. case INLINE_ALIGNMENT_TO_TOP: {
  3372. E.value.rect.position.y = -p_sd->ascent;
  3373. } break;
  3374. case INLINE_ALIGNMENT_TO_CENTER: {
  3375. E.value.rect.position.y = (-p_sd->ascent + p_sd->descent) / 2;
  3376. } break;
  3377. case INLINE_ALIGNMENT_TO_BASELINE: {
  3378. E.value.rect.position.y = 0;
  3379. } break;
  3380. case INLINE_ALIGNMENT_TO_BOTTOM: {
  3381. E.value.rect.position.y = p_sd->descent;
  3382. } break;
  3383. }
  3384. switch (E.value.inline_align & INLINE_ALIGNMENT_IMAGE_MASK) {
  3385. case INLINE_ALIGNMENT_BOTTOM_TO: {
  3386. E.value.rect.position.y -= E.value.rect.size.y;
  3387. } break;
  3388. case INLINE_ALIGNMENT_CENTER_TO: {
  3389. E.value.rect.position.y -= E.value.rect.size.y / 2;
  3390. } break;
  3391. case INLINE_ALIGNMENT_TOP_TO: {
  3392. // NOP
  3393. } break;
  3394. }
  3395. full_ascent = MAX(full_ascent, -E.value.rect.position.y);
  3396. full_descent = MAX(full_descent, E.value.rect.position.y + E.value.rect.size.y);
  3397. } else {
  3398. switch (E.value.inline_align & INLINE_ALIGNMENT_TEXT_MASK) {
  3399. case INLINE_ALIGNMENT_TO_TOP: {
  3400. E.value.rect.position.x = -p_sd->ascent;
  3401. } break;
  3402. case INLINE_ALIGNMENT_TO_CENTER: {
  3403. E.value.rect.position.x = (-p_sd->ascent + p_sd->descent) / 2;
  3404. } break;
  3405. case INLINE_ALIGNMENT_TO_BASELINE: {
  3406. E.value.rect.position.x = 0;
  3407. } break;
  3408. case INLINE_ALIGNMENT_TO_BOTTOM: {
  3409. E.value.rect.position.x = p_sd->descent;
  3410. } break;
  3411. }
  3412. switch (E.value.inline_align & INLINE_ALIGNMENT_IMAGE_MASK) {
  3413. case INLINE_ALIGNMENT_BOTTOM_TO: {
  3414. E.value.rect.position.x -= E.value.rect.size.x;
  3415. } break;
  3416. case INLINE_ALIGNMENT_CENTER_TO: {
  3417. E.value.rect.position.x -= E.value.rect.size.x / 2;
  3418. } break;
  3419. case INLINE_ALIGNMENT_TOP_TO: {
  3420. // NOP
  3421. } break;
  3422. }
  3423. full_ascent = MAX(full_ascent, -E.value.rect.position.x);
  3424. full_descent = MAX(full_descent, E.value.rect.position.x + E.value.rect.size.x);
  3425. }
  3426. }
  3427. }
  3428. p_sd->ascent = full_ascent;
  3429. p_sd->descent = full_descent;
  3430. }
  3431. RID TextServerAdvanced::shaped_text_substr(const RID &p_shaped, int64_t p_start, int64_t p_length) const {
  3432. _THREAD_SAFE_METHOD_
  3433. const ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
  3434. ERR_FAIL_COND_V(!sd, RID());
  3435. MutexLock lock(sd->mutex);
  3436. if (sd->parent != RID()) {
  3437. return shaped_text_substr(sd->parent, p_start, p_length);
  3438. }
  3439. if (!sd->valid) {
  3440. const_cast<TextServerAdvanced *>(this)->shaped_text_shape(p_shaped);
  3441. }
  3442. ERR_FAIL_COND_V(p_start < 0 || p_length < 0, RID());
  3443. ERR_FAIL_COND_V(sd->start > p_start || sd->end < p_start, RID());
  3444. ERR_FAIL_COND_V(sd->end < p_start + p_length, RID());
  3445. ShapedTextDataAdvanced *new_sd = memnew(ShapedTextDataAdvanced);
  3446. new_sd->parent = p_shaped;
  3447. new_sd->start = p_start;
  3448. new_sd->end = p_start + p_length;
  3449. new_sd->orientation = sd->orientation;
  3450. new_sd->direction = sd->direction;
  3451. new_sd->custom_punct = sd->custom_punct;
  3452. new_sd->para_direction = sd->para_direction;
  3453. for (int i = 0; i < TextServer::SPACING_MAX; i++) {
  3454. new_sd->extra_spacing[i] = sd->extra_spacing[i];
  3455. }
  3456. if (!_shape_substr(new_sd, sd, p_start, p_length)) {
  3457. memdelete(new_sd);
  3458. return RID();
  3459. }
  3460. return shaped_owner.make_rid(new_sd);
  3461. }
  3462. bool TextServerAdvanced::_shape_substr(ShapedTextDataAdvanced *p_new_sd, const ShapedTextDataAdvanced *p_sd, int64_t p_start, int64_t p_length) const {
  3463. if (p_new_sd->valid) {
  3464. return true;
  3465. }
  3466. p_new_sd->hb_buffer = hb_buffer_create();
  3467. p_new_sd->line_breaks_valid = p_sd->line_breaks_valid;
  3468. p_new_sd->justification_ops_valid = p_sd->justification_ops_valid;
  3469. p_new_sd->sort_valid = false;
  3470. p_new_sd->upos = p_sd->upos;
  3471. p_new_sd->uthk = p_sd->uthk;
  3472. if (p_length > 0) {
  3473. p_new_sd->text = p_sd->text.substr(p_start - p_sd->start, p_length);
  3474. p_new_sd->utf16 = p_new_sd->text.utf16();
  3475. p_new_sd->script_iter = memnew(ScriptIterator(p_new_sd->text, 0, p_new_sd->text.length()));
  3476. int sd_size = p_sd->glyphs.size();
  3477. const Glyph *sd_glyphs = p_sd->glyphs.ptr();
  3478. for (int ov = 0; ov < p_sd->bidi_override.size(); ov++) {
  3479. UErrorCode err = U_ZERO_ERROR;
  3480. if (p_sd->bidi_override[ov].x >= p_start + p_length || p_sd->bidi_override[ov].y <= p_start) {
  3481. continue;
  3482. }
  3483. int start = _convert_pos_inv(p_sd, MAX(0, p_start - p_sd->bidi_override[ov].x));
  3484. int end = _convert_pos_inv(p_sd, MIN(p_start + p_length, p_sd->bidi_override[ov].y) - p_sd->bidi_override[ov].x);
  3485. ERR_FAIL_COND_V_MSG((start < 0 || end - start > p_new_sd->utf16.length()), false, "Invalid BiDi override range.");
  3486. // Create temporary line bidi & shape.
  3487. UBiDi *bidi_iter = ubidi_openSized(end - start, 0, &err);
  3488. ERR_FAIL_COND_V_MSG(U_FAILURE(err), false, u_errorName(err));
  3489. ubidi_setLine(p_sd->bidi_iter[ov], start, end, bidi_iter, &err);
  3490. if (U_FAILURE(err)) {
  3491. ubidi_close(bidi_iter);
  3492. ERR_FAIL_V_MSG(false, u_errorName(err));
  3493. }
  3494. p_new_sd->bidi_iter.push_back(bidi_iter);
  3495. err = U_ZERO_ERROR;
  3496. int bidi_run_count = ubidi_countRuns(bidi_iter, &err);
  3497. ERR_FAIL_COND_V_MSG(U_FAILURE(err), false, u_errorName(err));
  3498. for (int i = 0; i < bidi_run_count; i++) {
  3499. int32_t _bidi_run_start = 0;
  3500. int32_t _bidi_run_length = 0;
  3501. ubidi_getVisualRun(bidi_iter, i, &_bidi_run_start, &_bidi_run_length);
  3502. int32_t bidi_run_start = _convert_pos(p_sd, p_sd->bidi_override[ov].x + start + _bidi_run_start);
  3503. int32_t bidi_run_end = _convert_pos(p_sd, p_sd->bidi_override[ov].x + start + _bidi_run_start + _bidi_run_length);
  3504. for (int j = 0; j < sd_size; j++) {
  3505. if ((sd_glyphs[j].start >= bidi_run_start) && (sd_glyphs[j].end <= bidi_run_end)) {
  3506. // Copy glyphs.
  3507. Glyph gl = sd_glyphs[j];
  3508. Variant key;
  3509. bool find_embedded = false;
  3510. if (gl.count == 1) {
  3511. for (const KeyValue<Variant, ShapedTextDataAdvanced::EmbeddedObject> &E : p_sd->objects) {
  3512. if (E.value.pos == gl.start) {
  3513. find_embedded = true;
  3514. key = E.key;
  3515. p_new_sd->objects[key] = E.value;
  3516. break;
  3517. }
  3518. }
  3519. }
  3520. if (find_embedded) {
  3521. if (p_new_sd->orientation == ORIENTATION_HORIZONTAL) {
  3522. p_new_sd->objects[key].rect.position.x = p_new_sd->width;
  3523. p_new_sd->width += p_new_sd->objects[key].rect.size.x;
  3524. } else {
  3525. p_new_sd->objects[key].rect.position.y = p_new_sd->width;
  3526. p_new_sd->width += p_new_sd->objects[key].rect.size.y;
  3527. }
  3528. } else {
  3529. if (gl.font_rid.is_valid()) {
  3530. if (p_new_sd->orientation == ORIENTATION_HORIZONTAL) {
  3531. p_new_sd->ascent = MAX(p_new_sd->ascent, MAX(font_get_ascent(gl.font_rid, gl.font_size), -gl.y_off));
  3532. p_new_sd->descent = MAX(p_new_sd->descent, MAX(font_get_descent(gl.font_rid, gl.font_size), gl.y_off));
  3533. } else {
  3534. p_new_sd->ascent = MAX(p_new_sd->ascent, Math::round(font_get_glyph_advance(gl.font_rid, gl.font_size, gl.index).x * 0.5));
  3535. p_new_sd->descent = MAX(p_new_sd->descent, Math::round(font_get_glyph_advance(gl.font_rid, gl.font_size, gl.index).x * 0.5));
  3536. }
  3537. } else if (p_new_sd->preserve_invalid || (p_new_sd->preserve_control && is_control(gl.index))) {
  3538. // Glyph not found, replace with hex code box.
  3539. if (p_new_sd->orientation == ORIENTATION_HORIZONTAL) {
  3540. p_new_sd->ascent = MAX(p_new_sd->ascent, get_hex_code_box_size(gl.font_size, gl.index).y);
  3541. } else {
  3542. p_new_sd->ascent = MAX(p_new_sd->ascent, Math::round(get_hex_code_box_size(gl.font_size, gl.index).x * 0.5));
  3543. p_new_sd->descent = MAX(p_new_sd->descent, Math::round(get_hex_code_box_size(gl.font_size, gl.index).x * 0.5));
  3544. }
  3545. }
  3546. p_new_sd->width += gl.advance * gl.repeat;
  3547. }
  3548. p_new_sd->glyphs.push_back(gl);
  3549. }
  3550. }
  3551. }
  3552. }
  3553. _realign(p_new_sd);
  3554. }
  3555. p_new_sd->valid = true;
  3556. return true;
  3557. }
  3558. RID TextServerAdvanced::shaped_text_get_parent(const RID &p_shaped) const {
  3559. ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
  3560. ERR_FAIL_COND_V(!sd, RID());
  3561. MutexLock lock(sd->mutex);
  3562. return sd->parent;
  3563. }
  3564. double TextServerAdvanced::shaped_text_fit_to_width(const RID &p_shaped, double p_width, BitField<TextServer::JustificationFlag> p_jst_flags) {
  3565. ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
  3566. ERR_FAIL_COND_V(!sd, 0.0);
  3567. MutexLock lock(sd->mutex);
  3568. if (!sd->valid) {
  3569. const_cast<TextServerAdvanced *>(this)->shaped_text_shape(p_shaped);
  3570. }
  3571. if (!sd->justification_ops_valid) {
  3572. const_cast<TextServerAdvanced *>(this)->shaped_text_update_justification_ops(p_shaped);
  3573. }
  3574. sd->fit_width_minimum_reached = false;
  3575. int start_pos = 0;
  3576. int end_pos = sd->glyphs.size() - 1;
  3577. if (p_jst_flags.has_flag(JUSTIFICATION_AFTER_LAST_TAB)) {
  3578. int start, end, delta;
  3579. if (sd->para_direction == DIRECTION_LTR) {
  3580. start = sd->glyphs.size() - 1;
  3581. end = -1;
  3582. delta = -1;
  3583. } else {
  3584. start = 0;
  3585. end = sd->glyphs.size();
  3586. delta = +1;
  3587. }
  3588. for (int i = start; i != end; i += delta) {
  3589. if ((sd->glyphs[i].flags & GRAPHEME_IS_TAB) == GRAPHEME_IS_TAB) {
  3590. if (sd->para_direction == DIRECTION_LTR) {
  3591. start_pos = i;
  3592. break;
  3593. } else {
  3594. end_pos = i;
  3595. break;
  3596. }
  3597. }
  3598. }
  3599. }
  3600. double justification_width;
  3601. if (p_jst_flags.has_flag(JUSTIFICATION_CONSTRAIN_ELLIPSIS)) {
  3602. if (sd->overrun_trim_data.trim_pos >= 0) {
  3603. if (sd->para_direction == DIRECTION_RTL) {
  3604. start_pos = sd->overrun_trim_data.trim_pos;
  3605. } else {
  3606. end_pos = sd->overrun_trim_data.trim_pos;
  3607. }
  3608. justification_width = sd->width_trimmed;
  3609. } else {
  3610. return Math::ceil(sd->width);
  3611. }
  3612. } else {
  3613. justification_width = sd->width;
  3614. }
  3615. if (p_jst_flags.has_flag(JUSTIFICATION_TRIM_EDGE_SPACES)) {
  3616. // Trim spaces.
  3617. while ((start_pos < end_pos) && ((sd->glyphs[start_pos].flags & GRAPHEME_IS_SPACE) == GRAPHEME_IS_SPACE || (sd->glyphs[start_pos].flags & GRAPHEME_IS_BREAK_HARD) == GRAPHEME_IS_BREAK_HARD || (sd->glyphs[start_pos].flags & GRAPHEME_IS_BREAK_SOFT) == GRAPHEME_IS_BREAK_SOFT)) {
  3618. justification_width -= sd->glyphs[start_pos].advance * sd->glyphs[start_pos].repeat;
  3619. sd->glyphs.write[start_pos].advance = 0;
  3620. start_pos += sd->glyphs[start_pos].count;
  3621. }
  3622. while ((start_pos < end_pos) && ((sd->glyphs[end_pos].flags & GRAPHEME_IS_SPACE) == GRAPHEME_IS_SPACE || (sd->glyphs[end_pos].flags & GRAPHEME_IS_BREAK_HARD) == GRAPHEME_IS_BREAK_HARD || (sd->glyphs[end_pos].flags & GRAPHEME_IS_BREAK_SOFT) == GRAPHEME_IS_BREAK_SOFT)) {
  3623. justification_width -= sd->glyphs[end_pos].advance * sd->glyphs[end_pos].repeat;
  3624. sd->glyphs.write[end_pos].advance = 0;
  3625. end_pos -= sd->glyphs[end_pos].count;
  3626. }
  3627. } else {
  3628. // Skip breaks, but do not reset size.
  3629. while ((start_pos < end_pos) && ((sd->glyphs[start_pos].flags & GRAPHEME_IS_BREAK_HARD) == GRAPHEME_IS_BREAK_HARD || (sd->glyphs[start_pos].flags & GRAPHEME_IS_BREAK_SOFT) == GRAPHEME_IS_BREAK_SOFT)) {
  3630. start_pos += sd->glyphs[start_pos].count;
  3631. }
  3632. while ((start_pos < end_pos) && ((sd->glyphs[end_pos].flags & GRAPHEME_IS_BREAK_HARD) == GRAPHEME_IS_BREAK_HARD || (sd->glyphs[end_pos].flags & GRAPHEME_IS_BREAK_SOFT) == GRAPHEME_IS_BREAK_SOFT)) {
  3633. end_pos -= sd->glyphs[end_pos].count;
  3634. }
  3635. }
  3636. int space_count = 0;
  3637. int elongation_count = 0;
  3638. for (int i = start_pos; i <= end_pos; i++) {
  3639. const Glyph &gl = sd->glyphs[i];
  3640. if (gl.count > 0) {
  3641. if ((gl.flags & GRAPHEME_IS_ELONGATION) == GRAPHEME_IS_ELONGATION) {
  3642. if ((i > 0) && ((sd->glyphs[i - 1].flags & GRAPHEME_IS_ELONGATION) != GRAPHEME_IS_ELONGATION)) {
  3643. // Expand once per elongation sequence.
  3644. elongation_count++;
  3645. }
  3646. }
  3647. if ((gl.flags & GRAPHEME_IS_SPACE) == GRAPHEME_IS_SPACE) {
  3648. space_count++;
  3649. }
  3650. }
  3651. }
  3652. if ((elongation_count > 0) && p_jst_flags.has_flag(JUSTIFICATION_KASHIDA)) {
  3653. double delta_width_per_kashida = (p_width - justification_width) / elongation_count;
  3654. for (int i = start_pos; i <= end_pos; i++) {
  3655. Glyph &gl = sd->glyphs.write[i];
  3656. if (gl.count > 0) {
  3657. if (((gl.flags & GRAPHEME_IS_ELONGATION) == GRAPHEME_IS_ELONGATION) && (gl.advance > 0)) {
  3658. if ((i > 0) && ((sd->glyphs[i - 1].flags & GRAPHEME_IS_ELONGATION) != GRAPHEME_IS_ELONGATION)) {
  3659. // Expand once per elongation sequence.
  3660. int count = delta_width_per_kashida / gl.advance;
  3661. int prev_count = gl.repeat;
  3662. if ((gl.flags & GRAPHEME_IS_VIRTUAL) == GRAPHEME_IS_VIRTUAL) {
  3663. gl.repeat = MAX(count, 0);
  3664. } else {
  3665. gl.repeat = MAX(count + 1, 1);
  3666. }
  3667. justification_width += (gl.repeat - prev_count) * gl.advance;
  3668. }
  3669. }
  3670. }
  3671. }
  3672. }
  3673. if ((space_count > 0) && p_jst_flags.has_flag(JUSTIFICATION_WORD_BOUND)) {
  3674. double delta_width_per_space = (p_width - justification_width) / space_count;
  3675. double adv_remain = 0;
  3676. for (int i = start_pos; i <= end_pos; i++) {
  3677. Glyph &gl = sd->glyphs.write[i];
  3678. if (gl.count > 0) {
  3679. if ((gl.flags & GRAPHEME_IS_SPACE) == GRAPHEME_IS_SPACE) {
  3680. double old_adv = gl.advance;
  3681. double new_advance;
  3682. if ((gl.flags & GRAPHEME_IS_VIRTUAL) == GRAPHEME_IS_VIRTUAL) {
  3683. new_advance = MAX(gl.advance + delta_width_per_space, 0.0);
  3684. } else {
  3685. new_advance = MAX(gl.advance + delta_width_per_space, 0.1 * gl.font_size);
  3686. }
  3687. gl.advance = new_advance;
  3688. adv_remain += (new_advance - gl.advance);
  3689. if (adv_remain >= 1.0) {
  3690. gl.advance++;
  3691. adv_remain -= 1.0;
  3692. } else if (adv_remain <= -1.0) {
  3693. gl.advance = MAX(gl.advance - 1, 0);
  3694. adv_remain -= 1.0;
  3695. }
  3696. justification_width += (gl.advance - old_adv);
  3697. }
  3698. }
  3699. }
  3700. }
  3701. if (Math::floor(p_width) < Math::floor(justification_width)) {
  3702. sd->fit_width_minimum_reached = true;
  3703. }
  3704. if (!p_jst_flags.has_flag(JUSTIFICATION_CONSTRAIN_ELLIPSIS)) {
  3705. sd->width = justification_width;
  3706. }
  3707. return Math::ceil(justification_width);
  3708. }
  3709. double TextServerAdvanced::shaped_text_tab_align(const RID &p_shaped, const PackedFloat32Array &p_tab_stops) {
  3710. ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
  3711. ERR_FAIL_COND_V(!sd, 0.0);
  3712. MutexLock lock(sd->mutex);
  3713. if (!sd->valid) {
  3714. const_cast<TextServerAdvanced *>(this)->shaped_text_shape(p_shaped);
  3715. }
  3716. if (!sd->line_breaks_valid) {
  3717. const_cast<TextServerAdvanced *>(this)->shaped_text_update_breaks(p_shaped);
  3718. }
  3719. for (int i = 0; i < p_tab_stops.size(); i++) {
  3720. if (p_tab_stops[i] <= 0) {
  3721. return 0.0;
  3722. }
  3723. }
  3724. int tab_index = 0;
  3725. double off = 0.0;
  3726. int start, end, delta;
  3727. if (sd->para_direction == DIRECTION_LTR) {
  3728. start = 0;
  3729. end = sd->glyphs.size();
  3730. delta = +1;
  3731. } else {
  3732. start = sd->glyphs.size() - 1;
  3733. end = -1;
  3734. delta = -1;
  3735. }
  3736. Glyph *gl = sd->glyphs.ptrw();
  3737. for (int i = start; i != end; i += delta) {
  3738. if ((gl[i].flags & GRAPHEME_IS_TAB) == GRAPHEME_IS_TAB) {
  3739. double tab_off = 0.0;
  3740. while (tab_off <= off) {
  3741. tab_off += p_tab_stops[tab_index];
  3742. tab_index++;
  3743. if (tab_index >= p_tab_stops.size()) {
  3744. tab_index = 0;
  3745. }
  3746. }
  3747. double old_adv = gl[i].advance;
  3748. gl[i].advance = tab_off - off;
  3749. sd->width += gl[i].advance - old_adv;
  3750. off = 0;
  3751. continue;
  3752. }
  3753. off += gl[i].advance * gl[i].repeat;
  3754. }
  3755. return 0.0;
  3756. }
  3757. void TextServerAdvanced::shaped_text_overrun_trim_to_width(const RID &p_shaped_line, double p_width, BitField<TextServer::TextOverrunFlag> p_trim_flags) {
  3758. ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped_line);
  3759. ERR_FAIL_COND_MSG(!sd, "ShapedTextDataAdvanced invalid.");
  3760. MutexLock lock(sd->mutex);
  3761. if (!sd->valid) {
  3762. shaped_text_shape(p_shaped_line);
  3763. }
  3764. sd->text_trimmed = false;
  3765. sd->overrun_trim_data.ellipsis_glyph_buf.clear();
  3766. bool add_ellipsis = p_trim_flags.has_flag(OVERRUN_ADD_ELLIPSIS);
  3767. bool cut_per_word = p_trim_flags.has_flag(OVERRUN_TRIM_WORD_ONLY);
  3768. bool enforce_ellipsis = p_trim_flags.has_flag(OVERRUN_ENFORCE_ELLIPSIS);
  3769. bool justification_aware = p_trim_flags.has_flag(OVERRUN_JUSTIFICATION_AWARE);
  3770. Glyph *sd_glyphs = sd->glyphs.ptrw();
  3771. if ((p_trim_flags & OVERRUN_TRIM) == OVERRUN_NO_TRIM || sd_glyphs == nullptr || p_width <= 0 || !(sd->width > p_width || enforce_ellipsis)) {
  3772. sd->overrun_trim_data.trim_pos = -1;
  3773. sd->overrun_trim_data.ellipsis_pos = -1;
  3774. return;
  3775. }
  3776. if (justification_aware && !sd->fit_width_minimum_reached) {
  3777. return;
  3778. }
  3779. Vector<ShapedTextDataAdvanced::Span> &spans = sd->spans;
  3780. if (sd->parent != RID()) {
  3781. ShapedTextDataAdvanced *parent_sd = shaped_owner.get_or_null(sd->parent);
  3782. ERR_FAIL_COND(!parent_sd->valid);
  3783. spans = parent_sd->spans;
  3784. }
  3785. if (spans.size() == 0) {
  3786. return;
  3787. }
  3788. int sd_size = sd->glyphs.size();
  3789. int last_gl_font_size = sd_glyphs[sd_size - 1].font_size;
  3790. // Find usable fonts, if fonts from the last glyph do not have required chars.
  3791. RID dot_gl_font_rid = sd_glyphs[sd_size - 1].font_rid;
  3792. if (!font_has_char(dot_gl_font_rid, '.')) {
  3793. const Array &fonts = spans[spans.size() - 1].fonts;
  3794. for (int i = 0; i < fonts.size(); i++) {
  3795. if (font_has_char(fonts[i], '.')) {
  3796. dot_gl_font_rid = fonts[i];
  3797. break;
  3798. }
  3799. }
  3800. }
  3801. RID whitespace_gl_font_rid = sd_glyphs[sd_size - 1].font_rid;
  3802. if (!font_has_char(whitespace_gl_font_rid, '.')) {
  3803. const Array &fonts = spans[spans.size() - 1].fonts;
  3804. for (int i = 0; i < fonts.size(); i++) {
  3805. if (font_has_char(fonts[i], ' ')) {
  3806. whitespace_gl_font_rid = fonts[i];
  3807. break;
  3808. }
  3809. }
  3810. }
  3811. int32_t dot_gl_idx = dot_gl_font_rid.is_valid() ? font_get_glyph_index(dot_gl_font_rid, last_gl_font_size, '.') : -10;
  3812. Vector2 dot_adv = dot_gl_font_rid.is_valid() ? font_get_glyph_advance(dot_gl_font_rid, last_gl_font_size, dot_gl_idx) : Vector2();
  3813. int32_t whitespace_gl_idx = whitespace_gl_font_rid.is_valid() ? font_get_glyph_index(whitespace_gl_font_rid, last_gl_font_size, ' ') : -10;
  3814. Vector2 whitespace_adv = whitespace_gl_font_rid.is_valid() ? font_get_glyph_advance(whitespace_gl_font_rid, last_gl_font_size, whitespace_gl_idx) : Vector2();
  3815. int ellipsis_width = 0;
  3816. if (add_ellipsis && whitespace_gl_font_rid.is_valid()) {
  3817. ellipsis_width = 3 * dot_adv.x + sd->extra_spacing[SPACING_GLYPH] + (cut_per_word ? whitespace_adv.x : 0);
  3818. }
  3819. int ell_min_characters = 6;
  3820. double width = sd->width;
  3821. bool is_rtl = sd->para_direction == DIRECTION_RTL;
  3822. int trim_pos = (is_rtl) ? sd_size : 0;
  3823. int ellipsis_pos = (enforce_ellipsis) ? 0 : -1;
  3824. int last_valid_cut = 0;
  3825. bool found = false;
  3826. int glyphs_from = (is_rtl) ? 0 : sd_size - 1;
  3827. int glyphs_to = (is_rtl) ? sd_size - 1 : -1;
  3828. int glyphs_delta = (is_rtl) ? +1 : -1;
  3829. for (int i = glyphs_from; i != glyphs_to; i += glyphs_delta) {
  3830. if (!is_rtl) {
  3831. width -= sd_glyphs[i].advance * sd_glyphs[i].repeat;
  3832. }
  3833. if (sd_glyphs[i].count > 0) {
  3834. bool above_min_char_threshold = ((is_rtl) ? sd_size - 1 - i : i) >= ell_min_characters;
  3835. if (width + (((above_min_char_threshold && add_ellipsis) || enforce_ellipsis) ? ellipsis_width : 0) <= p_width) {
  3836. if (cut_per_word && above_min_char_threshold) {
  3837. if ((sd_glyphs[i].flags & GRAPHEME_IS_BREAK_SOFT) == GRAPHEME_IS_BREAK_SOFT) {
  3838. last_valid_cut = i;
  3839. found = true;
  3840. }
  3841. } else {
  3842. last_valid_cut = i;
  3843. found = true;
  3844. }
  3845. if (found) {
  3846. trim_pos = last_valid_cut;
  3847. if (add_ellipsis && (above_min_char_threshold || enforce_ellipsis) && width - ellipsis_width <= p_width) {
  3848. ellipsis_pos = trim_pos;
  3849. }
  3850. break;
  3851. }
  3852. }
  3853. }
  3854. if (is_rtl) {
  3855. width -= sd_glyphs[i].advance * sd_glyphs[i].repeat;
  3856. }
  3857. }
  3858. sd->overrun_trim_data.trim_pos = trim_pos;
  3859. sd->overrun_trim_data.ellipsis_pos = ellipsis_pos;
  3860. if (trim_pos == 0 && enforce_ellipsis && add_ellipsis) {
  3861. sd->overrun_trim_data.ellipsis_pos = 0;
  3862. }
  3863. if ((trim_pos >= 0 && sd->width > p_width) || enforce_ellipsis) {
  3864. if (add_ellipsis && (ellipsis_pos > 0 || enforce_ellipsis)) {
  3865. // Insert an additional space when cutting word bound for aesthetics.
  3866. if (cut_per_word && (ellipsis_pos > 0)) {
  3867. Glyph gl;
  3868. gl.count = 1;
  3869. gl.advance = whitespace_adv.x;
  3870. gl.index = whitespace_gl_idx;
  3871. gl.font_rid = whitespace_gl_font_rid;
  3872. gl.font_size = last_gl_font_size;
  3873. gl.flags = GRAPHEME_IS_SPACE | GRAPHEME_IS_BREAK_SOFT | GRAPHEME_IS_VIRTUAL | (is_rtl ? GRAPHEME_IS_RTL : 0);
  3874. sd->overrun_trim_data.ellipsis_glyph_buf.append(gl);
  3875. }
  3876. // Add ellipsis dots.
  3877. if (dot_gl_idx != 0) {
  3878. Glyph gl;
  3879. gl.count = 1;
  3880. gl.repeat = 3;
  3881. gl.advance = dot_adv.x;
  3882. gl.index = dot_gl_idx;
  3883. gl.font_rid = dot_gl_font_rid;
  3884. gl.font_size = last_gl_font_size;
  3885. gl.flags = GRAPHEME_IS_PUNCTUATION | GRAPHEME_IS_VIRTUAL | (is_rtl ? GRAPHEME_IS_RTL : 0);
  3886. sd->overrun_trim_data.ellipsis_glyph_buf.append(gl);
  3887. }
  3888. }
  3889. sd->text_trimmed = true;
  3890. sd->width_trimmed = width + ((ellipsis_pos != -1) ? ellipsis_width : 0);
  3891. }
  3892. }
  3893. int64_t TextServerAdvanced::shaped_text_get_trim_pos(const RID &p_shaped) const {
  3894. ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
  3895. ERR_FAIL_COND_V_MSG(!sd, -1, "ShapedTextDataAdvanced invalid.");
  3896. MutexLock lock(sd->mutex);
  3897. return sd->overrun_trim_data.trim_pos;
  3898. }
  3899. int64_t TextServerAdvanced::shaped_text_get_ellipsis_pos(const RID &p_shaped) const {
  3900. ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
  3901. ERR_FAIL_COND_V_MSG(!sd, -1, "ShapedTextDataAdvanced invalid.");
  3902. MutexLock lock(sd->mutex);
  3903. return sd->overrun_trim_data.ellipsis_pos;
  3904. }
  3905. const Glyph *TextServerAdvanced::shaped_text_get_ellipsis_glyphs(const RID &p_shaped) const {
  3906. ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
  3907. ERR_FAIL_COND_V_MSG(!sd, nullptr, "ShapedTextDataAdvanced invalid.");
  3908. MutexLock lock(sd->mutex);
  3909. return sd->overrun_trim_data.ellipsis_glyph_buf.ptr();
  3910. }
  3911. int64_t TextServerAdvanced::shaped_text_get_ellipsis_glyph_count(const RID &p_shaped) const {
  3912. ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
  3913. ERR_FAIL_COND_V_MSG(!sd, 0, "ShapedTextDataAdvanced invalid.");
  3914. MutexLock lock(sd->mutex);
  3915. return sd->overrun_trim_data.ellipsis_glyph_buf.size();
  3916. }
  3917. bool TextServerAdvanced::shaped_text_update_breaks(const RID &p_shaped) {
  3918. ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
  3919. ERR_FAIL_COND_V(!sd, false);
  3920. MutexLock lock(sd->mutex);
  3921. if (!sd->valid) {
  3922. shaped_text_shape(p_shaped);
  3923. }
  3924. if (sd->line_breaks_valid) {
  3925. return true; // Nothing to do.
  3926. }
  3927. const UChar *data = sd->utf16.get_data();
  3928. if (!sd->break_ops_valid) {
  3929. sd->breaks.clear();
  3930. UErrorCode err = U_ZERO_ERROR;
  3931. int i = 0;
  3932. while (i < sd->spans.size()) {
  3933. String language = sd->spans[i].language;
  3934. int r_start = sd->spans[i].start;
  3935. while (i + 1 < sd->spans.size() && language == sd->spans[i + 1].language) {
  3936. i++;
  3937. }
  3938. int r_end = sd->spans[i].end;
  3939. UBreakIterator *bi = ubrk_open(UBRK_LINE, (language.is_empty()) ? TranslationServer::get_singleton()->get_tool_locale().ascii().get_data() : language.ascii().get_data(), data + _convert_pos_inv(sd, r_start), _convert_pos_inv(sd, r_end - r_start), &err);
  3940. if (U_FAILURE(err)) {
  3941. // No data loaded - use fallback.
  3942. for (int j = r_start; j < r_end; j++) {
  3943. char32_t c = sd->text[j - sd->start];
  3944. if (is_whitespace(c)) {
  3945. sd->breaks[j + 1] = false;
  3946. }
  3947. if (is_linebreak(c)) {
  3948. sd->breaks[j + 1] = true;
  3949. }
  3950. }
  3951. } else {
  3952. while (ubrk_next(bi) != UBRK_DONE) {
  3953. int pos = _convert_pos(sd, ubrk_current(bi)) + r_start;
  3954. if ((ubrk_getRuleStatus(bi) >= UBRK_LINE_HARD) && (ubrk_getRuleStatus(bi) < UBRK_LINE_HARD_LIMIT)) {
  3955. sd->breaks[pos] = true;
  3956. } else if ((ubrk_getRuleStatus(bi) >= UBRK_LINE_SOFT) && (ubrk_getRuleStatus(bi) < UBRK_LINE_SOFT_LIMIT)) {
  3957. sd->breaks[pos] = false;
  3958. }
  3959. }
  3960. }
  3961. ubrk_close(bi);
  3962. i++;
  3963. }
  3964. sd->break_ops_valid = true;
  3965. }
  3966. sd->sort_valid = false;
  3967. sd->glyphs_logical.clear();
  3968. int sd_size = sd->glyphs.size();
  3969. const char32_t *ch = sd->text.ptr();
  3970. Glyph *sd_glyphs = sd->glyphs.ptrw();
  3971. int c_punct_size = sd->custom_punct.length();
  3972. const char32_t *c_punct = sd->custom_punct.ptr();
  3973. for (int i = 0; i < sd_size; i++) {
  3974. if (sd_glyphs[i].count > 0) {
  3975. char32_t c = ch[sd_glyphs[i].start - sd->start];
  3976. if (c == 0xfffc) {
  3977. continue;
  3978. }
  3979. if (c == 0x0009 || c == 0x000b) {
  3980. sd_glyphs[i].flags |= GRAPHEME_IS_TAB;
  3981. }
  3982. if (is_whitespace(c)) {
  3983. sd_glyphs[i].flags |= GRAPHEME_IS_SPACE;
  3984. }
  3985. if (c_punct_size == 0) {
  3986. if (u_ispunct(c) && c != 0x005f) {
  3987. sd_glyphs[i].flags |= GRAPHEME_IS_PUNCTUATION;
  3988. }
  3989. } else {
  3990. for (int j = 0; j < c_punct_size; j++) {
  3991. if (c_punct[j] == c) {
  3992. sd_glyphs[i].flags |= GRAPHEME_IS_PUNCTUATION;
  3993. break;
  3994. }
  3995. }
  3996. }
  3997. if (is_underscore(c)) {
  3998. sd_glyphs[i].flags |= GRAPHEME_IS_UNDERSCORE;
  3999. }
  4000. if (sd->breaks.has(sd_glyphs[i].end)) {
  4001. if (sd->breaks[sd_glyphs[i].end] && (is_linebreak(c))) {
  4002. sd_glyphs[i].flags |= GRAPHEME_IS_BREAK_HARD;
  4003. } else if (is_whitespace(c)) {
  4004. sd_glyphs[i].flags |= GRAPHEME_IS_BREAK_SOFT;
  4005. } else {
  4006. int count = sd_glyphs[i].count;
  4007. // Do not add extra space at the end of the line.
  4008. if (sd_glyphs[i].end == sd->end) {
  4009. continue;
  4010. }
  4011. // Do not add extra space after existing space.
  4012. if (sd_glyphs[i].flags & GRAPHEME_IS_RTL) {
  4013. if ((i + count < sd_size - 1) && ((sd_glyphs[i + count].flags & (GRAPHEME_IS_SPACE | GRAPHEME_IS_BREAK_SOFT)) == (GRAPHEME_IS_SPACE | GRAPHEME_IS_BREAK_SOFT))) {
  4014. continue;
  4015. }
  4016. } else {
  4017. if ((i > 0) && ((sd_glyphs[i - 1].flags & (GRAPHEME_IS_SPACE | GRAPHEME_IS_BREAK_SOFT)) == (GRAPHEME_IS_SPACE | GRAPHEME_IS_BREAK_SOFT))) {
  4018. continue;
  4019. }
  4020. }
  4021. Glyph gl;
  4022. gl.start = sd_glyphs[i].start;
  4023. gl.end = sd_glyphs[i].end;
  4024. gl.count = 1;
  4025. gl.font_rid = sd_glyphs[i].font_rid;
  4026. gl.font_size = sd_glyphs[i].font_size;
  4027. gl.flags = GRAPHEME_IS_BREAK_SOFT | GRAPHEME_IS_VIRTUAL | GRAPHEME_IS_SPACE;
  4028. if (sd_glyphs[i].flags & GRAPHEME_IS_RTL) {
  4029. gl.flags |= GRAPHEME_IS_RTL;
  4030. sd->glyphs.insert(i, gl); // Insert before.
  4031. } else {
  4032. sd->glyphs.insert(i + count, gl); // Insert after.
  4033. }
  4034. i += count;
  4035. // Update write pointer and size.
  4036. sd_size = sd->glyphs.size();
  4037. sd_glyphs = sd->glyphs.ptrw();
  4038. continue;
  4039. }
  4040. }
  4041. i += (sd_glyphs[i].count - 1);
  4042. }
  4043. }
  4044. sd->line_breaks_valid = true;
  4045. return sd->line_breaks_valid;
  4046. }
  4047. _FORCE_INLINE_ int64_t _generate_kashida_justification_opportunies(const String &p_data, int64_t p_start, int64_t p_end) {
  4048. int64_t kashida_pos = -1;
  4049. int8_t priority = 100;
  4050. int64_t i = p_start;
  4051. char32_t pc = 0;
  4052. while ((p_end > p_start) && is_transparent(p_data[p_end - 1])) {
  4053. p_end--;
  4054. }
  4055. while (i < p_end) {
  4056. uint32_t c = p_data[i];
  4057. if (c == 0x0640) {
  4058. kashida_pos = i;
  4059. priority = 0;
  4060. }
  4061. if (priority >= 1 && i < p_end - 1) {
  4062. if (is_seen_sad(c) && (p_data[i + 1] != 0x200c)) {
  4063. kashida_pos = i;
  4064. priority = 1;
  4065. }
  4066. }
  4067. if (priority >= 2 && i > p_start) {
  4068. if (is_teh_marbuta(c) || is_dal(c) || (is_heh(c) && i == p_end - 1)) {
  4069. if (is_connected_to_prev(c, pc)) {
  4070. kashida_pos = i - 1;
  4071. priority = 2;
  4072. }
  4073. }
  4074. }
  4075. if (priority >= 3 && i > p_start) {
  4076. if (is_alef(c) || ((is_lam(c) || is_tah(c) || is_kaf(c) || is_gaf(c)) && i == p_end - 1)) {
  4077. if (is_connected_to_prev(c, pc)) {
  4078. kashida_pos = i - 1;
  4079. priority = 3;
  4080. }
  4081. }
  4082. }
  4083. if (priority >= 4 && i > p_start && i < p_end - 1) {
  4084. if (is_beh(c)) {
  4085. if (is_reh(p_data[i + 1]) || is_yeh(p_data[i + 1])) {
  4086. if (is_connected_to_prev(c, pc)) {
  4087. kashida_pos = i - 1;
  4088. priority = 4;
  4089. }
  4090. }
  4091. }
  4092. }
  4093. if (priority >= 5 && i > p_start) {
  4094. if (is_waw(c) || ((is_ain(c) || is_qaf(c) || is_feh(c)) && i == p_end - 1)) {
  4095. if (is_connected_to_prev(c, pc)) {
  4096. kashida_pos = i - 1;
  4097. priority = 5;
  4098. }
  4099. }
  4100. }
  4101. if (priority >= 6 && i > p_start) {
  4102. if (is_reh(c)) {
  4103. if (is_connected_to_prev(c, pc)) {
  4104. kashida_pos = i - 1;
  4105. priority = 6;
  4106. }
  4107. }
  4108. }
  4109. if (!is_transparent(c)) {
  4110. pc = c;
  4111. }
  4112. i++;
  4113. }
  4114. return kashida_pos;
  4115. }
  4116. bool TextServerAdvanced::shaped_text_update_justification_ops(const RID &p_shaped) {
  4117. ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
  4118. ERR_FAIL_COND_V(!sd, false);
  4119. MutexLock lock(sd->mutex);
  4120. if (!sd->valid) {
  4121. shaped_text_shape(p_shaped);
  4122. }
  4123. if (!sd->line_breaks_valid) {
  4124. shaped_text_update_breaks(p_shaped);
  4125. }
  4126. if (sd->justification_ops_valid) {
  4127. return true; // Nothing to do.
  4128. }
  4129. const UChar *data = sd->utf16.get_data();
  4130. int data_size = sd->utf16.length();
  4131. if (!sd->js_ops_valid) {
  4132. sd->jstops.clear();
  4133. // Use ICU word iterator and custom kashida detection.
  4134. UErrorCode err = U_ZERO_ERROR;
  4135. UBreakIterator *bi = ubrk_open(UBRK_WORD, "", data, data_size, &err);
  4136. if (U_FAILURE(err)) {
  4137. // No data - use fallback.
  4138. int limit = 0;
  4139. for (int i = 0; i < sd->text.length(); i++) {
  4140. if (is_whitespace(data[i])) {
  4141. int ks = _generate_kashida_justification_opportunies(sd->text, limit, i) + sd->start;
  4142. if (ks != -1) {
  4143. sd->jstops[ks] = true;
  4144. }
  4145. limit = i + 1;
  4146. }
  4147. }
  4148. int ks = _generate_kashida_justification_opportunies(sd->text, limit, sd->text.length()) + sd->start;
  4149. if (ks != -1) {
  4150. sd->jstops[ks] = true;
  4151. }
  4152. } else {
  4153. int limit = 0;
  4154. while (ubrk_next(bi) != UBRK_DONE) {
  4155. if (ubrk_getRuleStatus(bi) != UBRK_WORD_NONE) {
  4156. int i = _convert_pos(sd, ubrk_current(bi));
  4157. sd->jstops[i + sd->start] = false;
  4158. int ks = _generate_kashida_justification_opportunies(sd->text, limit, i);
  4159. if (ks != -1) {
  4160. sd->jstops[ks + sd->start] = true;
  4161. }
  4162. limit = i;
  4163. }
  4164. }
  4165. ubrk_close(bi);
  4166. }
  4167. sd->js_ops_valid = true;
  4168. }
  4169. sd->sort_valid = false;
  4170. sd->glyphs_logical.clear();
  4171. Glyph *sd_glyphs = sd->glyphs.ptrw();
  4172. int sd_size = sd->glyphs.size();
  4173. if (!sd->jstops.is_empty()) {
  4174. for (int i = 0; i < sd_size; i++) {
  4175. if (sd_glyphs[i].count > 0) {
  4176. char32_t c = sd->text[sd_glyphs[i].start - sd->start];
  4177. if (c == 0x0640 && sd_glyphs[i].start == sd_glyphs[i].end - 1) {
  4178. sd_glyphs[i].flags |= GRAPHEME_IS_ELONGATION;
  4179. }
  4180. if (sd->jstops.has(sd_glyphs[i].start)) {
  4181. if (c == 0xfffc) {
  4182. continue;
  4183. }
  4184. if (sd->jstops[sd_glyphs[i].start]) {
  4185. if (c != 0x0640) {
  4186. if (sd_glyphs[i].font_rid != RID()) {
  4187. Glyph gl = _shape_single_glyph(sd, 0x0640, HB_SCRIPT_ARABIC, HB_DIRECTION_RTL, sd->glyphs[i].font_rid, sd->glyphs[i].font_size);
  4188. if ((sd_glyphs[i].flags & GRAPHEME_IS_VALID) == GRAPHEME_IS_VALID) {
  4189. #if HB_VERSION_ATLEAST(5, 1, 0)
  4190. if ((i > 0) && ((sd_glyphs[i - 1].flags & GRAPHEME_IS_SAFE_TO_INSERT_TATWEEL) != GRAPHEME_IS_SAFE_TO_INSERT_TATWEEL)) {
  4191. continue;
  4192. }
  4193. #endif
  4194. gl.start = sd_glyphs[i].start;
  4195. gl.end = sd_glyphs[i].end;
  4196. gl.repeat = 0;
  4197. gl.count = 1;
  4198. if (sd->orientation == ORIENTATION_HORIZONTAL) {
  4199. gl.y_off = sd_glyphs[i].y_off;
  4200. } else {
  4201. gl.x_off = sd_glyphs[i].x_off;
  4202. }
  4203. gl.flags |= GRAPHEME_IS_ELONGATION | GRAPHEME_IS_VIRTUAL;
  4204. sd->glyphs.insert(i, gl);
  4205. i++;
  4206. // Update write pointer and size.
  4207. sd_size = sd->glyphs.size();
  4208. sd_glyphs = sd->glyphs.ptrw();
  4209. continue;
  4210. }
  4211. }
  4212. }
  4213. } else if ((sd_glyphs[i].flags & GRAPHEME_IS_SPACE) != GRAPHEME_IS_SPACE) {
  4214. int count = sd_glyphs[i].count;
  4215. // Do not add extra spaces at the end of the line.
  4216. if (sd_glyphs[i].end == sd->end) {
  4217. continue;
  4218. }
  4219. // Do not add extra space after existing space.
  4220. if (sd_glyphs[i].flags & GRAPHEME_IS_RTL) {
  4221. if ((i + count < sd_size - 1) && ((sd_glyphs[i + count].flags & (GRAPHEME_IS_SPACE | GRAPHEME_IS_BREAK_SOFT)) == (GRAPHEME_IS_SPACE | GRAPHEME_IS_BREAK_SOFT))) {
  4222. continue;
  4223. }
  4224. } else {
  4225. if ((i > 0) && ((sd_glyphs[i - 1].flags & (GRAPHEME_IS_SPACE | GRAPHEME_IS_BREAK_SOFT)) == (GRAPHEME_IS_SPACE | GRAPHEME_IS_BREAK_SOFT))) {
  4226. continue;
  4227. }
  4228. }
  4229. // Inject virtual space for alignment.
  4230. Glyph gl;
  4231. gl.start = sd_glyphs[i].start;
  4232. gl.end = sd_glyphs[i].end;
  4233. gl.count = 1;
  4234. gl.font_rid = sd_glyphs[i].font_rid;
  4235. gl.font_size = sd_glyphs[i].font_size;
  4236. gl.flags = GRAPHEME_IS_SPACE | GRAPHEME_IS_VIRTUAL;
  4237. if (sd_glyphs[i].flags & GRAPHEME_IS_RTL) {
  4238. gl.flags |= GRAPHEME_IS_RTL;
  4239. sd->glyphs.insert(i, gl); // Insert before.
  4240. } else {
  4241. sd->glyphs.insert(i + count, gl); // Insert after.
  4242. }
  4243. i += count;
  4244. // Update write pointer and size.
  4245. sd_size = sd->glyphs.size();
  4246. sd_glyphs = sd->glyphs.ptrw();
  4247. continue;
  4248. }
  4249. }
  4250. }
  4251. }
  4252. }
  4253. sd->justification_ops_valid = true;
  4254. return sd->justification_ops_valid;
  4255. }
  4256. Glyph TextServerAdvanced::_shape_single_glyph(ShapedTextDataAdvanced *p_sd, char32_t p_char, hb_script_t p_script, hb_direction_t p_direction, const RID &p_font, int64_t p_font_size) {
  4257. hb_font_t *hb_font = _font_get_hb_handle(p_font, p_font_size);
  4258. bool subpos = (font_get_subpixel_positioning(p_font) == SUBPIXEL_POSITIONING_ONE_HALF) || (font_get_subpixel_positioning(p_font) == SUBPIXEL_POSITIONING_ONE_QUARTER) || (font_get_subpixel_positioning(p_font) == SUBPIXEL_POSITIONING_AUTO && p_font_size <= SUBPIXEL_POSITIONING_ONE_HALF_MAX_SIZE);
  4259. ERR_FAIL_COND_V(hb_font == nullptr, Glyph());
  4260. hb_buffer_clear_contents(p_sd->hb_buffer);
  4261. hb_buffer_set_direction(p_sd->hb_buffer, p_direction);
  4262. hb_buffer_set_flags(p_sd->hb_buffer, (hb_buffer_flags_t)(HB_BUFFER_FLAG_DEFAULT));
  4263. hb_buffer_set_script(p_sd->hb_buffer, p_script);
  4264. hb_buffer_add_utf32(p_sd->hb_buffer, (const uint32_t *)&p_char, 1, 0, 1);
  4265. hb_shape(hb_font, p_sd->hb_buffer, nullptr, 0);
  4266. unsigned int glyph_count = 0;
  4267. hb_glyph_info_t *glyph_info = hb_buffer_get_glyph_infos(p_sd->hb_buffer, &glyph_count);
  4268. hb_glyph_position_t *glyph_pos = hb_buffer_get_glyph_positions(p_sd->hb_buffer, &glyph_count);
  4269. // Process glyphs.
  4270. Glyph gl;
  4271. if (p_direction == HB_DIRECTION_RTL || p_direction == HB_DIRECTION_BTT) {
  4272. gl.flags |= TextServer::GRAPHEME_IS_RTL;
  4273. }
  4274. gl.font_rid = p_font;
  4275. gl.font_size = p_font_size;
  4276. if (glyph_count > 0) {
  4277. double scale = font_get_scale(p_font, p_font_size);
  4278. if (p_sd->orientation == ORIENTATION_HORIZONTAL) {
  4279. if (subpos) {
  4280. gl.advance = glyph_pos[0].x_advance / (64.0 / scale) + _get_extra_advance(p_font, p_font_size);
  4281. } else {
  4282. gl.advance = Math::round(glyph_pos[0].x_advance / (64.0 / scale) + _get_extra_advance(p_font, p_font_size));
  4283. }
  4284. } else {
  4285. gl.advance = -Math::round(glyph_pos[0].y_advance / (64.0 / scale));
  4286. }
  4287. gl.count = 1;
  4288. gl.index = glyph_info[0].codepoint;
  4289. if (subpos) {
  4290. gl.x_off = glyph_pos[0].x_offset / (64.0 / scale);
  4291. } else {
  4292. gl.x_off = Math::round(glyph_pos[0].x_offset / (64.0 / scale));
  4293. }
  4294. gl.y_off = -Math::round(glyph_pos[0].y_offset / (64.0 / scale));
  4295. if ((glyph_info[0].codepoint != 0) || !u_isgraph(p_char)) {
  4296. gl.flags |= GRAPHEME_IS_VALID;
  4297. }
  4298. }
  4299. return gl;
  4300. }
  4301. _FORCE_INLINE_ void TextServerAdvanced::_add_featuers(const Dictionary &p_source, Vector<hb_feature_t> &r_ftrs) {
  4302. Array keys = p_source.keys();
  4303. Array values = p_source.values();
  4304. for (int i = 0; i < keys.size(); i++) {
  4305. int32_t value = values[i];
  4306. if (value >= 0) {
  4307. hb_feature_t feature;
  4308. if (keys[i].get_type() == Variant::STRING) {
  4309. feature.tag = name_to_tag(keys[i]);
  4310. } else {
  4311. feature.tag = keys[i];
  4312. }
  4313. feature.value = value;
  4314. feature.start = 0;
  4315. feature.end = -1;
  4316. r_ftrs.push_back(feature);
  4317. }
  4318. }
  4319. }
  4320. void TextServerAdvanced::_shape_run(ShapedTextDataAdvanced *p_sd, int64_t p_start, int64_t p_end, hb_script_t p_script, hb_direction_t p_direction, Array p_fonts, int64_t p_span, int64_t p_fb_index) {
  4321. int fs = p_sd->spans[p_span].font_size;
  4322. if (p_fb_index >= p_fonts.size()) {
  4323. // Add fallback glyphs.
  4324. for (int i = p_start; i < p_end; i++) {
  4325. if (p_sd->preserve_invalid || (p_sd->preserve_control && is_control(p_sd->text[i]))) {
  4326. Glyph gl;
  4327. gl.start = i + p_sd->start;
  4328. gl.end = i + 1 + p_sd->start;
  4329. gl.count = 1;
  4330. gl.index = p_sd->text[i];
  4331. gl.font_size = fs;
  4332. gl.font_rid = RID();
  4333. if (p_direction == HB_DIRECTION_RTL || p_direction == HB_DIRECTION_BTT) {
  4334. gl.flags |= TextServer::GRAPHEME_IS_RTL;
  4335. }
  4336. if (p_sd->orientation == ORIENTATION_HORIZONTAL) {
  4337. gl.advance = get_hex_code_box_size(fs, gl.index).x;
  4338. p_sd->ascent = MAX(p_sd->ascent, get_hex_code_box_size(fs, gl.index).y);
  4339. } else {
  4340. gl.advance = get_hex_code_box_size(fs, gl.index).y;
  4341. p_sd->ascent = MAX(p_sd->ascent, Math::round(get_hex_code_box_size(fs, gl.index).x * 0.5));
  4342. p_sd->descent = MAX(p_sd->descent, Math::round(get_hex_code_box_size(fs, gl.index).x * 0.5));
  4343. }
  4344. p_sd->width += gl.advance;
  4345. p_sd->glyphs.push_back(gl);
  4346. }
  4347. }
  4348. return;
  4349. }
  4350. RID f = p_fonts[p_fb_index];
  4351. FontAdvanced *fd = font_owner.get_or_null(f);
  4352. ERR_FAIL_COND(!fd);
  4353. MutexLock lock(fd->mutex);
  4354. Vector2i fss = _get_size(fd, fs);
  4355. hb_font_t *hb_font = _font_get_hb_handle(f, fs);
  4356. double scale = font_get_scale(f, fs);
  4357. double sp_sp = p_sd->extra_spacing[SPACING_SPACE];
  4358. double sp_gl = p_sd->extra_spacing[SPACING_GLYPH];
  4359. bool last_run = (p_sd->end == p_end);
  4360. double ea = _get_extra_advance(f, fs);
  4361. bool subpos = (font_get_subpixel_positioning(f) == SUBPIXEL_POSITIONING_ONE_HALF) || (font_get_subpixel_positioning(f) == SUBPIXEL_POSITIONING_ONE_QUARTER) || (font_get_subpixel_positioning(f) == SUBPIXEL_POSITIONING_AUTO && fs <= SUBPIXEL_POSITIONING_ONE_HALF_MAX_SIZE);
  4362. ERR_FAIL_COND(hb_font == nullptr);
  4363. hb_buffer_clear_contents(p_sd->hb_buffer);
  4364. hb_buffer_set_direction(p_sd->hb_buffer, p_direction);
  4365. int flags = (p_start == 0 ? HB_BUFFER_FLAG_BOT : 0) | (p_end == p_sd->text.length() ? HB_BUFFER_FLAG_EOT : 0);
  4366. if (p_sd->preserve_control) {
  4367. flags |= HB_BUFFER_FLAG_PRESERVE_DEFAULT_IGNORABLES;
  4368. } else {
  4369. flags |= HB_BUFFER_FLAG_DEFAULT;
  4370. }
  4371. #if HB_VERSION_ATLEAST(5, 1, 0)
  4372. flags |= HB_BUFFER_FLAG_PRODUCE_SAFE_TO_INSERT_TATWEEL;
  4373. #endif
  4374. hb_buffer_set_flags(p_sd->hb_buffer, (hb_buffer_flags_t)flags);
  4375. hb_buffer_set_script(p_sd->hb_buffer, p_script);
  4376. if (p_sd->spans[p_span].language.is_empty()) {
  4377. hb_language_t lang = hb_language_from_string(TranslationServer::get_singleton()->get_tool_locale().ascii().get_data(), -1);
  4378. hb_buffer_set_language(p_sd->hb_buffer, lang);
  4379. } else {
  4380. hb_language_t lang = hb_language_from_string(p_sd->spans[p_span].language.ascii().get_data(), -1);
  4381. hb_buffer_set_language(p_sd->hb_buffer, lang);
  4382. }
  4383. hb_buffer_add_utf32(p_sd->hb_buffer, (const uint32_t *)p_sd->text.ptr(), p_sd->text.length(), p_start, p_end - p_start);
  4384. Vector<hb_feature_t> ftrs;
  4385. _add_featuers(font_get_opentype_feature_overrides(f), ftrs);
  4386. _add_featuers(p_sd->spans[p_span].features, ftrs);
  4387. hb_shape(hb_font, p_sd->hb_buffer, ftrs.is_empty() ? nullptr : &ftrs[0], ftrs.size());
  4388. unsigned int glyph_count = 0;
  4389. hb_glyph_info_t *glyph_info = hb_buffer_get_glyph_infos(p_sd->hb_buffer, &glyph_count);
  4390. hb_glyph_position_t *glyph_pos = hb_buffer_get_glyph_positions(p_sd->hb_buffer, &glyph_count);
  4391. int mod = 0;
  4392. if (fd->antialiasing == FONT_ANTIALIASING_LCD) {
  4393. TextServer::FontLCDSubpixelLayout layout = (TextServer::FontLCDSubpixelLayout)(int)GLOBAL_GET("gui/theme/lcd_subpixel_layout");
  4394. if (layout != FONT_LCD_SUBPIXEL_LAYOUT_NONE) {
  4395. mod = (layout << 24);
  4396. }
  4397. }
  4398. // Process glyphs.
  4399. if (glyph_count > 0) {
  4400. Glyph *w = (Glyph *)memalloc(glyph_count * sizeof(Glyph));
  4401. int end = (p_direction == HB_DIRECTION_RTL || p_direction == HB_DIRECTION_BTT) ? p_end : 0;
  4402. uint32_t last_cluster_id = UINT32_MAX;
  4403. unsigned int last_cluster_index = 0;
  4404. bool last_cluster_valid = true;
  4405. for (unsigned int i = 0; i < glyph_count; i++) {
  4406. if ((i > 0) && (last_cluster_id != glyph_info[i].cluster)) {
  4407. if (p_direction == HB_DIRECTION_RTL || p_direction == HB_DIRECTION_BTT) {
  4408. end = w[last_cluster_index].start;
  4409. } else {
  4410. for (unsigned int j = last_cluster_index; j < i; j++) {
  4411. w[j].end = glyph_info[i].cluster;
  4412. }
  4413. }
  4414. if (p_direction == HB_DIRECTION_RTL || p_direction == HB_DIRECTION_BTT) {
  4415. w[last_cluster_index].flags |= GRAPHEME_IS_RTL;
  4416. }
  4417. if (last_cluster_valid) {
  4418. w[last_cluster_index].flags |= GRAPHEME_IS_VALID;
  4419. }
  4420. w[last_cluster_index].count = i - last_cluster_index;
  4421. last_cluster_index = i;
  4422. last_cluster_valid = true;
  4423. }
  4424. last_cluster_id = glyph_info[i].cluster;
  4425. Glyph &gl = w[i];
  4426. gl = Glyph();
  4427. gl.start = glyph_info[i].cluster;
  4428. gl.end = end;
  4429. gl.count = 0;
  4430. gl.font_rid = p_fonts[p_fb_index];
  4431. gl.font_size = fs;
  4432. if (glyph_info[i].mask & HB_GLYPH_FLAG_UNSAFE_TO_BREAK) {
  4433. gl.flags |= GRAPHEME_IS_CONNECTED;
  4434. }
  4435. #if HB_VERSION_ATLEAST(5, 1, 0)
  4436. if (glyph_info[i].mask & HB_GLYPH_FLAG_SAFE_TO_INSERT_TATWEEL) {
  4437. gl.flags |= GRAPHEME_IS_SAFE_TO_INSERT_TATWEEL;
  4438. }
  4439. #endif
  4440. gl.index = glyph_info[i].codepoint;
  4441. if (gl.index != 0) {
  4442. _ensure_glyph(fd, fss, gl.index | mod);
  4443. if (p_sd->orientation == ORIENTATION_HORIZONTAL) {
  4444. if (subpos) {
  4445. gl.advance = glyph_pos[i].x_advance / (64.0 / scale) + ea;
  4446. } else {
  4447. gl.advance = Math::round(glyph_pos[i].x_advance / (64.0 / scale) + ea);
  4448. }
  4449. } else {
  4450. gl.advance = -Math::round(glyph_pos[i].y_advance / (64.0 / scale));
  4451. }
  4452. if (subpos) {
  4453. gl.x_off = glyph_pos[i].x_offset / (64.0 / scale);
  4454. } else {
  4455. gl.x_off = Math::round(glyph_pos[i].x_offset / (64.0 / scale));
  4456. }
  4457. gl.y_off = -Math::round(glyph_pos[i].y_offset / (64.0 / scale));
  4458. }
  4459. if (!last_run || i < glyph_count - 1) {
  4460. // Do not add extra spacing to the last glyph of the string.
  4461. if (sp_sp && is_whitespace(p_sd->text[glyph_info[i].cluster])) {
  4462. gl.advance += sp_sp;
  4463. } else {
  4464. gl.advance += sp_gl;
  4465. }
  4466. }
  4467. if (p_sd->preserve_control) {
  4468. last_cluster_valid = last_cluster_valid && ((glyph_info[i].codepoint != 0) || (p_sd->text[glyph_info[i].cluster] == 0x0009) || (u_isblank(p_sd->text[glyph_info[i].cluster]) && (gl.advance != 0)) || (!u_isblank(p_sd->text[glyph_info[i].cluster]) && is_linebreak(p_sd->text[glyph_info[i].cluster])));
  4469. } else {
  4470. last_cluster_valid = last_cluster_valid && ((glyph_info[i].codepoint != 0) || (p_sd->text[glyph_info[i].cluster] == 0x0009) || (u_isblank(p_sd->text[glyph_info[i].cluster]) && (gl.advance != 0)) || (!u_isblank(p_sd->text[glyph_info[i].cluster]) && !u_isgraph(p_sd->text[glyph_info[i].cluster])));
  4471. }
  4472. }
  4473. if (p_direction == HB_DIRECTION_LTR || p_direction == HB_DIRECTION_TTB) {
  4474. for (unsigned int j = last_cluster_index; j < glyph_count; j++) {
  4475. w[j].end = p_end;
  4476. }
  4477. }
  4478. w[last_cluster_index].count = glyph_count - last_cluster_index;
  4479. if (p_direction == HB_DIRECTION_RTL || p_direction == HB_DIRECTION_BTT) {
  4480. w[last_cluster_index].flags |= GRAPHEME_IS_RTL;
  4481. }
  4482. if (last_cluster_valid) {
  4483. w[last_cluster_index].flags |= GRAPHEME_IS_VALID;
  4484. }
  4485. // Fallback.
  4486. int failed_subrun_start = p_end + 1;
  4487. int failed_subrun_end = p_start;
  4488. for (unsigned int i = 0; i < glyph_count; i++) {
  4489. if ((w[i].flags & GRAPHEME_IS_VALID) == GRAPHEME_IS_VALID) {
  4490. if (failed_subrun_start != p_end + 1) {
  4491. _shape_run(p_sd, failed_subrun_start, failed_subrun_end, p_script, p_direction, p_fonts, p_span, p_fb_index + 1);
  4492. failed_subrun_start = p_end + 1;
  4493. failed_subrun_end = p_start;
  4494. }
  4495. for (int j = 0; j < w[i].count; j++) {
  4496. if (p_sd->orientation == ORIENTATION_HORIZONTAL) {
  4497. p_sd->ascent = MAX(p_sd->ascent, -w[i + j].y_off);
  4498. p_sd->descent = MAX(p_sd->descent, w[i + j].y_off);
  4499. } else {
  4500. double gla = Math::round(font_get_glyph_advance(f, fs, w[i + j].index).x * 0.5);
  4501. p_sd->ascent = MAX(p_sd->ascent, gla);
  4502. p_sd->descent = MAX(p_sd->descent, gla);
  4503. }
  4504. p_sd->width += w[i + j].advance;
  4505. w[i + j].start += p_sd->start;
  4506. w[i + j].end += p_sd->start;
  4507. p_sd->glyphs.push_back(w[i + j]);
  4508. }
  4509. } else {
  4510. if (failed_subrun_start >= w[i].start) {
  4511. failed_subrun_start = w[i].start;
  4512. }
  4513. if (failed_subrun_end <= w[i].end) {
  4514. failed_subrun_end = w[i].end;
  4515. }
  4516. }
  4517. i += w[i].count - 1;
  4518. }
  4519. memfree(w);
  4520. if (failed_subrun_start != p_end + 1) {
  4521. _shape_run(p_sd, failed_subrun_start, failed_subrun_end, p_script, p_direction, p_fonts, p_span, p_fb_index + 1);
  4522. }
  4523. p_sd->ascent = MAX(p_sd->ascent, font_get_ascent(f, fs));
  4524. p_sd->descent = MAX(p_sd->descent, font_get_descent(f, fs));
  4525. p_sd->upos = MAX(p_sd->upos, font_get_underline_position(f, fs));
  4526. p_sd->uthk = MAX(p_sd->uthk, font_get_underline_thickness(f, fs));
  4527. }
  4528. }
  4529. bool TextServerAdvanced::shaped_text_shape(const RID &p_shaped) {
  4530. ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
  4531. ERR_FAIL_COND_V(!sd, false);
  4532. MutexLock lock(sd->mutex);
  4533. if (sd->valid) {
  4534. return true;
  4535. }
  4536. invalidate(sd, false);
  4537. if (sd->parent != RID()) {
  4538. shaped_text_shape(sd->parent);
  4539. ShapedTextDataAdvanced *parent_sd = shaped_owner.get_or_null(sd->parent);
  4540. ERR_FAIL_COND_V(!parent_sd->valid, false);
  4541. ERR_FAIL_COND_V(!_shape_substr(sd, parent_sd, sd->start, sd->end - sd->start), false);
  4542. return true;
  4543. }
  4544. if (sd->text.length() == 0) {
  4545. sd->valid = true;
  4546. return true;
  4547. }
  4548. sd->utf16 = sd->text.utf16();
  4549. const UChar *data = sd->utf16.get_data();
  4550. // Create script iterator.
  4551. if (sd->script_iter == nullptr) {
  4552. sd->script_iter = memnew(ScriptIterator(sd->text, 0, sd->text.length()));
  4553. }
  4554. if (sd->bidi_override.is_empty()) {
  4555. sd->bidi_override.push_back(Vector2i(sd->start, sd->end));
  4556. }
  4557. for (int ov = 0; ov < sd->bidi_override.size(); ov++) {
  4558. // Create BiDi iterator.
  4559. int start = _convert_pos_inv(sd, sd->bidi_override[ov].x - sd->start);
  4560. int end = _convert_pos_inv(sd, sd->bidi_override[ov].y - sd->start);
  4561. if (start < 0 || end - start > sd->utf16.length()) {
  4562. continue;
  4563. }
  4564. UErrorCode err = U_ZERO_ERROR;
  4565. UBiDi *bidi_iter = ubidi_openSized(end, 0, &err);
  4566. ERR_FAIL_COND_V_MSG(U_FAILURE(err), false, u_errorName(err));
  4567. switch (sd->direction) {
  4568. case DIRECTION_LTR: {
  4569. ubidi_setPara(bidi_iter, data + start, end - start, UBIDI_LTR, nullptr, &err);
  4570. sd->para_direction = DIRECTION_LTR;
  4571. } break;
  4572. case DIRECTION_RTL: {
  4573. ubidi_setPara(bidi_iter, data + start, end - start, UBIDI_RTL, nullptr, &err);
  4574. sd->para_direction = DIRECTION_RTL;
  4575. } break;
  4576. case DIRECTION_AUTO: {
  4577. UBiDiDirection direction = ubidi_getBaseDirection(data + start, end - start);
  4578. if (direction != UBIDI_NEUTRAL) {
  4579. ubidi_setPara(bidi_iter, data + start, end - start, direction, nullptr, &err);
  4580. sd->para_direction = (direction == UBIDI_RTL) ? DIRECTION_RTL : DIRECTION_LTR;
  4581. } else {
  4582. ubidi_setPara(bidi_iter, data + start, end - start, UBIDI_DEFAULT_LTR, nullptr, &err);
  4583. sd->para_direction = DIRECTION_LTR;
  4584. }
  4585. } break;
  4586. }
  4587. ERR_FAIL_COND_V_MSG(U_FAILURE(err), false, u_errorName(err));
  4588. sd->bidi_iter.push_back(bidi_iter);
  4589. err = U_ZERO_ERROR;
  4590. int bidi_run_count = ubidi_countRuns(bidi_iter, &err);
  4591. ERR_FAIL_COND_V_MSG(U_FAILURE(err), false, u_errorName(err));
  4592. for (int i = 0; i < bidi_run_count; i++) {
  4593. int32_t _bidi_run_start = 0;
  4594. int32_t _bidi_run_length = 0;
  4595. hb_direction_t bidi_run_direction = HB_DIRECTION_INVALID;
  4596. bool is_rtl = (ubidi_getVisualRun(bidi_iter, i, &_bidi_run_start, &_bidi_run_length) == UBIDI_LTR);
  4597. switch (sd->orientation) {
  4598. case ORIENTATION_HORIZONTAL: {
  4599. if (is_rtl) {
  4600. bidi_run_direction = HB_DIRECTION_LTR;
  4601. } else {
  4602. bidi_run_direction = HB_DIRECTION_RTL;
  4603. }
  4604. } break;
  4605. case ORIENTATION_VERTICAL: {
  4606. if (is_rtl) {
  4607. bidi_run_direction = HB_DIRECTION_TTB;
  4608. } else {
  4609. bidi_run_direction = HB_DIRECTION_BTT;
  4610. }
  4611. }
  4612. }
  4613. int32_t bidi_run_start = _convert_pos(sd, sd->bidi_override[ov].x - sd->start + _bidi_run_start);
  4614. int32_t bidi_run_end = _convert_pos(sd, sd->bidi_override[ov].x - sd->start + _bidi_run_start + _bidi_run_length);
  4615. // Shape runs.
  4616. int scr_from = (is_rtl) ? 0 : sd->script_iter->script_ranges.size() - 1;
  4617. int scr_to = (is_rtl) ? sd->script_iter->script_ranges.size() : -1;
  4618. int scr_delta = (is_rtl) ? +1 : -1;
  4619. for (int j = scr_from; j != scr_to; j += scr_delta) {
  4620. if ((sd->script_iter->script_ranges[j].start < bidi_run_end) && (sd->script_iter->script_ranges[j].end > bidi_run_start)) {
  4621. int32_t script_run_start = MAX(sd->script_iter->script_ranges[j].start, bidi_run_start);
  4622. int32_t script_run_end = MIN(sd->script_iter->script_ranges[j].end, bidi_run_end);
  4623. char scr_buffer[5] = { 0, 0, 0, 0, 0 };
  4624. hb_tag_to_string(hb_script_to_iso15924_tag(sd->script_iter->script_ranges[j].script), scr_buffer);
  4625. String script = String(scr_buffer);
  4626. int spn_from = (is_rtl) ? 0 : sd->spans.size() - 1;
  4627. int spn_to = (is_rtl) ? sd->spans.size() : -1;
  4628. int spn_delta = (is_rtl) ? +1 : -1;
  4629. for (int k = spn_from; k != spn_to; k += spn_delta) {
  4630. const ShapedTextDataAdvanced::Span &span = sd->spans[k];
  4631. if (span.start - sd->start >= script_run_end || span.end - sd->start <= script_run_start) {
  4632. continue;
  4633. }
  4634. if (span.embedded_key != Variant()) {
  4635. // Embedded object.
  4636. if (sd->orientation == ORIENTATION_HORIZONTAL) {
  4637. sd->objects[span.embedded_key].rect.position.x = sd->width;
  4638. sd->width += sd->objects[span.embedded_key].rect.size.x;
  4639. } else {
  4640. sd->objects[span.embedded_key].rect.position.y = sd->width;
  4641. sd->width += sd->objects[span.embedded_key].rect.size.y;
  4642. }
  4643. Glyph gl;
  4644. gl.start = span.start;
  4645. gl.end = span.end;
  4646. gl.count = 1;
  4647. gl.flags = GRAPHEME_IS_VALID | GRAPHEME_IS_VIRTUAL;
  4648. if (sd->orientation == ORIENTATION_HORIZONTAL) {
  4649. gl.advance = sd->objects[span.embedded_key].rect.size.x;
  4650. } else {
  4651. gl.advance = sd->objects[span.embedded_key].rect.size.y;
  4652. }
  4653. sd->glyphs.push_back(gl);
  4654. } else {
  4655. Array fonts;
  4656. Array fonts_scr_only;
  4657. Array fonts_no_match;
  4658. int font_count = span.fonts.size();
  4659. for (int l = 0; l < font_count; l++) {
  4660. if (font_is_script_supported(span.fonts[l], script)) {
  4661. if (font_is_language_supported(span.fonts[l], span.language)) {
  4662. fonts.push_back(sd->spans[k].fonts[l]);
  4663. } else {
  4664. fonts_scr_only.push_back(sd->spans[k].fonts[l]);
  4665. }
  4666. } else {
  4667. fonts_no_match.push_back(sd->spans[k].fonts[l]);
  4668. }
  4669. }
  4670. fonts.append_array(fonts_scr_only);
  4671. fonts.append_array(fonts_no_match);
  4672. _shape_run(sd, MAX(sd->spans[k].start - sd->start, script_run_start), MIN(sd->spans[k].end - sd->start, script_run_end), sd->script_iter->script_ranges[j].script, bidi_run_direction, fonts, k, 0);
  4673. }
  4674. }
  4675. }
  4676. }
  4677. }
  4678. }
  4679. _realign(sd);
  4680. sd->valid = true;
  4681. return sd->valid;
  4682. }
  4683. bool TextServerAdvanced::shaped_text_is_ready(const RID &p_shaped) const {
  4684. const ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
  4685. ERR_FAIL_COND_V(!sd, false);
  4686. MutexLock lock(sd->mutex);
  4687. return sd->valid;
  4688. }
  4689. const Glyph *TextServerAdvanced::shaped_text_get_glyphs(const RID &p_shaped) const {
  4690. const ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
  4691. ERR_FAIL_COND_V(!sd, nullptr);
  4692. MutexLock lock(sd->mutex);
  4693. if (!sd->valid) {
  4694. const_cast<TextServerAdvanced *>(this)->shaped_text_shape(p_shaped);
  4695. }
  4696. return sd->glyphs.ptr();
  4697. }
  4698. int64_t TextServerAdvanced::shaped_text_get_glyph_count(const RID &p_shaped) const {
  4699. const ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
  4700. ERR_FAIL_COND_V(!sd, 0);
  4701. MutexLock lock(sd->mutex);
  4702. if (!sd->valid) {
  4703. const_cast<TextServerAdvanced *>(this)->shaped_text_shape(p_shaped);
  4704. }
  4705. return sd->glyphs.size();
  4706. }
  4707. const Glyph *TextServerAdvanced::shaped_text_sort_logical(const RID &p_shaped) {
  4708. ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
  4709. ERR_FAIL_COND_V(!sd, nullptr);
  4710. MutexLock lock(sd->mutex);
  4711. if (!sd->valid) {
  4712. const_cast<TextServerAdvanced *>(this)->shaped_text_shape(p_shaped);
  4713. }
  4714. if (!sd->sort_valid) {
  4715. sd->glyphs_logical = sd->glyphs;
  4716. sd->glyphs_logical.sort_custom<GlyphCompare>();
  4717. sd->sort_valid = true;
  4718. }
  4719. return sd->glyphs_logical.ptr();
  4720. }
  4721. Vector2i TextServerAdvanced::shaped_text_get_range(const RID &p_shaped) const {
  4722. const ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
  4723. ERR_FAIL_COND_V(!sd, Vector2i());
  4724. MutexLock lock(sd->mutex);
  4725. return Vector2(sd->start, sd->end);
  4726. }
  4727. Array TextServerAdvanced::shaped_text_get_objects(const RID &p_shaped) const {
  4728. Array ret;
  4729. const ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
  4730. ERR_FAIL_COND_V(!sd, ret);
  4731. MutexLock lock(sd->mutex);
  4732. for (const KeyValue<Variant, ShapedTextDataAdvanced::EmbeddedObject> &E : sd->objects) {
  4733. ret.push_back(E.key);
  4734. }
  4735. return ret;
  4736. }
  4737. Rect2 TextServerAdvanced::shaped_text_get_object_rect(const RID &p_shaped, const Variant &p_key) const {
  4738. const ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
  4739. ERR_FAIL_COND_V(!sd, Rect2());
  4740. MutexLock lock(sd->mutex);
  4741. ERR_FAIL_COND_V(!sd->objects.has(p_key), Rect2());
  4742. if (!sd->valid) {
  4743. const_cast<TextServerAdvanced *>(this)->shaped_text_shape(p_shaped);
  4744. }
  4745. return sd->objects[p_key].rect;
  4746. }
  4747. Size2 TextServerAdvanced::shaped_text_get_size(const RID &p_shaped) const {
  4748. const ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
  4749. ERR_FAIL_COND_V(!sd, Size2());
  4750. MutexLock lock(sd->mutex);
  4751. if (!sd->valid) {
  4752. const_cast<TextServerAdvanced *>(this)->shaped_text_shape(p_shaped);
  4753. }
  4754. if (sd->orientation == TextServer::ORIENTATION_HORIZONTAL) {
  4755. return Size2((sd->text_trimmed ? sd->width_trimmed : sd->width), sd->ascent + sd->descent + sd->extra_spacing[SPACING_TOP] + sd->extra_spacing[SPACING_BOTTOM]).ceil();
  4756. } else {
  4757. return Size2(sd->ascent + sd->descent + sd->extra_spacing[SPACING_TOP] + sd->extra_spacing[SPACING_BOTTOM], (sd->text_trimmed ? sd->width_trimmed : sd->width)).ceil();
  4758. }
  4759. }
  4760. double TextServerAdvanced::shaped_text_get_ascent(const RID &p_shaped) const {
  4761. const ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
  4762. ERR_FAIL_COND_V(!sd, 0.0);
  4763. MutexLock lock(sd->mutex);
  4764. if (!sd->valid) {
  4765. const_cast<TextServerAdvanced *>(this)->shaped_text_shape(p_shaped);
  4766. }
  4767. return sd->ascent + sd->extra_spacing[SPACING_TOP];
  4768. }
  4769. double TextServerAdvanced::shaped_text_get_descent(const RID &p_shaped) const {
  4770. const ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
  4771. ERR_FAIL_COND_V(!sd, 0.0);
  4772. MutexLock lock(sd->mutex);
  4773. if (!sd->valid) {
  4774. const_cast<TextServerAdvanced *>(this)->shaped_text_shape(p_shaped);
  4775. }
  4776. return sd->descent + sd->extra_spacing[SPACING_BOTTOM];
  4777. }
  4778. double TextServerAdvanced::shaped_text_get_width(const RID &p_shaped) const {
  4779. const ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
  4780. ERR_FAIL_COND_V(!sd, 0.0);
  4781. MutexLock lock(sd->mutex);
  4782. if (!sd->valid) {
  4783. const_cast<TextServerAdvanced *>(this)->shaped_text_shape(p_shaped);
  4784. }
  4785. return Math::ceil(sd->text_trimmed ? sd->width_trimmed : sd->width);
  4786. }
  4787. double TextServerAdvanced::shaped_text_get_underline_position(const RID &p_shaped) const {
  4788. const ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
  4789. ERR_FAIL_COND_V(!sd, 0.0);
  4790. MutexLock lock(sd->mutex);
  4791. if (!sd->valid) {
  4792. const_cast<TextServerAdvanced *>(this)->shaped_text_shape(p_shaped);
  4793. }
  4794. return sd->upos;
  4795. }
  4796. double TextServerAdvanced::shaped_text_get_underline_thickness(const RID &p_shaped) const {
  4797. const ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
  4798. ERR_FAIL_COND_V(!sd, 0.0);
  4799. MutexLock lock(sd->mutex);
  4800. if (!sd->valid) {
  4801. const_cast<TextServerAdvanced *>(this)->shaped_text_shape(p_shaped);
  4802. }
  4803. return sd->uthk;
  4804. }
  4805. void TextServerAdvanced::_insert_num_systems_lang() {
  4806. // Eastern Arabic numerals.
  4807. {
  4808. NumSystemData ar;
  4809. ar.lang.insert(StringName("ar")); // Arabic
  4810. ar.lang.insert(StringName("ar_AE"));
  4811. ar.lang.insert(StringName("ar_BH"));
  4812. ar.lang.insert(StringName("ar_DJ"));
  4813. ar.lang.insert(StringName("ar_EG"));
  4814. ar.lang.insert(StringName("ar_ER"));
  4815. ar.lang.insert(StringName("ar_IL"));
  4816. ar.lang.insert(StringName("ar_IQ"));
  4817. ar.lang.insert(StringName("ar_JO"));
  4818. ar.lang.insert(StringName("ar_KM"));
  4819. ar.lang.insert(StringName("ar_KW"));
  4820. ar.lang.insert(StringName("ar_LB"));
  4821. ar.lang.insert(StringName("ar_MR"));
  4822. ar.lang.insert(StringName("ar_OM"));
  4823. ar.lang.insert(StringName("ar_PS"));
  4824. ar.lang.insert(StringName("ar_QA"));
  4825. ar.lang.insert(StringName("ar_SA"));
  4826. ar.lang.insert(StringName("ar_SD"));
  4827. ar.lang.insert(StringName("ar_SO"));
  4828. ar.lang.insert(StringName("ar_SS"));
  4829. ar.lang.insert(StringName("ar_SY"));
  4830. ar.lang.insert(StringName("ar_TD"));
  4831. ar.lang.insert(StringName("ar_YE"));
  4832. ar.lang.insert(StringName("ckb")); // Central Kurdish
  4833. ar.lang.insert(StringName("ckb_IQ"));
  4834. ar.lang.insert(StringName("ckb_IR"));
  4835. ar.lang.insert(StringName("sd")); // Sindhi
  4836. ar.lang.insert(StringName("sd_PK"));
  4837. ar.lang.insert(StringName("sd_Arab"));
  4838. ar.lang.insert(StringName("sd_Arab_PK"));
  4839. ar.digits = U"٠١٢٣٤٥٦٧٨٩٫";
  4840. ar.percent_sign = U"٪";
  4841. ar.exp = U"اس";
  4842. num_systems.push_back(ar);
  4843. }
  4844. // Persian and Urdu numerals.
  4845. {
  4846. NumSystemData pr;
  4847. pr.lang.insert(StringName("fa")); // Persian
  4848. pr.lang.insert(StringName("fa_AF"));
  4849. pr.lang.insert(StringName("fa_IR"));
  4850. pr.lang.insert(StringName("ks")); // Kashmiri
  4851. pr.lang.insert(StringName("ks_IN"));
  4852. pr.lang.insert(StringName("ks_Arab"));
  4853. pr.lang.insert(StringName("ks_Arab_IN"));
  4854. pr.lang.insert(StringName("lrc")); // Northern Luri
  4855. pr.lang.insert(StringName("lrc_IQ"));
  4856. pr.lang.insert(StringName("lrc_IR"));
  4857. pr.lang.insert(StringName("mzn")); // Mazanderani
  4858. pr.lang.insert(StringName("mzn_IR"));
  4859. pr.lang.insert(StringName("pa_PK")); // Panjabi
  4860. pr.lang.insert(StringName("pa_Arab"));
  4861. pr.lang.insert(StringName("pa_Arab_PK"));
  4862. pr.lang.insert(StringName("ps")); // Pushto
  4863. pr.lang.insert(StringName("ps_AF"));
  4864. pr.lang.insert(StringName("ps_PK"));
  4865. pr.lang.insert(StringName("ur_IN")); // Urdu
  4866. pr.lang.insert(StringName("uz_AF")); // Uzbek
  4867. pr.lang.insert(StringName("uz_Arab"));
  4868. pr.lang.insert(StringName("uz_Arab_AF"));
  4869. pr.digits = U"۰۱۲۳۴۵۶۷۸۹٫";
  4870. pr.percent_sign = U"٪";
  4871. pr.exp = U"اس";
  4872. num_systems.push_back(pr);
  4873. }
  4874. // Bengali numerals.
  4875. {
  4876. NumSystemData bn;
  4877. bn.lang.insert(StringName("as")); // Assamese
  4878. bn.lang.insert(StringName("as_IN"));
  4879. bn.lang.insert(StringName("bn")); // Bengali
  4880. bn.lang.insert(StringName("bn_BD"));
  4881. bn.lang.insert(StringName("bn_IN"));
  4882. bn.lang.insert(StringName("mni")); // Manipuri
  4883. bn.lang.insert(StringName("mni_IN"));
  4884. bn.lang.insert(StringName("mni_Beng"));
  4885. bn.lang.insert(StringName("mni_Beng_IN"));
  4886. bn.digits = U"০১২৩৪৫৬৭৮৯.";
  4887. bn.percent_sign = U"%";
  4888. bn.exp = U"e";
  4889. num_systems.push_back(bn);
  4890. }
  4891. // Devanagari numerals.
  4892. {
  4893. NumSystemData mr;
  4894. mr.lang.insert(StringName("mr")); // Marathi
  4895. mr.lang.insert(StringName("mr_IN"));
  4896. mr.lang.insert(StringName("ne")); // Nepali
  4897. mr.lang.insert(StringName("ne_IN"));
  4898. mr.lang.insert(StringName("ne_NP"));
  4899. mr.lang.insert(StringName("sa")); // Sanskrit
  4900. mr.lang.insert(StringName("sa_IN"));
  4901. mr.digits = U"०१२३४५६७८९.";
  4902. mr.percent_sign = U"%";
  4903. mr.exp = U"e";
  4904. num_systems.push_back(mr);
  4905. }
  4906. // Dzongkha numerals.
  4907. {
  4908. NumSystemData dz;
  4909. dz.lang.insert(StringName("dz")); // Dzongkha
  4910. dz.lang.insert(StringName("dz_BT"));
  4911. dz.digits = U"༠༡༢༣༤༥༦༧༨༩.";
  4912. dz.percent_sign = U"%";
  4913. dz.exp = U"e";
  4914. num_systems.push_back(dz);
  4915. }
  4916. // Santali numerals.
  4917. {
  4918. NumSystemData sat;
  4919. sat.lang.insert(StringName("sat")); // Santali
  4920. sat.lang.insert(StringName("sat_IN"));
  4921. sat.lang.insert(StringName("sat_Olck"));
  4922. sat.lang.insert(StringName("sat_Olck_IN"));
  4923. sat.digits = U"᱐᱑᱒᱓᱔᱕᱖᱗᱘᱙.";
  4924. sat.percent_sign = U"%";
  4925. sat.exp = U"e";
  4926. num_systems.push_back(sat);
  4927. }
  4928. // Burmese numerals.
  4929. {
  4930. NumSystemData my;
  4931. my.lang.insert(StringName("my")); // Burmese
  4932. my.lang.insert(StringName("my_MM"));
  4933. my.digits = U"၀၁၂၃၄၅၆၇၈၉.";
  4934. my.percent_sign = U"%";
  4935. my.exp = U"e";
  4936. num_systems.push_back(my);
  4937. }
  4938. // Chakma numerals.
  4939. {
  4940. NumSystemData ccp;
  4941. ccp.lang.insert(StringName("ccp")); // Chakma
  4942. ccp.lang.insert(StringName("ccp_BD"));
  4943. ccp.lang.insert(StringName("ccp_IN"));
  4944. ccp.digits = U"𑄶𑄷𑄸𑄹𑄺𑄻𑄼𑄽𑄾𑄿.";
  4945. ccp.percent_sign = U"%";
  4946. ccp.exp = U"e";
  4947. num_systems.push_back(ccp);
  4948. }
  4949. // Adlam numerals.
  4950. {
  4951. NumSystemData ff;
  4952. ff.lang.insert(StringName("ff")); // Fulah
  4953. ff.lang.insert(StringName("ff_Adlm_BF"));
  4954. ff.lang.insert(StringName("ff_Adlm_CM"));
  4955. ff.lang.insert(StringName("ff_Adlm_GH"));
  4956. ff.lang.insert(StringName("ff_Adlm_GM"));
  4957. ff.lang.insert(StringName("ff_Adlm_GN"));
  4958. ff.lang.insert(StringName("ff_Adlm_GW"));
  4959. ff.lang.insert(StringName("ff_Adlm_LR"));
  4960. ff.lang.insert(StringName("ff_Adlm_MR"));
  4961. ff.lang.insert(StringName("ff_Adlm_NE"));
  4962. ff.lang.insert(StringName("ff_Adlm_NG"));
  4963. ff.lang.insert(StringName("ff_Adlm_SL"));
  4964. ff.lang.insert(StringName("ff_Adlm_SN"));
  4965. ff.digits = U"𞥐𞥑𞥒𞥓𞥔𞥕𞥖𞥗𞥘𞥙.";
  4966. ff.percent_sign = U"%";
  4967. ff.exp = U"e";
  4968. num_systems.push_back(ff);
  4969. }
  4970. }
  4971. String TextServerAdvanced::format_number(const String &p_string, const String &p_language) const {
  4972. const StringName lang = (p_language.is_empty()) ? TranslationServer::get_singleton()->get_tool_locale() : p_language;
  4973. String res = p_string;
  4974. for (int i = 0; i < num_systems.size(); i++) {
  4975. if (num_systems[i].lang.has(lang)) {
  4976. if (num_systems[i].digits.is_empty()) {
  4977. return p_string;
  4978. }
  4979. res.replace("e", num_systems[i].exp);
  4980. res.replace("E", num_systems[i].exp);
  4981. char32_t *data = res.ptrw();
  4982. for (int j = 0; j < res.length(); j++) {
  4983. if (data[j] >= 0x30 && data[j] <= 0x39) {
  4984. data[j] = num_systems[i].digits[data[j] - 0x30];
  4985. } else if (data[j] == '.' || data[j] == ',') {
  4986. data[j] = num_systems[i].digits[10];
  4987. }
  4988. }
  4989. break;
  4990. }
  4991. }
  4992. return res;
  4993. }
  4994. String TextServerAdvanced::parse_number(const String &p_string, const String &p_language) const {
  4995. const StringName lang = (p_language.is_empty()) ? TranslationServer::get_singleton()->get_tool_locale() : p_language;
  4996. String res = p_string;
  4997. for (int i = 0; i < num_systems.size(); i++) {
  4998. if (num_systems[i].lang.has(lang)) {
  4999. if (num_systems[i].digits.is_empty()) {
  5000. return p_string;
  5001. }
  5002. res.replace(num_systems[i].exp, "e");
  5003. char32_t *data = res.ptrw();
  5004. for (int j = 0; j < res.length(); j++) {
  5005. if (data[j] == num_systems[i].digits[10]) {
  5006. data[j] = '.';
  5007. } else {
  5008. for (int k = 0; k < 10; k++) {
  5009. if (data[j] == num_systems[i].digits[k]) {
  5010. data[j] = 0x30 + k;
  5011. }
  5012. }
  5013. }
  5014. }
  5015. break;
  5016. }
  5017. }
  5018. return res;
  5019. }
  5020. String TextServerAdvanced::percent_sign(const String &p_language) const {
  5021. const StringName lang = (p_language.is_empty()) ? TranslationServer::get_singleton()->get_tool_locale() : p_language;
  5022. for (int i = 0; i < num_systems.size(); i++) {
  5023. if (num_systems[i].lang.has(lang)) {
  5024. if (num_systems[i].percent_sign.is_empty()) {
  5025. return "%";
  5026. }
  5027. return num_systems[i].percent_sign;
  5028. }
  5029. }
  5030. return "%";
  5031. }
  5032. int TextServerAdvanced::is_confusable(const String &p_string, const PackedStringArray &p_dict) const {
  5033. UErrorCode status = U_ZERO_ERROR;
  5034. int match_index = -1;
  5035. Char16String utf16 = p_string.utf16();
  5036. Vector<UChar *> skeletons;
  5037. skeletons.resize(p_dict.size());
  5038. USpoofChecker *sc = uspoof_open(&status);
  5039. uspoof_setChecks(sc, USPOOF_CONFUSABLE, &status);
  5040. for (int i = 0; i < p_dict.size(); i++) {
  5041. Char16String word = p_dict[i].utf16();
  5042. int32_t len = uspoof_getSkeleton(sc, 0, word.get_data(), -1, NULL, 0, &status);
  5043. skeletons.write[i] = (UChar *)memalloc(++len * sizeof(UChar));
  5044. status = U_ZERO_ERROR;
  5045. uspoof_getSkeleton(sc, 0, word.get_data(), -1, skeletons.write[i], len, &status);
  5046. }
  5047. int32_t len = uspoof_getSkeleton(sc, 0, utf16.get_data(), -1, NULL, 0, &status);
  5048. UChar *skel = (UChar *)memalloc(++len * sizeof(UChar));
  5049. status = U_ZERO_ERROR;
  5050. uspoof_getSkeleton(sc, 0, utf16.get_data(), -1, skel, len, &status);
  5051. for (int i = 0; i < skeletons.size(); i++) {
  5052. if (u_strcmp(skel, skeletons[i]) == 0) {
  5053. match_index = i;
  5054. break;
  5055. }
  5056. }
  5057. memfree(skel);
  5058. for (int i = 0; i < skeletons.size(); i++) {
  5059. memfree(skeletons.write[i]);
  5060. }
  5061. uspoof_close(sc);
  5062. ERR_FAIL_COND_V_MSG(U_FAILURE(status), -1, u_errorName(status));
  5063. return match_index;
  5064. }
  5065. bool TextServerAdvanced::spoof_check(const String &p_string) const {
  5066. UErrorCode status = U_ZERO_ERROR;
  5067. Char16String utf16 = p_string.utf16();
  5068. USet *allowed = uset_openEmpty();
  5069. uset_addAll(allowed, uspoof_getRecommendedSet(&status));
  5070. uset_addAll(allowed, uspoof_getInclusionSet(&status));
  5071. USpoofChecker *sc = uspoof_open(&status);
  5072. uspoof_setAllowedChars(sc, allowed, &status);
  5073. uspoof_setRestrictionLevel(sc, USPOOF_MODERATELY_RESTRICTIVE);
  5074. int32_t bitmask = uspoof_check(sc, utf16.get_data(), -1, NULL, &status);
  5075. uspoof_close(sc);
  5076. uset_close(allowed);
  5077. ERR_FAIL_COND_V_MSG(U_FAILURE(status), false, u_errorName(status));
  5078. return (bitmask != 0);
  5079. }
  5080. String TextServerAdvanced::strip_diacritics(const String &p_string) const {
  5081. UErrorCode err = U_ZERO_ERROR;
  5082. // Get NFKD normalizer singleton.
  5083. const UNormalizer2 *unorm = unorm2_getNFKDInstance(&err);
  5084. ERR_FAIL_COND_V_MSG(U_FAILURE(err), TextServer::strip_diacritics(p_string), u_errorName(err));
  5085. // Convert to UTF-16.
  5086. Char16String utf16 = p_string.utf16();
  5087. // Normalize.
  5088. Vector<char16_t> normalized;
  5089. err = U_ZERO_ERROR;
  5090. int32_t len = unorm2_normalize(unorm, utf16.get_data(), -1, nullptr, 0, &err);
  5091. ERR_FAIL_COND_V_MSG(err != U_BUFFER_OVERFLOW_ERROR, TextServer::strip_diacritics(p_string), u_errorName(err));
  5092. normalized.resize(len);
  5093. err = U_ZERO_ERROR;
  5094. unorm2_normalize(unorm, utf16.get_data(), -1, normalized.ptrw(), len, &err);
  5095. ERR_FAIL_COND_V_MSG(U_FAILURE(err), TextServer::strip_diacritics(p_string), u_errorName(err));
  5096. // Convert back to UTF-32.
  5097. String normalized_string = String::utf16(normalized.ptr(), len);
  5098. // Strip combining characters.
  5099. String result;
  5100. for (int i = 0; i < normalized_string.length(); i++) {
  5101. if (u_getCombiningClass(normalized_string[i]) == 0) {
  5102. result = result + normalized_string[i];
  5103. }
  5104. }
  5105. return result;
  5106. }
  5107. String TextServerAdvanced::string_to_upper(const String &p_string, const String &p_language) const {
  5108. const String lang = (p_language.is_empty()) ? TranslationServer::get_singleton()->get_tool_locale() : p_language;
  5109. // Convert to UTF-16.
  5110. Char16String utf16 = p_string.utf16();
  5111. Vector<char16_t> upper;
  5112. UErrorCode err = U_ZERO_ERROR;
  5113. int32_t len = u_strToUpper(nullptr, 0, utf16.get_data(), -1, lang.ascii().get_data(), &err);
  5114. ERR_FAIL_COND_V_MSG(err != U_BUFFER_OVERFLOW_ERROR, p_string, u_errorName(err));
  5115. upper.resize(len);
  5116. err = U_ZERO_ERROR;
  5117. u_strToUpper(upper.ptrw(), len, utf16.get_data(), -1, lang.ascii().get_data(), &err);
  5118. ERR_FAIL_COND_V_MSG(U_FAILURE(err), p_string, u_errorName(err));
  5119. // Convert back to UTF-32.
  5120. return String::utf16(upper.ptr(), len);
  5121. }
  5122. String TextServerAdvanced::string_to_lower(const String &p_string, const String &p_language) const {
  5123. const String lang = (p_language.is_empty()) ? TranslationServer::get_singleton()->get_tool_locale() : p_language;
  5124. // Convert to UTF-16.
  5125. Char16String utf16 = p_string.utf16();
  5126. Vector<char16_t> lower;
  5127. UErrorCode err = U_ZERO_ERROR;
  5128. int32_t len = u_strToLower(nullptr, 0, utf16.get_data(), -1, lang.ascii().get_data(), &err);
  5129. ERR_FAIL_COND_V_MSG(err != U_BUFFER_OVERFLOW_ERROR, p_string, u_errorName(err));
  5130. lower.resize(len);
  5131. err = U_ZERO_ERROR;
  5132. u_strToLower(lower.ptrw(), len, utf16.get_data(), -1, lang.ascii().get_data(), &err);
  5133. ERR_FAIL_COND_V_MSG(U_FAILURE(err), p_string, u_errorName(err));
  5134. // Convert back to UTF-32.
  5135. return String::utf16(lower.ptr(), len);
  5136. }
  5137. PackedInt32Array TextServerAdvanced::string_get_word_breaks(const String &p_string, const String &p_language) const {
  5138. const String lang = (p_language.is_empty()) ? TranslationServer::get_singleton()->get_tool_locale() : p_language;
  5139. // Convert to UTF-16.
  5140. Char16String utf16 = p_string.utf16();
  5141. HashSet<int> breaks;
  5142. UErrorCode err = U_ZERO_ERROR;
  5143. UBreakIterator *bi = ubrk_open(UBRK_LINE, lang.ascii().get_data(), (const UChar *)utf16.get_data(), utf16.length(), &err);
  5144. if (U_FAILURE(err)) {
  5145. // No data loaded - use fallback.
  5146. for (int i = 0; i < p_string.length(); i++) {
  5147. char32_t c = p_string[i];
  5148. if (is_whitespace(c) || is_linebreak(c)) {
  5149. breaks.insert(i);
  5150. }
  5151. }
  5152. } else {
  5153. while (ubrk_next(bi) != UBRK_DONE) {
  5154. int pos = _convert_pos(p_string, utf16, ubrk_current(bi)) - 1;
  5155. if (pos != p_string.length() - 1) {
  5156. breaks.insert(pos);
  5157. }
  5158. }
  5159. }
  5160. ubrk_close(bi);
  5161. PackedInt32Array ret;
  5162. for (int i = 0; i < p_string.length(); i++) {
  5163. char32_t c = p_string[i];
  5164. if (c == 0xfffc) {
  5165. continue;
  5166. }
  5167. if (u_ispunct(c) && c != 0x005F) {
  5168. ret.push_back(i);
  5169. continue;
  5170. }
  5171. if (is_underscore(c)) {
  5172. ret.push_back(i);
  5173. continue;
  5174. }
  5175. if (breaks.has(i)) {
  5176. ret.push_back(i);
  5177. continue;
  5178. }
  5179. }
  5180. return ret;
  5181. }
  5182. bool TextServerAdvanced::is_valid_identifier(const String &p_string) const {
  5183. enum UAX31SequenceStatus {
  5184. SEQ_NOT_STARTED,
  5185. SEQ_STARTED,
  5186. SEQ_STARTED_VIR,
  5187. SEQ_NEAR_END,
  5188. };
  5189. const char32_t *str = p_string.ptr();
  5190. int len = p_string.length();
  5191. if (len == 0) {
  5192. return false; // Empty string.
  5193. }
  5194. UErrorCode err = U_ZERO_ERROR;
  5195. Char16String utf16 = p_string.utf16();
  5196. const UNormalizer2 *norm_c = unorm2_getNFCInstance(&err);
  5197. if (U_FAILURE(err)) {
  5198. return false; // Failed to load normalizer.
  5199. }
  5200. bool isnurom = unorm2_isNormalized(norm_c, utf16.ptr(), utf16.length(), &err);
  5201. if (U_FAILURE(err) || !isnurom) {
  5202. return false; // Do not conform to Normalization Form C.
  5203. }
  5204. UAX31SequenceStatus A1_sequence_status = SEQ_NOT_STARTED;
  5205. UScriptCode A1_scr = USCRIPT_INHERITED;
  5206. UAX31SequenceStatus A2_sequence_status = SEQ_NOT_STARTED;
  5207. UScriptCode A2_scr = USCRIPT_INHERITED;
  5208. UAX31SequenceStatus B_sequence_status = SEQ_NOT_STARTED;
  5209. UScriptCode B_scr = USCRIPT_INHERITED;
  5210. for (int i = 0; i < len; i++) {
  5211. err = U_ZERO_ERROR;
  5212. UScriptCode scr = uscript_getScript(str[i], &err);
  5213. if (U_FAILURE(err)) {
  5214. return false; // Invalid script.
  5215. }
  5216. if (uscript_getUsage(scr) != USCRIPT_USAGE_RECOMMENDED) {
  5217. return false; // Not a recommended script.
  5218. }
  5219. uint8_t cat = u_charType(str[i]);
  5220. int32_t jt = u_getIntPropertyValue(str[i], UCHAR_JOINING_TYPE);
  5221. // UAX #31 section 2.3 subsections A1, A2 and B, check ZWNJ and ZWJ usage.
  5222. switch (A1_sequence_status) {
  5223. case SEQ_NEAR_END: {
  5224. if ((A1_scr > USCRIPT_INHERITED) && (scr > USCRIPT_INHERITED) && (scr != A1_scr)) {
  5225. return false; // Mixed script.
  5226. }
  5227. if (jt == U_JT_RIGHT_JOINING || jt == U_JT_DUAL_JOINING) {
  5228. A1_sequence_status = SEQ_NOT_STARTED; // Valid end of sequence, reset.
  5229. } else if (jt != U_JT_TRANSPARENT) {
  5230. return false; // Invalid end of sequence.
  5231. }
  5232. } break;
  5233. case SEQ_STARTED: {
  5234. if ((A1_scr > USCRIPT_INHERITED) && (scr > USCRIPT_INHERITED) && (scr != A1_scr)) {
  5235. A1_sequence_status = SEQ_NOT_STARTED; // Reset.
  5236. } else {
  5237. if (jt != U_JT_TRANSPARENT) {
  5238. if (str[i] == 0x200C /*ZWNJ*/) {
  5239. A1_sequence_status = SEQ_NEAR_END;
  5240. continue;
  5241. } else {
  5242. A1_sequence_status = SEQ_NOT_STARTED; // Reset.
  5243. }
  5244. }
  5245. }
  5246. } break;
  5247. default:
  5248. break;
  5249. }
  5250. if (A1_sequence_status == SEQ_NOT_STARTED) {
  5251. if (jt == U_JT_LEFT_JOINING || jt == U_JT_DUAL_JOINING) {
  5252. A1_sequence_status = SEQ_STARTED;
  5253. A1_scr = scr;
  5254. }
  5255. };
  5256. switch (A2_sequence_status) {
  5257. case SEQ_NEAR_END: {
  5258. if ((A2_scr > USCRIPT_INHERITED) && (scr > USCRIPT_INHERITED) && (scr != A2_scr)) {
  5259. return false; // Mixed script.
  5260. }
  5261. if (cat == U_UPPERCASE_LETTER || cat == U_LOWERCASE_LETTER || cat == U_TITLECASE_LETTER || cat == U_MODIFIER_LETTER || cat == U_OTHER_LETTER) {
  5262. A2_sequence_status = SEQ_NOT_STARTED; // Valid end of sequence, reset.
  5263. } else if (cat != U_MODIFIER_LETTER || u_getCombiningClass(str[i]) == 0) {
  5264. return false; // Invalid end of sequence.
  5265. }
  5266. } break;
  5267. case SEQ_STARTED_VIR: {
  5268. if ((A2_scr > USCRIPT_INHERITED) && (scr > USCRIPT_INHERITED) && (scr != A2_scr)) {
  5269. A2_sequence_status = SEQ_NOT_STARTED; // Reset.
  5270. } else {
  5271. if (str[i] == 0x200C /*ZWNJ*/) {
  5272. A2_sequence_status = SEQ_NEAR_END;
  5273. continue;
  5274. } else if (cat != U_MODIFIER_LETTER || u_getCombiningClass(str[i]) == 0) {
  5275. A2_sequence_status = SEQ_NOT_STARTED; // Reset.
  5276. }
  5277. }
  5278. } break;
  5279. case SEQ_STARTED: {
  5280. if ((A2_scr > USCRIPT_INHERITED) && (scr > USCRIPT_INHERITED) && (scr != A2_scr)) {
  5281. A2_sequence_status = SEQ_NOT_STARTED; // Reset.
  5282. } else {
  5283. if (u_getCombiningClass(str[i]) == 9 /*Virama Combining Class*/) {
  5284. A2_sequence_status = SEQ_STARTED_VIR;
  5285. } else if (cat != U_MODIFIER_LETTER) {
  5286. A2_sequence_status = SEQ_NOT_STARTED; // Reset.
  5287. }
  5288. }
  5289. } break;
  5290. default:
  5291. break;
  5292. }
  5293. if (A2_sequence_status == SEQ_NOT_STARTED) {
  5294. if (cat == U_UPPERCASE_LETTER || cat == U_LOWERCASE_LETTER || cat == U_TITLECASE_LETTER || cat == U_MODIFIER_LETTER || cat == U_OTHER_LETTER) {
  5295. A2_sequence_status = SEQ_STARTED;
  5296. A2_scr = scr;
  5297. }
  5298. }
  5299. switch (B_sequence_status) {
  5300. case SEQ_NEAR_END: {
  5301. if ((B_scr > USCRIPT_INHERITED) && (scr > USCRIPT_INHERITED) && (scr != B_scr)) {
  5302. return false; // Mixed script.
  5303. }
  5304. if (u_getIntPropertyValue(str[i], UCHAR_INDIC_SYLLABIC_CATEGORY) != U_INSC_VOWEL_DEPENDENT) {
  5305. B_sequence_status = SEQ_NOT_STARTED; // Valid end of sequence, reset.
  5306. } else {
  5307. return false; // Invalid end of sequence.
  5308. }
  5309. } break;
  5310. case SEQ_STARTED_VIR: {
  5311. if ((B_scr > USCRIPT_INHERITED) && (scr > USCRIPT_INHERITED) && (scr != B_scr)) {
  5312. B_sequence_status = SEQ_NOT_STARTED; // Reset.
  5313. } else {
  5314. if (str[i] == 0x200D /*ZWJ*/) {
  5315. B_sequence_status = SEQ_NEAR_END;
  5316. continue;
  5317. } else if (cat != U_MODIFIER_LETTER || u_getCombiningClass(str[i]) == 0) {
  5318. B_sequence_status = SEQ_NOT_STARTED; // Reset.
  5319. }
  5320. }
  5321. } break;
  5322. case SEQ_STARTED: {
  5323. if ((B_scr > USCRIPT_INHERITED) && (scr > USCRIPT_INHERITED) && (scr != B_scr)) {
  5324. B_sequence_status = SEQ_NOT_STARTED; // Reset.
  5325. } else {
  5326. if (u_getCombiningClass(str[i]) == 9 /*Virama Combining Class*/) {
  5327. B_sequence_status = SEQ_STARTED_VIR;
  5328. } else if (cat != U_MODIFIER_LETTER) {
  5329. B_sequence_status = SEQ_NOT_STARTED; // Reset.
  5330. }
  5331. }
  5332. } break;
  5333. default:
  5334. break;
  5335. }
  5336. if (B_sequence_status == SEQ_NOT_STARTED) {
  5337. if (cat == U_UPPERCASE_LETTER || cat == U_LOWERCASE_LETTER || cat == U_TITLECASE_LETTER || cat == U_MODIFIER_LETTER || cat == U_OTHER_LETTER) {
  5338. B_sequence_status = SEQ_STARTED;
  5339. B_scr = scr;
  5340. }
  5341. }
  5342. if (u_hasBinaryProperty(str[i], UCHAR_PATTERN_SYNTAX) || u_hasBinaryProperty(str[i], UCHAR_PATTERN_WHITE_SPACE) || u_hasBinaryProperty(str[i], UCHAR_NONCHARACTER_CODE_POINT)) {
  5343. return false; // Not a XID_Start or XID_Continue character.
  5344. }
  5345. if (i == 0) {
  5346. if (!(cat == U_LOWERCASE_LETTER || cat == U_UPPERCASE_LETTER || cat == U_TITLECASE_LETTER || cat == U_OTHER_LETTER || cat == U_MODIFIER_LETTER || cat == U_LETTER_NUMBER || str[0] == 0x2118 || str[0] == 0x212E || str[0] == 0x309B || str[0] == 0x309C || str[0] == 0x005F)) {
  5347. return false; // Not a XID_Start character.
  5348. }
  5349. } else {
  5350. if (!(cat == U_LOWERCASE_LETTER || cat == U_UPPERCASE_LETTER || cat == U_TITLECASE_LETTER || cat == U_OTHER_LETTER || cat == U_MODIFIER_LETTER || cat == U_LETTER_NUMBER || cat == U_NON_SPACING_MARK || cat == U_COMBINING_SPACING_MARK || cat == U_DECIMAL_DIGIT_NUMBER || cat == U_CONNECTOR_PUNCTUATION || str[i] == 0x2118 || str[i] == 0x212E || str[i] == 0x309B || str[i] == 0x309C || str[i] == 0x1369 || str[i] == 0x1371 || str[i] == 0x00B7 || str[i] == 0x0387 || str[i] == 0x19DA || str[i] == 0x0E33 || str[i] == 0x0EB3 || str[i] == 0xFF9E || str[i] == 0xFF9F)) {
  5351. return false; // Not a XID_Continue character.
  5352. }
  5353. }
  5354. }
  5355. return true;
  5356. }
  5357. TextServerAdvanced::TextServerAdvanced() {
  5358. _insert_num_systems_lang();
  5359. _insert_feature_sets();
  5360. _bmp_create_font_funcs();
  5361. }
  5362. TextServerAdvanced::~TextServerAdvanced() {
  5363. _bmp_free_font_funcs();
  5364. #ifdef MODULE_FREETYPE_ENABLED
  5365. if (ft_library != nullptr) {
  5366. FT_Done_FreeType(ft_library);
  5367. }
  5368. #endif
  5369. u_cleanup();
  5370. }