def import_from_otf(self, otfpath): """Import color data (CPAL/COLR) from a font file. otfpath: Path of the font file""" font = TTFont(otfpath) if not (font.has_key("COLR") and font.has_key("CPAL")): print("ERROR: No COLR and CPAL table present in %s" % otfpath) else: print("Reading palette data ...") cpal = table_C_P_A_L_("CPAL") cpal.decompile(font["CPAL"].data, font) for j in range(len(cpal.palettes)): palette = cpal.palettes[j] _palette = {} for i in range(len(palette)): color = palette[i] _palette[str(i)] = "#%02x%02x%02x%02x" % ( color.red, color.green, color.blue, color.alpha ) self.palettes.append(_palette) colr = table_C_O_L_R_("COLR") colr.decompile(font["COLR"].data, font) print("Reading layer data ...") for glyphname in colr.ColorLayers: layers = colr[glyphname] _glyph = ColorGlyph(self) _glyph.basename = glyphname for layer in layers: _glyph.layers.append(layer.name) _glyph.colors.append(layer.colorID) self[glyphname] = _glyph print("Done.") font.close()
def _export_colr(self, otfpath): font = TTFont(otfpath) if (font.has_key("COLR") and font.has_key("CPAL")): print(" WARNING: Replacing existing COLR and CPAL tables in %s" % otfpath) print(" Writing palette data ...") cpal = table_C_P_A_L_("CPAL") cpal.version = 0 cpal.numPaletteEntries = len(self.palettes[0]) cpal.palettes = [] for j in range(len(self.palettes)): palette = self.palettes[j] _palette = [] # keep a map of old to new indices (palette indices are # not saved in font) if j == 0: reindex = {0xffff: 0xffff} count = 0 for i in sorted(palette.keys(), key=lambda k: int(k)): _color = Color() _color.red = int(palette[i][1:3], 16) _color.green = int(palette[i][3:5], 16) _color.blue = int(palette[i][5:7], 16) if len(palette[i]) >= 9: _color.alpha = int(palette[i][7:9], 16) else: _color.alpha = 0xff if j == 0: reindex[int(i)] = count count += 1 _palette.append(_color) print(" Appending palette", _palette) #print("ReIndex:", reindex) cpal.palettes.append(_palette) print(" Writing layer data ...") colr = table_C_O_L_R_("COLR") colr.version = 0 colr.ColorLayers = {} for glyphname in self.keys(): _layer_records = [] for i in range(len(self[glyphname].layers)): glyph = self[glyphname] _layer_records.append( LayerRecord(glyph.layers[i], reindex[glyph.colors[i]]) ) colr[glyphname] = _layer_records # save font["CPAL"] = cpal font["COLR"] = colr font.save(otfpath[:-4] + "_colr" + otfpath[-4:]) font.close()
def test_decompile_and_dump_xml(self, font): colr = table_C_O_L_R_() colr.decompile(COLR_V0_DATA, font) dump(colr, font) assert getXML(colr.toXML, font) == COLR_V0_XML
def test_decompile_and_compile(self, font): colr = table_C_O_L_R_() colr.decompile(COLR_V0_DATA, font) diff_binary_fragments(colr.compile(font), COLR_V0_SAMPLE)
def test_load_from_xml_and_compile(self, font): colr = table_C_O_L_R_() for name, attrs, content in parseXML(COLR_V1_XML): colr.fromXML(name, attrs, content, font) diff_binary_fragments(colr.compile(font), COLR_V1_SAMPLE)
def test_load_from_xml_and_compile(self, font): colr = table_C_O_L_R_() for name, attrs, content in parseXML(COLR_V0_XML): colr.fromXML(name, attrs, content, font) assert colr.compile(font) == COLR_V0_DATA
def test_decompile_and_compile(self, font): colr = table_C_O_L_R_() colr.decompile(COLR_V0_DATA, font) assert colr.compile(font) == COLR_V0_DATA