All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 1/3] Add grub_font_get_no_fallback
@ 2024-05-16 19:31 Vladimir Serbinenko
  2024-05-16 19:31 ` [PATCH 2/3] gfxmenu: Add missing error handling Vladimir Serbinenko
  2024-05-16 19:31 ` [PATCH 3/3] Improve font fallback scanning and validation for gfxterm Vladimir Serbinenko
  0 siblings, 2 replies; 3+ messages in thread
From: Vladimir Serbinenko @ 2024-05-16 19:31 UTC (permalink / raw)
  To: grub-devel; +Cc: Vladimir Serbinenko

This allows the caller to handle the fallback

Signed-off-by: Vladimir Serbinenko <phcoder@gmail.com>
---
 grub-core/font/font.c | 21 ++++++++++++++++-----
 include/grub/font.h   |  2 ++
 2 files changed, 18 insertions(+), 5 deletions(-)

diff --git a/grub-core/font/font.c b/grub-core/font/font.c
index 18de52562..ea01db01b 100644
--- a/grub-core/font/font.c
+++ b/grub-core/font/font.c
@@ -904,12 +904,8 @@ remove_font (grub_font_t font)
     }
 }
 
-/* Get a font from the list of loaded fonts.  This function will return
-   another font if the requested font is not available.  If no fonts are
-   loaded, then a special 'null font' is returned, which contains no glyphs,
-   but is not a null pointer so the caller may omit checks for NULL.  */
 grub_font_t
-grub_font_get (const char *font_name)
+grub_font_get_no_fallback (const char *font_name)
 {
   struct grub_font_node *node;
 
@@ -920,6 +916,21 @@ grub_font_get (const char *font_name)
 	return font;
     }
 
