]> git.lizzy.rs Git - dragonfireclient.git/commitdiff
Improve TTF support for pixel-style fonts (#11848)
authorVincent Robinson <robinsonvincent89@gmail.com>
Thu, 30 Dec 2021 20:54:21 +0000 (12:54 -0800)
committerGitHub <noreply@github.com>
Thu, 30 Dec 2021 20:54:21 +0000 (12:54 -0800)
builtin/settingtypes.txt
minetest.conf.example
src/client/fontengine.cpp
src/defaultsettings.cpp

index 1bc5e79824e50aa276fbed2f04f3f2cdad4d98a9..22e69e30a2a67b86dc7b744ba26eaf6465af98d6 100644 (file)
@@ -900,9 +900,15 @@ font_shadow (Font shadow) int 1
 #    Opaqueness (alpha) of the shadow behind the default font, between 0 and 255.
 font_shadow_alpha (Font shadow alpha) int 127 0 255
 
-#    Font size of the default font in point (pt).
+#    Font size of the default font where 1 unit = 1 pixel at 96 DPI
 font_size (Font size) int 16 1
 
+#    For pixel-style fonts that do not scale well, this ensures that font sizes used
+#    with this font will always be divisible by this value, in pixels. For instance,
+#    a pixel font 16 pixels tall should have this set to 16, so it will only ever be
+#    sized 16, 32, 48, etc., so a mod requesting a size of 25 will get 32.
+font_size_divisible_by (Font size divisible by) int 1 1
+
 #    Path to the default font.
 #    If “freetype” setting is enabled: Must be a TrueType font.
 #    If “freetype” setting is disabled: Must be a bitmap or XML vectors font.
@@ -913,8 +919,14 @@ font_path_bold (Bold font path) filepath fonts/Arimo-Bold.ttf
 font_path_italic (Italic font path) filepath fonts/Arimo-Italic.ttf
 font_path_bold_italic (Bold and italic font path) filepath fonts/Arimo-BoldItalic.ttf
 
-#    Font size of the monospace font in point (pt).
-mono_font_size (Monospace font size) int 15 1
+#    Font size of the monospace font where 1 unit = 1 pixel at 96 DPI
+mono_font_size (Monospace font size) int 16 1
+
+#    For pixel-style fonts that do not scale well, this ensures that font sizes used
+#    with this font will always be divisible by this value, in pixels. For instance,
+#    a pixel font 16 pixels tall should have this set to 16, so it will only ever be
+#    sized 16, 32, 48, etc., so a mod requesting a size of 25 will get 32.
+mono_font_size_divisible_by (Monospace font size divisible by) int 1 1
 
 #    Path to the monospace font.
 #    If “freetype” setting is enabled: Must be a TrueType font.
index 3f4d014208062967bf1ff886e4836e298c49fa55..919c2d52c1322d510d9f1b6e716c282534611fc1 100644 (file)
 #    type: int min: 1
 # font_size = 16
 
+#    For pixel-style fonts that do not scale well, this ensures that font sizes used
+#    with this font will always be divisible by this value, in pixels. For instance,
+#    a pixel font 16 pixels tall should have this set to 16, so it will only ever be
+#    sized 16, 32, 48, etc., so a mod requesting a size of 25 will get 32.
+# font_size_divisible_by = 1
+
 #    Path to the default font.
 #    If “freetype” setting is enabled: Must be a TrueType font.
 #    If “freetype” setting is disabled: Must be a bitmap or XML vectors font.
 
 #    Font size of the monospace font in point (pt).
 #    type: int min: 1
-# mono_font_size = 15
+# mono_font_size = 16
+
+#    For pixel-style fonts that do not scale well, this ensures that font sizes used
+#    with this font will always be divisible by this value, in pixels. For instance,
+#    a pixel font 16 pixels tall should have this set to 16, so it will only ever be
+#    sized 16, 32, 48, etc., so a mod requesting a size of 25 will get 32.
+# mono_font_size_divisible_by = 1
 
 #    Path to the monospace font.
 #    If “freetype” setting is enabled: Must be a TrueType font.
index 35e908b0cc66f44027b7eaeea49a7d71b886fc77..e537b756cf7bcea7866cb9fbe6cc0c05dcd03e74 100644 (file)
@@ -66,11 +66,13 @@ FontEngine::FontEngine(gui::IGUIEnvironment* env) :
                g_settings->registerChangedCallback("font_path_bolditalic", font_setting_changed, NULL);
                g_settings->registerChangedCallback("font_shadow", font_setting_changed, NULL);
                g_settings->registerChangedCallback("font_shadow_alpha", font_setting_changed, NULL);
+               g_settings->registerChangedCallback("font_size_divisible_by", font_setting_changed, NULL);
                g_settings->registerChangedCallback("fallback_font_path", font_setting_changed, NULL);
        }
 
        g_settings->registerChangedCallback("mono_font_path", font_setting_changed, NULL);
        g_settings->registerChangedCallback("mono_font_size", font_setting_changed, NULL);
