Simple HiDPI support for GRUB2
PATCH RESEND v3 1/1 Simple HiDPI support for GRUB2
Hi,
This is a resend of my [PATCH v3].
Thank you.
Best Regards,
Zhang Boyang
===
Changes in [PATCH RESEND V3]:
Rebased to latest git head.
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. 2/2: Make behavior configurable through a new environment variable 'gfxterm_scale'.
Changes in [PATCH V2]:
Now caches scaled bitmaps in each corresponding glyph. So it can be reused if same glyph is used many times.
[PATCH V1]:
PATCH RESEND 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
@@ -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
@@ -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
@@ -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
@@ -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_fogrub_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
Re: [PATCH RESEND v3 1/2] font: Add font scaling feature to grub_font_draw_glyph()
Quoted 10 lines · 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.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 byGRUB_FONT_MAX_SCALE.
If you use overflow aware math operators defined as macros, e.g. grub_mul(), defined in include/grub/safemath.h probably you can drop these artificial limits.
Quoted 58 lines
Signed-off-by Zhang Boyang <[email protected]>--- 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@@ -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 +5
-0@@ -137,6 +137,8 @@ ascii_glyph_lookup (grub_uint32_t code) ascii_font_glyph[current]->offset_x =0 ; ascii_font_glyph[current]->offset_y =-2 ; ascii_font_glyph[current]->device_width =8 ; + ascii_font_glyph[current]->scaled_bitmap =NULL ; + ascii_font_glyph[current]->scale =0 ; ascii_font_glyph[current]->font =NULL ; grub_memcpy (ascii_font_glyph[current]->bitmap,@@ -172,6 +174,8 @@ grub_font_loader_init (void) unknown_glyph->offset_x =0 ; unknown_glyph->offset_y =-3 ; unknown_glyph->device_width =8 ; + unknown_glyph->scaled_bitmap =NULL ; + unknown_glyph->scale =0 ;grub_memcpy (unknown_glyph->bitmap, unknown_glyph_bitmap,sizeof (unknown_glyph_bitmap));@@ -646,6 +650,20 @@ grub_font_load (const char *filename) goto fail; } + if (font->max_char_width <= 0
grub-core/font/font.c grub_font_load (const char *filename) · line 663
Quoted 10 lines
grub-core/font/font.c +10
-0+ || font->max_char_width > GRUB_FONT_MAX_DIMENSION + || font->max_char_height <= 0 + || font->max_char_height > GRUB_FONT_MAX_DIMENSION + || font->ascent <= 0 + || font->ascent > GRUB_FONT_MAX_DIMENSION + || font->descent <= 0 + || font->descent > GRUB_FONT_MAX_DIMENSION) + { +grub_error (GRUB_ERR_BAD_FONT , +"invalid font file: bad font metrics" );
grub_error (GRUB_ERR_BAD_FONT, N_("invalid font file: bad font metrics"));
Every grub_error() message has to be processed by N_() macro. And yes, there is no space after N_ and before "(".
Additionally, I am OK with lines a bit longer than 80 chars. So, you can put grub_error() call in one line here and below.
Quoted 103 lines
grub-core/font/font.c +36
-3+ goto fail; + } +/* Add the font to the global font registry. */ if (register_font (font) !=0 ) goto fail;@@ -738,7 +756,7 @@ grub_font_get_glyph_internal (grub_font_t font, grub_uint32_t code)grub_uint16_t height;grub_int16_t xoff;grub_int16_t yoff; -grub_int16_t dwidth; +grub_uint16_t dwidth;int len;if (index_entry->glyph)@@ -760,7 +778,18 @@ grub_font_get_glyph_internal (grub_font_t font, grub_uint32_t code) || read_be_uint16 (font->file, &height) != 0 || read_be_int16 (font->file, &xoff) != 0 || read_be_int16 (font->file, &yoff) != 0 - || read_be_int16 (font->file, &dwidth) != 0) + || read_be_uint16 (font->file, &dwidth) != 0) + { +remove_font (font); +return 0 ; + } + +/* Reject illegal glyphs. */ +if (width > font->max_char_width + || height > font->max_char_height + || xoff < -GRUB_FONT_MAX_DIMENSION || xoff >GRUB_FONT_MAX_DIMENSION + || yoff < -GRUB_FONT_MAX_DIMENSION || yoff >GRUB_FONT_MAX_DIMENSION + || dwidth >GRUB_FONT_MAX_DIMENSION ) {remove_font (font);return 0 ;@@ -780,6 +809,8 @@ grub_font_get_glyph_internal (grub_font_t font, grub_uint32_t code) glyph->offset_x = xoff; glyph->offset_y = yoff; glyph->device_width = dwidth; + glyph->scaled_bitmap =NULL ; + glyph->scale =0 ;/* Don't try to read empty bitmaps (e.g., space characters). */ if (len !=0 )@@ -787,6 +818,7 @@ grub_font_get_glyph_internal (grub_font_t font, grub_uint32_t code)if (grub_file_read (font->file, glyph->bitmap, len) != len) {remove_font (font); +grub_free (glyph->scaled_bitmap);grub_free (glyph);return 0 ; }@@ -1054,6 +1086,8 @@ grub_font_dup_glyph (struct grub_font_glyph *glyph)return NULL ;grub_memcpy (ret, glyph,sizeof (*ret) + (glyph->width * glyph->height +7 ) /8 ); + ret->scaled_bitmap =NULL ; + ret->scale =0 ; return ret; }#endif @@ -1524,11 +1558,22 @@ grub_font_construct_glyph (grub_font_t hinted_font,if (max_glyph_size <sizeof (*glyph) + (bounds.width * bounds.height +GRUB_CHAR_BIT -1 ) /GRUB_CHAR_BIT ) { -grub_free (glyph); +if (glyph) + { +grub_free (glyph->scaled_bitmap); +grub_free (glyph); + } max_glyph_size = (sizeof (*glyph) + (bounds.width * bounds.height +GRUB_CHAR_BIT -1 ) /GRUB_CHAR_BIT ) *2 ;if (max_glyph_size <8 ) max_glyph_size =8 ; glyph =grub_malloc (max_glyph_size); +if (glyph) + { + glyph->scaled_bitmap =NULL ; + glyph->scale =0 ; + } +else + max_glyph_size =0 ; }if (!glyph) {@@ -1536,6 +1581,7 @@ grub_font_construct_glyph (grub_font_t hinted_font,return main_glyph; } +grub_free (glyph->scaled_bitmap);grub_memset (glyph,0 ,sizeof (*glyph) + (bounds.width * bounds.height +GRUB_CHAR_BIT -1 ) /GRUB_CHAR_BIT );@@ -1545,6 +1591,8 @@ grub_font_construct_glyph (grub_font_t hinted_font, glyph->height = bounds.height; glyph->offset_x = bounds.x; glyph->offset_y = bounds.y; + glyph->scaled_bitmap =NULL ; + glyph->scale =0 ;if (glyph_id->attributes &GRUB_UNICODE_GLYPH_ATTRIBUTE_MIRROR ) grub_font_blit_glyph_mirror (glyph, main_glyph,
grub-core/font/font.c grub_font_construct_glyph (grub_font_t hinted_font, · line 1616
Quoted 6 lines
grub-core/font/font.c +3
-0@@ -1563,12 +1611,50 @@ grub_font_construct_glyph (grub_font_t hinted_font,return glyph; } +/* Try to scale the glyph bitmap. If failed, glyph will not be touched. +If succeeded, scaled bitmap will be stored at glyph->scaled_bitmap , and +the effective scale factor will be stored at glyph->scale . */
Incorrectly formatted comment. Please fix this. Here you can find more details about comments formatting [1].
Quoted 6 lines
grub-core/font/font.c +6
-0+static void +try_scale_glyph (struct grub_font_glyph * glyph,int scale) +{ +int i, j; +grub_uint8_t *bitmap; +
grub-core/font/font.c grub_font_construct_glyph (grub_font_t hinted_font, · line 1632
Quoted 10 lines
grub-core/font/font.c +10
-0+if (glyph->scale == scale) +return ; + +if (scale >GRUB_FONT_MAX_SCALE + || glyph->height * scale >GRUB_FONT_MAX_DIMENSION + || glyph->width * scale >GRUB_FONT_MAX_DIMENSION ) +return ; + + bitmap =grub_zalloc (((glyph->width * scale) * + (glyph->height * scale) +7 ) /8 );
grub_calloc()? And I think you should use grub_mul() and grub_add() macros here and below...
grub-core/font/font.c grub_font_construct_glyph (grub_font_t hinted_font, · line 1633
Quoted 1 line
grub-core/font/font.c +1
-0+if (!bitmap)
if (bitmap = NULL)
Please use NULL explicitly in such comparisons.
Quoted 45 lines
grub-core/font/font.c +29
-4+return ; + +/* FIXME: suboptimal. */ +for (i =0 ; i < glyph->height * scale; i++) +for (j =0 ; j < glyph->width * scale; j++) + { +int n = (i / scale) * glyph->width + (j / scale); +int m = i * (glyph->width * scale) + j; +int pixel = (glyph->bitmap[n /8 ] >> (7 - n %8 )) &1 ; + bitmap[m /8 ] |= pixel << (7 - m %8 ); + } + +grub_free (glyph->scaled_bitmap); + glyph->scaled_bitmap = bitmap; + glyph->scale = scale; +} +/* Draw the specified glyph at (x, y). The y coordinate designates the baseline of the character, while the x coordinate designates the left side location of the character. */ grub_err_t grub_font_draw_glyph (struct grub_font_glyph * glyph, -grub_video_color_t color,int left_x,int baseline_y) +grub_video_color_t color,int left_x,int baseline_y, +int scale) {struct grub_video_bitmap glyph_bitmap;@@ -1601,11 +1687,29 @@ grub_font_draw_glyph (struct grub_font_glyph * glyph, &glyph_bitmap.mode_info.fg_alpha); glyph_bitmap.data = glyph->bitmap; -int bitmap_left = left_x + glyph->offset_x; -int bitmap_bottom = baseline_y - glyph->offset_y; -int bitmap_top = bitmap_bottom - glyph->height; +if (scale >1 ) + { +try_scale_glyph (glyph, scale); +if (glyph->scale == scale) + { + glyph_bitmap.mode_info.width = glyph->width * scale; + glyph_bitmap.mode_info.height = glyph->height * scale; + glyph_bitmap.mode_info.pitch = glyph->width * scale; + glyph_bitmap.data = glyph->scaled_bitmap; + }
grub-core/font/font.c grub_font_draw_glyph (struct grub_font_glyph * glyph, · line 1709
Quoted 10 lines
grub-core/font/font.c +10
-0+else + { +/* Scaled bitmap not suitable, fallback to no-scale. */ + scale =1 ; + } + } + +int bitmap_left = left_x + glyph->offset_x * scale; +int bitmap_bottom = baseline_y - glyph->offset_y * scale; +int bitmap_top = bitmap_bottom - glyph->height * scale;
You do a lot of scaling math here and there. It would be probably better if you use safe math macros wrapped in some scaling functions/macros.
Quoted 37 lines
grub-core/font/font.c +2
-1return grub_video_blit_bitmap (&glyph_bitmap,GRUB_VIDEO_BLIT_BLEND , bitmap_left, bitmap_top, -0 ,0 , glyph->width, glyph->height); +0 ,0 , + glyph->width * scale, glyph->height * scale); }grub-core/gfxmenu/font.c +1
-1@@ -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@@ -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 +0
-0
include/grub/font.h line 30
Quoted 7 lines
include/grub/font.h +4
-0@@ -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. */
Please fix this comment formatting.
include/grub/font.h line 36
Quoted 6 lines
include/grub/font.h +3
-0+#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. */
Ditto and below please...
Quoted 14 lines
include/grub/font.h +6
-0@@ -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.
include/grub/font.h struct grub_font_glyph *EXPORT_FUNC (grub_font_get_glyph_with_fallback) (grub_fo · line 161
Quoted 9 lines
include/grub/font.h +2
-1@@ -141,7 +154,8 @@ struct grub_font_glyph *EXPORT_FUNC (grub_font_get_glyph_with_fallback) (grub_fogrub_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,
Daniel
[1] https://www.gnu.org/software/grub/manual/grub-dev/grub-dev.html#Comments
PATCH RESEND v3 2/2 term/gfxterm: Preliminary HiDPI support
In reply to Zhang BoyangCurrently 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
@@ -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
@@ -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
@@ -29,7 +29,8 @@ grub_err_tEXPORT_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
Re: [PATCH RESEND v3 2/2] term/gfxterm: Preliminary HiDPI support
Quoted 86 lines · 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.Signed-off-by Zhang Boyang <[email protected]>--- 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@@ -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_cpugrub-core/gfxmenu/view.c +1
-0@@ -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 +3
-1@@ -82,6 +82,7 @@ struct grub_virtual_screen/* Font settings. */ grub_font_t font; +unsigned int scale;/* Terminal color settings. */ grub_uint8_t standard_color_setting;@@ -204,7 +205,7 @@ grub_virtual_screen_free (void)static grub_err_t grub_virtual_screen_setup (unsigned int x,unsigned int y,unsigned int width,unsigned int height, -grub_font_t font) +grub_font_t font,unsigned int scale) {unsigned int i;@@ -213,6 +214,7 @@ grub_virtual_screen_setup (unsigned int x, unsigned int y,/* Initialize with default data. */ virtual_screen.font = font; + virtual_screen.scale = scale; virtual_screen.width = width; virtual_screen.height = height; virtual_screen.offset_x = x;
grub-core/term/gfxterm.c grub_virtual_screen_setup (unsigned int x, unsigned int y, · line 225
Quoted 5 lines
grub-core/term/gfxterm.c +1
-1@@ -220,9 +222,9 @@ grub_virtual_screen_setup (unsigned int x, unsigned int y, virtual_screen.normal_char_width =calculate_normal_character_width (virtual_screen.font); virtual_screen.normal_char_height = -grub_font_get_max_char_height (virtual_screen.font); +grub_font_get_max_char_height (virtual_screen.font) * virtual_screen.scale;
Another good place for safe math macro...
grub-core/term/gfxterm.c grub_virtual_screen_setup (unsigned int x, unsigned int y, · line 227
Quoted 3 lines
grub-core/term/gfxterm.c +1
-1if (virtual_screen.normal_char_height ==0 ) - virtual_screen.normal_char_height =16 ; + virtual_screen.normal_char_height =16 * virtual_screen.scale;
... and here and below...
Quoted 43 lines
grub-core/term/gfxterm.c +8
-5virtual_screen.cursor_x =0 ; virtual_screen.cursor_y =0 ; virtual_screen.cursor_state =1 ;@@ -297,7 +299,8 @@ 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) + grub_font_t font, int scale, + int border_width) {/* Clean up any prior instance. */ destroy_window ();@@ -306,10 +309,10 @@ grub_gfxterm_set_window (struct grub_video_render_target *target, render_target = target;/* Create virtual screen. */ -if (grub_virtual_screen_setup (border_width, border_width, - width -2 * border_width, - height -2 * border_width, - font) +if (grub_virtual_screen_setup (border_width * scale, border_width * scale, + width -2 * border_width * scale, + height -2 * border_width * scale, + font, scale) !=GRUB_ERR_NONE ) {return grub_errno;@@ -332,11 +335,13 @@ static grub_err_tgrub_gfxterm_fullscreen (void) {const char *font_name; +const char *scale_conf;struct grub_video_mode_info mode_info;grub_video_color_t color;grub_err_t err;int double_redraw;grub_font_t font; +int scale; err =grub_video_get_info (&mode_info);/* Figure out what mode we ended up. */
grub-core/term/gfxterm.c grub_gfxterm_fullscreen (void) · line 377
Quoted 7 lines
grub-core/term/gfxterm.c +4
-0@@ -366,12 +371,34 @@ grub_gfxterm_fullscreen (void)if (!font)return grub_error (GRUB_ERR_BAD_FONT ,"no font loaded" ); +/* Decide font scale factor. */ + scale_conf =grub_env_get ("gfxterm_scale" ); + scale =0 ; +if (scale_conf)
if (scale_conf != NULL)
grub-core/term/gfxterm.c grub_gfxterm_fullscreen (void) · line 378
Quoted 1 line
grub-core/term/gfxterm.c +1
-0+ scale = (int )grub_strtoul (scale_conf,0 ,0 );
You completely ignore grub_strtoul() errors. Please check commit ac8a37dda (net/http: Allow use of non-standard TCP/IP ports) how it should be done.
grub-core/term/gfxterm.c grub_gfxterm_fullscreen (void) · line 387
Quoted 9 lines
grub-core/term/gfxterm.c +9
-0+if (scale <0 || scale >GRUB_FONT_MAX_SCALE ) + scale =0 ; +if (scale ==0 ) + { +if (mode_info.width >7680 && mode_info.height >4320 ) + scale =8 ; +else if (mode_info.width >3840 && mode_info.height >2160 ) + scale =4 ; +else if (mode_info.width >1920 && mode_info.height >1080 )
I would prefer if we come up with some proper math for this instead of hard coding it like above.
Quoted 65 lines
grub-core/term/gfxterm.c +16
-8+ scale =2 ; + else + scale =1 ; + } +if (grub_font_get_max_char_width (font) * scale >GRUB_FONT_MAX_DIMENSION + ||grub_font_get_max_char_height (font) * scale >GRUB_FONT_MAX_DIMENSION ) + scale =1 ; + 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 ); + font, scale,DEFAULT_BORDER_WIDTH ); } static grub_err_t@@ -642,7 +669,7 @@ paint_char (unsigned cx, unsigned cy) grub_errno =GRUB_ERR_NONE ;return ; } - ascent =grub_font_get_ascent (virtual_screen.font); + ascent =grub_font_get_ascent (virtual_screen.font) * virtual_screen.scale; width = virtual_screen.normal_char_width *calculate_character_width (glyph); height = virtual_screen.normal_char_height;@@ -656,7 +683,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,1 ); +grub_font_draw_glyph (glyph, color, x, y + ascent, virtual_screen.scale);grub_video_set_active_render_target (render_target);/* Mark character to be drawn. */ @@ -690,9 +717,9 @@ draw_cursor (int show)return ;/* Ensure that cursor doesn't go outside of character box. */ - ascent =grub_font_get_ascent (virtual_screen.font); -if (ascent > virtual_screen.normal_char_height -2 ) - ascent = virtual_screen.normal_char_height -2 ; + ascent =grub_font_get_ascent (virtual_screen.font) * virtual_screen.scale; +if (ascent > virtual_screen.normal_char_height -2 * virtual_screen.scale) + ascent = virtual_screen.normal_char_height -2 * virtual_screen.scale;/* Determine cursor properties and position on text layer. */ x = virtual_screen.cursor_x * virtual_screen.normal_char_width;@@ -701,7 +728,7 @@ draw_cursor (int show) y = ((virtual_screen.cursor_y + virtual_screen.total_scroll) * virtual_screen.normal_char_height + ascent); - height =2 ; + height =2 * virtual_screen.scale;/* Render cursor to text layer. */ grub_video_set_active_render_target (text_layer);@@ -957,18 +984,18 @@ calculate_normal_character_width (grub_font_t font) width = glyph->device_width; }if (!width) -return 8 ; +return 8 * virtual_screen.scale;
grub-core/term/gfxterm.c calculate_normal_character_width (grub_font_t font) · line 996
Quoted 10 lines
grub-core/term/gfxterm.c +2
-2-return width; +return width * virtual_screen.scale; }static unsigned char calculate_character_width (struct grub_font_glyph *glyph) {if (! glyph || glyph->device_width ==0 ) -return 1 ; +return 1 * virtual_screen.scale;
? I would just do "return virtual_screen.scale"...
Daniel
Re: [PATCH RESEND v3 2/2] term/gfxterm: Preliminary HiDPI support
In reply to Zhang BoyangDear Zhang,
Thank you for your patches for a much needed feature.
Maybe make the commit message summary/title a statement:
term/gfxterm: Add preliminary HiDPI support
Am 27.06.22 um 11:35 schrieb Zhang Boyang:
Quoted 6 lines
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.
Trying to work around the issue, I found the answer to *How can I change the resolution of the GRUB menu?* [1] saying to create bigger fonts, referencing *Increase Font in GRUB for High DPI Screens* [2]:
sudo grub-mkfont --output=/boot/grub/fonts/DejaVuSansMono24.pf2 \
--size=24 /usr/share/fonts/truetype/dejavu/DejaVuSansMono.ttf
GRUB_FONT=/boot/grub/fonts/DejaVuSansMono24.pf2
Can you elaborate how your scaling approach works quality wise? (I have no idea about the font format (pixel, vector?).)
Quoted 21 lines
Signed-off-by Zhang Boyang <[email protected]>--- 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 +1
-0@@ -3274,6 +3274,7 @@ These variables have special meaning to GRUB. * gfxmode:: * gfxpayload:: * gfxterm_font:: +* gfxterm_scale:: * grub_cpu:: * grub_platform:: * icondir::
docs/grub.texi If this variable is set, it names a font to use for text on the · line 3556
Quoted 8 lines
docs/grub.texi +5
-0@@ -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
*the* graphical terminal
How will it be scaled?
docs/grub.texi If this variable is set, it names a font to use for text on the · line 3558
Quoted 2 lines
docs/grub.texi +2
-0+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
… if the user doesn’t want …
Quoted 110 lines
docs/grub.texi +3
-0+@samp{gfxterm} to scale the font on screen. + + @node grub_cpu @subsection grub_cpugrub-core/gfxmenu/view.c +1
-0@@ -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 +21
-8@@ -82,6 +82,7 @@ struct grub_virtual_screen/* Font settings. */ grub_font_t font; +unsigned int scale;/* Terminal color settings. */ grub_uint8_t standard_color_setting;@@ -204,7 +205,7 @@ grub_virtual_screen_free (void)static grub_err_t grub_virtual_screen_setup (unsigned int x,unsigned int y,unsigned int width,unsigned int height, -grub_font_t font) +grub_font_t font,unsigned int scale) {unsigned int i;@@ -213,6 +214,7 @@ grub_virtual_screen_setup (unsigned int x, unsigned int y,/* Initialize with default data. */ virtual_screen.font = font; + virtual_screen.scale = scale; virtual_screen.width = width; virtual_screen.height = height; virtual_screen.offset_x = x;@@ -220,9 +222,9 @@ grub_virtual_screen_setup (unsigned int x, unsigned int y, virtual_screen.normal_char_width =calculate_normal_character_width (virtual_screen.font); virtual_screen.normal_char_height = -grub_font_get_max_char_height (virtual_screen.font); +grub_font_get_max_char_height (virtual_screen.font) * virtual_screen.scale;if (virtual_screen.normal_char_height ==0 ) - virtual_screen.normal_char_height =16 ; + virtual_screen.normal_char_height =16 * virtual_screen.scale; virtual_screen.cursor_x =0 ; virtual_screen.cursor_y =0 ; virtual_screen.cursor_state =1 ;@@ -297,7 +299,8 @@ 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) + grub_font_t font, int scale, + int border_width) {/* Clean up any prior instance. */ destroy_window ();@@ -306,10 +309,10 @@ grub_gfxterm_set_window (struct grub_video_render_target *target, render_target = target;/* Create virtual screen. */ -if (grub_virtual_screen_setup (border_width, border_width, - width -2 * border_width, - height -2 * border_width, - font) +if (grub_virtual_screen_setup (border_width * scale, border_width * scale, + width -2 * border_width * scale, + height -2 * border_width * scale, + font, scale) !=GRUB_ERR_NONE ) {return grub_errno;@@ -332,11 +335,13 @@ static grub_err_tgrub_gfxterm_fullscreen (void) {const char *font_name; +const char *scale_conf;struct grub_video_mode_info mode_info;grub_video_color_t color;grub_err_t err;int double_redraw;grub_font_t font; +int scale; err =grub_video_get_info (&mode_info);/* Figure out what mode we ended up. */ @@ -366,12 +371,34 @@ grub_gfxterm_fullscreen (void)if (!font)return grub_error (GRUB_ERR_BAD_FONT ,"no font loaded" ); +/* Decide font scale factor. */ + scale_conf =grub_env_get ("gfxterm_scale" ); + scale =0 ; +if (scale_conf) + scale = (int )grub_strtoul (scale_conf,0 ,0 ); +if (scale <0 || scale >GRUB_FONT_MAX_SCALE ) + scale =0 ; +if (scale ==0 )
grub-core/term/gfxterm.c grub_gfxterm_fullscreen (void) · line 391
Quoted 10 lines
grub-core/term/gfxterm.c +10
-0+ { +if (mode_info.width >7680 && mode_info.height >4320 ) + scale =8 ; +else if (mode_info.width >3840 && mode_info.height >2160 ) + scale =4 ; +else if (mode_info.width >1920 && mode_info.height >1080 ) + scale =2 ; +else + scale =1 ; + }
Would checking the width be enough, and then set scale to mode_info.width % 1920?
Quoted 97 lines
grub-core/term/gfxterm.c +19
-14+if (grub_font_get_max_char_width (font) * scale >GRUB_FONT_MAX_DIMENSION + ||grub_font_get_max_char_height (font) * scale >GRUB_FONT_MAX_DIMENSION ) + scale =1 ; + 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 ); + font, scale,DEFAULT_BORDER_WIDTH ); } static grub_err_t@@ -642,7 +669,7 @@ paint_char (unsigned cx, unsigned cy) grub_errno =GRUB_ERR_NONE ;return ; } - ascent =grub_font_get_ascent (virtual_screen.font); + ascent =grub_font_get_ascent (virtual_screen.font) * virtual_screen.scale; width = virtual_screen.normal_char_width *calculate_character_width (glyph); height = virtual_screen.normal_char_height;@@ -656,7 +683,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,1 ); +grub_font_draw_glyph (glyph, color, x, y + ascent, virtual_screen.scale);grub_video_set_active_render_target (render_target);/* Mark character to be drawn. */ @@ -690,9 +717,9 @@ draw_cursor (int show)return ;/* Ensure that cursor doesn't go outside of character box. */ - ascent =grub_font_get_ascent (virtual_screen.font); -if (ascent > virtual_screen.normal_char_height -2 ) - ascent = virtual_screen.normal_char_height -2 ; + ascent =grub_font_get_ascent (virtual_screen.font) * virtual_screen.scale; +if (ascent > virtual_screen.normal_char_height -2 * virtual_screen.scale) + ascent = virtual_screen.normal_char_height -2 * virtual_screen.scale;/* Determine cursor properties and position on text layer. */ x = virtual_screen.cursor_x * virtual_screen.normal_char_width;@@ -701,7 +728,7 @@ draw_cursor (int show) y = ((virtual_screen.cursor_y + virtual_screen.total_scroll) * virtual_screen.normal_char_height + ascent); - height =2 ; + height =2 * virtual_screen.scale;/* Render cursor to text layer. */ grub_video_set_active_render_target (text_layer);@@ -957,18 +984,18 @@ calculate_normal_character_width (grub_font_t font) width = glyph->device_width; }if (!width) -return 8 ; +return 8 * virtual_screen.scale; -return width; +return width * virtual_screen.scale; }static unsigned char calculate_character_width (struct grub_font_glyph *glyph) {if (! glyph || glyph->device_width ==0 ) -return 1 ; +return 1 * virtual_screen.scale; -return (glyph->device_width +return (glyph->device_width * virtual_screen.scale + (virtual_screen.normal_char_width -1 )) / virtual_screen.normal_char_width; }@@ -981,10 +1008,11 @@ grub_gfxterm_getcharwidth (struct grub_term_output *term __attribute__ ((unused) dev_width =grub_font_get_constructed_device_width (virtual_screen.font, c);if (dev_width ==0 ) -return 1 ; +return 1 * virtual_screen.scale; -return (dev_width + (virtual_screen.normal_char_width -1 )) - / virtual_screen.normal_char_width; +return (dev_width * virtual_screen.scale + + (virtual_screen.normal_char_width -1 )) + / virtual_screen.normal_char_width; } static struct grub_term_coordinateinclude/grub/gfxterm.h +0
-0
include/grub/gfxterm.h grub_err_t · line 36
Quoted 9 lines
include/grub/gfxterm.h +2
-1@@ -29,7 +29,8 @@ grub_err_tEXPORT_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 );
Kind regards,
Paul
[1]: https://askubuntu.com/questions/1091778/how-can-i-change-the-resolution-of-the-grub-menu
[2]: https://blog.wxm.be/2014/08/29/increase-font-in-grub-for-high-dpi.html