text_server_fb.cpp 178 KB

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