2018-08-14 16:22:21 +00:00
|
|
|
# writer.py Implements the Writer class.
|
2021-01-30 17:38:59 +00:00
|
|
|
# Handles colour, word wrap and tab stops
|
|
|
|
|
2021-09-07 09:31:25 +00:00
|
|
|
# V0.5.0 Sep 2021 Color now requires firmware >= 1.17.
|
2021-08-28 16:17:40 +00:00
|
|
|
# V0.4.3 Aug 2021 Support for fast blit to color displays (PR7682).
|
|
|
|
# V0.4.0 Jan 2021 Improved handling of word wrap and line clip. Upside-down
|
2021-01-30 17:38:59 +00:00
|
|
|
# rendering no longer supported: delegate to device driver.
|
2021-08-28 16:17:40 +00:00
|
|
|
# V0.3.5 Sept 2020 Fast rendering option for color displays
|
2018-08-14 16:22:21 +00:00
|
|
|
|
2020-11-02 16:30:46 +00:00
|
|
|
# Released under the MIT License (MIT). See LICENSE.
|
2021-01-31 17:53:24 +00:00
|
|
|
# Copyright (c) 2019-2021 Peter Hinch
|
2018-08-14 16:22:21 +00:00
|
|
|
|
|
|
|
# A Writer supports rendering text to a Display instance in a given font.
|
|
|
|
# Multiple Writer instances may be created, each rendering a font to the
|
|
|
|
# same Display object.
|
|
|
|
|
2021-08-31 07:54:15 +00:00
|
|
|
# Timings were run on a pyboard D SF6W comparing slow and fast rendering
|
|
|
|
# and averaging over multiple characters. Proportional fonts were used.
|
|
|
|
# 20 pixel high font, timings were 5.44ms/467μs, gain 11.7 (freesans20).
|
|
|
|
# 10 pixel high font, timings were 1.76ms/396μs, gain 4.36 (arial10).
|
2018-08-14 16:22:21 +00:00
|
|
|
|
2020-10-02 16:50:51 +00:00
|
|
|
|
2018-08-14 16:22:21 +00:00
|
|
|
import framebuf
|
2020-11-06 09:17:15 +00:00
|
|
|
from uctypes import bytearray_at, addressof
|
2021-08-28 16:17:40 +00:00
|
|
|
from sys import implementation
|
|
|
|
import os
|
|
|
|
|
2021-09-07 09:31:25 +00:00
|
|
|
__version__ = (0, 5, 0)
|
2021-08-28 16:17:40 +00:00
|
|
|
|
2021-09-07 09:31:25 +00:00
|
|
|
fast_mode = True # Does nothing. Kept to avoid breaking code.
|
2021-08-28 16:17:40 +00:00
|
|
|
|
2018-08-14 16:22:21 +00:00
|
|
|
class DisplayState():
|
|
|
|
def __init__(self):
|
|
|
|
self.text_row = 0
|
|
|
|
self.text_col = 0
|
|
|
|
|
|
|
|
def _get_id(device):
|
|
|
|
if not isinstance(device, framebuf.FrameBuffer):
|
|
|
|
raise ValueError('Device must be derived from FrameBuffer.')
|
|
|
|
return id(device)
|
|
|
|
|
|
|
|
# Basic Writer class for monochrome displays
|
|
|
|
class Writer():
|
|
|
|
|
|
|
|
state = {} # Holds a display state for each device
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def set_textpos(device, row=None, col=None):
|
|
|
|
devid = _get_id(device)
|
|
|
|
if devid not in Writer.state:
|
|
|
|
Writer.state[devid] = DisplayState()
|
|
|
|
s = Writer.state[devid] # Current state
|
|
|
|
if row is not None:
|
|
|
|
if row < 0 or row >= device.height:
|
|
|
|
raise ValueError('row is out of range')
|
2021-01-30 17:38:59 +00:00
|
|
|
s.text_row = row
|
2018-08-14 16:22:21 +00:00
|
|
|
if col is not None:
|
|
|
|
if col < 0 or col >= device.width:
|
|
|
|
raise ValueError('col is out of range')
|
2021-01-30 17:38:59 +00:00
|
|
|
s.text_col = col
|
2018-08-14 16:22:21 +00:00
|
|
|
return s.text_row, s.text_col
|
|
|
|
|
|
|
|
def __init__(self, device, font, verbose=True):
|
|
|
|
self.devid = _get_id(device)
|
|
|
|
self.device = device
|
|
|
|
if self.devid not in Writer.state:
|
|
|
|
Writer.state[self.devid] = DisplayState()
|
|
|
|
self.font = font
|
2021-02-06 15:16:39 +00:00
|
|
|
if font.height() >= device.height or font.max_width() >= device.width:
|
|
|
|
raise ValueError('Font too large for screen')
|
2018-08-14 16:22:21 +00:00
|
|
|
# Allow to work with reverse or normal font mapping
|
|
|
|
if font.hmap():
|
|
|
|
self.map = framebuf.MONO_HMSB if font.reverse() else framebuf.MONO_HLSB
|
|
|
|
else:
|
|
|
|
raise ValueError('Font must be horizontally mapped.')
|
|
|
|
if verbose:
|
|
|
|
fstr = 'Orientation: Horizontal. Reversal: {}. Width: {}. Height: {}.'
|
|
|
|
print(fstr.format(font.reverse(), device.width, device.height))
|
|
|
|
print('Start row = {} col = {}'.format(self._getstate().text_row, self._getstate().text_col))
|
|
|
|
self.screenwidth = device.width # In pixels
|
|
|
|
self.screenheight = device.height
|
|
|
|
self.bgcolor = 0 # Monochrome background and foreground colors
|
|
|
|
self.fgcolor = 1
|
2021-02-06 15:16:39 +00:00
|
|
|
self.row_clip = False # Clip or scroll when screen fullt
|
2018-08-14 16:22:21 +00:00
|
|
|
self.col_clip = False # Clip or new line when row is full
|
|
|
|
self.wrap = True # Word wrap
|
|
|
|
self.cpos = 0
|
|
|
|
self.tab = 4
|
|
|
|
|
|
|
|
self.glyph = None # Current char
|
|
|
|
self.char_height = 0
|
|
|
|
self.char_width = 0
|
2021-01-28 13:56:13 +00:00
|
|
|
self.clip_width = 0
|
2018-08-14 16:22:21 +00:00
|
|
|
|
|
|
|
def _getstate(self):
|
|
|
|
return Writer.state[self.devid]
|
|
|
|
|
|
|
|
def _newline(self):
|
|
|
|
s = self._getstate()
|
2018-08-31 10:17:58 +00:00
|
|
|
height = self.font.height()
|
2021-01-30 17:38:59 +00:00
|
|
|
s.text_row += height
|
|
|
|
s.text_col = 0
|
|
|
|
margin = self.screenheight - (s.text_row + height)
|
|
|
|
y = self.screenheight + margin
|
2018-08-14 16:22:21 +00:00
|
|
|
if margin < 0:
|
|
|
|
if not self.row_clip:
|
|
|
|
self.device.scroll(0, margin)
|
|
|
|
self.device.fill_rect(0, y, self.screenwidth, abs(margin), self.bgcolor)
|
|
|
|
s.text_row += margin
|
|
|
|
|
|
|
|
def set_clip(self, row_clip=None, col_clip=None, wrap=None):
|
|
|
|
if row_clip is not None:
|
|
|
|
self.row_clip = row_clip
|
|
|
|
if col_clip is not None:
|
|
|
|
self.col_clip = col_clip
|
|
|
|
if wrap is not None:
|
|
|
|
self.wrap = wrap
|
|
|
|
return self.row_clip, self.col_clip, self.wrap
|
|
|
|
|
2018-08-31 10:17:58 +00:00
|
|
|
@property
|
|
|
|
def height(self): # Property for consistency with device
|
2018-08-14 16:22:21 +00:00
|
|
|
return self.font.height()
|
|
|
|
|
|
|
|
def printstring(self, string, invert=False):
|
|
|
|
# word wrapping. Assumes words separated by single space.
|
2021-01-29 12:49:45 +00:00
|
|
|
q = string.split('\n')
|
|
|
|
last = len(q) - 1
|
|
|
|
for n, s in enumerate(q):
|
2018-08-14 16:22:21 +00:00
|
|
|
if s:
|
|
|
|
self._printline(s, invert)
|
2021-01-29 12:49:45 +00:00
|
|
|
if n != last:
|
2018-08-14 16:22:21 +00:00
|
|
|
self._printchar('\n')
|
|
|
|
|
|
|
|
def _printline(self, string, invert):
|
|
|
|
rstr = None
|
2021-01-29 12:49:45 +00:00
|
|
|
if self.wrap and self.stringlen(string, True): # Length > self.screenwidth
|
2018-08-14 16:22:21 +00:00
|
|
|
pos = 0
|
|
|
|
lstr = string[:]
|
2021-01-29 12:49:45 +00:00
|
|
|
while self.stringlen(lstr, True): # Length > self.screenwidth
|
2018-08-14 16:22:21 +00:00
|
|
|
pos = lstr.rfind(' ')
|
|
|
|
lstr = lstr[:pos].rstrip()
|
|
|
|
if pos > 0:
|
|
|
|
rstr = string[pos + 1:]
|
|
|
|
string = lstr
|
|
|
|
|
|
|
|
for char in string:
|
|
|
|
self._printchar(char, invert)
|
|
|
|
if rstr is not None:
|
|
|
|
self._printchar('\n')
|
|
|
|
self._printline(rstr, invert) # Recurse
|
|
|
|
|
2021-01-29 12:49:45 +00:00
|
|
|
def stringlen(self, string, oh=False):
|
|
|
|
sc = self._getstate().text_col # Start column
|
|
|
|
wd = self.screenwidth
|
2018-08-14 16:22:21 +00:00
|
|
|
l = 0
|
2021-01-29 12:49:45 +00:00
|
|
|
for char in string[:-1]:
|
2018-08-14 16:22:21 +00:00
|
|
|
_, _, char_width = self.font.get_ch(char)
|
2021-01-29 12:49:45 +00:00
|
|
|
l += char_width
|
|
|
|
if oh and l + sc > wd:
|
|
|
|
return True # All done. Save time.
|
|
|
|
char = string[-1]
|
|
|
|
_, _, char_width = self.font.get_ch(char)
|
|
|
|
if oh and l + sc + char_width > wd:
|
|
|
|
l += self._truelen(char) # Last char might have blank cols on RHS
|
|
|
|
else:
|
|
|
|
l += char_width # Public method. Return same value as old code.
|
|
|
|
return l + sc > wd if oh else l
|
|
|
|
|
|
|
|
# Return the printable width of a glyph less any blank columns on RHS
|
|
|
|
def _truelen(self, char):
|
|
|
|
glyph, ht, wd = self.font.get_ch(char)
|
|
|
|
div, mod = divmod(wd, 8)
|
|
|
|
gbytes = div + 1 if mod else div # No. of bytes per row of glyph
|
|
|
|
mc = 0 # Max non-blank column
|
|
|
|
data = glyph[(wd - 1) // 8] # Last byte of row 0
|
|
|
|
for row in range(ht): # Glyph row
|
|
|
|
for col in range(wd -1, -1, -1): # Glyph column
|
|
|
|
gbyte, gbit = divmod(col, 8)
|
|
|
|
if gbit == 0: # Next glyph byte
|
|
|
|
data = glyph[row * gbytes + gbyte]
|
|
|
|
if col <= mc:
|
|
|
|
break
|
|
|
|
if data & (1 << (7 - gbit)): # Pixel is lit (1)
|
|
|
|
mc = col # Eventually gives rightmost lit pixel
|
|
|
|
break
|
|
|
|
if mc + 1 == wd:
|
|
|
|
break # All done: no trailing space
|
2021-09-07 09:31:25 +00:00
|
|
|
# print('Truelen', char, wd, mc + 1) # TEST
|
2021-01-29 12:49:45 +00:00
|
|
|
return mc + 1
|
2018-08-14 16:22:21 +00:00
|
|
|
|
|
|
|
def _get_char(self, char, recurse):
|
|
|
|
if not recurse: # Handle tabs
|
|
|
|
if char == '\n':
|
|
|
|
self.cpos = 0
|
|
|
|
elif char == '\t':
|
|
|
|
nspaces = self.tab - (self.cpos % self.tab)
|
|
|
|
if nspaces == 0:
|
|
|
|
nspaces = self.tab
|
|
|
|
while nspaces:
|
|
|
|
nspaces -= 1
|
|
|
|
self._printchar(' ', recurse=True)
|
|
|
|
self.glyph = None # All done
|
|
|
|
return
|
|
|
|
|
|
|
|
self.glyph = None # Assume all done
|
|
|
|
if char == '\n':
|
|
|
|
self._newline()
|
|
|
|
return
|
|
|
|
glyph, char_height, char_width = self.font.get_ch(char)
|
|
|
|
s = self._getstate()
|
2021-01-28 13:56:13 +00:00
|
|
|
np = None # Allow restriction on printable columns
|
2021-01-30 17:38:59 +00:00
|
|
|
if s.text_row + char_height > self.screenheight:
|
|
|
|
if self.row_clip:
|
|
|
|
return
|
|
|
|
self._newline()
|
|
|
|
oh = s.text_col + char_width - self.screenwidth # Overhang (+ve)
|
|
|
|
if oh > 0:
|
|
|
|
if self.col_clip or self.wrap:
|
|
|
|
np = char_width - oh # No. of printable columns
|
|
|
|
if np <= 0:
|
2018-08-14 16:22:21 +00:00
|
|
|
return
|
2021-01-30 17:38:59 +00:00
|
|
|
else:
|
2018-08-14 16:22:21 +00:00
|
|
|
self._newline()
|
|
|
|
self.glyph = glyph
|
|
|
|
self.char_height = char_height
|
|
|
|
self.char_width = char_width
|
2021-01-28 13:56:13 +00:00
|
|
|
self.clip_width = char_width if np is None else np
|
2018-08-14 16:22:21 +00:00
|
|
|
|
|
|
|
# Method using blitting. Efficient rendering for monochrome displays.
|
|
|
|
# Tested on SSD1306. Invert is for black-on-white rendering.
|
|
|
|
def _printchar(self, char, invert=False, recurse=False):
|
|
|
|
s = self._getstate()
|
|
|
|
self._get_char(char, recurse)
|
|
|
|
if self.glyph is None:
|
|
|
|
return # All done
|
|
|
|
buf = bytearray(self.glyph)
|
|
|
|
if invert:
|
|
|
|
for i, v in enumerate(buf):
|
|
|
|
buf[i] = 0xFF & ~ v
|
2021-01-28 13:56:13 +00:00
|
|
|
fbc = framebuf.FrameBuffer(buf, self.clip_width, self.char_height, self.map)
|
2018-08-14 16:22:21 +00:00
|
|
|
self.device.blit(fbc, s.text_col, s.text_row)
|
|
|
|
s.text_col += self.char_width
|
|
|
|
self.cpos += 1
|
|
|
|
|
|
|
|
def tabsize(self, value=None):
|
|
|
|
if value is not None:
|
|
|
|
self.tab = value
|
|
|
|
return self.tab
|
|
|
|
|
2018-08-28 10:37:21 +00:00
|
|
|
def setcolor(self, *_):
|
|
|
|
return self.fgcolor, self.bgcolor
|
2018-08-14 16:22:21 +00:00
|
|
|
|
2021-06-24 08:57:19 +00:00
|
|
|
# Writer for colour displays.
|
2018-08-14 16:22:21 +00:00
|
|
|
class CWriter(Writer):
|
|
|
|
|
|
|
|
|
2018-08-28 10:37:21 +00:00
|
|
|
def __init__(self, device, font, fgcolor=None, bgcolor=None, verbose=True):
|
2021-09-07 09:31:25 +00:00
|
|
|
if not hasattr(device, 'palette'):
|
|
|
|
raise OSError('Incompatible device driver.')
|
|
|
|
if implementation[1] < (1, 17, 0):
|
|
|
|
raise OSError('Firmware must be >= 1.17.')
|
|
|
|
|
2018-08-14 16:22:21 +00:00
|
|
|
super().__init__(device, font, verbose)
|
2018-08-28 10:37:21 +00:00
|
|
|
if bgcolor is not None: # Assume monochrome.
|
|
|
|
self.bgcolor = bgcolor
|
2018-08-14 16:22:21 +00:00
|
|
|
if fgcolor is not None:
|
|
|
|
self.fgcolor = fgcolor
|
2018-08-28 10:37:21 +00:00
|
|
|
self.def_bgcolor = self.bgcolor
|
|
|
|
self.def_fgcolor = self.fgcolor
|
|
|
|
|
2021-09-07 09:31:25 +00:00
|
|
|
def _printchar(self, char, invert=False, recurse=False):
|
2020-10-02 16:50:51 +00:00
|
|
|
s = self._getstate()
|
|
|
|
self._get_char(char, recurse)
|
|
|
|
if self.glyph is None:
|
|
|
|
return # All done
|
|
|
|
buf = bytearray_at(addressof(self.glyph), len(self.glyph))
|
2021-08-28 16:17:40 +00:00
|
|
|
fbc = framebuf.FrameBuffer(buf, self.clip_width, self.char_height, self.map)
|
|
|
|
palette = self.device.palette
|
|
|
|
palette.bg(self.fgcolor if invert else self.bgcolor)
|
|
|
|
palette.fg(self.bgcolor if invert else self.fgcolor)
|
|
|
|
self.device.blit(fbc, s.text_col, s.text_row, -1, palette)
|
2020-10-02 16:50:51 +00:00
|
|
|
s.text_col += self.char_width
|
|
|
|
self.cpos += 1
|
2018-08-14 16:22:21 +00:00
|
|
|
|
2020-10-02 16:50:51 +00:00
|
|
|
def setcolor(self, fgcolor=None, bgcolor=None):
|
|
|
|
if fgcolor is None and bgcolor is None:
|
|
|
|
self.fgcolor = self.def_fgcolor
|
|
|
|
self.bgcolor = self.def_bgcolor
|
|
|
|
else:
|
|
|
|
if fgcolor is not None:
|
|
|
|
self.fgcolor = fgcolor
|
|
|
|
if bgcolor is not None:
|
|
|
|
self.bgcolor = bgcolor
|
|
|
|
return self.fgcolor, self.bgcolor
|