text_server_adv.cpp 270 KB

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