text_server_adv.cpp 255 KB

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