text_server_adv.cpp 273 KB

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