2013-09-19 20:02:17 -04:00
|
|
|
# Copyright 2013 Google, Inc. All Rights Reserved.
|
|
|
|
#
|
|
|
|
# Google Author(s): Behdad Esfahbod
|
|
|
|
|
2014-01-14 15:07:50 +08:00
|
|
|
from __future__ import print_function, division, absolute_import
|
2013-11-27 17:27:45 -05:00
|
|
|
from fontTools.misc.py23 import *
|
|
|
|
from fontTools.misc.textTools import safeEval
|
2013-11-27 02:34:11 -05:00
|
|
|
from . import DefaultTable
|
2016-04-27 13:14:54 +02:00
|
|
|
import array
|
2013-07-24 12:31:50 -04:00
|
|
|
import struct
|
2016-04-27 13:14:54 +02:00
|
|
|
import sys
|
2013-07-24 12:31:50 -04:00
|
|
|
|
|
|
|
|
|
|
|
class table_C_P_A_L_(DefaultTable.DefaultTable):
|
|
|
|
|
2016-04-27 13:14:54 +02:00
|
|
|
def __init__(self, tag=None):
|
|
|
|
DefaultTable.DefaultTable.__init__(self, tag)
|
|
|
|
self.palettes = []
|
|
|
|
self.paletteTypes = []
|
|
|
|
self.paletteLabels = []
|
|
|
|
self.paletteEntryLabels = []
|
|
|
|
|
2013-07-24 12:31:50 -04:00
|
|
|
def decompile(self, data, ttFont):
|
|
|
|
self.version, self.numPaletteEntries, numPalettes, numColorRecords, goffsetFirstColorRecord = struct.unpack(">HHHHL", data[:12])
|
2016-04-27 13:14:54 +02:00
|
|
|
assert (self.version <= 1), "Version of CPAL table is higher than I know how to handle"
|
2013-07-24 12:31:50 -04:00
|
|
|
self.palettes = []
|
|
|
|
pos = 12
|
|
|
|
for i in range(numPalettes):
|
|
|
|
startIndex = struct.unpack(">H", data[pos:pos+2])[0]
|
|
|
|
assert (startIndex + self.numPaletteEntries <= numColorRecords)
|
|
|
|
pos += 2
|
|
|
|
palette = []
|
|
|
|
ppos = goffsetFirstColorRecord + startIndex * 4
|
|
|
|
for j in range(self.numPaletteEntries):
|
|
|
|
palette.append( Color(*struct.unpack(">BBBB", data[ppos:ppos+4])) )
|
|
|
|
ppos += 4
|
|
|
|
self.palettes.append(palette)
|
2016-04-27 13:14:54 +02:00
|
|
|
if self.version == 0:
|
|
|
|
offsetToPaletteTypeArray = 0
|
|
|
|
offsetToPaletteLabelArray = 0
|
|
|
|
offsetToPaletteEntryLabelArray = 0
|
|
|
|
else:
|
|
|
|
pos = 12 + numPalettes * 2
|
|
|
|
(offsetToPaletteTypeArray, offsetToPaletteLabelArray,
|
|
|
|
offsetToPaletteEntryLabelArray) = (
|
|
|
|
struct.unpack(">LLL", data[pos:pos+12]))
|
|
|
|
self.paletteTypes = self._decompileUInt32Array(
|
|
|
|
data, offsetToPaletteTypeArray, numPalettes)
|
|
|
|
self.paletteLabels = self._decompileUInt16Array(
|
|
|
|
data, offsetToPaletteLabelArray, numPalettes)
|
|
|
|
self.paletteEntryLabels = self._decompileUInt16Array(
|
|
|
|
data, offsetToPaletteEntryLabelArray,
|
|
|
|
self.numPaletteEntries)
|
|
|
|
|
|
|
|
def _decompileUInt16Array(self, data, offset, numElements):
|
|
|
|
if offset == 0:
|
|
|
|
return [0] * numElements
|
|
|
|
result = array.array("H", data[offset : offset + 2 * numElements])
|
|
|
|
if sys.byteorder != "big":
|
|
|
|
result.byteswap()
|
|
|
|
assert len(result) == numElements, result
|
|
|
|
return result.tolist()
|
|
|
|
|
|
|
|
def _decompileUInt32Array(self, data, offset, numElements):
|
|
|
|
if offset == 0:
|
|
|
|
return [0] * numElements
|
|
|
|
result = array.array("I", data[offset : offset + 4 * numElements])
|
|
|
|
if sys.byteorder != "big":
|
|
|
|
result.byteswap()
|
|
|
|
assert len(result) == numElements, result
|
|
|
|
return result.tolist()
|
2013-07-24 12:31:50 -04:00
|
|
|
|
|
|
|
def compile(self, ttFont):
|
2016-04-26 17:40:00 +02:00
|
|
|
colorRecordIndices, colorRecords = self._compileColorRecords()
|
2016-04-27 13:14:54 +02:00
|
|
|
paletteTypes = self._compilePaletteTypes()
|
|
|
|
paletteLabels = self._compilePaletteLabels()
|
|
|
|
paletteEntryLabels = self._compilePaletteEntryLabels()
|
|
|
|
numColorRecords = len(colorRecords) // 4
|
2016-04-26 17:40:00 +02:00
|
|
|
offsetToFirstColorRecord = 12 + len(colorRecordIndices)
|
2016-04-27 13:14:54 +02:00
|
|
|
if self.version >= 1:
|
|
|
|
offsetToFirstColorRecord += 12
|
|
|
|
header = struct.pack(">HHHHL", self.version,
|
|
|
|
self.numPaletteEntries, len(self.palettes),
|
|
|
|
numColorRecords, offsetToFirstColorRecord)
|
|
|
|
if self.version == 0:
|
|
|
|
dataList = [header, colorRecordIndices, colorRecords]
|
|
|
|
else:
|
|
|
|
pos = offsetToFirstColorRecord + len(colorRecords)
|
|
|
|
if len(paletteTypes) == 0:
|
|
|
|
offsetToPaletteTypeArray = 0
|
|
|
|
else:
|
|
|
|
offsetToPaletteTypeArray = pos
|
|
|
|
pos += len(paletteTypes)
|
|
|
|
if len(paletteLabels) == 0:
|
|
|
|
offsetToPaletteLabelArray = 0
|
|
|
|
else:
|
|
|
|
offsetToPaletteLabelArray = pos
|
|
|
|
pos += len(paletteLabels)
|
|
|
|
if len(paletteEntryLabels) == 0:
|
|
|
|
offsetToPaletteEntryLabelArray = 0
|
|
|
|
else:
|
|
|
|
offsetToPaletteEntryLabelArray = pos
|
|
|
|
pos += len(paletteLabels)
|
|
|
|
header1 = struct.pack(">LLL",
|
|
|
|
offsetToPaletteTypeArray,
|
|
|
|
offsetToPaletteLabelArray,
|
|
|
|
offsetToPaletteEntryLabelArray)
|
|
|
|
dataList = [header, colorRecordIndices, header1,
|
|
|
|
colorRecords, paletteTypes, paletteLabels,
|
|
|
|
paletteEntryLabels]
|
2016-04-26 17:40:00 +02:00
|
|
|
return bytesjoin(dataList)
|
|
|
|
|
|
|
|
def _compilePalette(self, palette):
|
|
|
|
assert(len(palette) == self.numPaletteEntries)
|
|
|
|
pack = lambda c: struct.pack(">BBBB", c.blue, c.green, c.red, c.alpha)
|
|
|
|
return bytesjoin([pack(color) for color in palette])
|
|
|
|
|
|
|
|
def _compileColorRecords(self):
|
|
|
|
colorRecords, colorRecordIndices, pool = [], [], {}
|
2013-07-24 12:31:50 -04:00
|
|
|
for palette in self.palettes:
|
2016-04-26 17:40:00 +02:00
|
|
|
packedPalette = self._compilePalette(palette)
|
|
|
|
if packedPalette in pool:
|
|
|
|
index = pool[packedPalette]
|
|
|
|
else:
|
|
|
|
index = len(colorRecords)
|
|
|
|
colorRecords.append(packedPalette)
|
|
|
|
pool[packedPalette] = index
|
|
|
|
colorRecordIndices.append(struct.pack(">H", index * self.numPaletteEntries))
|
|
|
|
return bytesjoin(colorRecordIndices), bytesjoin(colorRecords)
|
2013-07-24 12:31:50 -04:00
|
|
|
|
2016-04-27 13:14:54 +02:00
|
|
|
def _compilePaletteTypes(self):
|
|
|
|
if self.version == 0 or not any(self.paletteTypes):
|
|
|
|
return b''
|
|
|
|
assert len(self.paletteTypes) == len(self.palettes)
|
|
|
|
result = bytesjoin([struct.pack(">I", ptype)
|
|
|
|
for ptype in self.paletteTypes])
|
|
|
|
assert len(result) == 4 * len(self.palettes)
|
|
|
|
return result
|
|
|
|
|
|
|
|
def _compilePaletteLabels(self):
|
|
|
|
if self.version == 0 or not any(self.paletteLabels):
|
|
|
|
return b''
|
|
|
|
assert len(self.paletteLabels) == len(self.palettes)
|
|
|
|
result = bytesjoin([struct.pack(">H", label)
|
|
|
|
for label in self.paletteLabels])
|
|
|
|
assert len(result) == 2 * len(self.palettes)
|
|
|
|
return result
|
|
|
|
|
|
|
|
def _compilePaletteEntryLabels(self):
|
|
|
|
if self.version == 0 or not any(self.paletteEntryLabels):
|
|
|
|
return b''
|
|
|
|
assert len(self.paletteEntryLabels) == self.numPaletteEntries
|
|
|
|
result = bytesjoin([struct.pack(">H", label)
|
|
|
|
for label in self.paletteEntryLabels])
|
|
|
|
assert len(result) == 2 * self.numPaletteEntries
|
|
|
|
return result
|
|
|
|
|
2013-07-24 12:31:50 -04:00
|
|
|
def toXML(self, writer, ttFont):
|
2016-04-27 13:14:54 +02:00
|
|
|
numPalettes = len(self.palettes)
|
|
|
|
paletteLabels = {i: nameID
|
|
|
|
for (i, nameID) in enumerate(self.paletteLabels)}
|
|
|
|
paletteTypes = {i: typ for (i, typ) in enumerate(self.paletteTypes)}
|
2013-07-24 12:31:50 -04:00
|
|
|
writer.simpletag("version", value=self.version)
|
|
|
|
writer.newline()
|
2016-04-27 13:14:54 +02:00
|
|
|
writer.simpletag("numPaletteEntries",
|
|
|
|
value=self.numPaletteEntries)
|
2013-07-24 12:31:50 -04:00
|
|
|
writer.newline()
|
|
|
|
for index, palette in enumerate(self.palettes):
|
2016-04-27 13:14:54 +02:00
|
|
|
attrs = {"index": index}
|
|
|
|
paletteType = paletteTypes.get(index)
|
|
|
|
paletteLabel = paletteLabels.get(index)
|
|
|
|
if self.version > 0 and paletteLabel is not None:
|
|
|
|
attrs["label"] = paletteLabel
|
|
|
|
if self.version > 0 and paletteType is not None:
|
|
|
|
attrs["type"] = paletteType
|
|
|
|
writer.begintag("palette", **attrs)
|
2013-07-24 12:31:50 -04:00
|
|
|
writer.newline()
|
2016-04-27 13:14:54 +02:00
|
|
|
if (self.version > 0 and paletteLabel and
|
|
|
|
ttFont and "name" in ttFont):
|
|
|
|
name = ttFont["name"].getDebugName(paletteLabel)
|
|
|
|
if name is not None:
|
|
|
|
writer.newline()
|
|
|
|
writer.comment(name)
|
|
|
|
writer.newline()
|
2013-07-24 12:31:50 -04:00
|
|
|
assert(len(palette) == self.numPaletteEntries)
|
|
|
|
for cindex, color in enumerate(palette):
|
|
|
|
color.toXML(writer, ttFont, cindex)
|
|
|
|
writer.endtag("palette")
|
|
|
|
writer.newline()
|
2016-04-27 13:14:54 +02:00
|
|
|
if self.version > 0 and any(self.paletteEntryLabels):
|
|
|
|
writer.begintag("paletteEntryLabels")
|
|
|
|
writer.newline()
|
|
|
|
for index, label in enumerate(self.paletteEntryLabels):
|
|
|
|
if label:
|
|
|
|
writer.simpletag("label", index=index, value=label)
|
|
|
|
writer.newline()
|
|
|
|
writer.endtag("paletteEntryLabels")
|
|
|
|
writer.newline()
|
2013-07-24 12:31:50 -04:00
|
|
|
|
2013-11-27 03:19:32 -05:00
|
|
|
def fromXML(self, name, attrs, content, ttFont):
|
2013-07-24 12:31:50 -04:00
|
|
|
if name == "palette":
|
2016-04-27 13:14:54 +02:00
|
|
|
self.paletteLabels.append(int(attrs.get("label", "0")))
|
|
|
|
self.paletteTypes.append(int(attrs.get("type", "0")))
|
2013-07-24 12:31:50 -04:00
|
|
|
palette = []
|
|
|
|
for element in content:
|
2013-11-27 23:37:57 -05:00
|
|
|
if isinstance(element, basestring):
|
2013-07-24 12:31:50 -04:00
|
|
|
continue
|
|
|
|
color = Color()
|
2013-11-27 03:19:32 -05:00
|
|
|
color.fromXML(element[0], element[1], element[2], ttFont)
|
2016-04-27 13:14:54 +02:00
|
|
|
palette.append(color)
|
2013-07-24 12:31:50 -04:00
|
|
|
self.palettes.append(palette)
|
2016-04-27 13:14:54 +02:00
|
|
|
elif name == "paletteEntryLabels":
|
|
|
|
colorLabels = {}
|
|
|
|
for element in content:
|
|
|
|
if isinstance(element, basestring):
|
|
|
|
continue
|
|
|
|
elementName, elementAttr, _ = element
|
|
|
|
if elementName == "label":
|
|
|
|
labelIndex = safeEval(elementAttr["index"])
|
|
|
|
nameID = safeEval(elementAttr["value"])
|
|
|
|
colorLabels[labelIndex] = nameID
|
|
|
|
self.paletteEntryLabels = [
|
|
|
|
colorLabels.get(i, 0)
|
|
|
|
for i in range(self.numPaletteEntries)]
|
2013-11-27 02:33:03 -05:00
|
|
|
elif "value" in attrs:
|
2015-04-26 00:15:26 -04:00
|
|
|
value = safeEval(attrs["value"])
|
2013-07-24 12:31:50 -04:00
|
|
|
setattr(self, name, value)
|
2016-04-27 13:14:54 +02:00
|
|
|
if name == "numPaletteEntries":
|
|
|
|
self.paletteEntryLabels = [0] * self.numPaletteEntries
|
|
|
|
|
2013-07-24 12:31:50 -04:00
|
|
|
|
2013-11-28 14:26:58 -05:00
|
|
|
class Color(object):
|
2013-07-24 12:31:50 -04:00
|
|
|
|
|
|
|
def __init__(self, blue=None, green=None, red=None, alpha=None):
|
2015-04-26 00:15:26 -04:00
|
|
|
self.blue = blue
|
2013-07-24 12:31:50 -04:00
|
|
|
self.green = green
|
2015-04-26 00:15:26 -04:00
|
|
|
self.red = red
|
2013-07-24 12:31:50 -04:00
|
|
|
self.alpha = alpha
|
|
|
|
|
|
|
|
def hex(self):
|
|
|
|
return "#%02X%02X%02X%02X" % (self.red, self.green, self.blue, self.alpha)
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return self.hex()
|
|
|
|
|
|
|
|
def toXML(self, writer, ttFont, index=None):
|
|
|
|
writer.simpletag("color", value=self.hex(), index=index)
|
|
|
|
writer.newline()
|
|
|
|
|
2013-11-27 03:19:32 -05:00
|
|
|
def fromXML(self, eltname, attrs, content, ttFont):
|
2013-07-24 12:31:50 -04:00
|
|
|
value = attrs["value"]
|
|
|
|
if value[0] == '#':
|
|
|
|
value = value[1:]
|
2015-04-26 00:15:26 -04:00
|
|
|
self.red = int(value[0:2], 16)
|
2013-07-24 12:31:50 -04:00
|
|
|
self.green = int(value[2:4], 16)
|
2015-04-26 00:15:26 -04:00
|
|
|
self.blue = int(value[4:6], 16)
|
2013-07-24 12:31:50 -04:00
|
|
|
self.alpha = int(value[6:8], 16) if len (value) >= 8 else 0xFF
|