text_server_adv.cpp 274 KB

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