text_server_adv.cpp 287 KB

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