cmap.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. # Copyright 2013 Google, Inc. All Rights Reserved.
  2. #
  3. # Google Author(s): Behdad Esfahbod, Roozbeh Pournader
  4. from fontTools.merge.unicode import is_Default_Ignorable
  5. from fontTools.pens.recordingPen import DecomposingRecordingPen
  6. import logging
  7. log = logging.getLogger("fontTools.merge")
  8. def computeMegaGlyphOrder(merger, glyphOrders):
  9. """Modifies passed-in glyphOrders to reflect new glyph names.
  10. Stores merger.glyphOrder."""
  11. megaOrder = {}
  12. for glyphOrder in glyphOrders:
  13. for i, glyphName in enumerate(glyphOrder):
  14. if glyphName in megaOrder:
  15. n = megaOrder[glyphName]
  16. while (glyphName + "." + repr(n)) in megaOrder:
  17. n += 1
  18. megaOrder[glyphName] = n
  19. glyphName += "." + repr(n)
  20. glyphOrder[i] = glyphName
  21. megaOrder[glyphName] = 1
  22. merger.glyphOrder = megaOrder = list(megaOrder.keys())
  23. def _glyphsAreSame(
  24. glyphSet1,
  25. glyphSet2,
  26. glyph1,
  27. glyph2,
  28. advanceTolerance=0.05,
  29. advanceToleranceEmpty=0.20,
  30. ):
  31. pen1 = DecomposingRecordingPen(glyphSet1)
  32. pen2 = DecomposingRecordingPen(glyphSet2)
  33. g1 = glyphSet1[glyph1]
  34. g2 = glyphSet2[glyph2]
  35. g1.draw(pen1)
  36. g2.draw(pen2)
  37. if pen1.value != pen2.value:
  38. return False
  39. # Allow more width tolerance for glyphs with no ink
  40. tolerance = advanceTolerance if pen1.value else advanceToleranceEmpty
  41. # TODO Warn if advances not the same but within tolerance.
  42. if abs(g1.width - g2.width) > g1.width * tolerance:
  43. return False
  44. if hasattr(g1, "height") and g1.height is not None:
  45. if abs(g1.height - g2.height) > g1.height * tolerance:
  46. return False
  47. return True
  48. # Valid (format, platformID, platEncID) triplets for cmap subtables containing
  49. # Unicode BMP-only and Unicode Full Repertoire semantics.
  50. # Cf. OpenType spec for "Platform specific encodings":
  51. # https://docs.microsoft.com/en-us/typography/opentype/spec/name
  52. class _CmapUnicodePlatEncodings:
  53. BMP = {(4, 3, 1), (4, 0, 3), (4, 0, 4), (4, 0, 6)}
  54. FullRepertoire = {(12, 3, 10), (12, 0, 4), (12, 0, 6)}
  55. def computeMegaCmap(merger, cmapTables):
  56. """Sets merger.cmap and merger.glyphOrder."""
  57. # TODO Handle format=14.
  58. # Only merge format 4 and 12 Unicode subtables, ignores all other subtables
  59. # If there is a format 12 table for a font, ignore the format 4 table of it
  60. chosenCmapTables = []
  61. for fontIdx, table in enumerate(cmapTables):
  62. format4 = None
  63. format12 = None
  64. for subtable in table.tables:
  65. properties = (subtable.format, subtable.platformID, subtable.platEncID)
  66. if properties in _CmapUnicodePlatEncodings.BMP:
  67. format4 = subtable
  68. elif properties in _CmapUnicodePlatEncodings.FullRepertoire:
  69. format12 = subtable
  70. else:
  71. log.warning(
  72. "Dropped cmap subtable from font '%s':\t"
  73. "format %2s, platformID %2s, platEncID %2s",
  74. fontIdx,
  75. subtable.format,
  76. subtable.platformID,
  77. subtable.platEncID,
  78. )
  79. if format12 is not None:
  80. chosenCmapTables.append((format12, fontIdx))
  81. elif format4 is not None:
  82. chosenCmapTables.append((format4, fontIdx))
  83. # Build the unicode mapping
  84. merger.cmap = cmap = {}
  85. fontIndexForGlyph = {}
  86. glyphSets = [None for f in merger.fonts] if hasattr(merger, "fonts") else None
  87. for table, fontIdx in chosenCmapTables:
  88. # handle duplicates
  89. for uni, gid in table.cmap.items():
  90. oldgid = cmap.get(uni, None)
  91. if oldgid is None:
  92. cmap[uni] = gid
  93. fontIndexForGlyph[gid] = fontIdx
  94. elif is_Default_Ignorable(uni) or uni in (0x25CC,): # U+25CC DOTTED CIRCLE
  95. continue
  96. elif oldgid != gid:
  97. # Char previously mapped to oldgid, now to gid.
  98. # Record, to fix up in GSUB 'locl' later.
  99. if merger.duplicateGlyphsPerFont[fontIdx].get(oldgid) is None:
  100. if glyphSets is not None:
  101. oldFontIdx = fontIndexForGlyph[oldgid]
  102. for idx in (fontIdx, oldFontIdx):
  103. if glyphSets[idx] is None:
  104. glyphSets[idx] = merger.fonts[idx].getGlyphSet()
  105. # if _glyphsAreSame(glyphSets[oldFontIdx], glyphSets[fontIdx], oldgid, gid):
  106. # continue
  107. merger.duplicateGlyphsPerFont[fontIdx][oldgid] = gid
  108. elif merger.duplicateGlyphsPerFont[fontIdx][oldgid] != gid:
  109. # Char previously mapped to oldgid but oldgid is already remapped to a different
  110. # gid, because of another Unicode character.
  111. # TODO: Try harder to do something about these.
  112. log.warning(
  113. "Dropped mapping from codepoint %#06X to glyphId '%s'", uni, gid
  114. )
  115. def renameCFFCharStrings(merger, glyphOrder, cffTable):
  116. """Rename topDictIndex charStrings based on glyphOrder."""
  117. td = cffTable.cff.topDictIndex[0]
  118. charStrings = {}
  119. for i, v in enumerate(td.CharStrings.charStrings.values()):
  120. glyphName = glyphOrder[i]
  121. charStrings[glyphName] = v
  122. td.CharStrings.charStrings = charStrings
  123. td.charset = list(glyphOrder)