summaryrefslogtreecommitdiff
path: root/misc/rf-scripts/AdjustWidth.py
blob: af09683008feb4737803bff51a900e7729132047 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#
# 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()
  print "Resizing glyph margins for %r" % font

  # how much to add or remove from each glyph's margin
  A = 16

  if font is not None:
    # first, check for errors and collect glyphs we should adjust
    glyphs = []
    glyphNamesToAdjust = set()
    ignored = []
    errors = 0

    for g in font:
      if g.width < 4:
        ignored.append((g.name, 'zero-width'))
        continue

      # if g.box is None:
      #   print '"%s": ["ignore", "empty"],' % (g.name)
      #   continue

      # skip glyphs
      #if g.name in ('c', 'e', 'o', 'r', 'j'):
      # continue

      if g.width % 4 != 0:
        print '"%s": ["error", "misaligned"],' % (g.name)
        errors += 1
        continue

      glyphs.append(g)
      glyphNamesToAdjust.add(g.name)

    if errors > 0:
      print "Stopping changes because there are errors"
    else:

      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 = ['

      adjustments = dict()

      onlyGlyphs = None # ['A', 'Lambda']  # DEBUG
      count = 0

      for g in glyphs:
        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])

        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)
        g.rightMargin = int(newRightMargin)

      print '] # resized_glyphs'

      font.update()

      # if len(ignored) > 0:
      #   print ''
      #   print '# name => [what, reason]'
      #   print "ignored_glyphs = ["
      #   for t in ignored:
      #     print '  "%s": ["ignore", %r],' % t
      #   print '] # ignored_glyphs'

  else:
    print "No fonts open"

  print "Done"