• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1from fontTools.misc.py23 import bytesjoin, tobytes
2from fontTools.misc.textTools import safeEval
3from . import DefaultTable
4import struct
5
6# https://developer.apple.com/fonts/TrueType-Reference-Manual/RM06/Chap6ltag.html
7
8class table__l_t_a_g(DefaultTable.DefaultTable):
9	def __init__(self, tag=None):
10		DefaultTable.DefaultTable.__init__(self, tag)
11		self.version, self.flags = 1, 0
12		self.tags = []
13
14	def addTag(self, tag):
15		"""Add 'tag' to the list of langauge tags if not already there.
16
17		Returns the integer index of 'tag' in the list of all tags.
18		"""
19		try:
20			return self.tags.index(tag)
21		except ValueError:
22			self.tags.append(tag)
23			return len(self.tags) - 1
24
25	def decompile(self, data, ttFont):
26		self.version, self.flags, numTags = struct.unpack(">LLL", data[:12])
27		assert self.version == 1
28		self.tags = []
29		for i in range(numTags):
30			pos = 12 + i * 4
31			offset, length = struct.unpack(">HH", data[pos:pos+4])
32			tag = data[offset:offset+length].decode("ascii")
33			self.tags.append(tag)
34
35	def compile(self, ttFont):
36		dataList = [struct.pack(">LLL", self.version, self.flags, len(self.tags))]
37		stringPool = ""
38		for tag in self.tags:
39			offset = stringPool.find(tag)
40			if offset < 0:
41				offset = len(stringPool)
42				stringPool = stringPool + tag
43			offset = offset + 12 + len(self.tags) * 4
44			dataList.append(struct.pack(">HH", offset, len(tag)))
45		dataList.append(tobytes(stringPool))
46		return bytesjoin(dataList)
47
48	def toXML(self, writer, ttFont):
49		writer.simpletag("version", value=self.version)
50		writer.newline()
51		writer.simpletag("flags", value=self.flags)
52		writer.newline()
53		for tag in self.tags:
54			writer.simpletag("LanguageTag", tag=tag)
55			writer.newline()
56
57	def fromXML(self, name, attrs, content, ttFont):
58		if not hasattr(self, "tags"):
59			self.tags = []
60		if name == "LanguageTag":
61			self.tags.append(attrs["tag"])
62		elif "value" in attrs:
63			value =  safeEval(attrs["value"])
64			setattr(self, name, value)
65