dynamic_font.cpp 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195
  1. /*************************************************************************/
  2. /* dynamic_font.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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. #ifdef FREETYPE_ENABLED
  31. #include "dynamic_font.h"
  32. #include "core/os/file_access.h"
  33. #include "core/os/os.h"
  34. #include FT_STROKER_H
  35. #define __STDC_LIMIT_MACROS
  36. #include <stdint.h>
  37. bool DynamicFontData::CacheID::operator<(CacheID right) const {
  38. return key < right.key;
  39. }
  40. Ref<DynamicFontAtSize> DynamicFontData::_get_dynamic_font_at_size(CacheID p_cache_id) {
  41. if (size_cache.has(p_cache_id)) {
  42. return Ref<DynamicFontAtSize>(size_cache[p_cache_id]);
  43. }
  44. Ref<DynamicFontAtSize> dfas;
  45. dfas.instance();
  46. dfas->font = Ref<DynamicFontData>(this);
  47. size_cache[p_cache_id] = dfas.ptr();
  48. dfas->id = p_cache_id;
  49. dfas->_load();
  50. return dfas;
  51. }
  52. void DynamicFontData::set_font_ptr(const uint8_t *p_font_mem, int p_font_mem_size) {
  53. font_mem = p_font_mem;
  54. font_mem_size = p_font_mem_size;
  55. }
  56. void DynamicFontData::set_font_path(const String &p_path) {
  57. font_path = p_path;
  58. }
  59. String DynamicFontData::get_font_path() const {
  60. return font_path;
  61. }
  62. void DynamicFontData::set_force_autohinter(bool p_force) {
  63. force_autohinter = p_force;
  64. }
  65. void DynamicFontData::_bind_methods() {
  66. ClassDB::bind_method(D_METHOD("set_antialiased", "antialiased"), &DynamicFontData::set_antialiased);
  67. ClassDB::bind_method(D_METHOD("is_antialiased"), &DynamicFontData::is_antialiased);
  68. ClassDB::bind_method(D_METHOD("set_font_path", "path"), &DynamicFontData::set_font_path);
  69. ClassDB::bind_method(D_METHOD("get_font_path"), &DynamicFontData::get_font_path);
  70. ClassDB::bind_method(D_METHOD("set_hinting", "mode"), &DynamicFontData::set_hinting);
  71. ClassDB::bind_method(D_METHOD("get_hinting"), &DynamicFontData::get_hinting);
  72. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "antialiased"), "set_antialiased", "is_antialiased");
  73. ADD_PROPERTY(PropertyInfo(Variant::INT, "hinting", PROPERTY_HINT_ENUM, "None,Light,Normal"), "set_hinting", "get_hinting");
  74. BIND_ENUM_CONSTANT(HINTING_NONE);
  75. BIND_ENUM_CONSTANT(HINTING_LIGHT);
  76. BIND_ENUM_CONSTANT(HINTING_NORMAL);
  77. ADD_PROPERTY(PropertyInfo(Variant::STRING, "font_path", PROPERTY_HINT_FILE, "*.ttf,*.otf"), "set_font_path", "get_font_path");
  78. }
  79. DynamicFontData::DynamicFontData() {
  80. antialiased = true;
  81. force_autohinter = false;
  82. hinting = DynamicFontData::HINTING_NORMAL;
  83. font_mem = NULL;
  84. font_mem_size = 0;
  85. }
  86. DynamicFontData::~DynamicFontData() {
  87. }
  88. ////////////////////
  89. HashMap<String, Vector<uint8_t> > DynamicFontAtSize::_fontdata;
  90. Error DynamicFontAtSize::_load() {
  91. int error = FT_Init_FreeType(&library);
  92. ERR_FAIL_COND_V_MSG(error != 0, ERR_CANT_CREATE, "Error initializing FreeType.");
  93. // FT_OPEN_STREAM is extremely slow only on Android.
  94. if (OS::get_singleton()->get_name() == "Android" && font->font_mem == NULL && font->font_path != String()) {
  95. // cache font only once for each font->font_path
  96. if (_fontdata.has(font->font_path)) {
  97. font->set_font_ptr(_fontdata[font->font_path].ptr(), _fontdata[font->font_path].size());
  98. } else {
  99. FileAccess *f = FileAccess::open(font->font_path, FileAccess::READ);
  100. if (!f) {
  101. FT_Done_FreeType(library);
  102. ERR_FAIL_V_MSG(ERR_CANT_OPEN, "Cannot open font file '" + font->font_path + "'.");
  103. }
  104. size_t len = f->get_len();
  105. _fontdata[font->font_path] = Vector<uint8_t>();
  106. Vector<uint8_t> &fontdata = _fontdata[font->font_path];
  107. fontdata.resize(len);
  108. f->get_buffer(fontdata.ptrw(), len);
  109. font->set_font_ptr(fontdata.ptr(), len);
  110. f->close();
  111. }
  112. }
  113. if (font->font_mem == NULL && font->font_path != String()) {
  114. FileAccess *f = FileAccess::open(font->font_path, FileAccess::READ);
  115. if (!f) {
  116. FT_Done_FreeType(library);
  117. ERR_FAIL_V_MSG(ERR_CANT_OPEN, "Cannot open font file '" + font->font_path + "'.");
  118. }
  119. memset(&stream, 0, sizeof(FT_StreamRec));
  120. stream.base = NULL;
  121. stream.size = f->get_len();
  122. stream.pos = 0;
  123. stream.descriptor.pointer = f;
  124. stream.read = _ft_stream_io;
  125. stream.close = _ft_stream_close;
  126. FT_Open_Args fargs;
  127. memset(&fargs, 0, sizeof(FT_Open_Args));
  128. fargs.flags = FT_OPEN_STREAM;
  129. fargs.stream = &stream;
  130. error = FT_Open_Face(library, &fargs, 0, &face);
  131. } else if (font->font_mem) {
  132. memset(&stream, 0, sizeof(FT_StreamRec));
  133. stream.base = (unsigned char *)font->font_mem;
  134. stream.size = font->font_mem_size;
  135. stream.pos = 0;
  136. FT_Open_Args fargs;
  137. memset(&fargs, 0, sizeof(FT_Open_Args));
  138. fargs.memory_base = (unsigned char *)font->font_mem;
  139. fargs.memory_size = font->font_mem_size;
  140. fargs.flags = FT_OPEN_MEMORY;
  141. fargs.stream = &stream;
  142. error = FT_Open_Face(library, &fargs, 0, &face);
  143. } else {
  144. FT_Done_FreeType(library);
  145. ERR_FAIL_V_MSG(ERR_UNCONFIGURED, "DynamicFont uninitialized.");
  146. }
  147. //error = FT_New_Face( library, src_path.utf8().get_data(),0,&face );
  148. if (error == FT_Err_Unknown_File_Format) {
  149. FT_Done_FreeType(library);
  150. ERR_FAIL_V_MSG(ERR_FILE_CANT_OPEN, "Unknown font format.");
  151. } else if (error) {
  152. FT_Done_FreeType(library);
  153. ERR_FAIL_V_MSG(ERR_FILE_CANT_OPEN, "Error loading font.");
  154. }
  155. if (FT_HAS_COLOR(face) && face->num_fixed_sizes > 0) {
  156. int best_match = 0;
  157. int diff = ABS(id.size - ((int64_t)face->available_sizes[0].width));
  158. scale_color_font = float(id.size * oversampling) / face->available_sizes[0].width;
  159. for (int i = 1; i < face->num_fixed_sizes; i++) {
  160. int ndiff = ABS(id.size - ((int64_t)face->available_sizes[i].width));
  161. if (ndiff < diff) {
  162. best_match = i;
  163. diff = ndiff;
  164. scale_color_font = float(id.size * oversampling) / face->available_sizes[i].width;
  165. }
  166. }
  167. FT_Select_Size(face, best_match);
  168. } else {
  169. FT_Set_Pixel_Sizes(face, 0, id.size * oversampling);
  170. }
  171. ascent = (face->size->metrics.ascender / 64.0) / oversampling * scale_color_font;
  172. descent = (-face->size->metrics.descender / 64.0) / oversampling * scale_color_font;
  173. linegap = 0;
  174. texture_flags = 0;
  175. if (id.mipmaps)
  176. texture_flags |= Texture::FLAG_MIPMAPS;
  177. if (id.filter)
  178. texture_flags |= Texture::FLAG_FILTER;
  179. valid = true;
  180. return OK;
  181. }
  182. float DynamicFontAtSize::font_oversampling = 1.0;
  183. float DynamicFontAtSize::get_height() const {
  184. return ascent + descent;
  185. }
  186. float DynamicFontAtSize::get_ascent() const {
  187. return ascent;
  188. }
  189. float DynamicFontAtSize::get_descent() const {
  190. return descent;
  191. }
  192. const Pair<const DynamicFontAtSize::Character *, DynamicFontAtSize *> DynamicFontAtSize::_find_char_with_font(CharType p_char, const Vector<Ref<DynamicFontAtSize> > &p_fallbacks) const {
  193. const Character *chr = char_map.getptr(p_char);
  194. ERR_FAIL_COND_V(!chr, (Pair<const Character *, DynamicFontAtSize *>(NULL, NULL)));
  195. if (!chr->found) {
  196. //not found, try in fallbacks
  197. for (int i = 0; i < p_fallbacks.size(); i++) {
  198. DynamicFontAtSize *fb = const_cast<DynamicFontAtSize *>(p_fallbacks[i].ptr());
  199. if (!fb->valid)
  200. continue;
  201. fb->_update_char(p_char);
  202. const Character *fallback_chr = fb->char_map.getptr(p_char);
  203. ERR_CONTINUE(!fallback_chr);
  204. if (!fallback_chr->found)
  205. continue;
  206. return Pair<const Character *, DynamicFontAtSize *>(fallback_chr, fb);
  207. }
  208. //not found, try 0xFFFD to display 'not found'.
  209. const_cast<DynamicFontAtSize *>(this)->_update_char(0xFFFD);
  210. chr = char_map.getptr(0xFFFD);
  211. ERR_FAIL_COND_V(!chr, (Pair<const Character *, DynamicFontAtSize *>(NULL, NULL)));
  212. }
  213. return Pair<const Character *, DynamicFontAtSize *>(chr, const_cast<DynamicFontAtSize *>(this));
  214. }
  215. Size2 DynamicFontAtSize::get_char_size(CharType p_char, CharType p_next, const Vector<Ref<DynamicFontAtSize> > &p_fallbacks) const {
  216. if (!valid)
  217. return Size2(1, 1);
  218. const_cast<DynamicFontAtSize *>(this)->_update_char(p_char);
  219. Pair<const Character *, DynamicFontAtSize *> char_pair_with_font = _find_char_with_font(p_char, p_fallbacks);
  220. const Character *ch = char_pair_with_font.first;
  221. ERR_FAIL_COND_V(!ch, Size2());
  222. Size2 ret(0, get_height());
  223. if (ch->found) {
  224. ret.x = ch->advance;
  225. }
  226. return ret;
  227. }
  228. String DynamicFontAtSize::get_available_chars() const {
  229. String chars;
  230. FT_UInt gindex;
  231. FT_ULong charcode = FT_Get_First_Char(face, &gindex);
  232. while (gindex != 0) {
  233. if (charcode != 0) {
  234. chars += CharType(charcode);
  235. }
  236. charcode = FT_Get_Next_Char(face, charcode, &gindex);
  237. }
  238. return chars;
  239. }
  240. void DynamicFontAtSize::set_texture_flags(uint32_t p_flags) {
  241. texture_flags = p_flags;
  242. for (int i = 0; i < textures.size(); i++) {
  243. Ref<ImageTexture> &tex = textures.write[i].texture;
  244. if (!tex.is_null())
  245. tex->set_flags(p_flags);
  246. }
  247. }
  248. float DynamicFontAtSize::draw_char(RID p_canvas_item, const Point2 &p_pos, CharType p_char, CharType p_next, const Color &p_modulate, const Vector<Ref<DynamicFontAtSize> > &p_fallbacks, bool p_advance_only, bool p_outline) const {
  249. if (!valid)
  250. return 0;
  251. const_cast<DynamicFontAtSize *>(this)->_update_char(p_char);
  252. Pair<const Character *, DynamicFontAtSize *> char_pair_with_font = _find_char_with_font(p_char, p_fallbacks);
  253. const Character *ch = char_pair_with_font.first;
  254. DynamicFontAtSize *font = char_pair_with_font.second;
  255. ERR_FAIL_COND_V(!ch, 0.0);
  256. float advance = 0.0;
  257. // use normal character size if there's no outline character
  258. if (p_outline && !ch->found) {
  259. FT_GlyphSlot slot = face->glyph;
  260. int error = FT_Load_Char(face, p_char, FT_HAS_COLOR(face) ? FT_LOAD_COLOR : FT_LOAD_DEFAULT);
  261. if (!error) {
  262. error = FT_Render_Glyph(face->glyph, FT_RENDER_MODE_NORMAL);
  263. if (!error) {
  264. Character character = Character::not_found();
  265. character = const_cast<DynamicFontAtSize *>(this)->_bitmap_to_character(slot->bitmap, slot->bitmap_top, slot->bitmap_left, slot->advance.x / 64.0);
  266. advance = character.advance;
  267. }
  268. }
  269. }
  270. if (ch->found) {
  271. ERR_FAIL_COND_V(ch->texture_idx < -1 || ch->texture_idx >= font->textures.size(), 0);
  272. if (!p_advance_only && ch->texture_idx != -1) {
  273. Point2 cpos = p_pos;
  274. cpos.x += ch->h_align;
  275. cpos.y -= font->get_ascent();
  276. cpos.y += ch->v_align;
  277. Color modulate = p_modulate;
  278. if (FT_HAS_COLOR(face)) {
  279. modulate.r = modulate.g = modulate.b = 1.0;
  280. }
  281. RID texture = font->textures[ch->texture_idx].texture->get_rid();
  282. VisualServer::get_singleton()->canvas_item_add_texture_rect_region(p_canvas_item, Rect2(cpos, ch->rect.size), texture, ch->rect_uv, modulate, false, RID(), false);
  283. }
  284. advance = ch->advance;
  285. }
  286. return advance;
  287. }
  288. unsigned long DynamicFontAtSize::_ft_stream_io(FT_Stream stream, unsigned long offset, unsigned char *buffer, unsigned long count) {
  289. FileAccess *f = (FileAccess *)stream->descriptor.pointer;
  290. if (f->get_position() != offset) {
  291. f->seek(offset);
  292. }
  293. if (count == 0)
  294. return 0;
  295. return f->get_buffer(buffer, count);
  296. }
  297. void DynamicFontAtSize::_ft_stream_close(FT_Stream stream) {
  298. FileAccess *f = (FileAccess *)stream->descriptor.pointer;
  299. f->close();
  300. memdelete(f);
  301. }
  302. DynamicFontAtSize::Character DynamicFontAtSize::Character::not_found() {
  303. Character ch;
  304. ch.texture_idx = -1;
  305. ch.advance = 0;
  306. ch.h_align = 0;
  307. ch.v_align = 0;
  308. ch.found = false;
  309. return ch;
  310. }
  311. DynamicFontAtSize::TexturePosition DynamicFontAtSize::_find_texture_pos_for_glyph(int p_color_size, Image::Format p_image_format, int p_width, int p_height) {
  312. TexturePosition ret;
  313. ret.index = -1;
  314. ret.x = 0;
  315. ret.y = 0;
  316. int mw = p_width;
  317. int mh = p_height;
  318. for (int i = 0; i < textures.size(); i++) {
  319. const CharTexture &ct = textures[i];
  320. if (ct.texture->get_format() != p_image_format)
  321. continue;
  322. if (mw > ct.texture_size || mh > ct.texture_size) //too big for this texture
  323. continue;
  324. ret.y = 0x7FFFFFFF;
  325. ret.x = 0;
  326. for (int j = 0; j < ct.texture_size - mw; j++) {
  327. int max_y = 0;
  328. for (int k = j; k < j + mw; k++) {
  329. int y = ct.offsets[k];
  330. if (y > max_y)
  331. max_y = y;
  332. }
  333. if (max_y < ret.y) {
  334. ret.y = max_y;
  335. ret.x = j;
  336. }
  337. }
  338. if (ret.y == 0x7FFFFFFF || ret.y + mh > ct.texture_size)
  339. continue; //fail, could not fit it here
  340. ret.index = i;
  341. break;
  342. }
  343. if (ret.index == -1) {
  344. //could not find texture to fit, create one
  345. ret.x = 0;
  346. ret.y = 0;
  347. int texsize = MAX(id.size * oversampling * 8, 256);
  348. if (mw > texsize)
  349. texsize = mw; //special case, adapt to it?
  350. if (mh > texsize)
  351. texsize = mh; //special case, adapt to it?
  352. texsize = next_power_of_2(texsize);
  353. texsize = MIN(texsize, 4096);
  354. CharTexture tex;
  355. tex.texture_size = texsize;
  356. tex.imgdata.resize(texsize * texsize * p_color_size); //grayscale alpha
  357. {
  358. //zero texture
  359. PoolVector<uint8_t>::Write w = tex.imgdata.write();
  360. ERR_FAIL_COND_V(texsize * texsize * p_color_size > tex.imgdata.size(), ret);
  361. for (int i = 0; i < texsize * texsize * p_color_size; i++) {
  362. w[i] = 0;
  363. }
  364. }
  365. tex.offsets.resize(texsize);
  366. for (int i = 0; i < texsize; i++) //zero offsets
  367. tex.offsets.write[i] = 0;
  368. textures.push_back(tex);
  369. ret.index = textures.size() - 1;
  370. }
  371. return ret;
  372. }
  373. DynamicFontAtSize::Character DynamicFontAtSize::_bitmap_to_character(FT_Bitmap bitmap, int yofs, int xofs, float advance) {
  374. int w = bitmap.width;
  375. int h = bitmap.rows;
  376. int mw = w + rect_margin * 2;
  377. int mh = h + rect_margin * 2;
  378. ERR_FAIL_COND_V(mw > 4096, Character::not_found());
  379. ERR_FAIL_COND_V(mh > 4096, Character::not_found());
  380. int color_size = bitmap.pixel_mode == FT_PIXEL_MODE_BGRA ? 4 : 2;
  381. Image::Format require_format = color_size == 4 ? Image::FORMAT_RGBA8 : Image::FORMAT_LA8;
  382. TexturePosition tex_pos = _find_texture_pos_for_glyph(color_size, require_format, mw, mh);
  383. ERR_FAIL_COND_V(tex_pos.index < 0, Character::not_found());
  384. //fit character in char texture
  385. CharTexture &tex = textures.write[tex_pos.index];
  386. {
  387. PoolVector<uint8_t>::Write wr = tex.imgdata.write();
  388. for (int i = 0; i < h; i++) {
  389. for (int j = 0; j < w; j++) {
  390. int ofs = ((i + tex_pos.y + rect_margin) * tex.texture_size + j + tex_pos.x + rect_margin) * color_size;
  391. ERR_FAIL_COND_V(ofs >= tex.imgdata.size(), Character::not_found());
  392. switch (bitmap.pixel_mode) {
  393. case FT_PIXEL_MODE_MONO: {
  394. int byte = i * bitmap.pitch + (j >> 3);
  395. int bit = 1 << (7 - (j % 8));
  396. wr[ofs + 0] = 255; //grayscale as 1
  397. wr[ofs + 1] = (bitmap.buffer[byte] & bit) ? 255 : 0;
  398. } break;
  399. case FT_PIXEL_MODE_GRAY:
  400. wr[ofs + 0] = 255; //grayscale as 1
  401. wr[ofs + 1] = bitmap.buffer[i * bitmap.pitch + j];
  402. break;
  403. case FT_PIXEL_MODE_BGRA: {
  404. int ofs_color = i * bitmap.pitch + (j << 2);
  405. wr[ofs + 2] = bitmap.buffer[ofs_color + 0];
  406. wr[ofs + 1] = bitmap.buffer[ofs_color + 1];
  407. wr[ofs + 0] = bitmap.buffer[ofs_color + 2];
  408. wr[ofs + 3] = bitmap.buffer[ofs_color + 3];
  409. } break;
  410. // TODO: FT_PIXEL_MODE_LCD
  411. default:
  412. ERR_FAIL_V_MSG(Character::not_found(), "Font uses unsupported pixel format: " + itos(bitmap.pixel_mode) + ".");
  413. break;
  414. }
  415. }
  416. }
  417. }
  418. //blit to image and texture
  419. {
  420. Ref<Image> img = memnew(Image(tex.texture_size, tex.texture_size, 0, require_format, tex.imgdata));
  421. if (tex.texture.is_null()) {
  422. tex.texture.instance();
  423. tex.texture->create_from_image(img, Texture::FLAG_VIDEO_SURFACE | texture_flags);
  424. } else {
  425. tex.texture->set_data(img); //update
  426. }
  427. }
  428. // update height array
  429. for (int k = tex_pos.x; k < tex_pos.x + mw; k++) {
  430. tex.offsets.write[k] = tex_pos.y + mh;
  431. }
  432. Character chr;
  433. chr.h_align = xofs * scale_color_font / oversampling;
  434. chr.v_align = ascent - (yofs * scale_color_font / oversampling); // + ascent - descent;
  435. chr.advance = advance * scale_color_font / oversampling;
  436. chr.texture_idx = tex_pos.index;
  437. chr.found = true;
  438. chr.rect_uv = Rect2(tex_pos.x + rect_margin, tex_pos.y + rect_margin, w, h);
  439. chr.rect = chr.rect_uv;
  440. chr.rect.position /= oversampling;
  441. chr.rect.size = chr.rect.size * scale_color_font / oversampling;
  442. return chr;
  443. }
  444. DynamicFontAtSize::Character DynamicFontAtSize::_make_outline_char(CharType p_char) {
  445. Character ret = Character::not_found();
  446. if (FT_Load_Char(face, p_char, FT_LOAD_NO_BITMAP | (font->force_autohinter ? FT_LOAD_FORCE_AUTOHINT : 0)) != 0)
  447. return ret;
  448. FT_Stroker stroker;
  449. if (FT_Stroker_New(library, &stroker) != 0)
  450. return ret;
  451. FT_Stroker_Set(stroker, (int)(id.outline_size * oversampling * 64.0), FT_STROKER_LINECAP_BUTT, FT_STROKER_LINEJOIN_ROUND, 0);
  452. FT_Glyph glyph;
  453. FT_BitmapGlyph glyph_bitmap;
  454. if (FT_Get_Glyph(face->glyph, &glyph) != 0)
  455. goto cleanup_stroker;
  456. if (FT_Glyph_Stroke(&glyph, stroker, 1) != 0)
  457. goto cleanup_glyph;
  458. if (FT_Glyph_To_Bitmap(&glyph, font->antialiased ? FT_RENDER_MODE_NORMAL : FT_RENDER_MODE_MONO, nullptr, 1) != 0)
  459. goto cleanup_glyph;
  460. glyph_bitmap = (FT_BitmapGlyph)glyph;
  461. ret = _bitmap_to_character(glyph_bitmap->bitmap, glyph_bitmap->top, glyph_bitmap->left, glyph->advance.x / 65536.0);
  462. cleanup_glyph:
  463. FT_Done_Glyph(glyph);
  464. cleanup_stroker:
  465. FT_Stroker_Done(stroker);
  466. return ret;
  467. }
  468. void DynamicFontAtSize::_update_char(CharType p_char) {
  469. if (char_map.has(p_char))
  470. return;
  471. _THREAD_SAFE_METHOD_
  472. Character character = Character::not_found();
  473. FT_GlyphSlot slot = face->glyph;
  474. if (FT_Get_Char_Index(face, p_char) == 0) {
  475. char_map[p_char] = character;
  476. return;
  477. }
  478. int ft_hinting;
  479. switch (font->hinting) {
  480. case DynamicFontData::HINTING_NONE:
  481. ft_hinting = FT_LOAD_NO_HINTING;
  482. break;
  483. case DynamicFontData::HINTING_LIGHT:
  484. ft_hinting = FT_LOAD_TARGET_LIGHT;
  485. break;
  486. default:
  487. ft_hinting = FT_LOAD_TARGET_NORMAL;
  488. break;
  489. }
  490. int error = FT_Load_Char(face, p_char, FT_HAS_COLOR(face) ? FT_LOAD_COLOR : FT_LOAD_DEFAULT | (font->force_autohinter ? FT_LOAD_FORCE_AUTOHINT : 0) | ft_hinting);
  491. if (error) {
  492. char_map[p_char] = character;
  493. return;
  494. }
  495. if (id.outline_size > 0) {
  496. character = _make_outline_char(p_char);
  497. } else {
  498. error = FT_Render_Glyph(face->glyph, font->antialiased ? FT_RENDER_MODE_NORMAL : FT_RENDER_MODE_MONO);
  499. if (!error)
  500. character = _bitmap_to_character(slot->bitmap, slot->bitmap_top, slot->bitmap_left, slot->advance.x / 64.0);
  501. }
  502. char_map[p_char] = character;
  503. }
  504. void DynamicFontAtSize::update_oversampling() {
  505. if (oversampling == font_oversampling || !valid)
  506. return;
  507. FT_Done_FreeType(library);
  508. textures.clear();
  509. char_map.clear();
  510. oversampling = font_oversampling;
  511. valid = false;
  512. _load();
  513. }
  514. DynamicFontAtSize::DynamicFontAtSize() {
  515. valid = false;
  516. rect_margin = 1;
  517. ascent = 1;
  518. descent = 1;
  519. linegap = 1;
  520. texture_flags = 0;
  521. oversampling = font_oversampling;
  522. scale_color_font = 1;
  523. }
  524. DynamicFontAtSize::~DynamicFontAtSize() {
  525. if (valid) {
  526. FT_Done_FreeType(library);
  527. }
  528. font->size_cache.erase(id);
  529. font.unref();
  530. }
  531. /////////////////////////
  532. void DynamicFont::_reload_cache() {
  533. ERR_FAIL_COND(cache_id.size < 1);
  534. if (!data.is_valid()) {
  535. data_at_size.unref();
  536. outline_data_at_size.unref();
  537. fallbacks.resize(0);
  538. fallback_data_at_size.resize(0);
  539. fallback_outline_data_at_size.resize(0);
  540. return;
  541. }
  542. data_at_size = data->_get_dynamic_font_at_size(cache_id);
  543. if (outline_cache_id.outline_size > 0) {
  544. outline_data_at_size = data->_get_dynamic_font_at_size(outline_cache_id);
  545. fallback_outline_data_at_size.resize(fallback_data_at_size.size());
  546. } else {
  547. outline_data_at_size.unref();
  548. fallback_outline_data_at_size.resize(0);
  549. }
  550. for (int i = 0; i < fallbacks.size(); i++) {
  551. fallback_data_at_size.write[i] = fallbacks.write[i]->_get_dynamic_font_at_size(cache_id);
  552. if (outline_cache_id.outline_size > 0)
  553. fallback_outline_data_at_size.write[i] = fallbacks.write[i]->_get_dynamic_font_at_size(outline_cache_id);
  554. }
  555. emit_changed();
  556. _change_notify();
  557. }
  558. void DynamicFont::set_font_data(const Ref<DynamicFontData> &p_data) {
  559. data = p_data;
  560. _reload_cache();
  561. emit_changed();
  562. _change_notify();
  563. }
  564. Ref<DynamicFontData> DynamicFont::get_font_data() const {
  565. return data;
  566. }
  567. void DynamicFont::set_size(int p_size) {
  568. if (cache_id.size == p_size)
  569. return;
  570. cache_id.size = p_size;
  571. outline_cache_id.size = p_size;
  572. _reload_cache();
  573. }
  574. int DynamicFont::get_size() const {
  575. return cache_id.size;
  576. }
  577. void DynamicFont::set_outline_size(int p_size) {
  578. if (outline_cache_id.outline_size == p_size)
  579. return;
  580. ERR_FAIL_COND(p_size < 0 || p_size > UINT8_MAX);
  581. outline_cache_id.outline_size = p_size;
  582. _reload_cache();
  583. }
  584. int DynamicFont::get_outline_size() const {
  585. return outline_cache_id.outline_size;
  586. }
  587. void DynamicFont::set_outline_color(Color p_color) {
  588. if (p_color != outline_color) {
  589. outline_color = p_color;
  590. emit_changed();
  591. _change_notify();
  592. }
  593. }
  594. Color DynamicFont::get_outline_color() const {
  595. return outline_color;
  596. }
  597. bool DynamicFont::get_use_mipmaps() const {
  598. return cache_id.mipmaps;
  599. }
  600. void DynamicFont::set_use_mipmaps(bool p_enable) {
  601. if (cache_id.mipmaps == p_enable)
  602. return;
  603. cache_id.mipmaps = p_enable;
  604. outline_cache_id.mipmaps = p_enable;
  605. _reload_cache();
  606. }
  607. bool DynamicFont::get_use_filter() const {
  608. return cache_id.filter;
  609. }
  610. void DynamicFont::set_use_filter(bool p_enable) {
  611. if (cache_id.filter == p_enable)
  612. return;
  613. cache_id.filter = p_enable;
  614. outline_cache_id.filter = p_enable;
  615. _reload_cache();
  616. }
  617. bool DynamicFontData::is_antialiased() const {
  618. return antialiased;
  619. }
  620. void DynamicFontData::set_antialiased(bool p_antialiased) {
  621. if (antialiased == p_antialiased)
  622. return;
  623. antialiased = p_antialiased;
  624. }
  625. DynamicFontData::Hinting DynamicFontData::get_hinting() const {
  626. return hinting;
  627. }
  628. void DynamicFontData::set_hinting(Hinting p_hinting) {
  629. if (hinting == p_hinting)
  630. return;
  631. hinting = p_hinting;
  632. }
  633. int DynamicFont::get_spacing(int p_type) const {
  634. if (p_type == SPACING_TOP) {
  635. return spacing_top;
  636. } else if (p_type == SPACING_BOTTOM) {
  637. return spacing_bottom;
  638. } else if (p_type == SPACING_CHAR) {
  639. return spacing_char;
  640. } else if (p_type == SPACING_SPACE) {
  641. return spacing_space;
  642. }
  643. return 0;
  644. }
  645. void DynamicFont::set_spacing(int p_type, int p_value) {
  646. if (p_type == SPACING_TOP) {
  647. spacing_top = p_value;
  648. } else if (p_type == SPACING_BOTTOM) {
  649. spacing_bottom = p_value;
  650. } else if (p_type == SPACING_CHAR) {
  651. spacing_char = p_value;
  652. } else if (p_type == SPACING_SPACE) {
  653. spacing_space = p_value;
  654. }
  655. emit_changed();
  656. _change_notify();
  657. }
  658. float DynamicFont::get_height() const {
  659. if (!data_at_size.is_valid())
  660. return 1;
  661. return data_at_size->get_height() + spacing_top + spacing_bottom;
  662. }
  663. float DynamicFont::get_ascent() const {
  664. if (!data_at_size.is_valid())
  665. return 1;
  666. return data_at_size->get_ascent() + spacing_top;
  667. }
  668. float DynamicFont::get_descent() const {
  669. if (!data_at_size.is_valid())
  670. return 1;
  671. return data_at_size->get_descent() + spacing_bottom;
  672. }
  673. Size2 DynamicFont::get_char_size(CharType p_char, CharType p_next) const {
  674. if (!data_at_size.is_valid())
  675. return Size2(1, 1);
  676. Size2 ret = data_at_size->get_char_size(p_char, p_next, fallback_data_at_size);
  677. if (p_char == ' ')
  678. ret.width += spacing_space + spacing_char;
  679. else if (p_next)
  680. ret.width += spacing_char;
  681. return ret;
  682. }
  683. String DynamicFont::get_available_chars() const {
  684. if (!data_at_size.is_valid())
  685. return "";
  686. String chars = data_at_size->get_available_chars();
  687. for (int i = 0; i < fallback_data_at_size.size(); i++) {
  688. String fallback_chars = fallback_data_at_size[i]->get_available_chars();
  689. for (int j = 0; j < fallback_chars.length(); j++) {
  690. if (chars.find_char(fallback_chars[j]) == -1) {
  691. chars += fallback_chars[j];
  692. }
  693. }
  694. }
  695. return chars;
  696. }
  697. bool DynamicFont::is_distance_field_hint() const {
  698. return false;
  699. }
  700. bool DynamicFont::has_outline() const {
  701. return outline_cache_id.outline_size > 0;
  702. }
  703. float DynamicFont::draw_char(RID p_canvas_item, const Point2 &p_pos, CharType p_char, CharType p_next, const Color &p_modulate, bool p_outline) const {
  704. const Ref<DynamicFontAtSize> &font_at_size = p_outline && outline_cache_id.outline_size > 0 ? outline_data_at_size : data_at_size;
  705. if (!font_at_size.is_valid())
  706. return 0;
  707. const Vector<Ref<DynamicFontAtSize> > &fallbacks = p_outline && outline_cache_id.outline_size > 0 ? fallback_outline_data_at_size : fallback_data_at_size;
  708. Color color = p_outline && outline_cache_id.outline_size > 0 ? p_modulate * outline_color : p_modulate;
  709. // If requested outline draw, but no outline is present, simply return advance without drawing anything
  710. bool advance_only = p_outline && outline_cache_id.outline_size == 0;
  711. return font_at_size->draw_char(p_canvas_item, p_pos, p_char, p_next, color, fallbacks, advance_only, p_outline) + spacing_char;
  712. }
  713. void DynamicFont::set_fallback(int p_idx, const Ref<DynamicFontData> &p_data) {
  714. ERR_FAIL_COND(p_data.is_null());
  715. ERR_FAIL_INDEX(p_idx, fallbacks.size());
  716. fallbacks.write[p_idx] = p_data;
  717. fallback_data_at_size.write[p_idx] = fallbacks.write[p_idx]->_get_dynamic_font_at_size(cache_id);
  718. }
  719. void DynamicFont::add_fallback(const Ref<DynamicFontData> &p_data) {
  720. ERR_FAIL_COND(p_data.is_null());
  721. fallbacks.push_back(p_data);
  722. fallback_data_at_size.push_back(fallbacks.write[fallbacks.size() - 1]->_get_dynamic_font_at_size(cache_id)); //const..
  723. if (outline_cache_id.outline_size > 0)
  724. fallback_outline_data_at_size.push_back(fallbacks.write[fallbacks.size() - 1]->_get_dynamic_font_at_size(outline_cache_id));
  725. _change_notify();
  726. emit_changed();
  727. _change_notify();
  728. }
  729. int DynamicFont::get_fallback_count() const {
  730. return fallbacks.size();
  731. }
  732. Ref<DynamicFontData> DynamicFont::get_fallback(int p_idx) const {
  733. ERR_FAIL_INDEX_V(p_idx, fallbacks.size(), Ref<DynamicFontData>());
  734. return fallbacks[p_idx];
  735. }
  736. void DynamicFont::remove_fallback(int p_idx) {
  737. ERR_FAIL_INDEX(p_idx, fallbacks.size());
  738. fallbacks.remove(p_idx);
  739. fallback_data_at_size.remove(p_idx);
  740. emit_changed();
  741. _change_notify();
  742. }
  743. bool DynamicFont::_set(const StringName &p_name, const Variant &p_value) {
  744. String str = p_name;
  745. if (str.begins_with("fallback/")) {
  746. int idx = str.get_slicec('/', 1).to_int();
  747. Ref<DynamicFontData> fd = p_value;
  748. if (fd.is_valid()) {
  749. if (idx == fallbacks.size()) {
  750. add_fallback(fd);
  751. return true;
  752. } else if (idx >= 0 && idx < fallbacks.size()) {
  753. set_fallback(idx, fd);
  754. return true;
  755. } else {
  756. return false;
  757. }
  758. } else if (idx >= 0 && idx < fallbacks.size()) {
  759. remove_fallback(idx);
  760. return true;
  761. }
  762. }
  763. return false;
  764. }
  765. bool DynamicFont::_get(const StringName &p_name, Variant &r_ret) const {
  766. String str = p_name;
  767. if (str.begins_with("fallback/")) {
  768. int idx = str.get_slicec('/', 1).to_int();
  769. if (idx == fallbacks.size()) {
  770. r_ret = Ref<DynamicFontData>();
  771. return true;
  772. } else if (idx >= 0 && idx < fallbacks.size()) {
  773. r_ret = get_fallback(idx);
  774. return true;
  775. }
  776. }
  777. return false;
  778. }
  779. void DynamicFont::_get_property_list(List<PropertyInfo> *p_list) const {
  780. for (int i = 0; i < fallbacks.size(); i++) {
  781. p_list->push_back(PropertyInfo(Variant::OBJECT, "fallback/" + itos(i), PROPERTY_HINT_RESOURCE_TYPE, "DynamicFontData"));
  782. }
  783. p_list->push_back(PropertyInfo(Variant::OBJECT, "fallback/" + itos(fallbacks.size()), PROPERTY_HINT_RESOURCE_TYPE, "DynamicFontData"));
  784. }
  785. void DynamicFont::_bind_methods() {
  786. ClassDB::bind_method(D_METHOD("set_font_data", "data"), &DynamicFont::set_font_data);
  787. ClassDB::bind_method(D_METHOD("get_font_data"), &DynamicFont::get_font_data);
  788. ClassDB::bind_method(D_METHOD("get_available_chars"), &DynamicFont::get_available_chars);
  789. ClassDB::bind_method(D_METHOD("set_size", "data"), &DynamicFont::set_size);
  790. ClassDB::bind_method(D_METHOD("get_size"), &DynamicFont::get_size);
  791. ClassDB::bind_method(D_METHOD("set_outline_size", "size"), &DynamicFont::set_outline_size);
  792. ClassDB::bind_method(D_METHOD("get_outline_size"), &DynamicFont::get_outline_size);
  793. ClassDB::bind_method(D_METHOD("set_outline_color", "color"), &DynamicFont::set_outline_color);
  794. ClassDB::bind_method(D_METHOD("get_outline_color"), &DynamicFont::get_outline_color);
  795. ClassDB::bind_method(D_METHOD("set_use_mipmaps", "enable"), &DynamicFont::set_use_mipmaps);
  796. ClassDB::bind_method(D_METHOD("get_use_mipmaps"), &DynamicFont::get_use_mipmaps);
  797. ClassDB::bind_method(D_METHOD("set_use_filter", "enable"), &DynamicFont::set_use_filter);
  798. ClassDB::bind_method(D_METHOD("get_use_filter"), &DynamicFont::get_use_filter);
  799. ClassDB::bind_method(D_METHOD("set_spacing", "type", "value"), &DynamicFont::set_spacing);
  800. ClassDB::bind_method(D_METHOD("get_spacing", "type"), &DynamicFont::get_spacing);
  801. ClassDB::bind_method(D_METHOD("add_fallback", "data"), &DynamicFont::add_fallback);
  802. ClassDB::bind_method(D_METHOD("set_fallback", "idx", "data"), &DynamicFont::set_fallback);
  803. ClassDB::bind_method(D_METHOD("get_fallback", "idx"), &DynamicFont::get_fallback);
  804. ClassDB::bind_method(D_METHOD("remove_fallback", "idx"), &DynamicFont::remove_fallback);
  805. ClassDB::bind_method(D_METHOD("get_fallback_count"), &DynamicFont::get_fallback_count);
  806. ADD_GROUP("Settings", "");
  807. ADD_PROPERTY(PropertyInfo(Variant::INT, "size", PROPERTY_HINT_RANGE, "1,1024,1"), "set_size", "get_size");
  808. ADD_PROPERTY(PropertyInfo(Variant::INT, "outline_size", PROPERTY_HINT_RANGE, "0,1024,1"), "set_outline_size", "get_outline_size");
  809. ADD_PROPERTY(PropertyInfo(Variant::COLOR, "outline_color"), "set_outline_color", "get_outline_color");
  810. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_mipmaps"), "set_use_mipmaps", "get_use_mipmaps");
  811. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_filter"), "set_use_filter", "get_use_filter");
  812. ADD_GROUP("Extra Spacing", "extra_spacing");
  813. ADD_PROPERTYI(PropertyInfo(Variant::INT, "extra_spacing_top"), "set_spacing", "get_spacing", SPACING_TOP);
  814. ADD_PROPERTYI(PropertyInfo(Variant::INT, "extra_spacing_bottom"), "set_spacing", "get_spacing", SPACING_BOTTOM);
  815. ADD_PROPERTYI(PropertyInfo(Variant::INT, "extra_spacing_char"), "set_spacing", "get_spacing", SPACING_CHAR);
  816. ADD_PROPERTYI(PropertyInfo(Variant::INT, "extra_spacing_space"), "set_spacing", "get_spacing", SPACING_SPACE);
  817. ADD_GROUP("Font", "");
  818. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "font_data", PROPERTY_HINT_RESOURCE_TYPE, "DynamicFontData"), "set_font_data", "get_font_data");
  819. BIND_ENUM_CONSTANT(SPACING_TOP);
  820. BIND_ENUM_CONSTANT(SPACING_BOTTOM);
  821. BIND_ENUM_CONSTANT(SPACING_CHAR);
  822. BIND_ENUM_CONSTANT(SPACING_SPACE);
  823. }
  824. Mutex *DynamicFont::dynamic_font_mutex = NULL;
  825. SelfList<DynamicFont>::List *DynamicFont::dynamic_fonts = NULL;
  826. DynamicFont::DynamicFont() :
  827. font_list(this) {
  828. cache_id.size = 16;
  829. outline_cache_id.size = 16;
  830. spacing_top = 0;
  831. spacing_bottom = 0;
  832. spacing_char = 0;
  833. spacing_space = 0;
  834. outline_color = Color(1, 1, 1);
  835. if (dynamic_font_mutex) {
  836. dynamic_font_mutex->lock();
  837. dynamic_fonts->add(&font_list);
  838. dynamic_font_mutex->unlock();
  839. }
  840. }
  841. DynamicFont::~DynamicFont() {
  842. if (dynamic_font_mutex) {
  843. dynamic_font_mutex->lock();
  844. dynamic_fonts->remove(&font_list);
  845. dynamic_font_mutex->unlock();
  846. }
  847. }
  848. void DynamicFont::initialize_dynamic_fonts() {
  849. dynamic_fonts = memnew(SelfList<DynamicFont>::List());
  850. dynamic_font_mutex = Mutex::create();
  851. }
  852. void DynamicFont::finish_dynamic_fonts() {
  853. memdelete(dynamic_font_mutex);
  854. dynamic_font_mutex = NULL;
  855. memdelete(dynamic_fonts);
  856. dynamic_fonts = NULL;
  857. }
  858. void DynamicFont::update_oversampling() {
  859. Vector<Ref<DynamicFont> > changed;
  860. if (dynamic_font_mutex)
  861. dynamic_font_mutex->lock();
  862. SelfList<DynamicFont> *E = dynamic_fonts->first();
  863. while (E) {
  864. if (E->self()->data_at_size.is_valid()) {
  865. E->self()->data_at_size->update_oversampling();
  866. if (E->self()->outline_data_at_size.is_valid()) {
  867. E->self()->outline_data_at_size->update_oversampling();
  868. }
  869. for (int i = 0; i < E->self()->fallback_data_at_size.size(); i++) {
  870. if (E->self()->fallback_data_at_size[i].is_valid()) {
  871. E->self()->fallback_data_at_size.write[i]->update_oversampling();
  872. if (E->self()->has_outline() && E->self()->fallback_outline_data_at_size[i].is_valid()) {
  873. E->self()->fallback_outline_data_at_size.write[i]->update_oversampling();
  874. }
  875. }
  876. }
  877. changed.push_back(Ref<DynamicFont>(E->self()));
  878. }
  879. E = E->next();
  880. }
  881. if (dynamic_font_mutex)
  882. dynamic_font_mutex->unlock();
  883. for (int i = 0; i < changed.size(); i++) {
  884. changed.write[i]->emit_changed();
  885. }
  886. }
  887. /////////////////////////
  888. RES ResourceFormatLoaderDynamicFont::load(const String &p_path, const String &p_original_path, Error *r_error) {
  889. if (r_error)
  890. *r_error = ERR_FILE_CANT_OPEN;
  891. Ref<DynamicFontData> dfont;
  892. dfont.instance();
  893. dfont->set_font_path(p_path);
  894. if (r_error)
  895. *r_error = OK;
  896. return dfont;
  897. }
  898. void ResourceFormatLoaderDynamicFont::get_recognized_extensions(List<String> *p_extensions) const {
  899. p_extensions->push_back("ttf");
  900. p_extensions->push_back("otf");
  901. }
  902. bool ResourceFormatLoaderDynamicFont::handles_type(const String &p_type) const {
  903. return (p_type == "DynamicFontData");
  904. }
  905. String ResourceFormatLoaderDynamicFont::get_resource_type(const String &p_path) const {
  906. String el = p_path.get_extension().to_lower();
  907. if (el == "ttf" || el == "otf")
  908. return "DynamicFontData";
  909. return "";
  910. }
  911. #endif