text_server_adv.cpp 275 KB

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