kopia lustrzana https://github.com/peterhinch/micropython-font-to-py
More efficient search in sparse fonts.
rodzic
8f818db3c6
commit
3078e311cd
|
@ -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
|
occupies flash memory rather than scarce RAM. Python code built into firmware
|
||||||
is known as frozen bytecode.
|
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
|
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.
|
between ordinal values of Unicode characters not in the standard ASCII set.
|
||||||
|
|
|
@ -393,50 +393,56 @@ class Font(dict):
|
||||||
STR01 = """# Code generated by font_to_py.py.
|
STR01 = """# Code generated by font_to_py.py.
|
||||||
# Font: {}{}
|
# Font: {}{}
|
||||||
# Cmd: {}
|
# Cmd: {}
|
||||||
version = '0.3'
|
version = '0.31'
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# Code emitted for charsets spanning a small range of ordinal values
|
# Code emitted for charsets spanning a small range of ordinal values
|
||||||
STR02 = """_mvfont = memoryview(_font)
|
STR02 = """_mvfont = memoryview(_font)
|
||||||
|
_mvi = memoryview(_index)
|
||||||
|
ifb = lambda l : l[0] | (l[1] << 8)
|
||||||
|
|
||||||
def get_ch(ch):
|
def get_ch(ch):
|
||||||
oc = ord(ch)
|
oc = ord(ch)
|
||||||
idx_offs = 2 * (oc - {0} + 1) if oc >= {0} and oc <= {1} else 0
|
ioff = 2 * (oc - {0} + 1) if oc >= {0} and oc <= {1} else 0
|
||||||
offset = int.from_bytes(_index[idx_offs : idx_offs + 2], 'little')
|
doff = ifb(_mvi[ioff : ])
|
||||||
width = int.from_bytes(_font[offset:offset + 2], 'little')
|
width = ifb(_mvfont[doff : ])
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# Code emiited for large charsets, assumed by build_arrays() to be sparse
|
# Code emiited for large charsets, assumed by build_arrays() to be sparse
|
||||||
# Binary sort of sparse index.
|
# Binary sort of sparse index.
|
||||||
STRSP = """_mvfont = memoryview(_font)
|
STRSP = """_mvfont = memoryview(_font)
|
||||||
_mvsp = memoryview(_sparse)
|
_mvsp = memoryview(_sparse)
|
||||||
|
ifb = lambda l : l[0] | (l[1] << 8)
|
||||||
|
|
||||||
def bs(lst, val):
|
def bs(lst, val):
|
||||||
n = len(lst) // 4
|
n = len(lst) // 4
|
||||||
if n == 1:
|
if n == 1:
|
||||||
v = int.from_bytes(lst[: 2], 'little')
|
return ifb(lst[2 : ]) if ifb(lst) == val else 0
|
||||||
return int.from_bytes(lst[2 : 4], 'little') if v == val else 0
|
|
||||||
sp = (n // 2) * 4
|
sp = (n // 2) * 4
|
||||||
res = bs(lst[: sp], val)
|
v = ifb(lst[sp - 4 : ])
|
||||||
return res if res else bs(lst[sp :], val)
|
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):
|
def get_ch(ch):
|
||||||
offset = bs(_mvsp, ord(ch))
|
doff = bs(_mvsp, ord(ch))
|
||||||
width = int.from_bytes(_font[offset : offset + 2], 'little')
|
width = ifb(_mvfont[doff : ])
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# Code emitted for horizontally mapped fonts.
|
# Code emitted for horizontally mapped fonts.
|
||||||
STR02H ="""
|
STR02H ="""
|
||||||
next_offs = offset + 2 + ((width - 1)//8 + 1) * {0}
|
next_offs = doff + 2 + ((width - 1)//8 + 1) * {0}
|
||||||
return _mvfont[offset + 2:next_offs], {0}, width
|
return _mvfont[doff + 2:next_offs], {0}, width
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# Code emitted for vertically mapped fonts.
|
# Code emitted for vertically mapped fonts.
|
||||||
STR02V ="""
|
STR02V ="""
|
||||||
next_offs = offset + 2 + (({0} - 1)//8 + 1) * width
|
next_offs = doff + 2 + (({0} - 1)//8 + 1) * width
|
||||||
return _mvfont[offset + 2:next_offs], {0}, width
|
return _mvfont[doff + 2:next_offs], {0}, width
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
Ładowanie…
Reference in New Issue