summaryrefslogtreecommitdiffstats
path: root/src/svg/svg-affine.cpp
blob: e5fcce69e9cbce0eb5817d4ab321a0103fe4e943 (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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * SVG data parser
 *
 * Authors:
 *   Lauris Kaplinski <lauris@kaplinski.com>
 *   Raph Levien <raph@acm.org>
 *
 * Copyright (C) 1999-2002 Lauris Kaplinski
 * Copyright (C) 1999 Raph Levien
 *
 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
 */

#include <cstring>
#include <string>
#include <cstdlib>
#include <cstdio>
#include <glib.h>
#include <2geom/transforms.h>
#include "svg.h"
#include "preferences.h"

bool
sp_svg_transform_read(gchar const *str, Geom::Affine *transform)
{
    int idx;
    char keyword[32];
    double args[6];
    int n_args;
    size_t key_len;

    if (str == nullptr) return false;

    Geom::Affine a(Geom::identity());

    idx = 0;
    while (str[idx]) {
        /* skip initial whitespace */
        while (g_ascii_isspace (str[idx])) idx++;

        // SVG2: allow commas in separation of transforms
        if (str[idx] == ',') {
            ++idx;
            while (g_ascii_isspace(str[idx]))
                ++idx;
        }

        /* parse keyword */
        for (key_len = 0; key_len < sizeof (keyword); key_len++) {
            char c;

            c = str[idx];
            if (g_ascii_isalpha (c) || c == '-') {
                keyword[key_len] = str[idx++];
            } else {
                break;
            }
        }
        if (key_len >= sizeof (keyword)) return false;
        keyword[key_len] = '\0';

        /* skip whitespace */
        while (g_ascii_isspace (str[idx])) idx++;

        if (str[idx] != '(') return false;
        idx++;

        for (n_args = 0; n_args < 6; n_args++) {
            char c;
            char *end_ptr;

            /* skip whitespace */
            while (g_ascii_isspace (str[idx])) idx++;
            c = str[idx];
            if (g_ascii_isdigit (c) || c == '+' || c == '-' || c == '.') {
                if (n_args == sizeof (args) / sizeof (args[0])) return false; /* Too many args */
                args[n_args] = g_ascii_strtod (str + idx, &end_ptr);
                
                //printf("took %d chars from '%s' to make %f\n",
                //		end_ptr-(str+idx),
                //		str+idx,
                //		args[n_args]);

                idx = end_ptr - (char *) str;

                while (g_ascii_isspace (str[idx])) idx++;

                /* skip optional comma */
                if (str[idx] == ',') idx++;
            } else if (c == ')') {
                break;
            } else {
                return false;
            }
        }
        idx++;

        /* ok, have parsed keyword and args, now modify the transform */
        if (!strcmp (keyword, "matrix")) {
            if (n_args != 6) return false;
            a = (*((Geom::Affine *) &(args)[0])) * a;
        } else if (!strcmp (keyword, "translate")) {
            if (n_args == 1) {
                args[1] = 0;
            } else if (n_args != 2) {
                return false;
            }
            a = Geom::Translate(args[0], args[1]) * a;
        } else if (!strcmp (keyword, "scale")) {
            if (n_args == 1) {
                args[1] = args[0];
            } else if (n_args != 2) {
                return false;
            }
            a = Geom::Scale(args[0], args[1]) * a;
        } else if (!strcmp (keyword, "rotate")) {
            if (n_args != 1 && n_args != 3) {
                return false;
            }
            Geom::Rotate const rot(Geom::rad_from_deg(args[0]));
            if (n_args == 3) {
                a = ( Geom::Translate(-args[1], -args[2])
                      * rot
                      * Geom::Translate(args[1], args[2])
                      * Geom::Affine(a) );
            } else {
                a = rot * a;
            }
        } else if (!strcmp (keyword, "skewX")) {
            if (n_args != 1) return false;
            a = ( Geom::Affine(1, 0,
                     tan(args[0] * M_PI / 180.0), 1,
                     0, 0)
                  * a );
        } else if (!strcmp (keyword, "skewY")) {
            if (n_args != 1) return false;
            a = ( Geom::Affine(1, tan(args[0] * M_PI / 180.0),
                     0, 1,
                     0, 0)
                  * a );
        } else {
            return false; /* unknown keyword */
        }
        /* Skip trailing whitespace */
             while (g_ascii_isspace (str[idx])) idx++;
    }

    *transform = a;
    return true;
}

#define EQ(a,b) (fabs ((a) - (b)) < 1e-9)

gchar *
sp_svg_transform_write(Geom::Affine const &transform)
{
    Inkscape::Preferences *prefs = Inkscape::Preferences::get();

    // this must be a bit grater than EPSILON
    double e = 1e-5 * transform.descrim();
    int prec = prefs->getInt("/options/svgoutput/numericprecision", 8);
    int min_exp = prefs->getInt("/options/svgoutput/minimumexponent", -8);

    // Special case: when all fields of the affine are zero,
    // the optimized transformation is scale(0)
    if (transform[0] == 0 && transform[1] == 0 && transform[2] == 0 &&
        transform[3] == 0 && transform[4] == 0 && transform[5] == 0)
    {
        return g_strdup("scale(0)");
    }

    // FIXME legacy C code!
    // the function sp_svg_number_write_de is stopping me from using a proper C++ string

    gchar c[256]; // string buffer
    unsigned p = 0; // position in the buffer

    if (transform.isIdentity()) {
        // We are more or less identity, so no transform attribute needed:
        return nullptr;
    } else if (transform.isScale()) {
        // We are more or less a uniform scale
        strcpy (c + p, "scale(");
        p += 6;
        p += sp_svg_number_write_de( c + p, sizeof(c) - p, transform[0], prec, min_exp );
        if (Geom::are_near(transform[0], transform[3], e)) {
            c[p++] = ')';
            c[p] = '\000';
        } else {
            c[p++] = ',';
            p += sp_svg_number_write_de( c + p, sizeof(c) - p, transform[3], prec, min_exp );
            c[p++] = ')';
            c[p] = '\000';
        }
    } else if (transform.isTranslation()) {
        // We are more or less a pure translation
        strcpy (c + p, "translate(");
        p += 10;
        p += sp_svg_number_write_de( c + p, sizeof(c) - p, transform[4], prec, min_exp );
        if (Geom::are_near(transform[5], 0.0, e)) {
            c[p++] = ')';
            c[p] = '\000';
        } else {
            c[p++] = ',';
            p += sp_svg_number_write_de( c + p, sizeof(c) - p, transform[5], prec, min_exp );
            c[p++] = ')';
            c[p] = '\000';
        }
    } else if (transform.isRotation()) {
        // We are more or less a pure rotation
        strcpy(c + p, "rotate(");
        p += 7;

        double angle = std::atan2(transform[1], transform[0]) * (180 / M_PI);
        p += sp_svg_number_write_de(c + p, sizeof(c) - p, angle, prec, min_exp);

        c[p++] = ')';
        c[p] = '\000';
    } else if (transform.withoutTranslation().isRotation()) {
        // Solution found by Johan Engelen
        // Refer to the matrix in svg-affine-test.h

        // We are a rotation about a special axis
        strcpy(c + p, "rotate(");
        p += 7;

        double angle = std::atan2(transform[1], transform[0]) * (180 / M_PI);
        p += sp_svg_number_write_de(c + p, sizeof(c) - p, angle, prec, min_exp);
        c[p++] = ',';

        Geom::Affine const& m = transform;
        double tx = (m[2]*m[5]+m[4]-m[4]*m[3]) / (1-m[3]-m[0]+m[0]*m[3]-m[2]*m[1]);
        p += sp_svg_number_write_de(c + p, sizeof(c) - p, tx, prec, min_exp);

        c[p++] = ',';

        double ty = (m[1]*tx + m[5]) / (1 - m[3]);
        p += sp_svg_number_write_de(c + p, sizeof(c) - p, ty, prec, min_exp);

        c[p++] = ')';
        c[p] = '\000';
    } else if (transform.isHShear()) {
        // We are more or less a pure skewX
        strcpy(c + p, "skewX(");
        p += 6;

        double angle = atan(transform[2]) * (180 / M_PI);
        p += sp_svg_number_write_de(c + p, sizeof(c) - p, angle, prec, min_exp);

        c[p++] = ')';
        c[p] = '\000';
    } else if (transform.isVShear()) {
        // We are more or less a pure skewY
        strcpy(c + p, "skewY(");
        p += 6;

        double angle = atan(transform[1]) * (180 / M_PI);
        p += sp_svg_number_write_de(c + p, sizeof(c) - p, angle, prec, min_exp);

        c[p++] = ')';
        c[p] = '\000';
    } else {
        strcpy (c + p, "matrix(");
        p += 7;
        p += sp_svg_number_write_de( c + p, sizeof(c) - p, transform[0], prec, min_exp );
        c[p++] = ',';
        p += sp_svg_number_write_de( c + p, sizeof(c) - p, transform[1], prec, min_exp );
        c[p++] = ',';
        p += sp_svg_number_write_de( c + p, sizeof(c) - p, transform[2], prec, min_exp );
        c[p++] = ',';
        p += sp_svg_number_write_de( c + p, sizeof(c) - p, transform[3], prec, min_exp );
        c[p++] = ',';
        p += sp_svg_number_write_de( c + p, sizeof(c) - p, transform[4], prec, min_exp );
        c[p++] = ',';
        p += sp_svg_number_write_de( c + p, sizeof(c) - p, transform[5], prec, min_exp );
        c[p++] = ')';
        c[p] = '\000';
    }

    assert(p <= sizeof(c));
    return g_strdup(c);
}


gchar *
sp_svg_transform_write(Geom::Affine const *transform)
{
    return sp_svg_transform_write(*transform);
}

/*
  Local Variables:
  mode:c++
  c-file-style:"stroustrup"
  c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
  indent-tabs-mode:nil
  fill-column:99
  End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :