dynamic_font_adv.cpp 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007
  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 pable
  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, NULL, NULL);
  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, NULL, NULL);
  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, NULL, NULL);
  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, NULL, NULL);
  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. DynamicFontDataAdvanced::~DynamicFontDataAdvanced() {
  853. clear_cache();
  854. if (library != nullptr) {
  855. FT_Done_FreeType(library);
  856. }
  857. }
  858. #endif // MODULE_FREETYPE_ENABLED