diff --git a/display/Makefile b/display/Makefile index 7f5f170..b53b8b0 100644 --- a/display/Makefile +++ b/display/Makefile @@ -103,7 +103,7 @@ src/generated_font_data.inc: $(GENFONT) $(GENFONT) --no-flip --bfseries \ -r "'0'" "'9'" \ -c "':'" \ - --space-width 6 \ + --space-width 6 \ -c 0xfffd \ -l "data/glyphs/Cantarell/20px/0x0000003a.glyph" \ -- \ diff --git a/utils/genfont.py b/utils/genfont.py index ec6e29f..33081ae 100755 --- a/utils/genfont.py +++ b/utils/genfont.py @@ -1,9 +1,7 @@ -#!/usr/bin/python2 -# encoding=utf8 -from __future__ import print_function +#!/usr/bin/python3 -import ConfigParser -import cStringIO +import configparser +import io import itertools import textwrap import logging @@ -66,7 +64,7 @@ def _strip_empty_rows(self): else: data = itertools.chain(*rows[skip_above:-skip_below]) - self.data = bytearray(data) + self.data = bytes(data) def _compress_bits(self): byteblocks = list(split_to_parts(self.data, 8)) @@ -79,7 +77,7 @@ def _compress_bits(self): curr_byte |= bitmask bitmask = bitmask >> 1 final[i] = curr_byte - self.data = final + self.data = bytes(final) def get_bytemap(self, alignment=1): rowsize = self.width @@ -103,7 +101,7 @@ def get_bytemap(self, alignment=1): desti += rowsize - self.width - return buf + return bytes(buf) def export_as_glyphfile(self, f): config = ConfigParser.ConfigParser() @@ -159,8 +157,8 @@ def import_glyphfile(self, f): break configlines.append(line) - config = ConfigParser.ConfigParser() - config.readfp(cStringIO.StringIO("".join(configlines))) + config = configparser.ConfigParser() + config.read_file(io.StringIO("".join(configlines))) y0 = config.getint("info", "baseline") codepoint = config.getint("info", "codepoint") @@ -173,10 +171,10 @@ def import_glyphfile(self, f): if not all(len(line) == width for line in glyphdata): raise ValueError("Glyph data is not equal-sized") - if not all(c in {"0", "1"} - for c in line - for line in glyphdata): - raise ValueError("Glyph data is malformed") + if not all(c in ("0", "1") + for line in glyphdata + for c in line): + raise ValueError("Glyph data is malformed", glyphdata) height = len(glyphdata) @@ -250,7 +248,7 @@ def size(self): + len(self.data) def add_data(self, data): - data = str(data) + data = bytes(data) try: offset = self.datamap[data] except KeyError: @@ -302,7 +300,7 @@ def calculate_ranges(self): def _add_indent(text, indent): return "\n".join( indent+line - for line in (text.split("\n") if isinstance(text, basestring) else text)) + for line in (text.split("\n") if isinstance(text, str) else text)) def _c_data(self, indent=" "): return self._add_indent( @@ -410,7 +408,7 @@ def __init__(self, font_family, font_size, def render_ustr(self, ustr): self._buffer[:] = self._nullbuffer[:] - self._layout.set_text(ustr.encode("utf8"), -1) + self._layout.set_text(ustr, -1) _, logical = self._layout.get_pixel_extents() @@ -438,7 +436,7 @@ def render_ustr(self, ustr): def struct_ustr(self, codepoint): glyph = GlyphStruct(codepoint) - glyph.set_bytemap(*self.render_ustr(unichr(codepoint))) + glyph.set_bytemap(*self.render_ustr(chr(codepoint))) return glyph def get_space_width(self): @@ -456,13 +454,13 @@ def render_glyphs(self, font, codepoints): yield self.struct_ustr(codepoint) def charint(v): - if v.startswith(b"'") and v.endswith(b"'"): + if v.startswith("'") and v.endswith("'"): v = v[1:-1] - return ord(v.decode('utf8')) + return ord(v) v = v.lower() - if v.startswith(b"0x"): + if v.startswith("0x"): return int(v[2:], 16) - elif v.startswith(b"0b"): + elif v.startswith("0b"): return int(v[2:], 2) else: return int(v) @@ -476,7 +474,7 @@ def charint(v): parser.add_argument( "font", metavar="FONT", - help="Use font family FONT" + help="Use font family FONT", ) parser.add_argument( "size", @@ -499,7 +497,7 @@ def charint(v): ) parser.add_argument( "-l", "--load", - nargs="+", + nargs="*", dest="load_files", action="append", metavar="FILE", @@ -551,6 +549,12 @@ def charint(v): dest="elf_section", help="ELF section to store the font in" ) + parser.add_argument( + "--space-width", + default=None, + type=int, + help="Override the space width from the font", + ) parser.add_argument( "--export", metavar="DIRECTORY", @@ -620,7 +624,11 @@ def filterfunc(glyph): glyphs.extend( renderer.render_glyphs(font, codepoints) ) - font.space_width = renderer.get_space_width() + + if args.space_width is not None: + font.space_width = args.space_width + else: + font.space_width = renderer.get_space_width() for glyph in glyphs: if args.export_dir: