|
| 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 | +} |
0 commit comments