text_server_fb.cpp 140 KB

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