Skip to contentSkip to conversation

font: Add font scaling feature to grub_font_draw_glyph()

PATCH Superseded Zhang Boyang 1 participant 3 messages Jun 1, 2022

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/2 font: Add font scaling feature to grub_font_draw_glyph()

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.

To avoid interger overflow during scaling, this patch intruduces dimension limits to font files, described by GRUB_FONT_MAX_DIMENSION. The scaled glyph should also obey this limit. In addition, scale factor is also limited by GRUB_FONT_MAX_SCALE.

---
 grub-core/commands/videotest.c |   4 +-
 grub-core/font/font.c          | 120 ++++++++++++++++++++++++++++++---
 grub-core/gfxmenu/font.c       |   2 +-
 grub-core/term/gfxterm.c       |   2 +-
 include/grub/font.h            |  16 ++++-
 5 files changed, 131 insertions(+), 13 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 +112 -8

208 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 +15 -1
index 708fa42ac..8fc646713 100644
@@ -24,6 +24,13 @@
 #include <grub/file.h>
 #include <grub/unicode.h>
 
+/* Limit font dimensions to avoid interger overflow. Because some font metrics
+   may be derived from this limit, we must reserve some space for arithmetics,
+   so this value can't be too high.  When scaling fonts, this limit should also
+   applies to the font after scaling.  */
+#define GRUB_FONT_MAX_DIMENSION 500
+#define GRUB_FONT_MAX_SCALE 10
+
 /* Forward declaration of opaque structure grub_font.
    Users only pass struct grub_font pointers to the font module functions,
    and do not have knowledge of the structure contents.  */
@@ -79,6 +86,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.  */
+  grub_uint16_t 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 +154,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
#1 lore raw

PATCH v3 2/2 term/gfxterm: Preliminary HiDPI support

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 | 72 ++++++++++++++++++++++++++++------------
 include/grub/gfxterm.h   |  3 +-
 4 files changed, 64 insertions(+), 23 deletions(-)
docs/grub.texi +11 -0
index acf462ccf..2d98ad6ff 100644
@@ -3274,6 +3274,7 @@ These variables have special meaning to GRUB.
 * gfxmode::
 * gfxpayload::
 * gfxterm_font::
+* gfxterm_scale::
 * grub_cpu::
 * grub_platform::
 * icondir::
@@ -3548,6 +3549,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 +50 -22

176 lines not on this page · raw message

include/grub/gfxterm.h +2 -1
index 7e1ff6dfc..73735a26e 100644
@@ -29,7 +29,8 @@ 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
#2 lore raw

PATCH v3 1/1 term/gfxterm: Preliminary HiDPI support

Hi,

Changes in [PATCH v3 1/2]:

To avoid interger overflow during scaling, this patch intruduces GRUB_FONT_MAX_DIMENSION and GRUB_FONT_MAX_SCALE. The font loader will now reject bad fonts and glyphs.

Changes in [PATCH v3 2/2]:

Make behavior configurable through a new environment variable 'gfxterm_scale'.

Changes in [PATCH v2 1/2]:

It now caches scaled bitmaps in each corresponding glyph. So it can be reused if same glyph is used many times.

Best Regards,
Zhang Boyang