text_server_fb.cpp 159 KB

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