font.cpp 130 KB

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