text_server_adv.cpp 286 KB

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