Path: blob/master/editor/import/resource_importer_svg.cpp
9898 views
/**************************************************************************/1/* resource_importer_svg.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "resource_importer_svg.h"3132#include "core/io/file_access.h"33#include "scene/resources/dpi_texture.h"3435String ResourceImporterSVG::get_importer_name() const {36return "svg";37}3839String ResourceImporterSVG::get_visible_name() const {40return "DPITexture";41}4243void ResourceImporterSVG::get_recognized_extensions(List<String> *p_extensions) const {44p_extensions->push_back("svg");45}4647String ResourceImporterSVG::get_save_extension() const {48return "dpitex";49}5051String ResourceImporterSVG::get_resource_type() const {52return "DPITexture";53}5455bool ResourceImporterSVG::get_option_visibility(const String &p_path, const String &p_option, const HashMap<StringName, Variant> &p_options) const {56return true;57}5859int ResourceImporterSVG::get_preset_count() const {60return 0;61}6263String ResourceImporterSVG::get_preset_name(int p_idx) const {64return String();65}6667void ResourceImporterSVG::get_import_options(const String &p_path, List<ImportOption> *r_options, int p_preset) const {68r_options->push_back(ImportOption(PropertyInfo(Variant::FLOAT, "base_scale", PROPERTY_HINT_RANGE, "0.001,100,0.001"), 1.0));69r_options->push_back(ImportOption(PropertyInfo(Variant::FLOAT, "saturation", PROPERTY_HINT_RANGE, "0.0,1.0,0.01"), 1.0));70r_options->push_back(ImportOption(PropertyInfo(Variant::DICTIONARY, "color_map"), Dictionary()));71r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "compress"), true));72}7374Error 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) {75Ref<DPITexture> dpi_tex;76dpi_tex.instantiate();7778String source = FileAccess::get_file_as_string(p_source_file);79ERR_FAIL_COND_V_MSG(source.is_empty(), ERR_CANT_OPEN, vformat("Cannot open file from path \"%s\".", p_source_file));8081double base_scale = p_options["base_scale"];82double saturation = p_options["saturation"];83Dictionary color_map = p_options["color_map"];8485dpi_tex->set_base_scale(base_scale);86dpi_tex->set_saturation(saturation);87dpi_tex->set_color_map(color_map);88dpi_tex->set_source(source);8990ERR_FAIL_COND_V_MSG(dpi_tex->get_rid().is_null(), ERR_CANT_OPEN, vformat("Failed loading SVG, unsupported or invalid SVG data in \"%s\".", p_source_file));9192int flg = 0;93if ((bool)p_options["compress"]) {94flg |= ResourceSaver::SaverFlags::FLAG_COMPRESS;95}9697print_verbose("Saving to: " + p_save_path + ".dpitex");98Error err = ResourceSaver::save(dpi_tex, p_save_path + ".dpitex", flg);99ERR_FAIL_COND_V_MSG(err != OK, err, vformat("Cannot save DPI texture to file \"%s.dpitex\".", p_save_path));100print_verbose("Done saving to: " + p_save_path + ".dpitex");101102return OK;103}104105106