text_server_fb.cpp 173 KB

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