2
0

text_server_adv.cpp 173 KB

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