text_server_fb.cpp 155 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620
  1. /**************************************************************************/
  2. /* text_server_fb.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "text_server_fb.h"
  31. #ifdef GDEXTENSION
  32. // Headers for building as GDExtension plug-in.
  33. #include <godot_cpp/classes/file_access.hpp>
  34. #include <godot_cpp/classes/os.hpp>
  35. #include <godot_cpp/classes/project_settings.hpp>
  36. #include <godot_cpp/classes/rendering_server.hpp>
  37. #include <godot_cpp/classes/translation_server.hpp>
  38. #include <godot_cpp/core/error_macros.hpp>
  39. #define OT_TAG(m_c1, m_c2, m_c3, m_c4) ((int32_t)((((uint32_t)(m_c1) & 0xff) << 24) | (((uint32_t)(m_c2) & 0xff) << 16) | (((uint32_t)(m_c3) & 0xff) << 8) | ((uint32_t)(m_c4) & 0xff)))
  40. using namespace godot;
  41. #define GLOBAL_GET(m_var) ProjectSettings::get_singleton()->get_setting_with_override(m_var)
  42. #elif defined(GODOT_MODULE)
  43. // Headers for building as built-in module.
  44. #include "core/config/project_settings.h"
  45. #include "core/error/error_macros.h"
  46. #include "core/string/print_string.h"
  47. #include "core/string/translation.h"
  48. #include "modules/modules_enabled.gen.h" // For freetype, msdfgen, svg.
  49. #endif
  50. // Thirdparty headers.
  51. #ifdef MODULE_MSDFGEN_ENABLED
  52. #ifdef _MSC_VER
  53. #pragma warning(disable : 4458)
  54. #endif
  55. #include <core/ShapeDistanceFinder.h>
  56. #include <core/contour-combiners.h>
  57. #include <core/edge-selectors.h>
  58. #include <msdfgen.h>
  59. #ifdef _MSC_VER
  60. #pragma warning(default : 4458)
  61. #endif
  62. #endif
  63. #ifdef MODULE_FREETYPE_ENABLED
  64. #include FT_SFNT_NAMES_H
  65. #include FT_TRUETYPE_IDS_H
  66. #ifdef MODULE_SVG_ENABLED
  67. #include "thorvg_svg_in_ot.h"
  68. #endif
  69. #endif
  70. /*************************************************************************/
  71. bool TextServerFallback::_has_feature(Feature p_feature) const {
  72. switch (p_feature) {
  73. case FEATURE_SIMPLE_LAYOUT:
  74. case FEATURE_FONT_BITMAP:
  75. #ifdef MODULE_FREETYPE_ENABLED
  76. case FEATURE_FONT_DYNAMIC:
  77. #endif
  78. #ifdef MODULE_MSDFGEN_ENABLED
  79. case FEATURE_FONT_MSDF:
  80. #endif
  81. return true;
  82. default: {
  83. }
  84. }
  85. return false;
  86. }
  87. String TextServerFallback::_get_name() const {
  88. #ifdef GDEXTENSION
  89. return "Fallback (GDExtension)";
  90. #elif defined(GODOT_MODULE)
  91. return "Fallback (Built-in)";
  92. #endif
  93. }
  94. int64_t TextServerFallback::_get_features() const {
  95. int64_t interface_features = FEATURE_SIMPLE_LAYOUT | FEATURE_FONT_BITMAP;
  96. #ifdef MODULE_FREETYPE_ENABLED
  97. interface_features |= FEATURE_FONT_DYNAMIC;
  98. #endif
  99. #ifdef MODULE_MSDFGEN_ENABLED
  100. interface_features |= FEATURE_FONT_MSDF;
  101. #endif
  102. return interface_features;
  103. }
  104. void TextServerFallback::_free_rid(const RID &p_rid) {
  105. _THREAD_SAFE_METHOD_
  106. if (font_owner.owns(p_rid)) {
  107. MutexLock ftlock(ft_mutex);
  108. FontFallback *fd = font_owner.get_or_null(p_rid);
  109. {
  110. MutexLock lock(fd->mutex);
  111. font_owner.free(p_rid);
  112. }
  113. memdelete(fd);
  114. } else if (font_var_owner.owns(p_rid)) {
  115. MutexLock ftlock(ft_mutex);
  116. FontFallbackLinkedVariation *fdv = font_var_owner.get_or_null(p_rid);
  117. {
  118. font_var_owner.free(p_rid);
  119. }
  120. memdelete(fdv);
  121. } else if (shaped_owner.owns(p_rid)) {
  122. ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_rid);
  123. {
  124. MutexLock lock(sd->mutex);
  125. shaped_owner.free(p_rid);
  126. }
  127. memdelete(sd);
  128. }
  129. }
  130. bool TextServerFallback::_has(const RID &p_rid) {
  131. _THREAD_SAFE_METHOD_
  132. return font_owner.owns(p_rid) || shaped_owner.owns(p_rid);
  133. }
  134. String TextServerFallback::_get_support_data_filename() const {
  135. return "";
  136. };
  137. String TextServerFallback::_get_support_data_info() const {
  138. return "Not supported";
  139. };
  140. bool TextServerFallback::_load_support_data(const String &p_filename) {
  141. return false; // No extra data used.
  142. }
  143. bool TextServerFallback::_save_support_data(const String &p_filename) const {
  144. return false; // No extra data used.
  145. }
  146. bool TextServerFallback::_is_locale_right_to_left(const String &p_locale) const {
  147. return false; // No RTL support.
  148. }
  149. _FORCE_INLINE_ void TextServerFallback::_insert_feature(const StringName &p_name, int32_t p_tag) {
  150. feature_sets.insert(p_name, p_tag);
  151. feature_sets_inv.insert(p_tag, p_name);
  152. }
  153. void TextServerFallback::_insert_feature_sets() {
  154. // Registered OpenType variation tag.
  155. _insert_feature("italic", OT_TAG('i', 't', 'a', 'l'));
  156. _insert_feature("optical_size", OT_TAG('o', 'p', 's', 'z'));
  157. _insert_feature("slant", OT_TAG('s', 'l', 'n', 't'));
  158. _insert_feature("width", OT_TAG('w', 'd', 't', 'h'));
  159. _insert_feature("weight", OT_TAG('w', 'g', 'h', 't'));
  160. }
  161. _FORCE_INLINE_ int32_t ot_tag_from_string(const char *p_str, int p_len) {
  162. char tag[4];
  163. uint32_t i;
  164. if (!p_str || !p_len || !*p_str) {
  165. return OT_TAG(0, 0, 0, 0);
  166. }
  167. if (p_len < 0 || p_len > 4) {
  168. p_len = 4;
  169. }
  170. for (i = 0; i < (uint32_t)p_len && p_str[i]; i++) {
  171. tag[i] = p_str[i];
  172. }
  173. for (; i < 4; i++) {
  174. tag[i] = ' ';
  175. }
  176. return OT_TAG(tag[0], tag[1], tag[2], tag[3]);
  177. }
  178. int64_t TextServerFallback::_name_to_tag(const String &p_name) const {
  179. if (feature_sets.has(p_name)) {
  180. return feature_sets[p_name];
  181. }
  182. // No readable name, use tag string.
  183. return ot_tag_from_string(p_name.replace("custom_", "").ascii().get_data(), -1);
  184. }
  185. _FORCE_INLINE_ void ot_tag_to_string(int32_t p_tag, char *p_buf) {
  186. p_buf[0] = (char)(uint8_t)(p_tag >> 24);
  187. p_buf[1] = (char)(uint8_t)(p_tag >> 16);
  188. p_buf[2] = (char)(uint8_t)(p_tag >> 8);
  189. p_buf[3] = (char)(uint8_t)(p_tag >> 0);
  190. }
  191. String TextServerFallback::_tag_to_name(int64_t p_tag) const {
  192. if (feature_sets_inv.has(p_tag)) {
  193. return feature_sets_inv[p_tag];
  194. }
  195. // No readable name, use tag string.
  196. char name[5];
  197. memset(name, 0, 5);
  198. ot_tag_to_string(p_tag, name);
  199. return String("custom_") + String(name);
  200. }
  201. /*************************************************************************/
  202. /* Font Glyph Rendering */
  203. /*************************************************************************/
  204. _FORCE_INLINE_ TextServerFallback::FontTexturePosition TextServerFallback::find_texture_pos_for_glyph(FontForSizeFallback *p_data, int p_color_size, Image::Format p_image_format, int p_width, int p_height, bool p_msdf) const {
  205. FontTexturePosition ret;
  206. int mw = p_width;
  207. int mh = p_height;
  208. ShelfPackTexture *ct = p_data->textures.ptrw();
  209. for (int32_t i = 0; i < p_data->textures.size(); i++) {
  210. if (ct[i].image.is_null()) {
  211. continue;
  212. }
  213. if (p_image_format != ct[i].image->get_format()) {
  214. continue;
  215. }
  216. if (mw > ct[i].texture_w || mh > ct[i].texture_h) { // Too big for this texture.
  217. continue;
  218. }
  219. ret = ct[i].pack_rect(i, mh, mw);
  220. if (ret.index != -1) {
  221. break;
  222. }
  223. }
  224. if (ret.index == -1) {
  225. // Could not find texture to fit, create one.
  226. int texsize = MAX(p_data->size.x * p_data->oversampling * 8, 256);
  227. texsize = next_power_of_2(texsize);
  228. if (p_msdf) {
  229. texsize = MIN(texsize, 2048);
  230. } else {
  231. texsize = MIN(texsize, 1024);
  232. }
  233. if (mw > texsize) { // Special case, adapt to it?
  234. texsize = next_power_of_2(mw);
  235. }
  236. if (mh > texsize) { // Special case, adapt to it?
  237. texsize = next_power_of_2(mh);
  238. }
  239. ShelfPackTexture tex = ShelfPackTexture(texsize, texsize);
  240. tex.image = Image::create_empty(texsize, texsize, false, p_image_format);
  241. {
  242. // Zero texture.
  243. uint8_t *w = tex.image->ptrw();
  244. ERR_FAIL_COND_V(texsize * texsize * p_color_size > tex.image->get_data_size(), ret);
  245. // Initialize the texture to all-white pixels to prevent artifacts when the
  246. // font is displayed at a non-default scale with filtering enabled.
  247. if (p_color_size == 2) {
  248. for (int i = 0; i < texsize * texsize * p_color_size; i += 2) { // FORMAT_LA8, BW font.
  249. w[i + 0] = 255;
  250. w[i + 1] = 0;
  251. }
  252. } else if (p_color_size == 4) {
  253. for (int i = 0; i < texsize * texsize * p_color_size; i += 4) { // FORMAT_RGBA8, Color font, Multichannel(+True) SDF.
  254. w[i + 0] = 255;
  255. w[i + 1] = 255;
  256. w[i + 2] = 255;
  257. w[i + 3] = 0;
  258. }
  259. } else {
  260. ERR_FAIL_V(ret);
  261. }
  262. }
  263. p_data->textures.push_back(tex);
  264. int32_t idx = p_data->textures.size() - 1;
  265. ret = p_data->textures.write[idx].pack_rect(idx, mh, mw);
  266. }
  267. return ret;
  268. }
  269. #ifdef MODULE_MSDFGEN_ENABLED
  270. struct MSContext {
  271. msdfgen::Point2 position;
  272. msdfgen::Shape *shape = nullptr;
  273. msdfgen::Contour *contour = nullptr;
  274. };
  275. class DistancePixelConversion {
  276. double invRange;
  277. public:
  278. _FORCE_INLINE_ explicit DistancePixelConversion(double range) :
  279. invRange(1 / range) {}
  280. _FORCE_INLINE_ void operator()(float *pixels, const msdfgen::MultiAndTrueDistance &distance) const {
  281. pixels[0] = float(invRange * distance.r + .5);
  282. pixels[1] = float(invRange * distance.g + .5);
  283. pixels[2] = float(invRange * distance.b + .5);
  284. pixels[3] = float(invRange * distance.a + .5);
  285. }
  286. };
  287. struct MSDFThreadData {
  288. msdfgen::Bitmap<float, 4> *output;
  289. msdfgen::Shape *shape;
  290. msdfgen::Projection *projection;
  291. DistancePixelConversion *distancePixelConversion;
  292. };
  293. static msdfgen::Point2 ft_point2(const FT_Vector &vector) {
  294. return msdfgen::Point2(vector.x / 60.0f, vector.y / 60.0f);
  295. }
  296. static int ft_move_to(const FT_Vector *to, void *user) {
  297. MSContext *context = static_cast<MSContext *>(user);
  298. if (!(context->contour && context->contour->edges.empty())) {
  299. context->contour = &context->shape->addContour();
  300. }
  301. context->position = ft_point2(*to);
  302. return 0;
  303. }
  304. static int ft_line_to(const FT_Vector *to, void *user) {
  305. MSContext *context = static_cast<MSContext *>(user);
  306. msdfgen::Point2 endpoint = ft_point2(*to);
  307. if (endpoint != context->position) {
  308. context->contour->addEdge(new msdfgen::LinearSegment(context->position, endpoint));
  309. context->position = endpoint;
  310. }
  311. return 0;
  312. }
  313. static int ft_conic_to(const FT_Vector *control, const FT_Vector *to, void *user) {
  314. MSContext *context = static_cast<MSContext *>(user);
  315. context->contour->addEdge(new msdfgen::QuadraticSegment(context->position, ft_point2(*control), ft_point2(*to)));
  316. context->position = ft_point2(*to);
  317. return 0;
  318. }
  319. static int ft_cubic_to(const FT_Vector *control1, const FT_Vector *control2, const FT_Vector *to, void *user) {
  320. MSContext *context = static_cast<MSContext *>(user);
  321. context->contour->addEdge(new msdfgen::CubicSegment(context->position, ft_point2(*control1), ft_point2(*control2), ft_point2(*to)));
  322. context->position = ft_point2(*to);
  323. return 0;
  324. }
  325. void TextServerFallback::_generateMTSDF_threaded(void *p_td, uint32_t p_y) {
  326. MSDFThreadData *td = static_cast<MSDFThreadData *>(p_td);
  327. msdfgen::ShapeDistanceFinder<msdfgen::OverlappingContourCombiner<msdfgen::MultiAndTrueDistanceSelector>> distanceFinder(*td->shape);
  328. int row = td->shape->inverseYAxis ? td->output->height() - p_y - 1 : p_y;
  329. for (int col = 0; col < td->output->width(); ++col) {
  330. int x = (p_y % 2) ? td->output->width() - col - 1 : col;
  331. msdfgen::Point2 p = td->projection->unproject(msdfgen::Point2(x + .5, p_y + .5));
  332. msdfgen::MultiAndTrueDistance distance = distanceFinder.distance(p);
  333. td->distancePixelConversion->operator()(td->output->operator()(x, row), distance);
  334. }
  335. }
  336. _FORCE_INLINE_ TextServerFallback::FontGlyph TextServerFallback::rasterize_msdf(FontFallback *p_font_data, FontForSizeFallback *p_data, int p_pixel_range, int p_rect_margin, FT_Outline *p_outline, const Vector2 &p_advance) const {
  337. msdfgen::Shape shape;
  338. shape.contours.clear();
  339. shape.inverseYAxis = false;
  340. MSContext context = {};
  341. context.shape = &shape;
  342. FT_Outline_Funcs ft_functions;
  343. ft_functions.move_to = &ft_move_to;
  344. ft_functions.line_to = &ft_line_to;
  345. ft_functions.conic_to = &ft_conic_to;
  346. ft_functions.cubic_to = &ft_cubic_to;
  347. ft_functions.shift = 0;
  348. ft_functions.delta = 0;
  349. int error = FT_Outline_Decompose(p_outline, &ft_functions, &context);
  350. ERR_FAIL_COND_V_MSG(error, FontGlyph(), "FreeType: Outline decomposition error: '" + String(FT_Error_String(error)) + "'.");
  351. if (!shape.contours.empty() && shape.contours.back().edges.empty()) {
  352. shape.contours.pop_back();
  353. }
  354. if (FT_Outline_Get_Orientation(p_outline) == 1) {
  355. for (int i = 0; i < (int)shape.contours.size(); ++i) {
  356. shape.contours[i].reverse();
  357. }
  358. }
  359. shape.inverseYAxis = true;
  360. shape.normalize();
  361. msdfgen::Shape::Bounds bounds = shape.getBounds(p_pixel_range);
  362. FontGlyph chr;
  363. chr.found = true;
  364. chr.advance = p_advance;
  365. if (shape.validate() && shape.contours.size() > 0) {
  366. int w = (bounds.r - bounds.l);
  367. int h = (bounds.t - bounds.b);
  368. if (w == 0 || h == 0) {
  369. chr.texture_idx = -1;
  370. chr.uv_rect = Rect2();
  371. chr.rect = Rect2();
  372. return chr;
  373. }
  374. int mw = w + p_rect_margin * 4;
  375. int mh = h + p_rect_margin * 4;
  376. ERR_FAIL_COND_V(mw > 4096, FontGlyph());
  377. ERR_FAIL_COND_V(mh > 4096, FontGlyph());
  378. FontTexturePosition tex_pos = find_texture_pos_for_glyph(p_data, 4, Image::FORMAT_RGBA8, mw, mh, true);
  379. ERR_FAIL_COND_V(tex_pos.index < 0, FontGlyph());
  380. ShelfPackTexture &tex = p_data->textures.write[tex_pos.index];
  381. edgeColoringSimple(shape, 3.0); // Max. angle.
  382. msdfgen::Bitmap<float, 4> image(w, h); // Texture size.
  383. DistancePixelConversion distancePixelConversion(p_pixel_range);
  384. msdfgen::Projection projection(msdfgen::Vector2(1.0, 1.0), msdfgen::Vector2(-bounds.l, -bounds.b));
  385. msdfgen::MSDFGeneratorConfig config(true, msdfgen::ErrorCorrectionConfig());
  386. MSDFThreadData td;
  387. td.output = &image;
  388. td.shape = &shape;
  389. td.projection = &projection;
  390. td.distancePixelConversion = &distancePixelConversion;
  391. WorkerThreadPool::GroupID group_task = WorkerThreadPool::get_singleton()->add_native_group_task(&TextServerFallback::_generateMTSDF_threaded, &td, h, -1, true, String("TextServerFBRenderMSDF"));
  392. WorkerThreadPool::get_singleton()->wait_for_group_task_completion(group_task);
  393. msdfgen::msdfErrorCorrection(image, shape, projection, p_pixel_range, config);
  394. {
  395. uint8_t *wr = tex.image->ptrw();
  396. for (int i = 0; i < h; i++) {
  397. for (int j = 0; j < w; j++) {
  398. int ofs = ((i + tex_pos.y + p_rect_margin * 2) * tex.texture_w + j + tex_pos.x + p_rect_margin * 2) * 4;
  399. ERR_FAIL_COND_V(ofs >= tex.image->get_data_size(), FontGlyph());
  400. wr[ofs + 0] = (uint8_t)(CLAMP(image(j, i)[0] * 256.f, 0.f, 255.f));
  401. wr[ofs + 1] = (uint8_t)(CLAMP(image(j, i)[1] * 256.f, 0.f, 255.f));
  402. wr[ofs + 2] = (uint8_t)(CLAMP(image(j, i)[2] * 256.f, 0.f, 255.f));
  403. wr[ofs + 3] = (uint8_t)(CLAMP(image(j, i)[3] * 256.f, 0.f, 255.f));
  404. }
  405. }
  406. }
  407. tex.dirty = true;
  408. chr.texture_idx = tex_pos.index;
  409. 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);
  410. chr.rect.position = Vector2(bounds.l - p_rect_margin, -bounds.t - p_rect_margin);
  411. chr.rect.size = chr.uv_rect.size;
  412. }
  413. return chr;
  414. }
  415. #endif
  416. #ifdef MODULE_FREETYPE_ENABLED
  417. _FORCE_INLINE_ TextServerFallback::FontGlyph TextServerFallback::rasterize_bitmap(FontForSizeFallback *p_data, int p_rect_margin, FT_Bitmap p_bitmap, int p_yofs, int p_xofs, const Vector2 &p_advance, bool p_bgra) const {
  418. FontGlyph chr;
  419. chr.advance = p_advance * p_data->scale / p_data->oversampling;
  420. chr.found = true;
  421. int w = p_bitmap.width;
  422. int h = p_bitmap.rows;
  423. if (w == 0 || h == 0) {
  424. chr.texture_idx = -1;
  425. chr.uv_rect = Rect2();
  426. chr.rect = Rect2();
  427. return chr;
  428. }
  429. int color_size = 2;
  430. switch (p_bitmap.pixel_mode) {
  431. case FT_PIXEL_MODE_MONO:
  432. case FT_PIXEL_MODE_GRAY: {
  433. color_size = 2;
  434. } break;
  435. case FT_PIXEL_MODE_BGRA: {
  436. color_size = 4;
  437. } break;
  438. case FT_PIXEL_MODE_LCD: {
  439. color_size = 4;
  440. w /= 3;
  441. } break;
  442. case FT_PIXEL_MODE_LCD_V: {
  443. color_size = 4;
  444. h /= 3;
  445. } break;
  446. }
  447. int mw = w + p_rect_margin * 4;
  448. int mh = h + p_rect_margin * 4;
  449. ERR_FAIL_COND_V(mw > 4096, FontGlyph());
  450. ERR_FAIL_COND_V(mh > 4096, FontGlyph());
  451. Image::Format require_format = color_size == 4 ? Image::FORMAT_RGBA8 : Image::FORMAT_LA8;
  452. FontTexturePosition tex_pos = find_texture_pos_for_glyph(p_data, color_size, require_format, mw, mh, false);
  453. ERR_FAIL_COND_V(tex_pos.index < 0, FontGlyph());
  454. // Fit character in char texture.
  455. ShelfPackTexture &tex = p_data->textures.write[tex_pos.index];
  456. {
  457. uint8_t *wr = tex.image->ptrw();
  458. for (int i = 0; i < h; i++) {
  459. for (int j = 0; j < w; j++) {
  460. int ofs = ((i + tex_pos.y + p_rect_margin * 2) * tex.texture_w + j + tex_pos.x + p_rect_margin * 2) * color_size;
  461. ERR_FAIL_COND_V(ofs >= tex.image->get_data_size(), FontGlyph());
  462. switch (p_bitmap.pixel_mode) {
  463. case FT_PIXEL_MODE_MONO: {
  464. int byte = i * p_bitmap.pitch + (j >> 3);
  465. int bit = 1 << (7 - (j % 8));
  466. wr[ofs + 0] = 255; // grayscale as 1
  467. wr[ofs + 1] = (p_bitmap.buffer[byte] & bit) ? 255 : 0;
  468. } break;
  469. case FT_PIXEL_MODE_GRAY:
  470. wr[ofs + 0] = 255; // grayscale as 1
  471. wr[ofs + 1] = p_bitmap.buffer[i * p_bitmap.pitch + j];
  472. break;
  473. case FT_PIXEL_MODE_BGRA: {
  474. int ofs_color = i * p_bitmap.pitch + (j << 2);
  475. wr[ofs + 2] = p_bitmap.buffer[ofs_color + 0];
  476. wr[ofs + 1] = p_bitmap.buffer[ofs_color + 1];
  477. wr[ofs + 0] = p_bitmap.buffer[ofs_color + 2];
  478. wr[ofs + 3] = p_bitmap.buffer[ofs_color + 3];
  479. } break;
  480. case FT_PIXEL_MODE_LCD: {
  481. int ofs_color = i * p_bitmap.pitch + (j * 3);
  482. if (p_bgra) {
  483. wr[ofs + 0] = p_bitmap.buffer[ofs_color + 2];
  484. wr[ofs + 1] = p_bitmap.buffer[ofs_color + 1];
  485. wr[ofs + 2] = p_bitmap.buffer[ofs_color + 0];
  486. wr[ofs + 3] = 255;
  487. } else {
  488. wr[ofs + 0] = p_bitmap.buffer[ofs_color + 0];
  489. wr[ofs + 1] = p_bitmap.buffer[ofs_color + 1];
  490. wr[ofs + 2] = p_bitmap.buffer[ofs_color + 2];
  491. wr[ofs + 3] = 255;
  492. }
  493. } break;
  494. case FT_PIXEL_MODE_LCD_V: {
  495. int ofs_color = i * p_bitmap.pitch * 3 + j;
  496. if (p_bgra) {
  497. wr[ofs + 0] = p_bitmap.buffer[ofs_color + p_bitmap.pitch * 2];
  498. wr[ofs + 1] = p_bitmap.buffer[ofs_color + p_bitmap.pitch];
  499. wr[ofs + 2] = p_bitmap.buffer[ofs_color + 0];
  500. wr[ofs + 3] = 255;
  501. } else {
  502. wr[ofs + 0] = p_bitmap.buffer[ofs_color + 0];
  503. wr[ofs + 1] = p_bitmap.buffer[ofs_color + p_bitmap.pitch];
  504. wr[ofs + 2] = p_bitmap.buffer[ofs_color + p_bitmap.pitch * 2];
  505. wr[ofs + 3] = 255;
  506. }
  507. } break;
  508. default:
  509. ERR_FAIL_V_MSG(FontGlyph(), "Font uses unsupported pixel format: " + String::num_int64(p_bitmap.pixel_mode) + ".");
  510. break;
  511. }
  512. }
  513. }
  514. }
  515. tex.dirty = true;
  516. chr.texture_idx = tex_pos.index;
  517. 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);
  518. chr.rect.position = Vector2(p_xofs - p_rect_margin, -p_yofs - p_rect_margin) * p_data->scale / p_data->oversampling;
  519. chr.rect.size = chr.uv_rect.size * p_data->scale / p_data->oversampling;
  520. return chr;
  521. }
  522. #endif
  523. /*************************************************************************/
  524. /* Font Cache */
  525. /*************************************************************************/
  526. _FORCE_INLINE_ bool TextServerFallback::_ensure_glyph(FontFallback *p_font_data, const Vector2i &p_size, int32_t p_glyph) const {
  527. ERR_FAIL_COND_V(!_ensure_cache_for_size(p_font_data, p_size), false);
  528. int32_t glyph_index = p_glyph & 0xffffff; // Remove subpixel shifts.
  529. FontForSizeFallback *fd = p_font_data->cache[p_size];
  530. if (fd->glyph_map.has(p_glyph)) {
  531. return fd->glyph_map[p_glyph].found;
  532. }
  533. if (glyph_index == 0) { // Non graphical or invalid glyph, do not render.
  534. fd->glyph_map[p_glyph] = FontGlyph();
  535. return true;
  536. }
  537. #ifdef MODULE_FREETYPE_ENABLED
  538. FontGlyph gl;
  539. if (fd->face) {
  540. FT_Int32 flags = FT_LOAD_DEFAULT;
  541. bool outline = p_size.y > 0;
  542. switch (p_font_data->hinting) {
  543. case TextServer::HINTING_NONE:
  544. flags |= FT_LOAD_NO_HINTING;
  545. break;
  546. case TextServer::HINTING_LIGHT:
  547. flags |= FT_LOAD_TARGET_LIGHT;
  548. break;
  549. default:
  550. flags |= FT_LOAD_TARGET_NORMAL;
  551. break;
  552. }
  553. if (p_font_data->force_autohinter) {
  554. flags |= FT_LOAD_FORCE_AUTOHINT;
  555. }
  556. if (outline || (p_font_data->disable_embedded_bitmaps && !FT_HAS_COLOR(fd->face))) {
  557. flags |= FT_LOAD_NO_BITMAP;
  558. } else if (FT_HAS_COLOR(fd->face)) {
  559. flags |= FT_LOAD_COLOR;
  560. }
  561. glyph_index = FT_Get_Char_Index(fd->face, glyph_index);
  562. FT_Fixed v, h;
  563. FT_Get_Advance(fd->face, glyph_index, flags, &h);
  564. FT_Get_Advance(fd->face, glyph_index, flags | FT_LOAD_VERTICAL_LAYOUT, &v);
  565. int error = FT_Load_Glyph(fd->face, glyph_index, flags);
  566. if (error) {
  567. fd->glyph_map[p_glyph] = FontGlyph();
  568. return false;
  569. }
  570. if (!p_font_data->msdf) {
  571. 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)) {
  572. FT_Pos xshift = (int)((p_glyph >> 27) & 3) << 4;
  573. FT_Outline_Translate(&fd->face->glyph->outline, xshift, 0);
  574. } 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)) {
  575. FT_Pos xshift = (int)((p_glyph >> 27) & 3) << 5;
  576. FT_Outline_Translate(&fd->face->glyph->outline, xshift, 0);
  577. }
  578. }
  579. if (p_font_data->embolden != 0.f) {
  580. FT_Pos strength = p_font_data->embolden * p_size.x * fd->oversampling * 4; // 26.6 fractional units (1 / 64).
  581. FT_Outline_Embolden(&fd->face->glyph->outline, strength);
  582. }
  583. if (p_font_data->transform != Transform2D()) {
  584. 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).
  585. FT_Outline_Transform(&fd->face->glyph->outline, &mat);
  586. }
  587. FT_Render_Mode aa_mode = FT_RENDER_MODE_NORMAL;
  588. bool bgra = false;
  589. switch (p_font_data->antialiasing) {
  590. case FONT_ANTIALIASING_NONE: {
  591. aa_mode = FT_RENDER_MODE_MONO;
  592. } break;
  593. case FONT_ANTIALIASING_GRAY: {
  594. aa_mode = FT_RENDER_MODE_NORMAL;
  595. } break;
  596. case FONT_ANTIALIASING_LCD: {
  597. int aa_layout = (int)((p_glyph >> 24) & 7);
  598. switch (aa_layout) {
  599. case FONT_LCD_SUBPIXEL_LAYOUT_HRGB: {
  600. aa_mode = FT_RENDER_MODE_LCD;
  601. bgra = false;
  602. } break;
  603. case FONT_LCD_SUBPIXEL_LAYOUT_HBGR: {
  604. aa_mode = FT_RENDER_MODE_LCD;
  605. bgra = true;
  606. } break;
  607. case FONT_LCD_SUBPIXEL_LAYOUT_VRGB: {
  608. aa_mode = FT_RENDER_MODE_LCD_V;
  609. bgra = false;
  610. } break;
  611. case FONT_LCD_SUBPIXEL_LAYOUT_VBGR: {
  612. aa_mode = FT_RENDER_MODE_LCD_V;
  613. bgra = true;
  614. } break;
  615. default: {
  616. aa_mode = FT_RENDER_MODE_NORMAL;
  617. } break;
  618. }
  619. } break;
  620. }
  621. if (!outline) {
  622. if (!p_font_data->msdf) {
  623. error = FT_Render_Glyph(fd->face->glyph, aa_mode);
  624. }
  625. FT_GlyphSlot slot = fd->face->glyph;
  626. if (!error) {
  627. if (p_font_data->msdf) {
  628. #ifdef MODULE_MSDFGEN_ENABLED
  629. 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);
  630. #else
  631. fd->glyph_map[p_glyph] = FontGlyph();
  632. ERR_FAIL_V_MSG(false, "Compiled without MSDFGEN support!");
  633. #endif
  634. } else {
  635. gl = rasterize_bitmap(fd, rect_range, slot->bitmap, slot->bitmap_top, slot->bitmap_left, Vector2((h + (1 << 9)) >> 10, (v + (1 << 9)) >> 10) / 64.0, bgra);
  636. }
  637. }
  638. } else {
  639. FT_Stroker stroker;
  640. if (FT_Stroker_New(ft_library, &stroker) != 0) {
  641. fd->glyph_map[p_glyph] = FontGlyph();
  642. ERR_FAIL_V_MSG(false, "FreeType: Failed to load glyph stroker.");
  643. }
  644. FT_Stroker_Set(stroker, (int)(fd->size.y * fd->oversampling * 16.0), FT_STROKER_LINECAP_BUTT, FT_STROKER_LINEJOIN_ROUND, 0);
  645. FT_Glyph glyph;
  646. FT_BitmapGlyph glyph_bitmap;
  647. if (FT_Get_Glyph(fd->face->glyph, &glyph) != 0) {
  648. goto cleanup_stroker;
  649. }
  650. if (FT_Glyph_Stroke(&glyph, stroker, 1) != 0) {
  651. goto cleanup_glyph;
  652. }
  653. if (FT_Glyph_To_Bitmap(&glyph, aa_mode, nullptr, 1) != 0) {
  654. goto cleanup_glyph;
  655. }
  656. glyph_bitmap = (FT_BitmapGlyph)glyph;
  657. gl = rasterize_bitmap(fd, rect_range, glyph_bitmap->bitmap, glyph_bitmap->top, glyph_bitmap->left, Vector2(), bgra);
  658. cleanup_glyph:
  659. FT_Done_Glyph(glyph);
  660. cleanup_stroker:
  661. FT_Stroker_Done(stroker);
  662. }
  663. fd->glyph_map[p_glyph] = gl;
  664. return gl.found;
  665. }
  666. #endif
  667. fd->glyph_map[p_glyph] = FontGlyph();
  668. return false;
  669. }
  670. _FORCE_INLINE_ bool TextServerFallback::_ensure_cache_for_size(FontFallback *p_font_data, const Vector2i &p_size) const {
  671. ERR_FAIL_COND_V(p_size.x <= 0, false);
  672. if (p_font_data->cache.has(p_size)) {
  673. return true;
  674. }
  675. FontForSizeFallback *fd = memnew(FontForSizeFallback);
  676. fd->size = p_size;
  677. if (p_font_data->data_ptr && (p_font_data->data_size > 0)) {
  678. // Init dynamic font.
  679. #ifdef MODULE_FREETYPE_ENABLED
  680. int error = 0;
  681. {
  682. MutexLock ftlock(ft_mutex);
  683. if (!ft_library) {
  684. error = FT_Init_FreeType(&ft_library);
  685. if (error != 0) {
  686. memdelete(fd);
  687. ERR_FAIL_V_MSG(false, "FreeType: Error initializing library: '" + String(FT_Error_String(error)) + "'.");
  688. }
  689. #ifdef MODULE_SVG_ENABLED
  690. FT_Property_Set(ft_library, "ot-svg", "svg-hooks", get_tvg_svg_in_ot_hooks());
  691. #endif
  692. }
  693. memset(&fd->stream, 0, sizeof(FT_StreamRec));
  694. fd->stream.base = (unsigned char *)p_font_data->data_ptr;
  695. fd->stream.size = p_font_data->data_size;
  696. fd->stream.pos = 0;
  697. FT_Open_Args fargs;
  698. memset(&fargs, 0, sizeof(FT_Open_Args));
  699. fargs.memory_base = (unsigned char *)p_font_data->data_ptr;
  700. fargs.memory_size = p_font_data->data_size;
  701. fargs.flags = FT_OPEN_MEMORY;
  702. fargs.stream = &fd->stream;
  703. int max_index = 0;
  704. FT_Face tmp_face = nullptr;
  705. error = FT_Open_Face(ft_library, &fargs, -1, &tmp_face);
  706. if (tmp_face && error == 0) {
  707. max_index = tmp_face->num_faces - 1;
  708. }
  709. if (tmp_face) {
  710. FT_Done_Face(tmp_face);
  711. }
  712. error = FT_Open_Face(ft_library, &fargs, CLAMP(p_font_data->face_index, 0, max_index), &fd->face);
  713. if (error) {
  714. FT_Done_Face(fd->face);
  715. fd->face = nullptr;
  716. memdelete(fd);
  717. ERR_FAIL_V_MSG(false, "FreeType: Error loading font: '" + String(FT_Error_String(error)) + "'.");
  718. }
  719. }
  720. if (p_font_data->msdf) {
  721. fd->oversampling = 1.0;
  722. fd->size.x = p_font_data->msdf_source_size;
  723. } else if (p_font_data->oversampling <= 0.0) {
  724. fd->oversampling = _font_get_global_oversampling();
  725. } else {
  726. fd->oversampling = p_font_data->oversampling;
  727. }
  728. if (FT_HAS_COLOR(fd->face) && fd->face->num_fixed_sizes > 0) {
  729. int best_match = 0;
  730. int diff = ABS(fd->size.x - ((int64_t)fd->face->available_sizes[0].width));
  731. fd->scale = double(fd->size.x * fd->oversampling) / fd->face->available_sizes[0].width;
  732. for (int i = 1; i < fd->face->num_fixed_sizes; i++) {
  733. int ndiff = ABS(fd->size.x - ((int64_t)fd->face->available_sizes[i].width));
  734. if (ndiff < diff) {
  735. best_match = i;
  736. diff = ndiff;
  737. fd->scale = double(fd->size.x * fd->oversampling) / fd->face->available_sizes[i].width;
  738. }
  739. }
  740. FT_Select_Size(fd->face, best_match);
  741. } else {
  742. FT_Set_Pixel_Sizes(fd->face, 0, Math::round(fd->size.x * fd->oversampling));
  743. if (fd->face->size->metrics.y_ppem != 0) {
  744. fd->scale = ((double)fd->size.x * fd->oversampling) / (double)fd->face->size->metrics.y_ppem;
  745. }
  746. }
  747. fd->ascent = (fd->face->size->metrics.ascender / 64.0) / fd->oversampling * fd->scale;
  748. fd->descent = (-fd->face->size->metrics.descender / 64.0) / fd->oversampling * fd->scale;
  749. fd->underline_position = (-FT_MulFix(fd->face->underline_position, fd->face->size->metrics.y_scale) / 64.0) / fd->oversampling * fd->scale;
  750. fd->underline_thickness = (FT_MulFix(fd->face->underline_thickness, fd->face->size->metrics.y_scale) / 64.0) / fd->oversampling * fd->scale;
  751. if (!p_font_data->face_init) {
  752. // When a font does not provide a `family_name`, FreeType tries to synthesize one based on other names.
  753. // FreeType automatically converts non-ASCII characters to "?" in the synthesized name.
  754. // To avoid that behavior, use the format-specific name directly if available.
  755. if (FT_IS_SFNT(fd->face)) {
  756. int name_count = FT_Get_Sfnt_Name_Count(fd->face);
  757. for (int i = 0; i < name_count; i++) {
  758. FT_SfntName sfnt_name;
  759. if (FT_Get_Sfnt_Name(fd->face, i, &sfnt_name) != 0) {
  760. continue;
  761. }
  762. if (sfnt_name.name_id != TT_NAME_ID_FONT_FAMILY && sfnt_name.name_id != TT_NAME_ID_TYPOGRAPHIC_FAMILY) {
  763. continue;
  764. }
  765. if (!p_font_data->font_name.is_empty() && sfnt_name.language_id != TT_MS_LANGID_ENGLISH_UNITED_STATES) {
  766. continue;
  767. }
  768. switch (sfnt_name.platform_id) {
  769. case TT_PLATFORM_APPLE_UNICODE: {
  770. p_font_data->font_name.parse_utf16((const char16_t *)sfnt_name.string, sfnt_name.string_len / 2, false);
  771. } break;
  772. case TT_PLATFORM_MICROSOFT: {
  773. if (sfnt_name.encoding_id == TT_MS_ID_UNICODE_CS || sfnt_name.encoding_id == TT_MS_ID_UCS_4) {
  774. p_font_data->font_name.parse_utf16((const char16_t *)sfnt_name.string, sfnt_name.string_len / 2, false);
  775. }
  776. } break;
  777. }
  778. }
  779. }
  780. if (p_font_data->font_name.is_empty() && fd->face->family_name != nullptr) {
  781. p_font_data->font_name = String::utf8((const char *)fd->face->family_name);
  782. }
  783. if (fd->face->style_name != nullptr) {
  784. p_font_data->style_name = String::utf8((const char *)fd->face->style_name);
  785. }
  786. p_font_data->weight = _font_get_weight_by_name(p_font_data->style_name.to_lower());
  787. p_font_data->stretch = _font_get_stretch_by_name(p_font_data->style_name.to_lower());
  788. p_font_data->style_flags = 0;
  789. if ((fd->face->style_flags & FT_STYLE_FLAG_BOLD) || p_font_data->weight >= 700) {
  790. p_font_data->style_flags.set_flag(FONT_BOLD);
  791. }
  792. if ((fd->face->style_flags & FT_STYLE_FLAG_ITALIC) || _is_ital_style(p_font_data->style_name.to_lower())) {
  793. p_font_data->style_flags.set_flag(FONT_ITALIC);
  794. }
  795. if (fd->face->face_flags & FT_FACE_FLAG_FIXED_WIDTH) {
  796. p_font_data->style_flags.set_flag(FONT_FIXED_WIDTH);
  797. }
  798. // Read OpenType variations.
  799. p_font_data->supported_varaitions.clear();
  800. if (fd->face->face_flags & FT_FACE_FLAG_MULTIPLE_MASTERS) {
  801. FT_MM_Var *amaster;
  802. FT_Get_MM_Var(fd->face, &amaster);
  803. for (FT_UInt i = 0; i < amaster->num_axis; i++) {
  804. 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);
  805. }
  806. FT_Done_MM_Var(ft_library, amaster);
  807. }
  808. p_font_data->face_init = true;
  809. }
  810. // Write variations.
  811. if (fd->face->face_flags & FT_FACE_FLAG_MULTIPLE_MASTERS) {
  812. FT_MM_Var *amaster;
  813. FT_Get_MM_Var(fd->face, &amaster);
  814. Vector<FT_Fixed> coords;
  815. coords.resize(amaster->num_axis);
  816. FT_Get_Var_Design_Coordinates(fd->face, coords.size(), coords.ptrw());
  817. for (FT_UInt i = 0; i < amaster->num_axis; i++) {
  818. // Reset to default.
  819. int32_t var_tag = amaster->axis[i].tag;
  820. double var_value = (double)amaster->axis[i].def / 65536.0;
  821. coords.write[i] = amaster->axis[i].def;
  822. if (p_font_data->variation_coordinates.has(var_tag)) {
  823. var_value = p_font_data->variation_coordinates[var_tag];
  824. coords.write[i] = CLAMP(var_value * 65536.0, amaster->axis[i].minimum, amaster->axis[i].maximum);
  825. }
  826. if (p_font_data->variation_coordinates.has(tag_to_name(var_tag))) {
  827. var_value = p_font_data->variation_coordinates[tag_to_name(var_tag)];
  828. coords.write[i] = CLAMP(var_value * 65536.0, amaster->axis[i].minimum, amaster->axis[i].maximum);
  829. }
  830. }
  831. FT_Set_Var_Design_Coordinates(fd->face, coords.size(), coords.ptrw());
  832. FT_Done_MM_Var(ft_library, amaster);
  833. }
  834. #else
  835. memdelete(fd);
  836. ERR_FAIL_V_MSG(false, "FreeType: Can't load dynamic font, engine is compiled without FreeType support!");
  837. #endif
  838. }
  839. p_font_data->cache[p_size] = fd;
  840. return true;
  841. }
  842. _FORCE_INLINE_ void TextServerFallback::_font_clear_cache(FontFallback *p_font_data) {
  843. MutexLock ftlock(ft_mutex);
  844. for (const KeyValue<Vector2i, FontForSizeFallback *> &E : p_font_data->cache) {
  845. memdelete(E.value);
  846. }
  847. p_font_data->cache.clear();
  848. p_font_data->face_init = false;
  849. p_font_data->supported_varaitions.clear();
  850. }
  851. RID TextServerFallback::_create_font() {
  852. _THREAD_SAFE_METHOD_
  853. FontFallback *fd = memnew(FontFallback);
  854. return font_owner.make_rid(fd);
  855. }
  856. RID TextServerFallback::_create_font_linked_variation(const RID &p_font_rid) {
  857. _THREAD_SAFE_METHOD_
  858. RID rid = p_font_rid;
  859. FontFallbackLinkedVariation *fdv = font_var_owner.get_or_null(rid);
  860. if (unlikely(fdv)) {
  861. rid = fdv->base_font;
  862. }
  863. ERR_FAIL_COND_V(!font_owner.owns(rid), RID());
  864. FontFallbackLinkedVariation *new_fdv = memnew(FontFallbackLinkedVariation);
  865. new_fdv->base_font = rid;
  866. return font_var_owner.make_rid(new_fdv);
  867. }
  868. void TextServerFallback::_font_set_data(const RID &p_font_rid, const PackedByteArray &p_data) {
  869. FontFallback *fd = _get_font_data(p_font_rid);
  870. ERR_FAIL_NULL(fd);
  871. MutexLock lock(fd->mutex);
  872. _font_clear_cache(fd);
  873. fd->data = p_data;
  874. fd->data_ptr = fd->data.ptr();
  875. fd->data_size = fd->data.size();
  876. }
  877. void TextServerFallback::_font_set_data_ptr(const RID &p_font_rid, const uint8_t *p_data_ptr, int64_t p_data_size) {
  878. FontFallback *fd = _get_font_data(p_font_rid);
  879. ERR_FAIL_NULL(fd);
  880. MutexLock lock(fd->mutex);
  881. _font_clear_cache(fd);
  882. fd->data.resize(0);
  883. fd->data_ptr = p_data_ptr;
  884. fd->data_size = p_data_size;
  885. }
  886. void TextServerFallback::_font_set_style(const RID &p_font_rid, BitField<FontStyle> p_style) {
  887. FontFallback *fd = _get_font_data(p_font_rid);
  888. ERR_FAIL_NULL(fd);
  889. MutexLock lock(fd->mutex);
  890. Vector2i size = _get_size(fd, 16);
  891. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  892. fd->style_flags = p_style;
  893. }
  894. void TextServerFallback::_font_set_face_index(const RID &p_font_rid, int64_t p_face_index) {
  895. ERR_FAIL_COND(p_face_index < 0);
  896. ERR_FAIL_COND(p_face_index >= 0x7FFF);
  897. FontFallback *fd = _get_font_data(p_font_rid);
  898. ERR_FAIL_NULL(fd);
  899. MutexLock lock(fd->mutex);
  900. if (fd->face_index != p_face_index) {
  901. fd->face_index = p_face_index;
  902. _font_clear_cache(fd);
  903. }
  904. }
  905. int64_t TextServerFallback::_font_get_face_index(const RID &p_font_rid) const {
  906. FontFallback *fd = _get_font_data(p_font_rid);
  907. ERR_FAIL_NULL_V(fd, 0);
  908. MutexLock lock(fd->mutex);
  909. return fd->face_index;
  910. }
  911. int64_t TextServerFallback::_font_get_face_count(const RID &p_font_rid) const {
  912. FontFallback *fd = _get_font_data(p_font_rid);
  913. ERR_FAIL_NULL_V(fd, 0);
  914. MutexLock lock(fd->mutex);
  915. int face_count = 0;
  916. if (fd->data_ptr && (fd->data_size > 0)) {
  917. // Init dynamic font.
  918. #ifdef MODULE_FREETYPE_ENABLED
  919. int error = 0;
  920. if (!ft_library) {
  921. error = FT_Init_FreeType(&ft_library);
  922. ERR_FAIL_COND_V_MSG(error != 0, false, "FreeType: Error initializing library: '" + String(FT_Error_String(error)) + "'.");
  923. #ifdef MODULE_SVG_ENABLED
  924. FT_Property_Set(ft_library, "ot-svg", "svg-hooks", get_tvg_svg_in_ot_hooks());
  925. #endif
  926. }
  927. FT_StreamRec stream;
  928. memset(&stream, 0, sizeof(FT_StreamRec));
  929. stream.base = (unsigned char *)fd->data_ptr;
  930. stream.size = fd->data_size;
  931. stream.pos = 0;
  932. FT_Open_Args fargs;
  933. memset(&fargs, 0, sizeof(FT_Open_Args));
  934. fargs.memory_base = (unsigned char *)fd->data_ptr;
  935. fargs.memory_size = fd->data_size;
  936. fargs.flags = FT_OPEN_MEMORY;
  937. fargs.stream = &stream;
  938. MutexLock ftlock(ft_mutex);
  939. FT_Face tmp_face = nullptr;
  940. error = FT_Open_Face(ft_library, &fargs, -1, &tmp_face);
  941. if (error == 0) {
  942. face_count = tmp_face->num_faces;
  943. FT_Done_Face(tmp_face);
  944. }
  945. #endif
  946. }
  947. return face_count;
  948. }
  949. BitField<TextServer::FontStyle> TextServerFallback::_font_get_style(const RID &p_font_rid) const {
  950. FontFallback *fd = _get_font_data(p_font_rid);
  951. ERR_FAIL_NULL_V(fd, 0);
  952. MutexLock lock(fd->mutex);
  953. Vector2i size = _get_size(fd, 16);
  954. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), 0);
  955. return fd->style_flags;
  956. }
  957. void TextServerFallback::_font_set_style_name(const RID &p_font_rid, const String &p_name) {
  958. FontFallback *fd = _get_font_data(p_font_rid);
  959. ERR_FAIL_NULL(fd);
  960. MutexLock lock(fd->mutex);
  961. Vector2i size = _get_size(fd, 16);
  962. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  963. fd->style_name = p_name;
  964. }
  965. String TextServerFallback::_font_get_style_name(const RID &p_font_rid) const {
  966. FontFallback *fd = _get_font_data(p_font_rid);
  967. ERR_FAIL_NULL_V(fd, String());
  968. MutexLock lock(fd->mutex);
  969. Vector2i size = _get_size(fd, 16);
  970. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), String());
  971. return fd->style_name;
  972. }
  973. void TextServerFallback::_font_set_weight(const RID &p_font_rid, int64_t p_weight) {
  974. FontFallback *fd = _get_font_data(p_font_rid);
  975. ERR_FAIL_NULL(fd);
  976. MutexLock lock(fd->mutex);
  977. Vector2i size = _get_size(fd, 16);
  978. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  979. fd->weight = CLAMP(p_weight, 100, 999);
  980. }
  981. int64_t TextServerFallback::_font_get_weight(const RID &p_font_rid) const {
  982. FontFallback *fd = _get_font_data(p_font_rid);
  983. ERR_FAIL_NULL_V(fd, 400);
  984. MutexLock lock(fd->mutex);
  985. Vector2i size = _get_size(fd, 16);
  986. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), 400);
  987. return fd->weight;
  988. }
  989. void TextServerFallback::_font_set_stretch(const RID &p_font_rid, int64_t p_stretch) {
  990. FontFallback *fd = _get_font_data(p_font_rid);
  991. ERR_FAIL_NULL(fd);
  992. MutexLock lock(fd->mutex);
  993. Vector2i size = _get_size(fd, 16);
  994. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  995. fd->stretch = CLAMP(p_stretch, 50, 200);
  996. }
  997. int64_t TextServerFallback::_font_get_stretch(const RID &p_font_rid) const {
  998. FontFallback *fd = _get_font_data(p_font_rid);
  999. ERR_FAIL_NULL_V(fd, 100);
  1000. MutexLock lock(fd->mutex);
  1001. Vector2i size = _get_size(fd, 16);
  1002. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), 100);
  1003. return fd->stretch;
  1004. }
  1005. void TextServerFallback::_font_set_name(const RID &p_font_rid, const String &p_name) {
  1006. FontFallback *fd = _get_font_data(p_font_rid);
  1007. ERR_FAIL_NULL(fd);
  1008. MutexLock lock(fd->mutex);
  1009. Vector2i size = _get_size(fd, 16);
  1010. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  1011. fd->font_name = p_name;
  1012. }
  1013. String TextServerFallback::_font_get_name(const RID &p_font_rid) const {
  1014. FontFallback *fd = _get_font_data(p_font_rid);
  1015. ERR_FAIL_NULL_V(fd, String());
  1016. MutexLock lock(fd->mutex);
  1017. Vector2i size = _get_size(fd, 16);
  1018. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), String());
  1019. return fd->font_name;
  1020. }
  1021. void TextServerFallback::_font_set_antialiasing(const RID &p_font_rid, TextServer::FontAntialiasing p_antialiasing) {
  1022. FontFallback *fd = _get_font_data(p_font_rid);
  1023. ERR_FAIL_NULL(fd);
  1024. MutexLock lock(fd->mutex);
  1025. if (fd->antialiasing != p_antialiasing) {
  1026. _font_clear_cache(fd);
  1027. fd->antialiasing = p_antialiasing;
  1028. }
  1029. }
  1030. TextServer::FontAntialiasing TextServerFallback::_font_get_antialiasing(const RID &p_font_rid) const {
  1031. FontFallback *fd = _get_font_data(p_font_rid);
  1032. ERR_FAIL_NULL_V(fd, TextServer::FONT_ANTIALIASING_NONE);
  1033. MutexLock lock(fd->mutex);
  1034. return fd->antialiasing;
  1035. }
  1036. void TextServerFallback::_font_set_disable_embedded_bitmaps(const RID &p_font_rid, bool p_disable_embedded_bitmaps) {
  1037. FontFallback *fd = _get_font_data(p_font_rid);
  1038. ERR_FAIL_NULL(fd);
  1039. MutexLock lock(fd->mutex);
  1040. if (fd->disable_embedded_bitmaps != p_disable_embedded_bitmaps) {
  1041. _font_clear_cache(fd);
  1042. fd->disable_embedded_bitmaps = p_disable_embedded_bitmaps;
  1043. }
  1044. }
  1045. bool TextServerFallback::_font_get_disable_embedded_bitmaps(const RID &p_font_rid) const {
  1046. FontFallback *fd = _get_font_data(p_font_rid);
  1047. ERR_FAIL_NULL_V(fd, false);
  1048. MutexLock lock(fd->mutex);
  1049. return fd->disable_embedded_bitmaps;
  1050. }
  1051. void TextServerFallback::_font_set_generate_mipmaps(const RID &p_font_rid, bool p_generate_mipmaps) {
  1052. FontFallback *fd = _get_font_data(p_font_rid);
  1053. ERR_FAIL_NULL(fd);
  1054. MutexLock lock(fd->mutex);
  1055. if (fd->mipmaps != p_generate_mipmaps) {
  1056. for (KeyValue<Vector2i, FontForSizeFallback *> &E : fd->cache) {
  1057. for (int i = 0; i < E.value->textures.size(); i++) {
  1058. E.value->textures.write[i].dirty = true;
  1059. E.value->textures.write[i].texture = Ref<ImageTexture>();
  1060. }
  1061. }
  1062. fd->mipmaps = p_generate_mipmaps;
  1063. }
  1064. }
  1065. bool TextServerFallback::_font_get_generate_mipmaps(const RID &p_font_rid) const {
  1066. FontFallback *fd = _get_font_data(p_font_rid);
  1067. ERR_FAIL_NULL_V(fd, false);
  1068. MutexLock lock(fd->mutex);
  1069. return fd->mipmaps;
  1070. }
  1071. void TextServerFallback::_font_set_multichannel_signed_distance_field(const RID &p_font_rid, bool p_msdf) {
  1072. FontFallback *fd = _get_font_data(p_font_rid);
  1073. ERR_FAIL_NULL(fd);
  1074. MutexLock lock(fd->mutex);
  1075. if (fd->msdf != p_msdf) {
  1076. _font_clear_cache(fd);
  1077. fd->msdf = p_msdf;
  1078. }
  1079. }
  1080. bool TextServerFallback::_font_is_multichannel_signed_distance_field(const RID &p_font_rid) const {
  1081. FontFallback *fd = _get_font_data(p_font_rid);
  1082. ERR_FAIL_NULL_V(fd, false);
  1083. MutexLock lock(fd->mutex);
  1084. return fd->msdf;
  1085. }
  1086. void TextServerFallback::_font_set_msdf_pixel_range(const RID &p_font_rid, int64_t p_msdf_pixel_range) {
  1087. FontFallback *fd = _get_font_data(p_font_rid);
  1088. ERR_FAIL_NULL(fd);
  1089. MutexLock lock(fd->mutex);
  1090. if (fd->msdf_range != p_msdf_pixel_range) {
  1091. _font_clear_cache(fd);
  1092. fd->msdf_range = p_msdf_pixel_range;
  1093. }
  1094. }
  1095. int64_t TextServerFallback::_font_get_msdf_pixel_range(const RID &p_font_rid) const {
  1096. FontFallback *fd = _get_font_data(p_font_rid);
  1097. ERR_FAIL_NULL_V(fd, false);
  1098. MutexLock lock(fd->mutex);
  1099. return fd->msdf_range;
  1100. }
  1101. void TextServerFallback::_font_set_msdf_size(const RID &p_font_rid, int64_t p_msdf_size) {
  1102. FontFallback *fd = _get_font_data(p_font_rid);
  1103. ERR_FAIL_NULL(fd);
  1104. MutexLock lock(fd->mutex);
  1105. if (fd->msdf_source_size != p_msdf_size) {
  1106. _font_clear_cache(fd);
  1107. fd->msdf_source_size = p_msdf_size;
  1108. }
  1109. }
  1110. int64_t TextServerFallback::_font_get_msdf_size(const RID &p_font_rid) const {
  1111. FontFallback *fd = _get_font_data(p_font_rid);
  1112. ERR_FAIL_NULL_V(fd, 0);
  1113. MutexLock lock(fd->mutex);
  1114. return fd->msdf_source_size;
  1115. }
  1116. void TextServerFallback::_font_set_fixed_size(const RID &p_font_rid, int64_t p_fixed_size) {
  1117. FontFallback *fd = _get_font_data(p_font_rid);
  1118. ERR_FAIL_NULL(fd);
  1119. MutexLock lock(fd->mutex);
  1120. fd->fixed_size = p_fixed_size;
  1121. }
  1122. int64_t TextServerFallback::_font_get_fixed_size(const RID &p_font_rid) const {
  1123. FontFallback *fd = _get_font_data(p_font_rid);
  1124. ERR_FAIL_NULL_V(fd, 0);
  1125. MutexLock lock(fd->mutex);
  1126. return fd->fixed_size;
  1127. }
  1128. void TextServerFallback::_font_set_fixed_size_scale_mode(const RID &p_font_rid, TextServer::FixedSizeScaleMode p_fixed_size_scale_mode) {
  1129. FontFallback *fd = _get_font_data(p_font_rid);
  1130. ERR_FAIL_NULL(fd);
  1131. MutexLock lock(fd->mutex);
  1132. fd->fixed_size_scale_mode = p_fixed_size_scale_mode;
  1133. }
  1134. TextServer::FixedSizeScaleMode TextServerFallback::_font_get_fixed_size_scale_mode(const RID &p_font_rid) const {
  1135. FontFallback *fd = _get_font_data(p_font_rid);
  1136. ERR_FAIL_NULL_V(fd, FIXED_SIZE_SCALE_DISABLE);
  1137. MutexLock lock(fd->mutex);
  1138. return fd->fixed_size_scale_mode;
  1139. }
  1140. void TextServerFallback::_font_set_allow_system_fallback(const RID &p_font_rid, bool p_allow_system_fallback) {
  1141. FontFallback *fd = _get_font_data(p_font_rid);
  1142. ERR_FAIL_NULL(fd);
  1143. MutexLock lock(fd->mutex);
  1144. fd->allow_system_fallback = p_allow_system_fallback;
  1145. }
  1146. bool TextServerFallback::_font_is_allow_system_fallback(const RID &p_font_rid) const {
  1147. FontFallback *fd = _get_font_data(p_font_rid);
  1148. ERR_FAIL_NULL_V(fd, false);
  1149. MutexLock lock(fd->mutex);
  1150. return fd->allow_system_fallback;
  1151. }
  1152. void TextServerFallback::_font_set_force_autohinter(const RID &p_font_rid, bool p_force_autohinter) {
  1153. FontFallback *fd = _get_font_data(p_font_rid);
  1154. ERR_FAIL_NULL(fd);
  1155. MutexLock lock(fd->mutex);
  1156. if (fd->force_autohinter != p_force_autohinter) {
  1157. _font_clear_cache(fd);
  1158. fd->force_autohinter = p_force_autohinter;
  1159. }
  1160. }
  1161. bool TextServerFallback::_font_is_force_autohinter(const RID &p_font_rid) const {
  1162. FontFallback *fd = _get_font_data(p_font_rid);
  1163. ERR_FAIL_NULL_V(fd, false);
  1164. MutexLock lock(fd->mutex);
  1165. return fd->force_autohinter;
  1166. }
  1167. void TextServerFallback::_font_set_hinting(const RID &p_font_rid, TextServer::Hinting p_hinting) {
  1168. FontFallback *fd = _get_font_data(p_font_rid);
  1169. ERR_FAIL_NULL(fd);
  1170. MutexLock lock(fd->mutex);
  1171. if (fd->hinting != p_hinting) {
  1172. _font_clear_cache(fd);
  1173. fd->hinting = p_hinting;
  1174. }
  1175. }
  1176. TextServer::Hinting TextServerFallback::_font_get_hinting(const RID &p_font_rid) const {
  1177. FontFallback *fd = _get_font_data(p_font_rid);
  1178. ERR_FAIL_NULL_V(fd, HINTING_NONE);
  1179. MutexLock lock(fd->mutex);
  1180. return fd->hinting;
  1181. }
  1182. void TextServerFallback::_font_set_subpixel_positioning(const RID &p_font_rid, TextServer::SubpixelPositioning p_subpixel) {
  1183. FontFallback *fd = _get_font_data(p_font_rid);
  1184. ERR_FAIL_NULL(fd);
  1185. MutexLock lock(fd->mutex);
  1186. fd->subpixel_positioning = p_subpixel;
  1187. }
  1188. TextServer::SubpixelPositioning TextServerFallback::_font_get_subpixel_positioning(const RID &p_font_rid) const {
  1189. FontFallback *fd = _get_font_data(p_font_rid);
  1190. ERR_FAIL_NULL_V(fd, SUBPIXEL_POSITIONING_DISABLED);
  1191. MutexLock lock(fd->mutex);
  1192. return fd->subpixel_positioning;
  1193. }
  1194. void TextServerFallback::_font_set_embolden(const RID &p_font_rid, double p_strength) {
  1195. FontFallback *fd = _get_font_data(p_font_rid);
  1196. ERR_FAIL_NULL(fd);
  1197. MutexLock lock(fd->mutex);
  1198. if (fd->embolden != p_strength) {
  1199. _font_clear_cache(fd);
  1200. fd->embolden = p_strength;
  1201. }
  1202. }
  1203. double TextServerFallback::_font_get_embolden(const RID &p_font_rid) const {
  1204. FontFallback *fd = _get_font_data(p_font_rid);
  1205. ERR_FAIL_NULL_V(fd, 0.0);
  1206. MutexLock lock(fd->mutex);
  1207. return fd->embolden;
  1208. }
  1209. void TextServerFallback::_font_set_spacing(const RID &p_font_rid, SpacingType p_spacing, int64_t p_value) {
  1210. ERR_FAIL_INDEX((int)p_spacing, 4);
  1211. FontFallbackLinkedVariation *fdv = font_var_owner.get_or_null(p_font_rid);
  1212. if (fdv) {
  1213. if (fdv->extra_spacing[p_spacing] != p_value) {
  1214. fdv->extra_spacing[p_spacing] = p_value;
  1215. }
  1216. } else {
  1217. FontFallback *fd = font_owner.get_or_null(p_font_rid);
  1218. ERR_FAIL_NULL(fd);
  1219. MutexLock lock(fd->mutex);
  1220. if (fd->extra_spacing[p_spacing] != p_value) {
  1221. _font_clear_cache(fd);
  1222. fd->extra_spacing[p_spacing] = p_value;
  1223. }
  1224. }
  1225. }
  1226. int64_t TextServerFallback::_font_get_spacing(const RID &p_font_rid, SpacingType p_spacing) const {
  1227. ERR_FAIL_INDEX_V((int)p_spacing, 4, 0);
  1228. FontFallbackLinkedVariation *fdv = font_var_owner.get_or_null(p_font_rid);
  1229. if (fdv) {
  1230. return fdv->extra_spacing[p_spacing];
  1231. } else {
  1232. FontFallback *fd = font_owner.get_or_null(p_font_rid);
  1233. ERR_FAIL_NULL_V(fd, 0);
  1234. MutexLock lock(fd->mutex);
  1235. return fd->extra_spacing[p_spacing];
  1236. }
  1237. }
  1238. void TextServerFallback::_font_set_baseline_offset(const RID &p_font_rid, double p_baseline_offset) {
  1239. FontFallbackLinkedVariation *fdv = font_var_owner.get_or_null(p_font_rid);
  1240. if (fdv) {
  1241. if (fdv->baseline_offset != p_baseline_offset) {
  1242. fdv->baseline_offset = p_baseline_offset;
  1243. }
  1244. } else {
  1245. FontFallback *fd = font_owner.get_or_null(p_font_rid);
  1246. ERR_FAIL_NULL(fd);
  1247. MutexLock lock(fd->mutex);
  1248. if (fd->baseline_offset != p_baseline_offset) {
  1249. _font_clear_cache(fd);
  1250. fd->baseline_offset = p_baseline_offset;
  1251. }
  1252. }
  1253. }
  1254. double TextServerFallback::_font_get_baseline_offset(const RID &p_font_rid) const {
  1255. FontFallbackLinkedVariation *fdv = font_var_owner.get_or_null(p_font_rid);
  1256. if (fdv) {
  1257. return fdv->baseline_offset;
  1258. } else {
  1259. FontFallback *fd = font_owner.get_or_null(p_font_rid);
  1260. ERR_FAIL_NULL_V(fd, 0.0);
  1261. MutexLock lock(fd->mutex);
  1262. return fd->baseline_offset;
  1263. }
  1264. }
  1265. void TextServerFallback::_font_set_transform(const RID &p_font_rid, const Transform2D &p_transform) {
  1266. FontFallback *fd = _get_font_data(p_font_rid);
  1267. ERR_FAIL_NULL(fd);
  1268. MutexLock lock(fd->mutex);
  1269. if (fd->transform != p_transform) {
  1270. _font_clear_cache(fd);
  1271. fd->transform = p_transform;
  1272. }
  1273. }
  1274. Transform2D TextServerFallback::_font_get_transform(const RID &p_font_rid) const {
  1275. FontFallback *fd = _get_font_data(p_font_rid);
  1276. ERR_FAIL_NULL_V(fd, Transform2D());
  1277. MutexLock lock(fd->mutex);
  1278. return fd->transform;
  1279. }
  1280. void TextServerFallback::_font_set_variation_coordinates(const RID &p_font_rid, const Dictionary &p_variation_coordinates) {
  1281. FontFallback *fd = _get_font_data(p_font_rid);
  1282. ERR_FAIL_NULL(fd);
  1283. MutexLock lock(fd->mutex);
  1284. if (!fd->variation_coordinates.recursive_equal(p_variation_coordinates, 1)) {
  1285. _font_clear_cache(fd);
  1286. fd->variation_coordinates = p_variation_coordinates.duplicate();
  1287. }
  1288. }
  1289. Dictionary TextServerFallback::_font_get_variation_coordinates(const RID &p_font_rid) const {
  1290. FontFallback *fd = _get_font_data(p_font_rid);
  1291. ERR_FAIL_NULL_V(fd, Dictionary());
  1292. MutexLock lock(fd->mutex);
  1293. return fd->variation_coordinates;
  1294. }
  1295. void TextServerFallback::_font_set_oversampling(const RID &p_font_rid, double p_oversampling) {
  1296. FontFallback *fd = _get_font_data(p_font_rid);
  1297. ERR_FAIL_NULL(fd);
  1298. MutexLock lock(fd->mutex);
  1299. if (fd->oversampling != p_oversampling) {
  1300. _font_clear_cache(fd);
  1301. fd->oversampling = p_oversampling;
  1302. }
  1303. }
  1304. double TextServerFallback::_font_get_oversampling(const RID &p_font_rid) const {
  1305. FontFallback *fd = _get_font_data(p_font_rid);
  1306. ERR_FAIL_NULL_V(fd, 0.0);
  1307. MutexLock lock(fd->mutex);
  1308. return fd->oversampling;
  1309. }
  1310. TypedArray<Vector2i> TextServerFallback::_font_get_size_cache_list(const RID &p_font_rid) const {
  1311. FontFallback *fd = _get_font_data(p_font_rid);
  1312. ERR_FAIL_NULL_V(fd, TypedArray<Vector2i>());
  1313. MutexLock lock(fd->mutex);
  1314. TypedArray<Vector2i> ret;
  1315. for (const KeyValue<Vector2i, FontForSizeFallback *> &E : fd->cache) {
  1316. ret.push_back(E.key);
  1317. }
  1318. return ret;
  1319. }
  1320. void TextServerFallback::_font_clear_size_cache(const RID &p_font_rid) {
  1321. FontFallback *fd = _get_font_data(p_font_rid);
  1322. ERR_FAIL_NULL(fd);
  1323. MutexLock lock(fd->mutex);
  1324. MutexLock ftlock(ft_mutex);
  1325. for (const KeyValue<Vector2i, FontForSizeFallback *> &E : fd->cache) {
  1326. memdelete(E.value);
  1327. }
  1328. fd->cache.clear();
  1329. }
  1330. void TextServerFallback::_font_remove_size_cache(const RID &p_font_rid, const Vector2i &p_size) {
  1331. FontFallback *fd = _get_font_data(p_font_rid);
  1332. ERR_FAIL_NULL(fd);
  1333. MutexLock lock(fd->mutex);
  1334. MutexLock ftlock(ft_mutex);
  1335. if (fd->cache.has(p_size)) {
  1336. memdelete(fd->cache[p_size]);
  1337. fd->cache.erase(p_size);
  1338. }
  1339. }
  1340. void TextServerFallback::_font_set_ascent(const RID &p_font_rid, int64_t p_size, double p_ascent) {
  1341. FontFallback *fd = _get_font_data(p_font_rid);
  1342. ERR_FAIL_NULL(fd);
  1343. MutexLock lock(fd->mutex);
  1344. Vector2i size = _get_size(fd, p_size);
  1345. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  1346. fd->cache[size]->ascent = p_ascent;
  1347. }
  1348. double TextServerFallback::_font_get_ascent(const RID &p_font_rid, int64_t p_size) const {
  1349. FontFallback *fd = _get_font_data(p_font_rid);
  1350. ERR_FAIL_NULL_V(fd, 0.0);
  1351. MutexLock lock(fd->mutex);
  1352. Vector2i size = _get_size(fd, p_size);
  1353. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), 0.0);
  1354. if (fd->msdf) {
  1355. return fd->cache[size]->ascent * (double)p_size / (double)fd->msdf_source_size;
  1356. } else if (fd->fixed_size > 0 && fd->fixed_size_scale_mode != FIXED_SIZE_SCALE_DISABLE && size.x != p_size) {
  1357. if (fd->fixed_size_scale_mode == FIXED_SIZE_SCALE_ENABLED) {
  1358. return fd->cache[size]->ascent * (double)p_size / (double)fd->fixed_size;
  1359. } else {
  1360. return fd->cache[size]->ascent * Math::round((double)p_size / (double)fd->fixed_size);
  1361. }
  1362. } else {
  1363. return fd->cache[size]->ascent;
  1364. }
  1365. }
  1366. void TextServerFallback::_font_set_descent(const RID &p_font_rid, int64_t p_size, double p_descent) {
  1367. FontFallback *fd = _get_font_data(p_font_rid);
  1368. ERR_FAIL_NULL(fd);
  1369. Vector2i size = _get_size(fd, p_size);
  1370. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  1371. fd->cache[size]->descent = p_descent;
  1372. }
  1373. double TextServerFallback::_font_get_descent(const RID &p_font_rid, int64_t p_size) const {
  1374. FontFallback *fd = _get_font_data(p_font_rid);
  1375. ERR_FAIL_NULL_V(fd, 0.0);
  1376. MutexLock lock(fd->mutex);
  1377. Vector2i size = _get_size(fd, p_size);
  1378. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), 0.0);
  1379. if (fd->msdf) {
  1380. return fd->cache[size]->descent * (double)p_size / (double)fd->msdf_source_size;
  1381. } else if (fd->fixed_size > 0 && fd->fixed_size_scale_mode != FIXED_SIZE_SCALE_DISABLE && size.x != p_size) {
  1382. if (fd->fixed_size_scale_mode == FIXED_SIZE_SCALE_ENABLED) {
  1383. return fd->cache[size]->descent * (double)p_size / (double)fd->fixed_size;
  1384. } else {
  1385. return fd->cache[size]->descent * Math::round((double)p_size / (double)fd->fixed_size);
  1386. }
  1387. } else {
  1388. return fd->cache[size]->descent;
  1389. }
  1390. }
  1391. void TextServerFallback::_font_set_underline_position(const RID &p_font_rid, int64_t p_size, double p_underline_position) {
  1392. FontFallback *fd = _get_font_data(p_font_rid);
  1393. ERR_FAIL_NULL(fd);
  1394. MutexLock lock(fd->mutex);
  1395. Vector2i size = _get_size(fd, p_size);
  1396. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  1397. fd->cache[size]->underline_position = p_underline_position;
  1398. }
  1399. double TextServerFallback::_font_get_underline_position(const RID &p_font_rid, int64_t p_size) const {
  1400. FontFallback *fd = _get_font_data(p_font_rid);
  1401. ERR_FAIL_NULL_V(fd, 0.0);
  1402. MutexLock lock(fd->mutex);
  1403. Vector2i size = _get_size(fd, p_size);
  1404. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), 0.0);
  1405. if (fd->msdf) {
  1406. return fd->cache[size]->underline_position * (double)p_size / (double)fd->msdf_source_size;
  1407. } else if (fd->fixed_size > 0 && fd->fixed_size_scale_mode != FIXED_SIZE_SCALE_DISABLE && size.x != p_size) {
  1408. if (fd->fixed_size_scale_mode == FIXED_SIZE_SCALE_ENABLED) {
  1409. return fd->cache[size]->underline_position * (double)p_size / (double)fd->fixed_size;
  1410. } else {
  1411. return fd->cache[size]->underline_position * Math::round((double)p_size / (double)fd->fixed_size);
  1412. }
  1413. } else {
  1414. return fd->cache[size]->underline_position;
  1415. }
  1416. }
  1417. void TextServerFallback::_font_set_underline_thickness(const RID &p_font_rid, int64_t p_size, double p_underline_thickness) {
  1418. FontFallback *fd = _get_font_data(p_font_rid);
  1419. ERR_FAIL_NULL(fd);
  1420. MutexLock lock(fd->mutex);
  1421. Vector2i size = _get_size(fd, p_size);
  1422. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  1423. fd->cache[size]->underline_thickness = p_underline_thickness;
  1424. }
  1425. double TextServerFallback::_font_get_underline_thickness(const RID &p_font_rid, int64_t p_size) const {
  1426. FontFallback *fd = _get_font_data(p_font_rid);
  1427. ERR_FAIL_NULL_V(fd, 0.0);
  1428. MutexLock lock(fd->mutex);
  1429. Vector2i size = _get_size(fd, p_size);
  1430. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), 0.0);
  1431. if (fd->msdf) {
  1432. return fd->cache[size]->underline_thickness * (double)p_size / (double)fd->msdf_source_size;
  1433. } else if (fd->fixed_size > 0 && fd->fixed_size_scale_mode != FIXED_SIZE_SCALE_DISABLE && size.x != p_size) {
  1434. if (fd->fixed_size_scale_mode == FIXED_SIZE_SCALE_ENABLED) {
  1435. return fd->cache[size]->underline_thickness * (double)p_size / (double)fd->fixed_size;
  1436. } else {
  1437. return fd->cache[size]->underline_thickness * Math::round((double)p_size / (double)fd->fixed_size);
  1438. }
  1439. } else {
  1440. return fd->cache[size]->underline_thickness;
  1441. }
  1442. }
  1443. void TextServerFallback::_font_set_scale(const RID &p_font_rid, int64_t p_size, double p_scale) {
  1444. FontFallback *fd = _get_font_data(p_font_rid);
  1445. ERR_FAIL_NULL(fd);
  1446. MutexLock lock(fd->mutex);
  1447. Vector2i size = _get_size(fd, p_size);
  1448. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  1449. #ifdef MODULE_FREETYPE_ENABLED
  1450. if (fd->cache[size]->face) {
  1451. return; // Do not override scale for dynamic fonts, it's calculated automatically.
  1452. }
  1453. #endif
  1454. fd->cache[size]->scale = p_scale;
  1455. }
  1456. double TextServerFallback::_font_get_scale(const RID &p_font_rid, int64_t p_size) const {
  1457. FontFallback *fd = _get_font_data(p_font_rid);
  1458. ERR_FAIL_NULL_V(fd, 0.0);
  1459. MutexLock lock(fd->mutex);
  1460. Vector2i size = _get_size(fd, p_size);
  1461. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), 0.0);
  1462. if (fd->msdf) {
  1463. return fd->cache[size]->scale * (double)p_size / (double)fd->msdf_source_size;
  1464. } else if (fd->fixed_size > 0 && fd->fixed_size_scale_mode != FIXED_SIZE_SCALE_DISABLE && size.x != p_size) {
  1465. if (fd->fixed_size_scale_mode == FIXED_SIZE_SCALE_ENABLED) {
  1466. return fd->cache[size]->scale * (double)p_size / (double)fd->fixed_size;
  1467. } else {
  1468. return fd->cache[size]->scale * Math::round((double)p_size / (double)fd->fixed_size);
  1469. }
  1470. } else {
  1471. return fd->cache[size]->scale / fd->cache[size]->oversampling;
  1472. }
  1473. }
  1474. int64_t TextServerFallback::_font_get_texture_count(const RID &p_font_rid, const Vector2i &p_size) const {
  1475. FontFallback *fd = _get_font_data(p_font_rid);
  1476. ERR_FAIL_NULL_V(fd, 0);
  1477. MutexLock lock(fd->mutex);
  1478. Vector2i size = _get_size_outline(fd, p_size);
  1479. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), 0);
  1480. return fd->cache[size]->textures.size();
  1481. }
  1482. void TextServerFallback::_font_clear_textures(const RID &p_font_rid, const Vector2i &p_size) {
  1483. FontFallback *fd = _get_font_data(p_font_rid);
  1484. ERR_FAIL_NULL(fd);
  1485. MutexLock lock(fd->mutex);
  1486. Vector2i size = _get_size_outline(fd, p_size);
  1487. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  1488. fd->cache[size]->textures.clear();
  1489. }
  1490. void TextServerFallback::_font_remove_texture(const RID &p_font_rid, const Vector2i &p_size, int64_t p_texture_index) {
  1491. FontFallback *fd = _get_font_data(p_font_rid);
  1492. ERR_FAIL_NULL(fd);
  1493. MutexLock lock(fd->mutex);
  1494. Vector2i size = _get_size_outline(fd, p_size);
  1495. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  1496. ERR_FAIL_INDEX(p_texture_index, fd->cache[size]->textures.size());
  1497. fd->cache[size]->textures.remove_at(p_texture_index);
  1498. }
  1499. void TextServerFallback::_font_set_texture_image(const RID &p_font_rid, const Vector2i &p_size, int64_t p_texture_index, const Ref<Image> &p_image) {
  1500. FontFallback *fd = _get_font_data(p_font_rid);
  1501. ERR_FAIL_NULL(fd);
  1502. ERR_FAIL_COND(p_image.is_null());
  1503. MutexLock lock(fd->mutex);
  1504. Vector2i size = _get_size_outline(fd, p_size);
  1505. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  1506. ERR_FAIL_COND(p_texture_index < 0);
  1507. if (p_texture_index >= fd->cache[size]->textures.size()) {
  1508. fd->cache[size]->textures.resize(p_texture_index + 1);
  1509. }
  1510. ShelfPackTexture &tex = fd->cache[size]->textures.write[p_texture_index];
  1511. tex.image = p_image;
  1512. tex.texture_w = p_image->get_width();
  1513. tex.texture_h = p_image->get_height();
  1514. Ref<Image> img = p_image;
  1515. if (fd->mipmaps && !img->has_mipmaps()) {
  1516. img = p_image->duplicate();
  1517. img->generate_mipmaps();
  1518. }
  1519. tex.texture = ImageTexture::create_from_image(img);
  1520. tex.dirty = false;
  1521. }
  1522. Ref<Image> TextServerFallback::_font_get_texture_image(const RID &p_font_rid, const Vector2i &p_size, int64_t p_texture_index) const {
  1523. FontFallback *fd = _get_font_data(p_font_rid);
  1524. ERR_FAIL_NULL_V(fd, Ref<Image>());
  1525. MutexLock lock(fd->mutex);
  1526. Vector2i size = _get_size_outline(fd, p_size);
  1527. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), Ref<Image>());
  1528. ERR_FAIL_INDEX_V(p_texture_index, fd->cache[size]->textures.size(), Ref<Image>());
  1529. const ShelfPackTexture &tex = fd->cache[size]->textures[p_texture_index];
  1530. return tex.image;
  1531. }
  1532. void TextServerFallback::_font_set_texture_offsets(const RID &p_font_rid, const Vector2i &p_size, int64_t p_texture_index, const PackedInt32Array &p_offsets) {
  1533. ERR_FAIL_COND(p_offsets.size() % 4 != 0);
  1534. FontFallback *fd = _get_font_data(p_font_rid);
  1535. ERR_FAIL_NULL(fd);
  1536. MutexLock lock(fd->mutex);
  1537. Vector2i size = _get_size_outline(fd, p_size);
  1538. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  1539. ERR_FAIL_COND(p_texture_index < 0);
  1540. if (p_texture_index >= fd->cache[size]->textures.size()) {
  1541. fd->cache[size]->textures.resize(p_texture_index + 1);
  1542. }
  1543. ShelfPackTexture &tex = fd->cache[size]->textures.write[p_texture_index];
  1544. tex.shelves.clear();
  1545. for (int32_t i = 0; i < p_offsets.size(); i += 4) {
  1546. tex.shelves.push_back(Shelf(p_offsets[i], p_offsets[i + 1], p_offsets[i + 2], p_offsets[i + 3]));
  1547. }
  1548. }
  1549. PackedInt32Array TextServerFallback::_font_get_texture_offsets(const RID &p_font_rid, const Vector2i &p_size, int64_t p_texture_index) const {
  1550. FontFallback *fd = _get_font_data(p_font_rid);
  1551. ERR_FAIL_NULL_V(fd, PackedInt32Array());
  1552. MutexLock lock(fd->mutex);
  1553. Vector2i size = _get_size_outline(fd, p_size);
  1554. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), PackedInt32Array());
  1555. ERR_FAIL_INDEX_V(p_texture_index, fd->cache[size]->textures.size(), PackedInt32Array());
  1556. const ShelfPackTexture &tex = fd->cache[size]->textures[p_texture_index];
  1557. PackedInt32Array ret;
  1558. ret.resize(tex.shelves.size() * 4);
  1559. int32_t *wr = ret.ptrw();
  1560. int32_t i = 0;
  1561. for (const Shelf &E : tex.shelves) {
  1562. wr[i * 4] = E.x;
  1563. wr[i * 4 + 1] = E.y;
  1564. wr[i * 4 + 2] = E.w;
  1565. wr[i * 4 + 3] = E.h;
  1566. i++;
  1567. }
  1568. return ret;
  1569. }
  1570. PackedInt32Array TextServerFallback::_font_get_glyph_list(const RID &p_font_rid, const Vector2i &p_size) const {
  1571. FontFallback *fd = _get_font_data(p_font_rid);
  1572. ERR_FAIL_NULL_V(fd, PackedInt32Array());
  1573. MutexLock lock(fd->mutex);
  1574. Vector2i size = _get_size_outline(fd, p_size);
  1575. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), PackedInt32Array());
  1576. PackedInt32Array ret;
  1577. const HashMap<int32_t, FontGlyph> &gl = fd->cache[size]->glyph_map;
  1578. for (const KeyValue<int32_t, FontGlyph> &E : gl) {
  1579. ret.push_back(E.key);
  1580. }
  1581. return ret;
  1582. }
  1583. void TextServerFallback::_font_clear_glyphs(const RID &p_font_rid, const Vector2i &p_size) {
  1584. FontFallback *fd = _get_font_data(p_font_rid);
  1585. ERR_FAIL_NULL(fd);
  1586. MutexLock lock(fd->mutex);
  1587. Vector2i size = _get_size_outline(fd, p_size);
  1588. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  1589. fd->cache[size]->glyph_map.clear();
  1590. }
  1591. void TextServerFallback::_font_remove_glyph(const RID &p_font_rid, const Vector2i &p_size, int64_t p_glyph) {
  1592. FontFallback *fd = _get_font_data(p_font_rid);
  1593. ERR_FAIL_NULL(fd);
  1594. MutexLock lock(fd->mutex);
  1595. Vector2i size = _get_size_outline(fd, p_size);
  1596. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  1597. fd->cache[size]->glyph_map.erase(p_glyph);
  1598. }
  1599. Vector2 TextServerFallback::_font_get_glyph_advance(const RID &p_font_rid, int64_t p_size, int64_t p_glyph) const {
  1600. FontFallback *fd = _get_font_data(p_font_rid);
  1601. ERR_FAIL_NULL_V(fd, Vector2());
  1602. MutexLock lock(fd->mutex);
  1603. Vector2i size = _get_size(fd, p_size);
  1604. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), Vector2());
  1605. int mod = 0;
  1606. if (fd->antialiasing == FONT_ANTIALIASING_LCD) {
  1607. TextServer::FontLCDSubpixelLayout layout = (TextServer::FontLCDSubpixelLayout)(int)GLOBAL_GET("gui/theme/lcd_subpixel_layout");
  1608. if (layout != FONT_LCD_SUBPIXEL_LAYOUT_NONE) {
  1609. mod = (layout << 24);
  1610. }
  1611. }
  1612. if (!_ensure_glyph(fd, size, p_glyph | mod)) {
  1613. return Vector2(); // Invalid or non graphicl glyph, do not display errors.
  1614. }
  1615. const HashMap<int32_t, FontGlyph> &gl = fd->cache[size]->glyph_map;
  1616. Vector2 ea;
  1617. if (fd->embolden != 0.0) {
  1618. ea.x = fd->embolden * double(size.x) / 64.0;
  1619. }
  1620. double scale = _font_get_scale(p_font_rid, p_size);
  1621. if (fd->msdf) {
  1622. return (gl[p_glyph | mod].advance + ea) * (double)p_size / (double)fd->msdf_source_size;
  1623. } else if (fd->fixed_size > 0 && fd->fixed_size_scale_mode != FIXED_SIZE_SCALE_DISABLE && size.x != p_size) {
  1624. if (fd->fixed_size_scale_mode == FIXED_SIZE_SCALE_ENABLED) {
  1625. return (gl[p_glyph | mod].advance + ea) * (double)p_size / (double)fd->fixed_size;
  1626. } else {
  1627. return (gl[p_glyph | mod].advance + ea) * Math::round((double)p_size / (double)fd->fixed_size);
  1628. }
  1629. } else if ((scale == 1.0) && ((fd->subpixel_positioning == SUBPIXEL_POSITIONING_DISABLED) || (fd->subpixel_positioning == SUBPIXEL_POSITIONING_AUTO && size.x > SUBPIXEL_POSITIONING_ONE_HALF_MAX_SIZE))) {
  1630. return (gl[p_glyph | mod].advance + ea).round();
  1631. } else {
  1632. return gl[p_glyph | mod].advance + ea;
  1633. }
  1634. }
  1635. void TextServerFallback::_font_set_glyph_advance(const RID &p_font_rid, int64_t p_size, int64_t p_glyph, const Vector2 &p_advance) {
  1636. FontFallback *fd = _get_font_data(p_font_rid);
  1637. ERR_FAIL_NULL(fd);
  1638. MutexLock lock(fd->mutex);
  1639. Vector2i size = _get_size(fd, p_size);
  1640. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  1641. HashMap<int32_t, FontGlyph> &gl = fd->cache[size]->glyph_map;
  1642. gl[p_glyph].advance = p_advance;
  1643. gl[p_glyph].found = true;
  1644. }
  1645. Vector2 TextServerFallback::_font_get_glyph_offset(const RID &p_font_rid, const Vector2i &p_size, int64_t p_glyph) const {
  1646. FontFallback *fd = _get_font_data(p_font_rid);
  1647. ERR_FAIL_NULL_V(fd, Vector2());
  1648. MutexLock lock(fd->mutex);
  1649. Vector2i size = _get_size_outline(fd, p_size);
  1650. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), Vector2());
  1651. int mod = 0;
  1652. if (fd->antialiasing == FONT_ANTIALIASING_LCD) {
  1653. TextServer::FontLCDSubpixelLayout layout = (TextServer::FontLCDSubpixelLayout)(int)GLOBAL_GET("gui/theme/lcd_subpixel_layout");
  1654. if (layout != FONT_LCD_SUBPIXEL_LAYOUT_NONE) {
  1655. mod = (layout << 24);
  1656. }
  1657. }
  1658. if (!_ensure_glyph(fd, size, p_glyph | mod)) {
  1659. return Vector2(); // Invalid or non graphicl glyph, do not display errors.
  1660. }
  1661. const HashMap<int32_t, FontGlyph> &gl = fd->cache[size]->glyph_map;
  1662. if (fd->msdf) {
  1663. return gl[p_glyph | mod].rect.position * (double)p_size.x / (double)fd->msdf_source_size;
  1664. } else if (fd->fixed_size > 0 && fd->fixed_size_scale_mode != FIXED_SIZE_SCALE_DISABLE && size.x != p_size.x) {
  1665. if (fd->fixed_size_scale_mode == FIXED_SIZE_SCALE_ENABLED) {
  1666. return gl[p_glyph | mod].rect.position * (double)p_size.x / (double)fd->fixed_size;
  1667. } else {
  1668. return gl[p_glyph | mod].rect.position * Math::round((double)p_size.x / (double)fd->fixed_size);
  1669. }
  1670. } else {
  1671. return gl[p_glyph | mod].rect.position;
  1672. }
  1673. }
  1674. void TextServerFallback::_font_set_glyph_offset(const RID &p_font_rid, const Vector2i &p_size, int64_t p_glyph, const Vector2 &p_offset) {
  1675. FontFallback *fd = _get_font_data(p_font_rid);
  1676. ERR_FAIL_NULL(fd);
  1677. MutexLock lock(fd->mutex);
  1678. Vector2i size = _get_size_outline(fd, p_size);
  1679. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  1680. HashMap<int32_t, FontGlyph> &gl = fd->cache[size]->glyph_map;
  1681. gl[p_glyph].rect.position = p_offset;
  1682. gl[p_glyph].found = true;
  1683. }
  1684. Vector2 TextServerFallback::_font_get_glyph_size(const RID &p_font_rid, const Vector2i &p_size, int64_t p_glyph) const {
  1685. FontFallback *fd = _get_font_data(p_font_rid);
  1686. ERR_FAIL_NULL_V(fd, Vector2());
  1687. MutexLock lock(fd->mutex);
  1688. Vector2i size = _get_size_outline(fd, p_size);
  1689. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), Vector2());
  1690. int mod = 0;
  1691. if (fd->antialiasing == FONT_ANTIALIASING_LCD) {
  1692. TextServer::FontLCDSubpixelLayout layout = (TextServer::FontLCDSubpixelLayout)(int)GLOBAL_GET("gui/theme/lcd_subpixel_layout");
  1693. if (layout != FONT_LCD_SUBPIXEL_LAYOUT_NONE) {
  1694. mod = (layout << 24);
  1695. }
  1696. }
  1697. if (!_ensure_glyph(fd, size, p_glyph | mod)) {
  1698. return Vector2(); // Invalid or non graphicl glyph, do not display errors.
  1699. }
  1700. const HashMap<int32_t, FontGlyph> &gl = fd->cache[size]->glyph_map;
  1701. if (fd->msdf) {
  1702. return gl[p_glyph | mod].rect.size * (double)p_size.x / (double)fd->msdf_source_size;
  1703. } else if (fd->fixed_size > 0 && fd->fixed_size_scale_mode != FIXED_SIZE_SCALE_DISABLE && size.x != p_size.x) {
  1704. if (fd->fixed_size_scale_mode == FIXED_SIZE_SCALE_ENABLED) {
  1705. return gl[p_glyph | mod].rect.size * (double)p_size.x / (double)fd->fixed_size;
  1706. } else {
  1707. return gl[p_glyph | mod].rect.size * Math::round((double)p_size.x / (double)fd->fixed_size);
  1708. }
  1709. } else {
  1710. return gl[p_glyph | mod].rect.size;
  1711. }
  1712. }
  1713. void TextServerFallback::_font_set_glyph_size(const RID &p_font_rid, const Vector2i &p_size, int64_t p_glyph, const Vector2 &p_gl_size) {
  1714. FontFallback *fd = _get_font_data(p_font_rid);
  1715. ERR_FAIL_NULL(fd);
  1716. MutexLock lock(fd->mutex);
  1717. Vector2i size = _get_size_outline(fd, p_size);
  1718. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  1719. HashMap<int32_t, FontGlyph> &gl = fd->cache[size]->glyph_map;
  1720. gl[p_glyph].rect.size = p_gl_size;
  1721. gl[p_glyph].found = true;
  1722. }
  1723. Rect2 TextServerFallback::_font_get_glyph_uv_rect(const RID &p_font_rid, const Vector2i &p_size, int64_t p_glyph) const {
  1724. FontFallback *fd = _get_font_data(p_font_rid);
  1725. ERR_FAIL_NULL_V(fd, Rect2());
  1726. MutexLock lock(fd->mutex);
  1727. Vector2i size = _get_size_outline(fd, p_size);
  1728. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), Rect2());
  1729. int mod = 0;
  1730. if (fd->antialiasing == FONT_ANTIALIASING_LCD) {
  1731. TextServer::FontLCDSubpixelLayout layout = (TextServer::FontLCDSubpixelLayout)(int)GLOBAL_GET("gui/theme/lcd_subpixel_layout");
  1732. if (layout != FONT_LCD_SUBPIXEL_LAYOUT_NONE) {
  1733. mod = (layout << 24);
  1734. }
  1735. }
  1736. if (!_ensure_glyph(fd, size, p_glyph | mod)) {
  1737. return Rect2(); // Invalid or non graphicl glyph, do not display errors.
  1738. }
  1739. const HashMap<int32_t, FontGlyph> &gl = fd->cache[size]->glyph_map;
  1740. return gl[p_glyph | mod].uv_rect;
  1741. }
  1742. void TextServerFallback::_font_set_glyph_uv_rect(const RID &p_font_rid, const Vector2i &p_size, int64_t p_glyph, const Rect2 &p_uv_rect) {
  1743. FontFallback *fd = _get_font_data(p_font_rid);
  1744. ERR_FAIL_NULL(fd);
  1745. MutexLock lock(fd->mutex);
  1746. Vector2i size = _get_size_outline(fd, p_size);
  1747. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  1748. HashMap<int32_t, FontGlyph> &gl = fd->cache[size]->glyph_map;
  1749. gl[p_glyph].uv_rect = p_uv_rect;
  1750. gl[p_glyph].found = true;
  1751. }
  1752. int64_t TextServerFallback::_font_get_glyph_texture_idx(const RID &p_font_rid, const Vector2i &p_size, int64_t p_glyph) const {
  1753. FontFallback *fd = _get_font_data(p_font_rid);
  1754. ERR_FAIL_NULL_V(fd, -1);
  1755. MutexLock lock(fd->mutex);
  1756. Vector2i size = _get_size_outline(fd, p_size);
  1757. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), -1);
  1758. int mod = 0;
  1759. if (fd->antialiasing == FONT_ANTIALIASING_LCD) {
  1760. TextServer::FontLCDSubpixelLayout layout = (TextServer::FontLCDSubpixelLayout)(int)GLOBAL_GET("gui/theme/lcd_subpixel_layout");
  1761. if (layout != FONT_LCD_SUBPIXEL_LAYOUT_NONE) {
  1762. mod = (layout << 24);
  1763. }
  1764. }
  1765. if (!_ensure_glyph(fd, size, p_glyph | mod)) {
  1766. return -1; // Invalid or non graphicl glyph, do not display errors.
  1767. }
  1768. const HashMap<int32_t, FontGlyph> &gl = fd->cache[size]->glyph_map;
  1769. return gl[p_glyph | mod].texture_idx;
  1770. }
  1771. void TextServerFallback::_font_set_glyph_texture_idx(const RID &p_font_rid, const Vector2i &p_size, int64_t p_glyph, int64_t p_texture_idx) {
  1772. FontFallback *fd = _get_font_data(p_font_rid);
  1773. ERR_FAIL_NULL(fd);
  1774. MutexLock lock(fd->mutex);
  1775. Vector2i size = _get_size_outline(fd, p_size);
  1776. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  1777. HashMap<int32_t, FontGlyph> &gl = fd->cache[size]->glyph_map;
  1778. gl[p_glyph].texture_idx = p_texture_idx;
  1779. gl[p_glyph].found = true;
  1780. }
  1781. RID TextServerFallback::_font_get_glyph_texture_rid(const RID &p_font_rid, const Vector2i &p_size, int64_t p_glyph) const {
  1782. FontFallback *fd = _get_font_data(p_font_rid);
  1783. ERR_FAIL_NULL_V(fd, RID());
  1784. MutexLock lock(fd->mutex);
  1785. Vector2i size = _get_size_outline(fd, p_size);
  1786. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), RID());
  1787. int mod = 0;
  1788. if (fd->antialiasing == FONT_ANTIALIASING_LCD) {
  1789. TextServer::FontLCDSubpixelLayout layout = (TextServer::FontLCDSubpixelLayout)(int)GLOBAL_GET("gui/theme/lcd_subpixel_layout");
  1790. if (layout != FONT_LCD_SUBPIXEL_LAYOUT_NONE) {
  1791. mod = (layout << 24);
  1792. }
  1793. }
  1794. if (!_ensure_glyph(fd, size, p_glyph | mod)) {
  1795. return RID(); // Invalid or non graphicl glyph, do not display errors.
  1796. }
  1797. const HashMap<int32_t, FontGlyph> &gl = fd->cache[size]->glyph_map;
  1798. ERR_FAIL_COND_V(gl[p_glyph | mod].texture_idx < -1 || gl[p_glyph | mod].texture_idx >= fd->cache[size]->textures.size(), RID());
  1799. if (RenderingServer::get_singleton() != nullptr) {
  1800. if (gl[p_glyph | mod].texture_idx != -1) {
  1801. if (fd->cache[size]->textures[gl[p_glyph | mod].texture_idx].dirty) {
  1802. ShelfPackTexture &tex = fd->cache[size]->textures.write[gl[p_glyph | mod].texture_idx];
  1803. Ref<Image> img = tex.image;
  1804. if (fd->mipmaps && !img->has_mipmaps()) {
  1805. img = tex.image->duplicate();
  1806. img->generate_mipmaps();
  1807. }
  1808. if (tex.texture.is_null()) {
  1809. tex.texture = ImageTexture::create_from_image(img);
  1810. } else {
  1811. tex.texture->update(img);
  1812. }
  1813. tex.dirty = false;
  1814. }
  1815. return fd->cache[size]->textures[gl[p_glyph | mod].texture_idx].texture->get_rid();
  1816. }
  1817. }
  1818. return RID();
  1819. }
  1820. Size2 TextServerFallback::_font_get_glyph_texture_size(const RID &p_font_rid, const Vector2i &p_size, int64_t p_glyph) const {
  1821. FontFallback *fd = _get_font_data(p_font_rid);
  1822. ERR_FAIL_NULL_V(fd, Size2());
  1823. MutexLock lock(fd->mutex);
  1824. Vector2i size = _get_size_outline(fd, p_size);
  1825. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), Size2());
  1826. int mod = 0;
  1827. if (fd->antialiasing == FONT_ANTIALIASING_LCD) {
  1828. TextServer::FontLCDSubpixelLayout layout = (TextServer::FontLCDSubpixelLayout)(int)GLOBAL_GET("gui/theme/lcd_subpixel_layout");
  1829. if (layout != FONT_LCD_SUBPIXEL_LAYOUT_NONE) {
  1830. mod = (layout << 24);
  1831. }
  1832. }
  1833. if (!_ensure_glyph(fd, size, p_glyph | mod)) {
  1834. return Size2(); // Invalid or non graphicl glyph, do not display errors.
  1835. }
  1836. const HashMap<int32_t, FontGlyph> &gl = fd->cache[size]->glyph_map;
  1837. ERR_FAIL_COND_V(gl[p_glyph | mod].texture_idx < -1 || gl[p_glyph | mod].texture_idx >= fd->cache[size]->textures.size(), Size2());
  1838. if (RenderingServer::get_singleton() != nullptr) {
  1839. if (gl[p_glyph | mod].texture_idx != -1) {
  1840. if (fd->cache[size]->textures[gl[p_glyph | mod].texture_idx].dirty) {
  1841. ShelfPackTexture &tex = fd->cache[size]->textures.write[gl[p_glyph | mod].texture_idx];
  1842. Ref<Image> img = tex.image;
  1843. if (fd->mipmaps && !img->has_mipmaps()) {
  1844. img = tex.image->duplicate();
  1845. img->generate_mipmaps();
  1846. }
  1847. if (tex.texture.is_null()) {
  1848. tex.texture = ImageTexture::create_from_image(img);
  1849. } else {
  1850. tex.texture->update(img);
  1851. }
  1852. tex.dirty = false;
  1853. }
  1854. return fd->cache[size]->textures[gl[p_glyph | mod].texture_idx].texture->get_size();
  1855. }
  1856. }
  1857. return Size2();
  1858. }
  1859. Dictionary TextServerFallback::_font_get_glyph_contours(const RID &p_font_rid, int64_t p_size, int64_t p_index) const {
  1860. FontFallback *fd = _get_font_data(p_font_rid);
  1861. ERR_FAIL_NULL_V(fd, Dictionary());
  1862. MutexLock lock(fd->mutex);
  1863. Vector2i size = _get_size(fd, p_size);
  1864. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), Dictionary());
  1865. #ifdef MODULE_FREETYPE_ENABLED
  1866. PackedVector3Array points;
  1867. PackedInt32Array contours;
  1868. int32_t index = p_index & 0xffffff; // Remove subpixel shifts.
  1869. int error = FT_Load_Glyph(fd->cache[size]->face, FT_Get_Char_Index(fd->cache[size]->face, index), FT_LOAD_NO_BITMAP | (fd->force_autohinter ? FT_LOAD_FORCE_AUTOHINT : 0));
  1870. ERR_FAIL_COND_V(error, Dictionary());
  1871. if (fd->embolden != 0.f) {
  1872. FT_Pos strength = fd->embolden * p_size * 4; // 26.6 fractional units (1 / 64).
  1873. FT_Outline_Embolden(&fd->cache[size]->face->glyph->outline, strength);
  1874. }
  1875. if (fd->transform != Transform2D()) {
  1876. 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).
  1877. FT_Outline_Transform(&fd->cache[size]->face->glyph->outline, &mat);
  1878. }
  1879. double scale = (1.0 / 64.0) / fd->cache[size]->oversampling * fd->cache[size]->scale;
  1880. if (fd->msdf) {
  1881. scale = scale * (double)p_size / (double)fd->msdf_source_size;
  1882. } else if (fd->fixed_size > 0 && fd->fixed_size_scale_mode != FIXED_SIZE_SCALE_DISABLE && size.x != p_size) {
  1883. if (fd->fixed_size_scale_mode == FIXED_SIZE_SCALE_ENABLED) {
  1884. scale = scale * (double)p_size / (double)fd->fixed_size;
  1885. } else {
  1886. scale = scale * Math::round((double)p_size / (double)fd->fixed_size);
  1887. }
  1888. }
  1889. for (short i = 0; i < fd->cache[size]->face->glyph->outline.n_points; i++) {
  1890. 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])));
  1891. }
  1892. for (short i = 0; i < fd->cache[size]->face->glyph->outline.n_contours; i++) {
  1893. contours.push_back(fd->cache[size]->face->glyph->outline.contours[i]);
  1894. }
  1895. bool orientation = (FT_Outline_Get_Orientation(&fd->cache[size]->face->glyph->outline) == FT_ORIENTATION_FILL_RIGHT);
  1896. Dictionary out;
  1897. out["points"] = points;
  1898. out["contours"] = contours;
  1899. out["orientation"] = orientation;
  1900. return out;
  1901. #else
  1902. return Dictionary();
  1903. #endif
  1904. }
  1905. TypedArray<Vector2i> TextServerFallback::_font_get_kerning_list(const RID &p_font_rid, int64_t p_size) const {
  1906. FontFallback *fd = _get_font_data(p_font_rid);
  1907. ERR_FAIL_NULL_V(fd, TypedArray<Vector2i>());
  1908. MutexLock lock(fd->mutex);
  1909. Vector2i size = _get_size(fd, p_size);
  1910. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), TypedArray<Vector2i>());
  1911. TypedArray<Vector2i> ret;
  1912. for (const KeyValue<Vector2i, Vector2> &E : fd->cache[size]->kerning_map) {
  1913. ret.push_back(E.key);
  1914. }
  1915. return ret;
  1916. }
  1917. void TextServerFallback::_font_clear_kerning_map(const RID &p_font_rid, int64_t p_size) {
  1918. FontFallback *fd = _get_font_data(p_font_rid);
  1919. ERR_FAIL_NULL(fd);
  1920. MutexLock lock(fd->mutex);
  1921. Vector2i size = _get_size(fd, p_size);
  1922. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  1923. fd->cache[size]->kerning_map.clear();
  1924. }
  1925. void TextServerFallback::_font_remove_kerning(const RID &p_font_rid, int64_t p_size, const Vector2i &p_glyph_pair) {
  1926. FontFallback *fd = _get_font_data(p_font_rid);
  1927. ERR_FAIL_NULL(fd);
  1928. MutexLock lock(fd->mutex);
  1929. Vector2i size = _get_size(fd, p_size);
  1930. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  1931. fd->cache[size]->kerning_map.erase(p_glyph_pair);
  1932. }
  1933. void TextServerFallback::_font_set_kerning(const RID &p_font_rid, int64_t p_size, const Vector2i &p_glyph_pair, const Vector2 &p_kerning) {
  1934. FontFallback *fd = _get_font_data(p_font_rid);
  1935. ERR_FAIL_NULL(fd);
  1936. MutexLock lock(fd->mutex);
  1937. Vector2i size = _get_size(fd, p_size);
  1938. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  1939. fd->cache[size]->kerning_map[p_glyph_pair] = p_kerning;
  1940. }
  1941. Vector2 TextServerFallback::_font_get_kerning(const RID &p_font_rid, int64_t p_size, const Vector2i &p_glyph_pair) const {
  1942. FontFallback *fd = _get_font_data(p_font_rid);
  1943. ERR_FAIL_NULL_V(fd, Vector2());
  1944. MutexLock lock(fd->mutex);
  1945. Vector2i size = _get_size(fd, p_size);
  1946. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), Vector2());
  1947. const HashMap<Vector2i, Vector2> &kern = fd->cache[size]->kerning_map;
  1948. if (kern.has(p_glyph_pair)) {
  1949. if (fd->msdf) {
  1950. return kern[p_glyph_pair] * (double)p_size / (double)fd->msdf_source_size;
  1951. } else if (fd->fixed_size > 0 && fd->fixed_size_scale_mode != FIXED_SIZE_SCALE_DISABLE && size.x != p_size) {
  1952. if (fd->fixed_size_scale_mode == FIXED_SIZE_SCALE_ENABLED) {
  1953. return kern[p_glyph_pair] * (double)p_size / (double)fd->fixed_size;
  1954. } else {
  1955. return kern[p_glyph_pair] * Math::round((double)p_size / (double)fd->fixed_size);
  1956. }
  1957. } else {
  1958. return kern[p_glyph_pair];
  1959. }
  1960. } else {
  1961. #ifdef MODULE_FREETYPE_ENABLED
  1962. if (fd->cache[size]->face) {
  1963. FT_Vector delta;
  1964. int32_t glyph_a = FT_Get_Char_Index(fd->cache[size]->face, p_glyph_pair.x);
  1965. int32_t glyph_b = FT_Get_Char_Index(fd->cache[size]->face, p_glyph_pair.y);
  1966. FT_Get_Kerning(fd->cache[size]->face, glyph_a, glyph_b, FT_KERNING_DEFAULT, &delta);
  1967. if (fd->msdf) {
  1968. return Vector2(delta.x, delta.y) * (double)p_size / (double)fd->msdf_source_size;
  1969. } else if (fd->fixed_size > 0 && fd->fixed_size_scale_mode != FIXED_SIZE_SCALE_DISABLE && size.x != p_size) {
  1970. if (fd->fixed_size_scale_mode == FIXED_SIZE_SCALE_ENABLED) {
  1971. return Vector2(delta.x, delta.y) * (double)p_size / (double)fd->fixed_size;
  1972. } else {
  1973. return Vector2(delta.x, delta.y) * Math::round((double)p_size / (double)fd->fixed_size);
  1974. }
  1975. } else {
  1976. return Vector2(delta.x, delta.y);
  1977. }
  1978. }
  1979. #endif
  1980. }
  1981. return Vector2();
  1982. }
  1983. int64_t TextServerFallback::_font_get_glyph_index(const RID &p_font_rid, int64_t p_size, int64_t p_char, int64_t p_variation_selector) const {
  1984. 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) + ".");
  1985. return (int64_t)p_char;
  1986. }
  1987. int64_t TextServerFallback::_font_get_char_from_glyph_index(const RID &p_font_rid, int64_t p_size, int64_t p_glyph_index) const {
  1988. return p_glyph_index;
  1989. }
  1990. bool TextServerFallback::_font_has_char(const RID &p_font_rid, int64_t p_char) const {
  1991. FontFallback *fd = _get_font_data(p_font_rid);
  1992. 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) + ".");
  1993. if (!fd) {
  1994. return false;
  1995. }
  1996. MutexLock lock(fd->mutex);
  1997. if (fd->cache.is_empty()) {
  1998. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, fd->msdf ? Vector2i(fd->msdf_source_size, 0) : Vector2i(16, 0)), false);
  1999. }
  2000. FontForSizeFallback *at_size = fd->cache.begin()->value;
  2001. #ifdef MODULE_FREETYPE_ENABLED
  2002. if (at_size && at_size->face) {
  2003. return FT_Get_Char_Index(at_size->face, p_char) != 0;
  2004. }
  2005. #endif
  2006. return (at_size) ? at_size->glyph_map.has((int32_t)p_char) : false;
  2007. }
  2008. String TextServerFallback::_font_get_supported_chars(const RID &p_font_rid) const {
  2009. FontFallback *fd = _get_font_data(p_font_rid);
  2010. ERR_FAIL_NULL_V(fd, String());
  2011. MutexLock lock(fd->mutex);
  2012. if (fd->cache.is_empty()) {
  2013. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, fd->msdf ? Vector2i(fd->msdf_source_size, 0) : Vector2i(16, 0)), String());
  2014. }
  2015. FontForSizeFallback *at_size = fd->cache.begin()->value;
  2016. String chars;
  2017. #ifdef MODULE_FREETYPE_ENABLED
  2018. if (at_size && at_size->face) {
  2019. FT_UInt gindex;
  2020. FT_ULong charcode = FT_Get_First_Char(at_size->face, &gindex);
  2021. while (gindex != 0) {
  2022. if (charcode != 0) {
  2023. chars = chars + String::chr(charcode);
  2024. }
  2025. charcode = FT_Get_Next_Char(at_size->face, charcode, &gindex);
  2026. }
  2027. return chars;
  2028. }
  2029. #endif
  2030. if (at_size) {
  2031. const HashMap<int32_t, FontGlyph> &gl = at_size->glyph_map;
  2032. for (const KeyValue<int32_t, FontGlyph> &E : gl) {
  2033. chars = chars + String::chr(E.key);
  2034. }
  2035. }
  2036. return chars;
  2037. }
  2038. void TextServerFallback::_font_render_range(const RID &p_font_rid, const Vector2i &p_size, int64_t p_start, int64_t p_end) {
  2039. FontFallback *fd = _get_font_data(p_font_rid);
  2040. ERR_FAIL_NULL(fd);
  2041. 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) + ".");
  2042. 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) + ".");
  2043. MutexLock lock(fd->mutex);
  2044. Vector2i size = _get_size_outline(fd, p_size);
  2045. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  2046. for (int64_t i = p_start; i <= p_end; i++) {
  2047. #ifdef MODULE_FREETYPE_ENABLED
  2048. int32_t idx = i;
  2049. if (fd->cache[size]->face) {
  2050. if (fd->msdf) {
  2051. _ensure_glyph(fd, size, (int32_t)idx);
  2052. } else {
  2053. for (int aa = 0; aa < ((fd->antialiasing == FONT_ANTIALIASING_LCD) ? FONT_LCD_SUBPIXEL_LAYOUT_MAX : 1); aa++) {
  2054. if ((fd->subpixel_positioning == SUBPIXEL_POSITIONING_ONE_QUARTER) || (fd->subpixel_positioning == SUBPIXEL_POSITIONING_AUTO && size.x <= SUBPIXEL_POSITIONING_ONE_QUARTER_MAX_SIZE)) {
  2055. _ensure_glyph(fd, size, (int32_t)idx | (0 << 27) | (aa << 24));
  2056. _ensure_glyph(fd, size, (int32_t)idx | (1 << 27) | (aa << 24));
  2057. _ensure_glyph(fd, size, (int32_t)idx | (2 << 27) | (aa << 24));
  2058. _ensure_glyph(fd, size, (int32_t)idx | (3 << 27) | (aa << 24));
  2059. } else if ((fd->subpixel_positioning == SUBPIXEL_POSITIONING_ONE_HALF) || (fd->subpixel_positioning == SUBPIXEL_POSITIONING_AUTO && size.x <= SUBPIXEL_POSITIONING_ONE_HALF_MAX_SIZE)) {
  2060. _ensure_glyph(fd, size, (int32_t)idx | (1 << 27) | (aa << 24));
  2061. _ensure_glyph(fd, size, (int32_t)idx | (0 << 27) | (aa << 24));
  2062. } else {
  2063. _ensure_glyph(fd, size, (int32_t)idx | (aa << 24));
  2064. }
  2065. }
  2066. }
  2067. }
  2068. #endif
  2069. }
  2070. }
  2071. void TextServerFallback::_font_render_glyph(const RID &p_font_rid, const Vector2i &p_size, int64_t p_index) {
  2072. FontFallback *fd = _get_font_data(p_font_rid);
  2073. ERR_FAIL_NULL(fd);
  2074. MutexLock lock(fd->mutex);
  2075. Vector2i size = _get_size_outline(fd, p_size);
  2076. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  2077. #ifdef MODULE_FREETYPE_ENABLED
  2078. int32_t idx = p_index & 0xffffff; // Remove subpixel shifts.
  2079. if (fd->cache[size]->face) {
  2080. if (fd->msdf) {
  2081. _ensure_glyph(fd, size, (int32_t)idx);
  2082. } else {
  2083. for (int aa = 0; aa < ((fd->antialiasing == FONT_ANTIALIASING_LCD) ? FONT_LCD_SUBPIXEL_LAYOUT_MAX : 1); aa++) {
  2084. if ((fd->subpixel_positioning == SUBPIXEL_POSITIONING_ONE_QUARTER) || (fd->subpixel_positioning == SUBPIXEL_POSITIONING_AUTO && size.x <= SUBPIXEL_POSITIONING_ONE_QUARTER_MAX_SIZE)) {
  2085. _ensure_glyph(fd, size, (int32_t)idx | (0 << 27) | (aa << 24));
  2086. _ensure_glyph(fd, size, (int32_t)idx | (1 << 27) | (aa << 24));
  2087. _ensure_glyph(fd, size, (int32_t)idx | (2 << 27) | (aa << 24));
  2088. _ensure_glyph(fd, size, (int32_t)idx | (3 << 27) | (aa << 24));
  2089. } else if ((fd->subpixel_positioning == SUBPIXEL_POSITIONING_ONE_HALF) || (fd->subpixel_positioning == SUBPIXEL_POSITIONING_AUTO && size.x <= SUBPIXEL_POSITIONING_ONE_HALF_MAX_SIZE)) {
  2090. _ensure_glyph(fd, size, (int32_t)idx | (1 << 27) | (aa << 24));
  2091. _ensure_glyph(fd, size, (int32_t)idx | (0 << 27) | (aa << 24));
  2092. } else {
  2093. _ensure_glyph(fd, size, (int32_t)idx | (aa << 24));
  2094. }
  2095. }
  2096. }
  2097. }
  2098. #endif
  2099. }
  2100. void TextServerFallback::_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 {
  2101. if (p_index == 0) {
  2102. return; // Non visual character, skip.
  2103. }
  2104. FontFallback *fd = _get_font_data(p_font_rid);
  2105. ERR_FAIL_NULL(fd);
  2106. MutexLock lock(fd->mutex);
  2107. Vector2i size = _get_size(fd, p_size);
  2108. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  2109. int32_t index = p_index & 0xffffff; // Remove subpixel shifts.
  2110. bool lcd_aa = false;
  2111. #ifdef MODULE_FREETYPE_ENABLED
  2112. if (!fd->msdf && fd->cache[size]->face) {
  2113. // LCD layout, bits 24, 25, 26
  2114. if (fd->antialiasing == FONT_ANTIALIASING_LCD) {
  2115. TextServer::FontLCDSubpixelLayout layout = (TextServer::FontLCDSubpixelLayout)(int)GLOBAL_GET("gui/theme/lcd_subpixel_layout");
  2116. if (layout != FONT_LCD_SUBPIXEL_LAYOUT_NONE) {
  2117. lcd_aa = true;
  2118. index = index | (layout << 24);
  2119. }
  2120. }
  2121. // Subpixel X-shift, bits 27, 28
  2122. if ((fd->subpixel_positioning == SUBPIXEL_POSITIONING_ONE_QUARTER) || (fd->subpixel_positioning == SUBPIXEL_POSITIONING_AUTO && size.x <= SUBPIXEL_POSITIONING_ONE_QUARTER_MAX_SIZE)) {
  2123. int xshift = (int)(Math::floor(4 * (p_pos.x + 0.125)) - 4 * Math::floor(p_pos.x + 0.125));
  2124. index = index | (xshift << 27);
  2125. } else if ((fd->subpixel_positioning == SUBPIXEL_POSITIONING_ONE_HALF) || (fd->subpixel_positioning == SUBPIXEL_POSITIONING_AUTO && size.x <= SUBPIXEL_POSITIONING_ONE_HALF_MAX_SIZE)) {
  2126. int xshift = (int)(Math::floor(2 * (p_pos.x + 0.25)) - 2 * Math::floor(p_pos.x + 0.25));
  2127. index = index | (xshift << 27);
  2128. }
  2129. }
  2130. #endif
  2131. if (!_ensure_glyph(fd, size, index)) {
  2132. return; // Invalid or non-graphical glyph, do not display errors, nothing to draw.
  2133. }
  2134. const FontGlyph &gl = fd->cache[size]->glyph_map[index];
  2135. if (gl.found) {
  2136. ERR_FAIL_COND(gl.texture_idx < -1 || gl.texture_idx >= fd->cache[size]->textures.size());
  2137. if (gl.texture_idx != -1) {
  2138. Color modulate = p_color;
  2139. #ifdef MODULE_FREETYPE_ENABLED
  2140. if (fd->cache[size]->face && fd->cache[size]->textures[gl.texture_idx].image.is_valid() && (fd->cache[size]->textures[gl.texture_idx].image->get_format() == Image::FORMAT_RGBA8) && !lcd_aa && !fd->msdf) {
  2141. modulate.r = modulate.g = modulate.b = 1.0;
  2142. }
  2143. #endif
  2144. if (RenderingServer::get_singleton() != nullptr) {
  2145. if (fd->cache[size]->textures[gl.texture_idx].dirty) {
  2146. ShelfPackTexture &tex = fd->cache[size]->textures.write[gl.texture_idx];
  2147. Ref<Image> img = tex.image;
  2148. if (fd->mipmaps && !img->has_mipmaps()) {
  2149. img = tex.image->duplicate();
  2150. img->generate_mipmaps();
  2151. }
  2152. if (tex.texture.is_null()) {
  2153. tex.texture = ImageTexture::create_from_image(img);
  2154. } else {
  2155. tex.texture->update(img);
  2156. }
  2157. tex.dirty = false;
  2158. }
  2159. RID texture = fd->cache[size]->textures[gl.texture_idx].texture->get_rid();
  2160. if (fd->msdf) {
  2161. Point2 cpos = p_pos;
  2162. cpos += gl.rect.position * (double)p_size / (double)fd->msdf_source_size;
  2163. Size2 csize = gl.rect.size * (double)p_size / (double)fd->msdf_source_size;
  2164. RenderingServer::get_singleton()->canvas_item_add_msdf_texture_rect_region(p_canvas, Rect2(cpos, csize), texture, gl.uv_rect, modulate, 0, fd->msdf_range, (double)p_size / (double)fd->msdf_source_size);
  2165. } else {
  2166. Point2 cpos = p_pos;
  2167. double scale = _font_get_scale(p_font_rid, p_size);
  2168. if ((fd->subpixel_positioning == SUBPIXEL_POSITIONING_ONE_QUARTER) || (fd->subpixel_positioning == SUBPIXEL_POSITIONING_AUTO && size.x <= SUBPIXEL_POSITIONING_ONE_QUARTER_MAX_SIZE)) {
  2169. cpos.x = cpos.x + 0.125;
  2170. } else if ((fd->subpixel_positioning == SUBPIXEL_POSITIONING_ONE_HALF) || (fd->subpixel_positioning == SUBPIXEL_POSITIONING_AUTO && size.x <= SUBPIXEL_POSITIONING_ONE_HALF_MAX_SIZE)) {
  2171. cpos.x = cpos.x + 0.25;
  2172. }
  2173. if (scale == 1.0) {
  2174. cpos.y = Math::floor(cpos.y);
  2175. cpos.x = Math::floor(cpos.x);
  2176. }
  2177. Vector2 gpos = gl.rect.position;
  2178. Size2 csize = gl.rect.size;
  2179. if (fd->fixed_size > 0 && fd->fixed_size_scale_mode != FIXED_SIZE_SCALE_DISABLE && size.x != p_size) {
  2180. if (fd->fixed_size_scale_mode == FIXED_SIZE_SCALE_ENABLED) {
  2181. double gl_scale = (double)p_size / (double)fd->fixed_size;
  2182. gpos *= gl_scale;
  2183. csize *= gl_scale;
  2184. } else {
  2185. double gl_scale = Math::round((double)p_size / (double)fd->fixed_size);
  2186. gpos *= gl_scale;
  2187. csize *= gl_scale;
  2188. }
  2189. }
  2190. cpos += gpos;
  2191. if (lcd_aa) {
  2192. RenderingServer::get_singleton()->canvas_item_add_lcd_texture_rect_region(p_canvas, Rect2(cpos, csize), texture, gl.uv_rect, modulate);
  2193. } else {
  2194. RenderingServer::get_singleton()->canvas_item_add_texture_rect_region(p_canvas, Rect2(cpos, csize), texture, gl.uv_rect, modulate, false, false);
  2195. }
  2196. }
  2197. }
  2198. }
  2199. }
  2200. }
  2201. void TextServerFallback::_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 {
  2202. if (p_index == 0) {
  2203. return; // Non visual character, skip.
  2204. }
  2205. FontFallback *fd = _get_font_data(p_font_rid);
  2206. ERR_FAIL_NULL(fd);
  2207. MutexLock lock(fd->mutex);
  2208. Vector2i size = _get_size_outline(fd, Vector2i(p_size, p_outline_size));
  2209. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  2210. int32_t index = p_index & 0xffffff; // Remove subpixel shifts.
  2211. bool lcd_aa = false;
  2212. #ifdef MODULE_FREETYPE_ENABLED
  2213. if (!fd->msdf && fd->cache[size]->face) {
  2214. // LCD layout, bits 24, 25, 26
  2215. if (fd->antialiasing == FONT_ANTIALIASING_LCD) {
  2216. TextServer::FontLCDSubpixelLayout layout = (TextServer::FontLCDSubpixelLayout)(int)GLOBAL_GET("gui/theme/lcd_subpixel_layout");
  2217. if (layout != FONT_LCD_SUBPIXEL_LAYOUT_NONE) {
  2218. lcd_aa = true;
  2219. index = index | (layout << 24);
  2220. }
  2221. }
  2222. // Subpixel X-shift, bits 27, 28
  2223. if ((fd->subpixel_positioning == SUBPIXEL_POSITIONING_ONE_QUARTER) || (fd->subpixel_positioning == SUBPIXEL_POSITIONING_AUTO && size.x <= SUBPIXEL_POSITIONING_ONE_QUARTER_MAX_SIZE)) {
  2224. int xshift = (int)(Math::floor(4 * (p_pos.x + 0.125)) - 4 * Math::floor(p_pos.x + 0.125));
  2225. index = index | (xshift << 27);
  2226. } else if ((fd->subpixel_positioning == SUBPIXEL_POSITIONING_ONE_HALF) || (fd->subpixel_positioning == SUBPIXEL_POSITIONING_AUTO && size.x <= SUBPIXEL_POSITIONING_ONE_HALF_MAX_SIZE)) {
  2227. int xshift = (int)(Math::floor(2 * (p_pos.x + 0.25)) - 2 * Math::floor(p_pos.x + 0.25));
  2228. index = index | (xshift << 27);
  2229. }
  2230. }
  2231. #endif
  2232. if (!_ensure_glyph(fd, size, index)) {
  2233. return; // Invalid or non-graphical glyph, do not display errors, nothing to draw.
  2234. }
  2235. const FontGlyph &gl = fd->cache[size]->glyph_map[index];
  2236. if (gl.found) {
  2237. ERR_FAIL_COND(gl.texture_idx < -1 || gl.texture_idx >= fd->cache[size]->textures.size());
  2238. if (gl.texture_idx != -1) {
  2239. Color modulate = p_color;
  2240. #ifdef MODULE_FREETYPE_ENABLED
  2241. if (fd->cache[size]->face && fd->cache[size]->textures[gl.texture_idx].image.is_valid() && (fd->cache[size]->textures[gl.texture_idx].image->get_format() == Image::FORMAT_RGBA8) && !lcd_aa && !fd->msdf) {
  2242. modulate.r = modulate.g = modulate.b = 1.0;
  2243. }
  2244. #endif
  2245. if (RenderingServer::get_singleton() != nullptr) {
  2246. if (fd->cache[size]->textures[gl.texture_idx].dirty) {
  2247. ShelfPackTexture &tex = fd->cache[size]->textures.write[gl.texture_idx];
  2248. Ref<Image> img = tex.image;
  2249. if (fd->mipmaps && !img->has_mipmaps()) {
  2250. img = tex.image->duplicate();
  2251. img->generate_mipmaps();
  2252. }
  2253. if (tex.texture.is_null()) {
  2254. tex.texture = ImageTexture::create_from_image(img);
  2255. } else {
  2256. tex.texture->update(img);
  2257. }
  2258. tex.dirty = false;
  2259. }
  2260. RID texture = fd->cache[size]->textures[gl.texture_idx].texture->get_rid();
  2261. if (fd->msdf) {
  2262. Point2 cpos = p_pos;
  2263. cpos += gl.rect.position * (double)p_size / (double)fd->msdf_source_size;
  2264. Size2 csize = gl.rect.size * (double)p_size / (double)fd->msdf_source_size;
  2265. RenderingServer::get_singleton()->canvas_item_add_msdf_texture_rect_region(p_canvas, Rect2(cpos, csize), texture, gl.uv_rect, modulate, p_outline_size, fd->msdf_range, (double)p_size / (double)fd->msdf_source_size);
  2266. } else {
  2267. Point2 cpos = p_pos;
  2268. double scale = _font_get_scale(p_font_rid, p_size);
  2269. if ((fd->subpixel_positioning == SUBPIXEL_POSITIONING_ONE_QUARTER) || (fd->subpixel_positioning == SUBPIXEL_POSITIONING_AUTO && size.x <= SUBPIXEL_POSITIONING_ONE_QUARTER_MAX_SIZE)) {
  2270. cpos.x = cpos.x + 0.125;
  2271. } else if ((fd->subpixel_positioning == SUBPIXEL_POSITIONING_ONE_HALF) || (fd->subpixel_positioning == SUBPIXEL_POSITIONING_AUTO && size.x <= SUBPIXEL_POSITIONING_ONE_HALF_MAX_SIZE)) {
  2272. cpos.x = cpos.x + 0.25;
  2273. }
  2274. if (scale == 1.0) {
  2275. cpos.y = Math::floor(cpos.y);
  2276. cpos.x = Math::floor(cpos.x);
  2277. }
  2278. Vector2 gpos = gl.rect.position;
  2279. Size2 csize = gl.rect.size;
  2280. if (fd->fixed_size > 0 && fd->fixed_size_scale_mode != FIXED_SIZE_SCALE_DISABLE && size.x != p_size) {
  2281. if (fd->fixed_size_scale_mode == FIXED_SIZE_SCALE_ENABLED) {
  2282. double gl_scale = (double)p_size / (double)fd->fixed_size;
  2283. gpos *= gl_scale;
  2284. csize *= gl_scale;
  2285. } else {
  2286. double gl_scale = Math::round((double)p_size / (double)fd->fixed_size);
  2287. gpos *= gl_scale;
  2288. csize *= gl_scale;
  2289. }
  2290. }
  2291. cpos += gpos;
  2292. if (lcd_aa) {
  2293. RenderingServer::get_singleton()->canvas_item_add_lcd_texture_rect_region(p_canvas, Rect2(cpos, csize), texture, gl.uv_rect, modulate);
  2294. } else {
  2295. RenderingServer::get_singleton()->canvas_item_add_texture_rect_region(p_canvas, Rect2(cpos, csize), texture, gl.uv_rect, modulate, false, false);
  2296. }
  2297. }
  2298. }
  2299. }
  2300. }
  2301. }
  2302. bool TextServerFallback::_font_is_language_supported(const RID &p_font_rid, const String &p_language) const {
  2303. FontFallback *fd = _get_font_data(p_font_rid);
  2304. ERR_FAIL_NULL_V(fd, false);
  2305. MutexLock lock(fd->mutex);
  2306. if (fd->language_support_overrides.has(p_language)) {
  2307. return fd->language_support_overrides[p_language];
  2308. } else {
  2309. return true;
  2310. }
  2311. }
  2312. void TextServerFallback::_font_set_language_support_override(const RID &p_font_rid, const String &p_language, bool p_supported) {
  2313. FontFallback *fd = _get_font_data(p_font_rid);
  2314. ERR_FAIL_NULL(fd);
  2315. MutexLock lock(fd->mutex);
  2316. fd->language_support_overrides[p_language] = p_supported;
  2317. }
  2318. bool TextServerFallback::_font_get_language_support_override(const RID &p_font_rid, const String &p_language) {
  2319. FontFallback *fd = _get_font_data(p_font_rid);
  2320. ERR_FAIL_NULL_V(fd, false);
  2321. MutexLock lock(fd->mutex);
  2322. return fd->language_support_overrides[p_language];
  2323. }
  2324. void TextServerFallback::_font_remove_language_support_override(const RID &p_font_rid, const String &p_language) {
  2325. FontFallback *fd = _get_font_data(p_font_rid);
  2326. ERR_FAIL_NULL(fd);
  2327. MutexLock lock(fd->mutex);
  2328. fd->language_support_overrides.erase(p_language);
  2329. }
  2330. PackedStringArray TextServerFallback::_font_get_language_support_overrides(const RID &p_font_rid) {
  2331. FontFallback *fd = _get_font_data(p_font_rid);
  2332. ERR_FAIL_NULL_V(fd, PackedStringArray());
  2333. MutexLock lock(fd->mutex);
  2334. PackedStringArray out;
  2335. for (const KeyValue<String, bool> &E : fd->language_support_overrides) {
  2336. out.push_back(E.key);
  2337. }
  2338. return out;
  2339. }
  2340. bool TextServerFallback::_font_is_script_supported(const RID &p_font_rid, const String &p_script) const {
  2341. FontFallback *fd = _get_font_data(p_font_rid);
  2342. ERR_FAIL_NULL_V(fd, false);
  2343. MutexLock lock(fd->mutex);
  2344. if (fd->script_support_overrides.has(p_script)) {
  2345. return fd->script_support_overrides[p_script];
  2346. } else {
  2347. return true;
  2348. }
  2349. }
  2350. void TextServerFallback::_font_set_script_support_override(const RID &p_font_rid, const String &p_script, bool p_supported) {
  2351. FontFallback *fd = _get_font_data(p_font_rid);
  2352. ERR_FAIL_NULL(fd);
  2353. MutexLock lock(fd->mutex);
  2354. fd->script_support_overrides[p_script] = p_supported;
  2355. }
  2356. bool TextServerFallback::_font_get_script_support_override(const RID &p_font_rid, const String &p_script) {
  2357. FontFallback *fd = _get_font_data(p_font_rid);
  2358. ERR_FAIL_NULL_V(fd, false);
  2359. MutexLock lock(fd->mutex);
  2360. return fd->script_support_overrides[p_script];
  2361. }
  2362. void TextServerFallback::_font_remove_script_support_override(const RID &p_font_rid, const String &p_script) {
  2363. FontFallback *fd = _get_font_data(p_font_rid);
  2364. ERR_FAIL_NULL(fd);
  2365. MutexLock lock(fd->mutex);
  2366. Vector2i size = _get_size(fd, 16);
  2367. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  2368. fd->script_support_overrides.erase(p_script);
  2369. }
  2370. PackedStringArray TextServerFallback::_font_get_script_support_overrides(const RID &p_font_rid) {
  2371. FontFallback *fd = _get_font_data(p_font_rid);
  2372. ERR_FAIL_NULL_V(fd, PackedStringArray());
  2373. MutexLock lock(fd->mutex);
  2374. PackedStringArray out;
  2375. for (const KeyValue<String, bool> &E : fd->script_support_overrides) {
  2376. out.push_back(E.key);
  2377. }
  2378. return out;
  2379. }
  2380. void TextServerFallback::_font_set_opentype_feature_overrides(const RID &p_font_rid, const Dictionary &p_overrides) {
  2381. FontFallback *fd = _get_font_data(p_font_rid);
  2382. ERR_FAIL_NULL(fd);
  2383. MutexLock lock(fd->mutex);
  2384. Vector2i size = _get_size(fd, 16);
  2385. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  2386. fd->feature_overrides = p_overrides;
  2387. }
  2388. Dictionary TextServerFallback::_font_get_opentype_feature_overrides(const RID &p_font_rid) const {
  2389. FontFallback *fd = _get_font_data(p_font_rid);
  2390. ERR_FAIL_NULL_V(fd, Dictionary());
  2391. MutexLock lock(fd->mutex);
  2392. return fd->feature_overrides;
  2393. }
  2394. Dictionary TextServerFallback::_font_supported_feature_list(const RID &p_font_rid) const {
  2395. return Dictionary();
  2396. }
  2397. Dictionary TextServerFallback::_font_supported_variation_list(const RID &p_font_rid) const {
  2398. FontFallback *fd = _get_font_data(p_font_rid);
  2399. ERR_FAIL_NULL_V(fd, Dictionary());
  2400. MutexLock lock(fd->mutex);
  2401. Vector2i size = _get_size(fd, 16);
  2402. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), Dictionary());
  2403. return fd->supported_varaitions;
  2404. }
  2405. double TextServerFallback::_font_get_global_oversampling() const {
  2406. return oversampling;
  2407. }
  2408. void TextServerFallback::_font_set_global_oversampling(double p_oversampling) {
  2409. _THREAD_SAFE_METHOD_
  2410. if (oversampling != p_oversampling) {
  2411. oversampling = p_oversampling;
  2412. List<RID> fonts;
  2413. font_owner.get_owned_list(&fonts);
  2414. bool font_cleared = false;
  2415. for (const RID &E : fonts) {
  2416. if (!_font_is_multichannel_signed_distance_field(E) && _font_get_oversampling(E) <= 0) {
  2417. _font_clear_size_cache(E);
  2418. font_cleared = true;
  2419. }
  2420. }
  2421. if (font_cleared) {
  2422. List<RID> text_bufs;
  2423. shaped_owner.get_owned_list(&text_bufs);
  2424. for (const RID &E : text_bufs) {
  2425. invalidate(shaped_owner.get_or_null(E));
  2426. }
  2427. }
  2428. }
  2429. }
  2430. /*************************************************************************/
  2431. /* Shaped text buffer interface */
  2432. /*************************************************************************/
  2433. void TextServerFallback::invalidate(ShapedTextDataFallback *p_shaped) {
  2434. p_shaped->valid = false;
  2435. p_shaped->sort_valid = false;
  2436. p_shaped->line_breaks_valid = false;
  2437. p_shaped->justification_ops_valid = false;
  2438. p_shaped->ascent = 0.0;
  2439. p_shaped->descent = 0.0;
  2440. p_shaped->width = 0.0;
  2441. p_shaped->upos = 0.0;
  2442. p_shaped->uthk = 0.0;
  2443. p_shaped->glyphs.clear();
  2444. p_shaped->glyphs_logical.clear();
  2445. }
  2446. void TextServerFallback::full_copy(ShapedTextDataFallback *p_shaped) {
  2447. ShapedTextDataFallback *parent = shaped_owner.get_or_null(p_shaped->parent);
  2448. for (const KeyValue<Variant, ShapedTextDataFallback::EmbeddedObject> &E : parent->objects) {
  2449. if (E.value.start >= p_shaped->start && E.value.start < p_shaped->end) {
  2450. p_shaped->objects[E.key] = E.value;
  2451. }
  2452. }
  2453. for (int k = 0; k < parent->spans.size(); k++) {
  2454. ShapedTextDataFallback::Span span = parent->spans[k];
  2455. if (span.start >= p_shaped->end || span.end <= p_shaped->start) {
  2456. continue;
  2457. }
  2458. span.start = MAX(p_shaped->start, span.start);
  2459. span.end = MIN(p_shaped->end, span.end);
  2460. p_shaped->spans.push_back(span);
  2461. }
  2462. p_shaped->parent = RID();
  2463. }
  2464. RID TextServerFallback::_create_shaped_text(TextServer::Direction p_direction, TextServer::Orientation p_orientation) {
  2465. _THREAD_SAFE_METHOD_
  2466. ERR_FAIL_COND_V_MSG(p_direction == DIRECTION_INHERITED, RID(), "Invalid text direction.");
  2467. ShapedTextDataFallback *sd = memnew(ShapedTextDataFallback);
  2468. sd->direction = p_direction;
  2469. sd->orientation = p_orientation;
  2470. return shaped_owner.make_rid(sd);
  2471. }
  2472. void TextServerFallback::_shaped_text_clear(const RID &p_shaped) {
  2473. ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  2474. ERR_FAIL_NULL(sd);
  2475. MutexLock lock(sd->mutex);
  2476. sd->parent = RID();
  2477. sd->start = 0;
  2478. sd->end = 0;
  2479. sd->text = String();
  2480. sd->spans.clear();
  2481. sd->objects.clear();
  2482. invalidate(sd);
  2483. }
  2484. void TextServerFallback::_shaped_text_set_direction(const RID &p_shaped, TextServer::Direction p_direction) {
  2485. ERR_FAIL_COND_MSG(p_direction == DIRECTION_INHERITED, "Invalid text direction.");
  2486. if (p_direction == DIRECTION_RTL) {
  2487. ERR_PRINT_ONCE("Right-to-left layout is not supported by this text server.");
  2488. }
  2489. }
  2490. TextServer::Direction TextServerFallback::_shaped_text_get_direction(const RID &p_shaped) const {
  2491. return TextServer::DIRECTION_LTR;
  2492. }
  2493. TextServer::Direction TextServerFallback::_shaped_text_get_inferred_direction(const RID &p_shaped) const {
  2494. return TextServer::DIRECTION_LTR;
  2495. }
  2496. void TextServerFallback::_shaped_text_set_custom_punctuation(const RID &p_shaped, const String &p_punct) {
  2497. _THREAD_SAFE_METHOD_
  2498. ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  2499. ERR_FAIL_NULL(sd);
  2500. if (sd->custom_punct != p_punct) {
  2501. if (sd->parent != RID()) {
  2502. full_copy(sd);
  2503. }
  2504. sd->custom_punct = p_punct;
  2505. invalidate(sd);
  2506. }
  2507. }
  2508. String TextServerFallback::_shaped_text_get_custom_punctuation(const RID &p_shaped) const {
  2509. _THREAD_SAFE_METHOD_
  2510. const ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  2511. ERR_FAIL_NULL_V(sd, String());
  2512. return sd->custom_punct;
  2513. }
  2514. void TextServerFallback::_shaped_text_set_custom_ellipsis(const RID &p_shaped, int64_t p_char) {
  2515. _THREAD_SAFE_METHOD_
  2516. ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  2517. ERR_FAIL_NULL(sd);
  2518. sd->el_char = p_char;
  2519. }
  2520. int64_t TextServerFallback::_shaped_text_get_custom_ellipsis(const RID &p_shaped) const {
  2521. _THREAD_SAFE_METHOD_
  2522. const ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  2523. ERR_FAIL_NULL_V(sd, 0);
  2524. return sd->el_char;
  2525. }
  2526. void TextServerFallback::_shaped_text_set_orientation(const RID &p_shaped, TextServer::Orientation p_orientation) {
  2527. ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  2528. ERR_FAIL_NULL(sd);
  2529. MutexLock lock(sd->mutex);
  2530. if (sd->orientation != p_orientation) {
  2531. if (sd->parent != RID()) {
  2532. full_copy(sd);
  2533. }
  2534. sd->orientation = p_orientation;
  2535. invalidate(sd);
  2536. }
  2537. }
  2538. void TextServerFallback::_shaped_text_set_bidi_override(const RID &p_shaped, const Array &p_override) {
  2539. // No BiDi support, ignore.
  2540. }
  2541. TextServer::Orientation TextServerFallback::_shaped_text_get_orientation(const RID &p_shaped) const {
  2542. const ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  2543. ERR_FAIL_NULL_V(sd, TextServer::ORIENTATION_HORIZONTAL);
  2544. MutexLock lock(sd->mutex);
  2545. return sd->orientation;
  2546. }
  2547. void TextServerFallback::_shaped_text_set_preserve_invalid(const RID &p_shaped, bool p_enabled) {
  2548. ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  2549. MutexLock lock(sd->mutex);
  2550. ERR_FAIL_NULL(sd);
  2551. if (sd->preserve_invalid != p_enabled) {
  2552. if (sd->parent != RID()) {
  2553. full_copy(sd);
  2554. }
  2555. sd->preserve_invalid = p_enabled;
  2556. invalidate(sd);
  2557. }
  2558. }
  2559. bool TextServerFallback::_shaped_text_get_preserve_invalid(const RID &p_shaped) const {
  2560. const ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  2561. ERR_FAIL_NULL_V(sd, false);
  2562. MutexLock lock(sd->mutex);
  2563. return sd->preserve_invalid;
  2564. }
  2565. void TextServerFallback::_shaped_text_set_preserve_control(const RID &p_shaped, bool p_enabled) {
  2566. ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  2567. ERR_FAIL_NULL(sd);
  2568. MutexLock lock(sd->mutex);
  2569. if (sd->preserve_control != p_enabled) {
  2570. if (sd->parent != RID()) {
  2571. full_copy(sd);
  2572. }
  2573. sd->preserve_control = p_enabled;
  2574. invalidate(sd);
  2575. }
  2576. }
  2577. bool TextServerFallback::_shaped_text_get_preserve_control(const RID &p_shaped) const {
  2578. const ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  2579. ERR_FAIL_NULL_V(sd, false);
  2580. MutexLock lock(sd->mutex);
  2581. return sd->preserve_control;
  2582. }
  2583. void TextServerFallback::_shaped_text_set_spacing(const RID &p_shaped, SpacingType p_spacing, int64_t p_value) {
  2584. ERR_FAIL_INDEX((int)p_spacing, 4);
  2585. ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  2586. ERR_FAIL_NULL(sd);
  2587. MutexLock lock(sd->mutex);
  2588. if (sd->extra_spacing[p_spacing] != p_value) {
  2589. if (sd->parent != RID()) {
  2590. full_copy(sd);
  2591. }
  2592. sd->extra_spacing[p_spacing] = p_value;
  2593. invalidate(sd);
  2594. }
  2595. }
  2596. int64_t TextServerFallback::_shaped_text_get_spacing(const RID &p_shaped, SpacingType p_spacing) const {
  2597. ERR_FAIL_INDEX_V((int)p_spacing, 4, 0);
  2598. const ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  2599. ERR_FAIL_NULL_V(sd, 0);
  2600. MutexLock lock(sd->mutex);
  2601. return sd->extra_spacing[p_spacing];
  2602. }
  2603. int64_t TextServerFallback::_shaped_get_span_count(const RID &p_shaped) const {
  2604. ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  2605. ERR_FAIL_NULL_V(sd, 0);
  2606. return sd->spans.size();
  2607. }
  2608. Variant TextServerFallback::_shaped_get_span_meta(const RID &p_shaped, int64_t p_index) const {
  2609. ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  2610. ERR_FAIL_NULL_V(sd, Variant());
  2611. ERR_FAIL_INDEX_V(p_index, sd->spans.size(), Variant());
  2612. return sd->spans[p_index].meta;
  2613. }
  2614. void TextServerFallback::_shaped_set_span_update_font(const RID &p_shaped, int64_t p_index, const TypedArray<RID> &p_fonts, int64_t p_size, const Dictionary &p_opentype_features) {
  2615. ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  2616. ERR_FAIL_NULL(sd);
  2617. ERR_FAIL_INDEX(p_index, sd->spans.size());
  2618. ShapedTextDataFallback::Span &span = sd->spans.ptrw()[p_index];
  2619. span.fonts.clear();
  2620. // Pre-sort fonts, push fonts with the language support first.
  2621. Array fonts_no_match;
  2622. int font_count = p_fonts.size();
  2623. for (int i = 0; i < font_count; i++) {
  2624. if (_font_is_language_supported(p_fonts[i], span.language)) {
  2625. span.fonts.push_back(p_fonts[i]);
  2626. } else {
  2627. fonts_no_match.push_back(p_fonts[i]);
  2628. }
  2629. }
  2630. span.fonts.append_array(fonts_no_match);
  2631. span.font_size = p_size;
  2632. span.features = p_opentype_features;
  2633. sd->valid = false;
  2634. }
  2635. bool TextServerFallback::_shaped_text_add_string(const RID &p_shaped, const String &p_text, const TypedArray<RID> &p_fonts, int64_t p_size, const Dictionary &p_opentype_features, const String &p_language, const Variant &p_meta) {
  2636. ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  2637. ERR_FAIL_NULL_V(sd, false);
  2638. MutexLock lock(sd->mutex);
  2639. ERR_FAIL_COND_V(p_size <= 0, false);
  2640. for (int i = 0; i < p_fonts.size(); i++) {
  2641. ERR_FAIL_NULL_V(_get_font_data(p_fonts[i]), false);
  2642. }
  2643. if (p_text.is_empty()) {
  2644. return true;
  2645. }
  2646. if (sd->parent != RID()) {
  2647. full_copy(sd);
  2648. }
  2649. ShapedTextDataFallback::Span span;
  2650. span.start = sd->text.length();
  2651. span.end = span.start + p_text.length();
  2652. // Pre-sort fonts, push fonts with the language support first.
  2653. Array fonts_no_match;
  2654. int font_count = p_fonts.size();
  2655. if (font_count > 0) {
  2656. span.fonts.push_back(p_fonts[0]);
  2657. }
  2658. for (int i = 1; i < font_count; i++) {
  2659. if (_font_is_language_supported(p_fonts[i], p_language)) {
  2660. span.fonts.push_back(p_fonts[i]);
  2661. } else {
  2662. fonts_no_match.push_back(p_fonts[i]);
  2663. }
  2664. }
  2665. span.fonts.append_array(fonts_no_match);
  2666. ERR_FAIL_COND_V(span.fonts.is_empty(), false);
  2667. span.font_size = p_size;
  2668. span.language = p_language;
  2669. span.meta = p_meta;
  2670. sd->spans.push_back(span);
  2671. sd->text = sd->text + p_text;
  2672. sd->end += p_text.length();
  2673. invalidate(sd);
  2674. return true;
  2675. }
  2676. bool TextServerFallback::_shaped_text_add_object(const RID &p_shaped, const Variant &p_key, const Size2 &p_size, InlineAlignment p_inline_align, int64_t p_length, double p_baseline) {
  2677. ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  2678. ERR_FAIL_NULL_V(sd, false);
  2679. MutexLock lock(sd->mutex);
  2680. ERR_FAIL_COND_V(p_key == Variant(), false);
  2681. ERR_FAIL_COND_V(sd->objects.has(p_key), false);
  2682. if (sd->parent != RID()) {
  2683. full_copy(sd);
  2684. }
  2685. ShapedTextDataFallback::Span span;
  2686. span.start = sd->start + sd->text.length();
  2687. span.end = span.start + p_length;
  2688. span.embedded_key = p_key;
  2689. ShapedTextDataFallback::EmbeddedObject obj;
  2690. obj.inline_align = p_inline_align;
  2691. obj.rect.size = p_size;
  2692. obj.start = span.start;
  2693. obj.end = span.end;
  2694. obj.baseline = p_baseline;
  2695. sd->spans.push_back(span);
  2696. sd->text = sd->text + String::chr(0xfffc).repeat(p_length);
  2697. sd->end += p_length;
  2698. sd->objects[p_key] = obj;
  2699. invalidate(sd);
  2700. return true;
  2701. }
  2702. bool TextServerFallback::_shaped_text_resize_object(const RID &p_shaped, const Variant &p_key, const Size2 &p_size, InlineAlignment p_inline_align, double p_baseline) {
  2703. ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  2704. ERR_FAIL_NULL_V(sd, false);
  2705. MutexLock lock(sd->mutex);
  2706. ERR_FAIL_COND_V(!sd->objects.has(p_key), false);
  2707. sd->objects[p_key].rect.size = p_size;
  2708. sd->objects[p_key].inline_align = p_inline_align;
  2709. sd->objects[p_key].baseline = p_baseline;
  2710. if (sd->valid) {
  2711. // Recalc string metrics.
  2712. sd->ascent = 0;
  2713. sd->descent = 0;
  2714. sd->width = 0;
  2715. sd->upos = 0;
  2716. sd->uthk = 0;
  2717. int sd_size = sd->glyphs.size();
  2718. for (int i = 0; i < sd_size; i++) {
  2719. Glyph gl = sd->glyphs[i];
  2720. Variant key;
  2721. if (gl.count == 1) {
  2722. for (const KeyValue<Variant, ShapedTextDataFallback::EmbeddedObject> &E : sd->objects) {
  2723. if (E.value.start == gl.start) {
  2724. key = E.key;
  2725. break;
  2726. }
  2727. }
  2728. }
  2729. if (key != Variant()) {
  2730. if (sd->orientation == ORIENTATION_HORIZONTAL) {
  2731. sd->objects[key].rect.position.x = sd->width;
  2732. sd->width += sd->objects[key].rect.size.x;
  2733. sd->glyphs.write[i].advance = sd->objects[key].rect.size.x;
  2734. } else {
  2735. sd->objects[key].rect.position.y = sd->width;
  2736. sd->width += sd->objects[key].rect.size.y;
  2737. sd->glyphs.write[i].advance = sd->objects[key].rect.size.y;
  2738. }
  2739. } else {
  2740. if (gl.font_rid.is_valid()) {
  2741. if (sd->orientation == ORIENTATION_HORIZONTAL) {
  2742. sd->ascent = MAX(sd->ascent, _font_get_ascent(gl.font_rid, gl.font_size) + _font_get_spacing(gl.font_rid, SPACING_TOP));
  2743. sd->descent = MAX(sd->descent, _font_get_descent(gl.font_rid, gl.font_size) + _font_get_spacing(gl.font_rid, SPACING_BOTTOM));
  2744. } else {
  2745. sd->ascent = MAX(sd->ascent, Math::round(_font_get_glyph_advance(gl.font_rid, gl.font_size, gl.index).x * 0.5));
  2746. sd->descent = MAX(sd->descent, Math::round(_font_get_glyph_advance(gl.font_rid, gl.font_size, gl.index).x * 0.5));
  2747. }
  2748. sd->upos = MAX(sd->upos, _font_get_underline_position(gl.font_rid, gl.font_size));
  2749. sd->uthk = MAX(sd->uthk, _font_get_underline_thickness(gl.font_rid, gl.font_size));
  2750. } else if (sd->preserve_invalid || (sd->preserve_control && is_control(gl.index))) {
  2751. // Glyph not found, replace with hex code box.
  2752. if (sd->orientation == ORIENTATION_HORIZONTAL) {
  2753. sd->ascent = MAX(sd->ascent, get_hex_code_box_size(gl.font_size, gl.index).y);
  2754. } else {
  2755. sd->ascent = MAX(sd->ascent, Math::round(get_hex_code_box_size(gl.font_size, gl.index).x * 0.5));
  2756. sd->descent = MAX(sd->descent, Math::round(get_hex_code_box_size(gl.font_size, gl.index).x * 0.5));
  2757. }
  2758. }
  2759. sd->width += gl.advance * gl.repeat;
  2760. }
  2761. }
  2762. _realign(sd);
  2763. }
  2764. return true;
  2765. }
  2766. void TextServerFallback::_realign(ShapedTextDataFallback *p_sd) const {
  2767. // Align embedded objects to baseline.
  2768. double full_ascent = p_sd->ascent;
  2769. double full_descent = p_sd->descent;
  2770. for (KeyValue<Variant, ShapedTextDataFallback::EmbeddedObject> &E : p_sd->objects) {
  2771. if ((E.value.start >= p_sd->start) && (E.value.start < p_sd->end)) {
  2772. if (p_sd->orientation == ORIENTATION_HORIZONTAL) {
  2773. switch (E.value.inline_align & INLINE_ALIGNMENT_TEXT_MASK) {
  2774. case INLINE_ALIGNMENT_TO_TOP: {
  2775. E.value.rect.position.y = -p_sd->ascent;
  2776. } break;
  2777. case INLINE_ALIGNMENT_TO_CENTER: {
  2778. E.value.rect.position.y = (-p_sd->ascent + p_sd->descent) / 2;
  2779. } break;
  2780. case INLINE_ALIGNMENT_TO_BASELINE: {
  2781. E.value.rect.position.y = 0;
  2782. } break;
  2783. case INLINE_ALIGNMENT_TO_BOTTOM: {
  2784. E.value.rect.position.y = p_sd->descent;
  2785. } break;
  2786. }
  2787. switch (E.value.inline_align & INLINE_ALIGNMENT_IMAGE_MASK) {
  2788. case INLINE_ALIGNMENT_BOTTOM_TO: {
  2789. E.value.rect.position.y -= E.value.rect.size.y;
  2790. } break;
  2791. case INLINE_ALIGNMENT_CENTER_TO: {
  2792. E.value.rect.position.y -= E.value.rect.size.y / 2;
  2793. } break;
  2794. case INLINE_ALIGNMENT_BASELINE_TO: {
  2795. E.value.rect.position.y -= E.value.baseline;
  2796. } break;
  2797. case INLINE_ALIGNMENT_TOP_TO: {
  2798. // NOP
  2799. } break;
  2800. }
  2801. full_ascent = MAX(full_ascent, -E.value.rect.position.y);
  2802. full_descent = MAX(full_descent, E.value.rect.position.y + E.value.rect.size.y);
  2803. } else {
  2804. switch (E.value.inline_align & INLINE_ALIGNMENT_TEXT_MASK) {
  2805. case INLINE_ALIGNMENT_TO_TOP: {
  2806. E.value.rect.position.x = -p_sd->ascent;
  2807. } break;
  2808. case INLINE_ALIGNMENT_TO_CENTER: {
  2809. E.value.rect.position.x = (-p_sd->ascent + p_sd->descent) / 2;
  2810. } break;
  2811. case INLINE_ALIGNMENT_TO_BASELINE: {
  2812. E.value.rect.position.x = 0;
  2813. } break;
  2814. case INLINE_ALIGNMENT_TO_BOTTOM: {
  2815. E.value.rect.position.x = p_sd->descent;
  2816. } break;
  2817. }
  2818. switch (E.value.inline_align & INLINE_ALIGNMENT_IMAGE_MASK) {
  2819. case INLINE_ALIGNMENT_BOTTOM_TO: {
  2820. E.value.rect.position.x -= E.value.rect.size.x;
  2821. } break;
  2822. case INLINE_ALIGNMENT_CENTER_TO: {
  2823. E.value.rect.position.x -= E.value.rect.size.x / 2;
  2824. } break;
  2825. case INLINE_ALIGNMENT_BASELINE_TO: {
  2826. E.value.rect.position.x -= E.value.baseline;
  2827. } break;
  2828. case INLINE_ALIGNMENT_TOP_TO: {
  2829. // NOP
  2830. } break;
  2831. }
  2832. full_ascent = MAX(full_ascent, -E.value.rect.position.x);
  2833. full_descent = MAX(full_descent, E.value.rect.position.x + E.value.rect.size.x);
  2834. }
  2835. }
  2836. }
  2837. p_sd->ascent = full_ascent;
  2838. p_sd->descent = full_descent;
  2839. }
  2840. RID TextServerFallback::_shaped_text_substr(const RID &p_shaped, int64_t p_start, int64_t p_length) const {
  2841. _THREAD_SAFE_METHOD_
  2842. const ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  2843. ERR_FAIL_NULL_V(sd, RID());
  2844. MutexLock lock(sd->mutex);
  2845. if (sd->parent != RID()) {
  2846. return _shaped_text_substr(sd->parent, p_start, p_length);
  2847. }
  2848. if (!sd->valid) {
  2849. const_cast<TextServerFallback *>(this)->_shaped_text_shape(p_shaped);
  2850. }
  2851. ERR_FAIL_COND_V(p_start < 0 || p_length < 0, RID());
  2852. ERR_FAIL_COND_V(sd->start > p_start || sd->end < p_start, RID());
  2853. ERR_FAIL_COND_V(sd->end < p_start + p_length, RID());
  2854. ShapedTextDataFallback *new_sd = memnew(ShapedTextDataFallback);
  2855. new_sd->parent = p_shaped;
  2856. new_sd->start = p_start;
  2857. new_sd->end = p_start + p_length;
  2858. new_sd->orientation = sd->orientation;
  2859. new_sd->direction = sd->direction;
  2860. new_sd->custom_punct = sd->custom_punct;
  2861. new_sd->para_direction = sd->para_direction;
  2862. new_sd->line_breaks_valid = sd->line_breaks_valid;
  2863. new_sd->justification_ops_valid = sd->justification_ops_valid;
  2864. new_sd->sort_valid = false;
  2865. new_sd->upos = sd->upos;
  2866. new_sd->uthk = sd->uthk;
  2867. for (int i = 0; i < TextServer::SPACING_MAX; i++) {
  2868. new_sd->extra_spacing[i] = sd->extra_spacing[i];
  2869. }
  2870. if (p_length > 0) {
  2871. new_sd->text = sd->text.substr(p_start - sd->start, p_length);
  2872. int sd_size = sd->glyphs.size();
  2873. const Glyph *sd_glyphs = sd->glyphs.ptr();
  2874. for (int i = 0; i < sd_size; i++) {
  2875. if ((sd_glyphs[i].start >= new_sd->start) && (sd_glyphs[i].end <= new_sd->end)) {
  2876. Glyph gl = sd_glyphs[i];
  2877. if (gl.end == p_start + p_length && ((gl.flags & GRAPHEME_IS_SOFT_HYPHEN) == GRAPHEME_IS_SOFT_HYPHEN)) {
  2878. gl.index = 0x00ad;
  2879. gl.advance = font_get_glyph_advance(gl.font_rid, gl.font_size, 0x00ad).x;
  2880. }
  2881. Variant key;
  2882. bool find_embedded = false;
  2883. if (gl.count == 1) {
  2884. for (const KeyValue<Variant, ShapedTextDataFallback::EmbeddedObject> &E : sd->objects) {
  2885. if (E.value.start == gl.start) {
  2886. find_embedded = true;
  2887. key = E.key;
  2888. new_sd->objects[key] = E.value;
  2889. break;
  2890. }
  2891. }
  2892. }
  2893. if (find_embedded) {
  2894. if (new_sd->orientation == ORIENTATION_HORIZONTAL) {
  2895. new_sd->objects[key].rect.position.x = new_sd->width;
  2896. new_sd->width += new_sd->objects[key].rect.size.x;
  2897. } else {
  2898. new_sd->objects[key].rect.position.y = new_sd->width;
  2899. new_sd->width += new_sd->objects[key].rect.size.y;
  2900. }
  2901. } else {
  2902. if (gl.font_rid.is_valid()) {
  2903. if (new_sd->orientation == ORIENTATION_HORIZONTAL) {
  2904. new_sd->ascent = MAX(new_sd->ascent, _font_get_ascent(gl.font_rid, gl.font_size) + _font_get_spacing(gl.font_rid, SPACING_TOP));
  2905. new_sd->descent = MAX(new_sd->descent, _font_get_descent(gl.font_rid, gl.font_size) + _font_get_spacing(gl.font_rid, SPACING_BOTTOM));
  2906. } else {
  2907. new_sd->ascent = MAX(new_sd->ascent, Math::round(_font_get_glyph_advance(gl.font_rid, gl.font_size, gl.index).x * 0.5));
  2908. new_sd->descent = MAX(new_sd->descent, Math::round(_font_get_glyph_advance(gl.font_rid, gl.font_size, gl.index).x * 0.5));
  2909. }
  2910. } else if (new_sd->preserve_invalid || (new_sd->preserve_control && is_control(gl.index))) {
  2911. // Glyph not found, replace with hex code box.
  2912. if (new_sd->orientation == ORIENTATION_HORIZONTAL) {
  2913. new_sd->ascent = MAX(new_sd->ascent, get_hex_code_box_size(gl.font_size, gl.index).y);
  2914. } else {
  2915. new_sd->ascent = MAX(new_sd->ascent, Math::round(get_hex_code_box_size(gl.font_size, gl.index).x * 0.5));
  2916. new_sd->descent = MAX(new_sd->descent, Math::round(get_hex_code_box_size(gl.font_size, gl.index).x * 0.5));
  2917. }
  2918. }
  2919. new_sd->width += gl.advance * gl.repeat;
  2920. }
  2921. new_sd->glyphs.push_back(gl);
  2922. }
  2923. }
  2924. _realign(new_sd);
  2925. }
  2926. new_sd->valid = true;
  2927. return shaped_owner.make_rid(new_sd);
  2928. }
  2929. RID TextServerFallback::_shaped_text_get_parent(const RID &p_shaped) const {
  2930. ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  2931. ERR_FAIL_NULL_V(sd, RID());
  2932. MutexLock lock(sd->mutex);
  2933. return sd->parent;
  2934. }
  2935. double TextServerFallback::_shaped_text_fit_to_width(const RID &p_shaped, double p_width, BitField<JustificationFlag> p_jst_flags) {
  2936. ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  2937. ERR_FAIL_NULL_V(sd, 0.0);
  2938. MutexLock lock(sd->mutex);
  2939. if (!sd->valid) {
  2940. const_cast<TextServerFallback *>(this)->_shaped_text_shape(p_shaped);
  2941. }
  2942. if (!sd->justification_ops_valid) {
  2943. const_cast<TextServerFallback *>(this)->_shaped_text_update_justification_ops(p_shaped);
  2944. }
  2945. int start_pos = 0;
  2946. int end_pos = sd->glyphs.size() - 1;
  2947. if (p_jst_flags.has_flag(JUSTIFICATION_AFTER_LAST_TAB)) {
  2948. int start, end, delta;
  2949. if (sd->para_direction == DIRECTION_LTR) {
  2950. start = sd->glyphs.size() - 1;
  2951. end = -1;
  2952. delta = -1;
  2953. } else {
  2954. start = 0;
  2955. end = sd->glyphs.size();
  2956. delta = +1;
  2957. }
  2958. for (int i = start; i != end; i += delta) {
  2959. if ((sd->glyphs[i].flags & GRAPHEME_IS_TAB) == GRAPHEME_IS_TAB) {
  2960. if (sd->para_direction == DIRECTION_LTR) {
  2961. start_pos = i;
  2962. break;
  2963. } else {
  2964. end_pos = i;
  2965. break;
  2966. }
  2967. }
  2968. }
  2969. }
  2970. double justification_width;
  2971. if (p_jst_flags.has_flag(JUSTIFICATION_CONSTRAIN_ELLIPSIS)) {
  2972. if (sd->overrun_trim_data.trim_pos >= 0) {
  2973. end_pos = sd->overrun_trim_data.trim_pos;
  2974. justification_width = sd->width_trimmed;
  2975. } else {
  2976. return Math::ceil(sd->width);
  2977. }
  2978. } else {
  2979. justification_width = sd->width;
  2980. }
  2981. if (p_jst_flags.has_flag(JUSTIFICATION_TRIM_EDGE_SPACES)) {
  2982. // Trim spaces.
  2983. while ((start_pos < end_pos) && ((sd->glyphs[start_pos].flags & GRAPHEME_IS_SOFT_HYPHEN) != GRAPHEME_IS_SOFT_HYPHEN) && ((sd->glyphs[start_pos].flags & GRAPHEME_IS_SPACE) == GRAPHEME_IS_SPACE || (sd->glyphs[start_pos].flags & GRAPHEME_IS_BREAK_HARD) == GRAPHEME_IS_BREAK_HARD || (sd->glyphs[start_pos].flags & GRAPHEME_IS_BREAK_SOFT) == GRAPHEME_IS_BREAK_SOFT)) {
  2984. justification_width -= sd->glyphs[start_pos].advance * sd->glyphs[start_pos].repeat;
  2985. sd->glyphs.write[start_pos].advance = 0;
  2986. start_pos += sd->glyphs[start_pos].count;
  2987. }
  2988. while ((start_pos < end_pos) && ((sd->glyphs[end_pos].flags & GRAPHEME_IS_SOFT_HYPHEN) != GRAPHEME_IS_SOFT_HYPHEN) && ((sd->glyphs[end_pos].flags & GRAPHEME_IS_SPACE) == GRAPHEME_IS_SPACE || (sd->glyphs[end_pos].flags & GRAPHEME_IS_BREAK_HARD) == GRAPHEME_IS_BREAK_HARD || (sd->glyphs[end_pos].flags & GRAPHEME_IS_BREAK_SOFT) == GRAPHEME_IS_BREAK_SOFT)) {
  2989. justification_width -= sd->glyphs[end_pos].advance * sd->glyphs[end_pos].repeat;
  2990. sd->glyphs.write[end_pos].advance = 0;
  2991. end_pos -= sd->glyphs[end_pos].count;
  2992. }
  2993. } else {
  2994. // Skip breaks, but do not reset size.
  2995. while ((start_pos < end_pos) && ((sd->glyphs[start_pos].flags & GRAPHEME_IS_SOFT_HYPHEN) != GRAPHEME_IS_SOFT_HYPHEN) && ((sd->glyphs[start_pos].flags & GRAPHEME_IS_BREAK_HARD) == GRAPHEME_IS_BREAK_HARD || (sd->glyphs[start_pos].flags & GRAPHEME_IS_BREAK_SOFT) == GRAPHEME_IS_BREAK_SOFT)) {
  2996. start_pos += sd->glyphs[start_pos].count;
  2997. }
  2998. while ((start_pos < end_pos) && ((sd->glyphs[end_pos].flags & GRAPHEME_IS_SOFT_HYPHEN) != GRAPHEME_IS_SOFT_HYPHEN) && ((sd->glyphs[end_pos].flags & GRAPHEME_IS_BREAK_HARD) == GRAPHEME_IS_BREAK_HARD || (sd->glyphs[end_pos].flags & GRAPHEME_IS_BREAK_SOFT) == GRAPHEME_IS_BREAK_SOFT)) {
  2999. end_pos -= sd->glyphs[end_pos].count;
  3000. }
  3001. }
  3002. int space_count = 0;
  3003. for (int i = start_pos; i <= end_pos; i++) {
  3004. const Glyph &gl = sd->glyphs[i];
  3005. if (gl.count > 0) {
  3006. if ((gl.flags & GRAPHEME_IS_SOFT_HYPHEN) != GRAPHEME_IS_SOFT_HYPHEN && (gl.flags & GRAPHEME_IS_SPACE) == GRAPHEME_IS_SPACE && (gl.flags & GRAPHEME_IS_PUNCTUATION) != GRAPHEME_IS_PUNCTUATION) {
  3007. space_count++;
  3008. }
  3009. }
  3010. }
  3011. if ((space_count > 0) && p_jst_flags.has_flag(JUSTIFICATION_WORD_BOUND)) {
  3012. double delta_width_per_space = (p_width - justification_width) / space_count;
  3013. for (int i = start_pos; i <= end_pos; i++) {
  3014. Glyph &gl = sd->glyphs.write[i];
  3015. if (gl.count > 0) {
  3016. if ((gl.flags & GRAPHEME_IS_SOFT_HYPHEN) != GRAPHEME_IS_SOFT_HYPHEN && (gl.flags & GRAPHEME_IS_SPACE) == GRAPHEME_IS_SPACE && (gl.flags & GRAPHEME_IS_PUNCTUATION) != GRAPHEME_IS_PUNCTUATION) {
  3017. double old_adv = gl.advance;
  3018. gl.advance = MAX(gl.advance + delta_width_per_space, Math::round(0.1 * gl.font_size));
  3019. justification_width += (gl.advance - old_adv);
  3020. }
  3021. }
  3022. }
  3023. }
  3024. if (Math::floor(p_width) < Math::floor(justification_width)) {
  3025. sd->fit_width_minimum_reached = true;
  3026. }
  3027. if (!p_jst_flags.has_flag(JUSTIFICATION_CONSTRAIN_ELLIPSIS)) {
  3028. sd->width = justification_width;
  3029. }
  3030. return Math::ceil(justification_width);
  3031. }
  3032. double TextServerFallback::_shaped_text_tab_align(const RID &p_shaped, const PackedFloat32Array &p_tab_stops) {
  3033. ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  3034. ERR_FAIL_NULL_V(sd, 0.0);
  3035. MutexLock lock(sd->mutex);
  3036. if (!sd->valid) {
  3037. const_cast<TextServerFallback *>(this)->_shaped_text_shape(p_shaped);
  3038. }
  3039. if (!sd->line_breaks_valid) {
  3040. const_cast<TextServerFallback *>(this)->_shaped_text_update_breaks(p_shaped);
  3041. }
  3042. for (int i = 0; i < p_tab_stops.size(); i++) {
  3043. if (p_tab_stops[i] <= 0) {
  3044. return 0.0;
  3045. }
  3046. }
  3047. int tab_index = 0;
  3048. double off = 0.0;
  3049. int start, end, delta;
  3050. if (sd->para_direction == DIRECTION_LTR) {
  3051. start = 0;
  3052. end = sd->glyphs.size();
  3053. delta = +1;
  3054. } else {
  3055. start = sd->glyphs.size() - 1;
  3056. end = -1;
  3057. delta = -1;
  3058. }
  3059. Glyph *gl = sd->glyphs.ptrw();
  3060. for (int i = start; i != end; i += delta) {
  3061. if ((gl[i].flags & GRAPHEME_IS_TAB) == GRAPHEME_IS_TAB) {
  3062. double tab_off = 0.0;
  3063. while (tab_off <= off) {
  3064. tab_off += p_tab_stops[tab_index];
  3065. tab_index++;
  3066. if (tab_index >= p_tab_stops.size()) {
  3067. tab_index = 0;
  3068. }
  3069. }
  3070. double old_adv = gl[i].advance;
  3071. gl[i].advance = tab_off - off;
  3072. sd->width += gl[i].advance - old_adv;
  3073. off = 0;
  3074. continue;
  3075. }
  3076. off += gl[i].advance * gl[i].repeat;
  3077. }
  3078. return 0.0;
  3079. }
  3080. bool TextServerFallback::_shaped_text_update_breaks(const RID &p_shaped) {
  3081. ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  3082. ERR_FAIL_NULL_V(sd, false);
  3083. MutexLock lock(sd->mutex);
  3084. if (!sd->valid) {
  3085. _shaped_text_shape(p_shaped);
  3086. }
  3087. if (sd->line_breaks_valid) {
  3088. return true; // Nothing to do.
  3089. }
  3090. int sd_size = sd->glyphs.size();
  3091. Glyph *sd_glyphs = sd->glyphs.ptrw();
  3092. int c_punct_size = sd->custom_punct.length();
  3093. const char32_t *c_punct = sd->custom_punct.ptr();
  3094. for (int i = 0; i < sd_size; i++) {
  3095. if (sd_glyphs[i].count > 0) {
  3096. char32_t c = sd->text[sd_glyphs[i].start - sd->start];
  3097. char32_t c_next = i < sd_size ? sd->text[sd_glyphs[i].start - sd->start + 1] : 0x0000;
  3098. if (c_punct_size == 0) {
  3099. if (is_punct(c) && c != 0x005F && c != ' ') {
  3100. sd_glyphs[i].flags |= GRAPHEME_IS_PUNCTUATION;
  3101. }
  3102. } else {
  3103. for (int j = 0; j < c_punct_size; j++) {
  3104. if (c_punct[j] == c) {
  3105. sd_glyphs[i].flags |= GRAPHEME_IS_PUNCTUATION;
  3106. break;
  3107. }
  3108. }
  3109. }
  3110. if (is_underscore(c)) {
  3111. sd->glyphs.write[i].flags |= GRAPHEME_IS_UNDERSCORE;
  3112. }
  3113. if (is_whitespace(c) && !is_linebreak(c)) {
  3114. sd_glyphs[i].flags |= GRAPHEME_IS_SPACE;
  3115. if (c != 0x00A0 && c != 0x202F && c != 0x2060 && c != 0x2007) { // Skip for non-breaking space variants.
  3116. sd_glyphs[i].flags |= GRAPHEME_IS_BREAK_SOFT;
  3117. }
  3118. }
  3119. if (is_linebreak(c)) {
  3120. sd_glyphs[i].flags |= GRAPHEME_IS_SPACE;
  3121. if (c != 0x000D || c_next != 0x000A) { // Skip first hard break in CR-LF pair.
  3122. sd_glyphs[i].flags |= GRAPHEME_IS_BREAK_HARD;
  3123. }
  3124. }
  3125. if (c == 0x0009 || c == 0x000b) {
  3126. sd_glyphs[i].flags |= GRAPHEME_IS_TAB;
  3127. }
  3128. if (c == 0x00ad) {
  3129. sd_glyphs[i].flags |= GRAPHEME_IS_SOFT_HYPHEN;
  3130. }
  3131. i += (sd_glyphs[i].count - 1);
  3132. }
  3133. }
  3134. sd->line_breaks_valid = true;
  3135. return sd->line_breaks_valid;
  3136. }
  3137. bool TextServerFallback::_shaped_text_update_justification_ops(const RID &p_shaped) {
  3138. ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  3139. ERR_FAIL_NULL_V(sd, false);
  3140. MutexLock lock(sd->mutex);
  3141. if (!sd->valid) {
  3142. _shaped_text_shape(p_shaped);
  3143. }
  3144. if (!sd->line_breaks_valid) {
  3145. _shaped_text_update_breaks(p_shaped);
  3146. }
  3147. sd->justification_ops_valid = true; // Not supported by fallback server.
  3148. return true;
  3149. }
  3150. RID TextServerFallback::_find_sys_font_for_text(const RID &p_fdef, const String &p_script_code, const String &p_language, const String &p_text) {
  3151. RID f;
  3152. // Try system fallback.
  3153. if (_font_is_allow_system_fallback(p_fdef)) {
  3154. String font_name = _font_get_name(p_fdef);
  3155. BitField<FontStyle> font_style = _font_get_style(p_fdef);
  3156. int font_weight = _font_get_weight(p_fdef);
  3157. int font_stretch = _font_get_stretch(p_fdef);
  3158. Dictionary dvar = _font_get_variation_coordinates(p_fdef);
  3159. static int64_t wgth_tag = name_to_tag("weight");
  3160. static int64_t wdth_tag = name_to_tag("width");
  3161. static int64_t ital_tag = name_to_tag("italic");
  3162. if (dvar.has(wgth_tag)) {
  3163. font_weight = dvar[wgth_tag].operator int();
  3164. }
  3165. if (dvar.has(wdth_tag)) {
  3166. font_stretch = dvar[wdth_tag].operator int();
  3167. }
  3168. if (dvar.has(ital_tag) && dvar[ital_tag].operator int() == 1) {
  3169. font_style.set_flag(TextServer::FONT_ITALIC);
  3170. }
  3171. String locale = (p_language.is_empty()) ? TranslationServer::get_singleton()->get_tool_locale() : p_language;
  3172. PackedStringArray fallback_font_name = OS::get_singleton()->get_system_font_path_for_text(font_name, p_text, locale, p_script_code, font_weight, font_stretch, font_style & TextServer::FONT_ITALIC);
  3173. #ifdef GDEXTENSION
  3174. for (int fb = 0; fb < fallback_font_name.size(); fb++) {
  3175. const String &E = fallback_font_name[fb];
  3176. #elif defined(GODOT_MODULE)
  3177. for (const String &E : fallback_font_name) {
  3178. #endif
  3179. SystemFontKey key = SystemFontKey(E, font_style & TextServer::FONT_ITALIC, font_weight, font_stretch, p_fdef, this);
  3180. if (system_fonts.has(key)) {
  3181. const SystemFontCache &sysf_cache = system_fonts[key];
  3182. int best_score = 0;
  3183. int best_match = -1;
  3184. for (int face_idx = 0; face_idx < sysf_cache.var.size(); face_idx++) {
  3185. const SystemFontCacheRec &F = sysf_cache.var[face_idx];
  3186. if (unlikely(!_font_has_char(F.rid, p_text[0]))) {
  3187. continue;
  3188. }
  3189. BitField<FontStyle> style = _font_get_style(F.rid);
  3190. int weight = _font_get_weight(F.rid);
  3191. int stretch = _font_get_stretch(F.rid);
  3192. int score = (20 - Math::abs(weight - font_weight) / 50);
  3193. score += (20 - Math::abs(stretch - font_stretch) / 10);
  3194. if (bool(style & TextServer::FONT_ITALIC) == bool(font_style & TextServer::FONT_ITALIC)) {
  3195. score += 30;
  3196. }
  3197. if (score >= best_score) {
  3198. best_score = score;
  3199. best_match = face_idx;
  3200. }
  3201. if (best_score == 70) {
  3202. break;
  3203. }
  3204. }
  3205. if (best_match != -1) {
  3206. f = sysf_cache.var[best_match].rid;
  3207. }
  3208. }
  3209. if (!f.is_valid()) {
  3210. if (system_fonts.has(key)) {
  3211. const SystemFontCache &sysf_cache = system_fonts[key];
  3212. if (sysf_cache.max_var == sysf_cache.var.size()) {
  3213. // All subfonts already tested, skip.
  3214. continue;
  3215. }
  3216. }
  3217. if (!system_font_data.has(E)) {
  3218. system_font_data[E] = FileAccess::get_file_as_bytes(E);
  3219. }
  3220. const PackedByteArray &font_data = system_font_data[E];
  3221. SystemFontCacheRec sysf;
  3222. sysf.rid = _create_font();
  3223. _font_set_data_ptr(sysf.rid, font_data.ptr(), font_data.size());
  3224. Dictionary var = dvar;
  3225. // Select matching style from collection.
  3226. int best_score = 0;
  3227. int best_match = -1;
  3228. for (int face_idx = 0; face_idx < _font_get_face_count(sysf.rid); face_idx++) {
  3229. _font_set_face_index(sysf.rid, face_idx);
  3230. if (unlikely(!_font_has_char(sysf.rid, p_text[0]))) {
  3231. continue;
  3232. }
  3233. BitField<FontStyle> style = _font_get_style(sysf.rid);
  3234. int weight = _font_get_weight(sysf.rid);
  3235. int stretch = _font_get_stretch(sysf.rid);
  3236. int score = (20 - Math::abs(weight - font_weight) / 50);
  3237. score += (20 - Math::abs(stretch - font_stretch) / 10);
  3238. if (bool(style & TextServer::FONT_ITALIC) == bool(font_style & TextServer::FONT_ITALIC)) {
  3239. score += 30;
  3240. }
  3241. if (score >= best_score) {
  3242. best_score = score;
  3243. best_match = face_idx;
  3244. }
  3245. if (best_score == 70) {
  3246. break;
  3247. }
  3248. }
  3249. if (best_match == -1) {
  3250. _free_rid(sysf.rid);
  3251. continue;
  3252. } else {
  3253. _font_set_face_index(sysf.rid, best_match);
  3254. }
  3255. sysf.index = best_match;
  3256. // If it's a variable font, apply weight, stretch and italic coordinates to match requested style.
  3257. if (best_score != 70) {
  3258. Dictionary ftr = _font_supported_variation_list(sysf.rid);
  3259. if (ftr.has(wdth_tag)) {
  3260. var[wdth_tag] = font_stretch;
  3261. _font_set_stretch(sysf.rid, font_stretch);
  3262. }
  3263. if (ftr.has(wgth_tag)) {
  3264. var[wgth_tag] = font_weight;
  3265. _font_set_weight(sysf.rid, font_weight);
  3266. }
  3267. if ((font_style & TextServer::FONT_ITALIC) && ftr.has(ital_tag)) {
  3268. var[ital_tag] = 1;
  3269. _font_set_style(sysf.rid, _font_get_style(sysf.rid) | TextServer::FONT_ITALIC);
  3270. }
  3271. }
  3272. _font_set_antialiasing(sysf.rid, key.antialiasing);
  3273. _font_set_disable_embedded_bitmaps(sysf.rid, key.disable_embedded_bitmaps);
  3274. _font_set_generate_mipmaps(sysf.rid, key.mipmaps);
  3275. _font_set_multichannel_signed_distance_field(sysf.rid, key.msdf);
  3276. _font_set_msdf_pixel_range(sysf.rid, key.msdf_range);
  3277. _font_set_msdf_size(sysf.rid, key.msdf_source_size);
  3278. _font_set_fixed_size(sysf.rid, key.fixed_size);
  3279. _font_set_force_autohinter(sysf.rid, key.force_autohinter);
  3280. _font_set_hinting(sysf.rid, key.hinting);
  3281. _font_set_subpixel_positioning(sysf.rid, key.subpixel_positioning);
  3282. _font_set_variation_coordinates(sysf.rid, var);
  3283. _font_set_oversampling(sysf.rid, key.oversampling);
  3284. _font_set_embolden(sysf.rid, key.embolden);
  3285. _font_set_transform(sysf.rid, key.transform);
  3286. _font_set_spacing(sysf.rid, SPACING_TOP, key.extra_spacing[SPACING_TOP]);
  3287. _font_set_spacing(sysf.rid, SPACING_BOTTOM, key.extra_spacing[SPACING_BOTTOM]);
  3288. _font_set_spacing(sysf.rid, SPACING_SPACE, key.extra_spacing[SPACING_SPACE]);
  3289. _font_set_spacing(sysf.rid, SPACING_GLYPH, key.extra_spacing[SPACING_GLYPH]);
  3290. if (system_fonts.has(key)) {
  3291. system_fonts[key].var.push_back(sysf);
  3292. } else {
  3293. SystemFontCache &sysf_cache = system_fonts[key];
  3294. sysf_cache.max_var = _font_get_face_count(sysf.rid);
  3295. sysf_cache.var.push_back(sysf);
  3296. }
  3297. f = sysf.rid;
  3298. }
  3299. break;
  3300. }
  3301. }
  3302. return f;
  3303. }
  3304. void TextServerFallback::_shaped_text_overrun_trim_to_width(const RID &p_shaped_line, double p_width, BitField<TextServer::TextOverrunFlag> p_trim_flags) {
  3305. ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped_line);
  3306. ERR_FAIL_NULL_MSG(sd, "ShapedTextDataFallback invalid.");
  3307. MutexLock lock(sd->mutex);
  3308. if (!sd->valid) {
  3309. _shaped_text_shape(p_shaped_line);
  3310. }
  3311. sd->text_trimmed = false;
  3312. sd->overrun_trim_data.ellipsis_glyph_buf.clear();
  3313. bool add_ellipsis = p_trim_flags.has_flag(OVERRUN_ADD_ELLIPSIS);
  3314. bool cut_per_word = p_trim_flags.has_flag(OVERRUN_TRIM_WORD_ONLY);
  3315. bool enforce_ellipsis = p_trim_flags.has_flag(OVERRUN_ENFORCE_ELLIPSIS);
  3316. bool justification_aware = p_trim_flags.has_flag(OVERRUN_JUSTIFICATION_AWARE);
  3317. Glyph *sd_glyphs = sd->glyphs.ptrw();
  3318. if ((p_trim_flags & OVERRUN_TRIM) == OVERRUN_NO_TRIM || sd_glyphs == nullptr || p_width <= 0 || !(sd->width > p_width || enforce_ellipsis)) {
  3319. sd->overrun_trim_data.trim_pos = -1;
  3320. sd->overrun_trim_data.ellipsis_pos = -1;
  3321. return;
  3322. }
  3323. if (justification_aware && !sd->fit_width_minimum_reached) {
  3324. return;
  3325. }
  3326. Vector<ShapedTextDataFallback::Span> &spans = sd->spans;
  3327. if (sd->parent != RID()) {
  3328. ShapedTextDataFallback *parent_sd = shaped_owner.get_or_null(sd->parent);
  3329. ERR_FAIL_COND(!parent_sd->valid);
  3330. spans = parent_sd->spans;
  3331. }
  3332. if (spans.size() == 0) {
  3333. return;
  3334. }
  3335. int sd_size = sd->glyphs.size();
  3336. int last_gl_font_size = sd_glyphs[sd_size - 1].font_size;
  3337. bool found_el_char = false;
  3338. // Find usable fonts, if fonts from the last glyph do not have required chars.
  3339. RID dot_gl_font_rid = sd_glyphs[sd_size - 1].font_rid;
  3340. if (!_font_has_char(dot_gl_font_rid, sd->el_char)) {
  3341. const Array &fonts = spans[spans.size() - 1].fonts;
  3342. for (int i = 0; i < fonts.size(); i++) {
  3343. if (_font_has_char(fonts[i], sd->el_char)) {
  3344. dot_gl_font_rid = fonts[i];
  3345. found_el_char = true;
  3346. break;
  3347. }
  3348. }
  3349. if (!found_el_char && OS::get_singleton()->has_feature("system_fonts") && fonts.size() > 0 && _font_is_allow_system_fallback(fonts[0])) {
  3350. const char32_t u32str[] = { sd->el_char, 0 };
  3351. RID rid = _find_sys_font_for_text(fonts[0], String(), spans[spans.size() - 1].language, u32str);
  3352. if (rid.is_valid()) {
  3353. dot_gl_font_rid = rid;
  3354. found_el_char = true;
  3355. }
  3356. }
  3357. } else {
  3358. found_el_char = true;
  3359. }
  3360. if (!found_el_char) {
  3361. bool found_dot_char = false;
  3362. dot_gl_font_rid = sd_glyphs[sd_size - 1].font_rid;
  3363. if (!_font_has_char(dot_gl_font_rid, '.')) {
  3364. const Array &fonts = spans[spans.size() - 1].fonts;
  3365. for (int i = 0; i < fonts.size(); i++) {
  3366. if (_font_has_char(fonts[i], '.')) {
  3367. dot_gl_font_rid = fonts[i];
  3368. found_dot_char = true;
  3369. break;
  3370. }
  3371. }
  3372. if (!found_dot_char && OS::get_singleton()->has_feature("system_fonts") && fonts.size() > 0 && _font_is_allow_system_fallback(fonts[0])) {
  3373. RID rid = _find_sys_font_for_text(fonts[0], String(), spans[spans.size() - 1].language, ".");
  3374. if (rid.is_valid()) {
  3375. dot_gl_font_rid = rid;
  3376. }
  3377. }
  3378. }
  3379. }
  3380. RID whitespace_gl_font_rid = sd_glyphs[sd_size - 1].font_rid;
  3381. if (!_font_has_char(whitespace_gl_font_rid, ' ')) {
  3382. const Array &fonts = spans[spans.size() - 1].fonts;
  3383. for (int i = 0; i < fonts.size(); i++) {
  3384. if (_font_has_char(fonts[i], ' ')) {
  3385. whitespace_gl_font_rid = fonts[i];
  3386. break;
  3387. }
  3388. }
  3389. }
  3390. int32_t dot_gl_idx = dot_gl_font_rid.is_valid() ? _font_get_glyph_index(dot_gl_font_rid, last_gl_font_size, (found_el_char ? sd->el_char : '.'), 0) : -1;
  3391. 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();
  3392. int32_t whitespace_gl_idx = whitespace_gl_font_rid.is_valid() ? _font_get_glyph_index(whitespace_gl_font_rid, last_gl_font_size, ' ', 0) : -1;
  3393. 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();
  3394. int ellipsis_width = 0;
  3395. if (add_ellipsis && whitespace_gl_font_rid.is_valid()) {
  3396. ellipsis_width = (found_el_char ? 1 : 3) * dot_adv.x + sd->extra_spacing[SPACING_GLYPH] + _font_get_spacing(dot_gl_font_rid, SPACING_GLYPH) + (cut_per_word ? whitespace_adv.x : 0);
  3397. }
  3398. int ell_min_characters = 6;
  3399. double width = sd->width;
  3400. int trim_pos = 0;
  3401. int ellipsis_pos = (enforce_ellipsis) ? 0 : -1;
  3402. int last_valid_cut = 0;
  3403. bool found = false;
  3404. if (enforce_ellipsis && (width + ellipsis_width <= p_width)) {
  3405. trim_pos = -1;
  3406. ellipsis_pos = sd_size;
  3407. } else {
  3408. for (int i = sd_size - 1; i != -1; i--) {
  3409. width -= sd_glyphs[i].advance * sd_glyphs[i].repeat;
  3410. if (sd_glyphs[i].count > 0) {
  3411. bool above_min_char_threshold = (i >= ell_min_characters);
  3412. if (width + (((above_min_char_threshold && add_ellipsis) || enforce_ellipsis) ? ellipsis_width : 0) <= p_width) {
  3413. if (cut_per_word && above_min_char_threshold) {
  3414. if ((sd_glyphs[i].flags & GRAPHEME_IS_BREAK_SOFT) == GRAPHEME_IS_BREAK_SOFT) {
  3415. last_valid_cut = i;
  3416. found = true;
  3417. }
  3418. } else {
  3419. last_valid_cut = i;
  3420. found = true;
  3421. }
  3422. if (found) {
  3423. trim_pos = last_valid_cut;
  3424. if (add_ellipsis && (above_min_char_threshold || enforce_ellipsis) && width - ellipsis_width <= p_width) {
  3425. ellipsis_pos = trim_pos;
  3426. }
  3427. break;
  3428. }
  3429. }
  3430. }
  3431. }
  3432. }
  3433. sd->overrun_trim_data.trim_pos = trim_pos;
  3434. sd->overrun_trim_data.ellipsis_pos = ellipsis_pos;
  3435. if (trim_pos == 0 && enforce_ellipsis && add_ellipsis) {
  3436. sd->overrun_trim_data.ellipsis_pos = 0;
  3437. }
  3438. if ((trim_pos >= 0 && sd->width > p_width) || enforce_ellipsis) {
  3439. if (add_ellipsis && (ellipsis_pos > 0 || enforce_ellipsis)) {
  3440. // Insert an additional space when cutting word bound for aesthetics.
  3441. if (cut_per_word && (ellipsis_pos > 0)) {
  3442. Glyph gl;
  3443. gl.count = 1;
  3444. gl.advance = whitespace_adv.x;
  3445. gl.index = whitespace_gl_idx;
  3446. gl.font_rid = whitespace_gl_font_rid;
  3447. gl.font_size = last_gl_font_size;
  3448. gl.flags = GRAPHEME_IS_SPACE | GRAPHEME_IS_BREAK_SOFT | GRAPHEME_IS_VIRTUAL;
  3449. sd->overrun_trim_data.ellipsis_glyph_buf.append(gl);
  3450. }
  3451. // Add ellipsis dots.
  3452. if (dot_gl_idx != 0) {
  3453. Glyph gl;
  3454. gl.count = 1;
  3455. gl.repeat = (found_el_char ? 1 : 3);
  3456. gl.advance = dot_adv.x;
  3457. gl.index = dot_gl_idx;
  3458. gl.font_rid = dot_gl_font_rid;
  3459. gl.font_size = last_gl_font_size;
  3460. gl.flags = GRAPHEME_IS_PUNCTUATION | GRAPHEME_IS_VIRTUAL;
  3461. sd->overrun_trim_data.ellipsis_glyph_buf.append(gl);
  3462. }
  3463. }
  3464. sd->text_trimmed = true;
  3465. sd->width_trimmed = width + ((ellipsis_pos != -1) ? ellipsis_width : 0);
  3466. }
  3467. }
  3468. int64_t TextServerFallback::_shaped_text_get_trim_pos(const RID &p_shaped) const {
  3469. ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  3470. ERR_FAIL_NULL_V_MSG(sd, -1, "ShapedTextDataFallback invalid.");
  3471. MutexLock lock(sd->mutex);
  3472. return sd->overrun_trim_data.trim_pos;
  3473. }
  3474. int64_t TextServerFallback::_shaped_text_get_ellipsis_pos(const RID &p_shaped) const {
  3475. ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  3476. ERR_FAIL_NULL_V_MSG(sd, -1, "ShapedTextDataFallback invalid.");
  3477. MutexLock lock(sd->mutex);
  3478. return sd->overrun_trim_data.ellipsis_pos;
  3479. }
  3480. const Glyph *TextServerFallback::_shaped_text_get_ellipsis_glyphs(const RID &p_shaped) const {
  3481. ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  3482. ERR_FAIL_NULL_V_MSG(sd, nullptr, "ShapedTextDataFallback invalid.");
  3483. MutexLock lock(sd->mutex);
  3484. return sd->overrun_trim_data.ellipsis_glyph_buf.ptr();
  3485. }
  3486. int64_t TextServerFallback::_shaped_text_get_ellipsis_glyph_count(const RID &p_shaped) const {
  3487. ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  3488. ERR_FAIL_NULL_V_MSG(sd, 0, "ShapedTextDataFallback invalid.");
  3489. MutexLock lock(sd->mutex);
  3490. return sd->overrun_trim_data.ellipsis_glyph_buf.size();
  3491. }
  3492. bool TextServerFallback::_shaped_text_shape(const RID &p_shaped) {
  3493. ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  3494. ERR_FAIL_NULL_V(sd, false);
  3495. MutexLock lock(sd->mutex);
  3496. if (sd->valid) {
  3497. return true;
  3498. }
  3499. if (sd->parent != RID()) {
  3500. full_copy(sd);
  3501. }
  3502. // Cleanup.
  3503. sd->justification_ops_valid = false;
  3504. sd->line_breaks_valid = false;
  3505. sd->ascent = 0.0;
  3506. sd->descent = 0.0;
  3507. sd->width = 0.0;
  3508. sd->glyphs.clear();
  3509. if (sd->text.length() == 0) {
  3510. sd->valid = true;
  3511. return true;
  3512. }
  3513. // "Shape" string.
  3514. for (int i = 0; i < sd->spans.size(); i++) {
  3515. const ShapedTextDataFallback::Span &span = sd->spans[i];
  3516. if (span.embedded_key != Variant()) {
  3517. // Embedded object.
  3518. if (sd->orientation == ORIENTATION_HORIZONTAL) {
  3519. sd->objects[span.embedded_key].rect.position.x = sd->width;
  3520. sd->width += sd->objects[span.embedded_key].rect.size.x;
  3521. } else {
  3522. sd->objects[span.embedded_key].rect.position.y = sd->width;
  3523. sd->width += sd->objects[span.embedded_key].rect.size.y;
  3524. }
  3525. Glyph gl;
  3526. gl.start = span.start;
  3527. gl.end = span.end;
  3528. gl.count = 1;
  3529. gl.index = 0;
  3530. gl.flags = GRAPHEME_IS_VALID | GRAPHEME_IS_EMBEDDED_OBJECT;
  3531. if (sd->orientation == ORIENTATION_HORIZONTAL) {
  3532. gl.advance = sd->objects[span.embedded_key].rect.size.x;
  3533. } else {
  3534. gl.advance = sd->objects[span.embedded_key].rect.size.y;
  3535. }
  3536. sd->glyphs.push_back(gl);
  3537. } else {
  3538. // Text span.
  3539. RID prev_font;
  3540. for (int j = span.start; j < span.end; j++) {
  3541. Glyph gl;
  3542. gl.start = j;
  3543. gl.end = j + 1;
  3544. gl.count = 1;
  3545. gl.font_size = span.font_size;
  3546. gl.index = (int32_t)sd->text[j - sd->start]; // Use codepoint.
  3547. if (gl.index == 0x0009 || gl.index == 0x000b) {
  3548. gl.index = 0x0020;
  3549. }
  3550. if (!sd->preserve_control && is_control(gl.index)) {
  3551. gl.index = 0x0020;
  3552. }
  3553. // Select first font which has character (font are already sorted by span language).
  3554. for (int k = 0; k < span.fonts.size(); k++) {
  3555. if (_font_has_char(span.fonts[k], gl.index)) {
  3556. gl.font_rid = span.fonts[k];
  3557. break;
  3558. }
  3559. }
  3560. if (!gl.font_rid.is_valid() && prev_font.is_valid()) {
  3561. if (_font_has_char(prev_font, gl.index)) {
  3562. gl.font_rid = prev_font;
  3563. }
  3564. }
  3565. if (!gl.font_rid.is_valid() && OS::get_singleton()->has_feature("system_fonts") && span.fonts.size() > 0) {
  3566. // Try system fallback.
  3567. RID fdef = span.fonts[0];
  3568. if (_font_is_allow_system_fallback(fdef)) {
  3569. String text = sd->text.substr(j, 1);
  3570. gl.font_rid = _find_sys_font_for_text(fdef, String(), span.language, text);
  3571. }
  3572. }
  3573. prev_font = gl.font_rid;
  3574. if (gl.font_rid.is_valid()) {
  3575. double scale = _font_get_scale(gl.font_rid, gl.font_size);
  3576. bool subpos = (scale != 1.0) || (_font_get_subpixel_positioning(gl.font_rid) == SUBPIXEL_POSITIONING_ONE_HALF) || (_font_get_subpixel_positioning(gl.font_rid) == SUBPIXEL_POSITIONING_ONE_QUARTER) || (_font_get_subpixel_positioning(gl.font_rid) == SUBPIXEL_POSITIONING_AUTO && gl.font_size <= SUBPIXEL_POSITIONING_ONE_HALF_MAX_SIZE);
  3577. if (sd->text[j - sd->start] != 0 && !is_linebreak(sd->text[j - sd->start])) {
  3578. if (sd->orientation == ORIENTATION_HORIZONTAL) {
  3579. gl.advance = _font_get_glyph_advance(gl.font_rid, gl.font_size, gl.index).x;
  3580. gl.x_off = 0;
  3581. gl.y_off = _font_get_baseline_offset(gl.font_rid) * (double)(_font_get_ascent(gl.font_rid, gl.font_size) + _font_get_descent(gl.font_rid, gl.font_size));
  3582. sd->ascent = MAX(sd->ascent, _font_get_ascent(gl.font_rid, gl.font_size) + _font_get_spacing(gl.font_rid, SPACING_TOP));
  3583. sd->descent = MAX(sd->descent, _font_get_descent(gl.font_rid, gl.font_size) + _font_get_spacing(gl.font_rid, SPACING_BOTTOM));
  3584. } else {
  3585. gl.advance = _font_get_glyph_advance(gl.font_rid, gl.font_size, gl.index).y;
  3586. gl.x_off = -Math::round(_font_get_glyph_advance(gl.font_rid, gl.font_size, gl.index).x * 0.5) + _font_get_baseline_offset(gl.font_rid) * (double)(_font_get_ascent(gl.font_rid, gl.font_size) + _font_get_descent(gl.font_rid, gl.font_size));
  3587. gl.y_off = _font_get_ascent(gl.font_rid, gl.font_size);
  3588. sd->ascent = MAX(sd->ascent, Math::round(_font_get_glyph_advance(gl.font_rid, gl.font_size, gl.index).x * 0.5));
  3589. sd->descent = MAX(sd->descent, Math::round(_font_get_glyph_advance(gl.font_rid, gl.font_size, gl.index).x * 0.5));
  3590. }
  3591. }
  3592. if (j < sd->end - 1) {
  3593. // Do not add extra spacing to the last glyph of the string.
  3594. if (is_whitespace(sd->text[j - sd->start])) {
  3595. gl.advance += sd->extra_spacing[SPACING_SPACE] + _font_get_spacing(gl.font_rid, SPACING_SPACE);
  3596. } else {
  3597. gl.advance += sd->extra_spacing[SPACING_GLYPH] + _font_get_spacing(gl.font_rid, SPACING_GLYPH);
  3598. }
  3599. }
  3600. sd->upos = MAX(sd->upos, _font_get_underline_position(gl.font_rid, gl.font_size));
  3601. sd->uthk = MAX(sd->uthk, _font_get_underline_thickness(gl.font_rid, gl.font_size));
  3602. // Add kerning to previous glyph.
  3603. if (sd->glyphs.size() > 0) {
  3604. Glyph &prev_gl = sd->glyphs.write[sd->glyphs.size() - 1];
  3605. if (prev_gl.font_rid == gl.font_rid && prev_gl.font_size == gl.font_size) {
  3606. if (sd->orientation == ORIENTATION_HORIZONTAL) {
  3607. prev_gl.advance += _font_get_kerning(gl.font_rid, gl.font_size, Vector2i(prev_gl.index, gl.index)).x;
  3608. } else {
  3609. prev_gl.advance += _font_get_kerning(gl.font_rid, gl.font_size, Vector2i(prev_gl.index, gl.index)).y;
  3610. }
  3611. }
  3612. }
  3613. if (sd->orientation == ORIENTATION_HORIZONTAL && !subpos) {
  3614. gl.advance = Math::round(gl.advance);
  3615. }
  3616. } else if (sd->preserve_invalid || (sd->preserve_control && is_control(gl.index))) {
  3617. // Glyph not found, replace with hex code box.
  3618. if (sd->orientation == ORIENTATION_HORIZONTAL) {
  3619. gl.advance = get_hex_code_box_size(gl.font_size, gl.index).x;
  3620. sd->ascent = MAX(sd->ascent, get_hex_code_box_size(gl.font_size, gl.index).y);
  3621. } else {
  3622. gl.advance = get_hex_code_box_size(gl.font_size, gl.index).y;
  3623. sd->ascent = MAX(sd->ascent, Math::round(get_hex_code_box_size(gl.font_size, gl.index).x * 0.5));
  3624. sd->descent = MAX(sd->descent, Math::round(get_hex_code_box_size(gl.font_size, gl.index).x * 0.5));
  3625. }
  3626. }
  3627. sd->width += gl.advance;
  3628. sd->glyphs.push_back(gl);
  3629. }
  3630. }
  3631. }
  3632. // Align embedded objects to baseline.
  3633. _realign(sd);
  3634. sd->valid = true;
  3635. return sd->valid;
  3636. }
  3637. bool TextServerFallback::_shaped_text_is_ready(const RID &p_shaped) const {
  3638. const ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  3639. ERR_FAIL_NULL_V(sd, false);
  3640. MutexLock lock(sd->mutex);
  3641. return sd->valid;
  3642. }
  3643. const Glyph *TextServerFallback::_shaped_text_get_glyphs(const RID &p_shaped) const {
  3644. const ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  3645. ERR_FAIL_NULL_V(sd, nullptr);
  3646. MutexLock lock(sd->mutex);
  3647. if (!sd->valid) {
  3648. const_cast<TextServerFallback *>(this)->_shaped_text_shape(p_shaped);
  3649. }
  3650. return sd->glyphs.ptr();
  3651. }
  3652. int64_t TextServerFallback::_shaped_text_get_glyph_count(const RID &p_shaped) const {
  3653. const ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  3654. ERR_FAIL_NULL_V(sd, 0);
  3655. MutexLock lock(sd->mutex);
  3656. if (!sd->valid) {
  3657. const_cast<TextServerFallback *>(this)->_shaped_text_shape(p_shaped);
  3658. }
  3659. return sd->glyphs.size();
  3660. }
  3661. const Glyph *TextServerFallback::_shaped_text_sort_logical(const RID &p_shaped) {
  3662. const ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  3663. ERR_FAIL_NULL_V(sd, nullptr);
  3664. MutexLock lock(sd->mutex);
  3665. if (!sd->valid) {
  3666. const_cast<TextServerFallback *>(this)->_shaped_text_shape(p_shaped);
  3667. }
  3668. return sd->glyphs.ptr(); // Already in the logical order, return as is.
  3669. }
  3670. Vector2i TextServerFallback::_shaped_text_get_range(const RID &p_shaped) const {
  3671. const ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  3672. ERR_FAIL_NULL_V(sd, Vector2i());
  3673. MutexLock lock(sd->mutex);
  3674. return Vector2(sd->start, sd->end);
  3675. }
  3676. Array TextServerFallback::_shaped_text_get_objects(const RID &p_shaped) const {
  3677. Array ret;
  3678. const ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  3679. ERR_FAIL_NULL_V(sd, ret);
  3680. MutexLock lock(sd->mutex);
  3681. for (const KeyValue<Variant, ShapedTextDataFallback::EmbeddedObject> &E : sd->objects) {
  3682. ret.push_back(E.key);
  3683. }
  3684. return ret;
  3685. }
  3686. Rect2 TextServerFallback::_shaped_text_get_object_rect(const RID &p_shaped, const Variant &p_key) const {
  3687. const ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  3688. ERR_FAIL_NULL_V(sd, Rect2());
  3689. MutexLock lock(sd->mutex);
  3690. ERR_FAIL_COND_V(!sd->objects.has(p_key), Rect2());
  3691. if (!sd->valid) {
  3692. const_cast<TextServerFallback *>(this)->_shaped_text_shape(p_shaped);
  3693. }
  3694. return sd->objects[p_key].rect;
  3695. }
  3696. Vector2i TextServerFallback::_shaped_text_get_object_range(const RID &p_shaped, const Variant &p_key) const {
  3697. const ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  3698. ERR_FAIL_NULL_V(sd, Vector2i());
  3699. MutexLock lock(sd->mutex);
  3700. ERR_FAIL_COND_V(!sd->objects.has(p_key), Vector2i());
  3701. return Vector2i(sd->objects[p_key].start, sd->objects[p_key].end);
  3702. }
  3703. int64_t TextServerFallback::_shaped_text_get_object_glyph(const RID &p_shaped, const Variant &p_key) const {
  3704. const ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  3705. ERR_FAIL_NULL_V(sd, -1);
  3706. MutexLock lock(sd->mutex);
  3707. ERR_FAIL_COND_V(!sd->objects.has(p_key), -1);
  3708. const ShapedTextDataFallback::EmbeddedObject &obj = sd->objects[p_key];
  3709. int sd_size = sd->glyphs.size();
  3710. const Glyph *sd_glyphs = sd->glyphs.ptr();
  3711. for (int i = 0; i < sd_size; i++) {
  3712. if (obj.start == sd_glyphs[i].start) {
  3713. return i;
  3714. }
  3715. }
  3716. return -1;
  3717. }
  3718. Size2 TextServerFallback::_shaped_text_get_size(const RID &p_shaped) const {
  3719. const ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  3720. ERR_FAIL_NULL_V(sd, Size2());
  3721. MutexLock lock(sd->mutex);
  3722. if (!sd->valid) {
  3723. const_cast<TextServerFallback *>(this)->_shaped_text_shape(p_shaped);
  3724. }
  3725. if (sd->orientation == TextServer::ORIENTATION_HORIZONTAL) {
  3726. return Size2(sd->width, sd->ascent + sd->descent + sd->extra_spacing[SPACING_TOP] + sd->extra_spacing[SPACING_BOTTOM]).ceil();
  3727. } else {
  3728. return Size2(sd->ascent + sd->descent + sd->extra_spacing[SPACING_TOP] + sd->extra_spacing[SPACING_BOTTOM], sd->width).ceil();
  3729. }
  3730. }
  3731. double TextServerFallback::_shaped_text_get_ascent(const RID &p_shaped) const {
  3732. const ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  3733. ERR_FAIL_NULL_V(sd, 0.0);
  3734. MutexLock lock(sd->mutex);
  3735. if (!sd->valid) {
  3736. const_cast<TextServerFallback *>(this)->_shaped_text_shape(p_shaped);
  3737. }
  3738. return sd->ascent + sd->extra_spacing[SPACING_TOP];
  3739. }
  3740. double TextServerFallback::_shaped_text_get_descent(const RID &p_shaped) const {
  3741. const ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  3742. ERR_FAIL_NULL_V(sd, 0.0);
  3743. MutexLock lock(sd->mutex);
  3744. if (!sd->valid) {
  3745. const_cast<TextServerFallback *>(this)->_shaped_text_shape(p_shaped);
  3746. }
  3747. return sd->descent + sd->extra_spacing[SPACING_BOTTOM];
  3748. }
  3749. double TextServerFallback::_shaped_text_get_width(const RID &p_shaped) const {
  3750. const ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  3751. ERR_FAIL_NULL_V(sd, 0.0);
  3752. MutexLock lock(sd->mutex);
  3753. if (!sd->valid) {
  3754. const_cast<TextServerFallback *>(this)->_shaped_text_shape(p_shaped);
  3755. }
  3756. return Math::ceil(sd->width);
  3757. }
  3758. double TextServerFallback::_shaped_text_get_underline_position(const RID &p_shaped) const {
  3759. const ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  3760. ERR_FAIL_NULL_V(sd, 0.0);
  3761. MutexLock lock(sd->mutex);
  3762. if (!sd->valid) {
  3763. const_cast<TextServerFallback *>(this)->_shaped_text_shape(p_shaped);
  3764. }
  3765. return sd->upos;
  3766. }
  3767. double TextServerFallback::_shaped_text_get_underline_thickness(const RID &p_shaped) const {
  3768. const ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  3769. ERR_FAIL_NULL_V(sd, 0.0);
  3770. MutexLock lock(sd->mutex);
  3771. if (!sd->valid) {
  3772. const_cast<TextServerFallback *>(this)->_shaped_text_shape(p_shaped);
  3773. }
  3774. return sd->uthk;
  3775. }
  3776. PackedInt32Array TextServerFallback::_shaped_text_get_character_breaks(const RID &p_shaped) const {
  3777. const ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  3778. ERR_FAIL_NULL_V(sd, PackedInt32Array());
  3779. MutexLock lock(sd->mutex);
  3780. if (!sd->valid) {
  3781. const_cast<TextServerFallback *>(this)->_shaped_text_shape(p_shaped);
  3782. }
  3783. PackedInt32Array ret;
  3784. int size = sd->end - sd->start;
  3785. if (size > 0) {
  3786. ret.resize(size);
  3787. for (int i = 0; i < size; i++) {
  3788. #ifdef GDEXTENSION
  3789. ret[i] = i + 1 + sd->start;
  3790. #else
  3791. ret.write[i] = i + 1 + sd->start;
  3792. #endif
  3793. }
  3794. }
  3795. return ret;
  3796. }
  3797. String TextServerFallback::_string_to_upper(const String &p_string, const String &p_language) const {
  3798. return p_string.to_upper();
  3799. }
  3800. String TextServerFallback::_string_to_lower(const String &p_string, const String &p_language) const {
  3801. return p_string.to_lower();
  3802. }
  3803. String TextServerFallback::_string_to_title(const String &p_string, const String &p_language) const {
  3804. return p_string.capitalize();
  3805. }
  3806. PackedInt32Array TextServerFallback::_string_get_word_breaks(const String &p_string, const String &p_language, int64_t p_chars_per_line) const {
  3807. PackedInt32Array ret;
  3808. if (p_chars_per_line > 0) {
  3809. int line_start = 0;
  3810. int last_break = -1;
  3811. int line_length = 0;
  3812. for (int i = 0; i < p_string.length(); i++) {
  3813. const char32_t c = p_string[i];
  3814. bool is_lb = is_linebreak(c);
  3815. bool is_ws = is_whitespace(c);
  3816. bool is_p = (is_punct(c) && c != 0x005F) || is_underscore(c) || c == '\t' || c == 0xfffc;
  3817. if (is_lb) {
  3818. if (line_length > 0) {
  3819. ret.push_back(line_start);
  3820. ret.push_back(i);
  3821. }
  3822. line_start = i;
  3823. line_length = 0;
  3824. last_break = -1;
  3825. continue;
  3826. } else if (is_ws || is_p) {
  3827. last_break = i;
  3828. }
  3829. if (line_length == p_chars_per_line) {
  3830. if (last_break != -1) {
  3831. int last_break_w_spaces = last_break;
  3832. while (last_break > line_start && is_whitespace(p_string[last_break - 1])) {
  3833. last_break--;
  3834. }
  3835. if (line_start != last_break) {
  3836. ret.push_back(line_start);
  3837. ret.push_back(last_break);
  3838. }
  3839. while (last_break_w_spaces < p_string.length() && is_whitespace(p_string[last_break_w_spaces])) {
  3840. last_break_w_spaces++;
  3841. }
  3842. line_start = last_break_w_spaces;
  3843. if (last_break_w_spaces < i) {
  3844. line_length = i - last_break_w_spaces;
  3845. } else {
  3846. i = last_break_w_spaces;
  3847. line_length = 0;
  3848. }
  3849. } else {
  3850. ret.push_back(line_start);
  3851. ret.push_back(i);
  3852. line_start = i;
  3853. line_length = 0;
  3854. }
  3855. last_break = -1;
  3856. }
  3857. line_length++;
  3858. }
  3859. if (line_length > 0) {
  3860. ret.push_back(line_start);
  3861. ret.push_back(p_string.length());
  3862. }
  3863. } else {
  3864. int word_start = 0; // -1 if no word encountered. Leading spaces are part of a word.
  3865. int word_length = 0;
  3866. for (int i = 0; i < p_string.length(); i++) {
  3867. const char32_t c = p_string[i];
  3868. bool is_lb = is_linebreak(c);
  3869. bool is_ws = is_whitespace(c);
  3870. bool is_p = (is_punct(c) && c != 0x005F) || is_underscore(c) || c == '\t' || c == 0xfffc;
  3871. if (word_start == -1) {
  3872. if (!is_lb && !is_ws && !is_p) {
  3873. word_start = i;
  3874. }
  3875. continue;
  3876. }
  3877. if (is_lb) {
  3878. if (word_start != -1 && word_length > 0) {
  3879. ret.push_back(word_start);
  3880. ret.push_back(i);
  3881. }
  3882. word_start = -1;
  3883. word_length = 0;
  3884. } else if (is_ws || is_p) {
  3885. if (word_start != -1 && word_length > 0) {
  3886. ret.push_back(word_start);
  3887. ret.push_back(i);
  3888. }
  3889. word_start = -1;
  3890. word_length = 0;
  3891. }
  3892. word_length++;
  3893. }
  3894. if (word_start != -1 && word_length > 0) {
  3895. ret.push_back(word_start);
  3896. ret.push_back(p_string.length());
  3897. }
  3898. }
  3899. return ret;
  3900. }
  3901. TextServerFallback::TextServerFallback() {
  3902. _insert_feature_sets();
  3903. };
  3904. void TextServerFallback::_cleanup() {
  3905. for (const KeyValue<SystemFontKey, SystemFontCache> &E : system_fonts) {
  3906. const Vector<SystemFontCacheRec> &sysf_cache = E.value.var;
  3907. for (const SystemFontCacheRec &F : sysf_cache) {
  3908. _free_rid(F.rid);
  3909. }
  3910. }
  3911. system_fonts.clear();
  3912. system_font_data.clear();
  3913. }
  3914. TextServerFallback::~TextServerFallback() {
  3915. #ifdef MODULE_FREETYPE_ENABLED
  3916. if (ft_library != nullptr) {
  3917. FT_Done_FreeType(ft_library);
  3918. }
  3919. #endif
  3920. };