+       g_settings->registerChangedCallback("mono_font_size_divisible_by", font_setting_changed, NULL);
        g_settings->registerChangedCallback("screen_dpi", font_setting_changed, NULL);
        g_settings->registerChangedCallback("gui_scaling", font_setting_changed, NULL);
 }
@@ -252,15 +254,18 @@ gui::IGUIFont *FontEngine::initFont(const FontSpec &spec)
        if (spec.italic)
                setting_suffix.append("_italic");
 
-       u32 size = std::floor(RenderingEngine::getDisplayDensity() *
-                       g_settings->getFloat("gui_scaling") * spec.size);
+       u32 size = std::max<u32>(spec.size * RenderingEngine::getDisplayDensity() *
+                       g_settings->getFloat("gui_scaling"), 1);
 
-       if (size == 0) {
-               errorstream << "FontEngine: attempt to use font size 0" << std::endl;
-               errorstream << "  display density: " << RenderingEngine::getDisplayDensity() << std::endl;
-               abort();
+       // Constrain the font size to a certain multiple, if necessary
+       u16 divisible_by = g_settings->getU16(setting_prefix + "font_size_divisible_by");
+       if (divisible_by > 1) {
+               size = std::max<u32>(
+                               std::round((double)size / divisible_by) * divisible_by, divisible_by);
        }
 
+       sanity_check(size != 0);
+
        u16 font_shadow       = 0;
        u16 font_shadow_alpha = 0;
        g_settings->getU16NoEx(setting_prefix + "font_shadow", font_shadow);
index 635ec22577f6c81ab1c1b24b150443c1192dee04..47790a55223c87e7c7d93bbdcfd4cd7d5ad44bff 100644 (file)
@@ -313,10 +313,12 @@ void set_default_settings()
        settings->setDefault("font_italic", "false");
        settings->setDefault("font_shadow", "1");
        settings->setDefault("font_shadow_alpha", "127");
+       settings->setDefault("font_size_divisible_by", "1");
        settings->setDefault("mono_font_path", porting::getDataPath("fonts" DIR_DELIM "Cousine-Regular.ttf"));
        settings->setDefault("mono_font_path_italic", porting::getDataPath("fonts" DIR_DELIM "Cousine-Italic.ttf"));
        settings->setDefault("mono_font_path_bold", porting::getDataPath("fonts" DIR_DELIM "Cousine-Bold.ttf"));
        settings->setDefault("mono_font_path_bold_italic", porting::getDataPath("fonts" DIR_DELIM "Cousine-BoldItalic.ttf"));
+       settings->setDefault("mono_font_size_divisible_by", "1");
        settings->setDefault("fallback_font_path", porting::getDataPath("fonts" DIR_DELIM "DroidSansFallbackFull.ttf"));
 
        std::string font_size_str = std::to_string(TTF_DEFAULT_FONT_SIZE);