summaryrefslogtreecommitdiff
path: root/scene/resources/font.h
diff options
context:
space:
mode:
Diffstat (limited to 'scene/resources/font.h')
-rw-r--r--scene/resources/font.h286
1 files changed, 166 insertions, 120 deletions
diff --git a/scene/resources/font.h b/scene/resources/font.h
index 200373aa8c..9a34edce64 100644
--- a/scene/resources/font.h
+++ b/scene/resources/font.h
@@ -41,17 +41,27 @@
class FontData : public Resource {
GDCLASS(FontData, Resource);
+ RES_BASE_EXTENSION("fontdata");
-public:
- enum SpacingType {
- SPACING_GLYPH,
- SPACING_SPACE,
- };
+ // Font source data.
+ const uint8_t *data_ptr = nullptr;
+ size_t data_size = 0;
+ PackedByteArray data;
-private:
- RID rid;
- int base_size = 16;
- String path;
+ bool antialiased = true;
+ bool msdf = false;
+ int msdf_pixel_range = 16;
+ int msdf_size = 48;
+ int fixed_size = 0;
+ bool force_autohinter = false;
+ TextServer::Hinting hinting = TextServer::HINTING_LIGHT;
+ real_t oversampling = 0.f;
+
+ // Cache.
+ mutable Vector<RID> cache;
+
+ _FORCE_INLINE_ void _clear_cache();
+ _FORCE_INLINE_ void _ensure_rid(int p_cache_index) const;
protected:
static void _bind_methods();
@@ -63,79 +73,132 @@ protected:
virtual void reset_state() override;
public:
- virtual RID get_rid() const override;
+ // Font source data.
+ virtual void set_data_ptr(const uint8_t *p_data, size_t p_size);
+ virtual void set_data(const PackedByteArray &p_data);
+ virtual PackedByteArray get_data() const;
- 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);
+ // Common properties.
+ virtual void set_antialiased(bool p_antialiased);
+ virtual bool is_antialiased() const;
- void new_bitmap(float p_height, float p_ascent, int p_base_size = 16);
+ virtual void set_multichannel_signed_distance_field(bool p_msdf);
+ virtual bool is_multichannel_signed_distance_field() const;
- void bitmap_add_texture(const Ref<Texture> &p_texture);
- void bitmap_add_char(char32_t p_char, int p_texture_idx, const Rect2 &p_rect, const Size2 &p_align, float p_advance);
- void bitmap_add_kerning_pair(char32_t p_A, char32_t p_B, int p_kerning);
+ virtual void set_msdf_pixel_range(int p_msdf_pixel_range);
+ virtual int get_msdf_pixel_range() const;
- void set_data_path(const String &p_path);
- String get_data_path() const;
+ virtual void set_msdf_size(int p_msdf_size);
+ virtual int get_msdf_size() const;
- float get_height(int p_size) const;
- float get_ascent(int p_size) const;
- float get_descent(int p_size) const;
+ virtual void set_fixed_size(int p_fixed_size);
+ virtual int get_fixed_size() const;
- Dictionary get_feature_list() const;
- Dictionary get_variation_list() const;
+ virtual void set_force_autohinter(bool p_force_autohinter);
+ virtual bool is_force_autohinter() const;
- void set_variation(const String &p_name, double p_value);
- double get_variation(const String &p_name) const;
+ virtual void set_hinting(TextServer::Hinting p_hinting);
+ virtual TextServer::Hinting get_hinting() const;
- float get_underline_position(int p_size) const;
- float get_underline_thickness(int p_size) const;
+ virtual void set_oversampling(real_t p_oversampling);
+ virtual real_t get_oversampling() const;
- int get_spacing(int p_type) const;
- void set_spacing(int p_type, int p_value);
+ // Cache.
+ virtual RID find_cache(const Dictionary &p_variation_coordinates) const;
- void set_antialiased(bool p_antialiased);
- bool get_antialiased() const;
+ virtual int get_cache_count() const;
+ virtual void clear_cache();
+ virtual void remove_cache(int p_cache_index);
- void set_distance_field_hint(bool p_distance_field);
- bool get_distance_field_hint() const;
+ virtual Array get_size_cache_list(int p_cache_index) const;
+ virtual void clear_size_cache(int p_cache_index);
+ virtual void remove_size_cache(int p_cache_index, const Vector2i &p_size);
- void set_force_autohinter(bool p_enabeld);
- bool get_force_autohinter() const;
+ virtual void set_variation_coordinates(int p_cache_index, const Dictionary &p_variation_coordinates);
+ virtual Dictionary get_variation_coordinates(int p_cache_index) const;
- void set_hinting(TextServer::Hinting p_hinting);
- TextServer::Hinting get_hinting() const;
+ virtual void set_ascent(int p_cache_index, int p_size, real_t p_ascent);
+ virtual real_t get_ascent(int p_cache_index, int p_size) const;
- bool has_char(char32_t p_char) const;
- String get_supported_chars() const;
+ virtual void set_descent(int p_cache_index, int p_size, real_t p_descent);
+ virtual real_t get_descent(int p_cache_index, int p_size) const;
- 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;
+ virtual void set_underline_position(int p_cache_index, int p_size, real_t p_underline_position);
+ virtual real_t get_underline_position(int p_cache_index, int p_size) const;
- bool has_outline() const;
- float get_base_size() const;
+ virtual void set_underline_thickness(int p_cache_index, int p_size, real_t p_underline_thickness);
+ virtual real_t get_underline_thickness(int p_cache_index, int p_size) const;
- 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;
+ virtual void set_scale(int p_cache_index, int p_size, real_t p_scale); // Rendering scale for bitmap fonts (e.g. emoji fonts).
+ virtual real_t get_scale(int p_cache_index, int p_size) const;
- 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;
+ virtual void set_spacing(int p_cache_index, int p_size, TextServer::SpacingType p_spacing, int p_value);
+ virtual int get_spacing(int p_cache_index, int p_size, TextServer::SpacingType p_spacing) const;
- uint32_t get_glyph_index(char32_t p_char, char32_t p_variation_selector = 0x0000) const;
+ virtual int get_texture_count(int p_cache_index, const Vector2i &p_size) const;
+ virtual void clear_textures(int p_cache_index, const Vector2i &p_size);
+ virtual void remove_texture(int p_cache_index, const Vector2i &p_size, int p_texture_index);
- 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;
+ virtual void set_texture_image(int p_cache_index, const Vector2i &p_size, int p_texture_index, const Ref<Image> &p_image);
+ virtual Ref<Image> get_texture_image(int p_cache_index, const Vector2i &p_size, int p_texture_index) const;
- FontData();
- FontData(const String &p_filename, int p_base_size);
- FontData(const PackedByteArray &p_data, const String &p_type, int p_base_size);
+ virtual void set_texture_offsets(int p_cache_index, const Vector2i &p_size, int p_texture_index, const PackedInt32Array &p_offset);
+ virtual PackedInt32Array get_texture_offsets(int p_cache_index, const Vector2i &p_size, int p_texture_index) const;
+
+ virtual Array get_glyph_list(int p_cache_index, const Vector2i &p_size) const;
+ virtual void clear_glyphs(int p_cache_index, const Vector2i &p_size);
+ virtual void remove_glyph(int p_cache_index, const Vector2i &p_size, int32_t p_glyph);
+
+ virtual void set_glyph_advance(int p_cache_index, int p_size, int32_t p_glyph, const Vector2 &p_advance);
+ virtual Vector2 get_glyph_advance(int p_cache_index, int p_size, int32_t p_glyph) const;
+
+ virtual void set_glyph_offset(int p_cache_index, const Vector2i &p_size, int32_t p_glyph, const Vector2 &p_offset);
+ virtual Vector2 get_glyph_offset(int p_cache_index, const Vector2i &p_size, int32_t p_glyph) const;
+
+ virtual void set_glyph_size(int p_cache_index, const Vector2i &p_size, int32_t p_glyph, const Vector2 &p_gl_size);
+ virtual Vector2 get_glyph_size(int p_cache_index, const Vector2i &p_size, int32_t p_glyph) const;
+
+ virtual void set_glyph_uv_rect(int p_cache_index, const Vector2i &p_size, int32_t p_glyph, const Rect2 &p_uv_rect);
+ virtual Rect2 get_glyph_uv_rect(int p_cache_index, const Vector2i &p_size, int32_t p_glyph) const;
+
+ virtual void set_glyph_texture_idx(int p_cache_index, const Vector2i &p_size, int32_t p_glyph, int p_texture_idx);
+ virtual int get_glyph_texture_idx(int p_cache_index, const Vector2i &p_size, int32_t p_glyph) const;
+ virtual Array get_kerning_list(int p_cache_index, int p_size) const;
+ virtual void clear_kerning_map(int p_cache_index, int p_size);
+ virtual void remove_kerning(int p_cache_index, int p_size, const Vector2i &p_glyph_pair);
+
+ virtual void set_kerning(int p_cache_index, int p_size, const Vector2i &p_glyph_pair, const Vector2 &p_kerning);
+ virtual Vector2 get_kerning(int p_cache_index, int p_size, const Vector2i &p_glyph_pair) const;
+
+ virtual void render_range(int p_cache_index, const Vector2i &p_size, char32_t p_start, char32_t p_end);
+ virtual void render_glyph(int p_cache_index, const Vector2i &p_size, int32_t p_index);
+
+ virtual RID get_cache_rid(int p_cache_index) const;
+
+ // Language/script support override.
+ virtual bool is_language_supported(const String &p_language) const;
+ virtual void set_language_support_override(const String &p_language, bool p_supported);
+ virtual bool get_language_support_override(const String &p_language) const;
+ virtual void remove_language_support_override(const String &p_language);
+ virtual Vector<String> get_language_support_overrides() const;
+
+ virtual bool is_script_supported(const String &p_script) const;
+ virtual void set_script_support_override(const String &p_script, bool p_supported);
+ virtual bool get_script_support_override(const String &p_script) const;
+ virtual void remove_script_support_override(const String &p_script);
+ virtual Vector<String> get_script_support_overrides() const;
+
+ // Base font properties.
+ virtual bool has_char(char32_t p_char) const;
+ virtual String get_supported_chars() const;
+
+ virtual int32_t get_glyph_index(int p_size, char32_t p_char, char32_t p_variation_selector = 0x0000) const;
+
+ virtual Dictionary get_supported_feature_list() const;
+ virtual Dictionary get_supported_variation_list() const;
+
+ FontData();
~FontData();
};
@@ -147,20 +210,22 @@ class TextParagraph;
class Font : public Resource {
GDCLASS(Font, Resource);
-public:
- enum SpacingType {
- SPACING_TOP,
- SPACING_BOTTOM,
- };
-
-private:
- int spacing_top = 0;
- int spacing_bottom = 0;
-
+ // Shaped string cache.
mutable LRUCache<uint64_t, Ref<TextLine>> cache;
mutable LRUCache<uint64_t, Ref<TextParagraph>> cache_wrap;
+ // Font data cache.
Vector<Ref<FontData>> data;
+ mutable Vector<RID> rids;
+
+ // Font config.
+ int base_size = 16;
+ Dictionary variation_coordinates;
+ int spacing_bottom = 0;
+ int spacing_top = 0;
+
+ _FORCE_INLINE_ void _data_changed();
+ _FORCE_INLINE_ void _ensure_rid(int p_index) const; // Find or create cache record.
protected:
static void _bind_methods();
@@ -171,41 +236,49 @@ protected:
virtual void reset_state() override;
- void _data_changed();
-
public:
Dictionary get_feature_list() const;
- // 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);
+ // Font data.
+ virtual void add_data(const Ref<FontData> &p_data);
+ virtual void set_data(int p_idx, const Ref<FontData> &p_data);
+ virtual int get_data_count() const;
+ virtual Ref<FontData> get_data(int p_idx) const;
+ virtual RID get_data_rid(int p_idx) const;
+ virtual void clear_data();
+ virtual void remove_data(int p_idx);
+
+ // Font configuration.
+ virtual void set_base_size(int p_size);
+ virtual int get_base_size() 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;
+ virtual void set_variation_coordinates(const Dictionary &p_variation_coordinates);
+ virtual Dictionary get_variation_coordinates() const;
- float get_underline_position(int p_size = -1) const;
- float get_underline_thickness(int p_size = -1) const;
+ virtual void set_spacing(TextServer::SpacingType p_spacing, int p_value);
+ virtual int get_spacing(TextServer::SpacingType p_spacing) const;
- int get_spacing(int p_type) const;
- void set_spacing(int p_type, int p_value);
+ // Font metrics.
+ virtual real_t get_height(int p_size = -1) const;
+ virtual real_t get_ascent(int p_size = -1) const;
+ virtual real_t get_descent(int p_size = -1) const;
+ virtual real_t get_underline_position(int p_size = -1) const;
+ virtual real_t get_underline_thickness(int p_size = -1) const;
// 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;
+ virtual Size2 get_string_size(const String &p_text, int p_size = -1, HAlign p_align = HALIGN_LEFT, real_t p_width = -1, uint8_t p_flags = TextServer::JUSTIFICATION_KASHIDA | TextServer::JUSTIFICATION_WORD_BOUND) const;
+ virtual Size2 get_multiline_string_size(const String &p_text, real_t p_width = -1, int p_size = -1, uint8_t p_flags = TextServer::BREAK_MANDATORY | TextServer::BREAK_WORD_BOUND) 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;
+ virtual void draw_string(RID p_canvas_item, const Point2 &p_pos, const String &p_text, HAlign p_align = HALIGN_LEFT, real_t 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;
+ virtual void draw_multiline_string(RID p_canvas_item, const Point2 &p_pos, const String &p_text, HAlign p_align = HALIGN_LEFT, real_t 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;
// Helper functions.
- bool has_char(char32_t p_char) const;
- String get_supported_chars() const;
+ virtual bool has_char(char32_t p_char) const;
+ virtual String get_supported_chars() const;
- 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;
+ // Drawing char.
+ virtual Size2 get_char_size(char32_t p_char, char32_t p_next = 0, int p_size = -1) const;
+ virtual real_t 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;
Vector<RID> get_rids() const;
@@ -215,31 +288,4 @@ public:
~Font();
};
-VARIANT_ENUM_CAST(FontData::SpacingType);
-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, CacheMode p_cache_mode = CACHE_MODE_REUSE);
- virtual void get_recognized_extensions_for_type(const String &p_type, List<String> *p_extensions) const;
- virtual void get_recognized_extensions(List<String> *p_extensions) const;
- virtual bool handles_type(const String &p_type) const;
- virtual String get_resource_type(const String &p_path) const;
-};
-
-#ifndef DISABLE_DEPRECATED
-
-class ResourceFormatLoaderCompatFont : 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, CacheMode p_cache_mode = CACHE_MODE_REUSE);
- virtual void get_recognized_extensions_for_type(const String &p_type, List<String> *p_extensions) const;
- virtual void get_recognized_extensions(List<String> *p_extensions) const;
- virtual bool handles_type(const String &p_type) const;
- virtual String get_resource_type(const String &p_path) const;
-};
-
-#endif /* DISABLE_DEPRECATED */
-
#endif /* FONT_H */