summaryrefslogtreecommitdiffstats
path: root/testfiles/src/svg-affine-test.cpp
blob: 300242d74b40ff5d9f5bb3fb6dd3f7640aa28759 (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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
// SPDX-License-Identifier: GPL-2.0-or-later
/** @file
 * Test for SVG colors
 *//*
 * Authors: see git history
 *
 * Copyright (C) 2010 Authors
 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
 */
#include <cstdlib>
#include <math.h>
#include <gtest/gtest.h>
#include <glib.h>

#include "svg/svg.h"
#include <2geom/affine.h>


struct test_t
{
    char const *str;
    Geom::Affine matrix;
};

static double const DEGREE = M_PI / 180.;

test_t const read_matrix_tests[5] = {{"matrix(0,0,0,0,0,0)", Geom::Affine(0, 0, 0, 0, 0, 0)},
                                     {" matrix(1,2,3,4,5,6)", Geom::Affine(1, 2, 3, 4, 5, 6)},
                                     {"matrix (1 2 -3,-4,5e6,-6e-7)", Geom::Affine(1, 2, -3, -4, 5e6, -6e-7)},
                                     {"matrix(1,2,3,4,5e6-3)", Geom::Affine(1, 2, 3, 4, 5e6, -3)},
                                     {"matrix(1,2,3,4,5e6.3)", Geom::Affine(1, 2, 3, 4, 5e6, 0.3)}};
test_t const read_translate_tests[3] = {{"translate(1)", Geom::Affine(1, 0, 0, 1, 1, 0)},
                                        {"translate(1,1)", Geom::Affine(1, 0, 0, 1, 1, 1)},
                                        {"translate(-1e3 .123e2)", Geom::Affine(1, 0, 0, 1, -1e3, .123e2)}};
test_t const read_scale_tests[3] = {{"scale(2)", Geom::Affine(2, 0, 0, 2, 0, 0)},
                                    {"scale(2,3)", Geom::Affine(2, 0, 0, 3, 0, 0)},
                                    {"scale(0.1e-2 -.475e0)", Geom::Affine(0.1e-2, 0, 0, -.475e0, 0, 0)}};
test_t const read_rotate_tests[4] = {
    {"rotate(13 )", Geom::Affine(cos(13. * DEGREE), sin(13. * DEGREE), -sin(13. * DEGREE), cos(13. * DEGREE), 0, 0)},
    {"rotate(-13)",
     Geom::Affine(cos(-13. * DEGREE), sin(-13. * DEGREE), -sin(-13. * DEGREE), cos(-13. * DEGREE), 0, 0)},
    {"rotate(373)", Geom::Affine(cos(13. * DEGREE), sin(13. * DEGREE), -sin(13. * DEGREE), cos(13. * DEGREE), 0, 0)},
    {"rotate(13,7,11)", Geom::Affine(cos(13. * DEGREE), sin(13. * DEGREE), -sin(13. * DEGREE), cos(13. * DEGREE),
                                     (1 - cos(13. * DEGREE)) * 7 + sin(13. * DEGREE) * 11,
                                     (1 - cos(13. * DEGREE)) * 11 - sin(13. * DEGREE) * 7)}};
test_t const read_skew_tests[3] = {{"skewX( 30)", Geom::Affine(1, 0, tan(30. * DEGREE), 1, 0, 0)},
                                   {"skewX(-30)", Geom::Affine(1, 0, tan(-30. * DEGREE), 1, 0, 0)},
                                   {"skewY(390)", Geom::Affine(1, tan(30. * DEGREE), 0, 1, 0, 0)}};
char const *const read_fail_tests[25] = {
    "matrix((1,2,3,4,5,6)",
    "matrix((1,2,3,4,5,6))",
    "matrix(1,2,3,4,5,6))",
    "matrix(,1,2,3,4,5,6)",
    "matrix(1,2,3,4,5,6,)",
    "matrix(1,2,3,4,5,)",
    "matrix(1,2,3,4,5)",
    "translate()",
    "translate(,)",
    "translate(1,)",
    "translate(1,6,)",
    "translate(1,6,0)",
    "scale()",
    "scale(1,6,2)",
    "rotate()",
    "rotate(1,6)",
    "rotate(1,6,)",
    "rotate(1,6,3,4)",
    "skewX()",
    "skewX(-)",
    "skewX(.)",
    "skewY(,)",
    "skewY(1,2)"};
test_t const write_matrix_tests[2] = {
    {"matrix(1,2,3,4,5,6)", Geom::Affine(1, 2, 3, 4, 5, 6)},
    {"matrix(-1,2123,3,0.4,1e-8,1e20)", Geom::Affine(-1, 2.123e3, 3 + 1e-14, 0.4, 1e-8, 1e20)}};
test_t const write_translate_tests[3] = {{"translate(1,1)", Geom::Affine(1, 0, 0, 1, 1, 1)},
                                         {"translate(1)", Geom::Affine(1, 0, 0, 1, 1, 0)},
                                         {"translate(-1345,0.123)", Geom::Affine(1, 0, 0, 1, -1.345e3, .123)}};
test_t const write_scale_tests[3] = {{"scale(0)", Geom::Affine(0, 0, 0, 0, 0, 0)},
                                     {"scale(7)", Geom::Affine(7, 0, 0, 7, 0, 0)},
                                     {"scale(2,3)", Geom::Affine(2, 0, 0, 3, 0, 0)}};
test_t const write_rotate_tests[3] = {
    {"rotate(13)", Geom::Affine(cos(13. * DEGREE), sin(13. * DEGREE), -sin(13. * DEGREE), cos(13. * DEGREE), 0, 0)},
    {"rotate(-13,7,11)", Geom::Affine(cos(-13. * DEGREE), sin(-13. * DEGREE), -sin(-13. * DEGREE), cos(-13. * DEGREE),
                                      (1 - cos(-13. * DEGREE)) * 7 + sin(-13. * DEGREE) * 11,
                                      (1 - cos(-13. * DEGREE)) * 11 - sin(-13. * DEGREE) * 7)},
    {"rotate(-34.5,6.7,89)",
     Geom::Affine(cos(-34.5 * DEGREE), sin(-34.5 * DEGREE), -sin(-34.5 * DEGREE), cos(-34.5 * DEGREE),
                  (1 - cos(-34.5 * DEGREE)) * 6.7 + sin(-34.5 * DEGREE) * 89,
                  (1 - cos(-34.5 * DEGREE)) * 89 - sin(-34.5 * DEGREE) * 6.7)}};
test_t const write_skew_tests[3] = {{"skewX(30)", Geom::Affine(1, 0, tan(30. * DEGREE), 1, 0, 0)},
                                    {"skewX(-30)", Geom::Affine(1, 0, tan(-30. * DEGREE), 1, 0, 0)},
                                    {"skewY(30)", Geom::Affine(1, tan(30. * DEGREE), 0, 1, 0, 0)}};

bool approx_equal_pred(Geom::Affine const &ref, Geom::Affine const &cm)
{
    double maxabsdiff = 0;
    for (size_t i = 0; i < 6; i++) {
        maxabsdiff = std::max(std::abs(ref[i] - cm[i]), maxabsdiff);
    }
    return maxabsdiff < 1e-14;
}

TEST(SvgAffineTest, testReadIdentity)
{
    char const *strs[] = {// 0,
                          " ", "",        "matrix(1,0,0,1,0,0)", "translate(0,0)", "scale(1,1)", "rotate(0,0,0)", "skewX(0)",
                          "skewY(0)"};
    size_t n = G_N_ELEMENTS(strs);
    for (size_t i = 0; i < n; i++) {
        Geom::Affine cm;
        EXPECT_TRUE(sp_svg_transform_read(strs[i], &cm)) << i;
        ASSERT_EQ(Geom::identity(), cm) << strs[i];
    }
}

TEST(SvgAffineTest, testWriteIdentity)
{
    auto str = sp_svg_transform_write(Geom::identity());
    ASSERT_TRUE(str == "");
}

TEST(SvgAffineTest, testReadMatrix)
{
    for (size_t i = 0; i < G_N_ELEMENTS(read_matrix_tests); i++) {
        Geom::Affine cm;
        ASSERT_TRUE(sp_svg_transform_read(read_matrix_tests[i].str, &cm)) << read_matrix_tests[i].str;
        ASSERT_TRUE(approx_equal_pred(read_matrix_tests[i].matrix, cm)) << read_matrix_tests[i].str;
    }
}

TEST(SvgAffineTest, testReadTranslate)
{
    for (size_t i = 0; i < G_N_ELEMENTS(read_translate_tests); i++) {
        Geom::Affine cm;
        ASSERT_TRUE(sp_svg_transform_read(read_translate_tests[i].str, &cm)) << read_translate_tests[i].str;
        ASSERT_TRUE(approx_equal_pred(read_translate_tests[i].matrix, cm)) << read_translate_tests[i].str;
    }
}

TEST(SvgAffineTest, testReadScale)
{
    for (size_t i = 0; i < G_N_ELEMENTS(read_scale_tests); i++) {
        Geom::Affine cm;
        ASSERT_TRUE(sp_svg_transform_read(read_scale_tests[i].str, &cm)) << read_scale_tests[i].str;
        ASSERT_TRUE(approx_equal_pred(read_scale_tests[i].matrix, cm)) << read_scale_tests[i].str;
    }
}

TEST(SvgAffineTest, testReadRotate)
{
    for (size_t i = 0; i < G_N_ELEMENTS(read_rotate_tests); i++) {
        Geom::Affine cm;
        ASSERT_TRUE(sp_svg_transform_read(read_rotate_tests[i].str, &cm)) << read_rotate_tests[i].str;
        ASSERT_TRUE(approx_equal_pred(read_rotate_tests[i].matrix, cm)) << read_rotate_tests[i].str;
    }
}

TEST(SvgAffineTest, testReadSkew)
{
    for (size_t i = 0; i < G_N_ELEMENTS(read_skew_tests); i++) {
        Geom::Affine cm;
        ASSERT_TRUE(sp_svg_transform_read(read_skew_tests[i].str, &cm)) << read_skew_tests[i].str;
        ASSERT_TRUE(approx_equal_pred(read_skew_tests[i].matrix, cm)) << read_skew_tests[i].str;
    }
}

TEST(SvgAffineTest, testWriteMatrix)
{
    for (size_t i = 0; i < G_N_ELEMENTS(write_matrix_tests); i++) {
        auto str = sp_svg_transform_write(write_matrix_tests[i].matrix);
        ASSERT_TRUE(!strcmp(str.c_str(), write_matrix_tests[i].str));
    }
}

TEST(SvgAffineTest, testWriteTranslate)
{
    for (size_t i = 0; i < G_N_ELEMENTS(write_translate_tests); i++) {
        auto str = sp_svg_transform_write(write_translate_tests[i].matrix);
        ASSERT_TRUE(!strcmp(str.c_str(), write_translate_tests[i].str));
    }
}

TEST(SvgAffineTest, testWriteScale)
{
    for (size_t i = 0; i < G_N_ELEMENTS(write_scale_tests); i++) {
        auto str = sp_svg_transform_write(write_scale_tests[i].matrix);
        ASSERT_TRUE(!strcmp(str.c_str(), write_scale_tests[i].str));
    }
}

TEST(SvgAffineTest, testWriteRotate)
{
    for (size_t i = 0; i < G_N_ELEMENTS(write_rotate_tests); i++) {
        auto str = sp_svg_transform_write(write_rotate_tests[i].matrix);
        ASSERT_TRUE(!strcmp(str.c_str(), write_rotate_tests[i].str));
    }
}

TEST(SvgAffineTest, testWriteSkew)
{
    for (size_t i = 0; i < G_N_ELEMENTS(write_skew_tests); i++) {
        auto str = sp_svg_transform_write(write_skew_tests[i].matrix);
        ASSERT_TRUE(!strcmp(str.c_str(), write_skew_tests[i].str));
    }
}

TEST(SvgAffineTest, testReadConcatenation)
{
    char const *str = "skewY(17)skewX(9)translate(7,13)scale(2)rotate(13)translate(3,5)";
    Geom::Affine ref(2.0199976232558053, 1.0674773585906016, -0.14125199392774669, 1.9055550612095459,
                     14.412730624347654, 28.499820929377454); // Precomputed using Mathematica
    Geom::Affine cm;
    ASSERT_TRUE(sp_svg_transform_read(str, &cm));
    ASSERT_TRUE(approx_equal_pred(ref, cm));
}

TEST(SvgAffineTest, testReadFailures)
{
    for (size_t i = 0; i < G_N_ELEMENTS(read_fail_tests); i++) {
        Geom::Affine cm;
        EXPECT_FALSE(sp_svg_transform_read(read_fail_tests[i], &cm)) << read_fail_tests[i];
    }
}

// vim: filetype=cpp:expandtab:shiftwidth=4:softtabstop=4:fileencoding=utf-8:textwidth=99 :