aboutsummaryrefslogtreecommitdiff
path: root/Tests/ttLib/tables/S_V_G__test.py
blob: 4c40556e26879aa51d6efd71c06d3cce5d40d7c2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import gzip
import io
import struct

from fontTools.misc import etree
from fontTools.misc.testTools import getXML, parseXML
from fontTools.ttLib import TTFont
from fontTools.ttLib.tables.S_V_G_ import table_S_V_G_

import pytest


def dump(table, ttFont=None):
    print("\n".join(getXML(table.toXML, ttFont)))


def compress(data: bytes) -> bytes:
    buf = io.BytesIO()
    with gzip.GzipFile(None, "w", fileobj=buf, mtime=0) as gz:
        gz.write(data)
    return buf.getvalue()


def strip_xml_whitespace(xml_string):
    def strip_or_none(text):
        text = text.strip() if text else None
        return text if text else None

    tree = etree.fromstring(xml_string)
    for e in tree.iter("*"):
        e.text = strip_or_none(e.text)
        e.tail = strip_or_none(e.tail)
    return etree.tostring(tree, encoding="utf-8")


SVG_DOCS = [
    strip_xml_whitespace(svg)
    for svg in (
        b"""\
        <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1">
          <defs>
            <rect x="100" y="-200" width="300" height="400" id="p1"/>
          </defs>
          <g id="glyph1">
            <use xlink:href="#p1" fill="#red"/>
          </g>
          <g id="glyph2">
            <use xlink:href="#p1" fill="#blue"/>
          </g>
          <g id="glyph4">
            <use xlink:href="#p1" fill="#green"/>
          </g>
        </svg>""",
        b"""\
        <svg xmlns="http://www.w3.org/2000/svg" version="1.1">
          <g id="glyph3">
            <path d="M0,0 L100,0 L50,100 Z" fill="#red"/>
            <path d="M10,10 L110,10 L60,110 Z" fill="#blue"/>
            <path d="M20,20 L120,20 L70,120 Z" fill="#green"/>
          </g>
        </svg>""",
    )
]


OTSVG_DATA = b"".join(
    [
        # SVG table header
        b"\x00\x00"  # version (0)
        b"\x00\x00\x00\x0a"  # offset to SVGDocumentList (10)
        b"\x00\x00\x00\x00"  # reserved (0)
        #  SVGDocumentList
        b"\x00\x03"  # number of SVGDocumentRecords (3)
        # SVGDocumentRecord[0]
        b"\x00\x01"  # startGlyphID (1)
        b"\x00\x02"  # endGlyphID (2)
        b"\x00\x00\x00\x26"  # svgDocOffset (2 + 12*3 == 38 == 0x26)
        + struct.pack(">L", len(SVG_DOCS[0]))  # svgDocLength
        # SVGDocumentRecord[1] (compressed)
        + b"\x00\x03"  # startGlyphID (3)
        b"\x00\x03"  # endGlyphID (3)
        + struct.pack(">L", 0x26 + len(SVG_DOCS[0]))  # svgDocOffset
        + struct.pack(">L", len(compress(SVG_DOCS[1])))  # svgDocLength
        # SVGDocumentRecord[2]
        + b"\x00\x04"  # startGlyphID (4)
        b"\x00\x04"  # endGlyphID (4)
        b"\x00\x00\x00\x26"  # svgDocOffset (38); records 0 and 2 point to same SVG doc
        + struct.pack(">L", len(SVG_DOCS[0]))  # svgDocLength
    ]
    + [SVG_DOCS[0], compress(SVG_DOCS[1])]
)

OTSVG_TTX = [
    '<svgDoc endGlyphID="2" startGlyphID="1">',
    f"  <![CDATA[{SVG_DOCS[0].decode()}]]>",
    "</svgDoc>",
    '<svgDoc compressed="1" endGlyphID="3" startGlyphID="3">',
    f"  <![CDATA[{SVG_DOCS[1].decode()}]]>",
    "</svgDoc>",
    '<svgDoc endGlyphID="4" startGlyphID="4">',
    f"  <![CDATA[{SVG_DOCS[0].decode()}]]>",
    "</svgDoc>",
]


@pytest.fixture
def font():
    font = TTFont()
    font.setGlyphOrder([".notdef"] + ["glyph%05d" % i for i in range(1, 30)])
    return font


def test_decompile_and_compile(font):
    table = table_S_V_G_()
    table.decompile(OTSVG_DATA, font)
    assert table.compile(font) == OTSVG_DATA


def test_decompile_and_dump_ttx(font):
    table = table_S_V_G_()
    table.decompile(OTSVG_DATA, font)

    dump(table, font)
    assert getXML(table.toXML, font) == OTSVG_TTX


def test_load_from_ttx_and_compile(font):
    table = table_S_V_G_()
    for name, attrs, content in parseXML(OTSVG_TTX):
        table.fromXML(name, attrs, content, font)
    assert table.compile(font) == OTSVG_DATA


def test_round_trip_ttx(font):
    table = table_S_V_G_()
    for name, attrs, content in parseXML(OTSVG_TTX):
        table.fromXML(name, attrs, content, font)
    compiled = table.compile(font)

    table = table_S_V_G_()
    table.decompile(compiled, font)
    assert getXML(table.toXML, font) == OTSVG_TTX


def test_unpack_svg_doc_as_3_tuple():
    # test that the legacy docList as list of 3-tuples interface still works
    # even after the new SVGDocument class with extra `compressed` attribute
    # was added
    table = table_S_V_G_()
    table.decompile(OTSVG_DATA, font)

    for doc, compressed in zip(table.docList, (False, True, False)):
        assert len(doc) == 3
        data, startGID, endGID = doc
        assert doc.data == data
        assert doc.startGlyphID == startGID
        assert doc.endGlyphID == endGID
        assert doc.compressed == compressed