2
0

dynamic_font_adv.cpp 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030
  1. /*************************************************************************/
  2. /* dynamic_font_adv.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_adv.h"
  31. #ifdef MODULE_FREETYPE_ENABLED
  32. #include FT_STROKER_H
  33. #include FT_ADVANCES_H
  34. #include FT_MULTIPLE_MASTERS_H
  35. DynamicFontDataAdvanced::DataAtSize *DynamicFontDataAdvanced::get_data_for_size(int p_size, int p_outline_size) {
  36. ERR_FAIL_COND_V(!valid, nullptr);
  37. ERR_FAIL_COND_V(p_size < 0 || p_size > UINT16_MAX, nullptr);
  38. ERR_FAIL_COND_V(p_outline_size < 0 || p_outline_size > UINT16_MAX, nullptr);
  39. CacheID id;
  40. id.size = p_size;
  41. id.outline_size = p_outline_size;
  42. DataAtSize *fds = nullptr;
  43. Map<CacheID, DataAtSize *>::Element *E = nullptr;
  44. if (p_outline_size != 0) {
  45. E = size_cache_outline.find(id);
  46. } else {
  47. E = size_cache.find(id);
  48. }
  49. if (E != nullptr) {
  50. fds = E->get();
  51. } else {
  52. if (font_mem == nullptr && font_path != String()) {
  53. if (!font_mem_cache.is_empty()) {
  54. font_mem = font_mem_cache.ptr();
  55. font_mem_size = font_mem_cache.size();
  56. } else {
  57. FileAccess *f = FileAccess::open(font_path, FileAccess::READ);
  58. if (!f) {
  59. ERR_FAIL_V_MSG(nullptr, "Cannot open font file '" + font_path + "'.");
  60. }
  61. size_t len = f->get_len();
  62. font_mem_cache.resize(len);
  63. f->get_buffer(font_mem_cache.ptrw(), len);
  64. font_mem = font_mem_cache.ptr();
  65. font_mem_size = len;
  66. f->close();
  67. }
  68. }
  69. int error = 0;
  70. fds = memnew(DataAtSize);
  71. if (font_mem) {
  72. memset(&fds->stream, 0, sizeof(FT_StreamRec));
  73. fds->stream.base = (unsigned char *)font_mem;
  74. fds->stream.size = font_mem_size;
  75. fds->stream.pos = 0;
  76. FT_Open_Args fargs;
  77. memset(&fargs, 0, sizeof(FT_Open_Args));
  78. fargs.memory_base = (unsigned char *)font_mem;
  79. fargs.memory_size = font_mem_size;
  80. fargs.flags = FT_OPEN_MEMORY;
  81. fargs.stream = &fds->stream;
  82. error = FT_Open_Face(library, &fargs, 0, &fds->face);
  83. } else {
  84. memdelete(fds);
  85. ERR_FAIL_V_MSG(nullptr, "DynamicFont uninitialized.");
  86. }
  87. if (error == FT_Err_Unknown_File_Format) {
  88. memdelete(fds);
  89. ERR_FAIL_V_MSG(nullptr, "Unknown font format.");
  90. } else if (error) {
  91. memdelete(fds);
  92. ERR_FAIL_V_MSG(nullptr, "Error loading font.");
  93. }
  94. oversampling = TS->font_get_oversampling();
  95. if (FT_HAS_COLOR(fds->face) && fds->face->num_fixed_sizes > 0) {
  96. int best_match = 0;
  97. int diff = ABS(p_size - ((int64_t)fds->face->available_sizes[0].width));
  98. fds->scale_color_font = float(p_size * oversampling) / fds->face->available_sizes[0].width;
  99. for (int i = 1; i < fds->face->num_fixed_sizes; i++) {
  100. int ndiff = ABS(p_size - ((int64_t)fds->face->available_sizes[i].width));
  101. if (ndiff < diff) {
  102. best_match = i;
  103. diff = ndiff;
  104. fds->scale_color_font = float(p_size * oversampling) / fds->face->available_sizes[i].width;
  105. }
  106. }
  107. FT_Select_Size(fds->face, best_match);
  108. } else {
  109. FT_Set_Pixel_Sizes(fds->face, 0, p_size * oversampling);
  110. }
  111. fds->size = p_size;
  112. fds->ascent = (fds->face->size->metrics.ascender / 64.0) / oversampling * fds->scale_color_font;
  113. fds->descent = (-fds->face->size->metrics.descender / 64.0) / oversampling * fds->scale_color_font;
  114. fds->underline_position = (-FT_MulFix(fds->face->underline_position, fds->face->size->metrics.y_scale) / 64.0) / oversampling * fds->scale_color_font;
  115. fds->underline_thickness = (FT_MulFix(fds->face->underline_thickness, fds->face->size->metrics.y_scale) / 64.0) / oversampling * fds->scale_color_font;
  116. //Load os2 TTF table
  117. fds->os2 = (TT_OS2 *)FT_Get_Sfnt_Table(fds->face, FT_SFNT_OS2);
  118. fds->hb_handle = hb_ft_font_create(fds->face, nullptr);
  119. if (fds->hb_handle == nullptr) {
  120. memdelete(fds);
  121. ERR_FAIL_V_MSG(nullptr, "Error loading HB font.");
  122. }
  123. if (p_outline_size != 0) {
  124. size_cache_outline[id] = fds;
  125. } else {
  126. size_cache[id] = fds;
  127. }
  128. // Write variations.
  129. if (fds->face->face_flags & FT_FACE_FLAG_MULTIPLE_MASTERS) {
  130. FT_MM_Var *amaster;
  131. FT_Get_MM_Var(fds->face, &amaster);
  132. Vector<hb_variation_t> hb_vars;
  133. Vector<FT_Fixed> coords;
  134. coords.resize(amaster->num_axis);
  135. FT_Get_Var_Design_Coordinates(fds->face, coords.size(), coords.ptrw());
  136. for (FT_UInt i = 0; i < amaster->num_axis; i++) {
  137. hb_variation_t var;
  138. // Reset to default.
  139. var.tag = amaster->axis[i].tag;
  140. var.value = (double)amaster->axis[i].def / 65536.f;
  141. coords.write[i] = amaster->axis[i].def;
  142. if (variations.has(var.tag)) {
  143. var.value = variations[var.tag];
  144. coords.write[i] = CLAMP(variations[var.tag] * 65536.f, amaster->axis[i].minimum, amaster->axis[i].maximum);
  145. }
  146. hb_vars.push_back(var);
  147. }
  148. FT_Set_Var_Design_Coordinates(fds->face, coords.size(), coords.ptrw());
  149. hb_font_set_variations(fds->hb_handle, hb_vars.is_empty() ? nullptr : &hb_vars[0], hb_vars.size());
  150. FT_Done_MM_Var(library, amaster);
  151. }
  152. }
  153. return fds;
  154. }
  155. Dictionary DynamicFontDataAdvanced::get_variation_list() const {
  156. _THREAD_SAFE_METHOD_
  157. DataAtSize *fds = const_cast<DynamicFontDataAdvanced *>(this)->get_data_for_size(base_size);
  158. if (fds == nullptr) {
  159. return Dictionary();
  160. }
  161. Dictionary ret;
  162. // Read variations.
  163. if (fds->face->face_flags & FT_FACE_FLAG_MULTIPLE_MASTERS) {
  164. FT_MM_Var *amaster;
  165. FT_Get_MM_Var(fds->face, &amaster);
  166. for (FT_UInt i = 0; i < amaster->num_axis; i++) {
  167. ret[(int32_t)amaster->axis[i].tag] = Vector3i(amaster->axis[i].minimum / 65536, amaster->axis[i].maximum / 65536, amaster->axis[i].def / 65536);
  168. }
  169. FT_Done_MM_Var(library, amaster);
  170. }
  171. return ret;
  172. }
  173. void DynamicFontDataAdvanced::set_variation(const String &p_name, double p_value) {
  174. _THREAD_SAFE_METHOD_
  175. int32_t tag = TS->name_to_tag(p_name);
  176. if (!variations.has(tag) || (variations[tag] != p_value)) {
  177. variations[tag] = p_value;
  178. clear_cache();
  179. }
  180. }
  181. double DynamicFontDataAdvanced::get_variation(const String &p_name) const {
  182. _THREAD_SAFE_METHOD_
  183. int32_t tag = TS->name_to_tag(p_name);
  184. if (!variations.has(tag)) {
  185. return 0.f;
  186. }
  187. return variations[tag];
  188. }
  189. Dictionary DynamicFontDataAdvanced::get_feature_list() const {
  190. _THREAD_SAFE_METHOD_
  191. DataAtSize *fds = const_cast<DynamicFontDataAdvanced *>(this)->get_data_for_size(base_size);
  192. if (fds == nullptr) {
  193. return Dictionary();
  194. }
  195. Dictionary out;
  196. // Read feature flags.
  197. unsigned int count = hb_ot_layout_table_get_feature_tags(hb_font_get_face(fds->hb_handle), HB_OT_TAG_GSUB, 0, nullptr, nullptr);
  198. if (count != 0) {
  199. hb_tag_t *feature_tags = (hb_tag_t *)memalloc(count * sizeof(hb_tag_t));
  200. hb_ot_layout_table_get_feature_tags(hb_font_get_face(fds->hb_handle), HB_OT_TAG_GSUB, 0, &count, feature_tags);
  201. for (unsigned int i = 0; i < count; i++) {
  202. out[feature_tags[i]] = 1;
  203. }
  204. memfree(feature_tags);
  205. }
  206. count = hb_ot_layout_table_get_feature_tags(hb_font_get_face(fds->hb_handle), HB_OT_TAG_GPOS, 0, nullptr, nullptr);
  207. if (count != 0) {
  208. hb_tag_t *feature_tags = (hb_tag_t *)memalloc(count * sizeof(hb_tag_t));
  209. hb_ot_layout_table_get_feature_tags(hb_font_get_face(fds->hb_handle), HB_OT_TAG_GPOS, 0, &count, feature_tags);
  210. for (unsigned int i = 0; i < count; i++) {
  211. out[feature_tags[i]] = 1;
  212. }
  213. memfree(feature_tags);
  214. }
  215. return out;
  216. }
  217. DynamicFontDataAdvanced::TexturePosition DynamicFontDataAdvanced::find_texture_pos_for_glyph(DynamicFontDataAdvanced::DataAtSize *p_data, int p_color_size, Image::Format p_image_format, int p_width, int p_height) {
  218. TexturePosition ret;
  219. ret.index = -1;
  220. int mw = p_width;
  221. int mh = p_height;
  222. for (int i = 0; i < p_data->textures.size(); i++) {
  223. const CharTexture &ct = p_data->textures[i];
  224. if (RenderingServer::get_singleton() != nullptr) {
  225. if (ct.texture->get_format() != p_image_format) {
  226. continue;
  227. }
  228. }
  229. if (mw > ct.texture_size || mh > ct.texture_size) { //too big for this texture
  230. continue;
  231. }
  232. ret.y = 0x7FFFFFFF;
  233. ret.x = 0;
  234. for (int j = 0; j < ct.texture_size - mw; j++) {
  235. int max_y = 0;
  236. for (int k = j; k < j + mw; k++) {
  237. int y = ct.offsets[k];
  238. if (y > max_y) {
  239. max_y = y;
  240. }
  241. }
  242. if (max_y < ret.y) {
  243. ret.y = max_y;
  244. ret.x = j;
  245. }
  246. }
  247. if (ret.y == 0x7FFFFFFF || ret.y + mh > ct.texture_size) {
  248. continue; //fail, could not fit it here
  249. }
  250. ret.index = i;
  251. break;
  252. }
  253. if (ret.index == -1) {
  254. //could not find texture to fit, create one
  255. ret.x = 0;
  256. ret.y = 0;
  257. int texsize = MAX(p_data->size * oversampling * 8, 256);
  258. if (mw > texsize) {
  259. texsize = mw; //special case, adapt to it?
  260. }
  261. if (mh > texsize) {
  262. texsize = mh; //special case, adapt to it?
  263. }
  264. texsize = next_power_of_2(texsize);
  265. texsize = MIN(texsize, 4096);
  266. CharTexture tex;
  267. tex.texture_size = texsize;
  268. tex.imgdata.resize(texsize * texsize * p_color_size); //grayscale alpha
  269. {
  270. //zero texture
  271. uint8_t *w = tex.imgdata.ptrw();
  272. ERR_FAIL_COND_V(texsize * texsize * p_color_size > tex.imgdata.size(), ret);
  273. // Initialize the texture to all-white pixels to prevent artifacts when the
  274. // font is displayed at a non-default scale with filtering enabled.
  275. if (p_color_size == 2) {
  276. for (int i = 0; i < texsize * texsize * p_color_size; i += 2) { // FORMAT_LA8
  277. w[i + 0] = 255;
  278. w[i + 1] = 0;
  279. }
  280. } else if (p_color_size == 4) {
  281. for (int i = 0; i < texsize * texsize * p_color_size; i += 4) { // FORMAT_RGBA8
  282. w[i + 0] = 255;
  283. w[i + 1] = 255;
  284. w[i + 2] = 255;
  285. w[i + 3] = 0;
  286. }
  287. } else {
  288. ERR_FAIL_V(ret);
  289. }
  290. }
  291. tex.offsets.resize(texsize);
  292. for (int i = 0; i < texsize; i++) { //zero offsets
  293. tex.offsets.write[i] = 0;
  294. }
  295. p_data->textures.push_back(tex);
  296. ret.index = p_data->textures.size() - 1;
  297. }
  298. return ret;
  299. }
  300. DynamicFontDataAdvanced::Character DynamicFontDataAdvanced::Character::not_found() {
  301. Character ch;
  302. return ch;
  303. }
  304. DynamicFontDataAdvanced::Character DynamicFontDataAdvanced::bitmap_to_character(DynamicFontDataAdvanced::DataAtSize *p_data, FT_Bitmap bitmap, int yofs, int xofs, const Vector2 &advance) {
  305. int w = bitmap.width;
  306. int h = bitmap.rows;
  307. int mw = w + rect_margin * 2;
  308. int mh = h + rect_margin * 2;
  309. ERR_FAIL_COND_V(mw > 4096, Character::not_found());
  310. ERR_FAIL_COND_V(mh > 4096, Character::not_found());
  311. int color_size = bitmap.pixel_mode == FT_PIXEL_MODE_BGRA ? 4 : 2;
  312. Image::Format require_format = color_size == 4 ? Image::FORMAT_RGBA8 : Image::FORMAT_LA8;
  313. TexturePosition tex_pos = find_texture_pos_for_glyph(p_data, color_size, require_format, mw, mh);
  314. ERR_FAIL_COND_V(tex_pos.index < 0, Character::not_found());
  315. //fit character in char texture
  316. CharTexture &tex = p_data->textures.write[tex_pos.index];
  317. {
  318. uint8_t *wr = tex.imgdata.ptrw();
  319. for (int i = 0; i < h; i++) {
  320. for (int j = 0; j < w; j++) {
  321. int ofs = ((i + tex_pos.y + rect_margin) * tex.texture_size + j + tex_pos.x + rect_margin) * color_size;
  322. ERR_FAIL_COND_V(ofs >= tex.imgdata.size(), Character::not_found());
  323. switch (bitmap.pixel_mode) {
  324. case FT_PIXEL_MODE_MONO: {
  325. int byte = i * bitmap.pitch + (j >> 3);
  326. int bit = 1 << (7 - (j % 8));
  327. wr[ofs + 0] = 255; //grayscale as 1
  328. wr[ofs + 1] = (bitmap.buffer[byte] & bit) ? 255 : 0;
  329. } break;
  330. case FT_PIXEL_MODE_GRAY:
  331. wr[ofs + 0] = 255; //grayscale as 1
  332. wr[ofs + 1] = bitmap.buffer[i * bitmap.pitch + j];
  333. break;
  334. case FT_PIXEL_MODE_BGRA: {
  335. int ofs_color = i * bitmap.pitch + (j << 2);
  336. wr[ofs + 2] = bitmap.buffer[ofs_color + 0];
  337. wr[ofs + 1] = bitmap.buffer[ofs_color + 1];
  338. wr[ofs + 0] = bitmap.buffer[ofs_color + 2];
  339. wr[ofs + 3] = bitmap.buffer[ofs_color + 3];
  340. } break;
  341. // TODO: FT_PIXEL_MODE_LCD
  342. default:
  343. ERR_FAIL_V_MSG(Character::not_found(), "Font uses unsupported pixel format: " + itos(bitmap.pixel_mode) + ".");
  344. break;
  345. }
  346. }
  347. }
  348. }
  349. //blit to image and texture
  350. {
  351. if (RenderingServer::get_singleton() != nullptr) {
  352. Ref<Image> img = memnew(Image(tex.texture_size, tex.texture_size, 0, require_format, tex.imgdata));
  353. if (tex.texture.is_null()) {
  354. tex.texture.instance();
  355. tex.texture->create_from_image(img);
  356. } else {
  357. tex.texture->update(img); //update
  358. }
  359. }
  360. }
  361. // update height array
  362. for (int k = tex_pos.x; k < tex_pos.x + mw; k++) {
  363. tex.offsets.write[k] = tex_pos.y + mh;
  364. }
  365. Character chr;
  366. chr.align = (Vector2(xofs, -yofs) * p_data->scale_color_font / oversampling).round();
  367. chr.advance = (advance * p_data->scale_color_font / oversampling).round();
  368. chr.texture_idx = tex_pos.index;
  369. chr.found = true;
  370. chr.rect_uv = Rect2(tex_pos.x + rect_margin, tex_pos.y + rect_margin, w, h);
  371. chr.rect = chr.rect_uv;
  372. chr.rect.position /= oversampling;
  373. chr.rect.size *= (p_data->scale_color_font / oversampling);
  374. return chr;
  375. }
  376. void DynamicFontDataAdvanced::update_glyph(int p_size, uint32_t p_index) {
  377. DataAtSize *fds = get_data_for_size(p_size, false);
  378. ERR_FAIL_COND(fds == nullptr);
  379. if (fds->glyph_map.has(p_index)) {
  380. return;
  381. }
  382. Character character = Character::not_found();
  383. FT_GlyphSlot slot = fds->face->glyph;
  384. if (p_index == 0) {
  385. fds->glyph_map[p_index] = character;
  386. return;
  387. }
  388. int ft_hinting;
  389. switch (hinting) {
  390. case TextServer::HINTING_NONE:
  391. ft_hinting = FT_LOAD_NO_HINTING;
  392. break;
  393. case TextServer::HINTING_LIGHT:
  394. ft_hinting = FT_LOAD_TARGET_LIGHT;
  395. break;
  396. default:
  397. ft_hinting = FT_LOAD_TARGET_NORMAL;
  398. break;
  399. }
  400. FT_Fixed v, h;
  401. FT_Get_Advance(fds->face, p_index, FT_HAS_COLOR(fds->face) ? FT_LOAD_COLOR : FT_LOAD_DEFAULT | (force_autohinter ? FT_LOAD_FORCE_AUTOHINT : 0) | ft_hinting, &h);
  402. FT_Get_Advance(fds->face, p_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);
  403. int error = FT_Load_Glyph(fds->face, p_index, FT_HAS_COLOR(fds->face) ? FT_LOAD_COLOR : FT_LOAD_DEFAULT | (force_autohinter ? FT_LOAD_FORCE_AUTOHINT : 0) | ft_hinting);
  404. if (error) {
  405. fds->glyph_map[p_index] = character;
  406. return;
  407. }
  408. error = FT_Render_Glyph(fds->face->glyph, antialiased ? FT_RENDER_MODE_NORMAL : FT_RENDER_MODE_MONO);
  409. if (!error) {
  410. character = bitmap_to_character(fds, slot->bitmap, slot->bitmap_top, slot->bitmap_left, Vector2((h + (1 << 9)) >> 10, (v + (1 << 9)) >> 10) / 64.0);
  411. }
  412. fds->glyph_map[p_index] = character;
  413. }
  414. void DynamicFontDataAdvanced::update_glyph_outline(int p_size, int p_outline_size, uint32_t p_index) {
  415. DataAtSize *fds = get_data_for_size(p_size, p_outline_size);
  416. ERR_FAIL_COND(fds == nullptr);
  417. if (fds->glyph_map.has(p_index)) {
  418. return;
  419. }
  420. Character character = Character::not_found();
  421. if (p_index == 0) {
  422. fds->glyph_map[p_index] = character;
  423. return;
  424. }
  425. int error = FT_Load_Glyph(fds->face, p_index, FT_LOAD_NO_BITMAP | (force_autohinter ? FT_LOAD_FORCE_AUTOHINT : 0));
  426. if (error) {
  427. fds->glyph_map[p_index] = character;
  428. return;
  429. }
  430. FT_Stroker stroker;
  431. if (FT_Stroker_New(library, &stroker) != 0) {
  432. fds->glyph_map[p_index] = character;
  433. return;
  434. }
  435. FT_Stroker_Set(stroker, (int)(p_outline_size * oversampling * 64.0), FT_STROKER_LINECAP_BUTT, FT_STROKER_LINEJOIN_ROUND, 0);
  436. FT_Glyph glyph;
  437. FT_BitmapGlyph glyph_bitmap;
  438. if (FT_Get_Glyph(fds->face->glyph, &glyph) != 0) {
  439. goto cleanup_stroker;
  440. }
  441. if (FT_Glyph_Stroke(&glyph, stroker, 1) != 0) {
  442. goto cleanup_glyph;
  443. }
  444. if (FT_Glyph_To_Bitmap(&glyph, antialiased ? FT_RENDER_MODE_NORMAL : FT_RENDER_MODE_MONO, nullptr, 1) != 0) {
  445. goto cleanup_glyph;
  446. }
  447. glyph_bitmap = (FT_BitmapGlyph)glyph;
  448. character = bitmap_to_character(fds, glyph_bitmap->bitmap, glyph_bitmap->top, glyph_bitmap->left, Vector2());
  449. cleanup_glyph:
  450. FT_Done_Glyph(glyph);
  451. cleanup_stroker:
  452. FT_Stroker_Done(stroker);
  453. fds->glyph_map[p_index] = character;
  454. }
  455. void DynamicFontDataAdvanced::clear_cache() {
  456. _THREAD_SAFE_METHOD_
  457. for (Map<CacheID, DataAtSize *>::Element *E = size_cache.front(); E; E = E->next()) {
  458. memdelete(E->get());
  459. }
  460. size_cache.clear();
  461. for (Map<CacheID, DataAtSize *>::Element *E = size_cache_outline.front(); E; E = E->next()) {
  462. memdelete(E->get());
  463. }
  464. size_cache_outline.clear();
  465. }
  466. Error DynamicFontDataAdvanced::load_from_file(const String &p_filename, int p_base_size) {
  467. _THREAD_SAFE_METHOD_
  468. if (library == nullptr) {
  469. int error = FT_Init_FreeType(&library);
  470. ERR_FAIL_COND_V_MSG(error != 0, ERR_CANT_CREATE, "Error initializing FreeType.");
  471. }
  472. clear_cache();
  473. font_path = p_filename;
  474. base_size = p_base_size;
  475. valid = true;
  476. DataAtSize *fds = get_data_for_size(base_size); // load base size.
  477. if (fds == nullptr) {
  478. valid = false;
  479. ERR_FAIL_V(ERR_CANT_CREATE);
  480. }
  481. return OK;
  482. }
  483. Error DynamicFontDataAdvanced::load_from_memory(const uint8_t *p_data, size_t p_size, int p_base_size) {
  484. _THREAD_SAFE_METHOD_
  485. if (library == nullptr) {
  486. int error = FT_Init_FreeType(&library);
  487. ERR_FAIL_COND_V_MSG(error != 0, ERR_CANT_CREATE, "Error initializing FreeType.");
  488. }
  489. clear_cache();
  490. font_mem = p_data;
  491. font_mem_size = p_size;
  492. base_size = p_base_size;
  493. valid = true;
  494. DataAtSize *fds = get_data_for_size(base_size); // load base size.
  495. if (fds == nullptr) {
  496. valid = false;
  497. ERR_FAIL_V(ERR_CANT_CREATE);
  498. }
  499. return OK;
  500. }
  501. float DynamicFontDataAdvanced::get_height(int p_size) const {
  502. _THREAD_SAFE_METHOD_
  503. DataAtSize *fds = const_cast<DynamicFontDataAdvanced *>(this)->get_data_for_size(p_size);
  504. ERR_FAIL_COND_V(fds == nullptr, 0.f);
  505. return fds->ascent + fds->descent;
  506. }
  507. float DynamicFontDataAdvanced::get_ascent(int p_size) const {
  508. _THREAD_SAFE_METHOD_
  509. DataAtSize *fds = const_cast<DynamicFontDataAdvanced *>(this)->get_data_for_size(p_size);
  510. ERR_FAIL_COND_V(fds == nullptr, 0.f);
  511. return fds->ascent;
  512. }
  513. float DynamicFontDataAdvanced::get_descent(int p_size) const {
  514. _THREAD_SAFE_METHOD_
  515. DataAtSize *fds = const_cast<DynamicFontDataAdvanced *>(this)->get_data_for_size(p_size);
  516. ERR_FAIL_COND_V(fds == nullptr, 0.f);
  517. return fds->descent;
  518. }
  519. float DynamicFontDataAdvanced::get_underline_position(int p_size) const {
  520. _THREAD_SAFE_METHOD_
  521. DataAtSize *fds = const_cast<DynamicFontDataAdvanced *>(this)->get_data_for_size(p_size);
  522. ERR_FAIL_COND_V(fds == nullptr, 0.f);
  523. return fds->underline_position;
  524. }
  525. float DynamicFontDataAdvanced::get_underline_thickness(int p_size) const {
  526. _THREAD_SAFE_METHOD_
  527. DataAtSize *fds = const_cast<DynamicFontDataAdvanced *>(this)->get_data_for_size(p_size);
  528. ERR_FAIL_COND_V(fds == nullptr, 0.f);
  529. return fds->underline_thickness;
  530. }
  531. bool DynamicFontDataAdvanced::is_script_supported(uint32_t p_script) const {
  532. _THREAD_SAFE_METHOD_
  533. DataAtSize *fds = const_cast<DynamicFontDataAdvanced *>(this)->get_data_for_size(base_size);
  534. ERR_FAIL_COND_V(fds == nullptr, false);
  535. unsigned int count = hb_ot_layout_table_get_script_tags(hb_font_get_face(fds->hb_handle), HB_OT_TAG_GSUB, 0, nullptr, nullptr);
  536. if (count != 0) {
  537. hb_tag_t *script_tags = (hb_tag_t *)memalloc(count * sizeof(hb_tag_t));
  538. hb_ot_layout_table_get_script_tags(hb_font_get_face(fds->hb_handle), HB_OT_TAG_GSUB, 0, &count, script_tags);
  539. for (unsigned int i = 0; i < count; i++) {
  540. if (p_script == script_tags[i]) {
  541. memfree(script_tags);
  542. return true;
  543. }
  544. }
  545. memfree(script_tags);
  546. }
  547. count = hb_ot_layout_table_get_script_tags(hb_font_get_face(fds->hb_handle), HB_OT_TAG_GPOS, 0, nullptr, nullptr);
  548. if (count != 0) {
  549. hb_tag_t *script_tags = (hb_tag_t *)memalloc(count * sizeof(hb_tag_t));
  550. hb_ot_layout_table_get_script_tags(hb_font_get_face(fds->hb_handle), HB_OT_TAG_GPOS, 0, &count, script_tags);
  551. for (unsigned int i = 0; i < count; i++) {
  552. if (p_script == script_tags[i]) {
  553. memfree(script_tags);
  554. return true;
  555. }
  556. }
  557. memfree(script_tags);
  558. }
  559. if (!fds->os2) {
  560. return false;
  561. }
  562. switch (p_script) {
  563. case HB_SCRIPT_COMMON:
  564. return (fds->os2->ulUnicodeRange1 & 1L << 4) || (fds->os2->ulUnicodeRange1 & 1L << 5) || (fds->os2->ulUnicodeRange1 & 1L << 6) || (fds->os2->ulUnicodeRange1 & 1L << 31) || (fds->os2->ulUnicodeRange2 & 1L << 0) || (fds->os2->ulUnicodeRange2 & 1L << 1) || (fds->os2->ulUnicodeRange2 & 1L << 2) || (fds->os2->ulUnicodeRange2 & 1L << 3) || (fds->os2->ulUnicodeRange2 & 1L << 4) || (fds->os2->ulUnicodeRange2 & 1L << 5) || (fds->os2->ulUnicodeRange2 & 1L << 6) || (fds->os2->ulUnicodeRange2 & 1L << 7) || (fds->os2->ulUnicodeRange2 & 1L << 8) || (fds->os2->ulUnicodeRange2 & 1L << 9) || (fds->os2->ulUnicodeRange2 & 1L << 10) || (fds->os2->ulUnicodeRange2 & 1L << 11) || (fds->os2->ulUnicodeRange2 & 1L << 12) || (fds->os2->ulUnicodeRange2 & 1L << 13) || (fds->os2->ulUnicodeRange2 & 1L << 14) || (fds->os2->ulUnicodeRange2 & 1L << 15) || (fds->os2->ulUnicodeRange2 & 1L << 30) || (fds->os2->ulUnicodeRange3 & 1L << 0) || (fds->os2->ulUnicodeRange3 & 1L << 1) || (fds->os2->ulUnicodeRange3 & 1L << 2) || (fds->os2->ulUnicodeRange3 & 1L << 4) || (fds->os2->ulUnicodeRange3 & 1L << 5) || (fds->os2->ulUnicodeRange3 & 1L << 18) || (fds->os2->ulUnicodeRange3 & 1L << 24) || (fds->os2->ulUnicodeRange3 & 1L << 25) || (fds->os2->ulUnicodeRange3 & 1L << 26) || (fds->os2->ulUnicodeRange3 & 1L << 27) || (fds->os2->ulUnicodeRange3 & 1L << 28) || (fds->os2->ulUnicodeRange4 & 1L << 3) || (fds->os2->ulUnicodeRange4 & 1L << 6) || (fds->os2->ulUnicodeRange4 & 1L << 15) || (fds->os2->ulUnicodeRange4 & 1L << 23) || (fds->os2->ulUnicodeRange4 & 1L << 24) || (fds->os2->ulUnicodeRange4 & 1L << 26);
  565. case HB_SCRIPT_LATIN:
  566. return (fds->os2->ulUnicodeRange1 & 1L << 0) || (fds->os2->ulUnicodeRange1 & 1L << 1) || (fds->os2->ulUnicodeRange1 & 1L << 2) || (fds->os2->ulUnicodeRange1 & 1L << 3) || (fds->os2->ulUnicodeRange1 & 1L << 29);
  567. case HB_SCRIPT_GREEK:
  568. return (fds->os2->ulUnicodeRange1 & 1L << 7) || (fds->os2->ulUnicodeRange1 & 1L << 30);
  569. case HB_SCRIPT_COPTIC:
  570. return (fds->os2->ulUnicodeRange1 & 1L << 8);
  571. case HB_SCRIPT_CYRILLIC:
  572. return (fds->os2->ulUnicodeRange1 & 1L << 9);
  573. case HB_SCRIPT_ARMENIAN:
  574. return (fds->os2->ulUnicodeRange1 & 1L << 10);
  575. case HB_SCRIPT_HEBREW:
  576. return (fds->os2->ulUnicodeRange1 & 1L << 11);
  577. case HB_SCRIPT_VAI:
  578. return (fds->os2->ulUnicodeRange1 & 1L << 12);
  579. case HB_SCRIPT_ARABIC:
  580. return (fds->os2->ulUnicodeRange1 & 1L << 13) || (fds->os2->ulUnicodeRange2 & 1L << 31) || (fds->os2->ulUnicodeRange3 & 1L << 3);
  581. case HB_SCRIPT_NKO:
  582. return (fds->os2->ulUnicodeRange1 & 1L << 14);
  583. case HB_SCRIPT_DEVANAGARI:
  584. return (fds->os2->ulUnicodeRange1 & 1L << 15);
  585. case HB_SCRIPT_BENGALI:
  586. return (fds->os2->ulUnicodeRange1 & 1L << 16);
  587. case HB_SCRIPT_GURMUKHI:
  588. return (fds->os2->ulUnicodeRange1 & 1L << 17);
  589. case HB_SCRIPT_GUJARATI:
  590. return (fds->os2->ulUnicodeRange1 & 1L << 18);
  591. case HB_SCRIPT_ORIYA:
  592. return (fds->os2->ulUnicodeRange1 & 1L << 19);
  593. case HB_SCRIPT_TAMIL:
  594. return (fds->os2->ulUnicodeRange1 & 1L << 20);
  595. case HB_SCRIPT_TELUGU:
  596. return (fds->os2->ulUnicodeRange1 & 1L << 21);
  597. case HB_SCRIPT_KANNADA:
  598. return (fds->os2->ulUnicodeRange1 & 1L << 22);
  599. case HB_SCRIPT_MALAYALAM:
  600. return (fds->os2->ulUnicodeRange1 & 1L << 23);
  601. case HB_SCRIPT_THAI:
  602. return (fds->os2->ulUnicodeRange1 & 1L << 24);
  603. case HB_SCRIPT_LAO:
  604. return (fds->os2->ulUnicodeRange1 & 1L << 25);
  605. case HB_SCRIPT_GEORGIAN:
  606. return (fds->os2->ulUnicodeRange1 & 1L << 26);
  607. case HB_SCRIPT_BALINESE:
  608. return (fds->os2->ulUnicodeRange1 & 1L << 27);
  609. case HB_SCRIPT_HANGUL:
  610. return (fds->os2->ulUnicodeRange1 & 1L << 28) || (fds->os2->ulUnicodeRange2 & 1L << 20) || (fds->os2->ulUnicodeRange2 & 1L << 24);
  611. case HB_SCRIPT_HAN:
  612. return (fds->os2->ulUnicodeRange2 & 1L << 21) || (fds->os2->ulUnicodeRange2 & 1L << 22) || (fds->os2->ulUnicodeRange2 & 1L << 23) || (fds->os2->ulUnicodeRange2 & 1L << 26) || (fds->os2->ulUnicodeRange2 & 1L << 27) || (fds->os2->ulUnicodeRange2 & 1L << 29);
  613. case HB_SCRIPT_HIRAGANA:
  614. return (fds->os2->ulUnicodeRange2 & 1L << 17);
  615. case HB_SCRIPT_KATAKANA:
  616. return (fds->os2->ulUnicodeRange2 & 1L << 18);
  617. case HB_SCRIPT_BOPOMOFO:
  618. return (fds->os2->ulUnicodeRange2 & 1L << 19);
  619. case HB_SCRIPT_TIBETAN:
  620. return (fds->os2->ulUnicodeRange3 & 1L << 6);
  621. case HB_SCRIPT_SYRIAC:
  622. return (fds->os2->ulUnicodeRange3 & 1L << 7);
  623. case HB_SCRIPT_THAANA:
  624. return (fds->os2->ulUnicodeRange3 & 1L << 8);
  625. case HB_SCRIPT_SINHALA:
  626. return (fds->os2->ulUnicodeRange3 & 1L << 9);
  627. case HB_SCRIPT_MYANMAR:
  628. return (fds->os2->ulUnicodeRange3 & 1L << 10);
  629. case HB_SCRIPT_ETHIOPIC:
  630. return (fds->os2->ulUnicodeRange3 & 1L << 11);
  631. case HB_SCRIPT_CHEROKEE:
  632. return (fds->os2->ulUnicodeRange3 & 1L << 12);
  633. case HB_SCRIPT_CANADIAN_SYLLABICS:
  634. return (fds->os2->ulUnicodeRange3 & 1L << 13);
  635. case HB_SCRIPT_OGHAM:
  636. return (fds->os2->ulUnicodeRange3 & 1L << 14);
  637. case HB_SCRIPT_RUNIC:
  638. return (fds->os2->ulUnicodeRange3 & 1L << 15);
  639. case HB_SCRIPT_KHMER:
  640. return (fds->os2->ulUnicodeRange3 & 1L << 16);
  641. case HB_SCRIPT_MONGOLIAN:
  642. return (fds->os2->ulUnicodeRange3 & 1L << 17);
  643. case HB_SCRIPT_YI:
  644. return (fds->os2->ulUnicodeRange3 & 1L << 19);
  645. case HB_SCRIPT_HANUNOO:
  646. case HB_SCRIPT_TAGBANWA:
  647. case HB_SCRIPT_BUHID:
  648. case HB_SCRIPT_TAGALOG:
  649. return (fds->os2->ulUnicodeRange3 & 1L << 20);
  650. case HB_SCRIPT_OLD_ITALIC:
  651. return (fds->os2->ulUnicodeRange3 & 1L << 21);
  652. case HB_SCRIPT_GOTHIC:
  653. return (fds->os2->ulUnicodeRange3 & 1L << 22);
  654. case HB_SCRIPT_DESERET:
  655. return (fds->os2->ulUnicodeRange3 & 1L << 23);
  656. case HB_SCRIPT_LIMBU:
  657. return (fds->os2->ulUnicodeRange3 & 1L << 29);
  658. case HB_SCRIPT_TAI_LE:
  659. return (fds->os2->ulUnicodeRange3 & 1L << 30);
  660. case HB_SCRIPT_NEW_TAI_LUE:
  661. return (fds->os2->ulUnicodeRange3 & 1L << 31);
  662. case HB_SCRIPT_BUGINESE:
  663. return (fds->os2->ulUnicodeRange4 & 1L << 0);
  664. case HB_SCRIPT_GLAGOLITIC:
  665. return (fds->os2->ulUnicodeRange4 & 1L << 1);
  666. case HB_SCRIPT_TIFINAGH:
  667. return (fds->os2->ulUnicodeRange4 & 1L << 2);
  668. case HB_SCRIPT_SYLOTI_NAGRI:
  669. return (fds->os2->ulUnicodeRange4 & 1L << 4);
  670. case HB_SCRIPT_LINEAR_B:
  671. return (fds->os2->ulUnicodeRange4 & 1L << 5);
  672. case HB_SCRIPT_UGARITIC:
  673. return (fds->os2->ulUnicodeRange4 & 1L << 7);
  674. case HB_SCRIPT_OLD_PERSIAN:
  675. return (fds->os2->ulUnicodeRange4 & 1L << 8);
  676. case HB_SCRIPT_SHAVIAN:
  677. return (fds->os2->ulUnicodeRange4 & 1L << 9);
  678. case HB_SCRIPT_OSMANYA:
  679. return (fds->os2->ulUnicodeRange4 & 1L << 10);
  680. case HB_SCRIPT_CYPRIOT:
  681. return (fds->os2->ulUnicodeRange4 & 1L << 11);
  682. case HB_SCRIPT_KHAROSHTHI:
  683. return (fds->os2->ulUnicodeRange4 & 1L << 12);
  684. case HB_SCRIPT_TAI_VIET:
  685. return (fds->os2->ulUnicodeRange4 & 1L << 13);
  686. case HB_SCRIPT_CUNEIFORM:
  687. return (fds->os2->ulUnicodeRange4 & 1L << 14);
  688. case HB_SCRIPT_SUNDANESE:
  689. return (fds->os2->ulUnicodeRange4 & 1L << 16);
  690. case HB_SCRIPT_LEPCHA:
  691. return (fds->os2->ulUnicodeRange4 & 1L << 17);
  692. case HB_SCRIPT_OL_CHIKI:
  693. return (fds->os2->ulUnicodeRange4 & 1L << 18);
  694. case HB_SCRIPT_SAURASHTRA:
  695. return (fds->os2->ulUnicodeRange4 & 1L << 19);
  696. case HB_SCRIPT_KAYAH_LI:
  697. return (fds->os2->ulUnicodeRange4 & 1L << 20);
  698. case HB_SCRIPT_REJANG:
  699. return (fds->os2->ulUnicodeRange4 & 1L << 21);
  700. case HB_SCRIPT_CHAM:
  701. return (fds->os2->ulUnicodeRange4 & 1L << 22);
  702. case HB_SCRIPT_ANATOLIAN_HIEROGLYPHS:
  703. return (fds->os2->ulUnicodeRange4 & 1L << 25);
  704. default:
  705. return false;
  706. };
  707. }
  708. void DynamicFontDataAdvanced::set_antialiased(bool p_antialiased) {
  709. if (antialiased != p_antialiased) {
  710. clear_cache();
  711. antialiased = p_antialiased;
  712. }
  713. }
  714. bool DynamicFontDataAdvanced::get_antialiased() const {
  715. return antialiased;
  716. }
  717. void DynamicFontDataAdvanced::set_force_autohinter(bool p_enabled) {
  718. if (force_autohinter != p_enabled) {
  719. clear_cache();
  720. force_autohinter = p_enabled;
  721. }
  722. }
  723. bool DynamicFontDataAdvanced::get_force_autohinter() const {
  724. return force_autohinter;
  725. }
  726. void DynamicFontDataAdvanced::set_hinting(TextServer::Hinting p_hinting) {
  727. if (hinting != p_hinting) {
  728. clear_cache();
  729. hinting = p_hinting;
  730. }
  731. }
  732. TextServer::Hinting DynamicFontDataAdvanced::get_hinting() const {
  733. return hinting;
  734. }
  735. bool DynamicFontDataAdvanced::has_outline() const {
  736. return true;
  737. }
  738. float DynamicFontDataAdvanced::get_base_size() const {
  739. return base_size;
  740. }
  741. String DynamicFontDataAdvanced::get_supported_chars() const {
  742. _THREAD_SAFE_METHOD_
  743. DataAtSize *fds = const_cast<DynamicFontDataAdvanced *>(this)->get_data_for_size(base_size);
  744. ERR_FAIL_COND_V(fds == nullptr, String());
  745. String chars;
  746. FT_UInt gindex;
  747. FT_ULong charcode = FT_Get_First_Char(fds->face, &gindex);
  748. while (gindex != 0) {
  749. if (charcode != 0) {
  750. chars += char32_t(charcode);
  751. }
  752. charcode = FT_Get_Next_Char(fds->face, charcode, &gindex);
  753. }
  754. return chars;
  755. }
  756. float DynamicFontDataAdvanced::get_font_scale(int p_size) const {
  757. _THREAD_SAFE_METHOD_
  758. DataAtSize *fds = const_cast<DynamicFontDataAdvanced *>(this)->get_data_for_size(p_size);
  759. ERR_FAIL_COND_V(fds == nullptr, 1.0f);
  760. return fds->scale_color_font / oversampling;
  761. }
  762. bool DynamicFontDataAdvanced::has_char(char32_t p_char) const {
  763. _THREAD_SAFE_METHOD_
  764. DataAtSize *fds = const_cast<DynamicFontDataAdvanced *>(this)->get_data_for_size(base_size);
  765. ERR_FAIL_COND_V(fds == nullptr, false);
  766. const_cast<DynamicFontDataAdvanced *>(this)->update_glyph(base_size, FT_Get_Char_Index(fds->face, p_char));
  767. Character ch = fds->glyph_map[FT_Get_Char_Index(fds->face, p_char)];
  768. return (ch.found);
  769. }
  770. hb_font_t *DynamicFontDataAdvanced::get_hb_handle(int p_size) {
  771. _THREAD_SAFE_METHOD_
  772. DataAtSize *fds = get_data_for_size(p_size);
  773. ERR_FAIL_COND_V(fds == nullptr, nullptr);
  774. return fds->hb_handle;
  775. }
  776. uint32_t DynamicFontDataAdvanced::get_glyph_index(char32_t p_char, char32_t p_variation_selector) const {
  777. _THREAD_SAFE_METHOD_
  778. DataAtSize *fds = const_cast<DynamicFontDataAdvanced *>(this)->get_data_for_size(base_size);
  779. ERR_FAIL_COND_V(fds == nullptr, 0);
  780. if (p_variation_selector == 0x0000) {
  781. return FT_Get_Char_Index(fds->face, p_char);
  782. } else {
  783. return FT_Face_GetCharVariantIndex(fds->face, p_char, p_variation_selector);
  784. }
  785. }
  786. Vector2 DynamicFontDataAdvanced::get_advance(uint32_t p_index, int p_size) const {
  787. _THREAD_SAFE_METHOD_
  788. DataAtSize *fds = const_cast<DynamicFontDataAdvanced *>(this)->get_data_for_size(p_size);
  789. ERR_FAIL_COND_V(fds == nullptr, Vector2());
  790. const_cast<DynamicFontDataAdvanced *>(this)->update_glyph(p_size, p_index);
  791. Character ch = fds->glyph_map[p_index];
  792. return ch.advance;
  793. }
  794. Vector2 DynamicFontDataAdvanced::get_kerning(uint32_t p_char, uint32_t p_next, int p_size) const {
  795. _THREAD_SAFE_METHOD_
  796. DataAtSize *fds = const_cast<DynamicFontDataAdvanced *>(this)->get_data_for_size(p_size);
  797. ERR_FAIL_COND_V(fds == nullptr, Vector2());
  798. FT_Vector delta;
  799. FT_Get_Kerning(fds->face, p_char, p_next, FT_KERNING_DEFAULT, &delta);
  800. return Vector2(delta.x, delta.y);
  801. }
  802. Vector2 DynamicFontDataAdvanced::draw_glyph(RID p_canvas, int p_size, const Vector2 &p_pos, uint32_t p_index, const Color &p_color) const {
  803. _THREAD_SAFE_METHOD_
  804. DataAtSize *fds = const_cast<DynamicFontDataAdvanced *>(this)->get_data_for_size(p_size);
  805. ERR_FAIL_COND_V(fds == nullptr, Vector2());
  806. const_cast<DynamicFontDataAdvanced *>(this)->update_glyph(p_size, p_index);
  807. Character ch = fds->glyph_map[p_index];
  808. Vector2 advance;
  809. if (ch.found) {
  810. ERR_FAIL_COND_V(ch.texture_idx < -1 || ch.texture_idx >= fds->textures.size(), Vector2());
  811. if (ch.texture_idx != -1) {
  812. Point2i cpos = p_pos;
  813. cpos += ch.align;
  814. Color modulate = p_color;
  815. if (FT_HAS_COLOR(fds->face)) {
  816. modulate.r = modulate.g = modulate.b = 1.0;
  817. }
  818. if (RenderingServer::get_singleton() != nullptr) {
  819. RID texture = fds->textures[ch.texture_idx].texture->get_rid();
  820. RenderingServer::get_singleton()->canvas_item_add_texture_rect_region(p_canvas, Rect2(cpos, ch.rect.size), texture, ch.rect_uv, modulate, false, false);
  821. }
  822. }
  823. advance = ch.advance;
  824. }
  825. return advance;
  826. }
  827. Vector2 DynamicFontDataAdvanced::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 {
  828. _THREAD_SAFE_METHOD_
  829. DataAtSize *fds = const_cast<DynamicFontDataAdvanced *>(this)->get_data_for_size(p_size, p_outline_size);
  830. ERR_FAIL_COND_V(fds == nullptr, Vector2());
  831. const_cast<DynamicFontDataAdvanced *>(this)->update_glyph_outline(p_size, p_outline_size, p_index);
  832. Character ch = fds->glyph_map[p_index];
  833. Vector2 advance;
  834. if (ch.found) {
  835. ERR_FAIL_COND_V(ch.texture_idx < -1 || ch.texture_idx >= fds->textures.size(), Vector2());
  836. if (ch.texture_idx != -1) {
  837. Point2i cpos = p_pos;
  838. cpos += ch.align;
  839. Color modulate = p_color;
  840. if (FT_HAS_COLOR(fds->face)) {
  841. modulate.r = modulate.g = modulate.b = 1.0;
  842. }
  843. if (RenderingServer::get_singleton() != nullptr) {
  844. RID texture = fds->textures[ch.texture_idx].texture->get_rid();
  845. RenderingServer::get_singleton()->canvas_item_add_texture_rect_region(p_canvas, Rect2(cpos, ch.rect.size), texture, ch.rect_uv, modulate, false, false);
  846. }
  847. }
  848. advance = ch.advance;
  849. }
  850. return advance;
  851. }
  852. bool DynamicFontDataAdvanced::get_glyph_contours(int p_size, uint32_t p_index, Vector<Vector3> &r_points, Vector<int32_t> &r_contours, bool &r_orientation) const {
  853. _THREAD_SAFE_METHOD_
  854. DataAtSize *fds = const_cast<DynamicFontDataAdvanced *>(this)->get_data_for_size(p_size);
  855. ERR_FAIL_COND_V(fds == nullptr, false);
  856. int error = FT_Load_Glyph(fds->face, p_index, FT_LOAD_NO_BITMAP | (force_autohinter ? FT_LOAD_FORCE_AUTOHINT : 0));
  857. ERR_FAIL_COND_V(error, false);
  858. r_points.clear();
  859. r_contours.clear();
  860. float h = fds->ascent;
  861. float scale = (1.0 / 64.0) / oversampling * fds->scale_color_font;
  862. for (short i = 0; i < fds->face->glyph->outline.n_points; i++) {
  863. r_points.push_back(Vector3(fds->face->glyph->outline.points[i].x * scale, h - fds->face->glyph->outline.points[i].y * scale, FT_CURVE_TAG(fds->face->glyph->outline.tags[i])));
  864. }
  865. for (short i = 0; i < fds->face->glyph->outline.n_contours; i++) {
  866. r_contours.push_back(fds->face->glyph->outline.contours[i]);
  867. }
  868. r_orientation = (FT_Outline_Get_Orientation(&fds->face->glyph->outline) == FT_ORIENTATION_FILL_RIGHT);
  869. return true;
  870. }
  871. DynamicFontDataAdvanced::~DynamicFontDataAdvanced() {
  872. clear_cache();
  873. if (library != nullptr) {
  874. FT_Done_FreeType(library);
  875. }
  876. }
  877. #endif // MODULE_FREETYPE_ENABLED