Skip to content

Commit 796a08d

Browse files
committed
Merge pull request godotengine#105655 from bruvzg/svg_tx_import
Add `SVGTexture` importer.
2 parents 6072656 + 15cecfd commit 796a08d

File tree

8 files changed

+208
-5
lines changed

8 files changed

+208
-5
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<class name="ResourceImporterSVG" inherits="ResourceImporter" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../class.xsd">
3+
<brief_description>
4+
Imports a SVG file as a scalable texture for use in 2D or 3D rendering.
5+
</brief_description>
6+
<description>
7+
This importer imports [SVGTexture] resources. See also [ResourceImporterTexture] and [ResourceImporterImage].
8+
</description>
9+
<tutorials>
10+
</tutorials>
11+
<members>
12+
<member name="base_scale" type="float" setter="" getter="" default="1.0">
13+
SVG texture scale. [code]1.0[/code] is the original SVG size. Higher values result in a larger image.
14+
</member>
15+
<member name="color_map" type="Dictionary" setter="" getter="" default="{}">
16+
If set, remaps SVG texture colors according to [Color]-[Color] map.
17+
</member>
18+
<member name="compress" type="bool" setter="" getter="" default="true">
19+
If [code]true[/code], uses lossless compression for the SVG source.
20+
</member>
21+
<member name="saturation" type="float" setter="" getter="" default="1.0">
22+
Overrides texture saturation.
23+
</member>
24+
</members>
25+
</class>

doc/classes/SVGTexture.xml

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,26 @@
1919
Creates a new [SVGTexture] and initializes it by allocating and setting the SVG data from string.
2020
</description>
2121
</method>
22+
<method name="get_source" qualifiers="const">
23+
<return type="String" />
24+
<description>
25+
Returns SVG source code.
26+
</description>
27+
</method>
2228
<method name="set_size_override">
2329
<return type="void" />
2430
<param index="0" name="size" type="Vector2i" />
2531
<description>
2632
Resizes the texture to the specified dimensions.
2733
</description>
2834
</method>
35+
<method name="set_source">
36+
<return type="void" />
37+
<param index="0" name="source" type="String" />
38+
<description>
39+
Sets SVG source code.
40+
</description>
41+
</method>
2942
</methods>
3043
<members>
3144
<member name="base_scale" type="float" setter="set_base_scale" getter="get_base_scale" default="1.0">
@@ -38,8 +51,5 @@
3851
<member name="saturation" type="float" setter="set_saturation" getter="get_saturation" default="1.0">
3952
Overrides texture saturation.
4053
</member>
41-
<member name="source" type="String" setter="set_source" getter="get_source" default="&quot;&quot;">
42-
SVG source code.
43-
</member>
4454
</members>
4555
</class>

