text_server_fb.cpp 160 KB

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