text_server_fb.cpp 173 KB

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