From ebbc25e89c1f3423b713fcc2df418e04df5743ef Mon Sep 17 00:00:00 2001 From: bruvzg <7645683+bruvzg@users.noreply.github.com> Date: Mon, 25 Oct 2021 11:49:18 +0300 Subject: [PATCH] Ignore empty Font resources as theme override. Add range hint to font_size properties. Remove excessive `base_size` Font property. --- doc/classes/CanvasItem.xml | 6 +- doc/classes/Font.xml | 25 +++-- editor/editor_fonts.cpp | 15 ++- editor/editor_properties.cpp | 3 +- editor/plugins/canvas_item_editor_plugin.cpp | 6 +- editor/plugins/theme_editor_preview.cpp | 5 +- editor/plugins/theme_editor_preview.h | 1 + editor/plugins/tiles/tile_data_editors.cpp | 15 +-- modules/text_server_adv/text_server_adv.cpp | 1 + modules/text_server_fb/text_server_fb.cpp | 1 + scene/gui/code_edit.cpp | 2 +- scene/gui/control.cpp | 16 +-- scene/gui/tree.cpp | 2 +- scene/main/canvas_item.cpp | 6 +- scene/main/canvas_item.h | 8 +- .../resources/default_theme/default_theme.cpp | 1 - scene/resources/font.cpp | 98 +++++++------------ scene/resources/font.h | 28 +++--- scene/resources/theme.cpp | 4 +- 19 files changed, 99 insertions(+), 144 deletions(-) diff --git a/doc/classes/CanvasItem.xml b/doc/classes/CanvasItem.xml index 738594ea5d9..7be18c1d5c4 100644 --- a/doc/classes/CanvasItem.xml +++ b/doc/classes/CanvasItem.xml @@ -53,7 +53,7 @@ - + @@ -146,7 +146,7 @@ - + @@ -238,7 +238,7 @@ - + diff --git a/doc/classes/Font.xml b/doc/classes/Font.xml index 39881e9eb72..01bc2837f14 100644 --- a/doc/classes/Font.xml +++ b/doc/classes/Font.xml @@ -82,7 +82,7 @@ - + @@ -99,7 +99,7 @@ - + @@ -116,7 +116,7 @@ - + @@ -128,7 +128,7 @@ - + Returns the average font ascent (number of pixels above the baseline). [b]Note:[/b] Real ascent of the string is context-dependent and can be significantly different from the value returned by this function. Use it only as rough estimate (e.g. as the ascent of empty line). @@ -138,7 +138,7 @@ - + Returns the size of a character, optionally taking kerning into account if the next character is provided. [b]Note:[/b] Do not use this function to calculate width of the string character by character, use [method get_string_size] or [TextLine] instead. The height returned is the font height (see also [method get_height]) and has no relation to the glyph height. @@ -166,7 +166,7 @@ - + Returns the average font descent (number of pixels below the baseline). [b]Note:[/b] Real descent of the string is context-dependent and can be significantly different from the value returned by this function. Use it only as rough estimate (e.g. as the descent of empty line). @@ -174,7 +174,7 @@ - + Returns the total average font height (ascent plus descent) in pixels. [b]Note:[/b] Real height of the string is context-dependent and can be significantly different from the value returned by this function. Use it only as rough estimate (e.g. as the height of empty line). @@ -184,7 +184,7 @@ - + Returns the size of a bounding box of a string broken into the lines, taking kerning and advance into account. @@ -201,7 +201,7 @@ - + @@ -220,7 +220,7 @@ - + Return average pixel offset of the underline below the baseline. [b]Note:[/b] Real underline position of the string is context-dependent and can be significantly different from the value returned by this function. Use it only as rough estimate. @@ -228,7 +228,7 @@ - + Return average thickness of the underline. [b]Note:[/b] Real underline thickness of the string is context-dependent and can be significantly different from the value returned by this function. Use it only as rough estimate. @@ -272,9 +272,6 @@ - - Default font size. - Extra spacing at the bottom of the line in pixels. diff --git a/editor/editor_fonts.cpp b/editor/editor_fonts.cpp index e5d6315ef7f..a644e3e991a 100644 --- a/editor/editor_fonts.cpp +++ b/editor/editor_fonts.cpp @@ -67,7 +67,7 @@ m_name->add_data(FontJapanese); \ m_name->add_data(FontFallback); -#define MAKE_DEFAULT_FONT(m_name, m_variations, m_base_size) \ +#define MAKE_DEFAULT_FONT(m_name, m_variations) \ Ref m_name; \ m_name.instantiate(); \ if (CustomFont.is_valid()) { \ @@ -89,12 +89,11 @@ } \ m_name->set_variation_coordinates(variations); \ } \ - m_name->set_base_size(m_base_size); \ m_name->set_spacing(TextServer::SPACING_TOP, -EDSCALE); \ m_name->set_spacing(TextServer::SPACING_BOTTOM, -EDSCALE); \ MAKE_FALLBACKS(m_name); -#define MAKE_BOLD_FONT(m_name, m_variations, m_base_size) \ +#define MAKE_BOLD_FONT(m_name, m_variations) \ Ref m_name; \ m_name.instantiate(); \ if (CustomFontBold.is_valid()) { \ @@ -116,12 +115,11 @@ } \ m_name->set_variation_coordinates(variations); \ } \ - m_name->set_base_size(m_base_size); \ m_name->set_spacing(TextServer::SPACING_TOP, -EDSCALE); \ m_name->set_spacing(TextServer::SPACING_BOTTOM, -EDSCALE); \ MAKE_FALLBACKS_BOLD(m_name); -#define MAKE_SOURCE_FONT(m_name, m_variations, m_base_size) \ +#define MAKE_SOURCE_FONT(m_name, m_variations) \ Ref m_name; \ m_name.instantiate(); \ if (CustomFontSource.is_valid()) { \ @@ -143,7 +141,6 @@ } \ m_name->set_variation_coordinates(variations); \ } \ - m_name->set_base_size(m_base_size); \ m_name->set_spacing(TextServer::SPACING_TOP, -EDSCALE); \ m_name->set_spacing(TextServer::SPACING_BOTTOM, -EDSCALE); \ MAKE_FALLBACKS(m_name); @@ -275,7 +272,7 @@ void editor_register_fonts(Ref p_theme) { Ref dfmono = load_cached_internal_font(_font_Hack_Regular, _font_Hack_Regular_size, font_hinting, font_antialiased, true); // Default font - MAKE_DEFAULT_FONT(df, String(), default_font_size); + MAKE_DEFAULT_FONT(df, String()); p_theme->set_default_theme_font(df); // Default theme font p_theme->set_default_theme_font_size(default_font_size); @@ -283,7 +280,7 @@ void editor_register_fonts(Ref p_theme) { p_theme->set_font("main", "EditorFonts", df); // Bold font - MAKE_BOLD_FONT(df_bold, String(), default_font_size); + MAKE_BOLD_FONT(df_bold, String()); p_theme->set_font_size("bold_size", "EditorFonts", default_font_size); p_theme->set_font("bold", "EditorFonts", df_bold); @@ -310,7 +307,7 @@ void editor_register_fonts(Ref p_theme) { // Documentation fonts String code_font_custom_variations = EditorSettings::get_singleton()->get("interface/editor/code_font_custom_variations"); - MAKE_SOURCE_FONT(df_code, code_font_custom_variations, default_font_size); + MAKE_SOURCE_FONT(df_code, code_font_custom_variations); p_theme->set_font_size("doc_size", "EditorFonts", int(EDITOR_GET("text_editor/help/help_font_size")) * EDSCALE); p_theme->set_font("doc", "EditorFonts", df); p_theme->set_font_size("doc_bold_size", "EditorFonts", int(EDITOR_GET("text_editor/help/help_font_size")) * EDSCALE); diff --git a/editor/editor_properties.cpp b/editor/editor_properties.cpp index 2d4a3ac7884..6a36304ba42 100644 --- a/editor/editor_properties.cpp +++ b/editor/editor_properties.cpp @@ -884,10 +884,11 @@ public: flag_rects.push_back(rect2); Ref font = get_theme_font(SNAME("font"), SNAME("Label")); + int font_size = get_theme_font_size(SNAME("font_size"), SNAME("Label")); Vector2 offset; offset.y = rect2.size.y * 0.75; - draw_string(font, rect2.position + offset, itos(layer_index + 1), HALIGN_CENTER, rect2.size.x, -1, on ? text_color_on : text_color); + draw_string(font, rect2.position + offset, itos(layer_index + 1), HALIGN_CENTER, rect2.size.x, font_size, on ? text_color_on : text_color); ofs.x += bsize + 1; diff --git a/editor/plugins/canvas_item_editor_plugin.cpp b/editor/plugins/canvas_item_editor_plugin.cpp index 8935f715e6a..5544d9261e2 100644 --- a/editor/plugins/canvas_item_editor_plugin.cpp +++ b/editor/plugins/canvas_item_editor_plugin.cpp @@ -3565,7 +3565,7 @@ void CanvasItemEditor::_draw_hover() { Ref font = get_theme_font(SNAME("font"), SNAME("Label")); int font_size = get_theme_font_size(SNAME("font_size"), SNAME("Label")); - Size2 node_name_size = font->get_string_size(node_name); + Size2 node_name_size = font->get_string_size(node_name, font_size); Size2 item_size = Size2(node_icon->get_size().x + 4 + node_name_size.x, MAX(node_icon->get_size().y, node_name_size.y - 3)); Point2 pos = transform.xform(hovering_results[i].position) - Point2(0, item_size.y) + (Point2(node_icon->get_size().x, -node_icon->get_size().y) / 4); @@ -4777,10 +4777,6 @@ void CanvasItemEditor::_popup_callback(int p_op) { if (key_pos) { ctrl->set_position(Point2()); } - /* - if (key_scale) - AnimationPlayerEditor::get_singleton()->get_track_editor()->insert_node_value_key(ctrl,"rect/size",ctrl->get_size()); - */ } } diff --git a/editor/plugins/theme_editor_preview.cpp b/editor/plugins/theme_editor_preview.cpp index d26d0ec39d7..e13a10fe3f7 100644 --- a/editor/plugins/theme_editor_preview.cpp +++ b/editor/plugins/theme_editor_preview.cpp @@ -117,7 +117,7 @@ void ThemeEditorPreview::_draw_picker_overlay() { } Rect2 highlight_label_rect = highlight_rect; - highlight_label_rect.size = theme_cache.preview_picker_font->get_string_size(highlight_name); + highlight_label_rect.size = theme_cache.preview_picker_font->get_string_size(highlight_name, theme_cache.font_size); int margin_top = theme_cache.preview_picker_label->get_margin(SIDE_TOP); int margin_left = theme_cache.preview_picker_label->get_margin(SIDE_LEFT); @@ -133,7 +133,7 @@ void ThemeEditorPreview::_draw_picker_overlay() { Point2 label_pos = highlight_label_rect.position; label_pos.y += highlight_label_rect.size.y - margin_bottom; label_pos.x += margin_left; - picker_overlay->draw_string(theme_cache.preview_picker_font, label_pos, highlight_name); + picker_overlay->draw_string(theme_cache.preview_picker_font, label_pos, highlight_name, HALIGN_LEFT, -1, theme_cache.font_size); } } @@ -188,6 +188,7 @@ void ThemeEditorPreview::_notification(int p_what) { theme_cache.preview_picker_overlay_color = get_theme_color(SNAME("preview_picker_overlay_color"), SNAME("ThemeEditor")); theme_cache.preview_picker_label = get_theme_stylebox(SNAME("preview_picker_label"), SNAME("ThemeEditor")); theme_cache.preview_picker_font = get_theme_font(SNAME("status_source"), SNAME("EditorFonts")); + theme_cache.font_size = get_theme_font_size(SNAME("font_size"), SNAME("EditorFonts")); } break; case NOTIFICATION_PROCESS: { time_left -= get_process_delta_time(); diff --git a/editor/plugins/theme_editor_preview.h b/editor/plugins/theme_editor_preview.h index 4e1b149e701..f9731192570 100644 --- a/editor/plugins/theme_editor_preview.h +++ b/editor/plugins/theme_editor_preview.h @@ -65,6 +65,7 @@ class ThemeEditorPreview : public VBoxContainer { Color preview_picker_overlay_color; Ref preview_picker_label; Ref preview_picker_font; + int font_size = 16; } theme_cache; double time_left = 0; diff --git a/editor/plugins/tiles/tile_data_editors.cpp b/editor/plugins/tiles/tile_data_editors.cpp index 104f46f7738..e9d80bb4b82 100644 --- a/editor/plugins/tiles/tile_data_editors.cpp +++ b/editor/plugins/tiles/tile_data_editors.cpp @@ -987,6 +987,7 @@ void TileDataDefaultEditor::draw_over_tile(CanvasItem *p_canvas_item, Transform2 p_canvas_item->draw_rect(rect, value); } else { Ref font = TileSetEditor::get_singleton()->get_theme_font(SNAME("bold"), SNAME("EditorFonts")); + int font_size = TileSetEditor::get_singleton()->get_theme_font_size(SNAME("bold_size"), SNAME("EditorFonts")); String text; switch (value.get_type()) { case Variant::INT: @@ -1018,8 +1019,8 @@ void TileDataDefaultEditor::draw_over_tile(CanvasItem *p_canvas_item, Transform2 } } - Vector2 string_size = font->get_string_size(text); - p_canvas_item->draw_string(font, p_transform.get_origin() + Vector2i(-string_size.x / 2, string_size.y / 2), text, HALIGN_CENTER, string_size.x, -1, color, 1, Color(0, 0, 0, 1)); + Vector2 string_size = font->get_string_size(text, font_size); + p_canvas_item->draw_string(font, p_transform.get_origin() + Vector2i(-string_size.x / 2, string_size.y / 2), text, HALIGN_CENTER, string_size.x, font_size, color, 1, Color(0, 0, 0, 1)); } } @@ -1582,6 +1583,7 @@ void TileDataTerrainsEditor::forward_draw_over_atlas(TileAtlasView *p_tile_atlas // Dim terrains with wrong terrain set. Ref font = TileSetEditor::get_singleton()->get_theme_font(SNAME("bold"), SNAME("EditorFonts")); + int font_size = TileSetEditor::get_singleton()->get_theme_font_size(SNAME("bold_size"), SNAME("EditorFonts")); for (int i = 0; i < p_tile_set_atlas_source->get_tiles_count(); i++) { Vector2i coords = p_tile_set_atlas_source->get_tile_id(i); if (coords != hovered_coords) { @@ -1604,8 +1606,8 @@ void TileDataTerrainsEditor::forward_draw_over_atlas(TileAtlasView *p_tile_atlas } else { text = "-"; } - Vector2 string_size = font->get_string_size(text); - p_canvas_item->draw_string(font, p_transform.xform(position) + Vector2i(-string_size.x / 2, string_size.y / 2), text, HALIGN_CENTER, string_size.x, -1, color, 1, Color(0, 0, 0, 1)); + Vector2 string_size = font->get_string_size(text, font_size); + p_canvas_item->draw_string(font, p_transform.xform(position) + Vector2i(-string_size.x / 2, string_size.y / 2), text, HALIGN_CENTER, string_size.x, font_size, color, 1, Color(0, 0, 0, 1)); } } } @@ -1755,6 +1757,7 @@ void TileDataTerrainsEditor::forward_draw_over_alternatives(TileAtlasView *p_til // Dim terrains with wrong terrain set. Ref font = TileSetEditor::get_singleton()->get_theme_font(SNAME("bold"), SNAME("EditorFonts")); + int font_size = TileSetEditor::get_singleton()->get_theme_font_size(SNAME("bold_size"), SNAME("EditorFonts")); for (int i = 0; i < p_tile_set_atlas_source->get_tiles_count(); i++) { Vector2i coords = p_tile_set_atlas_source->get_tile_id(i); for (int j = 1; j < p_tile_set_atlas_source->get_alternative_tiles_count(coords); j++) { @@ -1779,8 +1782,8 @@ void TileDataTerrainsEditor::forward_draw_over_alternatives(TileAtlasView *p_til } else { text = "-"; } - Vector2 string_size = font->get_string_size(text); - p_canvas_item->draw_string(font, p_transform.xform(position) + Vector2i(-string_size.x / 2, string_size.y / 2), text, HALIGN_CENTER, string_size.x, -1, color, 1, Color(0, 0, 0, 1)); + Vector2 string_size = font->get_string_size(text, font_size); + p_canvas_item->draw_string(font, p_transform.xform(position) + Vector2i(-string_size.x / 2, string_size.y / 2), text, HALIGN_CENTER, string_size.x, font_size, color, 1, Color(0, 0, 0, 1)); } } } diff --git a/modules/text_server_adv/text_server_adv.cpp b/modules/text_server_adv/text_server_adv.cpp index e95369ead72..c4591412651 100644 --- a/modules/text_server_adv/text_server_adv.cpp +++ b/modules/text_server_adv/text_server_adv.cpp @@ -1211,6 +1211,7 @@ _FORCE_INLINE_ bool TextServerAdvanced::_ensure_glyph(FontDataAdvanced *p_font_d } _FORCE_INLINE_ bool TextServerAdvanced::_ensure_cache_for_size(FontDataAdvanced *p_font_data, const Vector2i &p_size) const { + ERR_FAIL_COND_V(p_size.x <= 0, false); if (p_font_data->cache.has(p_size)) { return true; } diff --git a/modules/text_server_fb/text_server_fb.cpp b/modules/text_server_fb/text_server_fb.cpp index 193588c4ed0..f598eb80ea2 100644 --- a/modules/text_server_fb/text_server_fb.cpp +++ b/modules/text_server_fb/text_server_fb.cpp @@ -670,6 +670,7 @@ _FORCE_INLINE_ bool TextServerFallback::_ensure_glyph(FontDataFallback *p_font_d } _FORCE_INLINE_ bool TextServerFallback::_ensure_cache_for_size(FontDataFallback *p_font_data, const Vector2i &p_size) const { + ERR_FAIL_COND_V(p_size.x <= 0, false); if (p_font_data->cache.has(p_size)) { return true; } diff --git a/scene/gui/code_edit.cpp b/scene/gui/code_edit.cpp index c1db684d9b0..046d256867b 100644 --- a/scene/gui/code_edit.cpp +++ b/scene/gui/code_edit.cpp @@ -129,7 +129,7 @@ void CodeEdit::_notification(int p_what) { } const int scroll_width = code_completion_options_count > code_completion_max_lines ? code_completion_scroll_width : 0; - const int code_completion_base_width = font->get_string_size(code_completion_base).width; + const int code_completion_base_width = font->get_string_size(code_completion_base, font_size).width; if (caret_pos.x - code_completion_base_width + code_completion_rect.size.width + scroll_width > get_size().width) { code_completion_rect.position.x = get_size().width - code_completion_rect.size.width - scroll_width; } else { diff --git a/scene/gui/control.cpp b/scene/gui/control.cpp index e4048fbf091..77a1efd021b 100644 --- a/scene/gui/control.cpp +++ b/scene/gui/control.cpp @@ -400,7 +400,7 @@ void Control::_get_property_list(List *p_list) const { usage |= PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_CHECKED; } - p_list->push_back(PropertyInfo(Variant::INT, "theme_override_font_sizes/" + E, PROPERTY_HINT_NONE, "", usage)); + p_list->push_back(PropertyInfo(Variant::INT, "theme_override_font_sizes/" + E, PROPERTY_HINT_RANGE, "1,256,1,or_greater", usage)); } } { @@ -985,7 +985,7 @@ Ref Control::get_theme_stylebox(const StringName &p_name, const String Ref Control::get_theme_font(const StringName &p_name, const StringName &p_theme_type) const { if (p_theme_type == StringName() || p_theme_type == get_class_name() || p_theme_type == data.theme_type_variation) { const Ref *font = data.font_override.getptr(p_name); - if (font) { + if (font && (*font)->get_data_count() > 0) { return *font; } } @@ -998,7 +998,7 @@ Ref Control::get_theme_font(const StringName &p_name, const StringName &p_ int Control::get_theme_font_size(const StringName &p_name, const StringName &p_theme_type) const { if (p_theme_type == StringName() || p_theme_type == get_class_name() || p_theme_type == data.theme_type_variation) { const int *font_size = data.font_size_override.getptr(p_name); - if (font_size) { + if (font_size && (*font_size) > 0) { return *font_size; } } @@ -2586,16 +2586,6 @@ void Control::warp_mouse(const Point2 &p_to_pos) { } bool Control::is_text_field() const { - /* - if (get_script_instance()) { - Variant v=p_point; - const Variant *p[2]={&v,&p_data}; - Callable::CallError ce; - Variant ret = get_script_instance()->call("is_text_field",p,2,ce); - if (ce.error==Callable::CallError::CALL_OK) - return ret; - } - */ return false; } diff --git a/scene/gui/tree.cpp b/scene/gui/tree.cpp index 992d3644644..1245a37c4d4 100644 --- a/scene/gui/tree.cpp +++ b/scene/gui/tree.cpp @@ -4037,7 +4037,7 @@ int Tree::get_column_minimum_width(int p_column) const { // Check if the visible title of the column is wider. if (show_column_titles) { - min_width = MAX(cache.font->get_string_size(columns[p_column].title).width + cache.bg->get_margin(SIDE_LEFT) + cache.bg->get_margin(SIDE_RIGHT), min_width); + min_width = MAX(cache.font->get_string_size(columns[p_column].title, cache.font_size).width + cache.bg->get_margin(SIDE_LEFT) + cache.bg->get_margin(SIDE_RIGHT), min_width); } if (!columns[p_column].clip_content) { diff --git a/scene/main/canvas_item.cpp b/scene/main/canvas_item.cpp index 916833c9a71..50656848395 100644 --- a/scene/main/canvas_item.cpp +++ b/scene/main/canvas_item.cpp @@ -892,9 +892,9 @@ void CanvasItem::_bind_methods() { ClassDB::bind_method(D_METHOD("draw_primitive", "points", "colors", "uvs", "texture", "width"), &CanvasItem::draw_primitive, DEFVAL(Ref()), DEFVAL(1.0)); ClassDB::bind_method(D_METHOD("draw_polygon", "points", "colors", "uvs", "texture"), &CanvasItem::draw_polygon, DEFVAL(PackedVector2Array()), DEFVAL(Ref())); ClassDB::bind_method(D_METHOD("draw_colored_polygon", "points", "color", "uvs", "texture"), &CanvasItem::draw_colored_polygon, DEFVAL(PackedVector2Array()), DEFVAL(Ref())); - ClassDB::bind_method(D_METHOD("draw_string", "font", "pos", "text", "align", "width", "size", "modulate", "outline_size", "outline_modulate", "flags"), &CanvasItem::draw_string, DEFVAL(HALIGN_LEFT), DEFVAL(-1), DEFVAL(-1), DEFVAL(Color(1, 1, 1)), DEFVAL(0), DEFVAL(Color(1, 1, 1, 0)), DEFVAL(TextServer::JUSTIFICATION_KASHIDA | TextServer::JUSTIFICATION_WORD_BOUND)); - ClassDB::bind_method(D_METHOD("draw_multiline_string", "font", "pos", "text", "align", "width", "max_lines", "size", "modulate", "outline_size", "outline_modulate", "flags"), &CanvasItem::draw_multiline_string, DEFVAL(HALIGN_LEFT), DEFVAL(-1), DEFVAL(-1), DEFVAL(-1), DEFVAL(Color(1, 1, 1)), DEFVAL(0), DEFVAL(Color(1, 1, 1, 0)), DEFVAL(TextServer::BREAK_MANDATORY | TextServer::BREAK_WORD_BOUND | TextServer::JUSTIFICATION_KASHIDA | TextServer::JUSTIFICATION_WORD_BOUND)); - ClassDB::bind_method(D_METHOD("draw_char", "font", "pos", "char", "next", "size", "modulate", "outline_size", "outline_modulate"), &CanvasItem::draw_char, DEFVAL(""), DEFVAL(-1), DEFVAL(Color(1, 1, 1)), DEFVAL(0), DEFVAL(Color(1, 1, 1, 0))); + ClassDB::bind_method(D_METHOD("draw_string", "font", "pos", "text", "align", "width", "size", "modulate", "outline_size", "outline_modulate", "flags"), &CanvasItem::draw_string, DEFVAL(HALIGN_LEFT), DEFVAL(-1), DEFVAL(Font::DEFAULT_FONT_SIZE), DEFVAL(Color(1, 1, 1)), DEFVAL(0), DEFVAL(Color(1, 1, 1, 0)), DEFVAL(TextServer::JUSTIFICATION_KASHIDA | TextServer::JUSTIFICATION_WORD_BOUND)); + ClassDB::bind_method(D_METHOD("draw_multiline_string", "font", "pos", "text", "align", "width", "max_lines", "size", "modulate", "outline_size", "outline_modulate", "flags"), &CanvasItem::draw_multiline_string, DEFVAL(HALIGN_LEFT), DEFVAL(-1), DEFVAL(-1), DEFVAL(Font::DEFAULT_FONT_SIZE), DEFVAL(Color(1, 1, 1)), DEFVAL(0), DEFVAL(Color(1, 1, 1, 0)), DEFVAL(TextServer::BREAK_MANDATORY | TextServer::BREAK_WORD_BOUND | TextServer::JUSTIFICATION_KASHIDA | TextServer::JUSTIFICATION_WORD_BOUND)); + ClassDB::bind_method(D_METHOD("draw_char", "font", "pos", "char", "next", "size", "modulate", "outline_size", "outline_modulate"), &CanvasItem::draw_char, DEFVAL(""), DEFVAL(Font::DEFAULT_FONT_SIZE), DEFVAL(Color(1, 1, 1)), DEFVAL(0), DEFVAL(Color(1, 1, 1, 0))); ClassDB::bind_method(D_METHOD("draw_mesh", "mesh", "texture", "transform", "modulate"), &CanvasItem::draw_mesh, DEFVAL(Transform2D()), DEFVAL(Color(1, 1, 1, 1))); ClassDB::bind_method(D_METHOD("draw_multimesh", "multimesh", "texture"), &CanvasItem::draw_multimesh); ClassDB::bind_method(D_METHOD("draw_set_transform", "position", "rotation", "scale"), &CanvasItem::draw_set_transform, DEFVAL(0.0), DEFVAL(Size2(1.0, 1.0))); diff --git a/scene/main/canvas_item.h b/scene/main/canvas_item.h index ba9f47119dc..04376ad8097 100644 --- a/scene/main/canvas_item.h +++ b/scene/main/canvas_item.h @@ -34,10 +34,10 @@ #include "scene/main/node.h" #include "scene/main/scene_tree.h" #include "scene/resources/canvas_item_material.h" +#include "scene/resources/font.h" #include "servers/text_server.h" class CanvasLayer; -class Font; class MultiMesh; class StyleBox; class Window; @@ -235,9 +235,9 @@ public: void draw_mesh(const Ref &p_mesh, const Ref &p_texture, const Transform2D &p_transform = Transform2D(), const Color &p_modulate = Color(1, 1, 1)); void draw_multimesh(const Ref &p_multimesh, const Ref &p_texture); - void draw_string(const Ref &p_font, 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), uint16_t p_flags = TextServer::JUSTIFICATION_KASHIDA | TextServer::JUSTIFICATION_WORD_BOUND) const; - void draw_multiline_string(const Ref &p_font, 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), uint16_t p_flags = TextServer::BREAK_MANDATORY | TextServer::BREAK_WORD_BOUND | TextServer::JUSTIFICATION_KASHIDA | TextServer::JUSTIFICATION_WORD_BOUND) const; - real_t draw_char(const Ref &p_font, const Point2 &p_pos, const String &p_char, const String &p_next = "", 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; + void draw_string(const Ref &p_font, const Point2 &p_pos, const String &p_text, HAlign p_align = HALIGN_LEFT, real_t p_width = -1, int p_size = Font::DEFAULT_FONT_SIZE, const Color &p_modulate = Color(1, 1, 1), int p_outline_size = 0, const Color &p_outline_modulate = Color(1, 1, 1, 0), uint16_t p_flags = TextServer::JUSTIFICATION_KASHIDA | TextServer::JUSTIFICATION_WORD_BOUND) const; + void draw_multiline_string(const Ref &p_font, 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 = Font::DEFAULT_FONT_SIZE, const Color &p_modulate = Color(1, 1, 1), int p_outline_size = 0, const Color &p_outline_modulate = Color(1, 1, 1, 0), uint16_t p_flags = TextServer::BREAK_MANDATORY | TextServer::BREAK_WORD_BOUND | TextServer::JUSTIFICATION_KASHIDA | TextServer::JUSTIFICATION_WORD_BOUND) const; + real_t draw_char(const Ref &p_font, const Point2 &p_pos, const String &p_char, const String &p_next = "", int p_size = Font::DEFAULT_FONT_SIZE, const Color &p_modulate = Color(1, 1, 1), int p_outline_size = 0, const Color &p_outline_modulate = Color(1, 1, 1, 0)) const; void draw_set_transform(const Point2 &p_offset, real_t p_rot = 0.0, const Size2 &p_scale = Size2(1.0, 1.0)); void draw_set_transform_matrix(const Transform2D &p_matrix); diff --git a/scene/resources/default_theme/default_theme.cpp b/scene/resources/default_theme/default_theme.cpp index 9fdfd493c10..ed093d7ea30 100644 --- a/scene/resources/default_theme/default_theme.cpp +++ b/scene/resources/default_theme/default_theme.cpp @@ -1030,7 +1030,6 @@ void make_default_theme(bool p_hidpi, Ref p_font) { dynamic_font_data.instantiate(); dynamic_font_data->set_data_ptr(_font_OpenSans_SemiBold, _font_OpenSans_SemiBold_size); dynamic_font->add_data(dynamic_font_data); - dynamic_font->set_base_size(default_font_size); default_font = dynamic_font; } diff --git a/scene/resources/font.cpp b/scene/resources/font.cpp index 04e2b0dc702..819ae957154 100644 --- a/scene/resources/font.cpp +++ b/scene/resources/font.cpp @@ -1072,10 +1072,6 @@ void Font::_bind_methods() { ClassDB::bind_method(D_METHOD("clear_data"), &Font::clear_data); ClassDB::bind_method(D_METHOD("remove_data", "idx"), &Font::remove_data); - ClassDB::bind_method(D_METHOD("set_base_size", "size"), &Font::set_base_size); - ClassDB::bind_method(D_METHOD("get_base_size"), &Font::get_base_size); - ADD_PROPERTY(PropertyInfo(Variant::INT, "base_size"), "set_base_size", "get_base_size"); - ClassDB::bind_method(D_METHOD("set_variation_coordinates", "variation_coordinates"), &Font::set_variation_coordinates); ClassDB::bind_method(D_METHOD("get_variation_coordinates"), &Font::get_variation_coordinates); ADD_PROPERTY(PropertyInfo(Variant::DICTIONARY, "variation_coordinates"), "set_variation_coordinates", "get_variation_coordinates"); @@ -1087,20 +1083,20 @@ void Font::_bind_methods() { ADD_PROPERTYI(PropertyInfo(Variant::INT, "spacing_top"), "set_spacing", "get_spacing", TextServer::SPACING_TOP); ADD_PROPERTYI(PropertyInfo(Variant::INT, "spacing_bottom"), "set_spacing", "get_spacing", TextServer::SPACING_BOTTOM); - ClassDB::bind_method(D_METHOD("get_height", "size"), &Font::get_height, DEFVAL(-1)); - ClassDB::bind_method(D_METHOD("get_ascent", "size"), &Font::get_ascent, DEFVAL(-1)); - ClassDB::bind_method(D_METHOD("get_descent", "size"), &Font::get_descent, DEFVAL(-1)); - ClassDB::bind_method(D_METHOD("get_underline_position", "size"), &Font::get_underline_position, DEFVAL(-1)); - ClassDB::bind_method(D_METHOD("get_underline_thickness", "size"), &Font::get_underline_thickness, DEFVAL(-1)); + ClassDB::bind_method(D_METHOD("get_height", "size"), &Font::get_height, DEFVAL(DEFAULT_FONT_SIZE)); + ClassDB::bind_method(D_METHOD("get_ascent", "size"), &Font::get_ascent, DEFVAL(DEFAULT_FONT_SIZE)); + ClassDB::bind_method(D_METHOD("get_descent", "size"), &Font::get_descent, DEFVAL(DEFAULT_FONT_SIZE)); + ClassDB::bind_method(D_METHOD("get_underline_position", "size"), &Font::get_underline_position, DEFVAL(DEFAULT_FONT_SIZE)); + ClassDB::bind_method(D_METHOD("get_underline_thickness", "size"), &Font::get_underline_thickness, DEFVAL(DEFAULT_FONT_SIZE)); - ClassDB::bind_method(D_METHOD("get_string_size", "text", "size", "align", "width", "flags"), &Font::get_string_size, DEFVAL(-1), DEFVAL(HALIGN_LEFT), DEFVAL(-1), DEFVAL(TextServer::JUSTIFICATION_KASHIDA | TextServer::JUSTIFICATION_WORD_BOUND)); - ClassDB::bind_method(D_METHOD("get_multiline_string_size", "text", "width", "size", "flags"), &Font::get_multiline_string_size, DEFVAL(-1), DEFVAL(-1), DEFVAL(TextServer::BREAK_MANDATORY | TextServer::BREAK_WORD_BOUND)); + ClassDB::bind_method(D_METHOD("get_string_size", "text", "size", "align", "width", "flags"), &Font::get_string_size, DEFVAL(DEFAULT_FONT_SIZE), DEFVAL(HALIGN_LEFT), DEFVAL(-1), DEFVAL(TextServer::JUSTIFICATION_KASHIDA | TextServer::JUSTIFICATION_WORD_BOUND)); + ClassDB::bind_method(D_METHOD("get_multiline_string_size", "text", "width", "size", "flags"), &Font::get_multiline_string_size, DEFVAL(-1), DEFVAL(DEFAULT_FONT_SIZE), DEFVAL(TextServer::BREAK_MANDATORY | TextServer::BREAK_WORD_BOUND)); - ClassDB::bind_method(D_METHOD("draw_string", "canvas_item", "pos", "text", "align", "width", "size", "modulate", "outline_size", "outline_modulate", "flags"), &Font::draw_string, DEFVAL(HALIGN_LEFT), DEFVAL(-1), DEFVAL(-1), DEFVAL(Color(1, 1, 1)), DEFVAL(0), DEFVAL(Color(1, 1, 1, 0)), DEFVAL(TextServer::JUSTIFICATION_KASHIDA | TextServer::JUSTIFICATION_WORD_BOUND)); - ClassDB::bind_method(D_METHOD("draw_multiline_string", "canvas_item", "pos", "text", "align", "width", "max_lines", "size", "modulate", "outline_size", "outline_modulate", "flags"), &Font::draw_multiline_string, DEFVAL(HALIGN_LEFT), DEFVAL(-1), DEFVAL(-1), DEFVAL(-1), DEFVAL(Color(1, 1, 1)), DEFVAL(0), DEFVAL(Color(1, 1, 1, 0)), DEFVAL(TextServer::BREAK_MANDATORY | TextServer::BREAK_WORD_BOUND | TextServer::JUSTIFICATION_KASHIDA | TextServer::JUSTIFICATION_WORD_BOUND)); + ClassDB::bind_method(D_METHOD("draw_string", "canvas_item", "pos", "text", "align", "width", "size", "modulate", "outline_size", "outline_modulate", "flags"), &Font::draw_string, DEFVAL(HALIGN_LEFT), DEFVAL(-1), DEFVAL(DEFAULT_FONT_SIZE), DEFVAL(Color(1, 1, 1)), DEFVAL(0), DEFVAL(Color(1, 1, 1, 0)), DEFVAL(TextServer::JUSTIFICATION_KASHIDA | TextServer::JUSTIFICATION_WORD_BOUND)); + ClassDB::bind_method(D_METHOD("draw_multiline_string", "canvas_item", "pos", "text", "align", "width", "max_lines", "size", "modulate", "outline_size", "outline_modulate", "flags"), &Font::draw_multiline_string, DEFVAL(HALIGN_LEFT), DEFVAL(-1), DEFVAL(-1), DEFVAL(DEFAULT_FONT_SIZE), DEFVAL(Color(1, 1, 1)), DEFVAL(0), DEFVAL(Color(1, 1, 1, 0)), DEFVAL(TextServer::BREAK_MANDATORY | TextServer::BREAK_WORD_BOUND | TextServer::JUSTIFICATION_KASHIDA | TextServer::JUSTIFICATION_WORD_BOUND)); - ClassDB::bind_method(D_METHOD("get_char_size", "char", "next", "size"), &Font::get_char_size, DEFVAL(0), DEFVAL(-1)); - ClassDB::bind_method(D_METHOD("draw_char", "canvas_item", "pos", "char", "next", "size", "modulate", "outline_size", "outline_modulate"), &Font::draw_char, DEFVAL(0), DEFVAL(-1), DEFVAL(Color(1, 1, 1)), DEFVAL(0), DEFVAL(Color(1, 1, 1, 0))); + ClassDB::bind_method(D_METHOD("get_char_size", "char", "next", "size"), &Font::get_char_size, DEFVAL(0), DEFVAL(DEFAULT_FONT_SIZE)); + ClassDB::bind_method(D_METHOD("draw_char", "canvas_item", "pos", "char", "next", "size", "modulate", "outline_size", "outline_modulate"), &Font::draw_char, DEFVAL(0), DEFVAL(DEFAULT_FONT_SIZE), DEFVAL(Color(1, 1, 1)), DEFVAL(0), DEFVAL(Color(1, 1, 1, 0))); ClassDB::bind_method(D_METHOD("has_char", "char"), &Font::has_char); ClassDB::bind_method(D_METHOD("get_supported_chars"), &Font::get_supported_chars); @@ -1195,7 +1191,6 @@ void Font::reset_state() { data.clear(); rids.clear(); - base_size = 16; variation_coordinates.clear(); spacing_bottom = 0; spacing_top = 0; @@ -1308,14 +1303,6 @@ void Font::remove_data(int p_idx) { notify_property_list_changed(); } -void Font::set_base_size(int p_size) { - base_size = p_size; -} - -int Font::get_base_size() const { - return base_size; -} - void Font::set_variation_coordinates(const Dictionary &p_variation_coordinates) { _data_changed(); variation_coordinates = p_variation_coordinates; @@ -1355,51 +1342,46 @@ int Font::get_spacing(TextServer::SpacingType p_spacing) const { } real_t Font::get_height(int p_size) const { - int size = (p_size <= 0) ? base_size : p_size; real_t ret = 0.f; for (int i = 0; i < data.size(); i++) { _ensure_rid(i); - ret = MAX(ret, TS->font_get_ascent(rids[i], size) + TS->font_get_descent(rids[i], size)); + ret = MAX(ret, TS->font_get_ascent(rids[i], p_size) + TS->font_get_descent(rids[i], p_size)); } return ret + spacing_bottom + spacing_top; } real_t Font::get_ascent(int p_size) const { - int size = (p_size <= 0) ? base_size : p_size; real_t ret = 0.f; for (int i = 0; i < data.size(); i++) { _ensure_rid(i); - ret = MAX(ret, TS->font_get_ascent(rids[i], size)); + ret = MAX(ret, TS->font_get_ascent(rids[i], p_size)); } return ret + spacing_top; } real_t Font::get_descent(int p_size) const { - int size = (p_size <= 0) ? base_size : p_size; real_t ret = 0.f; for (int i = 0; i < data.size(); i++) { _ensure_rid(i); - ret = MAX(ret, TS->font_get_descent(rids[i], size)); + ret = MAX(ret, TS->font_get_descent(rids[i], p_size)); } return ret + spacing_bottom; } real_t Font::get_underline_position(int p_size) const { - int size = (p_size <= 0) ? base_size : p_size; real_t ret = 0.f; for (int i = 0; i < data.size(); i++) { _ensure_rid(i); - ret = MAX(ret, TS->font_get_underline_position(rids[i], size)); + ret = MAX(ret, TS->font_get_underline_position(rids[i], p_size)); } return ret + spacing_top; } real_t Font::get_underline_thickness(int p_size) const { - int size = (p_size <= 0) ? base_size : p_size; real_t ret = 0.f; for (int i = 0; i < data.size(); i++) { _ensure_rid(i); - ret = MAX(ret, TS->font_get_underline_thickness(rids[i], size)); + ret = MAX(ret, TS->font_get_underline_thickness(rids[i], p_size)); } return ret; } @@ -1407,8 +1389,6 @@ real_t Font::get_underline_thickness(int p_size) const { Size2 Font::get_string_size(const String &p_text, int p_size, HAlign p_align, real_t p_width, uint16_t p_flags) const { ERR_FAIL_COND_V(data.is_empty(), Size2()); - int size = (p_size <= 0) ? base_size : p_size; - for (int i = 0; i < data.size(); i++) { _ensure_rid(i); } @@ -1418,14 +1398,14 @@ Size2 Font::get_string_size(const String &p_text, int p_size, HAlign p_align, re hash = hash_djb2_one_64(hash_djb2_one_float(p_width), hash); hash = hash_djb2_one_64(p_flags, hash); } - hash = hash_djb2_one_64(size, hash); + hash = hash_djb2_one_64(p_size, hash); Ref buffer; if (cache.has(hash)) { buffer = cache.get(hash); } else { buffer.instantiate(); - buffer->add_string(p_text, Ref(this), size, Dictionary(), TranslationServer::get_singleton()->get_tool_locale()); + buffer->add_string(p_text, Ref(this), p_size, Dictionary(), TranslationServer::get_singleton()->get_tool_locale()); cache.insert(hash, buffer); } return buffer->get_size(); @@ -1434,8 +1414,6 @@ Size2 Font::get_string_size(const String &p_text, int p_size, HAlign p_align, re Size2 Font::get_multiline_string_size(const String &p_text, real_t p_width, int p_size, uint16_t p_flags) const { ERR_FAIL_COND_V(data.is_empty(), Size2()); - int size = (p_size <= 0) ? base_size : p_size; - for (int i = 0; i < data.size(); i++) { _ensure_rid(i); } @@ -1443,14 +1421,14 @@ Size2 Font::get_multiline_string_size(const String &p_text, real_t p_width, int uint64_t hash = p_text.hash64(); uint64_t wrp_hash = hash_djb2_one_64(hash_djb2_one_float(p_width), hash); wrp_hash = hash_djb2_one_64(p_flags, wrp_hash); - wrp_hash = hash_djb2_one_64(size, wrp_hash); + wrp_hash = hash_djb2_one_64(p_size, wrp_hash); Ref lines_buffer; if (cache_wrap.has(wrp_hash)) { lines_buffer = cache_wrap.get(wrp_hash); } else { lines_buffer.instantiate(); - lines_buffer->add_string(p_text, Ref(this), size, Dictionary(), TranslationServer::get_singleton()->get_tool_locale()); + lines_buffer->add_string(p_text, Ref(this), p_size, Dictionary(), TranslationServer::get_singleton()->get_tool_locale()); lines_buffer->set_width(p_width); lines_buffer->set_flags(p_flags); cache_wrap.insert(wrp_hash, lines_buffer); @@ -1473,8 +1451,6 @@ Size2 Font::get_multiline_string_size(const String &p_text, real_t p_width, int void Font::draw_string(RID p_canvas_item, const Point2 &p_pos, const String &p_text, HAlign p_align, real_t p_width, int p_size, const Color &p_modulate, int p_outline_size, const Color &p_outline_modulate, uint16_t p_flags) const { ERR_FAIL_COND(data.is_empty()); - int size = (p_size <= 0) ? base_size : p_size; - for (int i = 0; i < data.size(); i++) { _ensure_rid(i); } @@ -1484,14 +1460,14 @@ void Font::draw_string(RID p_canvas_item, const Point2 &p_pos, const String &p_t hash = hash_djb2_one_64(hash_djb2_one_float(p_width), hash); hash = hash_djb2_one_64(p_flags, hash); } - hash = hash_djb2_one_64(size, hash); + hash = hash_djb2_one_64(p_size, hash); Ref buffer; if (cache.has(hash)) { buffer = cache.get(hash); } else { buffer.instantiate(); - buffer->add_string(p_text, Ref(this), size, Dictionary(), TranslationServer::get_singleton()->get_tool_locale()); + buffer->add_string(p_text, Ref(this), p_size, Dictionary(), TranslationServer::get_singleton()->get_tool_locale()); cache.insert(hash, buffer); } @@ -1515,8 +1491,6 @@ void Font::draw_string(RID p_canvas_item, const Point2 &p_pos, const String &p_t void Font::draw_multiline_string(RID p_canvas_item, const Point2 &p_pos, const String &p_text, HAlign p_align, float p_width, int p_max_lines, int p_size, const Color &p_modulate, int p_outline_size, const Color &p_outline_modulate, uint16_t p_flags) const { ERR_FAIL_COND(data.is_empty()); - int size = (p_size <= 0) ? base_size : p_size; - for (int i = 0; i < data.size(); i++) { _ensure_rid(i); } @@ -1524,14 +1498,14 @@ void Font::draw_multiline_string(RID p_canvas_item, const Point2 &p_pos, const S uint64_t hash = p_text.hash64(); uint64_t wrp_hash = hash_djb2_one_64(hash_djb2_one_float(p_width), hash); wrp_hash = hash_djb2_one_64(p_flags, wrp_hash); - wrp_hash = hash_djb2_one_64(size, wrp_hash); + wrp_hash = hash_djb2_one_64(p_size, wrp_hash); Ref lines_buffer; if (cache_wrap.has(wrp_hash)) { lines_buffer = cache_wrap.get(wrp_hash); } else { lines_buffer.instantiate(); - lines_buffer->add_string(p_text, Ref(this), size, Dictionary(), TranslationServer::get_singleton()->get_tool_locale()); + lines_buffer->add_string(p_text, Ref(this), p_size, Dictionary(), TranslationServer::get_singleton()->get_tool_locale()); lines_buffer->set_width(p_width); lines_buffer->set_flags(p_flags); cache_wrap.insert(wrp_hash, lines_buffer); @@ -1573,16 +1547,14 @@ void Font::draw_multiline_string(RID p_canvas_item, const Point2 &p_pos, const S } Size2 Font::get_char_size(char32_t p_char, char32_t p_next, int p_size) const { - int size = (p_size <= 0) ? base_size : p_size; - for (int i = 0; i < data.size(); i++) { _ensure_rid(i); if (data[i]->has_char(p_char)) { - int32_t glyph_a = TS->font_get_glyph_index(rids[i], size, p_char, 0); - Size2 ret = Size2(TS->font_get_glyph_advance(rids[i], size, glyph_a).x, TS->font_get_ascent(rids[i], size) + TS->font_get_descent(rids[i], size)); + int32_t glyph_a = TS->font_get_glyph_index(rids[i], p_size, p_char, 0); + Size2 ret = Size2(TS->font_get_glyph_advance(rids[i], p_size, glyph_a).x, TS->font_get_ascent(rids[i], p_size) + TS->font_get_descent(rids[i], p_size)); if ((p_next != 0) && data[i]->has_char(p_next)) { - int32_t glyph_b = TS->font_get_glyph_index(rids[i], size, p_next, 0); - ret.x -= TS->font_get_kerning(rids[i], size, Vector2i(glyph_a, glyph_b)).x; + int32_t glyph_b = TS->font_get_glyph_index(rids[i], p_size, p_next, 0); + ret.x -= TS->font_get_kerning(rids[i], p_size, Vector2i(glyph_a, glyph_b)).x; } return ret; } @@ -1591,22 +1563,20 @@ Size2 Font::get_char_size(char32_t p_char, char32_t p_next, int p_size) const { } real_t Font::draw_char(RID p_canvas_item, const Point2 &p_pos, char32_t p_char, char32_t p_next, int p_size, const Color &p_modulate, int p_outline_size, const Color &p_outline_modulate) const { - int size = (p_size <= 0) ? base_size : p_size; - for (int i = 0; i < data.size(); i++) { _ensure_rid(i); if (data[i]->has_char(p_char)) { - int32_t glyph_a = TS->font_get_glyph_index(rids[i], size, p_char, 0); - real_t ret = TS->font_get_glyph_advance(rids[i], size, glyph_a).x; + int32_t glyph_a = TS->font_get_glyph_index(rids[i], p_size, p_char, 0); + real_t ret = TS->font_get_glyph_advance(rids[i], p_size, glyph_a).x; if ((p_next != 0) && data[i]->has_char(p_next)) { - int32_t glyph_b = TS->font_get_glyph_index(rids[i], size, p_next, 0); - ret -= TS->font_get_kerning(rids[i], size, Vector2i(glyph_a, glyph_b)).x; + int32_t glyph_b = TS->font_get_glyph_index(rids[i], p_size, p_next, 0); + ret -= TS->font_get_kerning(rids[i], p_size, Vector2i(glyph_a, glyph_b)).x; } if (p_outline_size > 0 && p_outline_modulate.a != 0.0f) { - TS->font_draw_glyph_outline(rids[i], p_canvas_item, size, p_outline_size, p_pos, glyph_a, p_outline_modulate); + TS->font_draw_glyph_outline(rids[i], p_canvas_item, p_size, p_outline_size, p_pos, glyph_a, p_outline_modulate); } - TS->font_draw_glyph(rids[i], p_canvas_item, size, p_pos, glyph_a, p_modulate); + TS->font_draw_glyph(rids[i], p_canvas_item, p_size, p_pos, glyph_a, p_modulate); return ret; } } diff --git a/scene/resources/font.h b/scene/resources/font.h index e65fdb0751a..e1f1f6d7428 100644 --- a/scene/resources/font.h +++ b/scene/resources/font.h @@ -219,7 +219,6 @@ class Font : public Resource { mutable Vector rids; // Font config. - int base_size = 16; Dictionary variation_coordinates; int spacing_bottom = 0; int spacing_top = 0; @@ -237,6 +236,8 @@ protected: virtual void reset_state() override; public: + static const int DEFAULT_FONT_SIZE = 16; + Dictionary get_feature_list() const; // Font data. @@ -249,9 +250,6 @@ public: virtual void remove_data(int p_idx); // Font configuration. - virtual void set_base_size(int p_size); - virtual int get_base_size() const; - virtual void set_variation_coordinates(const Dictionary &p_variation_coordinates); virtual Dictionary get_variation_coordinates() const; @@ -259,26 +257,26 @@ public: virtual int get_spacing(TextServer::SpacingType p_spacing) const; // 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; + virtual real_t get_height(int p_size = DEFAULT_FONT_SIZE) const; + virtual real_t get_ascent(int p_size = DEFAULT_FONT_SIZE) const; + virtual real_t get_descent(int p_size = DEFAULT_FONT_SIZE) const; + virtual real_t get_underline_position(int p_size = DEFAULT_FONT_SIZE) const; + virtual real_t get_underline_thickness(int p_size = DEFAULT_FONT_SIZE) const; // Drawing string. - virtual Size2 get_string_size(const String &p_text, int p_size = -1, HAlign p_align = HALIGN_LEFT, real_t p_width = -1, uint16_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, uint16_t p_flags = TextServer::BREAK_MANDATORY | TextServer::BREAK_WORD_BOUND) const; + virtual Size2 get_string_size(const String &p_text, int p_size = DEFAULT_FONT_SIZE, HAlign p_align = HALIGN_LEFT, real_t p_width = -1, uint16_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 = DEFAULT_FONT_SIZE, uint16_t p_flags = TextServer::BREAK_MANDATORY | TextServer::BREAK_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), uint16_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), uint16_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 = DEFAULT_FONT_SIZE, const Color &p_modulate = Color(1, 1, 1), int p_outline_size = 0, const Color &p_outline_modulate = Color(1, 1, 1, 0), uint16_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 = DEFAULT_FONT_SIZE, const Color &p_modulate = Color(1, 1, 1), int p_outline_size = 0, const Color &p_outline_modulate = Color(1, 1, 1, 0), uint16_t p_flags = TextServer::BREAK_MANDATORY | TextServer::BREAK_WORD_BOUND | TextServer::JUSTIFICATION_KASHIDA | TextServer::JUSTIFICATION_WORD_BOUND) const; // Helper functions. virtual bool has_char(char32_t p_char) const; virtual String get_supported_chars() 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; + virtual Size2 get_char_size(char32_t p_char, char32_t p_next = 0, int p_size = DEFAULT_FONT_SIZE) 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 = DEFAULT_FONT_SIZE, 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 get_rids() const; diff --git a/scene/resources/theme.cpp b/scene/resources/theme.cpp index 65cdc1e24e8..99977a20f23 100644 --- a/scene/resources/theme.cpp +++ b/scene/resources/theme.cpp @@ -169,7 +169,7 @@ void Theme::_get_property_list(List *p_list) const { const StringName *key2 = nullptr; while ((key2 = font_size_map[*key].next(key2))) { - list.push_back(PropertyInfo(Variant::INT, String() + *key + "/font_sizes/" + *key2)); + list.push_back(PropertyInfo(Variant::INT, String() + *key + "/font_sizes/" + *key2, PROPERTY_HINT_RANGE, "0,256,1,or_greater")); } } @@ -1655,7 +1655,7 @@ void Theme::_bind_methods() { ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "default_base_scale", PROPERTY_HINT_RANGE, "0.0,2.0,0.01,or_greater"), "set_default_base_scale", "get_default_base_scale"); ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "default_font", PROPERTY_HINT_RESOURCE_TYPE, "Font"), "set_default_font", "get_default_font"); - ADD_PROPERTY(PropertyInfo(Variant::INT, "default_font_size"), "set_default_font_size", "get_default_font_size"); + ADD_PROPERTY(PropertyInfo(Variant::INT, "default_font_size", PROPERTY_HINT_RANGE, "0,256,1,or_greater"), "set_default_font_size", "get_default_font_size"); BIND_ENUM_CONSTANT(DATA_TYPE_COLOR); BIND_ENUM_CONSTANT(DATA_TYPE_CONSTANT);