Skip to contentSkip to conversation

Font fixes and preliminary HiDPI support

PATCH In review Zhang Boyang 2 participants 10 messages Dec 5, 2022 – Dec 13, 2022

lore
Share & tools
lore
#0 lore raw

cover Font fixes and preliminary HiDPI support

Hi,

PATCH 1-3 are additional fixes to font code.

PATCH 4-5 are the refactored and rebased version of HiDPI patches. They add preliminary HiDPI support for gfxterm. There is no HiDPI support for gfxmenu yet.

Best Regards,
Zhang Boyang

#1 lore raw

PATCH 1/5 font: Check return value of grub_malloc() in ascii_glyph_lookup()

There is a problem in ascii_glyph_lookup(). It doesn't check the return value of grub_malloc(). If memory can't be allocated, then NULL pointer will be written to.

This patch fixes the problem by fallbacking to unknown_glyph in case of grub_malloc() returns NULL.

---
 grub-core/font/font.c | 5 +++++
 1 file changed, 5 insertions(+)
grub-core/font/font.c +5 -0
index 3821937e6..19a47f873 100644
@@ -131,6 +131,11 @@ ascii_glyph_lookup (grub_uint32_t code)
 	{
 	  ascii_font_glyph[current] =
 	    grub_malloc (sizeof (struct grub_font_glyph) + ASCII_BITMAP_SIZE);
+	  if (ascii_font_glyph[current] == NULL)
+	    {
+	      ascii_font_glyph[current] = unknown_glyph;
+	      continue;
+	    }
 
 	  ascii_font_glyph[current]->width = 8;
 	  ascii_font_glyph[current]->height = 16;
Signature · 1 line
2.30.2
Reviewed-by on v1 1/5
#2 lore raw

Re: [PATCH 1/5] font: Check return value of grub_malloc() in ascii_glyph_lookup()

Quoted 8 lines · Zhang Boyang

There is a problem in ascii_glyph_lookup(). It doesn't check the return value of grub_malloc(). If memory can't be allocated, then NULL pointer will be written to.

This patch fixes the problem by fallbacking to unknown_glyph in case of grub_malloc() returns NULL.

Daniel

#3 lore raw

PATCH 2/5 font: Assign null_font to unknown_glyph

In reply to Zhang Boyang

Like glyphs in ascii_font_glyph[], assign null_font to unknown_glyph->font in order to prevent grub_font_get_*() from dereferencing NULL pointer.

---
 grub-core/font/font.c | 1 +
 1 file changed, 1 insertion(+)
grub-core/font/font.c +1 -0
index 19a47f873..674043d0d 100644
@@ -177,6 +177,7 @@ grub_font_loader_init (void)
   unknown_glyph->offset_x = 0;
   unknown_glyph->offset_y = -3;
   unknown_glyph->device_width = 8;
+  unknown_glyph->font = &null_font;
   grub_memcpy (unknown_glyph->bitmap,
 	       unknown_glyph_bitmap, sizeof (unknown_glyph_bitmap));
 
Signature · 1 line
2.30.2
#5 lore raw

PATCH 3/5 font: Reject fonts with negative max_char_width or max_char_height

In reply to Zhang Boyang

If max_char_width or max_char_height is negative, bad values might be propagated by grub_font_get_max_char_width() or grub_font_get_max_char_height(). Prevent this from happening.

---
 grub-core/font/font.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
grub-core/font/font.c +2 -2
index 674043d0d..24adcb35a 100644
@@ -644,8 +644,8 @@ grub_font_load (const char *filename)
 	       font->max_char_width, font->max_char_height, font->num_chars);
 #endif
 
