text_server_adv.cpp 261 KB

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