From b854d8f078f2ef7ff5ddab8478f41db5da41ea4f Mon Sep 17 00:00:00 2001 From: Peter Hinch Date: Sat, 21 Sep 2019 05:24:02 +0100 Subject: [PATCH] Sparse fonts use non-recursive search. --- FONT_TO_PY.md | 7 ++++--- extended | 2 +- font_to_py.py | 24 +++++++++++------------- 3 files changed, 16 insertions(+), 17 deletions(-) diff --git a/FONT_TO_PY.md b/FONT_TO_PY.md index 28e8951..17b56ab 100644 --- a/FONT_TO_PY.md +++ b/FONT_TO_PY.md @@ -9,17 +9,18 @@ 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.31 notes +## V0.32 notes -10 Sept 2019 +21 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. 2. Output file has comment showing creation command line. 3. Repo includes the file `extended`. Using `-k extended` creates fonts - comprising the printable ASCII set plus `°μπωϕθαβγδλΩ`. Such a font has 95 + comprising the printable ASCII set plus `°μπωϕθαβγδλΩ£`. Such a font has 96 chars having ordinal values from 32-981. 4. Improvements to `font_test.py`. + 5. Code emitted for sparse fonts now uses non-recursive search algorithm. Python files produced are interchangeable with those from prior versions: the API is unchanged. diff --git a/extended b/extended index d1f19a2..a75378b 100644 --- a/extended +++ b/extended @@ -1 +1 @@ - !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~°Ωαβγδθλμπωϕ + !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~°Ωαβγδθλμπωϕ£ diff --git a/font_to_py.py b/font_to_py.py index 06aaac2..752eb32 100755 --- a/font_to_py.py +++ b/font_to_py.py @@ -393,7 +393,7 @@ class Font(dict): STR01 = """# Code generated by font_to_py.py. # Font: {}{} # Cmd: {} -version = '0.31' +version = '0.32' """ @@ -409,23 +409,21 @@ def get_ch(ch): width = ifb(_mvfont[doff : ]) """ -# Code emiited for large charsets, assumed by build_arrays() to be sparse -# Binary sort of sparse index. +# Code emiited for large charsets, assumed by build_arrays() to be sparse. +# Binary search of sorted 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: - return ifb(lst[2 : ]) if ifb(lst) == val else 0 - sp = (n // 2) * 4 - 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) + while True: + m = (len(lst) & ~ 7) >> 1 + v = ifb(lst[m:]) + if v == val: + return ifb(lst[m + 2:]) + if not m: + return 0 + lst = lst[m:] if v < val else lst[:m] def get_ch(ch): doff = bs(_mvsp, ord(ch))