1
1
mirror of https://github.com/rsms/inter.git synced 2024-12-04 03:27:39 +03:00
inter/misc/rf-scripts/AdjustWidth.py

98 lines
2.7 KiB
Python
Raw Normal View History

2017-08-22 10:05:20 +03:00
#
# This script changes the width of all glyphs by applying a multiplier.
# It keeps the contours centered as glyphs get wider or tighter.
#
from mojo.roboFont import version
from math import ceil, floor
if __name__ == "__main__":
font = CurrentFont()
ignoreGlyphsWithoutContours = True # like spaces
2018-02-18 05:14:27 +03:00
print "# Resizing glyph margins for %r" % font
2017-08-22 10:05:20 +03:00
# how much to add or remove from each glyph's margin
2018-02-18 05:14:27 +03:00
A = -4
2017-08-22 10:05:20 +03:00
if font is not None:
2018-01-14 04:54:45 +03:00
# first, check for errors and collect glyphs we should adjust
glyphs = []
2018-01-14 05:42:32 +03:00
glyphNamesToAdjust = set()
2018-01-14 04:54:45 +03:00
ignored = []
errors = 0
2017-08-22 10:05:20 +03:00
2018-01-14 04:54:45 +03:00
for g in font:
2018-01-14 00:57:57 +03:00
if g.width < 4:
2018-01-14 04:54:45 +03:00
ignored.append((g.name, 'zero-width'))
2017-08-22 10:05:20 +03:00
continue
if ignoreGlyphsWithoutContours and g.box is None:
# print '"%s": ["ignore", "empty"],' % (g.name)
ignored.append((g.name, 'empty'))
continue
2018-01-14 04:54:45 +03:00
# skip glyphs
#if g.name in ('c', 'e', 'o', 'r', 'j'):
# continue
2017-08-22 10:05:20 +03:00
2018-01-14 00:57:57 +03:00
if g.width % 4 != 0:
2018-02-18 05:14:27 +03:00
print 'error: %s is misaligned; width = %g (not an even multiple of 4)' % (g.name, g.width)
2018-01-14 00:57:57 +03:00
errors += 1
2017-08-22 10:05:20 +03:00
continue
2018-01-14 04:54:45 +03:00
glyphs.append(g)
2018-01-14 05:42:32 +03:00
glyphNamesToAdjust.add(g.name)
2017-08-22 10:05:20 +03:00
2018-01-14 00:57:57 +03:00
if errors > 0:
2018-01-14 04:54:45 +03:00
print "Stopping changes because there are errors"
2018-01-14 00:57:57 +03:00
else:
2018-01-14 04:54:45 +03:00
print '# Result from AdjustWidth.py with A=%g on %s %s' % (
A, font.info.familyName, font.info.styleName)
print '# name => [ (prevLeftMargin, prevRightMargin), (newLeft, newRight) ]'
print 'resized_glyphs = ['
2018-01-14 05:42:32 +03:00
adjustments = dict()
onlyGlyphs = None # ['A', 'Lambda'] # DEBUG
count = 0
2018-01-14 04:54:45 +03:00
for g in glyphs:
2018-01-14 05:42:32 +03:00
if onlyGlyphs is not None:
if not g.name in onlyGlyphs:
continue
if len(onlyGlyphs) == count:
break
count += 1
for comp in g.components:
# adjust offset of any components which are being adjusted
if comp.baseGlyph in glyphNamesToAdjust:
# x, y -- counter-balance x offset
comp.offset = (comp.offset[0] - A, comp.offset[1])
2018-01-14 04:54:45 +03:00
newLeftMargin = int(g.leftMargin + A)
newRightMargin = int(g.rightMargin + A)
print ' "%s": [(%g, %g), (%g, %g)],' % (
g.name, g.leftMargin, g.rightMargin, newLeftMargin, newRightMargin)
g.leftMargin = int(newLeftMargin)
2018-01-14 05:42:32 +03:00
g.rightMargin = int(newRightMargin)
2018-01-14 04:54:45 +03:00
print '] # resized_glyphs'
font.update()
2018-01-14 05:42:32 +03:00
# if len(ignored) > 0:
# print ''
# print '# name => [what, reason]'
# print "ignored_glyphs = ["
# for t in ignored:
# print ' "%s": ["ignore", %r],' % t
# print '] # ignored_glyphs'
2018-01-14 04:54:45 +03:00
2017-08-22 10:05:20 +03:00
else:
print "No fonts open"
print "Done"