font.cpp 139 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583
  1. /**************************************************************************/
  2. /* font.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "font.h"
  31. #include "font.compat.inc"
  32. #include "core/io/image_loader.h"
  33. #include "core/io/resource_loader.h"
  34. #include "core/string/translation.h"
  35. #include "core/templates/hash_map.h"
  36. #include "core/templates/hashfuncs.h"
  37. #include "scene/resources/image_texture.h"
  38. #include "scene/resources/text_line.h"
  39. #include "scene/resources/text_paragraph.h"
  40. #include "scene/resources/theme.h"
  41. #include "scene/theme/theme_db.h"
  42. /*************************************************************************/
  43. /* Font */
  44. /*************************************************************************/
  45. void Font::_bind_methods() {
  46. ClassDB::bind_method(D_METHOD("set_fallbacks", "fallbacks"), &Font::set_fallbacks);
  47. ClassDB::bind_method(D_METHOD("get_fallbacks"), &Font::get_fallbacks);
  48. // Output.
  49. ClassDB::bind_method(D_METHOD("find_variation", "variation_coordinates", "face_index", "strength", "transform", "spacing_top", "spacing_bottom", "spacing_space", "spacing_glyph", "baseline_offset"), &Font::find_variation, DEFVAL(0), DEFVAL(0.0), DEFVAL(Transform2D()), DEFVAL(0), DEFVAL(0), DEFVAL(0), DEFVAL(0), DEFVAL(0.0));
  50. ClassDB::bind_method(D_METHOD("get_rids"), &Font::get_rids);
  51. // Font metrics.
  52. ClassDB::bind_method(D_METHOD("get_height", "font_size"), &Font::get_height, DEFVAL(DEFAULT_FONT_SIZE));
  53. ClassDB::bind_method(D_METHOD("get_ascent", "font_size"), &Font::get_ascent, DEFVAL(DEFAULT_FONT_SIZE));
  54. ClassDB::bind_method(D_METHOD("get_descent", "font_size"), &Font::get_descent, DEFVAL(DEFAULT_FONT_SIZE));
  55. ClassDB::bind_method(D_METHOD("get_underline_position", "font_size"), &Font::get_underline_position, DEFVAL(DEFAULT_FONT_SIZE));
  56. ClassDB::bind_method(D_METHOD("get_underline_thickness", "font_size"), &Font::get_underline_thickness, DEFVAL(DEFAULT_FONT_SIZE));
  57. ClassDB::bind_method(D_METHOD("get_font_name"), &Font::get_font_name);
  58. ClassDB::bind_method(D_METHOD("get_font_style_name"), &Font::get_font_style_name);
  59. ClassDB::bind_method(D_METHOD("get_ot_name_strings"), &Font::get_ot_name_strings);
  60. ClassDB::bind_method(D_METHOD("get_font_style"), &Font::get_font_style);
  61. ClassDB::bind_method(D_METHOD("get_font_weight"), &Font::get_font_weight);
  62. ClassDB::bind_method(D_METHOD("get_font_stretch"), &Font::get_font_stretch);
  63. ClassDB::bind_method(D_METHOD("get_spacing", "spacing"), &Font::get_spacing);
  64. ClassDB::bind_method(D_METHOD("get_opentype_features"), &Font::get_opentype_features);
  65. // Drawing string.
  66. ClassDB::bind_method(D_METHOD("set_cache_capacity", "single_line", "multi_line"), &Font::set_cache_capacity);
  67. ClassDB::bind_method(D_METHOD("get_string_size", "text", "alignment", "width", "font_size", "justification_flags", "direction", "orientation"), &Font::get_string_size, DEFVAL(HORIZONTAL_ALIGNMENT_LEFT), DEFVAL(-1), DEFVAL(DEFAULT_FONT_SIZE), DEFVAL(TextServer::JUSTIFICATION_KASHIDA | TextServer::JUSTIFICATION_WORD_BOUND), DEFVAL(TextServer::DIRECTION_AUTO), DEFVAL(TextServer::ORIENTATION_HORIZONTAL));
  68. ClassDB::bind_method(D_METHOD("get_multiline_string_size", "text", "alignment", "width", "font_size", "max_lines", "brk_flags", "justification_flags", "direction", "orientation"), &Font::get_multiline_string_size, DEFVAL(HORIZONTAL_ALIGNMENT_LEFT), DEFVAL(-1), DEFVAL(DEFAULT_FONT_SIZE), DEFVAL(-1), DEFVAL(TextServer::BREAK_MANDATORY | TextServer::BREAK_WORD_BOUND), DEFVAL(TextServer::JUSTIFICATION_KASHIDA | TextServer::JUSTIFICATION_WORD_BOUND), DEFVAL(TextServer::DIRECTION_AUTO), DEFVAL(TextServer::ORIENTATION_HORIZONTAL));
  69. ClassDB::bind_method(D_METHOD("draw_string", "canvas_item", "pos", "text", "alignment", "width", "font_size", "modulate", "justification_flags", "direction", "orientation"), &Font::draw_string, DEFVAL(HORIZONTAL_ALIGNMENT_LEFT), DEFVAL(-1), DEFVAL(DEFAULT_FONT_SIZE), DEFVAL(Color(1.0, 1.0, 1.0)), DEFVAL(TextServer::JUSTIFICATION_KASHIDA | TextServer::JUSTIFICATION_WORD_BOUND), DEFVAL(TextServer::DIRECTION_AUTO), DEFVAL(TextServer::ORIENTATION_HORIZONTAL));
  70. ClassDB::bind_method(D_METHOD("draw_multiline_string", "canvas_item", "pos", "text", "alignment", "width", "font_size", "max_lines", "modulate", "brk_flags", "justification_flags", "direction", "orientation"), &Font::draw_multiline_string, DEFVAL(HORIZONTAL_ALIGNMENT_LEFT), DEFVAL(-1), DEFVAL(DEFAULT_FONT_SIZE), DEFVAL(-1), DEFVAL(Color(1.0, 1.0, 1.0)), DEFVAL(TextServer::BREAK_MANDATORY | TextServer::BREAK_WORD_BOUND), DEFVAL(TextServer::JUSTIFICATION_KASHIDA | TextServer::JUSTIFICATION_WORD_BOUND), DEFVAL(TextServer::DIRECTION_AUTO), DEFVAL(TextServer::ORIENTATION_HORIZONTAL));
  71. ClassDB::bind_method(D_METHOD("draw_string_outline", "canvas_item", "pos", "text", "alignment", "width", "font_size", "size", "modulate", "justification_flags", "direction", "orientation"), &Font::draw_string_outline, DEFVAL(HORIZONTAL_ALIGNMENT_LEFT), DEFVAL(-1), DEFVAL(DEFAULT_FONT_SIZE), DEFVAL(1), DEFVAL(Color(1.0, 1.0, 1.0)), DEFVAL(TextServer::JUSTIFICATION_KASHIDA | TextServer::JUSTIFICATION_WORD_BOUND), DEFVAL(TextServer::DIRECTION_AUTO), DEFVAL(TextServer::ORIENTATION_HORIZONTAL));
  72. ClassDB::bind_method(D_METHOD("draw_multiline_string_outline", "canvas_item", "pos", "text", "alignment", "width", "font_size", "max_lines", "size", "modulate", "brk_flags", "justification_flags", "direction", "orientation"), &Font::draw_multiline_string_outline, DEFVAL(HORIZONTAL_ALIGNMENT_LEFT), DEFVAL(-1), DEFVAL(DEFAULT_FONT_SIZE), DEFVAL(-1), DEFVAL(1), DEFVAL(Color(1.0, 1.0, 1.0)), DEFVAL(TextServer::BREAK_MANDATORY | TextServer::BREAK_WORD_BOUND), DEFVAL(TextServer::JUSTIFICATION_KASHIDA | TextServer::JUSTIFICATION_WORD_BOUND), DEFVAL(TextServer::DIRECTION_AUTO), DEFVAL(TextServer::ORIENTATION_HORIZONTAL));
  73. // Drawing char.
  74. ClassDB::bind_method(D_METHOD("get_char_size", "char", "font_size"), &Font::get_char_size);
  75. ClassDB::bind_method(D_METHOD("draw_char", "canvas_item", "pos", "char", "font_size", "modulate"), &Font::draw_char, DEFVAL(Color(1.0, 1.0, 1.0)));
  76. ClassDB::bind_method(D_METHOD("draw_char_outline", "canvas_item", "pos", "char", "font_size", "size", "modulate"), &Font::draw_char_outline, DEFVAL(-1), DEFVAL(Color(1.0, 1.0, 1.0)));
  77. // Helper functions.
  78. ClassDB::bind_method(D_METHOD("has_char", "char"), &Font::has_char);
  79. ClassDB::bind_method(D_METHOD("get_supported_chars"), &Font::get_supported_chars);
  80. ClassDB::bind_method(D_METHOD("is_language_supported", "language"), &Font::is_language_supported);
  81. ClassDB::bind_method(D_METHOD("is_script_supported", "script"), &Font::is_script_supported);
  82. ClassDB::bind_method(D_METHOD("get_supported_feature_list"), &Font::get_supported_feature_list);
  83. ClassDB::bind_method(D_METHOD("get_supported_variation_list"), &Font::get_supported_variation_list);
  84. ClassDB::bind_method(D_METHOD("get_face_count"), &Font::get_face_count);
  85. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "fallbacks", PROPERTY_HINT_ARRAY_TYPE, MAKE_RESOURCE_TYPE_HINT("Font")), "set_fallbacks", "get_fallbacks");
  86. }
  87. void Font::_update_rids_fb(const Ref<Font> &p_f, int p_depth) const {
  88. ERR_FAIL_COND(p_depth > MAX_FALLBACK_DEPTH);
  89. if (p_f.is_valid()) {
  90. RID rid = p_f->_get_rid();
  91. if (rid.is_valid()) {
  92. rids.push_back(rid);
  93. }
  94. const TypedArray<Font> &_fallbacks = p_f->get_fallbacks();
  95. for (int i = 0; i < _fallbacks.size(); i++) {
  96. _update_rids_fb(_fallbacks[i], p_depth + 1);
  97. }
  98. }
  99. }
  100. void Font::_update_rids() const {
  101. rids.clear();
  102. _update_rids_fb(const_cast<Font *>(this), 0);
  103. dirty_rids = false;
  104. }
  105. void Font::_invalidate_rids() {
  106. rids.clear();
  107. dirty_rids = true;
  108. cache.clear();
  109. cache_wrap.clear();
  110. emit_changed();
  111. }
  112. bool Font::_is_cyclic(const Ref<Font> &p_f, int p_depth) const {
  113. ERR_FAIL_COND_V(p_depth > MAX_FALLBACK_DEPTH, true);
  114. if (p_f.is_null()) {
  115. return false;
  116. }
  117. if (p_f == this) {
  118. return true;
  119. }
  120. for (int i = 0; i < p_f->fallbacks.size(); i++) {
  121. const Ref<Font> &f = p_f->fallbacks[i];
  122. if (_is_cyclic(f, p_depth + 1)) {
  123. return true;
  124. }
  125. }
  126. return false;
  127. }
  128. bool Font::_is_base_cyclic(const Ref<Font> &p_f, int p_depth) const {
  129. ERR_FAIL_COND_V(p_depth > MAX_FALLBACK_DEPTH, true);
  130. if (p_f.is_null()) {
  131. return false;
  132. }
  133. if (p_f == this) {
  134. return true;
  135. }
  136. Ref<FontVariation> fv = p_f;
  137. if (fv.is_valid()) {
  138. return _is_base_cyclic(fv->get_base_font(), p_depth + 1);
  139. }
  140. Ref<SystemFont> fs = p_f;
  141. if (fs.is_valid()) {
  142. return _is_base_cyclic(fs->get_base_font(), p_depth + 1);
  143. }
  144. return false;
  145. }
  146. void Font::reset_state() {
  147. _invalidate_rids();
  148. }
  149. // Fallbacks.
  150. void Font::set_fallbacks(const TypedArray<Font> &p_fallbacks) {
  151. for (int i = 0; i < p_fallbacks.size(); i++) {
  152. const Ref<Font> &f = p_fallbacks[i];
  153. ERR_FAIL_COND_MSG(_is_cyclic(f, 0), "Cyclic font fallback.");
  154. }
  155. for (int i = 0; i < fallbacks.size(); i++) {
  156. Ref<Font> f = fallbacks[i];
  157. if (f.is_valid()) {
  158. f->disconnect_changed(callable_mp(this, &Font::_invalidate_rids));
  159. }
  160. }
  161. fallbacks = p_fallbacks;
  162. for (int i = 0; i < fallbacks.size(); i++) {
  163. Ref<Font> f = fallbacks[i];
  164. if (f.is_valid()) {
  165. f->connect_changed(callable_mp(this, &Font::_invalidate_rids), CONNECT_REFERENCE_COUNTED);
  166. }
  167. }
  168. _invalidate_rids();
  169. }
  170. TypedArray<Font> Font::get_fallbacks() const {
  171. return fallbacks;
  172. }
  173. // Output.
  174. TypedArray<RID> Font::get_rids() const {
  175. if (dirty_rids) {
  176. _update_rids();
  177. }
  178. return rids;
  179. }
  180. // Drawing string.
  181. real_t Font::get_height(int p_font_size) const {
  182. if (dirty_rids) {
  183. _update_rids();
  184. }
  185. real_t ret = 0.f;
  186. for (int i = 0; i < rids.size(); i++) {
  187. ret = MAX(ret, TS->font_get_ascent(rids[i], p_font_size) + TS->font_get_descent(rids[i], p_font_size));
  188. }
  189. return ret + get_spacing(TextServer::SPACING_BOTTOM) + get_spacing(TextServer::SPACING_TOP);
  190. }
  191. real_t Font::get_ascent(int p_font_size) const {
  192. if (dirty_rids) {
  193. _update_rids();
  194. }
  195. real_t ret = 0.f;
  196. for (int i = 0; i < rids.size(); i++) {
  197. ret = MAX(ret, TS->font_get_ascent(rids[i], p_font_size));
  198. }
  199. return ret + get_spacing(TextServer::SPACING_TOP);
  200. }
  201. real_t Font::get_descent(int p_font_size) const {
  202. if (dirty_rids) {
  203. _update_rids();
  204. }
  205. real_t ret = 0.f;
  206. for (int i = 0; i < rids.size(); i++) {
  207. ret = MAX(ret, TS->font_get_descent(rids[i], p_font_size));
  208. }
  209. return ret + get_spacing(TextServer::SPACING_BOTTOM);
  210. }
  211. real_t Font::get_underline_position(int p_font_size) const {
  212. if (dirty_rids) {
  213. _update_rids();
  214. }
  215. real_t ret = 0.f;
  216. for (int i = 0; i < rids.size(); i++) {
  217. ret = MAX(ret, TS->font_get_underline_position(rids[i], p_font_size));
  218. }
  219. return ret + get_spacing(TextServer::SPACING_TOP);
  220. }
  221. real_t Font::get_underline_thickness(int p_font_size) const {
  222. if (dirty_rids) {
  223. _update_rids();
  224. }
  225. real_t ret = 0.f;
  226. for (int i = 0; i < rids.size(); i++) {
  227. ret = MAX(ret, TS->font_get_underline_thickness(rids[i], p_font_size));
  228. }
  229. return ret;
  230. }
  231. String Font::get_font_name() const {
  232. return TS->font_get_name(_get_rid());
  233. }
  234. Dictionary Font::get_ot_name_strings() const {
  235. return TS->font_get_ot_name_strings(_get_rid());
  236. }
  237. String Font::get_font_style_name() const {
  238. return TS->font_get_style_name(_get_rid());
  239. }
  240. BitField<TextServer::FontStyle> Font::get_font_style() const {
  241. return TS->font_get_style(_get_rid());
  242. }
  243. int Font::get_font_weight() const {
  244. return TS->font_get_weight(_get_rid());
  245. }
  246. int Font::get_font_stretch() const {
  247. return TS->font_get_stretch(_get_rid());
  248. }
  249. Dictionary Font::get_opentype_features() const {
  250. return Dictionary();
  251. }
  252. // Drawing string.
  253. void Font::set_cache_capacity(int p_single_line, int p_multi_line) {
  254. cache.set_capacity(p_single_line);
  255. cache_wrap.set_capacity(p_multi_line);
  256. }
  257. Size2 Font::get_string_size(const String &p_text, HorizontalAlignment p_alignment, float p_width, int p_font_size, BitField<TextServer::JustificationFlag> p_jst_flags, TextServer::Direction p_direction, TextServer::Orientation p_orientation) const {
  258. bool fill = (p_alignment == HORIZONTAL_ALIGNMENT_FILL);
  259. ShapedTextKey key = ShapedTextKey(p_text, p_font_size, fill ? p_width : 0.0, fill ? p_jst_flags : TextServer::JUSTIFICATION_NONE, TextServer::BREAK_NONE, p_direction, p_orientation);
  260. Ref<TextLine> buffer;
  261. if (cache.has(key)) {
  262. buffer = cache.get(key);
  263. } else {
  264. buffer.instantiate();
  265. buffer->set_direction(p_direction);
  266. buffer->set_orientation(p_orientation);
  267. buffer->set_text_overrun_behavior(TextServer::OVERRUN_TRIM_CHAR);
  268. buffer->add_string(p_text, Ref<Font>(this), p_font_size);
  269. cache.insert(key, buffer);
  270. }
  271. buffer->set_width(p_width);
  272. buffer->set_horizontal_alignment(p_alignment);
  273. if (p_alignment == HORIZONTAL_ALIGNMENT_FILL) {
  274. buffer->set_flags(p_jst_flags);
  275. }
  276. return buffer->get_size();
  277. }
  278. Size2 Font::get_multiline_string_size(const String &p_text, HorizontalAlignment p_alignment, float p_width, int p_font_size, int p_max_lines, BitField<TextServer::LineBreakFlag> p_brk_flags, BitField<TextServer::JustificationFlag> p_jst_flags, TextServer::Direction p_direction, TextServer::Orientation p_orientation) const {
  279. ShapedTextKey key = ShapedTextKey(p_text, p_font_size, p_width, p_jst_flags, p_brk_flags, p_direction, p_orientation);
  280. Ref<TextParagraph> lines_buffer;
  281. if (cache_wrap.has(key)) {
  282. lines_buffer = cache_wrap.get(key);
  283. } else {
  284. lines_buffer.instantiate();
  285. lines_buffer->set_direction(p_direction);
  286. lines_buffer->set_orientation(p_orientation);
  287. lines_buffer->add_string(p_text, Ref<Font>(this), p_font_size);
  288. lines_buffer->set_width(p_width);
  289. lines_buffer->set_break_flags(p_brk_flags);
  290. lines_buffer->set_justification_flags(p_jst_flags);
  291. lines_buffer->set_text_overrun_behavior(TextServer::OVERRUN_TRIM_CHAR);
  292. cache_wrap.insert(key, lines_buffer);
  293. }
  294. lines_buffer->set_alignment(p_alignment);
  295. lines_buffer->set_max_lines_visible(p_max_lines);
  296. return lines_buffer->get_size();
  297. }
  298. void Font::draw_string(RID p_canvas_item, const Point2 &p_pos, const String &p_text, HorizontalAlignment p_alignment, float p_width, int p_font_size, const Color &p_modulate, BitField<TextServer::JustificationFlag> p_jst_flags, TextServer::Direction p_direction, TextServer::Orientation p_orientation) const {
  299. bool fill = (p_alignment == HORIZONTAL_ALIGNMENT_FILL);
  300. ShapedTextKey key = ShapedTextKey(p_text, p_font_size, fill ? p_width : 0.0, fill ? p_jst_flags : TextServer::JUSTIFICATION_NONE, TextServer::BREAK_NONE, p_direction, p_orientation);
  301. Ref<TextLine> buffer;
  302. if (cache.has(key)) {
  303. buffer = cache.get(key);
  304. } else {
  305. buffer.instantiate();
  306. buffer->set_direction(p_direction);
  307. buffer->set_orientation(p_orientation);
  308. buffer->set_text_overrun_behavior(TextServer::OVERRUN_TRIM_CHAR);
  309. buffer->add_string(p_text, Ref<Font>(this), p_font_size);
  310. cache.insert(key, buffer);
  311. }
  312. Vector2 ofs = p_pos;
  313. if (p_orientation == TextServer::ORIENTATION_HORIZONTAL) {
  314. ofs.y -= buffer->get_line_ascent();
  315. } else {
  316. ofs.x -= buffer->get_line_ascent();
  317. }
  318. buffer->set_width(p_width);
  319. buffer->set_horizontal_alignment(p_alignment);
  320. if (p_alignment == HORIZONTAL_ALIGNMENT_FILL) {
  321. buffer->set_flags(p_jst_flags);
  322. }
  323. buffer->draw(p_canvas_item, ofs, p_modulate);
  324. }
  325. void Font::draw_multiline_string(RID p_canvas_item, const Point2 &p_pos, const String &p_text, HorizontalAlignment p_alignment, float p_width, int p_font_size, int p_max_lines, const Color &p_modulate, BitField<TextServer::LineBreakFlag> p_brk_flags, BitField<TextServer::JustificationFlag> p_jst_flags, TextServer::Direction p_direction, TextServer::Orientation p_orientation) const {
  326. ShapedTextKey key = ShapedTextKey(p_text, p_font_size, p_width, p_jst_flags, p_brk_flags, p_direction, p_orientation);
  327. Ref<TextParagraph> lines_buffer;
  328. if (cache_wrap.has(key)) {
  329. lines_buffer = cache_wrap.get(key);
  330. } else {
  331. lines_buffer.instantiate();
  332. lines_buffer->set_direction(p_direction);
  333. lines_buffer->set_orientation(p_orientation);
  334. lines_buffer->add_string(p_text, Ref<Font>(this), p_font_size);
  335. lines_buffer->set_width(p_width);
  336. lines_buffer->set_break_flags(p_brk_flags);
  337. lines_buffer->set_justification_flags(p_jst_flags);
  338. lines_buffer->set_text_overrun_behavior(TextServer::OVERRUN_TRIM_CHAR);
  339. cache_wrap.insert(key, lines_buffer);
  340. }
  341. Vector2 ofs = p_pos;
  342. if (p_orientation == TextServer::ORIENTATION_HORIZONTAL) {
  343. ofs.y -= lines_buffer->get_line_ascent(0);
  344. } else {
  345. ofs.x -= lines_buffer->get_line_ascent(0);
  346. }
  347. lines_buffer->set_alignment(p_alignment);
  348. lines_buffer->set_max_lines_visible(p_max_lines);
  349. lines_buffer->draw(p_canvas_item, ofs, p_modulate);
  350. }
  351. void Font::draw_string_outline(RID p_canvas_item, const Point2 &p_pos, const String &p_text, HorizontalAlignment p_alignment, float p_width, int p_font_size, int p_size, const Color &p_modulate, BitField<TextServer::JustificationFlag> p_jst_flags, TextServer::Direction p_direction, TextServer::Orientation p_orientation) const {
  352. bool fill = (p_alignment == HORIZONTAL_ALIGNMENT_FILL);
  353. ShapedTextKey key = ShapedTextKey(p_text, p_font_size, fill ? p_width : 0.0, fill ? p_jst_flags : TextServer::JUSTIFICATION_NONE, TextServer::BREAK_NONE, p_direction, p_orientation);
  354. Ref<TextLine> buffer;
  355. if (cache.has(key)) {
  356. buffer = cache.get(key);
  357. } else {
  358. buffer.instantiate();
  359. buffer->set_direction(p_direction);
  360. buffer->set_orientation(p_orientation);
  361. buffer->set_text_overrun_behavior(TextServer::OVERRUN_TRIM_CHAR);
  362. buffer->add_string(p_text, Ref<Font>(this), p_font_size);
  363. cache.insert(key, buffer);
  364. }
  365. Vector2 ofs = p_pos;
  366. if (p_orientation == TextServer::ORIENTATION_HORIZONTAL) {
  367. ofs.y -= buffer->get_line_ascent();
  368. } else {
  369. ofs.x -= buffer->get_line_ascent();
  370. }
  371. buffer->set_width(p_width);
  372. buffer->set_horizontal_alignment(p_alignment);
  373. if (p_alignment == HORIZONTAL_ALIGNMENT_FILL) {
  374. buffer->set_flags(p_jst_flags);
  375. }
  376. buffer->draw_outline(p_canvas_item, ofs, p_size, p_modulate);
  377. }
  378. void Font::draw_multiline_string_outline(RID p_canvas_item, const Point2 &p_pos, const String &p_text, HorizontalAlignment p_alignment, float p_width, int p_font_size, int p_max_lines, int p_size, const Color &p_modulate, BitField<TextServer::LineBreakFlag> p_brk_flags, BitField<TextServer::JustificationFlag> p_jst_flags, TextServer::Direction p_direction, TextServer::Orientation p_orientation) const {
  379. ShapedTextKey key = ShapedTextKey(p_text, p_font_size, p_width, p_jst_flags, p_brk_flags, p_direction, p_orientation);
  380. Ref<TextParagraph> lines_buffer;
  381. if (cache_wrap.has(key)) {
  382. lines_buffer = cache_wrap.get(key);
  383. } else {
  384. lines_buffer.instantiate();
  385. lines_buffer->set_direction(p_direction);
  386. lines_buffer->set_orientation(p_orientation);
  387. lines_buffer->add_string(p_text, Ref<Font>(this), p_font_size);
  388. lines_buffer->set_width(p_width);
  389. lines_buffer->set_break_flags(p_brk_flags);
  390. lines_buffer->set_justification_flags(p_jst_flags);
  391. lines_buffer->set_text_overrun_behavior(TextServer::OVERRUN_TRIM_CHAR);
  392. cache_wrap.insert(key, lines_buffer);
  393. }
  394. Vector2 ofs = p_pos;
  395. if (p_orientation == TextServer::ORIENTATION_HORIZONTAL) {
  396. ofs.y -= lines_buffer->get_line_ascent(0);
  397. } else {
  398. ofs.x -= lines_buffer->get_line_ascent(0);
  399. }
  400. lines_buffer->set_alignment(p_alignment);
  401. lines_buffer->set_max_lines_visible(p_max_lines);
  402. lines_buffer->draw_outline(p_canvas_item, ofs, p_size, p_modulate);
  403. }
  404. // Drawing char.
  405. Size2 Font::get_char_size(char32_t p_char, int p_font_size) const {
  406. if (dirty_rids) {
  407. _update_rids();
  408. }
  409. for (int i = 0; i < rids.size(); i++) {
  410. if (TS->font_has_char(rids[i], p_char)) {
  411. int32_t glyph = TS->font_get_glyph_index(rids[i], p_font_size, p_char, 0);
  412. return Size2(TS->font_get_glyph_advance(rids[i], p_font_size, glyph).x, get_height(p_font_size));
  413. }
  414. }
  415. return Size2();
  416. }
  417. real_t Font::draw_char(RID p_canvas_item, const Point2 &p_pos, char32_t p_char, int p_font_size, const Color &p_modulate) const {
  418. if (dirty_rids) {
  419. _update_rids();
  420. }
  421. for (int i = 0; i < rids.size(); i++) {
  422. if (TS->font_has_char(rids[i], p_char)) {
  423. int32_t glyph = TS->font_get_glyph_index(rids[i], p_font_size, p_char, 0);
  424. TS->font_draw_glyph(rids[i], p_canvas_item, p_font_size, p_pos, glyph, p_modulate);
  425. return TS->font_get_glyph_advance(rids[i], p_font_size, glyph).x;
  426. }
  427. }
  428. return 0.f;
  429. }
  430. real_t Font::draw_char_outline(RID p_canvas_item, const Point2 &p_pos, char32_t p_char, int p_font_size, int p_size, const Color &p_modulate) const {
  431. if (dirty_rids) {
  432. _update_rids();
  433. }
  434. for (int i = 0; i < rids.size(); i++) {
  435. if (TS->font_has_char(rids[i], p_char)) {
  436. int32_t glyph = TS->font_get_glyph_index(rids[i], p_font_size, p_char, 0);
  437. TS->font_draw_glyph_outline(rids[i], p_canvas_item, p_font_size, p_size, p_pos, glyph, p_modulate);
  438. return TS->font_get_glyph_advance(rids[i], p_font_size, glyph).x;
  439. }
  440. }
  441. return 0.f;
  442. }
  443. // Helper functions.
  444. bool Font::has_char(char32_t p_char) const {
  445. if (dirty_rids) {
  446. _update_rids();
  447. }
  448. for (int i = 0; i < rids.size(); i++) {
  449. if (TS->font_has_char(rids[i], p_char)) {
  450. return true;
  451. }
  452. }
  453. return false;
  454. }
  455. String Font::get_supported_chars() const {
  456. if (dirty_rids) {
  457. _update_rids();
  458. }
  459. String chars;
  460. for (int i = 0; i < rids.size(); i++) {
  461. String data_chars = TS->font_get_supported_chars(rids[i]);
  462. for (int j = 0; j < data_chars.length(); j++) {
  463. if (chars.find_char(data_chars[j]) == -1) {
  464. chars += data_chars[j];
  465. }
  466. }
  467. }
  468. return chars;
  469. }
  470. bool Font::is_language_supported(const String &p_language) const {
  471. return TS->font_is_language_supported(_get_rid(), p_language);
  472. }
  473. bool Font::is_script_supported(const String &p_script) const {
  474. return TS->font_is_script_supported(_get_rid(), p_script);
  475. }
  476. Dictionary Font::get_supported_feature_list() const {
  477. return TS->font_supported_feature_list(_get_rid());
  478. }
  479. Dictionary Font::get_supported_variation_list() const {
  480. return TS->font_supported_variation_list(_get_rid());
  481. }
  482. int64_t Font::get_face_count() const {
  483. return TS->font_get_face_count(_get_rid());
  484. }
  485. Font::Font() {
  486. cache.set_capacity(64);
  487. cache_wrap.set_capacity(16);
  488. }
  489. Font::~Font() {
  490. }
  491. /*************************************************************************/
  492. /* FontFile */
  493. /*************************************************************************/
  494. _FORCE_INLINE_ void FontFile::_clear_cache() {
  495. for (int i = 0; i < cache.size(); i++) {
  496. if (cache[i].is_valid()) {
  497. TS->free_rid(cache[i]);
  498. cache.write[i] = RID();
  499. }
  500. }
  501. }
  502. _FORCE_INLINE_ void FontFile::_ensure_rid(int p_cache_index, int p_make_linked_from) const {
  503. if (unlikely(p_cache_index >= cache.size())) {
  504. cache.resize(p_cache_index + 1);
  505. }
  506. if (unlikely(!cache[p_cache_index].is_valid())) {
  507. if (p_make_linked_from >= 0 && p_make_linked_from != p_cache_index && p_make_linked_from < cache.size()) {
  508. cache.write[p_cache_index] = TS->create_font_linked_variation(cache[p_make_linked_from]);
  509. } else {
  510. cache.write[p_cache_index] = TS->create_font();
  511. TS->font_set_data_ptr(cache[p_cache_index], data_ptr, data_size);
  512. TS->font_set_antialiasing(cache[p_cache_index], antialiasing);
  513. TS->font_set_generate_mipmaps(cache[p_cache_index], mipmaps);
  514. TS->font_set_disable_embedded_bitmaps(cache[p_cache_index], disable_embedded_bitmaps);
  515. TS->font_set_multichannel_signed_distance_field(cache[p_cache_index], msdf);
  516. TS->font_set_msdf_pixel_range(cache[p_cache_index], msdf_pixel_range);
  517. TS->font_set_msdf_size(cache[p_cache_index], msdf_size);
  518. TS->font_set_fixed_size(cache[p_cache_index], fixed_size);
  519. TS->font_set_fixed_size_scale_mode(cache[p_cache_index], fixed_size_scale_mode);
  520. TS->font_set_force_autohinter(cache[p_cache_index], force_autohinter);
  521. TS->font_set_allow_system_fallback(cache[p_cache_index], allow_system_fallback);
  522. TS->font_set_hinting(cache[p_cache_index], hinting);
  523. TS->font_set_subpixel_positioning(cache[p_cache_index], subpixel_positioning);
  524. TS->font_set_oversampling(cache[p_cache_index], oversampling);
  525. }
  526. }
  527. }
  528. void FontFile::_convert_packed_8bit(Ref<Image> &p_source, int p_page, int p_sz) {
  529. int w = p_source->get_width();
  530. int h = p_source->get_height();
  531. PackedByteArray imgdata = p_source->get_data();
  532. const uint8_t *r = imgdata.ptr();
  533. PackedByteArray imgdata_r;
  534. imgdata_r.resize(w * h * 2);
  535. uint8_t *wr = imgdata_r.ptrw();
  536. PackedByteArray imgdata_g;
  537. imgdata_g.resize(w * h * 2);
  538. uint8_t *wg = imgdata_g.ptrw();
  539. PackedByteArray imgdata_b;
  540. imgdata_b.resize(w * h * 2);
  541. uint8_t *wb = imgdata_b.ptrw();
  542. PackedByteArray imgdata_a;
  543. imgdata_a.resize(w * h * 2);
  544. uint8_t *wa = imgdata_a.ptrw();
  545. for (int i = 0; i < h; i++) {
  546. for (int j = 0; j < w; j++) {
  547. int ofs_src = (i * w + j) * 4;
  548. int ofs_dst = (i * w + j) * 2;
  549. wr[ofs_dst + 0] = 255;
  550. wr[ofs_dst + 1] = r[ofs_src + 0];
  551. wg[ofs_dst + 0] = 255;
  552. wg[ofs_dst + 1] = r[ofs_src + 1];
  553. wb[ofs_dst + 0] = 255;
  554. wb[ofs_dst + 1] = r[ofs_src + 2];
  555. wa[ofs_dst + 0] = 255;
  556. wa[ofs_dst + 1] = r[ofs_src + 3];
  557. }
  558. }
  559. Ref<Image> img_r = memnew(Image(w, h, false, Image::FORMAT_LA8, imgdata_r));
  560. set_texture_image(0, Vector2i(p_sz, 0), p_page * 4 + 0, img_r);
  561. Ref<Image> img_g = memnew(Image(w, h, false, Image::FORMAT_LA8, imgdata_g));
  562. set_texture_image(0, Vector2i(p_sz, 0), p_page * 4 + 1, img_g);
  563. Ref<Image> img_b = memnew(Image(w, h, false, Image::FORMAT_LA8, imgdata_b));
  564. set_texture_image(0, Vector2i(p_sz, 0), p_page * 4 + 2, img_b);
  565. Ref<Image> img_a = memnew(Image(w, h, false, Image::FORMAT_LA8, imgdata_a));
  566. set_texture_image(0, Vector2i(p_sz, 0), p_page * 4 + 3, img_a);
  567. }
  568. void FontFile::_convert_packed_4bit(Ref<Image> &p_source, int p_page, int p_sz) {
  569. int w = p_source->get_width();
  570. int h = p_source->get_height();
  571. PackedByteArray imgdata = p_source->get_data();
  572. const uint8_t *r = imgdata.ptr();
  573. PackedByteArray imgdata_r;
  574. imgdata_r.resize(w * h * 2);
  575. uint8_t *wr = imgdata_r.ptrw();
  576. PackedByteArray imgdata_g;
  577. imgdata_g.resize(w * h * 2);
  578. uint8_t *wg = imgdata_g.ptrw();
  579. PackedByteArray imgdata_b;
  580. imgdata_b.resize(w * h * 2);
  581. uint8_t *wb = imgdata_b.ptrw();
  582. PackedByteArray imgdata_a;
  583. imgdata_a.resize(w * h * 2);
  584. uint8_t *wa = imgdata_a.ptrw();
  585. PackedByteArray imgdata_ro;
  586. imgdata_ro.resize(w * h * 2);
  587. uint8_t *wro = imgdata_ro.ptrw();
  588. PackedByteArray imgdata_go;
  589. imgdata_go.resize(w * h * 2);
  590. uint8_t *wgo = imgdata_go.ptrw();
  591. PackedByteArray imgdata_bo;
  592. imgdata_bo.resize(w * h * 2);
  593. uint8_t *wbo = imgdata_bo.ptrw();
  594. PackedByteArray imgdata_ao;
  595. imgdata_ao.resize(w * h * 2);
  596. uint8_t *wao = imgdata_ao.ptrw();
  597. for (int i = 0; i < h; i++) {
  598. for (int j = 0; j < w; j++) {
  599. int ofs_src = (i * w + j) * 4;
  600. int ofs_dst = (i * w + j) * 2;
  601. wr[ofs_dst + 0] = 255;
  602. wro[ofs_dst + 0] = 255;
  603. if (r[ofs_src + 0] > 0x0F) {
  604. wr[ofs_dst + 1] = (r[ofs_src + 0] - 0x0F) * 2;
  605. wro[ofs_dst + 1] = 0;
  606. } else {
  607. wr[ofs_dst + 1] = 0;
  608. wro[ofs_dst + 1] = r[ofs_src + 0] * 2;
  609. }
  610. wg[ofs_dst + 0] = 255;
  611. wgo[ofs_dst + 0] = 255;
  612. if (r[ofs_src + 1] > 0x0F) {
  613. wg[ofs_dst + 1] = (r[ofs_src + 1] - 0x0F) * 2;
  614. wgo[ofs_dst + 1] = 0;
  615. } else {
  616. wg[ofs_dst + 1] = 0;
  617. wgo[ofs_dst + 1] = r[ofs_src + 1] * 2;
  618. }
  619. wb[ofs_dst + 0] = 255;
  620. wbo[ofs_dst + 0] = 255;
  621. if (r[ofs_src + 2] > 0x0F) {
  622. wb[ofs_dst + 1] = (r[ofs_src + 2] - 0x0F) * 2;
  623. wbo[ofs_dst + 1] = 0;
  624. } else {
  625. wb[ofs_dst + 1] = 0;
  626. wbo[ofs_dst + 1] = r[ofs_src + 2] * 2;
  627. }
  628. wa[ofs_dst + 0] = 255;
  629. wao[ofs_dst + 0] = 255;
  630. if (r[ofs_src + 3] > 0x0F) {
  631. wa[ofs_dst + 1] = (r[ofs_src + 3] - 0x0F) * 2;
  632. wao[ofs_dst + 1] = 0;
  633. } else {
  634. wa[ofs_dst + 1] = 0;
  635. wao[ofs_dst + 1] = r[ofs_src + 3] * 2;
  636. }
  637. }
  638. }
  639. Ref<Image> img_r = memnew(Image(w, h, false, Image::FORMAT_LA8, imgdata_r));
  640. set_texture_image(0, Vector2i(p_sz, 0), p_page * 4 + 0, img_r);
  641. Ref<Image> img_g = memnew(Image(w, h, false, Image::FORMAT_LA8, imgdata_g));
  642. set_texture_image(0, Vector2i(p_sz, 0), p_page * 4 + 1, img_g);
  643. Ref<Image> img_b = memnew(Image(w, h, false, Image::FORMAT_LA8, imgdata_b));
  644. set_texture_image(0, Vector2i(p_sz, 0), p_page * 4 + 2, img_b);
  645. Ref<Image> img_a = memnew(Image(w, h, false, Image::FORMAT_LA8, imgdata_a));
  646. set_texture_image(0, Vector2i(p_sz, 0), p_page * 4 + 3, img_a);
  647. Ref<Image> img_ro = memnew(Image(w, h, false, Image::FORMAT_LA8, imgdata_ro));
  648. set_texture_image(0, Vector2i(p_sz, 1), p_page * 4 + 0, img_ro);
  649. Ref<Image> img_go = memnew(Image(w, h, false, Image::FORMAT_LA8, imgdata_go));
  650. set_texture_image(0, Vector2i(p_sz, 1), p_page * 4 + 1, img_go);
  651. Ref<Image> img_bo = memnew(Image(w, h, false, Image::FORMAT_LA8, imgdata_bo));
  652. set_texture_image(0, Vector2i(p_sz, 1), p_page * 4 + 2, img_bo);
  653. Ref<Image> img_ao = memnew(Image(w, h, false, Image::FORMAT_LA8, imgdata_ao));
  654. set_texture_image(0, Vector2i(p_sz, 1), p_page * 4 + 3, img_ao);
  655. }
  656. void FontFile::_convert_rgba_4bit(Ref<Image> &p_source, int p_page, int p_sz) {
  657. int w = p_source->get_width();
  658. int h = p_source->get_height();
  659. PackedByteArray imgdata = p_source->get_data();
  660. const uint8_t *r = imgdata.ptr();
  661. PackedByteArray imgdata_g;
  662. imgdata_g.resize(w * h * 4);
  663. uint8_t *wg = imgdata_g.ptrw();
  664. PackedByteArray imgdata_o;
  665. imgdata_o.resize(w * h * 4);
  666. uint8_t *wo = imgdata_o.ptrw();
  667. for (int i = 0; i < h; i++) {
  668. for (int j = 0; j < w; j++) {
  669. int ofs = (i * w + j) * 4;
  670. if (r[ofs + 0] > 0x7F) {
  671. wg[ofs + 0] = r[ofs + 0];
  672. wo[ofs + 0] = 0;
  673. } else {
  674. wg[ofs + 0] = 0;
  675. wo[ofs + 0] = r[ofs + 0] * 2;
  676. }
  677. if (r[ofs + 1] > 0x7F) {
  678. wg[ofs + 1] = r[ofs + 1];
  679. wo[ofs + 1] = 0;
  680. } else {
  681. wg[ofs + 1] = 0;
  682. wo[ofs + 1] = r[ofs + 1] * 2;
  683. }
  684. if (r[ofs + 2] > 0x7F) {
  685. wg[ofs + 2] = r[ofs + 2];
  686. wo[ofs + 2] = 0;
  687. } else {
  688. wg[ofs + 2] = 0;
  689. wo[ofs + 2] = r[ofs + 2] * 2;
  690. }
  691. if (r[ofs + 3] > 0x7F) {
  692. wg[ofs + 3] = r[ofs + 3];
  693. wo[ofs + 3] = 0;
  694. } else {
  695. wg[ofs + 3] = 0;
  696. wo[ofs + 3] = r[ofs + 3] * 2;
  697. }
  698. }
  699. }
  700. Ref<Image> img_g = memnew(Image(w, h, false, Image::FORMAT_RGBA8, imgdata_g));
  701. set_texture_image(0, Vector2i(p_sz, 0), p_page, img_g);
  702. Ref<Image> img_o = memnew(Image(w, h, false, Image::FORMAT_RGBA8, imgdata_o));
  703. set_texture_image(0, Vector2i(p_sz, 1), p_page, img_o);
  704. }
  705. void FontFile::_convert_mono_8bit(Ref<Image> &p_source, int p_page, int p_ch, int p_sz, int p_ol) {
  706. int w = p_source->get_width();
  707. int h = p_source->get_height();
  708. PackedByteArray imgdata = p_source->get_data();
  709. const uint8_t *r = imgdata.ptr();
  710. int size = 4;
  711. if (p_source->get_format() == Image::FORMAT_L8) {
  712. size = 1;
  713. p_ch = 0;
  714. }
  715. PackedByteArray imgdata_g;
  716. imgdata_g.resize(w * h * 2);
  717. uint8_t *wg = imgdata_g.ptrw();
  718. for (int i = 0; i < h; i++) {
  719. for (int j = 0; j < w; j++) {
  720. int ofs_src = (i * w + j) * size;
  721. int ofs_dst = (i * w + j) * 2;
  722. wg[ofs_dst + 0] = 255;
  723. wg[ofs_dst + 1] = r[ofs_src + p_ch];
  724. }
  725. }
  726. Ref<Image> img_g = memnew(Image(w, h, false, Image::FORMAT_LA8, imgdata_g));
  727. set_texture_image(0, Vector2i(p_sz, p_ol), p_page, img_g);
  728. }
  729. void FontFile::_convert_mono_4bit(Ref<Image> &p_source, int p_page, int p_ch, int p_sz, int p_ol) {
  730. int w = p_source->get_width();
  731. int h = p_source->get_height();
  732. PackedByteArray imgdata = p_source->get_data();
  733. const uint8_t *r = imgdata.ptr();
  734. int size = 4;
  735. if (p_source->get_format() == Image::FORMAT_L8) {
  736. size = 1;
  737. p_ch = 0;
  738. }
  739. PackedByteArray imgdata_g;
  740. imgdata_g.resize(w * h * 2);
  741. uint8_t *wg = imgdata_g.ptrw();
  742. PackedByteArray imgdata_o;
  743. imgdata_o.resize(w * h * 2);
  744. uint8_t *wo = imgdata_o.ptrw();
  745. for (int i = 0; i < h; i++) {
  746. for (int j = 0; j < w; j++) {
  747. int ofs_src = (i * w + j) * size;
  748. int ofs_dst = (i * w + j) * 2;
  749. wg[ofs_dst + 0] = 255;
  750. wo[ofs_dst + 0] = 255;
  751. if (r[ofs_src + p_ch] > 0x7F) {
  752. wg[ofs_dst + 1] = r[ofs_src + p_ch];
  753. wo[ofs_dst + 1] = 0;
  754. } else {
  755. wg[ofs_dst + 1] = 0;
  756. wo[ofs_dst + 1] = r[ofs_src + p_ch] * 2;
  757. }
  758. }
  759. }
  760. Ref<Image> img_g = memnew(Image(w, h, false, Image::FORMAT_LA8, imgdata_g));
  761. set_texture_image(0, Vector2i(p_sz, 0), p_page, img_g);
  762. Ref<Image> img_o = memnew(Image(w, h, false, Image::FORMAT_LA8, imgdata_o));
  763. set_texture_image(0, Vector2i(p_sz, p_ol), p_page, img_o);
  764. }
  765. void FontFile::_bind_methods() {
  766. ClassDB::bind_method(D_METHOD("load_bitmap_font", "path"), &FontFile::load_bitmap_font);
  767. ClassDB::bind_method(D_METHOD("load_dynamic_font", "path"), &FontFile::load_dynamic_font);
  768. ClassDB::bind_method(D_METHOD("set_data", "data"), &FontFile::set_data);
  769. ClassDB::bind_method(D_METHOD("get_data"), &FontFile::get_data);
  770. ClassDB::bind_method(D_METHOD("set_font_name", "name"), &FontFile::set_font_name);
  771. ClassDB::bind_method(D_METHOD("set_font_style_name", "name"), &FontFile::set_font_style_name);
  772. ClassDB::bind_method(D_METHOD("set_font_style", "style"), &FontFile::set_font_style);
  773. ClassDB::bind_method(D_METHOD("set_font_weight", "weight"), &FontFile::set_font_weight);
  774. ClassDB::bind_method(D_METHOD("set_font_stretch", "stretch"), &FontFile::set_font_stretch);
  775. ClassDB::bind_method(D_METHOD("set_antialiasing", "antialiasing"), &FontFile::set_antialiasing);
  776. ClassDB::bind_method(D_METHOD("get_antialiasing"), &FontFile::get_antialiasing);
  777. ClassDB::bind_method(D_METHOD("set_disable_embedded_bitmaps", "disable_embedded_bitmaps"), &FontFile::set_disable_embedded_bitmaps);
  778. ClassDB::bind_method(D_METHOD("get_disable_embedded_bitmaps"), &FontFile::get_disable_embedded_bitmaps);
  779. ClassDB::bind_method(D_METHOD("set_generate_mipmaps", "generate_mipmaps"), &FontFile::set_generate_mipmaps);
  780. ClassDB::bind_method(D_METHOD("get_generate_mipmaps"), &FontFile::get_generate_mipmaps);
  781. ClassDB::bind_method(D_METHOD("set_multichannel_signed_distance_field", "msdf"), &FontFile::set_multichannel_signed_distance_field);
  782. ClassDB::bind_method(D_METHOD("is_multichannel_signed_distance_field"), &FontFile::is_multichannel_signed_distance_field);
  783. ClassDB::bind_method(D_METHOD("set_msdf_pixel_range", "msdf_pixel_range"), &FontFile::set_msdf_pixel_range);
  784. ClassDB::bind_method(D_METHOD("get_msdf_pixel_range"), &FontFile::get_msdf_pixel_range);
  785. ClassDB::bind_method(D_METHOD("set_msdf_size", "msdf_size"), &FontFile::set_msdf_size);
  786. ClassDB::bind_method(D_METHOD("get_msdf_size"), &FontFile::get_msdf_size);
  787. ClassDB::bind_method(D_METHOD("set_fixed_size", "fixed_size"), &FontFile::set_fixed_size);
  788. ClassDB::bind_method(D_METHOD("get_fixed_size"), &FontFile::get_fixed_size);
  789. ClassDB::bind_method(D_METHOD("set_fixed_size_scale_mode", "fixed_size_scale_mode"), &FontFile::set_fixed_size_scale_mode);
  790. ClassDB::bind_method(D_METHOD("get_fixed_size_scale_mode"), &FontFile::get_fixed_size_scale_mode);
  791. ClassDB::bind_method(D_METHOD("set_allow_system_fallback", "allow_system_fallback"), &FontFile::set_allow_system_fallback);
  792. ClassDB::bind_method(D_METHOD("is_allow_system_fallback"), &FontFile::is_allow_system_fallback);
  793. ClassDB::bind_method(D_METHOD("set_force_autohinter", "force_autohinter"), &FontFile::set_force_autohinter);
  794. ClassDB::bind_method(D_METHOD("is_force_autohinter"), &FontFile::is_force_autohinter);
  795. ClassDB::bind_method(D_METHOD("set_hinting", "hinting"), &FontFile::set_hinting);
  796. ClassDB::bind_method(D_METHOD("get_hinting"), &FontFile::get_hinting);
  797. ClassDB::bind_method(D_METHOD("set_subpixel_positioning", "subpixel_positioning"), &FontFile::set_subpixel_positioning);
  798. ClassDB::bind_method(D_METHOD("get_subpixel_positioning"), &FontFile::get_subpixel_positioning);
  799. ClassDB::bind_method(D_METHOD("set_oversampling", "oversampling"), &FontFile::set_oversampling);
  800. ClassDB::bind_method(D_METHOD("get_oversampling"), &FontFile::get_oversampling);
  801. ClassDB::bind_method(D_METHOD("get_cache_count"), &FontFile::get_cache_count);
  802. ClassDB::bind_method(D_METHOD("clear_cache"), &FontFile::clear_cache);
  803. ClassDB::bind_method(D_METHOD("remove_cache", "cache_index"), &FontFile::remove_cache);
  804. ClassDB::bind_method(D_METHOD("get_size_cache_list", "cache_index"), &FontFile::get_size_cache_list);
  805. ClassDB::bind_method(D_METHOD("clear_size_cache", "cache_index"), &FontFile::clear_size_cache);
  806. ClassDB::bind_method(D_METHOD("remove_size_cache", "cache_index", "size"), &FontFile::remove_size_cache);
  807. ClassDB::bind_method(D_METHOD("set_variation_coordinates", "cache_index", "variation_coordinates"), &FontFile::set_variation_coordinates);
  808. ClassDB::bind_method(D_METHOD("get_variation_coordinates", "cache_index"), &FontFile::get_variation_coordinates);
  809. ClassDB::bind_method(D_METHOD("set_embolden", "cache_index", "strength"), &FontFile::set_embolden);
  810. ClassDB::bind_method(D_METHOD("get_embolden", "cache_index"), &FontFile::get_embolden);
  811. ClassDB::bind_method(D_METHOD("set_transform", "cache_index", "transform"), &FontFile::set_transform);
  812. ClassDB::bind_method(D_METHOD("get_transform", "cache_index"), &FontFile::get_transform);
  813. ClassDB::bind_method(D_METHOD("set_extra_spacing", "cache_index", "spacing", "value"), &FontFile::set_extra_spacing);
  814. ClassDB::bind_method(D_METHOD("get_extra_spacing", "cache_index", "spacing"), &FontFile::get_extra_spacing);
  815. ClassDB::bind_method(D_METHOD("set_extra_baseline_offset", "cache_index", "baseline_offset"), &FontFile::set_extra_baseline_offset);
  816. ClassDB::bind_method(D_METHOD("get_extra_baseline_offset", "cache_index"), &FontFile::get_extra_baseline_offset);
  817. ClassDB::bind_method(D_METHOD("set_face_index", "cache_index", "face_index"), &FontFile::set_face_index);
  818. ClassDB::bind_method(D_METHOD("get_face_index", "cache_index"), &FontFile::get_face_index);
  819. ClassDB::bind_method(D_METHOD("set_cache_ascent", "cache_index", "size", "ascent"), &FontFile::set_cache_ascent);
  820. ClassDB::bind_method(D_METHOD("get_cache_ascent", "cache_index", "size"), &FontFile::get_cache_ascent);
  821. ClassDB::bind_method(D_METHOD("set_cache_descent", "cache_index", "size", "descent"), &FontFile::set_cache_descent);
  822. ClassDB::bind_method(D_METHOD("get_cache_descent", "cache_index", "size"), &FontFile::get_cache_descent);
  823. ClassDB::bind_method(D_METHOD("set_cache_underline_position", "cache_index", "size", "underline_position"), &FontFile::set_cache_underline_position);
  824. ClassDB::bind_method(D_METHOD("get_cache_underline_position", "cache_index", "size"), &FontFile::get_cache_underline_position);
  825. ClassDB::bind_method(D_METHOD("set_cache_underline_thickness", "cache_index", "size", "underline_thickness"), &FontFile::set_cache_underline_thickness);
  826. ClassDB::bind_method(D_METHOD("get_cache_underline_thickness", "cache_index", "size"), &FontFile::get_cache_underline_thickness);
  827. ClassDB::bind_method(D_METHOD("set_cache_scale", "cache_index", "size", "scale"), &FontFile::set_cache_scale);
  828. ClassDB::bind_method(D_METHOD("get_cache_scale", "cache_index", "size"), &FontFile::get_cache_scale);
  829. ClassDB::bind_method(D_METHOD("get_texture_count", "cache_index", "size"), &FontFile::get_texture_count);
  830. ClassDB::bind_method(D_METHOD("clear_textures", "cache_index", "size"), &FontFile::clear_textures);
  831. ClassDB::bind_method(D_METHOD("remove_texture", "cache_index", "size", "texture_index"), &FontFile::remove_texture);
  832. ClassDB::bind_method(D_METHOD("set_texture_image", "cache_index", "size", "texture_index", "image"), &FontFile::set_texture_image);
  833. ClassDB::bind_method(D_METHOD("get_texture_image", "cache_index", "size", "texture_index"), &FontFile::get_texture_image);
  834. ClassDB::bind_method(D_METHOD("set_texture_offsets", "cache_index", "size", "texture_index", "offset"), &FontFile::set_texture_offsets);
  835. ClassDB::bind_method(D_METHOD("get_texture_offsets", "cache_index", "size", "texture_index"), &FontFile::get_texture_offsets);
  836. ClassDB::bind_method(D_METHOD("get_glyph_list", "cache_index", "size"), &FontFile::get_glyph_list);
  837. ClassDB::bind_method(D_METHOD("clear_glyphs", "cache_index", "size"), &FontFile::clear_glyphs);
  838. ClassDB::bind_method(D_METHOD("remove_glyph", "cache_index", "size", "glyph"), &FontFile::remove_glyph);
  839. ClassDB::bind_method(D_METHOD("set_glyph_advance", "cache_index", "size", "glyph", "advance"), &FontFile::set_glyph_advance);
  840. ClassDB::bind_method(D_METHOD("get_glyph_advance", "cache_index", "size", "glyph"), &FontFile::get_glyph_advance);
  841. ClassDB::bind_method(D_METHOD("set_glyph_offset", "cache_index", "size", "glyph", "offset"), &FontFile::set_glyph_offset);
  842. ClassDB::bind_method(D_METHOD("get_glyph_offset", "cache_index", "size", "glyph"), &FontFile::get_glyph_offset);
  843. ClassDB::bind_method(D_METHOD("set_glyph_size", "cache_index", "size", "glyph", "gl_size"), &FontFile::set_glyph_size);
  844. ClassDB::bind_method(D_METHOD("get_glyph_size", "cache_index", "size", "glyph"), &FontFile::get_glyph_size);
  845. ClassDB::bind_method(D_METHOD("set_glyph_uv_rect", "cache_index", "size", "glyph", "uv_rect"), &FontFile::set_glyph_uv_rect);
  846. ClassDB::bind_method(D_METHOD("get_glyph_uv_rect", "cache_index", "size", "glyph"), &FontFile::get_glyph_uv_rect);
  847. ClassDB::bind_method(D_METHOD("set_glyph_texture_idx", "cache_index", "size", "glyph", "texture_idx"), &FontFile::set_glyph_texture_idx);
  848. ClassDB::bind_method(D_METHOD("get_glyph_texture_idx", "cache_index", "size", "glyph"), &FontFile::get_glyph_texture_idx);
  849. ClassDB::bind_method(D_METHOD("get_kerning_list", "cache_index", "size"), &FontFile::get_kerning_list);
  850. ClassDB::bind_method(D_METHOD("clear_kerning_map", "cache_index", "size"), &FontFile::clear_kerning_map);
  851. ClassDB::bind_method(D_METHOD("remove_kerning", "cache_index", "size", "glyph_pair"), &FontFile::remove_kerning);
  852. ClassDB::bind_method(D_METHOD("set_kerning", "cache_index", "size", "glyph_pair", "kerning"), &FontFile::set_kerning);
  853. ClassDB::bind_method(D_METHOD("get_kerning", "cache_index", "size", "glyph_pair"), &FontFile::get_kerning);
  854. ClassDB::bind_method(D_METHOD("render_range", "cache_index", "size", "start", "end"), &FontFile::render_range);
  855. ClassDB::bind_method(D_METHOD("render_glyph", "cache_index", "size", "index"), &FontFile::render_glyph);
  856. ClassDB::bind_method(D_METHOD("set_language_support_override", "language", "supported"), &FontFile::set_language_support_override);
  857. ClassDB::bind_method(D_METHOD("get_language_support_override", "language"), &FontFile::get_language_support_override);
  858. ClassDB::bind_method(D_METHOD("remove_language_support_override", "language"), &FontFile::remove_language_support_override);
  859. ClassDB::bind_method(D_METHOD("get_language_support_overrides"), &FontFile::get_language_support_overrides);
  860. ClassDB::bind_method(D_METHOD("set_script_support_override", "script", "supported"), &FontFile::set_script_support_override);
  861. ClassDB::bind_method(D_METHOD("get_script_support_override", "script"), &FontFile::get_script_support_override);
  862. ClassDB::bind_method(D_METHOD("remove_script_support_override", "script"), &FontFile::remove_script_support_override);
  863. ClassDB::bind_method(D_METHOD("get_script_support_overrides"), &FontFile::get_script_support_overrides);
  864. ClassDB::bind_method(D_METHOD("set_opentype_feature_overrides", "overrides"), &FontFile::set_opentype_feature_overrides);
  865. ClassDB::bind_method(D_METHOD("get_opentype_feature_overrides"), &FontFile::get_opentype_feature_overrides);
  866. ClassDB::bind_method(D_METHOD("get_glyph_index", "size", "char", "variation_selector"), &FontFile::get_glyph_index);
  867. ClassDB::bind_method(D_METHOD("get_char_from_glyph_index", "size", "glyph_index"), &FontFile::get_char_from_glyph_index);
  868. ADD_PROPERTY(PropertyInfo(Variant::PACKED_BYTE_ARRAY, "data", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE), "set_data", "get_data");
  869. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "generate_mipmaps", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE), "set_generate_mipmaps", "get_generate_mipmaps");
  870. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "disable_embedded_bitmaps", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE), "set_disable_embedded_bitmaps", "get_disable_embedded_bitmaps");
  871. ADD_PROPERTY(PropertyInfo(Variant::INT, "antialiasing", PROPERTY_HINT_ENUM, "None,Grayscale,LCD Subpixel", PROPERTY_USAGE_STORAGE), "set_antialiasing", "get_antialiasing");
  872. ADD_PROPERTY(PropertyInfo(Variant::STRING, "font_name", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE), "set_font_name", "get_font_name");
  873. ADD_PROPERTY(PropertyInfo(Variant::STRING, "style_name", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE), "set_font_style_name", "get_font_style_name");
  874. ADD_PROPERTY(PropertyInfo(Variant::INT, "font_style", PROPERTY_HINT_FLAGS, "Bold,Italic,Fixed Size", PROPERTY_USAGE_STORAGE), "set_font_style", "get_font_style");
  875. ADD_PROPERTY(PropertyInfo(Variant::INT, "font_weight", PROPERTY_HINT_RANGE, "100,999,25", PROPERTY_USAGE_STORAGE), "set_font_weight", "get_font_weight");
  876. ADD_PROPERTY(PropertyInfo(Variant::INT, "font_stretch", PROPERTY_HINT_RANGE, "50,200,25", PROPERTY_USAGE_STORAGE), "set_font_stretch", "get_font_stretch");
  877. ADD_PROPERTY(PropertyInfo(Variant::INT, "subpixel_positioning", PROPERTY_HINT_ENUM, "Disabled,Auto,One Half of a Pixel,One Quarter of a Pixel", PROPERTY_USAGE_STORAGE), "set_subpixel_positioning", "get_subpixel_positioning");
  878. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "multichannel_signed_distance_field", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE), "set_multichannel_signed_distance_field", "is_multichannel_signed_distance_field");
  879. ADD_PROPERTY(PropertyInfo(Variant::INT, "msdf_pixel_range", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE), "set_msdf_pixel_range", "get_msdf_pixel_range");
  880. ADD_PROPERTY(PropertyInfo(Variant::INT, "msdf_size", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE), "set_msdf_size", "get_msdf_size");
  881. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "allow_system_fallback", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE), "set_allow_system_fallback", "is_allow_system_fallback");
  882. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "force_autohinter", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE), "set_force_autohinter", "is_force_autohinter");
  883. ADD_PROPERTY(PropertyInfo(Variant::INT, "hinting", PROPERTY_HINT_ENUM, "None,Light,Normal", PROPERTY_USAGE_STORAGE), "set_hinting", "get_hinting");
  884. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "oversampling", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE), "set_oversampling", "get_oversampling");
  885. ADD_PROPERTY(PropertyInfo(Variant::INT, "fixed_size", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE), "set_fixed_size", "get_fixed_size");
  886. ADD_PROPERTY(PropertyInfo(Variant::INT, "fixed_size_scale_mode", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE), "set_fixed_size_scale_mode", "get_fixed_size_scale_mode");
  887. ADD_PROPERTY(PropertyInfo(Variant::DICTIONARY, "opentype_feature_overrides", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE), "set_opentype_feature_overrides", "get_opentype_feature_overrides");
  888. }
  889. void FontFile::_validate_property(PropertyInfo &p_property) const {
  890. if (p_property.name == "fallbacks") {
  891. p_property.usage &= ~PROPERTY_USAGE_EDITOR;
  892. }
  893. }
  894. bool FontFile::_set(const StringName &p_name, const Variant &p_value) {
  895. Vector<String> tokens = p_name.operator String().split("/");
  896. #ifndef DISABLE_DEPRECATED
  897. if (tokens.size() == 1 && tokens[0] == "font_path") {
  898. // Compatibility, DynamicFontData.
  899. load_dynamic_font(p_value);
  900. } else if (tokens.size() == 1 && tokens[0] == "override_oversampling") {
  901. set_oversampling(p_value);
  902. }
  903. if (tokens.size() == 1 && tokens[0] == "font_data") {
  904. // Compatibility, DynamicFont.
  905. Ref<Font> f = p_value;
  906. if (f.is_valid()) {
  907. fallbacks.push_back(f);
  908. return true;
  909. }
  910. return false;
  911. } else if (tokens.size() == 2 && tokens[0] == "fallback") {
  912. // Compatibility, DynamicFont.
  913. Ref<FontFile> f = p_value;
  914. if (f.is_valid()) {
  915. fallbacks.push_back(f);
  916. return true;
  917. }
  918. return false;
  919. } else if (tokens.size() == 1 && tokens[0] == "textures") {
  920. // Compatibility, BitmapFont.
  921. set_fixed_size(16);
  922. Array textures = p_value;
  923. for (int i = 0; i < textures.size(); i++) {
  924. Ref<ImageTexture> tex = textures[i];
  925. ERR_CONTINUE(!tex.is_valid());
  926. set_texture_image(0, Vector2i(16, 0), i, tex->get_image());
  927. }
  928. } else if (tokens.size() == 1 && tokens[0] == "chars") {
  929. // Compatibility, BitmapFont.
  930. set_fixed_size(16);
  931. PackedInt32Array arr = p_value;
  932. int len = arr.size();
  933. ERR_FAIL_COND_V(len % 9, false);
  934. if (!len) {
  935. return false;
  936. }
  937. int chars = len / 9;
  938. for (int i = 0; i < chars; i++) {
  939. const int32_t *char_data = &arr[i * 9];
  940. char32_t c = char_data[0];
  941. set_glyph_texture_idx(0, Vector2i(16, 0), c, char_data[1]);
  942. set_glyph_uv_rect(0, Vector2i(16, 0), c, Rect2(char_data[2], char_data[3], char_data[4], char_data[5]));
  943. set_glyph_offset(0, Vector2i(16, 0), c, Size2(char_data[6], char_data[7]));
  944. set_glyph_advance(0, 16, c, Vector2(char_data[8], 0));
  945. }
  946. } else if (tokens.size() == 1 && tokens[0] == "kernings") {
  947. // Compatibility, BitmapFont.
  948. set_fixed_size(16);
  949. PackedInt32Array arr = p_value;
  950. int len = arr.size();
  951. ERR_FAIL_COND_V(len % 3, false);
  952. if (!len) {
  953. return false;
  954. }
  955. for (int i = 0; i < len / 3; i++) {
  956. const int32_t *kern_data = &arr[i * 3];
  957. set_kerning(0, 16, Vector2i(kern_data[0], kern_data[1]), Vector2(kern_data[2], 0));
  958. }
  959. } else if (tokens.size() == 1 && tokens[0] == "height") {
  960. // Compatibility, BitmapFont.
  961. bmp_height = p_value;
  962. set_fixed_size(16);
  963. set_cache_descent(0, 16, bmp_height - bmp_ascent);
  964. } else if (tokens.size() == 1 && tokens[0] == "ascent") {
  965. // Compatibility, BitmapFont.
  966. bmp_ascent = p_value;
  967. set_fixed_size(16);
  968. set_cache_ascent(0, 16, bmp_ascent);
  969. set_cache_descent(0, 16, bmp_height - bmp_ascent);
  970. } else if (tokens.size() == 1 && tokens[0] == "fallback") {
  971. // Compatibility, BitmapFont.
  972. Ref<Font> f = p_value;
  973. if (f.is_valid()) {
  974. fallbacks.push_back(f);
  975. return true;
  976. }
  977. return false;
  978. }
  979. #endif // DISABLE_DEPRECATED
  980. if (tokens.size() == 2 && tokens[0] == "language_support_override") {
  981. const String &lang_code = tokens[1];
  982. set_language_support_override(lang_code, p_value);
  983. return true;
  984. } else if (tokens.size() == 2 && tokens[0] == "script_support_override") {
  985. const String &script_code = tokens[1];
  986. set_script_support_override(script_code, p_value);
  987. return true;
  988. } else if (tokens.size() >= 3 && tokens[0] == "cache") {
  989. int cache_index = tokens[1].to_int();
  990. if (tokens.size() == 3 && tokens[2] == "variation_coordinates") {
  991. set_variation_coordinates(cache_index, p_value);
  992. return true;
  993. } else if (tokens.size() == 3 && tokens[2] == "embolden") {
  994. set_embolden(cache_index, p_value);
  995. return true;
  996. } else if (tokens.size() == 3 && tokens[2] == "face_index") {
  997. set_face_index(cache_index, p_value);
  998. return true;
  999. } else if (tokens.size() == 3 && tokens[2] == "transform") {
  1000. set_transform(cache_index, p_value);
  1001. return true;
  1002. } else if (tokens.size() == 3 && tokens[2] == "spacing_top") {
  1003. set_extra_spacing(cache_index, TextServer::SPACING_TOP, p_value);
  1004. return true;
  1005. } else if (tokens.size() == 3 && tokens[2] == "spacing_bottom") {
  1006. set_extra_spacing(cache_index, TextServer::SPACING_BOTTOM, p_value);
  1007. return true;
  1008. } else if (tokens.size() == 3 && tokens[2] == "spacing_space") {
  1009. set_extra_spacing(cache_index, TextServer::SPACING_SPACE, p_value);
  1010. return true;
  1011. } else if (tokens.size() == 3 && tokens[2] == "spacing_glyph") {
  1012. set_extra_spacing(cache_index, TextServer::SPACING_GLYPH, p_value);
  1013. return true;
  1014. } else if (tokens.size() == 3 && tokens[2] == "baseline_offset") {
  1015. set_extra_baseline_offset(cache_index, p_value);
  1016. return true;
  1017. }
  1018. if (tokens.size() >= 5) {
  1019. Vector2i sz = Vector2i(tokens[2].to_int(), tokens[3].to_int());
  1020. if (tokens[4] == "ascent") {
  1021. set_cache_ascent(cache_index, sz.x, p_value);
  1022. return true;
  1023. } else if (tokens[4] == "descent") {
  1024. set_cache_descent(cache_index, sz.x, p_value);
  1025. return true;
  1026. } else if (tokens[4] == "underline_position") {
  1027. set_cache_underline_position(cache_index, sz.x, p_value);
  1028. return true;
  1029. } else if (tokens[4] == "underline_thickness") {
  1030. set_cache_underline_thickness(cache_index, sz.x, p_value);
  1031. return true;
  1032. } else if (tokens[4] == "scale") {
  1033. set_cache_scale(cache_index, sz.x, p_value);
  1034. return true;
  1035. } else if (tokens.size() == 7 && tokens[4] == "textures") {
  1036. int texture_index = tokens[5].to_int();
  1037. if (tokens[6] == "image") {
  1038. set_texture_image(cache_index, sz, texture_index, p_value);
  1039. return true;
  1040. } else if (tokens[6] == "offsets") {
  1041. set_texture_offsets(cache_index, sz, texture_index, p_value);
  1042. return true;
  1043. }
  1044. } else if (tokens.size() == 7 && tokens[4] == "glyphs") {
  1045. int32_t glyph_index = tokens[5].to_int();
  1046. if (tokens[6] == "advance") {
  1047. set_glyph_advance(cache_index, sz.x, glyph_index, p_value);
  1048. return true;
  1049. } else if (tokens[6] == "offset") {
  1050. set_glyph_offset(cache_index, sz, glyph_index, p_value);
  1051. return true;
  1052. } else if (tokens[6] == "size") {
  1053. set_glyph_size(cache_index, sz, glyph_index, p_value);
  1054. return true;
  1055. } else if (tokens[6] == "uv_rect") {
  1056. set_glyph_uv_rect(cache_index, sz, glyph_index, p_value);
  1057. return true;
  1058. } else if (tokens[6] == "texture_idx") {
  1059. set_glyph_texture_idx(cache_index, sz, glyph_index, p_value);
  1060. return true;
  1061. }
  1062. } else if (tokens.size() == 7 && tokens[4] == "kerning_overrides") {
  1063. Vector2i gp = Vector2i(tokens[5].to_int(), tokens[6].to_int());
  1064. set_kerning(cache_index, sz.x, gp, p_value);
  1065. return true;
  1066. }
  1067. }
  1068. }
  1069. return false;
  1070. }
  1071. bool FontFile::_get(const StringName &p_name, Variant &r_ret) const {
  1072. Vector<String> tokens = p_name.operator String().split("/");
  1073. if (tokens.size() == 2 && tokens[0] == "language_support_override") {
  1074. const String &lang_code = tokens[1];
  1075. r_ret = get_language_support_override(lang_code);
  1076. return true;
  1077. } else if (tokens.size() == 2 && tokens[0] == "script_support_override") {
  1078. const String &script_code = tokens[1];
  1079. r_ret = get_script_support_override(script_code);
  1080. return true;
  1081. } else if (tokens.size() >= 3 && tokens[0] == "cache") {
  1082. int cache_index = tokens[1].to_int();
  1083. if (tokens.size() == 3 && tokens[2] == "variation_coordinates") {
  1084. r_ret = get_variation_coordinates(cache_index);
  1085. return true;
  1086. } else if (tokens.size() == 3 && tokens[2] == "embolden") {
  1087. r_ret = get_embolden(cache_index);
  1088. return true;
  1089. } else if (tokens.size() == 3 && tokens[2] == "face_index") {
  1090. r_ret = get_face_index(cache_index);
  1091. return true;
  1092. } else if (tokens.size() == 3 && tokens[2] == "transform") {
  1093. r_ret = get_transform(cache_index);
  1094. return true;
  1095. } else if (tokens.size() == 3 && tokens[2] == "spacing_top") {
  1096. r_ret = get_extra_spacing(cache_index, TextServer::SPACING_TOP);
  1097. return true;
  1098. } else if (tokens.size() == 3 && tokens[2] == "spacing_bottom") {
  1099. r_ret = get_extra_spacing(cache_index, TextServer::SPACING_BOTTOM);
  1100. return true;
  1101. } else if (tokens.size() == 3 && tokens[2] == "spacing_space") {
  1102. r_ret = get_extra_spacing(cache_index, TextServer::SPACING_SPACE);
  1103. return true;
  1104. } else if (tokens.size() == 3 && tokens[2] == "spacing_glyph") {
  1105. r_ret = get_extra_spacing(cache_index, TextServer::SPACING_GLYPH);
  1106. return true;
  1107. } else if (tokens.size() == 3 && tokens[2] == "baseline_offset") {
  1108. r_ret = get_extra_baseline_offset(cache_index);
  1109. return true;
  1110. }
  1111. if (tokens.size() >= 5) {
  1112. Vector2i sz = Vector2i(tokens[2].to_int(), tokens[3].to_int());
  1113. if (tokens[4] == "ascent") {
  1114. r_ret = get_cache_ascent(cache_index, sz.x);
  1115. return true;
  1116. } else if (tokens[4] == "descent") {
  1117. r_ret = get_cache_descent(cache_index, sz.x);
  1118. return true;
  1119. } else if (tokens[4] == "underline_position") {
  1120. r_ret = get_cache_underline_position(cache_index, sz.x);
  1121. return true;
  1122. } else if (tokens[4] == "underline_thickness") {
  1123. r_ret = get_cache_underline_thickness(cache_index, sz.x);
  1124. return true;
  1125. } else if (tokens[4] == "scale") {
  1126. r_ret = get_cache_scale(cache_index, sz.x);
  1127. return true;
  1128. } else if (tokens.size() == 7 && tokens[4] == "textures") {
  1129. int texture_index = tokens[5].to_int();
  1130. if (tokens[6] == "image") {
  1131. r_ret = get_texture_image(cache_index, sz, texture_index);
  1132. return true;
  1133. } else if (tokens[6] == "offsets") {
  1134. r_ret = get_texture_offsets(cache_index, sz, texture_index);
  1135. return true;
  1136. }
  1137. } else if (tokens.size() == 7 && tokens[4] == "glyphs") {
  1138. int32_t glyph_index = tokens[5].to_int();
  1139. if (tokens[6] == "advance") {
  1140. r_ret = get_glyph_advance(cache_index, sz.x, glyph_index);
  1141. return true;
  1142. } else if (tokens[6] == "offset") {
  1143. r_ret = get_glyph_offset(cache_index, sz, glyph_index);
  1144. return true;
  1145. } else if (tokens[6] == "size") {
  1146. r_ret = get_glyph_size(cache_index, sz, glyph_index);
  1147. return true;
  1148. } else if (tokens[6] == "uv_rect") {
  1149. r_ret = get_glyph_uv_rect(cache_index, sz, glyph_index);
  1150. return true;
  1151. } else if (tokens[6] == "texture_idx") {
  1152. r_ret = get_glyph_texture_idx(cache_index, sz, glyph_index);
  1153. return true;
  1154. }
  1155. } else if (tokens.size() == 7 && tokens[4] == "kerning_overrides") {
  1156. Vector2i gp = Vector2i(tokens[5].to_int(), tokens[6].to_int());
  1157. r_ret = get_kerning(cache_index, sz.x, gp);
  1158. return true;
  1159. }
  1160. }
  1161. }
  1162. return false;
  1163. }
  1164. void FontFile::_get_property_list(List<PropertyInfo> *p_list) const {
  1165. Vector<String> lang_over = get_language_support_overrides();
  1166. for (int i = 0; i < lang_over.size(); i++) {
  1167. p_list->push_back(PropertyInfo(Variant::BOOL, "language_support_override/" + lang_over[i], PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE));
  1168. }
  1169. Vector<String> scr_over = get_script_support_overrides();
  1170. for (int i = 0; i < scr_over.size(); i++) {
  1171. p_list->push_back(PropertyInfo(Variant::BOOL, "script_support_override/" + scr_over[i], PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE));
  1172. }
  1173. for (int i = 0; i < cache.size(); i++) {
  1174. String prefix = "cache/" + itos(i) + "/";
  1175. TypedArray<Vector2i> sizes = get_size_cache_list(i);
  1176. p_list->push_back(PropertyInfo(Variant::DICTIONARY, prefix + "variation_coordinates", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE));
  1177. p_list->push_back(PropertyInfo(Variant::INT, prefix + "face_index", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE));
  1178. p_list->push_back(PropertyInfo(Variant::FLOAT, prefix + "embolden", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE));
  1179. p_list->push_back(PropertyInfo(Variant::TRANSFORM2D, prefix + "transform", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE));
  1180. p_list->push_back(PropertyInfo(Variant::INT, prefix + "spacing_top", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE));
  1181. p_list->push_back(PropertyInfo(Variant::INT, prefix + "spacing_bottom", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE));
  1182. p_list->push_back(PropertyInfo(Variant::INT, prefix + "spacing_space", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE));
  1183. p_list->push_back(PropertyInfo(Variant::INT, prefix + "spacing_glyph", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE));
  1184. p_list->push_back(PropertyInfo(Variant::FLOAT, prefix + "baseline_offset", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE));
  1185. for (int j = 0; j < sizes.size(); j++) {
  1186. Vector2i sz = sizes[j];
  1187. String prefix_sz = prefix + itos(sz.x) + "/" + itos(sz.y) + "/";
  1188. if (sz.y == 0) {
  1189. p_list->push_back(PropertyInfo(Variant::FLOAT, prefix_sz + "ascent", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE));
  1190. p_list->push_back(PropertyInfo(Variant::FLOAT, prefix_sz + "descent", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE));
  1191. p_list->push_back(PropertyInfo(Variant::FLOAT, prefix_sz + "underline_position", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE));
  1192. p_list->push_back(PropertyInfo(Variant::FLOAT, prefix_sz + "underline_thickness", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE));
  1193. p_list->push_back(PropertyInfo(Variant::FLOAT, prefix_sz + "scale", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE));
  1194. }
  1195. int tx_cnt = get_texture_count(i, sz);
  1196. for (int k = 0; k < tx_cnt; k++) {
  1197. p_list->push_back(PropertyInfo(Variant::PACKED_INT32_ARRAY, prefix_sz + "textures/" + itos(k) + "/offsets", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE));
  1198. p_list->push_back(PropertyInfo(Variant::OBJECT, prefix_sz + "textures/" + itos(k) + "/image", PROPERTY_HINT_RESOURCE_TYPE, "Image", PROPERTY_USAGE_STORAGE));
  1199. }
  1200. PackedInt32Array glyphs = get_glyph_list(i, sz);
  1201. for (int k = 0; k < glyphs.size(); k++) {
  1202. const int32_t &gl = glyphs[k];
  1203. if (sz.y == 0) {
  1204. p_list->push_back(PropertyInfo(Variant::VECTOR2, prefix_sz + "glyphs/" + itos(gl) + "/advance", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE));
  1205. }
  1206. p_list->push_back(PropertyInfo(Variant::VECTOR2, prefix_sz + "glyphs/" + itos(gl) + "/offset", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE));
  1207. p_list->push_back(PropertyInfo(Variant::VECTOR2, prefix_sz + "glyphs/" + itos(gl) + "/size", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE));
  1208. p_list->push_back(PropertyInfo(Variant::RECT2, prefix_sz + "glyphs/" + itos(gl) + "/uv_rect", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE));
  1209. p_list->push_back(PropertyInfo(Variant::INT, prefix_sz + "glyphs/" + itos(gl) + "/texture_idx", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE));
  1210. }
  1211. if (sz.y == 0) {
  1212. TypedArray<Vector2i> kerning_map = get_kerning_list(i, sz.x);
  1213. for (int k = 0; k < kerning_map.size(); k++) {
  1214. const Vector2i &gl_pair = kerning_map[k];
  1215. p_list->push_back(PropertyInfo(Variant::VECTOR2, prefix_sz + "kerning_overrides/" + itos(gl_pair.x) + "/" + itos(gl_pair.y), PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE));
  1216. }
  1217. }
  1218. }
  1219. }
  1220. }
  1221. void FontFile::reset_state() {
  1222. _clear_cache();
  1223. data.clear();
  1224. data_ptr = nullptr;
  1225. data_size = 0;
  1226. cache.clear();
  1227. antialiasing = TextServer::FONT_ANTIALIASING_GRAY;
  1228. mipmaps = false;
  1229. disable_embedded_bitmaps = true;
  1230. msdf = false;
  1231. force_autohinter = false;
  1232. allow_system_fallback = true;
  1233. hinting = TextServer::HINTING_LIGHT;
  1234. subpixel_positioning = TextServer::SUBPIXEL_POSITIONING_DISABLED;
  1235. msdf_pixel_range = 14;
  1236. msdf_size = 128;
  1237. fixed_size = 0;
  1238. fixed_size_scale_mode = TextServer::FIXED_SIZE_SCALE_DISABLE;
  1239. oversampling = 0.f;
  1240. Font::reset_state();
  1241. }
  1242. /*************************************************************************/
  1243. // OEM encoding mapping for 0x80..0xFF range.
  1244. static const char32_t _oem_to_unicode[][129] = {
  1245. U"\u20ac\ufffe\u201a\ufffe\u201e\u2026\u2020\u2021\ufffe\u2030\u0160\u2039\u015a\u0164\u017d\u0179\ufffe\u2018\u2019\u201c\u201d\u2022\u2013\u2014\ufffe\u2122\u0161\u203a\u015b\u0165\u017e\u017a\xa0\u02c7\u02d8\u0141\xa4\u0104\xa6\xa7\xa8\xa9\u015e\xab\xac\xad\xae\u017b\xb0\xb1\u02db\u0142\xb4\xb5\xb6\xb7\xb8\u0105\u015f\xbb\u013d\u02dd\u013e\u017c\u0154\xc1\xc2\u0102\xc4\u0139\u0106\xc7\u010c\xc9\u0118\xcb\u011a\xcd\xce\u010e\u0110\u0143\u0147\xd3\xd4\u0150\xd6\xd7\u0158\u016e\xda\u0170\xdc\xdd\u0162\xdf\u0155\xe1\xe2\u0103\xe4\u013a\u0107\xe7\u010d\xe9\u0119\xeb\u011b\xed\xee\u010f\u0111\u0144\u0148\xf3\xf4\u0151\xf6\xf7\u0159\u016f\xfa\u0171\xfc\xfd\u0163\u02d9", // 1250 - Latin 2
  1246. U"\u0402\u0403\u201a\u0453\u201e\u2026\u2020\u2021\u20ac\u2030\u0409\u2039\u040a\u040c\u040b\u040f\u0452\u2018\u2019\u201c\u201d\u2022\u2013\u2014\ufffe\u2122\u0459\u203a\u045a\u045c\u045b\u045f\xa0\u040e\u045e\u0408\xa4\u0490\xa6\xa7\u0401\xa9\u0404\xab\xac\xad\xae\u0407\xb0\xb1\u0406\u0456\u0491\xb5\xb6\xb7\u0451\u2116\u0454\xbb\u0458\u0405\u0455\u0457\u0410\u0411\u0412\u0413\u0414\u0415\u0416\u0417\u0418\u0419\u041a\u041b\u041c\u041d\u041e\u041f\u0420\u0421\u0422\u0423\u0424\u0425\u0426\u0427\u0428\u0429\u042a\u042b\u042c\u042d\u042e\u042f\u0430\u0431\u0432\u0433\u0434\u0435\u0436\u0437\u0438\u0439\u043a\u043b\u043c\u043d\u043e\u043f\u0440\u0441\u0442\u0443\u0444\u0445\u0446\u0447\u0448\u0449\u044a\u044b\u044c\u044d\u044e\u044f", // 1251 - Cyrillic
  1247. U"\u20ac\ufffe\u201a\u0192\u201e\u2026\u2020\u2021\u02c6\u2030\u0160\u2039\u0152\ufffe\u017d\ufffe\ufffe\u2018\u2019\u201c\u201d\u2022\u2013\u2014\u02dc\u2122\u0161\u203a\u0153\ufffe\u017e\u0178\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xad\xae\xaf\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\xbb\xbc\xbd\xbe\xbf\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd7\xd8\xd9\xda\xdb\xdc\xdd\xde\xdf\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff", // 1252 - Latin 1
  1248. U"\u20ac\ufffe\u201a\u0192\u201e\u2026\u2020\u2021\ufffe\u2030\ufffe\u2039\ufffe\ufffe\ufffe\ufffe\ufffe\u2018\u2019\u201c\u201d\u2022\u2013\u2014\ufffe\u2122\ufffe\u203a\ufffe\ufffe\ufffe\ufffe\xa0\u0385\u0386\xa3\xa4\xa5\xa6\xa7\xa8\xa9\ufffe\xab\xac\xad\xae\u2015\xb0\xb1\xb2\xb3\u0384\xb5\xb6\xb7\u0388\u0389\u038a\xbb\u038c\xbd\u038e\u038f\u0390\u0391\u0392\u0393\u0394\u0395\u0396\u0397\u0398\u0399\u039a\u039b\u039c\u039d\u039e\u039f\u03a0\u03a1\ufffe\u03a3\u03a4\u03a5\u03a6\u03a7\u03a8\u03a9\u03aa\u03ab\u03ac\u03ad\u03ae\u03af\u03b0\u03b1\u03b2\u03b3\u03b4\u03b5\u03b6\u03b7\u03b8\u03b9\u03ba\u03bb\u03bc\u03bd\u03be\u03bf\u03c0\u03c1\u03c2\u03c3\u03c4\u03c5\u03c6\u03c7\u03c8\u03c9\u03ca\u03cb\u03cc\u03cd\u03ce\ufffe", // 1253 - Greek
  1249. U"\u20ac\ufffe\u201a\u0192\u201e\u2026\u2020\u2021\u02c6\u2030\u0160\u2039\u0152\ufffe\ufffe\ufffe\ufffe\u2018\u2019\u201c\u201d\u2022\u2013\u2014\u02dc\u2122\u0161\u203a\u0153\ufffe\ufffe\u0178\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xad\xae\xaf\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\xbb\xbc\xbd\xbe\xbf\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf\u011e\xd1\xd2\xd3\xd4\xd5\xd6\xd7\xd8\xd9\xda\xdb\xdc\u0130\u015e\xdf\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef\u011f\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\u0131\u015f\xff", // 1254 - Turkish
  1250. U"\u20ac\ufffe\u201a\u0192\u201e\u2026\u2020\u2021\u02c6\u2030\ufffe\u2039\ufffe\ufffe\ufffe\ufffe\ufffe\u2018\u2019\u201c\u201d\u2022\u2013\u2014\u02dc\u2122\ufffe\u203a\ufffe\ufffe\ufffe\ufffe\xa0\xa1\xa2\xa3\u20aa\xa5\xa6\xa7\xa8\xa9\xd7\xab\xac\xad\xae\xaf\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xf7\xbb\xbc\xbd\xbe\xbf\u05b0\u05b1\u05b2\u05b3\u05b4\u05b5\u05b6\u05b7\u05b8\u05b9\ufffe\u05bb\u05bc\u05bd\u05be\u05bf\u05c0\u05c1\u05c2\u05c3\u05f0\u05f1\u05f2\u05f3\u05f4\ufffe\ufffe\ufffe\ufffe\ufffe\ufffe\ufffe\u05d0\u05d1\u05d2\u05d3\u05d4\u05d5\u05d6\u05d7\u05d8\u05d9\u05da\u05db\u05dc\u05dd\u05de\u05df\u05e0\u05e1\u05e2\u05e3\u05e4\u05e5\u05e6\u05e7\u05e8\u05e9\u05ea\ufffe\ufffe\u200e\u200f\ufffe", // 1255 - Hebrew
  1251. U"\u20ac\u067e\u201a\u0192\u201e\u2026\u2020\u2021\u02c6\u2030\u0679\u2039\u0152\u0686\u0698\u0688\u06af\u2018\u2019\u201c\u201d\u2022\u2013\u2014\u06a9\u2122\u0691\u203a\u0153\u200c\u200d\u06ba\xa0\u060c\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\u06be\xab\xac\xad\xae\xaf\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\u061b\xbb\xbc\xbd\xbe\u061f\u06c1\u0621\u0622\u0623\u0624\u0625\u0626\u0627\u0628\u0629\u062a\u062b\u062c\u062d\u062e\u062f\u0630\u0631\u0632\u0633\u0634\u0635\u0636\xd7\u0637\u0638\u0639\u063a\u0640\u0641\u0642\u0643\xe0\u0644\xe2\u0645\u0646\u0647\u0648\xe7\xe8\xe9\xea\xeb\u0649\u064a\xee\xef\u064b\u064c\u064d\u064e\xf4\u064f\u0650\xf7\u0651\xf9\u0652\xfb\xfc\u200e\u200f\u06d2", // 1256 - Arabic
  1252. U"\u20ac\ufffe\u201a\ufffe\u201e\u2026\u2020\u2021\ufffe\u2030\ufffe\u2039\ufffe\xa8\u02c7\xb8\ufffe\u2018\u2019\u201c\u201d\u2022\u2013\u2014\ufffe\u2122\ufffe\u203a\ufffe\xaf\u02db\ufffe\xa0\ufffe\xa2\xa3\xa4\ufffe\xa6\xa7\xd8\xa9\u0156\xab\xac\xad\xae\xc6\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7\xf8\xb9\u0157\xbb\xbc\xbd\xbe\xe6\u0104\u012e\u0100\u0106\xc4\xc5\u0118\u0112\u010c\xc9\u0179\u0116\u0122\u0136\u012a\u013b\u0160\u0143\u0145\xd3\u014c\xd5\xd6\xd7\u0172\u0141\u015a\u016a\xdc\u017b\u017d\xdf\u0105\u012f\u0101\u0107\xe4\xe5\u0119\u0113\u010d\xe9\u017a\u0117\u0123\u0137\u012b\u013c\u0161\u0144\u0146\xf3\u014d\xf5\xf6\xf7\u0173\u0142\u015b\u016b\xfc\u017c\u017e\u02d9", // 1257 - Baltic
  1253. U"\u20ac\ufffe\u201a\u0192\u201e\u2026\u2020\u2021\u02c6\u2030\ufffe\u2039\u0152\ufffe\ufffe\ufffe\ufffe\u2018\u2019\u201c\u201d\u2022\u2013\u2014\u02dc\u2122\ufffe\u203a\u0153\ufffe\ufffe\u0178\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xad\xae\xaf\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\xbb\xbc\xbd\xbe\xbf\xc0\xc1\xc2\u0102\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\u0300\xcd\xce\xcf\u0110\xd1\u0309\xd3\xd4\u01a0\xd6\xd7\xd8\xd9\xda\xdb\xdc\u01af\u0303\xdf\xe0\xe1\xe2\u0103\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\u0301\xed\xee\xef\u0111\xf1\u0323\xf3\xf4\u01a1\xf6\xf7\xf8\xf9\xfa\xfb\xfc\u01b0\u20ab\xff", // 1258 - Vietnamese
  1254. };
  1255. Error FontFile::load_bitmap_font(const String &p_path) {
  1256. return _load_bitmap_font(p_path, nullptr);
  1257. }
  1258. Error FontFile::_load_bitmap_font(const String &p_path, List<String> *r_image_files) {
  1259. reset_state();
  1260. antialiasing = TextServer::FONT_ANTIALIASING_NONE;
  1261. mipmaps = false;
  1262. disable_embedded_bitmaps = true;
  1263. msdf = false;
  1264. force_autohinter = false;
  1265. allow_system_fallback = true;
  1266. hinting = TextServer::HINTING_NONE;
  1267. oversampling = 1.0f;
  1268. Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::READ);
  1269. ERR_FAIL_COND_V_MSG(f.is_null(), ERR_CANT_CREATE, vformat("Cannot open font from file: %s.", p_path));
  1270. int base_size = 16;
  1271. int height = 0;
  1272. int ascent = 0;
  1273. int outline = 0;
  1274. BitField<TextServer::FontStyle> st_flags = 0;
  1275. String font_name;
  1276. bool packed = false;
  1277. uint8_t ch[4] = { 0, 0, 0, 0 }; // RGBA
  1278. int first_gl_ch = -1;
  1279. int first_ol_ch = -1;
  1280. int first_cm_ch = -1;
  1281. unsigned char magic[4];
  1282. f->get_buffer((unsigned char *)&magic, 4);
  1283. if (magic[0] == 'B' && magic[1] == 'M' && magic[2] == 'F') {
  1284. // Binary BMFont file.
  1285. ERR_FAIL_COND_V_MSG(magic[3] != 3, ERR_CANT_CREATE, vformat("Version %d of BMFont is not supported (should be 3).", (int)magic[3]));
  1286. uint8_t block_type = f->get_8();
  1287. uint32_t block_size = f->get_32();
  1288. bool unicode = false;
  1289. uint8_t encoding = 9;
  1290. while (!f->eof_reached()) {
  1291. uint64_t off = f->get_position();
  1292. switch (block_type) {
  1293. case 1: /* info */ {
  1294. ERR_FAIL_COND_V_MSG(block_size < 15, ERR_CANT_CREATE, "Invalid BMFont info block size.");
  1295. base_size = ABS(static_cast<int16_t>(f->get_16()));
  1296. if (base_size == 0) {
  1297. base_size = 16;
  1298. }
  1299. uint8_t flags = f->get_8();
  1300. if (flags & (1 << 3)) {
  1301. st_flags.set_flag(TextServer::FONT_BOLD);
  1302. }
  1303. if (flags & (1 << 2)) {
  1304. st_flags.set_flag(TextServer::FONT_ITALIC);
  1305. }
  1306. unicode = (flags & 0x02);
  1307. uint8_t encoding_id = f->get_8(); // non-unicode charset
  1308. if (!unicode) {
  1309. switch (encoding_id) {
  1310. case 0x00: {
  1311. encoding = 2;
  1312. } break;
  1313. case 0xB2: {
  1314. encoding = 6;
  1315. } break;
  1316. case 0xBA: {
  1317. encoding = 7;
  1318. } break;
  1319. case 0xEE: {
  1320. encoding = 0;
  1321. } break;
  1322. case 0xA1: {
  1323. encoding = 3;
  1324. } break;
  1325. case 0xB1: {
  1326. encoding = 5;
  1327. } break;
  1328. case 0xCC: {
  1329. encoding = 1;
  1330. } break;
  1331. case 0xA2: {
  1332. encoding = 4;
  1333. } break;
  1334. case 0xA3: {
  1335. encoding = 8;
  1336. } break;
  1337. default: {
  1338. WARN_PRINT(vformat("Unknown BMFont OEM encoding %x, parsing as Unicode (should be 0x00 - Latin 1, 0xB2 - Arabic, 0xBA - Baltic, 0xEE - Latin 2, 0xA1 - Greek, 0xB1 - Hebrew, 0xCC - Cyrillic, 0xA2 - Turkish, 0xA3 - Vietnamese).", encoding_id));
  1339. } break;
  1340. };
  1341. }
  1342. f->get_16(); // stretch_h, skip
  1343. f->get_8(); // aa, skip
  1344. f->get_32(); // padding, skip
  1345. f->get_16(); // spacing, skip
  1346. outline = f->get_8();
  1347. // font name
  1348. PackedByteArray name_data;
  1349. name_data.resize(block_size - 14);
  1350. f->get_buffer(name_data.ptrw(), block_size - 14);
  1351. font_name = String::utf8((const char *)name_data.ptr(), block_size - 14);
  1352. set_fixed_size(base_size);
  1353. } break;
  1354. case 2: /* common */ {
  1355. ERR_FAIL_COND_V_MSG(block_size != 15, ERR_CANT_CREATE, "Invalid BMFont common block size.");
  1356. height = f->get_16();
  1357. ascent = f->get_16();
  1358. f->get_32(); // scale, skip
  1359. f->get_16(); // pages, skip
  1360. uint8_t flags = f->get_8();
  1361. packed = (flags & 0x01);
  1362. ch[3] = f->get_8();
  1363. ch[0] = f->get_8();
  1364. ch[1] = f->get_8();
  1365. ch[2] = f->get_8();
  1366. for (int i = 0; i < 4; i++) {
  1367. if (ch[i] == 0 && first_gl_ch == -1) {
  1368. first_gl_ch = i;
  1369. }
  1370. if (ch[i] == 1 && first_ol_ch == -1) {
  1371. first_ol_ch = i;
  1372. if (outline == 0) {
  1373. outline = 1;
  1374. }
  1375. }
  1376. if (ch[i] == 2 && first_cm_ch == -1) {
  1377. first_cm_ch = i;
  1378. }
  1379. }
  1380. } break;
  1381. case 3: /* pages */ {
  1382. int page = 0;
  1383. CharString cs;
  1384. char32_t c = f->get_8();
  1385. while (!f->eof_reached() && f->get_position() <= off + block_size) {
  1386. if (c == '\0') {
  1387. String base_dir = p_path.get_base_dir();
  1388. String file = base_dir.path_join(String::utf8(cs.ptr(), cs.length()));
  1389. if (RenderingServer::get_singleton() != nullptr) {
  1390. Ref<Image> img;
  1391. img.instantiate();
  1392. Error err = ImageLoader::load_image(file, img);
  1393. ERR_FAIL_COND_V_MSG(err != OK, ERR_FILE_CANT_READ, vformat("Can't load font texture: %s.", file));
  1394. if (r_image_files) {
  1395. r_image_files->push_back(file);
  1396. }
  1397. if (packed) {
  1398. if (ch[3] == 0) { // 4 x 8 bit monochrome, no outline
  1399. outline = 0;
  1400. ERR_FAIL_COND_V_MSG(img->get_format() != Image::FORMAT_RGBA8, ERR_FILE_CANT_READ, "Unsupported BMFont texture format.");
  1401. _convert_packed_8bit(img, page, base_size);
  1402. } else if ((ch[3] == 2) && (outline > 0)) { // 4 x 4 bit monochrome, gl + outline
  1403. ERR_FAIL_COND_V_MSG(img->get_format() != Image::FORMAT_RGBA8, ERR_FILE_CANT_READ, "Unsupported BMFont texture format.");
  1404. _convert_packed_4bit(img, page, base_size);
  1405. } else {
  1406. ERR_FAIL_V_MSG(ERR_CANT_CREATE, "Unsupported BMFont texture format.");
  1407. }
  1408. } else {
  1409. if ((ch[0] == 0) && (ch[1] == 0) && (ch[2] == 0) && (ch[3] == 0)) { // RGBA8 color, no outline
  1410. outline = 0;
  1411. ERR_FAIL_COND_V_MSG(img->get_format() != Image::FORMAT_RGBA8, ERR_FILE_CANT_READ, "Unsupported BMFont texture format.");
  1412. set_texture_image(0, Vector2i(base_size, 0), page, img);
  1413. } else if ((ch[0] == 2) && (ch[1] == 2) && (ch[2] == 2) && (ch[3] == 2) && (outline > 0)) { // RGBA4 color, gl + outline
  1414. ERR_FAIL_COND_V_MSG(img->get_format() != Image::FORMAT_RGBA8, ERR_FILE_CANT_READ, "Unsupported BMFont texture format.");
  1415. _convert_rgba_4bit(img, page, base_size);
  1416. } else if ((first_gl_ch >= 0) && (first_ol_ch >= 0) && (outline > 0)) { // 1 x 8 bit monochrome, gl + outline
  1417. ERR_FAIL_COND_V_MSG(img->get_format() != Image::FORMAT_RGBA8 && img->get_format() != Image::FORMAT_L8, ERR_FILE_CANT_READ, "Unsupported BMFont texture format.");
  1418. _convert_mono_8bit(img, page, first_gl_ch, base_size, 0);
  1419. _convert_mono_8bit(img, page, first_ol_ch, base_size, 1);
  1420. } else if ((first_cm_ch >= 0) && (outline > 0)) { // 1 x 4 bit monochrome, gl + outline
  1421. ERR_FAIL_COND_V_MSG(img->get_format() != Image::FORMAT_RGBA8 && img->get_format() != Image::FORMAT_L8, ERR_FILE_CANT_READ, "Unsupported BMFont texture format.");
  1422. _convert_mono_4bit(img, page, first_cm_ch, base_size, 1);
  1423. } else if (first_gl_ch >= 0) { // 1 x 8 bit monochrome, no outline
  1424. outline = 0;
  1425. ERR_FAIL_COND_V_MSG(img->get_format() != Image::FORMAT_RGBA8 && img->get_format() != Image::FORMAT_L8, ERR_FILE_CANT_READ, "Unsupported BMFont texture format.");
  1426. _convert_mono_8bit(img, page, first_gl_ch, base_size, 0);
  1427. } else {
  1428. ERR_FAIL_V_MSG(ERR_CANT_CREATE, "Unsupported BMFont texture format.");
  1429. }
  1430. }
  1431. }
  1432. page++;
  1433. cs = "";
  1434. } else {
  1435. cs += c;
  1436. }
  1437. c = f->get_8();
  1438. }
  1439. } break;
  1440. case 4: /* chars */ {
  1441. int char_count = block_size / 20;
  1442. for (int i = 0; i < char_count; i++) {
  1443. Vector2 advance;
  1444. Vector2 size;
  1445. Vector2 offset;
  1446. Rect2 uv_rect;
  1447. char32_t idx = f->get_32();
  1448. if (!unicode && encoding < 9) {
  1449. if (idx >= 0x80 && idx <= 0xFF) {
  1450. idx = _oem_to_unicode[encoding][idx - 0x80];
  1451. } else if (idx > 0xFF) {
  1452. WARN_PRINT(vformat("Invalid BMFont OEM character %x (should be 0x00-0xFF).", idx));
  1453. idx = 0x00;
  1454. }
  1455. }
  1456. uv_rect.position.x = (int16_t)f->get_16();
  1457. uv_rect.position.y = (int16_t)f->get_16();
  1458. uv_rect.size.width = (int16_t)f->get_16();
  1459. size.width = uv_rect.size.width;
  1460. uv_rect.size.height = (int16_t)f->get_16();
  1461. size.height = uv_rect.size.height;
  1462. offset.x = (int16_t)f->get_16();
  1463. offset.y = (int16_t)f->get_16() - ascent;
  1464. advance.x = (int16_t)f->get_16();
  1465. if (advance.x < 0) {
  1466. advance.x = size.width + 1;
  1467. }
  1468. int texture_idx = f->get_8();
  1469. uint8_t channel = f->get_8();
  1470. int ch_off = 0;
  1471. if (packed) {
  1472. switch (channel) {
  1473. case 1:
  1474. ch_off = 2;
  1475. break; // B
  1476. case 2:
  1477. ch_off = 1;
  1478. break; // G
  1479. case 4:
  1480. ch_off = 0;
  1481. break; // R
  1482. case 8:
  1483. ch_off = 3;
  1484. break; // A
  1485. default:
  1486. ch_off = 0;
  1487. break;
  1488. }
  1489. }
  1490. set_glyph_advance(0, base_size, idx, advance);
  1491. set_glyph_offset(0, Vector2i(base_size, 0), idx, offset);
  1492. set_glyph_size(0, Vector2i(base_size, 0), idx, size);
  1493. set_glyph_uv_rect(0, Vector2i(base_size, 0), idx, uv_rect);
  1494. set_glyph_texture_idx(0, Vector2i(base_size, 0), idx, texture_idx * (packed ? 4 : 1) + ch_off);
  1495. if (outline > 0) {
  1496. set_glyph_offset(0, Vector2i(base_size, 1), idx, offset);
  1497. set_glyph_size(0, Vector2i(base_size, 1), idx, size);
  1498. set_glyph_uv_rect(0, Vector2i(base_size, 1), idx, uv_rect);
  1499. set_glyph_texture_idx(0, Vector2i(base_size, 1), idx, texture_idx * (packed ? 4 : 1) + ch_off);
  1500. }
  1501. }
  1502. } break;
  1503. case 5: /* kerning */ {
  1504. int pair_count = block_size / 10;
  1505. for (int i = 0; i < pair_count; i++) {
  1506. Vector2i kpk;
  1507. kpk.x = f->get_32();
  1508. kpk.y = f->get_32();
  1509. if (!unicode && encoding < 9) {
  1510. if (kpk.x >= 0x80 && kpk.x <= 0xFF) {
  1511. kpk.x = _oem_to_unicode[encoding][kpk.x - 0x80];
  1512. } else if (kpk.x > 0xFF) {
  1513. WARN_PRINT(vformat("Invalid BMFont OEM character %x (should be 0x00-0xFF).", kpk.x));
  1514. kpk.x = 0x00;
  1515. }
  1516. if (kpk.y >= 0x80 && kpk.y <= 0xFF) {
  1517. kpk.y = _oem_to_unicode[encoding][kpk.y - 0x80];
  1518. } else if (kpk.y > 0xFF) {
  1519. WARN_PRINT(vformat("Invalid BMFont OEM character %x (should be 0x00-0xFF).", kpk.y));
  1520. kpk.y = 0x00;
  1521. }
  1522. }
  1523. set_kerning(0, base_size, kpk, Vector2((int16_t)f->get_16(), 0));
  1524. }
  1525. } break;
  1526. default: {
  1527. ERR_FAIL_V_MSG(ERR_CANT_CREATE, "Invalid BMFont block type.");
  1528. } break;
  1529. }
  1530. f->seek(off + block_size);
  1531. block_type = f->get_8();
  1532. block_size = f->get_32();
  1533. }
  1534. } else {
  1535. // Text BMFont file.
  1536. f->seek(0);
  1537. bool unicode = false;
  1538. uint8_t encoding = 9;
  1539. while (true) {
  1540. String line = f->get_line();
  1541. int delimiter = line.find_char(' ');
  1542. String type = line.substr(0, delimiter);
  1543. int pos = delimiter + 1;
  1544. HashMap<String, String> keys;
  1545. while (pos < line.size() && line[pos] == ' ') {
  1546. pos++;
  1547. }
  1548. while (pos < line.size()) {
  1549. int eq = line.find_char('=', pos);
  1550. if (eq == -1) {
  1551. break;
  1552. }
  1553. String key = line.substr(pos, eq - pos);
  1554. int end = -1;
  1555. String value;
  1556. if (line[eq + 1] == '"') {
  1557. end = line.find_char('"', eq + 2);
  1558. if (end == -1) {
  1559. break;
  1560. }
  1561. value = line.substr(eq + 2, end - 1 - eq - 1);
  1562. pos = end + 1;
  1563. } else {
  1564. end = line.find_char(' ', eq + 1);
  1565. if (end == -1) {
  1566. end = line.size();
  1567. }
  1568. value = line.substr(eq + 1, end - eq);
  1569. pos = end;
  1570. }
  1571. while (pos < line.size() && line[pos] == ' ') {
  1572. pos++;
  1573. }
  1574. keys[key] = value;
  1575. }
  1576. if (type == "info") {
  1577. if (keys.has("size")) {
  1578. base_size = ABS(keys["size"].to_int());
  1579. if (base_size == 0) {
  1580. base_size = 16;
  1581. }
  1582. }
  1583. if (keys.has("outline")) {
  1584. outline = keys["outline"].to_int();
  1585. }
  1586. if (keys.has("bold")) {
  1587. if (keys["bold"].to_int()) {
  1588. st_flags.set_flag(TextServer::FONT_BOLD);
  1589. }
  1590. }
  1591. if (keys.has("italic")) {
  1592. if (keys["italic"].to_int()) {
  1593. st_flags.set_flag(TextServer::FONT_ITALIC);
  1594. }
  1595. }
  1596. if (keys.has("face")) {
  1597. font_name = keys["face"];
  1598. }
  1599. if (keys.has("unicode")) {
  1600. unicode = keys["unicode"].to_int();
  1601. }
  1602. if (!unicode) {
  1603. if (keys.has("charset")) {
  1604. String encoding_name = keys["charset"].to_upper();
  1605. if (encoding_name == "" || encoding_name == "ASCII" || encoding_name == "ANSI") {
  1606. encoding = 2;
  1607. } else if (encoding_name == "ARABIC") {
  1608. encoding = 6;
  1609. } else if (encoding_name == "BALTIC") {
  1610. encoding = 7;
  1611. } else if (encoding_name == "EASTEUROPE") {
  1612. encoding = 0;
  1613. } else if (encoding_name == "GREEK") {
  1614. encoding = 3;
  1615. } else if (encoding_name == "HEBREW") {
  1616. encoding = 5;
  1617. } else if (encoding_name == "RUSSIAN") {
  1618. encoding = 1;
  1619. } else if (encoding_name == "TURKISH") {
  1620. encoding = 4;
  1621. } else if (encoding_name == "VIETNAMESE") {
  1622. encoding = 8;
  1623. } else {
  1624. WARN_PRINT(vformat("Unknown BMFont OEM encoding %s, parsing as Unicode (should be ANSI, ASCII, ARABIC, BALTIC, EASTEUROPE, GREEK, HEBREW, RUSSIAN, TURKISH or VIETNAMESE).", encoding_name));
  1625. }
  1626. } else {
  1627. encoding = 2;
  1628. }
  1629. }
  1630. set_fixed_size(base_size);
  1631. } else if (type == "common") {
  1632. if (keys.has("lineHeight")) {
  1633. height = keys["lineHeight"].to_int();
  1634. }
  1635. if (keys.has("base")) {
  1636. ascent = keys["base"].to_int();
  1637. }
  1638. if (keys.has("packed")) {
  1639. packed = (keys["packed"].to_int() == 1);
  1640. }
  1641. if (keys.has("alphaChnl")) {
  1642. ch[3] = keys["alphaChnl"].to_int();
  1643. }
  1644. if (keys.has("redChnl")) {
  1645. ch[0] = keys["redChnl"].to_int();
  1646. }
  1647. if (keys.has("greenChnl")) {
  1648. ch[1] = keys["greenChnl"].to_int();
  1649. }
  1650. if (keys.has("blueChnl")) {
  1651. ch[2] = keys["blueChnl"].to_int();
  1652. }
  1653. for (int i = 0; i < 4; i++) {
  1654. if (ch[i] == 0 && first_gl_ch == -1) {
  1655. first_gl_ch = i;
  1656. }
  1657. if (ch[i] == 1 && first_ol_ch == -1) {
  1658. first_ol_ch = i;
  1659. if (outline == 0) {
  1660. outline = 1;
  1661. }
  1662. }
  1663. if (ch[i] == 2 && first_cm_ch == -1) {
  1664. first_cm_ch = i;
  1665. }
  1666. }
  1667. } else if (type == "page") {
  1668. int page = 0;
  1669. if (keys.has("id")) {
  1670. page = keys["id"].to_int();
  1671. }
  1672. if (keys.has("file")) {
  1673. String base_dir = p_path.get_base_dir();
  1674. String file = base_dir.path_join(keys["file"]);
  1675. if (RenderingServer::get_singleton() != nullptr) {
  1676. Ref<Image> img;
  1677. img.instantiate();
  1678. Error err = ImageLoader::load_image(file, img);
  1679. ERR_FAIL_COND_V_MSG(err != OK, ERR_FILE_CANT_READ, vformat("Can't load font texture: %s.", file));
  1680. if (r_image_files) {
  1681. r_image_files->push_back(file);
  1682. }
  1683. if (packed) {
  1684. if (ch[3] == 0) { // 4 x 8 bit monochrome, no outline
  1685. outline = 0;
  1686. ERR_FAIL_COND_V_MSG(img->get_format() != Image::FORMAT_RGBA8, ERR_FILE_CANT_READ, "Unsupported BMFont texture format.");
  1687. _convert_packed_8bit(img, page, base_size);
  1688. } else if ((ch[3] == 2) && (outline > 0)) { // 4 x 4 bit monochrome, gl + outline
  1689. ERR_FAIL_COND_V_MSG(img->get_format() != Image::FORMAT_RGBA8, ERR_FILE_CANT_READ, "Unsupported BMFont texture format.");
  1690. _convert_packed_4bit(img, page, base_size);
  1691. } else {
  1692. ERR_FAIL_V_MSG(ERR_CANT_CREATE, "Unsupported BMFont texture format.");
  1693. }
  1694. } else {
  1695. if ((ch[3] == 0) && (ch[0] == 4) && (ch[1] == 4) && (ch[2] == 4) && img->get_format() == Image::FORMAT_RGBA8) { // might be RGBA8 color, no outline (color part of the image should be sold white, but some apps designed for Godot 3 generate color fonts with this config)
  1696. outline = 0;
  1697. set_texture_image(0, Vector2i(base_size, 0), page, img);
  1698. } else if ((ch[0] == 0) && (ch[1] == 0) && (ch[2] == 0) && (ch[3] == 0)) { // RGBA8 color, no outline
  1699. outline = 0;
  1700. ERR_FAIL_COND_V_MSG(img->get_format() != Image::FORMAT_RGBA8, ERR_FILE_CANT_READ, "Unsupported BMFont texture format.");
  1701. set_texture_image(0, Vector2i(base_size, 0), page, img);
  1702. } else if ((ch[0] == 2) && (ch[1] == 2) && (ch[2] == 2) && (ch[3] == 2) && (outline > 0)) { // RGBA4 color, gl + outline
  1703. ERR_FAIL_COND_V_MSG(img->get_format() != Image::FORMAT_RGBA8, ERR_FILE_CANT_READ, "Unsupported BMFont texture format.");
  1704. _convert_rgba_4bit(img, page, base_size);
  1705. } else if ((first_gl_ch >= 0) && (first_ol_ch >= 0) && (outline > 0)) { // 1 x 8 bit monochrome, gl + outline
  1706. ERR_FAIL_COND_V_MSG(img->get_format() != Image::FORMAT_RGBA8 && img->get_format() != Image::FORMAT_L8, ERR_FILE_CANT_READ, "Unsupported BMFont texture format.");
  1707. _convert_mono_8bit(img, page, first_gl_ch, base_size, 0);
  1708. _convert_mono_8bit(img, page, first_ol_ch, base_size, 1);
  1709. } else if ((first_cm_ch >= 0) && (outline > 0)) { // 1 x 4 bit monochrome, gl + outline
  1710. ERR_FAIL_COND_V_MSG(img->get_format() != Image::FORMAT_RGBA8 && img->get_format() != Image::FORMAT_L8, ERR_FILE_CANT_READ, "Unsupported BMFont texture format.");
  1711. _convert_mono_4bit(img, page, first_cm_ch, base_size, 1);
  1712. } else if (first_gl_ch >= 0) { // 1 x 8 bit monochrome, no outline
  1713. outline = 0;
  1714. ERR_FAIL_COND_V_MSG(img->get_format() != Image::FORMAT_RGBA8 && img->get_format() != Image::FORMAT_L8, ERR_FILE_CANT_READ, "Unsupported BMFont texture format.");
  1715. _convert_mono_8bit(img, page, first_gl_ch, base_size, 0);
  1716. } else {
  1717. ERR_FAIL_V_MSG(ERR_CANT_CREATE, "Unsupported BMFont texture format.");
  1718. }
  1719. }
  1720. }
  1721. }
  1722. } else if (type == "char") {
  1723. char32_t idx = 0;
  1724. Vector2 advance;
  1725. Vector2 size;
  1726. Vector2 offset;
  1727. Rect2 uv_rect;
  1728. int texture_idx = -1;
  1729. uint8_t channel = 15;
  1730. if (keys.has("id")) {
  1731. idx = keys["id"].to_int();
  1732. if (!unicode && encoding < 9) {
  1733. if (idx >= 0x80 && idx <= 0xFF) {
  1734. idx = _oem_to_unicode[encoding][idx - 0x80];
  1735. } else if (idx > 0xFF) {
  1736. WARN_PRINT(vformat("Invalid BMFont OEM character %x (should be 0x00-0xFF).", idx));
  1737. idx = 0x00;
  1738. }
  1739. }
  1740. }
  1741. if (keys.has("x")) {
  1742. uv_rect.position.x = keys["x"].to_int();
  1743. }
  1744. if (keys.has("y")) {
  1745. uv_rect.position.y = keys["y"].to_int();
  1746. }
  1747. if (keys.has("width")) {
  1748. uv_rect.size.width = keys["width"].to_int();
  1749. size.width = keys["width"].to_int();
  1750. }
  1751. if (keys.has("height")) {
  1752. uv_rect.size.height = keys["height"].to_int();
  1753. size.height = keys["height"].to_int();
  1754. }
  1755. if (keys.has("xoffset")) {
  1756. offset.x = keys["xoffset"].to_int();
  1757. }
  1758. if (keys.has("yoffset")) {
  1759. offset.y = keys["yoffset"].to_int() - ascent;
  1760. }
  1761. if (keys.has("page")) {
  1762. texture_idx = keys["page"].to_int();
  1763. }
  1764. if (keys.has("xadvance")) {
  1765. advance.x = keys["xadvance"].to_int();
  1766. }
  1767. if (advance.x < 0) {
  1768. advance.x = size.width + 1;
  1769. }
  1770. if (keys.has("chnl")) {
  1771. channel = keys["chnl"].to_int();
  1772. }
  1773. int ch_off = 0;
  1774. if (packed) {
  1775. switch (channel) {
  1776. case 1:
  1777. ch_off = 2;
  1778. break; // B
  1779. case 2:
  1780. ch_off = 1;
  1781. break; // G
  1782. case 4:
  1783. ch_off = 0;
  1784. break; // R
  1785. case 8:
  1786. ch_off = 3;
  1787. break; // A
  1788. default:
  1789. ch_off = 0;
  1790. break;
  1791. }
  1792. }
  1793. set_glyph_advance(0, base_size, idx, advance);
  1794. set_glyph_offset(0, Vector2i(base_size, 0), idx, offset);
  1795. set_glyph_size(0, Vector2i(base_size, 0), idx, size);
  1796. set_glyph_uv_rect(0, Vector2i(base_size, 0), idx, uv_rect);
  1797. set_glyph_texture_idx(0, Vector2i(base_size, 0), idx, texture_idx * (packed ? 4 : 1) + ch_off);
  1798. if (outline > 0) {
  1799. set_glyph_offset(0, Vector2i(base_size, 1), idx, offset);
  1800. set_glyph_size(0, Vector2i(base_size, 1), idx, size);
  1801. set_glyph_uv_rect(0, Vector2i(base_size, 1), idx, uv_rect);
  1802. set_glyph_texture_idx(0, Vector2i(base_size, 1), idx, texture_idx * (packed ? 4 : 1) + ch_off);
  1803. }
  1804. } else if (type == "kerning") {
  1805. Vector2i kpk;
  1806. if (keys.has("first")) {
  1807. kpk.x = keys["first"].to_int();
  1808. }
  1809. if (keys.has("second")) {
  1810. kpk.y = keys["second"].to_int();
  1811. }
  1812. if (!unicode && encoding < 9) {
  1813. if (kpk.x >= 0x80 && kpk.x <= 0xFF) {
  1814. kpk.x = _oem_to_unicode[encoding][kpk.x - 0x80];
  1815. } else if (kpk.x > 0xFF) {
  1816. WARN_PRINT(vformat("Invalid BMFont OEM character %x (should be 0x00-0xFF).", kpk.x));
  1817. kpk.x = 0x00;
  1818. }
  1819. if (kpk.y >= 0x80 && kpk.y <= 0xFF) {
  1820. kpk.y = _oem_to_unicode[encoding][kpk.y - 0x80];
  1821. } else if (kpk.y > 0xFF) {
  1822. WARN_PRINT(vformat("Invalid BMFont OEM character %x (should be 0x00-0xFF).", kpk.x));
  1823. kpk.y = 0x00;
  1824. }
  1825. }
  1826. if (keys.has("amount")) {
  1827. set_kerning(0, base_size, kpk, Vector2(keys["amount"].to_int(), 0));
  1828. }
  1829. }
  1830. if (f->eof_reached()) {
  1831. break;
  1832. }
  1833. }
  1834. }
  1835. set_font_name(font_name);
  1836. set_font_style(st_flags);
  1837. if (st_flags & TextServer::FONT_BOLD) {
  1838. set_font_weight(700);
  1839. }
  1840. set_cache_ascent(0, base_size, ascent);
  1841. set_cache_descent(0, base_size, height - ascent);
  1842. return OK;
  1843. }
  1844. Error FontFile::load_dynamic_font(const String &p_path) {
  1845. reset_state();
  1846. Vector<uint8_t> font_data = FileAccess::get_file_as_bytes(p_path);
  1847. set_data(font_data);
  1848. return OK;
  1849. }
  1850. void FontFile::set_data_ptr(const uint8_t *p_data, size_t p_size) {
  1851. data.clear();
  1852. data_ptr = p_data;
  1853. data_size = p_size;
  1854. for (int i = 0; i < cache.size(); i++) {
  1855. if (cache[i].is_valid()) {
  1856. TS->font_set_data_ptr(cache[i], data_ptr, data_size);
  1857. }
  1858. }
  1859. }
  1860. void FontFile::set_data(const PackedByteArray &p_data) {
  1861. data = p_data;
  1862. data_ptr = data.ptr();
  1863. data_size = data.size();
  1864. for (int i = 0; i < cache.size(); i++) {
  1865. if (cache[i].is_valid()) {
  1866. TS->font_set_data_ptr(cache[i], data_ptr, data_size);
  1867. }
  1868. }
  1869. }
  1870. PackedByteArray FontFile::get_data() const {
  1871. if (unlikely((size_t)data.size() != data_size)) {
  1872. PackedByteArray *data_w = const_cast<PackedByteArray *>(&data);
  1873. data_w->resize(data_size);
  1874. memcpy(data_w->ptrw(), data_ptr, data_size);
  1875. }
  1876. return data;
  1877. }
  1878. void FontFile::set_font_name(const String &p_name) {
  1879. _ensure_rid(0);
  1880. TS->font_set_name(cache[0], p_name);
  1881. }
  1882. void FontFile::set_font_style_name(const String &p_name) {
  1883. _ensure_rid(0);
  1884. TS->font_set_style_name(cache[0], p_name);
  1885. }
  1886. void FontFile::set_font_style(BitField<TextServer::FontStyle> p_style) {
  1887. _ensure_rid(0);
  1888. TS->font_set_style(cache[0], p_style);
  1889. }
  1890. void FontFile::set_font_weight(int p_weight) {
  1891. _ensure_rid(0);
  1892. TS->font_set_weight(cache[0], p_weight);
  1893. }
  1894. void FontFile::set_font_stretch(int p_stretch) {
  1895. _ensure_rid(0);
  1896. TS->font_set_stretch(cache[0], p_stretch);
  1897. }
  1898. void FontFile::set_antialiasing(TextServer::FontAntialiasing p_antialiasing) {
  1899. if (antialiasing != p_antialiasing) {
  1900. antialiasing = p_antialiasing;
  1901. for (int i = 0; i < cache.size(); i++) {
  1902. _ensure_rid(i);
  1903. TS->font_set_antialiasing(cache[i], antialiasing);
  1904. }
  1905. emit_changed();
  1906. }
  1907. }
  1908. TextServer::FontAntialiasing FontFile::get_antialiasing() const {
  1909. return antialiasing;
  1910. }
  1911. void FontFile::set_disable_embedded_bitmaps(bool p_disable_embedded_bitmaps) {
  1912. if (disable_embedded_bitmaps != p_disable_embedded_bitmaps) {
  1913. disable_embedded_bitmaps = p_disable_embedded_bitmaps;
  1914. for (int i = 0; i < cache.size(); i++) {
  1915. _ensure_rid(i);
  1916. TS->font_set_disable_embedded_bitmaps(cache[i], disable_embedded_bitmaps);
  1917. }
  1918. emit_changed();
  1919. }
  1920. }
  1921. bool FontFile::get_disable_embedded_bitmaps() const {
  1922. return disable_embedded_bitmaps;
  1923. }
  1924. void FontFile::set_generate_mipmaps(bool p_generate_mipmaps) {
  1925. if (mipmaps != p_generate_mipmaps) {
  1926. mipmaps = p_generate_mipmaps;
  1927. for (int i = 0; i < cache.size(); i++) {
  1928. _ensure_rid(i);
  1929. TS->font_set_generate_mipmaps(cache[i], mipmaps);
  1930. }
  1931. emit_changed();
  1932. }
  1933. }
  1934. bool FontFile::get_generate_mipmaps() const {
  1935. return mipmaps;
  1936. }
  1937. void FontFile::set_multichannel_signed_distance_field(bool p_msdf) {
  1938. if (msdf != p_msdf) {
  1939. msdf = p_msdf;
  1940. for (int i = 0; i < cache.size(); i++) {
  1941. _ensure_rid(i);
  1942. TS->font_set_multichannel_signed_distance_field(cache[i], msdf);
  1943. }
  1944. emit_changed();
  1945. }
  1946. }
  1947. bool FontFile::is_multichannel_signed_distance_field() const {
  1948. return msdf;
  1949. }
  1950. void FontFile::set_msdf_pixel_range(int p_msdf_pixel_range) {
  1951. if (msdf_pixel_range != p_msdf_pixel_range) {
  1952. msdf_pixel_range = p_msdf_pixel_range;
  1953. for (int i = 0; i < cache.size(); i++) {
  1954. _ensure_rid(i);
  1955. TS->font_set_msdf_pixel_range(cache[i], msdf_pixel_range);
  1956. }
  1957. emit_changed();
  1958. }
  1959. }
  1960. int FontFile::get_msdf_pixel_range() const {
  1961. return msdf_pixel_range;
  1962. }
  1963. void FontFile::set_msdf_size(int p_msdf_size) {
  1964. if (msdf_size != p_msdf_size) {
  1965. msdf_size = p_msdf_size;
  1966. for (int i = 0; i < cache.size(); i++) {
  1967. _ensure_rid(i);
  1968. TS->font_set_msdf_size(cache[i], msdf_size);
  1969. }
  1970. emit_changed();
  1971. }
  1972. }
  1973. int FontFile::get_msdf_size() const {
  1974. return msdf_size;
  1975. }
  1976. void FontFile::set_fixed_size(int p_fixed_size) {
  1977. if (fixed_size != p_fixed_size) {
  1978. fixed_size = p_fixed_size;
  1979. for (int i = 0; i < cache.size(); i++) {
  1980. _ensure_rid(i);
  1981. TS->font_set_fixed_size(cache[i], fixed_size);
  1982. }
  1983. emit_changed();
  1984. }
  1985. }
  1986. int FontFile::get_fixed_size() const {
  1987. return fixed_size;
  1988. }
  1989. void FontFile::set_fixed_size_scale_mode(TextServer::FixedSizeScaleMode p_fixed_size_scale_mode) {
  1990. if (fixed_size_scale_mode != p_fixed_size_scale_mode) {
  1991. fixed_size_scale_mode = p_fixed_size_scale_mode;
  1992. for (int i = 0; i < cache.size(); i++) {
  1993. _ensure_rid(i);
  1994. TS->font_set_fixed_size_scale_mode(cache[i], fixed_size_scale_mode);
  1995. }
  1996. emit_changed();
  1997. }
  1998. }
  1999. TextServer::FixedSizeScaleMode FontFile::get_fixed_size_scale_mode() const {
  2000. return fixed_size_scale_mode;
  2001. }
  2002. void FontFile::set_allow_system_fallback(bool p_allow_system_fallback) {
  2003. if (allow_system_fallback != p_allow_system_fallback) {
  2004. allow_system_fallback = p_allow_system_fallback;
  2005. for (int i = 0; i < cache.size(); i++) {
  2006. _ensure_rid(i);
  2007. TS->font_set_allow_system_fallback(cache[i], allow_system_fallback);
  2008. }
  2009. emit_changed();
  2010. }
  2011. }
  2012. bool FontFile::is_allow_system_fallback() const {
  2013. return allow_system_fallback;
  2014. }
  2015. void FontFile::set_force_autohinter(bool p_force_autohinter) {
  2016. if (force_autohinter != p_force_autohinter) {
  2017. force_autohinter = p_force_autohinter;
  2018. for (int i = 0; i < cache.size(); i++) {
  2019. _ensure_rid(i);
  2020. TS->font_set_force_autohinter(cache[i], force_autohinter);
  2021. }
  2022. emit_changed();
  2023. }
  2024. }
  2025. bool FontFile::is_force_autohinter() const {
  2026. return force_autohinter;
  2027. }
  2028. void FontFile::set_hinting(TextServer::Hinting p_hinting) {
  2029. if (hinting != p_hinting) {
  2030. hinting = p_hinting;
  2031. for (int i = 0; i < cache.size(); i++) {
  2032. _ensure_rid(i);
  2033. TS->font_set_hinting(cache[i], hinting);
  2034. }
  2035. emit_changed();
  2036. }
  2037. }
  2038. TextServer::Hinting FontFile::get_hinting() const {
  2039. return hinting;
  2040. }
  2041. void FontFile::set_subpixel_positioning(TextServer::SubpixelPositioning p_subpixel) {
  2042. if (subpixel_positioning != p_subpixel) {
  2043. subpixel_positioning = p_subpixel;
  2044. for (int i = 0; i < cache.size(); i++) {
  2045. _ensure_rid(i);
  2046. TS->font_set_subpixel_positioning(cache[i], subpixel_positioning);
  2047. }
  2048. emit_changed();
  2049. }
  2050. }
  2051. TextServer::SubpixelPositioning FontFile::get_subpixel_positioning() const {
  2052. return subpixel_positioning;
  2053. }
  2054. void FontFile::set_oversampling(real_t p_oversampling) {
  2055. if (oversampling != p_oversampling) {
  2056. oversampling = p_oversampling;
  2057. for (int i = 0; i < cache.size(); i++) {
  2058. _ensure_rid(i);
  2059. TS->font_set_oversampling(cache[i], oversampling);
  2060. }
  2061. emit_changed();
  2062. }
  2063. }
  2064. real_t FontFile::get_oversampling() const {
  2065. return oversampling;
  2066. }
  2067. RID FontFile::find_variation(const Dictionary &p_variation_coordinates, int p_face_index, float p_strength, Transform2D p_transform, int p_spacing_top, int p_spacing_bottom, int p_spacing_space, int p_spacing_glyph, float p_baseline_offset) const {
  2068. // Find existing variation cache.
  2069. const Dictionary &supported_coords = get_supported_variation_list();
  2070. int make_linked_from = -1;
  2071. for (int i = 0; i < cache.size(); i++) {
  2072. if (cache[i].is_valid()) {
  2073. const Dictionary &cache_var = TS->font_get_variation_coordinates(cache[i]);
  2074. bool match = true;
  2075. bool match_linked = true;
  2076. match = match && (TS->font_get_face_index(cache[i]) == p_face_index);
  2077. match = match && (TS->font_get_embolden(cache[i]) == p_strength);
  2078. match = match && (TS->font_get_transform(cache[i]) == p_transform);
  2079. match_linked = match_linked && (TS->font_get_spacing(cache[i], TextServer::SPACING_TOP) == p_spacing_top);
  2080. match_linked = match_linked && (TS->font_get_spacing(cache[i], TextServer::SPACING_BOTTOM) == p_spacing_bottom);
  2081. match_linked = match_linked && (TS->font_get_spacing(cache[i], TextServer::SPACING_SPACE) == p_spacing_space);
  2082. match_linked = match_linked && (TS->font_get_spacing(cache[i], TextServer::SPACING_GLYPH) == p_spacing_glyph);
  2083. match_linked = match_linked && (TS->font_get_baseline_offset(cache[i]) == p_baseline_offset);
  2084. for (const Variant *V = supported_coords.next(nullptr); V && match; V = supported_coords.next(V)) {
  2085. const Vector3 &def = supported_coords[*V];
  2086. real_t c_v = def.z;
  2087. if (cache_var.has(*V)) {
  2088. real_t val = cache_var[*V];
  2089. c_v = CLAMP(val, def.x, def.y);
  2090. }
  2091. if (cache_var.has(TS->tag_to_name(*V))) {
  2092. real_t val = cache_var[TS->tag_to_name(*V)];
  2093. c_v = CLAMP(val, def.x, def.y);
  2094. }
  2095. real_t s_v = def.z;
  2096. if (p_variation_coordinates.has(*V)) {
  2097. real_t val = p_variation_coordinates[*V];
  2098. s_v = CLAMP(val, def.x, def.y);
  2099. }
  2100. if (p_variation_coordinates.has(TS->tag_to_name(*V))) {
  2101. real_t val = p_variation_coordinates[TS->tag_to_name(*V)];
  2102. s_v = CLAMP(val, def.x, def.y);
  2103. }
  2104. match = match && (c_v == s_v);
  2105. }
  2106. if (match) {
  2107. if (match_linked) {
  2108. return cache[i];
  2109. } else {
  2110. make_linked_from = i;
  2111. }
  2112. }
  2113. }
  2114. }
  2115. // Create new variation cache.
  2116. int idx = cache.size();
  2117. if (make_linked_from >= 0) {
  2118. _ensure_rid(idx, make_linked_from);
  2119. TS->font_set_spacing(cache[idx], TextServer::SPACING_TOP, p_spacing_top);
  2120. TS->font_set_spacing(cache[idx], TextServer::SPACING_BOTTOM, p_spacing_bottom);
  2121. TS->font_set_spacing(cache[idx], TextServer::SPACING_SPACE, p_spacing_space);
  2122. TS->font_set_spacing(cache[idx], TextServer::SPACING_GLYPH, p_spacing_glyph);
  2123. TS->font_set_baseline_offset(cache[idx], p_baseline_offset);
  2124. } else {
  2125. _ensure_rid(idx);
  2126. TS->font_set_variation_coordinates(cache[idx], p_variation_coordinates);
  2127. TS->font_set_face_index(cache[idx], p_face_index);
  2128. TS->font_set_embolden(cache[idx], p_strength);
  2129. TS->font_set_transform(cache[idx], p_transform);
  2130. TS->font_set_spacing(cache[idx], TextServer::SPACING_TOP, p_spacing_top);
  2131. TS->font_set_spacing(cache[idx], TextServer::SPACING_BOTTOM, p_spacing_bottom);
  2132. TS->font_set_spacing(cache[idx], TextServer::SPACING_SPACE, p_spacing_space);
  2133. TS->font_set_spacing(cache[idx], TextServer::SPACING_GLYPH, p_spacing_glyph);
  2134. TS->font_set_baseline_offset(cache[idx], p_baseline_offset);
  2135. }
  2136. return cache[idx];
  2137. }
  2138. RID FontFile::_get_rid() const {
  2139. _ensure_rid(0);
  2140. return cache[0];
  2141. }
  2142. int FontFile::get_cache_count() const {
  2143. return cache.size();
  2144. }
  2145. void FontFile::clear_cache() {
  2146. _clear_cache();
  2147. cache.clear();
  2148. emit_changed();
  2149. }
  2150. void FontFile::remove_cache(int p_cache_index) {
  2151. ERR_FAIL_INDEX(p_cache_index, cache.size());
  2152. if (cache[p_cache_index].is_valid()) {
  2153. TS->free_rid(cache.write[p_cache_index]);
  2154. }
  2155. cache.remove_at(p_cache_index);
  2156. emit_changed();
  2157. }
  2158. TypedArray<Vector2i> FontFile::get_size_cache_list(int p_cache_index) const {
  2159. ERR_FAIL_COND_V(p_cache_index < 0, Array());
  2160. _ensure_rid(p_cache_index);
  2161. return TS->font_get_size_cache_list(cache[p_cache_index]);
  2162. }
  2163. void FontFile::clear_size_cache(int p_cache_index) {
  2164. ERR_FAIL_COND(p_cache_index < 0);
  2165. _ensure_rid(p_cache_index);
  2166. TS->font_clear_size_cache(cache[p_cache_index]);
  2167. }
  2168. void FontFile::remove_size_cache(int p_cache_index, const Vector2i &p_size) {
  2169. ERR_FAIL_COND(p_cache_index < 0);
  2170. _ensure_rid(p_cache_index);
  2171. TS->font_remove_size_cache(cache[p_cache_index], p_size);
  2172. }
  2173. void FontFile::set_variation_coordinates(int p_cache_index, const Dictionary &p_variation_coordinates) {
  2174. ERR_FAIL_COND(p_cache_index < 0);
  2175. _ensure_rid(p_cache_index);
  2176. TS->font_set_variation_coordinates(cache[p_cache_index], p_variation_coordinates);
  2177. }
  2178. Dictionary FontFile::get_variation_coordinates(int p_cache_index) const {
  2179. ERR_FAIL_COND_V(p_cache_index < 0, Dictionary());
  2180. _ensure_rid(p_cache_index);
  2181. return TS->font_get_variation_coordinates(cache[p_cache_index]);
  2182. }
  2183. void FontFile::set_embolden(int p_cache_index, float p_strength) {
  2184. ERR_FAIL_COND(p_cache_index < 0);
  2185. _ensure_rid(p_cache_index);
  2186. TS->font_set_embolden(cache[p_cache_index], p_strength);
  2187. }
  2188. float FontFile::get_embolden(int p_cache_index) const {
  2189. ERR_FAIL_COND_V(p_cache_index < 0, 0.f);
  2190. _ensure_rid(p_cache_index);
  2191. return TS->font_get_embolden(cache[p_cache_index]);
  2192. }
  2193. void FontFile::set_transform(int p_cache_index, Transform2D p_transform) {
  2194. ERR_FAIL_COND(p_cache_index < 0);
  2195. _ensure_rid(p_cache_index);
  2196. TS->font_set_transform(cache[p_cache_index], p_transform);
  2197. }
  2198. Transform2D FontFile::get_transform(int p_cache_index) const {
  2199. ERR_FAIL_COND_V(p_cache_index < 0, Transform2D());
  2200. _ensure_rid(p_cache_index);
  2201. return TS->font_get_transform(cache[p_cache_index]);
  2202. }
  2203. void FontFile::set_extra_spacing(int p_cache_index, TextServer::SpacingType p_spacing, int64_t p_value) {
  2204. ERR_FAIL_COND(p_cache_index < 0);
  2205. _ensure_rid(p_cache_index);
  2206. TS->font_set_spacing(cache[p_cache_index], p_spacing, p_value);
  2207. }
  2208. int64_t FontFile::get_extra_spacing(int p_cache_index, TextServer::SpacingType p_spacing) const {
  2209. ERR_FAIL_COND_V(p_cache_index < 0, 0);
  2210. _ensure_rid(p_cache_index);
  2211. return TS->font_get_spacing(cache[p_cache_index], p_spacing);
  2212. }
  2213. float FontFile::get_extra_baseline_offset(int p_cache_index) const {
  2214. ERR_FAIL_COND_V(p_cache_index < 0, 0);
  2215. _ensure_rid(p_cache_index);
  2216. return TS->font_get_baseline_offset(cache[p_cache_index]);
  2217. }
  2218. void FontFile::set_extra_baseline_offset(int p_cache_index, float p_baseline_offset) {
  2219. ERR_FAIL_COND(p_cache_index < 0);
  2220. _ensure_rid(p_cache_index);
  2221. TS->font_set_baseline_offset(cache[p_cache_index], p_baseline_offset);
  2222. }
  2223. void FontFile::set_face_index(int p_cache_index, int64_t p_index) {
  2224. ERR_FAIL_COND(p_cache_index < 0);
  2225. ERR_FAIL_COND(p_index < 0);
  2226. ERR_FAIL_COND(p_index >= 0x7FFF);
  2227. _ensure_rid(p_cache_index);
  2228. TS->font_set_face_index(cache[p_cache_index], p_index);
  2229. }
  2230. int64_t FontFile::get_face_index(int p_cache_index) const {
  2231. ERR_FAIL_COND_V(p_cache_index < 0, 0);
  2232. _ensure_rid(p_cache_index);
  2233. return TS->font_get_face_index(cache[p_cache_index]);
  2234. }
  2235. void FontFile::set_cache_ascent(int p_cache_index, int p_size, real_t p_ascent) {
  2236. ERR_FAIL_COND(p_cache_index < 0);
  2237. _ensure_rid(p_cache_index);
  2238. TS->font_set_ascent(cache[p_cache_index], p_size, p_ascent);
  2239. }
  2240. real_t FontFile::get_cache_ascent(int p_cache_index, int p_size) const {
  2241. ERR_FAIL_COND_V(p_cache_index < 0, 0.f);
  2242. _ensure_rid(p_cache_index);
  2243. return TS->font_get_ascent(cache[p_cache_index], p_size);
  2244. }
  2245. void FontFile::set_cache_descent(int p_cache_index, int p_size, real_t p_descent) {
  2246. ERR_FAIL_COND(p_cache_index < 0);
  2247. _ensure_rid(p_cache_index);
  2248. TS->font_set_descent(cache[p_cache_index], p_size, p_descent);
  2249. }
  2250. real_t FontFile::get_cache_descent(int p_cache_index, int p_size) const {
  2251. ERR_FAIL_COND_V(p_cache_index < 0, 0.f);
  2252. _ensure_rid(p_cache_index);
  2253. return TS->font_get_descent(cache[p_cache_index], p_size);
  2254. }
  2255. void FontFile::set_cache_underline_position(int p_cache_index, int p_size, real_t p_underline_position) {
  2256. ERR_FAIL_COND(p_cache_index < 0);
  2257. _ensure_rid(p_cache_index);
  2258. TS->font_set_underline_position(cache[p_cache_index], p_size, p_underline_position);
  2259. }
  2260. real_t FontFile::get_cache_underline_position(int p_cache_index, int p_size) const {
  2261. ERR_FAIL_COND_V(p_cache_index < 0, 0.f);
  2262. _ensure_rid(p_cache_index);
  2263. return TS->font_get_underline_position(cache[p_cache_index], p_size);
  2264. }
  2265. void FontFile::set_cache_underline_thickness(int p_cache_index, int p_size, real_t p_underline_thickness) {
  2266. ERR_FAIL_COND(p_cache_index < 0);
  2267. _ensure_rid(p_cache_index);
  2268. TS->font_set_underline_thickness(cache[p_cache_index], p_size, p_underline_thickness);
  2269. }
  2270. real_t FontFile::get_cache_underline_thickness(int p_cache_index, int p_size) const {
  2271. ERR_FAIL_COND_V(p_cache_index < 0, 0.f);
  2272. _ensure_rid(p_cache_index);
  2273. return TS->font_get_underline_thickness(cache[p_cache_index], p_size);
  2274. }
  2275. void FontFile::set_cache_scale(int p_cache_index, int p_size, real_t p_scale) {
  2276. ERR_FAIL_COND(p_cache_index < 0);
  2277. _ensure_rid(p_cache_index);
  2278. TS->font_set_scale(cache[p_cache_index], p_size, p_scale);
  2279. }
  2280. real_t FontFile::get_cache_scale(int p_cache_index, int p_size) const {
  2281. ERR_FAIL_COND_V(p_cache_index < 0, 0.f);
  2282. _ensure_rid(p_cache_index);
  2283. return TS->font_get_scale(cache[p_cache_index], p_size);
  2284. }
  2285. int FontFile::get_texture_count(int p_cache_index, const Vector2i &p_size) const {
  2286. ERR_FAIL_COND_V(p_cache_index < 0, 0);
  2287. _ensure_rid(p_cache_index);
  2288. return TS->font_get_texture_count(cache[p_cache_index], p_size);
  2289. }
  2290. void FontFile::clear_textures(int p_cache_index, const Vector2i &p_size) {
  2291. ERR_FAIL_COND(p_cache_index < 0);
  2292. _ensure_rid(p_cache_index);
  2293. TS->font_clear_textures(cache[p_cache_index], p_size);
  2294. }
  2295. void FontFile::remove_texture(int p_cache_index, const Vector2i &p_size, int p_texture_index) {
  2296. ERR_FAIL_COND(p_cache_index < 0);
  2297. _ensure_rid(p_cache_index);
  2298. TS->font_remove_texture(cache[p_cache_index], p_size, p_texture_index);
  2299. }
  2300. void FontFile::set_texture_image(int p_cache_index, const Vector2i &p_size, int p_texture_index, const Ref<Image> &p_image) {
  2301. ERR_FAIL_COND(p_cache_index < 0);
  2302. _ensure_rid(p_cache_index);
  2303. TS->font_set_texture_image(cache[p_cache_index], p_size, p_texture_index, p_image);
  2304. }
  2305. Ref<Image> FontFile::get_texture_image(int p_cache_index, const Vector2i &p_size, int p_texture_index) const {
  2306. ERR_FAIL_COND_V(p_cache_index < 0, Ref<Image>());
  2307. _ensure_rid(p_cache_index);
  2308. return TS->font_get_texture_image(cache[p_cache_index], p_size, p_texture_index);
  2309. }
  2310. void FontFile::set_texture_offsets(int p_cache_index, const Vector2i &p_size, int p_texture_index, const PackedInt32Array &p_offset) {
  2311. ERR_FAIL_COND(p_cache_index < 0);
  2312. _ensure_rid(p_cache_index);
  2313. TS->font_set_texture_offsets(cache[p_cache_index], p_size, p_texture_index, p_offset);
  2314. }
  2315. PackedInt32Array FontFile::get_texture_offsets(int p_cache_index, const Vector2i &p_size, int p_texture_index) const {
  2316. ERR_FAIL_COND_V(p_cache_index < 0, PackedInt32Array());
  2317. _ensure_rid(p_cache_index);
  2318. return TS->font_get_texture_offsets(cache[p_cache_index], p_size, p_texture_index);
  2319. }
  2320. PackedInt32Array FontFile::get_glyph_list(int p_cache_index, const Vector2i &p_size) const {
  2321. ERR_FAIL_COND_V(p_cache_index < 0, PackedInt32Array());
  2322. _ensure_rid(p_cache_index);
  2323. return TS->font_get_glyph_list(cache[p_cache_index], p_size);
  2324. }
  2325. void FontFile::clear_glyphs(int p_cache_index, const Vector2i &p_size) {
  2326. ERR_FAIL_COND(p_cache_index < 0);
  2327. _ensure_rid(p_cache_index);
  2328. TS->font_clear_glyphs(cache[p_cache_index], p_size);
  2329. }
  2330. void FontFile::remove_glyph(int p_cache_index, const Vector2i &p_size, int32_t p_glyph) {
  2331. ERR_FAIL_COND(p_cache_index < 0);
  2332. _ensure_rid(p_cache_index);
  2333. TS->font_remove_glyph(cache[p_cache_index], p_size, p_glyph);
  2334. }
  2335. void FontFile::set_glyph_advance(int p_cache_index, int p_size, int32_t p_glyph, const Vector2 &p_advance) {
  2336. ERR_FAIL_COND(p_cache_index < 0);
  2337. _ensure_rid(p_cache_index);
  2338. TS->font_set_glyph_advance(cache[p_cache_index], p_size, p_glyph, p_advance);
  2339. }
  2340. Vector2 FontFile::get_glyph_advance(int p_cache_index, int p_size, int32_t p_glyph) const {
  2341. ERR_FAIL_COND_V(p_cache_index < 0, Vector2());
  2342. _ensure_rid(p_cache_index);
  2343. return TS->font_get_glyph_advance(cache[p_cache_index], p_size, p_glyph);
  2344. }
  2345. void FontFile::set_glyph_offset(int p_cache_index, const Vector2i &p_size, int32_t p_glyph, const Vector2 &p_offset) {
  2346. ERR_FAIL_COND(p_cache_index < 0);
  2347. _ensure_rid(p_cache_index);
  2348. TS->font_set_glyph_offset(cache[p_cache_index], p_size, p_glyph, p_offset);
  2349. }
  2350. Vector2 FontFile::get_glyph_offset(int p_cache_index, const Vector2i &p_size, int32_t p_glyph) const {
  2351. ERR_FAIL_COND_V(p_cache_index < 0, Vector2());
  2352. _ensure_rid(p_cache_index);
  2353. return TS->font_get_glyph_offset(cache[p_cache_index], p_size, p_glyph);
  2354. }
  2355. void FontFile::set_glyph_size(int p_cache_index, const Vector2i &p_size, int32_t p_glyph, const Vector2 &p_gl_size) {
  2356. ERR_FAIL_COND(p_cache_index < 0);
  2357. _ensure_rid(p_cache_index);
  2358. TS->font_set_glyph_size(cache[p_cache_index], p_size, p_glyph, p_gl_size);
  2359. }
  2360. Vector2 FontFile::get_glyph_size(int p_cache_index, const Vector2i &p_size, int32_t p_glyph) const {
  2361. ERR_FAIL_COND_V(p_cache_index < 0, Vector2());
  2362. _ensure_rid(p_cache_index);
  2363. return TS->font_get_glyph_size(cache[p_cache_index], p_size, p_glyph);
  2364. }
  2365. void FontFile::set_glyph_uv_rect(int p_cache_index, const Vector2i &p_size, int32_t p_glyph, const Rect2 &p_uv_rect) {
  2366. ERR_FAIL_COND(p_cache_index < 0);
  2367. _ensure_rid(p_cache_index);
  2368. TS->font_set_glyph_uv_rect(cache[p_cache_index], p_size, p_glyph, p_uv_rect);
  2369. }
  2370. Rect2 FontFile::get_glyph_uv_rect(int p_cache_index, const Vector2i &p_size, int32_t p_glyph) const {
  2371. ERR_FAIL_COND_V(p_cache_index < 0, Rect2());
  2372. _ensure_rid(p_cache_index);
  2373. return TS->font_get_glyph_uv_rect(cache[p_cache_index], p_size, p_glyph);
  2374. }
  2375. void FontFile::set_glyph_texture_idx(int p_cache_index, const Vector2i &p_size, int32_t p_glyph, int p_texture_idx) {
  2376. ERR_FAIL_COND(p_cache_index < 0);
  2377. _ensure_rid(p_cache_index);
  2378. TS->font_set_glyph_texture_idx(cache[p_cache_index], p_size, p_glyph, p_texture_idx);
  2379. }
  2380. int FontFile::get_glyph_texture_idx(int p_cache_index, const Vector2i &p_size, int32_t p_glyph) const {
  2381. ERR_FAIL_COND_V(p_cache_index < 0, 0);
  2382. _ensure_rid(p_cache_index);
  2383. return TS->font_get_glyph_texture_idx(cache[p_cache_index], p_size, p_glyph);
  2384. }
  2385. TypedArray<Vector2i> FontFile::get_kerning_list(int p_cache_index, int p_size) const {
  2386. ERR_FAIL_COND_V(p_cache_index < 0, Array());
  2387. _ensure_rid(p_cache_index);
  2388. return TS->font_get_kerning_list(cache[p_cache_index], p_size);
  2389. }
  2390. void FontFile::clear_kerning_map(int p_cache_index, int p_size) {
  2391. ERR_FAIL_COND(p_cache_index < 0);
  2392. _ensure_rid(p_cache_index);
  2393. TS->font_clear_kerning_map(cache[p_cache_index], p_size);
  2394. }
  2395. void FontFile::remove_kerning(int p_cache_index, int p_size, const Vector2i &p_glyph_pair) {
  2396. ERR_FAIL_COND(p_cache_index < 0);
  2397. _ensure_rid(p_cache_index);
  2398. TS->font_remove_kerning(cache[p_cache_index], p_size, p_glyph_pair);
  2399. }
  2400. void FontFile::set_kerning(int p_cache_index, int p_size, const Vector2i &p_glyph_pair, const Vector2 &p_kerning) {
  2401. ERR_FAIL_COND(p_cache_index < 0);
  2402. _ensure_rid(p_cache_index);
  2403. TS->font_set_kerning(cache[p_cache_index], p_size, p_glyph_pair, p_kerning);
  2404. }
  2405. Vector2 FontFile::get_kerning(int p_cache_index, int p_size, const Vector2i &p_glyph_pair) const {
  2406. ERR_FAIL_COND_V(p_cache_index < 0, Vector2());
  2407. _ensure_rid(p_cache_index);
  2408. return TS->font_get_kerning(cache[p_cache_index], p_size, p_glyph_pair);
  2409. }
  2410. void FontFile::render_range(int p_cache_index, const Vector2i &p_size, char32_t p_start, char32_t p_end) {
  2411. ERR_FAIL_COND(p_cache_index < 0);
  2412. _ensure_rid(p_cache_index);
  2413. TS->font_render_range(cache[p_cache_index], p_size, p_start, p_end);
  2414. }
  2415. void FontFile::render_glyph(int p_cache_index, const Vector2i &p_size, int32_t p_index) {
  2416. ERR_FAIL_COND(p_cache_index < 0);
  2417. _ensure_rid(p_cache_index);
  2418. TS->font_render_glyph(cache[p_cache_index], p_size, p_index);
  2419. }
  2420. void FontFile::set_language_support_override(const String &p_language, bool p_supported) {
  2421. _ensure_rid(0);
  2422. TS->font_set_language_support_override(cache[0], p_language, p_supported);
  2423. }
  2424. bool FontFile::get_language_support_override(const String &p_language) const {
  2425. _ensure_rid(0);
  2426. return TS->font_get_language_support_override(cache[0], p_language);
  2427. }
  2428. void FontFile::remove_language_support_override(const String &p_language) {
  2429. _ensure_rid(0);
  2430. TS->font_remove_language_support_override(cache[0], p_language);
  2431. }
  2432. Vector<String> FontFile::get_language_support_overrides() const {
  2433. _ensure_rid(0);
  2434. return TS->font_get_language_support_overrides(cache[0]);
  2435. }
  2436. void FontFile::set_script_support_override(const String &p_script, bool p_supported) {
  2437. _ensure_rid(0);
  2438. TS->font_set_script_support_override(cache[0], p_script, p_supported);
  2439. }
  2440. bool FontFile::get_script_support_override(const String &p_script) const {
  2441. _ensure_rid(0);
  2442. return TS->font_get_script_support_override(cache[0], p_script);
  2443. }
  2444. void FontFile::remove_script_support_override(const String &p_script) {
  2445. _ensure_rid(0);
  2446. TS->font_remove_script_support_override(cache[0], p_script);
  2447. }
  2448. Vector<String> FontFile::get_script_support_overrides() const {
  2449. _ensure_rid(0);
  2450. return TS->font_get_script_support_overrides(cache[0]);
  2451. }
  2452. void FontFile::set_opentype_feature_overrides(const Dictionary &p_overrides) {
  2453. _ensure_rid(0);
  2454. TS->font_set_opentype_feature_overrides(cache[0], p_overrides);
  2455. }
  2456. Dictionary FontFile::get_opentype_feature_overrides() const {
  2457. _ensure_rid(0);
  2458. return TS->font_get_opentype_feature_overrides(cache[0]);
  2459. }
  2460. int32_t FontFile::get_glyph_index(int p_size, char32_t p_char, char32_t p_variation_selector) const {
  2461. _ensure_rid(0);
  2462. return TS->font_get_glyph_index(cache[0], p_size, p_char, p_variation_selector);
  2463. }
  2464. char32_t FontFile::get_char_from_glyph_index(int p_size, int32_t p_glyph_index) const {
  2465. _ensure_rid(0);
  2466. return TS->font_get_char_from_glyph_index(cache[0], p_size, p_glyph_index);
  2467. }
  2468. FontFile::FontFile() {
  2469. }
  2470. FontFile::~FontFile() {
  2471. _clear_cache();
  2472. }
  2473. /*************************************************************************/
  2474. /* FontVariation */
  2475. /*************************************************************************/
  2476. void FontVariation::_bind_methods() {
  2477. ClassDB::bind_method(D_METHOD("set_base_font", "font"), &FontVariation::set_base_font);
  2478. ClassDB::bind_method(D_METHOD("get_base_font"), &FontVariation::get_base_font);
  2479. ClassDB::bind_method(D_METHOD("set_variation_opentype", "coords"), &FontVariation::set_variation_opentype);
  2480. ClassDB::bind_method(D_METHOD("get_variation_opentype"), &FontVariation::get_variation_opentype);
  2481. ClassDB::bind_method(D_METHOD("set_variation_embolden", "strength"), &FontVariation::set_variation_embolden);
  2482. ClassDB::bind_method(D_METHOD("get_variation_embolden"), &FontVariation::get_variation_embolden);
  2483. ClassDB::bind_method(D_METHOD("set_variation_face_index", "face_index"), &FontVariation::set_variation_face_index);
  2484. ClassDB::bind_method(D_METHOD("get_variation_face_index"), &FontVariation::get_variation_face_index);
  2485. ClassDB::bind_method(D_METHOD("set_variation_transform", "transform"), &FontVariation::set_variation_transform);
  2486. ClassDB::bind_method(D_METHOD("get_variation_transform"), &FontVariation::get_variation_transform);
  2487. ClassDB::bind_method(D_METHOD("set_opentype_features", "features"), &FontVariation::set_opentype_features);
  2488. ClassDB::bind_method(D_METHOD("set_spacing", "spacing", "value"), &FontVariation::set_spacing);
  2489. ClassDB::bind_method(D_METHOD("set_baseline_offset", "baseline_offset"), &FontVariation::set_baseline_offset);
  2490. ClassDB::bind_method(D_METHOD("get_baseline_offset"), &FontVariation::get_baseline_offset);
  2491. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "base_font", PROPERTY_HINT_RESOURCE_TYPE, "Font"), "set_base_font", "get_base_font");
  2492. ADD_GROUP("Variation", "variation_");
  2493. ADD_PROPERTY(PropertyInfo(Variant::DICTIONARY, "variation_opentype"), "set_variation_opentype", "get_variation_opentype");
  2494. ADD_PROPERTY(PropertyInfo(Variant::INT, "variation_face_index"), "set_variation_face_index", "get_variation_face_index");
  2495. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "variation_embolden", PROPERTY_HINT_RANGE, "-2,2,0.01"), "set_variation_embolden", "get_variation_embolden");
  2496. ADD_PROPERTY(PropertyInfo(Variant::TRANSFORM2D, "variation_transform", PROPERTY_HINT_NONE, "suffix:px"), "set_variation_transform", "get_variation_transform");
  2497. ADD_GROUP("OpenType Features", "opentype_");
  2498. ADD_PROPERTY(PropertyInfo(Variant::DICTIONARY, "opentype_features"), "set_opentype_features", "get_opentype_features");
  2499. ADD_GROUP("Extra Spacing", "spacing_");
  2500. ADD_PROPERTYI(PropertyInfo(Variant::INT, "spacing_glyph", PROPERTY_HINT_NONE, "suffix:px"), "set_spacing", "get_spacing", TextServer::SPACING_GLYPH);
  2501. ADD_PROPERTYI(PropertyInfo(Variant::INT, "spacing_space", PROPERTY_HINT_NONE, "suffix:px"), "set_spacing", "get_spacing", TextServer::SPACING_SPACE);
  2502. ADD_PROPERTYI(PropertyInfo(Variant::INT, "spacing_top", PROPERTY_HINT_NONE, "suffix:px"), "set_spacing", "get_spacing", TextServer::SPACING_TOP);
  2503. ADD_PROPERTYI(PropertyInfo(Variant::INT, "spacing_bottom", PROPERTY_HINT_NONE, "suffix:px"), "set_spacing", "get_spacing", TextServer::SPACING_BOTTOM);
  2504. ADD_GROUP("Baseline", "baseline_");
  2505. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "baseline_offset", PROPERTY_HINT_RANGE, "-2,2,0.005"), "set_baseline_offset", "get_baseline_offset");
  2506. }
  2507. void FontVariation::_update_rids() const {
  2508. Ref<Font> f = _get_base_font_or_default();
  2509. rids.clear();
  2510. if (fallbacks.is_empty() && f.is_valid()) {
  2511. RID rid = _get_rid();
  2512. if (rid.is_valid()) {
  2513. rids.push_back(rid);
  2514. }
  2515. const TypedArray<Font> &base_fallbacks = f->get_fallbacks();
  2516. for (int i = 0; i < base_fallbacks.size(); i++) {
  2517. _update_rids_fb(base_fallbacks[i], 0);
  2518. }
  2519. } else {
  2520. _update_rids_fb(const_cast<FontVariation *>(this), 0);
  2521. }
  2522. dirty_rids = false;
  2523. }
  2524. void FontVariation::reset_state() {
  2525. if (base_font.is_valid()) {
  2526. base_font->disconnect_changed(callable_mp(reinterpret_cast<Font *>(this), &Font::_invalidate_rids));
  2527. base_font.unref();
  2528. }
  2529. if (theme_font.is_valid()) {
  2530. theme_font->disconnect_changed(callable_mp(reinterpret_cast<Font *>(this), &Font::_invalidate_rids));
  2531. theme_font.unref();
  2532. }
  2533. variation = Variation();
  2534. opentype_features = Dictionary();
  2535. for (int i = 0; i < TextServer::SPACING_MAX; i++) {
  2536. extra_spacing[i] = 0;
  2537. }
  2538. baseline_offset = 0.0;
  2539. Font::reset_state();
  2540. }
  2541. void FontVariation::set_base_font(const Ref<Font> &p_font) {
  2542. if (base_font != p_font) {
  2543. if (base_font.is_valid()) {
  2544. base_font->disconnect_changed(callable_mp(reinterpret_cast<Font *>(this), &Font::_invalidate_rids));
  2545. }
  2546. base_font = p_font;
  2547. if (base_font.is_valid()) {
  2548. base_font->connect_changed(callable_mp(reinterpret_cast<Font *>(this), &Font::_invalidate_rids), CONNECT_REFERENCE_COUNTED);
  2549. }
  2550. _invalidate_rids();
  2551. notify_property_list_changed();
  2552. }
  2553. }
  2554. Ref<Font> FontVariation::get_base_font() const {
  2555. return base_font;
  2556. }
  2557. Ref<Font> FontVariation::_get_base_font_or_default() const {
  2558. if (theme_font.is_valid()) {
  2559. theme_font->disconnect_changed(callable_mp(reinterpret_cast<Font *>(const_cast<FontVariation *>(this)), &Font::_invalidate_rids));
  2560. theme_font.unref();
  2561. }
  2562. if (base_font.is_valid()) {
  2563. return base_font;
  2564. }
  2565. StringName theme_name = "font";
  2566. Vector<StringName> theme_types;
  2567. ThemeDB::get_singleton()->get_native_type_dependencies(get_class_name(), theme_types);
  2568. ThemeContext *global_context = ThemeDB::get_singleton()->get_default_theme_context();
  2569. Vector<Ref<Theme>> themes = global_context->get_themes();
  2570. if (Engine::get_singleton()->is_editor_hint()) {
  2571. themes.insert(0, ThemeDB::get_singleton()->get_project_theme());
  2572. }
  2573. for (const Ref<Theme> &theme : themes) {
  2574. if (theme.is_null()) {
  2575. continue;
  2576. }
  2577. for (const StringName &E : theme_types) {
  2578. if (!theme->has_font(theme_name, E)) {
  2579. continue;
  2580. }
  2581. Ref<Font> f = theme->get_font(theme_name, E);
  2582. if (_is_base_cyclic(f, 0)) {
  2583. continue;
  2584. }
  2585. if (f.is_valid()) {
  2586. theme_font = f;
  2587. theme_font->connect_changed(callable_mp(reinterpret_cast<Font *>(const_cast<FontVariation *>(this)), &Font::_invalidate_rids), CONNECT_REFERENCE_COUNTED);
  2588. }
  2589. return f;
  2590. }
  2591. }
  2592. Ref<Font> f = global_context->get_fallback_theme()->get_font(theme_name, StringName());
  2593. if (!_is_base_cyclic(f, 0)) {
  2594. if (f.is_valid()) {
  2595. theme_font = f;
  2596. theme_font->connect_changed(callable_mp(reinterpret_cast<Font *>(const_cast<FontVariation *>(this)), &Font::_invalidate_rids), CONNECT_REFERENCE_COUNTED);
  2597. }
  2598. return f;
  2599. }
  2600. return Ref<Font>();
  2601. }
  2602. void FontVariation::set_variation_opentype(const Dictionary &p_coords) {
  2603. if (!variation.opentype.recursive_equal(p_coords, 1)) {
  2604. variation.opentype = p_coords.duplicate();
  2605. _invalidate_rids();
  2606. }
  2607. }
  2608. Dictionary FontVariation::get_variation_opentype() const {
  2609. return variation.opentype.duplicate();
  2610. }
  2611. void FontVariation::set_variation_embolden(float p_strength) {
  2612. if (variation.embolden != p_strength) {
  2613. variation.embolden = p_strength;
  2614. _invalidate_rids();
  2615. }
  2616. }
  2617. float FontVariation::get_variation_embolden() const {
  2618. return variation.embolden;
  2619. }
  2620. void FontVariation::set_variation_transform(Transform2D p_transform) {
  2621. if (variation.transform != p_transform) {
  2622. variation.transform = p_transform;
  2623. _invalidate_rids();
  2624. }
  2625. }
  2626. Transform2D FontVariation::get_variation_transform() const {
  2627. return variation.transform;
  2628. }
  2629. void FontVariation::set_variation_face_index(int p_face_index) {
  2630. if (variation.face_index != p_face_index) {
  2631. variation.face_index = p_face_index;
  2632. _invalidate_rids();
  2633. }
  2634. }
  2635. int FontVariation::get_variation_face_index() const {
  2636. return variation.face_index;
  2637. }
  2638. void FontVariation::set_opentype_features(const Dictionary &p_features) {
  2639. if (!opentype_features.recursive_equal(p_features, 1)) {
  2640. opentype_features = p_features.duplicate();
  2641. _invalidate_rids();
  2642. }
  2643. }
  2644. Dictionary FontVariation::get_opentype_features() const {
  2645. return opentype_features.duplicate();
  2646. }
  2647. void FontVariation::set_spacing(TextServer::SpacingType p_spacing, int p_value) {
  2648. ERR_FAIL_INDEX((int)p_spacing, TextServer::SPACING_MAX);
  2649. if (extra_spacing[p_spacing] != p_value) {
  2650. extra_spacing[p_spacing] = p_value;
  2651. _invalidate_rids();
  2652. }
  2653. }
  2654. int FontVariation::get_spacing(TextServer::SpacingType p_spacing) const {
  2655. ERR_FAIL_INDEX_V((int)p_spacing, TextServer::SPACING_MAX, 0);
  2656. return extra_spacing[p_spacing];
  2657. }
  2658. void FontVariation::set_baseline_offset(float p_baseline_offset) {
  2659. if (baseline_offset != p_baseline_offset) {
  2660. baseline_offset = p_baseline_offset;
  2661. _invalidate_rids();
  2662. }
  2663. }
  2664. float FontVariation::get_baseline_offset() const {
  2665. return baseline_offset;
  2666. }
  2667. RID FontVariation::find_variation(const Dictionary &p_variation_coordinates, int p_face_index, float p_strength, Transform2D p_transform, int p_spacing_top, int p_spacing_bottom, int p_spacing_space, int p_spacing_glyph, float p_baseline_offset) const {
  2668. Ref<Font> f = _get_base_font_or_default();
  2669. if (f.is_valid()) {
  2670. return f->find_variation(p_variation_coordinates, p_face_index, p_strength, p_transform, p_spacing_top, p_spacing_bottom, p_spacing_space, p_spacing_glyph, p_baseline_offset);
  2671. }
  2672. return RID();
  2673. }
  2674. RID FontVariation::_get_rid() const {
  2675. Ref<Font> f = _get_base_font_or_default();
  2676. if (f.is_valid()) {
  2677. return f->find_variation(variation.opentype, variation.face_index, variation.embolden, variation.transform, extra_spacing[TextServer::SPACING_TOP], extra_spacing[TextServer::SPACING_BOTTOM], extra_spacing[TextServer::SPACING_SPACE], extra_spacing[TextServer::SPACING_GLYPH], baseline_offset);
  2678. }
  2679. return RID();
  2680. }
  2681. FontVariation::FontVariation() {
  2682. for (int i = 0; i < TextServer::SPACING_MAX; i++) {
  2683. extra_spacing[i] = 0;
  2684. }
  2685. }
  2686. FontVariation::~FontVariation() {
  2687. }
  2688. /*************************************************************************/
  2689. /* SystemFont */
  2690. /*************************************************************************/
  2691. void SystemFont::_bind_methods() {
  2692. ClassDB::bind_method(D_METHOD("set_antialiasing", "antialiasing"), &SystemFont::set_antialiasing);
  2693. ClassDB::bind_method(D_METHOD("get_antialiasing"), &SystemFont::get_antialiasing);
  2694. ClassDB::bind_method(D_METHOD("set_disable_embedded_bitmaps", "disable_embedded_bitmaps"), &SystemFont::set_disable_embedded_bitmaps);
  2695. ClassDB::bind_method(D_METHOD("get_disable_embedded_bitmaps"), &SystemFont::get_disable_embedded_bitmaps);
  2696. ClassDB::bind_method(D_METHOD("set_generate_mipmaps", "generate_mipmaps"), &SystemFont::set_generate_mipmaps);
  2697. ClassDB::bind_method(D_METHOD("get_generate_mipmaps"), &SystemFont::get_generate_mipmaps);
  2698. ClassDB::bind_method(D_METHOD("set_allow_system_fallback", "allow_system_fallback"), &SystemFont::set_allow_system_fallback);
  2699. ClassDB::bind_method(D_METHOD("is_allow_system_fallback"), &SystemFont::is_allow_system_fallback);
  2700. ClassDB::bind_method(D_METHOD("set_force_autohinter", "force_autohinter"), &SystemFont::set_force_autohinter);
  2701. ClassDB::bind_method(D_METHOD("is_force_autohinter"), &SystemFont::is_force_autohinter);
  2702. ClassDB::bind_method(D_METHOD("set_hinting", "hinting"), &SystemFont::set_hinting);
  2703. ClassDB::bind_method(D_METHOD("get_hinting"), &SystemFont::get_hinting);
  2704. ClassDB::bind_method(D_METHOD("set_subpixel_positioning", "subpixel_positioning"), &SystemFont::set_subpixel_positioning);
  2705. ClassDB::bind_method(D_METHOD("get_subpixel_positioning"), &SystemFont::get_subpixel_positioning);
  2706. ClassDB::bind_method(D_METHOD("set_multichannel_signed_distance_field", "msdf"), &SystemFont::set_multichannel_signed_distance_field);
  2707. ClassDB::bind_method(D_METHOD("is_multichannel_signed_distance_field"), &SystemFont::is_multichannel_signed_distance_field);
  2708. ClassDB::bind_method(D_METHOD("set_msdf_pixel_range", "msdf_pixel_range"), &SystemFont::set_msdf_pixel_range);
  2709. ClassDB::bind_method(D_METHOD("get_msdf_pixel_range"), &SystemFont::get_msdf_pixel_range);
  2710. ClassDB::bind_method(D_METHOD("set_msdf_size", "msdf_size"), &SystemFont::set_msdf_size);
  2711. ClassDB::bind_method(D_METHOD("get_msdf_size"), &SystemFont::get_msdf_size);
  2712. ClassDB::bind_method(D_METHOD("set_oversampling", "oversampling"), &SystemFont::set_oversampling);
  2713. ClassDB::bind_method(D_METHOD("get_oversampling"), &SystemFont::get_oversampling);
  2714. ClassDB::bind_method(D_METHOD("get_font_names"), &SystemFont::get_font_names);
  2715. ClassDB::bind_method(D_METHOD("set_font_names", "names"), &SystemFont::set_font_names);
  2716. ClassDB::bind_method(D_METHOD("get_font_italic"), &SystemFont::get_font_italic);
  2717. ClassDB::bind_method(D_METHOD("set_font_italic", "italic"), &SystemFont::set_font_italic);
  2718. ClassDB::bind_method(D_METHOD("set_font_weight", "weight"), &SystemFont::set_font_weight);
  2719. ClassDB::bind_method(D_METHOD("set_font_stretch", "stretch"), &SystemFont::set_font_stretch);
  2720. ADD_PROPERTY(PropertyInfo(Variant::PACKED_STRING_ARRAY, "font_names"), "set_font_names", "get_font_names");
  2721. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "font_italic"), "set_font_italic", "get_font_italic");
  2722. ADD_PROPERTY(PropertyInfo(Variant::INT, "font_weight", PROPERTY_HINT_RANGE, "100,999,25"), "set_font_weight", "get_font_weight");
  2723. ADD_PROPERTY(PropertyInfo(Variant::INT, "font_stretch", PROPERTY_HINT_RANGE, "50,200,25"), "set_font_stretch", "get_font_stretch");
  2724. ADD_PROPERTY(PropertyInfo(Variant::INT, "antialiasing", PROPERTY_HINT_ENUM, "None,Grayscale,LCD Subpixel", PROPERTY_USAGE_STORAGE), "set_antialiasing", "get_antialiasing");
  2725. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "generate_mipmaps"), "set_generate_mipmaps", "get_generate_mipmaps");
  2726. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "disable_embedded_bitmaps"), "set_disable_embedded_bitmaps", "get_disable_embedded_bitmaps");
  2727. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "allow_system_fallback"), "set_allow_system_fallback", "is_allow_system_fallback");
  2728. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "force_autohinter"), "set_force_autohinter", "is_force_autohinter");
  2729. ADD_PROPERTY(PropertyInfo(Variant::INT, "hinting", PROPERTY_HINT_ENUM, "None,Light,Normal"), "set_hinting", "get_hinting");
  2730. ADD_PROPERTY(PropertyInfo(Variant::INT, "subpixel_positioning", PROPERTY_HINT_ENUM, "Disabled,Auto,One Half of a Pixel,One Quarter of a Pixel"), "set_subpixel_positioning", "get_subpixel_positioning");
  2731. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "multichannel_signed_distance_field"), "set_multichannel_signed_distance_field", "is_multichannel_signed_distance_field");
  2732. ADD_PROPERTY(PropertyInfo(Variant::INT, "msdf_pixel_range"), "set_msdf_pixel_range", "get_msdf_pixel_range");
  2733. ADD_PROPERTY(PropertyInfo(Variant::INT, "msdf_size"), "set_msdf_size", "get_msdf_size");
  2734. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "oversampling", PROPERTY_HINT_RANGE, "0,10,0.1"), "set_oversampling", "get_oversampling");
  2735. }
  2736. void SystemFont::_update_rids() const {
  2737. Ref<Font> f = _get_base_font_or_default();
  2738. rids.clear();
  2739. if (fallbacks.is_empty() && f.is_valid()) {
  2740. RID rid = _get_rid();
  2741. if (rid.is_valid()) {
  2742. rids.push_back(rid);
  2743. }
  2744. const TypedArray<Font> &base_fallbacks = f->get_fallbacks();
  2745. for (int i = 0; i < base_fallbacks.size(); i++) {
  2746. _update_rids_fb(base_fallbacks[i], 0);
  2747. }
  2748. } else {
  2749. _update_rids_fb(const_cast<SystemFont *>(this), 0);
  2750. }
  2751. dirty_rids = false;
  2752. }
  2753. void SystemFont::_update_base_font() {
  2754. if (base_font.is_valid()) {
  2755. base_font->disconnect_changed(callable_mp(reinterpret_cast<Font *>(this), &Font::_invalidate_rids));
  2756. base_font.unref();
  2757. }
  2758. face_indeces.clear();
  2759. ftr_weight = 0;
  2760. ftr_stretch = 0;
  2761. ftr_italic = 0;
  2762. for (const String &E : names) {
  2763. if (E.is_empty()) {
  2764. continue;
  2765. }
  2766. String path = OS::get_singleton()->get_system_font_path(E, weight, stretch, italic);
  2767. if (path.is_empty()) {
  2768. continue;
  2769. }
  2770. Ref<FontFile> file;
  2771. file.instantiate();
  2772. Error err = file->load_dynamic_font(path);
  2773. if (err != OK) {
  2774. continue;
  2775. }
  2776. // If it's a font collection check all faces to match requested style and name.
  2777. int best_score = 0;
  2778. for (int i = 0; i < file->get_face_count(); i++) {
  2779. int score = 0;
  2780. file->set_face_index(0, i);
  2781. const String n = file->get_font_name();
  2782. if (n.to_upper() == E.to_upper()) {
  2783. score += 80;
  2784. }
  2785. BitField<TextServer::FontStyle> style = file->get_font_style();
  2786. int font_weight = file->get_font_weight();
  2787. int font_stretch = file->get_font_stretch();
  2788. score += (20 - Math::abs(font_weight - weight) / 50);
  2789. score += (20 - Math::abs(font_stretch - stretch) / 10);
  2790. if (bool(style & TextServer::FONT_ITALIC) == italic) {
  2791. score += 30;
  2792. }
  2793. if (score > best_score) {
  2794. face_indeces.clear();
  2795. }
  2796. if (score >= best_score) {
  2797. best_score = score;
  2798. face_indeces.push_back(i);
  2799. }
  2800. }
  2801. if (face_indeces.is_empty()) {
  2802. face_indeces.push_back(0);
  2803. }
  2804. file->set_face_index(0, face_indeces[0]);
  2805. // If it's a variable font, apply weight, stretch and italic coordinates to match requested style.
  2806. if (best_score != 150) {
  2807. Dictionary ftr = file->get_supported_variation_list();
  2808. if (ftr.has(TS->name_to_tag("width"))) {
  2809. ftr_stretch = stretch;
  2810. }
  2811. if (ftr.has(TS->name_to_tag("weight"))) {
  2812. ftr_weight = weight;
  2813. }
  2814. if (italic && ftr.has(TS->name_to_tag("italic"))) {
  2815. ftr_italic = 1;
  2816. }
  2817. }
  2818. // Apply font rendering settings.
  2819. file->set_antialiasing(antialiasing);
  2820. file->set_generate_mipmaps(mipmaps);
  2821. file->set_disable_embedded_bitmaps(disable_embedded_bitmaps);
  2822. file->set_force_autohinter(force_autohinter);
  2823. file->set_allow_system_fallback(allow_system_fallback);
  2824. file->set_hinting(hinting);
  2825. file->set_subpixel_positioning(subpixel_positioning);
  2826. file->set_multichannel_signed_distance_field(msdf);
  2827. file->set_msdf_pixel_range(msdf_pixel_range);
  2828. file->set_msdf_size(msdf_size);
  2829. file->set_oversampling(oversampling);
  2830. base_font = file;
  2831. break;
  2832. }
  2833. if (base_font.is_valid()) {
  2834. base_font->connect_changed(callable_mp(reinterpret_cast<Font *>(this), &Font::_invalidate_rids), CONNECT_REFERENCE_COUNTED);
  2835. }
  2836. _invalidate_rids();
  2837. }
  2838. void SystemFont::reset_state() {
  2839. if (base_font.is_valid()) {
  2840. base_font->disconnect_changed(callable_mp(reinterpret_cast<Font *>(this), &Font::_invalidate_rids));
  2841. base_font.unref();
  2842. }
  2843. if (theme_font.is_valid()) {
  2844. theme_font->disconnect_changed(callable_mp(reinterpret_cast<Font *>(this), &Font::_invalidate_rids));
  2845. theme_font.unref();
  2846. }
  2847. names.clear();
  2848. face_indeces.clear();
  2849. ftr_weight = 0;
  2850. ftr_stretch = 0;
  2851. ftr_italic = 0;
  2852. italic = false;
  2853. weight = 400;
  2854. stretch = 100;
  2855. antialiasing = TextServer::FONT_ANTIALIASING_GRAY;
  2856. mipmaps = false;
  2857. disable_embedded_bitmaps = true;
  2858. force_autohinter = false;
  2859. allow_system_fallback = true;
  2860. hinting = TextServer::HINTING_LIGHT;
  2861. subpixel_positioning = TextServer::SUBPIXEL_POSITIONING_DISABLED;
  2862. oversampling = 0.f;
  2863. msdf = false;
  2864. Font::reset_state();
  2865. }
  2866. Ref<Font> SystemFont::_get_base_font_or_default() const {
  2867. if (theme_font.is_valid()) {
  2868. theme_font->disconnect_changed(callable_mp(reinterpret_cast<Font *>(const_cast<SystemFont *>(this)), &Font::_invalidate_rids));
  2869. theme_font.unref();
  2870. }
  2871. if (base_font.is_valid()) {
  2872. return base_font;
  2873. }
  2874. StringName theme_name = "font";
  2875. Vector<StringName> theme_types;
  2876. ThemeDB::get_singleton()->get_native_type_dependencies(get_class_name(), theme_types);
  2877. ThemeContext *global_context = ThemeDB::get_singleton()->get_default_theme_context();
  2878. for (const Ref<Theme> &theme : global_context->get_themes()) {
  2879. if (theme.is_null()) {
  2880. continue;
  2881. }
  2882. for (const StringName &E : theme_types) {
  2883. if (!theme->has_font(theme_name, E)) {
  2884. continue;
  2885. }
  2886. Ref<Font> f = theme->get_font(theme_name, E);
  2887. if (_is_base_cyclic(f, 0)) {
  2888. continue;
  2889. }
  2890. if (f.is_valid()) {
  2891. theme_font = f;
  2892. theme_font->connect_changed(callable_mp(reinterpret_cast<Font *>(const_cast<SystemFont *>(this)), &Font::_invalidate_rids), CONNECT_REFERENCE_COUNTED);
  2893. }
  2894. return f;
  2895. }
  2896. }
  2897. Ref<Font> f = global_context->get_fallback_theme()->get_font(theme_name, StringName());
  2898. if (!_is_base_cyclic(f, 0)) {
  2899. if (f.is_valid()) {
  2900. theme_font = f;
  2901. theme_font->connect_changed(callable_mp(reinterpret_cast<Font *>(const_cast<SystemFont *>(this)), &Font::_invalidate_rids), CONNECT_REFERENCE_COUNTED);
  2902. }
  2903. return f;
  2904. }
  2905. return Ref<Font>();
  2906. }
  2907. void SystemFont::set_antialiasing(TextServer::FontAntialiasing p_antialiasing) {
  2908. if (antialiasing != p_antialiasing) {
  2909. antialiasing = p_antialiasing;
  2910. if (base_font.is_valid()) {
  2911. base_font->set_antialiasing(antialiasing);
  2912. }
  2913. emit_changed();
  2914. }
  2915. }
  2916. TextServer::FontAntialiasing SystemFont::get_antialiasing() const {
  2917. return antialiasing;
  2918. }
  2919. void SystemFont::set_disable_embedded_bitmaps(bool p_disable_embedded_bitmaps) {
  2920. if (disable_embedded_bitmaps != p_disable_embedded_bitmaps) {
  2921. disable_embedded_bitmaps = p_disable_embedded_bitmaps;
  2922. if (base_font.is_valid()) {
  2923. base_font->set_disable_embedded_bitmaps(disable_embedded_bitmaps);
  2924. }
  2925. emit_changed();
  2926. }
  2927. }
  2928. bool SystemFont::get_disable_embedded_bitmaps() const {
  2929. return disable_embedded_bitmaps;
  2930. }
  2931. void SystemFont::set_generate_mipmaps(bool p_generate_mipmaps) {
  2932. if (mipmaps != p_generate_mipmaps) {
  2933. mipmaps = p_generate_mipmaps;
  2934. if (base_font.is_valid()) {
  2935. base_font->set_generate_mipmaps(mipmaps);
  2936. }
  2937. emit_changed();
  2938. }
  2939. }
  2940. bool SystemFont::get_generate_mipmaps() const {
  2941. return mipmaps;
  2942. }
  2943. void SystemFont::set_allow_system_fallback(bool p_allow_system_fallback) {
  2944. if (allow_system_fallback != p_allow_system_fallback) {
  2945. allow_system_fallback = p_allow_system_fallback;
  2946. if (base_font.is_valid()) {
  2947. base_font->set_allow_system_fallback(allow_system_fallback);
  2948. }
  2949. emit_changed();
  2950. }
  2951. }
  2952. bool SystemFont::is_allow_system_fallback() const {
  2953. return allow_system_fallback;
  2954. }
  2955. void SystemFont::set_force_autohinter(bool p_force_autohinter) {
  2956. if (force_autohinter != p_force_autohinter) {
  2957. force_autohinter = p_force_autohinter;
  2958. if (base_font.is_valid()) {
  2959. base_font->set_force_autohinter(force_autohinter);
  2960. }
  2961. emit_changed();
  2962. }
  2963. }
  2964. bool SystemFont::is_force_autohinter() const {
  2965. return force_autohinter;
  2966. }
  2967. void SystemFont::set_hinting(TextServer::Hinting p_hinting) {
  2968. if (hinting != p_hinting) {
  2969. hinting = p_hinting;
  2970. if (base_font.is_valid()) {
  2971. base_font->set_hinting(hinting);
  2972. }
  2973. emit_changed();
  2974. }
  2975. }
  2976. TextServer::Hinting SystemFont::get_hinting() const {
  2977. return hinting;
  2978. }
  2979. void SystemFont::set_subpixel_positioning(TextServer::SubpixelPositioning p_subpixel) {
  2980. if (subpixel_positioning != p_subpixel) {
  2981. subpixel_positioning = p_subpixel;
  2982. if (base_font.is_valid()) {
  2983. base_font->set_subpixel_positioning(subpixel_positioning);
  2984. }
  2985. emit_changed();
  2986. }
  2987. }
  2988. TextServer::SubpixelPositioning SystemFont::get_subpixel_positioning() const {
  2989. return subpixel_positioning;
  2990. }
  2991. void SystemFont::set_multichannel_signed_distance_field(bool p_msdf) {
  2992. if (msdf != p_msdf) {
  2993. msdf = p_msdf;
  2994. if (base_font.is_valid()) {
  2995. base_font->set_multichannel_signed_distance_field(msdf);
  2996. }
  2997. emit_changed();
  2998. }
  2999. }
  3000. bool SystemFont::is_multichannel_signed_distance_field() const {
  3001. return msdf;
  3002. }
  3003. void SystemFont::set_msdf_pixel_range(int p_msdf_pixel_range) {
  3004. if (msdf_pixel_range != p_msdf_pixel_range) {
  3005. msdf_pixel_range = p_msdf_pixel_range;
  3006. if (base_font.is_valid()) {
  3007. base_font->set_msdf_pixel_range(msdf_pixel_range);
  3008. }
  3009. emit_changed();
  3010. }
  3011. }
  3012. int SystemFont::get_msdf_pixel_range() const {
  3013. return msdf_pixel_range;
  3014. }
  3015. void SystemFont::set_msdf_size(int p_msdf_size) {
  3016. if (msdf_size != p_msdf_size) {
  3017. msdf_size = p_msdf_size;
  3018. if (base_font.is_valid()) {
  3019. base_font->set_msdf_size(msdf_size);
  3020. }
  3021. emit_changed();
  3022. }
  3023. }
  3024. int SystemFont::get_msdf_size() const {
  3025. return msdf_size;
  3026. }
  3027. void SystemFont::set_oversampling(real_t p_oversampling) {
  3028. if (oversampling != p_oversampling) {
  3029. oversampling = p_oversampling;
  3030. if (base_font.is_valid()) {
  3031. base_font->set_oversampling(oversampling);
  3032. }
  3033. emit_changed();
  3034. }
  3035. }
  3036. real_t SystemFont::get_oversampling() const {
  3037. return oversampling;
  3038. }
  3039. void SystemFont::set_font_names(const PackedStringArray &p_names) {
  3040. if (names != p_names) {
  3041. names = p_names;
  3042. _update_base_font();
  3043. }
  3044. }
  3045. PackedStringArray SystemFont::get_font_names() const {
  3046. return names;
  3047. }
  3048. void SystemFont::set_font_italic(bool p_italic) {
  3049. if (italic != p_italic) {
  3050. italic = p_italic;
  3051. _update_base_font();
  3052. }
  3053. }
  3054. bool SystemFont::get_font_italic() const {
  3055. return italic;
  3056. }
  3057. void SystemFont::set_font_weight(int p_weight) {
  3058. if (weight != p_weight) {
  3059. weight = CLAMP(p_weight, 100, 999);
  3060. _update_base_font();
  3061. }
  3062. }
  3063. int SystemFont::get_font_weight() const {
  3064. return weight;
  3065. }
  3066. void SystemFont::set_font_stretch(int p_stretch) {
  3067. if (stretch != p_stretch) {
  3068. stretch = CLAMP(p_stretch, 50, 200);
  3069. _update_base_font();
  3070. }
  3071. }
  3072. int SystemFont::get_font_stretch() const {
  3073. return stretch;
  3074. }
  3075. int SystemFont::get_spacing(TextServer::SpacingType p_spacing) const {
  3076. if (base_font.is_valid()) {
  3077. return base_font->get_spacing(p_spacing);
  3078. } else {
  3079. return 0;
  3080. }
  3081. }
  3082. RID SystemFont::find_variation(const Dictionary &p_variation_coordinates, int p_face_index, float p_strength, Transform2D p_transform, int p_spacing_top, int p_spacing_bottom, int p_spacing_space, int p_spacing_glyph, float p_baseline_offset) const {
  3083. Ref<Font> f = _get_base_font_or_default();
  3084. if (f.is_valid()) {
  3085. Dictionary var = p_variation_coordinates;
  3086. if (ftr_weight > 0 && !var.has(TS->name_to_tag("weight"))) {
  3087. var[TS->name_to_tag("weight")] = ftr_weight;
  3088. }
  3089. if (ftr_stretch > 0 && !var.has(TS->name_to_tag("width"))) {
  3090. var[TS->name_to_tag("width")] = ftr_stretch;
  3091. }
  3092. if (ftr_italic > 0 && !var.has(TS->name_to_tag("italic"))) {
  3093. var[TS->name_to_tag("italic")] = ftr_italic;
  3094. }
  3095. if (!face_indeces.is_empty()) {
  3096. int face_index = CLAMP(p_face_index, 0, face_indeces.size() - 1);
  3097. return f->find_variation(var, face_indeces[face_index], p_strength, p_transform, p_spacing_top, p_spacing_bottom, p_spacing_space, p_spacing_glyph, p_baseline_offset);
  3098. } else {
  3099. return f->find_variation(var, 0, p_strength, p_transform, p_spacing_top, p_spacing_bottom, p_spacing_space, p_spacing_glyph, p_baseline_offset);
  3100. }
  3101. }
  3102. return RID();
  3103. }
  3104. RID SystemFont::_get_rid() const {
  3105. Ref<Font> f = _get_base_font_or_default();
  3106. if (f.is_valid()) {
  3107. if (!face_indeces.is_empty()) {
  3108. Dictionary var;
  3109. if (ftr_weight > 0) {
  3110. var[TS->name_to_tag("weight")] = ftr_weight;
  3111. }
  3112. if (ftr_stretch > 0) {
  3113. var[TS->name_to_tag("width")] = ftr_stretch;
  3114. }
  3115. if (ftr_italic > 0) {
  3116. var[TS->name_to_tag("italic")] = ftr_italic;
  3117. }
  3118. return f->find_variation(var, face_indeces[0]);
  3119. } else {
  3120. return f->_get_rid();
  3121. }
  3122. }
  3123. return RID();
  3124. }
  3125. int64_t SystemFont::get_face_count() const {
  3126. return face_indeces.size();
  3127. }
  3128. SystemFont::SystemFont() {
  3129. /* NOP */
  3130. }
  3131. SystemFont::~SystemFont() {
  3132. }