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