2
0

text_server_adv.cpp 238 KB

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