diff options
Diffstat (limited to 'scene/resources/font.cpp')
-rw-r--r-- | scene/resources/font.cpp | 147 |
1 files changed, 137 insertions, 10 deletions
diff --git a/scene/resources/font.cpp b/scene/resources/font.cpp index da35137a09..fab8642c20 100644 --- a/scene/resources/font.cpp +++ b/scene/resources/font.cpp @@ -53,6 +53,11 @@ void FontData::_bind_methods() { ClassDB::bind_method(D_METHOD("set_antialiased", "antialiased"), &FontData::set_antialiased); ClassDB::bind_method(D_METHOD("get_antialiased"), &FontData::get_antialiased); + ClassDB::bind_method(D_METHOD("get_variation_list"), &FontData::get_variation_list); + + ClassDB::bind_method(D_METHOD("set_variation", "tag", "value"), &FontData::set_variation); + ClassDB::bind_method(D_METHOD("get_variation", "tag"), &FontData::get_variation); + ClassDB::bind_method(D_METHOD("set_hinting", "hinting"), &FontData::set_hinting); ClassDB::bind_method(D_METHOD("get_hinting"), &FontData::get_hinting); @@ -115,6 +120,11 @@ bool FontData::_set(const StringName &p_name, const Variant &p_value) { set_script_support_override(scr, p_value); return true; } + if (str.begins_with("variation/")) { + String name = str.get_slicec('/', 1); + set_variation(name, p_value); + return true; + } return false; } @@ -137,6 +147,12 @@ bool FontData::_get(const StringName &p_name, Variant &r_ret) const { r_ret = get_script_support_override(scr); return true; } + if (str.begins_with("variation/")) { + String name = str.get_slicec('/', 1); + + r_ret = get_variation(name); + return true; + } return false; } @@ -153,6 +169,12 @@ void FontData::_get_property_list(List<PropertyInfo> *p_list) const { p_list->push_back(PropertyInfo(Variant::BOOL, "script_support_override/" + scr_over[i], PROPERTY_HINT_NONE, "", PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_STORAGE)); } p_list->push_back(PropertyInfo(Variant::NIL, "script_support_override/_new", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_EDITOR)); + + Dictionary variations = get_variation_list(); + for (const Variant *ftr = variations.next(nullptr); ftr != nullptr; ftr = variations.next(ftr)) { + Vector3i v = variations[*ftr]; + p_list->push_back(PropertyInfo(Variant::FLOAT, "variation/" + TS->tag_to_name(*ftr), PROPERTY_HINT_RANGE, itos(v.x) + "," + itos(v.y), PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_STORAGE)); + } } RID FontData::get_rid() const { @@ -239,6 +261,26 @@ float FontData::get_underline_thickness(int p_size) const { return TS->font_get_underline_thickness(rid, (p_size < 0) ? base_size : p_size); } +Dictionary FontData::get_variation_list() const { + if (rid == RID()) { + return Dictionary(); + } + return TS->font_get_variation_list(rid); +} + +void FontData::set_variation(const String &p_name, double p_value) { + ERR_FAIL_COND(rid == RID()); + TS->font_set_variation(rid, p_name, p_value); + emit_changed(); +} + +double FontData::get_variation(const String &p_name) const { + if (rid == RID()) { + return 0; + } + return TS->font_get_variation(rid, p_name); +} + void FontData::set_antialiased(bool p_antialiased) { ERR_FAIL_COND(rid == RID()); TS->font_set_antialiased(rid, p_antialiased); @@ -472,6 +514,32 @@ void Font::_data_changed() { bool Font::_set(const StringName &p_name, const Variant &p_value) { String str = p_name; +#ifndef DISABLE_DEPRECATED + if (str == "font_data") { // Compatibility, DynamicFont main data + Ref<FontData> fd = p_value; + if (fd.is_valid()) { + add_data(fd); + return true; + } + return false; + } else if (str.begins_with("fallback/")) { // Compatibility, DynamicFont fallback data + Ref<FontData> fd = p_value; + if (fd.is_valid()) { + add_data(fd); + return true; + } + return false; + } else if (str == "fallback") { // Compatibility, BitmapFont fallback + Ref<Font> f = p_value; + if (f.is_valid()) { + for (int i = 0; i < f->get_data_count(); i++) { + add_data(f->get_data(i)); + } + return true; + } + return false; + } +#endif /* DISABLE_DEPRECATED */ if (str.begins_with("data/")) { int idx = str.get_slicec('/', 1).to_int(); Ref<FontData> fd = p_value; @@ -595,41 +663,41 @@ Dictionary Font::get_feature_list() const { float Font::get_height(int p_size) const { float ret = 0.f; for (int i = 0; i < data.size(); i++) { - ret += data[i]->get_height(p_size); + ret = MAX(ret, data[i]->get_height(p_size)); } - return (ret / data.size()) + spacing_top + spacing_bottom; + return ret + spacing_top + spacing_bottom; } float Font::get_ascent(int p_size) const { float ret = 0.f; for (int i = 0; i < data.size(); i++) { - ret += data[i]->get_ascent(p_size); + ret = MAX(ret, data[i]->get_ascent(p_size)); } - return (ret / data.size()) + spacing_top; + return ret + spacing_top; } float Font::get_descent(int p_size) const { float ret = 0.f; for (int i = 0; i < data.size(); i++) { - ret += data[i]->get_descent(p_size); + ret = MAX(ret, data[i]->get_descent(p_size)); } - return (ret / data.size()) + spacing_bottom; + return ret + spacing_bottom; } float Font::get_underline_position(int p_size) const { float ret = 0.f; for (int i = 0; i < data.size(); i++) { - ret += data[i]->get_underline_position(p_size); + ret = MAX(ret, data[i]->get_underline_position(p_size)); } - return (ret / data.size()); + return ret; } float Font::get_underline_thickness(int p_size) const { float ret = 0.f; for (int i = 0; i < data.size(); i++) { - ret += data[i]->get_underline_thickness(p_size); + ret = MAX(ret, data[i]->get_underline_thickness(p_size)); } - return (ret / data.size()); + return ret; } int Font::get_spacing(int p_type) const { @@ -899,6 +967,23 @@ RES ResourceFormatLoaderFont::load(const String &p_path, const String &p_origina return dfont; } +void ResourceFormatLoaderFont::get_recognized_extensions_for_type(const String &p_type, List<String> *p_extensions) const { +#ifndef DISABLE_DEPRECATED + if (p_type == "DynacmicFontData") { + p_extensions->push_back("ttf"); + p_extensions->push_back("otf"); + p_extensions->push_back("woff"); + return; + } + if (p_type == "BitmapFont") { // BitmapFont (*.font, *fnt) is handled by ResourceFormatLoaderCompatFont + return; + } +#endif /* DISABLE_DEPRECATED */ + if (p_type == "" || handles_type(p_type)) { + get_recognized_extensions(p_extensions); + } +} + void ResourceFormatLoaderFont::get_recognized_extensions(List<String> *p_extensions) const { p_extensions->push_back("ttf"); p_extensions->push_back("otf"); @@ -918,3 +1003,45 @@ String ResourceFormatLoaderFont::get_resource_type(const String &p_path) const { } return ""; } + +#ifndef DISABLE_DEPRECATED + +RES ResourceFormatLoaderCompatFont::load(const String &p_path, const String &p_original_path, Error *r_error, bool p_use_sub_threads, float *r_progress, bool p_no_cache) { + if (r_error) { + *r_error = ERR_FILE_CANT_OPEN; + } + + Ref<FontData> dfont; + dfont.instance(); + dfont->load_resource(p_path); + + Ref<Font> font; + font.instance(); + font->add_data(dfont); + + if (r_error) { + *r_error = OK; + } + + return font; +} + +void ResourceFormatLoaderCompatFont::get_recognized_extensions_for_type(const String &p_type, List<String> *p_extensions) const { + if (p_type == "BitmapFont") { + p_extensions->push_back("font"); + p_extensions->push_back("fnt"); + } +} + +void ResourceFormatLoaderCompatFont::get_recognized_extensions(List<String> *p_extensions) const { +} + +bool ResourceFormatLoaderCompatFont::handles_type(const String &p_type) const { + return (p_type == "Font"); +} + +String ResourceFormatLoaderCompatFont::get_resource_type(const String &p_path) const { + return ""; +} + +#endif /* DISABLE_DEPRECATED */ |