]> git.ipfire.org Git - thirdparty/plymouth.git/commitdiff
label-freetype: Rework font loading
authorRay Strode <rstrode@redhat.com>
Wed, 3 Jan 2024 20:37:08 +0000 (15:37 -0500)
committerRay Strode <rstrode@redhat.com>
Wed, 3 Jan 2024 20:45:01 +0000 (15:45 -0500)
There's currently this function, set_font_with_fallback, that
almost always gets called with a NULL first argument in the
initramfs, forcing the _with_fallback part of the function to
run.

It's a little strange to have a function with a chunk of code that
hardly ever runs. Furthermore there's a bug where the error variable
is left uninitialized in this case leading to the freetype plugin
sporadically failing to load the fallback font.

This commit reworks things to drop set_font_with_fallback, and just
call FT_New_Face directly in the caller. Fallbacks are handled
at the point where the font path is determined
(previously called query_fc_match, now called find_default_font_path.

src/plugins/controls/label-freetype/plugin.c

index 8e3b0065e9dab790d8b70346cd2d6fd5604d5916..fc4446e0729586eed8771acebabc0f1c726e5fe6 100644 (file)
@@ -121,16 +121,15 @@ static void load_glyphs (ply_label_plugin_control_t *label,
 static void size_control (ply_label_plugin_control_t *label,
                           bool                        force);
 
-/* Query fontconfig, if available, for the default font. */
 static const char *
-query_fc_match ()
+find_default_font_path (void)
 {
         FILE *fp;
         static char fc_match_out[PATH_MAX];
 
         fp = popen ("/usr/bin/fc-match -f %{file}", "r");
         if (!fp)
-                return NULL;
+                return FONT_FALLBACK;
 
         fgets (fc_match_out, sizeof(fc_match_out), fp);
 
@@ -139,16 +138,15 @@ query_fc_match ()
         return fc_match_out;
 }
 
-/* Query fontconfig, if available, for the default monospace font. */
 static const char *
-query_fc_match_monospace ()
+find_default_monospace_font_path (void)
 {
         FILE *fp;
         static char fc_match_out[PATH_MAX];
 
         fp = popen ("/usr/bin/fc-match -f %{file} monospace", "r");
         if (!fp)
-                return NULL;
+                return MONOSPACE_FONT_FALLBACK;
 
         fgets (fc_match_out, sizeof(fc_match_out), fp);
 
@@ -157,29 +155,6 @@ query_fc_match_monospace ()
         return fc_match_out;
 }
 
-static FT_Error
-set_font_with_fallback (ply_label_plugin_control_t *label,
-                        const char                 *primary_font_path,
-                        const char                 *fallback_font_path)
-{
-        FT_Error error;
-        if (primary_font_path != NULL)
-                error = FT_New_Face (label->library, primary_font_path, 0, &label->face);
-
-        if (fallback_font_path != NULL && error != 0) {
-                ply_trace ("Could not load font '%s', trying fallback font '%s' (error %d)",
-                           primary_font_path?: "(unset)", fallback_font_path, (int) error);
-
-                if (!ply_file_exists (fallback_font_path)) {
-                        ply_trace ("Fallback font '%s' does not exist!", fallback_font_path);
-                        return error;
-                }
-                error = FT_New_Face (label->library, fallback_font_path, 0, &label->face);
-        }
-
-        return error;
-}
-
 static ply_label_plugin_control_t *
 create_control (void)
 {
@@ -798,19 +773,27 @@ set_font_for_control (ply_label_plugin_control_t *label,
         if (strstr (font, "Mono") || strstr (font, "mono")) {
                 if (!label->is_monospaced) {
                         FT_Done_Face (label->face);
-                        font_path = query_fc_match_monospace ();
-                        error = set_font_with_fallback (label, font_path, MONOSPACE_FONT_FALLBACK);
+
+                        font_path = find_default_monospace_font_path ();
+
+                        if (font_path != NULL)
+                                error = FT_New_Face (label->library, font_path, 0, &label->face);
+
                         label->is_monospaced = true;
                 }
         } else {
                 if (label->is_monospaced || label->face == NULL) {
                         FT_Done_Face (label->face);
-                        font_path = query_fc_match ();
-                        error = set_font_with_fallback (label, font_path, FONT_FALLBACK);
+
+                        font_path = find_default_font_path ();
+
+                        if (font_path != NULL)
+                                error = FT_New_Face (label->library, font_path, 0, &label->face);
+
                         label->is_monospaced = false;
                 }
         }
-        if (error) {
+        if (error != 0) {
                 FT_Done_Face (label->face);
                 label->face = NULL;