C_F_F_.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. from io import BytesIO
  2. from fontTools import cffLib
  3. from . import DefaultTable
  4. class table_C_F_F_(DefaultTable.DefaultTable):
  5. """Compact Font Format table (version 1)
  6. The ``CFF`` table embeds a CFF-formatted font. The CFF font format
  7. predates OpenType and could be used as a standalone font file, but the
  8. ``CFF`` table is also used to package CFF fonts into an OpenType
  9. container.
  10. .. note::
  11. ``CFF`` has been succeeded by ``CFF2``, which eliminates much of
  12. the redundancy incurred by embedding CFF version 1 in an OpenType
  13. font.
  14. See also https://learn.microsoft.com/en-us/typography/opentype/spec/cff
  15. """
  16. def __init__(self, tag=None):
  17. DefaultTable.DefaultTable.__init__(self, tag)
  18. self.cff = cffLib.CFFFontSet()
  19. self._gaveGlyphOrder = False
  20. def decompile(self, data, otFont):
  21. self.cff.decompile(BytesIO(data), otFont, isCFF2=False)
  22. assert len(self.cff) == 1, "can't deal with multi-font CFF tables."
  23. def compile(self, otFont):
  24. f = BytesIO()
  25. self.cff.compile(f, otFont, isCFF2=False)
  26. return f.getvalue()
  27. def haveGlyphNames(self):
  28. if hasattr(self.cff[self.cff.fontNames[0]], "ROS"):
  29. return False # CID-keyed font
  30. else:
  31. return True
  32. def getGlyphOrder(self):
  33. if self._gaveGlyphOrder:
  34. from fontTools import ttLib
  35. raise ttLib.TTLibError("illegal use of getGlyphOrder()")
  36. self._gaveGlyphOrder = True
  37. return self.cff[self.cff.fontNames[0]].getGlyphOrder()
  38. def setGlyphOrder(self, glyphOrder):
  39. pass
  40. # XXX
  41. # self.cff[self.cff.fontNames[0]].setGlyphOrder(glyphOrder)
  42. def toXML(self, writer, otFont):
  43. self.cff.toXML(writer)
  44. def fromXML(self, name, attrs, content, otFont):
  45. if not hasattr(self, "cff"):
  46. self.cff = cffLib.CFFFontSet()
  47. self.cff.fromXML(name, attrs, content, otFont)