2
0

text_server_fb.cpp 162 KB

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