text_server_adv.cpp 266 KB

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