text_server_adv.cpp 197 KB

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