text_server_adv.cpp 248 KB

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