-  if (font->max_char_width == 0
-      || font->max_char_height == 0
+  if (font->max_char_width <= 0
+      || font->max_char_height <= 0
       || font->num_chars == 0
       || font->char_index == 0 || font->ascent == 0 || font->descent == 0)
     {
Signature · 1 line
2.30.2
Reviewed-by on v1 3/5
#6 lore raw

Re: [PATCH 3/5] font: Reject fonts with negative max_char_width or max_char_height

Quoted 5 lines · Zhang Boyang

If max_char_width or max_char_height is negative, bad values might be propagated by grub_font_get_max_char_width() or grub_font_get_max_char_height(). Prevent this from happening.

Daniel

#7 lore raw

PATCH 4/5 font: Add font scaling feature to grub_font_draw_glyph()

In reply to Zhang Boyang

This patch adds an argument 'scale' to grub_font_draw_glyph(). If scale > 1, then the function will create a new scaled bitmap of the drawing glyph, and draws the scaled glyph. The scaled bitmap is cached in the glyph itself, so it can be reused if same glyph is used many times.

This patch also adds a new metadata entry named MAXS to font files, limiting the maximum acceptable scale factor at runtime for a specific font. Currently MAXS is set to 8, which is sufficient for 16K monitors.

---
 grub-core/commands/videotest.c |  4 +-
 grub-core/font/font.c          | 91 +++++++++++++++++++++++++++++++---
 grub-core/gfxmenu/font.c       |  2 +-
 grub-core/term/gfxterm.c       |  2 +-
 include/grub/font.h            |  9 +++-
 5 files changed, 97 insertions(+), 11 deletions(-)
grub-core/commands/videotest.c +2 -2
index ac145afc2..d95ee411d 100644
@@ -87,7 +87,7 @@ grub_cmd_videotest (grub_command_t cmd __attribute__ ((unused)),
       return grub_error (GRUB_ERR_BAD_FONT, "no font loaded");
 
     glyph = grub_font_get_glyph (fixed, '*');
-    grub_font_draw_glyph (glyph, color, 200 ,0);
+    grub_font_draw_glyph (glyph, color, 200, 0, 1);
 
     color = grub_video_map_rgb (255, 255, 255);
 
@@ -148,7 +148,7 @@ grub_cmd_videotest (grub_command_t cmd __attribute__ ((unused)),
       {
 	color = grub_video_map_color (i);
 	palette[i] = color;
-	grub_font_draw_glyph (glyph, color, 16 + i * 16, 220);
+	grub_font_draw_glyph (glyph, color, 16 + i * 16, 220, 1);
       }
   }
 
grub-core/font/font.c +85 -6

151 lines not on this page · raw message

grub-core/gfxmenu/font.c +1 -1
index 756c24f20..ed59ca954 100644
@@ -67,7 +67,7 @@ grub_font_draw_string (const char *str, grub_font_t font,
 	  err = grub_errno;
 	  goto out;
 	}
-      err = grub_font_draw_glyph (glyph, color, x, baseline_y);
+      err = grub_font_draw_glyph (glyph, color, x, baseline_y, 1);
       if (err)
 	goto out;
       x += glyph->device_width;
grub-core/term/gfxterm.c +1 -1
index 3c468f459..4512dee6f 100644
@@ -656,7 +656,7 @@ paint_char (unsigned cx, unsigned cy)
   /* Render glyph to text layer.  */
   grub_video_set_active_render_target (text_layer);
   grub_video_fill_rect (bgcolor, x, y, width, height);
-  grub_font_draw_glyph (glyph, color, x, y + ascent);
+  grub_font_draw_glyph (glyph, color, x, y + ascent, 1);
   grub_video_set_active_render_target (render_target);
 
   /* Mark character to be drawn.  */
include/grub/font.h +8 -1
index 708fa42ac..6417e5b58 100644
@@ -79,6 +79,12 @@ struct grub_font_glyph
   /* Number of pixels to advance to start the next character.  */
   grub_uint16_t device_width;
 
+  /* Pointer to cached scaled bitmap.  Allocated during a scaling drawing.  */
+  grub_uint8_t *scaled_bitmap;
+
+  /* The scale factor for cached scaled bitmap.  */
+  int scale;
+
   /* Row-major order, packed bits (no padding; rows can break within a byte).
      The length of the array is (width * height + 7) / 8.  Within a
      byte, the most significant bit is the first (leftmost/uppermost) pixel.
@@ -141,7 +147,8 @@ struct grub_font_glyph *EXPORT_FUNC (grub_font_get_glyph_with_fallback) (grub_fo
 
 grub_err_t EXPORT_FUNC (grub_font_draw_glyph) (struct grub_font_glyph *glyph,
 					       grub_video_color_t color,
-					       int left_x, int baseline_y);
+					       int left_x, int baseline_y,
+					       int scale);
 
 int
 EXPORT_FUNC (grub_font_get_constructed_device_width) (grub_font_t hinted_font,
Signature · 1 line
2.30.2
#8 lore raw

Re: [PATCH 4/5] font: Add font scaling feature to grub_font_draw_glyph()

Quoted 3 lines · Zhang Boyang

This patch also adds a new metadata entry named MAXS to font files, limiting the maximum acceptable scale factor at runtime for a specific font. Currently MAXS is set to 8, which is sufficient for 16K monitors.

Oh, I forgot to delete this paragraph of commit message when refactoring. There is no such change in this commit. Please ignore this paragraph.

Best Regards,
Zhang Boyang

#9 lore raw

PATCH 5/5 term/gfxterm: Preliminary HiDPI support

In reply to Zhang Boyang

Currently GRUB's default font is too small to see on a HiDPI monitor. This patch adds preliminary HiDPI support to gfxterm. It introduces a new environment variable 'gfxterm_scale'. If set to 0, and a high resolution monitor is detected, it will scale the font size automatically. If set to other number, that number will be the scale factor, overriding automatic scale factor calculation.

---
 docs/grub.texi           |  11 ++++
 grub-core/gfxmenu/view.c |   1 +
 grub-core/term/gfxterm.c | 120 ++++++++++++++++++++++++++++++++-------
 include/grub/gfxterm.h   |   8 ++-
 4 files changed, 117 insertions(+), 23 deletions(-)
docs/grub.texi +11 -0
index 50c811a88..b754a465b 100644
@@ -3309,6 +3309,7 @@ These variables have special meaning to GRUB.
 * gfxmode::
 * gfxpayload::
 * gfxterm_font::
+* gfxterm_scale::
 * grub_cpu::
 * grub_platform::
 * icondir::
@@ -3588,6 +3589,16 @@ If this variable is set, it names a font to use for text on the
 available font.
 
 
+@node gfxterm_scale
+@subsection gfxterm_scale
+
+If this variable is not set, or set to @samp{0}, the @samp{gfxterm}
+graphical terminal will scale the font automatically when a high resolution
+monitor is detected.  If set to other number, the font scale factor will be
+forced to that number.  Set this to @samp{1} if user don't want
+@samp{gfxterm} to scale the font on screen.
+
+
 @node grub_cpu
 @subsection grub_cpu
 
grub-core/gfxmenu/view.c +1 -0
index 6358004b2..94b9ef4db 100644
@@ -546,6 +546,7 @@ init_terminal (grub_gfxmenu_view_t view)
                            view->terminal_rect.height,
                            view->double_repaint,
                            terminal_font,
+                           1,
                            view->terminal_border);
   grub_gfxterm_decorator_hook = grub_gfxmenu_draw_terminal_box;
 }
grub-core/term/gfxterm.c +98 -22

226 lines not on this page · raw message

include/grub/gfxterm.h +7 -1
index 7e1ff6dfc..6cd99dd54 100644
@@ -25,11 +25,17 @@
 #include <grub/video.h>
 #include <grub/font.h>
 
+int
+EXPORT_FUNC (grub_gfxterm_adjust_scale_factor) (grub_font_t font, int scale,
+						int width, int height,
+						int border_width);
+
 grub_err_t
 EXPORT_FUNC (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);
+				       grub_font_t font, int scale,
+				       int border_width);
 
 void EXPORT_FUNC (grub_gfxterm_schedule_repaint) (void);
 
Signature · 1 line
2.30.2