+  return NULL;
+}
+
+/* Get a font from the list of loaded fonts.  This function will return
+   another font if the requested font is not available.  If no fonts are
+   loaded, then a special 'null font' is returned, which contains no glyphs,
+   but is not a null pointer so the caller may omit checks for NULL.  */
+grub_font_t
+grub_font_get (const char *font_name)
+{
+  grub_font_t font = grub_font_get_no_fallback (font_name);
+
+  if (font)
+    return font;
+
   /* If no font by that name is found, return the first font in the list
      as a fallback.  */
   if (grub_font_list && grub_font_list->value)
diff --git a/include/grub/font.h b/include/grub/font.h
index 708fa42ac..e6a43f8fc 100644
--- a/include/grub/font.h
+++ b/include/grub/font.h
@@ -107,6 +107,8 @@ grub_font_t EXPORT_FUNC(grub_font_load) (const char *filename);
    is returned as a fallback.  */
 grub_font_t EXPORT_FUNC (grub_font_get) (const char *font_name);
 
+grub_font_t EXPORT_FUNC(grub_font_get_no_fallback) (const char *font_name);
+					
 const char *EXPORT_FUNC (grub_font_get_name) (grub_font_t font);
 
 int EXPORT_FUNC (grub_font_get_max_char_width) (grub_font_t font);
-- 
2.39.2


_______________________________________________
Grub-devel mailing list
Grub-devel@gnu.org
https://lists.gnu.org/mailman/listinfo/grub-devel

^ permalink raw reply related	[flat|nested] 3+ messages in thread

* [PATCH 2/3] gfxmenu: Add missing error handling
  2024-05-16 19:31 [PATCH 1/3] Add grub_font_get_no_fallback Vladimir Serbinenko
@ 2024-05-16 19:31 ` Vladimir Serbinenko
  2024-05-16 19:31 ` [PATCH 3/3] Improve font fallback scanning and validation for gfxterm Vladimir Serbinenko
  1 sibling, 0 replies; 3+ messages in thread
From: Vladimir Serbinenko @ 2024-05-16 19:31 UTC (permalink / raw)
  To: grub-devel; +Cc: Vladimir Serbinenko

We don't currently handle errors from gfxterm

Signed-off-by: Vladimir Serbinenko <phcoder@gmail.com>
---
 grub-core/gfxmenu/view.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/grub-core/gfxmenu/view.c b/grub-core/gfxmenu/view.c
index e02eba8b0..19d3f2f14 100644
--- a/grub-core/gfxmenu/view.c
+++ b/grub-core/gfxmenu/view.c
@@ -539,7 +539,7 @@ init_terminal (grub_gfxmenu_view_t view)
   /* Note: currently there is no API for changing the gfxterm font
      on the fly, so whatever font the initially loaded theme specifies
      will be permanent.  */
-  grub_gfxterm_set_window (GRUB_VIDEO_RENDER_TARGET_DISPLAY,
+  grub_err_t err = grub_gfxterm_set_window (GRUB_VIDEO_RENDER_TARGET_DISPLAY,
                            view->terminal_rect.x,
                            view->terminal_rect.y,
                            view->terminal_rect.width,
@@ -547,6 +547,9 @@ init_terminal (grub_gfxmenu_view_t view)
                            view->double_repaint,
                            terminal_font,
                            view->terminal_border);
+  if (err)
+    return;
+
   grub_gfxterm_decorator_hook = grub_gfxmenu_draw_terminal_box;
 }
 
-- 
2.39.2


_______________________________________________
Grub-devel mailing list
Grub-devel@gnu.org
https://lists.gnu.org/mailman/listinfo/grub-devel

^ permalink raw reply related	[flat|nested] 3+ messages in thread

* [PATCH 3/3] Improve font fallback scanning and validation for gfxterm
  2024-05-16 19:31 [PATCH 1/3] Add grub_font_get_no_fallback Vladimir Serbinenko
  2024-05-16 19:31 ` [PATCH 2/3] gfxmenu: Add missing error handling Vladimir Serbinenko
@ 2024-05-16 19:31 ` Vladimir Serbinenko
  1 sibling, 0 replies; 3+ messages in thread
From: Vladimir Serbinenko @ 2024-05-16 19:31 UTC (permalink / raw)
  To: grub-devel; +Cc: Vladimir Serbinenko

Choosing a font which is too large breaks gfxterm. Be more diligent in
font choice. This being said it's better to specify the correct font explicitly
in config when several fonts are loaded.

Signed-off-by: Vladimir Serbinenko <phcoder@gmail.com>
---
 grub-core/gfxmenu/view.c |  7 +----
 grub-core/term/gfxterm.c | 56 ++++++++++++++++++++++++++++++++--------
 2 files changed, 46 insertions(+), 17 deletions(-)

diff --git a/grub-core/gfxmenu/view.c b/grub-core/gfxmenu/view.c
index 19d3f2f14..d96b5cdc9 100644
--- a/grub-core/gfxmenu/view.c
+++ b/grub-core/gfxmenu/view.c
@@ -524,12 +524,7 @@ init_terminal (grub_gfxmenu_view_t view)
 {
   grub_font_t terminal_font;
 
-  terminal_font = grub_font_get (view->terminal_font_name);
-  if (!terminal_font)
-    {
-      grub_error (GRUB_ERR_BAD_FONT, "no font loaded");
-      return;
-    }
+  terminal_font = grub_font_get_no_fallback (view->terminal_font_name);
 
   /* Check that terminal window size and position are sane. */
   terminal_sanity_check (view);
diff --git a/grub-core/term/gfxterm.c b/grub-core/term/gfxterm.c
index 3c468f459..addad5ee2 100644
--- a/grub-core/term/gfxterm.c
+++ b/grub-core/term/gfxterm.c
@@ -293,12 +293,56 @@ grub_gfxterm_schedule_repaint (void)
   repaint_scheduled = 1;
 }
 
+static int
+font_validate (grub_font_t font, int width, int height)
+{
+  int normal_char_width = calculate_normal_character_width (font);
+  int normal_char_height = grub_font_get_max_char_height (font);
+  if (normal_char_height == 0)
+    normal_char_height = 16;
+  if (normal_char_width == 0)
+    normal_char_width = 8;
+
+  /* Calculate size of text buffer.  */
+  int columns = width / normal_char_width;
+  int rows = height / normal_char_height;
+
+  return columns >= 40 && rows >= 12;
+}
+
 grub_err_t
 grub_gfxterm_set_window (struct grub_video_render_target *target,
 			 int x, int y, int width, int height,
 			 int double_repaint,
 			 grub_font_t font, int border_width)
 {
+  if (!font)
+    {
+      const char *font_name;
+      /* Select the font to use.  */
+      font_name = grub_env_get ("gfxterm_font");
+      if (! font_name)
+	font_name = "";   /* Allow fallback to any font.  */
+
+      font = grub_font_get_no_fallback (font_name);
+    }
+
+  if (!font || !font_validate(font, width, height))
+    {
+      struct grub_font_node *node;
+
+      font = NULL;
+
+      for (node = grub_font_list; node; node = node->next)
+	if (font_validate(node->value, width, height))
+	  font = node->value;
+
+      if (!font)
+	font = grub_font_get("");
+    }
+  if (!font)
+    return grub_error (GRUB_ERR_BAD_FONT, "no font loaded");
+
   /* Clean up any prior instance.  */
   destroy_window ();
 
@@ -331,12 +375,10 @@ grub_gfxterm_set_window (struct grub_video_render_target *target,
 static grub_err_t
 grub_gfxterm_fullscreen (void)
 {
-  const char *font_name;
   struct grub_video_mode_info mode_info;
   grub_video_color_t color;
   grub_err_t err;
   int double_redraw;
-  grub_font_t font;
 
   err = grub_video_get_info (&mode_info);
   /* Figure out what mode we ended up.  */
@@ -357,21 +399,13 @@ grub_gfxterm_fullscreen (void)
       grub_video_fill_rect (color, 0, 0, mode_info.width, mode_info.height);
     }
 
-  /* Select the font to use.  */
-  font_name = grub_env_get ("gfxterm_font");
-  if (! font_name)
-    font_name = "";   /* Allow fallback to any font.  */
-
-  font = grub_font_get (font_name);
-  if (!font)
-    return grub_error (GRUB_ERR_BAD_FONT, "no font loaded");
 
   grub_gfxterm_decorator_hook = NULL;
 
   return grub_gfxterm_set_window (GRUB_VIDEO_RENDER_TARGET_DISPLAY,
 				  0, 0, mode_info.width, mode_info.height,
 				  double_redraw,
-				  font, DEFAULT_BORDER_WIDTH);
+				  NULL, DEFAULT_BORDER_WIDTH);
 }
 
 static grub_err_t
-- 
2.39.2


_______________________________________________
Grub-devel mailing list
Grub-devel@gnu.org
https://lists.gnu.org/mailman/listinfo/grub-devel

^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2024-05-16 19:32 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-05-16 19:31 [PATCH 1/3] Add grub_font_get_no_fallback Vladimir Serbinenko
2024-05-16 19:31 ` [PATCH 2/3] gfxmenu: Add missing error handling Vladimir Serbinenko
2024-05-16 19:31 ` [PATCH 3/3] Improve font fallback scanning and validation for gfxterm Vladimir Serbinenko

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.