font.cpp 134 KB

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