text_server_adv.cpp 249 KB

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