<commit_before># Copyright 2013 Google, Inc. All Rights Reserved.
#
# Google Author(s): <NAME>

from fontTools.misc.py23 import *
from fontTools.misc.textTools import safeEval
from . import DefaultTable


class table_C_O_L_R_(DefaultTable.DefaultTable):

	""" This table is structured so that you can treat it like a dictionary keyed by glyph name.
	ttFont['COLR'][<glyphName>] will return the color layers for any glyph
	ttFont['COLR'][<glyphName>] = <value> will set the color layers for any glyph.
	"""

	def decompile(self, data, ttFont):
		from .otBase import OTTableReader
		from . import otTables

		reader = OTTableReader(data, tableTag=self.tableTag)
		tableClass = getattr(otTables, self.tableTag)
		table = tableClass()
		table.decompile(reader, ttFont)

		self.getGlyphName = ttFont.getGlyphName # for use in get/set item functions, for access by GID
		self.version = table.Version
		assert (self.version == 0), "Version of COLR table is higher than I know how to handle"

		self.ColorLayers = colorLayerLists = {}

		layerRecords = table.LayerRecordArray.LayerRecord
		numLayerRecords = len(layerRecords)
		for baseRec in table.BaseGlyphRecordArray.BaseGlyphRecord:
			baseGlyph = baseRec.BaseGlyph
			firstLayerIndex = baseRec.FirstLayerIndex
			numLayers = baseRec.NumLayers
			assert (firstLayerIndex + numLayers <= numLayerRecords)
			layers = []
			for i in range(firstLayerIndex, firstLayerIndex+numLayers):
				layerRec = layerRecords[i]
				layers.append(LayerRecord(layerRec.LayerGlyph, layerRec.PaletteIndex))
			colorLayerLists[baseGlyph] = layers

	def compile(self, ttFont):
		from .otBase import OTTableWriter
		from . import otTables

		ttFont.getReverseGlyphMap(rebuild=True)

		tableClass = getattr(otTables, self.tableTag)
		table = tableClass()

		table.Version = self.version

		table.BaseGlyphRecordArray = otTables.BaseGlyphRecordArray()
		table.BaseGlyphRecordArray.BaseGlyphRecord = baseGlyphRecords = []
		table.LayerRecordArray = otTables.LayerRecordArray()
		table.LayerRecordArray.LayerRecord = layerRecords = []

		for baseGlyph in sorted(self.ColorLayers.keys(), key=ttFont.getGlyphID):
			layers = self.ColorLayers[baseGlyph]

			baseRec = otTables.BaseGlyphRecord()
			baseRec.BaseGlyph = baseGlyph
			baseRec.FirstLayerIndex = len(layerRecords)
			baseRec.NumLayers = len(layers)
			baseGlyphRecords.append(baseRec)

			for layer in layers:
				layerRec = otTables.LayerRecord()
				layerRec.LayerGlyph = layer.name
				layerRec.PaletteIndex = layer.colorID
				layerRecords.append(layerRec)

		writer = OTTableWriter(tableTag=self.tableTag)
		table.compile(writer, ttFont)
		return writer.getAllData()

	def toXML(self, writer, ttFont):
		writer.simpletag("version", value=self.version)
		writer.newline()
		for baseGlyph in sorted(self.ColorLayers.keys(), key=ttFont.getGlyphID):
			writer.begintag("ColorGlyph", name=baseGlyph)
			writer.newline()
			for layer in self.ColorLayers[baseGlyph]:
				layer.toXML(writer, ttFont)
			writer.endtag("ColorGlyph")
			writer.newline()

	def fromXML(self, name, attrs, content, ttFont):
		if not hasattr(self, "ColorLayers"):
			self.ColorLayers = {}
		self.getGlyphName = ttFont.getGlyphName # for use in get/set item functions, for access by GID
		if name == "ColorGlyph":
			glyphName = attrs["name"]
			for element in content:
				if isinstance(element, basestring):
					continue
			layers = []
			for element in content:
				if isinstance(element, basestring):
					continue
				layer = LayerRecord()
				layer.fromXML(element[0], element[1], element[2], ttFont)
				layers.append (layer)
			self[glyphName] = layers
		elif "value" in attrs:
			setattr(self, name, safeEval(attrs["value"]))

	def __getitem__(self, glyphSelector):
		if isinstance(glyphSelector, int):
			# its a gid, convert to glyph name
			glyphSelector = self.getGlyphName(glyphSelector)

		if glyphSelector not in self.ColorLayers:
			return None

		return self.ColorLayers[glyphSelector]

	def __setitem__(self, glyphSelector, value):
		if isinstance(glyphSelector, int):
			# its a gid, convert to glyph name
			glyphSelector = self.getGlyphName(glyphSelector)

		if  value:
			self.ColorLayers[glyphSelector] = value
		elif glyphSelector in self.ColorLayers:
			del self.ColorLayers[glyphSelector]

	def __delitem__(self, glyphSelector):
		del self.ColorLayers[glyphSelector]

class LayerRecord(object):

	def __init__(self, name=None, colorID=None):
		self.name = name
		self.colorID = colorID

	def toXML(self, writer, ttFont):
		writer.simpletag("layer", name=self.name, colorID=self.colorID)
		writer.newline()

	def fromXML(self, eltname, attrs, content, ttFont):
		for (name, value) in attrs.items():
			if name == "name":
				if isinstance(value, int):
					value = ttFont.getGlyphName(value)
				setattr(self, name, value)
			else:
				setattr(self, name, safeEval(value))<commit_msg>COLR: remove ability to get/set by glyphID as incoherent with rest of ttLib classes<commit_after># Copyright 2013 Google, Inc. All Rights Reserved.
