generateGlyph.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. # Copyright 2015 Google Inc. All Rights Reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import re
  15. from string import find
  16. from anchors import alignComponentsToAnchors, getAnchorByName
  17. def parseComposite(composite):
  18. c = composite.split("=")
  19. d = c[1].split("/")
  20. glyphName = d[0]
  21. if len(d) == 1:
  22. offset = [0, 0]
  23. else:
  24. offset = [int(i) for i in d[1].split(",")]
  25. accentString = c[0]
  26. accents = accentString.split("+")
  27. baseName = accents.pop(0)
  28. accentNames = [i.split(":") for i in accents]
  29. return (glyphName, baseName, accentNames, offset)
  30. def copyMarkAnchors(f, g, srcname, width):
  31. unicode_range = set(
  32. range(0x0030, 0x02B0) + range(0x1E00, 0x1EFF) +
  33. [0x430, 0x435, 0x440, 0x441, 0x445, 0x455, 0x456, 0x471])
  34. for anchor in f[srcname].anchors:
  35. if anchor.name in ("top_dd", "bottom_dd", "top0315"):
  36. g.appendAnchor(anchor.name, (anchor.x + width, anchor.y))
  37. if ("top" == anchor.name and
  38. (g.unicode in unicode_range or
  39. g.name.endswith((".ccmp", ".smcp", ".NAV"))) and
  40. not any(a.name == "parent_top" for a in g.anchors)):
  41. g.appendAnchor("parent_top", anchor.position)
  42. if ("bottom" == anchor.name and
  43. (g.unicode in unicode_range or g.name.endswith(".smcp")) and
  44. not any(a.name == "bottom" for a in g.anchors)):
  45. g.appendAnchor("bottom", anchor.position)
  46. if any(a.name == "top" for a in g.anchors):
  47. return
  48. anchor_parent_top = getAnchorByName(g, "parent_top")
  49. if anchor_parent_top is not None:
  50. g.appendAnchor("top", anchor_parent_top.position)
  51. def generateGlyph(f,gname,glyphList={}):
  52. glyphName, baseName, accentNames, offset = parseComposite(gname)
  53. if baseName.find("_") != -1:
  54. g = f.newGlyph(glyphName)
  55. for componentName in baseName.split("_"):
  56. g.appendComponent(componentName, (g.width, 0))
  57. g.width += f[componentName].width
  58. setUnicodeValue(g, glyphList)
  59. else:
  60. if f.has_key(glyphName):
  61. print('Existing glyph "%s" found in font, ignoring composition '
  62. 'rule "%s"' % (glyphName, gname))
  63. return
  64. try:
  65. f.compileGlyph(glyphName, baseName, accentNames)
  66. except KeyError as e:
  67. print('KeyError raised for composition rule "%s", likely "%s" '
  68. 'anchor not found in glyph "%s"' % (gname, e, baseName))
  69. return
  70. g = f[glyphName]
  71. setUnicodeValue(g, glyphList)
  72. copyMarkAnchors(f, g, baseName, offset[1] + offset[0])
  73. if len(accentNames) > 0:
  74. alignComponentsToAnchors(f, glyphName, baseName, accentNames)
  75. if offset[0] != 0 or offset[1] != 0:
  76. g.width += offset[1] + offset[0]
  77. g.move((offset[0], 0), anchors=False)
  78. def setUnicodeValue(glyph, glyphList):
  79. """Try to ensure glyph has a unicode value -- used by FDK to make OTFs."""
  80. if glyph.name in glyphList:
  81. glyph.unicode = int(glyphList[glyph.name], 16)
  82. else:
  83. uvNameMatch = re.match("uni([\dA-F]{4})$", glyph.name)
  84. if uvNameMatch:
  85. glyph.unicode = int(uvNameMatch.group(1), 16)