text_server_adv.cpp 275 KB

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