Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Port genfonts to python3 #1

Open
wants to merge 2 commits into
base: devel
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion display/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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" \
-- \
Expand Down
58 changes: 33 additions & 25 deletions utils/genfont.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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))
Expand All @@ -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
Expand All @@ -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()
Expand Down Expand Up @@ -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")
Expand All @@ -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)

Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -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):
Expand All @@ -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)
Expand All @@ -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",
Expand All @@ -499,7 +497,7 @@ def charint(v):
)
parser.add_argument(
"-l", "--load",
nargs="+",
nargs="*",
dest="load_files",
action="append",
metavar="FILE",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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:
Expand Down