kopia lustrzana https://github.com/peterhinch/micropython-font-to-py
Better usage of memoryview reduces heap allocation
rodzic
67ea344993
commit
686d1c92bb
|
@ -168,13 +168,14 @@ _index =\
|
||||||
b'\x00\x00\x14\x00\x2e\x00\x4b\x00\x71\x00\x97\x00\xd2\x00\x0a\x01'\
|
b'\x00\x00\x14\x00\x2e\x00\x4b\x00\x71\x00\x97\x00\xd2\x00\x0a\x01'\
|
||||||
b'\x1b\x01\x35\x01\x4f\x01\x75\x01\x9e\x01\xb2\x01\xcc\x01\xe0\x01'\
|
b'\x1b\x01\x35\x01\x4f\x01\x75\x01\x9e\x01\xb2\x01\xcc\x01\xe0\x01'\
|
||||||
|
|
||||||
|
_mvfont = memoryview(_font)
|
||||||
# Boilerplate code omitted
|
# Boilerplate code omitted
|
||||||
|
|
||||||
def get_ch(ch):
|
def get_ch(ch):
|
||||||
# validate ch, if out of range use '?'
|
# validate ch, if out of range use '?'
|
||||||
# get offsets into _font and retrieve char width
|
# get offsets into _font and retrieve char width
|
||||||
# Return: memoryview of bitmap, height and width
|
# Return: memoryview of bitmap, height and width
|
||||||
return memoryview(_font[offset + 2, next_offset]), height, width
|
return mvfont[offset + 2, next_offset], height, width
|
||||||
```
|
```
|
||||||
|
|
||||||
``height`` and ``width`` are specified in bits (pixels).
|
``height`` and ``width`` are specified in bits (pixels).
|
||||||
|
|
|
@ -348,7 +348,7 @@ STR01 = """# Code generated by font-to-py.py.
|
||||||
version = '0.1'
|
version = '0.1'
|
||||||
"""
|
"""
|
||||||
|
|
||||||
STR02 = """
|
STR02 = """_mvfont = memoryview(_font)
|
||||||
|
|
||||||
def _chr_addr(ordch):
|
def _chr_addr(ordch):
|
||||||
offset = 2 * (ordch - 32)
|
offset = 2 * (ordch - 32)
|
||||||
|
@ -360,7 +360,7 @@ def get_ch(ch):
|
||||||
offset = _chr_addr(ordch)
|
offset = _chr_addr(ordch)
|
||||||
width = int.from_bytes(_font[offset:offset + 2], 'little')
|
width = int.from_bytes(_font[offset:offset + 2], 'little')
|
||||||
next_offs = _chr_addr(ordch +1)
|
next_offs = _chr_addr(ordch +1)
|
||||||
return memoryview(_font[offset + 2:next_offs]), {}, width
|
return _mvfont[offset + 2:next_offs], {}, width
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
@ -399,7 +399,7 @@ def write_data(stream, fnt, font_path, monospaced, hmap, reverse):
|
||||||
bw_index = ByteWriter(stream, '_index')
|
bw_index = ByteWriter(stream, '_index')
|
||||||
bw_index.odata(index)
|
bw_index.odata(index)
|
||||||
bw_index.eot()
|
bw_index.eot()
|
||||||
stream.write(STR02.format(height, height))
|
stream.write(STR02.format(height))
|
||||||
|
|
||||||
# BINARY OUTPUT
|
# BINARY OUTPUT
|
||||||
# hmap reverse magic bytes
|
# hmap reverse magic bytes
|
||||||
|
|
Ładowanie…
Reference in New Issue