editor/editor_node.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@
128128
#include "editor/import/resource_importer_imagefont.h"
129129
#include "editor/import/resource_importer_layered_texture.h"
130130
#include "editor/import/resource_importer_shader_file.h"
131+
#include "editor/import/resource_importer_svg.h"
131132
#include "editor/import/resource_importer_texture.h"
132133
#include "editor/import/resource_importer_texture_atlas.h"
133134
#include "editor/import/resource_importer_wav.h"
@@ -7275,6 +7276,10 @@ EditorNode::EditorNode() {
72757276
import_image.instantiate();
72767277
ResourceFormatImporter::get_singleton()->add_importer(import_image);
72777278

7279+
Ref<ResourceImporterSVG> import_svg;
7280+
import_svg.instantiate();
7281+
ResourceFormatImporter::get_singleton()->add_importer(import_svg);
7282+
72787283
Ref<ResourceImporterTextureAtlas> import_texture_atlas;
72797284
import_texture_atlas.instantiate();
72807285
ResourceFormatImporter::get_singleton()->add_importer(import_texture_atlas);
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/**************************************************************************/
2+
/* resource_importer_svg.cpp */
3+
/**************************************************************************/
4+
/* This file is part of: */
5+
/* GODOT ENGINE */
6+
/* https://godotengine.org */
7+
/**************************************************************************/
8+
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9+
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10+
/* */
11+
/* Permission is hereby granted, free of charge, to any person obtaining */
12+
/* a copy of this software and associated documentation files (the */
13+
/* "Software"), to deal in the Software without restriction, including */
14+
/* without limitation the rights to use, copy, modify, merge, publish, */
15+
/* distribute, sublicense, and/or sell copies of the Software, and to */
16+
/* permit persons to whom the Software is furnished to do so, subject to */
17+
/* the following conditions: */
18+
/* */
19+
/* The above copyright notice and this permission notice shall be */
20+
/* included in all copies or substantial portions of the Software. */
21+
/* */
22+
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23+
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24+
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25+
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26+
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27+
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28+
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29+
/**************************************************************************/
30+
31+
#include "resource_importer_svg.h"
32+
33+
#include "core/io/file_access.h"
34+
#include "scene/resources/svg_texture.h"
35+
36+
String ResourceImporterSVG::get_importer_name() const {
37+
return "svg";
38+
}
39+
40+
String ResourceImporterSVG::get_visible_name() const {
41+
return "SVGTexture";
42+
}
43+
44+
void ResourceImporterSVG::get_recognized_extensions(List<String> *p_extensions) const {
45+
p_extensions->push_back("svg");
46+
}
47+
48+
String ResourceImporterSVG::get_save_extension() const {
49+
return "svgtex";
50+
}
51+
52+
String ResourceImporterSVG::get_resource_type() const {
53+
return "SVGTexture";
54+
}
55+
56+
bool ResourceImporterSVG::get_option_visibility(const String &p_path, const String &p_option, const HashMap<StringName, Variant> &p_options) const {
57+
return true;
58+
}
59+
60+
int ResourceImporterSVG::get_preset_count() const {
61+
return 0;
62+
}
63+
64+
String ResourceImporterSVG::get_preset_name(int p_idx) const {
65+
return String();
66+
}
67+
68+
void ResourceImporterSVG::get_import_options(const String &p_path, List<ImportOption> *r_options, int p_preset) const {
69+
r_options->push_back(ImportOption(PropertyInfo(Variant::FLOAT, "base_scale", PROPERTY_HINT_RANGE, "0.001,100,0.001"), 1.0));
70+
r_options->push_back(ImportOption(PropertyInfo(Variant::FLOAT, "saturation", PROPERTY_HINT_RANGE, "0.0,1.0,0.01"), 1.0));
71+
r_options->push_back(ImportOption(PropertyInfo(Variant::DICTIONARY, "color_map"), Dictionary()));
72+
r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "compress"), true));
73+
}
74+
75+
Error ResourceImporterSVG::import(ResourceUID::ID p_source_id, const String &p_source_file, const String &p_save_path, const HashMap<StringName, Variant> &p_options, List<String> *r_platform_variants, List<String> *r_gen_files, Variant *r_metadata) {
76+
Ref<SVGTexture> svg_tex;
77+
svg_tex.instantiate();
78+
79+
String source = FileAccess::get_file_as_string(p_source_file);
80+
ERR_FAIL_COND_V_MSG(source.is_empty(), ERR_CANT_OPEN, vformat("Cannot open file from path \"%s\".", p_source_file));
81+
82+
double base_scale = p_options["base_scale"];
83+
double saturation = p_options["saturation"];
84+
Dictionary color_map = p_options["color_map"];
85+
86+
svg_tex->set_base_scale(base_scale);
87+
svg_tex->set_saturation(saturation);
88+
svg_tex->set_color_map(color_map);
89+
svg_tex->set_source(source);
90+
91+
ERR_FAIL_COND_V_MSG(svg_tex->get_rid().is_null(), ERR_CANT_OPEN, vformat("Failed loading SVG, unsupported or invalid SVG data in \"%s\".", p_source_file));
92+
93+
int flg = 0;
94+
if ((bool)p_options["compress"]) {
95+
flg |= ResourceSaver::SaverFlags::FLAG_COMPRESS;
96+
}
97+
98+
print_verbose("Saving to: " + p_save_path + ".svgtex");
99+
Error err = ResourceSaver::save(svg_tex, p_save_path + ".svgtex", flg);
100+
ERR_FAIL_COND_V_MSG(err != OK, err, vformat("Cannot save SVG texture to file \"%s.svgtex\".", p_save_path));
101+
print_verbose("Done saving to: " + p_save_path + ".svgtex");
102+
103+
return OK;
104+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/**************************************************************************/
2+
/* resource_importer_svg.h */
3+
/**************************************************************************/
4+
/* This file is part of: */
5+
/* GODOT ENGINE */
6+
/* https://godotengine.org */
7+
/**************************************************************************/
8+
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9+
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10+
/* */
11+
/* Permission is hereby granted, free of charge, to any person obtaining */
12+
/* a copy of this software and associated documentation files (the */
13+
/* "Software"), to deal in the Software without restriction, including */
14+
/* without limitation the rights to use, copy, modify, merge, publish, */
15+
/* distribute, sublicense, and/or sell copies of the Software, and to */
16+
/* permit persons to whom the Software is furnished to do so, subject to */
17+
/* the following conditions: */
18+
/* */
19+
/* The above copyright notice and this permission notice shall be */
20+
/* included in all copies or substantial portions of the Software. */
21+
/* */
22+
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23+
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24+
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25+
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26+
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27+
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28+
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29+
/**************************************************************************/
30+
31+
#pragma once
32+
33+
#include "core/io/resource_importer.h"
34+
35+
class ResourceImporterSVG : public ResourceImporter {
36+
GDCLASS(ResourceImporterSVG, ResourceImporter);
37+
38+
public:
39+
virtual String get_importer_name() const override;
40+
virtual String get_visible_name() const override;
41+
virtual void get_recognized_extensions(List<String> *p_extensions) const override;
42+
virtual String get_save_extension() const override;
43+
virtual String get_resource_type() const override;
44+
45+
virtual int get_preset_count() const override;
46+
virtual String get_preset_name(int p_idx) const override;
47+
48+
virtual void get_import_options(const String &p_path, List<ImportOption> *r_options, int p_preset = 0) const override;
49+
virtual bool get_option_visibility(const String &p_path, const String &p_option, const HashMap<StringName, Variant> &p_options) const override;
50+
51+
virtual Error import(ResourceUID::ID p_source_id, const String &p_source_file, const String &p_save_path, const HashMap<StringName, Variant> &p_options, List<String> *r_platform_variants, List<String> *r_gen_files = nullptr, Variant *r_metadata = nullptr) override;
52+
53+
virtual bool can_import_threaded() const override { return true; }
54+
};

editor/register_editor_types.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
#include "editor/import/resource_importer_imagefont.h"
6666
#include "editor/import/resource_importer_layered_texture.h"
6767
#include "editor/import/resource_importer_shader_file.h"
68+
#include "editor/import/resource_importer_svg.h"
6869
#include "editor/import/resource_importer_texture.h"
6970
#include "editor/import/resource_importer_texture_atlas.h"
7071
#include "editor/import/resource_importer_wav.h"
@@ -197,6 +198,7 @@ void register_editor_types() {
197198
GDREGISTER_CLASS(ResourceImporterDynamicFont);
198199
GDREGISTER_CLASS(ResourceImporterImage);
199200
GDREGISTER_CLASS(ResourceImporterImageFont);
201+
GDREGISTER_CLASS(ResourceImporterSVG);
200202
GDREGISTER_CLASS(ResourceImporterLayeredTexture);
201203
GDREGISTER_CLASS(ResourceImporterOBJ);
202204
GDREGISTER_CLASS(ResourceImporterScene);

scene/resources/svg_texture.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,9 @@ RID SVGTexture::_load_at_scale(double p_scale, bool p_set_size) const {
193193
const bool upsample = !Math::is_equal_approx(Math::round(p_scale * base_scale), p_scale * base_scale);
194194

195195
Error err = ImageLoaderSVG::create_image_from_string(img, source, p_scale * base_scale, upsample, cmap);
196-
ERR_FAIL_COND_V_MSG(err != OK, RID(), "Failed generating icon, unsupported or invalid SVG data in default theme.");
196+
if (err != OK) {
197+
return RID();
198+
}
197199
#else
198200
img = Image::create_empty(Math::round(16 * p_scale * base_scale), Math::round(16 * p_scale * base_scale), false, Image::FORMAT_RGBA8);
199201
#endif
@@ -372,7 +374,7 @@ void SVGTexture::_bind_methods() {
372374
ClassDB::bind_method(D_METHOD("get_color_map"), &SVGTexture::get_color_map);
373375
ClassDB::bind_method(D_METHOD("set_size_override", "size"), &SVGTexture::set_size_override);
374376

375-
ADD_PROPERTY(PropertyInfo(Variant::STRING, "source", PROPERTY_HINT_MULTILINE_TEXT), "set_source", "get_source");
377+
ADD_PROPERTY(PropertyInfo(Variant::STRING, "_source", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_INTERNAL | PROPERTY_USAGE_STORAGE), "set_source", "get_source");
376378
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "base_scale", PROPERTY_HINT_RANGE, "0.01,10.0,0.01"), "set_base_scale", "get_base_scale");
377379
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "saturation", PROPERTY_HINT_RANGE, "0.0,1.0,0.01"), "set_saturation", "get_saturation");
378380
ADD_PROPERTY(PropertyInfo(Variant::DICTIONARY, "color_map"), "set_color_map", "get_color_map");

scene/resources/svg_texture.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ class BitMap;
3737

3838
class SVGTexture : public Texture2D {
3939
GDCLASS(SVGTexture, Texture2D);
40+
RES_BASE_EXTENSION("svgtex");
4041

4142
String source;
4243
float base_scale = 1.0;

0 commit comments

Comments
 (0)