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