dynamic_font_fb.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  1. /*************************************************************************/
  2. /* dynamic_font_fb.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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 "dynamic_font_fb.h"
  31. #ifdef MODULE_FREETYPE_ENABLED
  32. #include FT_STROKER_H
  33. #include FT_ADVANCES_H
  34. DynamicFontDataFallback::DataAtSize *DynamicFontDataFallback::get_data_for_size(int p_size, int p_outline_size) {
  35. ERR_FAIL_COND_V(!valid, nullptr);
  36. ERR_FAIL_COND_V(p_size < 0 || p_size > UINT16_MAX, nullptr);
  37. ERR_FAIL_COND_V(p_outline_size < 0 || p_outline_size > UINT16_MAX, nullptr);
  38. CacheID id;
  39. id.size = p_size;
  40. id.outline_size = p_outline_size;
  41. DataAtSize *fds = nullptr;
  42. Map<CacheID, DataAtSize *>::Element *E = nullptr;
  43. if (p_outline_size != 0) {
  44. E = size_cache_outline.find(id);
  45. } else {
  46. E = size_cache.find(id);
  47. }
  48. if (E != nullptr) {
  49. fds = E->get();
  50. } else {
  51. if (font_mem == nullptr && font_path != String()) {
  52. if (!font_mem_cache.is_empty()) {
  53. font_mem = font_mem_cache.ptr();
  54. font_mem_size = font_mem_cache.size();
  55. } else {
  56. FileAccess *f = FileAccess::open(font_path, FileAccess::READ);
  57. if (!f) {
  58. ERR_FAIL_V_MSG(nullptr, "Cannot open font file '" + font_path + "'.");
  59. }
  60. size_t len = f->get_len();
  61. font_mem_cache.resize(len);
  62. f->get_buffer(font_mem_cache.ptrw(), len);
  63. font_mem = font_mem_cache.ptr();
  64. font_mem_size = len;
  65. f->close();
  66. }
  67. }
  68. int error = 0;
  69. fds = memnew(DataAtSize);
  70. if (font_mem) {
  71. memset(&fds->stream, 0, sizeof(FT_StreamRec));
  72. fds->stream.base = (unsigned char *)font_mem;
  73. fds->stream.size = font_mem_size;
  74. fds->stream.pos = 0;
  75. FT_Open_Args fargs;
  76. memset(&fargs, 0, sizeof(FT_Open_Args));
  77. fargs.memory_base = (unsigned char *)font_mem;
  78. fargs.memory_size = font_mem_size;
  79. fargs.flags = FT_OPEN_MEMORY;
  80. fargs.stream = &fds->stream;
  81. error = FT_Open_Face(library, &fargs, 0, &fds->face);
  82. } else {
  83. memdelete(fds);
  84. ERR_FAIL_V_MSG(nullptr, "DynamicFont uninitialized.");
  85. }
  86. if (error == FT_Err_Unknown_File_Format) {
  87. memdelete(fds);
  88. ERR_FAIL_V_MSG(nullptr, "Unknown font format.");
  89. } else if (error) {
  90. memdelete(fds);
  91. ERR_FAIL_V_MSG(nullptr, "Error loading font.");
  92. }
  93. oversampling = TS->font_get_oversampling();
  94. if (FT_HAS_COLOR(fds->face) && fds->face->num_fixed_sizes > 0) {
  95. int best_match = 0;
  96. int diff = ABS(p_size - ((int64_t)fds->face->available_sizes[0].width));
  97. fds->scale_color_font = float(p_size * oversampling) / fds->face->available_sizes[0].width;
  98. for (int i = 1; i < fds->face->num_fixed_sizes; i++) {
  99. int ndiff = ABS(p_size - ((int64_t)fds->face->available_sizes[i].width));
  100. if (ndiff < diff) {
  101. best_match = i;
  102. diff = ndiff;
  103. fds->scale_color_font = float(p_size * oversampling) / fds->face->available_sizes[i].width;
  104. }
  105. }
  106. FT_Select_Size(fds->face, best_match);
  107. } else {
  108. FT_Set_Pixel_Sizes(fds->face, 0, p_size * oversampling);
  109. }
  110. fds->size = p_size;
  111. fds->ascent = (fds->face->size->metrics.ascender / 64.0) / oversampling * fds->scale_color_font;
  112. fds->descent = (-fds->face->size->metrics.descender / 64.0) / oversampling * fds->scale_color_font;
  113. fds->underline_position = (-FT_MulFix(fds->face->underline_position, fds->face->size->metrics.y_scale) / 64.0) / oversampling * fds->scale_color_font;
  114. fds->underline_thickness = (FT_MulFix(fds->face->underline_thickness, fds->face->size->metrics.y_scale) / 64.0) / oversampling * fds->scale_color_font;
  115. if (p_outline_size != 0) {
  116. size_cache_outline[id] = fds;
  117. } else {
  118. size_cache[id] = fds;
  119. }
  120. }
  121. return fds;
  122. }
  123. DynamicFontDataFallback::TexturePosition DynamicFontDataFallback::find_texture_pos_for_glyph(DynamicFontDataFallback::DataAtSize *p_data, int p_color_size, Image::Format p_image_format, int p_width, int p_height) {
  124. TexturePosition ret;
  125. ret.index = -1;
  126. int mw = p_width;
  127. int mh = p_height;
  128. for (int i = 0; i < p_data->textures.size(); i++) {
  129. const CharTexture &ct = p_data->textures[i];
  130. if (RenderingServer::get_singleton() != nullptr) {
  131. if (ct.texture->get_format() != p_image_format) {
  132. continue;
  133. }
  134. }
  135. if (mw > ct.texture_size || mh > ct.texture_size) { //too big for this texture
  136. continue;
  137. }
  138. ret.y = 0x7FFFFFFF;
  139. ret.x = 0;
  140. for (int j = 0; j < ct.texture_size - mw; j++) {
  141. int max_y = 0;
  142. for (int k = j; k < j + mw; k++) {
  143. int y = ct.offsets[k];
  144. if (y > max_y) {
  145. max_y = y;
  146. }
  147. }
  148. if (max_y < ret.y) {
  149. ret.y = max_y;
  150. ret.x = j;
  151. }
  152. }
  153. if (ret.y == 0x7FFFFFFF || ret.y + mh > ct.texture_size) {
  154. continue; //fail, could not fit it here
  155. }
  156. ret.index = i;
  157. break;
  158. }
  159. if (ret.index == -1) {
  160. //could not find texture to fit, create one
  161. ret.x = 0;
  162. ret.y = 0;
  163. int texsize = MAX(p_data->size * oversampling * 8, 256);
  164. if (mw > texsize) {
  165. texsize = mw; //special case, adapt to it?
  166. }
  167. if (mh > texsize) {
  168. texsize = mh; //special case, adapt to it?
  169. }
  170. texsize = next_power_of_2(texsize);
  171. texsize = MIN(texsize, 4096);
  172. CharTexture tex;
  173. tex.texture_size = texsize;
  174. tex.imgdata.resize(texsize * texsize * p_color_size); //grayscale alpha
  175. {
  176. //zero texture
  177. uint8_t *w = tex.imgdata.ptrw();
  178. ERR_FAIL_COND_V(texsize * texsize * p_color_size > tex.imgdata.size(), ret);
  179. // Initialize the texture to all-white pixels to prevent artifacts when the
  180. // font is displayed at a non-default scale with filtering enabled.
  181. if (p_color_size == 2) {
  182. for (int i = 0; i < texsize * texsize * p_color_size; i += 2) { // FORMAT_LA8
  183. w[i + 0] = 255;
  184. w[i + 1] = 0;
  185. }
  186. } else if (p_color_size == 4) {
  187. for (int i = 0; i < texsize * texsize * p_color_size; i += 4) { // FORMAT_RGBA8
  188. w[i + 0] = 255;
  189. w[i + 1] = 255;
  190. w[i + 2] = 255;
  191. w[i + 3] = 0;
  192. }
  193. } else {
  194. ERR_FAIL_V(ret);
  195. }
  196. }
  197. tex.offsets.resize(texsize);
  198. for (int i = 0; i < texsize; i++) { //zero offsets
  199. tex.offsets.write[i] = 0;
  200. }
  201. p_data->textures.push_back(tex);
  202. ret.index = p_data->textures.size() - 1;
  203. }
  204. return ret;
  205. }
  206. DynamicFontDataFallback::Character DynamicFontDataFallback::Character::not_found() {
  207. Character ch;
  208. return ch;
  209. }
  210. DynamicFontDataFallback::Character DynamicFontDataFallback::bitmap_to_character(DynamicFontDataFallback::DataAtSize *p_data, FT_Bitmap bitmap, int yofs, int xofs, const Vector2 &advance) {
  211. int w = bitmap.width;
  212. int h = bitmap.rows;
  213. int mw = w + rect_margin * 2;
  214. int mh = h + rect_margin * 2;
  215. ERR_FAIL_COND_V(mw > 4096, Character::not_found());
  216. ERR_FAIL_COND_V(mh > 4096, Character::not_found());
  217. int color_size = bitmap.pixel_mode == FT_PIXEL_MODE_BGRA ? 4 : 2;
  218. Image::Format require_format = color_size == 4 ? Image::FORMAT_RGBA8 : Image::FORMAT_LA8;
  219. TexturePosition tex_pos = find_texture_pos_for_glyph(p_data, color_size, require_format, mw, mh);
  220. ERR_FAIL_COND_V(tex_pos.index < 0, Character::not_found());
  221. //fit character in char texture
  222. CharTexture &tex = p_data->textures.write[tex_pos.index];
  223. {
  224. uint8_t *wr = tex.imgdata.ptrw();
  225. for (int i = 0; i < h; i++) {
  226. for (int j = 0; j < w; j++) {
  227. int ofs = ((i + tex_pos.y + rect_margin) * tex.texture_size + j + tex_pos.x + rect_margin) * color_size;
  228. ERR_FAIL_COND_V(ofs >= tex.imgdata.size(), Character::not_found());
  229. switch (bitmap.pixel_mode) {
  230. case FT_PIXEL_MODE_MONO: {
  231. int byte = i * bitmap.pitch + (j >> 3);
  232. int bit = 1 << (7 - (j % 8));
  233. wr[ofs + 0] = 255; //grayscale as 1
  234. wr[ofs + 1] = (bitmap.buffer[byte] & bit) ? 255 : 0;
  235. } break;
  236. case FT_PIXEL_MODE_GRAY:
  237. wr[ofs + 0] = 255; //grayscale as 1
  238. wr[ofs + 1] = bitmap.buffer[i * bitmap.pitch + j];
  239. break;
  240. case FT_PIXEL_MODE_BGRA: {
  241. int ofs_color = i * bitmap.pitch + (j << 2);
  242. wr[ofs + 2] = bitmap.buffer[ofs_color + 0];
  243. wr[ofs + 1] = bitmap.buffer[ofs_color + 1];
  244. wr[ofs + 0] = bitmap.buffer[ofs_color + 2];
  245. wr[ofs + 3] = bitmap.buffer[ofs_color + 3];
  246. } break;
  247. // TODO: FT_PIXEL_MODE_LCD
  248. default:
  249. ERR_FAIL_V_MSG(Character::not_found(), "Font uses unsupported pixel format: " + itos(bitmap.pixel_mode) + ".");
  250. break;
  251. }
  252. }
  253. }
  254. }
  255. //blit to image and texture
  256. {
  257. if (RenderingServer::get_singleton() != nullptr) {
  258. Ref<Image> img = memnew(Image(tex.texture_size, tex.texture_size, 0, require_format, tex.imgdata));
  259. if (tex.texture.is_null()) {
  260. tex.texture.instance();
  261. tex.texture->create_from_image(img);
  262. } else {
  263. tex.texture->update(img); //update
  264. }
  265. }
  266. }
  267. // update height array
  268. for (int k = tex_pos.x; k < tex_pos.x + mw; k++) {
  269. tex.offsets.write[k] = tex_pos.y + mh;
  270. }
  271. Character chr;
  272. chr.align = (Vector2(xofs, -yofs) * p_data->scale_color_font / oversampling).round();
  273. chr.advance = (advance * p_data->scale_color_font / oversampling).round();
  274. chr.texture_idx = tex_pos.index;
  275. chr.found = true;
  276. chr.rect_uv = Rect2(tex_pos.x + rect_margin, tex_pos.y + rect_margin, w, h);
  277. chr.rect = chr.rect_uv;
  278. chr.rect.position /= oversampling;
  279. chr.rect.size *= (p_data->scale_color_font / oversampling);
  280. return chr;
  281. }
  282. void DynamicFontDataFallback::update_char(int p_size, char32_t p_char) {
  283. DataAtSize *fds = get_data_for_size(p_size, false);
  284. ERR_FAIL_COND(fds == nullptr);
  285. if (fds->char_map.has(p_char)) {
  286. return;
  287. }
  288. Character character = Character::not_found();
  289. FT_GlyphSlot slot = fds->face->glyph;
  290. FT_UInt gl_index = FT_Get_Char_Index(fds->face, p_char);
  291. if (gl_index == 0) {
  292. fds->char_map[p_char] = character;
  293. return;
  294. }
  295. int ft_hinting;
  296. switch (hinting) {
  297. case TextServer::HINTING_NONE:
  298. ft_hinting = FT_LOAD_NO_HINTING;
  299. break;
  300. case TextServer::HINTING_LIGHT:
  301. ft_hinting = FT_LOAD_TARGET_LIGHT;
  302. break;
  303. default:
  304. ft_hinting = FT_LOAD_TARGET_NORMAL;
  305. break;
  306. }
  307. FT_Fixed v, h;
  308. FT_Get_Advance(fds->face, gl_index, FT_HAS_COLOR(fds->face) ? FT_LOAD_COLOR : FT_LOAD_DEFAULT | (force_autohinter ? FT_LOAD_FORCE_AUTOHINT : 0) | ft_hinting, &h);
  309. FT_Get_Advance(fds->face, gl_index, FT_HAS_COLOR(fds->face) ? FT_LOAD_COLOR : FT_LOAD_DEFAULT | (force_autohinter ? FT_LOAD_FORCE_AUTOHINT : 0) | ft_hinting | FT_LOAD_VERTICAL_LAYOUT, &v);
  310. int error = FT_Load_Glyph(fds->face, gl_index, FT_HAS_COLOR(fds->face) ? FT_LOAD_COLOR : FT_LOAD_DEFAULT | (force_autohinter ? FT_LOAD_FORCE_AUTOHINT : 0) | ft_hinting);
  311. if (error) {
  312. fds->char_map[p_char] = character;
  313. return;
  314. }
  315. error = FT_Render_Glyph(fds->face->glyph, antialiased ? FT_RENDER_MODE_NORMAL : FT_RENDER_MODE_MONO);
  316. if (!error) {
  317. character = bitmap_to_character(fds, slot->bitmap, slot->bitmap_top, slot->bitmap_left, Vector2((h + (1 << 9)) >> 10, (v + (1 << 9)) >> 10) / 64.0);
  318. }
  319. fds->char_map[p_char] = character;
  320. }
  321. void DynamicFontDataFallback::update_char_outline(int p_size, int p_outline_size, char32_t p_char) {
  322. DataAtSize *fds = get_data_for_size(p_size, p_outline_size);
  323. ERR_FAIL_COND(fds == nullptr);
  324. if (fds->char_map.has(p_char)) {
  325. return;
  326. }
  327. Character character = Character::not_found();
  328. FT_UInt gl_index = FT_Get_Char_Index(fds->face, p_char);
  329. if (gl_index == 0) {
  330. fds->char_map[p_char] = character;
  331. return;
  332. }
  333. int error = FT_Load_Glyph(fds->face, gl_index, FT_LOAD_NO_BITMAP | (force_autohinter ? FT_LOAD_FORCE_AUTOHINT : 0));
  334. if (error) {
  335. fds->char_map[p_char] = character;
  336. return;
  337. }
  338. FT_Stroker stroker;
  339. if (FT_Stroker_New(library, &stroker) != 0) {
  340. fds->char_map[p_char] = character;
  341. return;
  342. }
  343. FT_Stroker_Set(stroker, (int)(p_outline_size * oversampling * 64.0), FT_STROKER_LINECAP_BUTT, FT_STROKER_LINEJOIN_ROUND, 0);
  344. FT_Glyph glyph;
  345. FT_BitmapGlyph glyph_bitmap;
  346. if (FT_Get_Glyph(fds->face->glyph, &glyph) != 0) {
  347. goto cleanup_stroker;
  348. }
  349. if (FT_Glyph_Stroke(&glyph, stroker, 1) != 0) {
  350. goto cleanup_glyph;
  351. }
  352. if (FT_Glyph_To_Bitmap(&glyph, antialiased ? FT_RENDER_MODE_NORMAL : FT_RENDER_MODE_MONO, nullptr, 1) != 0) {
  353. goto cleanup_glyph;
  354. }
  355. glyph_bitmap = (FT_BitmapGlyph)glyph;
  356. character = bitmap_to_character(fds, glyph_bitmap->bitmap, glyph_bitmap->top, glyph_bitmap->left, Vector2());
  357. cleanup_glyph:
  358. FT_Done_Glyph(glyph);
  359. cleanup_stroker:
  360. FT_Stroker_Done(stroker);
  361. fds->char_map[p_char] = character;
  362. }
  363. void DynamicFontDataFallback::clear_cache() {
  364. _THREAD_SAFE_METHOD_
  365. for (Map<CacheID, DataAtSize *>::Element *E = size_cache.front(); E; E = E->next()) {
  366. memdelete(E->get());
  367. }
  368. size_cache.clear();
  369. for (Map<CacheID, DataAtSize *>::Element *E = size_cache_outline.front(); E; E = E->next()) {
  370. memdelete(E->get());
  371. }
  372. size_cache_outline.clear();
  373. }
  374. Error DynamicFontDataFallback::load_from_file(const String &p_filename, int p_base_size) {
  375. _THREAD_SAFE_METHOD_
  376. if (library == nullptr) {
  377. int error = FT_Init_FreeType(&library);
  378. ERR_FAIL_COND_V_MSG(error != 0, ERR_CANT_CREATE, "Error initializing FreeType.");
  379. }
  380. clear_cache();
  381. font_path = p_filename;
  382. base_size = p_base_size;
  383. valid = true;
  384. DataAtSize *fds = get_data_for_size(base_size); // load base size.
  385. if (fds == nullptr) {
  386. valid = false;
  387. ERR_FAIL_V(ERR_CANT_CREATE);
  388. }
  389. return OK;
  390. }
  391. Error DynamicFontDataFallback::load_from_memory(const uint8_t *p_data, size_t p_size, int p_base_size) {
  392. _THREAD_SAFE_METHOD_
  393. if (library == nullptr) {
  394. int error = FT_Init_FreeType(&library);
  395. ERR_FAIL_COND_V_MSG(error != 0, ERR_CANT_CREATE, "Error initializing FreeType.");
  396. }
  397. clear_cache();
  398. font_mem = p_data;
  399. font_mem_size = p_size;
  400. base_size = p_base_size;
  401. valid = true;
  402. DataAtSize *fds = get_data_for_size(base_size); // load base size.
  403. if (fds == nullptr) {
  404. valid = false;
  405. ERR_FAIL_V(ERR_CANT_CREATE);
  406. }
  407. return OK;
  408. }
  409. float DynamicFontDataFallback::get_height(int p_size) const {
  410. _THREAD_SAFE_METHOD_
  411. DataAtSize *fds = const_cast<DynamicFontDataFallback *>(this)->get_data_for_size(p_size);
  412. ERR_FAIL_COND_V(fds == nullptr, 0.f);
  413. return fds->ascent + fds->descent;
  414. }
  415. float DynamicFontDataFallback::get_ascent(int p_size) const {
  416. _THREAD_SAFE_METHOD_
  417. DataAtSize *fds = const_cast<DynamicFontDataFallback *>(this)->get_data_for_size(p_size);
  418. ERR_FAIL_COND_V(fds == nullptr, 0.f);
  419. return fds->ascent;
  420. }
  421. float DynamicFontDataFallback::get_descent(int p_size) const {
  422. _THREAD_SAFE_METHOD_
  423. DataAtSize *fds = const_cast<DynamicFontDataFallback *>(this)->get_data_for_size(p_size);
  424. ERR_FAIL_COND_V(fds == nullptr, 0.f);
  425. return fds->descent;
  426. }
  427. float DynamicFontDataFallback::get_underline_position(int p_size) const {
  428. _THREAD_SAFE_METHOD_
  429. DataAtSize *fds = const_cast<DynamicFontDataFallback *>(this)->get_data_for_size(p_size);
  430. ERR_FAIL_COND_V(fds == nullptr, 0.f);
  431. return fds->underline_position;
  432. }
  433. float DynamicFontDataFallback::get_underline_thickness(int p_size) const {
  434. _THREAD_SAFE_METHOD_
  435. DataAtSize *fds = const_cast<DynamicFontDataFallback *>(this)->get_data_for_size(p_size);
  436. ERR_FAIL_COND_V(fds == nullptr, 0.f);
  437. return fds->underline_thickness;
  438. }
  439. void DynamicFontDataFallback::set_antialiased(bool p_antialiased) {
  440. if (antialiased != p_antialiased) {
  441. clear_cache();
  442. antialiased = p_antialiased;
  443. }
  444. }
  445. bool DynamicFontDataFallback::get_antialiased() const {
  446. return antialiased;
  447. }
  448. void DynamicFontDataFallback::set_force_autohinter(bool p_enabled) {
  449. if (force_autohinter != p_enabled) {
  450. clear_cache();
  451. force_autohinter = p_enabled;
  452. }
  453. }
  454. bool DynamicFontDataFallback::get_force_autohinter() const {
  455. return force_autohinter;
  456. }
  457. void DynamicFontDataFallback::set_hinting(TextServer::Hinting p_hinting) {
  458. if (hinting != p_hinting) {
  459. clear_cache();
  460. hinting = p_hinting;
  461. }
  462. }
  463. TextServer::Hinting DynamicFontDataFallback::get_hinting() const {
  464. return hinting;
  465. }
  466. bool DynamicFontDataFallback::has_outline() const {
  467. return true;
  468. }
  469. float DynamicFontDataFallback::get_base_size() const {
  470. return base_size;
  471. }
  472. bool DynamicFontDataFallback::has_char(char32_t p_char) const {
  473. _THREAD_SAFE_METHOD_
  474. DataAtSize *fds = const_cast<DynamicFontDataFallback *>(this)->get_data_for_size(base_size);
  475. ERR_FAIL_COND_V(fds == nullptr, false);
  476. const_cast<DynamicFontDataFallback *>(this)->update_char(base_size, p_char);
  477. Character ch = fds->char_map[p_char];
  478. return (ch.found);
  479. }
  480. String DynamicFontDataFallback::get_supported_chars() const {
  481. _THREAD_SAFE_METHOD_
  482. DataAtSize *fds = const_cast<DynamicFontDataFallback *>(this)->get_data_for_size(base_size);
  483. ERR_FAIL_COND_V(fds == nullptr, String());
  484. String chars;
  485. FT_UInt gindex;
  486. FT_ULong charcode = FT_Get_First_Char(fds->face, &gindex);
  487. while (gindex != 0) {
  488. if (charcode != 0) {
  489. chars += char32_t(charcode);
  490. }
  491. charcode = FT_Get_Next_Char(fds->face, charcode, &gindex);
  492. }
  493. return chars;
  494. }
  495. Vector2 DynamicFontDataFallback::get_advance(char32_t p_char, int p_size) const {
  496. _THREAD_SAFE_METHOD_
  497. DataAtSize *fds = const_cast<DynamicFontDataFallback *>(this)->get_data_for_size(p_size);
  498. ERR_FAIL_COND_V(fds == nullptr, Vector2());
  499. const_cast<DynamicFontDataFallback *>(this)->update_char(p_size, p_char);
  500. Character ch = fds->char_map[p_char];
  501. return ch.advance;
  502. }
  503. Vector2 DynamicFontDataFallback::get_kerning(char32_t p_char, char32_t p_next, int p_size) const {
  504. _THREAD_SAFE_METHOD_
  505. DataAtSize *fds = const_cast<DynamicFontDataFallback *>(this)->get_data_for_size(p_size);
  506. ERR_FAIL_COND_V(fds == nullptr, Vector2());
  507. FT_Vector delta;
  508. FT_Get_Kerning(fds->face, FT_Get_Char_Index(fds->face, p_char), FT_Get_Char_Index(fds->face, p_next), FT_KERNING_DEFAULT, &delta);
  509. return Vector2(delta.x, delta.y);
  510. }
  511. Vector2 DynamicFontDataFallback::draw_glyph(RID p_canvas, int p_size, const Vector2 &p_pos, uint32_t p_index, const Color &p_color) const {
  512. _THREAD_SAFE_METHOD_
  513. DataAtSize *fds = const_cast<DynamicFontDataFallback *>(this)->get_data_for_size(p_size);
  514. ERR_FAIL_COND_V(fds == nullptr, Vector2());
  515. const_cast<DynamicFontDataFallback *>(this)->update_char(p_size, p_index);
  516. Character ch = fds->char_map[p_index];
  517. Vector2 advance;
  518. if (ch.found) {
  519. ERR_FAIL_COND_V(ch.texture_idx < -1 || ch.texture_idx >= fds->textures.size(), Vector2());
  520. if (ch.texture_idx != -1) {
  521. Point2i cpos = p_pos;
  522. cpos += ch.align;
  523. Color modulate = p_color;
  524. if (FT_HAS_COLOR(fds->face)) {
  525. modulate.r = modulate.g = modulate.b = 1.0;
  526. }
  527. if (RenderingServer::get_singleton() != nullptr) {
  528. RID texture = fds->textures[ch.texture_idx].texture->get_rid();
  529. RenderingServer::get_singleton()->canvas_item_add_texture_rect_region(p_canvas, Rect2(cpos, ch.rect.size), texture, ch.rect_uv, modulate, false, false);
  530. }
  531. }
  532. advance = ch.advance;
  533. }
  534. return advance;
  535. }
  536. Vector2 DynamicFontDataFallback::draw_glyph_outline(RID p_canvas, int p_size, int p_outline_size, const Vector2 &p_pos, uint32_t p_index, const Color &p_color) const {
  537. _THREAD_SAFE_METHOD_
  538. DataAtSize *fds = const_cast<DynamicFontDataFallback *>(this)->get_data_for_size(p_size, p_outline_size);
  539. ERR_FAIL_COND_V(fds == nullptr, Vector2());
  540. const_cast<DynamicFontDataFallback *>(this)->update_char_outline(p_size, p_outline_size, p_index);
  541. Character ch = fds->char_map[p_index];
  542. Vector2 advance;
  543. if (ch.found) {
  544. ERR_FAIL_COND_V(ch.texture_idx < -1 || ch.texture_idx >= fds->textures.size(), Vector2());
  545. if (ch.texture_idx != -1) {
  546. Point2i cpos = p_pos;
  547. cpos += ch.align;
  548. Color modulate = p_color;
  549. if (FT_HAS_COLOR(fds->face)) {
  550. modulate.r = modulate.g = modulate.b = 1.0;
  551. }
  552. if (RenderingServer::get_singleton() != nullptr) {
  553. RID texture = fds->textures[ch.texture_idx].texture->get_rid();
  554. RenderingServer::get_singleton()->canvas_item_add_texture_rect_region(p_canvas, Rect2(cpos, ch.rect.size), texture, ch.rect_uv, modulate, false, false);
  555. }
  556. }
  557. advance = ch.advance;
  558. }
  559. return advance;
  560. }
  561. DynamicFontDataFallback::~DynamicFontDataFallback() {
  562. clear_cache();
  563. if (library != nullptr) {
  564. FT_Done_FreeType(library);
  565. }
  566. }
  567. #endif // MODULE_FREETYPE_ENABLED