text_server_fb.cpp 147 KB

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