From 1ab3ddf94a1006fa5b7e233215a92d46a071e97c Mon Sep 17 00:00:00 2001 From: Hugo Locurcio Date: Tue, 24 Aug 2021 04:29:18 +0200 Subject: [PATCH] Allow using WOFF fonts in DynamicFont This is already supported by FreeType, but it wasn't exposed. Adding support for WOFF2 would require linking a Brotli decompression library in Godot, so only WOFF1 is exposed here. --- doc/classes/DynamicFont.xml | 4 ++-- editor/editor_settings.cpp | 6 +++--- scene/resources/dynamic_font.cpp | 7 +++++-- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/doc/classes/DynamicFont.xml b/doc/classes/DynamicFont.xml index 54fe556d2cd..877c7521eab 100644 --- a/doc/classes/DynamicFont.xml +++ b/doc/classes/DynamicFont.xml @@ -4,8 +4,8 @@ DynamicFont renders vector font files at runtime. - DynamicFont renders vector font files (such as TTF or OTF) dynamically at runtime instead of using a prerendered texture atlas like [BitmapFont]. This trades the faster loading time of [BitmapFont]s for the ability to change font parameters like size and spacing during runtime. [DynamicFontData] is used for referencing the font file paths. DynamicFont also supports defining one or more fallback fonts, which will be used when displaying a character not supported by the main font. - DynamicFont uses the [url=https://www.freetype.org/]FreeType[/url] library for rasterization. + DynamicFont renders vector font files dynamically at runtime instead of using a prerendered texture atlas like [BitmapFont]. This trades the faster loading time of [BitmapFont]s for the ability to change font parameters like size and spacing during runtime. [DynamicFontData] is used for referencing the font file paths. DynamicFont also supports defining one or more fallback fonts, which will be used when displaying a character not supported by the main font. + DynamicFont uses the [url=https://www.freetype.org/]FreeType[/url] library for rasterization. Supported formats are TrueType ([code].ttf[/code]), OpenType ([code].otf[/code]) and Web Open Font Format 1 ([code].woff[/code]). Web Open Font Format 2 ([code].woff2[/code]) is [i]not[/i] supported. [codeblock] var dynamic_font = DynamicFont.new() dynamic_font.font_data = load("res://BarlowCondensed-Bold.ttf") diff --git a/editor/editor_settings.cpp b/editor/editor_settings.cpp index 6fed5737ded..e08a23b1c4e 100644 --- a/editor/editor_settings.cpp +++ b/editor/editor_settings.cpp @@ -325,11 +325,11 @@ void EditorSettings::_load_defaults(Ref p_extra_config) { hints["interface/editor/font_hinting"] = PropertyInfo(Variant::INT, "interface/editor/font_hinting", PROPERTY_HINT_ENUM, "Auto (Light),None,Light,Normal", PROPERTY_USAGE_DEFAULT); #endif _initial_set("interface/editor/main_font", ""); - hints["interface/editor/main_font"] = PropertyInfo(Variant::STRING, "interface/editor/main_font", PROPERTY_HINT_GLOBAL_FILE, "*.ttf,*.otf", PROPERTY_USAGE_DEFAULT); + hints["interface/editor/main_font"] = PropertyInfo(Variant::STRING, "interface/editor/main_font", PROPERTY_HINT_GLOBAL_FILE, "*.ttf,*.otf,*.woff", PROPERTY_USAGE_DEFAULT); _initial_set("interface/editor/main_font_bold", ""); - hints["interface/editor/main_font_bold"] = PropertyInfo(Variant::STRING, "interface/editor/main_font_bold", PROPERTY_HINT_GLOBAL_FILE, "*.ttf,*.otf", PROPERTY_USAGE_DEFAULT); + hints["interface/editor/main_font_bold"] = PropertyInfo(Variant::STRING, "interface/editor/main_font_bold", PROPERTY_HINT_GLOBAL_FILE, "*.ttf,*.otf,*.woff", PROPERTY_USAGE_DEFAULT); _initial_set("interface/editor/code_font", ""); - hints["interface/editor/code_font"] = PropertyInfo(Variant::STRING, "interface/editor/code_font", PROPERTY_HINT_GLOBAL_FILE, "*.ttf,*.otf", PROPERTY_USAGE_DEFAULT); + hints["interface/editor/code_font"] = PropertyInfo(Variant::STRING, "interface/editor/code_font", PROPERTY_HINT_GLOBAL_FILE, "*.ttf,*.otf,*.woff", PROPERTY_USAGE_DEFAULT); _initial_set("interface/editor/dim_editor_on_dialog_popup", true); _initial_set("interface/editor/low_processor_mode_sleep_usec", 6900); // ~144 FPS hints["interface/editor/low_processor_mode_sleep_usec"] = PropertyInfo(Variant::REAL, "interface/editor/low_processor_mode_sleep_usec", PROPERTY_HINT_RANGE, "1,100000,1", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_RESTART_IF_CHANGED); diff --git a/scene/resources/dynamic_font.cpp b/scene/resources/dynamic_font.cpp index 5260eebdcf4..2858305dbd6 100644 --- a/scene/resources/dynamic_font.cpp +++ b/scene/resources/dynamic_font.cpp @@ -95,7 +95,8 @@ void DynamicFontData::_bind_methods() { BIND_ENUM_CONSTANT(HINTING_LIGHT); BIND_ENUM_CONSTANT(HINTING_NORMAL); - ADD_PROPERTY(PropertyInfo(Variant::STRING, "font_path", PROPERTY_HINT_FILE, "*.ttf,*.otf"), "set_font_path", "get_font_path"); + // Only WOFF1 is supported as WOFF2 requires a Brotli decompression library to be linked. + ADD_PROPERTY(PropertyInfo(Variant::STRING, "font_path", PROPERTY_HINT_FILE, "*.ttf,*.otf,*.woff"), "set_font_path", "get_font_path"); } DynamicFontData::DynamicFontData() { @@ -1137,6 +1138,8 @@ RES ResourceFormatLoaderDynamicFont::load(const String &p_path, const String &p_ void ResourceFormatLoaderDynamicFont::get_recognized_extensions(List *p_extensions) const { p_extensions->push_back("ttf"); p_extensions->push_back("otf"); + // Only WOFF1 is supported as WOFF2 requires a Brotli decompression library to be linked. + p_extensions->push_back("woff"); } bool ResourceFormatLoaderDynamicFont::handles_type(const String &p_type) const { @@ -1145,7 +1148,7 @@ bool ResourceFormatLoaderDynamicFont::handles_type(const String &p_type) const { String ResourceFormatLoaderDynamicFont::get_resource_type(const String &p_path) const { String el = p_path.get_extension().to_lower(); - if (el == "ttf" || el == "otf") { + if (el == "ttf" || el == "otf" || el == "woff") { return "DynamicFontData"; } return "";