text_server_fb.cpp 177 KB

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