text_server_fb.cpp 154 KB

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