text_server_adv.cpp 239 KB

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