text_server_fb.cpp 139 KB

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