C_B_D_T_.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. # Copyright 2013 Google, Inc. All Rights Reserved.
  2. #
  3. # Google Author(s): Matt Fontaine
  4. from fontTools.misc.textTools import bytesjoin
  5. from fontTools.misc import sstruct
  6. from . import E_B_D_T_
  7. from .BitmapGlyphMetrics import (
  8. BigGlyphMetrics,
  9. bigGlyphMetricsFormat,
  10. SmallGlyphMetrics,
  11. smallGlyphMetricsFormat,
  12. )
  13. from .E_B_D_T_ import (
  14. BitmapGlyph,
  15. BitmapPlusSmallMetricsMixin,
  16. BitmapPlusBigMetricsMixin,
  17. )
  18. import struct
  19. class table_C_B_D_T_(E_B_D_T_.table_E_B_D_T_):
  20. # Change the data locator table being referenced.
  21. locatorName = "CBLC"
  22. # Modify the format class accessor for color bitmap use.
  23. def getImageFormatClass(self, imageFormat):
  24. try:
  25. return E_B_D_T_.table_E_B_D_T_.getImageFormatClass(self, imageFormat)
  26. except KeyError:
  27. return cbdt_bitmap_classes[imageFormat]
  28. # Helper method for removing export features not supported by color bitmaps.
  29. # Write data in the parent class will default to raw if an option is unsupported.
  30. def _removeUnsupportedForColor(dataFunctions):
  31. dataFunctions = dict(dataFunctions)
  32. del dataFunctions["row"]
  33. return dataFunctions
  34. class ColorBitmapGlyph(BitmapGlyph):
  35. fileExtension = ".png"
  36. xmlDataFunctions = _removeUnsupportedForColor(BitmapGlyph.xmlDataFunctions)
  37. class cbdt_bitmap_format_17(BitmapPlusSmallMetricsMixin, ColorBitmapGlyph):
  38. def decompile(self):
  39. self.metrics = SmallGlyphMetrics()
  40. dummy, data = sstruct.unpack2(smallGlyphMetricsFormat, self.data, self.metrics)
  41. (dataLen,) = struct.unpack(">L", data[:4])
  42. data = data[4:]
  43. # For the image data cut it to the size specified by dataLen.
  44. assert dataLen <= len(data), "Data overun in format 17"
  45. self.imageData = data[:dataLen]
  46. def compile(self, ttFont):
  47. dataList = []
  48. dataList.append(sstruct.pack(smallGlyphMetricsFormat, self.metrics))
  49. dataList.append(struct.pack(">L", len(self.imageData)))
  50. dataList.append(self.imageData)
  51. return bytesjoin(dataList)
  52. class cbdt_bitmap_format_18(BitmapPlusBigMetricsMixin, ColorBitmapGlyph):
  53. def decompile(self):
  54. self.metrics = BigGlyphMetrics()
  55. dummy, data = sstruct.unpack2(bigGlyphMetricsFormat, self.data, self.metrics)
  56. (dataLen,) = struct.unpack(">L", data[:4])
  57. data = data[4:]
  58. # For the image data cut it to the size specified by dataLen.
  59. assert dataLen <= len(data), "Data overun in format 18"
  60. self.imageData = data[:dataLen]
  61. def compile(self, ttFont):
  62. dataList = []
  63. dataList.append(sstruct.pack(bigGlyphMetricsFormat, self.metrics))
  64. dataList.append(struct.pack(">L", len(self.imageData)))
  65. dataList.append(self.imageData)
  66. return bytesjoin(dataList)
  67. class cbdt_bitmap_format_19(ColorBitmapGlyph):
  68. def decompile(self):
  69. (dataLen,) = struct.unpack(">L", self.data[:4])
  70. data = self.data[4:]
  71. assert dataLen <= len(data), "Data overun in format 19"
  72. self.imageData = data[:dataLen]
  73. def compile(self, ttFont):
  74. return struct.pack(">L", len(self.imageData)) + self.imageData
  75. # Dict for CBDT extended formats.
  76. cbdt_bitmap_classes = {
  77. 17: cbdt_bitmap_format_17,
  78. 18: cbdt_bitmap_format_18,
  79. 19: cbdt_bitmap_format_19,
  80. }