text_server_fb.cpp 174 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242
  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. #include <core/EdgeHolder.h>
  53. #include <core/ShapeDistanceFinder.h>
  54. #include <core/contour-combiners.h>
  55. #include <core/edge-selectors.h>
  56. #include <msdfgen.h>
  57. #endif
  58. #ifdef MODULE_FREETYPE_ENABLED
  59. #include FT_SFNT_NAMES_H
  60. #include FT_TRUETYPE_IDS_H
  61. #ifdef MODULE_SVG_ENABLED
  62. #include "thorvg_svg_in_ot.h"
  63. #endif
  64. #endif
  65. /*************************************************************************/
  66. bool TextServerFallback::_has_feature(Feature p_feature) const {
  67. switch (p_feature) {
  68. case FEATURE_SIMPLE_LAYOUT:
  69. case FEATURE_FONT_BITMAP:
  70. #ifdef MODULE_FREETYPE_ENABLED
  71. case FEATURE_FONT_DYNAMIC:
  72. #endif
  73. #ifdef MODULE_MSDFGEN_ENABLED
  74. case FEATURE_FONT_MSDF:
  75. #endif
  76. return true;
  77. default: {
  78. }
  79. }
  80. return false;
  81. }
  82. String TextServerFallback::_get_name() const {
  83. #ifdef GDEXTENSION
  84. return "Fallback (GDExtension)";
  85. #elif defined(GODOT_MODULE)
  86. return "Fallback (Built-in)";
  87. #endif
  88. }
  89. int64_t TextServerFallback::_get_features() const {
  90. int64_t interface_features = FEATURE_SIMPLE_LAYOUT | FEATURE_FONT_BITMAP;
  91. #ifdef MODULE_FREETYPE_ENABLED
  92. interface_features |= FEATURE_FONT_DYNAMIC;
  93. #endif
  94. #ifdef MODULE_MSDFGEN_ENABLED
  95. interface_features |= FEATURE_FONT_MSDF;
  96. #endif
  97. return interface_features;
  98. }
  99. void TextServerFallback::_free_rid(const RID &p_rid) {
  100. _THREAD_SAFE_METHOD_
  101. if (font_owner.owns(p_rid)) {
  102. MutexLock ftlock(ft_mutex);
  103. FontFallback *fd = font_owner.get_or_null(p_rid);
  104. for (const KeyValue<Vector2i, FontForSizeFallback *> &ffsd : fd->cache) {
  105. OversamplingLevel *ol = oversampling_levels.getptr(ffsd.value->viewport_oversampling);
  106. if (ol != nullptr) {
  107. ol->fonts.erase(ffsd.value);
  108. }
  109. }
  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 * 0.125, 256);
  231. texsize = next_power_of_2((uint32_t)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((uint32_t)mw);
  239. }
  240. if (mh > texsize) { // Special case, adapt to it?
  241. texsize = next_power_of_2((uint32_t)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;
  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;
  523. chr.rect.size = chr.uv_rect.size * p_data->scale;
  524. return chr;
  525. }
  526. #endif
  527. /*************************************************************************/
  528. /* Font Cache */
  529. /*************************************************************************/
  530. bool TextServerFallback::_ensure_glyph(FontFallback *p_font_data, const Vector2i &p_size, int32_t p_glyph, FontGlyph &r_glyph, uint32_t p_oversampling) const {
  531. FontForSizeFallback *fd = nullptr;
  532. ERR_FAIL_COND_V(!_ensure_cache_for_size(p_font_data, p_size, fd, false, p_oversampling), 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 * 64)) {
  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 * 64)) {
  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 / 16; // 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. FT_GlyphSlot slot = fd->face->glyph;
  630. bool from_svg = (slot->format == FT_GLYPH_FORMAT_SVG); // Need to check before FT_Render_Glyph as it will change format to bitmap.
  631. if (!outline) {
  632. if (!p_font_data->msdf) {
  633. error = FT_Render_Glyph(slot, aa_mode);
  634. }
  635. if (!error) {
  636. if (p_font_data->msdf) {
  637. #ifdef MODULE_MSDFGEN_ENABLED
  638. 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);
  639. #else
  640. fd->glyph_map[p_glyph] = FontGlyph();
  641. ERR_FAIL_V_MSG(false, "Compiled without MSDFGEN support!");
  642. #endif
  643. } else {
  644. 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);
  645. }
  646. }
  647. } else {
  648. FT_Stroker stroker;
  649. if (FT_Stroker_New(ft_library, &stroker) != 0) {
  650. fd->glyph_map[p_glyph] = FontGlyph();
  651. ERR_FAIL_V_MSG(false, "FreeType: Failed to load glyph stroker.");
  652. }
  653. FT_Stroker_Set(stroker, (int)(fd->size.y * 16.0), FT_STROKER_LINECAP_BUTT, FT_STROKER_LINEJOIN_ROUND, 0);
  654. FT_Glyph glyph;
  655. FT_BitmapGlyph glyph_bitmap;
  656. if (FT_Get_Glyph(fd->face->glyph, &glyph) != 0) {
  657. goto cleanup_stroker;
  658. }
  659. if (FT_Glyph_Stroke(&glyph, stroker, 1) != 0) {
  660. goto cleanup_glyph;
  661. }
  662. if (FT_Glyph_To_Bitmap(&glyph, aa_mode, nullptr, 1) != 0) {
  663. goto cleanup_glyph;
  664. }
  665. glyph_bitmap = (FT_BitmapGlyph)glyph;
  666. gl = rasterize_bitmap(fd, rect_range, glyph_bitmap->bitmap, glyph_bitmap->top, glyph_bitmap->left, Vector2(), bgra);
  667. cleanup_glyph:
  668. FT_Done_Glyph(glyph);
  669. cleanup_stroker:
  670. FT_Stroker_Done(stroker);
  671. }
  672. gl.from_svg = from_svg;
  673. E = fd->glyph_map.insert(p_glyph, gl);
  674. r_glyph = E->value;
  675. return gl.found;
  676. }
  677. #endif
  678. E = fd->glyph_map.insert(p_glyph, FontGlyph());
  679. r_glyph = E->value;
  680. return false;
  681. }
  682. bool TextServerFallback::_ensure_cache_for_size(FontFallback *p_font_data, const Vector2i &p_size, FontForSizeFallback *&r_cache_for_size, bool p_silent, uint32_t p_oversampling) const {
  683. ERR_FAIL_COND_V(p_size.x <= 0, false);
  684. HashMap<Vector2i, FontForSizeFallback *>::Iterator E = p_font_data->cache.find(p_size);
  685. if (E) {
  686. r_cache_for_size = E->value;
  687. // Size used directly, remove from oversampling list.
  688. if (p_oversampling == 0 && E->value->viewport_oversampling != 0) {
  689. OversamplingLevel *ol = oversampling_levels.getptr(E->value->viewport_oversampling);
  690. if (ol) {
  691. ol->fonts.erase(E->value);
  692. }
  693. }
  694. return true;
  695. }
  696. r_cache_for_size = nullptr;
  697. FontForSizeFallback *fd = memnew(FontForSizeFallback);
  698. fd->size = p_size;
  699. if (p_font_data->data_ptr && (p_font_data->data_size > 0)) {
  700. // Init dynamic font.
  701. #ifdef MODULE_FREETYPE_ENABLED
  702. int error = 0;
  703. {
  704. MutexLock ftlock(ft_mutex);
  705. if (!ft_library) {
  706. error = FT_Init_FreeType(&ft_library);
  707. if (error != 0) {
  708. memdelete(fd);
  709. if (p_silent) {
  710. return false;
  711. } else {
  712. ERR_FAIL_V_MSG(false, "FreeType: Error initializing library: '" + String(FT_Error_String(error)) + "'.");
  713. }
  714. }
  715. #ifdef MODULE_SVG_ENABLED
  716. FT_Property_Set(ft_library, "ot-svg", "svg-hooks", get_tvg_svg_in_ot_hooks());
  717. #endif
  718. }
  719. memset(&fd->stream, 0, sizeof(FT_StreamRec));
  720. fd->stream.base = (unsigned char *)p_font_data->data_ptr;
  721. fd->stream.size = p_font_data->data_size;
  722. fd->stream.pos = 0;
  723. FT_Open_Args fargs;
  724. memset(&fargs, 0, sizeof(FT_Open_Args));
  725. fargs.memory_base = (unsigned char *)p_font_data->data_ptr;
  726. fargs.memory_size = p_font_data->data_size;
  727. fargs.flags = FT_OPEN_MEMORY;
  728. fargs.stream = &fd->stream;
  729. int max_index = 0;
  730. FT_Face tmp_face = nullptr;
  731. error = FT_Open_Face(ft_library, &fargs, -1, &tmp_face);
  732. if (tmp_face && error == 0) {
  733. max_index = tmp_face->num_faces - 1;
  734. }
  735. if (tmp_face) {
  736. FT_Done_Face(tmp_face);
  737. }
  738. error = FT_Open_Face(ft_library, &fargs, CLAMP(p_font_data->face_index, 0, max_index), &fd->face);
  739. if (error) {
  740. FT_Done_Face(fd->face);
  741. fd->face = nullptr;
  742. memdelete(fd);
  743. if (p_silent) {
  744. return false;
  745. } else {
  746. ERR_FAIL_V_MSG(false, "FreeType: Error loading font: '" + String(FT_Error_String(error)) + "'.");
  747. }
  748. }
  749. }
  750. double sz = double(fd->size.x) / 64.0;
  751. if (p_font_data->msdf) {
  752. sz = p_font_data->msdf_source_size;
  753. }
  754. if (FT_HAS_COLOR(fd->face) && fd->face->num_fixed_sizes > 0) {
  755. int best_match = 0;
  756. int diff = Math::abs(sz - ((int64_t)fd->face->available_sizes[0].width));
  757. fd->scale = sz / fd->face->available_sizes[0].width;
  758. for (int i = 1; i < fd->face->num_fixed_sizes; i++) {
  759. int ndiff = Math::abs(sz - ((int64_t)fd->face->available_sizes[i].width));
  760. if (ndiff < diff) {
  761. best_match = i;
  762. diff = ndiff;
  763. fd->scale = sz / fd->face->available_sizes[i].width;
  764. }
  765. }
  766. FT_Select_Size(fd->face, best_match);
  767. } else {
  768. FT_Size_RequestRec req;
  769. req.type = FT_SIZE_REQUEST_TYPE_NOMINAL;
  770. req.width = sz * 64.0;
  771. req.height = sz * 64.0;
  772. req.horiResolution = 0;
  773. req.vertResolution = 0;
  774. FT_Request_Size(fd->face, &req);
  775. if (fd->face->size->metrics.y_ppem != 0) {
  776. fd->scale = sz / (double)fd->face->size->metrics.y_ppem;
  777. }
  778. }
  779. fd->ascent = (fd->face->size->metrics.ascender / 64.0) * fd->scale;
  780. fd->descent = (-fd->face->size->metrics.descender / 64.0) * fd->scale;
  781. fd->underline_position = (-FT_MulFix(fd->face->underline_position, fd->face->size->metrics.y_scale) / 64.0) * fd->scale;
  782. fd->underline_thickness = (FT_MulFix(fd->face->underline_thickness, fd->face->size->metrics.y_scale) / 64.0) * fd->scale;
  783. if (!p_font_data->face_init) {
  784. // When a font does not provide a `family_name`, FreeType tries to synthesize one based on other names.
  785. // FreeType automatically converts non-ASCII characters to "?" in the synthesized name.
  786. // To avoid that behavior, use the format-specific name directly if available.
  787. if (FT_IS_SFNT(fd->face)) {
  788. int name_count = FT_Get_Sfnt_Name_Count(fd->face);
  789. for (int i = 0; i < name_count; i++) {
  790. FT_SfntName sfnt_name;
  791. if (FT_Get_Sfnt_Name(fd->face, i, &sfnt_name) != 0) {
  792. continue;
  793. }
  794. if (sfnt_name.name_id != TT_NAME_ID_FONT_FAMILY && sfnt_name.name_id != TT_NAME_ID_TYPOGRAPHIC_FAMILY) {
  795. continue;
  796. }
  797. if (!p_font_data->font_name.is_empty() && sfnt_name.language_id != TT_MS_LANGID_ENGLISH_UNITED_STATES) {
  798. continue;
  799. }
  800. switch (sfnt_name.platform_id) {
  801. case TT_PLATFORM_APPLE_UNICODE: {
  802. p_font_data->font_name.clear();
  803. p_font_data->font_name.append_utf16((const char16_t *)sfnt_name.string, sfnt_name.string_len / 2, false);
  804. } break;
  805. case TT_PLATFORM_MICROSOFT: {
  806. if (sfnt_name.encoding_id == TT_MS_ID_UNICODE_CS || sfnt_name.encoding_id == TT_MS_ID_UCS_4) {
  807. p_font_data->font_name.clear();
  808. p_font_data->font_name.append_utf16((const char16_t *)sfnt_name.string, sfnt_name.string_len / 2, false);
  809. }
  810. } break;
  811. }
  812. }
  813. }
  814. if (p_font_data->font_name.is_empty() && fd->face->family_name != nullptr) {
  815. p_font_data->font_name = String::utf8((const char *)fd->face->family_name);
  816. }
  817. if (fd->face->style_name != nullptr) {
  818. p_font_data->style_name = String::utf8((const char *)fd->face->style_name);
  819. }
  820. p_font_data->weight = _font_get_weight_by_name(p_font_data->style_name.to_lower());
  821. p_font_data->stretch = _font_get_stretch_by_name(p_font_data->style_name.to_lower());
  822. p_font_data->style_flags = 0;
  823. if ((fd->face->style_flags & FT_STYLE_FLAG_BOLD) || p_font_data->weight >= 700) {
  824. p_font_data->style_flags.set_flag(FONT_BOLD);
  825. }
  826. if ((fd->face->style_flags & FT_STYLE_FLAG_ITALIC) || _is_ital_style(p_font_data->style_name.to_lower())) {
  827. p_font_data->style_flags.set_flag(FONT_ITALIC);
  828. }
  829. if (fd->face->face_flags & FT_FACE_FLAG_FIXED_WIDTH) {
  830. p_font_data->style_flags.set_flag(FONT_FIXED_WIDTH);
  831. }
  832. // Read OpenType variations.
  833. p_font_data->supported_varaitions.clear();
  834. if (fd->face->face_flags & FT_FACE_FLAG_MULTIPLE_MASTERS) {
  835. FT_MM_Var *amaster;
  836. FT_Get_MM_Var(fd->face, &amaster);
  837. for (FT_UInt i = 0; i < amaster->num_axis; i++) {
  838. 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);
  839. }
  840. FT_Done_MM_Var(ft_library, amaster);
  841. }
  842. p_font_data->face_init = true;
  843. }
  844. #if defined(MACOS_ENABLED) || defined(IOS_ENABLED)
  845. if (p_font_data->font_name == ".Apple Color Emoji UI" || p_font_data->font_name == "Apple Color Emoji") {
  846. // The baseline offset is missing from the Apple Color Emoji UI font data, so add it manually.
  847. // This issue doesn't occur with other system emoji fonts.
  848. if (!FT_Load_Glyph(fd->face, FT_Get_Char_Index(fd->face, 0x1F92E), FT_LOAD_DEFAULT | FT_LOAD_COLOR)) {
  849. if (fd->face->glyph->metrics.horiBearingY == fd->face->glyph->metrics.height) {
  850. p_font_data->baseline_offset = 0.15;
  851. }
  852. }
  853. }
  854. #endif
  855. // Write variations.
  856. if (fd->face->face_flags & FT_FACE_FLAG_MULTIPLE_MASTERS) {
  857. FT_MM_Var *amaster;
  858. FT_Get_MM_Var(fd->face, &amaster);
  859. Vector<FT_Fixed> coords;
  860. coords.resize(amaster->num_axis);
  861. FT_Get_Var_Design_Coordinates(fd->face, coords.size(), coords.ptrw());
  862. for (FT_UInt i = 0; i < amaster->num_axis; i++) {
  863. // Reset to default.
  864. int32_t var_tag = amaster->axis[i].tag;
  865. double var_value = (double)amaster->axis[i].def / 65536.0;
  866. coords.write[i] = amaster->axis[i].def;
  867. if (p_font_data->variation_coordinates.has(var_tag)) {
  868. var_value = p_font_data->variation_coordinates[var_tag];
  869. coords.write[i] = CLAMP(var_value * 65536.0, amaster->axis[i].minimum, amaster->axis[i].maximum);
  870. }
  871. if (p_font_data->variation_coordinates.has(tag_to_name(var_tag))) {
  872. var_value = p_font_data->variation_coordinates[tag_to_name(var_tag)];
  873. coords.write[i] = CLAMP(var_value * 65536.0, amaster->axis[i].minimum, amaster->axis[i].maximum);
  874. }
  875. }
  876. FT_Set_Var_Design_Coordinates(fd->face, coords.size(), coords.ptrw());
  877. FT_Done_MM_Var(ft_library, amaster);
  878. }
  879. #else
  880. memdelete(fd);
  881. if (p_silent) {
  882. return false;
  883. } else {
  884. ERR_FAIL_V_MSG(false, "FreeType: Can't load dynamic font, engine is compiled without FreeType support!");
  885. }
  886. #endif
  887. }
  888. fd->owner = p_font_data;
  889. p_font_data->cache.insert(p_size, fd);
  890. r_cache_for_size = fd;
  891. if (p_oversampling != 0) {
  892. OversamplingLevel *ol = oversampling_levels.getptr(p_oversampling);
  893. if (ol) {
  894. fd->viewport_oversampling = p_oversampling;
  895. ol->fonts.insert(fd);
  896. }
  897. }
  898. return true;
  899. }
  900. void TextServerFallback::_reference_oversampling_level(double p_oversampling) {
  901. uint32_t oversampling = CLAMP(p_oversampling, 0.1, 100.0) * 64;
  902. if (oversampling == 64) {
  903. return;
  904. }
  905. OversamplingLevel *ol = oversampling_levels.getptr(oversampling);
  906. if (ol) {
  907. ol->refcount++;
  908. } else {
  909. OversamplingLevel new_ol;
  910. oversampling_levels.insert(oversampling, new_ol);
  911. }
  912. }
  913. void TextServerFallback::_unreference_oversampling_level(double p_oversampling) {
  914. uint32_t oversampling = CLAMP(p_oversampling, 0.1, 100.0) * 64;
  915. if (oversampling == 64) {
  916. return;
  917. }
  918. OversamplingLevel *ol = oversampling_levels.getptr(oversampling);
  919. if (ol) {
  920. ol->refcount--;
  921. if (ol->refcount == 0) {
  922. for (FontForSizeFallback *fd : ol->fonts) {
  923. fd->owner->cache.erase(fd->size);
  924. memdelete(fd);
  925. }
  926. ol->fonts.clear();
  927. oversampling_levels.erase(oversampling);
  928. }
  929. }
  930. }
  931. _FORCE_INLINE_ bool TextServerFallback::_font_validate(const RID &p_font_rid) const {
  932. FontFallback *fd = _get_font_data(p_font_rid);
  933. ERR_FAIL_NULL_V(fd, false);
  934. MutexLock lock(fd->mutex);
  935. Vector2i size = _get_size(fd, 16);
  936. FontForSizeFallback *ffsd = nullptr;
  937. return _ensure_cache_for_size(fd, size, ffsd, true);
  938. }
  939. _FORCE_INLINE_ void TextServerFallback::_font_clear_cache(FontFallback *p_font_data) {
  940. MutexLock ftlock(ft_mutex);
  941. for (const KeyValue<Vector2i, FontForSizeFallback *> &E : p_font_data->cache) {
  942. if (E.value->viewport_oversampling != 0) {
  943. OversamplingLevel *ol = oversampling_levels.getptr(E.value->viewport_oversampling);
  944. if (ol) {
  945. ol->fonts.erase(E.value);
  946. }
  947. }
  948. memdelete(E.value);
  949. }
  950. p_font_data->cache.clear();
  951. p_font_data->face_init = false;
  952. p_font_data->supported_varaitions.clear();
  953. }
  954. RID TextServerFallback::_create_font() {
  955. _THREAD_SAFE_METHOD_
  956. FontFallback *fd = memnew(FontFallback);
  957. return font_owner.make_rid(fd);
  958. }
  959. RID TextServerFallback::_create_font_linked_variation(const RID &p_font_rid) {
  960. _THREAD_SAFE_METHOD_
  961. RID rid = p_font_rid;
  962. FontFallbackLinkedVariation *fdv = font_var_owner.get_or_null(rid);
  963. if (unlikely(fdv)) {
  964. rid = fdv->base_font;
  965. }
  966. ERR_FAIL_COND_V(!font_owner.owns(rid), RID());
  967. FontFallbackLinkedVariation *new_fdv = memnew(FontFallbackLinkedVariation);
  968. new_fdv->base_font = rid;
  969. return font_var_owner.make_rid(new_fdv);
  970. }
  971. void TextServerFallback::_font_set_data(const RID &p_font_rid, const PackedByteArray &p_data) {
  972. FontFallback *fd = _get_font_data(p_font_rid);
  973. ERR_FAIL_NULL(fd);
  974. MutexLock lock(fd->mutex);
  975. _font_clear_cache(fd);
  976. fd->data = p_data;
  977. fd->data_ptr = fd->data.ptr();
  978. fd->data_size = fd->data.size();
  979. }
  980. void TextServerFallback::_font_set_data_ptr(const RID &p_font_rid, const uint8_t *p_data_ptr, int64_t p_data_size) {
  981. FontFallback *fd = _get_font_data(p_font_rid);
  982. ERR_FAIL_NULL(fd);
  983. MutexLock lock(fd->mutex);
  984. _font_clear_cache(fd);
  985. fd->data.resize(0);
  986. fd->data_ptr = p_data_ptr;
  987. fd->data_size = p_data_size;
  988. }
  989. void TextServerFallback::_font_set_style(const RID &p_font_rid, BitField<FontStyle> p_style) {
  990. FontFallback *fd = _get_font_data(p_font_rid);
  991. ERR_FAIL_NULL(fd);
  992. MutexLock lock(fd->mutex);
  993. Vector2i size = _get_size(fd, 16);
  994. FontForSizeFallback *ffsd = nullptr;
  995. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size, ffsd));
  996. fd->style_flags = p_style;
  997. }
  998. void TextServerFallback::_font_set_face_index(const RID &p_font_rid, int64_t p_face_index) {
  999. ERR_FAIL_COND(p_face_index < 0);
  1000. ERR_FAIL_COND(p_face_index >= 0x7FFF);
  1001. FontFallback *fd = _get_font_data(p_font_rid);
  1002. ERR_FAIL_NULL(fd);
  1003. MutexLock lock(fd->mutex);
  1004. if (fd->face_index != p_face_index) {
  1005. fd->face_index = p_face_index;
  1006. _font_clear_cache(fd);
  1007. }
  1008. }
  1009. int64_t TextServerFallback::_font_get_face_index(const RID &p_font_rid) const {
  1010. FontFallback *fd = _get_font_data(p_font_rid);
  1011. ERR_FAIL_NULL_V(fd, 0);
  1012. MutexLock lock(fd->mutex);
  1013. return fd->face_index;
  1014. }
  1015. int64_t TextServerFallback::_font_get_face_count(const RID &p_font_rid) const {
  1016. FontFallback *fd = _get_font_data(p_font_rid);
  1017. ERR_FAIL_NULL_V(fd, 0);
  1018. MutexLock lock(fd->mutex);
  1019. int face_count = 0;
  1020. if (fd->data_ptr && (fd->data_size > 0)) {
  1021. // Init dynamic font.
  1022. #ifdef MODULE_FREETYPE_ENABLED
  1023. int error = 0;
  1024. if (!ft_library) {
  1025. error = FT_Init_FreeType(&ft_library);
  1026. ERR_FAIL_COND_V_MSG(error != 0, false, "FreeType: Error initializing library: '" + String(FT_Error_String(error)) + "'.");
  1027. #ifdef MODULE_SVG_ENABLED
  1028. FT_Property_Set(ft_library, "ot-svg", "svg-hooks", get_tvg_svg_in_ot_hooks());
  1029. #endif
  1030. }
  1031. FT_StreamRec stream;
  1032. memset(&stream, 0, sizeof(FT_StreamRec));
  1033. stream.base = (unsigned char *)fd->data_ptr;
  1034. stream.size = fd->data_size;
  1035. stream.pos = 0;
  1036. FT_Open_Args fargs;
  1037. memset(&fargs, 0, sizeof(FT_Open_Args));
  1038. fargs.memory_base = (unsigned char *)fd->data_ptr;
  1039. fargs.memory_size = fd->data_size;
  1040. fargs.flags = FT_OPEN_MEMORY;
  1041. fargs.stream = &stream;
  1042. MutexLock ftlock(ft_mutex);
  1043. FT_Face tmp_face = nullptr;
  1044. error = FT_Open_Face(ft_library, &fargs, -1, &tmp_face);
  1045. if (error == 0) {
  1046. face_count = tmp_face->num_faces;
  1047. FT_Done_Face(tmp_face);
  1048. }
  1049. #endif
  1050. }
  1051. return face_count;
  1052. }
  1053. BitField<TextServer::FontStyle> TextServerFallback::_font_get_style(const RID &p_font_rid) const {
  1054. FontFallback *fd = _get_font_data(p_font_rid);
  1055. ERR_FAIL_NULL_V(fd, 0);
  1056. MutexLock lock(fd->mutex);
  1057. Vector2i size = _get_size(fd, 16);
  1058. FontForSizeFallback *ffsd = nullptr;
  1059. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size, ffsd), 0);
  1060. return fd->style_flags;
  1061. }
  1062. void TextServerFallback::_font_set_style_name(const RID &p_font_rid, const String &p_name) {
  1063. FontFallback *fd = _get_font_data(p_font_rid);
  1064. ERR_FAIL_NULL(fd);
  1065. MutexLock lock(fd->mutex);
  1066. Vector2i size = _get_size(fd, 16);
  1067. FontForSizeFallback *ffsd = nullptr;
  1068. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size, ffsd));
  1069. fd->style_name = p_name;
  1070. }
  1071. String TextServerFallback::_font_get_style_name(const RID &p_font_rid) const {
  1072. FontFallback *fd = _get_font_data(p_font_rid);
  1073. ERR_FAIL_NULL_V(fd, String());
  1074. MutexLock lock(fd->mutex);
  1075. Vector2i size = _get_size(fd, 16);
  1076. FontForSizeFallback *ffsd = nullptr;
  1077. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size, ffsd), String());
  1078. return fd->style_name;
  1079. }
  1080. void TextServerFallback::_font_set_weight(const RID &p_font_rid, int64_t p_weight) {
  1081. FontFallback *fd = _get_font_data(p_font_rid);
  1082. ERR_FAIL_NULL(fd);
  1083. MutexLock lock(fd->mutex);
  1084. Vector2i size = _get_size(fd, 16);
  1085. FontForSizeFallback *ffsd = nullptr;
  1086. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size, ffsd));
  1087. fd->weight = CLAMP(p_weight, 100, 999);
  1088. }
  1089. int64_t TextServerFallback::_font_get_weight(const RID &p_font_rid) const {
  1090. FontFallback *fd = _get_font_data(p_font_rid);
  1091. ERR_FAIL_NULL_V(fd, 400);
  1092. MutexLock lock(fd->mutex);
  1093. Vector2i size = _get_size(fd, 16);
  1094. FontForSizeFallback *ffsd = nullptr;
  1095. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size, ffsd), 400);
  1096. return fd->weight;
  1097. }
  1098. void TextServerFallback::_font_set_stretch(const RID &p_font_rid, int64_t p_stretch) {
  1099. FontFallback *fd = _get_font_data(p_font_rid);
  1100. ERR_FAIL_NULL(fd);
  1101. MutexLock lock(fd->mutex);
  1102. Vector2i size = _get_size(fd, 16);
  1103. FontForSizeFallback *ffsd = nullptr;
  1104. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size, ffsd));
  1105. fd->stretch = CLAMP(p_stretch, 50, 200);
  1106. }
  1107. int64_t TextServerFallback::_font_get_stretch(const RID &p_font_rid) const {
  1108. FontFallback *fd = _get_font_data(p_font_rid);
  1109. ERR_FAIL_NULL_V(fd, 100);
  1110. MutexLock lock(fd->mutex);
  1111. Vector2i size = _get_size(fd, 16);
  1112. FontForSizeFallback *ffsd = nullptr;
  1113. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size, ffsd), 100);
  1114. return fd->stretch;
  1115. }
  1116. void TextServerFallback::_font_set_name(const RID &p_font_rid, const String &p_name) {
  1117. FontFallback *fd = _get_font_data(p_font_rid);
  1118. ERR_FAIL_NULL(fd);
  1119. MutexLock lock(fd->mutex);
  1120. Vector2i size = _get_size(fd, 16);
  1121. FontForSizeFallback *ffsd = nullptr;
  1122. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size, ffsd));
  1123. fd->font_name = p_name;
  1124. }
  1125. String TextServerFallback::_font_get_name(const RID &p_font_rid) const {
  1126. FontFallback *fd = _get_font_data(p_font_rid);
  1127. ERR_FAIL_NULL_V(fd, String());
  1128. MutexLock lock(fd->mutex);
  1129. Vector2i size = _get_size(fd, 16);
  1130. FontForSizeFallback *ffsd = nullptr;
  1131. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size, ffsd), String());
  1132. return fd->font_name;
  1133. }
  1134. void TextServerFallback::_font_set_antialiasing(const RID &p_font_rid, TextServer::FontAntialiasing p_antialiasing) {
  1135. FontFallback *fd = _get_font_data(p_font_rid);
  1136. ERR_FAIL_NULL(fd);
  1137. MutexLock lock(fd->mutex);
  1138. if (fd->antialiasing != p_antialiasing) {
  1139. _font_clear_cache(fd);
  1140. fd->antialiasing = p_antialiasing;
  1141. }
  1142. }
  1143. TextServer::FontAntialiasing TextServerFallback::_font_get_antialiasing(const RID &p_font_rid) const {
  1144. FontFallback *fd = _get_font_data(p_font_rid);
  1145. ERR_FAIL_NULL_V(fd, TextServer::FONT_ANTIALIASING_NONE);
  1146. MutexLock lock(fd->mutex);
  1147. return fd->antialiasing;
  1148. }
  1149. void TextServerFallback::_font_set_disable_embedded_bitmaps(const RID &p_font_rid, bool p_disable_embedded_bitmaps) {
  1150. FontFallback *fd = _get_font_data(p_font_rid);
  1151. ERR_FAIL_NULL(fd);
  1152. MutexLock lock(fd->mutex);
  1153. if (fd->disable_embedded_bitmaps != p_disable_embedded_bitmaps) {
  1154. _font_clear_cache(fd);
  1155. fd->disable_embedded_bitmaps = p_disable_embedded_bitmaps;
  1156. }
  1157. }
  1158. bool TextServerFallback::_font_get_disable_embedded_bitmaps(const RID &p_font_rid) const {
  1159. FontFallback *fd = _get_font_data(p_font_rid);
  1160. ERR_FAIL_NULL_V(fd, false);
  1161. MutexLock lock(fd->mutex);
  1162. return fd->disable_embedded_bitmaps;
  1163. }
  1164. void TextServerFallback::_font_set_generate_mipmaps(const RID &p_font_rid, bool p_generate_mipmaps) {
  1165. FontFallback *fd = _get_font_data(p_font_rid);
  1166. ERR_FAIL_NULL(fd);
  1167. MutexLock lock(fd->mutex);
  1168. if (fd->mipmaps != p_generate_mipmaps) {
  1169. for (KeyValue<Vector2i, FontForSizeFallback *> &E : fd->cache) {
  1170. for (int i = 0; i < E.value->textures.size(); i++) {
  1171. E.value->textures.write[i].dirty = true;
  1172. E.value->textures.write[i].texture = Ref<ImageTexture>();
  1173. }
  1174. }
  1175. fd->mipmaps = p_generate_mipmaps;
  1176. }
  1177. }
  1178. bool TextServerFallback::_font_get_generate_mipmaps(const RID &p_font_rid) const {
  1179. FontFallback *fd = _get_font_data(p_font_rid);
  1180. ERR_FAIL_NULL_V(fd, false);
  1181. MutexLock lock(fd->mutex);
  1182. return fd->mipmaps;
  1183. }
  1184. void TextServerFallback::_font_set_multichannel_signed_distance_field(const RID &p_font_rid, bool p_msdf) {
  1185. FontFallback *fd = _get_font_data(p_font_rid);
  1186. ERR_FAIL_NULL(fd);
  1187. MutexLock lock(fd->mutex);
  1188. if (fd->msdf != p_msdf) {
  1189. _font_clear_cache(fd);
  1190. fd->msdf = p_msdf;
  1191. }
  1192. }
  1193. bool TextServerFallback::_font_is_multichannel_signed_distance_field(const RID &p_font_rid) const {
  1194. FontFallback *fd = _get_font_data(p_font_rid);
  1195. ERR_FAIL_NULL_V(fd, false);
  1196. MutexLock lock(fd->mutex);
  1197. return fd->msdf;
  1198. }
  1199. void TextServerFallback::_font_set_msdf_pixel_range(const RID &p_font_rid, int64_t p_msdf_pixel_range) {
  1200. FontFallback *fd = _get_font_data(p_font_rid);
  1201. ERR_FAIL_NULL(fd);
  1202. MutexLock lock(fd->mutex);
  1203. if (fd->msdf_range != p_msdf_pixel_range) {
  1204. _font_clear_cache(fd);
  1205. fd->msdf_range = p_msdf_pixel_range;
  1206. }
  1207. }
  1208. int64_t TextServerFallback::_font_get_msdf_pixel_range(const RID &p_font_rid) const {
  1209. FontFallback *fd = _get_font_data(p_font_rid);
  1210. ERR_FAIL_NULL_V(fd, false);
  1211. MutexLock lock(fd->mutex);
  1212. return fd->msdf_range;
  1213. }
  1214. void TextServerFallback::_font_set_msdf_size(const RID &p_font_rid, int64_t p_msdf_size) {
  1215. FontFallback *fd = _get_font_data(p_font_rid);
  1216. ERR_FAIL_NULL(fd);
  1217. MutexLock lock(fd->mutex);
  1218. if (fd->msdf_source_size != p_msdf_size) {
  1219. _font_clear_cache(fd);
  1220. fd->msdf_source_size = p_msdf_size;
  1221. }
  1222. }
  1223. int64_t TextServerFallback::_font_get_msdf_size(const RID &p_font_rid) const {
  1224. FontFallback *fd = _get_font_data(p_font_rid);
  1225. ERR_FAIL_NULL_V(fd, 0);
  1226. MutexLock lock(fd->mutex);
  1227. return fd->msdf_source_size;
  1228. }
  1229. void TextServerFallback::_font_set_fixed_size(const RID &p_font_rid, int64_t p_fixed_size) {
  1230. FontFallback *fd = _get_font_data(p_font_rid);
  1231. ERR_FAIL_NULL(fd);
  1232. MutexLock lock(fd->mutex);
  1233. fd->fixed_size = p_fixed_size;
  1234. }
  1235. int64_t TextServerFallback::_font_get_fixed_size(const RID &p_font_rid) const {
  1236. FontFallback *fd = _get_font_data(p_font_rid);
  1237. ERR_FAIL_NULL_V(fd, 0);
  1238. MutexLock lock(fd->mutex);
  1239. return fd->fixed_size;
  1240. }
  1241. void TextServerFallback::_font_set_fixed_size_scale_mode(const RID &p_font_rid, TextServer::FixedSizeScaleMode p_fixed_size_scale_mode) {
  1242. FontFallback *fd = _get_font_data(p_font_rid);
  1243. ERR_FAIL_NULL(fd);
  1244. MutexLock lock(fd->mutex);
  1245. fd->fixed_size_scale_mode = p_fixed_size_scale_mode;
  1246. }
  1247. TextServer::FixedSizeScaleMode TextServerFallback::_font_get_fixed_size_scale_mode(const RID &p_font_rid) const {
  1248. FontFallback *fd = _get_font_data(p_font_rid);
  1249. ERR_FAIL_NULL_V(fd, FIXED_SIZE_SCALE_DISABLE);
  1250. MutexLock lock(fd->mutex);
  1251. return fd->fixed_size_scale_mode;
  1252. }
  1253. void TextServerFallback::_font_set_allow_system_fallback(const RID &p_font_rid, bool p_allow_system_fallback) {
  1254. FontFallback *fd = _get_font_data(p_font_rid);
  1255. ERR_FAIL_NULL(fd);
  1256. MutexLock lock(fd->mutex);
  1257. fd->allow_system_fallback = p_allow_system_fallback;
  1258. }
  1259. bool TextServerFallback::_font_is_allow_system_fallback(const RID &p_font_rid) const {
  1260. FontFallback *fd = _get_font_data(p_font_rid);
  1261. ERR_FAIL_NULL_V(fd, false);
  1262. MutexLock lock(fd->mutex);
  1263. return fd->allow_system_fallback;
  1264. }
  1265. void TextServerFallback::_font_set_force_autohinter(const RID &p_font_rid, bool p_force_autohinter) {
  1266. FontFallback *fd = _get_font_data(p_font_rid);
  1267. ERR_FAIL_NULL(fd);
  1268. MutexLock lock(fd->mutex);
  1269. if (fd->force_autohinter != p_force_autohinter) {
  1270. _font_clear_cache(fd);
  1271. fd->force_autohinter = p_force_autohinter;
  1272. }
  1273. }
  1274. bool TextServerFallback::_font_is_force_autohinter(const RID &p_font_rid) const {
  1275. FontFallback *fd = _get_font_data(p_font_rid);
  1276. ERR_FAIL_NULL_V(fd, false);
  1277. MutexLock lock(fd->mutex);
  1278. return fd->force_autohinter;
  1279. }
  1280. void TextServerFallback::_font_set_modulate_color_glyphs(const RID &p_font_rid, bool p_modulate) {
  1281. FontFallback *fd = _get_font_data(p_font_rid);
  1282. ERR_FAIL_NULL(fd);
  1283. MutexLock lock(fd->mutex);
  1284. if (fd->modulate_color_glyphs != p_modulate) {
  1285. fd->modulate_color_glyphs = p_modulate;
  1286. }
  1287. }
  1288. bool TextServerFallback::_font_is_modulate_color_glyphs(const RID &p_font_rid) const {
  1289. FontFallback *fd = _get_font_data(p_font_rid);
  1290. ERR_FAIL_NULL_V(fd, false);
  1291. MutexLock lock(fd->mutex);
  1292. return fd->modulate_color_glyphs;
  1293. }
  1294. void TextServerFallback::_font_set_hinting(const RID &p_font_rid, TextServer::Hinting p_hinting) {
  1295. FontFallback *fd = _get_font_data(p_font_rid);
  1296. ERR_FAIL_NULL(fd);
  1297. MutexLock lock(fd->mutex);
  1298. if (fd->hinting != p_hinting) {
  1299. _font_clear_cache(fd);
  1300. fd->hinting = p_hinting;
  1301. }
  1302. }
  1303. TextServer::Hinting TextServerFallback::_font_get_hinting(const RID &p_font_rid) const {
  1304. FontFallback *fd = _get_font_data(p_font_rid);
  1305. ERR_FAIL_NULL_V(fd, HINTING_NONE);
  1306. MutexLock lock(fd->mutex);
  1307. return fd->hinting;
  1308. }
  1309. void TextServerFallback::_font_set_subpixel_positioning(const RID &p_font_rid, TextServer::SubpixelPositioning p_subpixel) {
  1310. FontFallback *fd = _get_font_data(p_font_rid);
  1311. ERR_FAIL_NULL(fd);
  1312. MutexLock lock(fd->mutex);
  1313. fd->subpixel_positioning = p_subpixel;
  1314. }
  1315. TextServer::SubpixelPositioning TextServerFallback::_font_get_subpixel_positioning(const RID &p_font_rid) const {
  1316. FontFallback *fd = _get_font_data(p_font_rid);
  1317. ERR_FAIL_NULL_V(fd, SUBPIXEL_POSITIONING_DISABLED);
  1318. MutexLock lock(fd->mutex);
  1319. return fd->subpixel_positioning;
  1320. }
  1321. void TextServerFallback::_font_set_keep_rounding_remainders(const RID &p_font_rid, bool p_keep_rounding_remainders) {
  1322. FontFallback *fd = _get_font_data(p_font_rid);
  1323. ERR_FAIL_NULL(fd);
  1324. MutexLock lock(fd->mutex);
  1325. fd->keep_rounding_remainders = p_keep_rounding_remainders;
  1326. }
  1327. bool TextServerFallback::_font_get_keep_rounding_remainders(const RID &p_font_rid) const {
  1328. FontFallback *fd = _get_font_data(p_font_rid);
  1329. ERR_FAIL_NULL_V(fd, false);
  1330. MutexLock lock(fd->mutex);
  1331. return fd->keep_rounding_remainders;
  1332. }
  1333. void TextServerFallback::_font_set_embolden(const RID &p_font_rid, double p_strength) {
  1334. FontFallback *fd = _get_font_data(p_font_rid);
  1335. ERR_FAIL_NULL(fd);
  1336. MutexLock lock(fd->mutex);
  1337. if (fd->embolden != p_strength) {
  1338. _font_clear_cache(fd);
  1339. fd->embolden = p_strength;
  1340. }
  1341. }
  1342. double TextServerFallback::_font_get_embolden(const RID &p_font_rid) const {
  1343. FontFallback *fd = _get_font_data(p_font_rid);
  1344. ERR_FAIL_NULL_V(fd, 0.0);
  1345. MutexLock lock(fd->mutex);
  1346. return fd->embolden;
  1347. }
  1348. void TextServerFallback::_font_set_spacing(const RID &p_font_rid, SpacingType p_spacing, int64_t p_value) {
  1349. ERR_FAIL_INDEX((int)p_spacing, 4);
  1350. FontFallbackLinkedVariation *fdv = font_var_owner.get_or_null(p_font_rid);
  1351. if (fdv) {
  1352. if (fdv->extra_spacing[p_spacing] != p_value) {
  1353. fdv->extra_spacing[p_spacing] = p_value;
  1354. }
  1355. } else {
  1356. FontFallback *fd = font_owner.get_or_null(p_font_rid);
  1357. ERR_FAIL_NULL(fd);
  1358. MutexLock lock(fd->mutex);
  1359. if (fd->extra_spacing[p_spacing] != p_value) {
  1360. _font_clear_cache(fd);
  1361. fd->extra_spacing[p_spacing] = p_value;
  1362. }
  1363. }
  1364. }
  1365. int64_t TextServerFallback::_font_get_spacing(const RID &p_font_rid, SpacingType p_spacing) const {
  1366. ERR_FAIL_INDEX_V((int)p_spacing, 4, 0);
  1367. FontFallbackLinkedVariation *fdv = font_var_owner.get_or_null(p_font_rid);
  1368. if (fdv) {
  1369. return fdv->extra_spacing[p_spacing];
  1370. } else {
  1371. FontFallback *fd = font_owner.get_or_null(p_font_rid);
  1372. ERR_FAIL_NULL_V(fd, 0);
  1373. MutexLock lock(fd->mutex);
  1374. return fd->extra_spacing[p_spacing];
  1375. }
  1376. }
  1377. void TextServerFallback::_font_set_baseline_offset(const RID &p_font_rid, double p_baseline_offset) {
  1378. FontFallbackLinkedVariation *fdv = font_var_owner.get_or_null(p_font_rid);
  1379. if (fdv) {
  1380. if (fdv->baseline_offset != p_baseline_offset) {
  1381. fdv->baseline_offset = p_baseline_offset;
  1382. }
  1383. } else {
  1384. FontFallback *fd = font_owner.get_or_null(p_font_rid);
  1385. ERR_FAIL_NULL(fd);
  1386. MutexLock lock(fd->mutex);
  1387. if (fd->baseline_offset != p_baseline_offset) {
  1388. _font_clear_cache(fd);
  1389. fd->baseline_offset = p_baseline_offset;
  1390. }
  1391. }
  1392. }
  1393. double TextServerFallback::_font_get_baseline_offset(const RID &p_font_rid) const {
  1394. FontFallbackLinkedVariation *fdv = font_var_owner.get_or_null(p_font_rid);
  1395. if (fdv) {
  1396. return fdv->baseline_offset;
  1397. } else {
  1398. FontFallback *fd = font_owner.get_or_null(p_font_rid);
  1399. ERR_FAIL_NULL_V(fd, 0.0);
  1400. MutexLock lock(fd->mutex);
  1401. return fd->baseline_offset;
  1402. }
  1403. }
  1404. void TextServerFallback::_font_set_transform(const RID &p_font_rid, const Transform2D &p_transform) {
  1405. FontFallback *fd = _get_font_data(p_font_rid);
  1406. ERR_FAIL_NULL(fd);
  1407. MutexLock lock(fd->mutex);
  1408. if (fd->transform != p_transform) {
  1409. _font_clear_cache(fd);
  1410. fd->transform = p_transform;
  1411. }
  1412. }
  1413. Transform2D TextServerFallback::_font_get_transform(const RID &p_font_rid) const {
  1414. FontFallback *fd = _get_font_data(p_font_rid);
  1415. ERR_FAIL_NULL_V(fd, Transform2D());
  1416. MutexLock lock(fd->mutex);
  1417. return fd->transform;
  1418. }
  1419. void TextServerFallback::_font_set_variation_coordinates(const RID &p_font_rid, const Dictionary &p_variation_coordinates) {
  1420. FontFallback *fd = _get_font_data(p_font_rid);
  1421. ERR_FAIL_NULL(fd);
  1422. MutexLock lock(fd->mutex);
  1423. if (!fd->variation_coordinates.recursive_equal(p_variation_coordinates, 1)) {
  1424. _font_clear_cache(fd);
  1425. fd->variation_coordinates = p_variation_coordinates.duplicate();
  1426. }
  1427. }
  1428. double TextServerFallback::_font_get_oversampling(const RID &p_font_rid) const {
  1429. FontFallback *fd = _get_font_data(p_font_rid);
  1430. ERR_FAIL_NULL_V(fd, -1.0);
  1431. MutexLock lock(fd->mutex);
  1432. return fd->oversampling_override;
  1433. }
  1434. void TextServerFallback::_font_set_oversampling(const RID &p_font_rid, double p_oversampling) {
  1435. FontFallback *fd = _get_font_data(p_font_rid);
  1436. ERR_FAIL_NULL(fd);
  1437. MutexLock lock(fd->mutex);
  1438. if (fd->oversampling_override != p_oversampling) {
  1439. _font_clear_cache(fd);
  1440. fd->oversampling_override = p_oversampling;
  1441. }
  1442. }
  1443. Dictionary TextServerFallback::_font_get_variation_coordinates(const RID &p_font_rid) const {
  1444. FontFallback *fd = _get_font_data(p_font_rid);
  1445. ERR_FAIL_NULL_V(fd, Dictionary());
  1446. MutexLock lock(fd->mutex);
  1447. return fd->variation_coordinates;
  1448. }
  1449. TypedArray<Vector2i> TextServerFallback::_font_get_size_cache_list(const RID &p_font_rid) const {
  1450. FontFallback *fd = _get_font_data(p_font_rid);
  1451. ERR_FAIL_NULL_V(fd, TypedArray<Vector2i>());
  1452. MutexLock lock(fd->mutex);
  1453. TypedArray<Vector2i> ret;
  1454. for (const KeyValue<Vector2i, FontForSizeFallback *> &E : fd->cache) {
  1455. if ((E.key.x % 64 == 0) && (E.value->viewport_oversampling == 0)) {
  1456. ret.push_back(Vector2i(E.key.x / 64, E.key.y));
  1457. }
  1458. }
  1459. return ret;
  1460. }
  1461. TypedArray<Dictionary> TextServerFallback::_font_get_size_cache_info(const RID &p_font_rid) const {
  1462. FontFallback *fd = _get_font_data(p_font_rid);
  1463. ERR_FAIL_NULL_V(fd, TypedArray<Dictionary>());
  1464. MutexLock lock(fd->mutex);
  1465. TypedArray<Dictionary> ret;
  1466. for (const KeyValue<Vector2i, FontForSizeFallback *> &E : fd->cache) {
  1467. Dictionary size_info;
  1468. size_info["size_px"] = Vector2i(E.key.x / 64, E.key.y);
  1469. if (E.value->viewport_oversampling) {
  1470. size_info["viewport_oversampling"] = double(E.value->viewport_oversampling) / 64.0;
  1471. }
  1472. size_info["glyphs"] = E.value->glyph_map.size();
  1473. size_info["textures"] = E.value->textures.size();
  1474. uint64_t sz = 0;
  1475. for (const ShelfPackTexture &tx : E.value->textures) {
  1476. sz += tx.image->get_data_size() * 2;
  1477. }
  1478. size_info["textures_size"] = sz;
  1479. ret.push_back(size_info);
  1480. }
  1481. return ret;
  1482. }
  1483. void TextServerFallback::_font_clear_size_cache(const RID &p_font_rid) {
  1484. FontFallback *fd = _get_font_data(p_font_rid);
  1485. ERR_FAIL_NULL(fd);
  1486. MutexLock lock(fd->mutex);
  1487. MutexLock ftlock(ft_mutex);
  1488. for (const KeyValue<Vector2i, FontForSizeFallback *> &E : fd->cache) {
  1489. if (E.value->viewport_oversampling != 0) {
  1490. OversamplingLevel *ol = oversampling_levels.getptr(E.value->viewport_oversampling);
  1491. if (ol) {
  1492. ol->fonts.erase(E.value);
  1493. }
  1494. }
  1495. memdelete(E.value);
  1496. }
  1497. fd->cache.clear();
  1498. }
  1499. void TextServerFallback::_font_remove_size_cache(const RID &p_font_rid, const Vector2i &p_size) {
  1500. FontFallback *fd = _get_font_data(p_font_rid);
  1501. ERR_FAIL_NULL(fd);
  1502. MutexLock lock(fd->mutex);
  1503. MutexLock ftlock(ft_mutex);
  1504. Vector2i size = Vector2i(p_size.x * 64, p_size.y);
  1505. if (fd->cache.has(size)) {
  1506. if (fd->cache[size]->viewport_oversampling != 0) {
  1507. OversamplingLevel *ol = oversampling_levels.getptr(fd->cache[size]->viewport_oversampling);
  1508. if (ol) {
  1509. ol->fonts.erase(fd->cache[size]);
  1510. }
  1511. }
  1512. memdelete(fd->cache[size]);
  1513. fd->cache.erase(size);
  1514. }
  1515. }
  1516. void TextServerFallback::_font_set_ascent(const RID &p_font_rid, int64_t p_size, double p_ascent) {
  1517. FontFallback *fd = _get_font_data(p_font_rid);
  1518. ERR_FAIL_NULL(fd);
  1519. MutexLock lock(fd->mutex);
  1520. Vector2i size = _get_size(fd, p_size);
  1521. FontForSizeFallback *ffsd = nullptr;
  1522. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size, ffsd));
  1523. ffsd->ascent = p_ascent;
  1524. }
  1525. double TextServerFallback::_font_get_ascent(const RID &p_font_rid, int64_t p_size) const {
  1526. FontFallback *fd = _get_font_data(p_font_rid);
  1527. ERR_FAIL_NULL_V(fd, 0.0);
  1528. MutexLock lock(fd->mutex);
  1529. Vector2i size = _get_size(fd, p_size);
  1530. FontForSizeFallback *ffsd = nullptr;
  1531. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size, ffsd), 0.0);
  1532. if (fd->msdf) {
  1533. return ffsd->ascent * (double)p_size / (double)fd->msdf_source_size;
  1534. } else if (fd->fixed_size > 0 && fd->fixed_size_scale_mode != FIXED_SIZE_SCALE_DISABLE && size.x != p_size * 64) {
  1535. if (fd->fixed_size_scale_mode == FIXED_SIZE_SCALE_ENABLED) {
  1536. return ffsd->ascent * (double)p_size / (double)fd->fixed_size;
  1537. } else {
  1538. return ffsd->ascent * Math::round((double)p_size / (double)fd->fixed_size);
  1539. }
  1540. } else {
  1541. return ffsd->ascent;
  1542. }
  1543. }
  1544. void TextServerFallback::_font_set_descent(const RID &p_font_rid, int64_t p_size, double p_descent) {
  1545. FontFallback *fd = _get_font_data(p_font_rid);
  1546. ERR_FAIL_NULL(fd);
  1547. Vector2i size = _get_size(fd, p_size);
  1548. FontForSizeFallback *ffsd = nullptr;
  1549. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size, ffsd));
  1550. ffsd->descent = p_descent;
  1551. }
  1552. double TextServerFallback::_font_get_descent(const RID &p_font_rid, int64_t p_size) const {
  1553. FontFallback *fd = _get_font_data(p_font_rid);
  1554. ERR_FAIL_NULL_V(fd, 0.0);
  1555. MutexLock lock(fd->mutex);
  1556. Vector2i size = _get_size(fd, p_size);
  1557. FontForSizeFallback *ffsd = nullptr;
  1558. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size, ffsd), 0.0);
  1559. if (fd->msdf) {
  1560. return ffsd->descent * (double)p_size / (double)fd->msdf_source_size;
  1561. } else if (fd->fixed_size > 0 && fd->fixed_size_scale_mode != FIXED_SIZE_SCALE_DISABLE && size.x != p_size * 64) {
  1562. if (fd->fixed_size_scale_mode == FIXED_SIZE_SCALE_ENABLED) {
  1563. return ffsd->descent * (double)p_size / (double)fd->fixed_size;
  1564. } else {
  1565. return ffsd->descent * Math::round((double)p_size / (double)fd->fixed_size);
  1566. }
  1567. } else {
  1568. return ffsd->descent;
  1569. }
  1570. }
  1571. void TextServerFallback::_font_set_underline_position(const RID &p_font_rid, int64_t p_size, double p_underline_position) {
  1572. FontFallback *fd = _get_font_data(p_font_rid);
  1573. ERR_FAIL_NULL(fd);
  1574. MutexLock lock(fd->mutex);
  1575. Vector2i size = _get_size(fd, p_size);
  1576. FontForSizeFallback *ffsd = nullptr;
  1577. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size, ffsd));
  1578. ffsd->underline_position = p_underline_position;
  1579. }
  1580. double TextServerFallback::_font_get_underline_position(const RID &p_font_rid, int64_t p_size) const {
  1581. FontFallback *fd = _get_font_data(p_font_rid);
  1582. ERR_FAIL_NULL_V(fd, 0.0);
  1583. MutexLock lock(fd->mutex);
  1584. Vector2i size = _get_size(fd, p_size);
  1585. FontForSizeFallback *ffsd = nullptr;
  1586. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size, ffsd), 0.0);
  1587. if (fd->msdf) {
  1588. return ffsd->underline_position * (double)p_size / (double)fd->msdf_source_size;
  1589. } else if (fd->fixed_size > 0 && fd->fixed_size_scale_mode != FIXED_SIZE_SCALE_DISABLE && size.x != p_size * 64) {
  1590. if (fd->fixed_size_scale_mode == FIXED_SIZE_SCALE_ENABLED) {
  1591. return ffsd->underline_position * (double)p_size / (double)fd->fixed_size;
  1592. } else {
  1593. return ffsd->underline_position * Math::round((double)p_size / (double)fd->fixed_size);
  1594. }
  1595. } else {
  1596. return ffsd->underline_position;
  1597. }
  1598. }
  1599. void TextServerFallback::_font_set_underline_thickness(const RID &p_font_rid, int64_t p_size, double p_underline_thickness) {
  1600. FontFallback *fd = _get_font_data(p_font_rid);
  1601. ERR_FAIL_NULL(fd);
  1602. MutexLock lock(fd->mutex);
  1603. Vector2i size = _get_size(fd, p_size);
  1604. FontForSizeFallback *ffsd = nullptr;
  1605. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size, ffsd));
  1606. ffsd->underline_thickness = p_underline_thickness;
  1607. }
  1608. double TextServerFallback::_font_get_underline_thickness(const RID &p_font_rid, int64_t p_size) const {
  1609. FontFallback *fd = _get_font_data(p_font_rid);
  1610. ERR_FAIL_NULL_V(fd, 0.0);
  1611. MutexLock lock(fd->mutex);
  1612. Vector2i size = _get_size(fd, p_size);
  1613. FontForSizeFallback *ffsd = nullptr;
  1614. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size, ffsd), 0.0);
  1615. if (fd->msdf) {
  1616. return ffsd->underline_thickness * (double)p_size / (double)fd->msdf_source_size;
  1617. } else if (fd->fixed_size > 0 && fd->fixed_size_scale_mode != FIXED_SIZE_SCALE_DISABLE && size.x != p_size * 64) {
  1618. if (fd->fixed_size_scale_mode == FIXED_SIZE_SCALE_ENABLED) {
  1619. return ffsd->underline_thickness * (double)p_size / (double)fd->fixed_size;
  1620. } else {
  1621. return ffsd->underline_thickness * Math::round((double)p_size / (double)fd->fixed_size);
  1622. }
  1623. } else {
  1624. return ffsd->underline_thickness;
  1625. }
  1626. }
  1627. void TextServerFallback::_font_set_scale(const RID &p_font_rid, int64_t p_size, double p_scale) {
  1628. FontFallback *fd = _get_font_data(p_font_rid);
  1629. ERR_FAIL_NULL(fd);
  1630. MutexLock lock(fd->mutex);
  1631. Vector2i size = _get_size(fd, p_size);
  1632. FontForSizeFallback *ffsd = nullptr;
  1633. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size, ffsd));
  1634. #ifdef MODULE_FREETYPE_ENABLED
  1635. if (ffsd->face) {
  1636. return; // Do not override scale for dynamic fonts, it's calculated automatically.
  1637. }
  1638. #endif
  1639. ffsd->scale = p_scale;
  1640. }
  1641. double TextServerFallback::_font_get_scale(const RID &p_font_rid, int64_t p_size) const {
  1642. FontFallback *fd = _get_font_data(p_font_rid);
  1643. ERR_FAIL_NULL_V(fd, 0.0);
  1644. MutexLock lock(fd->mutex);
  1645. Vector2i size = _get_size(fd, p_size);
  1646. FontForSizeFallback *ffsd = nullptr;
  1647. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size, ffsd), 0.0);
  1648. if (fd->msdf) {
  1649. return ffsd->scale * (double)p_size / (double)fd->msdf_source_size;
  1650. } else if (fd->fixed_size > 0 && fd->fixed_size_scale_mode != FIXED_SIZE_SCALE_DISABLE && size.x != p_size * 64) {
  1651. if (fd->fixed_size_scale_mode == FIXED_SIZE_SCALE_ENABLED) {
  1652. return ffsd->scale * (double)p_size / (double)fd->fixed_size;
  1653. } else {
  1654. return ffsd->scale * Math::round((double)p_size / (double)fd->fixed_size);
  1655. }
  1656. } else {
  1657. return ffsd->scale;
  1658. }
  1659. }
  1660. int64_t TextServerFallback::_font_get_texture_count(const RID &p_font_rid, const Vector2i &p_size) const {
  1661. FontFallback *fd = _get_font_data(p_font_rid);
  1662. ERR_FAIL_NULL_V(fd, 0);
  1663. MutexLock lock(fd->mutex);
  1664. Vector2i size = _get_size_outline(fd, p_size);
  1665. FontForSizeFallback *ffsd = nullptr;
  1666. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size, ffsd), 0);
  1667. return ffsd->textures.size();
  1668. }
  1669. void TextServerFallback::_font_clear_textures(const RID &p_font_rid, const Vector2i &p_size) {
  1670. FontFallback *fd = _get_font_data(p_font_rid);
  1671. ERR_FAIL_NULL(fd);
  1672. MutexLock lock(fd->mutex);
  1673. Vector2i size = _get_size_outline(fd, p_size);
  1674. FontForSizeFallback *ffsd = nullptr;
  1675. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size, ffsd));
  1676. ffsd->textures.clear();
  1677. }
  1678. void TextServerFallback::_font_remove_texture(const RID &p_font_rid, const Vector2i &p_size, int64_t p_texture_index) {
  1679. FontFallback *fd = _get_font_data(p_font_rid);
  1680. ERR_FAIL_NULL(fd);
  1681. MutexLock lock(fd->mutex);
  1682. Vector2i size = _get_size_outline(fd, p_size);
  1683. FontForSizeFallback *ffsd = nullptr;
  1684. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size, ffsd));
  1685. ERR_FAIL_INDEX(p_texture_index, ffsd->textures.size());
  1686. ffsd->textures.remove_at(p_texture_index);
  1687. }
  1688. 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) {
  1689. FontFallback *fd = _get_font_data(p_font_rid);
  1690. ERR_FAIL_NULL(fd);
  1691. ERR_FAIL_COND(p_image.is_null());
  1692. MutexLock lock(fd->mutex);
  1693. Vector2i size = _get_size_outline(fd, p_size);
  1694. FontForSizeFallback *ffsd = nullptr;
  1695. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size, ffsd));
  1696. ERR_FAIL_COND(p_texture_index < 0);
  1697. if (p_texture_index >= ffsd->textures.size()) {
  1698. ffsd->textures.resize(p_texture_index + 1);
  1699. }
  1700. ShelfPackTexture &tex = ffsd->textures.write[p_texture_index];
  1701. tex.image = p_image;
  1702. tex.texture_w = p_image->get_width();
  1703. tex.texture_h = p_image->get_height();
  1704. Ref<Image> img = p_image;
  1705. if (fd->mipmaps && !img->has_mipmaps()) {
  1706. img = p_image->duplicate();
  1707. img->generate_mipmaps();
  1708. }
  1709. tex.texture = ImageTexture::create_from_image(img);
  1710. tex.dirty = false;
  1711. }
  1712. Ref<Image> TextServerFallback::_font_get_texture_image(const RID &p_font_rid, const Vector2i &p_size, int64_t p_texture_index) const {
  1713. FontFallback *fd = _get_font_data(p_font_rid);
  1714. ERR_FAIL_NULL_V(fd, Ref<Image>());
  1715. MutexLock lock(fd->mutex);
  1716. Vector2i size = _get_size_outline(fd, p_size);
  1717. FontForSizeFallback *ffsd = nullptr;
  1718. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size, ffsd), Ref<Image>());
  1719. ERR_FAIL_INDEX_V(p_texture_index, ffsd->textures.size(), Ref<Image>());
  1720. const ShelfPackTexture &tex = ffsd->textures[p_texture_index];
  1721. return tex.image;
  1722. }
  1723. void TextServerFallback::_font_set_texture_offsets(const RID &p_font_rid, const Vector2i &p_size, int64_t p_texture_index, const PackedInt32Array &p_offsets) {
  1724. ERR_FAIL_COND(p_offsets.size() % 4 != 0);
  1725. FontFallback *fd = _get_font_data(p_font_rid);
  1726. ERR_FAIL_NULL(fd);
  1727. MutexLock lock(fd->mutex);
  1728. Vector2i size = _get_size_outline(fd, p_size);
  1729. FontForSizeFallback *ffsd = nullptr;
  1730. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size, ffsd));
  1731. ERR_FAIL_COND(p_texture_index < 0);
  1732. if (p_texture_index >= ffsd->textures.size()) {
  1733. ffsd->textures.resize(p_texture_index + 1);
  1734. }
  1735. ShelfPackTexture &tex = ffsd->textures.write[p_texture_index];
  1736. tex.shelves.clear();
  1737. for (int32_t i = 0; i < p_offsets.size(); i += 4) {
  1738. tex.shelves.push_back(Shelf(p_offsets[i], p_offsets[i + 1], p_offsets[i + 2], p_offsets[i + 3]));
  1739. }
  1740. }
  1741. PackedInt32Array TextServerFallback::_font_get_texture_offsets(const RID &p_font_rid, const Vector2i &p_size, int64_t p_texture_index) const {
  1742. FontFallback *fd = _get_font_data(p_font_rid);
  1743. ERR_FAIL_NULL_V(fd, PackedInt32Array());
  1744. MutexLock lock(fd->mutex);
  1745. Vector2i size = _get_size_outline(fd, p_size);
  1746. FontForSizeFallback *ffsd = nullptr;
  1747. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size, ffsd), PackedInt32Array());
  1748. ERR_FAIL_INDEX_V(p_texture_index, ffsd->textures.size(), PackedInt32Array());
  1749. const ShelfPackTexture &tex = ffsd->textures[p_texture_index];
  1750. PackedInt32Array ret;
  1751. ret.resize(tex.shelves.size() * 4);
  1752. int32_t *wr = ret.ptrw();
  1753. int32_t i = 0;
  1754. for (const Shelf &E : tex.shelves) {
  1755. wr[i * 4] = E.x;
  1756. wr[i * 4 + 1] = E.y;
  1757. wr[i * 4 + 2] = E.w;
  1758. wr[i * 4 + 3] = E.h;
  1759. i++;
  1760. }
  1761. return ret;
  1762. }
  1763. PackedInt32Array TextServerFallback::_font_get_glyph_list(const RID &p_font_rid, const Vector2i &p_size) const {
  1764. FontFallback *fd = _get_font_data(p_font_rid);
  1765. ERR_FAIL_NULL_V(fd, PackedInt32Array());
  1766. MutexLock lock(fd->mutex);
  1767. Vector2i size = _get_size_outline(fd, p_size);
  1768. FontForSizeFallback *ffsd = nullptr;
  1769. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size, ffsd), PackedInt32Array());
  1770. PackedInt32Array ret;
  1771. const HashMap<int32_t, FontGlyph> &gl = ffsd->glyph_map;
  1772. for (const KeyValue<int32_t, FontGlyph> &E : gl) {
  1773. ret.push_back(E.key);
  1774. }
  1775. return ret;
  1776. }
  1777. void TextServerFallback::_font_clear_glyphs(const RID &p_font_rid, const Vector2i &p_size) {
  1778. FontFallback *fd = _get_font_data(p_font_rid);
  1779. ERR_FAIL_NULL(fd);
  1780. MutexLock lock(fd->mutex);
  1781. Vector2i size = _get_size_outline(fd, p_size);
  1782. FontForSizeFallback *ffsd = nullptr;
  1783. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size, ffsd));
  1784. ffsd->glyph_map.clear();
  1785. }
  1786. void TextServerFallback::_font_remove_glyph(const RID &p_font_rid, const Vector2i &p_size, int64_t p_glyph) {
  1787. FontFallback *fd = _get_font_data(p_font_rid);
  1788. ERR_FAIL_NULL(fd);
  1789. MutexLock lock(fd->mutex);
  1790. Vector2i size = _get_size_outline(fd, p_size);
  1791. FontForSizeFallback *ffsd = nullptr;
  1792. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size, ffsd));
  1793. ffsd->glyph_map.erase(p_glyph);
  1794. }
  1795. Vector2 TextServerFallback::_font_get_glyph_advance(const RID &p_font_rid, int64_t p_size, int64_t p_glyph) const {
  1796. FontFallback *fd = _get_font_data(p_font_rid);
  1797. ERR_FAIL_NULL_V(fd, Vector2());
  1798. MutexLock lock(fd->mutex);
  1799. Vector2i size = _get_size(fd, p_size);
  1800. FontForSizeFallback *ffsd = nullptr;
  1801. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size, ffsd), Vector2());
  1802. int mod = 0;
  1803. if (fd->antialiasing == FONT_ANTIALIASING_LCD) {
  1804. TextServer::FontLCDSubpixelLayout layout = lcd_subpixel_layout.get();
  1805. if (layout != FONT_LCD_SUBPIXEL_LAYOUT_NONE) {
  1806. mod = (layout << 24);
  1807. }
  1808. }
  1809. FontGlyph fgl;
  1810. if (!_ensure_glyph(fd, size, p_glyph | mod, fgl)) {
  1811. return Vector2(); // Invalid or non graphicl glyph, do not display errors.
  1812. }
  1813. Vector2 ea;
  1814. if (fd->embolden != 0.0) {
  1815. ea.x = fd->embolden * double(size.x) / 4096.0;
  1816. }
  1817. double scale = _font_get_scale(p_font_rid, p_size);
  1818. if (fd->msdf) {
  1819. return (fgl.advance + ea) * (double)p_size / (double)fd->msdf_source_size;
  1820. } else if (fd->fixed_size > 0 && fd->fixed_size_scale_mode != FIXED_SIZE_SCALE_DISABLE && size.x != p_size * 64) {
  1821. if (fd->fixed_size_scale_mode == FIXED_SIZE_SCALE_ENABLED) {
  1822. return (fgl.advance + ea) * (double)p_size / (double)fd->fixed_size;
  1823. } else {
  1824. return (fgl.advance + ea) * Math::round((double)p_size / (double)fd->fixed_size);
  1825. }
  1826. } 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 * 64))) {
  1827. return (fgl.advance + ea).round();
  1828. } else {
  1829. return fgl.advance + ea;
  1830. }
  1831. }
  1832. void TextServerFallback::_font_set_glyph_advance(const RID &p_font_rid, int64_t p_size, int64_t p_glyph, const Vector2 &p_advance) {
  1833. FontFallback *fd = _get_font_data(p_font_rid);
  1834. ERR_FAIL_NULL(fd);
  1835. MutexLock lock(fd->mutex);
  1836. Vector2i size = _get_size(fd, p_size);
  1837. FontForSizeFallback *ffsd = nullptr;
  1838. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size, ffsd));
  1839. FontGlyph &fgl = ffsd->glyph_map[p_glyph];
  1840. fgl.advance = p_advance;
  1841. fgl.found = true;
  1842. }
  1843. Vector2 TextServerFallback::_font_get_glyph_offset(const RID &p_font_rid, const Vector2i &p_size, int64_t p_glyph) const {
  1844. FontFallback *fd = _get_font_data(p_font_rid);
  1845. ERR_FAIL_NULL_V(fd, Vector2());
  1846. MutexLock lock(fd->mutex);
  1847. Vector2i size = _get_size_outline(fd, p_size);
  1848. FontForSizeFallback *ffsd = nullptr;
  1849. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size, ffsd), Vector2());
  1850. int mod = 0;
  1851. if (fd->antialiasing == FONT_ANTIALIASING_LCD) {
  1852. TextServer::FontLCDSubpixelLayout layout = lcd_subpixel_layout.get();
  1853. if (layout != FONT_LCD_SUBPIXEL_LAYOUT_NONE) {
  1854. mod = (layout << 24);
  1855. }
  1856. }
  1857. FontGlyph fgl;
  1858. if (!_ensure_glyph(fd, size, p_glyph | mod, fgl)) {
  1859. return Vector2(); // Invalid or non graphicl glyph, do not display errors.
  1860. }
  1861. if (fd->msdf) {
  1862. return fgl.rect.position * (double)p_size.x / (double)fd->msdf_source_size;
  1863. } else if (fd->fixed_size > 0 && fd->fixed_size_scale_mode != FIXED_SIZE_SCALE_DISABLE && size.x != p_size.x * 64) {
  1864. if (fd->fixed_size_scale_mode == FIXED_SIZE_SCALE_ENABLED) {
  1865. return fgl.rect.position * (double)p_size.x / (double)fd->fixed_size;
  1866. } else {
  1867. return fgl.rect.position * Math::round((double)p_size.x / (double)fd->fixed_size);
  1868. }
  1869. } else {
  1870. return fgl.rect.position;
  1871. }
  1872. }
  1873. void TextServerFallback::_font_set_glyph_offset(const RID &p_font_rid, const Vector2i &p_size, int64_t p_glyph, const Vector2 &p_offset) {
  1874. FontFallback *fd = _get_font_data(p_font_rid);
  1875. ERR_FAIL_NULL(fd);
  1876. MutexLock lock(fd->mutex);
  1877. Vector2i size = _get_size_outline(fd, p_size);
  1878. FontForSizeFallback *ffsd = nullptr;
  1879. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size, ffsd));
  1880. FontGlyph &fgl = ffsd->glyph_map[p_glyph];
  1881. fgl.rect.position = p_offset;
  1882. fgl.found = true;
  1883. }
  1884. Vector2 TextServerFallback::_font_get_glyph_size(const RID &p_font_rid, const Vector2i &p_size, int64_t p_glyph) const {
  1885. FontFallback *fd = _get_font_data(p_font_rid);
  1886. ERR_FAIL_NULL_V(fd, Vector2());
  1887. MutexLock lock(fd->mutex);
  1888. Vector2i size = _get_size_outline(fd, p_size);
  1889. FontForSizeFallback *ffsd = nullptr;
  1890. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size, ffsd), Vector2());
  1891. int mod = 0;
  1892. if (fd->antialiasing == FONT_ANTIALIASING_LCD) {
  1893. TextServer::FontLCDSubpixelLayout layout = lcd_subpixel_layout.get();
  1894. if (layout != FONT_LCD_SUBPIXEL_LAYOUT_NONE) {
  1895. mod = (layout << 24);
  1896. }
  1897. }
  1898. FontGlyph fgl;
  1899. if (!_ensure_glyph(fd, size, p_glyph | mod, fgl)) {
  1900. return Vector2(); // Invalid or non graphicl glyph, do not display errors.
  1901. }
  1902. if (fd->msdf) {
  1903. return fgl.rect.size * (double)p_size.x / (double)fd->msdf_source_size;
  1904. } else if (fd->fixed_size > 0 && fd->fixed_size_scale_mode != FIXED_SIZE_SCALE_DISABLE && size.x != p_size.x * 64) {
  1905. if (fd->fixed_size_scale_mode == FIXED_SIZE_SCALE_ENABLED) {
  1906. return fgl.rect.size * (double)p_size.x / (double)fd->fixed_size;
  1907. } else {
  1908. return fgl.rect.size * Math::round((double)p_size.x / (double)fd->fixed_size);
  1909. }
  1910. } else {
  1911. return fgl.rect.size;
  1912. }
  1913. }
  1914. void TextServerFallback::_font_set_glyph_size(const RID &p_font_rid, const Vector2i &p_size, int64_t p_glyph, const Vector2 &p_gl_size) {
  1915. FontFallback *fd = _get_font_data(p_font_rid);
  1916. ERR_FAIL_NULL(fd);
  1917. MutexLock lock(fd->mutex);
  1918. Vector2i size = _get_size_outline(fd, p_size);
  1919. FontForSizeFallback *ffsd = nullptr;
  1920. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size, ffsd));
  1921. FontGlyph &fgl = ffsd->glyph_map[p_glyph];
  1922. fgl.rect.size = p_gl_size;
  1923. fgl.found = true;
  1924. }
  1925. Rect2 TextServerFallback::_font_get_glyph_uv_rect(const RID &p_font_rid, const Vector2i &p_size, int64_t p_glyph) const {
  1926. FontFallback *fd = _get_font_data(p_font_rid);
  1927. ERR_FAIL_NULL_V(fd, Rect2());
  1928. MutexLock lock(fd->mutex);
  1929. Vector2i size = _get_size_outline(fd, p_size);
  1930. FontForSizeFallback *ffsd = nullptr;
  1931. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size, ffsd), Rect2());
  1932. int mod = 0;
  1933. if (fd->antialiasing == FONT_ANTIALIASING_LCD) {
  1934. TextServer::FontLCDSubpixelLayout layout = lcd_subpixel_layout.get();
  1935. if (layout != FONT_LCD_SUBPIXEL_LAYOUT_NONE) {
  1936. mod = (layout << 24);
  1937. }
  1938. }
  1939. FontGlyph fgl;
  1940. if (!_ensure_glyph(fd, size, p_glyph | mod, fgl)) {
  1941. return Rect2(); // Invalid or non graphicl glyph, do not display errors.
  1942. }
  1943. return fgl.uv_rect;
  1944. }
  1945. 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) {
  1946. FontFallback *fd = _get_font_data(p_font_rid);
  1947. ERR_FAIL_NULL(fd);
  1948. MutexLock lock(fd->mutex);
  1949. Vector2i size = _get_size_outline(fd, p_size);
  1950. FontForSizeFallback *ffsd = nullptr;
  1951. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size, ffsd));
  1952. FontGlyph &fgl = ffsd->glyph_map[p_glyph];
  1953. fgl.uv_rect = p_uv_rect;
  1954. fgl.found = true;
  1955. }
  1956. int64_t TextServerFallback::_font_get_glyph_texture_idx(const RID &p_font_rid, const Vector2i &p_size, int64_t p_glyph) const {
  1957. FontFallback *fd = _get_font_data(p_font_rid);
  1958. ERR_FAIL_NULL_V(fd, -1);
  1959. MutexLock lock(fd->mutex);
  1960. Vector2i size = _get_size_outline(fd, p_size);
  1961. FontForSizeFallback *ffsd = nullptr;
  1962. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size, ffsd), -1);
  1963. int mod = 0;
  1964. if (fd->antialiasing == FONT_ANTIALIASING_LCD) {
  1965. TextServer::FontLCDSubpixelLayout layout = lcd_subpixel_layout.get();
  1966. if (layout != FONT_LCD_SUBPIXEL_LAYOUT_NONE) {
  1967. mod = (layout << 24);
  1968. }
  1969. }
  1970. FontGlyph fgl;
  1971. if (!_ensure_glyph(fd, size, p_glyph | mod, fgl)) {
  1972. return -1; // Invalid or non graphicl glyph, do not display errors.
  1973. }
  1974. return fgl.texture_idx;
  1975. }
  1976. 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) {
  1977. FontFallback *fd = _get_font_data(p_font_rid);
  1978. ERR_FAIL_NULL(fd);
  1979. MutexLock lock(fd->mutex);
  1980. Vector2i size = _get_size_outline(fd, p_size);
  1981. FontForSizeFallback *ffsd = nullptr;
  1982. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size, ffsd));
  1983. FontGlyph &fgl = ffsd->glyph_map[p_glyph];
  1984. fgl.texture_idx = p_texture_idx;
  1985. fgl.found = true;
  1986. }
  1987. RID TextServerFallback::_font_get_glyph_texture_rid(const RID &p_font_rid, const Vector2i &p_size, int64_t p_glyph) const {
  1988. FontFallback *fd = _get_font_data(p_font_rid);
  1989. ERR_FAIL_NULL_V(fd, RID());
  1990. MutexLock lock(fd->mutex);
  1991. Vector2i size = _get_size_outline(fd, p_size);
  1992. FontForSizeFallback *ffsd = nullptr;
  1993. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size, ffsd), RID());
  1994. int mod = 0;
  1995. if (fd->antialiasing == FONT_ANTIALIASING_LCD) {
  1996. TextServer::FontLCDSubpixelLayout layout = lcd_subpixel_layout.get();
  1997. if (layout != FONT_LCD_SUBPIXEL_LAYOUT_NONE) {
  1998. mod = (layout << 24);
  1999. }
  2000. }
  2001. FontGlyph fgl;
  2002. if (!_ensure_glyph(fd, size, p_glyph | mod, fgl)) {
  2003. return RID(); // Invalid or non graphicl glyph, do not display errors.
  2004. }
  2005. ERR_FAIL_COND_V(fgl.texture_idx < -1 || fgl.texture_idx >= ffsd->textures.size(), RID());
  2006. if (RenderingServer::get_singleton() != nullptr) {
  2007. if (fgl.texture_idx != -1) {
  2008. if (ffsd->textures[fgl.texture_idx].dirty) {
  2009. ShelfPackTexture &tex = ffsd->textures.write[fgl.texture_idx];
  2010. Ref<Image> img = tex.image;
  2011. if (fgl.from_svg) {
  2012. // Same as the "fix alpha border" process option when importing SVGs
  2013. img->fix_alpha_edges();
  2014. }
  2015. if (fd->mipmaps && !img->has_mipmaps()) {
  2016. img = tex.image->duplicate();
  2017. img->generate_mipmaps();
  2018. }
  2019. if (tex.texture.is_null()) {
  2020. tex.texture = ImageTexture::create_from_image(img);
  2021. } else {
  2022. tex.texture->update(img);
  2023. }
  2024. tex.dirty = false;
  2025. }
  2026. return ffsd->textures[fgl.texture_idx].texture->get_rid();
  2027. }
  2028. }
  2029. return RID();
  2030. }
  2031. Size2 TextServerFallback::_font_get_glyph_texture_size(const RID &p_font_rid, const Vector2i &p_size, int64_t p_glyph) const {
  2032. FontFallback *fd = _get_font_data(p_font_rid);
  2033. ERR_FAIL_NULL_V(fd, Size2());
  2034. MutexLock lock(fd->mutex);
  2035. Vector2i size = _get_size_outline(fd, p_size);
  2036. FontForSizeFallback *ffsd = nullptr;
  2037. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size, ffsd), Size2());
  2038. int mod = 0;
  2039. if (fd->antialiasing == FONT_ANTIALIASING_LCD) {
  2040. TextServer::FontLCDSubpixelLayout layout = lcd_subpixel_layout.get();
  2041. if (layout != FONT_LCD_SUBPIXEL_LAYOUT_NONE) {
  2042. mod = (layout << 24);
  2043. }
  2044. }
  2045. FontGlyph fgl;
  2046. if (!_ensure_glyph(fd, size, p_glyph | mod, fgl)) {
  2047. return Size2(); // Invalid or non graphicl glyph, do not display errors.
  2048. }
  2049. ERR_FAIL_COND_V(fgl.texture_idx < -1 || fgl.texture_idx >= ffsd->textures.size(), Size2());
  2050. if (RenderingServer::get_singleton() != nullptr) {
  2051. if (fgl.texture_idx != -1) {
  2052. if (ffsd->textures[fgl.texture_idx].dirty) {
  2053. ShelfPackTexture &tex = ffsd->textures.write[fgl.texture_idx];
  2054. Ref<Image> img = tex.image;
  2055. if (fgl.from_svg) {
  2056. // Same as the "fix alpha border" process option when importing SVGs
  2057. img->fix_alpha_edges();
  2058. }
  2059. if (fd->mipmaps && !img->has_mipmaps()) {
  2060. img = tex.image->duplicate();
  2061. img->generate_mipmaps();
  2062. }
  2063. if (tex.texture.is_null()) {
  2064. tex.texture = ImageTexture::create_from_image(img);
  2065. } else {
  2066. tex.texture->update(img);
  2067. }
  2068. tex.dirty = false;
  2069. }
  2070. return ffsd->textures[fgl.texture_idx].texture->get_size();
  2071. }
  2072. }
  2073. return Size2();
  2074. }
  2075. Dictionary TextServerFallback::_font_get_glyph_contours(const RID &p_font_rid, int64_t p_size, int64_t p_index) const {
  2076. FontFallback *fd = _get_font_data(p_font_rid);
  2077. ERR_FAIL_NULL_V(fd, Dictionary());
  2078. MutexLock lock(fd->mutex);
  2079. Vector2i size = _get_size(fd, p_size);
  2080. FontForSizeFallback *ffsd = nullptr;
  2081. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size, ffsd), Dictionary());
  2082. #ifdef MODULE_FREETYPE_ENABLED
  2083. PackedVector3Array points;
  2084. PackedInt32Array contours;
  2085. int32_t index = p_index & 0xffffff; // Remove subpixel shifts.
  2086. 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));
  2087. ERR_FAIL_COND_V(error, Dictionary());
  2088. if (fd->embolden != 0.f) {
  2089. FT_Pos strength = fd->embolden * size.x / 16; // 26.6 fractional units (1 / 64).
  2090. FT_Outline_Embolden(&ffsd->face->glyph->outline, strength);
  2091. }
  2092. if (fd->transform != Transform2D()) {
  2093. 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).
  2094. FT_Outline_Transform(&ffsd->face->glyph->outline, &mat);
  2095. }
  2096. double scale = (1.0 / 64.0) * ffsd->scale;
  2097. if (fd->msdf) {
  2098. scale = scale * (double)p_size / (double)fd->msdf_source_size;
  2099. } else if (fd->fixed_size > 0 && fd->fixed_size_scale_mode != FIXED_SIZE_SCALE_DISABLE && size.x != p_size * 64) {
  2100. if (fd->fixed_size_scale_mode == FIXED_SIZE_SCALE_ENABLED) {
  2101. scale = scale * (double)p_size / (double)fd->fixed_size;
  2102. } else {
  2103. scale = scale * Math::round((double)p_size / (double)fd->fixed_size);
  2104. }
  2105. }
  2106. for (short i = 0; i < ffsd->face->glyph->outline.n_points; i++) {
  2107. 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])));
  2108. }
  2109. for (short i = 0; i < ffsd->face->glyph->outline.n_contours; i++) {
  2110. contours.push_back(ffsd->face->glyph->outline.contours[i]);
  2111. }
  2112. bool orientation = (FT_Outline_Get_Orientation(&ffsd->face->glyph->outline) == FT_ORIENTATION_FILL_RIGHT);
  2113. Dictionary out;
  2114. out["points"] = points;
  2115. out["contours"] = contours;
  2116. out["orientation"] = orientation;
  2117. return out;
  2118. #else
  2119. return Dictionary();
  2120. #endif
  2121. }
  2122. TypedArray<Vector2i> TextServerFallback::_font_get_kerning_list(const RID &p_font_rid, int64_t p_size) const {
  2123. FontFallback *fd = _get_font_data(p_font_rid);
  2124. ERR_FAIL_NULL_V(fd, TypedArray<Vector2i>());
  2125. MutexLock lock(fd->mutex);
  2126. Vector2i size = _get_size(fd, p_size);
  2127. FontForSizeFallback *ffsd = nullptr;
  2128. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size, ffsd), TypedArray<Vector2i>());
  2129. TypedArray<Vector2i> ret;
  2130. for (const KeyValue<Vector2i, Vector2> &E : ffsd->kerning_map) {
  2131. ret.push_back(E.key);
  2132. }
  2133. return ret;
  2134. }
  2135. void TextServerFallback::_font_clear_kerning_map(const RID &p_font_rid, int64_t p_size) {
  2136. FontFallback *fd = _get_font_data(p_font_rid);
  2137. ERR_FAIL_NULL(fd);
  2138. MutexLock lock(fd->mutex);
  2139. Vector2i size = _get_size(fd, p_size);
  2140. FontForSizeFallback *ffsd = nullptr;
  2141. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size, ffsd));
  2142. ffsd->kerning_map.clear();
  2143. }
  2144. void TextServerFallback::_font_remove_kerning(const RID &p_font_rid, int64_t p_size, const Vector2i &p_glyph_pair) {
  2145. FontFallback *fd = _get_font_data(p_font_rid);
  2146. ERR_FAIL_NULL(fd);
  2147. MutexLock lock(fd->mutex);
  2148. Vector2i size = _get_size(fd, p_size);
  2149. FontForSizeFallback *ffsd = nullptr;
  2150. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size, ffsd));
  2151. ffsd->kerning_map.erase(p_glyph_pair);
  2152. }
  2153. void TextServerFallback::_font_set_kerning(const RID &p_font_rid, int64_t p_size, const Vector2i &p_glyph_pair, const Vector2 &p_kerning) {
  2154. FontFallback *fd = _get_font_data(p_font_rid);
  2155. ERR_FAIL_NULL(fd);
  2156. MutexLock lock(fd->mutex);
  2157. Vector2i size = _get_size(fd, p_size);
  2158. FontForSizeFallback *ffsd = nullptr;
  2159. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size, ffsd));
  2160. ffsd->kerning_map[p_glyph_pair] = p_kerning;
  2161. }
  2162. Vector2 TextServerFallback::_font_get_kerning(const RID &p_font_rid, int64_t p_size, const Vector2i &p_glyph_pair) const {
  2163. FontFallback *fd = _get_font_data(p_font_rid);
  2164. ERR_FAIL_NULL_V(fd, Vector2());
  2165. MutexLock lock(fd->mutex);
  2166. Vector2i size = _get_size(fd, p_size);
  2167. FontForSizeFallback *ffsd = nullptr;
  2168. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size, ffsd), Vector2());
  2169. const HashMap<Vector2i, Vector2> &kern = ffsd->kerning_map;
  2170. if (kern.has(p_glyph_pair)) {
  2171. if (fd->msdf) {
  2172. return kern[p_glyph_pair] * (double)p_size / (double)fd->msdf_source_size;
  2173. } else if (fd->fixed_size > 0 && fd->fixed_size_scale_mode != FIXED_SIZE_SCALE_DISABLE && size.x != p_size * 64) {
  2174. if (fd->fixed_size_scale_mode == FIXED_SIZE_SCALE_ENABLED) {
  2175. return kern[p_glyph_pair] * (double)p_size / (double)fd->fixed_size;
  2176. } else {
  2177. return kern[p_glyph_pair] * Math::round((double)p_size / (double)fd->fixed_size);
  2178. }
  2179. } else {
  2180. return kern[p_glyph_pair];
  2181. }
  2182. } else {
  2183. #ifdef MODULE_FREETYPE_ENABLED
  2184. if (ffsd->face) {
  2185. FT_Vector delta;
  2186. int32_t glyph_a = FT_Get_Char_Index(ffsd->face, p_glyph_pair.x);
  2187. int32_t glyph_b = FT_Get_Char_Index(ffsd->face, p_glyph_pair.y);
  2188. FT_Get_Kerning(ffsd->face, glyph_a, glyph_b, FT_KERNING_DEFAULT, &delta);
  2189. if (fd->msdf) {
  2190. return Vector2(delta.x, delta.y) * (double)p_size / (double)fd->msdf_source_size;
  2191. } else if (fd->fixed_size > 0 && fd->fixed_size_scale_mode != FIXED_SIZE_SCALE_DISABLE && size.x != p_size * 64) {
  2192. if (fd->fixed_size_scale_mode == FIXED_SIZE_SCALE_ENABLED) {
  2193. return Vector2(delta.x, delta.y) * (double)p_size / (double)fd->fixed_size;
  2194. } else {
  2195. return Vector2(delta.x, delta.y) * Math::round((double)p_size / (double)fd->fixed_size);
  2196. }
  2197. } else {
  2198. return Vector2(delta.x, delta.y);
  2199. }
  2200. }
  2201. #endif
  2202. }
  2203. return Vector2();
  2204. }
  2205. 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 {
  2206. 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) + ".");
  2207. return (int64_t)p_char;
  2208. }
  2209. int64_t TextServerFallback::_font_get_char_from_glyph_index(const RID &p_font_rid, int64_t p_size, int64_t p_glyph_index) const {
  2210. return p_glyph_index;
  2211. }
  2212. bool TextServerFallback::_font_has_char(const RID &p_font_rid, int64_t p_char) const {
  2213. FontFallback *fd = _get_font_data(p_font_rid);
  2214. 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) + ".");
  2215. if (!fd) {
  2216. return false;
  2217. }
  2218. MutexLock lock(fd->mutex);
  2219. FontForSizeFallback *ffsd = nullptr;
  2220. if (fd->cache.is_empty()) {
  2221. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, fd->msdf ? Vector2i(fd->msdf_source_size * 64, 0) : Vector2i(16 * 64, 0), ffsd), false);
  2222. } else {
  2223. ffsd = fd->cache.begin()->value;
  2224. }
  2225. #ifdef MODULE_FREETYPE_ENABLED
  2226. if (ffsd->face) {
  2227. return FT_Get_Char_Index(ffsd->face, p_char) != 0;
  2228. }
  2229. #endif
  2230. return ffsd->glyph_map.has((int32_t)p_char);
  2231. }
  2232. String TextServerFallback::_font_get_supported_chars(const RID &p_font_rid) const {
  2233. FontFallback *fd = _get_font_data(p_font_rid);
  2234. ERR_FAIL_NULL_V(fd, String());
  2235. MutexLock lock(fd->mutex);
  2236. FontForSizeFallback *ffsd = nullptr;
  2237. if (fd->cache.is_empty()) {
  2238. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, fd->msdf ? Vector2i(fd->msdf_source_size * 64, 0) : Vector2i(16 * 64, 0), ffsd), String());
  2239. } else {
  2240. ffsd = fd->cache.begin()->value;
  2241. }
  2242. String chars;
  2243. #ifdef MODULE_FREETYPE_ENABLED
  2244. if (ffsd->face) {
  2245. FT_UInt gindex;
  2246. FT_ULong charcode = FT_Get_First_Char(ffsd->face, &gindex);
  2247. while (gindex != 0) {
  2248. if (charcode != 0) {
  2249. chars = chars + String::chr(charcode);
  2250. }
  2251. charcode = FT_Get_Next_Char(ffsd->face, charcode, &gindex);
  2252. }
  2253. return chars;
  2254. }
  2255. #endif
  2256. const HashMap<int32_t, FontGlyph> &gl = ffsd->glyph_map;
  2257. for (const KeyValue<int32_t, FontGlyph> &E : gl) {
  2258. chars = chars + String::chr(E.key);
  2259. }
  2260. return chars;
  2261. }
  2262. PackedInt32Array TextServerFallback::_font_get_supported_glyphs(const RID &p_font_rid) const {
  2263. FontFallback *fd = _get_font_data(p_font_rid);
  2264. ERR_FAIL_NULL_V(fd, PackedInt32Array());
  2265. MutexLock lock(fd->mutex);
  2266. FontForSizeFallback *at_size = nullptr;
  2267. if (fd->cache.is_empty()) {
  2268. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, fd->msdf ? Vector2i(fd->msdf_source_size * 64, 0) : Vector2i(16 * 64, 0), at_size), PackedInt32Array());
  2269. } else {
  2270. at_size = fd->cache.begin()->value;
  2271. }
  2272. PackedInt32Array glyphs;
  2273. #ifdef MODULE_FREETYPE_ENABLED
  2274. if (at_size && at_size->face) {
  2275. FT_UInt gindex;
  2276. FT_ULong charcode = FT_Get_First_Char(at_size->face, &gindex);
  2277. while (gindex != 0) {
  2278. glyphs.push_back(gindex);
  2279. charcode = FT_Get_Next_Char(at_size->face, charcode, &gindex);
  2280. }
  2281. return glyphs;
  2282. }
  2283. #endif
  2284. if (at_size) {
  2285. const HashMap<int32_t, FontGlyph> &gl = at_size->glyph_map;
  2286. for (const KeyValue<int32_t, FontGlyph> &E : gl) {
  2287. glyphs.push_back(E.key);
  2288. }
  2289. }
  2290. return glyphs;
  2291. }
  2292. void TextServerFallback::_font_render_range(const RID &p_font_rid, const Vector2i &p_size, int64_t p_start, int64_t p_end) {
  2293. FontFallback *fd = _get_font_data(p_font_rid);
  2294. ERR_FAIL_NULL(fd);
  2295. 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) + ".");
  2296. 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) + ".");
  2297. MutexLock lock(fd->mutex);
  2298. Vector2i size = _get_size_outline(fd, p_size);
  2299. FontForSizeFallback *ffsd = nullptr;
  2300. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size, ffsd));
  2301. for (int64_t i = p_start; i <= p_end; i++) {
  2302. #ifdef MODULE_FREETYPE_ENABLED
  2303. int32_t idx = i;
  2304. if (ffsd->face) {
  2305. FontGlyph fgl;
  2306. if (fd->msdf) {
  2307. _ensure_glyph(fd, size, (int32_t)idx, fgl);
  2308. } else {
  2309. for (int aa = 0; aa < ((fd->antialiasing == FONT_ANTIALIASING_LCD) ? FONT_LCD_SUBPIXEL_LAYOUT_MAX : 1); aa++) {
  2310. if ((fd->subpixel_positioning == SUBPIXEL_POSITIONING_ONE_QUARTER) || (fd->subpixel_positioning == SUBPIXEL_POSITIONING_AUTO && size.x <= SUBPIXEL_POSITIONING_ONE_QUARTER_MAX_SIZE * 64)) {
  2311. _ensure_glyph(fd, size, (int32_t)idx | (0 << 27) | (aa << 24), fgl);
  2312. _ensure_glyph(fd, size, (int32_t)idx | (1 << 27) | (aa << 24), fgl);
  2313. _ensure_glyph(fd, size, (int32_t)idx | (2 << 27) | (aa << 24), fgl);
  2314. _ensure_glyph(fd, size, (int32_t)idx | (3 << 27) | (aa << 24), fgl);
  2315. } else if ((fd->subpixel_positioning == SUBPIXEL_POSITIONING_ONE_HALF) || (fd->subpixel_positioning == SUBPIXEL_POSITIONING_AUTO && size.x <= SUBPIXEL_POSITIONING_ONE_HALF_MAX_SIZE * 64)) {
  2316. _ensure_glyph(fd, size, (int32_t)idx | (1 << 27) | (aa << 24), fgl);
  2317. _ensure_glyph(fd, size, (int32_t)idx | (0 << 27) | (aa << 24), fgl);
  2318. } else {
  2319. _ensure_glyph(fd, size, (int32_t)idx | (aa << 24), fgl);
  2320. }
  2321. }
  2322. }
  2323. }
  2324. #endif
  2325. }
  2326. }
  2327. void TextServerFallback::_font_render_glyph(const RID &p_font_rid, const Vector2i &p_size, int64_t p_index) {
  2328. FontFallback *fd = _get_font_data(p_font_rid);
  2329. ERR_FAIL_NULL(fd);
  2330. MutexLock lock(fd->mutex);
  2331. Vector2i size = _get_size_outline(fd, p_size);
  2332. FontForSizeFallback *ffsd = nullptr;
  2333. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size, ffsd));
  2334. #ifdef MODULE_FREETYPE_ENABLED
  2335. int32_t idx = p_index & 0xffffff; // Remove subpixel shifts.
  2336. if (ffsd->face) {
  2337. FontGlyph fgl;
  2338. if (fd->msdf) {
  2339. _ensure_glyph(fd, size, (int32_t)idx, fgl);
  2340. } else {
  2341. for (int aa = 0; aa < ((fd->antialiasing == FONT_ANTIALIASING_LCD) ? FONT_LCD_SUBPIXEL_LAYOUT_MAX : 1); aa++) {
  2342. if ((fd->subpixel_positioning == SUBPIXEL_POSITIONING_ONE_QUARTER) || (fd->subpixel_positioning == SUBPIXEL_POSITIONING_AUTO && size.x <= SUBPIXEL_POSITIONING_ONE_QUARTER_MAX_SIZE * 64)) {
  2343. _ensure_glyph(fd, size, (int32_t)idx | (0 << 27) | (aa << 24), fgl);
  2344. _ensure_glyph(fd, size, (int32_t)idx | (1 << 27) | (aa << 24), fgl);
  2345. _ensure_glyph(fd, size, (int32_t)idx | (2 << 27) | (aa << 24), fgl);
  2346. _ensure_glyph(fd, size, (int32_t)idx | (3 << 27) | (aa << 24), fgl);
  2347. } else if ((fd->subpixel_positioning == SUBPIXEL_POSITIONING_ONE_HALF) || (fd->subpixel_positioning == SUBPIXEL_POSITIONING_AUTO && size.x <= SUBPIXEL_POSITIONING_ONE_HALF_MAX_SIZE * 64)) {
  2348. _ensure_glyph(fd, size, (int32_t)idx | (1 << 27) | (aa << 24), fgl);
  2349. _ensure_glyph(fd, size, (int32_t)idx | (0 << 27) | (aa << 24), fgl);
  2350. } else {
  2351. _ensure_glyph(fd, size, (int32_t)idx | (aa << 24), fgl);
  2352. }
  2353. }
  2354. }
  2355. }
  2356. #endif
  2357. }
  2358. 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, float p_oversampling) const {
  2359. if (p_index == 0) {
  2360. return; // Non visual character, skip.
  2361. }
  2362. FontFallback *fd = _get_font_data(p_font_rid);
  2363. ERR_FAIL_NULL(fd);
  2364. MutexLock lock(fd->mutex);
  2365. // Oversampling.
  2366. bool viewport_oversampling = false;
  2367. float oversampling_factor = p_oversampling;
  2368. if (p_oversampling <= 0.0) {
  2369. if (fd->oversampling_override > 0.0) {
  2370. oversampling_factor = fd->oversampling_override;
  2371. } else if (vp_oversampling > 0.0) {
  2372. oversampling_factor = vp_oversampling;
  2373. viewport_oversampling = true;
  2374. } else {
  2375. oversampling_factor = 1.0;
  2376. }
  2377. }
  2378. bool skip_oversampling = fd->msdf || fd->fixed_size > 0;
  2379. if (skip_oversampling) {
  2380. oversampling_factor = 1.0;
  2381. } else {
  2382. uint64_t oversampling_level = CLAMP(oversampling_factor, 0.1, 100.0) * 64;
  2383. oversampling_factor = double(oversampling_level) / 64.0;
  2384. }
  2385. Vector2i size;
  2386. if (skip_oversampling) {
  2387. size = _get_size(fd, p_size);
  2388. } else {
  2389. size = Vector2i(p_size * 64 * oversampling_factor, 0);
  2390. }
  2391. FontForSizeFallback *ffsd = nullptr;
  2392. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size, ffsd, false, viewport_oversampling ? 64 * oversampling_factor : 0));
  2393. int32_t index = p_index & 0xffffff; // Remove subpixel shifts.
  2394. bool lcd_aa = false;
  2395. #ifdef MODULE_FREETYPE_ENABLED
  2396. if (!fd->msdf && ffsd->face) {
  2397. // LCD layout, bits 24, 25, 26
  2398. if (fd->antialiasing == FONT_ANTIALIASING_LCD) {
  2399. TextServer::FontLCDSubpixelLayout layout = lcd_subpixel_layout.get();
  2400. if (layout != FONT_LCD_SUBPIXEL_LAYOUT_NONE) {
  2401. lcd_aa = true;
  2402. index = index | (layout << 24);
  2403. }
  2404. }
  2405. // Subpixel X-shift, bits 27, 28
  2406. if ((fd->subpixel_positioning == SUBPIXEL_POSITIONING_ONE_QUARTER) || (fd->subpixel_positioning == SUBPIXEL_POSITIONING_AUTO && size.x <= SUBPIXEL_POSITIONING_ONE_QUARTER_MAX_SIZE * 64)) {
  2407. int xshift = (int)(Math::floor(4 * (p_pos.x + 0.125)) - 4 * Math::floor(p_pos.x + 0.125));
  2408. index = index | (xshift << 27);
  2409. } else if ((fd->subpixel_positioning == SUBPIXEL_POSITIONING_ONE_HALF) || (fd->subpixel_positioning == SUBPIXEL_POSITIONING_AUTO && size.x <= SUBPIXEL_POSITIONING_ONE_HALF_MAX_SIZE * 64)) {
  2410. int xshift = (int)(Math::floor(2 * (p_pos.x + 0.25)) - 2 * Math::floor(p_pos.x + 0.25));
  2411. index = index | (xshift << 27);
  2412. }
  2413. }
  2414. #endif
  2415. FontGlyph fgl;
  2416. if (!_ensure_glyph(fd, size, index, fgl, viewport_oversampling ? 64 * oversampling_factor : 0)) {
  2417. return; // Invalid or non-graphical glyph, do not display errors, nothing to draw.
  2418. }
  2419. if (fgl.found) {
  2420. ERR_FAIL_COND(fgl.texture_idx < -1 || fgl.texture_idx >= ffsd->textures.size());
  2421. if (fgl.texture_idx != -1) {
  2422. Color modulate = p_color;
  2423. #ifdef MODULE_FREETYPE_ENABLED
  2424. if (!fd->modulate_color_glyphs && 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) {
  2425. modulate.r = modulate.g = modulate.b = 1.0;
  2426. }
  2427. #endif
  2428. if (RenderingServer::get_singleton() != nullptr) {
  2429. if (ffsd->textures[fgl.texture_idx].dirty) {
  2430. ShelfPackTexture &tex = ffsd->textures.write[fgl.texture_idx];
  2431. Ref<Image> img = tex.image;
  2432. if (fgl.from_svg) {
  2433. // Same as the "fix alpha border" process option when importing SVGs
  2434. img->fix_alpha_edges();
  2435. }
  2436. if (fd->mipmaps && !img->has_mipmaps()) {
  2437. img = tex.image->duplicate();
  2438. img->generate_mipmaps();
  2439. }
  2440. if (tex.texture.is_null()) {
  2441. tex.texture = ImageTexture::create_from_image(img);
  2442. } else {
  2443. tex.texture->update(img);
  2444. }
  2445. tex.dirty = false;
  2446. }
  2447. RID texture = ffsd->textures[fgl.texture_idx].texture->get_rid();
  2448. if (fd->msdf) {
  2449. Point2 cpos = p_pos;
  2450. cpos += fgl.rect.position * (double)p_size / (double)fd->msdf_source_size;
  2451. Size2 csize = fgl.rect.size * (double)p_size / (double)fd->msdf_source_size;
  2452. 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);
  2453. } else {
  2454. Point2 cpos = p_pos;
  2455. double scale = _font_get_scale(p_font_rid, p_size) / oversampling_factor;
  2456. if ((fd->subpixel_positioning == SUBPIXEL_POSITIONING_ONE_QUARTER) || (fd->subpixel_positioning == SUBPIXEL_POSITIONING_AUTO && size.x <= SUBPIXEL_POSITIONING_ONE_QUARTER_MAX_SIZE)) {
  2457. cpos.x = cpos.x + 0.125;
  2458. } else if ((fd->subpixel_positioning == SUBPIXEL_POSITIONING_ONE_HALF) || (fd->subpixel_positioning == SUBPIXEL_POSITIONING_AUTO && size.x <= SUBPIXEL_POSITIONING_ONE_HALF_MAX_SIZE)) {
  2459. cpos.x = cpos.x + 0.25;
  2460. }
  2461. if (scale == 1.0) {
  2462. cpos.y = Math::floor(cpos.y);
  2463. cpos.x = Math::floor(cpos.x);
  2464. }
  2465. Vector2 gpos = fgl.rect.position;
  2466. Size2 csize = fgl.rect.size;
  2467. if (fd->fixed_size > 0 && fd->fixed_size_scale_mode != FIXED_SIZE_SCALE_DISABLE) {
  2468. if (size.x != p_size * 64) {
  2469. if (fd->fixed_size_scale_mode == FIXED_SIZE_SCALE_ENABLED) {
  2470. double gl_scale = (double)p_size / (double)fd->fixed_size;
  2471. gpos *= gl_scale;
  2472. csize *= gl_scale;
  2473. } else {
  2474. double gl_scale = Math::round((double)p_size / (double)fd->fixed_size);
  2475. gpos *= gl_scale;
  2476. csize *= gl_scale;
  2477. }
  2478. }
  2479. } else {
  2480. gpos /= oversampling_factor;
  2481. csize /= oversampling_factor;
  2482. }
  2483. cpos += gpos;
  2484. if (lcd_aa) {
  2485. RenderingServer::get_singleton()->canvas_item_add_lcd_texture_rect_region(p_canvas, Rect2(cpos, csize), texture, fgl.uv_rect, modulate);
  2486. } else {
  2487. RenderingServer::get_singleton()->canvas_item_add_texture_rect_region(p_canvas, Rect2(cpos, csize), texture, fgl.uv_rect, modulate, false, false);
  2488. }
  2489. }
  2490. }
  2491. }
  2492. }
  2493. }
  2494. 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, float p_oversampling) const {
  2495. if (p_index == 0) {
  2496. return; // Non visual character, skip.
  2497. }
  2498. FontFallback *fd = _get_font_data(p_font_rid);
  2499. ERR_FAIL_NULL(fd);
  2500. MutexLock lock(fd->mutex);
  2501. // Oversampling.
  2502. bool viewport_oversampling = false;
  2503. float oversampling_factor = p_oversampling;
  2504. if (p_oversampling <= 0.0) {
  2505. if (fd->oversampling_override > 0.0) {
  2506. oversampling_factor = fd->oversampling_override;
  2507. } else if (vp_oversampling > 0.0) {
  2508. oversampling_factor = vp_oversampling;
  2509. viewport_oversampling = true;
  2510. } else {
  2511. oversampling_factor = 1.0;
  2512. }
  2513. }
  2514. bool skip_oversampling = fd->msdf || fd->fixed_size > 0;
  2515. if (skip_oversampling) {
  2516. oversampling_factor = 1.0;
  2517. } else {
  2518. uint64_t oversampling_level = CLAMP(oversampling_factor, 0.1, 100.0) * 64;
  2519. oversampling_factor = double(oversampling_level) / 64.0;
  2520. }
  2521. Vector2i size;
  2522. if (skip_oversampling) {
  2523. size = _get_size_outline(fd, Vector2i(p_size, p_outline_size));
  2524. } else {
  2525. size = Vector2i(p_size * 64 * oversampling_factor, p_outline_size * oversampling_factor);
  2526. }
  2527. FontForSizeFallback *ffsd = nullptr;
  2528. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size, ffsd, false, viewport_oversampling ? 64 * oversampling_factor : 0));
  2529. int32_t index = p_index & 0xffffff; // Remove subpixel shifts.
  2530. bool lcd_aa = false;
  2531. #ifdef MODULE_FREETYPE_ENABLED
  2532. if (!fd->msdf && ffsd->face) {
  2533. // LCD layout, bits 24, 25, 26
  2534. if (fd->antialiasing == FONT_ANTIALIASING_LCD) {
  2535. TextServer::FontLCDSubpixelLayout layout = lcd_subpixel_layout.get();
  2536. if (layout != FONT_LCD_SUBPIXEL_LAYOUT_NONE) {
  2537. lcd_aa = true;
  2538. index = index | (layout << 24);
  2539. }
  2540. }
  2541. // Subpixel X-shift, bits 27, 28
  2542. if ((fd->subpixel_positioning == SUBPIXEL_POSITIONING_ONE_QUARTER) || (fd->subpixel_positioning == SUBPIXEL_POSITIONING_AUTO && size.x <= SUBPIXEL_POSITIONING_ONE_QUARTER_MAX_SIZE * 64)) {
  2543. int xshift = (int)(Math::floor(4 * (p_pos.x + 0.125)) - 4 * Math::floor(p_pos.x + 0.125));
  2544. index = index | (xshift << 27);
  2545. } else if ((fd->subpixel_positioning == SUBPIXEL_POSITIONING_ONE_HALF) || (fd->subpixel_positioning == SUBPIXEL_POSITIONING_AUTO && size.x <= SUBPIXEL_POSITIONING_ONE_HALF_MAX_SIZE * 64)) {
  2546. int xshift = (int)(Math::floor(2 * (p_pos.x + 0.25)) - 2 * Math::floor(p_pos.x + 0.25));
  2547. index = index | (xshift << 27);
  2548. }
  2549. }
  2550. #endif
  2551. FontGlyph fgl;
  2552. if (!_ensure_glyph(fd, size, index, fgl, viewport_oversampling ? 64 * oversampling_factor : 0)) {
  2553. return; // Invalid or non-graphical glyph, do not display errors, nothing to draw.
  2554. }
  2555. if (fgl.found) {
  2556. ERR_FAIL_COND(fgl.texture_idx < -1 || fgl.texture_idx >= ffsd->textures.size());
  2557. if (fgl.texture_idx != -1) {
  2558. Color modulate = p_color;
  2559. #ifdef MODULE_FREETYPE_ENABLED
  2560. 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) {
  2561. modulate.r = modulate.g = modulate.b = 1.0;
  2562. }
  2563. #endif
  2564. if (RenderingServer::get_singleton() != nullptr) {
  2565. if (ffsd->textures[fgl.texture_idx].dirty) {
  2566. ShelfPackTexture &tex = ffsd->textures.write[fgl.texture_idx];
  2567. Ref<Image> img = tex.image;
  2568. if (fd->mipmaps && !img->has_mipmaps()) {
  2569. img = tex.image->duplicate();
  2570. img->generate_mipmaps();
  2571. }
  2572. if (tex.texture.is_null()) {
  2573. tex.texture = ImageTexture::create_from_image(img);
  2574. } else {
  2575. tex.texture->update(img);
  2576. }
  2577. tex.dirty = false;
  2578. }
  2579. RID texture = ffsd->textures[fgl.texture_idx].texture->get_rid();
  2580. if (fd->msdf) {
  2581. Point2 cpos = p_pos;
  2582. cpos += fgl.rect.position * (double)p_size / (double)fd->msdf_source_size;
  2583. Size2 csize = fgl.rect.size * (double)p_size / (double)fd->msdf_source_size;
  2584. 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);
  2585. } else {
  2586. Point2 cpos = p_pos;
  2587. double scale = _font_get_scale(p_font_rid, p_size) / oversampling_factor;
  2588. if ((fd->subpixel_positioning == SUBPIXEL_POSITIONING_ONE_QUARTER) || (fd->subpixel_positioning == SUBPIXEL_POSITIONING_AUTO && size.x <= SUBPIXEL_POSITIONING_ONE_QUARTER_MAX_SIZE)) {
  2589. cpos.x = cpos.x + 0.125;
  2590. } else if ((fd->subpixel_positioning == SUBPIXEL_POSITIONING_ONE_HALF) || (fd->subpixel_positioning == SUBPIXEL_POSITIONING_AUTO && size.x <= SUBPIXEL_POSITIONING_ONE_HALF_MAX_SIZE)) {
  2591. cpos.x = cpos.x + 0.25;
  2592. }
  2593. if (scale == 1.0) {
  2594. cpos.y = Math::floor(cpos.y);
  2595. cpos.x = Math::floor(cpos.x);
  2596. }
  2597. Vector2 gpos = fgl.rect.position;
  2598. Size2 csize = fgl.rect.size;
  2599. if (fd->fixed_size > 0 && fd->fixed_size_scale_mode != FIXED_SIZE_SCALE_DISABLE) {
  2600. if (size.x != p_size * 64) {
  2601. if (fd->fixed_size_scale_mode == FIXED_SIZE_SCALE_ENABLED) {
  2602. double gl_scale = (double)p_size / (double)fd->fixed_size;
  2603. gpos *= gl_scale;
  2604. csize *= gl_scale;
  2605. } else {
  2606. double gl_scale = Math::round((double)p_size / (double)fd->fixed_size);
  2607. gpos *= gl_scale;
  2608. csize *= gl_scale;
  2609. }
  2610. }
  2611. } else {
  2612. gpos /= oversampling_factor;
  2613. csize /= oversampling_factor;
  2614. }
  2615. cpos += gpos;
  2616. if (lcd_aa) {
  2617. RenderingServer::get_singleton()->canvas_item_add_lcd_texture_rect_region(p_canvas, Rect2(cpos, csize), texture, fgl.uv_rect, modulate);
  2618. } else {
  2619. RenderingServer::get_singleton()->canvas_item_add_texture_rect_region(p_canvas, Rect2(cpos, csize), texture, fgl.uv_rect, modulate, false, false);
  2620. }
  2621. }
  2622. }
  2623. }
  2624. }
  2625. }
  2626. bool TextServerFallback::_font_is_language_supported(const RID &p_font_rid, const String &p_language) const {
  2627. FontFallback *fd = _get_font_data(p_font_rid);
  2628. ERR_FAIL_NULL_V(fd, false);
  2629. MutexLock lock(fd->mutex);
  2630. if (fd->language_support_overrides.has(p_language)) {
  2631. return fd->language_support_overrides[p_language];
  2632. } else {
  2633. return true;
  2634. }
  2635. }
  2636. void TextServerFallback::_font_set_language_support_override(const RID &p_font_rid, const String &p_language, bool p_supported) {
  2637. FontFallback *fd = _get_font_data(p_font_rid);
  2638. ERR_FAIL_NULL(fd);
  2639. MutexLock lock(fd->mutex);
  2640. fd->language_support_overrides[p_language] = p_supported;
  2641. }
  2642. bool TextServerFallback::_font_get_language_support_override(const RID &p_font_rid, const String &p_language) {
  2643. FontFallback *fd = _get_font_data(p_font_rid);
  2644. ERR_FAIL_NULL_V(fd, false);
  2645. MutexLock lock(fd->mutex);
  2646. return fd->language_support_overrides[p_language];
  2647. }
  2648. void TextServerFallback::_font_remove_language_support_override(const RID &p_font_rid, const String &p_language) {
  2649. FontFallback *fd = _get_font_data(p_font_rid);
  2650. ERR_FAIL_NULL(fd);
  2651. MutexLock lock(fd->mutex);
  2652. fd->language_support_overrides.erase(p_language);
  2653. }
  2654. PackedStringArray TextServerFallback::_font_get_language_support_overrides(const RID &p_font_rid) {
  2655. FontFallback *fd = _get_font_data(p_font_rid);
  2656. ERR_FAIL_NULL_V(fd, PackedStringArray());
  2657. MutexLock lock(fd->mutex);
  2658. PackedStringArray out;
  2659. for (const KeyValue<String, bool> &E : fd->language_support_overrides) {
  2660. out.push_back(E.key);
  2661. }
  2662. return out;
  2663. }
  2664. bool TextServerFallback::_font_is_script_supported(const RID &p_font_rid, const String &p_script) const {
  2665. FontFallback *fd = _get_font_data(p_font_rid);
  2666. ERR_FAIL_NULL_V(fd, false);
  2667. MutexLock lock(fd->mutex);
  2668. if (fd->script_support_overrides.has(p_script)) {
  2669. return fd->script_support_overrides[p_script];
  2670. } else {
  2671. return true;
  2672. }
  2673. }
  2674. void TextServerFallback::_font_set_script_support_override(const RID &p_font_rid, const String &p_script, bool p_supported) {
  2675. FontFallback *fd = _get_font_data(p_font_rid);
  2676. ERR_FAIL_NULL(fd);
  2677. MutexLock lock(fd->mutex);
  2678. fd->script_support_overrides[p_script] = p_supported;
  2679. }
  2680. bool TextServerFallback::_font_get_script_support_override(const RID &p_font_rid, const String &p_script) {
  2681. FontFallback *fd = _get_font_data(p_font_rid);
  2682. ERR_FAIL_NULL_V(fd, false);
  2683. MutexLock lock(fd->mutex);
  2684. return fd->script_support_overrides[p_script];
  2685. }
  2686. void TextServerFallback::_font_remove_script_support_override(const RID &p_font_rid, const String &p_script) {
  2687. FontFallback *fd = _get_font_data(p_font_rid);
  2688. ERR_FAIL_NULL(fd);
  2689. MutexLock lock(fd->mutex);
  2690. Vector2i size = _get_size(fd, 16);
  2691. FontForSizeFallback *ffsd = nullptr;
  2692. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size, ffsd));
  2693. fd->script_support_overrides.erase(p_script);
  2694. }
  2695. PackedStringArray TextServerFallback::_font_get_script_support_overrides(const RID &p_font_rid) {
  2696. FontFallback *fd = _get_font_data(p_font_rid);
  2697. ERR_FAIL_NULL_V(fd, PackedStringArray());
  2698. MutexLock lock(fd->mutex);
  2699. PackedStringArray out;
  2700. for (const KeyValue<String, bool> &E : fd->script_support_overrides) {
  2701. out.push_back(E.key);
  2702. }
  2703. return out;
  2704. }
  2705. void TextServerFallback::_font_set_opentype_feature_overrides(const RID &p_font_rid, const Dictionary &p_overrides) {
  2706. FontFallback *fd = _get_font_data(p_font_rid);
  2707. ERR_FAIL_NULL(fd);
  2708. MutexLock lock(fd->mutex);
  2709. Vector2i size = _get_size(fd, 16);
  2710. FontForSizeFallback *ffsd = nullptr;
  2711. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size, ffsd));
  2712. fd->feature_overrides = p_overrides;
  2713. }
  2714. Dictionary TextServerFallback::_font_get_opentype_feature_overrides(const RID &p_font_rid) const {
  2715. FontFallback *fd = _get_font_data(p_font_rid);
  2716. ERR_FAIL_NULL_V(fd, Dictionary());
  2717. MutexLock lock(fd->mutex);
  2718. return fd->feature_overrides;
  2719. }
  2720. Dictionary TextServerFallback::_font_supported_feature_list(const RID &p_font_rid) const {
  2721. return Dictionary();
  2722. }
  2723. Dictionary TextServerFallback::_font_supported_variation_list(const RID &p_font_rid) const {
  2724. FontFallback *fd = _get_font_data(p_font_rid);
  2725. ERR_FAIL_NULL_V(fd, Dictionary());
  2726. MutexLock lock(fd->mutex);
  2727. Vector2i size = _get_size(fd, 16);
  2728. FontForSizeFallback *ffsd = nullptr;
  2729. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size, ffsd), Dictionary());
  2730. return fd->supported_varaitions;
  2731. }
  2732. /*************************************************************************/
  2733. /* Shaped text buffer interface */
  2734. /*************************************************************************/
  2735. void TextServerFallback::invalidate(ShapedTextDataFallback *p_shaped) {
  2736. p_shaped->valid.clear();
  2737. p_shaped->sort_valid = false;
  2738. p_shaped->line_breaks_valid = false;
  2739. p_shaped->justification_ops_valid = false;
  2740. p_shaped->ascent = 0.0;
  2741. p_shaped->descent = 0.0;
  2742. p_shaped->width = 0.0;
  2743. p_shaped->upos = 0.0;
  2744. p_shaped->uthk = 0.0;
  2745. p_shaped->glyphs.clear();
  2746. p_shaped->glyphs_logical.clear();
  2747. p_shaped->runs.clear();
  2748. p_shaped->runs_dirty = true;
  2749. }
  2750. void TextServerFallback::full_copy(ShapedTextDataFallback *p_shaped) {
  2751. ShapedTextDataFallback *parent = shaped_owner.get_or_null(p_shaped->parent);
  2752. for (const KeyValue<Variant, ShapedTextDataFallback::EmbeddedObject> &E : parent->objects) {
  2753. if (E.value.start >= p_shaped->start && E.value.start < p_shaped->end) {
  2754. p_shaped->objects[E.key] = E.value;
  2755. }
  2756. }
  2757. for (int i = p_shaped->first_span; i <= p_shaped->last_span; i++) {
  2758. ShapedTextDataFallback::Span span = parent->spans[i];
  2759. span.start = MAX(p_shaped->start, span.start);
  2760. span.end = MIN(p_shaped->end, span.end);
  2761. p_shaped->spans.push_back(span);
  2762. }
  2763. p_shaped->first_span = 0;
  2764. p_shaped->last_span = 0;
  2765. p_shaped->parent = RID();
  2766. }
  2767. RID TextServerFallback::_create_shaped_text(TextServer::Direction p_direction, TextServer::Orientation p_orientation) {
  2768. _THREAD_SAFE_METHOD_
  2769. ERR_FAIL_COND_V_MSG(p_direction == DIRECTION_INHERITED, RID(), "Invalid text direction.");
  2770. ShapedTextDataFallback *sd = memnew(ShapedTextDataFallback);
  2771. sd->direction = p_direction;
  2772. sd->orientation = p_orientation;
  2773. return shaped_owner.make_rid(sd);
  2774. }
  2775. void TextServerFallback::_shaped_text_clear(const RID &p_shaped) {
  2776. ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  2777. ERR_FAIL_NULL(sd);
  2778. MutexLock lock(sd->mutex);
  2779. sd->parent = RID();
  2780. sd->start = 0;
  2781. sd->end = 0;
  2782. sd->text = String();
  2783. sd->spans.clear();
  2784. sd->first_span = 0;
  2785. sd->last_span = 0;
  2786. sd->objects.clear();
  2787. invalidate(sd);
  2788. }
  2789. void TextServerFallback::_shaped_text_set_direction(const RID &p_shaped, TextServer::Direction p_direction) {
  2790. ERR_FAIL_COND_MSG(p_direction == DIRECTION_INHERITED, "Invalid text direction.");
  2791. if (p_direction == DIRECTION_RTL) {
  2792. ERR_PRINT_ONCE("Right-to-left layout is not supported by this text server.");
  2793. }
  2794. }
  2795. TextServer::Direction TextServerFallback::_shaped_text_get_direction(const RID &p_shaped) const {
  2796. return TextServer::DIRECTION_LTR;
  2797. }
  2798. TextServer::Direction TextServerFallback::_shaped_text_get_inferred_direction(const RID &p_shaped) const {
  2799. return TextServer::DIRECTION_LTR;
  2800. }
  2801. void TextServerFallback::_shaped_text_set_custom_punctuation(const RID &p_shaped, const String &p_punct) {
  2802. _THREAD_SAFE_METHOD_
  2803. ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  2804. ERR_FAIL_NULL(sd);
  2805. if (sd->custom_punct != p_punct) {
  2806. if (sd->parent != RID()) {
  2807. full_copy(sd);
  2808. }
  2809. sd->custom_punct = p_punct;
  2810. invalidate(sd);
  2811. }
  2812. }
  2813. String TextServerFallback::_shaped_text_get_custom_punctuation(const RID &p_shaped) const {
  2814. _THREAD_SAFE_METHOD_
  2815. const ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  2816. ERR_FAIL_NULL_V(sd, String());
  2817. return sd->custom_punct;
  2818. }
  2819. void TextServerFallback::_shaped_text_set_custom_ellipsis(const RID &p_shaped, int64_t p_char) {
  2820. _THREAD_SAFE_METHOD_
  2821. ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  2822. ERR_FAIL_NULL(sd);
  2823. sd->el_char = p_char;
  2824. }
  2825. int64_t TextServerFallback::_shaped_text_get_custom_ellipsis(const RID &p_shaped) const {
  2826. _THREAD_SAFE_METHOD_
  2827. const ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  2828. ERR_FAIL_NULL_V(sd, 0);
  2829. return sd->el_char;
  2830. }
  2831. void TextServerFallback::_shaped_text_set_orientation(const RID &p_shaped, TextServer::Orientation p_orientation) {
  2832. ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  2833. ERR_FAIL_NULL(sd);
  2834. MutexLock lock(sd->mutex);
  2835. if (sd->orientation != p_orientation) {
  2836. if (sd->parent != RID()) {
  2837. full_copy(sd);
  2838. }
  2839. sd->orientation = p_orientation;
  2840. invalidate(sd);
  2841. }
  2842. }
  2843. void TextServerFallback::_shaped_text_set_bidi_override(const RID &p_shaped, const Array &p_override) {
  2844. // No BiDi support, ignore.
  2845. }
  2846. TextServer::Orientation TextServerFallback::_shaped_text_get_orientation(const RID &p_shaped) const {
  2847. const ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  2848. ERR_FAIL_NULL_V(sd, TextServer::ORIENTATION_HORIZONTAL);
  2849. MutexLock lock(sd->mutex);
  2850. return sd->orientation;
  2851. }
  2852. void TextServerFallback::_shaped_text_set_preserve_invalid(const RID &p_shaped, bool p_enabled) {
  2853. ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  2854. MutexLock lock(sd->mutex);
  2855. ERR_FAIL_NULL(sd);
  2856. if (sd->preserve_invalid != p_enabled) {
  2857. if (sd->parent != RID()) {
  2858. full_copy(sd);
  2859. }
  2860. sd->preserve_invalid = p_enabled;
  2861. invalidate(sd);
  2862. }
  2863. }
  2864. bool TextServerFallback::_shaped_text_get_preserve_invalid(const RID &p_shaped) const {
  2865. const ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  2866. ERR_FAIL_NULL_V(sd, false);
  2867. MutexLock lock(sd->mutex);
  2868. return sd->preserve_invalid;
  2869. }
  2870. void TextServerFallback::_shaped_text_set_preserve_control(const RID &p_shaped, bool p_enabled) {
  2871. ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  2872. ERR_FAIL_NULL(sd);
  2873. MutexLock lock(sd->mutex);
  2874. if (sd->preserve_control != p_enabled) {
  2875. if (sd->parent != RID()) {
  2876. full_copy(sd);
  2877. }
  2878. sd->preserve_control = p_enabled;
  2879. invalidate(sd);
  2880. }
  2881. }
  2882. bool TextServerFallback::_shaped_text_get_preserve_control(const RID &p_shaped) const {
  2883. const ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  2884. ERR_FAIL_NULL_V(sd, false);
  2885. MutexLock lock(sd->mutex);
  2886. return sd->preserve_control;
  2887. }
  2888. void TextServerFallback::_shaped_text_set_spacing(const RID &p_shaped, SpacingType p_spacing, int64_t p_value) {
  2889. ERR_FAIL_INDEX((int)p_spacing, 4);
  2890. ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  2891. ERR_FAIL_NULL(sd);
  2892. MutexLock lock(sd->mutex);
  2893. if (sd->extra_spacing[p_spacing] != p_value) {
  2894. if (sd->parent != RID()) {
  2895. full_copy(sd);
  2896. }
  2897. sd->extra_spacing[p_spacing] = p_value;
  2898. invalidate(sd);
  2899. }
  2900. }
  2901. int64_t TextServerFallback::_shaped_text_get_spacing(const RID &p_shaped, SpacingType p_spacing) const {
  2902. ERR_FAIL_INDEX_V((int)p_spacing, 4, 0);
  2903. const ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  2904. ERR_FAIL_NULL_V(sd, 0);
  2905. MutexLock lock(sd->mutex);
  2906. return sd->extra_spacing[p_spacing];
  2907. }
  2908. int64_t TextServerFallback::_shaped_get_span_count(const RID &p_shaped) const {
  2909. ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  2910. ERR_FAIL_NULL_V(sd, 0);
  2911. if (sd->parent != RID()) {
  2912. return sd->last_span - sd->first_span + 1;
  2913. } else {
  2914. return sd->spans.size();
  2915. }
  2916. }
  2917. Variant TextServerFallback::_shaped_get_span_meta(const RID &p_shaped, int64_t p_index) const {
  2918. ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  2919. ERR_FAIL_NULL_V(sd, Variant());
  2920. if (sd->parent != RID()) {
  2921. ShapedTextDataFallback *parent_sd = shaped_owner.get_or_null(sd->parent);
  2922. ERR_FAIL_COND_V(!parent_sd->valid.is_set(), Variant());
  2923. ERR_FAIL_INDEX_V(p_index + sd->first_span, parent_sd->spans.size(), Variant());
  2924. return parent_sd->spans[p_index + sd->first_span].meta;
  2925. } else {
  2926. ERR_FAIL_INDEX_V(p_index, sd->spans.size(), Variant());
  2927. return sd->spans[p_index].meta;
  2928. }
  2929. }
  2930. Variant TextServerFallback::_shaped_get_span_embedded_object(const RID &p_shaped, int64_t p_index) const {
  2931. ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  2932. ERR_FAIL_NULL_V(sd, Variant());
  2933. if (sd->parent != RID()) {
  2934. ShapedTextDataFallback *parent_sd = shaped_owner.get_or_null(sd->parent);
  2935. ERR_FAIL_COND_V(!parent_sd->valid.is_set(), Variant());
  2936. ERR_FAIL_INDEX_V(p_index + sd->first_span, parent_sd->spans.size(), Variant());
  2937. return parent_sd->spans[p_index + sd->first_span].embedded_key;
  2938. } else {
  2939. ERR_FAIL_INDEX_V(p_index, sd->spans.size(), Variant());
  2940. return sd->spans[p_index].embedded_key;
  2941. }
  2942. }
  2943. String TextServerFallback::_shaped_get_span_text(const RID &p_shaped, int64_t p_index) const {
  2944. ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  2945. ERR_FAIL_NULL_V(sd, String());
  2946. ShapedTextDataFallback *span_sd = sd;
  2947. if (sd->parent.is_valid()) {
  2948. span_sd = shaped_owner.get_or_null(sd->parent);
  2949. ERR_FAIL_NULL_V(span_sd, String());
  2950. }
  2951. ERR_FAIL_INDEX_V(p_index, span_sd->spans.size(), String());
  2952. return span_sd->text.substr(span_sd->spans[p_index].start, span_sd->spans[p_index].end - span_sd->spans[p_index].start);
  2953. }
  2954. Variant TextServerFallback::_shaped_get_span_object(const RID &p_shaped, int64_t p_index) const {
  2955. ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  2956. ERR_FAIL_NULL_V(sd, Variant());
  2957. ShapedTextDataFallback *span_sd = sd;
  2958. if (sd->parent.is_valid()) {
  2959. span_sd = shaped_owner.get_or_null(sd->parent);
  2960. ERR_FAIL_NULL_V(span_sd, Variant());
  2961. }
  2962. ERR_FAIL_INDEX_V(p_index, span_sd->spans.size(), Variant());
  2963. return span_sd->spans[p_index].embedded_key;
  2964. }
  2965. void TextServerFallback::_generate_runs(ShapedTextDataFallback *p_sd) const {
  2966. ERR_FAIL_NULL(p_sd);
  2967. p_sd->runs.clear();
  2968. ShapedTextDataFallback *span_sd = p_sd;
  2969. if (p_sd->parent.is_valid()) {
  2970. span_sd = shaped_owner.get_or_null(p_sd->parent);
  2971. ERR_FAIL_NULL(span_sd);
  2972. }
  2973. int sd_size = p_sd->glyphs.size();
  2974. Glyph *sd_gl = p_sd->glyphs.ptrw();
  2975. int span_count = span_sd->spans.size();
  2976. int span = -1;
  2977. int span_start = -1;
  2978. int span_end = -1;
  2979. TextRun run;
  2980. for (int i = 0; i < sd_size; i += sd_gl[i].count) {
  2981. const Glyph &gl = sd_gl[i];
  2982. if (gl.start < 0 || gl.end < 0) {
  2983. continue;
  2984. }
  2985. if (gl.start < span_start || gl.start >= span_end) {
  2986. span = -1;
  2987. span_start = -1;
  2988. span_end = -1;
  2989. for (int j = 0; j < span_count; j++) {
  2990. if (gl.start >= span_sd->spans[j].start && gl.end <= span_sd->spans[j].end) {
  2991. span = j;
  2992. span_start = span_sd->spans[j].start;
  2993. span_end = span_sd->spans[j].end;
  2994. break;
  2995. }
  2996. }
  2997. }
  2998. if (run.font_rid != gl.font_rid || run.font_size != gl.font_size || run.span_index != span) {
  2999. if (run.span_index >= 0) {
  3000. p_sd->runs.push_back(run);
  3001. }
  3002. run.range = Vector2i(gl.start, gl.end);
  3003. run.font_rid = gl.font_rid;
  3004. run.font_size = gl.font_size;
  3005. run.span_index = span;
  3006. }
  3007. run.range.x = MIN(run.range.x, gl.start);
  3008. run.range.y = MAX(run.range.y, gl.end);
  3009. }
  3010. if (run.span_index >= 0) {
  3011. p_sd->runs.push_back(run);
  3012. }
  3013. p_sd->runs_dirty = false;
  3014. }
  3015. int64_t TextServerFallback::_shaped_get_run_count(const RID &p_shaped) const {
  3016. ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  3017. ERR_FAIL_NULL_V(sd, 0);
  3018. MutexLock lock(sd->mutex);
  3019. if (!sd->valid.is_set()) {
  3020. const_cast<TextServerFallback *>(this)->_shaped_text_shape(p_shaped);
  3021. }
  3022. if (sd->runs_dirty) {
  3023. _generate_runs(sd);
  3024. }
  3025. return sd->runs.size();
  3026. }
  3027. String TextServerFallback::_shaped_get_run_text(const RID &p_shaped, int64_t p_index) const {
  3028. ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  3029. ERR_FAIL_NULL_V(sd, String());
  3030. MutexLock lock(sd->mutex);
  3031. if (!sd->valid.is_set()) {
  3032. const_cast<TextServerFallback *>(this)->_shaped_text_shape(p_shaped);
  3033. }
  3034. if (sd->runs_dirty) {
  3035. _generate_runs(sd);
  3036. }
  3037. ERR_FAIL_INDEX_V(p_index, sd->runs.size(), String());
  3038. return sd->text.substr(sd->runs[p_index].range.x - sd->start, sd->runs[p_index].range.y - sd->runs[p_index].range.x);
  3039. }
  3040. Vector2i TextServerFallback::_shaped_get_run_range(const RID &p_shaped, int64_t p_index) const {
  3041. ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  3042. ERR_FAIL_NULL_V(sd, Vector2i());
  3043. MutexLock lock(sd->mutex);
  3044. if (!sd->valid.is_set()) {
  3045. const_cast<TextServerFallback *>(this)->_shaped_text_shape(p_shaped);
  3046. }
  3047. if (sd->runs_dirty) {
  3048. _generate_runs(sd);
  3049. }
  3050. ERR_FAIL_INDEX_V(p_index, sd->runs.size(), Vector2i());
  3051. return sd->runs[p_index].range;
  3052. }
  3053. RID TextServerFallback::_shaped_get_run_font_rid(const RID &p_shaped, int64_t p_index) const {
  3054. ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  3055. ERR_FAIL_NULL_V(sd, RID());
  3056. MutexLock lock(sd->mutex);
  3057. if (!sd->valid.is_set()) {
  3058. const_cast<TextServerFallback *>(this)->_shaped_text_shape(p_shaped);
  3059. }
  3060. if (sd->runs_dirty) {
  3061. _generate_runs(sd);
  3062. }
  3063. ERR_FAIL_INDEX_V(p_index, sd->runs.size(), RID());
  3064. return sd->runs[p_index].font_rid;
  3065. }
  3066. int TextServerFallback::_shaped_get_run_font_size(const RID &p_shaped, int64_t p_index) const {
  3067. ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  3068. ERR_FAIL_NULL_V(sd, 0);
  3069. MutexLock lock(sd->mutex);
  3070. if (!sd->valid.is_set()) {
  3071. const_cast<TextServerFallback *>(this)->_shaped_text_shape(p_shaped);
  3072. }
  3073. if (sd->runs_dirty) {
  3074. _generate_runs(sd);
  3075. }
  3076. ERR_FAIL_INDEX_V(p_index, sd->runs.size(), 0);
  3077. return sd->runs[p_index].font_size;
  3078. }
  3079. String TextServerFallback::_shaped_get_run_language(const RID &p_shaped, int64_t p_index) const {
  3080. ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  3081. ERR_FAIL_NULL_V(sd, String());
  3082. MutexLock lock(sd->mutex);
  3083. if (!sd->valid.is_set()) {
  3084. const_cast<TextServerFallback *>(this)->_shaped_text_shape(p_shaped);
  3085. }
  3086. if (sd->runs_dirty) {
  3087. _generate_runs(sd);
  3088. }
  3089. ERR_FAIL_INDEX_V(p_index, sd->runs.size(), String());
  3090. int span_idx = sd->runs[p_index].span_index;
  3091. ShapedTextDataFallback *span_sd = sd;
  3092. if (sd->parent.is_valid()) {
  3093. span_sd = shaped_owner.get_or_null(sd->parent);
  3094. ERR_FAIL_NULL_V(span_sd, String());
  3095. }
  3096. ERR_FAIL_INDEX_V(span_idx, span_sd->spans.size(), String());
  3097. return span_sd->spans[span_idx].language;
  3098. }
  3099. TextServer::Direction TextServerFallback::_shaped_get_run_direction(const RID &p_shaped, int64_t p_index) const {
  3100. ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  3101. ERR_FAIL_NULL_V(sd, TextServer::DIRECTION_LTR);
  3102. MutexLock lock(sd->mutex);
  3103. if (!sd->valid.is_set()) {
  3104. const_cast<TextServerFallback *>(this)->_shaped_text_shape(p_shaped);
  3105. }
  3106. if (sd->runs_dirty) {
  3107. _generate_runs(sd);
  3108. }
  3109. ERR_FAIL_INDEX_V(p_index, sd->runs.size(), TextServer::DIRECTION_LTR);
  3110. return TextServer::DIRECTION_LTR;
  3111. }
  3112. Variant TextServerFallback::_shaped_get_run_object(const RID &p_shaped, int64_t p_index) const {
  3113. ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  3114. ERR_FAIL_NULL_V(sd, Variant());
  3115. MutexLock lock(sd->mutex);
  3116. if (!sd->valid.is_set()) {
  3117. const_cast<TextServerFallback *>(this)->_shaped_text_shape(p_shaped);
  3118. }
  3119. if (sd->runs_dirty) {
  3120. _generate_runs(sd);
  3121. }
  3122. ERR_FAIL_INDEX_V(p_index, sd->runs.size(), Variant());
  3123. int span_idx = sd->runs[p_index].span_index;
  3124. ShapedTextDataFallback *span_sd = sd;
  3125. if (sd->parent.is_valid()) {
  3126. span_sd = shaped_owner.get_or_null(sd->parent);
  3127. ERR_FAIL_NULL_V(span_sd, Variant());
  3128. }
  3129. ERR_FAIL_INDEX_V(span_idx, span_sd->spans.size(), Variant());
  3130. return span_sd->spans[span_idx].embedded_key;
  3131. }
  3132. 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) {
  3133. ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  3134. ERR_FAIL_NULL(sd);
  3135. if (sd->parent != RID()) {
  3136. full_copy(sd);
  3137. }
  3138. ERR_FAIL_INDEX(p_index, sd->spans.size());
  3139. ShapedTextDataFallback::Span &span = sd->spans.ptrw()[p_index];
  3140. span.fonts.clear();
  3141. // Pre-sort fonts, push fonts with the language support first.
  3142. Array fonts_no_match;
  3143. int font_count = p_fonts.size();
  3144. for (int i = 0; i < font_count; i++) {
  3145. if (_font_is_language_supported(p_fonts[i], span.language)) {
  3146. span.fonts.push_back(p_fonts[i]);
  3147. } else {
  3148. fonts_no_match.push_back(p_fonts[i]);
  3149. }
  3150. }
  3151. span.fonts.append_array(fonts_no_match);
  3152. span.font_size = p_size;
  3153. span.features = p_opentype_features;
  3154. sd->valid.clear();
  3155. }
  3156. 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) {
  3157. ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  3158. ERR_FAIL_NULL_V(sd, false);
  3159. MutexLock lock(sd->mutex);
  3160. ERR_FAIL_COND_V(p_size <= 0, false);
  3161. for (int i = 0; i < p_fonts.size(); i++) {
  3162. ERR_FAIL_NULL_V(_get_font_data(p_fonts[i]), false);
  3163. }
  3164. if (p_text.is_empty()) {
  3165. return true;
  3166. }
  3167. if (sd->parent != RID()) {
  3168. full_copy(sd);
  3169. }
  3170. ShapedTextDataFallback::Span span;
  3171. span.start = sd->text.length();
  3172. span.end = span.start + p_text.length();
  3173. // Pre-sort fonts, push fonts with the language support first.
  3174. Array fonts_no_match;
  3175. int font_count = p_fonts.size();
  3176. if (font_count > 0) {
  3177. span.fonts.push_back(p_fonts[0]);
  3178. }
  3179. for (int i = 1; i < font_count; i++) {
  3180. if (_font_is_language_supported(p_fonts[i], p_language)) {
  3181. span.fonts.push_back(p_fonts[i]);
  3182. } else {
  3183. fonts_no_match.push_back(p_fonts[i]);
  3184. }
  3185. }
  3186. span.fonts.append_array(fonts_no_match);
  3187. ERR_FAIL_COND_V(span.fonts.is_empty(), false);
  3188. span.font_size = p_size;
  3189. span.language = p_language;
  3190. span.meta = p_meta;
  3191. sd->spans.push_back(span);
  3192. sd->text = sd->text + p_text;
  3193. sd->end += p_text.length();
  3194. invalidate(sd);
  3195. return true;
  3196. }
  3197. 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) {
  3198. ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  3199. ERR_FAIL_NULL_V(sd, false);
  3200. MutexLock lock(sd->mutex);
  3201. ERR_FAIL_COND_V(p_key == Variant(), false);
  3202. ERR_FAIL_COND_V(sd->objects.has(p_key), false);
  3203. if (sd->parent != RID()) {
  3204. full_copy(sd);
  3205. }
  3206. ShapedTextDataFallback::Span span;
  3207. span.start = sd->start + sd->text.length();
  3208. span.end = span.start + p_length;
  3209. span.embedded_key = p_key;
  3210. ShapedTextDataFallback::EmbeddedObject obj;
  3211. obj.inline_align = p_inline_align;
  3212. obj.rect.size = p_size;
  3213. obj.start = span.start;
  3214. obj.end = span.end;
  3215. obj.baseline = p_baseline;
  3216. sd->spans.push_back(span);
  3217. sd->text = sd->text + String::chr(0xfffc).repeat(p_length);
  3218. sd->end += p_length;
  3219. sd->objects[p_key] = obj;
  3220. invalidate(sd);
  3221. return true;
  3222. }
  3223. String TextServerFallback::_shaped_get_text(const RID &p_shaped) const {
  3224. const ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  3225. ERR_FAIL_NULL_V(sd, String());
  3226. return sd->text;
  3227. }
  3228. 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) {
  3229. ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  3230. ERR_FAIL_NULL_V(sd, false);
  3231. MutexLock lock(sd->mutex);
  3232. ERR_FAIL_COND_V(!sd->objects.has(p_key), false);
  3233. sd->objects[p_key].rect.size = p_size;
  3234. sd->objects[p_key].inline_align = p_inline_align;
  3235. sd->objects[p_key].baseline = p_baseline;
  3236. if (sd->valid.is_set()) {
  3237. // Recalc string metrics.
  3238. sd->ascent = 0;
  3239. sd->descent = 0;
  3240. sd->width = 0;
  3241. sd->upos = 0;
  3242. sd->uthk = 0;
  3243. Vector<ShapedTextDataFallback::Span> &spans = sd->spans;
  3244. if (sd->parent != RID()) {
  3245. ShapedTextDataFallback *parent_sd = shaped_owner.get_or_null(sd->parent);
  3246. ERR_FAIL_COND_V(!parent_sd->valid.is_set(), false);
  3247. spans = parent_sd->spans;
  3248. }
  3249. int sd_size = sd->glyphs.size();
  3250. int span_size = spans.size();
  3251. for (int i = 0; i < sd_size; i++) {
  3252. Glyph gl = sd->glyphs[i];
  3253. Variant key;
  3254. 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) {
  3255. key = spans[gl.span_index + sd->first_span].embedded_key;
  3256. }
  3257. if (key != Variant()) {
  3258. if (sd->orientation == ORIENTATION_HORIZONTAL) {
  3259. sd->objects[key].rect.position.x = sd->width;
  3260. sd->width += sd->objects[key].rect.size.x;
  3261. sd->glyphs.write[i].advance = sd->objects[key].rect.size.x;
  3262. } else {
  3263. sd->objects[key].rect.position.y = sd->width;
  3264. sd->width += sd->objects[key].rect.size.y;
  3265. sd->glyphs.write[i].advance = sd->objects[key].rect.size.y;
  3266. }
  3267. } else {
  3268. if (gl.font_rid.is_valid()) {
  3269. if (sd->orientation == ORIENTATION_HORIZONTAL) {
  3270. sd->ascent = MAX(sd->ascent, _font_get_ascent(gl.font_rid, gl.font_size) + _font_get_spacing(gl.font_rid, SPACING_TOP));
  3271. sd->descent = MAX(sd->descent, _font_get_descent(gl.font_rid, gl.font_size) + _font_get_spacing(gl.font_rid, SPACING_BOTTOM));
  3272. } else {
  3273. sd->ascent = MAX(sd->ascent, Math::round(_font_get_glyph_advance(gl.font_rid, gl.font_size, gl.index).x * 0.5));
  3274. sd->descent = MAX(sd->descent, Math::round(_font_get_glyph_advance(gl.font_rid, gl.font_size, gl.index).x * 0.5));
  3275. }
  3276. sd->upos = MAX(sd->upos, _font_get_underline_position(gl.font_rid, gl.font_size));
  3277. sd->uthk = MAX(sd->uthk, _font_get_underline_thickness(gl.font_rid, gl.font_size));
  3278. } else if (sd->preserve_invalid || (sd->preserve_control && is_control(gl.index))) {
  3279. // Glyph not found, replace with hex code box.
  3280. if (sd->orientation == ORIENTATION_HORIZONTAL) {
  3281. sd->ascent = MAX(sd->ascent, get_hex_code_box_size(gl.font_size, gl.index).y * 0.85);
  3282. sd->descent = MAX(sd->descent, get_hex_code_box_size(gl.font_size, gl.index).y * 0.15);
  3283. } else {
  3284. sd->ascent = MAX(sd->ascent, Math::round(get_hex_code_box_size(gl.font_size, gl.index).x * 0.5));
  3285. sd->descent = MAX(sd->descent, Math::round(get_hex_code_box_size(gl.font_size, gl.index).x * 0.5));
  3286. }
  3287. }
  3288. sd->width += gl.advance * gl.repeat;
  3289. }
  3290. }
  3291. _realign(sd);
  3292. }
  3293. return true;
  3294. }
  3295. void TextServerFallback::_realign(ShapedTextDataFallback *p_sd) const {
  3296. // Align embedded objects to baseline.
  3297. double full_ascent = p_sd->ascent;
  3298. double full_descent = p_sd->descent;
  3299. for (KeyValue<Variant, ShapedTextDataFallback::EmbeddedObject> &E : p_sd->objects) {
  3300. if ((E.value.start >= p_sd->start) && (E.value.start < p_sd->end)) {
  3301. if (p_sd->orientation == ORIENTATION_HORIZONTAL) {
  3302. switch (E.value.inline_align & INLINE_ALIGNMENT_TEXT_MASK) {
  3303. case INLINE_ALIGNMENT_TO_TOP: {
  3304. E.value.rect.position.y = -p_sd->ascent;
  3305. } break;
  3306. case INLINE_ALIGNMENT_TO_CENTER: {
  3307. E.value.rect.position.y = (-p_sd->ascent + p_sd->descent) / 2;
  3308. } break;
  3309. case INLINE_ALIGNMENT_TO_BASELINE: {
  3310. E.value.rect.position.y = 0;
  3311. } break;
  3312. case INLINE_ALIGNMENT_TO_BOTTOM: {
  3313. E.value.rect.position.y = p_sd->descent;
  3314. } break;
  3315. }
  3316. switch (E.value.inline_align & INLINE_ALIGNMENT_IMAGE_MASK) {
  3317. case INLINE_ALIGNMENT_BOTTOM_TO: {
  3318. E.value.rect.position.y -= E.value.rect.size.y;
  3319. } break;
  3320. case INLINE_ALIGNMENT_CENTER_TO: {
  3321. E.value.rect.position.y -= E.value.rect.size.y / 2;
  3322. } break;
  3323. case INLINE_ALIGNMENT_BASELINE_TO: {
  3324. E.value.rect.position.y -= E.value.baseline;
  3325. } break;
  3326. case INLINE_ALIGNMENT_TOP_TO: {
  3327. // NOP
  3328. } break;
  3329. }
  3330. full_ascent = MAX(full_ascent, -E.value.rect.position.y);
  3331. full_descent = MAX(full_descent, E.value.rect.position.y + E.value.rect.size.y);
  3332. } else {
  3333. switch (E.value.inline_align & INLINE_ALIGNMENT_TEXT_MASK) {
  3334. case INLINE_ALIGNMENT_TO_TOP: {
  3335. E.value.rect.position.x = -p_sd->ascent;
  3336. } break;
  3337. case INLINE_ALIGNMENT_TO_CENTER: {
  3338. E.value.rect.position.x = (-p_sd->ascent + p_sd->descent) / 2;
  3339. } break;
  3340. case INLINE_ALIGNMENT_TO_BASELINE: {
  3341. E.value.rect.position.x = 0;
  3342. } break;
  3343. case INLINE_ALIGNMENT_TO_BOTTOM: {
  3344. E.value.rect.position.x = p_sd->descent;
  3345. } break;
  3346. }
  3347. switch (E.value.inline_align & INLINE_ALIGNMENT_IMAGE_MASK) {
  3348. case INLINE_ALIGNMENT_BOTTOM_TO: {
  3349. E.value.rect.position.x -= E.value.rect.size.x;
  3350. } break;
  3351. case INLINE_ALIGNMENT_CENTER_TO: {
  3352. E.value.rect.position.x -= E.value.rect.size.x / 2;
  3353. } break;
  3354. case INLINE_ALIGNMENT_BASELINE_TO: {
  3355. E.value.rect.position.x -= E.value.baseline;
  3356. } break;
  3357. case INLINE_ALIGNMENT_TOP_TO: {
  3358. // NOP
  3359. } break;
  3360. }
  3361. full_ascent = MAX(full_ascent, -E.value.rect.position.x);
  3362. full_descent = MAX(full_descent, E.value.rect.position.x + E.value.rect.size.x);
  3363. }
  3364. }
  3365. }
  3366. p_sd->ascent = full_ascent;
  3367. p_sd->descent = full_descent;
  3368. }
  3369. RID TextServerFallback::_shaped_text_substr(const RID &p_shaped, int64_t p_start, int64_t p_length) const {
  3370. _THREAD_SAFE_METHOD_
  3371. const ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  3372. ERR_FAIL_NULL_V(sd, RID());
  3373. MutexLock lock(sd->mutex);
  3374. if (sd->parent != RID()) {
  3375. return _shaped_text_substr(sd->parent, p_start, p_length);
  3376. }
  3377. if (!sd->valid.is_set()) {
  3378. const_cast<TextServerFallback *>(this)->_shaped_text_shape(p_shaped);
  3379. }
  3380. ERR_FAIL_COND_V(p_start < 0 || p_length < 0, RID());
  3381. ERR_FAIL_COND_V(sd->start > p_start || sd->end < p_start, RID());
  3382. ERR_FAIL_COND_V(sd->end < p_start + p_length, RID());
  3383. ShapedTextDataFallback *new_sd = memnew(ShapedTextDataFallback);
  3384. new_sd->parent = p_shaped;
  3385. new_sd->start = p_start;
  3386. new_sd->end = p_start + p_length;
  3387. new_sd->orientation = sd->orientation;
  3388. new_sd->direction = sd->direction;
  3389. new_sd->custom_punct = sd->custom_punct;
  3390. new_sd->para_direction = sd->para_direction;
  3391. new_sd->line_breaks_valid = sd->line_breaks_valid;
  3392. new_sd->justification_ops_valid = sd->justification_ops_valid;
  3393. new_sd->sort_valid = false;
  3394. new_sd->upos = sd->upos;
  3395. new_sd->uthk = sd->uthk;
  3396. for (int i = 0; i < TextServer::SPACING_MAX; i++) {
  3397. new_sd->extra_spacing[i] = sd->extra_spacing[i];
  3398. }
  3399. if (p_length > 0) {
  3400. new_sd->text = sd->text.substr(p_start - sd->start, p_length);
  3401. int span_size = sd->spans.size();
  3402. new_sd->first_span = 0;
  3403. new_sd->last_span = span_size - 1;
  3404. for (int i = 0; i < span_size; i++) {
  3405. const ShapedTextDataFallback::Span &span = sd->spans[i];
  3406. if (span.end <= p_start) {
  3407. new_sd->first_span = i + 1;
  3408. } else if (span.start >= p_start + p_length) {
  3409. new_sd->last_span = i - 1;
  3410. break;
  3411. }
  3412. }
  3413. int sd_size = sd->glyphs.size();
  3414. const Glyph *sd_glyphs = sd->glyphs.ptr();
  3415. for (int i = 0; i < sd_size; i++) {
  3416. if ((sd_glyphs[i].start >= new_sd->start) && (sd_glyphs[i].end <= new_sd->end)) {
  3417. Glyph gl = sd_glyphs[i];
  3418. if (gl.span_index >= 0) {
  3419. gl.span_index -= new_sd->first_span;
  3420. }
  3421. if (gl.end == p_start + p_length && ((gl.flags & GRAPHEME_IS_SOFT_HYPHEN) == GRAPHEME_IS_SOFT_HYPHEN)) {
  3422. gl.index = 0x00ad;
  3423. gl.advance = font_get_glyph_advance(gl.font_rid, gl.font_size, 0x00ad).x;
  3424. }
  3425. if ((gl.flags & GRAPHEME_IS_EMBEDDED_OBJECT) == GRAPHEME_IS_EMBEDDED_OBJECT && gl.span_index + new_sd->first_span >= 0 && gl.span_index + new_sd->first_span < span_size) {
  3426. Variant key = sd->spans[gl.span_index + new_sd->first_span].embedded_key;
  3427. if (key != Variant()) {
  3428. ShapedTextDataFallback::EmbeddedObject obj = sd->objects[key];
  3429. if (new_sd->orientation == ORIENTATION_HORIZONTAL) {
  3430. obj.rect.position.x = new_sd->width;
  3431. new_sd->width += obj.rect.size.x;
  3432. } else {
  3433. obj.rect.position.y = new_sd->width;
  3434. new_sd->width += obj.rect.size.y;
  3435. }
  3436. new_sd->objects[key] = obj;
  3437. }
  3438. } else {
  3439. if (gl.font_rid.is_valid()) {
  3440. if (new_sd->orientation == ORIENTATION_HORIZONTAL) {
  3441. new_sd->ascent = MAX(new_sd->ascent, _font_get_ascent(gl.font_rid, gl.font_size) + _font_get_spacing(gl.font_rid, SPACING_TOP));
  3442. new_sd->descent = MAX(new_sd->descent, _font_get_descent(gl.font_rid, gl.font_size) + _font_get_spacing(gl.font_rid, SPACING_BOTTOM));
  3443. } else {
  3444. new_sd->ascent = MAX(new_sd->ascent, Math::round(_font_get_glyph_advance(gl.font_rid, gl.font_size, gl.index).x * 0.5));
  3445. new_sd->descent = MAX(new_sd->descent, Math::round(_font_get_glyph_advance(gl.font_rid, gl.font_size, gl.index).x * 0.5));
  3446. }
  3447. } else if (new_sd->preserve_invalid || (new_sd->preserve_control && is_control(gl.index))) {
  3448. // Glyph not found, replace with hex code box.
  3449. if (new_sd->orientation == ORIENTATION_HORIZONTAL) {
  3450. new_sd->ascent = MAX(new_sd->ascent, get_hex_code_box_size(gl.font_size, gl.index).y * 0.85);
  3451. new_sd->descent = MAX(new_sd->descent, get_hex_code_box_size(gl.font_size, gl.index).y * 0.15);
  3452. } else {
  3453. new_sd->ascent = MAX(new_sd->ascent, Math::round(get_hex_code_box_size(gl.font_size, gl.index).x * 0.5));
  3454. new_sd->descent = MAX(new_sd->descent, Math::round(get_hex_code_box_size(gl.font_size, gl.index).x * 0.5));
  3455. }
  3456. }
  3457. new_sd->width += gl.advance * gl.repeat;
  3458. }
  3459. new_sd->glyphs.push_back(gl);
  3460. }
  3461. }
  3462. _realign(new_sd);
  3463. }
  3464. new_sd->valid.set();
  3465. return shaped_owner.make_rid(new_sd);
  3466. }
  3467. RID TextServerFallback::_shaped_text_get_parent(const RID &p_shaped) const {
  3468. ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  3469. ERR_FAIL_NULL_V(sd, RID());
  3470. MutexLock lock(sd->mutex);
  3471. return sd->parent;
  3472. }
  3473. double TextServerFallback::_shaped_text_fit_to_width(const RID &p_shaped, double p_width, BitField<JustificationFlag> p_jst_flags) {
  3474. ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  3475. ERR_FAIL_NULL_V(sd, 0.0);
  3476. MutexLock lock(sd->mutex);
  3477. if (!sd->valid.is_set()) {
  3478. _shaped_text_shape(p_shaped);
  3479. }
  3480. if (!sd->justification_ops_valid) {
  3481. _shaped_text_update_justification_ops(p_shaped);
  3482. }
  3483. int start_pos = 0;
  3484. int end_pos = sd->glyphs.size() - 1;
  3485. if (p_jst_flags.has_flag(JUSTIFICATION_AFTER_LAST_TAB)) {
  3486. int start, end, delta;
  3487. if (sd->para_direction == DIRECTION_LTR) {
  3488. start = sd->glyphs.size() - 1;
  3489. end = -1;
  3490. delta = -1;
  3491. } else {
  3492. start = 0;
  3493. end = sd->glyphs.size();
  3494. delta = +1;
  3495. }
  3496. for (int i = start; i != end; i += delta) {
  3497. if ((sd->glyphs[i].flags & GRAPHEME_IS_TAB) == GRAPHEME_IS_TAB) {
  3498. if (sd->para_direction == DIRECTION_LTR) {
  3499. start_pos = i;
  3500. break;
  3501. } else {
  3502. end_pos = i;
  3503. break;
  3504. }
  3505. }
  3506. }
  3507. }
  3508. double justification_width;
  3509. if (p_jst_flags.has_flag(JUSTIFICATION_CONSTRAIN_ELLIPSIS)) {
  3510. if (sd->overrun_trim_data.trim_pos >= 0) {
  3511. end_pos = sd->overrun_trim_data.trim_pos;
  3512. justification_width = sd->width_trimmed;
  3513. } else {
  3514. return Math::ceil(sd->width);
  3515. }
  3516. } else {
  3517. justification_width = sd->width;
  3518. }
  3519. if (p_jst_flags.has_flag(JUSTIFICATION_TRIM_EDGE_SPACES)) {
  3520. // Trim spaces.
  3521. 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)) {
  3522. justification_width -= sd->glyphs[start_pos].advance * sd->glyphs[start_pos].repeat;
  3523. sd->glyphs.write[start_pos].advance = 0;
  3524. start_pos += sd->glyphs[start_pos].count;
  3525. }
  3526. 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)) {
  3527. justification_width -= sd->glyphs[end_pos].advance * sd->glyphs[end_pos].repeat;
  3528. sd->glyphs.write[end_pos].advance = 0;
  3529. end_pos -= sd->glyphs[end_pos].count;
  3530. }
  3531. } else {
  3532. // Skip breaks, but do not reset size.
  3533. 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)) {
  3534. start_pos += sd->glyphs[start_pos].count;
  3535. }
  3536. 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)) {
  3537. end_pos -= sd->glyphs[end_pos].count;
  3538. }
  3539. }
  3540. int space_count = 0;
  3541. for (int i = start_pos; i <= end_pos; i++) {
  3542. const Glyph &gl = sd->glyphs[i];
  3543. if (gl.count > 0) {
  3544. 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) {
  3545. space_count++;
  3546. }
  3547. }
  3548. }
  3549. if ((space_count > 0) && p_jst_flags.has_flag(JUSTIFICATION_WORD_BOUND)) {
  3550. double delta_width_per_space = (p_width - justification_width) / space_count;
  3551. for (int i = start_pos; i <= end_pos; i++) {
  3552. Glyph &gl = sd->glyphs.write[i];
  3553. if (gl.count > 0) {
  3554. 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) {
  3555. double old_adv = gl.advance;
  3556. gl.advance = MAX(gl.advance + delta_width_per_space, Math::round(0.1 * gl.font_size));
  3557. justification_width += (gl.advance - old_adv);
  3558. }
  3559. }
  3560. }
  3561. }
  3562. if (Math::floor(p_width) < Math::floor(justification_width)) {
  3563. sd->fit_width_minimum_reached = true;
  3564. }
  3565. if (!p_jst_flags.has_flag(JUSTIFICATION_CONSTRAIN_ELLIPSIS)) {
  3566. sd->width = justification_width;
  3567. }
  3568. return Math::ceil(justification_width);
  3569. }
  3570. double TextServerFallback::_shaped_text_tab_align(const RID &p_shaped, const PackedFloat32Array &p_tab_stops) {
  3571. ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  3572. ERR_FAIL_NULL_V(sd, 0.0);
  3573. MutexLock lock(sd->mutex);
  3574. if (!sd->valid.is_set()) {
  3575. _shaped_text_shape(p_shaped);
  3576. }
  3577. if (!sd->line_breaks_valid) {
  3578. _shaped_text_update_breaks(p_shaped);
  3579. }
  3580. for (int i = 0; i < p_tab_stops.size(); i++) {
  3581. if (p_tab_stops[i] <= 0) {
  3582. return 0.0;
  3583. }
  3584. }
  3585. int tab_index = 0;
  3586. double off = 0.0;
  3587. int start, end, delta;
  3588. if (sd->para_direction == DIRECTION_LTR) {
  3589. start = 0;
  3590. end = sd->glyphs.size();
  3591. delta = +1;
  3592. } else {
  3593. start = sd->glyphs.size() - 1;
  3594. end = -1;
  3595. delta = -1;
  3596. }
  3597. Glyph *gl = sd->glyphs.ptrw();
  3598. for (int i = start; i != end; i += delta) {
  3599. if ((gl[i].flags & GRAPHEME_IS_TAB) == GRAPHEME_IS_TAB) {
  3600. double tab_off = 0.0;
  3601. while (tab_off <= off) {
  3602. tab_off += p_tab_stops[tab_index];
  3603. tab_index++;
  3604. if (tab_index >= p_tab_stops.size()) {
  3605. tab_index = 0;
  3606. }
  3607. }
  3608. double old_adv = gl[i].advance;
  3609. gl[i].advance = tab_off - off;
  3610. sd->width += gl[i].advance - old_adv;
  3611. off = 0;
  3612. continue;
  3613. }
  3614. off += gl[i].advance * gl[i].repeat;
  3615. }
  3616. return 0.0;
  3617. }
  3618. bool TextServerFallback::_shaped_text_update_breaks(const RID &p_shaped) {
  3619. ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  3620. ERR_FAIL_NULL_V(sd, false);
  3621. MutexLock lock(sd->mutex);
  3622. if (!sd->valid.is_set()) {
  3623. _shaped_text_shape(p_shaped);
  3624. }
  3625. if (sd->line_breaks_valid) {
  3626. return true; // Nothing to do.
  3627. }
  3628. int sd_size = sd->glyphs.size();
  3629. Glyph *sd_glyphs = sd->glyphs.ptrw();
  3630. int c_punct_size = sd->custom_punct.length();
  3631. const char32_t *c_punct = sd->custom_punct.ptr();
  3632. for (int i = 0; i < sd_size; i++) {
  3633. if (sd_glyphs[i].count > 0) {
  3634. char32_t c = sd->text[sd_glyphs[i].start - sd->start];
  3635. char32_t c_next = i < sd_size ? sd->text[sd_glyphs[i].start - sd->start + 1] : 0x0000;
  3636. if (c_punct_size == 0) {
  3637. if (is_punct(c) && c != 0x005F && c != ' ') {
  3638. sd_glyphs[i].flags |= GRAPHEME_IS_PUNCTUATION;
  3639. }
  3640. } else {
  3641. for (int j = 0; j < c_punct_size; j++) {
  3642. if (c_punct[j] == c) {
  3643. sd_glyphs[i].flags |= GRAPHEME_IS_PUNCTUATION;
  3644. break;
  3645. }
  3646. }
  3647. }
  3648. if (is_underscore(c)) {
  3649. sd->glyphs.write[i].flags |= GRAPHEME_IS_UNDERSCORE;
  3650. }
  3651. if (is_whitespace(c) && !is_linebreak(c)) {
  3652. sd_glyphs[i].flags |= GRAPHEME_IS_SPACE;
  3653. if (c != 0x00A0 && c != 0x202F && c != 0x2060 && c != 0x2007) { // Skip for non-breaking space variants.
  3654. sd_glyphs[i].flags |= GRAPHEME_IS_BREAK_SOFT;
  3655. }
  3656. }
  3657. if (is_linebreak(c)) {
  3658. sd_glyphs[i].flags |= GRAPHEME_IS_SPACE;
  3659. if (c != 0x000D || c_next != 0x000A) { // Skip first hard break in CR-LF pair.
  3660. sd_glyphs[i].flags |= GRAPHEME_IS_BREAK_HARD;
  3661. }
  3662. }
  3663. if (c == 0x0009 || c == 0x000b) {
  3664. sd_glyphs[i].flags |= GRAPHEME_IS_TAB;
  3665. }
  3666. if (c == 0x00ad) {
  3667. sd_glyphs[i].flags |= GRAPHEME_IS_SOFT_HYPHEN;
  3668. }
  3669. i += (sd_glyphs[i].count - 1);
  3670. }
  3671. }
  3672. sd->line_breaks_valid = true;
  3673. return sd->line_breaks_valid;
  3674. }
  3675. bool TextServerFallback::_shaped_text_update_justification_ops(const RID &p_shaped) {
  3676. ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  3677. ERR_FAIL_NULL_V(sd, false);
  3678. MutexLock lock(sd->mutex);
  3679. if (!sd->valid.is_set()) {
  3680. _shaped_text_shape(p_shaped);
  3681. }
  3682. if (!sd->line_breaks_valid) {
  3683. _shaped_text_update_breaks(p_shaped);
  3684. }
  3685. sd->justification_ops_valid = true; // Not supported by fallback server.
  3686. return true;
  3687. }
  3688. RID TextServerFallback::_find_sys_font_for_text(const RID &p_fdef, const String &p_script_code, const String &p_language, const String &p_text) {
  3689. RID f;
  3690. // Try system fallback.
  3691. if (_font_is_allow_system_fallback(p_fdef)) {
  3692. String font_name = _font_get_name(p_fdef);
  3693. BitField<FontStyle> font_style = _font_get_style(p_fdef);
  3694. int font_weight = _font_get_weight(p_fdef);
  3695. int font_stretch = _font_get_stretch(p_fdef);
  3696. Dictionary dvar = _font_get_variation_coordinates(p_fdef);
  3697. static int64_t wgth_tag = name_to_tag("weight");
  3698. static int64_t wdth_tag = name_to_tag("width");
  3699. static int64_t ital_tag = name_to_tag("italic");
  3700. if (dvar.has(wgth_tag)) {
  3701. font_weight = dvar[wgth_tag].operator int();
  3702. }
  3703. if (dvar.has(wdth_tag)) {
  3704. font_stretch = dvar[wdth_tag].operator int();
  3705. }
  3706. if (dvar.has(ital_tag) && dvar[ital_tag].operator int() == 1) {
  3707. font_style.set_flag(TextServer::FONT_ITALIC);
  3708. }
  3709. String locale = (p_language.is_empty()) ? TranslationServer::get_singleton()->get_tool_locale() : p_language;
  3710. 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);
  3711. #ifdef GDEXTENSION
  3712. for (int fb = 0; fb < fallback_font_name.size(); fb++) {
  3713. const String &E = fallback_font_name[fb];
  3714. #elif defined(GODOT_MODULE)
  3715. for (const String &E : fallback_font_name) {
  3716. #endif
  3717. SystemFontKey key = SystemFontKey(E, font_style & TextServer::FONT_ITALIC, font_weight, font_stretch, p_fdef, this);
  3718. if (system_fonts.has(key)) {
  3719. const SystemFontCache &sysf_cache = system_fonts[key];
  3720. int best_score = 0;
  3721. int best_match = -1;
  3722. for (int face_idx = 0; face_idx < sysf_cache.var.size(); face_idx++) {
  3723. const SystemFontCacheRec &F = sysf_cache.var[face_idx];
  3724. if (unlikely(!_font_has_char(F.rid, p_text[0]))) {
  3725. continue;
  3726. }
  3727. BitField<FontStyle> style = _font_get_style(F.rid);
  3728. int weight = _font_get_weight(F.rid);
  3729. int stretch = _font_get_stretch(F.rid);
  3730. int score = (20 - Math::abs(weight - font_weight) / 50);
  3731. score += (20 - Math::abs(stretch - font_stretch) / 10);
  3732. if (bool(style & TextServer::FONT_ITALIC) == bool(font_style & TextServer::FONT_ITALIC)) {
  3733. score += 30;
  3734. }
  3735. if (score >= best_score) {
  3736. best_score = score;
  3737. best_match = face_idx;
  3738. }
  3739. if (best_score == 70) {
  3740. break;
  3741. }
  3742. }
  3743. if (best_match != -1) {
  3744. f = sysf_cache.var[best_match].rid;
  3745. }
  3746. }
  3747. if (!f.is_valid()) {
  3748. if (system_fonts.has(key)) {
  3749. const SystemFontCache &sysf_cache = system_fonts[key];
  3750. if (sysf_cache.max_var == sysf_cache.var.size()) {
  3751. // All subfonts already tested, skip.
  3752. continue;
  3753. }
  3754. }
  3755. if (!system_font_data.has(E)) {
  3756. system_font_data[E] = FileAccess::get_file_as_bytes(E);
  3757. }
  3758. const PackedByteArray &font_data = system_font_data[E];
  3759. SystemFontCacheRec sysf;
  3760. sysf.rid = _create_font();
  3761. _font_set_data_ptr(sysf.rid, font_data.ptr(), font_data.size());
  3762. if (!_font_validate(sysf.rid)) {
  3763. _free_rid(sysf.rid);
  3764. continue;
  3765. }
  3766. Dictionary var = dvar;
  3767. // Select matching style from collection.
  3768. int best_score = 0;
  3769. int best_match = -1;
  3770. for (int face_idx = 0; face_idx < _font_get_face_count(sysf.rid); face_idx++) {
  3771. _font_set_face_index(sysf.rid, face_idx);
  3772. if (unlikely(!_font_has_char(sysf.rid, p_text[0]))) {
  3773. continue;
  3774. }
  3775. BitField<FontStyle> style = _font_get_style(sysf.rid);
  3776. int weight = _font_get_weight(sysf.rid);
  3777. int stretch = _font_get_stretch(sysf.rid);
  3778. int score = (20 - Math::abs(weight - font_weight) / 50);
  3779. score += (20 - Math::abs(stretch - font_stretch) / 10);
  3780. if (bool(style & TextServer::FONT_ITALIC) == bool(font_style & TextServer::FONT_ITALIC)) {
  3781. score += 30;
  3782. }
  3783. if (score >= best_score) {
  3784. best_score = score;
  3785. best_match = face_idx;
  3786. }
  3787. if (best_score == 70) {
  3788. break;
  3789. }
  3790. }
  3791. if (best_match == -1) {
  3792. _free_rid(sysf.rid);
  3793. continue;
  3794. } else {
  3795. _font_set_face_index(sysf.rid, best_match);
  3796. }
  3797. sysf.index = best_match;
  3798. // If it's a variable font, apply weight, stretch and italic coordinates to match requested style.
  3799. if (best_score != 70) {
  3800. Dictionary ftr = _font_supported_variation_list(sysf.rid);
  3801. if (ftr.has(wdth_tag)) {
  3802. var[wdth_tag] = font_stretch;
  3803. _font_set_stretch(sysf.rid, font_stretch);
  3804. }
  3805. if (ftr.has(wgth_tag)) {
  3806. var[wgth_tag] = font_weight;
  3807. _font_set_weight(sysf.rid, font_weight);
  3808. }
  3809. if ((font_style & TextServer::FONT_ITALIC) && ftr.has(ital_tag)) {
  3810. var[ital_tag] = 1;
  3811. _font_set_style(sysf.rid, _font_get_style(sysf.rid) | TextServer::FONT_ITALIC);
  3812. }
  3813. }
  3814. _font_set_antialiasing(sysf.rid, key.antialiasing);
  3815. _font_set_disable_embedded_bitmaps(sysf.rid, key.disable_embedded_bitmaps);
  3816. _font_set_generate_mipmaps(sysf.rid, key.mipmaps);
  3817. _font_set_multichannel_signed_distance_field(sysf.rid, key.msdf);
  3818. _font_set_msdf_pixel_range(sysf.rid, key.msdf_range);
  3819. _font_set_msdf_size(sysf.rid, key.msdf_source_size);
  3820. _font_set_fixed_size(sysf.rid, key.fixed_size);
  3821. _font_set_force_autohinter(sysf.rid, key.force_autohinter);
  3822. _font_set_hinting(sysf.rid, key.hinting);
  3823. _font_set_subpixel_positioning(sysf.rid, key.subpixel_positioning);
  3824. _font_set_keep_rounding_remainders(sysf.rid, key.keep_rounding_remainders);
  3825. _font_set_variation_coordinates(sysf.rid, var);
  3826. _font_set_embolden(sysf.rid, key.embolden);
  3827. _font_set_transform(sysf.rid, key.transform);
  3828. _font_set_spacing(sysf.rid, SPACING_TOP, key.extra_spacing[SPACING_TOP]);
  3829. _font_set_spacing(sysf.rid, SPACING_BOTTOM, key.extra_spacing[SPACING_BOTTOM]);
  3830. _font_set_spacing(sysf.rid, SPACING_SPACE, key.extra_spacing[SPACING_SPACE]);
  3831. _font_set_spacing(sysf.rid, SPACING_GLYPH, key.extra_spacing[SPACING_GLYPH]);
  3832. if (system_fonts.has(key)) {
  3833. system_fonts[key].var.push_back(sysf);
  3834. } else {
  3835. SystemFontCache &sysf_cache = system_fonts[key];
  3836. sysf_cache.max_var = _font_get_face_count(sysf.rid);
  3837. sysf_cache.var.push_back(sysf);
  3838. }
  3839. f = sysf.rid;
  3840. }
  3841. break;
  3842. }
  3843. }
  3844. return f;
  3845. }
  3846. void TextServerFallback::_shaped_text_overrun_trim_to_width(const RID &p_shaped_line, double p_width, BitField<TextServer::TextOverrunFlag> p_trim_flags) {
  3847. ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped_line);
  3848. ERR_FAIL_NULL_MSG(sd, "ShapedTextDataFallback invalid.");
  3849. MutexLock lock(sd->mutex);
  3850. if (!sd->valid.is_set()) {
  3851. _shaped_text_shape(p_shaped_line);
  3852. }
  3853. sd->text_trimmed = false;
  3854. sd->overrun_trim_data.ellipsis_glyph_buf.clear();
  3855. bool add_ellipsis = p_trim_flags.has_flag(OVERRUN_ADD_ELLIPSIS);
  3856. bool cut_per_word = p_trim_flags.has_flag(OVERRUN_TRIM_WORD_ONLY);
  3857. bool enforce_ellipsis = p_trim_flags.has_flag(OVERRUN_ENFORCE_ELLIPSIS);
  3858. bool justification_aware = p_trim_flags.has_flag(OVERRUN_JUSTIFICATION_AWARE);
  3859. Glyph *sd_glyphs = sd->glyphs.ptrw();
  3860. if ((p_trim_flags & OVERRUN_TRIM) == OVERRUN_NO_TRIM || sd_glyphs == nullptr || p_width <= 0 || !(sd->width > p_width || enforce_ellipsis)) {
  3861. sd->overrun_trim_data.trim_pos = -1;
  3862. sd->overrun_trim_data.ellipsis_pos = -1;
  3863. return;
  3864. }
  3865. if (justification_aware && !sd->fit_width_minimum_reached) {
  3866. return;
  3867. }
  3868. Vector<ShapedTextDataFallback::Span> &spans = sd->spans;
  3869. if (sd->parent != RID()) {
  3870. ShapedTextDataFallback *parent_sd = shaped_owner.get_or_null(sd->parent);
  3871. ERR_FAIL_COND(!parent_sd->valid.is_set());
  3872. spans = parent_sd->spans;
  3873. }
  3874. int span_size = spans.size();
  3875. if (span_size == 0) {
  3876. return;
  3877. }
  3878. int sd_size = sd->glyphs.size();
  3879. int last_gl_font_size = sd_glyphs[sd_size - 1].font_size;
  3880. bool found_el_char = false;
  3881. // Find usable fonts, if fonts from the last glyph do not have required chars.
  3882. RID dot_gl_font_rid = sd_glyphs[sd_size - 1].font_rid;
  3883. if (add_ellipsis || enforce_ellipsis) {
  3884. if (!_font_has_char(dot_gl_font_rid, sd->el_char)) {
  3885. const Array &fonts = spans[span_size - 1].fonts;
  3886. for (int i = 0; i < fonts.size(); i++) {
  3887. if (_font_has_char(fonts[i], sd->el_char)) {
  3888. dot_gl_font_rid = fonts[i];
  3889. found_el_char = true;
  3890. break;
  3891. }
  3892. }
  3893. if (!found_el_char && OS::get_singleton()->has_feature("system_fonts") && fonts.size() > 0 && _font_is_allow_system_fallback(fonts[0])) {
  3894. const char32_t u32str[] = { sd->el_char, 0 };
  3895. RID rid = _find_sys_font_for_text(fonts[0], String(), spans[span_size - 1].language, u32str);
  3896. if (rid.is_valid()) {
  3897. dot_gl_font_rid = rid;
  3898. found_el_char = true;
  3899. }
  3900. }
  3901. } else {
  3902. found_el_char = true;
  3903. }
  3904. if (!found_el_char) {
  3905. bool found_dot_char = false;
  3906. dot_gl_font_rid = sd_glyphs[sd_size - 1].font_rid;
  3907. if (!_font_has_char(dot_gl_font_rid, '.')) {
  3908. const Array &fonts = spans[span_size - 1].fonts;
  3909. for (int i = 0; i < fonts.size(); i++) {
  3910. if (_font_has_char(fonts[i], '.')) {
  3911. dot_gl_font_rid = fonts[i];
  3912. found_dot_char = true;
  3913. break;
  3914. }
  3915. }
  3916. if (!found_dot_char && OS::get_singleton()->has_feature("system_fonts") && fonts.size() > 0 && _font_is_allow_system_fallback(fonts[0])) {
  3917. RID rid = _find_sys_font_for_text(fonts[0], String(), spans[span_size - 1].language, ".");
  3918. if (rid.is_valid()) {
  3919. dot_gl_font_rid = rid;
  3920. }
  3921. }
  3922. }
  3923. }
  3924. }
  3925. RID whitespace_gl_font_rid = sd_glyphs[sd_size - 1].font_rid;
  3926. if (!_font_has_char(whitespace_gl_font_rid, ' ')) {
  3927. const Array &fonts = spans[span_size - 1].fonts;
  3928. for (int i = 0; i < fonts.size(); i++) {
  3929. if (_font_has_char(fonts[i], ' ')) {
  3930. whitespace_gl_font_rid = fonts[i];
  3931. break;
  3932. }
  3933. }
  3934. }
  3935. 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;
  3936. 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();
  3937. 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;
  3938. 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();
  3939. int ellipsis_width = 0;
  3940. if (add_ellipsis && whitespace_gl_font_rid.is_valid()) {
  3941. 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);
  3942. }
  3943. int ell_min_characters = 6;
  3944. double width = sd->width;
  3945. double width_without_el = width;
  3946. int trim_pos = 0;
  3947. int ellipsis_pos = (enforce_ellipsis) ? 0 : -1;
  3948. int last_valid_cut = -1;
  3949. int last_valid_cut_witout_el = -1;
  3950. if (enforce_ellipsis && (width + ellipsis_width <= p_width)) {
  3951. trim_pos = -1;
  3952. ellipsis_pos = sd_size;
  3953. } else {
  3954. for (int i = sd_size - 1; i != -1; i--) {
  3955. width -= sd_glyphs[i].advance * sd_glyphs[i].repeat;
  3956. if (sd_glyphs[i].count > 0) {
  3957. bool above_min_char_threshold = (i >= ell_min_characters);
  3958. if (!above_min_char_threshold && last_valid_cut_witout_el != -1) {
  3959. trim_pos = last_valid_cut_witout_el;
  3960. ellipsis_pos = -1;
  3961. width = width_without_el;
  3962. break;
  3963. }
  3964. if (!enforce_ellipsis && width <= p_width && last_valid_cut_witout_el == -1) {
  3965. if (cut_per_word && above_min_char_threshold) {
  3966. if ((sd_glyphs[i].flags & GRAPHEME_IS_BREAK_SOFT) == GRAPHEME_IS_BREAK_SOFT) {
  3967. last_valid_cut_witout_el = i;
  3968. width_without_el = width;
  3969. }
  3970. } else {
  3971. last_valid_cut_witout_el = i;
  3972. width_without_el = width;
  3973. }
  3974. }
  3975. if (width + (((above_min_char_threshold && add_ellipsis) || enforce_ellipsis) ? ellipsis_width : 0) <= p_width) {
  3976. if (cut_per_word && above_min_char_threshold) {
  3977. if ((sd_glyphs[i].flags & GRAPHEME_IS_BREAK_SOFT) == GRAPHEME_IS_BREAK_SOFT) {
  3978. last_valid_cut = i;
  3979. }
  3980. } else {
  3981. last_valid_cut = i;
  3982. }
  3983. if (last_valid_cut != -1) {
  3984. trim_pos = last_valid_cut;
  3985. if (add_ellipsis && (above_min_char_threshold || enforce_ellipsis) && width - ellipsis_width <= p_width) {
  3986. ellipsis_pos = trim_pos;
  3987. }
  3988. break;
  3989. }
  3990. }
  3991. }
  3992. }
  3993. }
  3994. sd->overrun_trim_data.trim_pos = trim_pos;
  3995. sd->overrun_trim_data.ellipsis_pos = ellipsis_pos;
  3996. if (trim_pos == 0 && enforce_ellipsis && add_ellipsis) {
  3997. sd->overrun_trim_data.ellipsis_pos = 0;
  3998. }
  3999. if ((trim_pos >= 0 && sd->width > p_width) || enforce_ellipsis) {
  4000. if (add_ellipsis && (ellipsis_pos > 0 || enforce_ellipsis)) {
  4001. // Insert an additional space when cutting word bound for aesthetics.
  4002. if (cut_per_word && (ellipsis_pos > 0)) {
  4003. Glyph gl;
  4004. gl.count = 1;
  4005. gl.advance = whitespace_adv.x;
  4006. gl.index = whitespace_gl_idx;
  4007. gl.font_rid = whitespace_gl_font_rid;
  4008. gl.font_size = last_gl_font_size;
  4009. gl.flags = GRAPHEME_IS_SPACE | GRAPHEME_IS_BREAK_SOFT | GRAPHEME_IS_VIRTUAL;
  4010. sd->overrun_trim_data.ellipsis_glyph_buf.append(gl);
  4011. }
  4012. // Add ellipsis dots.
  4013. if (dot_gl_idx != 0) {
  4014. Glyph gl;
  4015. gl.count = 1;
  4016. gl.repeat = (found_el_char ? 1 : 3);
  4017. gl.advance = dot_adv.x;
  4018. gl.index = dot_gl_idx;
  4019. gl.font_rid = dot_gl_font_rid;
  4020. gl.font_size = last_gl_font_size;
  4021. gl.flags = GRAPHEME_IS_PUNCTUATION | GRAPHEME_IS_VIRTUAL;
  4022. sd->overrun_trim_data.ellipsis_glyph_buf.append(gl);
  4023. }
  4024. }
  4025. sd->text_trimmed = true;
  4026. sd->width_trimmed = width + ((ellipsis_pos != -1) ? ellipsis_width : 0);
  4027. }
  4028. }
  4029. int64_t TextServerFallback::_shaped_text_get_trim_pos(const RID &p_shaped) const {
  4030. ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  4031. ERR_FAIL_NULL_V_MSG(sd, -1, "ShapedTextDataFallback invalid.");
  4032. MutexLock lock(sd->mutex);
  4033. return sd->overrun_trim_data.trim_pos;
  4034. }
  4035. int64_t TextServerFallback::_shaped_text_get_ellipsis_pos(const RID &p_shaped) const {
  4036. ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  4037. ERR_FAIL_NULL_V_MSG(sd, -1, "ShapedTextDataFallback invalid.");
  4038. MutexLock lock(sd->mutex);
  4039. return sd->overrun_trim_data.ellipsis_pos;
  4040. }
  4041. const Glyph *TextServerFallback::_shaped_text_get_ellipsis_glyphs(const RID &p_shaped) const {
  4042. ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  4043. ERR_FAIL_NULL_V_MSG(sd, nullptr, "ShapedTextDataFallback invalid.");
  4044. MutexLock lock(sd->mutex);
  4045. return sd->overrun_trim_data.ellipsis_glyph_buf.ptr();
  4046. }
  4047. int64_t TextServerFallback::_shaped_text_get_ellipsis_glyph_count(const RID &p_shaped) const {
  4048. ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  4049. ERR_FAIL_NULL_V_MSG(sd, 0, "ShapedTextDataFallback invalid.");
  4050. MutexLock lock(sd->mutex);
  4051. return sd->overrun_trim_data.ellipsis_glyph_buf.size();
  4052. }
  4053. bool TextServerFallback::_shaped_text_shape(const RID &p_shaped) {
  4054. ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  4055. ERR_FAIL_NULL_V(sd, false);
  4056. MutexLock lock(sd->mutex);
  4057. if (sd->valid.is_set()) {
  4058. return true;
  4059. }
  4060. if (sd->parent != RID()) {
  4061. full_copy(sd);
  4062. }
  4063. // Cleanup.
  4064. sd->justification_ops_valid = false;
  4065. sd->line_breaks_valid = false;
  4066. sd->ascent = 0.0;
  4067. sd->descent = 0.0;
  4068. sd->width = 0.0;
  4069. sd->glyphs.clear();
  4070. sd->runs.clear();
  4071. sd->runs_dirty = true;
  4072. if (sd->text.length() == 0) {
  4073. sd->valid.set();
  4074. return true;
  4075. }
  4076. // "Shape" string.
  4077. for (int i = 0; i < sd->spans.size(); i++) {
  4078. const ShapedTextDataFallback::Span &span = sd->spans[i];
  4079. if (span.embedded_key != Variant()) {
  4080. // Embedded object.
  4081. if (sd->orientation == ORIENTATION_HORIZONTAL) {
  4082. sd->objects[span.embedded_key].rect.position.x = sd->width;
  4083. sd->width += sd->objects[span.embedded_key].rect.size.x;
  4084. } else {
  4085. sd->objects[span.embedded_key].rect.position.y = sd->width;
  4086. sd->width += sd->objects[span.embedded_key].rect.size.y;
  4087. }
  4088. Glyph gl;
  4089. gl.span_index = i;
  4090. gl.start = span.start;
  4091. gl.end = span.end;
  4092. gl.count = 1;
  4093. gl.index = 0;
  4094. gl.flags = GRAPHEME_IS_VALID | GRAPHEME_IS_EMBEDDED_OBJECT;
  4095. if (sd->orientation == ORIENTATION_HORIZONTAL) {
  4096. gl.advance = sd->objects[span.embedded_key].rect.size.x;
  4097. } else {
  4098. gl.advance = sd->objects[span.embedded_key].rect.size.y;
  4099. }
  4100. sd->glyphs.push_back(gl);
  4101. } else {
  4102. // Text span.
  4103. RID prev_font;
  4104. for (int j = span.start; j < span.end; j++) {
  4105. Glyph gl;
  4106. gl.span_index = i;
  4107. gl.start = j;
  4108. gl.end = j + 1;
  4109. gl.count = 1;
  4110. gl.font_size = span.font_size;
  4111. gl.index = (int32_t)sd->text[j - sd->start]; // Use codepoint.
  4112. if (gl.index == 0x0009 || gl.index == 0x000b) {
  4113. gl.index = 0x0020;
  4114. }
  4115. if (!sd->preserve_control && is_control(gl.index)) {
  4116. gl.index = 0x0020;
  4117. }
  4118. // Select first font which has character (font are already sorted by span language).
  4119. for (int k = 0; k < span.fonts.size(); k++) {
  4120. if (_font_has_char(span.fonts[k], gl.index)) {
  4121. gl.font_rid = span.fonts[k];
  4122. break;
  4123. }
  4124. }
  4125. if (!gl.font_rid.is_valid() && prev_font.is_valid()) {
  4126. if (_font_has_char(prev_font, gl.index)) {
  4127. gl.font_rid = prev_font;
  4128. }
  4129. }
  4130. if (!gl.font_rid.is_valid() && OS::get_singleton()->has_feature("system_fonts") && span.fonts.size() > 0) {
  4131. // Try system fallback.
  4132. RID fdef = span.fonts[0];
  4133. if (_font_is_allow_system_fallback(fdef)) {
  4134. String text = sd->text.substr(j, 1);
  4135. gl.font_rid = _find_sys_font_for_text(fdef, String(), span.language, text);
  4136. }
  4137. }
  4138. prev_font = gl.font_rid;
  4139. if (gl.font_rid.is_valid()) {
  4140. double scale = _font_get_scale(gl.font_rid, gl.font_size);
  4141. 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);
  4142. if (sd->text[j - sd->start] != 0 && !is_linebreak(sd->text[j - sd->start])) {
  4143. if (sd->orientation == ORIENTATION_HORIZONTAL) {
  4144. gl.advance = _font_get_glyph_advance(gl.font_rid, gl.font_size, gl.index).x;
  4145. gl.x_off = 0;
  4146. 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));
  4147. sd->ascent = MAX(sd->ascent, _font_get_ascent(gl.font_rid, gl.font_size) + _font_get_spacing(gl.font_rid, SPACING_TOP));
  4148. sd->descent = MAX(sd->descent, _font_get_descent(gl.font_rid, gl.font_size) + _font_get_spacing(gl.font_rid, SPACING_BOTTOM));
  4149. } else {
  4150. gl.advance = _font_get_glyph_advance(gl.font_rid, gl.font_size, gl.index).y;
  4151. 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));
  4152. gl.y_off = _font_get_ascent(gl.font_rid, gl.font_size);
  4153. sd->ascent = MAX(sd->ascent, Math::round(_font_get_glyph_advance(gl.font_rid, gl.font_size, gl.index).x * 0.5));
  4154. sd->descent = MAX(sd->descent, Math::round(_font_get_glyph_advance(gl.font_rid, gl.font_size, gl.index).x * 0.5));
  4155. }
  4156. }
  4157. if (j < sd->end - 1) {
  4158. // Do not add extra spacing to the last glyph of the string.
  4159. if (is_whitespace(sd->text[j - sd->start])) {
  4160. gl.advance += sd->extra_spacing[SPACING_SPACE] + _font_get_spacing(gl.font_rid, SPACING_SPACE);
  4161. } else {
  4162. gl.advance += sd->extra_spacing[SPACING_GLYPH] + _font_get_spacing(gl.font_rid, SPACING_GLYPH);
  4163. }
  4164. }
  4165. sd->upos = MAX(sd->upos, _font_get_underline_position(gl.font_rid, gl.font_size));
  4166. sd->uthk = MAX(sd->uthk, _font_get_underline_thickness(gl.font_rid, gl.font_size));
  4167. // Add kerning to previous glyph.
  4168. if (sd->glyphs.size() > 0) {
  4169. Glyph &prev_gl = sd->glyphs.write[sd->glyphs.size() - 1];
  4170. if (prev_gl.font_rid == gl.font_rid && prev_gl.font_size == gl.font_size) {
  4171. if (sd->orientation == ORIENTATION_HORIZONTAL) {
  4172. prev_gl.advance += _font_get_kerning(gl.font_rid, gl.font_size, Vector2i(prev_gl.index, gl.index)).x;
  4173. } else {
  4174. prev_gl.advance += _font_get_kerning(gl.font_rid, gl.font_size, Vector2i(prev_gl.index, gl.index)).y;
  4175. }
  4176. }
  4177. }
  4178. if (sd->orientation == ORIENTATION_HORIZONTAL && !subpos) {
  4179. gl.advance = Math::round(gl.advance);
  4180. }
  4181. } else if (sd->preserve_invalid || (sd->preserve_control && is_control(gl.index))) {
  4182. // Glyph not found, replace with hex code box.
  4183. if (sd->orientation == ORIENTATION_HORIZONTAL) {
  4184. gl.advance = get_hex_code_box_size(gl.font_size, gl.index).x;
  4185. sd->ascent = MAX(sd->ascent, get_hex_code_box_size(gl.font_size, gl.index).y * 0.85);
  4186. sd->descent = MAX(sd->descent, get_hex_code_box_size(gl.font_size, gl.index).y * 0.15);
  4187. } else {
  4188. gl.advance = get_hex_code_box_size(gl.font_size, gl.index).y;
  4189. sd->ascent = MAX(sd->ascent, Math::round(get_hex_code_box_size(gl.font_size, gl.index).x * 0.5));
  4190. sd->descent = MAX(sd->descent, Math::round(get_hex_code_box_size(gl.font_size, gl.index).x * 0.5));
  4191. }
  4192. }
  4193. sd->width += gl.advance;
  4194. sd->glyphs.push_back(gl);
  4195. }
  4196. }
  4197. }
  4198. // Align embedded objects to baseline.
  4199. _realign(sd);
  4200. sd->valid.set();
  4201. return sd->valid.is_set();
  4202. }
  4203. bool TextServerFallback::_shaped_text_is_ready(const RID &p_shaped) const {
  4204. const ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  4205. ERR_FAIL_NULL_V(sd, false);
  4206. return sd->valid.is_set();
  4207. }
  4208. const Glyph *TextServerFallback::_shaped_text_get_glyphs(const RID &p_shaped) const {
  4209. const ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  4210. ERR_FAIL_NULL_V(sd, nullptr);
  4211. MutexLock lock(sd->mutex);
  4212. if (!sd->valid.is_set()) {
  4213. const_cast<TextServerFallback *>(this)->_shaped_text_shape(p_shaped);
  4214. }
  4215. return sd->glyphs.ptr();
  4216. }
  4217. int64_t TextServerFallback::_shaped_text_get_glyph_count(const RID &p_shaped) const {
  4218. const ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  4219. ERR_FAIL_NULL_V(sd, 0);
  4220. MutexLock lock(sd->mutex);
  4221. if (!sd->valid.is_set()) {
  4222. const_cast<TextServerFallback *>(this)->_shaped_text_shape(p_shaped);
  4223. }
  4224. return sd->glyphs.size();
  4225. }
  4226. const Glyph *TextServerFallback::_shaped_text_sort_logical(const RID &p_shaped) {
  4227. const ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  4228. ERR_FAIL_NULL_V(sd, nullptr);
  4229. MutexLock lock(sd->mutex);
  4230. if (!sd->valid.is_set()) {
  4231. _shaped_text_shape(p_shaped);
  4232. }
  4233. return sd->glyphs.ptr(); // Already in the logical order, return as is.
  4234. }
  4235. Vector2i TextServerFallback::_shaped_text_get_range(const RID &p_shaped) const {
  4236. const ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  4237. ERR_FAIL_NULL_V(sd, Vector2i());
  4238. MutexLock lock(sd->mutex);
  4239. return Vector2(sd->start, sd->end);
  4240. }
  4241. Array TextServerFallback::_shaped_text_get_objects(const RID &p_shaped) const {
  4242. Array ret;
  4243. const ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  4244. ERR_FAIL_NULL_V(sd, ret);
  4245. MutexLock lock(sd->mutex);
  4246. for (const KeyValue<Variant, ShapedTextDataFallback::EmbeddedObject> &E : sd->objects) {
  4247. ret.push_back(E.key);
  4248. }
  4249. return ret;
  4250. }
  4251. Rect2 TextServerFallback::_shaped_text_get_object_rect(const RID &p_shaped, const Variant &p_key) const {
  4252. const ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  4253. ERR_FAIL_NULL_V(sd, Rect2());
  4254. MutexLock lock(sd->mutex);
  4255. ERR_FAIL_COND_V(!sd->objects.has(p_key), Rect2());
  4256. if (!sd->valid.is_set()) {
  4257. const_cast<TextServerFallback *>(this)->_shaped_text_shape(p_shaped);
  4258. }
  4259. return sd->objects[p_key].rect;
  4260. }
  4261. Vector2i TextServerFallback::_shaped_text_get_object_range(const RID &p_shaped, const Variant &p_key) const {
  4262. const ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  4263. ERR_FAIL_NULL_V(sd, Vector2i());
  4264. MutexLock lock(sd->mutex);
  4265. ERR_FAIL_COND_V(!sd->objects.has(p_key), Vector2i());
  4266. return Vector2i(sd->objects[p_key].start, sd->objects[p_key].end);
  4267. }
  4268. int64_t TextServerFallback::_shaped_text_get_object_glyph(const RID &p_shaped, const Variant &p_key) const {
  4269. const ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  4270. ERR_FAIL_NULL_V(sd, -1);
  4271. MutexLock lock(sd->mutex);
  4272. ERR_FAIL_COND_V(!sd->objects.has(p_key), -1);
  4273. const ShapedTextDataFallback::EmbeddedObject &obj = sd->objects[p_key];
  4274. int sd_size = sd->glyphs.size();
  4275. const Glyph *sd_glyphs = sd->glyphs.ptr();
  4276. for (int i = 0; i < sd_size; i++) {
  4277. if (obj.start == sd_glyphs[i].start) {
  4278. return i;
  4279. }
  4280. }
  4281. return -1;
  4282. }
  4283. Size2 TextServerFallback::_shaped_text_get_size(const RID &p_shaped) const {
  4284. const ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  4285. ERR_FAIL_NULL_V(sd, Size2());
  4286. MutexLock lock(sd->mutex);
  4287. if (!sd->valid.is_set()) {
  4288. const_cast<TextServerFallback *>(this)->_shaped_text_shape(p_shaped);
  4289. }
  4290. if (sd->orientation == TextServer::ORIENTATION_HORIZONTAL) {
  4291. return Size2(sd->width, sd->ascent + sd->descent + sd->extra_spacing[SPACING_TOP] + sd->extra_spacing[SPACING_BOTTOM]).ceil();
  4292. } else {
  4293. return Size2(sd->ascent + sd->descent + sd->extra_spacing[SPACING_TOP] + sd->extra_spacing[SPACING_BOTTOM], sd->width).ceil();
  4294. }
  4295. }
  4296. double TextServerFallback::_shaped_text_get_ascent(const RID &p_shaped) const {
  4297. const ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  4298. ERR_FAIL_NULL_V(sd, 0.0);
  4299. MutexLock lock(sd->mutex);
  4300. if (!sd->valid.is_set()) {
  4301. const_cast<TextServerFallback *>(this)->_shaped_text_shape(p_shaped);
  4302. }
  4303. return sd->ascent + sd->extra_spacing[SPACING_TOP];
  4304. }
  4305. double TextServerFallback::_shaped_text_get_descent(const RID &p_shaped) const {
  4306. const ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  4307. ERR_FAIL_NULL_V(sd, 0.0);
  4308. MutexLock lock(sd->mutex);
  4309. if (!sd->valid.is_set()) {
  4310. const_cast<TextServerFallback *>(this)->_shaped_text_shape(p_shaped);
  4311. }
  4312. return sd->descent + sd->extra_spacing[SPACING_BOTTOM];
  4313. }
  4314. double TextServerFallback::_shaped_text_get_width(const RID &p_shaped) const {
  4315. const ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  4316. ERR_FAIL_NULL_V(sd, 0.0);
  4317. MutexLock lock(sd->mutex);
  4318. if (!sd->valid.is_set()) {
  4319. const_cast<TextServerFallback *>(this)->_shaped_text_shape(p_shaped);
  4320. }
  4321. return Math::ceil(sd->width);
  4322. }
  4323. double TextServerFallback::_shaped_text_get_underline_position(const RID &p_shaped) const {
  4324. const ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  4325. ERR_FAIL_NULL_V(sd, 0.0);
  4326. MutexLock lock(sd->mutex);
  4327. if (!sd->valid.is_set()) {
  4328. const_cast<TextServerFallback *>(this)->_shaped_text_shape(p_shaped);
  4329. }
  4330. return sd->upos;
  4331. }
  4332. double TextServerFallback::_shaped_text_get_underline_thickness(const RID &p_shaped) const {
  4333. const ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  4334. ERR_FAIL_NULL_V(sd, 0.0);
  4335. MutexLock lock(sd->mutex);
  4336. if (!sd->valid.is_set()) {
  4337. const_cast<TextServerFallback *>(this)->_shaped_text_shape(p_shaped);
  4338. }
  4339. return sd->uthk;
  4340. }
  4341. PackedInt32Array TextServerFallback::_shaped_text_get_character_breaks(const RID &p_shaped) const {
  4342. const ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
  4343. ERR_FAIL_NULL_V(sd, PackedInt32Array());
  4344. MutexLock lock(sd->mutex);
  4345. if (!sd->valid.is_set()) {
  4346. const_cast<TextServerFallback *>(this)->_shaped_text_shape(p_shaped);
  4347. }
  4348. PackedInt32Array ret;
  4349. int size = sd->end - sd->start;
  4350. if (size > 0) {
  4351. ret.resize(size);
  4352. for (int i = 0; i < size; i++) {
  4353. #ifdef GDEXTENSION
  4354. ret[i] = i + 1 + sd->start;
  4355. #else
  4356. ret.write[i] = i + 1 + sd->start;
  4357. #endif
  4358. }
  4359. }
  4360. return ret;
  4361. }
  4362. String TextServerFallback::_string_to_upper(const String &p_string, const String &p_language) const {
  4363. return p_string.to_upper();
  4364. }
  4365. String TextServerFallback::_string_to_lower(const String &p_string, const String &p_language) const {
  4366. return p_string.to_lower();
  4367. }
  4368. String TextServerFallback::_string_to_title(const String &p_string, const String &p_language) const {
  4369. return p_string.capitalize();
  4370. }
  4371. PackedInt32Array TextServerFallback::_string_get_word_breaks(const String &p_string, const String &p_language, int64_t p_chars_per_line) const {
  4372. PackedInt32Array ret;
  4373. if (p_chars_per_line > 0) {
  4374. int line_start = 0;
  4375. int last_break = -1;
  4376. int line_length = 0;
  4377. for (int i = 0; i < p_string.length(); i++) {
  4378. const char32_t c = p_string[i];
  4379. bool is_lb = is_linebreak(c);
  4380. bool is_ws = is_whitespace(c);
  4381. bool is_p = (is_punct(c) && c != 0x005F) || is_underscore(c) || c == '\t' || c == 0xfffc;
  4382. if (is_lb) {
  4383. if (line_length > 0) {
  4384. ret.push_back(line_start);
  4385. ret.push_back(i);
  4386. }
  4387. line_start = i;
  4388. line_length = 0;
  4389. last_break = -1;
  4390. continue;
  4391. } else if (is_ws || is_p) {
  4392. last_break = i;
  4393. }
  4394. if (line_length == p_chars_per_line) {
  4395. if (last_break != -1) {
  4396. int last_break_w_spaces = last_break;
  4397. while (last_break > line_start && is_whitespace(p_string[last_break - 1])) {
  4398. last_break--;
  4399. }
  4400. if (line_start != last_break) {
  4401. ret.push_back(line_start);
  4402. ret.push_back(last_break);
  4403. }
  4404. while (last_break_w_spaces < p_string.length() && is_whitespace(p_string[last_break_w_spaces])) {
  4405. last_break_w_spaces++;
  4406. }
  4407. line_start = last_break_w_spaces;
  4408. if (last_break_w_spaces < i) {
  4409. line_length = i - last_break_w_spaces;
  4410. } else {
  4411. i = last_break_w_spaces;
  4412. line_length = 0;
  4413. }
  4414. } else {
  4415. ret.push_back(line_start);
  4416. ret.push_back(i);
  4417. line_start = i;
  4418. line_length = 0;
  4419. }
  4420. last_break = -1;
  4421. }
  4422. line_length++;
  4423. }
  4424. if (line_length > 0) {
  4425. ret.push_back(line_start);
  4426. ret.push_back(p_string.length());
  4427. }
  4428. } else {
  4429. int word_start = 0; // -1 if no word encountered. Leading spaces are part of a word.
  4430. int word_length = 0;
  4431. for (int i = 0; i < p_string.length(); i++) {
  4432. const char32_t c = p_string[i];
  4433. bool is_lb = is_linebreak(c);
  4434. bool is_ws = is_whitespace(c);
  4435. bool is_p = (is_punct(c) && c != 0x005F) || is_underscore(c) || c == '\t' || c == 0xfffc;
  4436. if (word_start == -1) {
  4437. if (!is_lb && !is_ws && !is_p) {
  4438. word_start = i;
  4439. }
  4440. continue;
  4441. }
  4442. if (is_lb) {
  4443. if (word_start != -1 && word_length > 0) {
  4444. ret.push_back(word_start);
  4445. ret.push_back(i);
  4446. }
  4447. word_start = -1;
  4448. word_length = 0;
  4449. } else if (is_ws || is_p) {
  4450. if (word_start != -1 && word_length > 0) {
  4451. ret.push_back(word_start);
  4452. ret.push_back(i);
  4453. }
  4454. word_start = -1;
  4455. word_length = 0;
  4456. }
  4457. word_length++;
  4458. }
  4459. if (word_start != -1 && word_length > 0) {
  4460. ret.push_back(word_start);
  4461. ret.push_back(p_string.length());
  4462. }
  4463. }
  4464. return ret;
  4465. }
  4466. void TextServerFallback::_update_settings() {
  4467. lcd_subpixel_layout.set((TextServer::FontLCDSubpixelLayout)(int)GLOBAL_GET("gui/theme/lcd_subpixel_layout"));
  4468. }
  4469. TextServerFallback::TextServerFallback() {
  4470. _insert_feature_sets();
  4471. ProjectSettings::get_singleton()->connect("settings_changed", callable_mp(this, &TextServerFallback::_update_settings));
  4472. }
  4473. void TextServerFallback::_font_clear_system_fallback_cache() {
  4474. for (const KeyValue<SystemFontKey, SystemFontCache> &E : system_fonts) {
  4475. const Vector<SystemFontCacheRec> &sysf_cache = E.value.var;
  4476. for (const SystemFontCacheRec &F : sysf_cache) {
  4477. _free_rid(F.rid);
  4478. }
  4479. }
  4480. system_fonts.clear();
  4481. system_font_data.clear();
  4482. }
  4483. void TextServerFallback::_cleanup() {
  4484. font_clear_system_fallback_cache();
  4485. }
  4486. TextServerFallback::~TextServerFallback() {
  4487. #ifdef MODULE_FREETYPE_ENABLED
  4488. if (ft_library != nullptr) {
  4489. FT_Done_FreeType(ft_library);
  4490. }
  4491. #endif
  4492. }