More efficient search in sparse fonts.

pull/28/head
Peter Hinch 2019-09-10 09:36:41 +01:00
rodzic 8f818db3c6
commit 3078e311cd
2 zmienionych plików z 22 dodań i 16 usunięć

Wyświetl plik

@ -9,9 +9,9 @@ targets: the font file may be incorporated into a firmware build such that it
occupies flash memory rather than scarce RAM. Python code built into firmware
is known as frozen bytecode.
## V0.3 notes
## V0.31 notes
8 Sept 2019
10 Sept 2019
1. Reduced output file size for sparse fonts. These result from large gaps
between ordinal values of Unicode characters not in the standard ASCII set.

Wyświetl plik

@ -393,50 +393,56 @@ class Font(dict):
STR01 = """# Code generated by font_to_py.py.
# Font: {}{}
# Cmd: {}
version = '0.3'
version = '0.31'
"""
# Code emitted for charsets spanning a small range of ordinal values
STR02 = """_mvfont = memoryview(_font)
_mvi = memoryview(_index)
ifb = lambda l : l[0] | (l[1] << 8)
def get_ch(ch):
oc = ord(ch)
idx_offs = 2 * (oc - {0} + 1) if oc >= {0} and oc <= {1} else 0
offset = int.from_bytes(_index[idx_offs : idx_offs + 2], 'little')
width = int.from_bytes(_font[offset:offset + 2], 'little')
ioff = 2 * (oc - {0} + 1) if oc >= {0} and oc <= {1} else 0
doff = ifb(_mvi[ioff : ])
width = ifb(_mvfont[doff : ])
"""
# Code emiited for large charsets, assumed by build_arrays() to be sparse
# Binary sort of sparse index.
STRSP = """_mvfont = memoryview(_font)
_mvsp = memoryview(_sparse)
ifb = lambda l : l[0] | (l[1] << 8)
def bs(lst, val):
n = len(lst) // 4
if n == 1:
v = int.from_bytes(lst[: 2], 'little')
return int.from_bytes(lst[2 : 4], 'little') if v == val else 0
return ifb(lst[2 : ]) if ifb(lst) == val else 0
sp = (n // 2) * 4
res = bs(lst[: sp], val)
return res if res else bs(lst[sp :], val)
v = ifb(lst[sp - 4 : ])
if v == val:
return ifb(lst[sp - 2 : ])
if v > val:
return 0 if sp <= 4 else bs(lst[ : sp - 4], val)
return bs(lst[sp :], val)
def get_ch(ch):
offset = bs(_mvsp, ord(ch))
width = int.from_bytes(_font[offset : offset + 2], 'little')
doff = bs(_mvsp, ord(ch))
width = ifb(_mvfont[doff : ])
"""
# Code emitted for horizontally mapped fonts.
STR02H ="""
next_offs = offset + 2 + ((width - 1)//8 + 1) * {0}
return _mvfont[offset + 2:next_offs], {0}, width
next_offs = doff + 2 + ((width - 1)//8 + 1) * {0}
return _mvfont[doff + 2:next_offs], {0}, width
"""
# Code emitted for vertically mapped fonts.
STR02V ="""
next_offs = offset + 2 + (({0} - 1)//8 + 1) * width
return _mvfont[offset + 2:next_offs], {0}, width
next_offs = doff + 2 + (({0} - 1)//8 + 1) * width
return _mvfont[doff + 2:next_offs], {0}, width
"""