2017-04-11 10:01:47 +01:00
|
|
|
""" TSI{0,1,2,3,5} are private tables used by Microsoft Visual TrueType (VTT)
|
|
|
|
tool to store its hinting source data.
|
|
|
|
|
|
|
|
TSI1 contains the text of the glyph programs in the form of low-level assembly
|
|
|
|
code, as well as the 'extra' programs 'fpgm', 'ppgm' (i.e. 'prep'), and 'cvt'.
|
|
|
|
"""
|
2014-01-14 15:07:50 +08:00
|
|
|
from __future__ import print_function, division, absolute_import
|
2013-11-27 15:16:28 -05:00
|
|
|
from fontTools.misc.py23 import *
|
2013-11-27 17:27:45 -05:00
|
|
|
from . import DefaultTable
|
2017-04-11 18:40:52 +01:00
|
|
|
from fontTools.misc.loggingTools import LogMixin
|
2017-04-04 12:48:56 +01:00
|
|
|
|
1999-12-16 21:34:53 +00:00
|
|
|
|
2017-04-11 18:40:52 +01:00
|
|
|
class table_T_S_I__1(LogMixin, DefaultTable.DefaultTable):
|
2015-04-26 02:01:01 -04:00
|
|
|
|
1999-12-16 21:34:53 +00:00
|
|
|
extras = {0xfffa: "ppgm", 0xfffb: "cvt", 0xfffc: "reserved", 0xfffd: "fpgm"}
|
2015-04-26 02:01:01 -04:00
|
|
|
|
1999-12-16 21:34:53 +00:00
|
|
|
indextable = "TSI0"
|
2015-04-26 02:01:01 -04:00
|
|
|
|
1999-12-16 21:34:53 +00:00
|
|
|
def decompile(self, data, ttFont):
|
2017-04-04 12:48:56 +01:00
|
|
|
totalLength = len(data)
|
1999-12-16 21:34:53 +00:00
|
|
|
indextable = ttFont[self.indextable]
|
2017-04-04 12:48:56 +01:00
|
|
|
for indices, isExtra in zip(
|
|
|
|
(indextable.indices, indextable.extra_indices), (False, True)):
|
|
|
|
programs = {}
|
|
|
|
for i, (glyphID, textLength, textOffset) in enumerate(indices):
|
|
|
|
if isExtra:
|
|
|
|
name = self.extras[glyphID]
|
1999-12-16 21:34:53 +00:00
|
|
|
else:
|
2017-04-04 12:48:56 +01:00
|
|
|
name = ttFont.getGlyphName(glyphID)
|
|
|
|
if textOffset > totalLength:
|
|
|
|
self.log.warning("textOffset > totalLength; %r skipped" % name)
|
|
|
|
continue
|
|
|
|
if textLength < 0x8000:
|
|
|
|
# If the length stored in the record is less than 32768, then use
|
|
|
|
# that as the length of the record.
|
|
|
|
pass
|
|
|
|
elif textLength == 0x8000:
|
|
|
|
# If the length is 32768, compute the actual length as follows:
|
|
|
|
isLast = i == (len(indices)-1)
|
|
|
|
if isLast:
|
|
|
|
if isExtra:
|
|
|
|
# For the last "extra" record (the very last record of the
|
|
|
|
# table), the length is the difference between the total
|
|
|
|
# length of the TSI1 table and the textOffset of the final
|
|
|
|
# record.
|
|
|
|
nextTextOffset = totalLength
|
|
|
|
else:
|
|
|
|
# For the last "normal" record (the last record just prior
|
|
|
|
# to the record containing the "magic number"), the length
|
|
|
|
# is the difference between the textOffset of the record
|
|
|
|
# following the "magic number" (0xFFFE) record (i.e. the
|
|
|
|
# first "extra" record), and the textOffset of the last
|
|
|
|
# "normal" record.
|
|
|
|
nextTextOffset = indextable.extra_indices[0][2]
|
|
|
|
else:
|
|
|
|
# For all other records with a length of 0x8000, the length is
|
|
|
|
# the difference between the textOffset of the record in
|
|
|
|
# question and the textOffset of the next record.
|
|
|
|
nextTextOffset = indices[i+1][2]
|
|
|
|
assert nextTextOffset >= textOffset, "entries not sorted by offset"
|
|
|
|
if nextTextOffset > totalLength:
|
|
|
|
self.log.warning(
|
|
|
|
"nextTextOffset > totalLength; %r truncated" % name)
|
|
|
|
nextTextOffset = totalLength
|
|
|
|
textLength = nextTextOffset - textOffset
|
|
|
|
else:
|
|
|
|
from fontTools import ttLib
|
|
|
|
raise ttLib.TTLibError(
|
|
|
|
"%r textLength (%d) must not be > 32768" % (name, textLength))
|
|
|
|
text = data[textOffset:textOffset+textLength]
|
|
|
|
assert len(text) == textLength
|
|
|
|
if text:
|
|
|
|
programs[name] = text
|
|
|
|
if isExtra:
|
|
|
|
self.extraPrograms = programs
|
|
|
|
else:
|
|
|
|
self.glyphPrograms = programs
|
2015-04-26 02:01:01 -04:00
|
|
|
|
1999-12-16 21:34:53 +00:00
|
|
|
def compile(self, ttFont):
|
2000-01-04 14:02:05 +00:00
|
|
|
if not hasattr(self, "glyphPrograms"):
|
|
|
|
self.glyphPrograms = {}
|
|
|
|
self.extraPrograms = {}
|
2013-11-28 06:43:47 -05:00
|
|
|
data = b''
|
1999-12-16 21:34:53 +00:00
|
|
|
indextable = ttFont[self.indextable]
|
|
|
|
glyphNames = ttFont.getGlyphOrder()
|
2015-04-26 02:01:01 -04:00
|
|
|
|
1999-12-16 21:34:53 +00:00
|
|
|
indices = []
|
|
|
|
for i in range(len(glyphNames)):
|
|
|
|
if len(data) % 2:
|
2013-11-28 06:43:47 -05:00
|
|
|
data = data + b"\015" # align on 2-byte boundaries, fill with return chars. Yum.
|
1999-12-16 21:34:53 +00:00
|
|
|
name = glyphNames[i]
|
2013-11-27 02:33:03 -05:00
|
|
|
if name in self.glyphPrograms:
|
2014-12-02 21:58:45 +00:00
|
|
|
text = tobytes(self.glyphPrograms[name])
|
1999-12-16 21:34:53 +00:00
|
|
|
else:
|
2013-11-28 06:43:47 -05:00
|
|
|
text = b""
|
1999-12-16 21:34:53 +00:00
|
|
|
textLength = len(text)
|
|
|
|
if textLength >= 0x8000:
|
2017-04-04 12:48:56 +01:00
|
|
|
textLength = 0x8000
|
1999-12-16 21:34:53 +00:00
|
|
|
indices.append((i, textLength, len(data)))
|
|
|
|
data = data + text
|
2015-04-26 02:01:01 -04:00
|
|
|
|
1999-12-16 21:34:53 +00:00
|
|
|
extra_indices = []
|
2013-11-27 04:15:34 -05:00
|
|
|
codes = sorted(self.extras.items())
|
1999-12-16 21:34:53 +00:00
|
|
|
for i in range(len(codes)):
|
|
|
|
if len(data) % 2:
|
2013-11-28 06:43:47 -05:00
|
|
|
data = data + b"\015" # align on 2-byte boundaries, fill with return chars.
|
1999-12-16 21:34:53 +00:00
|
|
|
code, name = codes[i]
|
2013-11-27 02:33:03 -05:00
|
|
|
if name in self.extraPrograms:
|
2016-09-14 17:21:24 +01:00
|
|
|
text = tobytes(self.extraPrograms[name], encoding="utf-8")
|
1999-12-16 21:34:53 +00:00
|
|
|
else:
|
2013-11-28 06:43:47 -05:00
|
|
|
text = b""
|
1999-12-16 21:34:53 +00:00
|
|
|
textLength = len(text)
|
|
|
|
if textLength >= 0x8000:
|
2017-04-04 12:48:56 +01:00
|
|
|
textLength = 0x8000
|
1999-12-16 21:34:53 +00:00
|
|
|
extra_indices.append((code, textLength, len(data)))
|
|
|
|
data = data + text
|
|
|
|
indextable.set(indices, extra_indices)
|
|
|
|
return data
|
2015-04-26 02:01:01 -04:00
|
|
|
|
1999-12-16 21:34:53 +00:00
|
|
|
def toXML(self, writer, ttFont):
|
2013-11-27 04:15:34 -05:00
|
|
|
names = sorted(self.glyphPrograms.keys())
|
1999-12-16 21:34:53 +00:00
|
|
|
writer.newline()
|
|
|
|
for name in names:
|
|
|
|
text = self.glyphPrograms[name]
|
|
|
|
if not text:
|
|
|
|
continue
|
|
|
|
writer.begintag("glyphProgram", name=name)
|
|
|
|
writer.newline()
|
2014-12-02 21:38:43 +00:00
|
|
|
writer.write_noindent(text.replace(b"\r", b"\n"))
|
1999-12-16 21:34:53 +00:00
|
|
|
writer.newline()
|
|
|
|
writer.endtag("glyphProgram")
|
|
|
|
writer.newline()
|
|
|
|
writer.newline()
|
2013-11-29 21:51:15 +01:00
|
|
|
extra_names = sorted(self.extraPrograms.keys())
|
1999-12-16 21:34:53 +00:00
|
|
|
for name in extra_names:
|
|
|
|
text = self.extraPrograms[name]
|
|
|
|
if not text:
|
|
|
|
continue
|
|
|
|
writer.begintag("extraProgram", name=name)
|
|
|
|
writer.newline()
|
2014-12-02 21:38:43 +00:00
|
|
|
writer.write_noindent(text.replace(b"\r", b"\n"))
|
1999-12-16 21:34:53 +00:00
|
|
|
writer.newline()
|
|
|
|
writer.endtag("extraProgram")
|
|
|
|
writer.newline()
|
|
|
|
writer.newline()
|
2015-04-26 02:01:01 -04:00
|
|
|
|
2013-11-27 03:19:32 -05:00
|
|
|
def fromXML(self, name, attrs, content, ttFont):
|
1999-12-16 21:34:53 +00:00
|
|
|
if not hasattr(self, "glyphPrograms"):
|
|
|
|
self.glyphPrograms = {}
|
|
|
|
self.extraPrograms = {}
|
2013-11-27 21:17:35 -05:00
|
|
|
lines = strjoin(content).replace("\r", "\n").split("\n")
|
2013-11-27 05:47:34 -05:00
|
|
|
text = '\r'.join(lines[1:-1])
|
1999-12-16 21:34:53 +00:00
|
|
|
if name == "glyphProgram":
|
|
|
|
self.glyphPrograms[attrs["name"]] = text
|
|
|
|
elif name == "extraProgram":
|
|
|
|
self.extraPrograms[attrs["name"]] = text
|