Skip to content

Instantly share code, notes, and snippets.

@frankrolf
Created March 21, 2019 10:18
Show Gist options
  • Save frankrolf/acb6354487f26c1b685d7ef5cde751b7 to your computer and use it in GitHub Desktop.
Save frankrolf/acb6354487f26c1b685d7ef5cde751b7 to your computer and use it in GitHub Desktop.
Robofont script to calculate the global bounding box for all open fonts
import os
a = AllFonts()
class GlyphPoints(object):
def __init__(self, glyph):
point_list = [
point for contour in glyph.contours for point in contour.points]
self.name = glyph.name
self.xPoints = [point.x for point in point_list]
self.yPoints = [point.y for point in point_list]
allBoxes = []
for f in a:
allContours = [
contour for glyph in f for contour in glyph.contours if glyph.contours]
allPoints = [point for contour in allContours for point in contour.points]
max_x = max([point.x for point in allPoints])
min_x = min([point.x for point in allPoints])
max_y = max([point.y for point in allPoints])
min_y = min([point.y for point in allPoints])
bbox = min_x, min_y, max_x, max_y
if f.path:
name = os.path.basename(f.path)
else:
name = '{} {}'.format(f.info.familyName, f.info.styleName)
print('-' * len(name))
print(name)
print('bounding box: %s' % ' '.join([str(i) for i in bbox]))
allBoxes.append(bbox)
g_objects = [GlyphPoints(glyph) for glyph in f]
print('x min', min_x, ' '.join(
[go.name for go in g_objects if min_x in go.xPoints]))
print('y min', min_y, ' '.join(
[go.name for go in g_objects if min_y in go.yPoints]))
print('x max', max_x, ' '.join(
[go.name for go in g_objects if max_x in go.xPoints]))
print('y max', max_y, ' '.join(
[go.name for go in g_objects if max_y in go.yPoints]))
print()
bottom_x = min([box[0] for box in allBoxes])
bottom_y = min([box[1] for box in allBoxes])
top_x = max([box[2] for box in allBoxes])
top_y = max([box[3] for box in allBoxes])
print('global bounding box:')
print(bottom_x, bottom_y, top_x, top_y)
print()
print(f'winAscent {top_y};')
print(f'winDescent {abs(bottom_y)};')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment