Skip to content

lvfontio: pass bits per value, not the value count, to the bitmap - #12031

Open
YORROY123 wants to merge 2 commits into
adafruit:mainfrom
YORROY123:lvfontio-bits-per-value
Open

YORROY123 wants to merge 2 commits into
adafruit:mainfrom
YORROY123:lvfontio-bits-per-value

Conversation

@YORROY123

Copy link
Copy Markdown

common_hal_lvfontio_ondiskfont_construct() creates the glyph cache bitmap with 1 << self->header.bits_per_pixel as the bits per value. common_hal_displayio_bitmap_construct_from_buffer() takes bits per value, so this passes the number of values instead (the Python Bitmap() constructor converts value_count to bits before calling it). As a result:

  • 1, 2 and 4 bpp fonts get 2, 4 and 16 bits per pixel, so the cache takes 2x, 2x and 4x the memory it needs. This also applies to the supervisor terminal font, which is allocated on the port heap at boot.
  • An 8 bpp font asks for 256 bits per value and fails with NotImplementedError: Invalid bits per value.

This rounds the font's bpp up to a power of two (1, 2, 3→4, 4, 8) and uses it both for the buffer stride and for the bitmap.

Testing: I don't have a board with a display at hand, so I built shared-module/lvfontio/OnDiskFont.c and the bitmap functions from shared-module/displayio/legacy/Bitmap.c on the host with small stubs for the VM and the supervisor file API, under ASan/UBSan, and loaded small generated .lvfontbin files:

font bpp before: bits per value / buffer after
1 2 / 16 B 1 / 16 B
2 4 / 16 B 2 / 16 B
3 8 / 32 B 4 / 16 B
4 16 / 64 B 4 / 16 B
8 NotImplementedError 8 / 32 B

(8 glyph cache slots of 4x4 px.) In every case the pixel values read back from the bitmap after caching a glyph match the font data. Nothing else reads bits_per_value of the font bitmap; terminalio and the supervisor display only use the pixel values.

The bug and fix were found and tested with Claude Code (Claude Opus 5.5); I reviewed the change.

The glyph cache bitmap was created with 1 << bits_per_pixel as its bits
per value. That is the number of values, so 1, 2 and 4 bpp fonts used 2,
4 and 16 bits per pixel, and an 8 bpp font failed with "Invalid bits per
value". Round the font's bpp up to a power of two instead.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant