Fix indentation, format code according to autopep8

This commit is contained in:
Roel Nieskens 2019-09-10 13:59:15 +02:00
parent 833a43d032
commit 9eade97ded

View file

@ -17,7 +17,6 @@
# Google Author(s): Behdad Esfahbod, Stuart Gill, Roozbeh Pournader
#
from __future__ import print_function
import sys, struct, StringIO
from png import PNG
@ -26,6 +25,7 @@ from os import path
from nototools import font_data
def get_glyph_name_from_gsub(string, font, cmap_dict):
ligatures = font['GSUB'].table.LookupList.Lookup[0].SubTable[0].ligatures
first_glyph = cmap_dict[ord(string[0])]
@ -38,18 +38,22 @@ def get_glyph_name_from_gsub (string, font, cmap_dict):
def div(a, b):
return int(round(a / float(b)))
class FontMetrics:
def __init__(self, upem, ascent, descent):
self.upem = upem
self.ascent = ascent
self.descent = descent
class StrikeMetrics:
def __init__(self, font_metrics, advance, bitmap_width, bitmap_height):
self.width = bitmap_width # in pixels
self.height = bitmap_height # in pixels
self.advance = advance # in font units
self.x_ppem = self.y_ppem = div (bitmap_width * font_metrics.upem, advance)
self.x_ppem = self.y_ppem = div(bitmap_width * font_metrics.upem,
advance)
class GlyphMap:
def __init__(self, glyph, offset, image_format):
@ -60,7 +64,6 @@ class GlyphMap:
# Based on http://www.microsoft.com/typography/otspec/ebdt.htm
class CBDT:
def __init__(self, font_metrics, options=(), stream=None):
self.stream = stream if stream != None else bytearray()
self.options = options
@ -70,8 +73,10 @@ class CBDT:
def tell(self):
return len(self.stream) - self.base_offset
def write(self, data):
self.stream.extend(data)
def data(self):
return self.stream
@ -135,27 +140,25 @@ class CBDT:
# BYTE vertAdvance
try:
if big_metrics:
self.write (struct.pack ("BBbbBbbB",
height, width,
x_bearing, y_bearing,
advance,
vert_x_bearing, vert_y_bearing,
vert_advance))
self.write(
struct.pack("BBbbBbbB", height, width, x_bearing,
y_bearing, advance, vert_x_bearing,
vert_y_bearing, vert_advance))
else:
self.write (struct.pack ("BBbbB",
height, width,
x_bearing, y_bearing,
self.write(
struct.pack("BBbbB", height, width, x_bearing, y_bearing,
advance))
except Exception as e:
raise ValueError("%s, h: %d w: %d x: %d y: %d %d a:" % (
e, height, width, x_bearing, y_bearing, advance))
raise ValueError("%s, h: %d w: %d x: %d y: %d %d a:" %
(e, height, width, x_bearing, y_bearing, advance))
def write_format1(self, png):
import cairo
img = cairo.ImageSurface.create_from_png(png.stream())
if img.get_format() != cairo.FORMAT_ARGB32:
raise Exception ("Expected FORMAT_ARGB32, but image has format %d" % img.get_format ())
raise Exception("Expected FORMAT_ARGB32, but image has format %d" %
img.get_format())
width = img.get_width()
height = img.get_height()
@ -209,7 +212,6 @@ class CBDT:
# Based on http://www.microsoft.com/typography/otspec/eblc.htm
class CBLC:
def __init__(self, font_metrics, options=(), stream=None):
self.stream = stream if stream != None else bytearray()
self.streams = []
@ -220,13 +222,17 @@ class CBLC:
def tell(self):
return len(self.stream) - self.base_offset
def write(self, data):
self.stream.extend(data)
def data(self):
return self.stream
def push_stream(self, stream):
self.streams.append(self.stream)
self.stream = stream
def pop_stream(self):
stream = self.stream
self.stream = self.streams.pop()
@ -276,12 +282,21 @@ class CBLC:
line_height = div((ascent + descent) * y_ppem, upem)
ascent = div(ascent * y_ppem, upem)
descent = -(line_height - ascent)
self.write (struct.pack ("bbBbbbbbbbbb",
ascent, descent,
self.write(
struct.pack(
"bbBbbbbbbbbb",
ascent,
descent,
self.strike_metrics.width,
0, 0, 0,
0, 0, 0, 0, # TODO
0, 0))
0,
0,
0,
0,
0,
0,
0, # TODO
0,
0))
def write_sbitLineMetrics_vert(self):
self.write_sbitLineMetrics_hori() # XXX
@ -295,7 +310,8 @@ class CBLC:
imageDataOffset = glyph_maps[0].offset
self.write(struct.pack(">L", imageDataOffset)) # ULONG imageDataOffset
for gmap in glyph_maps[:-1]:
self.write (struct.pack(">L", gmap.offset - imageDataOffset)) # ULONG offsetArray
self.write(struct.pack(">L", gmap.offset -
imageDataOffset)) # ULONG offsetArray
assert gmap.image_format == image_format
self.write(struct.pack(">L", glyph_maps[-1].offset - imageDataOffset))
@ -322,7 +338,9 @@ class CBLC:
last_id = 0
for gmap in glyph_maps[1:-1]:
if last_glyph + 1 != gmap.glyph or last_image_format != gmap.image_format:
headers.extend (struct.pack(">HHL", start, last_glyph, headersLen + len (subtables)))
headers.extend(
struct.pack(">HHL", start, last_glyph,
headersLen + len(subtables)))
self.push_stream(subtables)
self.write_indexSubTable1(glyph_maps[start_id:last_id + 2])
self.pop_stream()
@ -332,7 +350,9 @@ class CBLC:
last_glyph = gmap.glyph
last_image_format = gmap.image_format
last_id += 1
headers.extend (struct.pack(">HHL", start, last_glyph, headersLen + len (subtables)))
headers.extend(
struct.pack(">HHL", start, last_glyph,
headersLen + len(subtables)))
self.push_stream(subtables)
self.write_indexSubTable1(glyph_maps[start_id:last_id + 2])
self.pop_stream()
@ -341,7 +361,8 @@ class CBLC:
numberOfIndexSubTables = count
bitmapSizeTableSize = 48 * self.num_strikes
indexSubTableArrayOffset = 8 + bitmapSizeTableSize + len (self.otherTables)
indexSubTableArrayOffset = 8 + bitmapSizeTableSize + len(
self.otherTables)
self.push_stream(self.bitmapSizeTables)
# bitmapSizeTable
@ -431,7 +452,8 @@ By default they are dropped.
If -C is given, unused chunks (color profile, etc) are NOT
dropped from the PNG images when embedding.
By default they are dropped.
""", file=sys.stderr)
""",
file=sys.stderr)
sys.exit(1)
font_file = argv[1]
@ -451,14 +473,12 @@ By default they are dropped.
except KeyError:
pass
print()
font = ttx.TTFont(font_file)
print("Loaded font '%s'." % font_file)
font_metrics = FontMetrics (font['head'].unitsPerEm,
font['hhea'].ascent,
font_metrics = FontMetrics(font['head'].unitsPerEm, font['hhea'].ascent,
-font['hhea'].descent)
print("Font metrics: upem=%d ascent=%d descent=%d." % \
(font_metrics.upem, font_metrics.ascent, font_metrics.descent))
@ -469,8 +489,8 @@ By default they are dropped.
if not unicode_cmap:
raise Exception("Failed to find a Unicode cmap.")
image_format = 1 if 'uncompressed' in options else (17
if 'small_glyph_metrics' in options else 18)
image_format = 1 if 'uncompressed' in options else (
17 if 'small_glyph_metrics' in options else 18)
ebdt = CBDT(font_metrics, options)
ebdt.write_header()
@ -502,7 +522,8 @@ By default they are dropped.
img_files[uchars] = img_file
if not img_files:
raise Exception("No image files found in '%s'." % glb)
print("Found images for %d characters in '%s'." % (len (img_files), glb))
print("Found images for %d characters in '%s'." %
(len(img_files), glb))
glyph_imgs = {}
advance = width = height = 0
@ -514,7 +535,8 @@ By default they are dropped.
print("no cmap entry for %x" % ord(uchars))
raise ValueError("%x" % ord(uchars))
else:
glyph_name = get_glyph_name_from_gsub (uchars, font, unicode_cmap.cmap)
glyph_name = get_glyph_name_from_gsub(uchars, font,
unicode_cmap.cmap)
glyph_id = font.getGlyphID(glyph_name)
glyph_imgs[glyph_id] = img_file
if "verbose" in options:
@ -529,10 +551,12 @@ By default they are dropped.
glyphs = sorted(glyph_imgs.keys())
if not glyphs:
raise Exception ("No common characters found between font and '%s'." % glb)
raise Exception(
"No common characters found between font and '%s'." % glb)
print("Embedding images for %d glyphs for this strike." % len(glyphs))
advance, width, height = (div (x, len (glyphs)) for x in (advance, width, height))
advance, width, height = (div(x, len(glyphs))
for x in (advance, width, height))
strike_metrics = StrikeMetrics(font_metrics, advance, width, height)
print("Strike ppem set to %d." % (strike_metrics.y_ppem))
@ -566,6 +590,5 @@ By default they are dropped.
font.save(out_file)
print("Output font '%s' generated." % out_file)
if __name__ == '__main__':
main(sys.argv)