text_server_adv.cpp 197 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649
  1. /*************************************************************************/
  2. /* text_server_adv.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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.hpp>
  34. #include <godot_cpp/classes/rendering_server.hpp>
  35. #include <godot_cpp/classes/translation_server.hpp>
  36. #include <godot_cpp/core/error_macros.hpp>
  37. using namespace godot;
  38. #else
  39. // Headers for building as built-in module.
  40. #include "core/core_bind.h"
  41. #include "core/error/error_macros.h"
  42. #include "core/string/print_string.h"
  43. #include "core/string/translation.h"
  44. #include "modules/modules_enabled.gen.h" // For freetype, msdfgen.
  45. using namespace core_bind;
  46. #endif
  47. // Built-in ICU data.
  48. #ifdef ICU_STATIC_DATA
  49. #include "icudata.gen.h"
  50. #endif
  51. // Thirdparty headers.
  52. #ifdef MODULE_MSDFGEN_ENABLED
  53. #include "core/ShapeDistanceFinder.h"
  54. #include "core/contour-combiners.h"
  55. #include "core/edge-selectors.h"
  56. #include "msdfgen.h"
  57. #endif
  58. /*************************************************************************/
  59. /* bmp_font_t HarfBuzz Bitmap font interface */
  60. /*************************************************************************/
  61. hb_font_funcs_t *TextServerAdvanced::funcs = nullptr;
  62. TextServerAdvanced::bmp_font_t *TextServerAdvanced::_bmp_font_create(TextServerAdvanced::FontDataForSizeAdvanced *p_face, bool p_unref) {
  63. bmp_font_t *bm_font = memnew(bmp_font_t);
  64. if (!bm_font) {
  65. return nullptr;
  66. }
  67. bm_font->face = p_face;
  68. bm_font->unref = p_unref;
  69. return bm_font;
  70. }
  71. void TextServerAdvanced::_bmp_font_destroy(void *p_data) {
  72. bmp_font_t *bm_font = static_cast<bmp_font_t *>(p_data);
  73. memdelete(bm_font);
  74. }
  75. 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) {
  76. const bmp_font_t *bm_font = static_cast<const bmp_font_t *>(p_font_data);
  77. if (!bm_font->face) {
  78. return false;
  79. }
  80. if (!bm_font->face->glyph_map.has(p_unicode)) {
  81. if (bm_font->face->glyph_map.has(0xf000u + p_unicode)) {
  82. *r_glyph = 0xf000u + p_unicode;
  83. return true;
  84. } else {
  85. return false;
  86. }
  87. }
  88. *r_glyph = p_unicode;
  89. return true;
  90. }
  91. 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) {
  92. const bmp_font_t *bm_font = static_cast<const bmp_font_t *>(p_font_data);
  93. if (!bm_font->face) {
  94. return 0;
  95. }
  96. if (!bm_font->face->glyph_map.has(p_glyph)) {
  97. return 0;
  98. }
  99. return bm_font->face->glyph_map[p_glyph].advance.x * 64;
  100. }
  101. 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) {
  102. const bmp_font_t *bm_font = static_cast<const bmp_font_t *>(p_font_data);
  103. if (!bm_font->face) {
  104. return 0;
  105. }
  106. if (!bm_font->face->glyph_map.has(p_glyph)) {
  107. return 0;
  108. }
  109. return -bm_font->face->glyph_map[p_glyph].advance.y * 64;
  110. }
  111. 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) {
  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. if (!bm_font->face->kerning_map.has(Vector2i(p_left_glyph, p_right_glyph))) {
  117. return 0;
  118. }
  119. return bm_font->face->kerning_map[Vector2i(p_left_glyph, p_right_glyph)].x * 64;
  120. }
  121. 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) {
  122. const bmp_font_t *bm_font = static_cast<const bmp_font_t *>(p_font_data);
  123. if (!bm_font->face) {
  124. return false;
  125. }
  126. if (!bm_font->face->glyph_map.has(p_glyph)) {
  127. return false;
  128. }
  129. *r_x = bm_font->face->glyph_map[p_glyph].advance.x * 32;
  130. *r_y = -bm_font->face->ascent * 64;
  131. return true;
  132. }
  133. 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) {
  134. const bmp_font_t *bm_font = static_cast<const bmp_font_t *>(p_font_data);
  135. if (!bm_font->face) {
  136. return false;
  137. }
  138. if (!bm_font->face->glyph_map.has(p_glyph)) {
  139. return false;
  140. }
  141. r_extents->x_bearing = 0;
  142. r_extents->y_bearing = 0;
  143. r_extents->width = bm_font->face->glyph_map[p_glyph].rect.size.x * 64;
  144. r_extents->height = bm_font->face->glyph_map[p_glyph].rect.size.y * 64;
  145. return true;
  146. }
  147. 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) {
  148. const bmp_font_t *bm_font = static_cast<const bmp_font_t *>(p_font_data);
  149. if (!bm_font->face) {
  150. return false;
  151. }
  152. r_metrics->ascender = bm_font->face->ascent;
  153. r_metrics->descender = bm_font->face->descent;
  154. r_metrics->line_gap = 0;
  155. return true;
  156. }
  157. void TextServerAdvanced::_bmp_create_font_funcs() {
  158. if (funcs == nullptr) {
  159. funcs = hb_font_funcs_create();
  160. hb_font_funcs_set_font_h_extents_func(funcs, _bmp_get_font_h_extents, nullptr, nullptr);
  161. hb_font_funcs_set_nominal_glyph_func(funcs, _bmp_get_nominal_glyph, nullptr, nullptr);
  162. hb_font_funcs_set_glyph_h_advance_func(funcs, _bmp_get_glyph_h_advance, nullptr, nullptr);
  163. hb_font_funcs_set_glyph_v_advance_func(funcs, _bmp_get_glyph_v_advance, nullptr, nullptr);
  164. hb_font_funcs_set_glyph_v_origin_func(funcs, _bmp_get_glyph_v_origin, nullptr, nullptr);
  165. hb_font_funcs_set_glyph_h_kerning_func(funcs, _bmp_get_glyph_h_kerning, nullptr, nullptr);
  166. hb_font_funcs_set_glyph_extents_func(funcs, _bmp_get_glyph_extents, nullptr, nullptr);
  167. hb_font_funcs_make_immutable(funcs);
  168. }
  169. }
  170. void TextServerAdvanced::_bmp_free_font_funcs() {
  171. if (funcs != nullptr) {
  172. hb_font_funcs_destroy(funcs);
  173. funcs = nullptr;
  174. }
  175. }
  176. void TextServerAdvanced::_bmp_font_set_funcs(hb_font_t *p_font, TextServerAdvanced::FontDataForSizeAdvanced *p_face, bool p_unref) {
  177. hb_font_set_funcs(p_font, funcs, _bmp_font_create(p_face, p_unref), _bmp_font_destroy);
  178. }
  179. hb_font_t *TextServerAdvanced::_bmp_font_create(TextServerAdvanced::FontDataForSizeAdvanced *p_face, hb_destroy_func_t p_destroy) {
  180. hb_font_t *font;
  181. hb_face_t *face = hb_face_create(nullptr, 0);
  182. font = hb_font_create(face);
  183. hb_face_destroy(face);
  184. _bmp_font_set_funcs(font, p_face, false);
  185. return font;
  186. }
  187. /*************************************************************************/
  188. /* Character properties. */
  189. /*************************************************************************/
  190. _FORCE_INLINE_ bool is_ain(char32_t p_chr) {
  191. return u_getIntPropertyValue(p_chr, UCHAR_JOINING_GROUP) == U_JG_AIN;
  192. }
  193. _FORCE_INLINE_ bool is_alef(char32_t p_chr) {
  194. return u_getIntPropertyValue(p_chr, UCHAR_JOINING_GROUP) == U_JG_ALEF;
  195. }
  196. _FORCE_INLINE_ bool is_beh(char32_t p_chr) {
  197. int32_t prop = u_getIntPropertyValue(p_chr, UCHAR_JOINING_GROUP);
  198. 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);
  199. }
  200. _FORCE_INLINE_ bool is_dal(char32_t p_chr) {
  201. return u_getIntPropertyValue(p_chr, UCHAR_JOINING_GROUP) == U_JG_DAL;
  202. }
  203. _FORCE_INLINE_ bool is_feh(char32_t p_chr) {
  204. return (u_getIntPropertyValue(p_chr, UCHAR_JOINING_GROUP) == U_JG_FEH) || (u_getIntPropertyValue(p_chr, UCHAR_JOINING_GROUP) == U_JG_AFRICAN_FEH);
  205. }
  206. _FORCE_INLINE_ bool is_gaf(char32_t p_chr) {
  207. return u_getIntPropertyValue(p_chr, UCHAR_JOINING_GROUP) == U_JG_GAF;
  208. }
  209. _FORCE_INLINE_ bool is_heh(char32_t p_chr) {
  210. return u_getIntPropertyValue(p_chr, UCHAR_JOINING_GROUP) == U_JG_HEH;
  211. }
  212. _FORCE_INLINE_ bool is_kaf(char32_t p_chr) {
  213. return u_getIntPropertyValue(p_chr, UCHAR_JOINING_GROUP) == U_JG_KAF;
  214. }
  215. _FORCE_INLINE_ bool is_lam(char32_t p_chr) {
  216. return u_getIntPropertyValue(p_chr, UCHAR_JOINING_GROUP) == U_JG_LAM;
  217. }
  218. _FORCE_INLINE_ bool is_qaf(char32_t p_chr) {
  219. return (u_getIntPropertyValue(p_chr, UCHAR_JOINING_GROUP) == U_JG_QAF) || (u_getIntPropertyValue(p_chr, UCHAR_JOINING_GROUP) == U_JG_AFRICAN_QAF);
  220. }
  221. _FORCE_INLINE_ bool is_reh(char32_t p_chr) {
  222. return u_getIntPropertyValue(p_chr, UCHAR_JOINING_GROUP) == U_JG_REH;
  223. }
  224. _FORCE_INLINE_ bool is_seen_sad(char32_t p_chr) {
  225. return (u_getIntPropertyValue(p_chr, UCHAR_JOINING_GROUP) == U_JG_SAD) || (u_getIntPropertyValue(p_chr, UCHAR_JOINING_GROUP) == U_JG_SEEN);
  226. }
  227. _FORCE_INLINE_ bool is_tah(char32_t p_chr) {
  228. return u_getIntPropertyValue(p_chr, UCHAR_JOINING_GROUP) == U_JG_TAH;
  229. }
  230. _FORCE_INLINE_ bool is_teh_marbuta(char32_t p_chr) {
  231. return u_getIntPropertyValue(p_chr, UCHAR_JOINING_GROUP) == U_JG_TEH_MARBUTA;
  232. }
  233. _FORCE_INLINE_ bool is_yeh(char32_t p_chr) {
  234. int32_t prop = u_getIntPropertyValue(p_chr, UCHAR_JOINING_GROUP);
  235. 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);
  236. }
  237. _FORCE_INLINE_ bool is_waw(char32_t p_chr) {
  238. return u_getIntPropertyValue(p_chr, UCHAR_JOINING_GROUP) == U_JG_WAW;
  239. }
  240. _FORCE_INLINE_ bool is_transparent(char32_t p_chr) {
  241. return u_getIntPropertyValue(p_chr, UCHAR_JOINING_TYPE) == U_JT_TRANSPARENT;
  242. }
  243. _FORCE_INLINE_ bool is_ligature(char32_t p_chr, char32_t p_nchr) {
  244. return (is_lam(p_chr) && is_alef(p_nchr));
  245. }
  246. _FORCE_INLINE_ bool is_connected_to_prev(char32_t p_chr, char32_t p_pchr) {
  247. int32_t prop = u_getIntPropertyValue(p_pchr, UCHAR_JOINING_TYPE);
  248. return (prop != U_JT_RIGHT_JOINING) && (prop != U_JT_NON_JOINING) ? !is_ligature(p_pchr, p_chr) : false;
  249. }
  250. /*************************************************************************/
  251. bool TextServerAdvanced::has_feature(Feature p_feature) const {
  252. switch (p_feature) {
  253. case FEATURE_SIMPLE_LAYOUT:
  254. case FEATURE_BIDI_LAYOUT:
  255. case FEATURE_VERTICAL_LAYOUT:
  256. case FEATURE_SHAPING:
  257. case FEATURE_KASHIDA_JUSTIFICATION:
  258. case FEATURE_BREAK_ITERATORS:
  259. case FEATURE_FONT_BITMAP:
  260. #ifdef MODULE_FREETYPE_ENABLED
  261. case FEATURE_FONT_DYNAMIC:
  262. #endif
  263. #ifdef MODULE_MSDFGEN_ENABLED
  264. case FEATURE_FONT_MSDF:
  265. #endif
  266. case FEATURE_FONT_VARIABLE:
  267. case FEATURE_CONTEXT_SENSITIVE_CASE_CONVERSION:
  268. case FEATURE_USE_SUPPORT_DATA:
  269. return true;
  270. default: {
  271. }
  272. }
  273. return false;
  274. }
  275. String TextServerAdvanced::get_name() const {
  276. #ifdef GDEXTENSION
  277. return "ICU / HarfBuzz / Graphite (GDExtension)";
  278. #else
  279. return "ICU / HarfBuzz / Graphite (Built-in)";
  280. #endif
  281. }
  282. int64_t TextServerAdvanced::get_features() const {
  283. 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;
  284. #ifdef MODULE_FREETYPE_ENABLED
  285. interface_features |= FEATURE_FONT_DYNAMIC;
  286. #endif
  287. #ifdef MODULE_MSDFGEN_ENABLED
  288. interface_features |= FEATURE_FONT_MSDF;
  289. #endif
  290. return interface_features;
  291. }
  292. void TextServerAdvanced::free_rid(const RID &p_rid) {
  293. _THREAD_SAFE_METHOD_
  294. if (font_owner.owns(p_rid)) {
  295. FontDataAdvanced *fd = font_owner.get_or_null(p_rid);
  296. font_owner.free(p_rid);
  297. memdelete(fd);
  298. } else if (shaped_owner.owns(p_rid)) {
  299. ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_rid);
  300. shaped_owner.free(p_rid);
  301. memdelete(sd);
  302. }
  303. }
  304. bool TextServerAdvanced::has(const RID &p_rid) {
  305. _THREAD_SAFE_METHOD_
  306. return font_owner.owns(p_rid) || shaped_owner.owns(p_rid);
  307. }
  308. bool TextServerAdvanced::load_support_data(const String &p_filename) {
  309. _THREAD_SAFE_METHOD_
  310. #ifdef ICU_STATIC_DATA
  311. if (!icu_data_loaded) {
  312. UErrorCode err = U_ZERO_ERROR;
  313. u_init(&err); // Do not check for errors, since we only load part of the data.
  314. icu_data_loaded = true;
  315. }
  316. #else
  317. if (!icu_data_loaded) {
  318. String filename = (p_filename.is_empty()) ? String("res://") + _MKSTR(ICU_DATA_NAME) : p_filename;
  319. Ref<File> f;
  320. f.instantiate();
  321. if (f->open(filename, File::READ) != OK) {
  322. return false;
  323. }
  324. uint64_t len = f->get_length();
  325. PackedByteArray icu_data = f->get_buffer(len);
  326. UErrorCode err = U_ZERO_ERROR;
  327. udata_setCommonData(icu_data.ptr(), &err);
  328. if (U_FAILURE(err)) {
  329. ERR_FAIL_V_MSG(false, u_errorName(err));
  330. }
  331. err = U_ZERO_ERROR;
  332. u_init(&err);
  333. if (U_FAILURE(err)) {
  334. ERR_FAIL_V_MSG(false, u_errorName(err));
  335. }
  336. icu_data_loaded = true;
  337. }
  338. #endif
  339. return true;
  340. }
  341. String TextServerAdvanced::get_support_data_filename() const {
  342. #ifdef ICU_STATIC_DATA
  343. return _MKSTR(ICU_DATA_NAME);
  344. #else
  345. return String();
  346. #endif
  347. }
  348. String TextServerAdvanced::get_support_data_info() const {
  349. #ifdef ICU_STATIC_DATA
  350. return String("ICU break iteration data (") + _MKSTR(ICU_DATA_NAME) + String(").");
  351. #else
  352. return String();
  353. #endif
  354. }
  355. bool TextServerAdvanced::save_support_data(const String &p_filename) const {
  356. _THREAD_SAFE_METHOD_
  357. #ifdef ICU_STATIC_DATA
  358. // Store data to the res file if it's available.
  359. Ref<File> f;
  360. f.instantiate();
  361. if (f->open(p_filename, File::WRITE) != OK) {
  362. return false;
  363. }
  364. PackedByteArray icu_data;
  365. icu_data.resize(U_ICUDATA_SIZE);
  366. memcpy(icu_data.ptrw(), U_ICUDATA_ENTRY_POINT, U_ICUDATA_SIZE);
  367. f->store_buffer(icu_data);
  368. return true;
  369. #else
  370. return false;
  371. #endif
  372. }
  373. bool TextServerAdvanced::is_locale_right_to_left(const String &p_locale) const {
  374. String l = p_locale.get_slicec('_', 0);
  375. if ((l == "ar") || (l == "dv") || (l == "he") || (l == "fa") || (l == "ff") || (l == "ku") || (l == "ur")) {
  376. return true;
  377. } else {
  378. return false;
  379. }
  380. }
  381. _FORCE_INLINE_ void TextServerAdvanced::_insert_feature(const StringName &p_name, int32_t p_tag) {
  382. feature_sets.insert(p_name, p_tag);
  383. feature_sets_inv.insert(p_tag, p_name);
  384. }
  385. void TextServerAdvanced::_insert_feature_sets() {
  386. // Registered OpenType feature tags.
  387. _insert_feature("access_all_alternates", HB_TAG('a', 'a', 'l', 't'));
  388. _insert_feature("above_base_forms", HB_TAG('a', 'b', 'v', 'f'));
  389. _insert_feature("above_base_mark_positioning", HB_TAG('a', 'b', 'v', 'm'));
  390. _insert_feature("above_base_substitutions", HB_TAG('a', 'b', 'v', 's'));
  391. _insert_feature("alternative_fractions", HB_TAG('a', 'f', 'r', 'c'));
  392. _insert_feature("akhands", HB_TAG('a', 'k', 'h', 'n'));
  393. _insert_feature("below_base_forms", HB_TAG('b', 'l', 'w', 'f'));
  394. _insert_feature("below_base_mark_positioning", HB_TAG('b', 'l', 'w', 'm'));
  395. _insert_feature("below_base_substitutions", HB_TAG('b', 'l', 'w', 's'));
  396. _insert_feature("contextual_alternates", HB_TAG('c', 'a', 'l', 't'));
  397. _insert_feature("case_sensitive_forms", HB_TAG('c', 'a', 's', 'e'));
  398. _insert_feature("glyph_composition", HB_TAG('c', 'c', 'm', 'p'));
  399. _insert_feature("conjunct_form_after_ro", HB_TAG('c', 'f', 'a', 'r'));
  400. _insert_feature("conjunct_forms", HB_TAG('c', 'j', 'c', 't'));
  401. _insert_feature("contextual_ligatures", HB_TAG('c', 'l', 'i', 'g'));
  402. _insert_feature("centered_cjk_punctuation", HB_TAG('c', 'p', 'c', 't'));
  403. _insert_feature("capital_spacing", HB_TAG('c', 'p', 's', 'p'));
  404. _insert_feature("contextual_swash", HB_TAG('c', 's', 'w', 'h'));
  405. _insert_feature("cursive_positioning", HB_TAG('c', 'u', 'r', 's'));
  406. _insert_feature("character_variant_01", HB_TAG('c', 'v', '0', '1'));
  407. _insert_feature("character_variant_02", HB_TAG('c', 'v', '0', '2'));
  408. _insert_feature("character_variant_03", HB_TAG('c', 'v', '0', '3'));
  409. _insert_feature("character_variant_04", HB_TAG('c', 'v', '0', '4'));
  410. _insert_feature("character_variant_05", HB_TAG('c', 'v', '0', '5'));
  411. _insert_feature("character_variant_06", HB_TAG('c', 'v', '0', '6'));
  412. _insert_feature("character_variant_07", HB_TAG('c', 'v', '0', '7'));
  413. _insert_feature("character_variant_08", HB_TAG('c', 'v', '0', '8'));
  414. _insert_feature("character_variant_09", HB_TAG('c', 'v', '0', '9'));
  415. _insert_feature("character_variant_10", HB_TAG('c', 'v', '1', '0'));
  416. _insert_feature("character_variant_11", HB_TAG('c', 'v', '1', '1'));
  417. _insert_feature("character_variant_12", HB_TAG('c', 'v', '1', '2'));
  418. _insert_feature("character_variant_13", HB_TAG('c', 'v', '1', '3'));
  419. _insert_feature("character_variant_14", HB_TAG('c', 'v', '1', '4'));
  420. _insert_feature("character_variant_15", HB_TAG('c', 'v', '1', '5'));
  421. _insert_feature("character_variant_16", HB_TAG('c', 'v', '1', '6'));
  422. _insert_feature("character_variant_17", HB_TAG('c', 'v', '1', '7'));
  423. _insert_feature("character_variant_18", HB_TAG('c', 'v', '1', '8'));
  424. _insert_feature("character_variant_19", HB_TAG('c', 'v', '1', '9'));
  425. _insert_feature("character_variant_20", HB_TAG('c', 'v', '2', '0'));
  426. _insert_feature("character_variant_21", HB_TAG('c', 'v', '2', '1'));
  427. _insert_feature("character_variant_22", HB_TAG('c', 'v', '2', '2'));
  428. _insert_feature("character_variant_23", HB_TAG('c', 'v', '2', '3'));
  429. _insert_feature("character_variant_24", HB_TAG('c', 'v', '2', '4'));
  430. _insert_feature("character_variant_25", HB_TAG('c', 'v', '2', '5'));
  431. _insert_feature("character_variant_26", HB_TAG('c', 'v', '2', '6'));
  432. _insert_feature("character_variant_27", HB_TAG('c', 'v', '2', '7'));
  433. _insert_feature("character_variant_28", HB_TAG('c', 'v', '2', '8'));
  434. _insert_feature("character_variant_29", HB_TAG('c', 'v', '2', '9'));
  435. _insert_feature("character_variant_30", HB_TAG('c', 'v', '3', '0'));
  436. _insert_feature("character_variant_31", HB_TAG('c', 'v', '3', '1'));
  437. _insert_feature("character_variant_32", HB_TAG('c', 'v', '3', '2'));
  438. _insert_feature("character_variant_33", HB_TAG('c', 'v', '3', '3'));
  439. _insert_feature("character_variant_34", HB_TAG('c', 'v', '3', '4'));
  440. _insert_feature("character_variant_35", HB_TAG('c', 'v', '3', '5'));
  441. _insert_feature("character_variant_36", HB_TAG('c', 'v', '3', '6'));
  442. _insert_feature("character_variant_37", HB_TAG('c', 'v', '3', '7'));
  443. _insert_feature("character_variant_38", HB_TAG('c', 'v', '3', '8'));
  444. _insert_feature("character_variant_39", HB_TAG('c', 'v', '3', '9'));
  445. _insert_feature("character_variant_40", HB_TAG('c', 'v', '4', '0'));
  446. _insert_feature("character_variant_41", HB_TAG('c', 'v', '4', '1'));
  447. _insert_feature("character_variant_42", HB_TAG('c', 'v', '4', '2'));
  448. _insert_feature("character_variant_43", HB_TAG('c', 'v', '4', '3'));
  449. _insert_feature("character_variant_44", HB_TAG('c', 'v', '4', '4'));
  450. _insert_feature("character_variant_45", HB_TAG('c', 'v', '4', '5'));
  451. _insert_feature("character_variant_46", HB_TAG('c', 'v', '4', '6'));
  452. _insert_feature("character_variant_47", HB_TAG('c', 'v', '4', '7'));
  453. _insert_feature("character_variant_48", HB_TAG('c', 'v', '4', '8'));
  454. _insert_feature("character_variant_49", HB_TAG('c', 'v', '4', '9'));
  455. _insert_feature("character_variant_50", HB_TAG('c', 'v', '5', '0'));
  456. _insert_feature("character_variant_51", HB_TAG('c', 'v', '5', '1'));
  457. _insert_feature("character_variant_52", HB_TAG('c', 'v', '5', '2'));
  458. _insert_feature("character_variant_53", HB_TAG('c', 'v', '5', '3'));
  459. _insert_feature("character_variant_54", HB_TAG('c', 'v', '5', '4'));
  460. _insert_feature("character_variant_55", HB_TAG('c', 'v', '5', '5'));
  461. _insert_feature("character_variant_56", HB_TAG('c', 'v', '5', '6'));
  462. _insert_feature("character_variant_57", HB_TAG('c', 'v', '5', '7'));
  463. _insert_feature("character_variant_58", HB_TAG('c', 'v', '5', '8'));
  464. _insert_feature("character_variant_59", HB_TAG('c', 'v', '5', '9'));
  465. _insert_feature("character_variant_60", HB_TAG('c', 'v', '6', '0'));
  466. _insert_feature("character_variant_61", HB_TAG('c', 'v', '6', '1'));
  467. _insert_feature("character_variant_62", HB_TAG('c', 'v', '6', '2'));
  468. _insert_feature("character_variant_63", HB_TAG('c', 'v', '6', '3'));
  469. _insert_feature("character_variant_64", HB_TAG('c', 'v', '6', '4'));
  470. _insert_feature("character_variant_65", HB_TAG('c', 'v', '6', '5'));
  471. _insert_feature("character_variant_66", HB_TAG('c', 'v', '6', '6'));
  472. _insert_feature("character_variant_67", HB_TAG('c', 'v', '6', '7'));
  473. _insert_feature("character_variant_68", HB_TAG('c', 'v', '6', '8'));
  474. _insert_feature("character_variant_69", HB_TAG('c', 'v', '6', '9'));
  475. _insert_feature("character_variant_70", HB_TAG('c', 'v', '7', '0'));
  476. _insert_feature("character_variant_71", HB_TAG('c', 'v', '7', '1'));
  477. _insert_feature("character_variant_72", HB_TAG('c', 'v', '7', '2'));
  478. _insert_feature("character_variant_73", HB_TAG('c', 'v', '7', '3'));
  479. _insert_feature("character_variant_74", HB_TAG('c', 'v', '7', '4'));
  480. _insert_feature("character_variant_75", HB_TAG('c', 'v', '7', '5'));
  481. _insert_feature("character_variant_76", HB_TAG('c', 'v', '7', '6'));
  482. _insert_feature("character_variant_77", HB_TAG('c', 'v', '7', '7'));
  483. _insert_feature("character_variant_78", HB_TAG('c', 'v', '7', '8'));
  484. _insert_feature("character_variant_79", HB_TAG('c', 'v', '7', '9'));
  485. _insert_feature("character_variant_80", HB_TAG('c', 'v', '8', '0'));
  486. _insert_feature("character_variant_81", HB_TAG('c', 'v', '8', '1'));
  487. _insert_feature("character_variant_82", HB_TAG('c', 'v', '8', '2'));
  488. _insert_feature("character_variant_83", HB_TAG('c', 'v', '8', '3'));
  489. _insert_feature("character_variant_84", HB_TAG('c', 'v', '8', '4'));
  490. _insert_feature("character_variant_85", HB_TAG('c', 'v', '8', '5'));
  491. _insert_feature("character_variant_86", HB_TAG('c', 'v', '8', '6'));
  492. _insert_feature("character_variant_87", HB_TAG('c', 'v', '8', '7'));
  493. _insert_feature("character_variant_88", HB_TAG('c', 'v', '8', '8'));
  494. _insert_feature("character_variant_89", HB_TAG('c', 'v', '8', '9'));
  495. _insert_feature("character_variant_90", HB_TAG('c', 'v', '9', '0'));
  496. _insert_feature("character_variant_91", HB_TAG('c', 'v', '9', '1'));
  497. _insert_feature("character_variant_92", HB_TAG('c', 'v', '9', '2'));
  498. _insert_feature("character_variant_93", HB_TAG('c', 'v', '9', '3'));
  499. _insert_feature("character_variant_94", HB_TAG('c', 'v', '9', '4'));
  500. _insert_feature("character_variant_95", HB_TAG('c', 'v', '9', '5'));
  501. _insert_feature("character_variant_96", HB_TAG('c', 'v', '9', '6'));
  502. _insert_feature("character_variant_97", HB_TAG('c', 'v', '9', '7'));
  503. _insert_feature("character_variant_98", HB_TAG('c', 'v', '9', '8'));
  504. _insert_feature("character_variant_99", HB_TAG('c', 'v', '9', '9'));
  505. _insert_feature("petite_capitals_from_capitals", HB_TAG('c', '2', 'p', 'c'));
  506. _insert_feature("small_capitals_from_capitals", HB_TAG('c', '2', 's', 'c'));
  507. _insert_feature("distances", HB_TAG('d', 'i', 's', 't'));
  508. _insert_feature("discretionary_ligatures", HB_TAG('d', 'l', 'i', 'g'));
  509. _insert_feature("denominators", HB_TAG('d', 'n', 'o', 'm'));
  510. _insert_feature("dotless_forms", HB_TAG('d', 't', 'l', 's'));
  511. _insert_feature("expert_forms", HB_TAG('e', 'x', 'p', 't'));
  512. _insert_feature("final_glyph_on_line_alternates", HB_TAG('f', 'a', 'l', 't'));
  513. _insert_feature("terminal_forms_2", HB_TAG('f', 'i', 'n', '2'));
  514. _insert_feature("terminal_forms_3", HB_TAG('f', 'i', 'n', '3'));
  515. _insert_feature("terminal_forms", HB_TAG('f', 'i', 'n', 'a'));
  516. _insert_feature("flattened_accent_forms", HB_TAG('f', 'l', 'a', 'c'));
  517. _insert_feature("fractions", HB_TAG('f', 'r', 'a', 'c'));
  518. _insert_feature("full_widths", HB_TAG('f', 'w', 'i', 'd'));
  519. _insert_feature("half_forms", HB_TAG('h', 'a', 'l', 'f'));
  520. _insert_feature("halant_forms", HB_TAG('h', 'a', 'l', 'n'));
  521. _insert_feature("alternate_half_widths", HB_TAG('h', 'a', 'l', 't'));
  522. _insert_feature("historical_forms", HB_TAG('h', 'i', 's', 't'));
  523. _insert_feature("horizontal_kana_alternates", HB_TAG('h', 'k', 'n', 'a'));
  524. _insert_feature("historical_ligatures", HB_TAG('h', 'l', 'i', 'g'));
  525. _insert_feature("hangul", HB_TAG('h', 'n', 'g', 'l'));
  526. _insert_feature("hojo_kanji_forms", HB_TAG('h', 'o', 'j', 'o'));
  527. _insert_feature("half_widths", HB_TAG('h', 'w', 'i', 'd'));
  528. _insert_feature("initial_forms", HB_TAG('i', 'n', 'i', 't'));
  529. _insert_feature("isolated_forms", HB_TAG('i', 's', 'o', 'l'));
  530. _insert_feature("italics", HB_TAG('i', 't', 'a', 'l'));
  531. _insert_feature("justification_alternates", HB_TAG('j', 'a', 'l', 't'));
  532. _insert_feature("jis78_forms", HB_TAG('j', 'p', '7', '8'));
  533. _insert_feature("jis83_forms", HB_TAG('j', 'p', '8', '3'));
  534. _insert_feature("jis90_forms", HB_TAG('j', 'p', '9', '0'));
  535. _insert_feature("jis2004_forms", HB_TAG('j', 'p', '0', '4'));
  536. _insert_feature("kerning", HB_TAG('k', 'e', 'r', 'n'));
  537. _insert_feature("left_bounds", HB_TAG('l', 'f', 'b', 'd'));
  538. _insert_feature("standard_ligatures", HB_TAG('l', 'i', 'g', 'a'));
  539. _insert_feature("leading_jamo_forms", HB_TAG('l', 'j', 'm', 'o'));
  540. _insert_feature("lining_figures", HB_TAG('l', 'n', 'u', 'm'));
  541. _insert_feature("localized_forms", HB_TAG('l', 'o', 'c', 'l'));
  542. _insert_feature("left_to_right_alternates", HB_TAG('l', 't', 'r', 'a'));
  543. _insert_feature("left_to_right_mirrored_forms", HB_TAG('l', 't', 'r', 'm'));
  544. _insert_feature("mark_positioning", HB_TAG('m', 'a', 'r', 'k'));
  545. _insert_feature("medial_forms_2", HB_TAG('m', 'e', 'd', '2'));
  546. _insert_feature("medial_forms", HB_TAG('m', 'e', 'd', 'i'));
  547. _insert_feature("mathematical_greek", HB_TAG('m', 'g', 'r', 'k'));
  548. _insert_feature("mark_to_mark_positioning", HB_TAG('m', 'k', 'm', 'k'));
  549. _insert_feature("mark_positioning_via_substitution", HB_TAG('m', 's', 'e', 't'));
  550. _insert_feature("alternate_annotation_forms", HB_TAG('n', 'a', 'l', 't'));
  551. _insert_feature("nlc_kanji_forms", HB_TAG('n', 'l', 'c', 'k'));
  552. _insert_feature("nukta_forms", HB_TAG('n', 'u', 'k', 't'));
  553. _insert_feature("numerators", HB_TAG('n', 'u', 'm', 'r'));
  554. _insert_feature("oldstyle_figures", HB_TAG('o', 'n', 'u', 'm'));
  555. _insert_feature("optical_bounds", HB_TAG('o', 'p', 'b', 'd'));
  556. _insert_feature("ordinals", HB_TAG('o', 'r', 'd', 'n'));
  557. _insert_feature("ornaments", HB_TAG('o', 'r', 'n', 'm'));
  558. _insert_feature("proportional_alternate_widths", HB_TAG('p', 'a', 'l', 't'));
  559. _insert_feature("petite_capitals", HB_TAG('p', 'c', 'a', 'p'));
  560. _insert_feature("proportional_kana", HB_TAG('p', 'k', 'n', 'a'));
  561. _insert_feature("proportional_figures", HB_TAG('p', 'n', 'u', 'm'));
  562. _insert_feature("pre_base_forms", HB_TAG('p', 'r', 'e', 'f'));
  563. _insert_feature("pre_base_substitutions", HB_TAG('p', 'r', 'e', 's'));
  564. _insert_feature("post_base_forms", HB_TAG('p', 's', 't', 'f'));
  565. _insert_feature("post_base_substitutions", HB_TAG('p', 's', 't', 's'));
  566. _insert_feature("proportional_widths", HB_TAG('p', 'w', 'i', 'd'));
  567. _insert_feature("quarter_widths", HB_TAG('q', 'w', 'i', 'd'));
  568. _insert_feature("randomize", HB_TAG('r', 'a', 'n', 'd'));
  569. _insert_feature("required_contextual_alternates", HB_TAG('r', 'c', 'l', 't'));
  570. _insert_feature("rakar_forms", HB_TAG('r', 'k', 'r', 'f'));
  571. _insert_feature("required_ligatures", HB_TAG('r', 'l', 'i', 'g'));
  572. _insert_feature("reph_forms", HB_TAG('r', 'p', 'h', 'f'));
  573. _insert_feature("right_bounds", HB_TAG('r', 't', 'b', 'd'));
  574. _insert_feature("right_to_left_alternates", HB_TAG('r', 't', 'l', 'a'));
  575. _insert_feature("right_to_left_mirrored_forms", HB_TAG('r', 't', 'l', 'm'));
  576. _insert_feature("ruby_notation_forms", HB_TAG('r', 'u', 'b', 'y'));
  577. _insert_feature("required_variation_alternates", HB_TAG('r', 'v', 'r', 'n'));
  578. _insert_feature("stylistic_alternates", HB_TAG('s', 'a', 'l', 't'));
  579. _insert_feature("scientific_inferiors", HB_TAG('s', 'i', 'n', 'f'));
  580. _insert_feature("optical_size", HB_TAG('s', 'i', 'z', 'e'));
  581. _insert_feature("small_capitals", HB_TAG('s', 'm', 'c', 'p'));
  582. _insert_feature("simplified_forms", HB_TAG('s', 'm', 'p', 'l'));
  583. _insert_feature("stylistic_set_01", HB_TAG('s', 's', '0', '1'));
  584. _insert_feature("stylistic_set_02", HB_TAG('s', 's', '0', '2'));
  585. _insert_feature("stylistic_set_03", HB_TAG('s', 's', '0', '3'));
  586. _insert_feature("stylistic_set_04", HB_TAG('s', 's', '0', '4'));
  587. _insert_feature("stylistic_set_05", HB_TAG('s', 's', '0', '5'));
  588. _insert_feature("stylistic_set_06", HB_TAG('s', 's', '0', '6'));
  589. _insert_feature("stylistic_set_07", HB_TAG('s', 's', '0', '7'));
  590. _insert_feature("stylistic_set_08", HB_TAG('s', 's', '0', '8'));
  591. _insert_feature("stylistic_set_09", HB_TAG('s', 's', '0', '9'));
  592. _insert_feature("stylistic_set_10", HB_TAG('s', 's', '1', '0'));
  593. _insert_feature("stylistic_set_11", HB_TAG('s', 's', '1', '1'));
  594. _insert_feature("stylistic_set_12", HB_TAG('s', 's', '1', '2'));
  595. _insert_feature("stylistic_set_13", HB_TAG('s', 's', '1', '3'));
  596. _insert_feature("stylistic_set_14", HB_TAG('s', 's', '1', '4'));
  597. _insert_feature("stylistic_set_15", HB_TAG('s', 's', '1', '5'));
  598. _insert_feature("stylistic_set_16", HB_TAG('s', 's', '1', '6'));
  599. _insert_feature("stylistic_set_17", HB_TAG('s', 's', '1', '7'));
  600. _insert_feature("stylistic_set_18", HB_TAG('s', 's', '1', '8'));
  601. _insert_feature("stylistic_set_19", HB_TAG('s', 's', '1', '9'));
  602. _insert_feature("stylistic_set_20", HB_TAG('s', 's', '2', '0'));
  603. _insert_feature("math_script_style_alternates", HB_TAG('s', 's', 't', 'y'));
  604. _insert_feature("stretching_glyph_decomposition", HB_TAG('s', 't', 'c', 'h'));
  605. _insert_feature("subscript", HB_TAG('s', 'u', 'b', 's'));
  606. _insert_feature("superscript", HB_TAG('s', 'u', 'p', 's'));
  607. _insert_feature("swash", HB_TAG('s', 'w', 's', 'h'));
  608. _insert_feature("titling", HB_TAG('t', 'i', 't', 'l'));
  609. _insert_feature("trailing_jamo_forms", HB_TAG('t', 'j', 'm', 'o'));
  610. _insert_feature("traditional_name_forms", HB_TAG('t', 'n', 'a', 'm'));
  611. _insert_feature("tabular_figures", HB_TAG('t', 'n', 'u', 'm'));
  612. _insert_feature("traditional_forms", HB_TAG('t', 'r', 'a', 'd'));
  613. _insert_feature("third_widths", HB_TAG('t', 'w', 'i', 'd'));
  614. _insert_feature("unicase", HB_TAG('u', 'n', 'i', 'c'));
  615. _insert_feature("alternate_vertical_metrics", HB_TAG('v', 'a', 'l', 't'));
  616. _insert_feature("vattu_variants", HB_TAG('v', 'a', 't', 'u'));
  617. _insert_feature("vertical_writing", HB_TAG('v', 'e', 'r', 't'));
  618. _insert_feature("alternate_vertical_half_metrics", HB_TAG('v', 'h', 'a', 'l'));
  619. _insert_feature("vowel_jamo_forms", HB_TAG('v', 'j', 'm', 'o'));
  620. _insert_feature("vertical_kana_alternates", HB_TAG('v', 'k', 'n', 'a'));
  621. _insert_feature("vertical_kerning", HB_TAG('v', 'k', 'r', 'n'));
  622. _insert_feature("proportional_alternate_vertical_metrics", HB_TAG('v', 'p', 'a', 'l'));
  623. _insert_feature("vertical_alternates_and_rotation", HB_TAG('v', 'r', 't', '2'));
  624. _insert_feature("vertical_alternates_for_rotation", HB_TAG('v', 'r', 't', 'r'));
  625. _insert_feature("slashed_zero", HB_TAG('z', 'e', 'r', 'o'));
  626. // Registered OpenType variation tag.
  627. _insert_feature("italic", HB_TAG('i', 't', 'a', 'l'));
  628. _insert_feature("optical_size", HB_TAG('o', 'p', 's', 'z'));
  629. _insert_feature("slant", HB_TAG('s', 'l', 'n', 't'));
  630. _insert_feature("width", HB_TAG('w', 'd', 't', 'h'));
  631. _insert_feature("weight", HB_TAG('w', 'g', 'h', 't'));
  632. }
  633. int64_t TextServerAdvanced::name_to_tag(const String &p_name) const {
  634. if (feature_sets.has(p_name)) {
  635. return feature_sets[p_name];
  636. }
  637. // No readable name, use tag string.
  638. return hb_tag_from_string(p_name.replace("custom_", "").ascii().get_data(), -1);
  639. }
  640. String TextServerAdvanced::tag_to_name(int64_t p_tag) const {
  641. if (feature_sets_inv.has(p_tag)) {
  642. return feature_sets_inv[p_tag];
  643. }
  644. // No readable name, use tag string.
  645. char name[5];
  646. memset(name, 0, 5);
  647. hb_tag_to_string(p_tag, name);
  648. return String("custom_") + String(name);
  649. }
  650. /*************************************************************************/
  651. /* Font Glyph Rendering */
  652. /*************************************************************************/
  653. _FORCE_INLINE_ TextServerAdvanced::FontTexturePosition TextServerAdvanced::find_texture_pos_for_glyph(FontDataForSizeAdvanced *p_data, int p_color_size, Image::Format p_image_format, int p_width, int p_height, bool p_msdf) const {
  654. FontTexturePosition ret;
  655. ret.index = -1;
  656. int mw = p_width;
  657. int mh = p_height;
  658. for (int i = 0; i < p_data->textures.size(); i++) {
  659. const FontTexture &ct = p_data->textures[i];
  660. if (mw > ct.texture_w || mh > ct.texture_h) { // Too big for this texture.
  661. continue;
  662. }
  663. if (ct.offsets.size() < ct.texture_w) {
  664. continue;
  665. }
  666. ret.y = 0x7fffffff;
  667. ret.x = 0;
  668. for (int j = 0; j < ct.texture_w - mw; j++) {
  669. int max_y = 0;
  670. for (int k = j; k < j + mw; k++) {
  671. int y = ct.offsets[k];
  672. if (y > max_y) {
  673. max_y = y;
  674. }
  675. }
  676. if (max_y < ret.y) {
  677. ret.y = max_y;
  678. ret.x = j;
  679. }
  680. }
  681. if (ret.y == 0x7fffffff || ret.y + mh > ct.texture_h) {
  682. continue; // Fail, could not fit it here.
  683. }
  684. ret.index = i;
  685. break;
  686. }
  687. if (ret.index == -1) {
  688. // Could not find texture to fit, create one.
  689. ret.x = 0;
  690. ret.y = 0;
  691. int texsize = MAX(p_data->size.x * p_data->oversampling * 8, 256);
  692. #ifdef GDEXTENSION
  693. texsize = Math::next_power_of_2(texsize);
  694. #else
  695. texsize = next_power_of_2(texsize);
  696. #endif
  697. if (p_msdf) {
  698. texsize = MIN(texsize, 2048);
  699. } else {
  700. texsize = MIN(texsize, 1024);
  701. }
  702. if (mw > texsize) { // Special case, adapt to it?
  703. #ifdef GDEXTENSION
  704. texsize = Math::next_power_of_2(mw);
  705. #else
  706. texsize = next_power_of_2(mw);
  707. #endif
  708. }
  709. if (mh > texsize) { // Special case, adapt to it?
  710. #ifdef GDEXTENSION
  711. texsize = Math::next_power_of_2(mh);
  712. #else
  713. texsize = next_power_of_2(mh);
  714. #endif
  715. }
  716. FontTexture tex;
  717. tex.texture_w = texsize;
  718. tex.texture_h = texsize;
  719. tex.format = p_image_format;
  720. tex.imgdata.resize(texsize * texsize * p_color_size);
  721. {
  722. // Zero texture.
  723. uint8_t *w = tex.imgdata.ptrw();
  724. ERR_FAIL_COND_V(texsize * texsize * p_color_size > tex.imgdata.size(), ret);
  725. // Initialize the texture to all-white pixels to prevent artifacts when the
  726. // font is displayed at a non-default scale with filtering enabled.
  727. if (p_color_size == 2) {
  728. for (int i = 0; i < texsize * texsize * p_color_size; i += 2) { // FORMAT_LA8, BW font.
  729. w[i + 0] = 255;
  730. w[i + 1] = 0;
  731. }
  732. } else if (p_color_size == 4) {
  733. for (int i = 0; i < texsize * texsize * p_color_size; i += 4) { // FORMAT_RGBA8, Color font, Multichannel(+True) SDF.
  734. w[i + 0] = 255;
  735. w[i + 1] = 255;
  736. w[i + 2] = 255;
  737. w[i + 3] = 0;
  738. }
  739. } else {
  740. ERR_FAIL_V(ret);
  741. }
  742. }
  743. tex.offsets.resize(texsize);
  744. int32_t *offw = tex.offsets.ptrw();
  745. for (int i = 0; i < texsize; i++) { // Zero offsets.
  746. offw[i] = 0;
  747. }
  748. p_data->textures.push_back(tex);
  749. ret.index = p_data->textures.size() - 1;
  750. }
  751. return ret;
  752. }
  753. #ifdef MODULE_MSDFGEN_ENABLED
  754. struct MSContext {
  755. msdfgen::Point2 position;
  756. msdfgen::Shape *shape = nullptr;
  757. msdfgen::Contour *contour = nullptr;
  758. };
  759. class DistancePixelConversion {
  760. double invRange;
  761. public:
  762. _FORCE_INLINE_ explicit DistancePixelConversion(double range) :
  763. invRange(1 / range) {}
  764. _FORCE_INLINE_ void operator()(float *pixels, const msdfgen::MultiAndTrueDistance &distance) const {
  765. pixels[0] = float(invRange * distance.r + .5);
  766. pixels[1] = float(invRange * distance.g + .5);
  767. pixels[2] = float(invRange * distance.b + .5);
  768. pixels[3] = float(invRange * distance.a + .5);
  769. }
  770. };
  771. struct MSDFThreadData {
  772. msdfgen::Bitmap<float, 4> *output;
  773. msdfgen::Shape *shape;
  774. msdfgen::Projection *projection;
  775. DistancePixelConversion *distancePixelConversion;
  776. };
  777. static msdfgen::Point2 ft_point2(const FT_Vector &vector) {
  778. return msdfgen::Point2(vector.x / 60.0f, vector.y / 60.0f);
  779. }
  780. static int ft_move_to(const FT_Vector *to, void *user) {
  781. MSContext *context = static_cast<MSContext *>(user);
  782. if (!(context->contour && context->contour->edges.empty())) {
  783. context->contour = &context->shape->addContour();
  784. }
  785. context->position = ft_point2(*to);
  786. return 0;
  787. }
  788. static int ft_line_to(const FT_Vector *to, void *user) {
  789. MSContext *context = static_cast<MSContext *>(user);
  790. msdfgen::Point2 endpoint = ft_point2(*to);
  791. if (endpoint != context->position) {
  792. context->contour->addEdge(new msdfgen::LinearSegment(context->position, endpoint));
  793. context->position = endpoint;
  794. }
  795. return 0;
  796. }
  797. static int ft_conic_to(const FT_Vector *control, const FT_Vector *to, void *user) {
  798. MSContext *context = static_cast<MSContext *>(user);
  799. context->contour->addEdge(new msdfgen::QuadraticSegment(context->position, ft_point2(*control), ft_point2(*to)));
  800. context->position = ft_point2(*to);
  801. return 0;
  802. }
  803. static int ft_cubic_to(const FT_Vector *control1, const FT_Vector *control2, const FT_Vector *to, void *user) {
  804. MSContext *context = static_cast<MSContext *>(user);
  805. context->contour->addEdge(new msdfgen::CubicSegment(context->position, ft_point2(*control1), ft_point2(*control2), ft_point2(*to)));
  806. context->position = ft_point2(*to);
  807. return 0;
  808. }
  809. void TextServerAdvanced::_generateMTSDF_threaded(uint32_t y, void *p_td) const {
  810. MSDFThreadData *td = static_cast<MSDFThreadData *>(p_td);
  811. msdfgen::ShapeDistanceFinder<msdfgen::OverlappingContourCombiner<msdfgen::MultiAndTrueDistanceSelector>> distanceFinder(*td->shape);
  812. int row = td->shape->inverseYAxis ? td->output->height() - y - 1 : y;
  813. for (int col = 0; col < td->output->width(); ++col) {
  814. int x = (y % 2) ? td->output->width() - col - 1 : col;
  815. msdfgen::Point2 p = td->projection->unproject(msdfgen::Point2(x + .5, y + .5));
  816. msdfgen::MultiAndTrueDistance distance = distanceFinder.distance(p);
  817. td->distancePixelConversion->operator()(td->output->operator()(x, row), distance);
  818. }
  819. }
  820. _FORCE_INLINE_ TextServerAdvanced::FontGlyph TextServerAdvanced::rasterize_msdf(FontDataAdvanced *p_font_data, FontDataForSizeAdvanced *p_data, int p_pixel_range, int p_rect_margin, FT_Outline *outline, const Vector2 &advance) const {
  821. msdfgen::Shape shape;
  822. shape.contours.clear();
  823. shape.inverseYAxis = false;
  824. MSContext context = {};
  825. context.shape = &shape;
  826. FT_Outline_Funcs ft_functions;
  827. ft_functions.move_to = &ft_move_to;
  828. ft_functions.line_to = &ft_line_to;
  829. ft_functions.conic_to = &ft_conic_to;
  830. ft_functions.cubic_to = &ft_cubic_to;
  831. ft_functions.shift = 0;
  832. ft_functions.delta = 0;
  833. int error = FT_Outline_Decompose(outline, &ft_functions, &context);
  834. ERR_FAIL_COND_V_MSG(error, FontGlyph(), "FreeType: Outline decomposition error: '" + String(FT_Error_String(error)) + "'.");
  835. if (!shape.contours.empty() && shape.contours.back().edges.empty()) {
  836. shape.contours.pop_back();
  837. }
  838. if (FT_Outline_Get_Orientation(outline) == 1) {
  839. for (int i = 0; i < (int)shape.contours.size(); ++i) {
  840. shape.contours[i].reverse();
  841. }
  842. }
  843. shape.inverseYAxis = true;
  844. shape.normalize();
  845. msdfgen::Shape::Bounds bounds = shape.getBounds(p_pixel_range);
  846. FontGlyph chr;
  847. chr.found = true;
  848. chr.advance = advance;
  849. if (shape.validate() && shape.contours.size() > 0) {
  850. int w = (bounds.r - bounds.l);
  851. int h = (bounds.t - bounds.b);
  852. int mw = w + p_rect_margin * 4;
  853. int mh = h + p_rect_margin * 4;
  854. ERR_FAIL_COND_V(mw > 4096, FontGlyph());
  855. ERR_FAIL_COND_V(mh > 4096, FontGlyph());
  856. FontTexturePosition tex_pos = find_texture_pos_for_glyph(p_data, 4, Image::FORMAT_RGBA8, mw, mh, true);
  857. ERR_FAIL_COND_V(tex_pos.index < 0, FontGlyph());
  858. FontTexture &tex = p_data->textures.write[tex_pos.index];
  859. edgeColoringSimple(shape, 3.0); // Max. angle.
  860. msdfgen::Bitmap<float, 4> image(w, h); // Texture size.
  861. DistancePixelConversion distancePixelConversion(p_pixel_range);
  862. msdfgen::Projection projection(msdfgen::Vector2(1.0, 1.0), msdfgen::Vector2(-bounds.l, -bounds.b));
  863. msdfgen::MSDFGeneratorConfig config(true, msdfgen::ErrorCorrectionConfig());
  864. MSDFThreadData td;
  865. td.output = &image;
  866. td.shape = &shape;
  867. td.projection = &projection;
  868. td.distancePixelConversion = &distancePixelConversion;
  869. if (p_font_data->work_pool.get_thread_count() == 0) {
  870. p_font_data->work_pool.init();
  871. }
  872. p_font_data->work_pool.do_work(h, this, &TextServerAdvanced::_generateMTSDF_threaded, &td);
  873. msdfgen::msdfErrorCorrection(image, shape, projection, p_pixel_range, config);
  874. {
  875. uint8_t *wr = tex.imgdata.ptrw();
  876. for (int i = 0; i < h; i++) {
  877. for (int j = 0; j < w; j++) {
  878. int ofs = ((i + tex_pos.y + p_rect_margin * 2) * tex.texture_w + j + tex_pos.x + p_rect_margin * 2) * 4;
  879. ERR_FAIL_COND_V(ofs >= tex.imgdata.size(), FontGlyph());
  880. wr[ofs + 0] = (uint8_t)(CLAMP(image(j, i)[0] * 256.f, 0.f, 255.f));
  881. wr[ofs + 1] = (uint8_t)(CLAMP(image(j, i)[1] * 256.f, 0.f, 255.f));
  882. wr[ofs + 2] = (uint8_t)(CLAMP(image(j, i)[2] * 256.f, 0.f, 255.f));
  883. wr[ofs + 3] = (uint8_t)(CLAMP(image(j, i)[3] * 256.f, 0.f, 255.f));
  884. }
  885. }
  886. }
  887. tex.dirty = true;
  888. // Update height array.
  889. int32_t *offw = tex.offsets.ptrw();
  890. for (int k = tex_pos.x; k < tex_pos.x + mw; k++) {
  891. offw[k] = tex_pos.y + mh;
  892. }
  893. chr.texture_idx = tex_pos.index;
  894. 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);
  895. chr.rect.position = Vector2(bounds.l - p_rect_margin, -bounds.t - p_rect_margin);
  896. chr.rect.size = chr.uv_rect.size;
  897. }
  898. return chr;
  899. }
  900. #endif
  901. #ifdef MODULE_FREETYPE_ENABLED
  902. _FORCE_INLINE_ TextServerAdvanced::FontGlyph TextServerAdvanced::rasterize_bitmap(FontDataForSizeAdvanced *p_data, int p_rect_margin, FT_Bitmap bitmap, int yofs, int xofs, const Vector2 &advance) const {
  903. int w = bitmap.width;
  904. int h = bitmap.rows;
  905. int mw = w + p_rect_margin * 4;
  906. int mh = h + p_rect_margin * 4;
  907. ERR_FAIL_COND_V(mw > 4096, FontGlyph());
  908. ERR_FAIL_COND_V(mh > 4096, FontGlyph());
  909. int color_size = bitmap.pixel_mode == FT_PIXEL_MODE_BGRA ? 4 : 2;
  910. Image::Format require_format = color_size == 4 ? Image::FORMAT_RGBA8 : Image::FORMAT_LA8;
  911. FontTexturePosition tex_pos = find_texture_pos_for_glyph(p_data, color_size, require_format, mw, mh, false);
  912. ERR_FAIL_COND_V(tex_pos.index < 0, FontGlyph());
  913. // Fit character in char texture.
  914. FontTexture &tex = p_data->textures.write[tex_pos.index];
  915. {
  916. uint8_t *wr = tex.imgdata.ptrw();
  917. for (int i = 0; i < h; i++) {
  918. for (int j = 0; j < w; j++) {
  919. int ofs = ((i + tex_pos.y + p_rect_margin * 2) * tex.texture_w + j + tex_pos.x + p_rect_margin * 2) * color_size;
  920. ERR_FAIL_COND_V(ofs >= tex.imgdata.size(), FontGlyph());
  921. switch (bitmap.pixel_mode) {
  922. case FT_PIXEL_MODE_MONO: {
  923. int byte = i * bitmap.pitch + (j >> 3);
  924. int bit = 1 << (7 - (j % 8));
  925. wr[ofs + 0] = 255; // grayscale as 1
  926. wr[ofs + 1] = (bitmap.buffer[byte] & bit) ? 255 : 0;
  927. } break;
  928. case FT_PIXEL_MODE_GRAY:
  929. wr[ofs + 0] = 255; // grayscale as 1
  930. wr[ofs + 1] = bitmap.buffer[i * bitmap.pitch + j];
  931. break;
  932. case FT_PIXEL_MODE_BGRA: {
  933. int ofs_color = i * bitmap.pitch + (j << 2);
  934. wr[ofs + 2] = bitmap.buffer[ofs_color + 0];
  935. wr[ofs + 1] = bitmap.buffer[ofs_color + 1];
  936. wr[ofs + 0] = bitmap.buffer[ofs_color + 2];
  937. wr[ofs + 3] = bitmap.buffer[ofs_color + 3];
  938. } break;
  939. default:
  940. ERR_FAIL_V_MSG(FontGlyph(), "Font uses unsupported pixel format: " + String::num_int64(bitmap.pixel_mode) + ".");
  941. break;
  942. }
  943. }
  944. }
  945. }
  946. tex.dirty = true;
  947. // Update height array.
  948. int32_t *offw = tex.offsets.ptrw();
  949. for (int k = tex_pos.x; k < tex_pos.x + mw; k++) {
  950. offw[k] = tex_pos.y + mh;
  951. }
  952. FontGlyph chr;
  953. chr.advance = advance * p_data->scale / p_data->oversampling;
  954. chr.texture_idx = tex_pos.index;
  955. chr.found = true;
  956. 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);
  957. chr.rect.position = Vector2(xofs - p_rect_margin, -yofs - p_rect_margin) * p_data->scale / p_data->oversampling;
  958. chr.rect.size = chr.uv_rect.size * p_data->scale / p_data->oversampling;
  959. return chr;
  960. }
  961. #endif
  962. /*************************************************************************/
  963. /* Font Cache */
  964. /*************************************************************************/
  965. _FORCE_INLINE_ bool TextServerAdvanced::_ensure_glyph(FontDataAdvanced *p_font_data, const Vector2i &p_size, int32_t p_glyph) const {
  966. ERR_FAIL_COND_V(!_ensure_cache_for_size(p_font_data, p_size), false);
  967. int32_t glyph_index = p_glyph & 0xffffff; // Remove subpixel shifts.
  968. FontDataForSizeAdvanced *fd = p_font_data->cache[p_size];
  969. if (fd->glyph_map.has(p_glyph)) {
  970. return fd->glyph_map[p_glyph].found;
  971. }
  972. if (glyph_index == 0) { // Non graphical or invalid glyph, do not render.
  973. fd->glyph_map[p_glyph] = FontGlyph();
  974. return true;
  975. }
  976. #ifdef MODULE_FREETYPE_ENABLED
  977. FontGlyph gl;
  978. if (fd->face) {
  979. FT_Int32 flags = FT_LOAD_DEFAULT;
  980. bool outline = p_size.y > 0;
  981. switch (p_font_data->hinting) {
  982. case TextServer::HINTING_NONE:
  983. flags |= FT_LOAD_NO_HINTING;
  984. break;
  985. case TextServer::HINTING_LIGHT:
  986. flags |= FT_LOAD_TARGET_LIGHT;
  987. break;
  988. default:
  989. flags |= FT_LOAD_TARGET_NORMAL;
  990. break;
  991. }
  992. if (p_font_data->force_autohinter) {
  993. flags |= FT_LOAD_FORCE_AUTOHINT;
  994. }
  995. if (outline) {
  996. flags |= FT_LOAD_NO_BITMAP;
  997. } else if (FT_HAS_COLOR(fd->face)) {
  998. flags |= FT_LOAD_COLOR;
  999. }
  1000. FT_Fixed v, h;
  1001. FT_Get_Advance(fd->face, glyph_index, flags, &h);
  1002. FT_Get_Advance(fd->face, glyph_index, flags | FT_LOAD_VERTICAL_LAYOUT, &v);
  1003. int error = FT_Load_Glyph(fd->face, glyph_index, flags);
  1004. if (error) {
  1005. fd->glyph_map[p_glyph] = FontGlyph();
  1006. return false;
  1007. }
  1008. if (!p_font_data->msdf) {
  1009. 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)) {
  1010. FT_Pos xshift = (int)((p_glyph >> 27) & 3) << 4;
  1011. FT_Outline_Translate(&fd->face->glyph->outline, xshift, 0);
  1012. } 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)) {
  1013. FT_Pos xshift = (int)((p_glyph >> 27) & 3) << 5;
  1014. FT_Outline_Translate(&fd->face->glyph->outline, xshift, 0);
  1015. }
  1016. }
  1017. if (p_font_data->embolden != 0.f) {
  1018. FT_Pos strength = p_font_data->embolden * p_size.x * 4; // 26.6 fractional units (1 / 64).
  1019. FT_Outline_Embolden(&fd->face->glyph->outline, strength);
  1020. }
  1021. if (p_font_data->transform != Transform2D()) {
  1022. 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).
  1023. FT_Outline_Transform(&fd->face->glyph->outline, &mat);
  1024. }
  1025. if (!outline) {
  1026. if (!p_font_data->msdf) {
  1027. error = FT_Render_Glyph(fd->face->glyph, p_font_data->antialiased ? FT_RENDER_MODE_NORMAL : FT_RENDER_MODE_MONO);
  1028. }
  1029. FT_GlyphSlot slot = fd->face->glyph;
  1030. if (!error) {
  1031. if (p_font_data->msdf) {
  1032. #ifdef MODULE_MSDFGEN_ENABLED
  1033. 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);
  1034. #else
  1035. fd->glyph_map[p_glyph] = FontGlyph();
  1036. ERR_FAIL_V_MSG(false, "Compiled without MSDFGEN support!");
  1037. #endif
  1038. } else {
  1039. 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);
  1040. }
  1041. }
  1042. } else {
  1043. FT_Stroker stroker;
  1044. if (FT_Stroker_New(ft_library, &stroker) != 0) {
  1045. fd->glyph_map[p_glyph] = FontGlyph();
  1046. ERR_FAIL_V_MSG(false, "FreeType: Failed to load glyph stroker.");
  1047. }
  1048. FT_Stroker_Set(stroker, (int)(fd->size.y * fd->oversampling * 16.0), FT_STROKER_LINECAP_BUTT, FT_STROKER_LINEJOIN_ROUND, 0);
  1049. FT_Glyph glyph;
  1050. FT_BitmapGlyph glyph_bitmap;
  1051. if (FT_Get_Glyph(fd->face->glyph, &glyph) != 0) {
  1052. goto cleanup_stroker;
  1053. }
  1054. if (FT_Glyph_Stroke(&glyph, stroker, 1) != 0) {
  1055. goto cleanup_glyph;
  1056. }
  1057. if (FT_Glyph_To_Bitmap(&glyph, p_font_data->antialiased ? FT_RENDER_MODE_NORMAL : FT_RENDER_MODE_MONO, nullptr, 1) != 0) {
  1058. goto cleanup_glyph;
  1059. }
  1060. glyph_bitmap = (FT_BitmapGlyph)glyph;
  1061. gl = rasterize_bitmap(fd, rect_range, glyph_bitmap->bitmap, glyph_bitmap->top, glyph_bitmap->left, Vector2());
  1062. cleanup_glyph:
  1063. FT_Done_Glyph(glyph);
  1064. cleanup_stroker:
  1065. FT_Stroker_Done(stroker);
  1066. }
  1067. fd->glyph_map[p_glyph] = gl;
  1068. return gl.found;
  1069. }
  1070. #endif
  1071. fd->glyph_map[p_glyph] = FontGlyph();
  1072. return false;
  1073. }
  1074. _FORCE_INLINE_ bool TextServerAdvanced::_ensure_cache_for_size(FontDataAdvanced *p_font_data, const Vector2i &p_size) const {
  1075. ERR_FAIL_COND_V(p_size.x <= 0, false);
  1076. if (p_font_data->cache.has(p_size)) {
  1077. return true;
  1078. }
  1079. FontDataForSizeAdvanced *fd = memnew(FontDataForSizeAdvanced);
  1080. fd->size = p_size;
  1081. if (p_font_data->data_ptr && (p_font_data->data_size > 0)) {
  1082. // Init dynamic font.
  1083. #ifdef MODULE_FREETYPE_ENABLED
  1084. int error = 0;
  1085. if (!ft_library) {
  1086. error = FT_Init_FreeType(&ft_library);
  1087. ERR_FAIL_COND_V_MSG(error != 0, false, "FreeType: Error initializing library: '" + String(FT_Error_String(error)) + "'.");
  1088. }
  1089. memset(&fd->stream, 0, sizeof(FT_StreamRec));
  1090. fd->stream.base = (unsigned char *)p_font_data->data_ptr;
  1091. fd->stream.size = p_font_data->data_size;
  1092. fd->stream.pos = 0;
  1093. FT_Open_Args fargs;
  1094. memset(&fargs, 0, sizeof(FT_Open_Args));
  1095. fargs.memory_base = (unsigned char *)p_font_data->data_ptr;
  1096. fargs.memory_size = p_font_data->data_size;
  1097. fargs.flags = FT_OPEN_MEMORY;
  1098. fargs.stream = &fd->stream;
  1099. error = FT_Open_Face(ft_library, &fargs, 0, &fd->face);
  1100. if (error) {
  1101. FT_Done_Face(fd->face);
  1102. fd->face = nullptr;
  1103. ERR_FAIL_V_MSG(false, "FreeType: Error loading font: '" + String(FT_Error_String(error)) + "'.");
  1104. }
  1105. if (p_font_data->msdf) {
  1106. fd->oversampling = 1.0;
  1107. fd->size.x = p_font_data->msdf_source_size;
  1108. } else if (p_font_data->oversampling <= 0.0) {
  1109. fd->oversampling = font_get_global_oversampling();
  1110. } else {
  1111. fd->oversampling = p_font_data->oversampling;
  1112. }
  1113. if (FT_HAS_COLOR(fd->face) && fd->face->num_fixed_sizes > 0) {
  1114. int best_match = 0;
  1115. int diff = ABS(fd->size.x - ((int64_t)fd->face->available_sizes[0].width));
  1116. fd->scale = double(fd->size.x * fd->oversampling) / fd->face->available_sizes[0].width;
  1117. for (int i = 1; i < fd->face->num_fixed_sizes; i++) {
  1118. int ndiff = ABS(fd->size.x - ((int64_t)fd->face->available_sizes[i].width));
  1119. if (ndiff < diff) {
  1120. best_match = i;
  1121. diff = ndiff;
  1122. fd->scale = double(fd->size.x * fd->oversampling) / fd->face->available_sizes[i].width;
  1123. }
  1124. }
  1125. FT_Select_Size(fd->face, best_match);
  1126. } else {
  1127. FT_Set_Pixel_Sizes(fd->face, 0, double(fd->size.x * fd->oversampling));
  1128. fd->scale = ((double)fd->size.x * fd->oversampling) / (double)fd->face->size->metrics.y_ppem;
  1129. }
  1130. fd->hb_handle = hb_ft_font_create(fd->face, nullptr);
  1131. fd->ascent = (fd->face->size->metrics.ascender / 64.0) / fd->oversampling * fd->scale;
  1132. fd->descent = (-fd->face->size->metrics.descender / 64.0) / fd->oversampling * fd->scale;
  1133. fd->underline_position = (-FT_MulFix(fd->face->underline_position, fd->face->size->metrics.y_scale) / 64.0) / fd->oversampling * fd->scale;
  1134. fd->underline_thickness = (FT_MulFix(fd->face->underline_thickness, fd->face->size->metrics.y_scale) / 64.0) / fd->oversampling * fd->scale;
  1135. hb_font_set_synthetic_slant(fd->hb_handle, p_font_data->transform[0][1]);
  1136. if (!p_font_data->face_init) {
  1137. // Get style flags and name.
  1138. if (fd->face->family_name != nullptr) {
  1139. p_font_data->font_name = String::utf8((const char *)fd->face->family_name);
  1140. }
  1141. if (fd->face->style_name != nullptr) {
  1142. p_font_data->style_name = String::utf8((const char *)fd->face->style_name);
  1143. }
  1144. p_font_data->style_flags = 0;
  1145. if (fd->face->style_flags & FT_STYLE_FLAG_BOLD) {
  1146. p_font_data->style_flags |= FONT_BOLD;
  1147. }
  1148. if (fd->face->style_flags & FT_STYLE_FLAG_ITALIC) {
  1149. p_font_data->style_flags |= FONT_ITALIC;
  1150. }
  1151. if (fd->face->face_flags & FT_FACE_FLAG_FIXED_WIDTH) {
  1152. p_font_data->style_flags |= FONT_FIXED_WIDTH;
  1153. }
  1154. // Get supported scripts from OpenType font data.
  1155. p_font_data->supported_scripts.clear();
  1156. unsigned int count = hb_ot_layout_table_get_script_tags(hb_font_get_face(fd->hb_handle), HB_OT_TAG_GSUB, 0, nullptr, nullptr);
  1157. if (count != 0) {
  1158. hb_tag_t *script_tags = (hb_tag_t *)memalloc(count * sizeof(hb_tag_t));
  1159. hb_ot_layout_table_get_script_tags(hb_font_get_face(fd->hb_handle), HB_OT_TAG_GSUB, 0, &count, script_tags);
  1160. for (unsigned int i = 0; i < count; i++) {
  1161. p_font_data->supported_scripts.insert(script_tags[i]);
  1162. }
  1163. memfree(script_tags);
  1164. }
  1165. count = hb_ot_layout_table_get_script_tags(hb_font_get_face(fd->hb_handle), HB_OT_TAG_GPOS, 0, nullptr, nullptr);
  1166. if (count != 0) {
  1167. hb_tag_t *script_tags = (hb_tag_t *)memalloc(count * sizeof(hb_tag_t));
  1168. hb_ot_layout_table_get_script_tags(hb_font_get_face(fd->hb_handle), HB_OT_TAG_GPOS, 0, &count, script_tags);
  1169. for (unsigned int i = 0; i < count; i++) {
  1170. p_font_data->supported_scripts.insert(script_tags[i]);
  1171. }
  1172. memfree(script_tags);
  1173. }
  1174. // Get supported scripts from OS2 table.
  1175. TT_OS2 *os2 = (TT_OS2 *)FT_Get_Sfnt_Table(fd->face, FT_SFNT_OS2);
  1176. if (os2) {
  1177. 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)) {
  1178. p_font_data->supported_scripts.insert(HB_SCRIPT_COMMON);
  1179. }
  1180. if ((os2->ulUnicodeRange1 & 1L << 0) || (os2->ulUnicodeRange1 & 1L << 1) || (os2->ulUnicodeRange1 & 1L << 2) || (os2->ulUnicodeRange1 & 1L << 3) || (os2->ulUnicodeRange1 & 1L << 29)) {
  1181. p_font_data->supported_scripts.insert(HB_SCRIPT_LATIN);
  1182. }
  1183. if ((os2->ulUnicodeRange1 & 1L << 7) || (os2->ulUnicodeRange1 & 1L << 30)) {
  1184. p_font_data->supported_scripts.insert(HB_SCRIPT_GREEK);
  1185. }
  1186. if (os2->ulUnicodeRange1 & 1L << 8) {
  1187. p_font_data->supported_scripts.insert(HB_SCRIPT_COPTIC);
  1188. }
  1189. if (os2->ulUnicodeRange1 & 1L << 9) {
  1190. p_font_data->supported_scripts.insert(HB_SCRIPT_CYRILLIC);
  1191. }
  1192. if (os2->ulUnicodeRange1 & 1L << 10) {
  1193. p_font_data->supported_scripts.insert(HB_SCRIPT_ARMENIAN);
  1194. }
  1195. if (os2->ulUnicodeRange1 & 1L << 11) {
  1196. p_font_data->supported_scripts.insert(HB_SCRIPT_HEBREW);
  1197. }
  1198. if (os2->ulUnicodeRange1 & 1L << 12) {
  1199. p_font_data->supported_scripts.insert(HB_SCRIPT_VAI);
  1200. }
  1201. if ((os2->ulUnicodeRange1 & 1L << 13) || (os2->ulUnicodeRange2 & 1L << 31) || (os2->ulUnicodeRange3 & 1L << 3)) {
  1202. p_font_data->supported_scripts.insert(HB_SCRIPT_ARABIC);
  1203. }
  1204. if (os2->ulUnicodeRange1 & 1L << 14) {
  1205. p_font_data->supported_scripts.insert(HB_SCRIPT_NKO);
  1206. }
  1207. if (os2->ulUnicodeRange1 & 1L << 15) {
  1208. p_font_data->supported_scripts.insert(HB_SCRIPT_DEVANAGARI);
  1209. }
  1210. if (os2->ulUnicodeRange1 & 1L << 16) {
  1211. p_font_data->supported_scripts.insert(HB_SCRIPT_BENGALI);
  1212. }
  1213. if (os2->ulUnicodeRange1 & 1L << 17) {
  1214. p_font_data->supported_scripts.insert(HB_SCRIPT_GURMUKHI);
  1215. }
  1216. if (os2->ulUnicodeRange1 & 1L << 18) {
  1217. p_font_data->supported_scripts.insert(HB_SCRIPT_GUJARATI);
  1218. }
  1219. if (os2->ulUnicodeRange1 & 1L << 19) {
  1220. p_font_data->supported_scripts.insert(HB_SCRIPT_ORIYA);
  1221. }
  1222. if (os2->ulUnicodeRange1 & 1L << 20) {
  1223. p_font_data->supported_scripts.insert(HB_SCRIPT_TAMIL);
  1224. }
  1225. if (os2->ulUnicodeRange1 & 1L << 21) {
  1226. p_font_data->supported_scripts.insert(HB_SCRIPT_TELUGU);
  1227. }
  1228. if (os2->ulUnicodeRange1 & 1L << 22) {
  1229. p_font_data->supported_scripts.insert(HB_SCRIPT_KANNADA);
  1230. }
  1231. if (os2->ulUnicodeRange1 & 1L << 23) {
  1232. p_font_data->supported_scripts.insert(HB_SCRIPT_MALAYALAM);
  1233. }
  1234. if (os2->ulUnicodeRange1 & 1L << 24) {
  1235. p_font_data->supported_scripts.insert(HB_SCRIPT_THAI);
  1236. }
  1237. if (os2->ulUnicodeRange1 & 1L << 25) {
  1238. p_font_data->supported_scripts.insert(HB_SCRIPT_LAO);
  1239. }
  1240. if (os2->ulUnicodeRange1 & 1L << 26) {
  1241. p_font_data->supported_scripts.insert(HB_SCRIPT_GEORGIAN);
  1242. }
  1243. if (os2->ulUnicodeRange1 & 1L << 27) {
  1244. p_font_data->supported_scripts.insert(HB_SCRIPT_BALINESE);
  1245. }
  1246. if ((os2->ulUnicodeRange1 & 1L << 28) || (os2->ulUnicodeRange2 & 1L << 20) || (os2->ulUnicodeRange2 & 1L << 24)) {
  1247. p_font_data->supported_scripts.insert(HB_SCRIPT_HANGUL);
  1248. }
  1249. 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)) {
  1250. p_font_data->supported_scripts.insert(HB_SCRIPT_HAN);
  1251. }
  1252. if (os2->ulUnicodeRange2 & 1L << 17) {
  1253. p_font_data->supported_scripts.insert(HB_SCRIPT_HIRAGANA);
  1254. }
  1255. if (os2->ulUnicodeRange2 & 1L << 18) {
  1256. p_font_data->supported_scripts.insert(HB_SCRIPT_KATAKANA);
  1257. }
  1258. if (os2->ulUnicodeRange2 & 1L << 19) {
  1259. p_font_data->supported_scripts.insert(HB_SCRIPT_BOPOMOFO);
  1260. }
  1261. if (os2->ulUnicodeRange3 & 1L << 6) {
  1262. p_font_data->supported_scripts.insert(HB_SCRIPT_TIBETAN);
  1263. }
  1264. if (os2->ulUnicodeRange3 & 1L << 7) {
  1265. p_font_data->supported_scripts.insert(HB_SCRIPT_SYRIAC);
  1266. }
  1267. if (os2->ulUnicodeRange3 & 1L << 8) {
  1268. p_font_data->supported_scripts.insert(HB_SCRIPT_THAANA);
  1269. }
  1270. if (os2->ulUnicodeRange3 & 1L << 9) {
  1271. p_font_data->supported_scripts.insert(HB_SCRIPT_SINHALA);
  1272. }
  1273. if (os2->ulUnicodeRange3 & 1L << 10) {
  1274. p_font_data->supported_scripts.insert(HB_SCRIPT_MYANMAR);
  1275. }
  1276. if (os2->ulUnicodeRange3 & 1L << 11) {
  1277. p_font_data->supported_scripts.insert(HB_SCRIPT_ETHIOPIC);
  1278. }
  1279. if (os2->ulUnicodeRange3 & 1L << 12) {
  1280. p_font_data->supported_scripts.insert(HB_SCRIPT_CHEROKEE);
  1281. }
  1282. if (os2->ulUnicodeRange3 & 1L << 13) {
  1283. p_font_data->supported_scripts.insert(HB_SCRIPT_CANADIAN_SYLLABICS);
  1284. }
  1285. if (os2->ulUnicodeRange3 & 1L << 14) {
  1286. p_font_data->supported_scripts.insert(HB_SCRIPT_OGHAM);
  1287. }
  1288. if (os2->ulUnicodeRange3 & 1L << 15) {
  1289. p_font_data->supported_scripts.insert(HB_SCRIPT_RUNIC);
  1290. }
  1291. if (os2->ulUnicodeRange3 & 1L << 16) {
  1292. p_font_data->supported_scripts.insert(HB_SCRIPT_KHMER);
  1293. }
  1294. if (os2->ulUnicodeRange3 & 1L << 17) {
  1295. p_font_data->supported_scripts.insert(HB_SCRIPT_MONGOLIAN);
  1296. }
  1297. if (os2->ulUnicodeRange3 & 1L << 19) {
  1298. p_font_data->supported_scripts.insert(HB_SCRIPT_YI);
  1299. }
  1300. if (os2->ulUnicodeRange3 & 1L << 20) {
  1301. p_font_data->supported_scripts.insert(HB_SCRIPT_HANUNOO);
  1302. p_font_data->supported_scripts.insert(HB_SCRIPT_TAGBANWA);
  1303. p_font_data->supported_scripts.insert(HB_SCRIPT_BUHID);
  1304. p_font_data->supported_scripts.insert(HB_SCRIPT_TAGALOG);
  1305. }
  1306. if (os2->ulUnicodeRange3 & 1L << 21) {
  1307. p_font_data->supported_scripts.insert(HB_SCRIPT_OLD_ITALIC);
  1308. }
  1309. if (os2->ulUnicodeRange3 & 1L << 22) {
  1310. p_font_data->supported_scripts.insert(HB_SCRIPT_GOTHIC);
  1311. }
  1312. if (os2->ulUnicodeRange3 & 1L << 23) {
  1313. p_font_data->supported_scripts.insert(HB_SCRIPT_DESERET);
  1314. }
  1315. if (os2->ulUnicodeRange3 & 1L << 29) {
  1316. p_font_data->supported_scripts.insert(HB_SCRIPT_LIMBU);
  1317. }
  1318. if (os2->ulUnicodeRange3 & 1L << 30) {
  1319. p_font_data->supported_scripts.insert(HB_SCRIPT_TAI_LE);
  1320. }
  1321. if (os2->ulUnicodeRange3 & 1L << 31) {
  1322. p_font_data->supported_scripts.insert(HB_SCRIPT_NEW_TAI_LUE);
  1323. }
  1324. if (os2->ulUnicodeRange4 & 1L << 0) {
  1325. p_font_data->supported_scripts.insert(HB_SCRIPT_BUGINESE);
  1326. }
  1327. if (os2->ulUnicodeRange4 & 1L << 1) {
  1328. p_font_data->supported_scripts.insert(HB_SCRIPT_GLAGOLITIC);
  1329. }
  1330. if (os2->ulUnicodeRange4 & 1L << 2) {
  1331. p_font_data->supported_scripts.insert(HB_SCRIPT_TIFINAGH);
  1332. }
  1333. if (os2->ulUnicodeRange4 & 1L << 4) {
  1334. p_font_data->supported_scripts.insert(HB_SCRIPT_SYLOTI_NAGRI);
  1335. }
  1336. if (os2->ulUnicodeRange4 & 1L << 5) {
  1337. p_font_data->supported_scripts.insert(HB_SCRIPT_LINEAR_B);
  1338. }
  1339. if (os2->ulUnicodeRange4 & 1L << 7) {
  1340. p_font_data->supported_scripts.insert(HB_SCRIPT_UGARITIC);
  1341. }
  1342. if (os2->ulUnicodeRange4 & 1L << 8) {
  1343. p_font_data->supported_scripts.insert(HB_SCRIPT_OLD_PERSIAN);
  1344. }
  1345. if (os2->ulUnicodeRange4 & 1L << 9) {
  1346. p_font_data->supported_scripts.insert(HB_SCRIPT_SHAVIAN);
  1347. }
  1348. if (os2->ulUnicodeRange4 & 1L << 10) {
  1349. p_font_data->supported_scripts.insert(HB_SCRIPT_OSMANYA);
  1350. }
  1351. if (os2->ulUnicodeRange4 & 1L << 11) {
  1352. p_font_data->supported_scripts.insert(HB_SCRIPT_CYPRIOT);
  1353. }
  1354. if (os2->ulUnicodeRange4 & 1L << 12) {
  1355. p_font_data->supported_scripts.insert(HB_SCRIPT_KHAROSHTHI);
  1356. }
  1357. if (os2->ulUnicodeRange4 & 1L << 13) {
  1358. p_font_data->supported_scripts.insert(HB_SCRIPT_TAI_VIET);
  1359. }
  1360. if (os2->ulUnicodeRange4 & 1L << 14) {
  1361. p_font_data->supported_scripts.insert(HB_SCRIPT_CUNEIFORM);
  1362. }
  1363. if (os2->ulUnicodeRange4 & 1L << 16) {
  1364. p_font_data->supported_scripts.insert(HB_SCRIPT_SUNDANESE);
  1365. }
  1366. if (os2->ulUnicodeRange4 & 1L << 17) {
  1367. p_font_data->supported_scripts.insert(HB_SCRIPT_LEPCHA);
  1368. }
  1369. if (os2->ulUnicodeRange4 & 1L << 18) {
  1370. p_font_data->supported_scripts.insert(HB_SCRIPT_OL_CHIKI);
  1371. }
  1372. if (os2->ulUnicodeRange4 & 1L << 19) {
  1373. p_font_data->supported_scripts.insert(HB_SCRIPT_SAURASHTRA);
  1374. }
  1375. if (os2->ulUnicodeRange4 & 1L << 20) {
  1376. p_font_data->supported_scripts.insert(HB_SCRIPT_KAYAH_LI);
  1377. }
  1378. if (os2->ulUnicodeRange4 & 1L << 21) {
  1379. p_font_data->supported_scripts.insert(HB_SCRIPT_REJANG);
  1380. }
  1381. if (os2->ulUnicodeRange4 & 1L << 22) {
  1382. p_font_data->supported_scripts.insert(HB_SCRIPT_CHAM);
  1383. }
  1384. if (os2->ulUnicodeRange4 & 1L << 25) {
  1385. p_font_data->supported_scripts.insert(HB_SCRIPT_ANATOLIAN_HIEROGLYPHS);
  1386. }
  1387. }
  1388. // Read OpenType feature tags.
  1389. p_font_data->supported_features.clear();
  1390. count = hb_ot_layout_table_get_feature_tags(hb_font_get_face(fd->hb_handle), HB_OT_TAG_GSUB, 0, nullptr, nullptr);
  1391. if (count != 0) {
  1392. hb_tag_t *feature_tags = (hb_tag_t *)memalloc(count * sizeof(hb_tag_t));
  1393. hb_ot_layout_table_get_feature_tags(hb_font_get_face(fd->hb_handle), HB_OT_TAG_GSUB, 0, &count, feature_tags);
  1394. for (unsigned int i = 0; i < count; i++) {
  1395. p_font_data->supported_features[feature_tags[i]] = 1;
  1396. }
  1397. memfree(feature_tags);
  1398. }
  1399. count = hb_ot_layout_table_get_feature_tags(hb_font_get_face(fd->hb_handle), HB_OT_TAG_GPOS, 0, nullptr, nullptr);
  1400. if (count != 0) {
  1401. hb_tag_t *feature_tags = (hb_tag_t *)memalloc(count * sizeof(hb_tag_t));
  1402. hb_ot_layout_table_get_feature_tags(hb_font_get_face(fd->hb_handle), HB_OT_TAG_GPOS, 0, &count, feature_tags);
  1403. for (unsigned int i = 0; i < count; i++) {
  1404. p_font_data->supported_features[feature_tags[i]] = 1;
  1405. }
  1406. memfree(feature_tags);
  1407. }
  1408. // Read OpenType variations.
  1409. p_font_data->supported_varaitions.clear();
  1410. if (fd->face->face_flags & FT_FACE_FLAG_MULTIPLE_MASTERS) {
  1411. FT_MM_Var *amaster;
  1412. FT_Get_MM_Var(fd->face, &amaster);
  1413. for (FT_UInt i = 0; i < amaster->num_axis; i++) {
  1414. 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);
  1415. }
  1416. FT_Done_MM_Var(ft_library, amaster);
  1417. }
  1418. p_font_data->face_init = true;
  1419. }
  1420. // Write variations.
  1421. if (fd->face->face_flags & FT_FACE_FLAG_MULTIPLE_MASTERS) {
  1422. FT_MM_Var *amaster;
  1423. FT_Get_MM_Var(fd->face, &amaster);
  1424. Vector<hb_variation_t> hb_vars;
  1425. Vector<FT_Fixed> coords;
  1426. coords.resize(amaster->num_axis);
  1427. FT_Get_Var_Design_Coordinates(fd->face, coords.size(), coords.ptrw());
  1428. for (FT_UInt i = 0; i < amaster->num_axis; i++) {
  1429. hb_variation_t var;
  1430. // Reset to default.
  1431. var.tag = amaster->axis[i].tag;
  1432. var.value = (double)amaster->axis[i].def / 65536.0;
  1433. coords.write[i] = amaster->axis[i].def;
  1434. if (p_font_data->variation_coordinates.has(var.tag)) {
  1435. var.value = p_font_data->variation_coordinates[var.tag];
  1436. coords.write[i] = CLAMP(var.value * 65536.0, amaster->axis[i].minimum, amaster->axis[i].maximum);
  1437. }
  1438. if (p_font_data->variation_coordinates.has(tag_to_name(var.tag))) {
  1439. var.value = p_font_data->variation_coordinates[tag_to_name(var.tag)];
  1440. coords.write[i] = CLAMP(var.value * 65536.0, amaster->axis[i].minimum, amaster->axis[i].maximum);
  1441. }
  1442. hb_vars.push_back(var);
  1443. }
  1444. FT_Set_Var_Design_Coordinates(fd->face, coords.size(), coords.ptrw());
  1445. hb_font_set_variations(fd->hb_handle, hb_vars.is_empty() ? nullptr : &hb_vars[0], hb_vars.size());
  1446. FT_Done_MM_Var(ft_library, amaster);
  1447. }
  1448. #else
  1449. ERR_FAIL_V_MSG(false, "FreeType: Can't load dynamic font, engine is compiled without FreeType support!");
  1450. #endif
  1451. } else {
  1452. // Init bitmap font.
  1453. fd->hb_handle = _bmp_font_create(fd, nullptr);
  1454. }
  1455. p_font_data->cache[p_size] = fd;
  1456. return true;
  1457. }
  1458. _FORCE_INLINE_ void TextServerAdvanced::_font_clear_cache(FontDataAdvanced *p_font_data) {
  1459. for (const KeyValue<Vector2i, FontDataForSizeAdvanced *> &E : p_font_data->cache) {
  1460. memdelete(E.value);
  1461. }
  1462. p_font_data->cache.clear();
  1463. p_font_data->face_init = false;
  1464. p_font_data->supported_features.clear();
  1465. p_font_data->supported_varaitions.clear();
  1466. p_font_data->supported_scripts.clear();
  1467. }
  1468. hb_font_t *TextServerAdvanced::_font_get_hb_handle(const RID &p_font_rid, int64_t p_size) const {
  1469. FontDataAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1470. ERR_FAIL_COND_V(!fd, nullptr);
  1471. MutexLock lock(fd->mutex);
  1472. Vector2i size = _get_size(fd, p_size);
  1473. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), nullptr);
  1474. return fd->cache[size]->hb_handle;
  1475. }
  1476. RID TextServerAdvanced::create_font() {
  1477. _THREAD_SAFE_METHOD_
  1478. FontDataAdvanced *fd = memnew(FontDataAdvanced);
  1479. return font_owner.make_rid(fd);
  1480. }
  1481. void TextServerAdvanced::font_set_data(const RID &p_font_rid, const PackedByteArray &p_data) {
  1482. FontDataAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1483. ERR_FAIL_COND(!fd);
  1484. MutexLock lock(fd->mutex);
  1485. _font_clear_cache(fd);
  1486. fd->data = p_data;
  1487. fd->data_ptr = fd->data.ptr();
  1488. fd->data_size = fd->data.size();
  1489. }
  1490. void TextServerAdvanced::font_set_data_ptr(const RID &p_font_rid, const uint8_t *p_data_ptr, int64_t p_data_size) {
  1491. FontDataAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1492. ERR_FAIL_COND(!fd);
  1493. MutexLock lock(fd->mutex);
  1494. _font_clear_cache(fd);
  1495. fd->data.resize(0);
  1496. fd->data_ptr = p_data_ptr;
  1497. fd->data_size = p_data_size;
  1498. }
  1499. void TextServerAdvanced::font_set_style(const RID &p_font_rid, int64_t /*FontStyle*/ p_style) {
  1500. FontDataAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1501. ERR_FAIL_COND(!fd);
  1502. MutexLock lock(fd->mutex);
  1503. Vector2i size = _get_size(fd, 16);
  1504. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  1505. fd->style_flags = p_style;
  1506. }
  1507. int64_t /*FontStyle*/ TextServerAdvanced::font_get_style(const RID &p_font_rid) const {
  1508. FontDataAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1509. ERR_FAIL_COND_V(!fd, 0);
  1510. MutexLock lock(fd->mutex);
  1511. Vector2i size = _get_size(fd, 16);
  1512. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), 0);
  1513. return fd->style_flags;
  1514. }
  1515. void TextServerAdvanced::font_set_style_name(const RID &p_font_rid, const String &p_name) {
  1516. FontDataAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1517. ERR_FAIL_COND(!fd);
  1518. MutexLock lock(fd->mutex);
  1519. Vector2i size = _get_size(fd, 16);
  1520. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  1521. fd->style_name = p_name;
  1522. }
  1523. String TextServerAdvanced::font_get_style_name(const RID &p_font_rid) const {
  1524. FontDataAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1525. ERR_FAIL_COND_V(!fd, String());
  1526. MutexLock lock(fd->mutex);
  1527. Vector2i size = _get_size(fd, 16);
  1528. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), String());
  1529. return fd->style_name;
  1530. }
  1531. void TextServerAdvanced::font_set_name(const RID &p_font_rid, const String &p_name) {
  1532. FontDataAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1533. ERR_FAIL_COND(!fd);
  1534. MutexLock lock(fd->mutex);
  1535. Vector2i size = _get_size(fd, 16);
  1536. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  1537. fd->font_name = p_name;
  1538. }
  1539. String TextServerAdvanced::font_get_name(const RID &p_font_rid) const {
  1540. FontDataAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1541. ERR_FAIL_COND_V(!fd, String());
  1542. MutexLock lock(fd->mutex);
  1543. Vector2i size = _get_size(fd, 16);
  1544. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), String());
  1545. return fd->font_name;
  1546. }
  1547. void TextServerAdvanced::font_set_antialiased(const RID &p_font_rid, bool p_antialiased) {
  1548. FontDataAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1549. ERR_FAIL_COND(!fd);
  1550. MutexLock lock(fd->mutex);
  1551. if (fd->antialiased != p_antialiased) {
  1552. _font_clear_cache(fd);
  1553. fd->antialiased = p_antialiased;
  1554. }
  1555. }
  1556. bool TextServerAdvanced::font_is_antialiased(const RID &p_font_rid) const {
  1557. FontDataAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1558. ERR_FAIL_COND_V(!fd, false);
  1559. MutexLock lock(fd->mutex);
  1560. return fd->antialiased;
  1561. }
  1562. void TextServerAdvanced::font_set_generate_mipmaps(const RID &p_font_rid, bool p_generate_mipmaps) {
  1563. FontDataAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1564. ERR_FAIL_COND(!fd);
  1565. MutexLock lock(fd->mutex);
  1566. if (fd->mipmaps != p_generate_mipmaps) {
  1567. for (KeyValue<Vector2i, FontDataForSizeAdvanced *> &E : fd->cache) {
  1568. for (int i = 0; i < E.value->textures.size(); i++) {
  1569. E.value->textures.write[i].dirty = true;
  1570. }
  1571. }
  1572. fd->mipmaps = p_generate_mipmaps;
  1573. }
  1574. }
  1575. bool TextServerAdvanced::font_get_generate_mipmaps(const RID &p_font_rid) const {
  1576. FontDataAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1577. ERR_FAIL_COND_V(!fd, false);
  1578. MutexLock lock(fd->mutex);
  1579. return fd->mipmaps;
  1580. }
  1581. void TextServerAdvanced::font_set_multichannel_signed_distance_field(const RID &p_font_rid, bool p_msdf) {
  1582. FontDataAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1583. ERR_FAIL_COND(!fd);
  1584. MutexLock lock(fd->mutex);
  1585. if (fd->msdf != p_msdf) {
  1586. _font_clear_cache(fd);
  1587. fd->msdf = p_msdf;
  1588. }
  1589. }
  1590. bool TextServerAdvanced::font_is_multichannel_signed_distance_field(const RID &p_font_rid) const {
  1591. FontDataAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1592. ERR_FAIL_COND_V(!fd, false);
  1593. MutexLock lock(fd->mutex);
  1594. return fd->msdf;
  1595. }
  1596. void TextServerAdvanced::font_set_msdf_pixel_range(const RID &p_font_rid, int64_t p_msdf_pixel_range) {
  1597. FontDataAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1598. ERR_FAIL_COND(!fd);
  1599. MutexLock lock(fd->mutex);
  1600. if (fd->msdf_range != p_msdf_pixel_range) {
  1601. _font_clear_cache(fd);
  1602. fd->msdf_range = p_msdf_pixel_range;
  1603. }
  1604. }
  1605. int64_t TextServerAdvanced::font_get_msdf_pixel_range(const RID &p_font_rid) const {
  1606. FontDataAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1607. ERR_FAIL_COND_V(!fd, false);
  1608. MutexLock lock(fd->mutex);
  1609. return fd->msdf_range;
  1610. }
  1611. void TextServerAdvanced::font_set_msdf_size(const RID &p_font_rid, int64_t p_msdf_size) {
  1612. FontDataAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1613. ERR_FAIL_COND(!fd);
  1614. MutexLock lock(fd->mutex);
  1615. if (fd->msdf_source_size != p_msdf_size) {
  1616. _font_clear_cache(fd);
  1617. fd->msdf_source_size = p_msdf_size;
  1618. }
  1619. }
  1620. int64_t TextServerAdvanced::font_get_msdf_size(const RID &p_font_rid) const {
  1621. FontDataAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1622. ERR_FAIL_COND_V(!fd, false);
  1623. MutexLock lock(fd->mutex);
  1624. return fd->msdf_source_size;
  1625. }
  1626. void TextServerAdvanced::font_set_fixed_size(const RID &p_font_rid, int64_t p_fixed_size) {
  1627. FontDataAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1628. ERR_FAIL_COND(!fd);
  1629. MutexLock lock(fd->mutex);
  1630. fd->fixed_size = p_fixed_size;
  1631. }
  1632. int64_t TextServerAdvanced::font_get_fixed_size(const RID &p_font_rid) const {
  1633. FontDataAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1634. ERR_FAIL_COND_V(!fd, false);
  1635. MutexLock lock(fd->mutex);
  1636. return fd->fixed_size;
  1637. }
  1638. void TextServerAdvanced::font_set_force_autohinter(const RID &p_font_rid, bool p_force_autohinter) {
  1639. FontDataAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1640. ERR_FAIL_COND(!fd);
  1641. MutexLock lock(fd->mutex);
  1642. if (fd->force_autohinter != p_force_autohinter) {
  1643. _font_clear_cache(fd);
  1644. fd->force_autohinter = p_force_autohinter;
  1645. }
  1646. }
  1647. bool TextServerAdvanced::font_is_force_autohinter(const RID &p_font_rid) const {
  1648. FontDataAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1649. ERR_FAIL_COND_V(!fd, false);
  1650. MutexLock lock(fd->mutex);
  1651. return fd->force_autohinter;
  1652. }
  1653. void TextServerAdvanced::font_set_hinting(const RID &p_font_rid, TextServer::Hinting p_hinting) {
  1654. FontDataAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1655. ERR_FAIL_COND(!fd);
  1656. MutexLock lock(fd->mutex);
  1657. if (fd->hinting != p_hinting) {
  1658. _font_clear_cache(fd);
  1659. fd->hinting = p_hinting;
  1660. }
  1661. }
  1662. TextServer::Hinting TextServerAdvanced::font_get_hinting(const RID &p_font_rid) const {
  1663. FontDataAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1664. ERR_FAIL_COND_V(!fd, HINTING_NONE);
  1665. MutexLock lock(fd->mutex);
  1666. return fd->hinting;
  1667. }
  1668. void TextServerAdvanced::font_set_subpixel_positioning(const RID &p_font_rid, TextServer::SubpixelPositioning p_subpixel) {
  1669. FontDataAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1670. ERR_FAIL_COND(!fd);
  1671. MutexLock lock(fd->mutex);
  1672. fd->subpixel_positioning = p_subpixel;
  1673. }
  1674. TextServer::SubpixelPositioning TextServerAdvanced::font_get_subpixel_positioning(const RID &p_font_rid) const {
  1675. FontDataAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1676. ERR_FAIL_COND_V(!fd, SUBPIXEL_POSITIONING_DISABLED);
  1677. MutexLock lock(fd->mutex);
  1678. return fd->subpixel_positioning;
  1679. }
  1680. void TextServerAdvanced::font_set_embolden(const RID &p_font_rid, double p_strength) {
  1681. FontDataAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1682. ERR_FAIL_COND(!fd);
  1683. MutexLock lock(fd->mutex);
  1684. if (fd->embolden != p_strength) {
  1685. _font_clear_cache(fd);
  1686. fd->embolden = p_strength;
  1687. }
  1688. }
  1689. double TextServerAdvanced::font_get_embolden(const RID &p_font_rid) const {
  1690. FontDataAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1691. ERR_FAIL_COND_V(!fd, 0.0);
  1692. MutexLock lock(fd->mutex);
  1693. return fd->embolden;
  1694. }
  1695. void TextServerAdvanced::font_set_transform(const RID &p_font_rid, const Transform2D &p_transform) {
  1696. FontDataAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1697. ERR_FAIL_COND(!fd);
  1698. MutexLock lock(fd->mutex);
  1699. if (fd->transform != p_transform) {
  1700. _font_clear_cache(fd);
  1701. fd->transform = p_transform;
  1702. }
  1703. }
  1704. Transform2D TextServerAdvanced::font_get_transform(const RID &p_font_rid) const {
  1705. FontDataAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1706. ERR_FAIL_COND_V(!fd, Transform2D());
  1707. MutexLock lock(fd->mutex);
  1708. return fd->transform;
  1709. }
  1710. void TextServerAdvanced::font_set_variation_coordinates(const RID &p_font_rid, const Dictionary &p_variation_coordinates) {
  1711. FontDataAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1712. ERR_FAIL_COND(!fd);
  1713. MutexLock lock(fd->mutex);
  1714. if (fd->variation_coordinates != p_variation_coordinates) {
  1715. _font_clear_cache(fd);
  1716. fd->variation_coordinates = p_variation_coordinates;
  1717. }
  1718. }
  1719. Dictionary TextServerAdvanced::font_get_variation_coordinates(const RID &p_font_rid) const {
  1720. FontDataAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1721. ERR_FAIL_COND_V(!fd, Dictionary());
  1722. MutexLock lock(fd->mutex);
  1723. return fd->variation_coordinates;
  1724. }
  1725. void TextServerAdvanced::font_set_oversampling(const RID &p_font_rid, double p_oversampling) {
  1726. FontDataAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1727. ERR_FAIL_COND(!fd);
  1728. MutexLock lock(fd->mutex);
  1729. if (fd->oversampling != p_oversampling) {
  1730. _font_clear_cache(fd);
  1731. fd->oversampling = p_oversampling;
  1732. }
  1733. }
  1734. double TextServerAdvanced::font_get_oversampling(const RID &p_font_rid) const {
  1735. FontDataAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1736. ERR_FAIL_COND_V(!fd, 0.0);
  1737. MutexLock lock(fd->mutex);
  1738. return fd->oversampling;
  1739. }
  1740. Array TextServerAdvanced::font_get_size_cache_list(const RID &p_font_rid) const {
  1741. FontDataAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1742. ERR_FAIL_COND_V(!fd, Array());
  1743. MutexLock lock(fd->mutex);
  1744. Array ret;
  1745. for (const KeyValue<Vector2i, FontDataForSizeAdvanced *> &E : fd->cache) {
  1746. ret.push_back(E.key);
  1747. }
  1748. return ret;
  1749. }
  1750. void TextServerAdvanced::font_clear_size_cache(const RID &p_font_rid) {
  1751. FontDataAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1752. ERR_FAIL_COND(!fd);
  1753. MutexLock lock(fd->mutex);
  1754. for (const KeyValue<Vector2i, FontDataForSizeAdvanced *> &E : fd->cache) {
  1755. memdelete(E.value);
  1756. }
  1757. fd->cache.clear();
  1758. }
  1759. void TextServerAdvanced::font_remove_size_cache(const RID &p_font_rid, const Vector2i &p_size) {
  1760. FontDataAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1761. ERR_FAIL_COND(!fd);
  1762. MutexLock lock(fd->mutex);
  1763. if (fd->cache.has(p_size)) {
  1764. memdelete(fd->cache[p_size]);
  1765. fd->cache.erase(p_size);
  1766. }
  1767. }
  1768. void TextServerAdvanced::font_set_ascent(const RID &p_font_rid, int64_t p_size, double p_ascent) {
  1769. FontDataAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1770. ERR_FAIL_COND(!fd);
  1771. MutexLock lock(fd->mutex);
  1772. Vector2i size = _get_size(fd, p_size);
  1773. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  1774. fd->cache[size]->ascent = p_ascent;
  1775. }
  1776. double TextServerAdvanced::font_get_ascent(const RID &p_font_rid, int64_t p_size) const {
  1777. FontDataAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1778. ERR_FAIL_COND_V(!fd, 0.0);
  1779. MutexLock lock(fd->mutex);
  1780. Vector2i size = _get_size(fd, p_size);
  1781. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), 0.0);
  1782. if (fd->msdf) {
  1783. return fd->cache[size]->ascent * (double)p_size / (double)fd->msdf_source_size;
  1784. } else {
  1785. return fd->cache[size]->ascent;
  1786. }
  1787. }
  1788. void TextServerAdvanced::font_set_descent(const RID &p_font_rid, int64_t p_size, double p_descent) {
  1789. FontDataAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1790. ERR_FAIL_COND(!fd);
  1791. Vector2i size = _get_size(fd, p_size);
  1792. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  1793. fd->cache[size]->descent = p_descent;
  1794. }
  1795. double TextServerAdvanced::font_get_descent(const RID &p_font_rid, int64_t p_size) const {
  1796. FontDataAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1797. ERR_FAIL_COND_V(!fd, 0.0);
  1798. MutexLock lock(fd->mutex);
  1799. Vector2i size = _get_size(fd, p_size);
  1800. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), 0.0);
  1801. if (fd->msdf) {
  1802. return fd->cache[size]->descent * (double)p_size / (double)fd->msdf_source_size;
  1803. } else {
  1804. return fd->cache[size]->descent;
  1805. }
  1806. }
  1807. void TextServerAdvanced::font_set_underline_position(const RID &p_font_rid, int64_t p_size, double p_underline_position) {
  1808. FontDataAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1809. ERR_FAIL_COND(!fd);
  1810. MutexLock lock(fd->mutex);
  1811. Vector2i size = _get_size(fd, p_size);
  1812. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  1813. fd->cache[size]->underline_position = p_underline_position;
  1814. }
  1815. double TextServerAdvanced::font_get_underline_position(const RID &p_font_rid, int64_t p_size) const {
  1816. FontDataAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1817. ERR_FAIL_COND_V(!fd, 0.0);
  1818. MutexLock lock(fd->mutex);
  1819. Vector2i size = _get_size(fd, p_size);
  1820. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), 0.0);
  1821. if (fd->msdf) {
  1822. return fd->cache[size]->underline_position * (double)p_size / (double)fd->msdf_source_size;
  1823. } else {
  1824. return fd->cache[size]->underline_position;
  1825. }
  1826. }
  1827. void TextServerAdvanced::font_set_underline_thickness(const RID &p_font_rid, int64_t p_size, double p_underline_thickness) {
  1828. FontDataAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1829. ERR_FAIL_COND(!fd);
  1830. MutexLock lock(fd->mutex);
  1831. Vector2i size = _get_size(fd, p_size);
  1832. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  1833. fd->cache[size]->underline_thickness = p_underline_thickness;
  1834. }
  1835. double TextServerAdvanced::font_get_underline_thickness(const RID &p_font_rid, int64_t p_size) const {
  1836. FontDataAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1837. ERR_FAIL_COND_V(!fd, 0.0);
  1838. MutexLock lock(fd->mutex);
  1839. Vector2i size = _get_size(fd, p_size);
  1840. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), 0.0);
  1841. if (fd->msdf) {
  1842. return fd->cache[size]->underline_thickness * (double)p_size / (double)fd->msdf_source_size;
  1843. } else {
  1844. return fd->cache[size]->underline_thickness;
  1845. }
  1846. }
  1847. void TextServerAdvanced::font_set_scale(const RID &p_font_rid, int64_t p_size, double p_scale) {
  1848. FontDataAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1849. ERR_FAIL_COND(!fd);
  1850. MutexLock lock(fd->mutex);
  1851. Vector2i size = _get_size(fd, p_size);
  1852. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  1853. #ifdef MODULE_FREETYPE_ENABLED
  1854. if (fd->cache[size]->face) {
  1855. return; // Do not override scale for dynamic fonts, it's calculated automatically.
  1856. }
  1857. #endif
  1858. fd->cache[size]->scale = p_scale;
  1859. }
  1860. double TextServerAdvanced::font_get_scale(const RID &p_font_rid, int64_t p_size) const {
  1861. FontDataAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1862. ERR_FAIL_COND_V(!fd, 0.0);
  1863. MutexLock lock(fd->mutex);
  1864. Vector2i size = _get_size(fd, p_size);
  1865. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), 0.0);
  1866. if (fd->msdf) {
  1867. return fd->cache[size]->scale * (double)p_size / (double)fd->msdf_source_size;
  1868. } else {
  1869. return fd->cache[size]->scale / fd->cache[size]->oversampling;
  1870. }
  1871. }
  1872. void TextServerAdvanced::font_set_spacing(const RID &p_font_rid, int64_t p_size, TextServer::SpacingType p_spacing, int64_t p_value) {
  1873. FontDataAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1874. ERR_FAIL_COND(!fd);
  1875. MutexLock lock(fd->mutex);
  1876. Vector2i size = _get_size(fd, p_size);
  1877. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  1878. switch (p_spacing) {
  1879. case TextServer::SPACING_GLYPH: {
  1880. fd->cache[size]->spacing_glyph = p_value;
  1881. } break;
  1882. case TextServer::SPACING_SPACE: {
  1883. fd->cache[size]->spacing_space = p_value;
  1884. } break;
  1885. default: {
  1886. ERR_FAIL_MSG("Invalid spacing type: " + String::num_int64(p_spacing));
  1887. } break;
  1888. }
  1889. }
  1890. int64_t TextServerAdvanced::font_get_spacing(const RID &p_font_rid, int64_t p_size, TextServer::SpacingType p_spacing) const {
  1891. FontDataAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1892. ERR_FAIL_COND_V(!fd, 0);
  1893. MutexLock lock(fd->mutex);
  1894. Vector2i size = _get_size(fd, p_size);
  1895. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), 0);
  1896. switch (p_spacing) {
  1897. case TextServer::SPACING_GLYPH: {
  1898. if (fd->msdf) {
  1899. return fd->cache[size]->spacing_glyph * (double)p_size / (double)fd->msdf_source_size;
  1900. } else {
  1901. return fd->cache[size]->spacing_glyph;
  1902. }
  1903. } break;
  1904. case TextServer::SPACING_SPACE: {
  1905. if (fd->msdf) {
  1906. return fd->cache[size]->spacing_space * (double)p_size / (double)fd->msdf_source_size;
  1907. } else {
  1908. return fd->cache[size]->spacing_space;
  1909. }
  1910. } break;
  1911. default: {
  1912. ERR_FAIL_V_MSG(0, "Invalid spacing type: " + String::num_int64(p_spacing));
  1913. } break;
  1914. }
  1915. return 0;
  1916. }
  1917. int64_t TextServerAdvanced::font_get_texture_count(const RID &p_font_rid, const Vector2i &p_size) const {
  1918. FontDataAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1919. ERR_FAIL_COND_V(!fd, 0);
  1920. MutexLock lock(fd->mutex);
  1921. Vector2i size = _get_size_outline(fd, p_size);
  1922. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), 0);
  1923. return fd->cache[size]->textures.size();
  1924. }
  1925. void TextServerAdvanced::font_clear_textures(const RID &p_font_rid, const Vector2i &p_size) {
  1926. FontDataAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1927. ERR_FAIL_COND(!fd);
  1928. MutexLock lock(fd->mutex);
  1929. Vector2i size = _get_size_outline(fd, p_size);
  1930. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  1931. fd->cache[size]->textures.clear();
  1932. }
  1933. void TextServerAdvanced::font_remove_texture(const RID &p_font_rid, const Vector2i &p_size, int64_t p_texture_index) {
  1934. FontDataAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1935. ERR_FAIL_COND(!fd);
  1936. MutexLock lock(fd->mutex);
  1937. Vector2i size = _get_size_outline(fd, p_size);
  1938. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  1939. ERR_FAIL_INDEX(p_texture_index, fd->cache[size]->textures.size());
  1940. fd->cache[size]->textures.remove_at(p_texture_index);
  1941. }
  1942. 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) {
  1943. FontDataAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1944. ERR_FAIL_COND(!fd);
  1945. ERR_FAIL_COND(p_image.is_null());
  1946. MutexLock lock(fd->mutex);
  1947. Vector2i size = _get_size_outline(fd, p_size);
  1948. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  1949. ERR_FAIL_COND(p_texture_index < 0);
  1950. if (p_texture_index >= fd->cache[size]->textures.size()) {
  1951. fd->cache[size]->textures.resize(p_texture_index + 1);
  1952. }
  1953. FontTexture &tex = fd->cache[size]->textures.write[p_texture_index];
  1954. tex.imgdata = p_image->get_data();
  1955. tex.texture_w = p_image->get_width();
  1956. tex.texture_h = p_image->get_height();
  1957. tex.format = p_image->get_format();
  1958. Ref<Image> img;
  1959. img.instantiate();
  1960. img->create_from_data(tex.texture_w, tex.texture_h, false, tex.format, tex.imgdata);
  1961. if (fd->mipmaps) {
  1962. img->generate_mipmaps();
  1963. }
  1964. tex.texture = Ref<ImageTexture>();
  1965. tex.texture.instantiate();
  1966. tex.texture->create_from_image(img);
  1967. tex.dirty = false;
  1968. }
  1969. Ref<Image> TextServerAdvanced::font_get_texture_image(const RID &p_font_rid, const Vector2i &p_size, int64_t p_texture_index) const {
  1970. FontDataAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1971. ERR_FAIL_COND_V(!fd, Ref<Image>());
  1972. MutexLock lock(fd->mutex);
  1973. Vector2i size = _get_size_outline(fd, p_size);
  1974. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), Ref<Image>());
  1975. ERR_FAIL_INDEX_V(p_texture_index, fd->cache[size]->textures.size(), Ref<Image>());
  1976. const FontTexture &tex = fd->cache[size]->textures[p_texture_index];
  1977. Ref<Image> img;
  1978. img.instantiate();
  1979. img->create_from_data(tex.texture_w, tex.texture_h, false, tex.format, tex.imgdata);
  1980. return img;
  1981. }
  1982. void TextServerAdvanced::font_set_texture_offsets(const RID &p_font_rid, const Vector2i &p_size, int64_t p_texture_index, const PackedInt32Array &p_offset) {
  1983. FontDataAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1984. ERR_FAIL_COND(!fd);
  1985. MutexLock lock(fd->mutex);
  1986. Vector2i size = _get_size_outline(fd, p_size);
  1987. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  1988. ERR_FAIL_COND(p_texture_index < 0);
  1989. if (p_texture_index >= fd->cache[size]->textures.size()) {
  1990. fd->cache[size]->textures.resize(p_texture_index + 1);
  1991. }
  1992. FontTexture &tex = fd->cache[size]->textures.write[p_texture_index];
  1993. tex.offsets = p_offset;
  1994. }
  1995. PackedInt32Array TextServerAdvanced::font_get_texture_offsets(const RID &p_font_rid, const Vector2i &p_size, int64_t p_texture_index) const {
  1996. FontDataAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1997. ERR_FAIL_COND_V(!fd, PackedInt32Array());
  1998. MutexLock lock(fd->mutex);
  1999. Vector2i size = _get_size_outline(fd, p_size);
  2000. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), PackedInt32Array());
  2001. ERR_FAIL_INDEX_V(p_texture_index, fd->cache[size]->textures.size(), PackedInt32Array());
  2002. const FontTexture &tex = fd->cache[size]->textures[p_texture_index];
  2003. return tex.offsets;
  2004. }
  2005. Array TextServerAdvanced::font_get_glyph_list(const RID &p_font_rid, const Vector2i &p_size) const {
  2006. FontDataAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2007. ERR_FAIL_COND_V(!fd, Array());
  2008. MutexLock lock(fd->mutex);
  2009. Vector2i size = _get_size_outline(fd, p_size);
  2010. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), Array());
  2011. Array ret;
  2012. const HashMap<int32_t, FontGlyph> &gl = fd->cache[size]->glyph_map;
  2013. for (const KeyValue<int32_t, FontGlyph> &E : gl) {
  2014. ret.push_back(E.key);
  2015. }
  2016. return ret;
  2017. }
  2018. void TextServerAdvanced::font_clear_glyphs(const RID &p_font_rid, const Vector2i &p_size) {
  2019. FontDataAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2020. ERR_FAIL_COND(!fd);
  2021. MutexLock lock(fd->mutex);
  2022. Vector2i size = _get_size_outline(fd, p_size);
  2023. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  2024. fd->cache[size]->glyph_map.clear();
  2025. }
  2026. void TextServerAdvanced::font_remove_glyph(const RID &p_font_rid, const Vector2i &p_size, int64_t p_glyph) {
  2027. FontDataAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2028. ERR_FAIL_COND(!fd);
  2029. MutexLock lock(fd->mutex);
  2030. Vector2i size = _get_size_outline(fd, p_size);
  2031. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  2032. fd->cache[size]->glyph_map.erase(p_glyph);
  2033. }
  2034. double TextServerAdvanced::_get_extra_advance(RID p_font_rid, int p_font_size) const {
  2035. const FontDataAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2036. ERR_FAIL_COND_V(!fd, 0.0);
  2037. MutexLock lock(fd->mutex);
  2038. Vector2i size = _get_size(fd, p_font_size);
  2039. if (fd->embolden != 0.0) {
  2040. return fd->embolden * double(size.x) / 64.0;
  2041. } else {
  2042. return 0.0;
  2043. }
  2044. }
  2045. Vector2 TextServerAdvanced::font_get_glyph_advance(const RID &p_font_rid, int64_t p_size, int64_t p_glyph) const {
  2046. FontDataAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2047. ERR_FAIL_COND_V(!fd, Vector2());
  2048. MutexLock lock(fd->mutex);
  2049. Vector2i size = _get_size(fd, p_size);
  2050. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), Vector2());
  2051. if (!_ensure_glyph(fd, size, p_glyph)) {
  2052. return Vector2(); // Invalid or non graphicl glyph, do not display errors.
  2053. }
  2054. const HashMap<int32_t, FontGlyph> &gl = fd->cache[size]->glyph_map;
  2055. Vector2 ea;
  2056. if (fd->embolden != 0.0) {
  2057. ea.x = fd->embolden * double(size.x) / 64.0;
  2058. }
  2059. if (fd->msdf) {
  2060. return (gl[p_glyph].advance + ea) * (double)p_size / (double)fd->msdf_source_size;
  2061. } else if ((fd->subpixel_positioning == SUBPIXEL_POSITIONING_DISABLED) || (fd->subpixel_positioning == SUBPIXEL_POSITIONING_AUTO && size.x > SUBPIXEL_POSITIONING_ONE_HALF_MAX_SIZE)) {
  2062. return (gl[p_glyph].advance + ea).round();
  2063. } else {
  2064. return gl[p_glyph].advance + ea;
  2065. }
  2066. }
  2067. void TextServerAdvanced::font_set_glyph_advance(const RID &p_font_rid, int64_t p_size, int64_t p_glyph, const Vector2 &p_advance) {
  2068. FontDataAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2069. ERR_FAIL_COND(!fd);
  2070. MutexLock lock(fd->mutex);
  2071. Vector2i size = _get_size(fd, p_size);
  2072. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  2073. HashMap<int32_t, FontGlyph> &gl = fd->cache[size]->glyph_map;
  2074. gl[p_glyph].advance = p_advance;
  2075. gl[p_glyph].found = true;
  2076. }
  2077. Vector2 TextServerAdvanced::font_get_glyph_offset(const RID &p_font_rid, const Vector2i &p_size, int64_t p_glyph) const {
  2078. FontDataAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2079. ERR_FAIL_COND_V(!fd, Vector2());
  2080. MutexLock lock(fd->mutex);
  2081. Vector2i size = _get_size_outline(fd, p_size);
  2082. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), Vector2());
  2083. if (!_ensure_glyph(fd, size, p_glyph)) {
  2084. return Vector2(); // Invalid or non graphicl glyph, do not display errors.
  2085. }
  2086. const HashMap<int32_t, FontGlyph> &gl = fd->cache[size]->glyph_map;
  2087. if (fd->msdf) {
  2088. return gl[p_glyph].rect.position * (double)p_size.x / (double)fd->msdf_source_size;
  2089. } else {
  2090. return gl[p_glyph].rect.position;
  2091. }
  2092. }
  2093. void TextServerAdvanced::font_set_glyph_offset(const RID &p_font_rid, const Vector2i &p_size, int64_t p_glyph, const Vector2 &p_offset) {
  2094. FontDataAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2095. ERR_FAIL_COND(!fd);
  2096. MutexLock lock(fd->mutex);
  2097. Vector2i size = _get_size_outline(fd, p_size);
  2098. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  2099. HashMap<int32_t, FontGlyph> &gl = fd->cache[size]->glyph_map;
  2100. gl[p_glyph].rect.position = p_offset;
  2101. gl[p_glyph].found = true;
  2102. }
  2103. Vector2 TextServerAdvanced::font_get_glyph_size(const RID &p_font_rid, const Vector2i &p_size, int64_t p_glyph) const {
  2104. FontDataAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2105. ERR_FAIL_COND_V(!fd, Vector2());
  2106. MutexLock lock(fd->mutex);
  2107. Vector2i size = _get_size_outline(fd, p_size);
  2108. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), Vector2());
  2109. if (!_ensure_glyph(fd, size, p_glyph)) {
  2110. return Vector2(); // Invalid or non graphicl glyph, do not display errors.
  2111. }
  2112. const HashMap<int32_t, FontGlyph> &gl = fd->cache[size]->glyph_map;
  2113. if (fd->msdf) {
  2114. return gl[p_glyph].rect.size * (double)p_size.x / (double)fd->msdf_source_size;
  2115. } else {
  2116. return gl[p_glyph].rect.size;
  2117. }
  2118. }
  2119. void TextServerAdvanced::font_set_glyph_size(const RID &p_font_rid, const Vector2i &p_size, int64_t p_glyph, const Vector2 &p_gl_size) {
  2120. FontDataAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2121. ERR_FAIL_COND(!fd);
  2122. MutexLock lock(fd->mutex);
  2123. Vector2i size = _get_size_outline(fd, p_size);
  2124. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  2125. HashMap<int32_t, FontGlyph> &gl = fd->cache[size]->glyph_map;
  2126. gl[p_glyph].rect.size = p_gl_size;
  2127. gl[p_glyph].found = true;
  2128. }
  2129. Rect2 TextServerAdvanced::font_get_glyph_uv_rect(const RID &p_font_rid, const Vector2i &p_size, int64_t p_glyph) const {
  2130. FontDataAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2131. ERR_FAIL_COND_V(!fd, Rect2());
  2132. MutexLock lock(fd->mutex);
  2133. Vector2i size = _get_size_outline(fd, p_size);
  2134. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), Rect2());
  2135. if (!_ensure_glyph(fd, size, p_glyph)) {
  2136. return Rect2(); // Invalid or non graphicl glyph, do not display errors.
  2137. }
  2138. const HashMap<int32_t, FontGlyph> &gl = fd->cache[size]->glyph_map;
  2139. return gl[p_glyph].uv_rect;
  2140. }
  2141. 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) {
  2142. FontDataAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2143. ERR_FAIL_COND(!fd);
  2144. MutexLock lock(fd->mutex);
  2145. Vector2i size = _get_size_outline(fd, p_size);
  2146. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  2147. HashMap<int32_t, FontGlyph> &gl = fd->cache[size]->glyph_map;
  2148. gl[p_glyph].uv_rect = p_uv_rect;
  2149. gl[p_glyph].found = true;
  2150. }
  2151. int64_t TextServerAdvanced::font_get_glyph_texture_idx(const RID &p_font_rid, const Vector2i &p_size, int64_t p_glyph) const {
  2152. FontDataAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2153. ERR_FAIL_COND_V(!fd, -1);
  2154. MutexLock lock(fd->mutex);
  2155. Vector2i size = _get_size_outline(fd, p_size);
  2156. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), -1);
  2157. if (!_ensure_glyph(fd, size, p_glyph)) {
  2158. return -1; // Invalid or non graphicl glyph, do not display errors.
  2159. }
  2160. const HashMap<int32_t, FontGlyph> &gl = fd->cache[size]->glyph_map;
  2161. return gl[p_glyph].texture_idx;
  2162. }
  2163. 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) {
  2164. FontDataAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2165. ERR_FAIL_COND(!fd);
  2166. MutexLock lock(fd->mutex);
  2167. Vector2i size = _get_size_outline(fd, p_size);
  2168. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  2169. HashMap<int32_t, FontGlyph> &gl = fd->cache[size]->glyph_map;
  2170. gl[p_glyph].texture_idx = p_texture_idx;
  2171. gl[p_glyph].found = true;
  2172. }
  2173. RID TextServerAdvanced::font_get_glyph_texture_rid(const RID &p_font_rid, const Vector2i &p_size, int64_t p_glyph) const {
  2174. FontDataAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2175. ERR_FAIL_COND_V(!fd, RID());
  2176. MutexLock lock(fd->mutex);
  2177. Vector2i size = _get_size_outline(fd, p_size);
  2178. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), RID());
  2179. if (!_ensure_glyph(fd, size, p_glyph)) {
  2180. return RID(); // Invalid or non graphicl glyph, do not display errors.
  2181. }
  2182. const HashMap<int32_t, FontGlyph> &gl = fd->cache[size]->glyph_map;
  2183. ERR_FAIL_COND_V(gl[p_glyph].texture_idx < -1 || gl[p_glyph].texture_idx >= fd->cache[size]->textures.size(), RID());
  2184. if (RenderingServer::get_singleton() != nullptr) {
  2185. if (gl[p_glyph].texture_idx != -1) {
  2186. if (fd->cache[size]->textures[gl[p_glyph].texture_idx].dirty) {
  2187. FontTexture &tex = fd->cache[size]->textures.write[gl[p_glyph].texture_idx];
  2188. Ref<Image> img;
  2189. img.instantiate();
  2190. img->create_from_data(tex.texture_w, tex.texture_h, false, tex.format, tex.imgdata);
  2191. if (fd->mipmaps) {
  2192. img->generate_mipmaps();
  2193. }
  2194. if (tex.texture.is_null()) {
  2195. tex.texture.instantiate();
  2196. tex.texture->create_from_image(img);
  2197. } else {
  2198. tex.texture->update(img);
  2199. }
  2200. tex.dirty = false;
  2201. }
  2202. return fd->cache[size]->textures[gl[p_glyph].texture_idx].texture->get_rid();
  2203. }
  2204. }
  2205. return RID();
  2206. }
  2207. Size2 TextServerAdvanced::font_get_glyph_texture_size(const RID &p_font_rid, const Vector2i &p_size, int64_t p_glyph) const {
  2208. FontDataAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2209. ERR_FAIL_COND_V(!fd, Size2());
  2210. MutexLock lock(fd->mutex);
  2211. Vector2i size = _get_size_outline(fd, p_size);
  2212. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), Size2());
  2213. if (!_ensure_glyph(fd, size, p_glyph)) {
  2214. return Size2(); // Invalid or non graphicl glyph, do not display errors.
  2215. }
  2216. const HashMap<int32_t, FontGlyph> &gl = fd->cache[size]->glyph_map;
  2217. ERR_FAIL_COND_V(gl[p_glyph].texture_idx < -1 || gl[p_glyph].texture_idx >= fd->cache[size]->textures.size(), Size2());
  2218. if (RenderingServer::get_singleton() != nullptr) {
  2219. if (gl[p_glyph].texture_idx != -1) {
  2220. if (fd->cache[size]->textures[gl[p_glyph].texture_idx].dirty) {
  2221. FontTexture &tex = fd->cache[size]->textures.write[gl[p_glyph].texture_idx];
  2222. Ref<Image> img;
  2223. img.instantiate();
  2224. img->create_from_data(tex.texture_w, tex.texture_h, false, tex.format, tex.imgdata);
  2225. if (fd->mipmaps) {
  2226. img->generate_mipmaps();
  2227. }
  2228. if (tex.texture.is_null()) {
  2229. tex.texture.instantiate();
  2230. tex.texture->create_from_image(img);
  2231. } else {
  2232. tex.texture->update(img);
  2233. }
  2234. tex.dirty = false;
  2235. }
  2236. return fd->cache[size]->textures[gl[p_glyph].texture_idx].texture->get_size();
  2237. }
  2238. }
  2239. return Size2();
  2240. }
  2241. Dictionary TextServerAdvanced::font_get_glyph_contours(const RID &p_font_rid, int64_t p_size, int64_t p_index) const {
  2242. FontDataAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2243. ERR_FAIL_COND_V(!fd, Dictionary());
  2244. MutexLock lock(fd->mutex);
  2245. Vector2i size = _get_size(fd, p_size);
  2246. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), Dictionary());
  2247. #ifdef MODULE_FREETYPE_ENABLED
  2248. PackedVector3Array points;
  2249. PackedInt32Array contours;
  2250. int32_t index = p_index & 0xffffff; // Remove subpixel shifts.
  2251. int error = FT_Load_Glyph(fd->cache[size]->face, index, FT_LOAD_NO_BITMAP | (fd->force_autohinter ? FT_LOAD_FORCE_AUTOHINT : 0));
  2252. ERR_FAIL_COND_V(error, Dictionary());
  2253. if (fd->embolden != 0.f) {
  2254. FT_Pos strength = fd->embolden * p_size * 4; // 26.6 fractional units (1 / 64).
  2255. FT_Outline_Embolden(&fd->cache[size]->face->glyph->outline, strength);
  2256. }
  2257. if (fd->transform != Transform2D()) {
  2258. 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).
  2259. FT_Outline_Transform(&fd->cache[size]->face->glyph->outline, &mat);
  2260. }
  2261. double scale = (1.0 / 64.0) / fd->cache[size]->oversampling * fd->cache[size]->scale;
  2262. if (fd->msdf) {
  2263. scale = scale * (double)p_size / (double)fd->msdf_source_size;
  2264. }
  2265. for (short i = 0; i < fd->cache[size]->face->glyph->outline.n_points; i++) {
  2266. points.push_back(Vector3(fd->cache[size]->face->glyph->outline.points[i].x * scale, -fd->cache[size]->face->glyph->outline.points[i].y * scale, FT_CURVE_TAG(fd->cache[size]->face->glyph->outline.tags[i])));
  2267. }
  2268. for (short i = 0; i < fd->cache[size]->face->glyph->outline.n_contours; i++) {
  2269. contours.push_back(fd->cache[size]->face->glyph->outline.contours[i]);
  2270. }
  2271. bool orientation = (FT_Outline_Get_Orientation(&fd->cache[size]->face->glyph->outline) == FT_ORIENTATION_FILL_RIGHT);
  2272. Dictionary out;
  2273. out["points"] = points;
  2274. out["contours"] = contours;
  2275. out["orientation"] = orientation;
  2276. return out;
  2277. #else
  2278. return Dictionary();
  2279. #endif
  2280. }
  2281. Array TextServerAdvanced::font_get_kerning_list(const RID &p_font_rid, int64_t p_size) const {
  2282. FontDataAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2283. ERR_FAIL_COND_V(!fd, Array());
  2284. MutexLock lock(fd->mutex);
  2285. Vector2i size = _get_size(fd, p_size);
  2286. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), Array());
  2287. Array ret;
  2288. for (const KeyValue<Vector2i, FontDataForSizeAdvanced *> &E : fd->cache) {
  2289. ret.push_back(E.key);
  2290. }
  2291. return ret;
  2292. }
  2293. void TextServerAdvanced::font_clear_kerning_map(const RID &p_font_rid, int64_t p_size) {
  2294. FontDataAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2295. ERR_FAIL_COND(!fd);
  2296. MutexLock lock(fd->mutex);
  2297. Vector2i size = _get_size(fd, p_size);
  2298. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  2299. fd->cache[size]->kerning_map.clear();
  2300. }
  2301. void TextServerAdvanced::font_remove_kerning(const RID &p_font_rid, int64_t p_size, const Vector2i &p_glyph_pair) {
  2302. FontDataAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2303. ERR_FAIL_COND(!fd);
  2304. MutexLock lock(fd->mutex);
  2305. Vector2i size = _get_size(fd, p_size);
  2306. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  2307. fd->cache[size]->kerning_map.erase(p_glyph_pair);
  2308. }
  2309. void TextServerAdvanced::font_set_kerning(const RID &p_font_rid, int64_t p_size, const Vector2i &p_glyph_pair, const Vector2 &p_kerning) {
  2310. FontDataAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2311. ERR_FAIL_COND(!fd);
  2312. MutexLock lock(fd->mutex);
  2313. Vector2i size = _get_size(fd, p_size);
  2314. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  2315. fd->cache[size]->kerning_map[p_glyph_pair] = p_kerning;
  2316. }
  2317. Vector2 TextServerAdvanced::font_get_kerning(const RID &p_font_rid, int64_t p_size, const Vector2i &p_glyph_pair) const {
  2318. FontDataAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2319. ERR_FAIL_COND_V(!fd, Vector2());
  2320. MutexLock lock(fd->mutex);
  2321. Vector2i size = _get_size(fd, p_size);
  2322. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), Vector2());
  2323. const HashMap<Vector2i, Vector2, VariantHasher, VariantComparator> &kern = fd->cache[size]->kerning_map;
  2324. if (kern.has(p_glyph_pair)) {
  2325. if (fd->msdf) {
  2326. return kern[p_glyph_pair] * (double)p_size / (double)fd->msdf_source_size;
  2327. } else {
  2328. return kern[p_glyph_pair];
  2329. }
  2330. } else {
  2331. #ifdef MODULE_FREETYPE_ENABLED
  2332. if (fd->cache[size]->face) {
  2333. FT_Vector delta;
  2334. FT_Get_Kerning(fd->cache[size]->face, p_glyph_pair.x, p_glyph_pair.y, FT_KERNING_DEFAULT, &delta);
  2335. if (fd->msdf) {
  2336. return Vector2(delta.x, delta.y) * (double)p_size / (double)fd->msdf_source_size;
  2337. } else {
  2338. return Vector2(delta.x, delta.y);
  2339. }
  2340. }
  2341. #endif
  2342. }
  2343. return Vector2();
  2344. }
  2345. 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 {
  2346. FontDataAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2347. ERR_FAIL_COND_V(!fd, 0);
  2348. 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) + ".");
  2349. 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) + ".");
  2350. MutexLock lock(fd->mutex);
  2351. Vector2i size = _get_size(fd, p_size);
  2352. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), 0);
  2353. #ifdef MODULE_FREETYPE_ENABLED
  2354. if (fd->cache[size]->face) {
  2355. if (p_variation_selector) {
  2356. return FT_Face_GetCharVariantIndex(fd->cache[size]->face, p_char, p_variation_selector);
  2357. } else {
  2358. return FT_Get_Char_Index(fd->cache[size]->face, p_char);
  2359. }
  2360. } else {
  2361. return (int64_t)p_char;
  2362. }
  2363. #else
  2364. return (int64_t)p_char;
  2365. #endif
  2366. }
  2367. bool TextServerAdvanced::font_has_char(const RID &p_font_rid, int64_t p_char) const {
  2368. FontDataAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2369. ERR_FAIL_COND_V(!fd, false);
  2370. 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) + ".");
  2371. MutexLock lock(fd->mutex);
  2372. if (fd->cache.is_empty()) {
  2373. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, fd->msdf ? Vector2i(fd->msdf_source_size, 0) : Vector2i(16, 0)), false);
  2374. }
  2375. FontDataForSizeAdvanced *at_size = fd->cache.begin()->value;
  2376. #ifdef MODULE_FREETYPE_ENABLED
  2377. if (at_size && at_size->face) {
  2378. return FT_Get_Char_Index(at_size->face, p_char) != 0;
  2379. }
  2380. #endif
  2381. return (at_size) ? at_size->glyph_map.has((int32_t)p_char) : false;
  2382. }
  2383. String TextServerAdvanced::font_get_supported_chars(const RID &p_font_rid) const {
  2384. FontDataAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2385. ERR_FAIL_COND_V(!fd, String());
  2386. MutexLock lock(fd->mutex);
  2387. if (fd->cache.is_empty()) {
  2388. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, fd->msdf ? Vector2i(fd->msdf_source_size, 0) : Vector2i(16, 0)), String());
  2389. }
  2390. FontDataForSizeAdvanced *at_size = fd->cache.begin()->value;
  2391. String chars;
  2392. #ifdef MODULE_FREETYPE_ENABLED
  2393. if (at_size && at_size->face) {
  2394. FT_UInt gindex;
  2395. FT_ULong charcode = FT_Get_First_Char(at_size->face, &gindex);
  2396. while (gindex != 0) {
  2397. if (charcode != 0) {
  2398. chars = chars + String::chr(charcode);
  2399. }
  2400. charcode = FT_Get_Next_Char(at_size->face, charcode, &gindex);
  2401. }
  2402. return chars;
  2403. }
  2404. #endif
  2405. if (at_size) {
  2406. const HashMap<int32_t, FontGlyph> &gl = at_size->glyph_map;
  2407. for (const KeyValue<int32_t, FontGlyph> &E : gl) {
  2408. chars = chars + String::chr(E.key);
  2409. }
  2410. }
  2411. return chars;
  2412. }
  2413. void TextServerAdvanced::font_render_range(const RID &p_font_rid, const Vector2i &p_size, int64_t p_start, int64_t p_end) {
  2414. FontDataAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2415. ERR_FAIL_COND(!fd);
  2416. 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) + ".");
  2417. 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) + ".");
  2418. MutexLock lock(fd->mutex);
  2419. Vector2i size = _get_size_outline(fd, p_size);
  2420. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  2421. for (int64_t i = p_start; i <= p_end; i++) {
  2422. #ifdef MODULE_FREETYPE_ENABLED
  2423. int32_t idx = FT_Get_Char_Index(fd->cache[size]->face, i);
  2424. if (fd->cache[size]->face) {
  2425. if (fd->msdf) {
  2426. _ensure_glyph(fd, size, (int32_t)idx);
  2427. } else {
  2428. if ((fd->subpixel_positioning == SUBPIXEL_POSITIONING_ONE_QUARTER) || (fd->subpixel_positioning == SUBPIXEL_POSITIONING_AUTO && size.x <= SUBPIXEL_POSITIONING_ONE_QUARTER_MAX_SIZE)) {
  2429. _ensure_glyph(fd, size, (int32_t)idx | (0 << 27));
  2430. _ensure_glyph(fd, size, (int32_t)idx | (1 << 27));
  2431. _ensure_glyph(fd, size, (int32_t)idx | (2 << 27));
  2432. _ensure_glyph(fd, size, (int32_t)idx | (3 << 27));
  2433. } else if ((fd->subpixel_positioning == SUBPIXEL_POSITIONING_ONE_HALF) || (fd->subpixel_positioning == SUBPIXEL_POSITIONING_AUTO && size.x <= SUBPIXEL_POSITIONING_ONE_HALF_MAX_SIZE)) {
  2434. _ensure_glyph(fd, size, (int32_t)idx | (1 << 27));
  2435. _ensure_glyph(fd, size, (int32_t)idx | (0 << 27));
  2436. } else {
  2437. _ensure_glyph(fd, size, (int32_t)idx);
  2438. }
  2439. }
  2440. }
  2441. #endif
  2442. }
  2443. }
  2444. void TextServerAdvanced::font_render_glyph(const RID &p_font_rid, const Vector2i &p_size, int64_t p_index) {
  2445. FontDataAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2446. ERR_FAIL_COND(!fd);
  2447. MutexLock lock(fd->mutex);
  2448. Vector2i size = _get_size_outline(fd, p_size);
  2449. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  2450. #ifdef MODULE_FREETYPE_ENABLED
  2451. int32_t idx = p_index & 0xffffff; // Remove subpixel shifts.
  2452. if (fd->cache[size]->face) {
  2453. if (fd->msdf) {
  2454. _ensure_glyph(fd, size, (int32_t)idx);
  2455. } else {
  2456. if ((fd->subpixel_positioning == SUBPIXEL_POSITIONING_ONE_QUARTER) || (fd->subpixel_positioning == SUBPIXEL_POSITIONING_AUTO && size.x <= SUBPIXEL_POSITIONING_ONE_QUARTER_MAX_SIZE)) {
  2457. _ensure_glyph(fd, size, (int32_t)idx | (0 << 27));
  2458. _ensure_glyph(fd, size, (int32_t)idx | (1 << 27));
  2459. _ensure_glyph(fd, size, (int32_t)idx | (2 << 27));
  2460. _ensure_glyph(fd, size, (int32_t)idx | (3 << 27));
  2461. } else if ((fd->subpixel_positioning == SUBPIXEL_POSITIONING_ONE_HALF) || (fd->subpixel_positioning == SUBPIXEL_POSITIONING_AUTO && size.x <= SUBPIXEL_POSITIONING_ONE_HALF_MAX_SIZE)) {
  2462. _ensure_glyph(fd, size, (int32_t)idx | (1 << 27));
  2463. _ensure_glyph(fd, size, (int32_t)idx | (0 << 27));
  2464. } else {
  2465. _ensure_glyph(fd, size, (int32_t)idx);
  2466. }
  2467. }
  2468. }
  2469. #endif
  2470. }
  2471. void TextServerAdvanced::font_draw_glyph(const RID &p_font_rid, const RID &p_canvas, int64_t p_size, const Vector2 &p_pos, int64_t p_index, const Color &p_color) const {
  2472. FontDataAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2473. ERR_FAIL_COND(!fd);
  2474. MutexLock lock(fd->mutex);
  2475. Vector2i size = _get_size(fd, p_size);
  2476. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  2477. int32_t index = p_index & 0xffffff; // Remove subpixel shifts.
  2478. #ifdef MODULE_FREETYPE_ENABLED
  2479. if (!fd->msdf && fd->cache[size]->face) {
  2480. if ((fd->subpixel_positioning == SUBPIXEL_POSITIONING_ONE_QUARTER) || (fd->subpixel_positioning == SUBPIXEL_POSITIONING_AUTO && size.x <= SUBPIXEL_POSITIONING_ONE_QUARTER_MAX_SIZE)) {
  2481. int xshift = (int)(Math::floor(4 * (p_pos.x + 0.125)) - 4 * Math::floor(p_pos.x + 0.125));
  2482. index = index | (xshift << 27);
  2483. } else if ((fd->subpixel_positioning == SUBPIXEL_POSITIONING_ONE_HALF) || (fd->subpixel_positioning == SUBPIXEL_POSITIONING_AUTO && size.x <= SUBPIXEL_POSITIONING_ONE_HALF_MAX_SIZE)) {
  2484. int xshift = (int)(Math::floor(2 * (p_pos.x + 0.25)) - 2 * Math::floor(p_pos.x + 0.25));
  2485. index = index | (xshift << 27);
  2486. }
  2487. }
  2488. #endif
  2489. if (!_ensure_glyph(fd, size, index)) {
  2490. return; // Invalid or non-graphical glyph, do not display errors, nothing to draw.
  2491. }
  2492. const FontGlyph &gl = fd->cache[size]->glyph_map[index];
  2493. if (gl.found) {
  2494. ERR_FAIL_COND(gl.texture_idx < -1 || gl.texture_idx >= fd->cache[size]->textures.size());
  2495. if (gl.texture_idx != -1) {
  2496. Color modulate = p_color;
  2497. #ifdef MODULE_FREETYPE_ENABLED
  2498. if (fd->cache[size]->face && FT_HAS_COLOR(fd->cache[size]->face)) {
  2499. modulate.r = modulate.g = modulate.b = 1.0;
  2500. }
  2501. #endif
  2502. if (RenderingServer::get_singleton() != nullptr) {
  2503. if (fd->cache[size]->textures[gl.texture_idx].dirty) {
  2504. FontTexture &tex = fd->cache[size]->textures.write[gl.texture_idx];
  2505. Ref<Image> img;
  2506. img.instantiate();
  2507. img->create_from_data(tex.texture_w, tex.texture_h, false, tex.format, tex.imgdata);
  2508. if (fd->mipmaps) {
  2509. img->generate_mipmaps();
  2510. }
  2511. if (tex.texture.is_null()) {
  2512. tex.texture.instantiate();
  2513. tex.texture->create_from_image(img);
  2514. } else {
  2515. tex.texture->update(img);
  2516. }
  2517. tex.dirty = false;
  2518. }
  2519. RID texture = fd->cache[size]->textures[gl.texture_idx].texture->get_rid();
  2520. if (fd->msdf) {
  2521. Point2 cpos = p_pos;
  2522. cpos += gl.rect.position * (double)p_size / (double)fd->msdf_source_size;
  2523. Size2 csize = gl.rect.size * (double)p_size / (double)fd->msdf_source_size;
  2524. RenderingServer::get_singleton()->canvas_item_add_msdf_texture_rect_region(p_canvas, Rect2(cpos, csize), texture, gl.uv_rect, modulate, 0, fd->msdf_range);
  2525. } else {
  2526. Point2 cpos = p_pos;
  2527. cpos.y = Math::floor(cpos.y);
  2528. if ((fd->subpixel_positioning == SUBPIXEL_POSITIONING_ONE_QUARTER) || (fd->subpixel_positioning == SUBPIXEL_POSITIONING_AUTO && size.x <= SUBPIXEL_POSITIONING_ONE_QUARTER_MAX_SIZE)) {
  2529. cpos.x = ((int)Math::floor(cpos.x + 0.125));
  2530. } else if ((fd->subpixel_positioning == SUBPIXEL_POSITIONING_ONE_HALF) || (fd->subpixel_positioning == SUBPIXEL_POSITIONING_AUTO && size.x <= SUBPIXEL_POSITIONING_ONE_HALF_MAX_SIZE)) {
  2531. cpos.x = ((int)Math::floor(cpos.x + 0.25));
  2532. } else {
  2533. cpos.x = Math::floor(cpos.x);
  2534. }
  2535. cpos += gl.rect.position;
  2536. Size2 csize = gl.rect.size;
  2537. RenderingServer::get_singleton()->canvas_item_add_texture_rect_region(p_canvas, Rect2(cpos, csize), texture, gl.uv_rect, modulate, false, false);
  2538. }
  2539. }
  2540. }
  2541. }
  2542. }
  2543. void TextServerAdvanced::font_draw_glyph_outline(const RID &p_font_rid, const RID &p_canvas, int64_t p_size, int64_t p_outline_size, const Vector2 &p_pos, int64_t p_index, const Color &p_color) const {
  2544. FontDataAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2545. ERR_FAIL_COND(!fd);
  2546. MutexLock lock(fd->mutex);
  2547. Vector2i size = _get_size_outline(fd, Vector2i(p_size, p_outline_size));
  2548. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  2549. int32_t index = p_index & 0xffffff; // Remove subpixel shifts.
  2550. #ifdef MODULE_FREETYPE_ENABLED
  2551. if (!fd->msdf && fd->cache[size]->face) {
  2552. if ((fd->subpixel_positioning == SUBPIXEL_POSITIONING_ONE_QUARTER) || (fd->subpixel_positioning == SUBPIXEL_POSITIONING_AUTO && size.x <= SUBPIXEL_POSITIONING_ONE_QUARTER_MAX_SIZE)) {
  2553. int xshift = (int)(Math::floor(4 * (p_pos.x + 0.125)) - 4 * Math::floor(p_pos.x + 0.125));
  2554. index = index | (xshift << 27);
  2555. } else if ((fd->subpixel_positioning == SUBPIXEL_POSITIONING_ONE_HALF) || (fd->subpixel_positioning == SUBPIXEL_POSITIONING_AUTO && size.x <= SUBPIXEL_POSITIONING_ONE_HALF_MAX_SIZE)) {
  2556. int xshift = (int)(Math::floor(2 * (p_pos.x + 0.25)) - 2 * Math::floor(p_pos.x + 0.25));
  2557. index = index | (xshift << 27);
  2558. }
  2559. }
  2560. #endif
  2561. if (!_ensure_glyph(fd, size, index)) {
  2562. return; // Invalid or non-graphical glyph, do not display errors, nothing to draw.
  2563. }
  2564. const FontGlyph &gl = fd->cache[size]->glyph_map[index];
  2565. if (gl.found) {
  2566. ERR_FAIL_COND(gl.texture_idx < -1 || gl.texture_idx >= fd->cache[size]->textures.size());
  2567. if (gl.texture_idx != -1) {
  2568. Color modulate = p_color;
  2569. #ifdef MODULE_FREETYPE_ENABLED
  2570. if (fd->cache[size]->face && FT_HAS_COLOR(fd->cache[size]->face)) {
  2571. modulate.r = modulate.g = modulate.b = 1.0;
  2572. }
  2573. #endif
  2574. if (RenderingServer::get_singleton() != nullptr) {
  2575. if (fd->cache[size]->textures[gl.texture_idx].dirty) {
  2576. FontTexture &tex = fd->cache[size]->textures.write[gl.texture_idx];
  2577. Ref<Image> img;
  2578. img.instantiate();
  2579. img->create_from_data(tex.texture_w, tex.texture_h, false, tex.format, tex.imgdata);
  2580. if (fd->mipmaps) {
  2581. img->generate_mipmaps();
  2582. }
  2583. if (tex.texture.is_null()) {
  2584. tex.texture.instantiate();
  2585. tex.texture->create_from_image(img);
  2586. } else {
  2587. tex.texture->update(img);
  2588. }
  2589. tex.dirty = false;
  2590. }
  2591. RID texture = fd->cache[size]->textures[gl.texture_idx].texture->get_rid();
  2592. if (fd->msdf) {
  2593. Point2 cpos = p_pos;
  2594. cpos += gl.rect.position * (double)p_size / (double)fd->msdf_source_size;
  2595. Size2 csize = gl.rect.size * (double)p_size / (double)fd->msdf_source_size;
  2596. RenderingServer::get_singleton()->canvas_item_add_msdf_texture_rect_region(p_canvas, Rect2(cpos, csize), texture, gl.uv_rect, modulate, p_outline_size * 2, fd->msdf_range);
  2597. } else {
  2598. Point2 cpos = p_pos;
  2599. cpos.y = Math::floor(cpos.y);
  2600. if ((fd->subpixel_positioning == SUBPIXEL_POSITIONING_ONE_QUARTER) || (fd->subpixel_positioning == SUBPIXEL_POSITIONING_AUTO && size.x <= SUBPIXEL_POSITIONING_ONE_QUARTER_MAX_SIZE)) {
  2601. cpos.x = ((int)Math::floor(cpos.x + 0.125));
  2602. } else if ((fd->subpixel_positioning == SUBPIXEL_POSITIONING_ONE_HALF) || (fd->subpixel_positioning == SUBPIXEL_POSITIONING_AUTO && size.x <= SUBPIXEL_POSITIONING_ONE_HALF_MAX_SIZE)) {
  2603. cpos.x = ((int)Math::floor(cpos.x + 0.25));
  2604. } else {
  2605. cpos.x = Math::floor(cpos.x);
  2606. }
  2607. cpos += gl.rect.position;
  2608. Size2 csize = gl.rect.size;
  2609. RenderingServer::get_singleton()->canvas_item_add_texture_rect_region(p_canvas, Rect2(cpos, csize), texture, gl.uv_rect, modulate, false, false);
  2610. }
  2611. }
  2612. }
  2613. }
  2614. }
  2615. bool TextServerAdvanced::font_is_language_supported(const RID &p_font_rid, const String &p_language) const {
  2616. FontDataAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2617. ERR_FAIL_COND_V(!fd, false);
  2618. MutexLock lock(fd->mutex);
  2619. if (fd->language_support_overrides.has(p_language)) {
  2620. return fd->language_support_overrides[p_language];
  2621. } else {
  2622. return true;
  2623. }
  2624. }
  2625. void TextServerAdvanced::font_set_language_support_override(const RID &p_font_rid, const String &p_language, bool p_supported) {
  2626. FontDataAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2627. ERR_FAIL_COND(!fd);
  2628. MutexLock lock(fd->mutex);
  2629. fd->language_support_overrides[p_language] = p_supported;
  2630. }
  2631. bool TextServerAdvanced::font_get_language_support_override(const RID &p_font_rid, const String &p_language) {
  2632. FontDataAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2633. ERR_FAIL_COND_V(!fd, false);
  2634. MutexLock lock(fd->mutex);
  2635. return fd->language_support_overrides[p_language];
  2636. }
  2637. void TextServerAdvanced::font_remove_language_support_override(const RID &p_font_rid, const String &p_language) {
  2638. FontDataAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2639. ERR_FAIL_COND(!fd);
  2640. MutexLock lock(fd->mutex);
  2641. fd->language_support_overrides.erase(p_language);
  2642. }
  2643. PackedStringArray TextServerAdvanced::font_get_language_support_overrides(const RID &p_font_rid) {
  2644. FontDataAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2645. ERR_FAIL_COND_V(!fd, PackedStringArray());
  2646. MutexLock lock(fd->mutex);
  2647. PackedStringArray out;
  2648. for (const KeyValue<String, bool> &E : fd->language_support_overrides) {
  2649. out.push_back(E.key);
  2650. }
  2651. return out;
  2652. }
  2653. bool TextServerAdvanced::font_is_script_supported(const RID &p_font_rid, const String &p_script) const {
  2654. FontDataAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2655. ERR_FAIL_COND_V(!fd, false);
  2656. MutexLock lock(fd->mutex);
  2657. if (fd->script_support_overrides.has(p_script)) {
  2658. return fd->script_support_overrides[p_script];
  2659. } else {
  2660. Vector2i size = _get_size(fd, 16);
  2661. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), false);
  2662. return fd->supported_scripts.has(hb_tag_from_string(p_script.ascii().get_data(), -1));
  2663. }
  2664. }
  2665. void TextServerAdvanced::font_set_script_support_override(const RID &p_font_rid, const String &p_script, bool p_supported) {
  2666. FontDataAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2667. ERR_FAIL_COND(!fd);
  2668. MutexLock lock(fd->mutex);
  2669. fd->script_support_overrides[p_script] = p_supported;
  2670. }
  2671. bool TextServerAdvanced::font_get_script_support_override(const RID &p_font_rid, const String &p_script) {
  2672. FontDataAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2673. ERR_FAIL_COND_V(!fd, false);
  2674. MutexLock lock(fd->mutex);
  2675. return fd->script_support_overrides[p_script];
  2676. }
  2677. void TextServerAdvanced::font_remove_script_support_override(const RID &p_font_rid, const String &p_script) {
  2678. FontDataAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2679. ERR_FAIL_COND(!fd);
  2680. MutexLock lock(fd->mutex);
  2681. fd->script_support_overrides.erase(p_script);
  2682. }
  2683. PackedStringArray TextServerAdvanced::font_get_script_support_overrides(const RID &p_font_rid) {
  2684. FontDataAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2685. ERR_FAIL_COND_V(!fd, PackedStringArray());
  2686. MutexLock lock(fd->mutex);
  2687. PackedStringArray out;
  2688. for (const KeyValue<String, bool> &E : fd->script_support_overrides) {
  2689. out.push_back(E.key);
  2690. }
  2691. return out;
  2692. }
  2693. void TextServerAdvanced::font_set_opentype_feature_overrides(const RID &p_font_rid, const Dictionary &p_overrides) {
  2694. FontDataAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2695. ERR_FAIL_COND(!fd);
  2696. MutexLock lock(fd->mutex);
  2697. Vector2i size = _get_size(fd, 16);
  2698. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  2699. fd->feature_overrides = p_overrides;
  2700. }
  2701. Dictionary TextServerAdvanced::font_get_opentype_feature_overrides(const RID &p_font_rid) const {
  2702. FontDataAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2703. ERR_FAIL_COND_V(!fd, Dictionary());
  2704. MutexLock lock(fd->mutex);
  2705. return fd->feature_overrides;
  2706. }
  2707. Dictionary TextServerAdvanced::font_supported_feature_list(const RID &p_font_rid) const {
  2708. FontDataAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2709. ERR_FAIL_COND_V(!fd, Dictionary());
  2710. MutexLock lock(fd->mutex);
  2711. Vector2i size = _get_size(fd, 16);
  2712. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), Dictionary());
  2713. return fd->supported_features;
  2714. }
  2715. Dictionary TextServerAdvanced::font_supported_variation_list(const RID &p_font_rid) const {
  2716. FontDataAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2717. ERR_FAIL_COND_V(!fd, Dictionary());
  2718. MutexLock lock(fd->mutex);
  2719. Vector2i size = _get_size(fd, 16);
  2720. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), Dictionary());
  2721. return fd->supported_varaitions;
  2722. }
  2723. double TextServerAdvanced::font_get_global_oversampling() const {
  2724. return oversampling;
  2725. }
  2726. void TextServerAdvanced::font_set_global_oversampling(double p_oversampling) {
  2727. _THREAD_SAFE_METHOD_
  2728. if (oversampling != p_oversampling) {
  2729. oversampling = p_oversampling;
  2730. List<RID> fonts;
  2731. font_owner.get_owned_list(&fonts);
  2732. bool font_cleared = false;
  2733. for (const RID &E : fonts) {
  2734. if (!font_is_multichannel_signed_distance_field(E) && font_get_oversampling(E) <= 0) {
  2735. font_clear_size_cache(E);
  2736. font_cleared = true;
  2737. }
  2738. }
  2739. if (font_cleared) {
  2740. List<RID> text_bufs;
  2741. shaped_owner.get_owned_list(&text_bufs);
  2742. for (const RID &E : text_bufs) {
  2743. invalidate(shaped_owner.get_or_null(E), false);
  2744. }
  2745. }
  2746. }
  2747. }
  2748. /*************************************************************************/
  2749. /* Shaped text buffer interface */
  2750. /*************************************************************************/
  2751. int64_t TextServerAdvanced::_convert_pos(const String &p_utf32, const Char16String &p_utf16, int64_t p_pos) const {
  2752. int64_t limit = p_pos;
  2753. if (p_utf32.length() != p_utf16.length()) {
  2754. const UChar *data = p_utf16.get_data();
  2755. for (int i = 0; i < p_pos; i++) {
  2756. if (U16_IS_LEAD(data[i])) {
  2757. limit--;
  2758. }
  2759. }
  2760. }
  2761. return limit;
  2762. }
  2763. int64_t TextServerAdvanced::_convert_pos(const ShapedTextDataAdvanced *p_sd, int64_t p_pos) const {
  2764. int64_t limit = p_pos;
  2765. if (p_sd->text.length() != p_sd->utf16.length()) {
  2766. const UChar *data = p_sd->utf16.get_data();
  2767. for (int i = 0; i < p_pos; i++) {
  2768. if (U16_IS_LEAD(data[i])) {
  2769. limit--;
  2770. }
  2771. }
  2772. }
  2773. return limit;
  2774. }
  2775. int64_t TextServerAdvanced::_convert_pos_inv(const ShapedTextDataAdvanced *p_sd, int64_t p_pos) const {
  2776. int64_t limit = p_pos;
  2777. if (p_sd->text.length() != p_sd->utf16.length()) {
  2778. for (int i = 0; i < p_pos; i++) {
  2779. if (p_sd->text[i] > 0xffff) {
  2780. limit++;
  2781. }
  2782. }
  2783. }
  2784. return limit;
  2785. }
  2786. void TextServerAdvanced::invalidate(TextServerAdvanced::ShapedTextDataAdvanced *p_shaped, bool p_text) {
  2787. p_shaped->valid = false;
  2788. p_shaped->sort_valid = false;
  2789. p_shaped->line_breaks_valid = false;
  2790. p_shaped->justification_ops_valid = false;
  2791. p_shaped->text_trimmed = false;
  2792. p_shaped->ascent = 0.0;
  2793. p_shaped->descent = 0.0;
  2794. p_shaped->width = 0.0;
  2795. p_shaped->upos = 0.0;
  2796. p_shaped->uthk = 0.0;
  2797. p_shaped->glyphs.clear();
  2798. p_shaped->glyphs_logical.clear();
  2799. p_shaped->overrun_trim_data = TrimData();
  2800. p_shaped->utf16 = Char16String();
  2801. for (int i = 0; i < p_shaped->bidi_iter.size(); i++) {
  2802. ubidi_close(p_shaped->bidi_iter[i]);
  2803. }
  2804. p_shaped->bidi_iter.clear();
  2805. if (p_text) {
  2806. if (p_shaped->script_iter != nullptr) {
  2807. memdelete(p_shaped->script_iter);
  2808. p_shaped->script_iter = nullptr;
  2809. }
  2810. p_shaped->break_ops_valid = false;
  2811. p_shaped->js_ops_valid = false;
  2812. }
  2813. }
  2814. void TextServerAdvanced::full_copy(ShapedTextDataAdvanced *p_shaped) {
  2815. ShapedTextDataAdvanced *parent = shaped_owner.get_or_null(p_shaped->parent);
  2816. for (const KeyValue<Variant, ShapedTextDataAdvanced::EmbeddedObject> &E : parent->objects) {
  2817. if (E.value.pos >= p_shaped->start && E.value.pos < p_shaped->end) {
  2818. p_shaped->objects[E.key] = E.value;
  2819. }
  2820. }
  2821. for (int i = 0; i < parent->spans.size(); i++) {
  2822. ShapedTextDataAdvanced::Span span = parent->spans[i];
  2823. if (span.start >= p_shaped->end || span.end <= p_shaped->start) {
  2824. continue;
  2825. }
  2826. span.start = MAX(p_shaped->start, span.start);
  2827. span.end = MIN(p_shaped->end, span.end);
  2828. p_shaped->spans.push_back(span);
  2829. }
  2830. p_shaped->parent = RID();
  2831. }
  2832. RID TextServerAdvanced::create_shaped_text(TextServer::Direction p_direction, TextServer::Orientation p_orientation) {
  2833. _THREAD_SAFE_METHOD_
  2834. ShapedTextDataAdvanced *sd = memnew(ShapedTextDataAdvanced);
  2835. sd->hb_buffer = hb_buffer_create();
  2836. sd->direction = p_direction;
  2837. sd->orientation = p_orientation;
  2838. return shaped_owner.make_rid(sd);
  2839. }
  2840. void TextServerAdvanced::shaped_text_clear(const RID &p_shaped) {
  2841. ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
  2842. ERR_FAIL_COND(!sd);
  2843. MutexLock lock(sd->mutex);
  2844. sd->parent = RID();
  2845. sd->start = 0;
  2846. sd->end = 0;
  2847. sd->text = String();
  2848. sd->spans.clear();
  2849. sd->objects.clear();
  2850. sd->bidi_override.clear();
  2851. invalidate(sd, true);
  2852. }
  2853. void TextServerAdvanced::shaped_text_set_direction(const RID &p_shaped, TextServer::Direction p_direction) {
  2854. ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
  2855. ERR_FAIL_COND(!sd);
  2856. MutexLock lock(sd->mutex);
  2857. if (sd->direction != p_direction) {
  2858. if (sd->parent != RID()) {
  2859. full_copy(sd);
  2860. }
  2861. sd->direction = p_direction;
  2862. invalidate(sd, false);
  2863. }
  2864. }
  2865. TextServer::Direction TextServerAdvanced::shaped_text_get_direction(const RID &p_shaped) const {
  2866. const ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
  2867. ERR_FAIL_COND_V(!sd, TextServer::DIRECTION_LTR);
  2868. MutexLock lock(sd->mutex);
  2869. return sd->direction;
  2870. }
  2871. TextServer::Direction TextServerAdvanced::shaped_text_get_inferred_direction(const RID &p_shaped) const {
  2872. const ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
  2873. ERR_FAIL_COND_V(!sd, TextServer::DIRECTION_LTR);
  2874. MutexLock lock(sd->mutex);
  2875. return sd->para_direction;
  2876. }
  2877. void TextServerAdvanced::shaped_text_set_custom_punctuation(const RID &p_shaped, const String &p_punct) {
  2878. _THREAD_SAFE_METHOD_
  2879. ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
  2880. ERR_FAIL_COND(!sd);
  2881. if (sd->custom_punct != p_punct) {
  2882. if (sd->parent != RID()) {
  2883. full_copy(sd);
  2884. }
  2885. sd->custom_punct = p_punct;
  2886. invalidate(sd, false);
  2887. }
  2888. }
  2889. String TextServerAdvanced::shaped_text_get_custom_punctuation(const RID &p_shaped) const {
  2890. _THREAD_SAFE_METHOD_
  2891. const ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
  2892. ERR_FAIL_COND_V(!sd, String());
  2893. return sd->custom_punct;
  2894. }
  2895. void TextServerAdvanced::shaped_text_set_bidi_override(const RID &p_shaped, const Array &p_override) {
  2896. ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
  2897. ERR_FAIL_COND(!sd);
  2898. MutexLock lock(sd->mutex);
  2899. if (sd->parent != RID()) {
  2900. full_copy(sd);
  2901. }
  2902. sd->bidi_override.clear();
  2903. for (int i = 0; i < p_override.size(); i++) {
  2904. if (p_override[i].get_type() == Variant::VECTOR2I) {
  2905. sd->bidi_override.push_back(p_override[i]);
  2906. }
  2907. }
  2908. invalidate(sd, false);
  2909. }
  2910. void TextServerAdvanced::shaped_text_set_orientation(const RID &p_shaped, TextServer::Orientation p_orientation) {
  2911. ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
  2912. ERR_FAIL_COND(!sd);
  2913. MutexLock lock(sd->mutex);
  2914. if (sd->orientation != p_orientation) {
  2915. if (sd->parent != RID()) {
  2916. full_copy(sd);
  2917. }
  2918. sd->orientation = p_orientation;
  2919. invalidate(sd, false);
  2920. }
  2921. }
  2922. void TextServerAdvanced::shaped_text_set_preserve_invalid(const RID &p_shaped, bool p_enabled) {
  2923. ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
  2924. ERR_FAIL_COND(!sd);
  2925. MutexLock lock(sd->mutex);
  2926. ERR_FAIL_COND(sd->parent != RID());
  2927. if (sd->preserve_invalid != p_enabled) {
  2928. sd->preserve_invalid = p_enabled;
  2929. invalidate(sd, false);
  2930. }
  2931. }
  2932. bool TextServerAdvanced::shaped_text_get_preserve_invalid(const RID &p_shaped) const {
  2933. const ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
  2934. ERR_FAIL_COND_V(!sd, false);
  2935. MutexLock lock(sd->mutex);
  2936. return sd->preserve_invalid;
  2937. }
  2938. void TextServerAdvanced::shaped_text_set_preserve_control(const RID &p_shaped, bool p_enabled) {
  2939. ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
  2940. ERR_FAIL_COND(!sd);
  2941. MutexLock lock(sd->mutex);
  2942. if (sd->preserve_control != p_enabled) {
  2943. if (sd->parent != RID()) {
  2944. full_copy(sd);
  2945. }
  2946. sd->preserve_control = p_enabled;
  2947. invalidate(sd, false);
  2948. }
  2949. }
  2950. bool TextServerAdvanced::shaped_text_get_preserve_control(const RID &p_shaped) const {
  2951. const ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
  2952. ERR_FAIL_COND_V(!sd, false);
  2953. MutexLock lock(sd->mutex);
  2954. return sd->preserve_control;
  2955. }
  2956. TextServer::Orientation TextServerAdvanced::shaped_text_get_orientation(const RID &p_shaped) const {
  2957. const ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
  2958. ERR_FAIL_COND_V(!sd, TextServer::ORIENTATION_HORIZONTAL);
  2959. MutexLock lock(sd->mutex);
  2960. return sd->orientation;
  2961. }
  2962. int64_t TextServerAdvanced::shaped_get_span_count(const RID &p_shaped) const {
  2963. ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
  2964. ERR_FAIL_COND_V(!sd, 0);
  2965. return sd->spans.size();
  2966. }
  2967. Variant TextServerAdvanced::shaped_get_span_meta(const RID &p_shaped, int64_t p_index) const {
  2968. ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
  2969. ERR_FAIL_COND_V(!sd, Variant());
  2970. ERR_FAIL_INDEX_V(p_index, sd->spans.size(), Variant());
  2971. return sd->spans[p_index].meta;
  2972. }
  2973. void TextServerAdvanced::shaped_set_span_update_font(const RID &p_shaped, int64_t p_index, const Array &p_fonts, int64_t p_size, const Dictionary &p_opentype_features) {
  2974. ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
  2975. ERR_FAIL_COND(!sd);
  2976. ERR_FAIL_INDEX(p_index, sd->spans.size());
  2977. ShapedTextDataAdvanced::Span &span = sd->spans.ptrw()[p_index];
  2978. bool changed = (span.font_size != p_size) || (span.features != p_opentype_features) || (p_fonts.size() != span.fonts.size());
  2979. if (!changed) {
  2980. for (int i = 0; i < p_fonts.size(); i++) {
  2981. changed = changed || (span.fonts[i] != p_fonts[i]);
  2982. }
  2983. }
  2984. if (changed) {
  2985. span.fonts = p_fonts;
  2986. span.font_size = p_size;
  2987. span.features = p_opentype_features;
  2988. invalidate(sd, false);
  2989. }
  2990. }
  2991. bool TextServerAdvanced::shaped_text_add_string(const RID &p_shaped, const String &p_text, const Array &p_fonts, int64_t p_size, const Dictionary &p_opentype_features, const String &p_language, const Variant &p_meta) {
  2992. ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
  2993. ERR_FAIL_COND_V(!sd, false);
  2994. ERR_FAIL_COND_V(p_size <= 0, false);
  2995. MutexLock lock(sd->mutex);
  2996. for (int i = 0; i < p_fonts.size(); i++) {
  2997. ERR_FAIL_COND_V(!font_owner.get_or_null(p_fonts[i]), false);
  2998. }
  2999. if (p_text.is_empty()) {
  3000. return true;
  3001. }
  3002. if (sd->parent != RID()) {
  3003. full_copy(sd);
  3004. }
  3005. ShapedTextDataAdvanced::Span span;
  3006. span.start = sd->text.length();
  3007. span.end = span.start + p_text.length();
  3008. span.fonts = p_fonts; // Do not pre-sort, spans will be divided to subruns later.
  3009. span.font_size = p_size;
  3010. span.language = p_language;
  3011. span.features = p_opentype_features;
  3012. span.meta = p_meta;
  3013. sd->spans.push_back(span);
  3014. sd->text = sd->text + p_text;
  3015. sd->end += p_text.length();
  3016. invalidate(sd, true);
  3017. return true;
  3018. }
  3019. 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) {
  3020. _THREAD_SAFE_METHOD_
  3021. ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
  3022. ERR_FAIL_COND_V(!sd, false);
  3023. ERR_FAIL_COND_V(p_key == Variant(), false);
  3024. ERR_FAIL_COND_V(sd->objects.has(p_key), false);
  3025. if (sd->parent != RID()) {
  3026. full_copy(sd);
  3027. }
  3028. ShapedTextDataAdvanced::Span span;
  3029. span.start = sd->start + sd->text.length();
  3030. span.end = span.start + p_length;
  3031. span.embedded_key = p_key;
  3032. ShapedTextDataAdvanced::EmbeddedObject obj;
  3033. obj.inline_align = p_inline_align;
  3034. obj.rect.size = p_size;
  3035. obj.pos = span.start;
  3036. sd->spans.push_back(span);
  3037. sd->text = sd->text + String::chr(0xfffc).repeat(p_length);
  3038. sd->end += p_length;
  3039. sd->objects[p_key] = obj;
  3040. invalidate(sd, true);
  3041. return true;
  3042. }
  3043. bool TextServerAdvanced::shaped_text_resize_object(const RID &p_shaped, const Variant &p_key, const Size2 &p_size, InlineAlignment p_inline_align) {
  3044. ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
  3045. ERR_FAIL_COND_V(!sd, false);
  3046. MutexLock lock(sd->mutex);
  3047. ERR_FAIL_COND_V(!sd->objects.has(p_key), false);
  3048. sd->objects[p_key].rect.size = p_size;
  3049. sd->objects[p_key].inline_align = p_inline_align;
  3050. if (sd->valid) {
  3051. // Recalc string metrics.
  3052. sd->ascent = 0;
  3053. sd->descent = 0;
  3054. sd->width = 0;
  3055. sd->upos = 0;
  3056. sd->uthk = 0;
  3057. int sd_size = sd->glyphs.size();
  3058. for (int i = 0; i < sd_size; i++) {
  3059. Glyph gl = sd->glyphs[i];
  3060. Variant key;
  3061. if (gl.count == 1) {
  3062. for (const KeyValue<Variant, ShapedTextDataAdvanced::EmbeddedObject> &E : sd->objects) {
  3063. if (E.value.pos == gl.start) {
  3064. key = E.key;
  3065. break;
  3066. }
  3067. }
  3068. }
  3069. if (key != Variant()) {
  3070. if (sd->orientation == ORIENTATION_HORIZONTAL) {
  3071. sd->objects[key].rect.position.x = sd->width;
  3072. sd->width += sd->objects[key].rect.size.x;
  3073. sd->glyphs.write[i].advance = sd->objects[key].rect.size.x;
  3074. } else {
  3075. sd->objects[key].rect.position.y = sd->width;
  3076. sd->width += sd->objects[key].rect.size.y;
  3077. sd->glyphs.write[i].advance = sd->objects[key].rect.size.y;
  3078. }
  3079. } else {
  3080. if (gl.font_rid.is_valid()) {
  3081. if (sd->orientation == ORIENTATION_HORIZONTAL) {
  3082. sd->ascent = MAX(sd->ascent, MAX(font_get_ascent(gl.font_rid, gl.font_size), -gl.y_off));
  3083. sd->descent = MAX(sd->descent, MAX(font_get_descent(gl.font_rid, gl.font_size), gl.y_off));
  3084. } else {
  3085. sd->ascent = MAX(sd->ascent, Math::round(font_get_glyph_advance(gl.font_rid, gl.font_size, gl.index).x * 0.5));
  3086. sd->descent = MAX(sd->descent, Math::round(font_get_glyph_advance(gl.font_rid, gl.font_size, gl.index).x * 0.5));
  3087. }
  3088. sd->upos = MAX(sd->upos, font_get_underline_position(gl.font_rid, gl.font_size));
  3089. sd->uthk = MAX(sd->uthk, font_get_underline_thickness(gl.font_rid, gl.font_size));
  3090. } else if (sd->preserve_invalid || (sd->preserve_control && is_control(gl.index))) {
  3091. // Glyph not found, replace with hex code box.
  3092. if (sd->orientation == ORIENTATION_HORIZONTAL) {
  3093. sd->ascent = MAX(sd->ascent, get_hex_code_box_size(gl.font_size, gl.index).y);
  3094. } else {
  3095. sd->ascent = MAX(sd->ascent, Math::round(get_hex_code_box_size(gl.font_size, gl.index).x * 0.5));
  3096. sd->descent = MAX(sd->descent, Math::round(get_hex_code_box_size(gl.font_size, gl.index).x * 0.5));
  3097. }
  3098. }
  3099. sd->width += gl.advance * gl.repeat;
  3100. }
  3101. }
  3102. _realign(sd);
  3103. }
  3104. return true;
  3105. }
  3106. void TextServerAdvanced::_realign(ShapedTextDataAdvanced *p_sd) const {
  3107. // Align embedded objects to baseline.
  3108. double full_ascent = p_sd->ascent;
  3109. double full_descent = p_sd->descent;
  3110. for (KeyValue<Variant, ShapedTextDataAdvanced::EmbeddedObject> &E : p_sd->objects) {
  3111. if ((E.value.pos >= p_sd->start) && (E.value.pos < p_sd->end)) {
  3112. if (p_sd->orientation == ORIENTATION_HORIZONTAL) {
  3113. switch (E.value.inline_align & INLINE_ALIGNMENT_TEXT_MASK) {
  3114. case INLINE_ALIGNMENT_TO_TOP: {
  3115. E.value.rect.position.y = -p_sd->ascent;
  3116. } break;
  3117. case INLINE_ALIGNMENT_TO_CENTER: {
  3118. E.value.rect.position.y = (-p_sd->ascent + p_sd->descent) / 2;
  3119. } break;
  3120. case INLINE_ALIGNMENT_TO_BASELINE: {
  3121. E.value.rect.position.y = 0;
  3122. } break;
  3123. case INLINE_ALIGNMENT_TO_BOTTOM: {
  3124. E.value.rect.position.y = p_sd->descent;
  3125. } break;
  3126. }
  3127. switch (E.value.inline_align & INLINE_ALIGNMENT_IMAGE_MASK) {
  3128. case INLINE_ALIGNMENT_BOTTOM_TO: {
  3129. E.value.rect.position.y -= E.value.rect.size.y;
  3130. } break;
  3131. case INLINE_ALIGNMENT_CENTER_TO: {
  3132. E.value.rect.position.y -= E.value.rect.size.y / 2;
  3133. } break;
  3134. case INLINE_ALIGNMENT_TOP_TO: {
  3135. // NOP
  3136. } break;
  3137. }
  3138. full_ascent = MAX(full_ascent, -E.value.rect.position.y);
  3139. full_descent = MAX(full_descent, E.value.rect.position.y + E.value.rect.size.y);
  3140. } else {
  3141. switch (E.value.inline_align & INLINE_ALIGNMENT_TEXT_MASK) {
  3142. case INLINE_ALIGNMENT_TO_TOP: {
  3143. E.value.rect.position.x = -p_sd->ascent;
  3144. } break;
  3145. case INLINE_ALIGNMENT_TO_CENTER: {
  3146. E.value.rect.position.x = (-p_sd->ascent + p_sd->descent) / 2;
  3147. } break;
  3148. case INLINE_ALIGNMENT_TO_BASELINE: {
  3149. E.value.rect.position.x = 0;
  3150. } break;
  3151. case INLINE_ALIGNMENT_TO_BOTTOM: {
  3152. E.value.rect.position.x = p_sd->descent;
  3153. } break;
  3154. }
  3155. switch (E.value.inline_align & INLINE_ALIGNMENT_IMAGE_MASK) {
  3156. case INLINE_ALIGNMENT_BOTTOM_TO: {
  3157. E.value.rect.position.x -= E.value.rect.size.x;
  3158. } break;
  3159. case INLINE_ALIGNMENT_CENTER_TO: {
  3160. E.value.rect.position.x -= E.value.rect.size.x / 2;
  3161. } break;
  3162. case INLINE_ALIGNMENT_TOP_TO: {
  3163. // NOP
  3164. } break;
  3165. }
  3166. full_ascent = MAX(full_ascent, -E.value.rect.position.x);
  3167. full_descent = MAX(full_descent, E.value.rect.position.x + E.value.rect.size.x);
  3168. }
  3169. }
  3170. }
  3171. p_sd->ascent = full_ascent;
  3172. p_sd->descent = full_descent;
  3173. }
  3174. RID TextServerAdvanced::shaped_text_substr(const RID &p_shaped, int64_t p_start, int64_t p_length) const {
  3175. _THREAD_SAFE_METHOD_
  3176. const ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
  3177. ERR_FAIL_COND_V(!sd, RID());
  3178. MutexLock lock(sd->mutex);
  3179. if (sd->parent != RID()) {
  3180. return shaped_text_substr(sd->parent, p_start, p_length);
  3181. }
  3182. if (!sd->valid) {
  3183. const_cast<TextServerAdvanced *>(this)->shaped_text_shape(p_shaped);
  3184. }
  3185. ERR_FAIL_COND_V(p_start < 0 || p_length < 0, RID());
  3186. ERR_FAIL_COND_V(sd->start > p_start || sd->end < p_start, RID());
  3187. ERR_FAIL_COND_V(sd->end < p_start + p_length, RID());
  3188. ShapedTextDataAdvanced *new_sd = memnew(ShapedTextDataAdvanced);
  3189. new_sd->parent = p_shaped;
  3190. new_sd->start = p_start;
  3191. new_sd->end = p_start + p_length;
  3192. new_sd->orientation = sd->orientation;
  3193. new_sd->direction = sd->direction;
  3194. new_sd->custom_punct = sd->custom_punct;
  3195. new_sd->para_direction = sd->para_direction;
  3196. if (!_shape_substr(new_sd, sd, p_start, p_length)) {
  3197. memdelete(new_sd);
  3198. return RID();
  3199. }
  3200. return shaped_owner.make_rid(new_sd);
  3201. }
  3202. bool TextServerAdvanced::_shape_substr(ShapedTextDataAdvanced *p_new_sd, const ShapedTextDataAdvanced *p_sd, int64_t p_start, int64_t p_length) const {
  3203. if (p_new_sd->valid) {
  3204. return true;
  3205. }
  3206. p_new_sd->hb_buffer = hb_buffer_create();
  3207. p_new_sd->line_breaks_valid = p_sd->line_breaks_valid;
  3208. p_new_sd->justification_ops_valid = p_sd->justification_ops_valid;
  3209. p_new_sd->sort_valid = false;
  3210. p_new_sd->upos = p_sd->upos;
  3211. p_new_sd->uthk = p_sd->uthk;
  3212. if (p_length > 0) {
  3213. p_new_sd->text = p_sd->text.substr(p_start - p_sd->start, p_length);
  3214. p_new_sd->utf16 = p_new_sd->text.utf16();
  3215. p_new_sd->script_iter = memnew(ScriptIterator(p_new_sd->text, 0, p_new_sd->text.length()));
  3216. int sd_size = p_sd->glyphs.size();
  3217. const Glyph *sd_glyphs = p_sd->glyphs.ptr();
  3218. for (int ov = 0; ov < p_sd->bidi_override.size(); ov++) {
  3219. UErrorCode err = U_ZERO_ERROR;
  3220. if (p_sd->bidi_override[ov].x >= p_start + p_length || p_sd->bidi_override[ov].y <= p_start) {
  3221. continue;
  3222. }
  3223. int start = _convert_pos_inv(p_sd, MAX(0, p_start - p_sd->bidi_override[ov].x));
  3224. int end = _convert_pos_inv(p_sd, MIN(p_start + p_length, p_sd->bidi_override[ov].y) - p_sd->bidi_override[ov].x);
  3225. ERR_FAIL_COND_V_MSG((start < 0 || end - start > p_new_sd->utf16.length()), false, "Invalid BiDi override range.");
  3226. // Create temporary line bidi & shape.
  3227. UBiDi *bidi_iter = ubidi_openSized(end - start, 0, &err);
  3228. ERR_FAIL_COND_V_MSG(U_FAILURE(err), false, u_errorName(err));
  3229. ubidi_setLine(p_sd->bidi_iter[ov], start, end, bidi_iter, &err);
  3230. if (U_FAILURE(err)) {
  3231. ubidi_close(bidi_iter);
  3232. ERR_FAIL_V_MSG(false, u_errorName(err));
  3233. }
  3234. p_new_sd->bidi_iter.push_back(bidi_iter);
  3235. err = U_ZERO_ERROR;
  3236. int bidi_run_count = ubidi_countRuns(bidi_iter, &err);
  3237. ERR_FAIL_COND_V_MSG(U_FAILURE(err), false, u_errorName(err));
  3238. for (int i = 0; i < bidi_run_count; i++) {
  3239. int32_t _bidi_run_start = 0;
  3240. int32_t _bidi_run_length = 0;
  3241. ubidi_getVisualRun(bidi_iter, i, &_bidi_run_start, &_bidi_run_length);
  3242. int32_t bidi_run_start = _convert_pos(p_sd, p_sd->bidi_override[ov].x + start + _bidi_run_start);
  3243. int32_t bidi_run_end = _convert_pos(p_sd, p_sd->bidi_override[ov].x + start + _bidi_run_start + _bidi_run_length);
  3244. for (int j = 0; j < sd_size; j++) {
  3245. if ((sd_glyphs[j].start >= bidi_run_start) && (sd_glyphs[j].end <= bidi_run_end)) {
  3246. // Copy glyphs.
  3247. Glyph gl = sd_glyphs[j];
  3248. Variant key;
  3249. bool find_embedded = false;
  3250. if (gl.count == 1) {
  3251. for (const KeyValue<Variant, ShapedTextDataAdvanced::EmbeddedObject> &E : p_sd->objects) {
  3252. if (E.value.pos == gl.start) {
  3253. find_embedded = true;
  3254. key = E.key;
  3255. p_new_sd->objects[key] = E.value;
  3256. break;
  3257. }
  3258. }
  3259. }
  3260. if (find_embedded) {
  3261. if (p_new_sd->orientation == ORIENTATION_HORIZONTAL) {
  3262. p_new_sd->objects[key].rect.position.x = p_new_sd->width;
  3263. p_new_sd->width += p_new_sd->objects[key].rect.size.x;
  3264. } else {
  3265. p_new_sd->objects[key].rect.position.y = p_new_sd->width;
  3266. p_new_sd->width += p_new_sd->objects[key].rect.size.y;
  3267. }
  3268. } else {
  3269. if (gl.font_rid.is_valid()) {
  3270. if (p_new_sd->orientation == ORIENTATION_HORIZONTAL) {
  3271. p_new_sd->ascent = MAX(p_new_sd->ascent, MAX(font_get_ascent(gl.font_rid, gl.font_size), -gl.y_off));
  3272. p_new_sd->descent = MAX(p_new_sd->descent, MAX(font_get_descent(gl.font_rid, gl.font_size), gl.y_off));
  3273. } else {
  3274. 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));
  3275. 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));
  3276. }
  3277. } else if (p_new_sd->preserve_invalid || (p_new_sd->preserve_control && is_control(gl.index))) {
  3278. // Glyph not found, replace with hex code box.
  3279. if (p_new_sd->orientation == ORIENTATION_HORIZONTAL) {
  3280. p_new_sd->ascent = MAX(p_new_sd->ascent, get_hex_code_box_size(gl.font_size, gl.index).y);
  3281. } else {
  3282. p_new_sd->ascent = MAX(p_new_sd->ascent, Math::round(get_hex_code_box_size(gl.font_size, gl.index).x * 0.5));
  3283. p_new_sd->descent = MAX(p_new_sd->descent, Math::round(get_hex_code_box_size(gl.font_size, gl.index).x * 0.5));
  3284. }
  3285. }
  3286. p_new_sd->width += gl.advance * gl.repeat;
  3287. }
  3288. p_new_sd->glyphs.push_back(gl);
  3289. }
  3290. }
  3291. }
  3292. }
  3293. _realign(p_new_sd);
  3294. }
  3295. p_new_sd->valid = true;
  3296. return true;
  3297. }
  3298. RID TextServerAdvanced::shaped_text_get_parent(const RID &p_shaped) const {
  3299. ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
  3300. ERR_FAIL_COND_V(!sd, RID());
  3301. MutexLock lock(sd->mutex);
  3302. return sd->parent;
  3303. }
  3304. double TextServerAdvanced::shaped_text_fit_to_width(const RID &p_shaped, double p_width, int64_t /*JustificationFlag*/ p_jst_flags) {
  3305. ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
  3306. ERR_FAIL_COND_V(!sd, 0.0);
  3307. MutexLock lock(sd->mutex);
  3308. if (!sd->valid) {
  3309. const_cast<TextServerAdvanced *>(this)->shaped_text_shape(p_shaped);
  3310. }
  3311. if (!sd->justification_ops_valid) {
  3312. const_cast<TextServerAdvanced *>(this)->shaped_text_update_justification_ops(p_shaped);
  3313. }
  3314. sd->fit_width_minimum_reached = false;
  3315. int start_pos = 0;
  3316. int end_pos = sd->glyphs.size() - 1;
  3317. if ((p_jst_flags & JUSTIFICATION_AFTER_LAST_TAB) == JUSTIFICATION_AFTER_LAST_TAB) {
  3318. int start, end, delta;
  3319. if (sd->para_direction == DIRECTION_LTR) {
  3320. start = sd->glyphs.size() - 1;
  3321. end = -1;
  3322. delta = -1;
  3323. } else {
  3324. start = 0;
  3325. end = sd->glyphs.size();
  3326. delta = +1;
  3327. }
  3328. for (int i = start; i != end; i += delta) {
  3329. if ((sd->glyphs[i].flags & GRAPHEME_IS_TAB) == GRAPHEME_IS_TAB) {
  3330. if (sd->para_direction == DIRECTION_LTR) {
  3331. start_pos = i;
  3332. break;
  3333. } else {
  3334. end_pos = i;
  3335. break;
  3336. }
  3337. }
  3338. }
  3339. }
  3340. double justification_width;
  3341. if ((p_jst_flags & JUSTIFICATION_CONSTRAIN_ELLIPSIS) == JUSTIFICATION_CONSTRAIN_ELLIPSIS) {
  3342. if (sd->overrun_trim_data.trim_pos >= 0) {
  3343. if (sd->para_direction == DIRECTION_RTL) {
  3344. start_pos = sd->overrun_trim_data.trim_pos;
  3345. } else {
  3346. end_pos = sd->overrun_trim_data.trim_pos;
  3347. }
  3348. justification_width = sd->width_trimmed;
  3349. } else {
  3350. return Math::ceil(sd->width);
  3351. }
  3352. } else {
  3353. justification_width = sd->width;
  3354. }
  3355. if ((p_jst_flags & JUSTIFICATION_TRIM_EDGE_SPACES) == JUSTIFICATION_TRIM_EDGE_SPACES) {
  3356. // Trim spaces.
  3357. while ((start_pos < end_pos) && ((sd->glyphs[start_pos].flags & GRAPHEME_IS_SPACE) == GRAPHEME_IS_SPACE || (sd->glyphs[start_pos].flags & GRAPHEME_IS_BREAK_HARD) == GRAPHEME_IS_BREAK_HARD || (sd->glyphs[start_pos].flags & GRAPHEME_IS_BREAK_SOFT) == GRAPHEME_IS_BREAK_SOFT)) {
  3358. justification_width -= sd->glyphs[start_pos].advance * sd->glyphs[start_pos].repeat;
  3359. sd->glyphs.write[start_pos].advance = 0;
  3360. start_pos += sd->glyphs[start_pos].count;
  3361. }
  3362. while ((start_pos < end_pos) && ((sd->glyphs[end_pos].flags & GRAPHEME_IS_SPACE) == GRAPHEME_IS_SPACE || (sd->glyphs[end_pos].flags & GRAPHEME_IS_BREAK_HARD) == GRAPHEME_IS_BREAK_HARD || (sd->glyphs[end_pos].flags & GRAPHEME_IS_BREAK_SOFT) == GRAPHEME_IS_BREAK_SOFT)) {
  3363. justification_width -= sd->glyphs[end_pos].advance * sd->glyphs[end_pos].repeat;
  3364. sd->glyphs.write[end_pos].advance = 0;
  3365. end_pos -= sd->glyphs[end_pos].count;
  3366. }
  3367. } else {
  3368. // Skip breaks, but do not reset size.
  3369. while ((start_pos < end_pos) && ((sd->glyphs[start_pos].flags & GRAPHEME_IS_BREAK_HARD) == GRAPHEME_IS_BREAK_HARD || (sd->glyphs[start_pos].flags & GRAPHEME_IS_BREAK_SOFT) == GRAPHEME_IS_BREAK_SOFT)) {
  3370. start_pos += sd->glyphs[start_pos].count;
  3371. }
  3372. while ((start_pos < end_pos) && ((sd->glyphs[end_pos].flags & GRAPHEME_IS_BREAK_HARD) == GRAPHEME_IS_BREAK_HARD || (sd->glyphs[end_pos].flags & GRAPHEME_IS_BREAK_SOFT) == GRAPHEME_IS_BREAK_SOFT)) {
  3373. end_pos -= sd->glyphs[end_pos].count;
  3374. }
  3375. }
  3376. int space_count = 0;
  3377. int elongation_count = 0;
  3378. for (int i = start_pos; i <= end_pos; i++) {
  3379. const Glyph &gl = sd->glyphs[i];
  3380. if (gl.count > 0) {
  3381. if ((gl.flags & GRAPHEME_IS_ELONGATION) == GRAPHEME_IS_ELONGATION) {
  3382. if ((i > 0) && ((sd->glyphs[i - 1].flags & GRAPHEME_IS_ELONGATION) != GRAPHEME_IS_ELONGATION)) {
  3383. // Expand once per elongation sequence.
  3384. elongation_count++;
  3385. }
  3386. }
  3387. if ((gl.flags & GRAPHEME_IS_SPACE) == GRAPHEME_IS_SPACE) {
  3388. space_count++;
  3389. }
  3390. }
  3391. }
  3392. if ((elongation_count > 0) && ((p_jst_flags & JUSTIFICATION_KASHIDA) == JUSTIFICATION_KASHIDA)) {
  3393. double delta_width_per_kashida = (p_width - justification_width) / elongation_count;
  3394. for (int i = start_pos; i <= end_pos; i++) {
  3395. Glyph &gl = sd->glyphs.write[i];
  3396. if (gl.count > 0) {
  3397. if (((gl.flags & GRAPHEME_IS_ELONGATION) == GRAPHEME_IS_ELONGATION) && (gl.advance > 0)) {
  3398. if ((i > 0) && ((sd->glyphs[i - 1].flags & GRAPHEME_IS_ELONGATION) != GRAPHEME_IS_ELONGATION)) {
  3399. // Expand once per elongation sequence.
  3400. int count = delta_width_per_kashida / gl.advance;
  3401. int prev_count = gl.repeat;
  3402. if ((gl.flags & GRAPHEME_IS_VIRTUAL) == GRAPHEME_IS_VIRTUAL) {
  3403. gl.repeat = MAX(count, 0);
  3404. } else {
  3405. gl.repeat = MAX(count + 1, 1);
  3406. }
  3407. justification_width += (gl.repeat - prev_count) * gl.advance;
  3408. }
  3409. }
  3410. }
  3411. }
  3412. }
  3413. if ((space_count > 0) && ((p_jst_flags & JUSTIFICATION_WORD_BOUND) == JUSTIFICATION_WORD_BOUND)) {
  3414. double delta_width_per_space = (p_width - justification_width) / space_count;
  3415. double adv_remain = 0;
  3416. for (int i = start_pos; i <= end_pos; i++) {
  3417. Glyph &gl = sd->glyphs.write[i];
  3418. if (gl.count > 0) {
  3419. if ((gl.flags & GRAPHEME_IS_SPACE) == GRAPHEME_IS_SPACE) {
  3420. double old_adv = gl.advance;
  3421. double new_advance;
  3422. if ((gl.flags & GRAPHEME_IS_VIRTUAL) == GRAPHEME_IS_VIRTUAL) {
  3423. new_advance = MAX(gl.advance + delta_width_per_space, 0.0);
  3424. } else {
  3425. new_advance = MAX(gl.advance + delta_width_per_space, 0.1 * gl.font_size);
  3426. }
  3427. gl.advance = new_advance;
  3428. adv_remain += (new_advance - gl.advance);
  3429. if (adv_remain >= 1.0) {
  3430. gl.advance++;
  3431. adv_remain -= 1.0;
  3432. } else if (adv_remain <= -1.0) {
  3433. gl.advance = MAX(gl.advance - 1, 0);
  3434. adv_remain -= 1.0;
  3435. }
  3436. justification_width += (gl.advance - old_adv);
  3437. }
  3438. }
  3439. }
  3440. }
  3441. if (Math::floor(p_width) < Math::floor(justification_width)) {
  3442. sd->fit_width_minimum_reached = true;
  3443. }
  3444. if ((p_jst_flags & JUSTIFICATION_CONSTRAIN_ELLIPSIS) != JUSTIFICATION_CONSTRAIN_ELLIPSIS) {
  3445. sd->width = justification_width;
  3446. }
  3447. return Math::ceil(justification_width);
  3448. }
  3449. double TextServerAdvanced::shaped_text_tab_align(const RID &p_shaped, const PackedFloat32Array &p_tab_stops) {
  3450. ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
  3451. ERR_FAIL_COND_V(!sd, 0.0);
  3452. MutexLock lock(sd->mutex);
  3453. if (!sd->valid) {
  3454. const_cast<TextServerAdvanced *>(this)->shaped_text_shape(p_shaped);
  3455. }
  3456. if (!sd->line_breaks_valid) {
  3457. const_cast<TextServerAdvanced *>(this)->shaped_text_update_breaks(p_shaped);
  3458. }
  3459. for (int i = 0; i < p_tab_stops.size(); i++) {
  3460. if (p_tab_stops[i] <= 0) {
  3461. return 0.0;
  3462. }
  3463. }
  3464. int tab_index = 0;
  3465. double off = 0.0;
  3466. int start, end, delta;
  3467. if (sd->para_direction == DIRECTION_LTR) {
  3468. start = 0;
  3469. end = sd->glyphs.size();
  3470. delta = +1;
  3471. } else {
  3472. start = sd->glyphs.size() - 1;
  3473. end = -1;
  3474. delta = -1;
  3475. }
  3476. Glyph *gl = sd->glyphs.ptrw();
  3477. for (int i = start; i != end; i += delta) {
  3478. if ((gl[i].flags & GRAPHEME_IS_TAB) == GRAPHEME_IS_TAB) {
  3479. double tab_off = 0.0;
  3480. while (tab_off <= off) {
  3481. tab_off += p_tab_stops[tab_index];
  3482. tab_index++;
  3483. if (tab_index >= p_tab_stops.size()) {
  3484. tab_index = 0;
  3485. }
  3486. }
  3487. double old_adv = gl[i].advance;
  3488. gl[i].advance = tab_off - off;
  3489. sd->width += gl[i].advance - old_adv;
  3490. off = 0;
  3491. continue;
  3492. }
  3493. off += gl[i].advance * gl[i].repeat;
  3494. }
  3495. return 0.0;
  3496. }
  3497. void TextServerAdvanced::shaped_text_overrun_trim_to_width(const RID &p_shaped_line, double p_width, int64_t p_trim_flags) {
  3498. ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped_line);
  3499. ERR_FAIL_COND_MSG(!sd, "ShapedTextDataAdvanced invalid.");
  3500. MutexLock lock(sd->mutex);
  3501. if (!sd->valid) {
  3502. shaped_text_shape(p_shaped_line);
  3503. }
  3504. sd->text_trimmed = false;
  3505. sd->overrun_trim_data.ellipsis_glyph_buf.clear();
  3506. bool add_ellipsis = (p_trim_flags & OVERRUN_ADD_ELLIPSIS) == OVERRUN_ADD_ELLIPSIS;
  3507. bool cut_per_word = (p_trim_flags & OVERRUN_TRIM_WORD_ONLY) == OVERRUN_TRIM_WORD_ONLY;
  3508. bool enforce_ellipsis = (p_trim_flags & OVERRUN_ENFORCE_ELLIPSIS) == OVERRUN_ENFORCE_ELLIPSIS;
  3509. bool justification_aware = (p_trim_flags & OVERRUN_JUSTIFICATION_AWARE) == OVERRUN_JUSTIFICATION_AWARE;
  3510. Glyph *sd_glyphs = sd->glyphs.ptrw();
  3511. if ((p_trim_flags & OVERRUN_TRIM) == OVERRUN_NO_TRIMMING || sd_glyphs == nullptr || p_width <= 0 || !(sd->width > p_width || enforce_ellipsis)) {
  3512. sd->overrun_trim_data.trim_pos = -1;
  3513. sd->overrun_trim_data.ellipsis_pos = -1;
  3514. return;
  3515. }
  3516. if (justification_aware && !sd->fit_width_minimum_reached) {
  3517. return;
  3518. }
  3519. Vector<ShapedTextDataAdvanced::Span> &spans = sd->spans;
  3520. if (sd->parent != RID()) {
  3521. ShapedTextDataAdvanced *parent_sd = shaped_owner.get_or_null(sd->parent);
  3522. ERR_FAIL_COND(!parent_sd->valid);
  3523. spans = parent_sd->spans;
  3524. }
  3525. if (spans.size() == 0) {
  3526. return;
  3527. }
  3528. int sd_size = sd->glyphs.size();
  3529. int last_gl_font_size = sd_glyphs[sd_size - 1].font_size;
  3530. // Find usable fonts, if fonts from the last glyph do not have required chars.
  3531. RID dot_gl_font_rid = sd_glyphs[sd_size - 1].font_rid;
  3532. if (!font_has_char(dot_gl_font_rid, '.')) {
  3533. const Array &fonts = spans[spans.size() - 1].fonts;
  3534. for (int i = 0; i < fonts.size(); i++) {
  3535. if (font_has_char(fonts[i], '.')) {
  3536. dot_gl_font_rid = fonts[i];
  3537. break;
  3538. }
  3539. }
  3540. }
  3541. RID whitespace_gl_font_rid = sd_glyphs[sd_size - 1].font_rid;
  3542. if (!font_has_char(whitespace_gl_font_rid, '.')) {
  3543. const Array &fonts = spans[spans.size() - 1].fonts;
  3544. for (int i = 0; i < fonts.size(); i++) {
  3545. if (font_has_char(fonts[i], ' ')) {
  3546. whitespace_gl_font_rid = fonts[i];
  3547. break;
  3548. }
  3549. }
  3550. }
  3551. int32_t dot_gl_idx = dot_gl_font_rid.is_valid() ? font_get_glyph_index(dot_gl_font_rid, last_gl_font_size, '.') : -10;
  3552. Vector2 dot_adv = dot_gl_font_rid.is_valid() ? font_get_glyph_advance(dot_gl_font_rid, last_gl_font_size, dot_gl_idx) : Vector2();
  3553. int32_t whitespace_gl_idx = whitespace_gl_font_rid.is_valid() ? font_get_glyph_index(whitespace_gl_font_rid, last_gl_font_size, ' ') : -10;
  3554. 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();
  3555. int ellipsis_width = 0;
  3556. if (add_ellipsis && whitespace_gl_font_rid.is_valid()) {
  3557. ellipsis_width = 3 * dot_adv.x + font_get_spacing(whitespace_gl_font_rid, last_gl_font_size, SPACING_GLYPH) + (cut_per_word ? whitespace_adv.x : 0);
  3558. }
  3559. int ell_min_characters = 6;
  3560. double width = sd->width;
  3561. bool is_rtl = sd->para_direction == DIRECTION_RTL;
  3562. int trim_pos = (is_rtl) ? sd_size : 0;
  3563. int ellipsis_pos = (enforce_ellipsis) ? 0 : -1;
  3564. int last_valid_cut = 0;
  3565. bool found = false;
  3566. int glyphs_from = (is_rtl) ? 0 : sd_size - 1;
  3567. int glyphs_to = (is_rtl) ? sd_size - 1 : -1;
  3568. int glyphs_delta = (is_rtl) ? +1 : -1;
  3569. for (int i = glyphs_from; i != glyphs_to; i += glyphs_delta) {
  3570. if (!is_rtl) {
  3571. width -= sd_glyphs[i].advance * sd_glyphs[i].repeat;
  3572. }
  3573. if (sd_glyphs[i].count > 0) {
  3574. bool above_min_char_threshold = ((is_rtl) ? sd_size - 1 - i : i) >= ell_min_characters;
  3575. if (width + (((above_min_char_threshold && add_ellipsis) || enforce_ellipsis) ? ellipsis_width : 0) <= p_width) {
  3576. if (cut_per_word && above_min_char_threshold) {
  3577. if ((sd_glyphs[i].flags & GRAPHEME_IS_BREAK_SOFT) == GRAPHEME_IS_BREAK_SOFT) {
  3578. last_valid_cut = i;
  3579. found = true;
  3580. }
  3581. } else {
  3582. last_valid_cut = i;
  3583. found = true;
  3584. }
  3585. if (found) {
  3586. trim_pos = last_valid_cut;
  3587. if (add_ellipsis && (above_min_char_threshold || enforce_ellipsis) && width - ellipsis_width <= p_width) {
  3588. ellipsis_pos = trim_pos;
  3589. }
  3590. break;
  3591. }
  3592. }
  3593. }
  3594. if (is_rtl) {
  3595. width -= sd_glyphs[i].advance * sd_glyphs[i].repeat;
  3596. }
  3597. }
  3598. sd->overrun_trim_data.trim_pos = trim_pos;
  3599. sd->overrun_trim_data.ellipsis_pos = ellipsis_pos;
  3600. if (trim_pos == 0 && enforce_ellipsis && add_ellipsis) {
  3601. sd->overrun_trim_data.ellipsis_pos = 0;
  3602. }
  3603. if ((trim_pos >= 0 && sd->width > p_width) || enforce_ellipsis) {
  3604. if (add_ellipsis && (ellipsis_pos > 0 || enforce_ellipsis)) {
  3605. // Insert an additional space when cutting word bound for aesthetics.
  3606. if (cut_per_word && (ellipsis_pos > 0)) {
  3607. Glyph gl;
  3608. gl.count = 1;
  3609. gl.advance = whitespace_adv.x;
  3610. gl.index = whitespace_gl_idx;
  3611. gl.font_rid = whitespace_gl_font_rid;
  3612. gl.font_size = last_gl_font_size;
  3613. gl.flags = GRAPHEME_IS_SPACE | GRAPHEME_IS_BREAK_SOFT | GRAPHEME_IS_VIRTUAL | (is_rtl ? GRAPHEME_IS_RTL : 0);
  3614. sd->overrun_trim_data.ellipsis_glyph_buf.append(gl);
  3615. }
  3616. // Add ellipsis dots.
  3617. if (dot_gl_idx != 0) {
  3618. Glyph gl;
  3619. gl.count = 1;
  3620. gl.repeat = 3;
  3621. gl.advance = dot_adv.x;
  3622. gl.index = dot_gl_idx;
  3623. gl.font_rid = dot_gl_font_rid;
  3624. gl.font_size = last_gl_font_size;
  3625. gl.flags = GRAPHEME_IS_PUNCTUATION | GRAPHEME_IS_VIRTUAL | (is_rtl ? GRAPHEME_IS_RTL : 0);
  3626. sd->overrun_trim_data.ellipsis_glyph_buf.append(gl);
  3627. }
  3628. }
  3629. sd->text_trimmed = true;
  3630. sd->width_trimmed = width + ((ellipsis_pos != -1) ? ellipsis_width : 0);
  3631. }
  3632. }
  3633. int64_t TextServerAdvanced::shaped_text_get_trim_pos(const RID &p_shaped) const {
  3634. ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
  3635. ERR_FAIL_COND_V_MSG(!sd, -1, "ShapedTextDataAdvanced invalid.");
  3636. MutexLock lock(sd->mutex);
  3637. return sd->overrun_trim_data.trim_pos;
  3638. }
  3639. int64_t TextServerAdvanced::shaped_text_get_ellipsis_pos(const RID &p_shaped) const {
  3640. ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
  3641. ERR_FAIL_COND_V_MSG(!sd, -1, "ShapedTextDataAdvanced invalid.");
  3642. MutexLock lock(sd->mutex);
  3643. return sd->overrun_trim_data.ellipsis_pos;
  3644. }
  3645. const Glyph *TextServerAdvanced::shaped_text_get_ellipsis_glyphs(const RID &p_shaped) const {
  3646. ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
  3647. ERR_FAIL_COND_V_MSG(!sd, nullptr, "ShapedTextDataAdvanced invalid.");
  3648. MutexLock lock(sd->mutex);
  3649. return sd->overrun_trim_data.ellipsis_glyph_buf.ptr();
  3650. }
  3651. int64_t TextServerAdvanced::shaped_text_get_ellipsis_glyph_count(const RID &p_shaped) const {
  3652. ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
  3653. ERR_FAIL_COND_V_MSG(!sd, 0, "ShapedTextDataAdvanced invalid.");
  3654. MutexLock lock(sd->mutex);
  3655. return sd->overrun_trim_data.ellipsis_glyph_buf.size();
  3656. }
  3657. bool TextServerAdvanced::shaped_text_update_breaks(const RID &p_shaped) {
  3658. ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
  3659. ERR_FAIL_COND_V(!sd, false);
  3660. MutexLock lock(sd->mutex);
  3661. if (!sd->valid) {
  3662. shaped_text_shape(p_shaped);
  3663. }
  3664. if (sd->line_breaks_valid) {
  3665. return true; // Nothing to do.
  3666. }
  3667. const UChar *data = sd->utf16.get_data();
  3668. if (!sd->break_ops_valid) {
  3669. sd->breaks.clear();
  3670. UErrorCode err = U_ZERO_ERROR;
  3671. int i = 0;
  3672. while (i < sd->spans.size()) {
  3673. String language = sd->spans[i].language;
  3674. int r_start = sd->spans[i].start;
  3675. while (i + 1 < sd->spans.size() && language == sd->spans[i + 1].language) {
  3676. i++;
  3677. }
  3678. int r_end = sd->spans[i].end;
  3679. UBreakIterator *bi = ubrk_open(UBRK_LINE, language.ascii().get_data(), data + _convert_pos_inv(sd, r_start), _convert_pos_inv(sd, r_end - r_start), &err);
  3680. if (U_FAILURE(err)) {
  3681. // No data loaded - use fallback.
  3682. for (int j = r_start; j < r_end; j++) {
  3683. char32_t c = sd->text[j - sd->start];
  3684. if (is_whitespace(c)) {
  3685. sd->breaks[j + 1] = false;
  3686. }
  3687. if (is_linebreak(c)) {
  3688. sd->breaks[j + 1] = true;
  3689. }
  3690. }
  3691. } else {
  3692. while (ubrk_next(bi) != UBRK_DONE) {
  3693. int pos = _convert_pos(sd, ubrk_current(bi)) + r_start;
  3694. if ((ubrk_getRuleStatus(bi) >= UBRK_LINE_HARD) && (ubrk_getRuleStatus(bi) < UBRK_LINE_HARD_LIMIT)) {
  3695. sd->breaks[pos] = true;
  3696. } else if ((ubrk_getRuleStatus(bi) >= UBRK_LINE_SOFT) && (ubrk_getRuleStatus(bi) < UBRK_LINE_SOFT_LIMIT)) {
  3697. sd->breaks[pos] = false;
  3698. }
  3699. }
  3700. }
  3701. ubrk_close(bi);
  3702. i++;
  3703. }
  3704. sd->break_ops_valid = true;
  3705. }
  3706. sd->sort_valid = false;
  3707. sd->glyphs_logical.clear();
  3708. int sd_size = sd->glyphs.size();
  3709. const char32_t *ch = sd->text.ptr();
  3710. Glyph *sd_glyphs = sd->glyphs.ptrw();
  3711. int c_punct_size = sd->custom_punct.length();
  3712. const char32_t *c_punct = sd->custom_punct.ptr();
  3713. for (int i = 0; i < sd_size; i++) {
  3714. if (sd_glyphs[i].count > 0) {
  3715. char32_t c = ch[sd_glyphs[i].start - sd->start];
  3716. if (c == 0xfffc) {
  3717. continue;
  3718. }
  3719. if (c == 0x0009 || c == 0x000b) {
  3720. sd_glyphs[i].flags |= GRAPHEME_IS_TAB;
  3721. }
  3722. if (is_whitespace(c)) {
  3723. sd_glyphs[i].flags |= GRAPHEME_IS_SPACE;
  3724. }
  3725. if (c_punct_size == 0) {
  3726. if (u_ispunct(c) && c != 0x005f) {
  3727. sd_glyphs[i].flags |= GRAPHEME_IS_PUNCTUATION;
  3728. }
  3729. } else {
  3730. for (int j = 0; j < c_punct_size; j++) {
  3731. if (c_punct[j] == c) {
  3732. sd_glyphs[i].flags |= GRAPHEME_IS_PUNCTUATION;
  3733. break;
  3734. }
  3735. }
  3736. }
  3737. if (is_underscore(c)) {
  3738. sd_glyphs[i].flags |= GRAPHEME_IS_UNDERSCORE;
  3739. }
  3740. if (sd->breaks.has(sd_glyphs[i].end)) {
  3741. if (sd->breaks[sd_glyphs[i].end] && (is_linebreak(c))) {
  3742. sd_glyphs[i].flags |= GRAPHEME_IS_BREAK_HARD;
  3743. } else if (is_whitespace(c)) {
  3744. sd_glyphs[i].flags |= GRAPHEME_IS_BREAK_SOFT;
  3745. } else {
  3746. int count = sd_glyphs[i].count;
  3747. // Do not add extra space at the end of the line.
  3748. if (sd_glyphs[i].end == sd->end) {
  3749. continue;
  3750. }
  3751. // Do not add extra space after existing space.
  3752. if (sd_glyphs[i].flags & GRAPHEME_IS_RTL) {
  3753. 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))) {
  3754. continue;
  3755. }
  3756. } else {
  3757. if ((i > 0) && ((sd_glyphs[i - 1].flags & (GRAPHEME_IS_SPACE | GRAPHEME_IS_BREAK_SOFT)) == (GRAPHEME_IS_SPACE | GRAPHEME_IS_BREAK_SOFT))) {
  3758. continue;
  3759. }
  3760. }
  3761. Glyph gl;
  3762. gl.start = sd_glyphs[i].start;
  3763. gl.end = sd_glyphs[i].end;
  3764. gl.count = 1;
  3765. gl.font_rid = sd_glyphs[i].font_rid;
  3766. gl.font_size = sd_glyphs[i].font_size;
  3767. gl.flags = GRAPHEME_IS_BREAK_SOFT | GRAPHEME_IS_VIRTUAL | GRAPHEME_IS_SPACE;
  3768. if (sd_glyphs[i].flags & GRAPHEME_IS_RTL) {
  3769. gl.flags |= GRAPHEME_IS_RTL;
  3770. sd->glyphs.insert(i, gl); // Insert before.
  3771. } else {
  3772. sd->glyphs.insert(i + count, gl); // Insert after.
  3773. }
  3774. i += count;
  3775. // Update write pointer and size.
  3776. sd_size = sd->glyphs.size();
  3777. sd_glyphs = sd->glyphs.ptrw();
  3778. continue;
  3779. }
  3780. }
  3781. i += (sd_glyphs[i].count - 1);
  3782. }
  3783. }
  3784. sd->line_breaks_valid = true;
  3785. return sd->line_breaks_valid;
  3786. }
  3787. _FORCE_INLINE_ int64_t _generate_kashida_justification_opportunies(const String &p_data, int64_t p_start, int64_t p_end) {
  3788. int64_t kashida_pos = -1;
  3789. int8_t priority = 100;
  3790. int64_t i = p_start;
  3791. char32_t pc = 0;
  3792. while ((p_end > p_start) && is_transparent(p_data[p_end - 1])) {
  3793. p_end--;
  3794. }
  3795. while (i < p_end) {
  3796. uint32_t c = p_data[i];
  3797. if (c == 0x0640) {
  3798. kashida_pos = i;
  3799. priority = 0;
  3800. }
  3801. if (priority >= 1 && i < p_end - 1) {
  3802. if (is_seen_sad(c) && (p_data[i + 1] != 0x200c)) {
  3803. kashida_pos = i;
  3804. priority = 1;
  3805. }
  3806. }
  3807. if (priority >= 2 && i > p_start) {
  3808. if (is_teh_marbuta(c) || is_dal(c) || (is_heh(c) && i == p_end - 1)) {
  3809. if (is_connected_to_prev(c, pc)) {
  3810. kashida_pos = i - 1;
  3811. priority = 2;
  3812. }
  3813. }
  3814. }
  3815. if (priority >= 3 && i > p_start) {
  3816. if (is_alef(c) || ((is_lam(c) || is_tah(c) || is_kaf(c) || is_gaf(c)) && i == p_end - 1)) {
  3817. if (is_connected_to_prev(c, pc)) {
  3818. kashida_pos = i - 1;
  3819. priority = 3;
  3820. }
  3821. }
  3822. }
  3823. if (priority >= 4 && i > p_start && i < p_end - 1) {
  3824. if (is_beh(c)) {
  3825. if (is_reh(p_data[i + 1]) || is_yeh(p_data[i + 1])) {
  3826. if (is_connected_to_prev(c, pc)) {
  3827. kashida_pos = i - 1;
  3828. priority = 4;
  3829. }
  3830. }
  3831. }
  3832. }
  3833. if (priority >= 5 && i > p_start) {
  3834. if (is_waw(c) || ((is_ain(c) || is_qaf(c) || is_feh(c)) && i == p_end - 1)) {
  3835. if (is_connected_to_prev(c, pc)) {
  3836. kashida_pos = i - 1;
  3837. priority = 5;
  3838. }
  3839. }
  3840. }
  3841. if (priority >= 6 && i > p_start) {
  3842. if (is_reh(c)) {
  3843. if (is_connected_to_prev(c, pc)) {
  3844. kashida_pos = i - 1;
  3845. priority = 6;
  3846. }
  3847. }
  3848. }
  3849. if (!is_transparent(c)) {
  3850. pc = c;
  3851. }
  3852. i++;
  3853. }
  3854. return kashida_pos;
  3855. }
  3856. bool TextServerAdvanced::shaped_text_update_justification_ops(const RID &p_shaped) {
  3857. ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
  3858. ERR_FAIL_COND_V(!sd, false);
  3859. MutexLock lock(sd->mutex);
  3860. if (!sd->valid) {
  3861. shaped_text_shape(p_shaped);
  3862. }
  3863. if (!sd->line_breaks_valid) {
  3864. shaped_text_update_breaks(p_shaped);
  3865. }
  3866. if (sd->justification_ops_valid) {
  3867. return true; // Nothing to do.
  3868. }
  3869. const UChar *data = sd->utf16.get_data();
  3870. int data_size = sd->utf16.length();
  3871. if (!sd->js_ops_valid) {
  3872. sd->jstops.clear();
  3873. // Use ICU word iterator and custom kashida detection.
  3874. UErrorCode err = U_ZERO_ERROR;
  3875. UBreakIterator *bi = ubrk_open(UBRK_WORD, "", data, data_size, &err);
  3876. if (U_FAILURE(err)) {
  3877. // No data - use fallback.
  3878. int limit = 0;
  3879. for (int i = 0; i < sd->text.length(); i++) {
  3880. if (is_whitespace(data[i])) {
  3881. int ks = _generate_kashida_justification_opportunies(sd->text, limit, i) + sd->start;
  3882. if (ks != -1) {
  3883. sd->jstops[ks] = true;
  3884. }
  3885. limit = i + 1;
  3886. }
  3887. }
  3888. int ks = _generate_kashida_justification_opportunies(sd->text, limit, sd->text.length()) + sd->start;
  3889. if (ks != -1) {
  3890. sd->jstops[ks] = true;
  3891. }
  3892. } else {
  3893. int limit = 0;
  3894. while (ubrk_next(bi) != UBRK_DONE) {
  3895. if (ubrk_getRuleStatus(bi) != UBRK_WORD_NONE) {
  3896. int i = _convert_pos(sd, ubrk_current(bi));
  3897. sd->jstops[i + sd->start] = false;
  3898. int ks = _generate_kashida_justification_opportunies(sd->text, limit, i);
  3899. if (ks != -1) {
  3900. sd->jstops[ks + sd->start] = true;
  3901. }
  3902. limit = i;
  3903. }
  3904. }
  3905. ubrk_close(bi);
  3906. }
  3907. sd->js_ops_valid = true;
  3908. }
  3909. sd->sort_valid = false;
  3910. sd->glyphs_logical.clear();
  3911. Glyph *sd_glyphs = sd->glyphs.ptrw();
  3912. int sd_size = sd->glyphs.size();
  3913. if (!sd->jstops.is_empty()) {
  3914. for (int i = 0; i < sd_size; i++) {
  3915. if (sd_glyphs[i].count > 0) {
  3916. char32_t c = sd->text[sd_glyphs[i].start - sd->start];
  3917. if (c == 0x0640) {
  3918. sd_glyphs[i].flags |= GRAPHEME_IS_ELONGATION;
  3919. }
  3920. if (sd->jstops.has(sd_glyphs[i].start)) {
  3921. if (c == 0xfffc) {
  3922. continue;
  3923. }
  3924. if (sd->jstops[sd_glyphs[i].start]) {
  3925. if (c != 0x0640) {
  3926. if (sd_glyphs[i].font_rid != RID()) {
  3927. Glyph gl = _shape_single_glyph(sd, 0x0640, HB_SCRIPT_ARABIC, HB_DIRECTION_RTL, sd->glyphs[i].font_rid, sd->glyphs[i].font_size);
  3928. if ((sd_glyphs[i].flags & GRAPHEME_IS_VALID) == GRAPHEME_IS_VALID) {
  3929. gl.start = sd_glyphs[i].start;
  3930. gl.end = sd_glyphs[i].end;
  3931. gl.repeat = 0;
  3932. gl.count = 1;
  3933. if (sd->orientation == ORIENTATION_HORIZONTAL) {
  3934. gl.y_off = sd_glyphs[i].y_off;
  3935. } else {
  3936. gl.x_off = sd_glyphs[i].x_off;
  3937. }
  3938. gl.flags |= GRAPHEME_IS_ELONGATION | GRAPHEME_IS_VIRTUAL;
  3939. sd->glyphs.insert(i, gl);
  3940. i++;
  3941. // Update write pointer and size.
  3942. sd_size = sd->glyphs.size();
  3943. sd_glyphs = sd->glyphs.ptrw();
  3944. continue;
  3945. }
  3946. }
  3947. }
  3948. } else if ((sd_glyphs[i].flags & GRAPHEME_IS_SPACE) != GRAPHEME_IS_SPACE) {
  3949. int count = sd_glyphs[i].count;
  3950. // Do not add extra spaces at the end of the line.
  3951. if (sd_glyphs[i].end == sd->end) {
  3952. continue;
  3953. }
  3954. // Do not add extra space after existing space.
  3955. if (sd_glyphs[i].flags & GRAPHEME_IS_RTL) {
  3956. 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))) {
  3957. continue;
  3958. }
  3959. } else {
  3960. if ((i > 0) && ((sd_glyphs[i - 1].flags & (GRAPHEME_IS_SPACE | GRAPHEME_IS_BREAK_SOFT)) == (GRAPHEME_IS_SPACE | GRAPHEME_IS_BREAK_SOFT))) {
  3961. continue;
  3962. }
  3963. }
  3964. // Inject virtual space for alignment.
  3965. Glyph gl;
  3966. gl.start = sd_glyphs[i].start;
  3967. gl.end = sd_glyphs[i].end;
  3968. gl.count = 1;
  3969. gl.font_rid = sd_glyphs[i].font_rid;
  3970. gl.font_size = sd_glyphs[i].font_size;
  3971. gl.flags = GRAPHEME_IS_SPACE | GRAPHEME_IS_VIRTUAL;
  3972. if (sd_glyphs[i].flags & GRAPHEME_IS_RTL) {
  3973. gl.flags |= GRAPHEME_IS_RTL;
  3974. sd->glyphs.insert(i, gl); // Insert before.
  3975. } else {
  3976. sd->glyphs.insert(i + count, gl); // Insert after.
  3977. }
  3978. i += count;
  3979. // Update write pointer and size.
  3980. sd_size = sd->glyphs.size();
  3981. sd_glyphs = sd->glyphs.ptrw();
  3982. continue;
  3983. }
  3984. }
  3985. }
  3986. }
  3987. }
  3988. sd->justification_ops_valid = true;
  3989. return sd->justification_ops_valid;
  3990. }
  3991. 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) {
  3992. hb_font_t *hb_font = _font_get_hb_handle(p_font, p_font_size);
  3993. bool subpos = (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);
  3994. ERR_FAIL_COND_V(hb_font == nullptr, Glyph());
  3995. hb_buffer_clear_contents(p_sd->hb_buffer);
  3996. hb_buffer_set_direction(p_sd->hb_buffer, p_direction);
  3997. hb_buffer_set_flags(p_sd->hb_buffer, (hb_buffer_flags_t)(HB_BUFFER_FLAG_DEFAULT));
  3998. hb_buffer_set_script(p_sd->hb_buffer, p_script);
  3999. hb_buffer_add_utf32(p_sd->hb_buffer, (const uint32_t *)&p_char, 1, 0, 1);
  4000. hb_shape(hb_font, p_sd->hb_buffer, nullptr, 0);
  4001. unsigned int glyph_count = 0;
  4002. hb_glyph_info_t *glyph_info = hb_buffer_get_glyph_infos(p_sd->hb_buffer, &glyph_count);
  4003. hb_glyph_position_t *glyph_pos = hb_buffer_get_glyph_positions(p_sd->hb_buffer, &glyph_count);
  4004. // Process glyphs.
  4005. Glyph gl;
  4006. if (p_direction == HB_DIRECTION_RTL || p_direction == HB_DIRECTION_BTT) {
  4007. gl.flags |= TextServer::GRAPHEME_IS_RTL;
  4008. }
  4009. gl.font_rid = p_font;
  4010. gl.font_size = p_font_size;
  4011. if (glyph_count > 0) {
  4012. double scale = font_get_scale(p_font, p_font_size);
  4013. if (p_sd->orientation == ORIENTATION_HORIZONTAL) {
  4014. if (subpos) {
  4015. gl.advance = glyph_pos[0].x_advance / (64.0 / scale) + _get_extra_advance(p_font, p_font_size);
  4016. } else {
  4017. gl.advance = Math::round(glyph_pos[0].x_advance / (64.0 / scale) + _get_extra_advance(p_font, p_font_size));
  4018. }
  4019. } else {
  4020. gl.advance = -Math::round(glyph_pos[0].y_advance / (64.0 / scale));
  4021. }
  4022. gl.count = 1;
  4023. gl.index = glyph_info[0].codepoint;
  4024. if (subpos) {
  4025. gl.x_off = glyph_pos[0].x_offset / (64.0 / scale);
  4026. } else {
  4027. gl.x_off = Math::round(glyph_pos[0].x_offset / (64.0 / scale));
  4028. }
  4029. gl.y_off = -Math::round(glyph_pos[0].y_offset / (64.0 / scale));
  4030. if ((glyph_info[0].codepoint != 0) || !u_isgraph(p_char)) {
  4031. gl.flags |= GRAPHEME_IS_VALID;
  4032. }
  4033. }
  4034. return gl;
  4035. }
  4036. _FORCE_INLINE_ void TextServerAdvanced::_add_featuers(const Dictionary &p_source, Vector<hb_feature_t> &r_ftrs) {
  4037. Array keys = p_source.keys();
  4038. Array values = p_source.values();
  4039. for (int i = 0; i < keys.size(); i++) {
  4040. int32_t value = values[i];
  4041. if (value >= 0) {
  4042. hb_feature_t feature;
  4043. if (keys[i].get_type() == Variant::STRING) {
  4044. feature.tag = name_to_tag(keys[i]);
  4045. } else {
  4046. feature.tag = keys[i];
  4047. }
  4048. feature.value = value;
  4049. feature.start = 0;
  4050. feature.end = -1;
  4051. r_ftrs.push_back(feature);
  4052. }
  4053. }
  4054. }
  4055. 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, Array p_fonts, int64_t p_span, int64_t p_fb_index) {
  4056. int fs = p_sd->spans[p_span].font_size;
  4057. if (p_fb_index >= p_fonts.size()) {
  4058. // Add fallback glyphs.
  4059. for (int i = p_start; i < p_end; i++) {
  4060. if (p_sd->preserve_invalid || (p_sd->preserve_control && is_control(p_sd->text[i]))) {
  4061. Glyph gl;
  4062. gl.start = i + p_sd->start;
  4063. gl.end = i + 1 + p_sd->start;
  4064. gl.count = 1;
  4065. gl.index = p_sd->text[i];
  4066. gl.font_size = fs;
  4067. gl.font_rid = RID();
  4068. if (p_direction == HB_DIRECTION_RTL || p_direction == HB_DIRECTION_BTT) {
  4069. gl.flags |= TextServer::GRAPHEME_IS_RTL;
  4070. }
  4071. if (p_sd->orientation == ORIENTATION_HORIZONTAL) {
  4072. gl.advance = get_hex_code_box_size(fs, gl.index).x;
  4073. p_sd->ascent = MAX(p_sd->ascent, get_hex_code_box_size(fs, gl.index).y);
  4074. } else {
  4075. gl.advance = get_hex_code_box_size(fs, gl.index).y;
  4076. p_sd->ascent = MAX(p_sd->ascent, Math::round(get_hex_code_box_size(fs, gl.index).x * 0.5));
  4077. p_sd->descent = MAX(p_sd->descent, Math::round(get_hex_code_box_size(fs, gl.index).x * 0.5));
  4078. }
  4079. p_sd->width += gl.advance;
  4080. p_sd->glyphs.push_back(gl);
  4081. }
  4082. }
  4083. return;
  4084. }
  4085. RID f = p_fonts[p_fb_index];
  4086. FontDataAdvanced *fd = font_owner.get_or_null(f);
  4087. Vector2i fss = _get_size(fd, fs);
  4088. hb_font_t *hb_font = _font_get_hb_handle(f, fs);
  4089. double scale = font_get_scale(f, fs);
  4090. double sp_sp = font_get_spacing(f, fs, SPACING_SPACE);
  4091. double sp_gl = font_get_spacing(f, fs, SPACING_GLYPH);
  4092. double ea = _get_extra_advance(f, fs);
  4093. bool subpos = (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);
  4094. ERR_FAIL_COND(hb_font == nullptr);
  4095. hb_buffer_clear_contents(p_sd->hb_buffer);
  4096. hb_buffer_set_direction(p_sd->hb_buffer, p_direction);
  4097. if (p_sd->preserve_control) {
  4098. hb_buffer_set_flags(p_sd->hb_buffer, (hb_buffer_flags_t)(HB_BUFFER_FLAG_PRESERVE_DEFAULT_IGNORABLES | (p_start == 0 ? HB_BUFFER_FLAG_BOT : 0) | (p_end == p_sd->text.length() ? HB_BUFFER_FLAG_EOT : 0)));
  4099. } else {
  4100. hb_buffer_set_flags(p_sd->hb_buffer, (hb_buffer_flags_t)(HB_BUFFER_FLAG_DEFAULT | (p_start == 0 ? HB_BUFFER_FLAG_BOT : 0) | (p_end == p_sd->text.length() ? HB_BUFFER_FLAG_EOT : 0)));
  4101. }
  4102. hb_buffer_set_script(p_sd->hb_buffer, p_script);
  4103. if (!p_sd->spans[p_span].language.is_empty()) {
  4104. hb_language_t lang = hb_language_from_string(p_sd->spans[p_span].language.ascii().get_data(), -1);
  4105. hb_buffer_set_language(p_sd->hb_buffer, lang);
  4106. }
  4107. 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);
  4108. Vector<hb_feature_t> ftrs;
  4109. _add_featuers(font_get_opentype_feature_overrides(f), ftrs);
  4110. _add_featuers(p_sd->spans[p_span].features, ftrs);
  4111. hb_shape(hb_font, p_sd->hb_buffer, ftrs.is_empty() ? nullptr : &ftrs[0], ftrs.size());
  4112. unsigned int glyph_count = 0;
  4113. hb_glyph_info_t *glyph_info = hb_buffer_get_glyph_infos(p_sd->hb_buffer, &glyph_count);
  4114. hb_glyph_position_t *glyph_pos = hb_buffer_get_glyph_positions(p_sd->hb_buffer, &glyph_count);
  4115. // Process glyphs.
  4116. if (glyph_count > 0) {
  4117. Glyph *w = (Glyph *)memalloc(glyph_count * sizeof(Glyph));
  4118. int end = (p_direction == HB_DIRECTION_RTL || p_direction == HB_DIRECTION_BTT) ? p_end : 0;
  4119. uint32_t last_cluster_id = UINT32_MAX;
  4120. unsigned int last_cluster_index = 0;
  4121. bool last_cluster_valid = true;
  4122. for (unsigned int i = 0; i < glyph_count; i++) {
  4123. if ((i > 0) && (last_cluster_id != glyph_info[i].cluster)) {
  4124. if (p_direction == HB_DIRECTION_RTL || p_direction == HB_DIRECTION_BTT) {
  4125. end = w[last_cluster_index].start;
  4126. } else {
  4127. for (unsigned int j = last_cluster_index; j < i; j++) {
  4128. w[j].end = glyph_info[i].cluster;
  4129. }
  4130. }
  4131. if (p_direction == HB_DIRECTION_RTL || p_direction == HB_DIRECTION_BTT) {
  4132. w[last_cluster_index].flags |= GRAPHEME_IS_RTL;
  4133. }
  4134. if (last_cluster_valid) {
  4135. w[last_cluster_index].flags |= GRAPHEME_IS_VALID;
  4136. }
  4137. w[last_cluster_index].count = i - last_cluster_index;
  4138. last_cluster_index = i;
  4139. last_cluster_valid = true;
  4140. }
  4141. last_cluster_id = glyph_info[i].cluster;
  4142. Glyph &gl = w[i];
  4143. gl = Glyph();
  4144. gl.start = glyph_info[i].cluster;
  4145. gl.end = end;
  4146. gl.count = 0;
  4147. gl.font_rid = p_fonts[p_fb_index];
  4148. gl.font_size = fs;
  4149. if (glyph_info[i].mask & HB_GLYPH_FLAG_DEFINED) {
  4150. gl.flags |= GRAPHEME_IS_CONNECTED;
  4151. }
  4152. gl.index = glyph_info[i].codepoint;
  4153. if (gl.index != 0) {
  4154. _ensure_glyph(fd, fss, gl.index);
  4155. if (p_sd->orientation == ORIENTATION_HORIZONTAL) {
  4156. if (subpos) {
  4157. gl.advance = glyph_pos[i].x_advance / (64.0 / scale) + ea;
  4158. } else {
  4159. gl.advance = Math::round(glyph_pos[i].x_advance / (64.0 / scale) + ea);
  4160. }
  4161. } else {
  4162. gl.advance = -Math::round(glyph_pos[i].y_advance / (64.0 / scale));
  4163. }
  4164. if (subpos) {
  4165. gl.x_off = glyph_pos[i].x_offset / (64.0 / scale);
  4166. } else {
  4167. gl.x_off = Math::round(glyph_pos[i].x_offset / (64.0 / scale));
  4168. }
  4169. gl.y_off = -Math::round(glyph_pos[i].y_offset / (64.0 / scale));
  4170. }
  4171. if (sp_sp && is_whitespace(p_sd->text[glyph_info[i].cluster])) {
  4172. gl.advance += sp_sp;
  4173. } else {
  4174. gl.advance += sp_gl;
  4175. }
  4176. if (p_sd->preserve_control) {
  4177. last_cluster_valid = last_cluster_valid && ((glyph_info[i].codepoint != 0) || (p_sd->text[glyph_info[i].cluster] == 0x0009) || (u_isblank(p_sd->text[glyph_info[i].cluster]) && (gl.advance != 0)) || (!u_isblank(p_sd->text[glyph_info[i].cluster]) && is_linebreak(p_sd->text[glyph_info[i].cluster])));
  4178. } else {
  4179. last_cluster_valid = last_cluster_valid && ((glyph_info[i].codepoint != 0) || (p_sd->text[glyph_info[i].cluster] == 0x0009) || (u_isblank(p_sd->text[glyph_info[i].cluster]) && (gl.advance != 0)) || (!u_isblank(p_sd->text[glyph_info[i].cluster]) && !u_isgraph(p_sd->text[glyph_info[i].cluster])));
  4180. }
  4181. }
  4182. if (p_direction == HB_DIRECTION_LTR || p_direction == HB_DIRECTION_TTB) {
  4183. for (unsigned int j = last_cluster_index; j < glyph_count; j++) {
  4184. w[j].end = p_end;
  4185. }
  4186. }
  4187. w[last_cluster_index].count = glyph_count - last_cluster_index;
  4188. if (p_direction == HB_DIRECTION_RTL || p_direction == HB_DIRECTION_BTT) {
  4189. w[last_cluster_index].flags |= GRAPHEME_IS_RTL;
  4190. }
  4191. if (last_cluster_valid) {
  4192. w[last_cluster_index].flags |= GRAPHEME_IS_VALID;
  4193. }
  4194. // Fallback.
  4195. int failed_subrun_start = p_end + 1;
  4196. int failed_subrun_end = p_start;
  4197. for (unsigned int i = 0; i < glyph_count; i++) {
  4198. if ((w[i].flags & GRAPHEME_IS_VALID) == GRAPHEME_IS_VALID) {
  4199. if (failed_subrun_start != p_end + 1) {
  4200. _shape_run(p_sd, failed_subrun_start, failed_subrun_end, p_script, p_direction, p_fonts, p_span, p_fb_index + 1);
  4201. failed_subrun_start = p_end + 1;
  4202. failed_subrun_end = p_start;
  4203. }
  4204. for (int j = 0; j < w[i].count; j++) {
  4205. if (p_sd->orientation == ORIENTATION_HORIZONTAL) {
  4206. p_sd->ascent = MAX(p_sd->ascent, -w[i + j].y_off);
  4207. p_sd->descent = MAX(p_sd->descent, w[i + j].y_off);
  4208. } else {
  4209. double gla = Math::round(font_get_glyph_advance(f, fs, w[i + j].index).x * 0.5);
  4210. p_sd->ascent = MAX(p_sd->ascent, gla);
  4211. p_sd->descent = MAX(p_sd->descent, gla);
  4212. }
  4213. p_sd->width += w[i + j].advance;
  4214. w[i + j].start += p_sd->start;
  4215. w[i + j].end += p_sd->start;
  4216. p_sd->glyphs.push_back(w[i + j]);
  4217. }
  4218. } else {
  4219. if (failed_subrun_start >= w[i].start) {
  4220. failed_subrun_start = w[i].start;
  4221. }
  4222. if (failed_subrun_end <= w[i].end) {
  4223. failed_subrun_end = w[i].end;
  4224. }
  4225. }
  4226. i += w[i].count - 1;
  4227. }
  4228. memfree(w);
  4229. if (failed_subrun_start != p_end + 1) {
  4230. _shape_run(p_sd, failed_subrun_start, failed_subrun_end, p_script, p_direction, p_fonts, p_span, p_fb_index + 1);
  4231. }
  4232. p_sd->ascent = MAX(p_sd->ascent, font_get_ascent(f, fs));
  4233. p_sd->descent = MAX(p_sd->descent, font_get_descent(f, fs));
  4234. p_sd->upos = MAX(p_sd->upos, font_get_underline_position(f, fs));
  4235. p_sd->uthk = MAX(p_sd->uthk, font_get_underline_thickness(f, fs));
  4236. }
  4237. }
  4238. bool TextServerAdvanced::shaped_text_shape(const RID &p_shaped) {
  4239. ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
  4240. ERR_FAIL_COND_V(!sd, false);
  4241. MutexLock lock(sd->mutex);
  4242. if (sd->valid) {
  4243. return true;
  4244. }
  4245. invalidate(sd, false);
  4246. if (sd->parent != RID()) {
  4247. shaped_text_shape(sd->parent);
  4248. ShapedTextDataAdvanced *parent_sd = shaped_owner.get_or_null(sd->parent);
  4249. ERR_FAIL_COND_V(!parent_sd->valid, false);
  4250. ERR_FAIL_COND_V(!_shape_substr(sd, parent_sd, sd->start, sd->end - sd->start), false);
  4251. return true;
  4252. }
  4253. if (sd->text.length() == 0) {
  4254. sd->valid = true;
  4255. return true;
  4256. }
  4257. sd->utf16 = sd->text.utf16();
  4258. const UChar *data = sd->utf16.get_data();
  4259. // Create script iterator.
  4260. if (sd->script_iter == nullptr) {
  4261. sd->script_iter = memnew(ScriptIterator(sd->text, 0, sd->text.length()));
  4262. }
  4263. if (sd->bidi_override.is_empty()) {
  4264. sd->bidi_override.push_back(Vector2i(sd->start, sd->end));
  4265. }
  4266. for (int ov = 0; ov < sd->bidi_override.size(); ov++) {
  4267. // Create BiDi iterator.
  4268. int start = _convert_pos_inv(sd, sd->bidi_override[ov].x - sd->start);
  4269. int end = _convert_pos_inv(sd, sd->bidi_override[ov].y - sd->start);
  4270. if (start < 0 || end - start > sd->utf16.length()) {
  4271. continue;
  4272. }
  4273. UErrorCode err = U_ZERO_ERROR;
  4274. UBiDi *bidi_iter = ubidi_openSized(end, 0, &err);
  4275. ERR_FAIL_COND_V_MSG(U_FAILURE(err), false, u_errorName(err));
  4276. switch (sd->direction) {
  4277. case DIRECTION_LTR: {
  4278. ubidi_setPara(bidi_iter, data + start, end - start, UBIDI_LTR, nullptr, &err);
  4279. sd->para_direction = DIRECTION_LTR;
  4280. } break;
  4281. case DIRECTION_RTL: {
  4282. ubidi_setPara(bidi_iter, data + start, end - start, UBIDI_RTL, nullptr, &err);
  4283. sd->para_direction = DIRECTION_RTL;
  4284. } break;
  4285. case DIRECTION_AUTO: {
  4286. UBiDiDirection direction = ubidi_getBaseDirection(data + start, end - start);
  4287. if (direction != UBIDI_NEUTRAL) {
  4288. ubidi_setPara(bidi_iter, data + start, end - start, direction, nullptr, &err);
  4289. sd->para_direction = (direction == UBIDI_RTL) ? DIRECTION_RTL : DIRECTION_LTR;
  4290. } else {
  4291. ubidi_setPara(bidi_iter, data + start, end - start, UBIDI_DEFAULT_LTR, nullptr, &err);
  4292. sd->para_direction = DIRECTION_LTR;
  4293. }
  4294. } break;
  4295. }
  4296. ERR_FAIL_COND_V_MSG(U_FAILURE(err), false, u_errorName(err));
  4297. sd->bidi_iter.push_back(bidi_iter);
  4298. err = U_ZERO_ERROR;
  4299. int bidi_run_count = ubidi_countRuns(bidi_iter, &err);
  4300. ERR_FAIL_COND_V_MSG(U_FAILURE(err), false, u_errorName(err));
  4301. for (int i = 0; i < bidi_run_count; i++) {
  4302. int32_t _bidi_run_start = 0;
  4303. int32_t _bidi_run_length = 0;
  4304. hb_direction_t bidi_run_direction = HB_DIRECTION_INVALID;
  4305. bool is_rtl = (ubidi_getVisualRun(bidi_iter, i, &_bidi_run_start, &_bidi_run_length) == UBIDI_LTR);
  4306. switch (sd->orientation) {
  4307. case ORIENTATION_HORIZONTAL: {
  4308. if (is_rtl) {
  4309. bidi_run_direction = HB_DIRECTION_LTR;
  4310. } else {
  4311. bidi_run_direction = HB_DIRECTION_RTL;
  4312. }
  4313. } break;
  4314. case ORIENTATION_VERTICAL: {
  4315. if (is_rtl) {
  4316. bidi_run_direction = HB_DIRECTION_TTB;
  4317. } else {
  4318. bidi_run_direction = HB_DIRECTION_BTT;
  4319. }
  4320. }
  4321. }
  4322. int32_t bidi_run_start = _convert_pos(sd, sd->bidi_override[ov].x - sd->start + _bidi_run_start);
  4323. int32_t bidi_run_end = _convert_pos(sd, sd->bidi_override[ov].x - sd->start + _bidi_run_start + _bidi_run_length);
  4324. // Shape runs.
  4325. int scr_from = (is_rtl) ? 0 : sd->script_iter->script_ranges.size() - 1;
  4326. int scr_to = (is_rtl) ? sd->script_iter->script_ranges.size() : -1;
  4327. int scr_delta = (is_rtl) ? +1 : -1;
  4328. for (int j = scr_from; j != scr_to; j += scr_delta) {
  4329. if ((sd->script_iter->script_ranges[j].start < bidi_run_end) && (sd->script_iter->script_ranges[j].end > bidi_run_start)) {
  4330. int32_t script_run_start = MAX(sd->script_iter->script_ranges[j].start, bidi_run_start);
  4331. int32_t script_run_end = MIN(sd->script_iter->script_ranges[j].end, bidi_run_end);
  4332. char scr_buffer[5] = { 0, 0, 0, 0, 0 };
  4333. hb_tag_to_string(hb_script_to_iso15924_tag(sd->script_iter->script_ranges[j].script), scr_buffer);
  4334. String script = String(scr_buffer);
  4335. int spn_from = (is_rtl) ? 0 : sd->spans.size() - 1;
  4336. int spn_to = (is_rtl) ? sd->spans.size() : -1;
  4337. int spn_delta = (is_rtl) ? +1 : -1;
  4338. for (int k = spn_from; k != spn_to; k += spn_delta) {
  4339. const ShapedTextDataAdvanced::Span &span = sd->spans[k];
  4340. if (span.start - sd->start >= script_run_end || span.end - sd->start <= script_run_start) {
  4341. continue;
  4342. }
  4343. if (span.embedded_key != Variant()) {
  4344. // Embedded object.
  4345. if (sd->orientation == ORIENTATION_HORIZONTAL) {
  4346. sd->objects[span.embedded_key].rect.position.x = sd->width;
  4347. sd->width += sd->objects[span.embedded_key].rect.size.x;
  4348. } else {
  4349. sd->objects[span.embedded_key].rect.position.y = sd->width;
  4350. sd->width += sd->objects[span.embedded_key].rect.size.y;
  4351. }
  4352. Glyph gl;
  4353. gl.start = span.start;
  4354. gl.end = span.end;
  4355. gl.count = 1;
  4356. gl.flags = GRAPHEME_IS_VALID | GRAPHEME_IS_VIRTUAL;
  4357. if (sd->orientation == ORIENTATION_HORIZONTAL) {
  4358. gl.advance = sd->objects[span.embedded_key].rect.size.x;
  4359. } else {
  4360. gl.advance = sd->objects[span.embedded_key].rect.size.y;
  4361. }
  4362. sd->glyphs.push_back(gl);
  4363. } else {
  4364. Array fonts;
  4365. Array fonts_scr_only;
  4366. Array fonts_no_match;
  4367. int font_count = span.fonts.size();
  4368. for (int l = 0; l < font_count; l++) {
  4369. if (font_is_script_supported(span.fonts[l], script)) {
  4370. if (font_is_language_supported(span.fonts[l], span.language)) {
  4371. fonts.push_back(sd->spans[k].fonts[l]);
  4372. } else {
  4373. fonts_scr_only.push_back(sd->spans[k].fonts[l]);
  4374. }
  4375. } else {
  4376. fonts_no_match.push_back(sd->spans[k].fonts[l]);
  4377. }
  4378. }
  4379. fonts.append_array(fonts_scr_only);
  4380. fonts.append_array(fonts_no_match);
  4381. _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);
  4382. }
  4383. }
  4384. }
  4385. }
  4386. }
  4387. }
  4388. _realign(sd);
  4389. sd->valid = true;
  4390. return sd->valid;
  4391. }
  4392. bool TextServerAdvanced::shaped_text_is_ready(const RID &p_shaped) const {
  4393. const ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
  4394. ERR_FAIL_COND_V(!sd, false);
  4395. MutexLock lock(sd->mutex);
  4396. return sd->valid;
  4397. }
  4398. const Glyph *TextServerAdvanced::shaped_text_get_glyphs(const RID &p_shaped) const {
  4399. const ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
  4400. ERR_FAIL_COND_V(!sd, nullptr);
  4401. MutexLock lock(sd->mutex);
  4402. if (!sd->valid) {
  4403. const_cast<TextServerAdvanced *>(this)->shaped_text_shape(p_shaped);
  4404. }
  4405. return sd->glyphs.ptr();
  4406. }
  4407. int64_t TextServerAdvanced::shaped_text_get_glyph_count(const RID &p_shaped) const {
  4408. const ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
  4409. ERR_FAIL_COND_V(!sd, 0);
  4410. MutexLock lock(sd->mutex);
  4411. if (!sd->valid) {
  4412. const_cast<TextServerAdvanced *>(this)->shaped_text_shape(p_shaped);
  4413. }
  4414. return sd->glyphs.size();
  4415. }
  4416. const Glyph *TextServerAdvanced::shaped_text_sort_logical(const RID &p_shaped) {
  4417. ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
  4418. ERR_FAIL_COND_V(!sd, nullptr);
  4419. MutexLock lock(sd->mutex);
  4420. if (!sd->valid) {
  4421. const_cast<TextServerAdvanced *>(this)->shaped_text_shape(p_shaped);
  4422. }
  4423. if (!sd->sort_valid) {
  4424. sd->glyphs_logical = sd->glyphs;
  4425. sd->glyphs_logical.sort_custom<GlyphCompare>();
  4426. sd->sort_valid = true;
  4427. }
  4428. return sd->glyphs_logical.ptr();
  4429. }
  4430. Vector2i TextServerAdvanced::shaped_text_get_range(const RID &p_shaped) const {
  4431. const ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
  4432. ERR_FAIL_COND_V(!sd, Vector2i());
  4433. MutexLock lock(sd->mutex);
  4434. return Vector2(sd->start, sd->end);
  4435. }
  4436. Array TextServerAdvanced::shaped_text_get_objects(const RID &p_shaped) const {
  4437. Array ret;
  4438. const ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
  4439. ERR_FAIL_COND_V(!sd, ret);
  4440. MutexLock lock(sd->mutex);
  4441. for (const KeyValue<Variant, ShapedTextDataAdvanced::EmbeddedObject> &E : sd->objects) {
  4442. ret.push_back(E.key);
  4443. }
  4444. return ret;
  4445. }
  4446. Rect2 TextServerAdvanced::shaped_text_get_object_rect(const RID &p_shaped, const Variant &p_key) const {
  4447. const ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
  4448. ERR_FAIL_COND_V(!sd, Rect2());
  4449. MutexLock lock(sd->mutex);
  4450. ERR_FAIL_COND_V(!sd->objects.has(p_key), Rect2());
  4451. if (!sd->valid) {
  4452. const_cast<TextServerAdvanced *>(this)->shaped_text_shape(p_shaped);
  4453. }
  4454. return sd->objects[p_key].rect;
  4455. }
  4456. Size2 TextServerAdvanced::shaped_text_get_size(const RID &p_shaped) const {
  4457. const ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
  4458. ERR_FAIL_COND_V(!sd, Size2());
  4459. MutexLock lock(sd->mutex);
  4460. if (!sd->valid) {
  4461. const_cast<TextServerAdvanced *>(this)->shaped_text_shape(p_shaped);
  4462. }
  4463. if (sd->orientation == TextServer::ORIENTATION_HORIZONTAL) {
  4464. return Size2((sd->text_trimmed ? sd->width_trimmed : sd->width), sd->ascent + sd->descent).ceil();
  4465. } else {
  4466. return Size2(sd->ascent + sd->descent, (sd->text_trimmed ? sd->width_trimmed : sd->width)).ceil();
  4467. }
  4468. }
  4469. double TextServerAdvanced::shaped_text_get_ascent(const RID &p_shaped) const {
  4470. const ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
  4471. ERR_FAIL_COND_V(!sd, 0.0);
  4472. MutexLock lock(sd->mutex);
  4473. if (!sd->valid) {
  4474. const_cast<TextServerAdvanced *>(this)->shaped_text_shape(p_shaped);
  4475. }
  4476. return sd->ascent;
  4477. }
  4478. double TextServerAdvanced::shaped_text_get_descent(const RID &p_shaped) const {
  4479. const ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
  4480. ERR_FAIL_COND_V(!sd, 0.0);
  4481. MutexLock lock(sd->mutex);
  4482. if (!sd->valid) {
  4483. const_cast<TextServerAdvanced *>(this)->shaped_text_shape(p_shaped);
  4484. }
  4485. return sd->descent;
  4486. }
  4487. double TextServerAdvanced::shaped_text_get_width(const RID &p_shaped) const {
  4488. const ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
  4489. ERR_FAIL_COND_V(!sd, 0.0);
  4490. MutexLock lock(sd->mutex);
  4491. if (!sd->valid) {
  4492. const_cast<TextServerAdvanced *>(this)->shaped_text_shape(p_shaped);
  4493. }
  4494. return Math::ceil(sd->text_trimmed ? sd->width_trimmed : sd->width);
  4495. }
  4496. double TextServerAdvanced::shaped_text_get_underline_position(const RID &p_shaped) const {
  4497. const ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
  4498. ERR_FAIL_COND_V(!sd, 0.0);
  4499. MutexLock lock(sd->mutex);
  4500. if (!sd->valid) {
  4501. const_cast<TextServerAdvanced *>(this)->shaped_text_shape(p_shaped);
  4502. }
  4503. return sd->upos;
  4504. }
  4505. double TextServerAdvanced::shaped_text_get_underline_thickness(const RID &p_shaped) const {
  4506. const ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
  4507. ERR_FAIL_COND_V(!sd, 0.0);
  4508. MutexLock lock(sd->mutex);
  4509. if (!sd->valid) {
  4510. const_cast<TextServerAdvanced *>(this)->shaped_text_shape(p_shaped);
  4511. }
  4512. return sd->uthk;
  4513. }
  4514. void TextServerAdvanced::_insert_num_systems_lang() {
  4515. // Eastern Arabic numerals.
  4516. {
  4517. NumSystemData ar;
  4518. ar.lang.insert(StringName("ar")); // Arabic
  4519. ar.lang.insert(StringName("ar_AE"));
  4520. ar.lang.insert(StringName("ar_BH"));
  4521. ar.lang.insert(StringName("ar_DJ"));
  4522. ar.lang.insert(StringName("ar_EG"));
  4523. ar.lang.insert(StringName("ar_ER"));
  4524. ar.lang.insert(StringName("ar_IL"));
  4525. ar.lang.insert(StringName("ar_IQ"));
  4526. ar.lang.insert(StringName("ar_JO"));
  4527. ar.lang.insert(StringName("ar_KM"));
  4528. ar.lang.insert(StringName("ar_KW"));
  4529. ar.lang.insert(StringName("ar_LB"));
  4530. ar.lang.insert(StringName("ar_MR"));
  4531. ar.lang.insert(StringName("ar_OM"));
  4532. ar.lang.insert(StringName("ar_PS"));
  4533. ar.lang.insert(StringName("ar_QA"));
  4534. ar.lang.insert(StringName("ar_SA"));
  4535. ar.lang.insert(StringName("ar_SD"));
  4536. ar.lang.insert(StringName("ar_SO"));
  4537. ar.lang.insert(StringName("ar_SS"));
  4538. ar.lang.insert(StringName("ar_SY"));
  4539. ar.lang.insert(StringName("ar_TD"));
  4540. ar.lang.insert(StringName("ar_YE"));
  4541. ar.lang.insert(StringName("ckb")); // Central Kurdish
  4542. ar.lang.insert(StringName("ckb_IQ"));
  4543. ar.lang.insert(StringName("ckb_IR"));
  4544. ar.lang.insert(StringName("sd")); // Sindhi
  4545. ar.lang.insert(StringName("sd_PK"));
  4546. ar.lang.insert(StringName("sd_Arab"));
  4547. ar.lang.insert(StringName("sd_Arab_PK"));
  4548. ar.digits = U"٠١٢٣٤٥٦٧٨٩٫";
  4549. ar.percent_sign = U"٪";
  4550. ar.exp = U"اس";
  4551. num_systems.push_back(ar);
  4552. }
  4553. // Persian and Urdu numerals.
  4554. {
  4555. NumSystemData pr;
  4556. pr.lang.insert(StringName("fa")); // Persian
  4557. pr.lang.insert(StringName("fa_AF"));
  4558. pr.lang.insert(StringName("fa_IR"));
  4559. pr.lang.insert(StringName("ks")); // Kashmiri
  4560. pr.lang.insert(StringName("ks_IN"));
  4561. pr.lang.insert(StringName("ks_Arab"));
  4562. pr.lang.insert(StringName("ks_Arab_IN"));
  4563. pr.lang.insert(StringName("lrc")); // Northern Luri
  4564. pr.lang.insert(StringName("lrc_IQ"));
  4565. pr.lang.insert(StringName("lrc_IR"));
  4566. pr.lang.insert(StringName("mzn")); // Mazanderani
  4567. pr.lang.insert(StringName("mzn_IR"));
  4568. pr.lang.insert(StringName("pa_PK")); // Panjabi
  4569. pr.lang.insert(StringName("pa_Arab"));
  4570. pr.lang.insert(StringName("pa_Arab_PK"));
  4571. pr.lang.insert(StringName("ps")); // Pushto
  4572. pr.lang.insert(StringName("ps_AF"));
  4573. pr.lang.insert(StringName("ps_PK"));
  4574. pr.lang.insert(StringName("ur_IN")); // Urdu
  4575. pr.lang.insert(StringName("uz_AF")); // Uzbek
  4576. pr.lang.insert(StringName("uz_Arab"));
  4577. pr.lang.insert(StringName("uz_Arab_AF"));
  4578. pr.digits = U"۰۱۲۳۴۵۶۷۸۹٫";
  4579. pr.percent_sign = U"٪";
  4580. pr.exp = U"اس";
  4581. num_systems.push_back(pr);
  4582. }
  4583. // Bengali numerals.
  4584. {
  4585. NumSystemData bn;
  4586. bn.lang.insert(StringName("as")); // Assamese
  4587. bn.lang.insert(StringName("as_IN"));
  4588. bn.lang.insert(StringName("bn")); // Bengali
  4589. bn.lang.insert(StringName("bn_BD"));
  4590. bn.lang.insert(StringName("bn_IN"));
  4591. bn.lang.insert(StringName("mni")); // Manipuri
  4592. bn.lang.insert(StringName("mni_IN"));
  4593. bn.lang.insert(StringName("mni_Beng"));
  4594. bn.lang.insert(StringName("mni_Beng_IN"));
  4595. bn.digits = U"০১২৩৪৫৬৭৮৯.";
  4596. bn.percent_sign = U"%";
  4597. bn.exp = U"e";
  4598. num_systems.push_back(bn);
  4599. }
  4600. // Devanagari numerals.
  4601. {
  4602. NumSystemData mr;
  4603. mr.lang.insert(StringName("mr")); // Marathi
  4604. mr.lang.insert(StringName("mr_IN"));
  4605. mr.lang.insert(StringName("ne")); // Nepali
  4606. mr.lang.insert(StringName("ne_IN"));
  4607. mr.lang.insert(StringName("ne_NP"));
  4608. mr.lang.insert(StringName("sa")); // Sanskrit
  4609. mr.lang.insert(StringName("sa_IN"));
  4610. mr.digits = U"०१२३४५६७८९.";
  4611. mr.percent_sign = U"%";
  4612. mr.exp = U"e";
  4613. num_systems.push_back(mr);
  4614. }
  4615. // Dzongkha numerals.
  4616. {
  4617. NumSystemData dz;
  4618. dz.lang.insert(StringName("dz")); // Dzongkha
  4619. dz.lang.insert(StringName("dz_BT"));
  4620. dz.digits = U"༠༡༢༣༤༥༦༧༨༩.";
  4621. dz.percent_sign = U"%";
  4622. dz.exp = U"e";
  4623. num_systems.push_back(dz);
  4624. }
  4625. // Santali numerals.
  4626. {
  4627. NumSystemData sat;
  4628. sat.lang.insert(StringName("sat")); // Santali
  4629. sat.lang.insert(StringName("sat_IN"));
  4630. sat.lang.insert(StringName("sat_Olck"));
  4631. sat.lang.insert(StringName("sat_Olck_IN"));
  4632. sat.digits = U"᱐᱑᱒᱓᱔᱕᱖᱗᱘᱙.";
  4633. sat.percent_sign = U"%";
  4634. sat.exp = U"e";
  4635. num_systems.push_back(sat);
  4636. }
  4637. // Burmese numerals.
  4638. {
  4639. NumSystemData my;
  4640. my.lang.insert(StringName("my")); // Burmese
  4641. my.lang.insert(StringName("my_MM"));
  4642. my.digits = U"၀၁၂၃၄၅၆၇၈၉.";
  4643. my.percent_sign = U"%";
  4644. my.exp = U"e";
  4645. num_systems.push_back(my);
  4646. }
  4647. // Chakma numerals.
  4648. {
  4649. NumSystemData ccp;
  4650. ccp.lang.insert(StringName("ccp")); // Chakma
  4651. ccp.lang.insert(StringName("ccp_BD"));
  4652. ccp.lang.insert(StringName("ccp_IN"));
  4653. ccp.digits = U"𑄶𑄷𑄸𑄹𑄺𑄻𑄼𑄽𑄾𑄿.";
  4654. ccp.percent_sign = U"%";
  4655. ccp.exp = U"e";
  4656. num_systems.push_back(ccp);
  4657. }
  4658. // Adlam numerals.
  4659. {
  4660. NumSystemData ff;
  4661. ff.lang.insert(StringName("ff")); // Fulah
  4662. ff.lang.insert(StringName("ff_Adlm_BF"));
  4663. ff.lang.insert(StringName("ff_Adlm_CM"));
  4664. ff.lang.insert(StringName("ff_Adlm_GH"));
  4665. ff.lang.insert(StringName("ff_Adlm_GM"));
  4666. ff.lang.insert(StringName("ff_Adlm_GN"));
  4667. ff.lang.insert(StringName("ff_Adlm_GW"));
  4668. ff.lang.insert(StringName("ff_Adlm_LR"));
  4669. ff.lang.insert(StringName("ff_Adlm_MR"));
  4670. ff.lang.insert(StringName("ff_Adlm_NE"));
  4671. ff.lang.insert(StringName("ff_Adlm_NG"));
  4672. ff.lang.insert(StringName("ff_Adlm_SL"));
  4673. ff.lang.insert(StringName("ff_Adlm_SN"));
  4674. ff.digits = U"𞥐𞥑𞥒𞥓𞥔𞥕𞥖𞥗𞥘𞥙.";
  4675. ff.percent_sign = U"%";
  4676. ff.exp = U"e";
  4677. num_systems.push_back(ff);
  4678. }
  4679. }
  4680. String TextServerAdvanced::format_number(const String &p_string, const String &p_language) const {
  4681. const StringName lang = (p_language.is_empty()) ? TranslationServer::get_singleton()->get_tool_locale() : p_language;
  4682. String res = p_string;
  4683. for (int i = 0; i < num_systems.size(); i++) {
  4684. if (num_systems[i].lang.has(lang)) {
  4685. if (num_systems[i].digits.is_empty()) {
  4686. return p_string;
  4687. }
  4688. res.replace("e", num_systems[i].exp);
  4689. res.replace("E", num_systems[i].exp);
  4690. char32_t *data = res.ptrw();
  4691. for (int j = 0; j < res.length(); j++) {
  4692. if (data[j] >= 0x30 && data[j] <= 0x39) {
  4693. data[j] = num_systems[i].digits[data[j] - 0x30];
  4694. } else if (data[j] == '.' || data[j] == ',') {
  4695. data[j] = num_systems[i].digits[10];
  4696. }
  4697. }
  4698. break;
  4699. }
  4700. }
  4701. return res;
  4702. }
  4703. String TextServerAdvanced::parse_number(const String &p_string, const String &p_language) const {
  4704. const StringName lang = (p_language.is_empty()) ? TranslationServer::get_singleton()->get_tool_locale() : p_language;
  4705. String res = p_string;
  4706. for (int i = 0; i < num_systems.size(); i++) {
  4707. if (num_systems[i].lang.has(lang)) {
  4708. if (num_systems[i].digits.is_empty()) {
  4709. return p_string;
  4710. }
  4711. res.replace(num_systems[i].exp, "e");
  4712. char32_t *data = res.ptrw();
  4713. for (int j = 0; j < res.length(); j++) {
  4714. if (data[j] == num_systems[i].digits[10]) {
  4715. data[j] = '.';
  4716. } else {
  4717. for (int k = 0; k < 10; k++) {
  4718. if (data[j] == num_systems[i].digits[k]) {
  4719. data[j] = 0x30 + k;
  4720. }
  4721. }
  4722. }
  4723. }
  4724. break;
  4725. }
  4726. }
  4727. return res;
  4728. }
  4729. String TextServerAdvanced::percent_sign(const String &p_language) const {
  4730. const StringName lang = (p_language.is_empty()) ? TranslationServer::get_singleton()->get_tool_locale() : p_language;
  4731. for (int i = 0; i < num_systems.size(); i++) {
  4732. if (num_systems[i].lang.has(lang)) {
  4733. if (num_systems[i].percent_sign.is_empty()) {
  4734. return "%";
  4735. }
  4736. return num_systems[i].percent_sign;
  4737. }
  4738. }
  4739. return "%";
  4740. }
  4741. String TextServerAdvanced::strip_diacritics(const String &p_string) const {
  4742. UErrorCode err = U_ZERO_ERROR;
  4743. // Get NFKD normalizer singleton.
  4744. const UNormalizer2 *unorm = unorm2_getNFKDInstance(&err);
  4745. ERR_FAIL_COND_V_MSG(U_FAILURE(err), TextServer::strip_diacritics(p_string), u_errorName(err));
  4746. // Convert to UTF-16.
  4747. Char16String utf16 = p_string.utf16();
  4748. // Normalize.
  4749. Vector<char16_t> normalized;
  4750. err = U_ZERO_ERROR;
  4751. int32_t len = unorm2_normalize(unorm, utf16.get_data(), -1, nullptr, 0, &err);
  4752. ERR_FAIL_COND_V_MSG(err != U_BUFFER_OVERFLOW_ERROR, TextServer::strip_diacritics(p_string), u_errorName(err));
  4753. normalized.resize(len);
  4754. err = U_ZERO_ERROR;
  4755. unorm2_normalize(unorm, utf16.get_data(), -1, normalized.ptrw(), len, &err);
  4756. ERR_FAIL_COND_V_MSG(U_FAILURE(err), TextServer::strip_diacritics(p_string), u_errorName(err));
  4757. // Convert back to UTF-32.
  4758. String normalized_string = String::utf16(normalized.ptr(), len);
  4759. // Strip combining characters.
  4760. String result;
  4761. for (int i = 0; i < normalized_string.length(); i++) {
  4762. if (u_getCombiningClass(normalized_string[i]) == 0) {
  4763. result = result + normalized_string[i];
  4764. }
  4765. }
  4766. return result;
  4767. }
  4768. String TextServerAdvanced::string_to_upper(const String &p_string, const String &p_language) const {
  4769. // Convert to UTF-16.
  4770. Char16String utf16 = p_string.utf16();
  4771. Vector<char16_t> upper;
  4772. UErrorCode err = U_ZERO_ERROR;
  4773. int32_t len = u_strToUpper(nullptr, 0, utf16.get_data(), -1, p_language.ascii().get_data(), &err);
  4774. ERR_FAIL_COND_V_MSG(err != U_BUFFER_OVERFLOW_ERROR, p_string, u_errorName(err));
  4775. upper.resize(len);
  4776. err = U_ZERO_ERROR;
  4777. u_strToUpper(upper.ptrw(), len, utf16.get_data(), -1, p_language.ascii().get_data(), &err);
  4778. ERR_FAIL_COND_V_MSG(U_FAILURE(err), p_string, u_errorName(err));
  4779. // Convert back to UTF-32.
  4780. return String::utf16(upper.ptr(), len);
  4781. }
  4782. String TextServerAdvanced::string_to_lower(const String &p_string, const String &p_language) const {
  4783. // Convert to UTF-16.
  4784. Char16String utf16 = p_string.utf16();
  4785. Vector<char16_t> lower;
  4786. UErrorCode err = U_ZERO_ERROR;
  4787. int32_t len = u_strToLower(nullptr, 0, utf16.get_data(), -1, p_language.ascii().get_data(), &err);
  4788. ERR_FAIL_COND_V_MSG(err != U_BUFFER_OVERFLOW_ERROR, p_string, u_errorName(err));
  4789. lower.resize(len);
  4790. err = U_ZERO_ERROR;
  4791. u_strToLower(lower.ptrw(), len, utf16.get_data(), -1, p_language.ascii().get_data(), &err);
  4792. ERR_FAIL_COND_V_MSG(U_FAILURE(err), p_string, u_errorName(err));
  4793. // Convert back to UTF-32.
  4794. return String::utf16(lower.ptr(), len);
  4795. }
  4796. PackedInt32Array TextServerAdvanced::string_get_word_breaks(const String &p_string, const String &p_language) const {
  4797. // Convert to UTF-16.
  4798. Char16String utf16 = p_string.utf16();
  4799. HashSet<int> breaks;
  4800. UErrorCode err = U_ZERO_ERROR;
  4801. UBreakIterator *bi = ubrk_open(UBRK_LINE, p_language.ascii().get_data(), (const UChar *)utf16.get_data(), utf16.length(), &err);
  4802. if (U_FAILURE(err)) {
  4803. // No data loaded - use fallback.
  4804. for (int i = 0; i < p_string.length(); i++) {
  4805. char32_t c = p_string[i];
  4806. if (is_whitespace(c) || is_linebreak(c)) {
  4807. breaks.insert(i);
  4808. }
  4809. }
  4810. } else {
  4811. while (ubrk_next(bi) != UBRK_DONE) {
  4812. int pos = _convert_pos(p_string, utf16, ubrk_current(bi)) - 1;
  4813. if (pos != p_string.length() - 1) {
  4814. breaks.insert(pos);
  4815. }
  4816. }
  4817. }
  4818. ubrk_close(bi);
  4819. PackedInt32Array ret;
  4820. for (int i = 0; i < p_string.length(); i++) {
  4821. char32_t c = p_string[i];
  4822. if (c == 0xfffc) {
  4823. continue;
  4824. }
  4825. if (u_ispunct(c) && c != 0x005F) {
  4826. ret.push_back(i);
  4827. continue;
  4828. }
  4829. if (is_underscore(c)) {
  4830. ret.push_back(i);
  4831. continue;
  4832. }
  4833. if (breaks.has(i)) {
  4834. ret.push_back(i);
  4835. continue;
  4836. }
  4837. }
  4838. return ret;
  4839. }
  4840. TextServerAdvanced::TextServerAdvanced() {
  4841. _insert_num_systems_lang();
  4842. _insert_feature_sets();
  4843. _bmp_create_font_funcs();
  4844. }
  4845. TextServerAdvanced::~TextServerAdvanced() {
  4846. _bmp_free_font_funcs();
  4847. #ifdef MODULE_FREETYPE_ENABLED
  4848. if (ft_library != nullptr) {
  4849. FT_Done_FreeType(ft_library);
  4850. }
  4851. #endif
  4852. u_cleanup();
  4853. }