text_server_fb.cpp 139 KB

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