diff options
author | bruvzg <7645683+bruvzg@users.noreply.github.com> | 2020-12-10 17:30:25 +0200 |
---|---|---|
committer | bruvzg <7645683+bruvzg@users.noreply.github.com> | 2021-03-31 09:32:14 +0300 |
commit | 0d3fa2a12558d90e7b9cc05f38e8b5c866a2d794 (patch) | |
tree | 23385098a4f26118be7ab4537c49db73d1d60186 /modules | |
parent | 2a66714bfb4654f466479c7b0bc8be103ee9218a (diff) |
[Complex Text Layouts] Provide access to glyph contour points.
Diffstat (limited to 'modules')
-rw-r--r-- | modules/gdnative/include/text/godot_text.h | 1 | ||||
-rw-r--r-- | modules/gdnative/text/text_server_gdnative.cpp | 6 | ||||
-rw-r--r-- | modules/gdnative/text/text_server_gdnative.h | 2 | ||||
-rw-r--r-- | modules/text_server_adv/dynamic_font_adv.cpp | 23 | ||||
-rw-r--r-- | modules/text_server_adv/dynamic_font_adv.h | 2 | ||||
-rw-r--r-- | modules/text_server_adv/font_adv.h | 6 | ||||
-rw-r--r-- | modules/text_server_adv/text_server_adv.cpp | 7 | ||||
-rw-r--r-- | modules/text_server_adv/text_server_adv.h | 2 | ||||
-rw-r--r-- | modules/text_server_fb/dynamic_font_fb.cpp | 23 | ||||
-rw-r--r-- | modules/text_server_fb/dynamic_font_fb.h | 2 | ||||
-rw-r--r-- | modules/text_server_fb/font_fb.h | 2 | ||||
-rw-r--r-- | modules/text_server_fb/text_server_fb.cpp | 7 | ||||
-rw-r--r-- | modules/text_server_fb/text_server_fb.h | 2 |
13 files changed, 83 insertions, 2 deletions
diff --git a/modules/gdnative/include/text/godot_text.h b/modules/gdnative/include/text/godot_text.h index 86fc745134..99c78c72e6 100644 --- a/modules/gdnative/include/text/godot_text.h +++ b/modules/gdnative/include/text/godot_text.h @@ -118,6 +118,7 @@ typedef struct { godot_vector2 (*font_get_glyph_kerning)(void *, godot_rid *, uint32_t, uint32_t, int); godot_vector2 (*font_draw_glyph)(void *, godot_rid *, godot_rid *, int, const godot_vector2 *, uint32_t, const godot_color *); godot_vector2 (*font_draw_glyph_outline)(void *, godot_rid *, godot_rid *, int, int, const godot_vector2 *, uint32_t, const godot_color *); + bool (*font_get_glyph_contours)(void *, godot_rid *, int, uint32_t, godot_packed_vector3_array *, godot_packed_int32_array *, bool *); float (*font_get_oversampling)(void *); void (*font_set_oversampling)(void *, float); godot_packed_string_array (*get_system_fonts)(void *); diff --git a/modules/gdnative/text/text_server_gdnative.cpp b/modules/gdnative/text/text_server_gdnative.cpp index 7cd8de5f2e..7cb749309a 100644 --- a/modules/gdnative/text/text_server_gdnative.cpp +++ b/modules/gdnative/text/text_server_gdnative.cpp @@ -359,6 +359,12 @@ Vector2 TextServerGDNative::font_draw_glyph_outline(RID p_font, RID p_canvas, in return advance; } +bool TextServerGDNative::font_get_glyph_contours(RID p_font, int p_size, uint32_t p_index, Vector<Vector3> &r_points, Vector<int32_t> &r_contours, bool &r_orientation) const { + ERR_FAIL_COND_V(interface == nullptr, false); + ERR_FAIL_COND_V(interface->font_get_glyph_contours == nullptr, false); + return interface->font_get_glyph_contours(data, (godot_rid *)&p_font, p_size, p_index, (godot_packed_vector3_array *)&r_points, (godot_packed_int32_array *)&r_contours, (bool *)&r_orientation); +} + float TextServerGDNative::font_get_oversampling() const { ERR_FAIL_COND_V(interface == nullptr, 1.f); return interface->font_get_oversampling(data); diff --git a/modules/gdnative/text/text_server_gdnative.h b/modules/gdnative/text/text_server_gdnative.h index 931bb44885..7e42b16fe1 100644 --- a/modules/gdnative/text/text_server_gdnative.h +++ b/modules/gdnative/text/text_server_gdnative.h @@ -126,6 +126,8 @@ public: virtual Vector2 font_draw_glyph(RID p_font, RID p_canvas, int p_size, const Vector2 &p_pos, uint32_t p_index, const Color &p_color = Color(1, 1, 1)) const override; virtual Vector2 font_draw_glyph_outline(RID p_font, RID p_canvas, int p_size, int p_outline_size, const Vector2 &p_pos, uint32_t p_index, const Color &p_color = Color(1, 1, 1)) const override; + virtual bool font_get_glyph_contours(RID p_font, int p_size, uint32_t p_index, Vector<Vector3> &r_points, Vector<int32_t> &r_contours, bool &r_orientation) const override; + virtual float font_get_oversampling() const override; virtual void font_set_oversampling(float p_oversampling) override; diff --git a/modules/text_server_adv/dynamic_font_adv.cpp b/modules/text_server_adv/dynamic_font_adv.cpp index 2521e68dda..19d04bdb02 100644 --- a/modules/text_server_adv/dynamic_font_adv.cpp +++ b/modules/text_server_adv/dynamic_font_adv.cpp @@ -997,6 +997,29 @@ Vector2 DynamicFontDataAdvanced::draw_glyph_outline(RID p_canvas, int p_size, in return advance; } +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 { + _THREAD_SAFE_METHOD_ + DataAtSize *fds = const_cast<DynamicFontDataAdvanced *>(this)->get_data_for_size(p_size); + ERR_FAIL_COND_V(fds == nullptr, false); + + int error = FT_Load_Glyph(fds->face, p_index, FT_LOAD_NO_BITMAP | (force_autohinter ? FT_LOAD_FORCE_AUTOHINT : 0)); + ERR_FAIL_COND_V(error, false); + + r_points.clear(); + r_contours.clear(); + + float h = fds->ascent; + float scale = (1.0 / 64.0) / oversampling * fds->scale_color_font; + for (short i = 0; i < fds->face->glyph->outline.n_points; i++) { + 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]))); + } + for (short i = 0; i < fds->face->glyph->outline.n_contours; i++) { + r_contours.push_back(fds->face->glyph->outline.contours[i]); + } + r_orientation = (FT_Outline_Get_Orientation(&fds->face->glyph->outline) == FT_ORIENTATION_FILL_RIGHT); + return true; +} + DynamicFontDataAdvanced::~DynamicFontDataAdvanced() { clear_cache(); if (library != nullptr) { diff --git a/modules/text_server_adv/dynamic_font_adv.h b/modules/text_server_adv/dynamic_font_adv.h index d69a30b321..1292966f0c 100644 --- a/modules/text_server_adv/dynamic_font_adv.h +++ b/modules/text_server_adv/dynamic_font_adv.h @@ -186,6 +186,8 @@ public: virtual Vector2 draw_glyph(RID p_canvas, int p_size, const Vector2 &p_pos, uint32_t p_index, const Color &p_color) const override; virtual Vector2 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 override; + virtual bool get_glyph_contours(int p_size, uint32_t p_index, Vector<Vector3> &r_points, Vector<int32_t> &r_contours, bool &r_orientation) const override; + virtual ~DynamicFontDataAdvanced() override; }; diff --git a/modules/text_server_adv/font_adv.h b/modules/text_server_adv/font_adv.h index 2b6d977451..4fadefc569 100644 --- a/modules/text_server_adv/font_adv.h +++ b/modules/text_server_adv/font_adv.h @@ -92,8 +92,8 @@ struct FontDataAdvanced { virtual bool has_outline() const = 0; virtual float get_base_size() const = 0; - virtual bool is_lang_supported(const String &p_lang) const { return false; }; - virtual bool is_script_supported(uint32_t p_script) const { return false; }; + virtual bool is_lang_supported(const String &p_lang) const { return true; }; + virtual bool is_script_supported(uint32_t p_script) const { return true; }; virtual bool has_char(char32_t p_char) const = 0; virtual String get_supported_chars() const = 0; @@ -107,6 +107,8 @@ struct FontDataAdvanced { virtual Vector2 draw_glyph(RID p_canvas, int p_size, const Vector2 &p_pos, uint32_t p_index, const Color &p_color) const = 0; virtual Vector2 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 = 0; + virtual bool get_glyph_contours(int p_size, uint32_t p_index, Vector<Vector3> &r_points, Vector<int32_t> &r_contours, bool &r_orientation) const { return false; }; + virtual ~FontDataAdvanced(){}; }; diff --git a/modules/text_server_adv/text_server_adv.cpp b/modules/text_server_adv/text_server_adv.cpp index 43b8f18101..9007c696eb 100644 --- a/modules/text_server_adv/text_server_adv.cpp +++ b/modules/text_server_adv/text_server_adv.cpp @@ -906,6 +906,13 @@ Vector2 TextServerAdvanced::font_draw_glyph_outline(RID p_font, RID p_canvas, in return fd->draw_glyph_outline(p_canvas, p_size, p_outline_size, p_pos, p_index, p_color); } +bool TextServerAdvanced::font_get_glyph_contours(RID p_font, int p_size, uint32_t p_index, Vector<Vector3> &r_points, Vector<int32_t> &r_contours, bool &r_orientation) const { + _THREAD_SAFE_METHOD_ + const FontDataAdvanced *fd = font_owner.getornull(p_font); + ERR_FAIL_COND_V(!fd, false); + return fd->get_glyph_contours(p_size, p_index, r_points, r_contours, r_orientation); +} + float TextServerAdvanced::font_get_oversampling() const { return oversampling; } diff --git a/modules/text_server_adv/text_server_adv.h b/modules/text_server_adv/text_server_adv.h index b53b5716e5..4ad23ca059 100644 --- a/modules/text_server_adv/text_server_adv.h +++ b/modules/text_server_adv/text_server_adv.h @@ -188,6 +188,8 @@ public: virtual Vector2 font_draw_glyph(RID p_font, RID p_canvas, int p_size, const Vector2 &p_pos, uint32_t p_index, const Color &p_color = Color(1, 1, 1)) const override; virtual Vector2 font_draw_glyph_outline(RID p_font, RID p_canvas, int p_size, int p_outline_size, const Vector2 &p_pos, uint32_t p_index, const Color &p_color = Color(1, 1, 1)) const override; + virtual bool font_get_glyph_contours(RID p_font, int p_size, uint32_t p_index, Vector<Vector3> &r_points, Vector<int32_t> &r_contours, bool &r_orientation) const override; + virtual float font_get_oversampling() const override; virtual void font_set_oversampling(float p_oversampling) override; diff --git a/modules/text_server_fb/dynamic_font_fb.cpp b/modules/text_server_fb/dynamic_font_fb.cpp index 66d36bc885..dec1d6f83f 100644 --- a/modules/text_server_fb/dynamic_font_fb.cpp +++ b/modules/text_server_fb/dynamic_font_fb.cpp @@ -680,6 +680,29 @@ Vector2 DynamicFontDataFallback::draw_glyph_outline(RID p_canvas, int p_size, in return advance; } +bool DynamicFontDataFallback::get_glyph_contours(int p_size, uint32_t p_index, Vector<Vector3> &r_points, Vector<int32_t> &r_contours, bool &r_orientation) const { + _THREAD_SAFE_METHOD_ + DataAtSize *fds = const_cast<DynamicFontDataFallback *>(this)->get_data_for_size(p_size); + ERR_FAIL_COND_V(fds == nullptr, false); + + int error = FT_Load_Glyph(fds->face, p_index, FT_LOAD_NO_BITMAP | (force_autohinter ? FT_LOAD_FORCE_AUTOHINT : 0)); + ERR_FAIL_COND_V(error, false); + + r_points.clear(); + r_contours.clear(); + + float h = fds->ascent; + float scale = (1.0 / 64.0) / oversampling * fds->scale_color_font; + for (short i = 0; i < fds->face->glyph->outline.n_points; i++) { + 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]))); + } + for (short i = 0; i < fds->face->glyph->outline.n_contours; i++) { + r_contours.push_back(fds->face->glyph->outline.contours[i]); + } + r_orientation = (FT_Outline_Get_Orientation(&fds->face->glyph->outline) == FT_ORIENTATION_FILL_RIGHT); + return true; +} + DynamicFontDataFallback::~DynamicFontDataFallback() { clear_cache(); if (library != nullptr) { diff --git a/modules/text_server_fb/dynamic_font_fb.h b/modules/text_server_fb/dynamic_font_fb.h index eb70f46666..b34c8cbed5 100644 --- a/modules/text_server_fb/dynamic_font_fb.h +++ b/modules/text_server_fb/dynamic_font_fb.h @@ -164,6 +164,8 @@ public: virtual Vector2 draw_glyph(RID p_canvas, int p_size, const Vector2 &p_pos, uint32_t p_index, const Color &p_color) const override; virtual Vector2 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 override; + virtual bool get_glyph_contours(int p_size, uint32_t p_index, Vector<Vector3> &r_points, Vector<int32_t> &r_contours, bool &r_orientation) const override; + virtual ~DynamicFontDataFallback() override; }; diff --git a/modules/text_server_fb/font_fb.h b/modules/text_server_fb/font_fb.h index 218f3df03a..fe9888b7f4 100644 --- a/modules/text_server_fb/font_fb.h +++ b/modules/text_server_fb/font_fb.h @@ -93,6 +93,8 @@ struct FontDataFallback { virtual Vector2 draw_glyph(RID p_canvas, int p_size, const Vector2 &p_pos, uint32_t p_index, const Color &p_color) const = 0; virtual Vector2 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 = 0; + virtual bool get_glyph_contours(int p_size, uint32_t p_index, Vector<Vector3> &r_points, Vector<int32_t> &r_contours, bool &r_orientation) const { return false; }; + virtual ~FontDataFallback(){}; }; diff --git a/modules/text_server_fb/text_server_fb.cpp b/modules/text_server_fb/text_server_fb.cpp index f46f96d30d..98a67ef309 100644 --- a/modules/text_server_fb/text_server_fb.cpp +++ b/modules/text_server_fb/text_server_fb.cpp @@ -452,6 +452,13 @@ Vector2 TextServerFallback::font_draw_glyph_outline(RID p_font, RID p_canvas, in return fd->draw_glyph_outline(p_canvas, p_size, p_outline_size, p_pos, p_index, p_color); } +bool TextServerFallback::font_get_glyph_contours(RID p_font, int p_size, uint32_t p_index, Vector<Vector3> &r_points, Vector<int32_t> &r_contours, bool &r_orientation) const { + _THREAD_SAFE_METHOD_ + const FontDataFallback *fd = font_owner.getornull(p_font); + ERR_FAIL_COND_V(!fd, false); + return fd->get_glyph_contours(p_size, p_index, r_points, r_contours, r_orientation); +} + float TextServerFallback::font_get_oversampling() const { return oversampling; } diff --git a/modules/text_server_fb/text_server_fb.h b/modules/text_server_fb/text_server_fb.h index b10369d172..8f5eb1d315 100644 --- a/modules/text_server_fb/text_server_fb.h +++ b/modules/text_server_fb/text_server_fb.h @@ -137,6 +137,8 @@ public: virtual Vector2 font_draw_glyph(RID p_font, RID p_canvas, int p_size, const Vector2 &p_pos, uint32_t p_index, const Color &p_color = Color(1, 1, 1)) const override; virtual Vector2 font_draw_glyph_outline(RID p_font, RID p_canvas, int p_size, int p_outline_size, const Vector2 &p_pos, uint32_t p_index, const Color &p_color = Color(1, 1, 1)) const override; + virtual bool font_get_glyph_contours(RID p_font, int p_size, uint32_t p_index, Vector<Vector3> &r_points, Vector<int32_t> &r_contours, bool &r_orientation) const override; + virtual float font_get_oversampling() const override; virtual void font_set_oversampling(float p_oversampling) override; |