text_server_adv.cpp 226 KB

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