2
0

text_server_fb.cpp 128 KB

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