diff options
Diffstat (limited to 'scene/resources/font.h')
-rw-r--r-- | scene/resources/font.h | 239 |
1 files changed, 118 insertions, 121 deletions
diff --git a/scene/resources/font.h b/scene/resources/font.h index d8c350bb41..226979c4a0 100644 --- a/scene/resources/font.h +++ b/scene/resources/font.h @@ -32,173 +32,170 @@ #define FONT_H #include "core/io/resource.h" +#include "core/templates/lru.h" #include "core/templates/map.h" #include "scene/resources/texture.h" +#include "servers/text_server.h" -class Font : public Resource { - GDCLASS(Font, Resource); +/*************************************************************************/ + +class FontData : public Resource { + GDCLASS(FontData, Resource); + + RID rid; + int base_size = 16; + String path; protected: static void _bind_methods(); + bool _set(const StringName &p_name, const Variant &p_value); + bool _get(const StringName &p_name, Variant &r_ret) const; + void _get_property_list(List<PropertyInfo> *p_list) const; + public: - virtual float get_height() const = 0; + virtual RID get_rid() const override; - virtual float get_ascent() const = 0; - virtual float get_descent() const = 0; - virtual float get_underline_position() const = 0; - virtual float get_underline_thickness() const = 0; + void load_resource(const String &p_filename, int p_base_size = 16); + void load_memory(const uint8_t *p_data, size_t p_size, const String &p_type, int p_base_size = 16); + void _load_memory(const PackedByteArray &p_data, const String &p_type, int p_base_size = 16); - virtual Size2 get_char_size(char32_t p_char, char32_t p_next = 0) const = 0; - Size2 get_string_size(const String &p_string) const; - Size2 get_wordwrap_string_size(const String &p_string, float p_width) const; + void set_data_path(const String &p_path); + String get_data_path() const; - virtual bool is_distance_field_hint() const = 0; + float get_height(int p_size) const; + float get_ascent(int p_size) const; + float get_descent(int p_size) const; - void draw(RID p_canvas_item, const Point2 &p_pos, const String &p_text, const Color &p_modulate = Color(1, 1, 1), int p_clip_w = -1, const Color &p_outline_modulate = Color(1, 1, 1)) const; - void draw_halign(RID p_canvas_item, const Point2 &p_pos, HAlign p_align, float p_width, const String &p_text, const Color &p_modulate = Color(1, 1, 1), const Color &p_outline_modulate = Color(1, 1, 1)) const; + Dictionary get_feature_list() const; - virtual bool has_outline() const { return false; } - virtual float draw_char(RID p_canvas_item, const Point2 &p_pos, char32_t p_char, char32_t p_next = 0, const Color &p_modulate = Color(1, 1, 1), bool p_outline = false) const = 0; + float get_underline_position(int p_size) const; + float get_underline_thickness(int p_size) const; - void update_changes(); - Font(); -}; + void set_antialiased(bool p_antialiased); + bool get_antialiased() const; -// Helper class to that draws outlines immediately and draws characters in its destructor. -class FontDrawer { - const Ref<Font> &font; - Color outline_color; - bool has_outline; - - struct PendingDraw { - RID canvas_item; - Point2 pos; - char32_t chr; - char32_t next; - Color modulate; - }; + void set_distance_field_hint(bool p_distance_field); + bool get_distance_field_hint() const; - Vector<PendingDraw> pending_draws; + void set_force_autohinter(bool p_enabeld); + bool get_force_autohinter() const; -public: - FontDrawer(const Ref<Font> &p_font, const Color &p_outline_color) : - font(p_font), - outline_color(p_outline_color) { - has_outline = p_font->has_outline(); - } - - float draw_char(RID p_canvas_item, const Point2 &p_pos, char32_t p_char, char32_t p_next = 0, const Color &p_modulate = Color(1, 1, 1)) { - if (has_outline) { - PendingDraw draw = { p_canvas_item, p_pos, p_char, p_next, p_modulate }; - pending_draws.push_back(draw); - } - return font->draw_char(p_canvas_item, p_pos, p_char, p_next, has_outline ? outline_color : p_modulate, has_outline); - } - - ~FontDrawer() { - for (int i = 0; i < pending_draws.size(); ++i) { - const PendingDraw &draw = pending_draws[i]; - font->draw_char(draw.canvas_item, draw.pos, draw.chr, draw.next, draw.modulate, false); - } - } -}; + void set_hinting(TextServer::Hinting p_hinting); + TextServer::Hinting get_hinting() const; -class BitmapFont : public Font { - GDCLASS(BitmapFont, Font); - RES_BASE_EXTENSION("font"); + bool has_char(char32_t p_char) const; + String get_supported_chars() const; - Vector<Ref<Texture2D>> textures; + Vector2 get_glyph_advance(uint32_t p_index, int p_size) const; + Vector2 get_glyph_kerning(uint32_t p_index_a, uint32_t p_index_b, int p_size) const; -public: - struct Character { - int texture_idx; - Rect2 rect; - float v_align; - float h_align; - float advance; - - Character() { - texture_idx = 0; - v_align = 0; - } - }; + bool has_outline() const; + float get_base_size() const; - struct KerningPairKey { - union { - struct { - uint32_t A, B; - }; + bool is_language_supported(const String &p_language) const; + void set_language_support_override(const String &p_language, bool p_supported); + bool get_language_support_override(const String &p_language) const; + void remove_language_support_override(const String &p_language); + Vector<String> get_language_support_overrides() const; - uint64_t pair; - }; + bool is_script_supported(const String &p_script) const; + void set_script_support_override(const String &p_script, bool p_supported); + bool get_script_support_override(const String &p_script) const; + void remove_script_support_override(const String &p_script); + Vector<String> get_script_support_overrides() const; - _FORCE_INLINE_ bool operator<(const KerningPairKey &p_r) const { return pair < p_r.pair; } + uint32_t get_glyph_index(char32_t p_char, char32_t p_variation_selector = 0x0000) const; + + Vector2 draw_glyph(RID p_canvas, int p_size, const Vector2 &p_pos, uint32_t p_index, const Color &p_color = Color(1, 1, 1)) const; + 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 = Color(1, 1, 1)) const; + + FontData(); + FontData(const String &p_filename, int p_base_size); + FontData(const PackedByteArray &p_data, const String &p_type, int p_base_size); + + ~FontData(); +}; + +/*************************************************************************/ + +class TextLine; +class TextParagraph; + +class Font : public Resource { + GDCLASS(Font, Resource); + +public: + enum SpacingType { + SPACING_TOP, + SPACING_BOTTOM }; private: - HashMap<char32_t, Character> char_map; - Map<KerningPairKey, int> kerning_map; - - float height; - float ascent; - bool distance_field_hint; + int spacing_top = 0; + int spacing_bottom = 0; - void _set_chars(const Vector<int> &p_chars); - Vector<int> _get_chars() const; - void _set_kernings(const Vector<int> &p_kernings); - Vector<int> _get_kernings() const; - void _set_textures(const Vector<Variant> &p_textures); - Vector<Variant> _get_textures() const; + mutable LRUCache<uint64_t, Ref<TextLine>> cache; + mutable LRUCache<uint64_t, Ref<TextParagraph>> cache_wrap; - Ref<BitmapFont> fallback; + Vector<Ref<FontData>> data; protected: static void _bind_methods(); -public: - Error create_from_fnt(const String &p_file); + bool _set(const StringName &p_name, const Variant &p_value); + bool _get(const StringName &p_name, Variant &r_ret) const; + void _get_property_list(List<PropertyInfo> *p_list) const; - void set_height(float p_height); - float get_height() const override; + void _data_changed(); - void set_ascent(float p_ascent); - float get_ascent() const override; - float get_descent() const override; - float get_underline_position() const override; - float get_underline_thickness() const override; +public: + Dictionary get_feature_list() const; - void add_texture(const Ref<Texture2D> &p_texture); - void add_char(char32_t p_char, int p_texture_idx, const Rect2 &p_rect, const Size2 &p_align, float p_advance = -1); + // Font data control. + void add_data(const Ref<FontData> &p_data); + void set_data(int p_idx, const Ref<FontData> &p_data); + int get_data_count() const; + Ref<FontData> get_data(int p_idx) const; + void remove_data(int p_idx); - int get_character_count() const; - Vector<char32_t> get_char_keys() const; - Character get_character(char32_t p_char) const; + float get_height(int p_size = -1) const; + float get_ascent(int p_size = -1) const; + float get_descent(int p_size = -1) const; - int get_texture_count() const; - Ref<Texture2D> get_texture(int p_idx) const; + float get_underline_position(int p_size = -1) const; + float get_underline_thickness(int p_size = -1) const; - void add_kerning_pair(char32_t p_A, char32_t p_B, int p_kerning); - int get_kerning_pair(char32_t p_A, char32_t p_B) const; - Vector<KerningPairKey> get_kerning_pair_keys() const; + int get_spacing(int p_type) const; + void set_spacing(int p_type, int p_value); - Size2 get_char_size(char32_t p_char, char32_t p_next = 0) const override; + // Drawing string. + Size2 get_string_size(const String &p_text, int p_size = -1) const; + Size2 get_multiline_string_size(const String &p_text, float p_width = -1, int p_size = -1, uint8_t p_flags = TextServer::BREAK_MANDATORY | TextServer::BREAK_WORD_BOUND) const; - void set_fallback(const Ref<BitmapFont> &p_fallback); - Ref<BitmapFont> get_fallback() const; + void draw_string(RID p_canvas_item, const Point2 &p_pos, const String &p_text, HAlign p_align = HALIGN_LEFT, float p_width = -1, int p_size = -1, const Color &p_modulate = Color(1, 1, 1), int p_outline_size = 0, const Color &p_outline_modulate = Color(1, 1, 1, 0), uint8_t p_flags = TextServer::JUSTIFICATION_KASHIDA | TextServer::JUSTIFICATION_WORD_BOUND) const; + void draw_multiline_string(RID p_canvas_item, const Point2 &p_pos, const String &p_text, HAlign p_align = HALIGN_LEFT, float p_width = -1, int p_max_lines = -1, int p_size = -1, const Color &p_modulate = Color(1, 1, 1), int p_outline_size = 0, const Color &p_outline_modulate = Color(1, 1, 1, 0), uint8_t p_flags = TextServer::BREAK_MANDATORY | TextServer::BREAK_WORD_BOUND | TextServer::JUSTIFICATION_KASHIDA | TextServer::JUSTIFICATION_WORD_BOUND) const; - void clear(); + // Helper functions. + bool has_char(char32_t p_char) const; + String get_supported_chars() const; - void set_distance_field_hint(bool p_distance_field); - bool is_distance_field_hint() const override; + Size2 get_char_size(char32_t p_char, char32_t p_next = 0, int p_size = -1) const; + float draw_char(RID p_canvas_item, const Point2 &p_pos, char32_t p_char, char32_t p_next = 0, int p_size = -1, const Color &p_modulate = Color(1, 1, 1), int p_outline_size = 0, const Color &p_outline_modulate = Color(1, 1, 1, 0)) const; - float draw_char(RID p_canvas_item, const Point2 &p_pos, char32_t p_char, char32_t p_next = 0, const Color &p_modulate = Color(1, 1, 1), bool p_outline = false) const override; + Vector<RID> get_rids() const; + + void update_changes(); - BitmapFont(); - ~BitmapFont(); + Font(); + ~Font(); }; -class ResourceFormatLoaderBMFont : public ResourceFormatLoader { +VARIANT_ENUM_CAST(Font::SpacingType); + +/*************************************************************************/ + +class ResourceFormatLoaderFont : public ResourceFormatLoader { public: virtual RES load(const String &p_path, const String &p_original_path = "", Error *r_error = nullptr, bool p_use_sub_threads = false, float *r_progress = nullptr, bool p_no_cache = false); virtual void get_recognized_extensions(List<String> *p_extensions) const; |