font.cpp 116 KB

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