summaryrefslogtreecommitdiff
path: root/misc/fontbuildlib/info.py
blob: 12b59dd6b96ce300900a07dbc5db207d7b7df8f4 (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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import subprocess
import re
from datetime import datetime
from common import getGitHash, getVersion
from .util import readTextFile, BASEDIR, pjoin


_gitHash = None
def getGitHash():
  global _gitHash
  if _gitHash is None:
    _gitHash = ''
    try:
      _gitHash = subprocess.check_output(
        ['git', '-C', BASEDIR, 'rev-parse', '--short', 'HEAD'],
        stderr=subprocess.STDOUT,
        **_enc_kwargs
      ).strip()
    except:
      try:
        # git rev-parse --short HEAD > githash.txt
        _gitHash = readTextFile(pjoin(BASEDIR, 'githash.txt')).strip()
      except:
        pass
  return _gitHash


_version = None
def getVersion():
  global _version
  if _version is None:
    _version = readTextFile(pjoin(BASEDIR, 'version.txt')).strip()
  return _version



def updateFontVersion(font, dummy, isVF):
  if dummy:
    version = "1.0"
    buildtag = "src"
    now = datetime(2016, 1, 1, 0, 0, 0, 0)
  else:
    version = getVersion()
    buildtag = getGitHash()
    now = datetime.utcnow()
  versionMajor, versionMinor = [int(num) for num in version.split(".")]
  font.info.version = version
  font.info.versionMajor = versionMajor
  font.info.versionMinor = versionMinor
  font.info.woffMajorVersion = versionMajor
  font.info.woffMinorVersion = versionMinor
  font.info.year = now.year
  font.info.openTypeNameVersion = "Version %d.%03d;git-%s" % (versionMajor, versionMinor, buildtag)
  psFamily = re.sub(r'\s', '', font.info.familyName)
  if isVF:
    font.info.openTypeNameUniqueID = "%s:VF:%d:%s" % (psFamily, now.year, buildtag)
  else:
    psStyle = re.sub(r'\s', '', font.info.styleName)
    font.info.openTypeNameUniqueID = "%s-%s:%d:%s" % (psFamily, psStyle, now.year, buildtag)
  font.info.openTypeHeadCreated = now.strftime("%Y/%m/%d %H:%M:%S")



# setFontInfo patches font.info
def setFontInfo(font, weight=None):
  #
  # For UFO3 names, see
  # https://github.com/unified-font-object/ufo-spec/blob/gh-pages/versions/
  #   ufo3/fontinfo.plist.md
  # For OpenType NAME table IDs, see
  # https://docs.microsoft.com/en-us/typography/opentype/spec/name#name-ids

  if weight is None:
    weight = font.info.openTypeOS2WeightClass

  # Add " BETA" to light weights
  if weight < 400:
    font.info.styleName = font.info.styleName + " BETA"

  family = font.info.familyName  # i.e. "Inter"
  style = font.info.styleName    # e.g. "Medium Italic"

  # Update italicAngle
  isitalic = style.find("Italic") != -1
  if isitalic:
    font.info.italicAngle = float('%.8g' % font.info.italicAngle)
  else:
    font.info.italicAngle = 0  # avoid "-0.0" value in UFO

  # weight
  font.info.openTypeOS2WeightClass = weight

  # version (dummy)
  updateFontVersion(font, dummy=True, isVF=False)

  # Names
  family_nosp = re.sub(r'\s', '', family)
  style_nosp = re.sub(r'\s', '', style)
  font.info.macintoshFONDName = "%s %s" % (family_nosp, style_nosp)
  font.info.postscriptFontName = "%s-%s" % (family_nosp, style_nosp)

  # name ID 16 "Typographic Family name"
  font.info.openTypeNamePreferredFamilyName = family

  # name ID 17 "Typographic Subfamily name"
  font.info.openTypeNamePreferredSubfamilyName = style

  # name ID 1 "Family name" (legacy, but required)
  # Restriction:
  #   "shared among at most four fonts that differ only in weight or style"
  # So we map as follows:
  # - Regular => "Family", ("regular" | "italic" | "bold" | "bold italic")
  # - Medium  => "Family Medium", ("regular" | "italic")
  # - Black   => "Family Black", ("regular" | "italic")
  # and so on.
  subfamily = stripItalic(style).strip() # "A Italic" => "A", "A" => "A"
  if len(subfamily) == 0:
    subfamily = "Regular"
  subfamily_lc = subfamily.lower()
  if subfamily_lc == "regular" or subfamily_lc == "bold":
    font.info.styleMapFamilyName = family
    # name ID 2 "Subfamily name" (legacy, but required)
    # Value must be one of: "regular", "italic", "bold", "bold italic"
    if subfamily_lc == "regular":
      if isitalic:
        font.info.styleMapStyleName = "italic"
      else:
        font.info.styleMapStyleName = "regular"
    else: # bold
      if isitalic:
        font.info.styleMapStyleName = "bold italic"
      else:
        font.info.styleMapStyleName = "bold"
  else:
    font.info.styleMapFamilyName = (family + ' ' + subfamily).strip()
    # name ID 2 "Subfamily name" (legacy, but required)
    if isitalic:
      font.info.styleMapStyleName = "italic"
    else:
      font.info.styleMapStyleName = "regular"



stripItalic_re = re.compile(r'(?:^|\b)italic\b|italic$', re.I | re.U)


def stripItalic(name):
  return stripItalic_re.sub('', name.strip())