Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Complex Text Layouts] Add compatibility for legacy Font resources. #43931

Merged
merged 1 commit into from
Dec 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion core/object/class_db.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -243,8 +243,11 @@ HashMap<StringName, StringName> ClassDB::resource_base_extensions;
HashMap<StringName, StringName> ClassDB::compat_classes;

bool ClassDB::_is_parent_class(const StringName &p_class, const StringName &p_inherits) {
StringName inherits = p_class;
if (!classes.has(p_class)) {
return false;
}

StringName inherits = p_class;
while (inherits.operator String().length()) {
if (inherits == p_inherits) {
return true;
Expand Down
19 changes: 18 additions & 1 deletion scene/register_scene_types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,10 @@ static Ref<ResourceFormatLoaderText> resource_loader_text;

static Ref<ResourceFormatLoaderFont> resource_loader_font;

#ifndef DISABLE_DEPRECATED
static Ref<ResourceFormatLoaderCompatFont> resource_loader_compat_font;
#endif /* DISABLE_DEPRECATED */

static Ref<ResourceFormatLoaderStreamTexture2D> resource_loader_stream_texture;
static Ref<ResourceFormatLoaderStreamTextureLayered> resource_loader_texture_layered;
static Ref<ResourceFormatLoaderStreamTexture3D> resource_loader_texture_3d;
Expand All @@ -251,6 +255,11 @@ void register_scene_types() {
resource_loader_font.instance();
ResourceLoader::add_resource_format_loader(resource_loader_font);

#ifndef DISABLE_DEPRECATED
resource_loader_compat_font.instance();
ResourceLoader::add_resource_format_loader(resource_loader_compat_font);
#endif /* DISABLE_DEPRECATED */

resource_loader_stream_texture.instance();
ResourceLoader::add_resource_format_loader(resource_loader_stream_texture);

Expand Down Expand Up @@ -799,6 +808,9 @@ void register_scene_types() {
#ifndef DISABLE_DEPRECATED
// Dropped in 4.0, near approximation.
ClassDB::add_compatibility_class("AnimationTreePlayer", "AnimationTree");
ClassDB::add_compatibility_class("BitmapFont", "Font");
ClassDB::add_compatibility_class("DynamicFont", "Font");
ClassDB::add_compatibility_class("DynamicFontData", "FontData");
ClassDB::add_compatibility_class("ToolButton", "Button");

// Renamed in 4.0.
Expand Down Expand Up @@ -917,7 +929,7 @@ void register_scene_types() {
ClassDB::add_compatibility_class("StreamTexture", "StreamTexture2D");
ClassDB::add_compatibility_class("Light2D", "PointLight2D");

#endif
#endif /* DISABLE_DEPRECATED */

OS::get_singleton()->yield(); //may take time to init

Expand Down Expand Up @@ -969,6 +981,11 @@ void unregister_scene_types() {
ResourceLoader::remove_resource_format_loader(resource_loader_font);
resource_loader_font.unref();

#ifndef DISABLE_DEPRECATED
ResourceLoader::remove_resource_format_loader(resource_loader_compat_font);
resource_loader_compat_font.unref();
#endif /* DISABLE_DEPRECATED */

ResourceLoader::remove_resource_format_loader(resource_loader_texture_layered);
resource_loader_texture_layered.unref();

Expand Down
85 changes: 85 additions & 0 deletions scene/resources/font.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,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;
Expand Down Expand Up @@ -899,6 +925,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");
Expand All @@ -918,3 +961,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 */
16 changes: 15 additions & 1 deletion scene/resources/font.h
Original file line number Diff line number Diff line change
Expand Up @@ -198,9 +198,23 @@ VARIANT_ENUM_CAST(Font::SpacingType);
class ResourceFormatLoaderFont : public ResourceFormatLoader {
public:
virtual RES load(const String &p_path, const String &p_original_path = "", Error *r_error = nullptr, bool p_use_sub_threads = false, float *r_progress = nullptr, bool p_no_cache = false);
virtual void get_recognized_extensions_for_type(const String &p_type, List<String> *p_extensions) const;
virtual void get_recognized_extensions(List<String> *p_extensions) const;
virtual bool handles_type(const String &p_type) const;
virtual String get_resource_type(const String &p_path) const;
};

#endif
#ifndef DISABLE_DEPRECATED

class ResourceFormatLoaderCompatFont : public ResourceFormatLoader {
public:
virtual RES load(const String &p_path, const String &p_original_path = "", Error *r_error = nullptr, bool p_use_sub_threads = false, float *r_progress = nullptr, bool p_no_cache = false);
virtual void get_recognized_extensions_for_type(const String &p_type, List<String> *p_extensions) const;
virtual void get_recognized_extensions(List<String> *p_extensions) const;
virtual bool handles_type(const String &p_type) const;
virtual String get_resource_type(const String &p_path) const;
};

#endif /* DISABLE_DEPRECATED */

#endif /* FONT_H */