Created
April 20, 2024 11:58
-
-
Save cwalther/fb2b2ca00c59b0577089fbdc4d7e0a88 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from framebuf import FrameBuffer, MONO_HMSB, MONO_HLSB | |
# currently only works for monochrome (fg, bg in {-1, 0, 1}) | |
class KeyedPalette(FrameBuffer): | |
def __init__(self, fg=0, bg=-1): | |
buf = bytearray(1) | |
if fg == -1: | |
self.key = fg = bg^1 | |
elif bg == -1: | |
self.key = bg = fg^1 | |
else: | |
self.key = -1 | |
buf[0] = (fg << 1) | bg | |
super().__init__(buf, 2, 1, MONO_HMSB) | |
def write(fb, s, x, y, font, palette=None): | |
for char in s: | |
glyph, char_height, char_width = font.get_ch(char) | |
if glyph: | |
fbc = FrameBuffer(bytearray(glyph), char_width, char_height, MONO_HMSB if font.reverse() else MONO_HLSB) | |
fb.blit(fbc, x, y, getattr(palette, 'key', -1), palette) | |
x += char_width | |
def measure(s, font): | |
w = 0 | |
for char in s: | |
glyph, h, cw = font.get_ch(char) | |
w += cw | |
return w, h |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Glad to help!