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