Path: blob/master/editor/import/resource_importer_bmfont.cpp
9896 views
/**************************************************************************/1/* resource_importer_bmfont.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_bmfont.h"3132#include "core/io/config_file.h"33#include "core/io/resource_saver.h"3435String ResourceImporterBMFont::get_importer_name() const {36return "font_data_bmfont";37}3839String ResourceImporterBMFont::get_visible_name() const {40return "Font Data (AngelCode BMFont)";41}4243void ResourceImporterBMFont::get_recognized_extensions(List<String> *p_extensions) const {44if (p_extensions) {45p_extensions->push_back("font");46p_extensions->push_back("fnt");47}48}4950String ResourceImporterBMFont::get_save_extension() const {51return "fontdata";52}5354String ResourceImporterBMFont::get_resource_type() const {55return "FontFile";56}5758bool ResourceImporterBMFont::get_option_visibility(const String &p_path, const String &p_option, const HashMap<StringName, Variant> &p_options) const {59return true;60}6162void ResourceImporterBMFont::get_import_options(const String &p_path, List<ImportOption> *r_options, int p_preset) const {63r_options->push_back(ImportOption(PropertyInfo(Variant::ARRAY, "fallbacks", PROPERTY_HINT_ARRAY_TYPE, MAKE_RESOURCE_TYPE_HINT("Font")), Array()));6465r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "compress"), true));66r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "scaling_mode", PROPERTY_HINT_ENUM, "Disabled,Enabled (Integer),Enabled (Fractional)"), TextServer::FIXED_SIZE_SCALE_ENABLED));67}6869Error ResourceImporterBMFont::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) {70print_verbose("Importing BMFont font from: " + p_source_file);7172Array fallbacks = p_options["fallbacks"];73TextServer::FixedSizeScaleMode smode = (TextServer::FixedSizeScaleMode)p_options["scaling_mode"].operator int();7475Ref<FontFile> font;76font.instantiate();7778List<String> image_files;79Error err = font->_load_bitmap_font(p_source_file, &image_files);80ERR_FAIL_COND_V_MSG(err != OK, err, "Cannot load font to file \"" + p_source_file + "\".");8182// Update import settings for the image files used by font.83for (const String &file : image_files) {84Ref<ConfigFile> config;85config.instantiate();8687err = config->load(file + ".import");88if (err == OK) {89config->clear();90config->set_value("remap", "importer", "skip");9192config->save(file + ".import");93}94}9596font->set_allow_system_fallback(false);97font->set_fallbacks(fallbacks);98font->set_fixed_size_scale_mode(smode);99100int flg = 0;101if ((bool)p_options["compress"]) {102flg |= ResourceSaver::SaverFlags::FLAG_COMPRESS;103}104105print_verbose("Saving to: " + p_save_path + ".fontdata");106err = ResourceSaver::save(font, p_save_path + ".fontdata", flg);107ERR_FAIL_COND_V_MSG(err != OK, err, "Cannot save font to file \"" + p_save_path + ".res\".");108print_verbose("Done saving to: " + p_save_path + ".fontdata");109return OK;110}111112113