generateGlyph.py 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. for anchor in f[srcname].anchors:
  32. if anchor.name in ("top_dd", "bottom_dd", "top0315"):
  33. g.appendAnchor(anchor.name, (anchor.x + width, anchor.y))
  34. if ("top" == anchor.name and
  35. not any(a.name == "parent_top" for a in g.anchors)):
  36. g.appendAnchor("parent_top", anchor.position)
  37. if ("bottom" == anchor.name and
  38. not any(a.name == "bottom" for a in g.anchors)):
  39. g.appendAnchor("bottom", anchor.position)
  40. if any(a.name == "top" for a in g.anchors):
  41. return
  42. anchor_parent_top = getAnchorByName(g, "parent_top")
  43. if anchor_parent_top is not None:
  44. g.appendAnchor("top", anchor_parent_top.position)
  45. def generateGlyph(f,gname,glyphList={}):
  46. glyphName, baseName, accentNames, offset = parseComposite(gname)
  47. if f.has_key(glyphName):
  48. print('Existing glyph "%s" found in font, ignoring composition rule '
  49. '"%s"' % (glyphName, gname))
  50. return
  51. if baseName.find("_") != -1:
  52. g = f.newGlyph(glyphName)
  53. for componentName in baseName.split("_"):
  54. g.appendComponent(componentName, (g.width, 0))
  55. g.width += f[componentName].width
  56. setUnicodeValue(g, glyphList)
  57. else:
  58. try:
  59. f.compileGlyph(glyphName, baseName, accentNames)
  60. except KeyError as e:
  61. print('KeyError raised for composition rule "%s", likely "%s" '
  62. 'anchor not found in glyph "%s"' % (gname, e, baseName))
  63. return
  64. g = f[glyphName]
  65. setUnicodeValue(g, glyphList)
  66. copyMarkAnchors(f, g, baseName, offset[1] + offset[0])
  67. if len(accentNames) > 0:
  68. alignComponentsToAnchors(f, glyphName, baseName, accentNames)
  69. if offset[0] != 0 or offset[1] != 0:
  70. g.width += offset[1] + offset[0]
  71. g.move((offset[0], 0), anchors=False)
  72. def setUnicodeValue(glyph, glyphList):
  73. """Try to ensure glyph has a unicode value -- used by FDK to make OTFs."""
  74. if glyph.name in glyphList:
  75. glyph.unicode = int(glyphList[glyph.name], 16)
  76. else:
  77. uvNameMatch = re.match("uni([\dA-F]{4})$", glyph.name)
  78. if uvNameMatch:
  79. glyph.unicode = int(uvNameMatch.group(1), 16)