Bit reversal and addresof done

pull/7/head
Peter Hinch 2016-04-05 08:39:10 +01:00
rodzic 25f9d68916
commit c5e4fd8cb2
3 zmienionych plików z 39 dodań i 24 usunięć

Wyświetl plik

@ -1,5 +1,5 @@
class PyFont(object):
class TFTFont(object):
def __init__(self, font, index, vert, horiz, nchars):
self.firstchar = 32 # ord(first_character) Future use: absent from C file.
self.nchars = nchars # No. of chars in font
@ -21,9 +21,32 @@ class PyFont(object):
return self._index[offset] + (self._index[offset + 1] << 8)
def get_ch(self, ch):
relch = max(0, ch - self.firstchar) # This is the behaviour you requested. In my view it should throw a ValueError.
if relch > self.nchars:
relch = 0
from uctypes import addrssof
relch = ch - self.firstchar
if relch > self.nchars or relch < 0:
raise ValueError('Character value {:} is unsupported.'.format(ch))
offset = self.get_idx(relch)
delta = self.get_idx(relch + 1) - offset
bv = self.bits_vert
mv = self.mv
if self.monospaced:
mv[: delta] = self._font[offset : offset + delta]
mv[delta : self.bytes_per_ch] = self._mvzero[delta : self.bytes_per_ch]
return addressof(self.char), self.bits_vert, delta, self.bits_horiz
else:
return addressof(self._font) + offset, self.bits_vert, delta, delta // self.bytes_vert
# *************** TEST CODE ***************
# This runs on a PC and allows font creation and cfonts_to_py.py to be tested without hardware
# It can be deleted. Usage:
# import fonts
# fonts.fonts['freesans23x25'].render(0x41)
# dict allows access to multiple fonts in fonts file
def get_ch_test(self, ch):
relch = ch - self.firstchar
if relch > self.nchars or relch < 0:
raise ValueError('Character value {:} is unsupported.'.format(ch))
offset = self.get_idx(relch)
delta = self.get_idx(relch + 1) - offset
bv = self.bits_vert
@ -32,15 +55,13 @@ class PyFont(object):
bh = self.bits_horiz # Char width in bits
mv[: delta] = self._font[offset : offset + delta]
mv[delta : self.bytes_per_ch] = self._mvzero[delta : self.bytes_per_ch]
bh = self.bits_horiz
else:
mv[: delta] = self._font[offset : offset + delta]
bh = delta // self.bytes_vert
return bh # horizontal increment for character location
# Demo/test of rendering to the REPL
def render(self, ch): # enter with ord(ch)
bh = self.get_ch(ch)
bh = self.get_ch_test(ch)
bv = self.bits_vert # Cache for speed
mv = self.mv
for bit_vert in range(bv): # for each vertical line

Wyświetl plik

@ -16,11 +16,6 @@
# with a dictionary 'fonts.fonts' indexed by name and the value being a PyFont instance.
# The name index is the font filename less path and extension
# TODO check that new format below works for all cases of character line length as per inded
# terminal7hex = b'\x08\x0C\x20\x80\x01'\
# b'\x08\x00\x00\x3F\x00\x40\x80\x3F\x00\x00\x00\x0F\x80\x10\x40\x0F\x80'\
# b'\x08\x00\x00\x3F\x00\x40\x80\x3F\x00\x00\x00\x08\x40\x1F\xC0\x00\x40'\
import argparse, os
def getname(sourcefile):
@ -38,7 +33,7 @@ def rbits(n): # reverse bits in a byte
n >>= 1
return res
# Given a string 0f form '0x23' representing a byte, return a string of same form but with the bits
# Given a string 0f form '0x23' representing a byte, return a string of form 'xc4' with the bits
# comprising the byte reversed
def rbits_text(string):
return 'x{:02x}'.format(rbits(int(string, 16)))
@ -130,7 +125,7 @@ def process(infile, outfile, sourcefile):
outfile.write("b'")
for hexnum in nums:
outfile.write('\\')
# outfile.write(hexnum.strip()[1:4]) # Don't reverse bits
# outfile.write(hexnum.strip()[1:4]) # Don't reverse bits
outfile.write(rbits_text(hexnum.strip()[0:4])) # reverse bits
hbit_bytes -= 1
if hbit_bytes == 0:
@ -141,14 +136,14 @@ def process(infile, outfile, sourcefile):
if phase == 4 :
outfile.write("\n")
write_index(outfile, name, index)
outfile.write('{:} = pyfont.PyFont(_{:}, _{:}_index, {}, {}, {})\n\n'.format(name, name, name, vert, horiz, chars_processed))
outfile.write('{:} = TFTfont.TFTFont(_{:}, _{:}_index, {}, {}, {})\n\n'.format(name, name, name, vert, horiz, chars_processed))
print('{}: Characters in font: {} width: {} height: {}'.format(name, chars_processed, horiz, vert))
else:
print(''.join(("File: '", sourcefile, "' is not a valid C font file")))
def write_header(outfile):
outfile.write('# Code generated by CfontToPython.py\n')
outfile.write('import pyfont\n')
outfile.write('# Code generated by cfonts_to_py.py\n')
outfile.write('import TFTfont\n')
def write_trailer(sourcefiles, outfile):
outfile.write('fonts = {')
@ -169,10 +164,10 @@ def load_c(sourcefiles, destfile):
print(err)
if __name__ == "__main__":
parser = argparse.ArgumentParser(__file__, description=
parser = argparse.ArgumentParser(__file__, description =
"Convert C fonts generated by GLCD font creator to Python.\nSample usage:\n ./cfonts_to_py Arial16x16.c ubuntu5x7.py\nProduces fonts.py")
parser.add_argument('infiles', metavar='N', type=str, nargs='+', help='input file paths')
parser.add_argument("--outfile", "-o", default='fonts.py', help="Path and name of output file", required=False)
parser.add_argument('infiles', metavar ='N', type = str, nargs = '+', help = 'input file paths')
parser.add_argument("--outfile", "-o", default = 'fonts.py', help = "Path and name of output file", required = False)
args = parser.parse_args()
errlist = [f for f in args.infiles if not f[0].isalpha()]
if len(errlist):
@ -181,4 +176,3 @@ if __name__ == "__main__":
print(f)
else:
load_c(args.infiles, args.outfile)

Wyświetl plik

@ -1,5 +1,5 @@
# Code generated by CfontToPython.py
import pyfont
# Code generated by cfonts_to_py.py
import TFTfont
_freesans23x25 = b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'\
b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x3f\xff\x70\x00\x3f\xfe\x70\x00'\
b'\x00\x00\x00\x00\x1e\x00\x00\x00\x1f\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x1e\x00\x00\x00\x1f\x80\x00\x00'\
@ -111,7 +111,7 @@ b'\x8c\x0e\xc0\x0e\xf0\x0e\x0c\x0f\x38\x0f\x50\x0f\x7c\x0f\xa8\x0f'\
b'\xec\x0f\x18\x10\x44\x10\x70\x10\x88\x10\x94\x10\xb0\x10\xdc\x10'\
b'\x24\x11\x24\x11'
freesans23x25 = pyfont.PyFont(_freesans23x25, _freesans23x25_index, 25, 23, 96)
freesans23x25 = TFTfont.TFTFont(_freesans23x25, _freesans23x25_index, 25, 23, 96)
fonts = {"freesans23x25":freesans23x25,
}