Prerender text and blit to correct position

Fixes #1.
This commit is contained in:
Markus Koch 2020-07-05 19:31:07 +02:00
parent 34592b6060
commit a3f12aa96b

View File

@ -16,6 +16,7 @@ class display(object):
self.font_info = ImageFont.truetype('/usr/share/fonts/TTF/DejaVuSans.ttf', 10) self.font_info = ImageFont.truetype('/usr/share/fonts/TTF/DejaVuSans.ttf', 10)
self.img = Image.new('1', (self.width, self.height), color = not self.fg) self.img = Image.new('1', (self.width, self.height), color = not self.fg)
self.d = ImageDraw.Draw(self.img) self.d = ImageDraw.Draw(self.img)
self.bar_redraw = True
self.dbg = 0 self.dbg = 0
def render(self): def render(self):
@ -28,9 +29,8 @@ class display(object):
print("animate NYI") print("animate NYI")
def get_image(self): def get_image(self):
#self.img.save('pil_text_font.png') self.img.save("/tmp/wr/frame%d.png" % self.dbg)
#self.img.save("/tmp/wr/frame%d.png" % self.dbg) self.img.tobytes()
print(str(len(self.img.tobytes())))
self.dbg += 1 self.dbg += 1
class display_playback(display): class display_playback(display):
@ -41,6 +41,7 @@ class display_playback(display):
self.title_scroll_max = -1 self.title_scroll_max = -1
self.info = "" self.info = ""
self.syms = []; self.syms = [];
self.title_img = Image.new('1', (0, 0), color = not self.fg)
def update_text(self, title): def update_text(self, title):
self.title = title self.title = title
@ -51,45 +52,55 @@ class display_playback(display):
self.title_scroll_max,h = self.d.textsize(self.title, font=self.font_title) self.title_scroll_max,h = self.d.textsize(self.title, font=self.font_title)
self.title_scroll = self.title_scroll_max - 10 self.title_scroll = self.title_scroll_max - 10
self.title += self.title # Wraparound for scrolling (pt2) self.title += self.title # Wraparound for scrolling (pt2)
# Prerender
w,h = self.d.textsize(self.title, font=self.font_title)
self.title_img = Image.new('1', (w, h), color = not self.fg)
font_shift = 5
ImageDraw.Draw(self.title_img).text((0,-font_shift), self.title, font=self.font_title, fill=self.fg)
self.title_img = self.title_img.crop((0,0,w,h-font_shift))
else: else:
self.title_scroll = 0 self.title_scroll = 0
self.title_scroll_max = -1 self.title_scroll_max = -1
def update_info(self, info): def update_info(self, info):
self.info = info self.info = info
self.bar_redraw = True
def update_syms(self, syms): def update_syms(self, syms):
self.syms = syms; self.syms = syms
self.bar_redraw = True
def scroll_title(self, amount = 1): def scroll_title(self, amount = 1):
if (self.title_scroll >=0): if (self.title_scroll >= 0):
self.title_scroll = (self.title_scroll + amount) % self.title_scroll_max self.title_scroll = (self.title_scroll + amount) % self.title_scroll_max
def animate(self, ticks): def animate(self, ticks):
self.scroll_title(ticks) self.scroll_title(ticks)
def render(self): def _render_bar(self):
# Clear framebuffer # Clear framebuffer
self.d.rectangle((0,0,self.width,self.height), fill = not self.fg) self.d.rectangle((0,self.height - 1 - 10,self.width,self.height), fill = self.fg)
# Title
self.d.text((-self.title_scroll,-5), self.title, font=self.font_title, fill=self.fg)
# Playback info # Playback info
w,h = self.d.textsize(self.info, font=self.font_info) w,h = self.d.textsize(self.info, font=self.font_info)
self.d.text((self.width-w, self.height - 1 - 10 - 1), self.info, font=self.font_info, fill=self.fg) self.d.text((self.width-w, self.height - 1 - 10 - 1), self.info, font=self.font_info, fill=not self.fg)
# Status icons # Status icons
status = "" status = "".join([" " + icon + " " for icon in self.syms])
status = status.join([" " + icon + " " for icon in self.syms]) self.d.text((-1, self.height - 1 - 10 - 1), status, font=self.font_info, fill=not self.fg)
self.d.text((-1, self.height - 1 - 10 - 1), status, font=self.font_info, fill=self.fg)
def render(self):
# Title (prerendered bitmap to shift)
self.img.paste(self.title_img, (-self.title_scroll,0))
# Other things were pre-rendered to the framebuffer directly
if (self.bar_redraw):
self.bar_redraw = False
self._render_bar()
# TODO: Maybe a progress bar? # TODO: Maybe a progress bar?
#self.d.line((0,15, 127,15), fill=self.fg) #self.d.line((0,15, 127,15), fill=self.fg)
# Serialize
# TODO: Serialize
class display_manager(object): class display_manager(object):
def __init__(self): def __init__(self):
self.tick = 0 self.tick = 0