text_server_adv.cpp 293 KB

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