font.cpp 145 KB

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