#
# Google Author(s): <NAME>

from fontTools.misc.py23 import *
from fontTools.misc.textTools import safeEval
from . import DefaultTable


class table_C_O_L_R_(DefaultTable.DefaultTable):

	""" This table is structured so that you can treat it like a dictionary keyed by glyph name.
	ttFont['COLR'][<glyphName>] will return the color layers for any glyph
	ttFont['COLR'][<glyphName>] = <value> will set the color layers for any glyph.
	"""

	def decompile(self, data, ttFont):
		from .otBase import OTTableReader
		from . import otTables

		reader = OTTableReader(data, tableTag=self.tableTag)
		tableClass = getattr(otTables, self.tableTag)
		table = tableClass()
		table.decompile(reader, ttFont)

		self.version = table.Version
		assert (self.version == 0), "Version of COLR table is higher than I know how to handle"

		self.ColorLayers = colorLayerLists = {}

		layerRecords = table.LayerRecordArray.LayerRecord
		numLayerRecords = len(layerRecords)
		for baseRec in table.BaseGlyphRecordArray.BaseGlyphRecord:
			baseGlyph = baseRec.BaseGlyph
			firstLayerIndex = baseRec.FirstLayerIndex
			numLayers = baseRec.NumLayers
			assert (firstLayerIndex + numLayers <= numLayerRecords)
			layers = []
			for i in range(firstLayerIndex, firstLayerIndex+numLayers):
				layerRec = layerRecords[i]
				layers.append(LayerRecord(layerRec.LayerGlyph, layerRec.PaletteIndex))
			colorLayerLists[baseGlyph] = layers

	def compile(self, ttFont):
		from .otBase import OTTableWriter
		from . import otTables

		ttFont.getReverseGlyphMap(rebuild=True)

		tableClass = getattr(otTables, self.tableTag)
		table = tableClass()

		table.Version = self.version

		table.BaseGlyphRecordArray = otTables.BaseGlyphRecordArray()
		table.BaseGlyphRecordArray.BaseGlyphRecord = baseGlyphRecords = []
		table.LayerRecordArray = otTables.LayerRecordArray()
		table.LayerRecordArray.LayerRecord = layerRecords = []

		for baseGlyph in sorted(self.ColorLayers.keys(), key=ttFont.getGlyphID):
			layers = self.ColorLayers[baseGlyph]

			baseRec = otTables.BaseGlyphRecord()
			baseRec.BaseGlyph = baseGlyph
			baseRec.FirstLayerIndex = len(layerRecords)
			baseRec.NumLayers = len(layers)
			baseGlyphRecords.append(baseRec)

			for layer in layers:
				layerRec = otTables.LayerRecord()
				layerRec.LayerGlyph = layer.name
				layerRec.PaletteIndex = layer.colorID
				layerRecords.append(layerRec)

		writer = OTTableWriter(tableTag=self.tableTag)
		table.compile(writer, ttFont)
		return writer.getAllData()

	def toXML(self, writer, ttFont):
		writer.simpletag("version", value=self.version)
		writer.newline()
		for baseGlyph in sorted(self.ColorLayers.keys(), key=ttFont.getGlyphID):
			writer.begintag("ColorGlyph", name=baseGlyph)
			writer.newline()
			for layer in self.ColorLayers[baseGlyph]:
				layer.toXML(writer, ttFont)
			writer.endtag("ColorGlyph")
			writer.newline()

	def fromXML(self, name, attrs, content, ttFont):
		if not hasattr(self, "ColorLayers"):
			self.ColorLayers = {}
		if name == "ColorGlyph":
			glyphName = attrs["name"]
			for element in content:
				if isinstance(element, basestring):
					continue
			layers = []
			for element in content:
				if isinstance(element, basestring):
					continue
				layer = LayerRecord()
				layer.fromXML(element[0], element[1], element[2], ttFont)
				layers.append (layer)
			self[glyphName] = layers
		elif "value" in attrs:
			setattr(self, name, safeEval(attrs["value"]))

	def __getitem__(self, glyphName):
		if not isinstance(glyphName, str):
			raise TypeError(f"expected str, found {type(glyphName).__name__}")
		return self.ColorLayers[glyphName]

	def __setitem__(self, glyphName, value):
		if not isinstance(glyphName, str):
			raise TypeError(f"expected str, found {type(glyphName).__name__}")
		if value is not None:
			self.ColorLayers[glyphName] = value
		elif glyphName in self.ColorLayers:
			del self.ColorLayers[glyphName]

	def __delitem__(self, glyphName):
		del self.ColorLayers[glyphName]

class LayerRecord(object):

	def __init__(self, name=None, colorID=None):
		self.name = name
		self.colorID = colorID

	def toXML(self, writer, ttFont):
		writer.simpletag("layer", name=self.name, colorID=self.colorID)
		writer.newline()

	def fromXML(self, eltname, attrs, content, ttFont):
		for (name, value) in attrs.items():
			if name == "name":
				if isinstance(value, int):
					value = ttFont.getGlyphName(value)
				setattr(self, name, value)
			else:
				setattr(self, name, safeEval(value))
{'fraction_non_alphanumeric': 0.10385224274406332, 'fraction_numerical': 0.0021108179419525065, 'mean_word_length': 4.637120761451517, 'pattern_counts': {'":': 4, '<': 13, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '959697', 'n_tokens_mistral': 3500, 'n_tokens_neox': 3009, 'n_words': 746}