From 9f5048fb36e8ad4b88ec7cea901c66d8b3837c8b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Pa=CC=84vels=20Nadtoc=CC=8Cajevs?=
<7645683+bruvzg@users.noreply.github.com>
Date: Mon, 14 Apr 2025 11:27:02 +0300
Subject: [PATCH 1/2] Implement SVGTexture auto-scalable with font
oversampling.
---
doc/classes/SVGTexture.xml | 45 ++++
editor/icons/SVGTexture.svg | 1 +
scene/main/viewport.cpp | 4 +
scene/register_scene_types.cpp | 2 +
scene/resources/svg_texture.cpp | 380 ++++++++++++++++++++++++++++++++
scene/resources/svg_texture.h | 104 +++++++++
6 files changed, 536 insertions(+)
create mode 100644 doc/classes/SVGTexture.xml
create mode 100644 editor/icons/SVGTexture.svg
create mode 100644 scene/resources/svg_texture.cpp
create mode 100644 scene/resources/svg_texture.h
diff --git a/doc/classes/SVGTexture.xml b/doc/classes/SVGTexture.xml
new file mode 100644
index 00000000000..2c738e08697
--- /dev/null
+++ b/doc/classes/SVGTexture.xml
@@ -0,0 +1,45 @@
+
+
+
+ A scalable [Texture2D] based on an SVG image.
+
+
+ A scalable [Texture2D] based on an SVG image. [SVGTexture]s are automatically rescaled to match font oversampling.
+
+
+
+
+
+
+
+
+
+
+
+ Creates a new [SVGTexture] and initializes it by allocating and setting the SVG data from string.
+
+
+
+
+
+
+ Resizes the texture to the specified dimensions.
+
+
+
+
+
+ SVG texture scale.
+
+
+ If sets, remaps SVG texture colors according to [Color]-[Color] map.
+
+
+
+ Overrides texture saturation.
+
+
+ SVG source code.
+
+
+
diff --git a/editor/icons/SVGTexture.svg b/editor/icons/SVGTexture.svg
new file mode 100644
index 00000000000..c44a76a3fdb
--- /dev/null
+++ b/editor/icons/SVGTexture.svg
@@ -0,0 +1 @@
+
diff --git a/scene/main/viewport.cpp b/scene/main/viewport.cpp
index 8f1a8ae1a76..658572a248c 100644
--- a/scene/main/viewport.cpp
+++ b/scene/main/viewport.cpp
@@ -44,6 +44,7 @@
#include "scene/main/canvas_layer.h"
#include "scene/main/window.h"
#include "scene/resources/mesh.h"
+#include "scene/resources/svg_texture.h"
#include "scene/resources/text_line.h"
#include "scene/resources/world_2d.h"
#include "servers/audio_server.h"
@@ -1085,6 +1086,9 @@ bool Viewport::_set_size(const Size2i &p_size, const Size2 &p_size_2d_override,
if (new_font_oversampling != font_oversampling) {
TS->reference_oversampling_level(new_font_oversampling);
TS->unreference_oversampling_level(font_oversampling);
+
+ SVGTexture::reference_scaling_level(new_font_oversampling);
+ SVGTexture::unreference_scaling_level(font_oversampling);
}
size = new_size;
diff --git a/scene/register_scene_types.cpp b/scene/register_scene_types.cpp
index 54fcb34fd91..2d271af33b5 100644
--- a/scene/register_scene_types.cpp
+++ b/scene/register_scene_types.cpp
@@ -148,6 +148,7 @@
#include "scene/resources/style_box_line.h"
#include "scene/resources/style_box_texture.h"
#include "scene/resources/surface_tool.h"
+#include "scene/resources/svg_texture.h"
#include "scene/resources/syntax_highlighter.h"
#include "scene/resources/text_line.h"
#include "scene/resources/text_paragraph.h"
@@ -935,6 +936,7 @@ void register_scene_types() {
#ifndef _3D_DISABLED
GDREGISTER_CLASS(AudioStreamPlayer3D);
GDREGISTER_VIRTUAL_CLASS(PrimitiveMesh);
+ GDREGISTER_CLASS(SVGTexture);
GDREGISTER_CLASS(BoxMesh);
GDREGISTER_CLASS(CapsuleMesh);
GDREGISTER_CLASS(CylinderMesh);
diff --git a/scene/resources/svg_texture.cpp b/scene/resources/svg_texture.cpp
new file mode 100644
index 00000000000..9f88c0b6ff1
--- /dev/null
+++ b/scene/resources/svg_texture.cpp
@@ -0,0 +1,380 @@
+/**************************************************************************/
+/* svg_texture.cpp */
+/**************************************************************************/
+/* This file is part of: */
+/* GODOT ENGINE */
+/* https://godotengine.org */
+/**************************************************************************/
+/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
+/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
+/* */
+/* Permission is hereby granted, free of charge, to any person obtaining */
+/* a copy of this software and associated documentation files (the */
+/* "Software"), to deal in the Software without restriction, including */
+/* without limitation the rights to use, copy, modify, merge, publish, */
+/* distribute, sublicense, and/or sell copies of the Software, and to */
+/* permit persons to whom the Software is furnished to do so, subject to */
+/* the following conditions: */
+/* */
+/* The above copyright notice and this permission notice shall be */
+/* included in all copies or substantial portions of the Software. */
+/* */
+/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
+/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
+/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
+/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
+/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
+/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
+/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
+/**************************************************************************/
+
+#include "svg_texture.h"
+
+#include "core/io/image_loader.h"
+#include "scene/main/canvas_item.h"
+#include "scene/main/viewport.h"
+#include "scene/resources/bit_map.h"
+#include "scene/resources/placeholder_textures.h"
+
+#include "modules/modules_enabled.gen.h" // For svg.
+#ifdef MODULE_SVG_ENABLED
+#include "modules/svg/image_loader_svg.h"
+#endif
+
+Mutex SVGTexture::mutex;
+HashMap SVGTexture::scaling_levels;
+
+void SVGTexture::reference_scaling_level(double p_scale) {
+ if (Math::is_equal_approx(p_scale, 1.0)) {
+ return;
+ }
+
+ MutexLock lock(mutex);
+ ScalingLevel *sl = scaling_levels.getptr(p_scale);
+ if (sl) {
+ sl->refcount++;
+ } else {
+ ScalingLevel new_sl;
+ scaling_levels.insert(p_scale, new_sl);
+ }
+}
+
+void SVGTexture::unreference_scaling_level(double p_scale) {
+ if (Math::is_equal_approx(p_scale, 1.0)) {
+ return;
+ }
+
+ MutexLock lock(mutex);
+ ScalingLevel *sl = scaling_levels.getptr(p_scale);
+ if (sl) {
+ sl->refcount--;
+ if (sl->refcount == 0) {
+ for (SVGTexture *tx : sl->textures) {
+ tx->_remove_scale(p_scale);
+ }
+ sl->textures.clear();
+ scaling_levels.erase(p_scale);
+ }
+ }
+}
+
+Ref SVGTexture::create_from_string(const String &p_source, float p_scale, float p_saturation, const Dictionary &p_color_map) {
+ Ref svg_texture;
+ svg_texture.instantiate();
+ svg_texture->set_source(p_source);
+ svg_texture->set_base_scale(p_scale);
+ svg_texture->set_saturation(p_saturation);
+ svg_texture->set_color_map(p_color_map);
+ return svg_texture;
+}
+
+void SVGTexture::set_source(const String &p_source) {
+ if (source == p_source) {
+ return;
+ }
+ source = p_source;
+ _update_texture();
+}
+
+String SVGTexture::get_source() const {
+ return source;
+}
+
+void SVGTexture::set_base_scale(float p_scale) {
+ if (base_scale == p_scale) {
+ return;
+ }
+ ERR_FAIL_COND(p_scale <= 0.0);
+
+ base_scale = p_scale;
+ _update_texture();
+}
+
+float SVGTexture::get_base_scale() const {
+ return base_scale;
+}
+
+void SVGTexture::set_saturation(float p_saturation) {
+ if (saturation == p_saturation) {
+ return;
+ }
+
+ saturation = p_saturation;
+ _update_texture();
+}
+
+float SVGTexture::get_saturation() const {
+ return saturation;
+}
+
+void SVGTexture::set_color_map(const Dictionary &p_color_map) {
+ if (color_map == p_color_map) {
+ return;
+ }
+ color_map = p_color_map;
+ cmap.clear();
+ for (const Variant *E = color_map.next(); E; E = color_map.next(E)) {
+ cmap[*E] = color_map[*E];
+ }
+ _update_texture();
+}
+
+Dictionary SVGTexture::get_color_map() const {
+ return color_map;
+}
+
+void SVGTexture::_remove_scale(double p_scale) {
+ if (Math::is_equal_approx(p_scale, 1.0)) {
+ return;
+ }
+
+ RID *rid = texture_cache.getptr(p_scale);
+ if (rid) {
+ if (rid->is_valid()) {
+ RenderingServer::get_singleton()->free(*rid);
+ }
+ texture_cache.erase(p_scale);
+ }
+}
+
+RID SVGTexture::_ensure_scale(double p_scale) const {
+ if (Math::is_equal_approx(p_scale, 1.0)) {
+ if (!base_texture.is_valid()) {
+ base_texture = _load_at_scale(p_scale, true);
+ }
+ return base_texture;
+ }
+ RID *rid = texture_cache.getptr(p_scale);
+ if (rid) {
+ return *rid;
+ }
+
+ MutexLock lock(mutex);
+ ScalingLevel *sl = scaling_levels.getptr(p_scale);
+ ERR_FAIL_NULL_V_MSG(sl, RID(), "Invalid scaling level");
+ sl->textures.insert(const_cast(this));
+
+ RID new_rid = _load_at_scale(p_scale, false);
+ texture_cache[p_scale] = new_rid;
+ return new_rid;
+}
+
+RID SVGTexture::_load_at_scale(double p_scale, bool p_set_size) const {
+ Ref img = memnew(Image);
+#ifdef MODULE_SVG_ENABLED
+ const bool upsample = !Math::is_equal_approx(Math::round(p_scale * base_scale), p_scale * base_scale);
+
+ Error err = ImageLoaderSVG::create_image_from_string(img, source, p_scale * base_scale, upsample, cmap);
+ ERR_FAIL_COND_V_MSG(err != OK, RID(), "Failed generating icon, unsupported or invalid SVG data in default theme.");
+#else
+ img = Image::create_empty(Math::round(16 * p_scale * base_scale), Math::round(16 * p_scale * base_scale), false, Image::FORMAT_RGBA8);
+#endif
+ if (saturation != 1.0) {
+ img->adjust_bcs(1.0, 1.0, saturation);
+ }
+
+ if (p_set_size) {
+ size.x = img->get_width();
+ base_size.x = img->get_width();
+ if (size_override.x != 0) {
+ size.x = size_override.x;
+ }
+ size.y = img->get_height();
+ base_size.y = img->get_height();
+ if (size_override.y != 0) {
+ size.y = size_override.y;
+ }
+ }
+
+ RID rid = RenderingServer::get_singleton()->texture_2d_create(img);
+ RenderingServer::get_singleton()->texture_set_size_override(rid, size.x, size.y);
+ return rid;
+}
+
+void SVGTexture::_clear() {
+ for (KeyValue &tx : texture_cache) {
+ if (tx.value.is_valid()) {
+ RenderingServer::get_singleton()->free(tx.value);
+ }
+ }
+ texture_cache.clear();
+ if (base_texture.is_valid()) {
+ RenderingServer::get_singleton()->free(base_texture);
+ }
+ base_texture = RID();
+ alpha_cache.unref();
+}
+
+void SVGTexture::_update_texture() {
+ _clear();
+ _ensure_scale(1.0);
+
+ emit_changed();
+}
+
+Ref SVGTexture::get_image() const {
+ RID rid = _ensure_scale(1.0);
+ if (rid.is_valid()) {
+ return RenderingServer::get_singleton()->texture_2d_get(rid);
+ } else {
+ return Ref();
+ }
+}
+
+int SVGTexture::get_width() const {
+ return size.x;
+}
+
+int SVGTexture::get_height() const {
+ return size.y;
+}
+
+RID SVGTexture::get_rid() const {
+ return _ensure_scale(1.0);
+}
+
+bool SVGTexture::has_alpha() const {
+ return true;
+}
+
+void SVGTexture::draw(RID p_canvas_item, const Point2 &p_pos, const Color &p_modulate, bool p_transpose) const {
+ double scale = 1.0;
+ CanvasItem *ci = CanvasItem::get_current_item_drawn();
+ if (ci) {
+ Viewport *vp = ci->get_viewport();
+ if (vp) {
+ scale = vp->get_oversampling();
+ }
+ }
+ RID rid = _ensure_scale(scale);
+
+ RenderingServer::get_singleton()->canvas_item_add_texture_rect(p_canvas_item, Rect2(p_pos, size), rid, false, p_modulate, p_transpose);
+}
+
+void SVGTexture::draw_rect(RID p_canvas_item, const Rect2 &p_rect, bool p_tile, const Color &p_modulate, bool p_transpose) const {
+ double scale = 1.0;
+ CanvasItem *ci = CanvasItem::get_current_item_drawn();
+ if (ci) {
+ Viewport *vp = ci->get_viewport();
+ if (vp) {
+ scale = vp->get_oversampling();
+ }
+ }
+ RID rid = _ensure_scale(scale);
+
+ RenderingServer::get_singleton()->canvas_item_add_texture_rect(p_canvas_item, p_rect, rid, p_tile, p_modulate, p_transpose);
+}
+
+void SVGTexture::draw_rect_region(RID p_canvas_item, const Rect2 &p_rect, const Rect2 &p_src_rect, const Color &p_modulate, bool p_transpose, bool p_clip_uv) const {
+ double scale = 1.0;
+ CanvasItem *ci = CanvasItem::get_current_item_drawn();
+ if (ci) {
+ Viewport *vp = ci->get_viewport();
+ if (vp) {
+ scale = vp->get_oversampling();
+ }
+ }
+ RID rid = _ensure_scale(scale);
+
+ RenderingServer::get_singleton()->canvas_item_add_texture_rect_region(p_canvas_item, p_rect, rid, p_src_rect, p_modulate, p_transpose, p_clip_uv);
+}
+
+bool SVGTexture::is_pixel_opaque(int p_x, int p_y) const {
+ if (!alpha_cache.is_valid()) {
+ Ref img = get_image();
+ if (img.is_valid()) {
+ alpha_cache.instantiate();
+ alpha_cache->create_from_image_alpha(img);
+ }
+ }
+
+ if (alpha_cache.is_valid()) {
+ int aw = int(alpha_cache->get_size().width);
+ int ah = int(alpha_cache->get_size().height);
+ if (aw == 0 || ah == 0) {
+ return true;
+ }
+
+ int x = p_x * aw / size.x;
+ int y = p_y * ah / size.y;
+
+ x = CLAMP(x, 0, aw);
+ y = CLAMP(y, 0, ah);
+
+ return alpha_cache->get_bit(x, y);
+ }
+
+ return true;
+}
+
+void SVGTexture::set_size_override(const Size2i &p_size) {
+ if (size_override == p_size) {
+ return;
+ }
+ size_override = p_size;
+ size = base_size;
+ if (size_override.x != 0) {
+ size.x = size_override.x;
+ }
+ if (size_override.y != 0) {
+ size.y = size_override.y;
+ }
+ for (KeyValue &tx : texture_cache) {
+ if (tx.value.is_valid()) {
+ RenderingServer::get_singleton()->texture_set_size_override(tx.value, size.x, size.y);
+ }
+ }
+ if (base_texture.is_valid()) {
+ RenderingServer::get_singleton()->texture_set_size_override(base_texture, size.x, size.y);
+ }
+
+ emit_changed();
+}
+
+void SVGTexture::_bind_methods() {
+ ClassDB::bind_static_method("SVGTexture", D_METHOD("create_from_string", "source", "scale", "saturation", "color_map"), &SVGTexture::create_from_string, DEFVAL(1.0), DEFVAL(1.0), DEFVAL(Dictionary()));
+
+ ClassDB::bind_method(D_METHOD("set_source", "source"), &SVGTexture::set_source);
+ ClassDB::bind_method(D_METHOD("get_source"), &SVGTexture::get_source);
+ ClassDB::bind_method(D_METHOD("set_base_scale", "base_scale"), &SVGTexture::set_base_scale);
+ ClassDB::bind_method(D_METHOD("get_base_scale"), &SVGTexture::get_base_scale);
+ ClassDB::bind_method(D_METHOD("set_saturation", "saturation"), &SVGTexture::set_saturation);
+ ClassDB::bind_method(D_METHOD("get_saturation"), &SVGTexture::get_saturation);
+ ClassDB::bind_method(D_METHOD("set_color_map", "color_map"), &SVGTexture::set_color_map);
+ ClassDB::bind_method(D_METHOD("get_color_map"), &SVGTexture::get_color_map);
+ ClassDB::bind_method(D_METHOD("set_size_override", "size"), &SVGTexture::set_size_override);
+
+ ADD_PROPERTY(PropertyInfo(Variant::STRING, "source", PROPERTY_HINT_MULTILINE_TEXT), "set_source", "get_source");
+ ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "base_scale", PROPERTY_HINT_RANGE, "0.01,10.0,0.01"), "set_base_scale", "get_base_scale");
+ ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "saturation", PROPERTY_HINT_RANGE, "0.0,1.0,0.01"), "set_saturation", "get_saturation");
+ ADD_PROPERTY(PropertyInfo(Variant::DICTIONARY, "color_map"), "set_color_map", "get_color_map");
+}
+
+SVGTexture::~SVGTexture() {
+ _clear();
+
+ MutexLock lock(mutex);
+ for (KeyValue &sl : scaling_levels) {
+ sl.value.textures.erase(this);
+ }
+}
diff --git a/scene/resources/svg_texture.h b/scene/resources/svg_texture.h
new file mode 100644
index 00000000000..65ceec8c0a2
--- /dev/null
+++ b/scene/resources/svg_texture.h
@@ -0,0 +1,104 @@
+/**************************************************************************/
+/* svg_texture.h */
+/**************************************************************************/
+/* This file is part of: */
+/* GODOT ENGINE */
+/* https://godotengine.org */
+/**************************************************************************/
+/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
+/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
+/* */
+/* Permission is hereby granted, free of charge, to any person obtaining */
+/* a copy of this software and associated documentation files (the */
+/* "Software"), to deal in the Software without restriction, including */
+/* without limitation the rights to use, copy, modify, merge, publish, */
+/* distribute, sublicense, and/or sell copies of the Software, and to */
+/* permit persons to whom the Software is furnished to do so, subject to */
+/* the following conditions: */
+/* */
+/* The above copyright notice and this permission notice shall be */
+/* included in all copies or substantial portions of the Software. */
+/* */
+/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
+/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
+/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
+/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
+/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
+/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
+/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
+/**************************************************************************/
+
+#pragma once
+
+#include "core/templates/lru.h"
+#include "scene/resources/texture.h"
+
+class BitMap;
+
+class SVGTexture : public Texture2D {
+ GDCLASS(SVGTexture, Texture2D);
+
+ String source;
+ float base_scale = 1.0;
+ float saturation = 1.0;
+ Dictionary color_map;
+ Size2 size_override;
+
+ struct ScalingLevel {
+ HashSet textures;
+ int32_t refcount = 1;
+ };
+ static Mutex mutex;
+ static HashMap scaling_levels;
+
+ mutable RID base_texture;
+ mutable HashMap texture_cache;
+ mutable Ref alpha_cache;
+ mutable HashMap cmap;
+ mutable Size2 base_size;
+ mutable Size2 size;
+
+ void _remove_scale(double p_scale);
+ RID _ensure_scale(double p_scale) const;
+ RID _load_at_scale(double p_scale, bool p_set_size) const;
+ void _update_texture();
+ void _clear();
+
+protected:
+ static void _bind_methods();
+
+public:
+ static Ref create_from_string(const String &p_source, float p_scale = 1.0, float p_saturation = 1.0, const Dictionary &p_color_map = Dictionary());
+
+ void set_source(const String &p_source);
+ String get_source() const;
+
+ void set_base_scale(float p_scale);
+ float get_base_scale() const;
+
+ void set_color_map(const Dictionary &p_color_map);
+ Dictionary get_color_map() const;
+
+ void set_saturation(float p_saturation);
+ float get_saturation() const;
+
+ Ref get_image() const override;
+
+ int get_width() const override;
+ int get_height() const override;
+
+ virtual RID get_rid() const override;
+
+ bool has_alpha() const override;
+ virtual void draw(RID p_canvas_item, const Point2 &p_pos, const Color &p_modulate = Color(1, 1, 1), bool p_transpose = false) const override;
+ virtual void draw_rect(RID p_canvas_item, const Rect2 &p_rect, bool p_tile = false, const Color &p_modulate = Color(1, 1, 1), bool p_transpose = false) const override;
+ virtual void draw_rect_region(RID p_canvas_item, const Rect2 &p_rect, const Rect2 &p_src_rect, const Color &p_modulate = Color(1, 1, 1), bool p_transpose = false, bool p_clip_uv = true) const override;
+
+ void set_size_override(const Size2i &p_size);
+ bool is_pixel_opaque(int p_x, int p_y) const override;
+
+ static void reference_scaling_level(double p_scale);
+ static void unreference_scaling_level(double p_scale);
+
+ ~SVGTexture();
+};
From 24bb0d5c4f7b4ed744dda8e34a012c8d2db5cc66 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Pa=CC=84vels=20Nadtoc=CC=8Cajevs?=
<7645683+bruvzg@users.noreply.github.com>
Date: Tue, 15 Apr 2025 09:35:38 +0300
Subject: [PATCH 2/2] Use SVGTextures for editor and default theme icons.
---
doc/classes/SVGTexture.xml | 6 ++---
editor/themes/editor_icons.cpp | 35 +++++++++-----------------
editor/themes/editor_theme_manager.cpp | 3 ++-
scene/resources/svg_texture.cpp | 11 ++++----
scene/theme/default_theme.cpp | 28 +++------------------
5 files changed, 26 insertions(+), 57 deletions(-)
diff --git a/doc/classes/SVGTexture.xml b/doc/classes/SVGTexture.xml
index 2c738e08697..3133d58e39b 100644
--- a/doc/classes/SVGTexture.xml
+++ b/doc/classes/SVGTexture.xml
@@ -4,7 +4,7 @@
A scalable [Texture2D] based on an SVG image.
- A scalable [Texture2D] based on an SVG image. [SVGTexture]s are automatically rescaled to match font oversampling.
+ A scalable [Texture2D] based on an SVG image. [SVGTexture]s are automatically re-rasterized to match font oversampling.
@@ -29,10 +29,10 @@
- SVG texture scale.
+ SVG texture scale. [code]1.0[/code] is the original SVG size. Higher values result in a larger image.
- If sets, remaps SVG texture colors according to [Color]-[Color] map.
+ If set, remaps SVG texture colors according to [Color]-[Color] map.
diff --git a/editor/themes/editor_icons.cpp b/editor/themes/editor_icons.cpp
index a415a9191e1..4b58c9cd361 100644
--- a/editor/themes/editor_icons.cpp
+++ b/editor/themes/editor_icons.cpp
@@ -35,6 +35,7 @@
#include "editor/themes/editor_icons.gen.h"
#include "editor/themes/editor_scale.h"
#include "scene/resources/image_texture.h"
+#include "scene/resources/svg_texture.h"
#include "modules/svg/image_loader_svg.h"
@@ -47,20 +48,8 @@ void editor_configure_icons(bool p_dark_theme) {
}
// See also `generate_icon()` in `scene/theme/default_theme.cpp`.
-Ref editor_generate_icon(int p_index, float p_scale, float p_saturation, const HashMap &p_convert_colors = HashMap()) {
- Ref img = memnew(Image);
-
- // Upsample icon generation only if the editor scale isn't an integer multiplier.
- // Generating upsampled icons is slower, and the benefit is hardly visible
- // with integer editor scales.
- const bool upsample = !Math::is_equal_approx(Math::round(p_scale), p_scale);
- Error err = ImageLoaderSVG::create_image_from_string(img, editor_icons_sources[p_index], p_scale, upsample, p_convert_colors);
- ERR_FAIL_COND_V_MSG(err != OK, Ref(), "Failed generating icon, unsupported or invalid SVG data in editor theme.");
- if (p_saturation != 1.0) {
- img->adjust_bcs(1.0, 1.0, p_saturation);
- }
-
- return ImageTexture::create_from_image(img);
+Ref editor_generate_icon(int p_index, float p_scale, float p_saturation, const Dictionary &p_convert_colors = Dictionary()) {
+ return SVGTexture::create_from_string(editor_icons_sources[p_index], p_scale, p_saturation, p_convert_colors);
}
float get_gizmo_handle_scale(const String &p_gizmo_handle_name, float p_gizmo_handle_scale) {
@@ -94,8 +83,8 @@ void editor_register_icons(const Ref &p_theme, bool p_dark_theme, float p
// And then some icons are completely excluded from the conversion.
// Standard color conversion map.
- HashMap color_conversion_map_light;
- HashMap color_conversion_map_dark;
+ Dictionary color_conversion_map_light;
+ Dictionary color_conversion_map_dark;
// Icons by default are set up for the dark theme, so if the theme is light,
// we apply the dark-to-light color conversion map.
for (KeyValue &E : EditorColorMap::get_color_conversion_map()) {
@@ -112,7 +101,7 @@ void editor_register_icons(const Ref &p_theme, bool p_dark_theme, float p
color_conversion_map_light[Color::html("#5fff97")] = success_color;
color_conversion_map_light[Color::html("#ffdd65")] = warning_color;
- HashMap color_conversion_map = p_dark_theme ? color_conversion_map_dark : color_conversion_map_light;
+ Dictionary color_conversion_map = p_dark_theme ? color_conversion_map_dark : color_conversion_map_light;
// The names of the icons used in native menus.
HashSet native_menu_icons;
@@ -138,7 +127,7 @@ void editor_register_icons(const Ref &p_theme, bool p_dark_theme, float p
// Accent color conversion map.
// It is used on some icons (checkbox, radio, toggle, etc.), regardless of the dark
// or light mode.
- HashMap accent_color_map;
+ Dictionary accent_color_map;
HashSet accent_color_icons;
const Color accent_color = p_theme->get_color(SNAME("accent_color"), EditorStringName(Editor));
@@ -164,14 +153,14 @@ void editor_register_icons(const Ref &p_theme, bool p_dark_theme, float p
saturation = 1.0;
}
- Ref icon_dark = editor_generate_icon(i, get_gizmo_handle_scale(editor_icon_name, p_gizmo_handle_scale), saturation, color_conversion_map_dark);
- Ref icon_light = editor_generate_icon(i, get_gizmo_handle_scale(editor_icon_name, p_gizmo_handle_scale), saturation, color_conversion_map_light);
+ Ref icon_dark = editor_generate_icon(i, get_gizmo_handle_scale(editor_icon_name, p_gizmo_handle_scale), saturation, color_conversion_map_dark);
+ Ref icon_light = editor_generate_icon(i, get_gizmo_handle_scale(editor_icon_name, p_gizmo_handle_scale), saturation, color_conversion_map_light);
p_theme->set_icon(editor_icon_name + "Dark", EditorStringName(EditorIcons), icon_dark);
p_theme->set_icon(editor_icon_name + "Light", EditorStringName(EditorIcons), icon_light);
p_theme->set_icon(editor_icon_name, EditorStringName(EditorIcons), p_dark_theme ? icon_dark : icon_light);
} else {
- Ref icon;
+ Ref icon;
if (accent_color_icons.has(editor_icon_name)) {
icon = editor_generate_icon(i, get_gizmo_handle_scale(editor_icon_name, p_gizmo_handle_scale), 1.0, accent_color_map);
} else {
@@ -198,7 +187,7 @@ void editor_register_icons(const Ref &p_theme, bool p_dark_theme, float p
const float scale = (float)p_thumb_size / 64.0 * EDSCALE;
for (int i = 0; i < editor_bg_thumbs_count; i++) {
const int index = editor_bg_thumbs_indices[i];
- Ref icon;
+ Ref icon;
if (accent_color_icons.has(editor_icons_names[index])) {
icon = editor_generate_icon(index, scale, 1.0, accent_color_map);
@@ -221,7 +210,7 @@ void editor_register_icons(const Ref &p_theme, bool p_dark_theme, float p
const float scale = (float)p_thumb_size / 32.0 * EDSCALE;
for (int i = 0; i < editor_md_thumbs_count; i++) {
const int index = editor_md_thumbs_indices[i];
- Ref icon;
+ Ref icon;
if (accent_color_icons.has(editor_icons_names[index])) {
icon = editor_generate_icon(index, scale, 1.0, accent_color_map);
diff --git a/editor/themes/editor_theme_manager.cpp b/editor/themes/editor_theme_manager.cpp
index 5cf28870a85..b7472a93a08 100644
--- a/editor/themes/editor_theme_manager.cpp
+++ b/editor/themes/editor_theme_manager.cpp
@@ -44,6 +44,7 @@
#include "scene/resources/style_box_flat.h"
#include "scene/resources/style_box_line.h"
#include "scene/resources/style_box_texture.h"
+#include "scene/resources/svg_texture.h"
#include "scene/resources/texture.h"
// Theme configuration.
@@ -1724,7 +1725,7 @@ void EditorThemeManager::_populate_standard_styles(const Ref &p_the
p_theme->set_constant("port_h_offset", "GraphNode", 1);
p_theme->set_constant("separation", "GraphNode", 1 * EDSCALE);
- Ref port_icon = p_theme->get_icon(SNAME("GuiGraphNodePort"), EditorStringName(EditorIcons));
+ Ref port_icon = p_theme->get_icon(SNAME("GuiGraphNodePort"), EditorStringName(EditorIcons));
// The true size is 24x24 This is necessary for sharp port icons at high zoom levels in GraphEdit (up to ~200%).
port_icon->set_size_override(Size2(12, 12));
p_theme->set_icon("port", "GraphNode", port_icon);
diff --git a/scene/resources/svg_texture.cpp b/scene/resources/svg_texture.cpp
index 9f88c0b6ff1..3e3ce5994c1 100644
--- a/scene/resources/svg_texture.cpp
+++ b/scene/resources/svg_texture.cpp
@@ -159,7 +159,7 @@ void SVGTexture::_remove_scale(double p_scale) {
RID SVGTexture::_ensure_scale(double p_scale) const {
if (Math::is_equal_approx(p_scale, 1.0)) {
- if (!base_texture.is_valid()) {
+ if (base_texture.is_null()) {
base_texture = _load_at_scale(p_scale, true);
}
return base_texture;
@@ -180,7 +180,8 @@ RID SVGTexture::_ensure_scale(double p_scale) const {
}
RID SVGTexture::_load_at_scale(double p_scale, bool p_set_size) const {
- Ref img = memnew(Image);
+ Ref img;
+ img.instantiate();
#ifdef MODULE_SVG_ENABLED
const bool upsample = !Math::is_equal_approx(Math::round(p_scale * base_scale), p_scale * base_scale);
@@ -300,7 +301,7 @@ void SVGTexture::draw_rect_region(RID p_canvas_item, const Rect2 &p_rect, const
}
bool SVGTexture::is_pixel_opaque(int p_x, int p_y) const {
- if (!alpha_cache.is_valid()) {
+ if (alpha_cache.is_null()) {
Ref img = get_image();
if (img.is_valid()) {
alpha_cache.instantiate();
@@ -318,8 +319,8 @@ bool SVGTexture::is_pixel_opaque(int p_x, int p_y) const {
int x = p_x * aw / size.x;
int y = p_y * ah / size.y;
- x = CLAMP(x, 0, aw);
- y = CLAMP(y, 0, ah);
+ x = CLAMP(x, 0, aw - 1);
+ y = CLAMP(y, 0, ah - 1);
return alpha_cache->get_bit(x, y);
}
diff --git a/scene/theme/default_theme.cpp b/scene/theme/default_theme.cpp
index f9b21faaa81..cb7c95e3857 100644
--- a/scene/theme/default_theme.cpp
+++ b/scene/theme/default_theme.cpp
@@ -38,16 +38,12 @@
#include "scene/resources/image_texture.h"
#include "scene/resources/style_box_flat.h"
#include "scene/resources/style_box_line.h"
+#include "scene/resources/svg_texture.h"
#include "scene/resources/theme.h"
#include "scene/scene_string_names.h"
#include "scene/theme/theme_db.h"
#include "servers/text_server.h"
-#include "modules/modules_enabled.gen.h" // For svg.
-#ifdef MODULE_SVG_ENABLED
-#include "modules/svg/image_loader_svg.h"
-#endif
-
static const int default_font_size = 16;
static float scale = 1.0;
@@ -80,26 +76,8 @@ static Ref sb_expand(Ref p_sbox, float p_left, float
}
// See also `editor_generate_icon()` in `editor/themes/editor_icons.cpp`.
-static Ref generate_icon(int p_index) {
- Ref img = memnew(Image);
-
-#ifdef MODULE_SVG_ENABLED
- // Upsample icon generation only if the scale isn't an integer multiplier.
- // Generating upsampled icons is slower, and the benefit is hardly visible
- // with integer scales.
- const bool upsample = !Math::is_equal_approx(Math::round(scale), scale);
-
- Error err = ImageLoaderSVG::create_image_from_string(img, default_theme_icons_sources[p_index], scale, upsample, HashMap());
- ERR_FAIL_COND_V_MSG(err != OK, Ref(), "Failed generating icon, unsupported or invalid SVG data in default theme.");
-
- img->fix_alpha_edges();
-#else
- // If the SVG module is disabled, we can't really display the UI well, but at least we won't crash.
- // 16 pixels is used as it's the most common base size for Godot icons.
- img = Image::create_empty(Math::round(16 * scale), Math::round(16 * scale), false, Image::FORMAT_RGBA8);
-#endif
-
- return ImageTexture::create_from_image(img);
+static Ref generate_icon(int p_index) {
+ return SVGTexture::create_from_string(default_theme_icons_sources[p_index], scale);
}
static Ref make_empty_stylebox(float p_margin_left = -1, float p_margin_top = -1, float p_margin_right = -1, float p_margin_bottom = -1) {