text_server_fb.cpp 173 KB

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