diff options
Diffstat (limited to 'servers/text_server.h')
-rw-r--r-- | servers/text_server.h | 301 |
1 files changed, 142 insertions, 159 deletions
diff --git a/servers/text_server.h b/servers/text_server.h index 90ad9b493b..3a5f946fbf 100644 --- a/servers/text_server.h +++ b/servers/text_server.h @@ -34,13 +34,14 @@ #include "core/object/ref_counted.h" #include "core/os/os.h" #include "core/templates/rid.h" +#include "core/variant/native_ptr.h" #include "core/variant/variant.h" -#include "scene/resources/texture.h" -class CanvasTexture; +struct Glyph; +struct CaretInfo; -class TextServer : public Object { - GDCLASS(TextServer, Object); +class TextServer : public RefCounted { + GDCLASS(TextServer, RefCounted); public: enum Direction { @@ -63,12 +64,12 @@ public: JUSTIFICATION_CONSTRAIN_ELLIPSIS = 1 << 4, }; - enum LineBreakFlag { + enum LineBreakFlag { // LineBreakFlag can be passed in the same value as the JustificationFlag, do not use the same values. BREAK_NONE = 0, - BREAK_MANDATORY = 1 << 4, - BREAK_WORD_BOUND = 1 << 5, - BREAK_GRAPHEME_BOUND = 1 << 6, - BREAK_WORD_BOUND_ADAPTIVE = 1 << 5 | 1 << 7, + BREAK_MANDATORY = 1 << 5, + BREAK_WORD_BOUND = 1 << 6, + BREAK_GRAPHEME_BOUND = 1 << 7, + BREAK_WORD_BOUND_ADAPTIVE = 1 << 6 | 1 << 8, }; enum TextOverrunFlag { @@ -124,50 +125,13 @@ public: SPACING_BOTTOM, }; - struct Glyph { - int start = -1; // Start offset in the source string. - int end = -1; // End offset in the source string. - - uint8_t count = 0; // Number of glyphs in the grapheme, set in the first glyph only. - uint8_t repeat = 1; // Draw multiple times in the row. - uint16_t flags = 0; // Grapheme flags (valid, rtl, virtual), set in the first glyph only. - - real_t x_off = 0.f; // Offset from the origin of the glyph on baseline. - real_t y_off = 0.f; - real_t advance = 0.f; // Advance to the next glyph along baseline(x for horizontal layout, y for vertical). - - RID font_rid; // Font resource. - int font_size = 0; // Font size; - int32_t index = 0; // Glyph index (font specific) or UTF-32 codepoint (for the invalid glyphs). - - bool operator==(const Glyph &p_a) const; - bool operator!=(const Glyph &p_a) const; - - bool operator<(const Glyph &p_a) const; - bool operator>(const Glyph &p_a) const; - }; - - struct GlyphCompare { // For line breaking reordering. - _FORCE_INLINE_ bool operator()(const Glyph &l, const Glyph &r) const { - if (l.start == r.start) { - if (l.count == r.count) { - if ((l.flags & GRAPHEME_IS_VIRTUAL) == GRAPHEME_IS_VIRTUAL) { - return false; - } else { - return true; - } - } - return l.count > r.count; // Sort first glyph with count & flags, order of the rest are irrelevant. - } else { - return l.start < r.start; - } - } - }; + void _draw_hex_code_box_number(RID p_canvas, int p_size, const Vector2 &p_pos, uint8_t p_index, const Color &p_color) const; +protected: struct TrimData { int trim_pos = -1; int ellipsis_pos = -1; - Vector<TextServer::Glyph> ellipsis_glyph_buf; + Vector<Glyph> ellipsis_glyph_buf; }; struct ShapedTextData { @@ -215,45 +179,37 @@ public: bool preserve_invalid = true; // Draw hex code box instead of missing characters. bool preserve_control = false; // Draw control characters. - real_t ascent = 0.f; // Ascent for horizontal layout, 1/2 of width for vertical. - real_t descent = 0.f; // Descent for horizontal layout, 1/2 of width for vertical. - real_t width = 0.f; // Width for horizontal layout, height for vertical. - real_t width_trimmed = 0.f; + float ascent = 0.f; // Ascent for horizontal layout, 1/2 of width for vertical. + float descent = 0.f; // Descent for horizontal layout, 1/2 of width for vertical. + float width = 0.f; // Width for horizontal layout, height for vertical. + float width_trimmed = 0.f; - real_t upos = 0.f; - real_t uthk = 0.f; + float upos = 0.f; + float uthk = 0.f; TrimData overrun_trim_data; bool fit_width_minimum_reached = false; - Vector<TextServer::Glyph> glyphs; - Vector<TextServer::Glyph> glyphs_logical; + Vector<Glyph> glyphs; + Vector<Glyph> glyphs_logical; }; -protected: static void _bind_methods(); - static Vector3 hex_code_box_font_size[2]; - static Ref<CanvasTexture> hex_code_box_font_tex[2]; - public: - static void initialize_hex_code_box_fonts(); - static void finish_hex_code_box_fonts(); - - virtual bool has_feature(Feature p_feature) = 0; + virtual bool has_feature(Feature p_feature) const = 0; virtual String get_name() const = 0; + virtual uint32_t get_features() const = 0; virtual void free(RID p_rid) = 0; virtual bool has(RID p_rid) = 0; virtual bool load_support_data(const String &p_filename) = 0; -#ifdef TOOLS_ENABLED - virtual String get_support_data_filename() = 0; - virtual String get_support_data_info() = 0; - virtual bool save_support_data(const String &p_filename) = 0; -#endif + virtual String get_support_data_filename() const = 0; + virtual String get_support_data_info() const = 0; + virtual bool save_support_data(const String &p_filename) const = 0; - virtual bool is_locale_right_to_left(const String &p_locale) = 0; + virtual bool is_locale_right_to_left(const String &p_locale) const = 0; virtual int32_t name_to_tag(const String &p_name) const { return 0; }; virtual String tag_to_name(int32_t p_tag) const { return ""; }; @@ -282,33 +238,33 @@ public: virtual void font_set_force_autohinter(RID p_font_rid, bool p_force_autohinter) = 0; virtual bool font_is_force_autohinter(RID p_font_rid) const = 0; - virtual void font_set_hinting(RID p_font_rid, TextServer::Hinting p_hinting) = 0; - virtual TextServer::Hinting font_get_hinting(RID p_font_rid) const = 0; + virtual void font_set_hinting(RID p_font_rid, Hinting p_hinting) = 0; + virtual Hinting font_get_hinting(RID p_font_rid) const = 0; virtual void font_set_variation_coordinates(RID p_font_rid, const Dictionary &p_variation_coordinates) = 0; virtual Dictionary font_get_variation_coordinates(RID p_font_rid) const = 0; - virtual void font_set_oversampling(RID p_font_rid, real_t p_oversampling) = 0; - virtual real_t font_get_oversampling(RID p_font_rid) const = 0; + virtual void font_set_oversampling(RID p_font_rid, float p_oversampling) = 0; + virtual float font_get_oversampling(RID p_font_rid) const = 0; virtual Array font_get_size_cache_list(RID p_font_rid) const = 0; virtual void font_clear_size_cache(RID p_font_rid) = 0; virtual void font_remove_size_cache(RID p_font_rid, const Vector2i &p_size) = 0; - virtual void font_set_ascent(RID p_font_rid, int p_size, real_t p_ascent) = 0; - virtual real_t font_get_ascent(RID p_font_rid, int p_size) const = 0; + virtual void font_set_ascent(RID p_font_rid, int p_size, float p_ascent) = 0; + virtual float font_get_ascent(RID p_font_rid, int p_size) const = 0; - virtual void font_set_descent(RID p_font_rid, int p_size, real_t p_descent) = 0; - virtual real_t font_get_descent(RID p_font_rid, int p_size) const = 0; + virtual void font_set_descent(RID p_font_rid, int p_size, float p_descent) = 0; + virtual float font_get_descent(RID p_font_rid, int p_size) const = 0; - virtual void font_set_underline_position(RID p_font_rid, int p_size, real_t p_underline_position) = 0; - virtual real_t font_get_underline_position(RID p_font_rid, int p_size) const = 0; + virtual void font_set_underline_position(RID p_font_rid, int p_size, float p_underline_position) = 0; + virtual float font_get_underline_position(RID p_font_rid, int p_size) const = 0; - virtual void font_set_underline_thickness(RID p_font_rid, int p_size, real_t p_underline_thickness) = 0; - virtual real_t font_get_underline_thickness(RID p_font_rid, int p_size) const = 0; + virtual void font_set_underline_thickness(RID p_font_rid, int p_size, float p_underline_thickness) = 0; + virtual float font_get_underline_thickness(RID p_font_rid, int p_size) const = 0; - virtual void font_set_scale(RID p_font_rid, int p_size, real_t p_scale) = 0; - virtual real_t font_get_scale(RID p_font_rid, int p_size) const = 0; + virtual void font_set_scale(RID p_font_rid, int p_size, float p_scale) = 0; + virtual float font_get_scale(RID p_font_rid, int p_size) const = 0; virtual void font_set_spacing(RID p_font_rid, int p_size, SpacingType p_spacing, int p_value) = 0; virtual int font_get_spacing(RID p_font_rid, int p_size, SpacingType p_spacing) const = 0; @@ -342,7 +298,7 @@ public: virtual int font_get_glyph_texture_idx(RID p_font_rid, const Vector2i &p_size, int32_t p_glyph) const = 0; virtual void font_set_glyph_texture_idx(RID p_font_rid, const Vector2i &p_size, int32_t p_glyph, int p_texture_idx) = 0; - virtual bool font_get_glyph_contours(RID p_font, int p_size, int32_t p_index, Vector<Vector3> &r_points, Vector<int32_t> &r_contours, bool &r_orientation) const = 0; + virtual Dictionary font_get_glyph_contours(RID p_font, int p_size, int32_t p_index) const = 0; virtual Array font_get_kerning_list(RID p_font_rid, int p_size) const = 0; virtual void font_clear_kerning_map(RID p_font_rid, int p_size) = 0; @@ -377,11 +333,11 @@ public: virtual Dictionary font_supported_feature_list(RID p_font_rid) const = 0; virtual Dictionary font_supported_variation_list(RID p_font_rid) const = 0; - virtual real_t font_get_global_oversampling() const = 0; - virtual void font_set_global_oversampling(real_t p_oversampling) = 0; + virtual float font_get_global_oversampling() const = 0; + virtual void font_set_global_oversampling(float p_oversampling) = 0; - Vector2 get_hex_code_box_size(int p_size, char32_t p_index) const; - void draw_hex_code_box(RID p_canvas, int p_size, const Vector2 &p_pos, char32_t p_index, const Color &p_color) const; + virtual Vector2 get_hex_code_box_size(int p_size, char32_t p_index) const; + virtual void draw_hex_code_box(RID p_canvas, int p_size, const Vector2 &p_pos, char32_t p_index, const Color &p_color) const; /* Shaped text buffer interface */ @@ -392,7 +348,7 @@ public: virtual void shaped_text_set_direction(RID p_shaped, Direction p_direction = DIRECTION_AUTO) = 0; virtual Direction shaped_text_get_direction(RID p_shaped) const = 0; - virtual void shaped_text_set_bidi_override(RID p_shaped, const Vector<Vector2i> &p_override) = 0; + virtual void shaped_text_set_bidi_override(RID p_shaped, const Array &p_override) = 0; virtual void shaped_text_set_orientation(RID p_shaped, Orientation p_orientation = ORIENTATION_HORIZONTAL) = 0; virtual Orientation shaped_text_get_orientation(RID p_shaped) const = 0; @@ -410,8 +366,8 @@ public: virtual RID shaped_text_substr(RID p_shaped, int p_start, int p_length) const = 0; // Copy shaped substring (e.g. line break) without reshaping, but correctly reordered, preservers range. virtual RID shaped_text_get_parent(RID p_shaped) const = 0; - virtual real_t shaped_text_fit_to_width(RID p_shaped, real_t p_width, uint8_t /*JustificationFlag*/ p_jst_flags = JUSTIFICATION_WORD_BOUND | JUSTIFICATION_KASHIDA) = 0; - virtual real_t shaped_text_tab_align(RID p_shaped, const Vector<real_t> &p_tab_stops) = 0; + virtual float shaped_text_fit_to_width(RID p_shaped, float p_width, uint16_t /*JustificationFlag*/ p_jst_flags = JUSTIFICATION_WORD_BOUND | JUSTIFICATION_KASHIDA) = 0; + virtual float shaped_text_tab_align(RID p_shaped, const PackedFloat32Array &p_tab_stops) = 0; virtual bool shaped_text_shape(RID p_shaped) = 0; virtual bool shaped_text_update_breaks(RID p_shaped) = 0; @@ -419,66 +375,109 @@ public: virtual bool shaped_text_is_ready(RID p_shaped) const = 0; - virtual Vector<Glyph> shaped_text_get_glyphs(RID p_shaped) const = 0; + virtual const Glyph *shaped_text_get_glyphs(RID p_shaped) const = 0; + Array _shaped_text_get_glyphs_wrapper(RID p_shaped) const; + virtual const Glyph *shaped_text_sort_logical(RID p_shaped) = 0; + Array _shaped_text_sort_logical_wrapper(RID p_shaped); + virtual int shaped_text_get_glyph_count(RID p_shaped) const = 0; virtual Vector2i shaped_text_get_range(RID p_shaped) const = 0; - virtual Vector<Glyph> shaped_text_sort_logical(RID p_shaped) = 0; + virtual PackedInt32Array shaped_text_get_line_breaks_adv(RID p_shaped, const PackedFloat32Array &p_width, int p_start = 0, bool p_once = true, uint16_t /*TextBreakFlag*/ p_break_flags = BREAK_MANDATORY | BREAK_WORD_BOUND) const; + virtual PackedInt32Array shaped_text_get_line_breaks(RID p_shaped, float p_width, int p_start = 0, uint16_t /*TextBreakFlag*/ p_break_flags = BREAK_MANDATORY | BREAK_WORD_BOUND) const; + virtual PackedInt32Array shaped_text_get_word_breaks(RID p_shaped, int p_grapheme_flags = GRAPHEME_IS_SPACE | GRAPHEME_IS_PUNCTUATION) const; - virtual Vector<Vector2i> shaped_text_get_line_breaks_adv(RID p_shaped, const Vector<real_t> &p_width, int p_start = 0, bool p_once = true, uint8_t /*TextBreakFlag*/ p_break_flags = BREAK_MANDATORY | BREAK_WORD_BOUND) const; - virtual Vector<Vector2i> shaped_text_get_line_breaks(RID p_shaped, real_t p_width, int p_start = 0, uint8_t /*TextBreakFlag*/ p_break_flags = BREAK_MANDATORY | BREAK_WORD_BOUND) const; - virtual Vector<Vector2i> shaped_text_get_word_breaks(RID p_shaped, int p_grapheme_flags = GRAPHEME_IS_SPACE | GRAPHEME_IS_PUNCTUATION) const; + virtual int shaped_text_get_trim_pos(RID p_shaped) const = 0; + virtual int shaped_text_get_ellipsis_pos(RID p_shaped) const = 0; + virtual const Glyph *shaped_text_get_ellipsis_glyphs(RID p_shaped) const = 0; + Array _shaped_text_get_ellipsis_glyphs_wrapper(RID p_shaped) const; + virtual int shaped_text_get_ellipsis_glyph_count(RID p_shaped) const = 0; + + virtual void shaped_text_overrun_trim_to_width(RID p_shaped, float p_width, uint16_t p_trim_flags) = 0; - virtual TrimData shaped_text_get_trim_data(RID p_shaped) const; - virtual void shaped_text_overrun_trim_to_width(RID p_shaped, real_t p_width, uint8_t p_trim_flags) = 0; virtual Array shaped_text_get_objects(RID p_shaped) const = 0; virtual Rect2 shaped_text_get_object_rect(RID p_shaped, Variant p_key) const = 0; virtual Size2 shaped_text_get_size(RID p_shaped) const = 0; - virtual real_t shaped_text_get_ascent(RID p_shaped) const = 0; - virtual real_t shaped_text_get_descent(RID p_shaped) const = 0; - virtual real_t shaped_text_get_width(RID p_shaped) const = 0; - virtual real_t shaped_text_get_underline_position(RID p_shaped) const = 0; - virtual real_t shaped_text_get_underline_thickness(RID p_shaped) const = 0; + virtual float shaped_text_get_ascent(RID p_shaped) const = 0; + virtual float shaped_text_get_descent(RID p_shaped) const = 0; + virtual float shaped_text_get_width(RID p_shaped) const = 0; + virtual float shaped_text_get_underline_position(RID p_shaped) const = 0; + virtual float shaped_text_get_underline_thickness(RID p_shaped) const = 0; + + virtual Direction shaped_text_get_dominant_direction_in_range(RID p_shaped, int p_start, int p_end) const; - virtual Direction shaped_text_get_dominant_direciton_in_range(RID p_shaped, int p_start, int p_end) const; + virtual CaretInfo shaped_text_get_carets(RID p_shaped, int p_position) const; + Dictionary _shaped_text_get_carets_wrapper(RID p_shaped, int p_position) const; - virtual void shaped_text_get_carets(RID p_shaped, int p_position, Rect2 &p_leading_caret, Direction &p_leading_dir, Rect2 &p_trailing_caret, Direction &p_trailing_dir) const; virtual Vector<Vector2> shaped_text_get_selection(RID p_shaped, int p_start, int p_end) const; - virtual int shaped_text_hit_test_grapheme(RID p_shaped, real_t p_coords) const; // Return grapheme index. - virtual int shaped_text_hit_test_position(RID p_shaped, real_t p_coords) const; // Return caret/selection position. + virtual int shaped_text_hit_test_grapheme(RID p_shaped, float p_coords) const; // Return grapheme index. + virtual int shaped_text_hit_test_position(RID p_shaped, float p_coords) const; // Return caret/selection position. - virtual int shaped_text_next_grapheme_pos(RID p_shaped, int p_pos); - virtual int shaped_text_prev_grapheme_pos(RID p_shaped, int p_pos); + virtual int shaped_text_next_grapheme_pos(RID p_shaped, int p_pos) const; + virtual int shaped_text_prev_grapheme_pos(RID p_shaped, int p_pos) const; // The pen position is always placed on the baseline and moveing left to right. - virtual void shaped_text_draw(RID p_shaped, RID p_canvas, const Vector2 &p_pos, real_t p_clip_l = -1.f, real_t p_clip_r = -1.f, const Color &p_color = Color(1, 1, 1)) const; - virtual void shaped_text_draw_outline(RID p_shaped, RID p_canvas, const Vector2 &p_pos, real_t p_clip_l = -1.f, real_t p_clip_r = -1.f, int p_outline_size = 1, const Color &p_color = Color(1, 1, 1)) const; + virtual void shaped_text_draw(RID p_shaped, RID p_canvas, const Vector2 &p_pos, float p_clip_l = -1.f, float p_clip_r = -1.f, const Color &p_color = Color(1, 1, 1)) const; + virtual void shaped_text_draw_outline(RID p_shaped, RID p_canvas, const Vector2 &p_pos, float p_clip_l = -1.f, float p_clip_r = -1.f, int p_outline_size = 1, const Color &p_color = Color(1, 1, 1)) const; // Number conversion. virtual String format_number(const String &p_string, const String &p_language = "") const { return p_string; }; virtual String parse_number(const String &p_string, const String &p_language = "") const { return p_string; }; virtual String percent_sign(const String &p_language = "") const { return "%"; }; - /* GDScript wrappers */ - RID _create_font_memory(const PackedByteArray &p_data, int p_base_size = 16); + TextServer(); + ~TextServer(); +}; + +/*************************************************************************/ - Dictionary _font_get_glyph_contours(RID p_font, int p_size, int32_t p_index) const; +struct Glyph { + int start = -1; // Start offset in the source string. + int end = -1; // End offset in the source string. - Array _shaped_text_get_glyphs(RID p_shaped) const; - Dictionary _shaped_text_get_carets(RID p_shaped, int p_position) const; + uint8_t count = 0; // Number of glyphs in the grapheme, set in the first glyph only. + uint8_t repeat = 1; // Draw multiple times in the row. + uint16_t flags = 0; // Grapheme flags (valid, rtl, virtual), set in the first glyph only. - void _shaped_text_set_bidi_override(RID p_shaped, const Array &p_override); + float x_off = 0.f; // Offset from the origin of the glyph on baseline. + float y_off = 0.f; + float advance = 0.f; // Advance to the next glyph along baseline(x for horizontal layout, y for vertical). - Array _shaped_text_get_line_breaks_adv(RID p_shaped, const PackedFloat32Array &p_width, int p_start, bool p_once, uint8_t p_break_flags) const; - Array _shaped_text_get_line_breaks(RID p_shaped, real_t p_width, int p_start, uint8_t p_break_flags) const; - Array _shaped_text_get_word_breaks(RID p_shaped) const; + RID font_rid; // Font resource. + int font_size = 0; // Font size; + int32_t index = 0; // Glyph index (font specific) or UTF-32 codepoint (for the invalid glyphs). - Array _shaped_text_get_selection(RID p_shaped, int p_start, int p_end) const; + bool operator==(const Glyph &p_a) const; + bool operator!=(const Glyph &p_a) const; - TextServer(); - ~TextServer(); + bool operator<(const Glyph &p_a) const; + bool operator>(const Glyph &p_a) const; +}; + +struct CaretInfo { + Rect2 l_caret; + Rect2 t_caret; + TextServer::Direction l_dir; + TextServer::Direction t_dir; +}; + +struct GlyphCompare { // For line breaking reordering. + _FORCE_INLINE_ bool operator()(const Glyph &l, const Glyph &r) const { + if (l.start == r.start) { + if (l.count == r.count) { + if ((l.flags & TextServer::GRAPHEME_IS_VIRTUAL) == TextServer::GRAPHEME_IS_VIRTUAL) { + return false; + } else { + return true; + } + } + return l.count > r.count; // Sort first glyph with count & flags, order of the rest are irrelevant. + } else { + return l.start < r.start; + } + } }; /*************************************************************************/ @@ -486,52 +485,32 @@ public: class TextServerManager : public Object { GDCLASS(TextServerManager, Object); -public: - typedef TextServer *(*CreateFunction)(Error &r_error, void *p_user_data); - protected: static void _bind_methods(); private: static TextServerManager *singleton; - static TextServer *server; - enum { - MAX_SERVERS = 64 - }; - struct TextServerCreate { - String name; - CreateFunction create_function = nullptr; - uint32_t features = 0; - TextServer *instance = nullptr; - void *user_data = nullptr; - }; - - static TextServerCreate server_create_functions[MAX_SERVERS]; - static int server_create_count; + Ref<TextServer> primary_interface; + Vector<Ref<TextServer>> interfaces; public: _FORCE_INLINE_ static TextServerManager *get_singleton() { return singleton; } - static void register_create_function(const String &p_name, uint32_t p_features, CreateFunction p_function, void *p_user_data); - static int get_interface_count(); - static String get_interface_name(int p_index); - static uint32_t get_interface_features(int p_index); - static TextServer *initialize(int p_index, Error &r_error); - static TextServer *get_primary_interface(); - - /* GDScript wrappers */ - int _get_interface_count() const; - String _get_interface_name(int p_index) const; - uint32_t _get_interface_features(int p_index) const; - TextServer *_get_interface(int p_index) const; - Array _get_interfaces() const; - TextServer *_find_interface(const String &p_name) const; + void add_interface(const Ref<TextServer> &p_interface); + void remove_interface(const Ref<TextServer> &p_interface); + int get_interface_count() const; + Ref<TextServer> get_interface(int p_index) const; + Ref<TextServer> find_interface(const String &p_name) const; + Array get_interfaces() const; - bool _set_primary_interface(int p_index); - TextServer *_get_primary_interface() const; + _FORCE_INLINE_ Ref<TextServer> get_primary_interface() const { + return primary_interface; + } + Ref<TextServer> _get_primary_interface() const; + void set_primary_interface(const Ref<TextServer> &p_primary_interface); TextServerManager(); ~TextServerManager(); @@ -539,7 +518,7 @@ public: /*************************************************************************/ -#define TS TextServerManager::get_primary_interface() +#define TS TextServerManager::get_singleton()->get_primary_interface() VARIANT_ENUM_CAST(TextServer::Direction); VARIANT_ENUM_CAST(TextServer::Orientation); @@ -552,4 +531,8 @@ VARIANT_ENUM_CAST(TextServer::Feature); VARIANT_ENUM_CAST(TextServer::ContourPointTag); VARIANT_ENUM_CAST(TextServer::SpacingType); +GDVIRTUAL_NATIVE_PTR(Glyph); +GDVIRTUAL_NATIVE_PTR(Glyph *); +GDVIRTUAL_NATIVE_PTR(CaretInfo); + #endif // TEXT_SERVER_H |