dynamic_font_adv.cpp 32 KB

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