font.cpp 143 KB

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