Skip to content

lvfontio doesn't consider ascent/descent #10850

@FoamyGuy

Description

@FoamyGuy

Currently the lvfontio module reads the font_size and ascent out of the font file header.

// Skip version (4 bytes) and padding (1 byte)
// Parse font metrics at offset 6
self->header.font_size = head_buf[6] | (head_buf[7] << 8);
self->header.ascent = head_buf[8] | (head_buf[9] << 8);
self->header.default_advance_width = head_buf[22] | (head_buf[23] << 8);

and it uses the font_size as the height returned by common_hal_lvfontio_ondiskfont_get_dimensions

if (height != NULL) {
*height = self->header.font_size;
}

But it seems that font_size may not fully encompass all of the vertical pixels the font requires. I have noticed that >1bpp versions of the same font have a higher ascent value but the same font_size

Loaded 4bpp font from fonts/unifont-16.0.02-en_US_4bb.lvfontbin
Successfully loaded LVGL font
Font metrics:
  Ascent: 15
  Descent: -4
  font_size: 16
  Bounding box: width=0, height=16, x_offset=0, y_offset=-4

Loaded 1bpp font from fonts/unifont-16.0.02-en_US.lvfontbin
Successfully loaded LVGL font
Font metrics:
  Ascent: 14
  Descent: -4
  font_size: 16
  Bounding box: width=0, height=16, x_offset=0, y_offset=-4

This seems to be the root cause of the descenders or bottom row(s) of pixels getting cut off when attempting to set the terminal to use a font that is >1bpp

Default font descenders visible 'y' and 'g' characters:

Image

2bpp version of same font descenders clipped:

Image

adafruit_bitmap_font does not seem to have the same issue, using that and BitmapLabel the descenders are rendered in full.

Codex suggested this patch to resolve it by reading the descent value from the header and using it + ascent to calculate the full size.
lvfontio-descent-header.patch

That does work to stop the bottom from getting clipped, but it means that the tile size for the Terminal is bigger, more space in between rows, and doesn't evenly align or evenly divide into the display size.

Image

In practice it seems like for this font (unifont v16.0.02) that the descent doesn't actually use the full ascent + descent range available at least on standard ASCII glyphs. The 'y' and 'g' each only seem to be missing 1 pixel row which to me looks like it matches the 1px difference in ascent value between the two fonts. When using the ascent + decent calculation it will end up at 18 or 19px for height instead of the 16 that we have now, or the 17 that seem like it would fit everything.

I'm interested in thoughts on whether this is something we want to try to resolve, and if so how best to do it in a way that ideally doesn't change the current default fonts terminal size, but does still allow custom >1bpp fonts to render without clipping.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions