text_server_adv.cpp 234 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493
  1. /*************************************************************************/
  2. /* text_server_adv.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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_adv.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. using namespace godot;
  40. #define GLOBAL_GET(m_var) ProjectSettings::get_singleton()->get(m_var)
  41. #else
  42. // Headers for building as built-in module.
  43. #include "core/config/project_settings.h"
  44. #include "core/error/error_macros.h"
  45. #include "core/object/worker_thread_pool.h"
  46. #include "core/string/print_string.h"
  47. #include "core/string/translation.h"
  48. #include "modules/modules_enabled.gen.h" // For freetype, msdfgen, svg.
  49. #endif
  50. // Built-in ICU data.
  51. #ifdef ICU_STATIC_DATA
  52. #include "icudata.gen.h"
  53. #endif
  54. // Thirdparty headers.
  55. #ifdef MODULE_MSDFGEN_ENABLED
  56. #include "core/ShapeDistanceFinder.h"
  57. #include "core/contour-combiners.h"
  58. #include "core/edge-selectors.h"
  59. #include "msdfgen.h"
  60. #endif
  61. #ifdef MODULE_SVG_ENABLED
  62. #include "thorvg_svg_in_ot.h"
  63. #endif
  64. /*************************************************************************/
  65. /* bmp_font_t HarfBuzz Bitmap font interface */
  66. /*************************************************************************/
  67. hb_font_funcs_t *TextServerAdvanced::funcs = nullptr;
  68. TextServerAdvanced::bmp_font_t *TextServerAdvanced::_bmp_font_create(TextServerAdvanced::FontForSizeAdvanced *p_face, bool p_unref) {
  69. bmp_font_t *bm_font = memnew(bmp_font_t);
  70. if (!bm_font) {
  71. return nullptr;
  72. }
  73. bm_font->face = p_face;
  74. bm_font->unref = p_unref;
  75. return bm_font;
  76. }
  77. void TextServerAdvanced::_bmp_font_destroy(void *p_data) {
  78. bmp_font_t *bm_font = static_cast<bmp_font_t *>(p_data);
  79. memdelete(bm_font);
  80. }
  81. hb_bool_t TextServerAdvanced::_bmp_get_nominal_glyph(hb_font_t *p_font, void *p_font_data, hb_codepoint_t p_unicode, hb_codepoint_t *r_glyph, void *p_user_data) {
  82. const bmp_font_t *bm_font = static_cast<const bmp_font_t *>(p_font_data);
  83. if (!bm_font->face) {
  84. return false;
  85. }
  86. if (!bm_font->face->glyph_map.has(p_unicode)) {
  87. if (bm_font->face->glyph_map.has(0xf000u + p_unicode)) {
  88. *r_glyph = 0xf000u + p_unicode;
  89. return true;
  90. } else {
  91. return false;
  92. }
  93. }
  94. *r_glyph = p_unicode;
  95. return true;
  96. }
  97. hb_position_t TextServerAdvanced::_bmp_get_glyph_h_advance(hb_font_t *p_font, void *p_font_data, hb_codepoint_t p_glyph, void *p_user_data) {
  98. const bmp_font_t *bm_font = static_cast<const bmp_font_t *>(p_font_data);
  99. if (!bm_font->face) {
  100. return 0;
  101. }
  102. if (!bm_font->face->glyph_map.has(p_glyph)) {
  103. return 0;
  104. }
  105. return bm_font->face->glyph_map[p_glyph].advance.x * 64;
  106. }
  107. hb_position_t TextServerAdvanced::_bmp_get_glyph_v_advance(hb_font_t *p_font, void *p_font_data, hb_codepoint_t p_glyph, void *p_user_data) {
  108. const bmp_font_t *bm_font = static_cast<const bmp_font_t *>(p_font_data);
  109. if (!bm_font->face) {
  110. return 0;
  111. }
  112. if (!bm_font->face->glyph_map.has(p_glyph)) {
  113. return 0;
  114. }
  115. return -bm_font->face->glyph_map[p_glyph].advance.y * 64;
  116. }
  117. hb_position_t TextServerAdvanced::_bmp_get_glyph_h_kerning(hb_font_t *p_font, void *p_font_data, hb_codepoint_t p_left_glyph, hb_codepoint_t p_right_glyph, void *p_user_data) {
  118. const bmp_font_t *bm_font = static_cast<const bmp_font_t *>(p_font_data);
  119. if (!bm_font->face) {
  120. return 0;
  121. }
  122. if (!bm_font->face->kerning_map.has(Vector2i(p_left_glyph, p_right_glyph))) {
  123. return 0;
  124. }
  125. return bm_font->face->kerning_map[Vector2i(p_left_glyph, p_right_glyph)].x * 64;
  126. }
  127. hb_bool_t TextServerAdvanced::_bmp_get_glyph_v_origin(hb_font_t *p_font, void *p_font_data, hb_codepoint_t p_glyph, hb_position_t *r_x, hb_position_t *r_y, void *p_user_data) {
  128. const bmp_font_t *bm_font = static_cast<const bmp_font_t *>(p_font_data);
  129. if (!bm_font->face) {
  130. return false;
  131. }
  132. if (!bm_font->face->glyph_map.has(p_glyph)) {
  133. return false;
  134. }
  135. *r_x = bm_font->face->glyph_map[p_glyph].advance.x * 32;
  136. *r_y = -bm_font->face->ascent * 64;
  137. return true;
  138. }
  139. hb_bool_t TextServerAdvanced::_bmp_get_glyph_extents(hb_font_t *p_font, void *p_font_data, hb_codepoint_t p_glyph, hb_glyph_extents_t *r_extents, void *p_user_data) {
  140. const bmp_font_t *bm_font = static_cast<const bmp_font_t *>(p_font_data);
  141. if (!bm_font->face) {
  142. return false;
  143. }
  144. if (!bm_font->face->glyph_map.has(p_glyph)) {
  145. return false;
  146. }
  147. r_extents->x_bearing = 0;
  148. r_extents->y_bearing = 0;
  149. r_extents->width = bm_font->face->glyph_map[p_glyph].rect.size.x * 64;
  150. r_extents->height = bm_font->face->glyph_map[p_glyph].rect.size.y * 64;
  151. return true;
  152. }
  153. hb_bool_t TextServerAdvanced::_bmp_get_font_h_extents(hb_font_t *p_font, void *p_font_data, hb_font_extents_t *r_metrics, void *p_user_data) {
  154. const bmp_font_t *bm_font = static_cast<const bmp_font_t *>(p_font_data);
  155. if (!bm_font->face) {
  156. return false;
  157. }
  158. r_metrics->ascender = bm_font->face->ascent;
  159. r_metrics->descender = bm_font->face->descent;
  160. r_metrics->line_gap = 0;
  161. return true;
  162. }
  163. void TextServerAdvanced::_bmp_create_font_funcs() {
  164. if (funcs == nullptr) {
  165. funcs = hb_font_funcs_create();
  166. hb_font_funcs_set_font_h_extents_func(funcs, _bmp_get_font_h_extents, nullptr, nullptr);
  167. hb_font_funcs_set_nominal_glyph_func(funcs, _bmp_get_nominal_glyph, nullptr, nullptr);
  168. hb_font_funcs_set_glyph_h_advance_func(funcs, _bmp_get_glyph_h_advance, nullptr, nullptr);
  169. hb_font_funcs_set_glyph_v_advance_func(funcs, _bmp_get_glyph_v_advance, nullptr, nullptr);
  170. hb_font_funcs_set_glyph_v_origin_func(funcs, _bmp_get_glyph_v_origin, nullptr, nullptr);
  171. hb_font_funcs_set_glyph_h_kerning_func(funcs, _bmp_get_glyph_h_kerning, nullptr, nullptr);
  172. hb_font_funcs_set_glyph_extents_func(funcs, _bmp_get_glyph_extents, nullptr, nullptr);
  173. hb_font_funcs_make_immutable(funcs);
  174. }
  175. }
  176. void TextServerAdvanced::_bmp_free_font_funcs() {
  177. if (funcs != nullptr) {
  178. hb_font_funcs_destroy(funcs);
  179. funcs = nullptr;
  180. }
  181. }
  182. void TextServerAdvanced::_bmp_font_set_funcs(hb_font_t *p_font, TextServerAdvanced::FontForSizeAdvanced *p_face, bool p_unref) {
  183. hb_font_set_funcs(p_font, funcs, _bmp_font_create(p_face, p_unref), _bmp_font_destroy);
  184. }
  185. hb_font_t *TextServerAdvanced::_bmp_font_create(TextServerAdvanced::FontForSizeAdvanced *p_face, hb_destroy_func_t p_destroy) {
  186. hb_font_t *font;
  187. hb_face_t *face = hb_face_create(nullptr, 0);
  188. font = hb_font_create(face);
  189. hb_face_destroy(face);
  190. _bmp_font_set_funcs(font, p_face, false);
  191. return font;
  192. }
  193. /*************************************************************************/
  194. /* Character properties. */
  195. /*************************************************************************/
  196. _FORCE_INLINE_ bool is_ain(char32_t p_chr) {
  197. return u_getIntPropertyValue(p_chr, UCHAR_JOINING_GROUP) == U_JG_AIN;
  198. }
  199. _FORCE_INLINE_ bool is_alef(char32_t p_chr) {
  200. return u_getIntPropertyValue(p_chr, UCHAR_JOINING_GROUP) == U_JG_ALEF;
  201. }
  202. _FORCE_INLINE_ bool is_beh(char32_t p_chr) {
  203. int32_t prop = u_getIntPropertyValue(p_chr, UCHAR_JOINING_GROUP);
  204. return (prop == U_JG_BEH) || (prop == U_JG_NOON) || (prop == U_JG_AFRICAN_NOON) || (prop == U_JG_NYA) || (prop == U_JG_YEH) || (prop == U_JG_FARSI_YEH);
  205. }
  206. _FORCE_INLINE_ bool is_dal(char32_t p_chr) {
  207. return u_getIntPropertyValue(p_chr, UCHAR_JOINING_GROUP) == U_JG_DAL;
  208. }
  209. _FORCE_INLINE_ bool is_feh(char32_t p_chr) {
  210. return (u_getIntPropertyValue(p_chr, UCHAR_JOINING_GROUP) == U_JG_FEH) || (u_getIntPropertyValue(p_chr, UCHAR_JOINING_GROUP) == U_JG_AFRICAN_FEH);
  211. }
  212. _FORCE_INLINE_ bool is_gaf(char32_t p_chr) {
  213. return u_getIntPropertyValue(p_chr, UCHAR_JOINING_GROUP) == U_JG_GAF;
  214. }
  215. _FORCE_INLINE_ bool is_heh(char32_t p_chr) {
  216. return u_getIntPropertyValue(p_chr, UCHAR_JOINING_GROUP) == U_JG_HEH;
  217. }
  218. _FORCE_INLINE_ bool is_kaf(char32_t p_chr) {
  219. return u_getIntPropertyValue(p_chr, UCHAR_JOINING_GROUP) == U_JG_KAF;
  220. }
  221. _FORCE_INLINE_ bool is_lam(char32_t p_chr) {
  222. return u_getIntPropertyValue(p_chr, UCHAR_JOINING_GROUP) == U_JG_LAM;
  223. }
  224. _FORCE_INLINE_ bool is_qaf(char32_t p_chr) {
  225. return (u_getIntPropertyValue(p_chr, UCHAR_JOINING_GROUP) == U_JG_QAF) || (u_getIntPropertyValue(p_chr, UCHAR_JOINING_GROUP) == U_JG_AFRICAN_QAF);
  226. }
  227. _FORCE_INLINE_ bool is_reh(char32_t p_chr) {
  228. return u_getIntPropertyValue(p_chr, UCHAR_JOINING_GROUP) == U_JG_REH;
  229. }
  230. _FORCE_INLINE_ bool is_seen_sad(char32_t p_chr) {
  231. return (u_getIntPropertyValue(p_chr, UCHAR_JOINING_GROUP) == U_JG_SAD) || (u_getIntPropertyValue(p_chr, UCHAR_JOINING_GROUP) == U_JG_SEEN);
  232. }
  233. _FORCE_INLINE_ bool is_tah(char32_t p_chr) {
  234. return u_getIntPropertyValue(p_chr, UCHAR_JOINING_GROUP) == U_JG_TAH;
  235. }
  236. _FORCE_INLINE_ bool is_teh_marbuta(char32_t p_chr) {
  237. return u_getIntPropertyValue(p_chr, UCHAR_JOINING_GROUP) == U_JG_TEH_MARBUTA;
  238. }
  239. _FORCE_INLINE_ bool is_yeh(char32_t p_chr) {
  240. int32_t prop = u_getIntPropertyValue(p_chr, UCHAR_JOINING_GROUP);
  241. return (prop == U_JG_YEH) || (prop == U_JG_FARSI_YEH) || (prop == U_JG_YEH_BARREE) || (prop == U_JG_BURUSHASKI_YEH_BARREE) || (prop == U_JG_YEH_WITH_TAIL);
  242. }
  243. _FORCE_INLINE_ bool is_waw(char32_t p_chr) {
  244. return u_getIntPropertyValue(p_chr, UCHAR_JOINING_GROUP) == U_JG_WAW;
  245. }
  246. _FORCE_INLINE_ bool is_transparent(char32_t p_chr) {
  247. return u_getIntPropertyValue(p_chr, UCHAR_JOINING_TYPE) == U_JT_TRANSPARENT;
  248. }
  249. _FORCE_INLINE_ bool is_ligature(char32_t p_chr, char32_t p_nchr) {
  250. return (is_lam(p_chr) && is_alef(p_nchr));
  251. }
  252. _FORCE_INLINE_ bool is_connected_to_prev(char32_t p_chr, char32_t p_pchr) {
  253. int32_t prop = u_getIntPropertyValue(p_pchr, UCHAR_JOINING_TYPE);
  254. return (prop != U_JT_RIGHT_JOINING) && (prop != U_JT_NON_JOINING) ? !is_ligature(p_pchr, p_chr) : false;
  255. }
  256. /*************************************************************************/
  257. bool TextServerAdvanced::_has_feature(Feature p_feature) const {
  258. switch (p_feature) {
  259. case FEATURE_SIMPLE_LAYOUT:
  260. case FEATURE_BIDI_LAYOUT:
  261. case FEATURE_VERTICAL_LAYOUT:
  262. case FEATURE_SHAPING:
  263. case FEATURE_KASHIDA_JUSTIFICATION:
  264. case FEATURE_BREAK_ITERATORS:
  265. case FEATURE_FONT_BITMAP:
  266. #ifdef MODULE_FREETYPE_ENABLED
  267. case FEATURE_FONT_DYNAMIC:
  268. #endif
  269. #ifdef MODULE_MSDFGEN_ENABLED
  270. case FEATURE_FONT_MSDF:
  271. #endif
  272. case FEATURE_FONT_VARIABLE:
  273. case FEATURE_CONTEXT_SENSITIVE_CASE_CONVERSION:
  274. case FEATURE_USE_SUPPORT_DATA:
  275. case FEATURE_UNICODE_IDENTIFIERS:
  276. case FEATURE_UNICODE_SECURITY:
  277. return true;
  278. default: {
  279. }
  280. }
  281. return false;
  282. }
  283. String TextServerAdvanced::_get_name() const {
  284. #ifdef GDEXTENSION
  285. return "ICU / HarfBuzz / Graphite (GDExtension)";
  286. #else
  287. return "ICU / HarfBuzz / Graphite (Built-in)";
  288. #endif
  289. }
  290. int64_t TextServerAdvanced::_get_features() const {
  291. int64_t interface_features = FEATURE_SIMPLE_LAYOUT | FEATURE_BIDI_LAYOUT | FEATURE_VERTICAL_LAYOUT | FEATURE_SHAPING | FEATURE_KASHIDA_JUSTIFICATION | FEATURE_BREAK_ITERATORS | FEATURE_FONT_BITMAP | FEATURE_FONT_VARIABLE | FEATURE_CONTEXT_SENSITIVE_CASE_CONVERSION | FEATURE_USE_SUPPORT_DATA;
  292. #ifdef MODULE_FREETYPE_ENABLED
  293. interface_features |= FEATURE_FONT_DYNAMIC;
  294. #endif
  295. #ifdef MODULE_MSDFGEN_ENABLED
  296. interface_features |= FEATURE_FONT_MSDF;
  297. #endif
  298. return interface_features;
  299. }
  300. void TextServerAdvanced::_free_rid(const RID &p_rid) {
  301. _THREAD_SAFE_METHOD_
  302. if (font_owner.owns(p_rid)) {
  303. FontAdvanced *fd = font_owner.get_or_null(p_rid);
  304. font_owner.free(p_rid);
  305. memdelete(fd);
  306. } else if (shaped_owner.owns(p_rid)) {
  307. ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_rid);
  308. shaped_owner.free(p_rid);
  309. memdelete(sd);
  310. }
  311. }
  312. bool TextServerAdvanced::_has(const RID &p_rid) {
  313. _THREAD_SAFE_METHOD_
  314. return font_owner.owns(p_rid) || shaped_owner.owns(p_rid);
  315. }
  316. bool TextServerAdvanced::_load_support_data(const String &p_filename) {
  317. _THREAD_SAFE_METHOD_
  318. #ifdef ICU_STATIC_DATA
  319. if (!icu_data_loaded) {
  320. UErrorCode err = U_ZERO_ERROR;
  321. u_init(&err); // Do not check for errors, since we only load part of the data.
  322. icu_data_loaded = true;
  323. }
  324. #else
  325. if (!icu_data_loaded) {
  326. String filename = (p_filename.is_empty()) ? String("res://") + _MKSTR(ICU_DATA_NAME) : p_filename;
  327. Ref<FileAccess> f = FileAccess::open(filename, FileAccess::READ);
  328. if (f.is_null()) {
  329. return false;
  330. }
  331. uint64_t len = f->get_length();
  332. #ifdef GDEXTENSION
  333. PackedByteArray icu_data = f->get_buffer(len);
  334. #else
  335. PackedByteArray icu_data = f->_get_buffer(len);
  336. #endif
  337. UErrorCode err = U_ZERO_ERROR;
  338. udata_setCommonData(icu_data.ptr(), &err);
  339. if (U_FAILURE(err)) {
  340. ERR_FAIL_V_MSG(false, u_errorName(err));
  341. }
  342. err = U_ZERO_ERROR;
  343. u_init(&err);
  344. if (U_FAILURE(err)) {
  345. ERR_FAIL_V_MSG(false, u_errorName(err));
  346. }
  347. icu_data_loaded = true;
  348. }
  349. #endif
  350. return true;
  351. }
  352. String TextServerAdvanced::_get_support_data_filename() const {
  353. #ifdef ICU_STATIC_DATA
  354. return _MKSTR(ICU_DATA_NAME);
  355. #else
  356. return String();
  357. #endif
  358. }
  359. String TextServerAdvanced::_get_support_data_info() const {
  360. #ifdef ICU_STATIC_DATA
  361. return String("ICU break iteration data (") + _MKSTR(ICU_DATA_NAME) + String(").");
  362. #else
  363. return String();
  364. #endif
  365. }
  366. bool TextServerAdvanced::_save_support_data(const String &p_filename) const {
  367. _THREAD_SAFE_METHOD_
  368. #ifdef ICU_STATIC_DATA
  369. // Store data to the res file if it's available.
  370. Ref<FileAccess> f = FileAccess::open(p_filename, FileAccess::WRITE);
  371. if (f.is_null()) {
  372. return false;
  373. }
  374. PackedByteArray icu_data;
  375. icu_data.resize(U_ICUDATA_SIZE);
  376. memcpy(icu_data.ptrw(), U_ICUDATA_ENTRY_POINT, U_ICUDATA_SIZE);
  377. #ifdef GDEXTENSION
  378. f->store_buffer(icu_data);
  379. #else
  380. f->_store_buffer(icu_data);
  381. #endif
  382. return true;
  383. #else
  384. return false;
  385. #endif
  386. }
  387. bool TextServerAdvanced::_is_locale_right_to_left(const String &p_locale) const {
  388. String l = p_locale.get_slicec('_', 0);
  389. if ((l == "ar") || (l == "dv") || (l == "he") || (l == "fa") || (l == "ff") || (l == "ku") || (l == "ur")) {
  390. return true;
  391. } else {
  392. return false;
  393. }
  394. }
  395. _FORCE_INLINE_ void TextServerAdvanced::_insert_feature(const StringName &p_name, int32_t p_tag, Variant::Type p_vtype, bool p_hidden) {
  396. FeatureInfo fi;
  397. fi.name = p_name;
  398. fi.vtype = p_vtype;
  399. fi.hidden = p_hidden;
  400. feature_sets.insert(p_name, p_tag);
  401. feature_sets_inv.insert(p_tag, fi);
  402. }
  403. void TextServerAdvanced::_insert_feature_sets() {
  404. // Registered OpenType feature tags.
  405. // Name, Tag, Data Type, Hidden
  406. _insert_feature("access_all_alternates", HB_TAG('a', 'a', 'l', 't'), Variant::Type::INT, false);
  407. _insert_feature("above_base_forms", HB_TAG('a', 'b', 'v', 'f'), Variant::Type::INT, true);
  408. _insert_feature("above_base_mark_positioning", HB_TAG('a', 'b', 'v', 'm'), Variant::Type::INT, true);
  409. _insert_feature("above_base_substitutions", HB_TAG('a', 'b', 'v', 's'), Variant::Type::INT, true);
  410. _insert_feature("alternative_fractions", HB_TAG('a', 'f', 'r', 'c'), Variant::Type::INT, false);
  411. _insert_feature("akhands", HB_TAG('a', 'k', 'h', 'n'), Variant::Type::INT, true);
  412. _insert_feature("below_base_forms", HB_TAG('b', 'l', 'w', 'f'), Variant::Type::INT, true);
  413. _insert_feature("below_base_mark_positioning", HB_TAG('b', 'l', 'w', 'm'), Variant::Type::INT, true);
  414. _insert_feature("below_base_substitutions", HB_TAG('b', 'l', 'w', 's'), Variant::Type::INT, true);
  415. _insert_feature("contextual_alternates", HB_TAG('c', 'a', 'l', 't'), Variant::Type::BOOL, false);
  416. _insert_feature("case_sensitive_forms", HB_TAG('c', 'a', 's', 'e'), Variant::Type::BOOL, false);
  417. _insert_feature("glyph_composition", HB_TAG('c', 'c', 'm', 'p'), Variant::Type::INT, true);
  418. _insert_feature("conjunct_form_after_ro", HB_TAG('c', 'f', 'a', 'r'), Variant::Type::INT, true);
  419. _insert_feature("contextual_half_width_spacing", HB_TAG('c', 'h', 'w', 's'), Variant::Type::INT, true);
  420. _insert_feature("conjunct_forms", HB_TAG('c', 'j', 'c', 't'), Variant::Type::INT, true);
  421. _insert_feature("contextual_ligatures", HB_TAG('c', 'l', 'i', 'g'), Variant::Type::BOOL, false);
  422. _insert_feature("centered_cjk_punctuation", HB_TAG('c', 'p', 'c', 't'), Variant::Type::BOOL, false);
  423. _insert_feature("capital_spacing", HB_TAG('c', 'p', 's', 'p'), Variant::Type::BOOL, false);
  424. _insert_feature("contextual_swash", HB_TAG('c', 's', 'w', 'h'), Variant::Type::INT, false);
  425. _insert_feature("cursive_positioning", HB_TAG('c', 'u', 'r', 's'), Variant::Type::INT, true);
  426. _insert_feature("character_variant_01", HB_TAG('c', 'v', '0', '1'), Variant::Type::BOOL, false);
  427. _insert_feature("character_variant_02", HB_TAG('c', 'v', '0', '2'), Variant::Type::BOOL, false);
  428. _insert_feature("character_variant_03", HB_TAG('c', 'v', '0', '3'), Variant::Type::BOOL, false);
  429. _insert_feature("character_variant_04", HB_TAG('c', 'v', '0', '4'), Variant::Type::BOOL, false);
  430. _insert_feature("character_variant_05", HB_TAG('c', 'v', '0', '5'), Variant::Type::BOOL, false);
  431. _insert_feature("character_variant_06", HB_TAG('c', 'v', '0', '6'), Variant::Type::BOOL, false);
  432. _insert_feature("character_variant_07", HB_TAG('c', 'v', '0', '7'), Variant::Type::BOOL, false);
  433. _insert_feature("character_variant_08", HB_TAG('c', 'v', '0', '8'), Variant::Type::BOOL, false);
  434. _insert_feature("character_variant_09", HB_TAG('c', 'v', '0', '9'), Variant::Type::BOOL, false);
  435. _insert_feature("character_variant_10", HB_TAG('c', 'v', '1', '0'), Variant::Type::BOOL, false);
  436. _insert_feature("character_variant_11", HB_TAG('c', 'v', '1', '1'), Variant::Type::BOOL, false);
  437. _insert_feature("character_variant_12", HB_TAG('c', 'v', '1', '2'), Variant::Type::BOOL, false);
  438. _insert_feature("character_variant_13", HB_TAG('c', 'v', '1', '3'), Variant::Type::BOOL, false);
  439. _insert_feature("character_variant_14", HB_TAG('c', 'v', '1', '4'), Variant::Type::BOOL, false);
  440. _insert_feature("character_variant_15", HB_TAG('c', 'v', '1', '5'), Variant::Type::BOOL, false);
  441. _insert_feature("character_variant_16", HB_TAG('c', 'v', '1', '6'), Variant::Type::BOOL, false);
  442. _insert_feature("character_variant_17", HB_TAG('c', 'v', '1', '7'), Variant::Type::BOOL, false);
  443. _insert_feature("character_variant_18", HB_TAG('c', 'v', '1', '8'), Variant::Type::BOOL, false);
  444. _insert_feature("character_variant_19", HB_TAG('c', 'v', '1', '9'), Variant::Type::BOOL, false);
  445. _insert_feature("character_variant_20", HB_TAG('c', 'v', '2', '0'), Variant::Type::BOOL, false);
  446. _insert_feature("character_variant_21", HB_TAG('c', 'v', '2', '1'), Variant::Type::BOOL, false);
  447. _insert_feature("character_variant_22", HB_TAG('c', 'v', '2', '2'), Variant::Type::BOOL, false);
  448. _insert_feature("character_variant_23", HB_TAG('c', 'v', '2', '3'), Variant::Type::BOOL, false);
  449. _insert_feature("character_variant_24", HB_TAG('c', 'v', '2', '4'), Variant::Type::BOOL, false);
  450. _insert_feature("character_variant_25", HB_TAG('c', 'v', '2', '5'), Variant::Type::BOOL, false);
  451. _insert_feature("character_variant_26", HB_TAG('c', 'v', '2', '6'), Variant::Type::BOOL, false);
  452. _insert_feature("character_variant_27", HB_TAG('c', 'v', '2', '7'), Variant::Type::BOOL, false);
  453. _insert_feature("character_variant_28", HB_TAG('c', 'v', '2', '8'), Variant::Type::BOOL, false);
  454. _insert_feature("character_variant_29", HB_TAG('c', 'v', '2', '9'), Variant::Type::BOOL, false);
  455. _insert_feature("character_variant_30", HB_TAG('c', 'v', '3', '0'), Variant::Type::BOOL, false);
  456. _insert_feature("character_variant_31", HB_TAG('c', 'v', '3', '1'), Variant::Type::BOOL, false);
  457. _insert_feature("character_variant_32", HB_TAG('c', 'v', '3', '2'), Variant::Type::BOOL, false);
  458. _insert_feature("character_variant_33", HB_TAG('c', 'v', '3', '3'), Variant::Type::BOOL, false);
  459. _insert_feature("character_variant_34", HB_TAG('c', 'v', '3', '4'), Variant::Type::BOOL, false);
  460. _insert_feature("character_variant_35", HB_TAG('c', 'v', '3', '5'), Variant::Type::BOOL, false);
  461. _insert_feature("character_variant_36", HB_TAG('c', 'v', '3', '6'), Variant::Type::BOOL, false);
  462. _insert_feature("character_variant_37", HB_TAG('c', 'v', '3', '7'), Variant::Type::BOOL, false);
  463. _insert_feature("character_variant_38", HB_TAG('c', 'v', '3', '8'), Variant::Type::BOOL, false);
  464. _insert_feature("character_variant_39", HB_TAG('c', 'v', '3', '9'), Variant::Type::BOOL, false);
  465. _insert_feature("character_variant_40", HB_TAG('c', 'v', '4', '0'), Variant::Type::BOOL, false);
  466. _insert_feature("character_variant_41", HB_TAG('c', 'v', '4', '1'), Variant::Type::BOOL, false);
  467. _insert_feature("character_variant_42", HB_TAG('c', 'v', '4', '2'), Variant::Type::BOOL, false);
  468. _insert_feature("character_variant_43", HB_TAG('c', 'v', '4', '3'), Variant::Type::BOOL, false);
  469. _insert_feature("character_variant_44", HB_TAG('c', 'v', '4', '4'), Variant::Type::BOOL, false);
  470. _insert_feature("character_variant_45", HB_TAG('c', 'v', '4', '5'), Variant::Type::BOOL, false);
  471. _insert_feature("character_variant_46", HB_TAG('c', 'v', '4', '6'), Variant::Type::BOOL, false);
  472. _insert_feature("character_variant_47", HB_TAG('c', 'v', '4', '7'), Variant::Type::BOOL, false);
  473. _insert_feature("character_variant_48", HB_TAG('c', 'v', '4', '8'), Variant::Type::BOOL, false);
  474. _insert_feature("character_variant_49", HB_TAG('c', 'v', '4', '9'), Variant::Type::BOOL, false);
  475. _insert_feature("character_variant_50", HB_TAG('c', 'v', '5', '0'), Variant::Type::BOOL, false);
  476. _insert_feature("character_variant_51", HB_TAG('c', 'v', '5', '1'), Variant::Type::BOOL, false);
  477. _insert_feature("character_variant_52", HB_TAG('c', 'v', '5', '2'), Variant::Type::BOOL, false);
  478. _insert_feature("character_variant_53", HB_TAG('c', 'v', '5', '3'), Variant::Type::BOOL, false);
  479. _insert_feature("character_variant_54", HB_TAG('c', 'v', '5', '4'), Variant::Type::BOOL, false);
  480. _insert_feature("character_variant_55", HB_TAG('c', 'v', '5', '5'), Variant::Type::BOOL, false);
  481. _insert_feature("character_variant_56", HB_TAG('c', 'v', '5', '6'), Variant::Type::BOOL, false);
  482. _insert_feature("character_variant_57", HB_TAG('c', 'v', '5', '7'), Variant::Type::BOOL, false);
  483. _insert_feature("character_variant_58", HB_TAG('c', 'v', '5', '8'), Variant::Type::BOOL, false);
  484. _insert_feature("character_variant_59", HB_TAG('c', 'v', '5', '9'), Variant::Type::BOOL, false);
  485. _insert_feature("character_variant_60", HB_TAG('c', 'v', '6', '0'), Variant::Type::BOOL, false);
  486. _insert_feature("character_variant_61", HB_TAG('c', 'v', '6', '1'), Variant::Type::BOOL, false);
  487. _insert_feature("character_variant_62", HB_TAG('c', 'v', '6', '2'), Variant::Type::BOOL, false);
  488. _insert_feature("character_variant_63", HB_TAG('c', 'v', '6', '3'), Variant::Type::BOOL, false);
  489. _insert_feature("character_variant_64", HB_TAG('c', 'v', '6', '4'), Variant::Type::BOOL, false);
  490. _insert_feature("character_variant_65", HB_TAG('c', 'v', '6', '5'), Variant::Type::BOOL, false);
  491. _insert_feature("character_variant_66", HB_TAG('c', 'v', '6', '6'), Variant::Type::BOOL, false);
  492. _insert_feature("character_variant_67", HB_TAG('c', 'v', '6', '7'), Variant::Type::BOOL, false);
  493. _insert_feature("character_variant_68", HB_TAG('c', 'v', '6', '8'), Variant::Type::BOOL, false);
  494. _insert_feature("character_variant_69", HB_TAG('c', 'v', '6', '9'), Variant::Type::BOOL, false);
  495. _insert_feature("character_variant_70", HB_TAG('c', 'v', '7', '0'), Variant::Type::BOOL, false);
  496. _insert_feature("character_variant_71", HB_TAG('c', 'v', '7', '1'), Variant::Type::BOOL, false);
  497. _insert_feature("character_variant_72", HB_TAG('c', 'v', '7', '2'), Variant::Type::BOOL, false);
  498. _insert_feature("character_variant_73", HB_TAG('c', 'v', '7', '3'), Variant::Type::BOOL, false);
  499. _insert_feature("character_variant_74", HB_TAG('c', 'v', '7', '4'), Variant::Type::BOOL, false);
  500. _insert_feature("character_variant_75", HB_TAG('c', 'v', '7', '5'), Variant::Type::BOOL, false);
  501. _insert_feature("character_variant_76", HB_TAG('c', 'v', '7', '6'), Variant::Type::BOOL, false);
  502. _insert_feature("character_variant_77", HB_TAG('c', 'v', '7', '7'), Variant::Type::BOOL, false);
  503. _insert_feature("character_variant_78", HB_TAG('c', 'v', '7', '8'), Variant::Type::BOOL, false);
  504. _insert_feature("character_variant_79", HB_TAG('c', 'v', '7', '9'), Variant::Type::BOOL, false);
  505. _insert_feature("character_variant_80", HB_TAG('c', 'v', '8', '0'), Variant::Type::BOOL, false);
  506. _insert_feature("character_variant_81", HB_TAG('c', 'v', '8', '1'), Variant::Type::BOOL, false);
  507. _insert_feature("character_variant_82", HB_TAG('c', 'v', '8', '2'), Variant::Type::BOOL, false);
  508. _insert_feature("character_variant_83", HB_TAG('c', 'v', '8', '3'), Variant::Type::BOOL, false);
  509. _insert_feature("character_variant_84", HB_TAG('c', 'v', '8', '4'), Variant::Type::BOOL, false);
  510. _insert_feature("character_variant_85", HB_TAG('c', 'v', '8', '5'), Variant::Type::BOOL, false);
  511. _insert_feature("character_variant_86", HB_TAG('c', 'v', '8', '6'), Variant::Type::BOOL, false);
  512. _insert_feature("character_variant_87", HB_TAG('c', 'v', '8', '7'), Variant::Type::BOOL, false);
  513. _insert_feature("character_variant_88", HB_TAG('c', 'v', '8', '8'), Variant::Type::BOOL, false);
  514. _insert_feature("character_variant_89", HB_TAG('c', 'v', '8', '9'), Variant::Type::BOOL, false);
  515. _insert_feature("character_variant_90", HB_TAG('c', 'v', '9', '0'), Variant::Type::BOOL, false);
  516. _insert_feature("character_variant_91", HB_TAG('c', 'v', '9', '1'), Variant::Type::BOOL, false);
  517. _insert_feature("character_variant_92", HB_TAG('c', 'v', '9', '2'), Variant::Type::BOOL, false);
  518. _insert_feature("character_variant_93", HB_TAG('c', 'v', '9', '3'), Variant::Type::BOOL, false);
  519. _insert_feature("character_variant_94", HB_TAG('c', 'v', '9', '4'), Variant::Type::BOOL, false);
  520. _insert_feature("character_variant_95", HB_TAG('c', 'v', '9', '5'), Variant::Type::BOOL, false);
  521. _insert_feature("character_variant_96", HB_TAG('c', 'v', '9', '6'), Variant::Type::BOOL, false);
  522. _insert_feature("character_variant_97", HB_TAG('c', 'v', '9', '7'), Variant::Type::BOOL, false);
  523. _insert_feature("character_variant_98", HB_TAG('c', 'v', '9', '8'), Variant::Type::BOOL, false);
  524. _insert_feature("character_variant_99", HB_TAG('c', 'v', '9', '9'), Variant::Type::BOOL, false);
  525. _insert_feature("petite_capitals_from_capitals", HB_TAG('c', '2', 'p', 'c'), Variant::Type::BOOL, false);
  526. _insert_feature("small_capitals_from_capitals", HB_TAG('c', '2', 's', 'c'), Variant::Type::BOOL, false);
  527. _insert_feature("distances", HB_TAG('d', 'i', 's', 't'), Variant::Type::INT, true);
  528. _insert_feature("discretionary_ligatures", HB_TAG('d', 'l', 'i', 'g'), Variant::Type::BOOL, false);
  529. _insert_feature("denominators", HB_TAG('d', 'n', 'o', 'm'), Variant::Type::BOOL, false);
  530. _insert_feature("dotless_forms", HB_TAG('d', 't', 'l', 's'), Variant::Type::INT, true);
  531. _insert_feature("expert_forms", HB_TAG('e', 'x', 'p', 't'), Variant::Type::BOOL, true);
  532. _insert_feature("final_glyph_on_line_alternates", HB_TAG('f', 'a', 'l', 't'), Variant::Type::INT, false);
  533. _insert_feature("terminal_forms_2", HB_TAG('f', 'i', 'n', '2'), Variant::Type::INT, true);
  534. _insert_feature("terminal_forms_3", HB_TAG('f', 'i', 'n', '3'), Variant::Type::INT, true);
  535. _insert_feature("terminal_forms", HB_TAG('f', 'i', 'n', 'a'), Variant::Type::INT, true);
  536. _insert_feature("flattened_accent_forms", HB_TAG('f', 'l', 'a', 'c'), Variant::Type::INT, true);
  537. _insert_feature("fractions", HB_TAG('f', 'r', 'a', 'c'), Variant::Type::BOOL, false);
  538. _insert_feature("full_widths", HB_TAG('f', 'w', 'i', 'd'), Variant::Type::BOOL, false);
  539. _insert_feature("half_forms", HB_TAG('h', 'a', 'l', 'f'), Variant::Type::INT, true);
  540. _insert_feature("halant_forms", HB_TAG('h', 'a', 'l', 'n'), Variant::Type::INT, true);
  541. _insert_feature("alternate_half_widths", HB_TAG('h', 'a', 'l', 't'), Variant::Type::BOOL, false);
  542. _insert_feature("historical_forms", HB_TAG('h', 'i', 's', 't'), Variant::Type::INT, false);
  543. _insert_feature("horizontal_kana_alternates", HB_TAG('h', 'k', 'n', 'a'), Variant::Type::BOOL, false);
  544. _insert_feature("historical_ligatures", HB_TAG('h', 'l', 'i', 'g'), Variant::Type::BOOL, false);
  545. _insert_feature("hangul", HB_TAG('h', 'n', 'g', 'l'), Variant::Type::INT, false);
  546. _insert_feature("hojo_kanji_forms", HB_TAG('h', 'o', 'j', 'o'), Variant::Type::INT, false);
  547. _insert_feature("half_widths", HB_TAG('h', 'w', 'i', 'd'), Variant::Type::BOOL, false);
  548. _insert_feature("initial_forms", HB_TAG('i', 'n', 'i', 't'), Variant::Type::INT, true);
  549. _insert_feature("isolated_forms", HB_TAG('i', 's', 'o', 'l'), Variant::Type::INT, true);
  550. _insert_feature("italics", HB_TAG('i', 't', 'a', 'l'), Variant::Type::INT, false);
  551. _insert_feature("justification_alternates", HB_TAG('j', 'a', 'l', 't'), Variant::Type::INT, false);
  552. _insert_feature("jis78_forms", HB_TAG('j', 'p', '7', '8'), Variant::Type::INT, false);
  553. _insert_feature("jis83_forms", HB_TAG('j', 'p', '8', '3'), Variant::Type::INT, false);
  554. _insert_feature("jis90_forms", HB_TAG('j', 'p', '9', '0'), Variant::Type::INT, false);
  555. _insert_feature("jis2004_forms", HB_TAG('j', 'p', '0', '4'), Variant::Type::INT, false);
  556. _insert_feature("kerning", HB_TAG('k', 'e', 'r', 'n'), Variant::Type::BOOL, false);
  557. _insert_feature("left_bounds", HB_TAG('l', 'f', 'b', 'd'), Variant::Type::INT, false);
  558. _insert_feature("standard_ligatures", HB_TAG('l', 'i', 'g', 'a'), Variant::Type::BOOL, false);
  559. _insert_feature("leading_jamo_forms", HB_TAG('l', 'j', 'm', 'o'), Variant::Type::INT, true);
  560. _insert_feature("lining_figures", HB_TAG('l', 'n', 'u', 'm'), Variant::Type::INT, false);
  561. _insert_feature("localized_forms", HB_TAG('l', 'o', 'c', 'l'), Variant::Type::INT, true);
  562. _insert_feature("left_to_right_alternates", HB_TAG('l', 't', 'r', 'a'), Variant::Type::INT, true);
  563. _insert_feature("left_to_right_mirrored_forms", HB_TAG('l', 't', 'r', 'm'), Variant::Type::INT, true);
  564. _insert_feature("mark_positioning", HB_TAG('m', 'a', 'r', 'k'), Variant::Type::INT, true);
  565. _insert_feature("medial_forms_2", HB_TAG('m', 'e', 'd', '2'), Variant::Type::INT, true);
  566. _insert_feature("medial_forms", HB_TAG('m', 'e', 'd', 'i'), Variant::Type::INT, true);
  567. _insert_feature("mathematical_greek", HB_TAG('m', 'g', 'r', 'k'), Variant::Type::BOOL, false);
  568. _insert_feature("mark_to_mark_positioning", HB_TAG('m', 'k', 'm', 'k'), Variant::Type::INT, true);
  569. _insert_feature("mark_positioning_via_substitution", HB_TAG('m', 's', 'e', 't'), Variant::Type::INT, true);
  570. _insert_feature("alternate_annotation_forms", HB_TAG('n', 'a', 'l', 't'), Variant::Type::INT, false);
  571. _insert_feature("nlc_kanji_forms", HB_TAG('n', 'l', 'c', 'k'), Variant::Type::INT, false);
  572. _insert_feature("nukta_forms", HB_TAG('n', 'u', 'k', 't'), Variant::Type::INT, true);
  573. _insert_feature("numerators", HB_TAG('n', 'u', 'm', 'r'), Variant::Type::BOOL, false);
  574. _insert_feature("oldstyle_figures", HB_TAG('o', 'n', 'u', 'm'), Variant::Type::INT, false);
  575. _insert_feature("optical_bounds", HB_TAG('o', 'p', 'b', 'd'), Variant::Type::INT, true);
  576. _insert_feature("ordinals", HB_TAG('o', 'r', 'd', 'n'), Variant::Type::BOOL, false);
  577. _insert_feature("ornaments", HB_TAG('o', 'r', 'n', 'm'), Variant::Type::INT, false);
  578. _insert_feature("proportional_alternate_widths", HB_TAG('p', 'a', 'l', 't'), Variant::Type::BOOL, false);
  579. _insert_feature("petite_capitals", HB_TAG('p', 'c', 'a', 'p'), Variant::Type::BOOL, false);
  580. _insert_feature("proportional_kana", HB_TAG('p', 'k', 'n', 'a'), Variant::Type::BOOL, false);
  581. _insert_feature("proportional_figures", HB_TAG('p', 'n', 'u', 'm'), Variant::Type::BOOL, false);
  582. _insert_feature("pre_base_forms", HB_TAG('p', 'r', 'e', 'f'), Variant::Type::INT, true);
  583. _insert_feature("pre_base_substitutions", HB_TAG('p', 'r', 'e', 's'), Variant::Type::INT, true);
  584. _insert_feature("post_base_forms", HB_TAG('p', 's', 't', 'f'), Variant::Type::INT, true);
  585. _insert_feature("post_base_substitutions", HB_TAG('p', 's', 't', 's'), Variant::Type::INT, true);
  586. _insert_feature("proportional_widths", HB_TAG('p', 'w', 'i', 'd'), Variant::Type::BOOL, false);
  587. _insert_feature("quarter_widths", HB_TAG('q', 'w', 'i', 'd'), Variant::Type::BOOL, false);
  588. _insert_feature("randomize", HB_TAG('r', 'a', 'n', 'd'), Variant::Type::INT, false);
  589. _insert_feature("required_contextual_alternates", HB_TAG('r', 'c', 'l', 't'), Variant::Type::BOOL, true);
  590. _insert_feature("rakar_forms", HB_TAG('r', 'k', 'r', 'f'), Variant::Type::INT, true);
  591. _insert_feature("required_ligatures", HB_TAG('r', 'l', 'i', 'g'), Variant::Type::BOOL, true);
  592. _insert_feature("reph_forms", HB_TAG('r', 'p', 'h', 'f'), Variant::Type::INT, true);
  593. _insert_feature("right_bounds", HB_TAG('r', 't', 'b', 'd'), Variant::Type::INT, false);
  594. _insert_feature("right_to_left_alternates", HB_TAG('r', 't', 'l', 'a'), Variant::Type::INT, true);
  595. _insert_feature("right_to_left_mirrored_forms", HB_TAG('r', 't', 'l', 'm'), Variant::Type::INT, true);
  596. _insert_feature("ruby_notation_forms", HB_TAG('r', 'u', 'b', 'y'), Variant::Type::INT, false);
  597. _insert_feature("required_variation_alternates", HB_TAG('r', 'v', 'r', 'n'), Variant::Type::INT, true);
  598. _insert_feature("stylistic_alternates", HB_TAG('s', 'a', 'l', 't'), Variant::Type::INT, false);
  599. _insert_feature("scientific_inferiors", HB_TAG('s', 'i', 'n', 'f'), Variant::Type::BOOL, false);
  600. _insert_feature("optical_size", HB_TAG('s', 'i', 'z', 'e'), Variant::Type::INT, false);
  601. _insert_feature("small_capitals", HB_TAG('s', 'm', 'c', 'p'), Variant::Type::BOOL, false);
  602. _insert_feature("simplified_forms", HB_TAG('s', 'm', 'p', 'l'), Variant::Type::INT, false);
  603. _insert_feature("stylistic_set_01", HB_TAG('s', 's', '0', '1'), Variant::Type::BOOL, false);
  604. _insert_feature("stylistic_set_02", HB_TAG('s', 's', '0', '2'), Variant::Type::BOOL, false);
  605. _insert_feature("stylistic_set_03", HB_TAG('s', 's', '0', '3'), Variant::Type::BOOL, false);
  606. _insert_feature("stylistic_set_04", HB_TAG('s', 's', '0', '4'), Variant::Type::BOOL, false);
  607. _insert_feature("stylistic_set_05", HB_TAG('s', 's', '0', '5'), Variant::Type::BOOL, false);
  608. _insert_feature("stylistic_set_06", HB_TAG('s', 's', '0', '6'), Variant::Type::BOOL, false);
  609. _insert_feature("stylistic_set_07", HB_TAG('s', 's', '0', '7'), Variant::Type::BOOL, false);
  610. _insert_feature("stylistic_set_08", HB_TAG('s', 's', '0', '8'), Variant::Type::BOOL, false);
  611. _insert_feature("stylistic_set_09", HB_TAG('s', 's', '0', '9'), Variant::Type::BOOL, false);
  612. _insert_feature("stylistic_set_10", HB_TAG('s', 's', '1', '0'), Variant::Type::BOOL, false);
  613. _insert_feature("stylistic_set_11", HB_TAG('s', 's', '1', '1'), Variant::Type::BOOL, false);
  614. _insert_feature("stylistic_set_12", HB_TAG('s', 's', '1', '2'), Variant::Type::BOOL, false);
  615. _insert_feature("stylistic_set_13", HB_TAG('s', 's', '1', '3'), Variant::Type::BOOL, false);
  616. _insert_feature("stylistic_set_14", HB_TAG('s', 's', '1', '4'), Variant::Type::BOOL, false);
  617. _insert_feature("stylistic_set_15", HB_TAG('s', 's', '1', '5'), Variant::Type::BOOL, false);
  618. _insert_feature("stylistic_set_16", HB_TAG('s', 's', '1', '6'), Variant::Type::BOOL, false);
  619. _insert_feature("stylistic_set_17", HB_TAG('s', 's', '1', '7'), Variant::Type::BOOL, false);
  620. _insert_feature("stylistic_set_18", HB_TAG('s', 's', '1', '8'), Variant::Type::BOOL, false);
  621. _insert_feature("stylistic_set_19", HB_TAG('s', 's', '1', '9'), Variant::Type::BOOL, false);
  622. _insert_feature("stylistic_set_20", HB_TAG('s', 's', '2', '0'), Variant::Type::BOOL, false);
  623. _insert_feature("math_script_style_alternates", HB_TAG('s', 's', 't', 'y'), Variant::Type::INT, true);
  624. _insert_feature("stretching_glyph_decomposition", HB_TAG('s', 't', 'c', 'h'), Variant::Type::INT, true);
  625. _insert_feature("subscript", HB_TAG('s', 'u', 'b', 's'), Variant::Type::BOOL, false);
  626. _insert_feature("superscript", HB_TAG('s', 'u', 'p', 's'), Variant::Type::BOOL, false);
  627. _insert_feature("swash", HB_TAG('s', 'w', 's', 'h'), Variant::Type::INT, false);
  628. _insert_feature("titling", HB_TAG('t', 'i', 't', 'l'), Variant::Type::BOOL, false);
  629. _insert_feature("trailing_jamo_forms", HB_TAG('t', 'j', 'm', 'o'), Variant::Type::INT, true);
  630. _insert_feature("traditional_name_forms", HB_TAG('t', 'n', 'a', 'm'), Variant::Type::INT, false);
  631. _insert_feature("tabular_figures", HB_TAG('t', 'n', 'u', 'm'), Variant::Type::BOOL, false);
  632. _insert_feature("traditional_forms", HB_TAG('t', 'r', 'a', 'd'), Variant::Type::INT, false);
  633. _insert_feature("third_widths", HB_TAG('t', 'w', 'i', 'd'), Variant::Type::BOOL, false);
  634. _insert_feature("unicase", HB_TAG('u', 'n', 'i', 'c'), Variant::Type::BOOL, false);
  635. _insert_feature("alternate_vertical_metrics", HB_TAG('v', 'a', 'l', 't'), Variant::Type::INT, false);
  636. _insert_feature("vattu_variants", HB_TAG('v', 'a', 't', 'u'), Variant::Type::INT, true);
  637. _insert_feature("vertical_contextual_half_width_spacing", HB_TAG('v', 'c', 'h', 'w'), Variant::Type::BOOL, false);
  638. _insert_feature("vertical_alternates", HB_TAG('v', 'e', 'r', 't'), Variant::Type::INT, false);
  639. _insert_feature("alternate_vertical_half_metrics", HB_TAG('v', 'h', 'a', 'l'), Variant::Type::BOOL, false);
  640. _insert_feature("vowel_jamo_forms", HB_TAG('v', 'j', 'm', 'o'), Variant::Type::INT, true);
  641. _insert_feature("vertical_kana_alternates", HB_TAG('v', 'k', 'n', 'a'), Variant::Type::INT, false);
  642. _insert_feature("vertical_kerning", HB_TAG('v', 'k', 'r', 'n'), Variant::Type::BOOL, false);
  643. _insert_feature("proportional_alternate_vertical_metrics", HB_TAG('v', 'p', 'a', 'l'), Variant::Type::BOOL, false);
  644. _insert_feature("vertical_alternates_and_rotation", HB_TAG('v', 'r', 't', '2'), Variant::Type::INT, false);
  645. _insert_feature("vertical_alternates_for_rotation", HB_TAG('v', 'r', 't', 'r'), Variant::Type::INT, false);
  646. _insert_feature("slashed_zero", HB_TAG('z', 'e', 'r', 'o'), Variant::Type::BOOL, false);
  647. // Registered OpenType variation tag.
  648. _insert_feature("italic", HB_TAG('i', 't', 'a', 'l'), Variant::Type::INT, false);
  649. _insert_feature("optical_size", HB_TAG('o', 'p', 's', 'z'), Variant::Type::INT, false);
  650. _insert_feature("slant", HB_TAG('s', 'l', 'n', 't'), Variant::Type::INT, false);
  651. _insert_feature("width", HB_TAG('w', 'd', 't', 'h'), Variant::Type::INT, false);
  652. _insert_feature("weight", HB_TAG('w', 'g', 'h', 't'), Variant::Type::INT, false);
  653. }
  654. int64_t TextServerAdvanced::_name_to_tag(const String &p_name) const {
  655. if (feature_sets.has(p_name)) {
  656. return feature_sets[p_name];
  657. }
  658. // No readable name, use tag string.
  659. return hb_tag_from_string(p_name.replace("custom_", "").ascii().get_data(), -1);
  660. }
  661. Variant::Type TextServerAdvanced::_get_tag_type(int64_t p_tag) const {
  662. if (feature_sets_inv.has(p_tag)) {
  663. return feature_sets_inv[p_tag].vtype;
  664. }
  665. return Variant::Type::INT;
  666. }
  667. bool TextServerAdvanced::_get_tag_hidden(int64_t p_tag) const {
  668. if (feature_sets_inv.has(p_tag)) {
  669. return feature_sets_inv[p_tag].hidden;
  670. }
  671. return false;
  672. }
  673. String TextServerAdvanced::_tag_to_name(int64_t p_tag) const {
  674. if (feature_sets_inv.has(p_tag)) {
  675. return feature_sets_inv[p_tag].name;
  676. }
  677. // No readable name, use tag string.
  678. char name[5];
  679. memset(name, 0, 5);
  680. hb_tag_to_string(p_tag, name);
  681. return String("custom_") + String(name);
  682. }
  683. /*************************************************************************/
  684. /* Font Glyph Rendering */
  685. /*************************************************************************/
  686. _FORCE_INLINE_ TextServerAdvanced::FontTexturePosition TextServerAdvanced::find_texture_pos_for_glyph(FontForSizeAdvanced *p_data, int p_color_size, Image::Format p_image_format, int p_width, int p_height, bool p_msdf) const {
  687. FontTexturePosition ret;
  688. int mw = p_width;
  689. int mh = p_height;
  690. ShelfPackTexture *ct = p_data->textures.ptrw();
  691. for (int32_t i = 0; i < p_data->textures.size(); i++) {
  692. if (p_image_format != ct[i].format) {
  693. continue;
  694. }
  695. if (mw > ct[i].texture_w || mh > ct[i].texture_h) { // Too big for this texture.
  696. continue;
  697. }
  698. ret = ct[i].pack_rect(i, mh, mw);
  699. if (ret.index != -1) {
  700. break;
  701. }
  702. }
  703. if (ret.index == -1) {
  704. // Could not find texture to fit, create one.
  705. int texsize = MAX(p_data->size.x * p_data->oversampling * 8, 256);
  706. #ifdef GDEXTENSION
  707. texsize = Math::next_power_of_2(texsize);
  708. #else
  709. texsize = next_power_of_2(texsize);
  710. #endif
  711. if (p_msdf) {
  712. texsize = MIN(texsize, 2048);
  713. } else {
  714. texsize = MIN(texsize, 1024);
  715. }
  716. if (mw > texsize) { // Special case, adapt to it?
  717. #ifdef GDEXTENSION
  718. texsize = Math::next_power_of_2(mw);
  719. #else
  720. texsize = next_power_of_2(mw);
  721. #endif
  722. }
  723. if (mh > texsize) { // Special case, adapt to it?
  724. #ifdef GDEXTENSION
  725. texsize = Math::next_power_of_2(mh);
  726. #else
  727. texsize = next_power_of_2(mh);
  728. #endif
  729. }
  730. ShelfPackTexture tex = ShelfPackTexture(texsize, texsize);
  731. tex.format = p_image_format;
  732. tex.imgdata.resize(texsize * texsize * p_color_size);
  733. {
  734. // Zero texture.
  735. uint8_t *w = tex.imgdata.ptrw();
  736. ERR_FAIL_COND_V(texsize * texsize * p_color_size > tex.imgdata.size(), ret);
  737. // Initialize the texture to all-white pixels to prevent artifacts when the
  738. // font is displayed at a non-default scale with filtering enabled.
  739. if (p_color_size == 2) {
  740. for (int i = 0; i < texsize * texsize * p_color_size; i += 2) { // FORMAT_LA8, BW font.
  741. w[i + 0] = 255;
  742. w[i + 1] = 0;
  743. }
  744. } else if (p_color_size == 4) {
  745. for (int i = 0; i < texsize * texsize * p_color_size; i += 4) { // FORMAT_RGBA8, Color font, Multichannel(+True) SDF.
  746. w[i + 0] = 255;
  747. w[i + 1] = 255;
  748. w[i + 2] = 255;
  749. w[i + 3] = 0;
  750. }
  751. } else {
  752. ERR_FAIL_V(ret);
  753. }
  754. }
  755. p_data->textures.push_back(tex);
  756. int32_t idx = p_data->textures.size() - 1;
  757. ret = p_data->textures.write[idx].pack_rect(idx, mh, mw);
  758. }
  759. return ret;
  760. }
  761. #ifdef MODULE_MSDFGEN_ENABLED
  762. struct MSContext {
  763. msdfgen::Point2 position;
  764. msdfgen::Shape *shape = nullptr;
  765. msdfgen::Contour *contour = nullptr;
  766. };
  767. class DistancePixelConversion {
  768. double invRange;
  769. public:
  770. _FORCE_INLINE_ explicit DistancePixelConversion(double range) :
  771. invRange(1 / range) {}
  772. _FORCE_INLINE_ void operator()(float *pixels, const msdfgen::MultiAndTrueDistance &distance) const {
  773. pixels[0] = float(invRange * distance.r + .5);
  774. pixels[1] = float(invRange * distance.g + .5);
  775. pixels[2] = float(invRange * distance.b + .5);
  776. pixels[3] = float(invRange * distance.a + .5);
  777. }
  778. };
  779. struct MSDFThreadData {
  780. msdfgen::Bitmap<float, 4> *output;
  781. msdfgen::Shape *shape;
  782. msdfgen::Projection *projection;
  783. DistancePixelConversion *distancePixelConversion;
  784. };
  785. static msdfgen::Point2 ft_point2(const FT_Vector &vector) {
  786. return msdfgen::Point2(vector.x / 60.0f, vector.y / 60.0f);
  787. }
  788. static int ft_move_to(const FT_Vector *to, void *user) {
  789. MSContext *context = static_cast<MSContext *>(user);
  790. if (!(context->contour && context->contour->edges.empty())) {
  791. context->contour = &context->shape->addContour();
  792. }
  793. context->position = ft_point2(*to);
  794. return 0;
  795. }
  796. static int ft_line_to(const FT_Vector *to, void *user) {
  797. MSContext *context = static_cast<MSContext *>(user);
  798. msdfgen::Point2 endpoint = ft_point2(*to);
  799. if (endpoint != context->position) {
  800. context->contour->addEdge(new msdfgen::LinearSegment(context->position, endpoint));
  801. context->position = endpoint;
  802. }
  803. return 0;
  804. }
  805. static int ft_conic_to(const FT_Vector *control, const FT_Vector *to, void *user) {
  806. MSContext *context = static_cast<MSContext *>(user);
  807. context->contour->addEdge(new msdfgen::QuadraticSegment(context->position, ft_point2(*control), ft_point2(*to)));
  808. context->position = ft_point2(*to);
  809. return 0;
  810. }
  811. static int ft_cubic_to(const FT_Vector *control1, const FT_Vector *control2, const FT_Vector *to, void *user) {
  812. MSContext *context = static_cast<MSContext *>(user);
  813. context->contour->addEdge(new msdfgen::CubicSegment(context->position, ft_point2(*control1), ft_point2(*control2), ft_point2(*to)));
  814. context->position = ft_point2(*to);
  815. return 0;
  816. }
  817. void TextServerAdvanced::_generateMTSDF_threaded(uint32_t y, void *p_td) const {
  818. MSDFThreadData *td = static_cast<MSDFThreadData *>(p_td);
  819. msdfgen::ShapeDistanceFinder<msdfgen::OverlappingContourCombiner<msdfgen::MultiAndTrueDistanceSelector>> distanceFinder(*td->shape);
  820. int row = td->shape->inverseYAxis ? td->output->height() - y - 1 : y;
  821. for (int col = 0; col < td->output->width(); ++col) {
  822. int x = (y % 2) ? td->output->width() - col - 1 : col;
  823. msdfgen::Point2 p = td->projection->unproject(msdfgen::Point2(x + .5, y + .5));
  824. msdfgen::MultiAndTrueDistance distance = distanceFinder.distance(p);
  825. td->distancePixelConversion->operator()(td->output->operator()(x, row), distance);
  826. }
  827. }
  828. _FORCE_INLINE_ TextServerAdvanced::FontGlyph TextServerAdvanced::rasterize_msdf(FontAdvanced *p_font_data, FontForSizeAdvanced *p_data, int p_pixel_range, int p_rect_margin, FT_Outline *outline, const Vector2 &advance) const {
  829. msdfgen::Shape shape;
  830. shape.contours.clear();
  831. shape.inverseYAxis = false;
  832. MSContext context = {};
  833. context.shape = &shape;
  834. FT_Outline_Funcs ft_functions;
  835. ft_functions.move_to = &ft_move_to;
  836. ft_functions.line_to = &ft_line_to;
  837. ft_functions.conic_to = &ft_conic_to;
  838. ft_functions.cubic_to = &ft_cubic_to;
  839. ft_functions.shift = 0;
  840. ft_functions.delta = 0;
  841. int error = FT_Outline_Decompose(outline, &ft_functions, &context);
  842. ERR_FAIL_COND_V_MSG(error, FontGlyph(), "FreeType: Outline decomposition error: '" + String(FT_Error_String(error)) + "'.");
  843. if (!shape.contours.empty() && shape.contours.back().edges.empty()) {
  844. shape.contours.pop_back();
  845. }
  846. if (FT_Outline_Get_Orientation(outline) == 1) {
  847. for (int i = 0; i < (int)shape.contours.size(); ++i) {
  848. shape.contours[i].reverse();
  849. }
  850. }
  851. shape.inverseYAxis = true;
  852. shape.normalize();
  853. msdfgen::Shape::Bounds bounds = shape.getBounds(p_pixel_range);
  854. FontGlyph chr;
  855. chr.found = true;
  856. chr.advance = advance;
  857. if (shape.validate() && shape.contours.size() > 0) {
  858. int w = (bounds.r - bounds.l);
  859. int h = (bounds.t - bounds.b);
  860. int mw = w + p_rect_margin * 4;
  861. int mh = h + p_rect_margin * 4;
  862. ERR_FAIL_COND_V(mw > 4096, FontGlyph());
  863. ERR_FAIL_COND_V(mh > 4096, FontGlyph());
  864. FontTexturePosition tex_pos = find_texture_pos_for_glyph(p_data, 4, Image::FORMAT_RGBA8, mw, mh, true);
  865. ERR_FAIL_COND_V(tex_pos.index < 0, FontGlyph());
  866. ShelfPackTexture &tex = p_data->textures.write[tex_pos.index];
  867. edgeColoringSimple(shape, 3.0); // Max. angle.
  868. msdfgen::Bitmap<float, 4> image(w, h); // Texture size.
  869. DistancePixelConversion distancePixelConversion(p_pixel_range);
  870. msdfgen::Projection projection(msdfgen::Vector2(1.0, 1.0), msdfgen::Vector2(-bounds.l, -bounds.b));
  871. msdfgen::MSDFGeneratorConfig config(true, msdfgen::ErrorCorrectionConfig());
  872. MSDFThreadData td;
  873. td.output = &image;
  874. td.shape = &shape;
  875. td.projection = &projection;
  876. td.distancePixelConversion = &distancePixelConversion;
  877. #ifdef GDEXTENSION
  878. for (int i = 0; i < h; i++) {
  879. _generateMTSDF_threaded(i, &td);
  880. }
  881. #else
  882. WorkerThreadPool::GroupID group_task = WorkerThreadPool::get_singleton()->add_template_group_task(this, &TextServerAdvanced::_generateMTSDF_threaded, &td, h, -1, true, SNAME("FontServerRasterizeMSDF"));
  883. WorkerThreadPool::get_singleton()->wait_for_group_task_completion(group_task);
  884. #endif
  885. msdfgen::msdfErrorCorrection(image, shape, projection, p_pixel_range, config);
  886. {
  887. uint8_t *wr = tex.imgdata.ptrw();
  888. for (int i = 0; i < h; i++) {
  889. for (int j = 0; j < w; j++) {
  890. int ofs = ((i + tex_pos.y + p_rect_margin * 2) * tex.texture_w + j + tex_pos.x + p_rect_margin * 2) * 4;
  891. ERR_FAIL_COND_V(ofs >= tex.imgdata.size(), FontGlyph());
  892. wr[ofs + 0] = (uint8_t)(CLAMP(image(j, i)[0] * 256.f, 0.f, 255.f));
  893. wr[ofs + 1] = (uint8_t)(CLAMP(image(j, i)[1] * 256.f, 0.f, 255.f));
  894. wr[ofs + 2] = (uint8_t)(CLAMP(image(j, i)[2] * 256.f, 0.f, 255.f));
  895. wr[ofs + 3] = (uint8_t)(CLAMP(image(j, i)[3] * 256.f, 0.f, 255.f));
  896. }
  897. }
  898. }
  899. tex.dirty = true;
  900. chr.texture_idx = tex_pos.index;
  901. 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);
  902. chr.rect.position = Vector2(bounds.l - p_rect_margin, -bounds.t - p_rect_margin);
  903. chr.rect.size = chr.uv_rect.size;
  904. }
  905. return chr;
  906. }
  907. #endif
  908. #ifdef MODULE_FREETYPE_ENABLED
  909. _FORCE_INLINE_ TextServerAdvanced::FontGlyph TextServerAdvanced::rasterize_bitmap(FontForSizeAdvanced *p_data, int p_rect_margin, FT_Bitmap bitmap, int yofs, int xofs, const Vector2 &advance, bool p_bgra) const {
  910. int w = bitmap.width;
  911. int h = bitmap.rows;
  912. int color_size = 2;
  913. switch (bitmap.pixel_mode) {
  914. case FT_PIXEL_MODE_MONO:
  915. case FT_PIXEL_MODE_GRAY: {
  916. color_size = 2;
  917. } break;
  918. case FT_PIXEL_MODE_BGRA: {
  919. color_size = 4;
  920. } break;
  921. case FT_PIXEL_MODE_LCD: {
  922. color_size = 4;
  923. w /= 3;
  924. } break;
  925. case FT_PIXEL_MODE_LCD_V: {
  926. color_size = 4;
  927. h /= 3;
  928. } break;
  929. }
  930. int mw = w + p_rect_margin * 4;
  931. int mh = h + p_rect_margin * 4;
  932. ERR_FAIL_COND_V(mw > 4096, FontGlyph());
  933. ERR_FAIL_COND_V(mh > 4096, FontGlyph());
  934. Image::Format require_format = color_size == 4 ? Image::FORMAT_RGBA8 : Image::FORMAT_LA8;
  935. FontTexturePosition tex_pos = find_texture_pos_for_glyph(p_data, color_size, require_format, mw, mh, false);
  936. ERR_FAIL_COND_V(tex_pos.index < 0, FontGlyph());
  937. // Fit character in char texture.
  938. ShelfPackTexture &tex = p_data->textures.write[tex_pos.index];
  939. {
  940. uint8_t *wr = tex.imgdata.ptrw();
  941. for (int i = 0; i < h; i++) {
  942. for (int j = 0; j < w; j++) {
  943. int ofs = ((i + tex_pos.y + p_rect_margin * 2) * tex.texture_w + j + tex_pos.x + p_rect_margin * 2) * color_size;
  944. ERR_FAIL_COND_V(ofs >= tex.imgdata.size(), FontGlyph());
  945. switch (bitmap.pixel_mode) {
  946. case FT_PIXEL_MODE_MONO: {
  947. int byte = i * bitmap.pitch + (j >> 3);
  948. int bit = 1 << (7 - (j % 8));
  949. wr[ofs + 0] = 255; // grayscale as 1
  950. wr[ofs + 1] = (bitmap.buffer[byte] & bit) ? 255 : 0;
  951. } break;
  952. case FT_PIXEL_MODE_GRAY:
  953. wr[ofs + 0] = 255; // grayscale as 1
  954. wr[ofs + 1] = bitmap.buffer[i * bitmap.pitch + j];
  955. break;
  956. case FT_PIXEL_MODE_BGRA: {
  957. int ofs_color = i * bitmap.pitch + (j << 2);
  958. wr[ofs + 2] = bitmap.buffer[ofs_color + 0];
  959. wr[ofs + 1] = bitmap.buffer[ofs_color + 1];
  960. wr[ofs + 0] = bitmap.buffer[ofs_color + 2];
  961. wr[ofs + 3] = bitmap.buffer[ofs_color + 3];
  962. } break;
  963. case FT_PIXEL_MODE_LCD: {
  964. int ofs_color = i * bitmap.pitch + (j * 3);
  965. if (p_bgra) {
  966. wr[ofs + 0] = bitmap.buffer[ofs_color + 0];
  967. wr[ofs + 1] = bitmap.buffer[ofs_color + 1];
  968. wr[ofs + 2] = bitmap.buffer[ofs_color + 2];
  969. wr[ofs + 3] = 255;
  970. } else {
  971. wr[ofs + 0] = bitmap.buffer[ofs_color + 2];
  972. wr[ofs + 1] = bitmap.buffer[ofs_color + 1];
  973. wr[ofs + 2] = bitmap.buffer[ofs_color + 0];
  974. wr[ofs + 3] = 255;
  975. }
  976. } break;
  977. case FT_PIXEL_MODE_LCD_V: {
  978. int ofs_color = i * bitmap.pitch * 3 + j;
  979. if (p_bgra) {
  980. wr[ofs + 0] = bitmap.buffer[ofs_color + bitmap.pitch * 2];
  981. wr[ofs + 1] = bitmap.buffer[ofs_color + bitmap.pitch];
  982. wr[ofs + 2] = bitmap.buffer[ofs_color + 0];
  983. wr[ofs + 3] = 255;
  984. } else {
  985. wr[ofs + 0] = bitmap.buffer[ofs_color + 0];
  986. wr[ofs + 1] = bitmap.buffer[ofs_color + bitmap.pitch];
  987. wr[ofs + 2] = bitmap.buffer[ofs_color + bitmap.pitch * 2];
  988. wr[ofs + 3] = 255;
  989. }
  990. } break;
  991. default:
  992. ERR_FAIL_V_MSG(FontGlyph(), "Font uses unsupported pixel format: " + String::num_int64(bitmap.pixel_mode) + ".");
  993. break;
  994. }
  995. }
  996. }
  997. }
  998. tex.dirty = true;
  999. FontGlyph chr;
  1000. chr.advance = advance * p_data->scale / p_data->oversampling;
  1001. chr.texture_idx = tex_pos.index;
  1002. chr.found = true;
  1003. 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);
  1004. chr.rect.position = Vector2(xofs - p_rect_margin, -yofs - p_rect_margin) * p_data->scale / p_data->oversampling;
  1005. chr.rect.size = chr.uv_rect.size * p_data->scale / p_data->oversampling;
  1006. return chr;
  1007. }
  1008. #endif
  1009. /*************************************************************************/
  1010. /* Font Cache */
  1011. /*************************************************************************/
  1012. _FORCE_INLINE_ bool TextServerAdvanced::_ensure_glyph(FontAdvanced *p_font_data, const Vector2i &p_size, int32_t p_glyph) const {
  1013. ERR_FAIL_COND_V(!_ensure_cache_for_size(p_font_data, p_size), false);
  1014. int32_t glyph_index = p_glyph & 0xffffff; // Remove subpixel shifts.
  1015. FontForSizeAdvanced *fd = p_font_data->cache[p_size];
  1016. if (fd->glyph_map.has(p_glyph)) {
  1017. return fd->glyph_map[p_glyph].found;
  1018. }
  1019. if (glyph_index == 0) { // Non graphical or invalid glyph, do not render.
  1020. fd->glyph_map[p_glyph] = FontGlyph();
  1021. return true;
  1022. }
  1023. #ifdef MODULE_FREETYPE_ENABLED
  1024. FontGlyph gl;
  1025. if (fd->face) {
  1026. FT_Int32 flags = FT_LOAD_DEFAULT;
  1027. bool outline = p_size.y > 0;
  1028. switch (p_font_data->hinting) {
  1029. case TextServer::HINTING_NONE:
  1030. flags |= FT_LOAD_NO_HINTING;
  1031. break;
  1032. case TextServer::HINTING_LIGHT:
  1033. flags |= FT_LOAD_TARGET_LIGHT;
  1034. break;
  1035. default:
  1036. flags |= FT_LOAD_TARGET_NORMAL;
  1037. break;
  1038. }
  1039. if (p_font_data->force_autohinter) {
  1040. flags |= FT_LOAD_FORCE_AUTOHINT;
  1041. }
  1042. if (outline) {
  1043. flags |= FT_LOAD_NO_BITMAP;
  1044. } else if (FT_HAS_COLOR(fd->face)) {
  1045. flags |= FT_LOAD_COLOR;
  1046. }
  1047. FT_Fixed v, h;
  1048. FT_Get_Advance(fd->face, glyph_index, flags, &h);
  1049. FT_Get_Advance(fd->face, glyph_index, flags | FT_LOAD_VERTICAL_LAYOUT, &v);
  1050. int error = FT_Load_Glyph(fd->face, glyph_index, flags);
  1051. if (error) {
  1052. fd->glyph_map[p_glyph] = FontGlyph();
  1053. return false;
  1054. }
  1055. if (!p_font_data->msdf) {
  1056. 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)) {
  1057. FT_Pos xshift = (int)((p_glyph >> 27) & 3) << 4;
  1058. FT_Outline_Translate(&fd->face->glyph->outline, xshift, 0);
  1059. } 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)) {
  1060. FT_Pos xshift = (int)((p_glyph >> 27) & 3) << 5;
  1061. FT_Outline_Translate(&fd->face->glyph->outline, xshift, 0);
  1062. }
  1063. }
  1064. if (p_font_data->embolden != 0.f) {
  1065. FT_Pos strength = p_font_data->embolden * p_size.x * 4; // 26.6 fractional units (1 / 64).
  1066. FT_Outline_Embolden(&fd->face->glyph->outline, strength);
  1067. }
  1068. if (p_font_data->transform != Transform2D()) {
  1069. 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).
  1070. FT_Outline_Transform(&fd->face->glyph->outline, &mat);
  1071. }
  1072. FT_Render_Mode aa_mode = FT_RENDER_MODE_NORMAL;
  1073. bool bgra = false;
  1074. switch (p_font_data->antialiasing) {
  1075. case FONT_ANTIALIASING_NONE: {
  1076. aa_mode = FT_RENDER_MODE_MONO;
  1077. } break;
  1078. case FONT_ANTIALIASING_GRAY: {
  1079. aa_mode = FT_RENDER_MODE_NORMAL;
  1080. } break;
  1081. case FONT_ANTIALIASING_LCD: {
  1082. int aa_layout = (int)((p_glyph >> 24) & 7);
  1083. switch (aa_layout) {
  1084. case FONT_LCD_SUBPIXEL_LAYOUT_HRGB: {
  1085. aa_mode = FT_RENDER_MODE_LCD;
  1086. bgra = false;
  1087. } break;
  1088. case FONT_LCD_SUBPIXEL_LAYOUT_HBGR: {
  1089. aa_mode = FT_RENDER_MODE_LCD;
  1090. bgra = true;
  1091. } break;
  1092. case FONT_LCD_SUBPIXEL_LAYOUT_VRGB: {
  1093. aa_mode = FT_RENDER_MODE_LCD_V;
  1094. bgra = false;
  1095. } break;
  1096. case FONT_LCD_SUBPIXEL_LAYOUT_VBGR: {
  1097. aa_mode = FT_RENDER_MODE_LCD_V;
  1098. bgra = true;
  1099. } break;
  1100. default: {
  1101. aa_mode = FT_RENDER_MODE_NORMAL;
  1102. } break;
  1103. }
  1104. } break;
  1105. }
  1106. if (!outline) {
  1107. if (!p_font_data->msdf) {
  1108. error = FT_Render_Glyph(fd->face->glyph, aa_mode);
  1109. }
  1110. FT_GlyphSlot slot = fd->face->glyph;
  1111. if (!error) {
  1112. if (p_font_data->msdf) {
  1113. #ifdef MODULE_MSDFGEN_ENABLED
  1114. 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);
  1115. #else
  1116. fd->glyph_map[p_glyph] = FontGlyph();
  1117. ERR_FAIL_V_MSG(false, "Compiled without MSDFGEN support!");
  1118. #endif
  1119. } else {
  1120. 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);
  1121. }
  1122. }
  1123. } else {
  1124. FT_Stroker stroker;
  1125. if (FT_Stroker_New(ft_library, &stroker) != 0) {
  1126. fd->glyph_map[p_glyph] = FontGlyph();
  1127. ERR_FAIL_V_MSG(false, "FreeType: Failed to load glyph stroker.");
  1128. }
  1129. FT_Stroker_Set(stroker, (int)(fd->size.y * fd->oversampling * 16.0), FT_STROKER_LINECAP_BUTT, FT_STROKER_LINEJOIN_ROUND, 0);
  1130. FT_Glyph glyph;
  1131. FT_BitmapGlyph glyph_bitmap;
  1132. if (FT_Get_Glyph(fd->face->glyph, &glyph) != 0) {
  1133. goto cleanup_stroker;
  1134. }
  1135. if (FT_Glyph_Stroke(&glyph, stroker, 1) != 0) {
  1136. goto cleanup_glyph;
  1137. }
  1138. if (FT_Glyph_To_Bitmap(&glyph, aa_mode, nullptr, 1) != 0) {
  1139. goto cleanup_glyph;
  1140. }
  1141. glyph_bitmap = (FT_BitmapGlyph)glyph;
  1142. gl = rasterize_bitmap(fd, rect_range, glyph_bitmap->bitmap, glyph_bitmap->top, glyph_bitmap->left, Vector2(), bgra);
  1143. cleanup_glyph:
  1144. FT_Done_Glyph(glyph);
  1145. cleanup_stroker:
  1146. FT_Stroker_Done(stroker);
  1147. }
  1148. fd->glyph_map[p_glyph] = gl;
  1149. return gl.found;
  1150. }
  1151. #endif
  1152. fd->glyph_map[p_glyph] = FontGlyph();
  1153. return false;
  1154. }
  1155. _FORCE_INLINE_ bool TextServerAdvanced::_ensure_cache_for_size(FontAdvanced *p_font_data, const Vector2i &p_size) const {
  1156. ERR_FAIL_COND_V(p_size.x <= 0, false);
  1157. if (p_font_data->cache.has(p_size)) {
  1158. return true;
  1159. }
  1160. FontForSizeAdvanced *fd = memnew(FontForSizeAdvanced);
  1161. fd->size = p_size;
  1162. if (p_font_data->data_ptr && (p_font_data->data_size > 0)) {
  1163. // Init dynamic font.
  1164. #ifdef MODULE_FREETYPE_ENABLED
  1165. int error = 0;
  1166. if (!ft_library) {
  1167. error = FT_Init_FreeType(&ft_library);
  1168. if (error != 0) {
  1169. memdelete(fd);
  1170. ERR_FAIL_V_MSG(false, "FreeType: Error initializing library: '" + String(FT_Error_String(error)) + "'.");
  1171. }
  1172. #ifdef MODULE_SVG_ENABLED
  1173. FT_Property_Set(ft_library, "ot-svg", "svg-hooks", get_tvg_svg_in_ot_hooks());
  1174. #endif
  1175. }
  1176. memset(&fd->stream, 0, sizeof(FT_StreamRec));
  1177. fd->stream.base = (unsigned char *)p_font_data->data_ptr;
  1178. fd->stream.size = p_font_data->data_size;
  1179. fd->stream.pos = 0;
  1180. FT_Open_Args fargs;
  1181. memset(&fargs, 0, sizeof(FT_Open_Args));
  1182. fargs.memory_base = (unsigned char *)p_font_data->data_ptr;
  1183. fargs.memory_size = p_font_data->data_size;
  1184. fargs.flags = FT_OPEN_MEMORY;
  1185. fargs.stream = &fd->stream;
  1186. int max_index = 0;
  1187. FT_Face tmp_face = nullptr;
  1188. error = FT_Open_Face(ft_library, &fargs, -1, &tmp_face);
  1189. if (tmp_face && error == 0) {
  1190. max_index = tmp_face->num_faces - 1;
  1191. }
  1192. if (tmp_face) {
  1193. FT_Done_Face(tmp_face);
  1194. }
  1195. error = FT_Open_Face(ft_library, &fargs, CLAMP(p_font_data->face_index, 0, max_index), &fd->face);
  1196. if (error) {
  1197. FT_Done_Face(fd->face);
  1198. fd->face = nullptr;
  1199. memdelete(fd);
  1200. ERR_FAIL_V_MSG(false, "FreeType: Error loading font: '" + String(FT_Error_String(error)) + "'.");
  1201. }
  1202. if (p_font_data->msdf) {
  1203. fd->oversampling = 1.0;
  1204. fd->size.x = p_font_data->msdf_source_size;
  1205. } else if (p_font_data->oversampling <= 0.0) {
  1206. fd->oversampling = _font_get_global_oversampling();
  1207. } else {
  1208. fd->oversampling = p_font_data->oversampling;
  1209. }
  1210. if (FT_HAS_COLOR(fd->face) && fd->face->num_fixed_sizes > 0) {
  1211. int best_match = 0;
  1212. int diff = ABS(fd->size.x - ((int64_t)fd->face->available_sizes[0].width));
  1213. fd->scale = double(fd->size.x * fd->oversampling) / fd->face->available_sizes[0].width;
  1214. for (int i = 1; i < fd->face->num_fixed_sizes; i++) {
  1215. int ndiff = ABS(fd->size.x - ((int64_t)fd->face->available_sizes[i].width));
  1216. if (ndiff < diff) {
  1217. best_match = i;
  1218. diff = ndiff;
  1219. fd->scale = double(fd->size.x * fd->oversampling) / fd->face->available_sizes[i].width;
  1220. }
  1221. }
  1222. FT_Select_Size(fd->face, best_match);
  1223. } else {
  1224. FT_Set_Pixel_Sizes(fd->face, 0, double(fd->size.x * fd->oversampling));
  1225. fd->scale = ((double)fd->size.x * fd->oversampling) / (double)fd->face->size->metrics.y_ppem;
  1226. }
  1227. fd->hb_handle = hb_ft_font_create(fd->face, nullptr);
  1228. fd->ascent = (fd->face->size->metrics.ascender / 64.0) / fd->oversampling * fd->scale;
  1229. fd->descent = (-fd->face->size->metrics.descender / 64.0) / fd->oversampling * fd->scale;
  1230. fd->underline_position = (-FT_MulFix(fd->face->underline_position, fd->face->size->metrics.y_scale) / 64.0) / fd->oversampling * fd->scale;
  1231. fd->underline_thickness = (FT_MulFix(fd->face->underline_thickness, fd->face->size->metrics.y_scale) / 64.0) / fd->oversampling * fd->scale;
  1232. #if HB_VERSION_ATLEAST(3, 3, 0)
  1233. hb_font_set_synthetic_slant(fd->hb_handle, p_font_data->transform[0][1]);
  1234. #else
  1235. #ifndef _MSC_VER
  1236. #warning Building with HarfBuzz < 3.3.0, synthetic slant offset correction disabled.
  1237. #endif
  1238. #endif
  1239. if (!p_font_data->face_init) {
  1240. // Get style flags and name.
  1241. if (fd->face->family_name != nullptr) {
  1242. p_font_data->font_name = String::utf8((const char *)fd->face->family_name);
  1243. }
  1244. if (fd->face->style_name != nullptr) {
  1245. p_font_data->style_name = String::utf8((const char *)fd->face->style_name);
  1246. }
  1247. p_font_data->weight = _font_get_weight_by_name(p_font_data->style_name.to_lower());
  1248. p_font_data->stretch = _font_get_stretch_by_name(p_font_data->style_name.to_lower());
  1249. p_font_data->style_flags = 0;
  1250. if ((fd->face->style_flags & FT_STYLE_FLAG_BOLD) || p_font_data->weight >= 700) {
  1251. p_font_data->style_flags.set_flag(FONT_BOLD);
  1252. }
  1253. if ((fd->face->style_flags & FT_STYLE_FLAG_ITALIC) || _is_ital_style(p_font_data->style_name.to_lower())) {
  1254. p_font_data->style_flags.set_flag(FONT_ITALIC);
  1255. }
  1256. if (fd->face->face_flags & FT_FACE_FLAG_FIXED_WIDTH) {
  1257. p_font_data->style_flags.set_flag(FONT_FIXED_WIDTH);
  1258. }
  1259. hb_face_t *hb_face = hb_font_get_face(fd->hb_handle);
  1260. // Get supported scripts from OpenType font data.
  1261. p_font_data->supported_scripts.clear();
  1262. unsigned int count = hb_ot_layout_table_get_script_tags(hb_face, HB_OT_TAG_GSUB, 0, nullptr, nullptr);
  1263. if (count != 0) {
  1264. hb_tag_t *script_tags = (hb_tag_t *)memalloc(count * sizeof(hb_tag_t));
  1265. hb_ot_layout_table_get_script_tags(hb_face, HB_OT_TAG_GSUB, 0, &count, script_tags);
  1266. for (unsigned int i = 0; i < count; i++) {
  1267. p_font_data->supported_scripts.insert(script_tags[i]);
  1268. }
  1269. memfree(script_tags);
  1270. }
  1271. count = hb_ot_layout_table_get_script_tags(hb_face, HB_OT_TAG_GPOS, 0, nullptr, nullptr);
  1272. if (count != 0) {
  1273. hb_tag_t *script_tags = (hb_tag_t *)memalloc(count * sizeof(hb_tag_t));
  1274. hb_ot_layout_table_get_script_tags(hb_face, HB_OT_TAG_GPOS, 0, &count, script_tags);
  1275. for (unsigned int i = 0; i < count; i++) {
  1276. p_font_data->supported_scripts.insert(script_tags[i]);
  1277. }
  1278. memfree(script_tags);
  1279. }
  1280. // Get supported scripts from OS2 table.
  1281. TT_OS2 *os2 = (TT_OS2 *)FT_Get_Sfnt_Table(fd->face, FT_SFNT_OS2);
  1282. if (os2) {
  1283. if ((os2->ulUnicodeRange1 & 1L << 4) || (os2->ulUnicodeRange1 & 1L << 5) || (os2->ulUnicodeRange1 & 1L << 6) || (os2->ulUnicodeRange1 & 1L << 31) || (os2->ulUnicodeRange2 & 1L << 0) || (os2->ulUnicodeRange2 & 1L << 1) || (os2->ulUnicodeRange2 & 1L << 2) || (os2->ulUnicodeRange2 & 1L << 3) || (os2->ulUnicodeRange2 & 1L << 4) || (os2->ulUnicodeRange2 & 1L << 5) || (os2->ulUnicodeRange2 & 1L << 6) || (os2->ulUnicodeRange2 & 1L << 7) || (os2->ulUnicodeRange2 & 1L << 8) || (os2->ulUnicodeRange2 & 1L << 9) || (os2->ulUnicodeRange2 & 1L << 10) || (os2->ulUnicodeRange2 & 1L << 11) || (os2->ulUnicodeRange2 & 1L << 12) || (os2->ulUnicodeRange2 & 1L << 13) || (os2->ulUnicodeRange2 & 1L << 14) || (os2->ulUnicodeRange2 & 1L << 15) || (os2->ulUnicodeRange2 & 1L << 30) || (os2->ulUnicodeRange3 & 1L << 0) || (os2->ulUnicodeRange3 & 1L << 1) || (os2->ulUnicodeRange3 & 1L << 2) || (os2->ulUnicodeRange3 & 1L << 4) || (os2->ulUnicodeRange3 & 1L << 5) || (os2->ulUnicodeRange3 & 1L << 18) || (os2->ulUnicodeRange3 & 1L << 24) || (os2->ulUnicodeRange3 & 1L << 25) || (os2->ulUnicodeRange3 & 1L << 26) || (os2->ulUnicodeRange3 & 1L << 27) || (os2->ulUnicodeRange3 & 1L << 28) || (os2->ulUnicodeRange4 & 1L << 3) || (os2->ulUnicodeRange4 & 1L << 6) || (os2->ulUnicodeRange4 & 1L << 15) || (os2->ulUnicodeRange4 & 1L << 23) || (os2->ulUnicodeRange4 & 1L << 24) || (os2->ulUnicodeRange4 & 1L << 26)) {
  1284. p_font_data->supported_scripts.insert(HB_SCRIPT_COMMON);
  1285. }
  1286. if ((os2->ulUnicodeRange1 & 1L << 0) || (os2->ulUnicodeRange1 & 1L << 1) || (os2->ulUnicodeRange1 & 1L << 2) || (os2->ulUnicodeRange1 & 1L << 3) || (os2->ulUnicodeRange1 & 1L << 29)) {
  1287. p_font_data->supported_scripts.insert(HB_SCRIPT_LATIN);
  1288. }
  1289. if ((os2->ulUnicodeRange1 & 1L << 7) || (os2->ulUnicodeRange1 & 1L << 30)) {
  1290. p_font_data->supported_scripts.insert(HB_SCRIPT_GREEK);
  1291. }
  1292. if (os2->ulUnicodeRange1 & 1L << 8) {
  1293. p_font_data->supported_scripts.insert(HB_SCRIPT_COPTIC);
  1294. }
  1295. if (os2->ulUnicodeRange1 & 1L << 9) {
  1296. p_font_data->supported_scripts.insert(HB_SCRIPT_CYRILLIC);
  1297. }
  1298. if (os2->ulUnicodeRange1 & 1L << 10) {
  1299. p_font_data->supported_scripts.insert(HB_SCRIPT_ARMENIAN);
  1300. }
  1301. if (os2->ulUnicodeRange1 & 1L << 11) {
  1302. p_font_data->supported_scripts.insert(HB_SCRIPT_HEBREW);
  1303. }
  1304. if (os2->ulUnicodeRange1 & 1L << 12) {
  1305. p_font_data->supported_scripts.insert(HB_SCRIPT_VAI);
  1306. }
  1307. if ((os2->ulUnicodeRange1 & 1L << 13) || (os2->ulUnicodeRange2 & 1L << 31) || (os2->ulUnicodeRange3 & 1L << 3)) {
  1308. p_font_data->supported_scripts.insert(HB_SCRIPT_ARABIC);
  1309. }
  1310. if (os2->ulUnicodeRange1 & 1L << 14) {
  1311. p_font_data->supported_scripts.insert(HB_SCRIPT_NKO);
  1312. }
  1313. if (os2->ulUnicodeRange1 & 1L << 15) {
  1314. p_font_data->supported_scripts.insert(HB_SCRIPT_DEVANAGARI);
  1315. }
  1316. if (os2->ulUnicodeRange1 & 1L << 16) {
  1317. p_font_data->supported_scripts.insert(HB_SCRIPT_BENGALI);
  1318. }
  1319. if (os2->ulUnicodeRange1 & 1L << 17) {
  1320. p_font_data->supported_scripts.insert(HB_SCRIPT_GURMUKHI);
  1321. }
  1322. if (os2->ulUnicodeRange1 & 1L << 18) {
  1323. p_font_data->supported_scripts.insert(HB_SCRIPT_GUJARATI);
  1324. }
  1325. if (os2->ulUnicodeRange1 & 1L << 19) {
  1326. p_font_data->supported_scripts.insert(HB_SCRIPT_ORIYA);
  1327. }
  1328. if (os2->ulUnicodeRange1 & 1L << 20) {
  1329. p_font_data->supported_scripts.insert(HB_SCRIPT_TAMIL);
  1330. }
  1331. if (os2->ulUnicodeRange1 & 1L << 21) {
  1332. p_font_data->supported_scripts.insert(HB_SCRIPT_TELUGU);
  1333. }
  1334. if (os2->ulUnicodeRange1 & 1L << 22) {
  1335. p_font_data->supported_scripts.insert(HB_SCRIPT_KANNADA);
  1336. }
  1337. if (os2->ulUnicodeRange1 & 1L << 23) {
  1338. p_font_data->supported_scripts.insert(HB_SCRIPT_MALAYALAM);
  1339. }
  1340. if (os2->ulUnicodeRange1 & 1L << 24) {
  1341. p_font_data->supported_scripts.insert(HB_SCRIPT_THAI);
  1342. }
  1343. if (os2->ulUnicodeRange1 & 1L << 25) {
  1344. p_font_data->supported_scripts.insert(HB_SCRIPT_LAO);
  1345. }
  1346. if (os2->ulUnicodeRange1 & 1L << 26) {
  1347. p_font_data->supported_scripts.insert(HB_SCRIPT_GEORGIAN);
  1348. }
  1349. if (os2->ulUnicodeRange1 & 1L << 27) {
  1350. p_font_data->supported_scripts.insert(HB_SCRIPT_BALINESE);
  1351. }
  1352. if ((os2->ulUnicodeRange1 & 1L << 28) || (os2->ulUnicodeRange2 & 1L << 20) || (os2->ulUnicodeRange2 & 1L << 24)) {
  1353. p_font_data->supported_scripts.insert(HB_SCRIPT_HANGUL);
  1354. }
  1355. if ((os2->ulUnicodeRange2 & 1L << 21) || (os2->ulUnicodeRange2 & 1L << 22) || (os2->ulUnicodeRange2 & 1L << 23) || (os2->ulUnicodeRange2 & 1L << 26) || (os2->ulUnicodeRange2 & 1L << 27) || (os2->ulUnicodeRange2 & 1L << 29)) {
  1356. p_font_data->supported_scripts.insert(HB_SCRIPT_HAN);
  1357. }
  1358. if (os2->ulUnicodeRange2 & 1L << 17) {
  1359. p_font_data->supported_scripts.insert(HB_SCRIPT_HIRAGANA);
  1360. }
  1361. if (os2->ulUnicodeRange2 & 1L << 18) {
  1362. p_font_data->supported_scripts.insert(HB_SCRIPT_KATAKANA);
  1363. }
  1364. if (os2->ulUnicodeRange2 & 1L << 19) {
  1365. p_font_data->supported_scripts.insert(HB_SCRIPT_BOPOMOFO);
  1366. }
  1367. if (os2->ulUnicodeRange3 & 1L << 6) {
  1368. p_font_data->supported_scripts.insert(HB_SCRIPT_TIBETAN);
  1369. }
  1370. if (os2->ulUnicodeRange3 & 1L << 7) {
  1371. p_font_data->supported_scripts.insert(HB_SCRIPT_SYRIAC);
  1372. }
  1373. if (os2->ulUnicodeRange3 & 1L << 8) {
  1374. p_font_data->supported_scripts.insert(HB_SCRIPT_THAANA);
  1375. }
  1376. if (os2->ulUnicodeRange3 & 1L << 9) {
  1377. p_font_data->supported_scripts.insert(HB_SCRIPT_SINHALA);
  1378. }
  1379. if (os2->ulUnicodeRange3 & 1L << 10) {
  1380. p_font_data->supported_scripts.insert(HB_SCRIPT_MYANMAR);
  1381. }
  1382. if (os2->ulUnicodeRange3 & 1L << 11) {
  1383. p_font_data->supported_scripts.insert(HB_SCRIPT_ETHIOPIC);
  1384. }
  1385. if (os2->ulUnicodeRange3 & 1L << 12) {
  1386. p_font_data->supported_scripts.insert(HB_SCRIPT_CHEROKEE);
  1387. }
  1388. if (os2->ulUnicodeRange3 & 1L << 13) {
  1389. p_font_data->supported_scripts.insert(HB_SCRIPT_CANADIAN_SYLLABICS);
  1390. }
  1391. if (os2->ulUnicodeRange3 & 1L << 14) {
  1392. p_font_data->supported_scripts.insert(HB_SCRIPT_OGHAM);
  1393. }
  1394. if (os2->ulUnicodeRange3 & 1L << 15) {
  1395. p_font_data->supported_scripts.insert(HB_SCRIPT_RUNIC);
  1396. }
  1397. if (os2->ulUnicodeRange3 & 1L << 16) {
  1398. p_font_data->supported_scripts.insert(HB_SCRIPT_KHMER);
  1399. }
  1400. if (os2->ulUnicodeRange3 & 1L << 17) {
  1401. p_font_data->supported_scripts.insert(HB_SCRIPT_MONGOLIAN);
  1402. }
  1403. if (os2->ulUnicodeRange3 & 1L << 19) {
  1404. p_font_data->supported_scripts.insert(HB_SCRIPT_YI);
  1405. }
  1406. if (os2->ulUnicodeRange3 & 1L << 20) {
  1407. p_font_data->supported_scripts.insert(HB_SCRIPT_HANUNOO);
  1408. p_font_data->supported_scripts.insert(HB_SCRIPT_TAGBANWA);
  1409. p_font_data->supported_scripts.insert(HB_SCRIPT_BUHID);
  1410. p_font_data->supported_scripts.insert(HB_SCRIPT_TAGALOG);
  1411. }
  1412. if (os2->ulUnicodeRange3 & 1L << 21) {
  1413. p_font_data->supported_scripts.insert(HB_SCRIPT_OLD_ITALIC);
  1414. }
  1415. if (os2->ulUnicodeRange3 & 1L << 22) {
  1416. p_font_data->supported_scripts.insert(HB_SCRIPT_GOTHIC);
  1417. }
  1418. if (os2->ulUnicodeRange3 & 1L << 23) {
  1419. p_font_data->supported_scripts.insert(HB_SCRIPT_DESERET);
  1420. }
  1421. if (os2->ulUnicodeRange3 & 1L << 29) {
  1422. p_font_data->supported_scripts.insert(HB_SCRIPT_LIMBU);
  1423. }
  1424. if (os2->ulUnicodeRange3 & 1L << 30) {
  1425. p_font_data->supported_scripts.insert(HB_SCRIPT_TAI_LE);
  1426. }
  1427. if (os2->ulUnicodeRange3 & 1L << 31) {
  1428. p_font_data->supported_scripts.insert(HB_SCRIPT_NEW_TAI_LUE);
  1429. }
  1430. if (os2->ulUnicodeRange4 & 1L << 0) {
  1431. p_font_data->supported_scripts.insert(HB_SCRIPT_BUGINESE);
  1432. }
  1433. if (os2->ulUnicodeRange4 & 1L << 1) {
  1434. p_font_data->supported_scripts.insert(HB_SCRIPT_GLAGOLITIC);
  1435. }
  1436. if (os2->ulUnicodeRange4 & 1L << 2) {
  1437. p_font_data->supported_scripts.insert(HB_SCRIPT_TIFINAGH);
  1438. }
  1439. if (os2->ulUnicodeRange4 & 1L << 4) {
  1440. p_font_data->supported_scripts.insert(HB_SCRIPT_SYLOTI_NAGRI);
  1441. }
  1442. if (os2->ulUnicodeRange4 & 1L << 5) {
  1443. p_font_data->supported_scripts.insert(HB_SCRIPT_LINEAR_B);
  1444. }
  1445. if (os2->ulUnicodeRange4 & 1L << 7) {
  1446. p_font_data->supported_scripts.insert(HB_SCRIPT_UGARITIC);
  1447. }
  1448. if (os2->ulUnicodeRange4 & 1L << 8) {
  1449. p_font_data->supported_scripts.insert(HB_SCRIPT_OLD_PERSIAN);
  1450. }
  1451. if (os2->ulUnicodeRange4 & 1L << 9) {
  1452. p_font_data->supported_scripts.insert(HB_SCRIPT_SHAVIAN);
  1453. }
  1454. if (os2->ulUnicodeRange4 & 1L << 10) {
  1455. p_font_data->supported_scripts.insert(HB_SCRIPT_OSMANYA);
  1456. }
  1457. if (os2->ulUnicodeRange4 & 1L << 11) {
  1458. p_font_data->supported_scripts.insert(HB_SCRIPT_CYPRIOT);
  1459. }
  1460. if (os2->ulUnicodeRange4 & 1L << 12) {
  1461. p_font_data->supported_scripts.insert(HB_SCRIPT_KHAROSHTHI);
  1462. }
  1463. if (os2->ulUnicodeRange4 & 1L << 13) {
  1464. p_font_data->supported_scripts.insert(HB_SCRIPT_TAI_VIET);
  1465. }
  1466. if (os2->ulUnicodeRange4 & 1L << 14) {
  1467. p_font_data->supported_scripts.insert(HB_SCRIPT_CUNEIFORM);
  1468. }
  1469. if (os2->ulUnicodeRange4 & 1L << 16) {
  1470. p_font_data->supported_scripts.insert(HB_SCRIPT_SUNDANESE);
  1471. }
  1472. if (os2->ulUnicodeRange4 & 1L << 17) {
  1473. p_font_data->supported_scripts.insert(HB_SCRIPT_LEPCHA);
  1474. }
  1475. if (os2->ulUnicodeRange4 & 1L << 18) {
  1476. p_font_data->supported_scripts.insert(HB_SCRIPT_OL_CHIKI);
  1477. }
  1478. if (os2->ulUnicodeRange4 & 1L << 19) {
  1479. p_font_data->supported_scripts.insert(HB_SCRIPT_SAURASHTRA);
  1480. }
  1481. if (os2->ulUnicodeRange4 & 1L << 20) {
  1482. p_font_data->supported_scripts.insert(HB_SCRIPT_KAYAH_LI);
  1483. }
  1484. if (os2->ulUnicodeRange4 & 1L << 21) {
  1485. p_font_data->supported_scripts.insert(HB_SCRIPT_REJANG);
  1486. }
  1487. if (os2->ulUnicodeRange4 & 1L << 22) {
  1488. p_font_data->supported_scripts.insert(HB_SCRIPT_CHAM);
  1489. }
  1490. if (os2->ulUnicodeRange4 & 1L << 25) {
  1491. p_font_data->supported_scripts.insert(HB_SCRIPT_ANATOLIAN_HIEROGLYPHS);
  1492. }
  1493. }
  1494. // Read OpenType feature tags.
  1495. p_font_data->supported_features.clear();
  1496. count = hb_ot_layout_table_get_feature_tags(hb_face, HB_OT_TAG_GSUB, 0, nullptr, nullptr);
  1497. if (count != 0) {
  1498. hb_tag_t *feature_tags = (hb_tag_t *)memalloc(count * sizeof(hb_tag_t));
  1499. hb_ot_layout_table_get_feature_tags(hb_face, HB_OT_TAG_GSUB, 0, &count, feature_tags);
  1500. for (unsigned int i = 0; i < count; i++) {
  1501. Dictionary ftr;
  1502. #if HB_VERSION_ATLEAST(2, 1, 0)
  1503. hb_ot_name_id_t lbl_id;
  1504. if (hb_ot_layout_feature_get_name_ids(hb_face, HB_OT_TAG_GSUB, i, &lbl_id, nullptr, nullptr, nullptr, nullptr)) {
  1505. PackedInt32Array lbl;
  1506. unsigned int text_size = hb_ot_name_get_utf32(hb_face, lbl_id, hb_language_from_string(TranslationServer::get_singleton()->get_tool_locale().ascii().get_data(), -1), nullptr, nullptr) + 1;
  1507. lbl.resize(text_size);
  1508. memset((uint32_t *)lbl.ptrw(), 0, sizeof(uint32_t) * text_size);
  1509. hb_ot_name_get_utf32(hb_face, lbl_id, hb_language_from_string(TranslationServer::get_singleton()->get_tool_locale().ascii().get_data(), -1), &text_size, (uint32_t *)lbl.ptrw());
  1510. ftr["label"] = String((const char32_t *)lbl.ptr());
  1511. }
  1512. #else
  1513. #ifndef _MSC_VER
  1514. #warning Building with HarfBuzz < 2.1.0, readable OpenType feature names disabled.
  1515. #endif
  1516. #endif
  1517. ftr["type"] = _get_tag_type(feature_tags[i]);
  1518. ftr["hidden"] = _get_tag_hidden(feature_tags[i]);
  1519. p_font_data->supported_features[feature_tags[i]] = ftr;
  1520. }
  1521. memfree(feature_tags);
  1522. }
  1523. count = hb_ot_layout_table_get_feature_tags(hb_face, HB_OT_TAG_GPOS, 0, nullptr, nullptr);
  1524. if (count != 0) {
  1525. hb_tag_t *feature_tags = (hb_tag_t *)memalloc(count * sizeof(hb_tag_t));
  1526. hb_ot_layout_table_get_feature_tags(hb_face, HB_OT_TAG_GPOS, 0, &count, feature_tags);
  1527. for (unsigned int i = 0; i < count; i++) {
  1528. Dictionary ftr;
  1529. #if HB_VERSION_ATLEAST(2, 1, 0)
  1530. hb_ot_name_id_t lbl_id;
  1531. if (hb_ot_layout_feature_get_name_ids(hb_face, HB_OT_TAG_GPOS, i, &lbl_id, nullptr, nullptr, nullptr, nullptr)) {
  1532. PackedInt32Array lbl;
  1533. unsigned int text_size = hb_ot_name_get_utf32(hb_face, lbl_id, hb_language_from_string(TranslationServer::get_singleton()->get_tool_locale().ascii().get_data(), -1), nullptr, nullptr) + 1;
  1534. lbl.resize(text_size);
  1535. memset((uint32_t *)lbl.ptrw(), 0, sizeof(uint32_t) * text_size);
  1536. hb_ot_name_get_utf32(hb_face, lbl_id, hb_language_from_string(TranslationServer::get_singleton()->get_tool_locale().ascii().get_data(), -1), &text_size, (uint32_t *)lbl.ptrw());
  1537. ftr["label"] = String((const char32_t *)lbl.ptr());
  1538. }
  1539. #else
  1540. #ifndef _MSC_VER
  1541. #warning Building with HarfBuzz < 2.1.0, readable OpenType feature names disabled.
  1542. #endif
  1543. #endif
  1544. ftr["type"] = _get_tag_type(feature_tags[i]);
  1545. ftr["hidden"] = _get_tag_hidden(feature_tags[i]);
  1546. p_font_data->supported_features[feature_tags[i]] = ftr;
  1547. }
  1548. memfree(feature_tags);
  1549. }
  1550. // Read OpenType variations.
  1551. p_font_data->supported_varaitions.clear();
  1552. if (fd->face->face_flags & FT_FACE_FLAG_MULTIPLE_MASTERS) {
  1553. FT_MM_Var *amaster;
  1554. FT_Get_MM_Var(fd->face, &amaster);
  1555. for (FT_UInt i = 0; i < amaster->num_axis; i++) {
  1556. 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);
  1557. }
  1558. FT_Done_MM_Var(ft_library, amaster);
  1559. }
  1560. p_font_data->face_init = true;
  1561. }
  1562. // Write variations.
  1563. if (fd->face->face_flags & FT_FACE_FLAG_MULTIPLE_MASTERS) {
  1564. FT_MM_Var *amaster;
  1565. FT_Get_MM_Var(fd->face, &amaster);
  1566. Vector<hb_variation_t> hb_vars;
  1567. Vector<FT_Fixed> coords;
  1568. coords.resize(amaster->num_axis);
  1569. FT_Get_Var_Design_Coordinates(fd->face, coords.size(), coords.ptrw());
  1570. for (FT_UInt i = 0; i < amaster->num_axis; i++) {
  1571. hb_variation_t var;
  1572. // Reset to default.
  1573. var.tag = amaster->axis[i].tag;
  1574. var.value = (double)amaster->axis[i].def / 65536.0;
  1575. coords.write[i] = amaster->axis[i].def;
  1576. if (p_font_data->variation_coordinates.has(var.tag)) {
  1577. var.value = p_font_data->variation_coordinates[var.tag];
  1578. coords.write[i] = CLAMP(var.value * 65536.0, amaster->axis[i].minimum, amaster->axis[i].maximum);
  1579. }
  1580. if (p_font_data->variation_coordinates.has(_tag_to_name(var.tag))) {
  1581. var.value = p_font_data->variation_coordinates[_tag_to_name(var.tag)];
  1582. coords.write[i] = CLAMP(var.value * 65536.0, amaster->axis[i].minimum, amaster->axis[i].maximum);
  1583. }
  1584. hb_vars.push_back(var);
  1585. }
  1586. FT_Set_Var_Design_Coordinates(fd->face, coords.size(), coords.ptrw());
  1587. hb_font_set_variations(fd->hb_handle, hb_vars.is_empty() ? nullptr : &hb_vars[0], hb_vars.size());
  1588. FT_Done_MM_Var(ft_library, amaster);
  1589. }
  1590. #else
  1591. memdelete(fd);
  1592. ERR_FAIL_V_MSG(false, "FreeType: Can't load dynamic font, engine is compiled without FreeType support!");
  1593. #endif
  1594. } else {
  1595. // Init bitmap font.
  1596. fd->hb_handle = _bmp_font_create(fd, nullptr);
  1597. }
  1598. p_font_data->cache[p_size] = fd;
  1599. return true;
  1600. }
  1601. _FORCE_INLINE_ void TextServerAdvanced::_font_clear_cache(FontAdvanced *p_font_data) {
  1602. for (const KeyValue<Vector2i, FontForSizeAdvanced *> &E : p_font_data->cache) {
  1603. memdelete(E.value);
  1604. }
  1605. p_font_data->cache.clear();
  1606. p_font_data->face_init = false;
  1607. p_font_data->supported_features.clear();
  1608. p_font_data->supported_varaitions.clear();
  1609. p_font_data->supported_scripts.clear();
  1610. }
  1611. hb_font_t *TextServerAdvanced::_font_get_hb_handle(const RID &p_font_rid, int64_t p_size) const {
  1612. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1613. ERR_FAIL_COND_V(!fd, nullptr);
  1614. MutexLock lock(fd->mutex);
  1615. Vector2i size = _get_size(fd, p_size);
  1616. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), nullptr);
  1617. return fd->cache[size]->hb_handle;
  1618. }
  1619. RID TextServerAdvanced::_create_font() {
  1620. _THREAD_SAFE_METHOD_
  1621. FontAdvanced *fd = memnew(FontAdvanced);
  1622. return font_owner.make_rid(fd);
  1623. }
  1624. void TextServerAdvanced::_font_set_data(const RID &p_font_rid, const PackedByteArray &p_data) {
  1625. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1626. ERR_FAIL_COND(!fd);
  1627. MutexLock lock(fd->mutex);
  1628. _font_clear_cache(fd);
  1629. fd->data = p_data;
  1630. fd->data_ptr = fd->data.ptr();
  1631. fd->data_size = fd->data.size();
  1632. }
  1633. void TextServerAdvanced::_font_set_data_ptr(const RID &p_font_rid, const uint8_t *p_data_ptr, int64_t p_data_size) {
  1634. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1635. ERR_FAIL_COND(!fd);
  1636. MutexLock lock(fd->mutex);
  1637. _font_clear_cache(fd);
  1638. fd->data.resize(0);
  1639. fd->data_ptr = p_data_ptr;
  1640. fd->data_size = p_data_size;
  1641. }
  1642. void TextServerAdvanced::_font_set_face_index(const RID &p_font_rid, int64_t p_face_index) {
  1643. ERR_FAIL_COND(p_face_index < 0);
  1644. ERR_FAIL_COND(p_face_index >= 0x7FFF);
  1645. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1646. ERR_FAIL_COND(!fd);
  1647. MutexLock lock(fd->mutex);
  1648. if (fd->face_index != p_face_index) {
  1649. fd->face_index = p_face_index;
  1650. _font_clear_cache(fd);
  1651. }
  1652. }
  1653. int64_t TextServerAdvanced::_font_get_face_index(const RID &p_font_rid) const {
  1654. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1655. ERR_FAIL_COND_V(!fd, 0);
  1656. MutexLock lock(fd->mutex);
  1657. return fd->face_index;
  1658. }
  1659. int64_t TextServerAdvanced::_font_get_face_count(const RID &p_font_rid) const {
  1660. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1661. ERR_FAIL_COND_V(!fd, 0);
  1662. MutexLock lock(fd->mutex);
  1663. int face_count = 0;
  1664. if (fd->data_ptr && (fd->data_size > 0)) {
  1665. // Init dynamic font.
  1666. #ifdef MODULE_FREETYPE_ENABLED
  1667. int error = 0;
  1668. if (!ft_library) {
  1669. error = FT_Init_FreeType(&ft_library);
  1670. ERR_FAIL_COND_V_MSG(error != 0, false, "FreeType: Error initializing library: '" + String(FT_Error_String(error)) + "'.");
  1671. #ifdef MODULE_SVG_ENABLED
  1672. FT_Property_Set(ft_library, "ot-svg", "svg-hooks", get_tvg_svg_in_ot_hooks());
  1673. #endif
  1674. }
  1675. FT_StreamRec stream;
  1676. memset(&stream, 0, sizeof(FT_StreamRec));
  1677. stream.base = (unsigned char *)fd->data_ptr;
  1678. stream.size = fd->data_size;
  1679. stream.pos = 0;
  1680. FT_Open_Args fargs;
  1681. memset(&fargs, 0, sizeof(FT_Open_Args));
  1682. fargs.memory_base = (unsigned char *)fd->data_ptr;
  1683. fargs.memory_size = fd->data_size;
  1684. fargs.flags = FT_OPEN_MEMORY;
  1685. fargs.stream = &stream;
  1686. FT_Face tmp_face = nullptr;
  1687. error = FT_Open_Face(ft_library, &fargs, -1, &tmp_face);
  1688. if (error == 0) {
  1689. face_count = tmp_face->num_faces;
  1690. FT_Done_Face(tmp_face);
  1691. }
  1692. #endif
  1693. }
  1694. return face_count;
  1695. }
  1696. void TextServerAdvanced::_font_set_style(const RID &p_font_rid, BitField<FontStyle> p_style) {
  1697. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1698. ERR_FAIL_COND(!fd);
  1699. MutexLock lock(fd->mutex);
  1700. Vector2i size = _get_size(fd, 16);
  1701. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  1702. fd->style_flags = p_style;
  1703. }
  1704. BitField<TextServer::FontStyle> TextServerAdvanced::_font_get_style(const RID &p_font_rid) const {
  1705. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1706. ERR_FAIL_COND_V(!fd, 0);
  1707. MutexLock lock(fd->mutex);
  1708. Vector2i size = _get_size(fd, 16);
  1709. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), 0);
  1710. return fd->style_flags;
  1711. }
  1712. void TextServerAdvanced::_font_set_style_name(const RID &p_font_rid, const String &p_name) {
  1713. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1714. ERR_FAIL_COND(!fd);
  1715. MutexLock lock(fd->mutex);
  1716. Vector2i size = _get_size(fd, 16);
  1717. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  1718. fd->style_name = p_name;
  1719. }
  1720. String TextServerAdvanced::_font_get_style_name(const RID &p_font_rid) const {
  1721. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1722. ERR_FAIL_COND_V(!fd, String());
  1723. MutexLock lock(fd->mutex);
  1724. Vector2i size = _get_size(fd, 16);
  1725. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), String());
  1726. return fd->style_name;
  1727. }
  1728. void TextServerAdvanced::_font_set_weight(const RID &p_font_rid, int64_t p_weight) {
  1729. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1730. ERR_FAIL_COND(!fd);
  1731. MutexLock lock(fd->mutex);
  1732. Vector2i size = _get_size(fd, 16);
  1733. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  1734. fd->weight = CLAMP(p_weight, 100, 999);
  1735. }
  1736. int64_t TextServerAdvanced::_font_get_weight(const RID &p_font_rid) const {
  1737. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1738. ERR_FAIL_COND_V(!fd, 400);
  1739. MutexLock lock(fd->mutex);
  1740. Vector2i size = _get_size(fd, 16);
  1741. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), 400);
  1742. return fd->weight;
  1743. }
  1744. void TextServerAdvanced::_font_set_stretch(const RID &p_font_rid, int64_t p_stretch) {
  1745. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1746. ERR_FAIL_COND(!fd);
  1747. MutexLock lock(fd->mutex);
  1748. Vector2i size = _get_size(fd, 16);
  1749. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  1750. fd->stretch = CLAMP(p_stretch, 50, 200);
  1751. }
  1752. int64_t TextServerAdvanced::_font_get_stretch(const RID &p_font_rid) const {
  1753. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1754. ERR_FAIL_COND_V(!fd, 100);
  1755. MutexLock lock(fd->mutex);
  1756. Vector2i size = _get_size(fd, 16);
  1757. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), 100);
  1758. return fd->stretch;
  1759. }
  1760. void TextServerAdvanced::_font_set_name(const RID &p_font_rid, const String &p_name) {
  1761. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1762. ERR_FAIL_COND(!fd);
  1763. MutexLock lock(fd->mutex);
  1764. Vector2i size = _get_size(fd, 16);
  1765. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  1766. fd->font_name = p_name;
  1767. }
  1768. String TextServerAdvanced::_font_get_name(const RID &p_font_rid) const {
  1769. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1770. ERR_FAIL_COND_V(!fd, String());
  1771. MutexLock lock(fd->mutex);
  1772. Vector2i size = _get_size(fd, 16);
  1773. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), String());
  1774. return fd->font_name;
  1775. }
  1776. void TextServerAdvanced::_font_set_antialiasing(const RID &p_font_rid, TextServer::FontAntialiasing p_antialiasing) {
  1777. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1778. ERR_FAIL_COND(!fd);
  1779. MutexLock lock(fd->mutex);
  1780. if (fd->antialiasing != p_antialiasing) {
  1781. _font_clear_cache(fd);
  1782. fd->antialiasing = p_antialiasing;
  1783. }
  1784. }
  1785. TextServer::FontAntialiasing TextServerAdvanced::_font_get_antialiasing(const RID &p_font_rid) const {
  1786. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1787. ERR_FAIL_COND_V(!fd, TextServer::FONT_ANTIALIASING_NONE);
  1788. MutexLock lock(fd->mutex);
  1789. return fd->antialiasing;
  1790. }
  1791. void TextServerAdvanced::_font_set_generate_mipmaps(const RID &p_font_rid, bool p_generate_mipmaps) {
  1792. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1793. ERR_FAIL_COND(!fd);
  1794. MutexLock lock(fd->mutex);
  1795. if (fd->mipmaps != p_generate_mipmaps) {
  1796. for (KeyValue<Vector2i, FontForSizeAdvanced *> &E : fd->cache) {
  1797. for (int i = 0; i < E.value->textures.size(); i++) {
  1798. E.value->textures.write[i].dirty = true;
  1799. E.value->textures.write[i].texture = Ref<ImageTexture>();
  1800. }
  1801. }
  1802. fd->mipmaps = p_generate_mipmaps;
  1803. }
  1804. }
  1805. bool TextServerAdvanced::_font_get_generate_mipmaps(const RID &p_font_rid) const {
  1806. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1807. ERR_FAIL_COND_V(!fd, false);
  1808. MutexLock lock(fd->mutex);
  1809. return fd->mipmaps;
  1810. }
  1811. void TextServerAdvanced::_font_set_multichannel_signed_distance_field(const RID &p_font_rid, bool p_msdf) {
  1812. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1813. ERR_FAIL_COND(!fd);
  1814. MutexLock lock(fd->mutex);
  1815. if (fd->msdf != p_msdf) {
  1816. _font_clear_cache(fd);
  1817. fd->msdf = p_msdf;
  1818. }
  1819. }
  1820. bool TextServerAdvanced::_font_is_multichannel_signed_distance_field(const RID &p_font_rid) const {
  1821. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1822. ERR_FAIL_COND_V(!fd, false);
  1823. MutexLock lock(fd->mutex);
  1824. return fd->msdf;
  1825. }
  1826. void TextServerAdvanced::_font_set_msdf_pixel_range(const RID &p_font_rid, int64_t p_msdf_pixel_range) {
  1827. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1828. ERR_FAIL_COND(!fd);
  1829. MutexLock lock(fd->mutex);
  1830. if (fd->msdf_range != p_msdf_pixel_range) {
  1831. _font_clear_cache(fd);
  1832. fd->msdf_range = p_msdf_pixel_range;
  1833. }
  1834. }
  1835. int64_t TextServerAdvanced::_font_get_msdf_pixel_range(const RID &p_font_rid) const {
  1836. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1837. ERR_FAIL_COND_V(!fd, false);
  1838. MutexLock lock(fd->mutex);
  1839. return fd->msdf_range;
  1840. }
  1841. void TextServerAdvanced::_font_set_msdf_size(const RID &p_font_rid, int64_t p_msdf_size) {
  1842. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1843. ERR_FAIL_COND(!fd);
  1844. MutexLock lock(fd->mutex);
  1845. if (fd->msdf_source_size != p_msdf_size) {
  1846. _font_clear_cache(fd);
  1847. fd->msdf_source_size = p_msdf_size;
  1848. }
  1849. }
  1850. int64_t TextServerAdvanced::_font_get_msdf_size(const RID &p_font_rid) const {
  1851. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1852. ERR_FAIL_COND_V(!fd, false);
  1853. MutexLock lock(fd->mutex);
  1854. return fd->msdf_source_size;
  1855. }
  1856. void TextServerAdvanced::_font_set_fixed_size(const RID &p_font_rid, int64_t p_fixed_size) {
  1857. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1858. ERR_FAIL_COND(!fd);
  1859. MutexLock lock(fd->mutex);
  1860. fd->fixed_size = p_fixed_size;
  1861. }
  1862. int64_t TextServerAdvanced::_font_get_fixed_size(const RID &p_font_rid) const {
  1863. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1864. ERR_FAIL_COND_V(!fd, false);
  1865. MutexLock lock(fd->mutex);
  1866. return fd->fixed_size;
  1867. }
  1868. void TextServerAdvanced::_font_set_allow_system_fallback(const RID &p_font_rid, bool p_allow_system_fallback) {
  1869. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1870. ERR_FAIL_COND(!fd);
  1871. MutexLock lock(fd->mutex);
  1872. fd->allow_system_fallback = p_allow_system_fallback;
  1873. }
  1874. bool TextServerAdvanced::_font_is_allow_system_fallback(const RID &p_font_rid) const {
  1875. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1876. ERR_FAIL_COND_V(!fd, false);
  1877. MutexLock lock(fd->mutex);
  1878. return fd->allow_system_fallback;
  1879. }
  1880. void TextServerAdvanced::_font_set_force_autohinter(const RID &p_font_rid, bool p_force_autohinter) {
  1881. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1882. ERR_FAIL_COND(!fd);
  1883. MutexLock lock(fd->mutex);
  1884. if (fd->force_autohinter != p_force_autohinter) {
  1885. _font_clear_cache(fd);
  1886. fd->force_autohinter = p_force_autohinter;
  1887. }
  1888. }
  1889. bool TextServerAdvanced::_font_is_force_autohinter(const RID &p_font_rid) const {
  1890. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1891. ERR_FAIL_COND_V(!fd, false);
  1892. MutexLock lock(fd->mutex);
  1893. return fd->force_autohinter;
  1894. }
  1895. void TextServerAdvanced::_font_set_hinting(const RID &p_font_rid, TextServer::Hinting p_hinting) {
  1896. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1897. ERR_FAIL_COND(!fd);
  1898. MutexLock lock(fd->mutex);
  1899. if (fd->hinting != p_hinting) {
  1900. _font_clear_cache(fd);
  1901. fd->hinting = p_hinting;
  1902. }
  1903. }
  1904. TextServer::Hinting TextServerAdvanced::_font_get_hinting(const RID &p_font_rid) const {
  1905. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1906. ERR_FAIL_COND_V(!fd, HINTING_NONE);
  1907. MutexLock lock(fd->mutex);
  1908. return fd->hinting;
  1909. }
  1910. void TextServerAdvanced::_font_set_subpixel_positioning(const RID &p_font_rid, TextServer::SubpixelPositioning p_subpixel) {
  1911. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1912. ERR_FAIL_COND(!fd);
  1913. MutexLock lock(fd->mutex);
  1914. fd->subpixel_positioning = p_subpixel;
  1915. }
  1916. TextServer::SubpixelPositioning TextServerAdvanced::_font_get_subpixel_positioning(const RID &p_font_rid) const {
  1917. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1918. ERR_FAIL_COND_V(!fd, SUBPIXEL_POSITIONING_DISABLED);
  1919. MutexLock lock(fd->mutex);
  1920. return fd->subpixel_positioning;
  1921. }
  1922. void TextServerAdvanced::_font_set_embolden(const RID &p_font_rid, double p_strength) {
  1923. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1924. ERR_FAIL_COND(!fd);
  1925. MutexLock lock(fd->mutex);
  1926. if (fd->embolden != p_strength) {
  1927. _font_clear_cache(fd);
  1928. fd->embolden = p_strength;
  1929. }
  1930. }
  1931. double TextServerAdvanced::_font_get_embolden(const RID &p_font_rid) const {
  1932. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1933. ERR_FAIL_COND_V(!fd, 0.0);
  1934. MutexLock lock(fd->mutex);
  1935. return fd->embolden;
  1936. }
  1937. void TextServerAdvanced::_font_set_transform(const RID &p_font_rid, const Transform2D &p_transform) {
  1938. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1939. ERR_FAIL_COND(!fd);
  1940. MutexLock lock(fd->mutex);
  1941. if (fd->transform != p_transform) {
  1942. _font_clear_cache(fd);
  1943. fd->transform = p_transform;
  1944. }
  1945. }
  1946. Transform2D TextServerAdvanced::_font_get_transform(const RID &p_font_rid) const {
  1947. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1948. ERR_FAIL_COND_V(!fd, Transform2D());
  1949. MutexLock lock(fd->mutex);
  1950. return fd->transform;
  1951. }
  1952. void TextServerAdvanced::_font_set_variation_coordinates(const RID &p_font_rid, const Dictionary &p_variation_coordinates) {
  1953. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1954. ERR_FAIL_COND(!fd);
  1955. MutexLock lock(fd->mutex);
  1956. if (fd->variation_coordinates != p_variation_coordinates) {
  1957. _font_clear_cache(fd);
  1958. fd->variation_coordinates = p_variation_coordinates;
  1959. }
  1960. }
  1961. Dictionary TextServerAdvanced::_font_get_variation_coordinates(const RID &p_font_rid) const {
  1962. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1963. ERR_FAIL_COND_V(!fd, Dictionary());
  1964. MutexLock lock(fd->mutex);
  1965. return fd->variation_coordinates;
  1966. }
  1967. void TextServerAdvanced::_font_set_oversampling(const RID &p_font_rid, double p_oversampling) {
  1968. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1969. ERR_FAIL_COND(!fd);
  1970. MutexLock lock(fd->mutex);
  1971. if (fd->oversampling != p_oversampling) {
  1972. _font_clear_cache(fd);
  1973. fd->oversampling = p_oversampling;
  1974. }
  1975. }
  1976. double TextServerAdvanced::_font_get_oversampling(const RID &p_font_rid) const {
  1977. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1978. ERR_FAIL_COND_V(!fd, 0.0);
  1979. MutexLock lock(fd->mutex);
  1980. return fd->oversampling;
  1981. }
  1982. TypedArray<Vector2i> TextServerAdvanced::_font_get_size_cache_list(const RID &p_font_rid) const {
  1983. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1984. ERR_FAIL_COND_V(!fd, TypedArray<Vector2i>());
  1985. MutexLock lock(fd->mutex);
  1986. TypedArray<Vector2i> ret;
  1987. for (const KeyValue<Vector2i, FontForSizeAdvanced *> &E : fd->cache) {
  1988. ret.push_back(E.key);
  1989. }
  1990. return ret;
  1991. }
  1992. void TextServerAdvanced::_font_clear_size_cache(const RID &p_font_rid) {
  1993. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  1994. ERR_FAIL_COND(!fd);
  1995. MutexLock lock(fd->mutex);
  1996. for (const KeyValue<Vector2i, FontForSizeAdvanced *> &E : fd->cache) {
  1997. memdelete(E.value);
  1998. }
  1999. fd->cache.clear();
  2000. }
  2001. void TextServerAdvanced::_font_remove_size_cache(const RID &p_font_rid, const Vector2i &p_size) {
  2002. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2003. ERR_FAIL_COND(!fd);
  2004. MutexLock lock(fd->mutex);
  2005. if (fd->cache.has(p_size)) {
  2006. memdelete(fd->cache[p_size]);
  2007. fd->cache.erase(p_size);
  2008. }
  2009. }
  2010. void TextServerAdvanced::_font_set_ascent(const RID &p_font_rid, int64_t p_size, double p_ascent) {
  2011. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2012. ERR_FAIL_COND(!fd);
  2013. MutexLock lock(fd->mutex);
  2014. Vector2i size = _get_size(fd, p_size);
  2015. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  2016. fd->cache[size]->ascent = p_ascent;
  2017. }
  2018. double TextServerAdvanced::_font_get_ascent(const RID &p_font_rid, int64_t p_size) const {
  2019. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2020. ERR_FAIL_COND_V(!fd, 0.0);
  2021. MutexLock lock(fd->mutex);
  2022. Vector2i size = _get_size(fd, p_size);
  2023. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), 0.0);
  2024. if (fd->msdf) {
  2025. return fd->cache[size]->ascent * (double)p_size / (double)fd->msdf_source_size;
  2026. } else {
  2027. return fd->cache[size]->ascent;
  2028. }
  2029. }
  2030. void TextServerAdvanced::_font_set_descent(const RID &p_font_rid, int64_t p_size, double p_descent) {
  2031. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2032. ERR_FAIL_COND(!fd);
  2033. Vector2i size = _get_size(fd, p_size);
  2034. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  2035. fd->cache[size]->descent = p_descent;
  2036. }
  2037. double TextServerAdvanced::_font_get_descent(const RID &p_font_rid, int64_t p_size) const {
  2038. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2039. ERR_FAIL_COND_V(!fd, 0.0);
  2040. MutexLock lock(fd->mutex);
  2041. Vector2i size = _get_size(fd, p_size);
  2042. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), 0.0);
  2043. if (fd->msdf) {
  2044. return fd->cache[size]->descent * (double)p_size / (double)fd->msdf_source_size;
  2045. } else {
  2046. return fd->cache[size]->descent;
  2047. }
  2048. }
  2049. void TextServerAdvanced::_font_set_underline_position(const RID &p_font_rid, int64_t p_size, double p_underline_position) {
  2050. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2051. ERR_FAIL_COND(!fd);
  2052. MutexLock lock(fd->mutex);
  2053. Vector2i size = _get_size(fd, p_size);
  2054. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  2055. fd->cache[size]->underline_position = p_underline_position;
  2056. }
  2057. double TextServerAdvanced::_font_get_underline_position(const RID &p_font_rid, int64_t p_size) const {
  2058. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2059. ERR_FAIL_COND_V(!fd, 0.0);
  2060. MutexLock lock(fd->mutex);
  2061. Vector2i size = _get_size(fd, p_size);
  2062. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), 0.0);
  2063. if (fd->msdf) {
  2064. return fd->cache[size]->underline_position * (double)p_size / (double)fd->msdf_source_size;
  2065. } else {
  2066. return fd->cache[size]->underline_position;
  2067. }
  2068. }
  2069. void TextServerAdvanced::_font_set_underline_thickness(const RID &p_font_rid, int64_t p_size, double p_underline_thickness) {
  2070. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2071. ERR_FAIL_COND(!fd);
  2072. MutexLock lock(fd->mutex);
  2073. Vector2i size = _get_size(fd, p_size);
  2074. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  2075. fd->cache[size]->underline_thickness = p_underline_thickness;
  2076. }
  2077. double TextServerAdvanced::_font_get_underline_thickness(const RID &p_font_rid, int64_t p_size) const {
  2078. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2079. ERR_FAIL_COND_V(!fd, 0.0);
  2080. MutexLock lock(fd->mutex);
  2081. Vector2i size = _get_size(fd, p_size);
  2082. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), 0.0);
  2083. if (fd->msdf) {
  2084. return fd->cache[size]->underline_thickness * (double)p_size / (double)fd->msdf_source_size;
  2085. } else {
  2086. return fd->cache[size]->underline_thickness;
  2087. }
  2088. }
  2089. void TextServerAdvanced::_font_set_scale(const RID &p_font_rid, int64_t p_size, double p_scale) {
  2090. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2091. ERR_FAIL_COND(!fd);
  2092. MutexLock lock(fd->mutex);
  2093. Vector2i size = _get_size(fd, p_size);
  2094. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  2095. #ifdef MODULE_FREETYPE_ENABLED
  2096. if (fd->cache[size]->face) {
  2097. return; // Do not override scale for dynamic fonts, it's calculated automatically.
  2098. }
  2099. #endif
  2100. fd->cache[size]->scale = p_scale;
  2101. }
  2102. double TextServerAdvanced::_font_get_scale(const RID &p_font_rid, int64_t p_size) const {
  2103. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2104. ERR_FAIL_COND_V(!fd, 0.0);
  2105. MutexLock lock(fd->mutex);
  2106. Vector2i size = _get_size(fd, p_size);
  2107. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), 0.0);
  2108. if (fd->msdf) {
  2109. return fd->cache[size]->scale * (double)p_size / (double)fd->msdf_source_size;
  2110. } else {
  2111. return fd->cache[size]->scale / fd->cache[size]->oversampling;
  2112. }
  2113. }
  2114. int64_t TextServerAdvanced::_font_get_texture_count(const RID &p_font_rid, const Vector2i &p_size) const {
  2115. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2116. ERR_FAIL_COND_V(!fd, 0);
  2117. MutexLock lock(fd->mutex);
  2118. Vector2i size = _get_size_outline(fd, p_size);
  2119. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), 0);
  2120. return fd->cache[size]->textures.size();
  2121. }
  2122. void TextServerAdvanced::_font_clear_textures(const RID &p_font_rid, const Vector2i &p_size) {
  2123. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2124. ERR_FAIL_COND(!fd);
  2125. MutexLock lock(fd->mutex);
  2126. Vector2i size = _get_size_outline(fd, p_size);
  2127. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  2128. fd->cache[size]->textures.clear();
  2129. }
  2130. void TextServerAdvanced::_font_remove_texture(const RID &p_font_rid, const Vector2i &p_size, int64_t p_texture_index) {
  2131. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2132. ERR_FAIL_COND(!fd);
  2133. MutexLock lock(fd->mutex);
  2134. Vector2i size = _get_size_outline(fd, p_size);
  2135. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  2136. ERR_FAIL_INDEX(p_texture_index, fd->cache[size]->textures.size());
  2137. fd->cache[size]->textures.remove_at(p_texture_index);
  2138. }
  2139. void TextServerAdvanced::_font_set_texture_image(const RID &p_font_rid, const Vector2i &p_size, int64_t p_texture_index, const Ref<Image> &p_image) {
  2140. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2141. ERR_FAIL_COND(!fd);
  2142. ERR_FAIL_COND(p_image.is_null());
  2143. MutexLock lock(fd->mutex);
  2144. Vector2i size = _get_size_outline(fd, p_size);
  2145. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  2146. ERR_FAIL_COND(p_texture_index < 0);
  2147. if (p_texture_index >= fd->cache[size]->textures.size()) {
  2148. fd->cache[size]->textures.resize(p_texture_index + 1);
  2149. }
  2150. ShelfPackTexture &tex = fd->cache[size]->textures.write[p_texture_index];
  2151. tex.imgdata = p_image->get_data();
  2152. tex.texture_w = p_image->get_width();
  2153. tex.texture_h = p_image->get_height();
  2154. tex.format = p_image->get_format();
  2155. Ref<Image> img = Image::create_from_data(tex.texture_w, tex.texture_h, false, tex.format, tex.imgdata);
  2156. if (fd->mipmaps) {
  2157. img->generate_mipmaps();
  2158. }
  2159. tex.texture = ImageTexture::create_from_image(img);
  2160. tex.dirty = false;
  2161. }
  2162. Ref<Image> TextServerAdvanced::_font_get_texture_image(const RID &p_font_rid, const Vector2i &p_size, int64_t p_texture_index) const {
  2163. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2164. ERR_FAIL_COND_V(!fd, Ref<Image>());
  2165. MutexLock lock(fd->mutex);
  2166. Vector2i size = _get_size_outline(fd, p_size);
  2167. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), Ref<Image>());
  2168. ERR_FAIL_INDEX_V(p_texture_index, fd->cache[size]->textures.size(), Ref<Image>());
  2169. const ShelfPackTexture &tex = fd->cache[size]->textures[p_texture_index];
  2170. return Image::create_from_data(tex.texture_w, tex.texture_h, false, tex.format, tex.imgdata);
  2171. }
  2172. void TextServerAdvanced::_font_set_texture_offsets(const RID &p_font_rid, const Vector2i &p_size, int64_t p_texture_index, const PackedInt32Array &p_offsets) {
  2173. ERR_FAIL_COND(p_offsets.size() % 4 != 0);
  2174. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2175. ERR_FAIL_COND(!fd);
  2176. MutexLock lock(fd->mutex);
  2177. Vector2i size = _get_size_outline(fd, p_size);
  2178. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  2179. ERR_FAIL_COND(p_texture_index < 0);
  2180. if (p_texture_index >= fd->cache[size]->textures.size()) {
  2181. fd->cache[size]->textures.resize(p_texture_index + 1);
  2182. }
  2183. ShelfPackTexture &tex = fd->cache[size]->textures.write[p_texture_index];
  2184. tex.shelves.clear();
  2185. for (int32_t i = 0; i < p_offsets.size(); i += 4) {
  2186. tex.shelves.push_back(Shelf(p_offsets[i], p_offsets[i + 1], p_offsets[i + 2], p_offsets[i + 3]));
  2187. }
  2188. }
  2189. PackedInt32Array TextServerAdvanced::_font_get_texture_offsets(const RID &p_font_rid, const Vector2i &p_size, int64_t p_texture_index) const {
  2190. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2191. ERR_FAIL_COND_V(!fd, PackedInt32Array());
  2192. MutexLock lock(fd->mutex);
  2193. Vector2i size = _get_size_outline(fd, p_size);
  2194. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), PackedInt32Array());
  2195. ERR_FAIL_INDEX_V(p_texture_index, fd->cache[size]->textures.size(), PackedInt32Array());
  2196. const ShelfPackTexture &tex = fd->cache[size]->textures[p_texture_index];
  2197. PackedInt32Array ret;
  2198. ret.resize(tex.shelves.size() * 4);
  2199. int32_t *wr = ret.ptrw();
  2200. int32_t i = 0;
  2201. for (const Shelf &E : tex.shelves) {
  2202. wr[i * 4] = E.x;
  2203. wr[i * 4 + 1] = E.y;
  2204. wr[i * 4 + 2] = E.w;
  2205. wr[i * 4 + 3] = E.h;
  2206. i++;
  2207. }
  2208. return ret;
  2209. }
  2210. PackedInt32Array TextServerAdvanced::_font_get_glyph_list(const RID &p_font_rid, const Vector2i &p_size) const {
  2211. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2212. ERR_FAIL_COND_V(!fd, PackedInt32Array());
  2213. MutexLock lock(fd->mutex);
  2214. Vector2i size = _get_size_outline(fd, p_size);
  2215. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), PackedInt32Array());
  2216. PackedInt32Array ret;
  2217. const HashMap<int32_t, FontGlyph> &gl = fd->cache[size]->glyph_map;
  2218. for (const KeyValue<int32_t, FontGlyph> &E : gl) {
  2219. ret.push_back(E.key);
  2220. }
  2221. return ret;
  2222. }
  2223. void TextServerAdvanced::_font_clear_glyphs(const RID &p_font_rid, const Vector2i &p_size) {
  2224. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2225. ERR_FAIL_COND(!fd);
  2226. MutexLock lock(fd->mutex);
  2227. Vector2i size = _get_size_outline(fd, p_size);
  2228. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  2229. fd->cache[size]->glyph_map.clear();
  2230. }
  2231. void TextServerAdvanced::_font_remove_glyph(const RID &p_font_rid, const Vector2i &p_size, int64_t p_glyph) {
  2232. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2233. ERR_FAIL_COND(!fd);
  2234. MutexLock lock(fd->mutex);
  2235. Vector2i size = _get_size_outline(fd, p_size);
  2236. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  2237. fd->cache[size]->glyph_map.erase(p_glyph);
  2238. }
  2239. double TextServerAdvanced::_get_extra_advance(RID p_font_rid, int p_font_size) const {
  2240. const FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2241. ERR_FAIL_COND_V(!fd, 0.0);
  2242. MutexLock lock(fd->mutex);
  2243. Vector2i size = _get_size(fd, p_font_size);
  2244. if (fd->embolden != 0.0) {
  2245. return fd->embolden * double(size.x) / 64.0;
  2246. } else {
  2247. return 0.0;
  2248. }
  2249. }
  2250. Vector2 TextServerAdvanced::_font_get_glyph_advance(const RID &p_font_rid, int64_t p_size, int64_t p_glyph) const {
  2251. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2252. ERR_FAIL_COND_V(!fd, Vector2());
  2253. MutexLock lock(fd->mutex);
  2254. Vector2i size = _get_size(fd, p_size);
  2255. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), Vector2());
  2256. int mod = 0;
  2257. if (fd->antialiasing == FONT_ANTIALIASING_LCD) {
  2258. TextServer::FontLCDSubpixelLayout layout = (TextServer::FontLCDSubpixelLayout)(int)GLOBAL_GET("gui/theme/lcd_subpixel_layout");
  2259. if (layout != FONT_LCD_SUBPIXEL_LAYOUT_NONE) {
  2260. mod = (layout << 24);
  2261. }
  2262. }
  2263. if (!_ensure_glyph(fd, size, p_glyph | mod)) {
  2264. return Vector2(); // Invalid or non graphicl glyph, do not display errors.
  2265. }
  2266. const HashMap<int32_t, FontGlyph> &gl = fd->cache[size]->glyph_map;
  2267. Vector2 ea;
  2268. if (fd->embolden != 0.0) {
  2269. ea.x = fd->embolden * double(size.x) / 64.0;
  2270. }
  2271. double scale = _font_get_scale(p_font_rid, p_size);
  2272. if (fd->msdf) {
  2273. return (gl[p_glyph | mod].advance + ea) * (double)p_size / (double)fd->msdf_source_size;
  2274. } 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))) {
  2275. return (gl[p_glyph | mod].advance + ea).round();
  2276. } else {
  2277. return gl[p_glyph | mod].advance + ea;
  2278. }
  2279. }
  2280. void TextServerAdvanced::_font_set_glyph_advance(const RID &p_font_rid, int64_t p_size, int64_t p_glyph, const Vector2 &p_advance) {
  2281. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2282. ERR_FAIL_COND(!fd);
  2283. MutexLock lock(fd->mutex);
  2284. Vector2i size = _get_size(fd, p_size);
  2285. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  2286. HashMap<int32_t, FontGlyph> &gl = fd->cache[size]->glyph_map;
  2287. gl[p_glyph].advance = p_advance;
  2288. gl[p_glyph].found = true;
  2289. }
  2290. Vector2 TextServerAdvanced::_font_get_glyph_offset(const RID &p_font_rid, const Vector2i &p_size, int64_t p_glyph) const {
  2291. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2292. ERR_FAIL_COND_V(!fd, Vector2());
  2293. MutexLock lock(fd->mutex);
  2294. Vector2i size = _get_size_outline(fd, p_size);
  2295. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), Vector2());
  2296. int mod = 0;
  2297. if (fd->antialiasing == FONT_ANTIALIASING_LCD) {
  2298. TextServer::FontLCDSubpixelLayout layout = (TextServer::FontLCDSubpixelLayout)(int)GLOBAL_GET("gui/theme/lcd_subpixel_layout");
  2299. if (layout != FONT_LCD_SUBPIXEL_LAYOUT_NONE) {
  2300. mod = (layout << 24);
  2301. }
  2302. }
  2303. if (!_ensure_glyph(fd, size, p_glyph | mod)) {
  2304. return Vector2(); // Invalid or non graphicl glyph, do not display errors.
  2305. }
  2306. const HashMap<int32_t, FontGlyph> &gl = fd->cache[size]->glyph_map;
  2307. if (fd->msdf) {
  2308. return gl[p_glyph | mod].rect.position * (double)p_size.x / (double)fd->msdf_source_size;
  2309. } else {
  2310. return gl[p_glyph | mod].rect.position;
  2311. }
  2312. }
  2313. void TextServerAdvanced::_font_set_glyph_offset(const RID &p_font_rid, const Vector2i &p_size, int64_t p_glyph, const Vector2 &p_offset) {
  2314. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2315. ERR_FAIL_COND(!fd);
  2316. MutexLock lock(fd->mutex);
  2317. Vector2i size = _get_size_outline(fd, p_size);
  2318. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  2319. HashMap<int32_t, FontGlyph> &gl = fd->cache[size]->glyph_map;
  2320. gl[p_glyph].rect.position = p_offset;
  2321. gl[p_glyph].found = true;
  2322. }
  2323. Vector2 TextServerAdvanced::_font_get_glyph_size(const RID &p_font_rid, const Vector2i &p_size, int64_t p_glyph) const {
  2324. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2325. ERR_FAIL_COND_V(!fd, Vector2());
  2326. MutexLock lock(fd->mutex);
  2327. Vector2i size = _get_size_outline(fd, p_size);
  2328. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), Vector2());
  2329. int mod = 0;
  2330. if (fd->antialiasing == FONT_ANTIALIASING_LCD) {
  2331. TextServer::FontLCDSubpixelLayout layout = (TextServer::FontLCDSubpixelLayout)(int)GLOBAL_GET("gui/theme/lcd_subpixel_layout");
  2332. if (layout != FONT_LCD_SUBPIXEL_LAYOUT_NONE) {
  2333. mod = (layout << 24);
  2334. }
  2335. }
  2336. if (!_ensure_glyph(fd, size, p_glyph | mod)) {
  2337. return Vector2(); // Invalid or non graphicl glyph, do not display errors.
  2338. }
  2339. const HashMap<int32_t, FontGlyph> &gl = fd->cache[size]->glyph_map;
  2340. if (fd->msdf) {
  2341. return gl[p_glyph | mod].rect.size * (double)p_size.x / (double)fd->msdf_source_size;
  2342. } else {
  2343. return gl[p_glyph | mod].rect.size;
  2344. }
  2345. }
  2346. void TextServerAdvanced::_font_set_glyph_size(const RID &p_font_rid, const Vector2i &p_size, int64_t p_glyph, const Vector2 &p_gl_size) {
  2347. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2348. ERR_FAIL_COND(!fd);
  2349. MutexLock lock(fd->mutex);
  2350. Vector2i size = _get_size_outline(fd, p_size);
  2351. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  2352. HashMap<int32_t, FontGlyph> &gl = fd->cache[size]->glyph_map;
  2353. gl[p_glyph].rect.size = p_gl_size;
  2354. gl[p_glyph].found = true;
  2355. }
  2356. Rect2 TextServerAdvanced::_font_get_glyph_uv_rect(const RID &p_font_rid, const Vector2i &p_size, int64_t p_glyph) const {
  2357. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2358. ERR_FAIL_COND_V(!fd, Rect2());
  2359. MutexLock lock(fd->mutex);
  2360. Vector2i size = _get_size_outline(fd, p_size);
  2361. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), Rect2());
  2362. int mod = 0;
  2363. if (fd->antialiasing == FONT_ANTIALIASING_LCD) {
  2364. TextServer::FontLCDSubpixelLayout layout = (TextServer::FontLCDSubpixelLayout)(int)GLOBAL_GET("gui/theme/lcd_subpixel_layout");
  2365. if (layout != FONT_LCD_SUBPIXEL_LAYOUT_NONE) {
  2366. mod = (layout << 24);
  2367. }
  2368. }
  2369. if (!_ensure_glyph(fd, size, p_glyph | mod)) {
  2370. return Rect2(); // Invalid or non graphicl glyph, do not display errors.
  2371. }
  2372. const HashMap<int32_t, FontGlyph> &gl = fd->cache[size]->glyph_map;
  2373. return gl[p_glyph | mod].uv_rect;
  2374. }
  2375. void TextServerAdvanced::_font_set_glyph_uv_rect(const RID &p_font_rid, const Vector2i &p_size, int64_t p_glyph, const Rect2 &p_uv_rect) {
  2376. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2377. ERR_FAIL_COND(!fd);
  2378. MutexLock lock(fd->mutex);
  2379. Vector2i size = _get_size_outline(fd, p_size);
  2380. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  2381. HashMap<int32_t, FontGlyph> &gl = fd->cache[size]->glyph_map;
  2382. gl[p_glyph].uv_rect = p_uv_rect;
  2383. gl[p_glyph].found = true;
  2384. }
  2385. int64_t TextServerAdvanced::_font_get_glyph_texture_idx(const RID &p_font_rid, const Vector2i &p_size, int64_t p_glyph) const {
  2386. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2387. ERR_FAIL_COND_V(!fd, -1);
  2388. MutexLock lock(fd->mutex);
  2389. Vector2i size = _get_size_outline(fd, p_size);
  2390. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), -1);
  2391. int mod = 0;
  2392. if (fd->antialiasing == FONT_ANTIALIASING_LCD) {
  2393. TextServer::FontLCDSubpixelLayout layout = (TextServer::FontLCDSubpixelLayout)(int)GLOBAL_GET("gui/theme/lcd_subpixel_layout");
  2394. if (layout != FONT_LCD_SUBPIXEL_LAYOUT_NONE) {
  2395. mod = (layout << 24);
  2396. }
  2397. }
  2398. if (!_ensure_glyph(fd, size, p_glyph | mod)) {
  2399. return -1; // Invalid or non graphicl glyph, do not display errors.
  2400. }
  2401. const HashMap<int32_t, FontGlyph> &gl = fd->cache[size]->glyph_map;
  2402. return gl[p_glyph | mod].texture_idx;
  2403. }
  2404. void TextServerAdvanced::_font_set_glyph_texture_idx(const RID &p_font_rid, const Vector2i &p_size, int64_t p_glyph, int64_t p_texture_idx) {
  2405. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2406. ERR_FAIL_COND(!fd);
  2407. MutexLock lock(fd->mutex);
  2408. Vector2i size = _get_size_outline(fd, p_size);
  2409. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  2410. HashMap<int32_t, FontGlyph> &gl = fd->cache[size]->glyph_map;
  2411. gl[p_glyph].texture_idx = p_texture_idx;
  2412. gl[p_glyph].found = true;
  2413. }
  2414. RID TextServerAdvanced::_font_get_glyph_texture_rid(const RID &p_font_rid, const Vector2i &p_size, int64_t p_glyph) const {
  2415. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2416. ERR_FAIL_COND_V(!fd, RID());
  2417. MutexLock lock(fd->mutex);
  2418. Vector2i size = _get_size_outline(fd, p_size);
  2419. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), RID());
  2420. int mod = 0;
  2421. if (fd->antialiasing == FONT_ANTIALIASING_LCD) {
  2422. TextServer::FontLCDSubpixelLayout layout = (TextServer::FontLCDSubpixelLayout)(int)GLOBAL_GET("gui/theme/lcd_subpixel_layout");
  2423. if (layout != FONT_LCD_SUBPIXEL_LAYOUT_NONE) {
  2424. mod = (layout << 24);
  2425. }
  2426. }
  2427. if (!_ensure_glyph(fd, size, p_glyph | mod)) {
  2428. return RID(); // Invalid or non graphicl glyph, do not display errors.
  2429. }
  2430. const HashMap<int32_t, FontGlyph> &gl = fd->cache[size]->glyph_map;
  2431. ERR_FAIL_COND_V(gl[p_glyph | mod].texture_idx < -1 || gl[p_glyph | mod].texture_idx >= fd->cache[size]->textures.size(), RID());
  2432. if (RenderingServer::get_singleton() != nullptr) {
  2433. if (gl[p_glyph | mod].texture_idx != -1) {
  2434. if (fd->cache[size]->textures[gl[p_glyph | mod].texture_idx].dirty) {
  2435. ShelfPackTexture &tex = fd->cache[size]->textures.write[gl[p_glyph | mod].texture_idx];
  2436. Ref<Image> img = Image::create_from_data(tex.texture_w, tex.texture_h, false, tex.format, tex.imgdata);
  2437. if (fd->mipmaps) {
  2438. img->generate_mipmaps();
  2439. }
  2440. if (tex.texture.is_null()) {
  2441. tex.texture = ImageTexture::create_from_image(img);
  2442. } else {
  2443. tex.texture->update(img);
  2444. }
  2445. tex.dirty = false;
  2446. }
  2447. return fd->cache[size]->textures[gl[p_glyph | mod].texture_idx].texture->get_rid();
  2448. }
  2449. }
  2450. return RID();
  2451. }
  2452. Size2 TextServerAdvanced::_font_get_glyph_texture_size(const RID &p_font_rid, const Vector2i &p_size, int64_t p_glyph) const {
  2453. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2454. ERR_FAIL_COND_V(!fd, Size2());
  2455. MutexLock lock(fd->mutex);
  2456. Vector2i size = _get_size_outline(fd, p_size);
  2457. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), Size2());
  2458. int mod = 0;
  2459. if (fd->antialiasing == FONT_ANTIALIASING_LCD) {
  2460. TextServer::FontLCDSubpixelLayout layout = (TextServer::FontLCDSubpixelLayout)(int)GLOBAL_GET("gui/theme/lcd_subpixel_layout");
  2461. if (layout != FONT_LCD_SUBPIXEL_LAYOUT_NONE) {
  2462. mod = (layout << 24);
  2463. }
  2464. }
  2465. if (!_ensure_glyph(fd, size, p_glyph | mod)) {
  2466. return Size2(); // Invalid or non graphicl glyph, do not display errors.
  2467. }
  2468. const HashMap<int32_t, FontGlyph> &gl = fd->cache[size]->glyph_map;
  2469. ERR_FAIL_COND_V(gl[p_glyph | mod].texture_idx < -1 || gl[p_glyph | mod].texture_idx >= fd->cache[size]->textures.size(), Size2());
  2470. if (RenderingServer::get_singleton() != nullptr) {
  2471. if (gl[p_glyph | mod].texture_idx != -1) {
  2472. if (fd->cache[size]->textures[gl[p_glyph | mod].texture_idx].dirty) {
  2473. ShelfPackTexture &tex = fd->cache[size]->textures.write[gl[p_glyph | mod].texture_idx];
  2474. Ref<Image> img = Image::create_from_data(tex.texture_w, tex.texture_h, false, tex.format, tex.imgdata);
  2475. if (fd->mipmaps) {
  2476. img->generate_mipmaps();
  2477. }
  2478. if (tex.texture.is_null()) {
  2479. tex.texture = ImageTexture::create_from_image(img);
  2480. } else {
  2481. tex.texture->update(img);
  2482. }
  2483. tex.dirty = false;
  2484. }
  2485. return fd->cache[size]->textures[gl[p_glyph | mod].texture_idx].texture->get_size();
  2486. }
  2487. }
  2488. return Size2();
  2489. }
  2490. Dictionary TextServerAdvanced::_font_get_glyph_contours(const RID &p_font_rid, int64_t p_size, int64_t p_index) const {
  2491. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2492. ERR_FAIL_COND_V(!fd, Dictionary());
  2493. MutexLock lock(fd->mutex);
  2494. Vector2i size = _get_size(fd, p_size);
  2495. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), Dictionary());
  2496. #ifdef MODULE_FREETYPE_ENABLED
  2497. PackedVector3Array points;
  2498. PackedInt32Array contours;
  2499. int32_t index = p_index & 0xffffff; // Remove subpixel shifts.
  2500. int error = FT_Load_Glyph(fd->cache[size]->face, index, FT_LOAD_NO_BITMAP | (fd->force_autohinter ? FT_LOAD_FORCE_AUTOHINT : 0));
  2501. ERR_FAIL_COND_V(error, Dictionary());
  2502. if (fd->embolden != 0.f) {
  2503. FT_Pos strength = fd->embolden * p_size * 4; // 26.6 fractional units (1 / 64).
  2504. FT_Outline_Embolden(&fd->cache[size]->face->glyph->outline, strength);
  2505. }
  2506. if (fd->transform != Transform2D()) {
  2507. 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).
  2508. FT_Outline_Transform(&fd->cache[size]->face->glyph->outline, &mat);
  2509. }
  2510. double scale = (1.0 / 64.0) / fd->cache[size]->oversampling * fd->cache[size]->scale;
  2511. if (fd->msdf) {
  2512. scale = scale * (double)p_size / (double)fd->msdf_source_size;
  2513. }
  2514. for (short i = 0; i < fd->cache[size]->face->glyph->outline.n_points; i++) {
  2515. points.push_back(Vector3(fd->cache[size]->face->glyph->outline.points[i].x * scale, -fd->cache[size]->face->glyph->outline.points[i].y * scale, FT_CURVE_TAG(fd->cache[size]->face->glyph->outline.tags[i])));
  2516. }
  2517. for (short i = 0; i < fd->cache[size]->face->glyph->outline.n_contours; i++) {
  2518. contours.push_back(fd->cache[size]->face->glyph->outline.contours[i]);
  2519. }
  2520. bool orientation = (FT_Outline_Get_Orientation(&fd->cache[size]->face->glyph->outline) == FT_ORIENTATION_FILL_RIGHT);
  2521. Dictionary out;
  2522. out["points"] = points;
  2523. out["contours"] = contours;
  2524. out["orientation"] = orientation;
  2525. return out;
  2526. #else
  2527. return Dictionary();
  2528. #endif
  2529. }
  2530. TypedArray<Vector2i> TextServerAdvanced::_font_get_kerning_list(const RID &p_font_rid, int64_t p_size) const {
  2531. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2532. ERR_FAIL_COND_V(!fd, TypedArray<Vector2i>());
  2533. MutexLock lock(fd->mutex);
  2534. Vector2i size = _get_size(fd, p_size);
  2535. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), TypedArray<Vector2i>());
  2536. TypedArray<Vector2i> ret;
  2537. for (const KeyValue<Vector2i, FontForSizeAdvanced *> &E : fd->cache) {
  2538. ret.push_back(E.key);
  2539. }
  2540. return ret;
  2541. }
  2542. void TextServerAdvanced::_font_clear_kerning_map(const RID &p_font_rid, int64_t p_size) {
  2543. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2544. ERR_FAIL_COND(!fd);
  2545. MutexLock lock(fd->mutex);
  2546. Vector2i size = _get_size(fd, p_size);
  2547. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  2548. fd->cache[size]->kerning_map.clear();
  2549. }
  2550. void TextServerAdvanced::_font_remove_kerning(const RID &p_font_rid, int64_t p_size, const Vector2i &p_glyph_pair) {
  2551. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2552. ERR_FAIL_COND(!fd);
  2553. MutexLock lock(fd->mutex);
  2554. Vector2i size = _get_size(fd, p_size);
  2555. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  2556. fd->cache[size]->kerning_map.erase(p_glyph_pair);
  2557. }
  2558. void TextServerAdvanced::_font_set_kerning(const RID &p_font_rid, int64_t p_size, const Vector2i &p_glyph_pair, const Vector2 &p_kerning) {
  2559. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2560. ERR_FAIL_COND(!fd);
  2561. MutexLock lock(fd->mutex);
  2562. Vector2i size = _get_size(fd, p_size);
  2563. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  2564. fd->cache[size]->kerning_map[p_glyph_pair] = p_kerning;
  2565. }
  2566. Vector2 TextServerAdvanced::_font_get_kerning(const RID &p_font_rid, int64_t p_size, const Vector2i &p_glyph_pair) const {
  2567. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2568. ERR_FAIL_COND_V(!fd, Vector2());
  2569. MutexLock lock(fd->mutex);
  2570. Vector2i size = _get_size(fd, p_size);
  2571. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), Vector2());
  2572. const HashMap<Vector2i, Vector2> &kern = fd->cache[size]->kerning_map;
  2573. if (kern.has(p_glyph_pair)) {
  2574. if (fd->msdf) {
  2575. return kern[p_glyph_pair] * (double)p_size / (double)fd->msdf_source_size;
  2576. } else {
  2577. return kern[p_glyph_pair];
  2578. }
  2579. } else {
  2580. #ifdef MODULE_FREETYPE_ENABLED
  2581. if (fd->cache[size]->face) {
  2582. FT_Vector delta;
  2583. FT_Get_Kerning(fd->cache[size]->face, p_glyph_pair.x, p_glyph_pair.y, FT_KERNING_DEFAULT, &delta);
  2584. if (fd->msdf) {
  2585. return Vector2(delta.x, delta.y) * (double)p_size / (double)fd->msdf_source_size;
  2586. } else {
  2587. return Vector2(delta.x, delta.y);
  2588. }
  2589. }
  2590. #endif
  2591. }
  2592. return Vector2();
  2593. }
  2594. int64_t TextServerAdvanced::_font_get_glyph_index(const RID &p_font_rid, int64_t p_size, int64_t p_char, int64_t p_variation_selector) const {
  2595. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2596. ERR_FAIL_COND_V(!fd, 0);
  2597. 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) + ".");
  2598. ERR_FAIL_COND_V_MSG((p_variation_selector >= 0xd800 && p_variation_selector <= 0xdfff) || (p_variation_selector > 0x10ffff), 0, "Unicode parsing error: Invalid unicode codepoint " + String::num_int64(p_variation_selector, 16) + ".");
  2599. MutexLock lock(fd->mutex);
  2600. Vector2i size = _get_size(fd, p_size);
  2601. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), 0);
  2602. #ifdef MODULE_FREETYPE_ENABLED
  2603. if (fd->cache[size]->face) {
  2604. if (p_variation_selector) {
  2605. return FT_Face_GetCharVariantIndex(fd->cache[size]->face, p_char, p_variation_selector);
  2606. } else {
  2607. return FT_Get_Char_Index(fd->cache[size]->face, p_char);
  2608. }
  2609. } else {
  2610. return (int64_t)p_char;
  2611. }
  2612. #else
  2613. return (int64_t)p_char;
  2614. #endif
  2615. }
  2616. bool TextServerAdvanced::_font_has_char(const RID &p_font_rid, int64_t p_char) const {
  2617. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2618. 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) + ".");
  2619. if (!fd) {
  2620. return false;
  2621. }
  2622. MutexLock lock(fd->mutex);
  2623. if (fd->cache.is_empty()) {
  2624. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, fd->msdf ? Vector2i(fd->msdf_source_size, 0) : Vector2i(16, 0)), false);
  2625. }
  2626. FontForSizeAdvanced *at_size = fd->cache.begin()->value;
  2627. #ifdef MODULE_FREETYPE_ENABLED
  2628. if (at_size && at_size->face) {
  2629. return FT_Get_Char_Index(at_size->face, p_char) != 0;
  2630. }
  2631. #endif
  2632. return (at_size) ? at_size->glyph_map.has((int32_t)p_char) : false;
  2633. }
  2634. String TextServerAdvanced::_font_get_supported_chars(const RID &p_font_rid) const {
  2635. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2636. ERR_FAIL_COND_V(!fd, String());
  2637. MutexLock lock(fd->mutex);
  2638. if (fd->cache.is_empty()) {
  2639. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, fd->msdf ? Vector2i(fd->msdf_source_size, 0) : Vector2i(16, 0)), String());
  2640. }
  2641. FontForSizeAdvanced *at_size = fd->cache.begin()->value;
  2642. String chars;
  2643. #ifdef MODULE_FREETYPE_ENABLED
  2644. if (at_size && at_size->face) {
  2645. FT_UInt gindex;
  2646. FT_ULong charcode = FT_Get_First_Char(at_size->face, &gindex);
  2647. while (gindex != 0) {
  2648. if (charcode != 0) {
  2649. chars = chars + String::chr(charcode);
  2650. }
  2651. charcode = FT_Get_Next_Char(at_size->face, charcode, &gindex);
  2652. }
  2653. return chars;
  2654. }
  2655. #endif
  2656. if (at_size) {
  2657. const HashMap<int32_t, FontGlyph> &gl = at_size->glyph_map;
  2658. for (const KeyValue<int32_t, FontGlyph> &E : gl) {
  2659. chars = chars + String::chr(E.key);
  2660. }
  2661. }
  2662. return chars;
  2663. }
  2664. void TextServerAdvanced::_font_render_range(const RID &p_font_rid, const Vector2i &p_size, int64_t p_start, int64_t p_end) {
  2665. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2666. ERR_FAIL_COND(!fd);
  2667. 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) + ".");
  2668. 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) + ".");
  2669. MutexLock lock(fd->mutex);
  2670. Vector2i size = _get_size_outline(fd, p_size);
  2671. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  2672. for (int64_t i = p_start; i <= p_end; i++) {
  2673. #ifdef MODULE_FREETYPE_ENABLED
  2674. int32_t idx = FT_Get_Char_Index(fd->cache[size]->face, i);
  2675. if (fd->cache[size]->face) {
  2676. if (fd->msdf) {
  2677. _ensure_glyph(fd, size, (int32_t)idx);
  2678. } else {
  2679. for (int aa = 0; aa < ((fd->antialiasing == FONT_ANTIALIASING_LCD) ? FONT_LCD_SUBPIXEL_LAYOUT_MAX : 1); aa++) {
  2680. if ((fd->subpixel_positioning == SUBPIXEL_POSITIONING_ONE_QUARTER) || (fd->subpixel_positioning == SUBPIXEL_POSITIONING_AUTO && size.x <= SUBPIXEL_POSITIONING_ONE_QUARTER_MAX_SIZE)) {
  2681. _ensure_glyph(fd, size, (int32_t)idx | (0 << 27) | (aa << 24));
  2682. _ensure_glyph(fd, size, (int32_t)idx | (1 << 27) | (aa << 24));
  2683. _ensure_glyph(fd, size, (int32_t)idx | (2 << 27) | (aa << 24));
  2684. _ensure_glyph(fd, size, (int32_t)idx | (3 << 27) | (aa << 24));
  2685. } else if ((fd->subpixel_positioning == SUBPIXEL_POSITIONING_ONE_HALF) || (fd->subpixel_positioning == SUBPIXEL_POSITIONING_AUTO && size.x <= SUBPIXEL_POSITIONING_ONE_HALF_MAX_SIZE)) {
  2686. _ensure_glyph(fd, size, (int32_t)idx | (1 << 27) | (aa << 24));
  2687. _ensure_glyph(fd, size, (int32_t)idx | (0 << 27) | (aa << 24));
  2688. } else {
  2689. _ensure_glyph(fd, size, (int32_t)idx | (aa << 24));
  2690. }
  2691. }
  2692. }
  2693. }
  2694. #endif
  2695. }
  2696. }
  2697. void TextServerAdvanced::_font_render_glyph(const RID &p_font_rid, const Vector2i &p_size, int64_t p_index) {
  2698. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2699. ERR_FAIL_COND(!fd);
  2700. MutexLock lock(fd->mutex);
  2701. Vector2i size = _get_size_outline(fd, p_size);
  2702. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  2703. #ifdef MODULE_FREETYPE_ENABLED
  2704. int32_t idx = p_index & 0xffffff; // Remove subpixel shifts.
  2705. if (fd->cache[size]->face) {
  2706. if (fd->msdf) {
  2707. _ensure_glyph(fd, size, (int32_t)idx);
  2708. } else {
  2709. for (int aa = 0; aa < ((fd->antialiasing == FONT_ANTIALIASING_LCD) ? FONT_LCD_SUBPIXEL_LAYOUT_MAX : 1); aa++) {
  2710. if ((fd->subpixel_positioning == SUBPIXEL_POSITIONING_ONE_QUARTER) || (fd->subpixel_positioning == SUBPIXEL_POSITIONING_AUTO && size.x <= SUBPIXEL_POSITIONING_ONE_QUARTER_MAX_SIZE)) {
  2711. _ensure_glyph(fd, size, (int32_t)idx | (0 << 27) | (aa << 24));
  2712. _ensure_glyph(fd, size, (int32_t)idx | (1 << 27) | (aa << 24));
  2713. _ensure_glyph(fd, size, (int32_t)idx | (2 << 27) | (aa << 24));
  2714. _ensure_glyph(fd, size, (int32_t)idx | (3 << 27) | (aa << 24));
  2715. } else if ((fd->subpixel_positioning == SUBPIXEL_POSITIONING_ONE_HALF) || (fd->subpixel_positioning == SUBPIXEL_POSITIONING_AUTO && size.x <= SUBPIXEL_POSITIONING_ONE_HALF_MAX_SIZE)) {
  2716. _ensure_glyph(fd, size, (int32_t)idx | (1 << 27) | (aa << 24));
  2717. _ensure_glyph(fd, size, (int32_t)idx | (0 << 27) | (aa << 24));
  2718. } else {
  2719. _ensure_glyph(fd, size, (int32_t)idx | (aa << 24));
  2720. }
  2721. }
  2722. }
  2723. }
  2724. #endif
  2725. }
  2726. void TextServerAdvanced::_font_draw_glyph(const RID &p_font_rid, const RID &p_canvas, int64_t p_size, const Vector2 &p_pos, int64_t p_index, const Color &p_color) const {
  2727. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2728. ERR_FAIL_COND(!fd);
  2729. MutexLock lock(fd->mutex);
  2730. Vector2i size = _get_size(fd, p_size);
  2731. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  2732. int32_t index = p_index & 0xffffff; // Remove subpixel shifts.
  2733. bool lcd_aa = false;
  2734. #ifdef MODULE_FREETYPE_ENABLED
  2735. if (!fd->msdf && fd->cache[size]->face) {
  2736. // LCD layout, bits 24, 25, 26
  2737. if (fd->antialiasing == FONT_ANTIALIASING_LCD) {
  2738. TextServer::FontLCDSubpixelLayout layout = (TextServer::FontLCDSubpixelLayout)(int)GLOBAL_GET("gui/theme/lcd_subpixel_layout");
  2739. if (layout != FONT_LCD_SUBPIXEL_LAYOUT_NONE) {
  2740. lcd_aa = true;
  2741. index = index | (layout << 24);
  2742. }
  2743. }
  2744. // Subpixel X-shift, bits 27, 28
  2745. if ((fd->subpixel_positioning == SUBPIXEL_POSITIONING_ONE_QUARTER) || (fd->subpixel_positioning == SUBPIXEL_POSITIONING_AUTO && size.x <= SUBPIXEL_POSITIONING_ONE_QUARTER_MAX_SIZE)) {
  2746. int xshift = (int)(Math::floor(4 * (p_pos.x + 0.125)) - 4 * Math::floor(p_pos.x + 0.125));
  2747. index = index | (xshift << 27);
  2748. } else if ((fd->subpixel_positioning == SUBPIXEL_POSITIONING_ONE_HALF) || (fd->subpixel_positioning == SUBPIXEL_POSITIONING_AUTO && size.x <= SUBPIXEL_POSITIONING_ONE_HALF_MAX_SIZE)) {
  2749. int xshift = (int)(Math::floor(2 * (p_pos.x + 0.25)) - 2 * Math::floor(p_pos.x + 0.25));
  2750. index = index | (xshift << 27);
  2751. }
  2752. }
  2753. #endif
  2754. if (!_ensure_glyph(fd, size, index)) {
  2755. return; // Invalid or non-graphical glyph, do not display errors, nothing to draw.
  2756. }
  2757. const FontGlyph &gl = fd->cache[size]->glyph_map[index];
  2758. if (gl.found) {
  2759. ERR_FAIL_COND(gl.texture_idx < -1 || gl.texture_idx >= fd->cache[size]->textures.size());
  2760. if (gl.texture_idx != -1) {
  2761. Color modulate = p_color;
  2762. #ifdef MODULE_FREETYPE_ENABLED
  2763. if (fd->cache[size]->face && (fd->cache[size]->textures[gl.texture_idx].format == Image::FORMAT_RGBA8) && !lcd_aa && !fd->msdf) {
  2764. modulate.r = modulate.g = modulate.b = 1.0;
  2765. }
  2766. #endif
  2767. if (RenderingServer::get_singleton() != nullptr) {
  2768. if (fd->cache[size]->textures[gl.texture_idx].dirty) {
  2769. ShelfPackTexture &tex = fd->cache[size]->textures.write[gl.texture_idx];
  2770. Ref<Image> img = Image::create_from_data(tex.texture_w, tex.texture_h, false, tex.format, tex.imgdata);
  2771. if (fd->mipmaps) {
  2772. img->generate_mipmaps();
  2773. }
  2774. if (tex.texture.is_null()) {
  2775. tex.texture = ImageTexture::create_from_image(img);
  2776. } else {
  2777. tex.texture->update(img);
  2778. }
  2779. tex.dirty = false;
  2780. }
  2781. RID texture = fd->cache[size]->textures[gl.texture_idx].texture->get_rid();
  2782. if (fd->msdf) {
  2783. Point2 cpos = p_pos;
  2784. cpos += gl.rect.position * (double)p_size / (double)fd->msdf_source_size;
  2785. Size2 csize = gl.rect.size * (double)p_size / (double)fd->msdf_source_size;
  2786. RenderingServer::get_singleton()->canvas_item_add_msdf_texture_rect_region(p_canvas, Rect2(cpos, csize), texture, gl.uv_rect, modulate, 0, fd->msdf_range);
  2787. } else {
  2788. double scale = _font_get_scale(p_font_rid, p_size);
  2789. Point2 cpos = p_pos;
  2790. if ((fd->subpixel_positioning == SUBPIXEL_POSITIONING_ONE_QUARTER) || (fd->subpixel_positioning == SUBPIXEL_POSITIONING_AUTO && size.x <= SUBPIXEL_POSITIONING_ONE_QUARTER_MAX_SIZE)) {
  2791. cpos.x = cpos.x + 0.125;
  2792. } else if ((fd->subpixel_positioning == SUBPIXEL_POSITIONING_ONE_HALF) || (fd->subpixel_positioning == SUBPIXEL_POSITIONING_AUTO && size.x <= SUBPIXEL_POSITIONING_ONE_HALF_MAX_SIZE)) {
  2793. cpos.x = cpos.x + 0.25;
  2794. }
  2795. if (scale == 1.0) {
  2796. cpos.y = Math::floor(cpos.y);
  2797. cpos.x = Math::floor(cpos.x);
  2798. }
  2799. cpos += gl.rect.position;
  2800. Size2 csize = gl.rect.size;
  2801. if (lcd_aa) {
  2802. RenderingServer::get_singleton()->canvas_item_add_lcd_texture_rect_region(p_canvas, Rect2(cpos, csize), texture, gl.uv_rect, modulate);
  2803. } else {
  2804. RenderingServer::get_singleton()->canvas_item_add_texture_rect_region(p_canvas, Rect2(cpos, csize), texture, gl.uv_rect, modulate, false, false);
  2805. }
  2806. }
  2807. }
  2808. }
  2809. }
  2810. }
  2811. void TextServerAdvanced::_font_draw_glyph_outline(const RID &p_font_rid, const RID &p_canvas, int64_t p_size, int64_t p_outline_size, const Vector2 &p_pos, int64_t p_index, const Color &p_color) const {
  2812. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2813. ERR_FAIL_COND(!fd);
  2814. MutexLock lock(fd->mutex);
  2815. Vector2i size = _get_size_outline(fd, Vector2i(p_size, p_outline_size));
  2816. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  2817. int32_t index = p_index & 0xffffff; // Remove subpixel shifts.
  2818. bool lcd_aa = false;
  2819. #ifdef MODULE_FREETYPE_ENABLED
  2820. if (!fd->msdf && fd->cache[size]->face) {
  2821. // LCD layout, bits 24, 25, 26
  2822. if (fd->antialiasing == FONT_ANTIALIASING_LCD) {
  2823. TextServer::FontLCDSubpixelLayout layout = (TextServer::FontLCDSubpixelLayout)(int)GLOBAL_GET("gui/theme/lcd_subpixel_layout");
  2824. if (layout != FONT_LCD_SUBPIXEL_LAYOUT_NONE) {
  2825. lcd_aa = true;
  2826. index = index | (layout << 24);
  2827. }
  2828. }
  2829. // Subpixel X-shift, bits 27, 28
  2830. if ((fd->subpixel_positioning == SUBPIXEL_POSITIONING_ONE_QUARTER) || (fd->subpixel_positioning == SUBPIXEL_POSITIONING_AUTO && size.x <= SUBPIXEL_POSITIONING_ONE_QUARTER_MAX_SIZE)) {
  2831. int xshift = (int)(Math::floor(4 * (p_pos.x + 0.125)) - 4 * Math::floor(p_pos.x + 0.125));
  2832. index = index | (xshift << 27);
  2833. } else if ((fd->subpixel_positioning == SUBPIXEL_POSITIONING_ONE_HALF) || (fd->subpixel_positioning == SUBPIXEL_POSITIONING_AUTO && size.x <= SUBPIXEL_POSITIONING_ONE_HALF_MAX_SIZE)) {
  2834. int xshift = (int)(Math::floor(2 * (p_pos.x + 0.25)) - 2 * Math::floor(p_pos.x + 0.25));
  2835. index = index | (xshift << 27);
  2836. }
  2837. }
  2838. #endif
  2839. if (!_ensure_glyph(fd, size, index)) {
  2840. return; // Invalid or non-graphical glyph, do not display errors, nothing to draw.
  2841. }
  2842. const FontGlyph &gl = fd->cache[size]->glyph_map[index];
  2843. if (gl.found) {
  2844. ERR_FAIL_COND(gl.texture_idx < -1 || gl.texture_idx >= fd->cache[size]->textures.size());
  2845. if (gl.texture_idx != -1) {
  2846. Color modulate = p_color;
  2847. #ifdef MODULE_FREETYPE_ENABLED
  2848. if (fd->cache[size]->face && (fd->cache[size]->textures[gl.texture_idx].format == Image::FORMAT_RGBA8) && !lcd_aa && !fd->msdf) {
  2849. modulate.r = modulate.g = modulate.b = 1.0;
  2850. }
  2851. #endif
  2852. if (RenderingServer::get_singleton() != nullptr) {
  2853. if (fd->cache[size]->textures[gl.texture_idx].dirty) {
  2854. ShelfPackTexture &tex = fd->cache[size]->textures.write[gl.texture_idx];
  2855. Ref<Image> img = Image::create_from_data(tex.texture_w, tex.texture_h, false, tex.format, tex.imgdata);
  2856. if (fd->mipmaps) {
  2857. img->generate_mipmaps();
  2858. }
  2859. if (tex.texture.is_null()) {
  2860. tex.texture = ImageTexture::create_from_image(img);
  2861. } else {
  2862. tex.texture->update(img);
  2863. }
  2864. tex.dirty = false;
  2865. }
  2866. RID texture = fd->cache[size]->textures[gl.texture_idx].texture->get_rid();
  2867. if (fd->msdf) {
  2868. Point2 cpos = p_pos;
  2869. cpos += gl.rect.position * (double)p_size / (double)fd->msdf_source_size;
  2870. Size2 csize = gl.rect.size * (double)p_size / (double)fd->msdf_source_size;
  2871. RenderingServer::get_singleton()->canvas_item_add_msdf_texture_rect_region(p_canvas, Rect2(cpos, csize), texture, gl.uv_rect, modulate, p_outline_size * 2, fd->msdf_range);
  2872. } else {
  2873. Point2 cpos = p_pos;
  2874. double scale = _font_get_scale(p_font_rid, p_size);
  2875. if ((fd->subpixel_positioning == SUBPIXEL_POSITIONING_ONE_QUARTER) || (fd->subpixel_positioning == SUBPIXEL_POSITIONING_AUTO && size.x <= SUBPIXEL_POSITIONING_ONE_QUARTER_MAX_SIZE)) {
  2876. cpos.x = cpos.x + 0.125;
  2877. } else if ((fd->subpixel_positioning == SUBPIXEL_POSITIONING_ONE_HALF) || (fd->subpixel_positioning == SUBPIXEL_POSITIONING_AUTO && size.x <= SUBPIXEL_POSITIONING_ONE_HALF_MAX_SIZE)) {
  2878. cpos.x = cpos.x + 0.25;
  2879. }
  2880. if (scale == 1.0) {
  2881. cpos.y = Math::floor(cpos.y);
  2882. cpos.x = Math::floor(cpos.x);
  2883. }
  2884. cpos += gl.rect.position;
  2885. Size2 csize = gl.rect.size;
  2886. if (lcd_aa) {
  2887. RenderingServer::get_singleton()->canvas_item_add_lcd_texture_rect_region(p_canvas, Rect2(cpos, csize), texture, gl.uv_rect, modulate);
  2888. } else {
  2889. RenderingServer::get_singleton()->canvas_item_add_texture_rect_region(p_canvas, Rect2(cpos, csize), texture, gl.uv_rect, modulate, false, false);
  2890. }
  2891. }
  2892. }
  2893. }
  2894. }
  2895. }
  2896. bool TextServerAdvanced::_font_is_language_supported(const RID &p_font_rid, const String &p_language) const {
  2897. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2898. ERR_FAIL_COND_V(!fd, false);
  2899. MutexLock lock(fd->mutex);
  2900. if (fd->language_support_overrides.has(p_language)) {
  2901. return fd->language_support_overrides[p_language];
  2902. } else {
  2903. return true;
  2904. }
  2905. }
  2906. void TextServerAdvanced::_font_set_language_support_override(const RID &p_font_rid, const String &p_language, bool p_supported) {
  2907. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2908. ERR_FAIL_COND(!fd);
  2909. MutexLock lock(fd->mutex);
  2910. fd->language_support_overrides[p_language] = p_supported;
  2911. }
  2912. bool TextServerAdvanced::_font_get_language_support_override(const RID &p_font_rid, const String &p_language) {
  2913. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2914. ERR_FAIL_COND_V(!fd, false);
  2915. MutexLock lock(fd->mutex);
  2916. return fd->language_support_overrides[p_language];
  2917. }
  2918. void TextServerAdvanced::_font_remove_language_support_override(const RID &p_font_rid, const String &p_language) {
  2919. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2920. ERR_FAIL_COND(!fd);
  2921. MutexLock lock(fd->mutex);
  2922. fd->language_support_overrides.erase(p_language);
  2923. }
  2924. PackedStringArray TextServerAdvanced::_font_get_language_support_overrides(const RID &p_font_rid) {
  2925. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2926. ERR_FAIL_COND_V(!fd, PackedStringArray());
  2927. MutexLock lock(fd->mutex);
  2928. PackedStringArray out;
  2929. for (const KeyValue<String, bool> &E : fd->language_support_overrides) {
  2930. out.push_back(E.key);
  2931. }
  2932. return out;
  2933. }
  2934. bool TextServerAdvanced::_font_is_script_supported(const RID &p_font_rid, const String &p_script) const {
  2935. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2936. ERR_FAIL_COND_V(!fd, false);
  2937. MutexLock lock(fd->mutex);
  2938. if (fd->script_support_overrides.has(p_script)) {
  2939. return fd->script_support_overrides[p_script];
  2940. } else {
  2941. Vector2i size = _get_size(fd, 16);
  2942. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), false);
  2943. return fd->supported_scripts.has(hb_tag_from_string(p_script.ascii().get_data(), -1));
  2944. }
  2945. }
  2946. void TextServerAdvanced::_font_set_script_support_override(const RID &p_font_rid, const String &p_script, bool p_supported) {
  2947. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2948. ERR_FAIL_COND(!fd);
  2949. MutexLock lock(fd->mutex);
  2950. fd->script_support_overrides[p_script] = p_supported;
  2951. }
  2952. bool TextServerAdvanced::_font_get_script_support_override(const RID &p_font_rid, const String &p_script) {
  2953. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2954. ERR_FAIL_COND_V(!fd, false);
  2955. MutexLock lock(fd->mutex);
  2956. return fd->script_support_overrides[p_script];
  2957. }
  2958. void TextServerAdvanced::_font_remove_script_support_override(const RID &p_font_rid, const String &p_script) {
  2959. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2960. ERR_FAIL_COND(!fd);
  2961. MutexLock lock(fd->mutex);
  2962. fd->script_support_overrides.erase(p_script);
  2963. }
  2964. PackedStringArray TextServerAdvanced::_font_get_script_support_overrides(const RID &p_font_rid) {
  2965. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2966. ERR_FAIL_COND_V(!fd, PackedStringArray());
  2967. MutexLock lock(fd->mutex);
  2968. PackedStringArray out;
  2969. for (const KeyValue<String, bool> &E : fd->script_support_overrides) {
  2970. out.push_back(E.key);
  2971. }
  2972. return out;
  2973. }
  2974. void TextServerAdvanced::_font_set_opentype_feature_overrides(const RID &p_font_rid, const Dictionary &p_overrides) {
  2975. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2976. ERR_FAIL_COND(!fd);
  2977. MutexLock lock(fd->mutex);
  2978. Vector2i size = _get_size(fd, 16);
  2979. ERR_FAIL_COND(!_ensure_cache_for_size(fd, size));
  2980. fd->feature_overrides = p_overrides;
  2981. }
  2982. Dictionary TextServerAdvanced::_font_get_opentype_feature_overrides(const RID &p_font_rid) const {
  2983. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2984. ERR_FAIL_COND_V(!fd, Dictionary());
  2985. MutexLock lock(fd->mutex);
  2986. return fd->feature_overrides;
  2987. }
  2988. Dictionary TextServerAdvanced::_font_supported_feature_list(const RID &p_font_rid) const {
  2989. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2990. ERR_FAIL_COND_V(!fd, Dictionary());
  2991. MutexLock lock(fd->mutex);
  2992. Vector2i size = _get_size(fd, 16);
  2993. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), Dictionary());
  2994. return fd->supported_features;
  2995. }
  2996. Dictionary TextServerAdvanced::_font_supported_variation_list(const RID &p_font_rid) const {
  2997. FontAdvanced *fd = font_owner.get_or_null(p_font_rid);
  2998. ERR_FAIL_COND_V(!fd, Dictionary());
  2999. MutexLock lock(fd->mutex);
  3000. Vector2i size = _get_size(fd, 16);
  3001. ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size), Dictionary());
  3002. return fd->supported_varaitions;
  3003. }
  3004. double TextServerAdvanced::_font_get_global_oversampling() const {
  3005. return oversampling;
  3006. }
  3007. void TextServerAdvanced::_font_set_global_oversampling(double p_oversampling) {
  3008. _THREAD_SAFE_METHOD_
  3009. if (oversampling != p_oversampling) {
  3010. oversampling = p_oversampling;
  3011. List<RID> fonts;
  3012. font_owner.get_owned_list(&fonts);
  3013. bool font_cleared = false;
  3014. for (const RID &E : fonts) {
  3015. if (!_font_is_multichannel_signed_distance_field(E) && _font_get_oversampling(E) <= 0) {
  3016. _font_clear_size_cache(E);
  3017. font_cleared = true;
  3018. }
  3019. }
  3020. if (font_cleared) {
  3021. List<RID> text_bufs;
  3022. shaped_owner.get_owned_list(&text_bufs);
  3023. for (const RID &E : text_bufs) {
  3024. invalidate(shaped_owner.get_or_null(E), false);
  3025. }
  3026. }
  3027. }
  3028. }
  3029. /*************************************************************************/
  3030. /* Shaped text buffer interface */
  3031. /*************************************************************************/
  3032. int64_t TextServerAdvanced::_convert_pos(const String &p_utf32, const Char16String &p_utf16, int64_t p_pos) const {
  3033. int64_t limit = p_pos;
  3034. if (p_utf32.length() != p_utf16.length()) {
  3035. const UChar *data = p_utf16.get_data();
  3036. for (int i = 0; i < p_pos; i++) {
  3037. if (U16_IS_LEAD(data[i])) {
  3038. limit--;
  3039. }
  3040. }
  3041. }
  3042. return limit;
  3043. }
  3044. int64_t TextServerAdvanced::_convert_pos(const ShapedTextDataAdvanced *p_sd, int64_t p_pos) const {
  3045. int64_t limit = p_pos;
  3046. if (p_sd->text.length() != p_sd->utf16.length()) {
  3047. const UChar *data = p_sd->utf16.get_data();
  3048. for (int i = 0; i < p_pos; i++) {
  3049. if (U16_IS_LEAD(data[i])) {
  3050. limit--;
  3051. }
  3052. }
  3053. }
  3054. return limit;
  3055. }
  3056. int64_t TextServerAdvanced::_convert_pos_inv(const ShapedTextDataAdvanced *p_sd, int64_t p_pos) const {
  3057. int64_t limit = p_pos;
  3058. if (p_sd->text.length() != p_sd->utf16.length()) {
  3059. for (int i = 0; i < p_pos; i++) {
  3060. if (p_sd->text[i] > 0xffff) {
  3061. limit++;
  3062. }
  3063. }
  3064. }
  3065. return limit;
  3066. }
  3067. void TextServerAdvanced::invalidate(TextServerAdvanced::ShapedTextDataAdvanced *p_shaped, bool p_text) {
  3068. p_shaped->valid = false;
  3069. p_shaped->sort_valid = false;
  3070. p_shaped->line_breaks_valid = false;
  3071. p_shaped->justification_ops_valid = false;
  3072. p_shaped->text_trimmed = false;
  3073. p_shaped->ascent = 0.0;
  3074. p_shaped->descent = 0.0;
  3075. p_shaped->width = 0.0;
  3076. p_shaped->upos = 0.0;
  3077. p_shaped->uthk = 0.0;
  3078. p_shaped->glyphs.clear();
  3079. p_shaped->glyphs_logical.clear();
  3080. p_shaped->overrun_trim_data = TrimData();
  3081. p_shaped->utf16 = Char16String();
  3082. for (int i = 0; i < p_shaped->bidi_iter.size(); i++) {
  3083. ubidi_close(p_shaped->bidi_iter[i]);
  3084. }
  3085. p_shaped->bidi_iter.clear();
  3086. if (p_text) {
  3087. if (p_shaped->script_iter != nullptr) {
  3088. memdelete(p_shaped->script_iter);
  3089. p_shaped->script_iter = nullptr;
  3090. }
  3091. p_shaped->break_ops_valid = false;
  3092. p_shaped->js_ops_valid = false;
  3093. }
  3094. }
  3095. void TextServerAdvanced::full_copy(ShapedTextDataAdvanced *p_shaped) {
  3096. ShapedTextDataAdvanced *parent = shaped_owner.get_or_null(p_shaped->parent);
  3097. for (const KeyValue<Variant, ShapedTextDataAdvanced::EmbeddedObject> &E : parent->objects) {
  3098. if (E.value.pos >= p_shaped->start && E.value.pos < p_shaped->end) {
  3099. p_shaped->objects[E.key] = E.value;
  3100. }
  3101. }
  3102. for (int i = 0; i < parent->spans.size(); i++) {
  3103. ShapedTextDataAdvanced::Span span = parent->spans[i];
  3104. if (span.start >= p_shaped->end || span.end <= p_shaped->start) {
  3105. continue;
  3106. }
  3107. span.start = MAX(p_shaped->start, span.start);
  3108. span.end = MIN(p_shaped->end, span.end);
  3109. p_shaped->spans.push_back(span);
  3110. }
  3111. p_shaped->parent = RID();
  3112. }
  3113. RID TextServerAdvanced::_create_shaped_text(TextServer::Direction p_direction, TextServer::Orientation p_orientation) {
  3114. _THREAD_SAFE_METHOD_
  3115. ShapedTextDataAdvanced *sd = memnew(ShapedTextDataAdvanced);
  3116. sd->hb_buffer = hb_buffer_create();
  3117. sd->direction = p_direction;
  3118. sd->orientation = p_orientation;
  3119. return shaped_owner.make_rid(sd);
  3120. }
  3121. void TextServerAdvanced::_shaped_text_clear(const RID &p_shaped) {
  3122. ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
  3123. ERR_FAIL_COND(!sd);
  3124. MutexLock lock(sd->mutex);
  3125. sd->parent = RID();
  3126. sd->start = 0;
  3127. sd->end = 0;
  3128. sd->text = String();
  3129. sd->spans.clear();
  3130. sd->objects.clear();
  3131. sd->bidi_override.clear();
  3132. invalidate(sd, true);
  3133. }
  3134. void TextServerAdvanced::_shaped_text_set_direction(const RID &p_shaped, TextServer::Direction p_direction) {
  3135. ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
  3136. ERR_FAIL_COND(!sd);
  3137. MutexLock lock(sd->mutex);
  3138. if (sd->direction != p_direction) {
  3139. if (sd->parent != RID()) {
  3140. full_copy(sd);
  3141. }
  3142. sd->direction = p_direction;
  3143. invalidate(sd, false);
  3144. }
  3145. }
  3146. TextServer::Direction TextServerAdvanced::_shaped_text_get_direction(const RID &p_shaped) const {
  3147. const ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
  3148. ERR_FAIL_COND_V(!sd, TextServer::DIRECTION_LTR);
  3149. MutexLock lock(sd->mutex);
  3150. return sd->direction;
  3151. }
  3152. TextServer::Direction TextServerAdvanced::_shaped_text_get_inferred_direction(const RID &p_shaped) const {
  3153. const ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
  3154. ERR_FAIL_COND_V(!sd, TextServer::DIRECTION_LTR);
  3155. MutexLock lock(sd->mutex);
  3156. return sd->para_direction;
  3157. }
  3158. void TextServerAdvanced::_shaped_text_set_custom_punctuation(const RID &p_shaped, const String &p_punct) {
  3159. _THREAD_SAFE_METHOD_
  3160. ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
  3161. ERR_FAIL_COND(!sd);
  3162. if (sd->custom_punct != p_punct) {
  3163. if (sd->parent != RID()) {
  3164. full_copy(sd);
  3165. }
  3166. sd->custom_punct = p_punct;
  3167. invalidate(sd, false);
  3168. }
  3169. }
  3170. String TextServerAdvanced::_shaped_text_get_custom_punctuation(const RID &p_shaped) const {
  3171. _THREAD_SAFE_METHOD_
  3172. const ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
  3173. ERR_FAIL_COND_V(!sd, String());
  3174. return sd->custom_punct;
  3175. }
  3176. void TextServerAdvanced::_shaped_text_set_bidi_override(const RID &p_shaped, const Array &p_override) {
  3177. ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
  3178. ERR_FAIL_COND(!sd);
  3179. MutexLock lock(sd->mutex);
  3180. if (sd->parent != RID()) {
  3181. full_copy(sd);
  3182. }
  3183. sd->bidi_override.clear();
  3184. for (int i = 0; i < p_override.size(); i++) {
  3185. if (p_override[i].get_type() == Variant::VECTOR2I) {
  3186. sd->bidi_override.push_back(p_override[i]);
  3187. }
  3188. }
  3189. invalidate(sd, false);
  3190. }
  3191. void TextServerAdvanced::_shaped_text_set_orientation(const RID &p_shaped, TextServer::Orientation p_orientation) {
  3192. ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
  3193. ERR_FAIL_COND(!sd);
  3194. MutexLock lock(sd->mutex);
  3195. if (sd->orientation != p_orientation) {
  3196. if (sd->parent != RID()) {
  3197. full_copy(sd);
  3198. }
  3199. sd->orientation = p_orientation;
  3200. invalidate(sd, false);
  3201. }
  3202. }
  3203. void TextServerAdvanced::_shaped_text_set_preserve_invalid(const RID &p_shaped, bool p_enabled) {
  3204. ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
  3205. ERR_FAIL_COND(!sd);
  3206. MutexLock lock(sd->mutex);
  3207. ERR_FAIL_COND(sd->parent != RID());
  3208. if (sd->preserve_invalid != p_enabled) {
  3209. sd->preserve_invalid = p_enabled;
  3210. invalidate(sd, false);
  3211. }
  3212. }
  3213. bool TextServerAdvanced::_shaped_text_get_preserve_invalid(const RID &p_shaped) const {
  3214. const ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
  3215. ERR_FAIL_COND_V(!sd, false);
  3216. MutexLock lock(sd->mutex);
  3217. return sd->preserve_invalid;
  3218. }
  3219. void TextServerAdvanced::_shaped_text_set_preserve_control(const RID &p_shaped, bool p_enabled) {
  3220. ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
  3221. ERR_FAIL_COND(!sd);
  3222. MutexLock lock(sd->mutex);
  3223. if (sd->preserve_control != p_enabled) {
  3224. if (sd->parent != RID()) {
  3225. full_copy(sd);
  3226. }
  3227. sd->preserve_control = p_enabled;
  3228. invalidate(sd, false);
  3229. }
  3230. }
  3231. bool TextServerAdvanced::_shaped_text_get_preserve_control(const RID &p_shaped) const {
  3232. const ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
  3233. ERR_FAIL_COND_V(!sd, false);
  3234. MutexLock lock(sd->mutex);
  3235. return sd->preserve_control;
  3236. }
  3237. void TextServerAdvanced::_shaped_text_set_spacing(const RID &p_shaped, SpacingType p_spacing, int64_t p_value) {
  3238. ERR_FAIL_INDEX((int)p_spacing, 4);
  3239. ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
  3240. ERR_FAIL_COND(!sd);
  3241. MutexLock lock(sd->mutex);
  3242. if (sd->extra_spacing[p_spacing] != p_value) {
  3243. if (sd->parent != RID()) {
  3244. full_copy(sd);
  3245. }
  3246. sd->extra_spacing[p_spacing] = p_value;
  3247. invalidate(sd, false);
  3248. }
  3249. }
  3250. int64_t TextServerAdvanced::_shaped_text_get_spacing(const RID &p_shaped, SpacingType p_spacing) const {
  3251. ERR_FAIL_INDEX_V((int)p_spacing, 4, 0);
  3252. const ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
  3253. ERR_FAIL_COND_V(!sd, 0);
  3254. MutexLock lock(sd->mutex);
  3255. return sd->extra_spacing[p_spacing];
  3256. }
  3257. TextServer::Orientation TextServerAdvanced::_shaped_text_get_orientation(const RID &p_shaped) const {
  3258. const ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
  3259. ERR_FAIL_COND_V(!sd, TextServer::ORIENTATION_HORIZONTAL);
  3260. MutexLock lock(sd->mutex);
  3261. return sd->orientation;
  3262. }
  3263. int64_t TextServerAdvanced::_shaped_get_span_count(const RID &p_shaped) const {
  3264. ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
  3265. ERR_FAIL_COND_V(!sd, 0);
  3266. return sd->spans.size();
  3267. }
  3268. Variant TextServerAdvanced::_shaped_get_span_meta(const RID &p_shaped, int64_t p_index) const {
  3269. ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
  3270. ERR_FAIL_COND_V(!sd, Variant());
  3271. ERR_FAIL_INDEX_V(p_index, sd->spans.size(), Variant());
  3272. return sd->spans[p_index].meta;
  3273. }
  3274. void TextServerAdvanced::_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) {
  3275. ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
  3276. ERR_FAIL_COND(!sd);
  3277. ERR_FAIL_INDEX(p_index, sd->spans.size());
  3278. ShapedTextDataAdvanced::Span &span = sd->spans.ptrw()[p_index];
  3279. span.fonts = p_fonts;
  3280. span.font_size = p_size;
  3281. span.features = p_opentype_features;
  3282. invalidate(sd, false);
  3283. }
  3284. bool TextServerAdvanced::_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) {
  3285. ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
  3286. ERR_FAIL_COND_V(!sd, false);
  3287. ERR_FAIL_COND_V(p_size <= 0, false);
  3288. MutexLock lock(sd->mutex);
  3289. for (int i = 0; i < p_fonts.size(); i++) {
  3290. ERR_FAIL_COND_V(!font_owner.get_or_null(p_fonts[i]), false);
  3291. }
  3292. if (p_text.is_empty()) {
  3293. return true;
  3294. }
  3295. if (sd->parent != RID()) {
  3296. full_copy(sd);
  3297. }
  3298. ShapedTextDataAdvanced::Span span;
  3299. span.start = sd->text.length();
  3300. span.end = span.start + p_text.length();
  3301. span.fonts = p_fonts; // Do not pre-sort, spans will be divided to subruns later.
  3302. span.font_size = p_size;
  3303. span.language = p_language;
  3304. span.features = p_opentype_features;
  3305. span.meta = p_meta;
  3306. sd->spans.push_back(span);
  3307. sd->text = sd->text + p_text;
  3308. sd->end += p_text.length();
  3309. invalidate(sd, true);
  3310. return true;
  3311. }
  3312. bool TextServerAdvanced::_shaped_text_add_object(const RID &p_shaped, const Variant &p_key, const Size2 &p_size, InlineAlignment p_inline_align, int64_t p_length) {
  3313. _THREAD_SAFE_METHOD_
  3314. ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
  3315. ERR_FAIL_COND_V(!sd, false);
  3316. ERR_FAIL_COND_V(p_key == Variant(), false);
  3317. ERR_FAIL_COND_V(sd->objects.has(p_key), false);
  3318. if (sd->parent != RID()) {
  3319. full_copy(sd);
  3320. }
  3321. ShapedTextDataAdvanced::Span span;
  3322. span.start = sd->start + sd->text.length();
  3323. span.end = span.start + p_length;
  3324. span.embedded_key = p_key;
  3325. ShapedTextDataAdvanced::EmbeddedObject obj;
  3326. obj.inline_align = p_inline_align;
  3327. obj.rect.size = p_size;
  3328. obj.pos = span.start;
  3329. sd->spans.push_back(span);
  3330. sd->text = sd->text + String::chr(0xfffc).repeat(p_length);
  3331. sd->end += p_length;
  3332. sd->objects[p_key] = obj;
  3333. invalidate(sd, true);
  3334. return true;
  3335. }
  3336. bool TextServerAdvanced::_shaped_text_resize_object(const RID &p_shaped, const Variant &p_key, const Size2 &p_size, InlineAlignment p_inline_align) {
  3337. ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
  3338. ERR_FAIL_COND_V(!sd, false);
  3339. MutexLock lock(sd->mutex);
  3340. ERR_FAIL_COND_V(!sd->objects.has(p_key), false);
  3341. sd->objects[p_key].rect.size = p_size;
  3342. sd->objects[p_key].inline_align = p_inline_align;
  3343. if (sd->valid) {
  3344. // Recalc string metrics.
  3345. sd->ascent = 0;
  3346. sd->descent = 0;
  3347. sd->width = 0;
  3348. sd->upos = 0;
  3349. sd->uthk = 0;
  3350. int sd_size = sd->glyphs.size();
  3351. for (int i = 0; i < sd_size; i++) {
  3352. Glyph gl = sd->glyphs[i];
  3353. Variant key;
  3354. if (gl.count == 1) {
  3355. for (const KeyValue<Variant, ShapedTextDataAdvanced::EmbeddedObject> &E : sd->objects) {
  3356. if (E.value.pos == gl.start) {
  3357. key = E.key;
  3358. break;
  3359. }
  3360. }
  3361. }
  3362. if (key != Variant()) {
  3363. if (sd->orientation == ORIENTATION_HORIZONTAL) {
  3364. sd->objects[key].rect.position.x = sd->width;
  3365. sd->width += sd->objects[key].rect.size.x;
  3366. sd->glyphs.write[i].advance = sd->objects[key].rect.size.x;
  3367. } else {
  3368. sd->objects[key].rect.position.y = sd->width;
  3369. sd->width += sd->objects[key].rect.size.y;
  3370. sd->glyphs.write[i].advance = sd->objects[key].rect.size.y;
  3371. }
  3372. } else {
  3373. if (gl.font_rid.is_valid()) {
  3374. if (sd->orientation == ORIENTATION_HORIZONTAL) {
  3375. sd->ascent = MAX(sd->ascent, MAX(_font_get_ascent(gl.font_rid, gl.font_size), -gl.y_off));
  3376. sd->descent = MAX(sd->descent, MAX(_font_get_descent(gl.font_rid, gl.font_size), gl.y_off));
  3377. } else {
  3378. sd->ascent = MAX(sd->ascent, Math::round(_font_get_glyph_advance(gl.font_rid, gl.font_size, gl.index).x * 0.5));
  3379. sd->descent = MAX(sd->descent, Math::round(_font_get_glyph_advance(gl.font_rid, gl.font_size, gl.index).x * 0.5));
  3380. }
  3381. sd->upos = MAX(sd->upos, _font_get_underline_position(gl.font_rid, gl.font_size));
  3382. sd->uthk = MAX(sd->uthk, _font_get_underline_thickness(gl.font_rid, gl.font_size));
  3383. } else if (sd->preserve_invalid || (sd->preserve_control && is_control(gl.index))) {
  3384. // Glyph not found, replace with hex code box.
  3385. if (sd->orientation == ORIENTATION_HORIZONTAL) {
  3386. sd->ascent = MAX(sd->ascent, get_hex_code_box_size(gl.font_size, gl.index).y);
  3387. } else {
  3388. sd->ascent = MAX(sd->ascent, Math::round(get_hex_code_box_size(gl.font_size, gl.index).x * 0.5));
  3389. sd->descent = MAX(sd->descent, Math::round(get_hex_code_box_size(gl.font_size, gl.index).x * 0.5));
  3390. }
  3391. }
  3392. sd->width += gl.advance * gl.repeat;
  3393. }
  3394. }
  3395. _realign(sd);
  3396. }
  3397. return true;
  3398. }
  3399. void TextServerAdvanced::_realign(ShapedTextDataAdvanced *p_sd) const {
  3400. // Align embedded objects to baseline.
  3401. double full_ascent = p_sd->ascent;
  3402. double full_descent = p_sd->descent;
  3403. for (KeyValue<Variant, ShapedTextDataAdvanced::EmbeddedObject> &E : p_sd->objects) {
  3404. if ((E.value.pos >= p_sd->start) && (E.value.pos < p_sd->end)) {
  3405. if (p_sd->orientation == ORIENTATION_HORIZONTAL) {
  3406. switch (E.value.inline_align & INLINE_ALIGNMENT_TEXT_MASK) {
  3407. case INLINE_ALIGNMENT_TO_TOP: {
  3408. E.value.rect.position.y = -p_sd->ascent;
  3409. } break;
  3410. case INLINE_ALIGNMENT_TO_CENTER: {
  3411. E.value.rect.position.y = (-p_sd->ascent + p_sd->descent) / 2;
  3412. } break;
  3413. case INLINE_ALIGNMENT_TO_BASELINE: {
  3414. E.value.rect.position.y = 0;
  3415. } break;
  3416. case INLINE_ALIGNMENT_TO_BOTTOM: {
  3417. E.value.rect.position.y = p_sd->descent;
  3418. } break;
  3419. }
  3420. switch (E.value.inline_align & INLINE_ALIGNMENT_IMAGE_MASK) {
  3421. case INLINE_ALIGNMENT_BOTTOM_TO: {
  3422. E.value.rect.position.y -= E.value.rect.size.y;
  3423. } break;
  3424. case INLINE_ALIGNMENT_CENTER_TO: {
  3425. E.value.rect.position.y -= E.value.rect.size.y / 2;
  3426. } break;
  3427. case INLINE_ALIGNMENT_TOP_TO: {
  3428. // NOP
  3429. } break;
  3430. }
  3431. full_ascent = MAX(full_ascent, -E.value.rect.position.y);
  3432. full_descent = MAX(full_descent, E.value.rect.position.y + E.value.rect.size.y);
  3433. } else {
  3434. switch (E.value.inline_align & INLINE_ALIGNMENT_TEXT_MASK) {
  3435. case INLINE_ALIGNMENT_TO_TOP: {
  3436. E.value.rect.position.x = -p_sd->ascent;
  3437. } break;
  3438. case INLINE_ALIGNMENT_TO_CENTER: {
  3439. E.value.rect.position.x = (-p_sd->ascent + p_sd->descent) / 2;
  3440. } break;
  3441. case INLINE_ALIGNMENT_TO_BASELINE: {
  3442. E.value.rect.position.x = 0;
  3443. } break;
  3444. case INLINE_ALIGNMENT_TO_BOTTOM: {
  3445. E.value.rect.position.x = p_sd->descent;
  3446. } break;
  3447. }
  3448. switch (E.value.inline_align & INLINE_ALIGNMENT_IMAGE_MASK) {
  3449. case INLINE_ALIGNMENT_BOTTOM_TO: {
  3450. E.value.rect.position.x -= E.value.rect.size.x;
  3451. } break;
  3452. case INLINE_ALIGNMENT_CENTER_TO: {
  3453. E.value.rect.position.x -= E.value.rect.size.x / 2;
  3454. } break;
  3455. case INLINE_ALIGNMENT_TOP_TO: {
  3456. // NOP
  3457. } break;
  3458. }
  3459. full_ascent = MAX(full_ascent, -E.value.rect.position.x);
  3460. full_descent = MAX(full_descent, E.value.rect.position.x + E.value.rect.size.x);
  3461. }
  3462. }
  3463. }
  3464. p_sd->ascent = full_ascent;
  3465. p_sd->descent = full_descent;
  3466. }
  3467. RID TextServerAdvanced::_shaped_text_substr(const RID &p_shaped, int64_t p_start, int64_t p_length) const {
  3468. _THREAD_SAFE_METHOD_
  3469. const ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
  3470. ERR_FAIL_COND_V(!sd, RID());
  3471. MutexLock lock(sd->mutex);
  3472. if (sd->parent != RID()) {
  3473. return _shaped_text_substr(sd->parent, p_start, p_length);
  3474. }
  3475. if (!sd->valid) {
  3476. const_cast<TextServerAdvanced *>(this)->_shaped_text_shape(p_shaped);
  3477. }
  3478. ERR_FAIL_COND_V(p_start < 0 || p_length < 0, RID());
  3479. ERR_FAIL_COND_V(sd->start > p_start || sd->end < p_start, RID());
  3480. ERR_FAIL_COND_V(sd->end < p_start + p_length, RID());
  3481. ShapedTextDataAdvanced *new_sd = memnew(ShapedTextDataAdvanced);
  3482. new_sd->parent = p_shaped;
  3483. new_sd->start = p_start;
  3484. new_sd->end = p_start + p_length;
  3485. new_sd->orientation = sd->orientation;
  3486. new_sd->direction = sd->direction;
  3487. new_sd->custom_punct = sd->custom_punct;
  3488. new_sd->para_direction = sd->para_direction;
  3489. for (int i = 0; i < TextServer::SPACING_MAX; i++) {
  3490. new_sd->extra_spacing[i] = sd->extra_spacing[i];
  3491. }
  3492. if (!_shape_substr(new_sd, sd, p_start, p_length)) {
  3493. memdelete(new_sd);
  3494. return RID();
  3495. }
  3496. return shaped_owner.make_rid(new_sd);
  3497. }
  3498. bool TextServerAdvanced::_shape_substr(ShapedTextDataAdvanced *p_new_sd, const ShapedTextDataAdvanced *p_sd, int64_t p_start, int64_t p_length) const {
  3499. if (p_new_sd->valid) {
  3500. return true;
  3501. }
  3502. p_new_sd->hb_buffer = hb_buffer_create();
  3503. p_new_sd->line_breaks_valid = p_sd->line_breaks_valid;
  3504. p_new_sd->justification_ops_valid = p_sd->justification_ops_valid;
  3505. p_new_sd->sort_valid = false;
  3506. p_new_sd->upos = p_sd->upos;
  3507. p_new_sd->uthk = p_sd->uthk;
  3508. if (p_length > 0) {
  3509. p_new_sd->text = p_sd->text.substr(p_start - p_sd->start, p_length);
  3510. p_new_sd->utf16 = p_new_sd->text.utf16();
  3511. p_new_sd->script_iter = memnew(ScriptIterator(p_new_sd->text, 0, p_new_sd->text.length()));
  3512. int sd_size = p_sd->glyphs.size();
  3513. const Glyph *sd_glyphs = p_sd->glyphs.ptr();
  3514. for (int ov = 0; ov < p_sd->bidi_override.size(); ov++) {
  3515. UErrorCode err = U_ZERO_ERROR;
  3516. if (p_sd->bidi_override[ov].x >= p_start + p_length || p_sd->bidi_override[ov].y <= p_start) {
  3517. continue;
  3518. }
  3519. int start = _convert_pos_inv(p_sd, MAX(0, p_start - p_sd->bidi_override[ov].x));
  3520. int end = _convert_pos_inv(p_sd, MIN(p_start + p_length, p_sd->bidi_override[ov].y) - p_sd->bidi_override[ov].x);
  3521. ERR_FAIL_COND_V_MSG((start < 0 || end - start > p_new_sd->utf16.length()), false, "Invalid BiDi override range.");
  3522. // Create temporary line bidi & shape.
  3523. UBiDi *bidi_iter = ubidi_openSized(end - start, 0, &err);
  3524. ERR_FAIL_COND_V_MSG(U_FAILURE(err), false, u_errorName(err));
  3525. ubidi_setLine(p_sd->bidi_iter[ov], start, end, bidi_iter, &err);
  3526. if (U_FAILURE(err)) {
  3527. ubidi_close(bidi_iter);
  3528. ERR_FAIL_V_MSG(false, u_errorName(err));
  3529. }
  3530. p_new_sd->bidi_iter.push_back(bidi_iter);
  3531. err = U_ZERO_ERROR;
  3532. int bidi_run_count = ubidi_countRuns(bidi_iter, &err);
  3533. ERR_FAIL_COND_V_MSG(U_FAILURE(err), false, u_errorName(err));
  3534. for (int i = 0; i < bidi_run_count; i++) {
  3535. int32_t _bidi_run_start = 0;
  3536. int32_t _bidi_run_length = 0;
  3537. ubidi_getVisualRun(bidi_iter, i, &_bidi_run_start, &_bidi_run_length);
  3538. int32_t bidi_run_start = _convert_pos(p_sd, p_sd->bidi_override[ov].x + start + _bidi_run_start);
  3539. int32_t bidi_run_end = _convert_pos(p_sd, p_sd->bidi_override[ov].x + start + _bidi_run_start + _bidi_run_length);
  3540. for (int j = 0; j < sd_size; j++) {
  3541. if ((sd_glyphs[j].start >= bidi_run_start) && (sd_glyphs[j].end <= bidi_run_end)) {
  3542. // Copy glyphs.
  3543. Glyph gl = sd_glyphs[j];
  3544. Variant key;
  3545. bool find_embedded = false;
  3546. if (gl.count == 1) {
  3547. for (const KeyValue<Variant, ShapedTextDataAdvanced::EmbeddedObject> &E : p_sd->objects) {
  3548. if (E.value.pos == gl.start) {
  3549. find_embedded = true;
  3550. key = E.key;
  3551. p_new_sd->objects[key] = E.value;
  3552. break;
  3553. }
  3554. }
  3555. }
  3556. if (find_embedded) {
  3557. if (p_new_sd->orientation == ORIENTATION_HORIZONTAL) {
  3558. p_new_sd->objects[key].rect.position.x = p_new_sd->width;
  3559. p_new_sd->width += p_new_sd->objects[key].rect.size.x;
  3560. } else {
  3561. p_new_sd->objects[key].rect.position.y = p_new_sd->width;
  3562. p_new_sd->width += p_new_sd->objects[key].rect.size.y;
  3563. }
  3564. } else {
  3565. if (gl.font_rid.is_valid()) {
  3566. if (p_new_sd->orientation == ORIENTATION_HORIZONTAL) {
  3567. p_new_sd->ascent = MAX(p_new_sd->ascent, MAX(_font_get_ascent(gl.font_rid, gl.font_size), -gl.y_off));
  3568. p_new_sd->descent = MAX(p_new_sd->descent, MAX(_font_get_descent(gl.font_rid, gl.font_size), gl.y_off));
  3569. } else {
  3570. p_new_sd->ascent = MAX(p_new_sd->ascent, Math::round(_font_get_glyph_advance(gl.font_rid, gl.font_size, gl.index).x * 0.5));
  3571. p_new_sd->descent = MAX(p_new_sd->descent, Math::round(_font_get_glyph_advance(gl.font_rid, gl.font_size, gl.index).x * 0.5));
  3572. }
  3573. } else if (p_new_sd->preserve_invalid || (p_new_sd->preserve_control && is_control(gl.index))) {
  3574. // Glyph not found, replace with hex code box.
  3575. if (p_new_sd->orientation == ORIENTATION_HORIZONTAL) {
  3576. p_new_sd->ascent = MAX(p_new_sd->ascent, get_hex_code_box_size(gl.font_size, gl.index).y);
  3577. } else {
  3578. p_new_sd->ascent = MAX(p_new_sd->ascent, Math::round(get_hex_code_box_size(gl.font_size, gl.index).x * 0.5));
  3579. p_new_sd->descent = MAX(p_new_sd->descent, Math::round(get_hex_code_box_size(gl.font_size, gl.index).x * 0.5));
  3580. }
  3581. }
  3582. p_new_sd->width += gl.advance * gl.repeat;
  3583. }
  3584. p_new_sd->glyphs.push_back(gl);
  3585. }
  3586. }
  3587. }
  3588. }
  3589. _realign(p_new_sd);
  3590. }
  3591. p_new_sd->valid = true;
  3592. return true;
  3593. }
  3594. RID TextServerAdvanced::_shaped_text_get_parent(const RID &p_shaped) const {
  3595. ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
  3596. ERR_FAIL_COND_V(!sd, RID());
  3597. MutexLock lock(sd->mutex);
  3598. return sd->parent;
  3599. }
  3600. double TextServerAdvanced::_shaped_text_fit_to_width(const RID &p_shaped, double p_width, BitField<TextServer::JustificationFlag> p_jst_flags) {
  3601. ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
  3602. ERR_FAIL_COND_V(!sd, 0.0);
  3603. MutexLock lock(sd->mutex);
  3604. if (!sd->valid) {
  3605. const_cast<TextServerAdvanced *>(this)->_shaped_text_shape(p_shaped);
  3606. }
  3607. if (!sd->justification_ops_valid) {
  3608. const_cast<TextServerAdvanced *>(this)->_shaped_text_update_justification_ops(p_shaped);
  3609. }
  3610. sd->fit_width_minimum_reached = false;
  3611. int start_pos = 0;
  3612. int end_pos = sd->glyphs.size() - 1;
  3613. if (p_jst_flags.has_flag(JUSTIFICATION_AFTER_LAST_TAB)) {
  3614. int start, end, delta;
  3615. if (sd->para_direction == DIRECTION_LTR) {
  3616. start = sd->glyphs.size() - 1;
  3617. end = -1;
  3618. delta = -1;
  3619. } else {
  3620. start = 0;
  3621. end = sd->glyphs.size();
  3622. delta = +1;
  3623. }
  3624. for (int i = start; i != end; i += delta) {
  3625. if ((sd->glyphs[i].flags & GRAPHEME_IS_TAB) == GRAPHEME_IS_TAB) {
  3626. if (sd->para_direction == DIRECTION_LTR) {
  3627. start_pos = i;
  3628. break;
  3629. } else {
  3630. end_pos = i;
  3631. break;
  3632. }
  3633. }
  3634. }
  3635. }
  3636. double justification_width;
  3637. if (p_jst_flags.has_flag(JUSTIFICATION_CONSTRAIN_ELLIPSIS)) {
  3638. if (sd->overrun_trim_data.trim_pos >= 0) {
  3639. if (sd->para_direction == DIRECTION_RTL) {
  3640. start_pos = sd->overrun_trim_data.trim_pos;
  3641. } else {
  3642. end_pos = sd->overrun_trim_data.trim_pos;
  3643. }
  3644. justification_width = sd->width_trimmed;
  3645. } else {
  3646. return Math::ceil(sd->width);
  3647. }
  3648. } else {
  3649. justification_width = sd->width;
  3650. }
  3651. if (p_jst_flags.has_flag(JUSTIFICATION_TRIM_EDGE_SPACES)) {
  3652. // Trim spaces.
  3653. while ((start_pos < end_pos) && ((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)) {
  3654. justification_width -= sd->glyphs[start_pos].advance * sd->glyphs[start_pos].repeat;
  3655. sd->glyphs.write[start_pos].advance = 0;
  3656. start_pos += sd->glyphs[start_pos].count;
  3657. }
  3658. while ((start_pos < end_pos) && ((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)) {
  3659. justification_width -= sd->glyphs[end_pos].advance * sd->glyphs[end_pos].repeat;
  3660. sd->glyphs.write[end_pos].advance = 0;
  3661. end_pos -= sd->glyphs[end_pos].count;
  3662. }
  3663. } else {
  3664. // Skip breaks, but do not reset size.
  3665. while ((start_pos < end_pos) && ((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)) {
  3666. start_pos += sd->glyphs[start_pos].count;
  3667. }
  3668. while ((start_pos < end_pos) && ((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)) {
  3669. end_pos -= sd->glyphs[end_pos].count;
  3670. }
  3671. }
  3672. int space_count = 0;
  3673. int elongation_count = 0;
  3674. for (int i = start_pos; i <= end_pos; i++) {
  3675. const Glyph &gl = sd->glyphs[i];
  3676. if (gl.count > 0) {
  3677. if ((gl.flags & GRAPHEME_IS_ELONGATION) == GRAPHEME_IS_ELONGATION) {
  3678. if ((i > 0) && ((sd->glyphs[i - 1].flags & GRAPHEME_IS_ELONGATION) != GRAPHEME_IS_ELONGATION)) {
  3679. // Expand once per elongation sequence.
  3680. elongation_count++;
  3681. }
  3682. }
  3683. if ((gl.flags & GRAPHEME_IS_SPACE) == GRAPHEME_IS_SPACE) {
  3684. space_count++;
  3685. }
  3686. }
  3687. }
  3688. if ((elongation_count > 0) && p_jst_flags.has_flag(JUSTIFICATION_KASHIDA)) {
  3689. double delta_width_per_kashida = (p_width - justification_width) / elongation_count;
  3690. for (int i = start_pos; i <= end_pos; i++) {
  3691. Glyph &gl = sd->glyphs.write[i];
  3692. if (gl.count > 0) {
  3693. if (((gl.flags & GRAPHEME_IS_ELONGATION) == GRAPHEME_IS_ELONGATION) && (gl.advance > 0)) {
  3694. if ((i > 0) && ((sd->glyphs[i - 1].flags & GRAPHEME_IS_ELONGATION) != GRAPHEME_IS_ELONGATION)) {
  3695. // Expand once per elongation sequence.
  3696. int count = delta_width_per_kashida / gl.advance;
  3697. int prev_count = gl.repeat;
  3698. if ((gl.flags & GRAPHEME_IS_VIRTUAL) == GRAPHEME_IS_VIRTUAL) {
  3699. gl.repeat = MAX(count, 0);
  3700. } else {
  3701. gl.repeat = MAX(count + 1, 1);
  3702. }
  3703. justification_width += (gl.repeat - prev_count) * gl.advance;
  3704. }
  3705. }
  3706. }
  3707. }
  3708. }
  3709. if ((space_count > 0) && p_jst_flags.has_flag(JUSTIFICATION_WORD_BOUND)) {
  3710. double delta_width_per_space = (p_width - justification_width) / space_count;
  3711. double adv_remain = 0;
  3712. for (int i = start_pos; i <= end_pos; i++) {
  3713. Glyph &gl = sd->glyphs.write[i];
  3714. if (gl.count > 0) {
  3715. if ((gl.flags & GRAPHEME_IS_SPACE) == GRAPHEME_IS_SPACE) {
  3716. double old_adv = gl.advance;
  3717. double new_advance;
  3718. if ((gl.flags & GRAPHEME_IS_VIRTUAL) == GRAPHEME_IS_VIRTUAL) {
  3719. new_advance = MAX(gl.advance + delta_width_per_space, 0.0);
  3720. } else {
  3721. new_advance = MAX(gl.advance + delta_width_per_space, 0.1 * gl.font_size);
  3722. }
  3723. gl.advance = new_advance;
  3724. adv_remain += (new_advance - gl.advance);
  3725. if (adv_remain >= 1.0) {
  3726. gl.advance++;
  3727. adv_remain -= 1.0;
  3728. } else if (adv_remain <= -1.0) {
  3729. gl.advance = MAX(gl.advance - 1, 0);
  3730. adv_remain -= 1.0;
  3731. }
  3732. justification_width += (gl.advance - old_adv);
  3733. }
  3734. }
  3735. }
  3736. }
  3737. if (Math::floor(p_width) < Math::floor(justification_width)) {
  3738. sd->fit_width_minimum_reached = true;
  3739. }
  3740. if (!p_jst_flags.has_flag(JUSTIFICATION_CONSTRAIN_ELLIPSIS)) {
  3741. sd->width = justification_width;
  3742. }
  3743. return Math::ceil(justification_width);
  3744. }
  3745. double TextServerAdvanced::_shaped_text_tab_align(const RID &p_shaped, const PackedFloat32Array &p_tab_stops) {
  3746. ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
  3747. ERR_FAIL_COND_V(!sd, 0.0);
  3748. MutexLock lock(sd->mutex);
  3749. if (!sd->valid) {
  3750. const_cast<TextServerAdvanced *>(this)->_shaped_text_shape(p_shaped);
  3751. }
  3752. if (!sd->line_breaks_valid) {
  3753. const_cast<TextServerAdvanced *>(this)->_shaped_text_update_breaks(p_shaped);
  3754. }
  3755. for (int i = 0; i < p_tab_stops.size(); i++) {
  3756. if (p_tab_stops[i] <= 0) {
  3757. return 0.0;
  3758. }
  3759. }
  3760. int tab_index = 0;
  3761. double off = 0.0;
  3762. int start, end, delta;
  3763. if (sd->para_direction == DIRECTION_LTR) {
  3764. start = 0;
  3765. end = sd->glyphs.size();
  3766. delta = +1;
  3767. } else {
  3768. start = sd->glyphs.size() - 1;
  3769. end = -1;
  3770. delta = -1;
  3771. }
  3772. Glyph *gl = sd->glyphs.ptrw();
  3773. for (int i = start; i != end; i += delta) {
  3774. if ((gl[i].flags & GRAPHEME_IS_TAB) == GRAPHEME_IS_TAB) {
  3775. double tab_off = 0.0;
  3776. while (tab_off <= off) {
  3777. tab_off += p_tab_stops[tab_index];
  3778. tab_index++;
  3779. if (tab_index >= p_tab_stops.size()) {
  3780. tab_index = 0;
  3781. }
  3782. }
  3783. double old_adv = gl[i].advance;
  3784. gl[i].advance = tab_off - off;
  3785. sd->width += gl[i].advance - old_adv;
  3786. off = 0;
  3787. continue;
  3788. }
  3789. off += gl[i].advance * gl[i].repeat;
  3790. }
  3791. return 0.0;
  3792. }
  3793. void TextServerAdvanced::_shaped_text_overrun_trim_to_width(const RID &p_shaped_line, double p_width, BitField<TextServer::TextOverrunFlag> p_trim_flags) {
  3794. ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped_line);
  3795. ERR_FAIL_COND_MSG(!sd, "ShapedTextDataAdvanced invalid.");
  3796. MutexLock lock(sd->mutex);
  3797. if (!sd->valid) {
  3798. _shaped_text_shape(p_shaped_line);
  3799. }
  3800. sd->text_trimmed = false;
  3801. sd->overrun_trim_data.ellipsis_glyph_buf.clear();
  3802. bool add_ellipsis = p_trim_flags.has_flag(OVERRUN_ADD_ELLIPSIS);
  3803. bool cut_per_word = p_trim_flags.has_flag(OVERRUN_TRIM_WORD_ONLY);
  3804. bool enforce_ellipsis = p_trim_flags.has_flag(OVERRUN_ENFORCE_ELLIPSIS);
  3805. bool justification_aware = p_trim_flags.has_flag(OVERRUN_JUSTIFICATION_AWARE);
  3806. Glyph *sd_glyphs = sd->glyphs.ptrw();
  3807. if ((p_trim_flags & OVERRUN_TRIM) == OVERRUN_NO_TRIM || sd_glyphs == nullptr || p_width <= 0 || !(sd->width > p_width || enforce_ellipsis)) {
  3808. sd->overrun_trim_data.trim_pos = -1;
  3809. sd->overrun_trim_data.ellipsis_pos = -1;
  3810. return;
  3811. }
  3812. if (justification_aware && !sd->fit_width_minimum_reached) {
  3813. return;
  3814. }
  3815. Vector<ShapedTextDataAdvanced::Span> &spans = sd->spans;
  3816. if (sd->parent != RID()) {
  3817. ShapedTextDataAdvanced *parent_sd = shaped_owner.get_or_null(sd->parent);
  3818. ERR_FAIL_COND(!parent_sd->valid);
  3819. spans = parent_sd->spans;
  3820. }
  3821. if (spans.size() == 0) {
  3822. return;
  3823. }
  3824. int sd_size = sd->glyphs.size();
  3825. int last_gl_font_size = sd_glyphs[sd_size - 1].font_size;
  3826. // Find usable fonts, if fonts from the last glyph do not have required chars.
  3827. RID dot_gl_font_rid = sd_glyphs[sd_size - 1].font_rid;
  3828. if (!_font_has_char(dot_gl_font_rid, '.')) {
  3829. const Array &fonts = spans[spans.size() - 1].fonts;
  3830. for (int i = 0; i < fonts.size(); i++) {
  3831. if (_font_has_char(fonts[i], '.')) {
  3832. dot_gl_font_rid = fonts[i];
  3833. break;
  3834. }
  3835. }
  3836. }
  3837. RID whitespace_gl_font_rid = sd_glyphs[sd_size - 1].font_rid;
  3838. if (!_font_has_char(whitespace_gl_font_rid, '.')) {
  3839. const Array &fonts = spans[spans.size() - 1].fonts;
  3840. for (int i = 0; i < fonts.size(); i++) {
  3841. if (_font_has_char(fonts[i], ' ')) {
  3842. whitespace_gl_font_rid = fonts[i];
  3843. break;
  3844. }
  3845. }
  3846. }
  3847. int32_t dot_gl_idx = dot_gl_font_rid.is_valid() ? _font_get_glyph_index(dot_gl_font_rid, last_gl_font_size, '.', 0) : -10;
  3848. Vector2 dot_adv = dot_gl_font_rid.is_valid() ? _font_get_glyph_advance(dot_gl_font_rid, last_gl_font_size, dot_gl_idx) : Vector2();
  3849. int32_t whitespace_gl_idx = whitespace_gl_font_rid.is_valid() ? _font_get_glyph_index(whitespace_gl_font_rid, last_gl_font_size, ' ', 0) : -10;
  3850. 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();
  3851. int ellipsis_width = 0;
  3852. if (add_ellipsis && whitespace_gl_font_rid.is_valid()) {
  3853. ellipsis_width = 3 * dot_adv.x + sd->extra_spacing[SPACING_GLYPH] + (cut_per_word ? whitespace_adv.x : 0);
  3854. }
  3855. int ell_min_characters = 6;
  3856. double width = sd->width;
  3857. bool is_rtl = sd->para_direction == DIRECTION_RTL;
  3858. int trim_pos = (is_rtl) ? sd_size : 0;
  3859. int ellipsis_pos = (enforce_ellipsis) ? 0 : -1;
  3860. int last_valid_cut = 0;
  3861. bool found = false;
  3862. int glyphs_from = (is_rtl) ? 0 : sd_size - 1;
  3863. int glyphs_to = (is_rtl) ? sd_size - 1 : -1;
  3864. int glyphs_delta = (is_rtl) ? +1 : -1;
  3865. if (enforce_ellipsis && (width + ellipsis_width <= p_width)) {
  3866. trim_pos = -1;
  3867. ellipsis_pos = (is_rtl) ? 0 : sd_size;
  3868. } else {
  3869. for (int i = glyphs_from; i != glyphs_to; i += glyphs_delta) {
  3870. if (!is_rtl) {
  3871. width -= sd_glyphs[i].advance * sd_glyphs[i].repeat;
  3872. }
  3873. if (sd_glyphs[i].count > 0) {
  3874. bool above_min_char_threshold = ((is_rtl) ? sd_size - 1 - i : i) >= ell_min_characters;
  3875. if (width + (((above_min_char_threshold && add_ellipsis) || enforce_ellipsis) ? ellipsis_width : 0) <= p_width) {
  3876. if (cut_per_word && above_min_char_threshold) {
  3877. if ((sd_glyphs[i].flags & GRAPHEME_IS_BREAK_SOFT) == GRAPHEME_IS_BREAK_SOFT) {
  3878. last_valid_cut = i;
  3879. found = true;
  3880. }
  3881. } else {
  3882. last_valid_cut = i;
  3883. found = true;
  3884. }
  3885. if (found) {
  3886. trim_pos = last_valid_cut;
  3887. if (add_ellipsis && (above_min_char_threshold || enforce_ellipsis) && width - ellipsis_width <= p_width) {
  3888. ellipsis_pos = trim_pos;
  3889. }
  3890. break;
  3891. }
  3892. }
  3893. }
  3894. if (is_rtl) {
  3895. width -= sd_glyphs[i].advance * sd_glyphs[i].repeat;
  3896. }
  3897. }
  3898. }
  3899. sd->overrun_trim_data.trim_pos = trim_pos;
  3900. sd->overrun_trim_data.ellipsis_pos = ellipsis_pos;
  3901. if (trim_pos == 0 && enforce_ellipsis && add_ellipsis) {
  3902. sd->overrun_trim_data.ellipsis_pos = 0;
  3903. }
  3904. if ((trim_pos >= 0 && sd->width > p_width) || enforce_ellipsis) {
  3905. if (add_ellipsis && (ellipsis_pos > 0 || enforce_ellipsis)) {
  3906. // Insert an additional space when cutting word bound for aesthetics.
  3907. if (cut_per_word && (ellipsis_pos > 0)) {
  3908. Glyph gl;
  3909. gl.count = 1;
  3910. gl.advance = whitespace_adv.x;
  3911. gl.index = whitespace_gl_idx;
  3912. gl.font_rid = whitespace_gl_font_rid;
  3913. gl.font_size = last_gl_font_size;
  3914. gl.flags = GRAPHEME_IS_SPACE | GRAPHEME_IS_BREAK_SOFT | GRAPHEME_IS_VIRTUAL | (is_rtl ? GRAPHEME_IS_RTL : 0);
  3915. sd->overrun_trim_data.ellipsis_glyph_buf.append(gl);
  3916. }
  3917. // Add ellipsis dots.
  3918. if (dot_gl_idx != 0) {
  3919. Glyph gl;
  3920. gl.count = 1;
  3921. gl.repeat = 3;
  3922. gl.advance = dot_adv.x;
  3923. gl.index = dot_gl_idx;
  3924. gl.font_rid = dot_gl_font_rid;
  3925. gl.font_size = last_gl_font_size;
  3926. gl.flags = GRAPHEME_IS_PUNCTUATION | GRAPHEME_IS_VIRTUAL | (is_rtl ? GRAPHEME_IS_RTL : 0);
  3927. sd->overrun_trim_data.ellipsis_glyph_buf.append(gl);
  3928. }
  3929. }
  3930. sd->text_trimmed = true;
  3931. sd->width_trimmed = width + ((ellipsis_pos != -1) ? ellipsis_width : 0);
  3932. }
  3933. }
  3934. int64_t TextServerAdvanced::_shaped_text_get_trim_pos(const RID &p_shaped) const {
  3935. ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
  3936. ERR_FAIL_COND_V_MSG(!sd, -1, "ShapedTextDataAdvanced invalid.");
  3937. MutexLock lock(sd->mutex);
  3938. return sd->overrun_trim_data.trim_pos;
  3939. }
  3940. int64_t TextServerAdvanced::_shaped_text_get_ellipsis_pos(const RID &p_shaped) const {
  3941. ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
  3942. ERR_FAIL_COND_V_MSG(!sd, -1, "ShapedTextDataAdvanced invalid.");
  3943. MutexLock lock(sd->mutex);
  3944. return sd->overrun_trim_data.ellipsis_pos;
  3945. }
  3946. const Glyph *TextServerAdvanced::_shaped_text_get_ellipsis_glyphs(const RID &p_shaped) const {
  3947. ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
  3948. ERR_FAIL_COND_V_MSG(!sd, nullptr, "ShapedTextDataAdvanced invalid.");
  3949. MutexLock lock(sd->mutex);
  3950. return sd->overrun_trim_data.ellipsis_glyph_buf.ptr();
  3951. }
  3952. int64_t TextServerAdvanced::_shaped_text_get_ellipsis_glyph_count(const RID &p_shaped) const {
  3953. ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
  3954. ERR_FAIL_COND_V_MSG(!sd, 0, "ShapedTextDataAdvanced invalid.");
  3955. MutexLock lock(sd->mutex);
  3956. return sd->overrun_trim_data.ellipsis_glyph_buf.size();
  3957. }
  3958. bool TextServerAdvanced::_shaped_text_update_breaks(const RID &p_shaped) {
  3959. ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
  3960. ERR_FAIL_COND_V(!sd, false);
  3961. MutexLock lock(sd->mutex);
  3962. if (!sd->valid) {
  3963. _shaped_text_shape(p_shaped);
  3964. }
  3965. if (sd->line_breaks_valid) {
  3966. return true; // Nothing to do.
  3967. }
  3968. const UChar *data = sd->utf16.get_data();
  3969. if (!sd->break_ops_valid) {
  3970. sd->breaks.clear();
  3971. sd->break_inserts = 0;
  3972. UErrorCode err = U_ZERO_ERROR;
  3973. int i = 0;
  3974. while (i < sd->spans.size()) {
  3975. String language = sd->spans[i].language;
  3976. int r_start = sd->spans[i].start;
  3977. while (i + 1 < sd->spans.size() && language == sd->spans[i + 1].language) {
  3978. i++;
  3979. }
  3980. int r_end = sd->spans[i].end;
  3981. UBreakIterator *bi = ubrk_open(UBRK_LINE, (language.is_empty()) ? TranslationServer::get_singleton()->get_tool_locale().ascii().get_data() : language.ascii().get_data(), data + _convert_pos_inv(sd, r_start), _convert_pos_inv(sd, r_end - r_start), &err);
  3982. if (U_FAILURE(err)) {
  3983. // No data loaded - use fallback.
  3984. for (int j = r_start; j < r_end; j++) {
  3985. char32_t c = sd->text[j - sd->start];
  3986. if (is_whitespace(c)) {
  3987. sd->breaks[j + 1] = false;
  3988. }
  3989. if (is_linebreak(c)) {
  3990. sd->breaks[j + 1] = true;
  3991. }
  3992. }
  3993. } else {
  3994. while (ubrk_next(bi) != UBRK_DONE) {
  3995. int pos = _convert_pos(sd, ubrk_current(bi)) + r_start;
  3996. if ((ubrk_getRuleStatus(bi) >= UBRK_LINE_HARD) && (ubrk_getRuleStatus(bi) < UBRK_LINE_HARD_LIMIT)) {
  3997. sd->breaks[pos] = true;
  3998. } else if ((ubrk_getRuleStatus(bi) >= UBRK_LINE_SOFT) && (ubrk_getRuleStatus(bi) < UBRK_LINE_SOFT_LIMIT)) {
  3999. sd->breaks[pos] = false;
  4000. }
  4001. int pos_p = pos - 1 - sd->start;
  4002. char32_t c = sd->text[pos_p];
  4003. if (pos - sd->start != sd->end && !is_whitespace(c) && (c != 0xfffc)) {
  4004. sd->break_inserts++;
  4005. }
  4006. }
  4007. }
  4008. ubrk_close(bi);
  4009. i++;
  4010. }
  4011. sd->break_ops_valid = true;
  4012. }
  4013. Vector<Glyph> glyphs_new;
  4014. bool rewrite = false;
  4015. int sd_shift = 0;
  4016. int sd_size = sd->glyphs.size();
  4017. Glyph *sd_glyphs = sd->glyphs.ptrw();
  4018. Glyph *sd_glyphs_new = nullptr;
  4019. if (sd->break_inserts > 0) {
  4020. glyphs_new.resize(sd->glyphs.size() + sd->break_inserts);
  4021. sd_glyphs_new = glyphs_new.ptrw();
  4022. rewrite = true;
  4023. } else {
  4024. sd_glyphs_new = sd_glyphs;
  4025. }
  4026. sd->sort_valid = false;
  4027. sd->glyphs_logical.clear();
  4028. const char32_t *ch = sd->text.ptr();
  4029. int c_punct_size = sd->custom_punct.length();
  4030. const char32_t *c_punct = sd->custom_punct.ptr();
  4031. for (int i = 0; i < sd_size; i++) {
  4032. if (rewrite) {
  4033. for (int j = 0; j < sd_glyphs[i].count; j++) {
  4034. sd_glyphs_new[sd_shift + i + j] = sd_glyphs[i + j];
  4035. }
  4036. }
  4037. if (sd_glyphs[i].count > 0) {
  4038. char32_t c = ch[sd_glyphs[i].start - sd->start];
  4039. if (c == 0xfffc) {
  4040. i += (sd_glyphs[i].count - 1);
  4041. continue;
  4042. }
  4043. if (c == 0x0009 || c == 0x000b) {
  4044. sd_glyphs_new[sd_shift + i].flags |= GRAPHEME_IS_TAB;
  4045. }
  4046. if (is_whitespace(c)) {
  4047. sd_glyphs_new[sd_shift + i].flags |= GRAPHEME_IS_SPACE;
  4048. }
  4049. if (c_punct_size == 0) {
  4050. if (u_ispunct(c) && c != 0x005f) {
  4051. sd_glyphs_new[sd_shift + i].flags |= GRAPHEME_IS_PUNCTUATION;
  4052. }
  4053. } else {
  4054. for (int j = 0; j < c_punct_size; j++) {
  4055. if (c_punct[j] == c) {
  4056. sd_glyphs_new[sd_shift + i].flags |= GRAPHEME_IS_PUNCTUATION;
  4057. break;
  4058. }
  4059. }
  4060. }
  4061. if (is_underscore(c)) {
  4062. sd_glyphs_new[sd_shift + i].flags |= GRAPHEME_IS_UNDERSCORE;
  4063. }
  4064. if (sd->breaks.has(sd_glyphs[i].end)) {
  4065. if (sd->breaks[sd_glyphs[i].end] && (is_linebreak(c))) {
  4066. sd_glyphs_new[sd_shift + i].flags |= GRAPHEME_IS_BREAK_HARD;
  4067. } else if (is_whitespace(c)) {
  4068. sd_glyphs_new[sd_shift + i].flags |= GRAPHEME_IS_BREAK_SOFT;
  4069. } else {
  4070. int count = sd_glyphs[i].count;
  4071. // Do not add extra space at the end of the line.
  4072. if (sd_glyphs[i].end == sd->end) {
  4073. i += (sd_glyphs[i].count - 1);
  4074. continue;
  4075. }
  4076. // Do not add extra space after existing space.
  4077. if (sd_glyphs[i].flags & GRAPHEME_IS_RTL) {
  4078. if ((i + count < sd_size - 1) && ((sd_glyphs[i + count].flags & (GRAPHEME_IS_SPACE | GRAPHEME_IS_BREAK_SOFT)) == (GRAPHEME_IS_SPACE | GRAPHEME_IS_BREAK_SOFT))) {
  4079. i += (sd_glyphs[i].count - 1);
  4080. continue;
  4081. }
  4082. } else {
  4083. if ((sd_glyphs[i].flags & (GRAPHEME_IS_SPACE | GRAPHEME_IS_BREAK_SOFT)) == (GRAPHEME_IS_SPACE | GRAPHEME_IS_BREAK_SOFT)) {
  4084. i += (sd_glyphs[i].count - 1);
  4085. continue;
  4086. }
  4087. }
  4088. Glyph gl;
  4089. gl.start = sd_glyphs[i].start;
  4090. gl.end = sd_glyphs[i].end;
  4091. gl.count = 1;
  4092. gl.font_rid = sd_glyphs[i].font_rid;
  4093. gl.font_size = sd_glyphs[i].font_size;
  4094. gl.flags = GRAPHEME_IS_BREAK_SOFT | GRAPHEME_IS_VIRTUAL | GRAPHEME_IS_SPACE;
  4095. if (sd_glyphs[i].flags & GRAPHEME_IS_RTL) {
  4096. gl.flags |= GRAPHEME_IS_RTL;
  4097. for (int j = sd_glyphs[i].count - 1; j >= 0; j--) {
  4098. sd_glyphs_new[sd_shift + i + j + 1] = sd_glyphs_new[sd_shift + i + j];
  4099. }
  4100. sd_glyphs_new[sd_shift + i] = gl;
  4101. } else {
  4102. sd_glyphs_new[sd_shift + i + count] = gl;
  4103. }
  4104. sd_shift++;
  4105. ERR_FAIL_COND_V_MSG(sd_shift > sd->break_inserts, false, "Invalid break insert count!");
  4106. }
  4107. }
  4108. i += (sd_glyphs[i].count - 1);
  4109. }
  4110. }
  4111. if (sd_shift < sd->break_inserts) {
  4112. // Note: should not happen with a normal text, but might be a case with special fonts that substitute a long string (with breaks opportunities in it) with a single glyph (like Font Awesome).
  4113. glyphs_new.resize(sd->glyphs.size() + sd_shift);
  4114. }
  4115. if (sd->break_inserts > 0) {
  4116. sd->glyphs = glyphs_new;
  4117. }
  4118. sd->line_breaks_valid = true;
  4119. return sd->line_breaks_valid;
  4120. }
  4121. _FORCE_INLINE_ int64_t _generate_kashida_justification_opportunies(const String &p_data, int64_t p_start, int64_t p_end) {
  4122. int64_t kashida_pos = -1;
  4123. int8_t priority = 100;
  4124. int64_t i = p_start;
  4125. char32_t pc = 0;
  4126. while ((p_end > p_start) && is_transparent(p_data[p_end - 1])) {
  4127. p_end--;
  4128. }
  4129. while (i < p_end) {
  4130. uint32_t c = p_data[i];
  4131. if (c == 0x0640) {
  4132. kashida_pos = i;
  4133. priority = 0;
  4134. }
  4135. if (priority >= 1 && i < p_end - 1) {
  4136. if (is_seen_sad(c) && (p_data[i + 1] != 0x200c)) {
  4137. kashida_pos = i;
  4138. priority = 1;
  4139. }
  4140. }
  4141. if (priority >= 2 && i > p_start) {
  4142. if (is_teh_marbuta(c) || is_dal(c) || (is_heh(c) && i == p_end - 1)) {
  4143. if (is_connected_to_prev(c, pc)) {
  4144. kashida_pos = i - 1;
  4145. priority = 2;
  4146. }
  4147. }
  4148. }
  4149. if (priority >= 3 && i > p_start) {
  4150. if (is_alef(c) || ((is_lam(c) || is_tah(c) || is_kaf(c) || is_gaf(c)) && i == p_end - 1)) {
  4151. if (is_connected_to_prev(c, pc)) {
  4152. kashida_pos = i - 1;
  4153. priority = 3;
  4154. }
  4155. }
  4156. }
  4157. if (priority >= 4 && i > p_start && i < p_end - 1) {
  4158. if (is_beh(c)) {
  4159. if (is_reh(p_data[i + 1]) || is_yeh(p_data[i + 1])) {
  4160. if (is_connected_to_prev(c, pc)) {
  4161. kashida_pos = i - 1;
  4162. priority = 4;
  4163. }
  4164. }
  4165. }
  4166. }
  4167. if (priority >= 5 && i > p_start) {
  4168. if (is_waw(c) || ((is_ain(c) || is_qaf(c) || is_feh(c)) && i == p_end - 1)) {
  4169. if (is_connected_to_prev(c, pc)) {
  4170. kashida_pos = i - 1;
  4171. priority = 5;
  4172. }
  4173. }
  4174. }
  4175. if (priority >= 6 && i > p_start) {
  4176. if (is_reh(c)) {
  4177. if (is_connected_to_prev(c, pc)) {
  4178. kashida_pos = i - 1;
  4179. priority = 6;
  4180. }
  4181. }
  4182. }
  4183. if (!is_transparent(c)) {
  4184. pc = c;
  4185. }
  4186. i++;
  4187. }
  4188. return kashida_pos;
  4189. }
  4190. bool TextServerAdvanced::_shaped_text_update_justification_ops(const RID &p_shaped) {
  4191. ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
  4192. ERR_FAIL_COND_V(!sd, false);
  4193. MutexLock lock(sd->mutex);
  4194. if (!sd->valid) {
  4195. _shaped_text_shape(p_shaped);
  4196. }
  4197. if (!sd->line_breaks_valid) {
  4198. _shaped_text_update_breaks(p_shaped);
  4199. }
  4200. if (sd->justification_ops_valid) {
  4201. return true; // Nothing to do.
  4202. }
  4203. const UChar *data = sd->utf16.get_data();
  4204. int data_size = sd->utf16.length();
  4205. if (!sd->js_ops_valid) {
  4206. sd->jstops.clear();
  4207. // Use ICU word iterator and custom kashida detection.
  4208. UErrorCode err = U_ZERO_ERROR;
  4209. UBreakIterator *bi = ubrk_open(UBRK_WORD, "", data, data_size, &err);
  4210. if (U_FAILURE(err)) {
  4211. // No data - use fallback.
  4212. int limit = 0;
  4213. for (int i = 0; i < sd->text.length(); i++) {
  4214. if (is_whitespace(data[i])) {
  4215. int ks = _generate_kashida_justification_opportunies(sd->text, limit, i) + sd->start;
  4216. if (ks != -1) {
  4217. sd->jstops[ks] = true;
  4218. }
  4219. limit = i + 1;
  4220. }
  4221. }
  4222. int ks = _generate_kashida_justification_opportunies(sd->text, limit, sd->text.length()) + sd->start;
  4223. if (ks != -1) {
  4224. sd->jstops[ks] = true;
  4225. }
  4226. } else {
  4227. int limit = 0;
  4228. while (ubrk_next(bi) != UBRK_DONE) {
  4229. if (ubrk_getRuleStatus(bi) != UBRK_WORD_NONE) {
  4230. int i = _convert_pos(sd, ubrk_current(bi));
  4231. sd->jstops[i + sd->start] = false;
  4232. int ks = _generate_kashida_justification_opportunies(sd->text, limit, i);
  4233. if (ks != -1) {
  4234. sd->jstops[ks + sd->start] = true;
  4235. }
  4236. limit = i;
  4237. }
  4238. }
  4239. ubrk_close(bi);
  4240. }
  4241. sd->js_ops_valid = true;
  4242. }
  4243. sd->sort_valid = false;
  4244. sd->glyphs_logical.clear();
  4245. Glyph *sd_glyphs = sd->glyphs.ptrw();
  4246. int sd_size = sd->glyphs.size();
  4247. if (!sd->jstops.is_empty()) {
  4248. for (int i = 0; i < sd_size; i++) {
  4249. if (sd_glyphs[i].count > 0) {
  4250. char32_t c = sd->text[sd_glyphs[i].start - sd->start];
  4251. if (c == 0x0640 && sd_glyphs[i].start == sd_glyphs[i].end - 1) {
  4252. sd_glyphs[i].flags |= GRAPHEME_IS_ELONGATION;
  4253. }
  4254. if (sd->jstops.has(sd_glyphs[i].start)) {
  4255. if (c == 0xfffc) {
  4256. continue;
  4257. }
  4258. if (sd->jstops[sd_glyphs[i].start]) {
  4259. if (c != 0x0640) {
  4260. if (sd_glyphs[i].font_rid != RID()) {
  4261. Glyph gl = _shape_single_glyph(sd, 0x0640, HB_SCRIPT_ARABIC, HB_DIRECTION_RTL, sd->glyphs[i].font_rid, sd->glyphs[i].font_size);
  4262. if ((sd_glyphs[i].flags & GRAPHEME_IS_VALID) == GRAPHEME_IS_VALID) {
  4263. #if HB_VERSION_ATLEAST(5, 1, 0)
  4264. if ((i > 0) && ((sd_glyphs[i - 1].flags & GRAPHEME_IS_SAFE_TO_INSERT_TATWEEL) != GRAPHEME_IS_SAFE_TO_INSERT_TATWEEL)) {
  4265. continue;
  4266. }
  4267. #endif
  4268. gl.start = sd_glyphs[i].start;
  4269. gl.end = sd_glyphs[i].end;
  4270. gl.repeat = 0;
  4271. gl.count = 1;
  4272. if (sd->orientation == ORIENTATION_HORIZONTAL) {
  4273. gl.y_off = sd_glyphs[i].y_off;
  4274. } else {
  4275. gl.x_off = sd_glyphs[i].x_off;
  4276. }
  4277. gl.flags |= GRAPHEME_IS_ELONGATION | GRAPHEME_IS_VIRTUAL;
  4278. sd->glyphs.insert(i, gl);
  4279. i++;
  4280. // Update write pointer and size.
  4281. sd_size = sd->glyphs.size();
  4282. sd_glyphs = sd->glyphs.ptrw();
  4283. continue;
  4284. }
  4285. }
  4286. }
  4287. } else if ((sd_glyphs[i].flags & GRAPHEME_IS_SPACE) != GRAPHEME_IS_SPACE) {
  4288. int count = sd_glyphs[i].count;
  4289. // Do not add extra spaces at the end of the line.
  4290. if (sd_glyphs[i].end == sd->end) {
  4291. continue;
  4292. }
  4293. // Do not add extra space after existing space.
  4294. if (sd_glyphs[i].flags & GRAPHEME_IS_RTL) {
  4295. if ((i + count < sd_size - 1) && ((sd_glyphs[i + count].flags & (GRAPHEME_IS_SPACE | GRAPHEME_IS_BREAK_SOFT)) == (GRAPHEME_IS_SPACE | GRAPHEME_IS_BREAK_SOFT))) {
  4296. continue;
  4297. }
  4298. } else {
  4299. if ((i > 0) && ((sd_glyphs[i - 1].flags & (GRAPHEME_IS_SPACE | GRAPHEME_IS_BREAK_SOFT)) == (GRAPHEME_IS_SPACE | GRAPHEME_IS_BREAK_SOFT))) {
  4300. continue;
  4301. }
  4302. }
  4303. // Inject virtual space for alignment.
  4304. Glyph gl;
  4305. gl.start = sd_glyphs[i].start;
  4306. gl.end = sd_glyphs[i].end;
  4307. gl.count = 1;
  4308. gl.font_rid = sd_glyphs[i].font_rid;
  4309. gl.font_size = sd_glyphs[i].font_size;
  4310. gl.flags = GRAPHEME_IS_SPACE | GRAPHEME_IS_VIRTUAL;
  4311. if (sd_glyphs[i].flags & GRAPHEME_IS_RTL) {
  4312. gl.flags |= GRAPHEME_IS_RTL;
  4313. sd->glyphs.insert(i, gl); // Insert before.
  4314. } else {
  4315. sd->glyphs.insert(i + count, gl); // Insert after.
  4316. }
  4317. i += count;
  4318. // Update write pointer and size.
  4319. sd_size = sd->glyphs.size();
  4320. sd_glyphs = sd->glyphs.ptrw();
  4321. continue;
  4322. }
  4323. }
  4324. }
  4325. }
  4326. }
  4327. sd->justification_ops_valid = true;
  4328. return sd->justification_ops_valid;
  4329. }
  4330. Glyph TextServerAdvanced::_shape_single_glyph(ShapedTextDataAdvanced *p_sd, char32_t p_char, hb_script_t p_script, hb_direction_t p_direction, const RID &p_font, int64_t p_font_size) {
  4331. hb_font_t *hb_font = _font_get_hb_handle(p_font, p_font_size);
  4332. double scale = _font_get_scale(p_font, p_font_size);
  4333. bool subpos = (scale != 1.0) || (_font_get_subpixel_positioning(p_font) == SUBPIXEL_POSITIONING_ONE_HALF) || (_font_get_subpixel_positioning(p_font) == SUBPIXEL_POSITIONING_ONE_QUARTER) || (_font_get_subpixel_positioning(p_font) == SUBPIXEL_POSITIONING_AUTO && p_font_size <= SUBPIXEL_POSITIONING_ONE_HALF_MAX_SIZE);
  4334. ERR_FAIL_COND_V(hb_font == nullptr, Glyph());
  4335. hb_buffer_clear_contents(p_sd->hb_buffer);
  4336. hb_buffer_set_direction(p_sd->hb_buffer, p_direction);
  4337. hb_buffer_set_flags(p_sd->hb_buffer, (hb_buffer_flags_t)(HB_BUFFER_FLAG_DEFAULT));
  4338. hb_buffer_set_script(p_sd->hb_buffer, p_script);
  4339. hb_buffer_add_utf32(p_sd->hb_buffer, (const uint32_t *)&p_char, 1, 0, 1);
  4340. hb_shape(hb_font, p_sd->hb_buffer, nullptr, 0);
  4341. unsigned int glyph_count = 0;
  4342. hb_glyph_info_t *glyph_info = hb_buffer_get_glyph_infos(p_sd->hb_buffer, &glyph_count);
  4343. hb_glyph_position_t *glyph_pos = hb_buffer_get_glyph_positions(p_sd->hb_buffer, &glyph_count);
  4344. // Process glyphs.
  4345. Glyph gl;
  4346. if (p_direction == HB_DIRECTION_RTL || p_direction == HB_DIRECTION_BTT) {
  4347. gl.flags |= TextServer::GRAPHEME_IS_RTL;
  4348. }
  4349. gl.font_rid = p_font;
  4350. gl.font_size = p_font_size;
  4351. if (glyph_count > 0) {
  4352. if (p_sd->orientation == ORIENTATION_HORIZONTAL) {
  4353. if (subpos) {
  4354. gl.advance = (double)glyph_pos[0].x_advance / (64.0 / scale) + _get_extra_advance(p_font, p_font_size);
  4355. } else {
  4356. gl.advance = Math::round((double)glyph_pos[0].x_advance / (64.0 / scale) + _get_extra_advance(p_font, p_font_size));
  4357. }
  4358. } else {
  4359. gl.advance = -Math::round((double)glyph_pos[0].y_advance / (64.0 / scale));
  4360. }
  4361. gl.count = 1;
  4362. gl.index = glyph_info[0].codepoint;
  4363. if (subpos) {
  4364. gl.x_off = (double)glyph_pos[0].x_offset / (64.0 / scale);
  4365. } else {
  4366. gl.x_off = Math::round((double)glyph_pos[0].x_offset / (64.0 / scale));
  4367. }
  4368. gl.y_off = -Math::round((double)glyph_pos[0].y_offset / (64.0 / scale));
  4369. if ((glyph_info[0].codepoint != 0) || !u_isgraph(p_char)) {
  4370. gl.flags |= GRAPHEME_IS_VALID;
  4371. }
  4372. }
  4373. return gl;
  4374. }
  4375. _FORCE_INLINE_ void TextServerAdvanced::_add_featuers(const Dictionary &p_source, Vector<hb_feature_t> &r_ftrs) {
  4376. Array keys = p_source.keys();
  4377. Array values = p_source.values();
  4378. for (int i = 0; i < keys.size(); i++) {
  4379. int32_t value = values[i];
  4380. if (value >= 0) {
  4381. hb_feature_t feature;
  4382. if (keys[i].get_type() == Variant::STRING) {
  4383. feature.tag = _name_to_tag(keys[i]);
  4384. } else {
  4385. feature.tag = keys[i];
  4386. }
  4387. feature.value = value;
  4388. feature.start = 0;
  4389. feature.end = -1;
  4390. r_ftrs.push_back(feature);
  4391. }
  4392. }
  4393. }
  4394. void TextServerAdvanced::_shape_run(ShapedTextDataAdvanced *p_sd, int64_t p_start, int64_t p_end, hb_script_t p_script, hb_direction_t p_direction, TypedArray<RID> p_fonts, int64_t p_span, int64_t p_fb_index, int64_t p_prev_start, int64_t p_prev_end) {
  4395. RID f;
  4396. int fs = p_sd->spans[p_span].font_size;
  4397. if (p_fb_index >= 0 && p_fb_index < p_fonts.size()) {
  4398. // Try font from list.
  4399. f = p_fonts[p_fb_index];
  4400. } else if (OS::get_singleton()->has_feature("system_fonts") && p_fonts.size() > 0 && ((p_fb_index == p_fonts.size()) || (p_fb_index > p_fonts.size() && p_start != p_prev_start))) {
  4401. // Try system fallback.
  4402. RID fdef = p_fonts[0];
  4403. if (_font_is_allow_system_fallback(fdef)) {
  4404. String text = p_sd->text.substr(p_start, 1);
  4405. String font_name = _font_get_name(fdef);
  4406. BitField<FontStyle> font_style = _font_get_style(fdef);
  4407. int font_weight = _font_get_weight(fdef);
  4408. int font_stretch = _font_get_stretch(fdef);
  4409. Dictionary dvar = _font_get_variation_coordinates(fdef);
  4410. static int64_t wgth_tag = _name_to_tag("weight");
  4411. static int64_t wdth_tag = _name_to_tag("width");
  4412. static int64_t ital_tag = _name_to_tag("italic");
  4413. if (dvar.has(wgth_tag)) {
  4414. font_weight = dvar[wgth_tag].operator int();
  4415. }
  4416. if (dvar.has(wdth_tag)) {
  4417. font_stretch = dvar[wdth_tag].operator int();
  4418. }
  4419. if (dvar.has(ital_tag) && dvar[ital_tag].operator int() == 1) {
  4420. font_style.set_flag(TextServer::FONT_ITALIC);
  4421. }
  4422. char scr_buffer[5] = { 0, 0, 0, 0, 0 };
  4423. hb_tag_to_string(hb_script_to_iso15924_tag(p_script), scr_buffer);
  4424. String script_code = String(scr_buffer);
  4425. String locale = (p_sd->spans[p_span].language.is_empty()) ? TranslationServer::get_singleton()->get_tool_locale() : p_sd->spans[p_span].language;
  4426. PackedStringArray fallback_font_name = OS::get_singleton()->get_system_font_path_for_text(font_name, text, locale, script_code, font_weight, font_stretch, font_style & TextServer::FONT_ITALIC);
  4427. #ifdef GDEXTENSION
  4428. for (int fb = 0; fb < fallback_font_name.size(); fb++) {
  4429. const String &E = fallback_font_name[fb];
  4430. #else
  4431. for (const String &E : fallback_font_name) {
  4432. #endif
  4433. SystemFontKey key = SystemFontKey(E, font_style & TextServer::FONT_ITALIC, font_weight, font_stretch, fdef, this);
  4434. if (system_fonts.has(key)) {
  4435. const SystemFontCache &sysf_cache = system_fonts[key];
  4436. int best_score = 0;
  4437. int best_match = -1;
  4438. for (int face_idx = 0; face_idx < sysf_cache.var.size(); face_idx++) {
  4439. const SystemFontCacheRec &F = sysf_cache.var[face_idx];
  4440. if (unlikely(!_font_has_char(F.rid, text[0]))) {
  4441. continue;
  4442. }
  4443. BitField<FontStyle> style = _font_get_style(F.rid);
  4444. int weight = _font_get_weight(F.rid);
  4445. int stretch = _font_get_stretch(F.rid);
  4446. int score = (20 - Math::abs(weight - font_weight) / 50);
  4447. score += (20 - Math::abs(stretch - font_stretch) / 10);
  4448. if (bool(style & TextServer::FONT_ITALIC) == bool(font_style & TextServer::FONT_ITALIC)) {
  4449. score += 30;
  4450. }
  4451. if (score >= best_score) {
  4452. best_score = score;
  4453. best_match = face_idx;
  4454. }
  4455. if (best_score == 70) {
  4456. break;
  4457. }
  4458. }
  4459. if (best_match != -1) {
  4460. f = sysf_cache.var[best_match].rid;
  4461. }
  4462. }
  4463. if (!f.is_valid()) {
  4464. if (system_fonts.has(key)) {
  4465. const SystemFontCache &sysf_cache = system_fonts[key];
  4466. if (sysf_cache.max_var == sysf_cache.var.size()) {
  4467. // All subfonts already tested, skip.
  4468. continue;
  4469. }
  4470. }
  4471. if (!system_font_data.has(E)) {
  4472. system_font_data[E] = FileAccess::get_file_as_bytes(E);
  4473. }
  4474. const PackedByteArray &font_data = system_font_data[E];
  4475. SystemFontCacheRec sysf;
  4476. sysf.rid = _create_font();
  4477. _font_set_data_ptr(sysf.rid, font_data.ptr(), font_data.size());
  4478. Dictionary var = dvar;
  4479. // Select matching style from collection.
  4480. int best_score = 0;
  4481. int best_match = -1;
  4482. for (int face_idx = 0; face_idx < _font_get_face_count(sysf.rid); face_idx++) {
  4483. _font_set_face_index(sysf.rid, face_idx);
  4484. if (unlikely(!_font_has_char(sysf.rid, text[0]))) {
  4485. continue;
  4486. }
  4487. BitField<FontStyle> style = _font_get_style(sysf.rid);
  4488. int weight = _font_get_weight(sysf.rid);
  4489. int stretch = _font_get_stretch(sysf.rid);
  4490. int score = (20 - Math::abs(weight - font_weight) / 50);
  4491. score += (20 - Math::abs(stretch - font_stretch) / 10);
  4492. if (bool(style & TextServer::FONT_ITALIC) == bool(font_style & TextServer::FONT_ITALIC)) {
  4493. score += 30;
  4494. }
  4495. if (score >= best_score) {
  4496. best_score = score;
  4497. best_match = face_idx;
  4498. }
  4499. if (best_score == 70) {
  4500. break;
  4501. }
  4502. }
  4503. if (best_match == -1) {
  4504. _free_rid(sysf.rid);
  4505. continue;
  4506. } else {
  4507. _font_set_face_index(sysf.rid, best_match);
  4508. }
  4509. sysf.index = best_match;
  4510. // If it's a variable font, apply weight, stretch and italic coordinates to match requested style.
  4511. if (best_score != 70) {
  4512. Dictionary ftr = _font_supported_variation_list(sysf.rid);
  4513. if (ftr.has(wdth_tag)) {
  4514. var[wdth_tag] = font_stretch;
  4515. _font_set_stretch(sysf.rid, font_stretch);
  4516. }
  4517. if (ftr.has(wgth_tag)) {
  4518. var[wgth_tag] = font_weight;
  4519. _font_set_weight(sysf.rid, font_weight);
  4520. }
  4521. if ((font_style & TextServer::FONT_ITALIC) && ftr.has(ital_tag)) {
  4522. var[ital_tag] = 1;
  4523. _font_set_style(sysf.rid, _font_get_style(sysf.rid) | TextServer::FONT_ITALIC);
  4524. }
  4525. }
  4526. _font_set_antialiasing(sysf.rid, key.antialiasing);
  4527. _font_set_generate_mipmaps(sysf.rid, key.mipmaps);
  4528. _font_set_multichannel_signed_distance_field(sysf.rid, key.msdf);
  4529. _font_set_msdf_pixel_range(sysf.rid, key.msdf_range);
  4530. _font_set_msdf_size(sysf.rid, key.msdf_source_size);
  4531. _font_set_fixed_size(sysf.rid, key.fixed_size);
  4532. _font_set_force_autohinter(sysf.rid, key.force_autohinter);
  4533. _font_set_hinting(sysf.rid, key.hinting);
  4534. _font_set_subpixel_positioning(sysf.rid, key.subpixel_positioning);
  4535. _font_set_variation_coordinates(sysf.rid, var);
  4536. _font_set_oversampling(sysf.rid, key.oversampling);
  4537. _font_set_embolden(sysf.rid, key.embolden);
  4538. _font_set_transform(sysf.rid, key.transform);
  4539. if (system_fonts.has(key)) {
  4540. system_fonts[key].var.push_back(sysf);
  4541. } else {
  4542. SystemFontCache &sysf_cache = system_fonts[key];
  4543. sysf_cache.max_var = _font_get_face_count(sysf.rid);
  4544. sysf_cache.var.push_back(sysf);
  4545. }
  4546. f = sysf.rid;
  4547. }
  4548. break;
  4549. }
  4550. }
  4551. }
  4552. if (!f.is_valid()) {
  4553. // No valid font, use fallback hex code boxes.
  4554. for (int i = p_start; i < p_end; i++) {
  4555. if (p_sd->preserve_invalid || (p_sd->preserve_control && is_control(p_sd->text[i]))) {
  4556. Glyph gl;
  4557. gl.start = i + p_sd->start;
  4558. gl.end = i + 1 + p_sd->start;
  4559. gl.count = 1;
  4560. gl.index = p_sd->text[i];
  4561. gl.font_size = fs;
  4562. gl.font_rid = RID();
  4563. if (p_direction == HB_DIRECTION_RTL || p_direction == HB_DIRECTION_BTT) {
  4564. gl.flags |= TextServer::GRAPHEME_IS_RTL;
  4565. }
  4566. if (p_sd->orientation == ORIENTATION_HORIZONTAL) {
  4567. gl.advance = get_hex_code_box_size(fs, gl.index).x;
  4568. p_sd->ascent = MAX(p_sd->ascent, get_hex_code_box_size(fs, gl.index).y);
  4569. } else {
  4570. gl.advance = get_hex_code_box_size(fs, gl.index).y;
  4571. gl.y_off = get_hex_code_box_size(fs, gl.index).y;
  4572. gl.x_off = -Math::round(get_hex_code_box_size(fs, gl.index).x * 0.5);
  4573. p_sd->ascent = MAX(p_sd->ascent, Math::round(get_hex_code_box_size(fs, gl.index).x * 0.5));
  4574. p_sd->descent = MAX(p_sd->descent, Math::round(get_hex_code_box_size(fs, gl.index).x * 0.5));
  4575. }
  4576. p_sd->width += gl.advance;
  4577. p_sd->glyphs.push_back(gl);
  4578. }
  4579. }
  4580. return;
  4581. }
  4582. FontAdvanced *fd = font_owner.get_or_null(f);
  4583. ERR_FAIL_COND(!fd);
  4584. MutexLock lock(fd->mutex);
  4585. Vector2i fss = _get_size(fd, fs);
  4586. hb_font_t *hb_font = _font_get_hb_handle(f, fs);
  4587. double scale = _font_get_scale(f, fs);
  4588. double sp_sp = p_sd->extra_spacing[SPACING_SPACE];
  4589. double sp_gl = p_sd->extra_spacing[SPACING_GLYPH];
  4590. bool last_run = (p_sd->end == p_end);
  4591. double ea = _get_extra_advance(f, fs);
  4592. bool subpos = (scale != 1.0) || (_font_get_subpixel_positioning(f) == SUBPIXEL_POSITIONING_ONE_HALF) || (_font_get_subpixel_positioning(f) == SUBPIXEL_POSITIONING_ONE_QUARTER) || (_font_get_subpixel_positioning(f) == SUBPIXEL_POSITIONING_AUTO && fs <= SUBPIXEL_POSITIONING_ONE_HALF_MAX_SIZE);
  4593. ERR_FAIL_COND(hb_font == nullptr);
  4594. hb_buffer_clear_contents(p_sd->hb_buffer);
  4595. hb_buffer_set_direction(p_sd->hb_buffer, p_direction);
  4596. int flags = (p_start == 0 ? HB_BUFFER_FLAG_BOT : 0) | (p_end == p_sd->text.length() ? HB_BUFFER_FLAG_EOT : 0);
  4597. if (p_sd->preserve_control) {
  4598. flags |= HB_BUFFER_FLAG_PRESERVE_DEFAULT_IGNORABLES;
  4599. } else {
  4600. flags |= HB_BUFFER_FLAG_DEFAULT;
  4601. }
  4602. #if HB_VERSION_ATLEAST(5, 1, 0)
  4603. flags |= HB_BUFFER_FLAG_PRODUCE_SAFE_TO_INSERT_TATWEEL;
  4604. #endif
  4605. hb_buffer_set_flags(p_sd->hb_buffer, (hb_buffer_flags_t)flags);
  4606. hb_buffer_set_script(p_sd->hb_buffer, p_script);
  4607. if (p_sd->spans[p_span].language.is_empty()) {
  4608. hb_language_t lang = hb_language_from_string(TranslationServer::get_singleton()->get_tool_locale().ascii().get_data(), -1);
  4609. hb_buffer_set_language(p_sd->hb_buffer, lang);
  4610. } else {
  4611. hb_language_t lang = hb_language_from_string(p_sd->spans[p_span].language.ascii().get_data(), -1);
  4612. hb_buffer_set_language(p_sd->hb_buffer, lang);
  4613. }
  4614. hb_buffer_add_utf32(p_sd->hb_buffer, (const uint32_t *)p_sd->text.ptr(), p_sd->text.length(), p_start, p_end - p_start);
  4615. Vector<hb_feature_t> ftrs;
  4616. _add_featuers(_font_get_opentype_feature_overrides(f), ftrs);
  4617. _add_featuers(p_sd->spans[p_span].features, ftrs);
  4618. hb_shape(hb_font, p_sd->hb_buffer, ftrs.is_empty() ? nullptr : &ftrs[0], ftrs.size());
  4619. unsigned int glyph_count = 0;
  4620. hb_glyph_info_t *glyph_info = hb_buffer_get_glyph_infos(p_sd->hb_buffer, &glyph_count);
  4621. hb_glyph_position_t *glyph_pos = hb_buffer_get_glyph_positions(p_sd->hb_buffer, &glyph_count);
  4622. int mod = 0;
  4623. if (fd->antialiasing == FONT_ANTIALIASING_LCD) {
  4624. TextServer::FontLCDSubpixelLayout layout = (TextServer::FontLCDSubpixelLayout)(int)GLOBAL_GET("gui/theme/lcd_subpixel_layout");
  4625. if (layout != FONT_LCD_SUBPIXEL_LAYOUT_NONE) {
  4626. mod = (layout << 24);
  4627. }
  4628. }
  4629. // Process glyphs.
  4630. if (glyph_count > 0) {
  4631. Glyph *w = (Glyph *)memalloc(glyph_count * sizeof(Glyph));
  4632. int end = (p_direction == HB_DIRECTION_RTL || p_direction == HB_DIRECTION_BTT) ? p_end : 0;
  4633. uint32_t last_cluster_id = UINT32_MAX;
  4634. unsigned int last_cluster_index = 0;
  4635. bool last_cluster_valid = true;
  4636. for (unsigned int i = 0; i < glyph_count; i++) {
  4637. if ((i > 0) && (last_cluster_id != glyph_info[i].cluster)) {
  4638. if (p_direction == HB_DIRECTION_RTL || p_direction == HB_DIRECTION_BTT) {
  4639. end = w[last_cluster_index].start;
  4640. } else {
  4641. for (unsigned int j = last_cluster_index; j < i; j++) {
  4642. w[j].end = glyph_info[i].cluster;
  4643. }
  4644. }
  4645. if (p_direction == HB_DIRECTION_RTL || p_direction == HB_DIRECTION_BTT) {
  4646. w[last_cluster_index].flags |= GRAPHEME_IS_RTL;
  4647. }
  4648. if (last_cluster_valid) {
  4649. w[last_cluster_index].flags |= GRAPHEME_IS_VALID;
  4650. }
  4651. w[last_cluster_index].count = i - last_cluster_index;
  4652. last_cluster_index = i;
  4653. last_cluster_valid = true;
  4654. }
  4655. last_cluster_id = glyph_info[i].cluster;
  4656. Glyph &gl = w[i];
  4657. gl = Glyph();
  4658. gl.start = glyph_info[i].cluster;
  4659. gl.end = end;
  4660. gl.count = 0;
  4661. gl.font_rid = f;
  4662. gl.font_size = fs;
  4663. if (glyph_info[i].mask & HB_GLYPH_FLAG_UNSAFE_TO_BREAK) {
  4664. gl.flags |= GRAPHEME_IS_CONNECTED;
  4665. }
  4666. #if HB_VERSION_ATLEAST(5, 1, 0)
  4667. if (glyph_info[i].mask & HB_GLYPH_FLAG_SAFE_TO_INSERT_TATWEEL) {
  4668. gl.flags |= GRAPHEME_IS_SAFE_TO_INSERT_TATWEEL;
  4669. }
  4670. #endif
  4671. gl.index = glyph_info[i].codepoint;
  4672. if (gl.index != 0) {
  4673. _ensure_glyph(fd, fss, gl.index | mod);
  4674. if (p_sd->orientation == ORIENTATION_HORIZONTAL) {
  4675. if (subpos) {
  4676. gl.advance = (double)glyph_pos[i].x_advance / (64.0 / scale) + ea;
  4677. } else {
  4678. gl.advance = Math::round((double)glyph_pos[i].x_advance / (64.0 / scale) + ea);
  4679. }
  4680. } else {
  4681. gl.advance = -Math::round((double)glyph_pos[i].y_advance / (64.0 / scale));
  4682. }
  4683. if (subpos) {
  4684. gl.x_off = (double)glyph_pos[i].x_offset / (64.0 / scale);
  4685. } else {
  4686. gl.x_off = Math::round((double)glyph_pos[i].x_offset / (64.0 / scale));
  4687. }
  4688. gl.y_off = -Math::round((double)glyph_pos[i].y_offset / (64.0 / scale));
  4689. }
  4690. if (!last_run || i < glyph_count - 1) {
  4691. // Do not add extra spacing to the last glyph of the string.
  4692. if (sp_sp && is_whitespace(p_sd->text[glyph_info[i].cluster])) {
  4693. gl.advance += sp_sp;
  4694. } else {
  4695. gl.advance += sp_gl;
  4696. }
  4697. }
  4698. if (p_sd->preserve_control) {
  4699. last_cluster_valid = last_cluster_valid && ((glyph_info[i].codepoint != 0) || (p_sd->text[glyph_info[i].cluster] == 0x0009) || (u_isblank(p_sd->text[glyph_info[i].cluster]) && (gl.advance != 0)) || (!u_isblank(p_sd->text[glyph_info[i].cluster]) && is_linebreak(p_sd->text[glyph_info[i].cluster])));
  4700. } else {
  4701. last_cluster_valid = last_cluster_valid && ((glyph_info[i].codepoint != 0) || (p_sd->text[glyph_info[i].cluster] == 0x0009) || (u_isblank(p_sd->text[glyph_info[i].cluster]) && (gl.advance != 0)) || (!u_isblank(p_sd->text[glyph_info[i].cluster]) && !u_isgraph(p_sd->text[glyph_info[i].cluster])));
  4702. }
  4703. }
  4704. if (p_direction == HB_DIRECTION_LTR || p_direction == HB_DIRECTION_TTB) {
  4705. for (unsigned int j = last_cluster_index; j < glyph_count; j++) {
  4706. w[j].end = p_end;
  4707. }
  4708. }
  4709. w[last_cluster_index].count = glyph_count - last_cluster_index;
  4710. if (p_direction == HB_DIRECTION_RTL || p_direction == HB_DIRECTION_BTT) {
  4711. w[last_cluster_index].flags |= GRAPHEME_IS_RTL;
  4712. }
  4713. if (last_cluster_valid) {
  4714. w[last_cluster_index].flags |= GRAPHEME_IS_VALID;
  4715. }
  4716. // Fallback.
  4717. int failed_subrun_start = p_end + 1;
  4718. int failed_subrun_end = p_start;
  4719. for (unsigned int i = 0; i < glyph_count; i++) {
  4720. if ((w[i].flags & GRAPHEME_IS_VALID) == GRAPHEME_IS_VALID) {
  4721. if (failed_subrun_start != p_end + 1) {
  4722. _shape_run(p_sd, failed_subrun_start, failed_subrun_end, p_script, p_direction, p_fonts, p_span, p_fb_index + 1, p_start, p_end);
  4723. failed_subrun_start = p_end + 1;
  4724. failed_subrun_end = p_start;
  4725. }
  4726. for (int j = 0; j < w[i].count; j++) {
  4727. if (p_sd->orientation == ORIENTATION_HORIZONTAL) {
  4728. p_sd->ascent = MAX(p_sd->ascent, -w[i + j].y_off);
  4729. p_sd->descent = MAX(p_sd->descent, w[i + j].y_off);
  4730. } else {
  4731. double gla = Math::round(_font_get_glyph_advance(f, fs, w[i + j].index).x * 0.5);
  4732. p_sd->ascent = MAX(p_sd->ascent, gla);
  4733. p_sd->descent = MAX(p_sd->descent, gla);
  4734. }
  4735. p_sd->width += w[i + j].advance;
  4736. w[i + j].start += p_sd->start;
  4737. w[i + j].end += p_sd->start;
  4738. p_sd->glyphs.push_back(w[i + j]);
  4739. }
  4740. } else {
  4741. if (failed_subrun_start >= w[i].start) {
  4742. failed_subrun_start = w[i].start;
  4743. }
  4744. if (failed_subrun_end <= w[i].end) {
  4745. failed_subrun_end = w[i].end;
  4746. }
  4747. }
  4748. i += w[i].count - 1;
  4749. }
  4750. memfree(w);
  4751. if (failed_subrun_start != p_end + 1) {
  4752. _shape_run(p_sd, failed_subrun_start, failed_subrun_end, p_script, p_direction, p_fonts, p_span, p_fb_index + 1, p_start, p_end);
  4753. }
  4754. p_sd->ascent = MAX(p_sd->ascent, _font_get_ascent(f, fs));
  4755. p_sd->descent = MAX(p_sd->descent, _font_get_descent(f, fs));
  4756. p_sd->upos = MAX(p_sd->upos, _font_get_underline_position(f, fs));
  4757. p_sd->uthk = MAX(p_sd->uthk, _font_get_underline_thickness(f, fs));
  4758. }
  4759. }
  4760. bool TextServerAdvanced::_shaped_text_shape(const RID &p_shaped) {
  4761. ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
  4762. ERR_FAIL_COND_V(!sd, false);
  4763. MutexLock lock(sd->mutex);
  4764. if (sd->valid) {
  4765. return true;
  4766. }
  4767. invalidate(sd, false);
  4768. if (sd->parent != RID()) {
  4769. _shaped_text_shape(sd->parent);
  4770. ShapedTextDataAdvanced *parent_sd = shaped_owner.get_or_null(sd->parent);
  4771. ERR_FAIL_COND_V(!parent_sd->valid, false);
  4772. ERR_FAIL_COND_V(!_shape_substr(sd, parent_sd, sd->start, sd->end - sd->start), false);
  4773. return true;
  4774. }
  4775. if (sd->text.length() == 0) {
  4776. sd->valid = true;
  4777. return true;
  4778. }
  4779. sd->utf16 = sd->text.utf16();
  4780. const UChar *data = sd->utf16.get_data();
  4781. // Create script iterator.
  4782. if (sd->script_iter == nullptr) {
  4783. sd->script_iter = memnew(ScriptIterator(sd->text, 0, sd->text.length()));
  4784. }
  4785. if (sd->bidi_override.is_empty()) {
  4786. sd->bidi_override.push_back(Vector2i(sd->start, sd->end));
  4787. }
  4788. for (int ov = 0; ov < sd->bidi_override.size(); ov++) {
  4789. // Create BiDi iterator.
  4790. int start = _convert_pos_inv(sd, sd->bidi_override[ov].x - sd->start);
  4791. int end = _convert_pos_inv(sd, sd->bidi_override[ov].y - sd->start);
  4792. if (start < 0 || end - start > sd->utf16.length()) {
  4793. continue;
  4794. }
  4795. UErrorCode err = U_ZERO_ERROR;
  4796. UBiDi *bidi_iter = ubidi_openSized(end, 0, &err);
  4797. ERR_FAIL_COND_V_MSG(U_FAILURE(err), false, u_errorName(err));
  4798. switch (sd->direction) {
  4799. case DIRECTION_LTR: {
  4800. ubidi_setPara(bidi_iter, data + start, end - start, UBIDI_LTR, nullptr, &err);
  4801. sd->para_direction = DIRECTION_LTR;
  4802. } break;
  4803. case DIRECTION_RTL: {
  4804. ubidi_setPara(bidi_iter, data + start, end - start, UBIDI_RTL, nullptr, &err);
  4805. sd->para_direction = DIRECTION_RTL;
  4806. } break;
  4807. case DIRECTION_AUTO: {
  4808. UBiDiDirection direction = ubidi_getBaseDirection(data + start, end - start);
  4809. if (direction != UBIDI_NEUTRAL) {
  4810. ubidi_setPara(bidi_iter, data + start, end - start, direction, nullptr, &err);
  4811. sd->para_direction = (direction == UBIDI_RTL) ? DIRECTION_RTL : DIRECTION_LTR;
  4812. } else {
  4813. ubidi_setPara(bidi_iter, data + start, end - start, UBIDI_DEFAULT_LTR, nullptr, &err);
  4814. sd->para_direction = DIRECTION_LTR;
  4815. }
  4816. } break;
  4817. }
  4818. ERR_FAIL_COND_V_MSG(U_FAILURE(err), false, u_errorName(err));
  4819. sd->bidi_iter.push_back(bidi_iter);
  4820. err = U_ZERO_ERROR;
  4821. int bidi_run_count = ubidi_countRuns(bidi_iter, &err);
  4822. ERR_FAIL_COND_V_MSG(U_FAILURE(err), false, u_errorName(err));
  4823. for (int i = 0; i < bidi_run_count; i++) {
  4824. int32_t _bidi_run_start = 0;
  4825. int32_t _bidi_run_length = 0;
  4826. hb_direction_t bidi_run_direction = HB_DIRECTION_INVALID;
  4827. bool is_rtl = (ubidi_getVisualRun(bidi_iter, i, &_bidi_run_start, &_bidi_run_length) == UBIDI_LTR);
  4828. switch (sd->orientation) {
  4829. case ORIENTATION_HORIZONTAL: {
  4830. if (is_rtl) {
  4831. bidi_run_direction = HB_DIRECTION_LTR;
  4832. } else {
  4833. bidi_run_direction = HB_DIRECTION_RTL;
  4834. }
  4835. } break;
  4836. case ORIENTATION_VERTICAL: {
  4837. if (is_rtl) {
  4838. bidi_run_direction = HB_DIRECTION_TTB;
  4839. } else {
  4840. bidi_run_direction = HB_DIRECTION_BTT;
  4841. }
  4842. }
  4843. }
  4844. int32_t bidi_run_start = _convert_pos(sd, sd->bidi_override[ov].x - sd->start + _bidi_run_start);
  4845. int32_t bidi_run_end = _convert_pos(sd, sd->bidi_override[ov].x - sd->start + _bidi_run_start + _bidi_run_length);
  4846. // Shape runs.
  4847. int scr_from = (is_rtl) ? 0 : sd->script_iter->script_ranges.size() - 1;
  4848. int scr_to = (is_rtl) ? sd->script_iter->script_ranges.size() : -1;
  4849. int scr_delta = (is_rtl) ? +1 : -1;
  4850. for (int j = scr_from; j != scr_to; j += scr_delta) {
  4851. if ((sd->script_iter->script_ranges[j].start < bidi_run_end) && (sd->script_iter->script_ranges[j].end > bidi_run_start)) {
  4852. int32_t script_run_start = MAX(sd->script_iter->script_ranges[j].start, bidi_run_start);
  4853. int32_t script_run_end = MIN(sd->script_iter->script_ranges[j].end, bidi_run_end);
  4854. char scr_buffer[5] = { 0, 0, 0, 0, 0 };
  4855. hb_tag_to_string(hb_script_to_iso15924_tag(sd->script_iter->script_ranges[j].script), scr_buffer);
  4856. String script_code = String(scr_buffer);
  4857. int spn_from = (is_rtl) ? 0 : sd->spans.size() - 1;
  4858. int spn_to = (is_rtl) ? sd->spans.size() : -1;
  4859. int spn_delta = (is_rtl) ? +1 : -1;
  4860. for (int k = spn_from; k != spn_to; k += spn_delta) {
  4861. const ShapedTextDataAdvanced::Span &span = sd->spans[k];
  4862. if (span.start - sd->start >= script_run_end || span.end - sd->start <= script_run_start) {
  4863. continue;
  4864. }
  4865. if (span.embedded_key != Variant()) {
  4866. // Embedded object.
  4867. if (sd->orientation == ORIENTATION_HORIZONTAL) {
  4868. sd->objects[span.embedded_key].rect.position.x = sd->width;
  4869. sd->width += sd->objects[span.embedded_key].rect.size.x;
  4870. } else {
  4871. sd->objects[span.embedded_key].rect.position.y = sd->width;
  4872. sd->width += sd->objects[span.embedded_key].rect.size.y;
  4873. }
  4874. Glyph gl;
  4875. gl.start = span.start;
  4876. gl.end = span.end;
  4877. gl.count = 1;
  4878. gl.flags = GRAPHEME_IS_VALID | GRAPHEME_IS_VIRTUAL;
  4879. if (sd->orientation == ORIENTATION_HORIZONTAL) {
  4880. gl.advance = sd->objects[span.embedded_key].rect.size.x;
  4881. } else {
  4882. gl.advance = sd->objects[span.embedded_key].rect.size.y;
  4883. }
  4884. sd->glyphs.push_back(gl);
  4885. } else {
  4886. Array fonts;
  4887. Array fonts_scr_only;
  4888. Array fonts_no_match;
  4889. int font_count = span.fonts.size();
  4890. if (font_count > 0) {
  4891. fonts.push_back(sd->spans[k].fonts[0]);
  4892. }
  4893. for (int l = 1; l < font_count; l++) {
  4894. if (_font_is_script_supported(span.fonts[l], script_code)) {
  4895. if (_font_is_language_supported(span.fonts[l], span.language)) {
  4896. fonts.push_back(sd->spans[k].fonts[l]);
  4897. } else {
  4898. fonts_scr_only.push_back(sd->spans[k].fonts[l]);
  4899. }
  4900. } else {
  4901. fonts_no_match.push_back(sd->spans[k].fonts[l]);
  4902. }
  4903. }
  4904. fonts.append_array(fonts_scr_only);
  4905. fonts.append_array(fonts_no_match);
  4906. _shape_run(sd, MAX(sd->spans[k].start - sd->start, script_run_start), MIN(sd->spans[k].end - sd->start, script_run_end), sd->script_iter->script_ranges[j].script, bidi_run_direction, fonts, k, 0, 0, 0);
  4907. }
  4908. }
  4909. }
  4910. }
  4911. }
  4912. }
  4913. _realign(sd);
  4914. sd->valid = true;
  4915. return sd->valid;
  4916. }
  4917. bool TextServerAdvanced::_shaped_text_is_ready(const RID &p_shaped) const {
  4918. const ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
  4919. ERR_FAIL_COND_V(!sd, false);
  4920. MutexLock lock(sd->mutex);
  4921. return sd->valid;
  4922. }
  4923. const Glyph *TextServerAdvanced::_shaped_text_get_glyphs(const RID &p_shaped) const {
  4924. const ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
  4925. ERR_FAIL_COND_V(!sd, nullptr);
  4926. MutexLock lock(sd->mutex);
  4927. if (!sd->valid) {
  4928. const_cast<TextServerAdvanced *>(this)->_shaped_text_shape(p_shaped);
  4929. }
  4930. return sd->glyphs.ptr();
  4931. }
  4932. int64_t TextServerAdvanced::_shaped_text_get_glyph_count(const RID &p_shaped) const {
  4933. const ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
  4934. ERR_FAIL_COND_V(!sd, 0);
  4935. MutexLock lock(sd->mutex);
  4936. if (!sd->valid) {
  4937. const_cast<TextServerAdvanced *>(this)->_shaped_text_shape(p_shaped);
  4938. }
  4939. return sd->glyphs.size();
  4940. }
  4941. const Glyph *TextServerAdvanced::_shaped_text_sort_logical(const RID &p_shaped) {
  4942. ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
  4943. ERR_FAIL_COND_V(!sd, nullptr);
  4944. MutexLock lock(sd->mutex);
  4945. if (!sd->valid) {
  4946. const_cast<TextServerAdvanced *>(this)->_shaped_text_shape(p_shaped);
  4947. }
  4948. if (!sd->sort_valid) {
  4949. sd->glyphs_logical = sd->glyphs;
  4950. sd->glyphs_logical.sort_custom<GlyphCompare>();
  4951. sd->sort_valid = true;
  4952. }
  4953. return sd->glyphs_logical.ptr();
  4954. }
  4955. Vector2i TextServerAdvanced::_shaped_text_get_range(const RID &p_shaped) const {
  4956. const ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
  4957. ERR_FAIL_COND_V(!sd, Vector2i());
  4958. MutexLock lock(sd->mutex);
  4959. return Vector2(sd->start, sd->end);
  4960. }
  4961. Array TextServerAdvanced::_shaped_text_get_objects(const RID &p_shaped) const {
  4962. Array ret;
  4963. const ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
  4964. ERR_FAIL_COND_V(!sd, ret);
  4965. MutexLock lock(sd->mutex);
  4966. for (const KeyValue<Variant, ShapedTextDataAdvanced::EmbeddedObject> &E : sd->objects) {
  4967. ret.push_back(E.key);
  4968. }
  4969. return ret;
  4970. }
  4971. Rect2 TextServerAdvanced::_shaped_text_get_object_rect(const RID &p_shaped, const Variant &p_key) const {
  4972. const ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
  4973. ERR_FAIL_COND_V(!sd, Rect2());
  4974. MutexLock lock(sd->mutex);
  4975. ERR_FAIL_COND_V(!sd->objects.has(p_key), Rect2());
  4976. if (!sd->valid) {
  4977. const_cast<TextServerAdvanced *>(this)->_shaped_text_shape(p_shaped);
  4978. }
  4979. return sd->objects[p_key].rect;
  4980. }
  4981. Size2 TextServerAdvanced::_shaped_text_get_size(const RID &p_shaped) const {
  4982. const ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
  4983. ERR_FAIL_COND_V(!sd, Size2());
  4984. MutexLock lock(sd->mutex);
  4985. if (!sd->valid) {
  4986. const_cast<TextServerAdvanced *>(this)->_shaped_text_shape(p_shaped);
  4987. }
  4988. if (sd->orientation == TextServer::ORIENTATION_HORIZONTAL) {
  4989. return Size2((sd->text_trimmed ? sd->width_trimmed : sd->width), sd->ascent + sd->descent + sd->extra_spacing[SPACING_TOP] + sd->extra_spacing[SPACING_BOTTOM]).ceil();
  4990. } else {
  4991. return Size2(sd->ascent + sd->descent + sd->extra_spacing[SPACING_TOP] + sd->extra_spacing[SPACING_BOTTOM], (sd->text_trimmed ? sd->width_trimmed : sd->width)).ceil();
  4992. }
  4993. }
  4994. double TextServerAdvanced::_shaped_text_get_ascent(const RID &p_shaped) const {
  4995. const ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
  4996. ERR_FAIL_COND_V(!sd, 0.0);
  4997. MutexLock lock(sd->mutex);
  4998. if (!sd->valid) {
  4999. const_cast<TextServerAdvanced *>(this)->_shaped_text_shape(p_shaped);
  5000. }
  5001. return sd->ascent + sd->extra_spacing[SPACING_TOP];
  5002. }
  5003. double TextServerAdvanced::_shaped_text_get_descent(const RID &p_shaped) const {
  5004. const ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
  5005. ERR_FAIL_COND_V(!sd, 0.0);
  5006. MutexLock lock(sd->mutex);
  5007. if (!sd->valid) {
  5008. const_cast<TextServerAdvanced *>(this)->_shaped_text_shape(p_shaped);
  5009. }
  5010. return sd->descent + sd->extra_spacing[SPACING_BOTTOM];
  5011. }
  5012. double TextServerAdvanced::_shaped_text_get_width(const RID &p_shaped) const {
  5013. const ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
  5014. ERR_FAIL_COND_V(!sd, 0.0);
  5015. MutexLock lock(sd->mutex);
  5016. if (!sd->valid) {
  5017. const_cast<TextServerAdvanced *>(this)->_shaped_text_shape(p_shaped);
  5018. }
  5019. return Math::ceil(sd->text_trimmed ? sd->width_trimmed : sd->width);
  5020. }
  5021. double TextServerAdvanced::_shaped_text_get_underline_position(const RID &p_shaped) const {
  5022. const ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
  5023. ERR_FAIL_COND_V(!sd, 0.0);
  5024. MutexLock lock(sd->mutex);
  5025. if (!sd->valid) {
  5026. const_cast<TextServerAdvanced *>(this)->_shaped_text_shape(p_shaped);
  5027. }
  5028. return sd->upos;
  5029. }
  5030. double TextServerAdvanced::_shaped_text_get_underline_thickness(const RID &p_shaped) const {
  5031. const ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped);
  5032. ERR_FAIL_COND_V(!sd, 0.0);
  5033. MutexLock lock(sd->mutex);
  5034. if (!sd->valid) {
  5035. const_cast<TextServerAdvanced *>(this)->_shaped_text_shape(p_shaped);
  5036. }
  5037. return sd->uthk;
  5038. }
  5039. void TextServerAdvanced::_insert_num_systems_lang() {
  5040. // Eastern Arabic numerals.
  5041. {
  5042. NumSystemData ar;
  5043. ar.lang.insert(StringName("ar")); // Arabic
  5044. ar.lang.insert(StringName("ar_AE"));
  5045. ar.lang.insert(StringName("ar_BH"));
  5046. ar.lang.insert(StringName("ar_DJ"));
  5047. ar.lang.insert(StringName("ar_EG"));
  5048. ar.lang.insert(StringName("ar_ER"));
  5049. ar.lang.insert(StringName("ar_IL"));
  5050. ar.lang.insert(StringName("ar_IQ"));
  5051. ar.lang.insert(StringName("ar_JO"));
  5052. ar.lang.insert(StringName("ar_KM"));
  5053. ar.lang.insert(StringName("ar_KW"));
  5054. ar.lang.insert(StringName("ar_LB"));
  5055. ar.lang.insert(StringName("ar_MR"));
  5056. ar.lang.insert(StringName("ar_OM"));
  5057. ar.lang.insert(StringName("ar_PS"));
  5058. ar.lang.insert(StringName("ar_QA"));
  5059. ar.lang.insert(StringName("ar_SA"));
  5060. ar.lang.insert(StringName("ar_SD"));
  5061. ar.lang.insert(StringName("ar_SO"));
  5062. ar.lang.insert(StringName("ar_SS"));
  5063. ar.lang.insert(StringName("ar_SY"));
  5064. ar.lang.insert(StringName("ar_TD"));
  5065. ar.lang.insert(StringName("ar_YE"));
  5066. ar.lang.insert(StringName("ckb")); // Central Kurdish
  5067. ar.lang.insert(StringName("ckb_IQ"));
  5068. ar.lang.insert(StringName("ckb_IR"));
  5069. ar.lang.insert(StringName("sd")); // Sindhi
  5070. ar.lang.insert(StringName("sd_PK"));
  5071. ar.lang.insert(StringName("sd_Arab"));
  5072. ar.lang.insert(StringName("sd_Arab_PK"));
  5073. ar.digits = U"٠١٢٣٤٥٦٧٨٩٫";
  5074. ar.percent_sign = U"٪";
  5075. ar.exp = U"اس";
  5076. num_systems.push_back(ar);
  5077. }
  5078. // Persian and Urdu numerals.
  5079. {
  5080. NumSystemData pr;
  5081. pr.lang.insert(StringName("fa")); // Persian
  5082. pr.lang.insert(StringName("fa_AF"));
  5083. pr.lang.insert(StringName("fa_IR"));
  5084. pr.lang.insert(StringName("ks")); // Kashmiri
  5085. pr.lang.insert(StringName("ks_IN"));
  5086. pr.lang.insert(StringName("ks_Arab"));
  5087. pr.lang.insert(StringName("ks_Arab_IN"));
  5088. pr.lang.insert(StringName("lrc")); // Northern Luri
  5089. pr.lang.insert(StringName("lrc_IQ"));
  5090. pr.lang.insert(StringName("lrc_IR"));
  5091. pr.lang.insert(StringName("mzn")); // Mazanderani
  5092. pr.lang.insert(StringName("mzn_IR"));
  5093. pr.lang.insert(StringName("pa_PK")); // Panjabi
  5094. pr.lang.insert(StringName("pa_Arab"));
  5095. pr.lang.insert(StringName("pa_Arab_PK"));
  5096. pr.lang.insert(StringName("ps")); // Pushto
  5097. pr.lang.insert(StringName("ps_AF"));
  5098. pr.lang.insert(StringName("ps_PK"));
  5099. pr.lang.insert(StringName("ur_IN")); // Urdu
  5100. pr.lang.insert(StringName("uz_AF")); // Uzbek
  5101. pr.lang.insert(StringName("uz_Arab"));
  5102. pr.lang.insert(StringName("uz_Arab_AF"));
  5103. pr.digits = U"۰۱۲۳۴۵۶۷۸۹٫";
  5104. pr.percent_sign = U"٪";
  5105. pr.exp = U"اس";
  5106. num_systems.push_back(pr);
  5107. }
  5108. // Bengali numerals.
  5109. {
  5110. NumSystemData bn;
  5111. bn.lang.insert(StringName("as")); // Assamese
  5112. bn.lang.insert(StringName("as_IN"));
  5113. bn.lang.insert(StringName("bn")); // Bengali
  5114. bn.lang.insert(StringName("bn_BD"));
  5115. bn.lang.insert(StringName("bn_IN"));
  5116. bn.lang.insert(StringName("mni")); // Manipuri
  5117. bn.lang.insert(StringName("mni_IN"));
  5118. bn.lang.insert(StringName("mni_Beng"));
  5119. bn.lang.insert(StringName("mni_Beng_IN"));
  5120. bn.digits = U"০১২৩৪৫৬৭৮৯.";
  5121. bn.percent_sign = U"%";
  5122. bn.exp = U"e";
  5123. num_systems.push_back(bn);
  5124. }
  5125. // Devanagari numerals.
  5126. {
  5127. NumSystemData mr;
  5128. mr.lang.insert(StringName("mr")); // Marathi
  5129. mr.lang.insert(StringName("mr_IN"));
  5130. mr.lang.insert(StringName("ne")); // Nepali
  5131. mr.lang.insert(StringName("ne_IN"));
  5132. mr.lang.insert(StringName("ne_NP"));
  5133. mr.lang.insert(StringName("sa")); // Sanskrit
  5134. mr.lang.insert(StringName("sa_IN"));
  5135. mr.digits = U"०१२३४५६७८९.";
  5136. mr.percent_sign = U"%";
  5137. mr.exp = U"e";
  5138. num_systems.push_back(mr);
  5139. }
  5140. // Dzongkha numerals.
  5141. {
  5142. NumSystemData dz;
  5143. dz.lang.insert(StringName("dz")); // Dzongkha
  5144. dz.lang.insert(StringName("dz_BT"));
  5145. dz.digits = U"༠༡༢༣༤༥༦༧༨༩.";
  5146. dz.percent_sign = U"%";
  5147. dz.exp = U"e";
  5148. num_systems.push_back(dz);
  5149. }
  5150. // Santali numerals.
  5151. {
  5152. NumSystemData sat;
  5153. sat.lang.insert(StringName("sat")); // Santali
  5154. sat.lang.insert(StringName("sat_IN"));
  5155. sat.lang.insert(StringName("sat_Olck"));
  5156. sat.lang.insert(StringName("sat_Olck_IN"));
  5157. sat.digits = U"᱐᱑᱒᱓᱔᱕᱖᱗᱘᱙.";
  5158. sat.percent_sign = U"%";
  5159. sat.exp = U"e";
  5160. num_systems.push_back(sat);
  5161. }
  5162. // Burmese numerals.
  5163. {
  5164. NumSystemData my;
  5165. my.lang.insert(StringName("my")); // Burmese
  5166. my.lang.insert(StringName("my_MM"));
  5167. my.digits = U"၀၁၂၃၄၅၆၇၈၉.";
  5168. my.percent_sign = U"%";
  5169. my.exp = U"e";
  5170. num_systems.push_back(my);
  5171. }
  5172. // Chakma numerals.
  5173. {
  5174. NumSystemData ccp;
  5175. ccp.lang.insert(StringName("ccp")); // Chakma
  5176. ccp.lang.insert(StringName("ccp_BD"));
  5177. ccp.lang.insert(StringName("ccp_IN"));
  5178. ccp.digits = U"𑄶𑄷𑄸𑄹𑄺𑄻𑄼𑄽𑄾𑄿.";
  5179. ccp.percent_sign = U"%";
  5180. ccp.exp = U"e";
  5181. num_systems.push_back(ccp);
  5182. }
  5183. // Adlam numerals.
  5184. {
  5185. NumSystemData ff;
  5186. ff.lang.insert(StringName("ff")); // Fulah
  5187. ff.lang.insert(StringName("ff_Adlm_BF"));
  5188. ff.lang.insert(StringName("ff_Adlm_CM"));
  5189. ff.lang.insert(StringName("ff_Adlm_GH"));
  5190. ff.lang.insert(StringName("ff_Adlm_GM"));
  5191. ff.lang.insert(StringName("ff_Adlm_GN"));
  5192. ff.lang.insert(StringName("ff_Adlm_GW"));
  5193. ff.lang.insert(StringName("ff_Adlm_LR"));
  5194. ff.lang.insert(StringName("ff_Adlm_MR"));
  5195. ff.lang.insert(StringName("ff_Adlm_NE"));
  5196. ff.lang.insert(StringName("ff_Adlm_NG"));
  5197. ff.lang.insert(StringName("ff_Adlm_SL"));
  5198. ff.lang.insert(StringName("ff_Adlm_SN"));
  5199. ff.digits = U"𞥐𞥑𞥒𞥓𞥔𞥕𞥖𞥗𞥘𞥙.";
  5200. ff.percent_sign = U"%";
  5201. ff.exp = U"e";
  5202. num_systems.push_back(ff);
  5203. }
  5204. }
  5205. String TextServerAdvanced::_format_number(const String &p_string, const String &p_language) const {
  5206. const StringName lang = (p_language.is_empty()) ? TranslationServer::get_singleton()->get_tool_locale() : p_language;
  5207. String res = p_string;
  5208. for (int i = 0; i < num_systems.size(); i++) {
  5209. if (num_systems[i].lang.has(lang)) {
  5210. if (num_systems[i].digits.is_empty()) {
  5211. return p_string;
  5212. }
  5213. res.replace("e", num_systems[i].exp);
  5214. res.replace("E", num_systems[i].exp);
  5215. char32_t *data = res.ptrw();
  5216. for (int j = 0; j < res.length(); j++) {
  5217. if (data[j] >= 0x30 && data[j] <= 0x39) {
  5218. data[j] = num_systems[i].digits[data[j] - 0x30];
  5219. } else if (data[j] == '.' || data[j] == ',') {
  5220. data[j] = num_systems[i].digits[10];
  5221. }
  5222. }
  5223. break;
  5224. }
  5225. }
  5226. return res;
  5227. }
  5228. String TextServerAdvanced::_parse_number(const String &p_string, const String &p_language) const {
  5229. const StringName lang = (p_language.is_empty()) ? TranslationServer::get_singleton()->get_tool_locale() : p_language;
  5230. String res = p_string;
  5231. for (int i = 0; i < num_systems.size(); i++) {
  5232. if (num_systems[i].lang.has(lang)) {
  5233. if (num_systems[i].digits.is_empty()) {
  5234. return p_string;
  5235. }
  5236. res.replace(num_systems[i].exp, "e");
  5237. char32_t *data = res.ptrw();
  5238. for (int j = 0; j < res.length(); j++) {
  5239. if (data[j] == num_systems[i].digits[10]) {
  5240. data[j] = '.';
  5241. } else {
  5242. for (int k = 0; k < 10; k++) {
  5243. if (data[j] == num_systems[i].digits[k]) {
  5244. data[j] = 0x30 + k;
  5245. }
  5246. }
  5247. }
  5248. }
  5249. break;
  5250. }
  5251. }
  5252. return res;
  5253. }
  5254. String TextServerAdvanced::_percent_sign(const String &p_language) const {
  5255. const StringName lang = (p_language.is_empty()) ? TranslationServer::get_singleton()->get_tool_locale() : p_language;
  5256. for (int i = 0; i < num_systems.size(); i++) {
  5257. if (num_systems[i].lang.has(lang)) {
  5258. if (num_systems[i].percent_sign.is_empty()) {
  5259. return "%";
  5260. }
  5261. return num_systems[i].percent_sign;
  5262. }
  5263. }
  5264. return "%";
  5265. }
  5266. int64_t TextServerAdvanced::_is_confusable(const String &p_string, const PackedStringArray &p_dict) const {
  5267. UErrorCode status = U_ZERO_ERROR;
  5268. int64_t match_index = -1;
  5269. Char16String utf16 = p_string.utf16();
  5270. Vector<UChar *> skeletons;
  5271. skeletons.resize(p_dict.size());
  5272. USpoofChecker *sc = uspoof_open(&status);
  5273. uspoof_setChecks(sc, USPOOF_CONFUSABLE, &status);
  5274. for (int i = 0; i < p_dict.size(); i++) {
  5275. Char16String word = p_dict[i].utf16();
  5276. int32_t len = uspoof_getSkeleton(sc, 0, word.get_data(), -1, NULL, 0, &status);
  5277. skeletons.write[i] = (UChar *)memalloc(++len * sizeof(UChar));
  5278. status = U_ZERO_ERROR;
  5279. uspoof_getSkeleton(sc, 0, word.get_data(), -1, skeletons.write[i], len, &status);
  5280. }
  5281. int32_t len = uspoof_getSkeleton(sc, 0, utf16.get_data(), -1, NULL, 0, &status);
  5282. UChar *skel = (UChar *)memalloc(++len * sizeof(UChar));
  5283. status = U_ZERO_ERROR;
  5284. uspoof_getSkeleton(sc, 0, utf16.get_data(), -1, skel, len, &status);
  5285. for (int i = 0; i < skeletons.size(); i++) {
  5286. if (u_strcmp(skel, skeletons[i]) == 0) {
  5287. match_index = i;
  5288. break;
  5289. }
  5290. }
  5291. memfree(skel);
  5292. for (int i = 0; i < skeletons.size(); i++) {
  5293. memfree(skeletons.write[i]);
  5294. }
  5295. uspoof_close(sc);
  5296. ERR_FAIL_COND_V_MSG(U_FAILURE(status), -1, u_errorName(status));
  5297. return match_index;
  5298. }
  5299. bool TextServerAdvanced::_spoof_check(const String &p_string) const {
  5300. UErrorCode status = U_ZERO_ERROR;
  5301. Char16String utf16 = p_string.utf16();
  5302. USet *allowed = uset_openEmpty();
  5303. uset_addAll(allowed, uspoof_getRecommendedSet(&status));
  5304. uset_addAll(allowed, uspoof_getInclusionSet(&status));
  5305. USpoofChecker *sc = uspoof_open(&status);
  5306. uspoof_setAllowedChars(sc, allowed, &status);
  5307. uspoof_setRestrictionLevel(sc, USPOOF_MODERATELY_RESTRICTIVE);
  5308. int32_t bitmask = uspoof_check(sc, utf16.get_data(), -1, NULL, &status);
  5309. uspoof_close(sc);
  5310. uset_close(allowed);
  5311. ERR_FAIL_COND_V_MSG(U_FAILURE(status), false, u_errorName(status));
  5312. return (bitmask != 0);
  5313. }
  5314. String TextServerAdvanced::_strip_diacritics(const String &p_string) const {
  5315. UErrorCode err = U_ZERO_ERROR;
  5316. // Get NFKD normalizer singleton.
  5317. const UNormalizer2 *unorm = unorm2_getNFKDInstance(&err);
  5318. ERR_FAIL_COND_V_MSG(U_FAILURE(err), TextServer::strip_diacritics(p_string), u_errorName(err));
  5319. // Convert to UTF-16.
  5320. Char16String utf16 = p_string.utf16();
  5321. // Normalize.
  5322. Vector<char16_t> normalized;
  5323. err = U_ZERO_ERROR;
  5324. int32_t len = unorm2_normalize(unorm, utf16.get_data(), -1, nullptr, 0, &err);
  5325. ERR_FAIL_COND_V_MSG(err != U_BUFFER_OVERFLOW_ERROR, TextServer::strip_diacritics(p_string), u_errorName(err));
  5326. normalized.resize(len);
  5327. err = U_ZERO_ERROR;
  5328. unorm2_normalize(unorm, utf16.get_data(), -1, normalized.ptrw(), len, &err);
  5329. ERR_FAIL_COND_V_MSG(U_FAILURE(err), TextServer::strip_diacritics(p_string), u_errorName(err));
  5330. // Convert back to UTF-32.
  5331. String normalized_string = String::utf16(normalized.ptr(), len);
  5332. // Strip combining characters.
  5333. String result;
  5334. for (int i = 0; i < normalized_string.length(); i++) {
  5335. if (u_getCombiningClass(normalized_string[i]) == 0) {
  5336. #ifdef GDEXTENSION
  5337. result = result + String::chr(normalized_string[i]);
  5338. #else
  5339. result = result + normalized_string[i];
  5340. #endif
  5341. }
  5342. }
  5343. return result;
  5344. }
  5345. String TextServerAdvanced::_string_to_upper(const String &p_string, const String &p_language) const {
  5346. const String lang = (p_language.is_empty()) ? TranslationServer::get_singleton()->get_tool_locale() : p_language;
  5347. // Convert to UTF-16.
  5348. Char16String utf16 = p_string.utf16();
  5349. Vector<char16_t> upper;
  5350. UErrorCode err = U_ZERO_ERROR;
  5351. int32_t len = u_strToUpper(nullptr, 0, utf16.get_data(), -1, lang.ascii().get_data(), &err);
  5352. ERR_FAIL_COND_V_MSG(err != U_BUFFER_OVERFLOW_ERROR, p_string, u_errorName(err));
  5353. upper.resize(len);
  5354. err = U_ZERO_ERROR;
  5355. u_strToUpper(upper.ptrw(), len, utf16.get_data(), -1, lang.ascii().get_data(), &err);
  5356. ERR_FAIL_COND_V_MSG(U_FAILURE(err), p_string, u_errorName(err));
  5357. // Convert back to UTF-32.
  5358. return String::utf16(upper.ptr(), len);
  5359. }
  5360. String TextServerAdvanced::_string_to_lower(const String &p_string, const String &p_language) const {
  5361. const String lang = (p_language.is_empty()) ? TranslationServer::get_singleton()->get_tool_locale() : p_language;
  5362. // Convert to UTF-16.
  5363. Char16String utf16 = p_string.utf16();
  5364. Vector<char16_t> lower;
  5365. UErrorCode err = U_ZERO_ERROR;
  5366. int32_t len = u_strToLower(nullptr, 0, utf16.get_data(), -1, lang.ascii().get_data(), &err);
  5367. ERR_FAIL_COND_V_MSG(err != U_BUFFER_OVERFLOW_ERROR, p_string, u_errorName(err));
  5368. lower.resize(len);
  5369. err = U_ZERO_ERROR;
  5370. u_strToLower(lower.ptrw(), len, utf16.get_data(), -1, lang.ascii().get_data(), &err);
  5371. ERR_FAIL_COND_V_MSG(U_FAILURE(err), p_string, u_errorName(err));
  5372. // Convert back to UTF-32.
  5373. return String::utf16(lower.ptr(), len);
  5374. }
  5375. PackedInt32Array TextServerAdvanced::_string_get_word_breaks(const String &p_string, const String &p_language) const {
  5376. const String lang = (p_language.is_empty()) ? TranslationServer::get_singleton()->get_tool_locale() : p_language;
  5377. // Convert to UTF-16.
  5378. Char16String utf16 = p_string.utf16();
  5379. HashSet<int> breaks;
  5380. UErrorCode err = U_ZERO_ERROR;
  5381. UBreakIterator *bi = ubrk_open(UBRK_LINE, lang.ascii().get_data(), (const UChar *)utf16.get_data(), utf16.length(), &err);
  5382. if (U_FAILURE(err)) {
  5383. // No data loaded - use fallback.
  5384. for (int i = 0; i < p_string.length(); i++) {
  5385. char32_t c = p_string[i];
  5386. if (is_whitespace(c) || is_linebreak(c)) {
  5387. breaks.insert(i);
  5388. }
  5389. }
  5390. } else {
  5391. while (ubrk_next(bi) != UBRK_DONE) {
  5392. int pos = _convert_pos(p_string, utf16, ubrk_current(bi)) - 1;
  5393. if (pos != p_string.length() - 1) {
  5394. breaks.insert(pos);
  5395. }
  5396. }
  5397. }
  5398. ubrk_close(bi);
  5399. PackedInt32Array ret;
  5400. for (int i = 0; i < p_string.length(); i++) {
  5401. char32_t c = p_string[i];
  5402. if (c == 0xfffc) {
  5403. continue;
  5404. }
  5405. if (u_ispunct(c) && c != 0x005F) {
  5406. ret.push_back(i);
  5407. continue;
  5408. }
  5409. if (is_underscore(c)) {
  5410. ret.push_back(i);
  5411. continue;
  5412. }
  5413. if (breaks.has(i)) {
  5414. ret.push_back(i);
  5415. continue;
  5416. }
  5417. }
  5418. return ret;
  5419. }
  5420. bool TextServerAdvanced::_is_valid_identifier(const String &p_string) const {
  5421. enum UAX31SequenceStatus {
  5422. SEQ_NOT_STARTED,
  5423. SEQ_STARTED,
  5424. SEQ_STARTED_VIR,
  5425. SEQ_NEAR_END,
  5426. };
  5427. const char32_t *str = p_string.ptr();
  5428. int len = p_string.length();
  5429. if (len == 0) {
  5430. return false; // Empty string.
  5431. }
  5432. UErrorCode err = U_ZERO_ERROR;
  5433. Char16String utf16 = p_string.utf16();
  5434. const UNormalizer2 *norm_c = unorm2_getNFCInstance(&err);
  5435. if (U_FAILURE(err)) {
  5436. return false; // Failed to load normalizer.
  5437. }
  5438. bool isnurom = unorm2_isNormalized(norm_c, utf16.get_data(), utf16.length(), &err);
  5439. if (U_FAILURE(err) || !isnurom) {
  5440. return false; // Do not conform to Normalization Form C.
  5441. }
  5442. UAX31SequenceStatus A1_sequence_status = SEQ_NOT_STARTED;
  5443. UScriptCode A1_scr = USCRIPT_INHERITED;
  5444. UAX31SequenceStatus A2_sequence_status = SEQ_NOT_STARTED;
  5445. UScriptCode A2_scr = USCRIPT_INHERITED;
  5446. UAX31SequenceStatus B_sequence_status = SEQ_NOT_STARTED;
  5447. UScriptCode B_scr = USCRIPT_INHERITED;
  5448. for (int i = 0; i < len; i++) {
  5449. err = U_ZERO_ERROR;
  5450. UScriptCode scr = uscript_getScript(str[i], &err);
  5451. if (U_FAILURE(err)) {
  5452. return false; // Invalid script.
  5453. }
  5454. if (uscript_getUsage(scr) != USCRIPT_USAGE_RECOMMENDED) {
  5455. return false; // Not a recommended script.
  5456. }
  5457. uint8_t cat = u_charType(str[i]);
  5458. int32_t jt = u_getIntPropertyValue(str[i], UCHAR_JOINING_TYPE);
  5459. // UAX #31 section 2.3 subsections A1, A2 and B, check ZWNJ and ZWJ usage.
  5460. switch (A1_sequence_status) {
  5461. case SEQ_NEAR_END: {
  5462. if ((A1_scr > USCRIPT_INHERITED) && (scr > USCRIPT_INHERITED) && (scr != A1_scr)) {
  5463. return false; // Mixed script.
  5464. }
  5465. if (jt == U_JT_RIGHT_JOINING || jt == U_JT_DUAL_JOINING) {
  5466. A1_sequence_status = SEQ_NOT_STARTED; // Valid end of sequence, reset.
  5467. } else if (jt != U_JT_TRANSPARENT) {
  5468. return false; // Invalid end of sequence.
  5469. }
  5470. } break;
  5471. case SEQ_STARTED: {
  5472. if ((A1_scr > USCRIPT_INHERITED) && (scr > USCRIPT_INHERITED) && (scr != A1_scr)) {
  5473. A1_sequence_status = SEQ_NOT_STARTED; // Reset.
  5474. } else {
  5475. if (jt != U_JT_TRANSPARENT) {
  5476. if (str[i] == 0x200C /*ZWNJ*/) {
  5477. A1_sequence_status = SEQ_NEAR_END;
  5478. continue;
  5479. } else {
  5480. A1_sequence_status = SEQ_NOT_STARTED; // Reset.
  5481. }
  5482. }
  5483. }
  5484. } break;
  5485. default:
  5486. break;
  5487. }
  5488. if (A1_sequence_status == SEQ_NOT_STARTED) {
  5489. if (jt == U_JT_LEFT_JOINING || jt == U_JT_DUAL_JOINING) {
  5490. A1_sequence_status = SEQ_STARTED;
  5491. A1_scr = scr;
  5492. }
  5493. };
  5494. switch (A2_sequence_status) {
  5495. case SEQ_NEAR_END: {
  5496. if ((A2_scr > USCRIPT_INHERITED) && (scr > USCRIPT_INHERITED) && (scr != A2_scr)) {
  5497. return false; // Mixed script.
  5498. }
  5499. if (cat == U_UPPERCASE_LETTER || cat == U_LOWERCASE_LETTER || cat == U_TITLECASE_LETTER || cat == U_MODIFIER_LETTER || cat == U_OTHER_LETTER) {
  5500. A2_sequence_status = SEQ_NOT_STARTED; // Valid end of sequence, reset.
  5501. } else if (cat != U_MODIFIER_LETTER || u_getCombiningClass(str[i]) == 0) {
  5502. return false; // Invalid end of sequence.
  5503. }
  5504. } break;
  5505. case SEQ_STARTED_VIR: {
  5506. if ((A2_scr > USCRIPT_INHERITED) && (scr > USCRIPT_INHERITED) && (scr != A2_scr)) {
  5507. A2_sequence_status = SEQ_NOT_STARTED; // Reset.
  5508. } else {
  5509. if (str[i] == 0x200C /*ZWNJ*/) {
  5510. A2_sequence_status = SEQ_NEAR_END;
  5511. continue;
  5512. } else if (cat != U_MODIFIER_LETTER || u_getCombiningClass(str[i]) == 0) {
  5513. A2_sequence_status = SEQ_NOT_STARTED; // Reset.
  5514. }
  5515. }
  5516. } break;
  5517. case SEQ_STARTED: {
  5518. if ((A2_scr > USCRIPT_INHERITED) && (scr > USCRIPT_INHERITED) && (scr != A2_scr)) {
  5519. A2_sequence_status = SEQ_NOT_STARTED; // Reset.
  5520. } else {
  5521. if (u_getCombiningClass(str[i]) == 9 /*Virama Combining Class*/) {
  5522. A2_sequence_status = SEQ_STARTED_VIR;
  5523. } else if (cat != U_MODIFIER_LETTER) {
  5524. A2_sequence_status = SEQ_NOT_STARTED; // Reset.
  5525. }
  5526. }
  5527. } break;
  5528. default:
  5529. break;
  5530. }
  5531. if (A2_sequence_status == SEQ_NOT_STARTED) {
  5532. if (cat == U_UPPERCASE_LETTER || cat == U_LOWERCASE_LETTER || cat == U_TITLECASE_LETTER || cat == U_MODIFIER_LETTER || cat == U_OTHER_LETTER) {
  5533. A2_sequence_status = SEQ_STARTED;
  5534. A2_scr = scr;
  5535. }
  5536. }
  5537. switch (B_sequence_status) {
  5538. case SEQ_NEAR_END: {
  5539. if ((B_scr > USCRIPT_INHERITED) && (scr > USCRIPT_INHERITED) && (scr != B_scr)) {
  5540. return false; // Mixed script.
  5541. }
  5542. if (u_getIntPropertyValue(str[i], UCHAR_INDIC_SYLLABIC_CATEGORY) != U_INSC_VOWEL_DEPENDENT) {
  5543. B_sequence_status = SEQ_NOT_STARTED; // Valid end of sequence, reset.
  5544. } else {
  5545. return false; // Invalid end of sequence.
  5546. }
  5547. } break;
  5548. case SEQ_STARTED_VIR: {
  5549. if ((B_scr > USCRIPT_INHERITED) && (scr > USCRIPT_INHERITED) && (scr != B_scr)) {
  5550. B_sequence_status = SEQ_NOT_STARTED; // Reset.
  5551. } else {
  5552. if (str[i] == 0x200D /*ZWJ*/) {
  5553. B_sequence_status = SEQ_NEAR_END;
  5554. continue;
  5555. } else if (cat != U_MODIFIER_LETTER || u_getCombiningClass(str[i]) == 0) {
  5556. B_sequence_status = SEQ_NOT_STARTED; // Reset.
  5557. }
  5558. }
  5559. } break;
  5560. case SEQ_STARTED: {
  5561. if ((B_scr > USCRIPT_INHERITED) && (scr > USCRIPT_INHERITED) && (scr != B_scr)) {
  5562. B_sequence_status = SEQ_NOT_STARTED; // Reset.
  5563. } else {
  5564. if (u_getCombiningClass(str[i]) == 9 /*Virama Combining Class*/) {
  5565. B_sequence_status = SEQ_STARTED_VIR;
  5566. } else if (cat != U_MODIFIER_LETTER) {
  5567. B_sequence_status = SEQ_NOT_STARTED; // Reset.
  5568. }
  5569. }
  5570. } break;
  5571. default:
  5572. break;
  5573. }
  5574. if (B_sequence_status == SEQ_NOT_STARTED) {
  5575. if (cat == U_UPPERCASE_LETTER || cat == U_LOWERCASE_LETTER || cat == U_TITLECASE_LETTER || cat == U_MODIFIER_LETTER || cat == U_OTHER_LETTER) {
  5576. B_sequence_status = SEQ_STARTED;
  5577. B_scr = scr;
  5578. }
  5579. }
  5580. if (u_hasBinaryProperty(str[i], UCHAR_PATTERN_SYNTAX) || u_hasBinaryProperty(str[i], UCHAR_PATTERN_WHITE_SPACE) || u_hasBinaryProperty(str[i], UCHAR_NONCHARACTER_CODE_POINT)) {
  5581. return false; // Not a XID_Start or XID_Continue character.
  5582. }
  5583. if (i == 0) {
  5584. if (!(cat == U_LOWERCASE_LETTER || cat == U_UPPERCASE_LETTER || cat == U_TITLECASE_LETTER || cat == U_OTHER_LETTER || cat == U_MODIFIER_LETTER || cat == U_LETTER_NUMBER || str[0] == 0x2118 || str[0] == 0x212E || str[0] == 0x309B || str[0] == 0x309C || str[0] == 0x005F)) {
  5585. return false; // Not a XID_Start character.
  5586. }
  5587. } else {
  5588. if (!(cat == U_LOWERCASE_LETTER || cat == U_UPPERCASE_LETTER || cat == U_TITLECASE_LETTER || cat == U_OTHER_LETTER || cat == U_MODIFIER_LETTER || cat == U_LETTER_NUMBER || cat == U_NON_SPACING_MARK || cat == U_COMBINING_SPACING_MARK || cat == U_DECIMAL_DIGIT_NUMBER || cat == U_CONNECTOR_PUNCTUATION || str[i] == 0x2118 || str[i] == 0x212E || str[i] == 0x309B || str[i] == 0x309C || str[i] == 0x1369 || str[i] == 0x1371 || str[i] == 0x00B7 || str[i] == 0x0387 || str[i] == 0x19DA || str[i] == 0x0E33 || str[i] == 0x0EB3 || str[i] == 0xFF9E || str[i] == 0xFF9F)) {
  5589. return false; // Not a XID_Continue character.
  5590. }
  5591. }
  5592. }
  5593. return true;
  5594. }
  5595. TextServerAdvanced::TextServerAdvanced() {
  5596. _insert_num_systems_lang();
  5597. _insert_feature_sets();
  5598. _bmp_create_font_funcs();
  5599. }
  5600. void TextServerAdvanced::_cleanup() {
  5601. for (const KeyValue<SystemFontKey, SystemFontCache> &E : system_fonts) {
  5602. const Vector<SystemFontCacheRec> &sysf_cache = E.value.var;
  5603. for (const SystemFontCacheRec &F : sysf_cache) {
  5604. _free_rid(F.rid);
  5605. }
  5606. }
  5607. system_fonts.clear();
  5608. system_font_data.clear();
  5609. }
  5610. TextServerAdvanced::~TextServerAdvanced() {
  5611. _bmp_free_font_funcs();
  5612. #ifdef MODULE_FREETYPE_ENABLED
  5613. if (ft_library != nullptr) {
  5614. FT_Done_FreeType(ft_library);
  5615. }
  5616. #endif
  5617. u_cleanup();
  5618. }