5
* Lauris Kaplinski <lauris@kaplinski.com>
6
* Raph Levien <raph@acm.org>
8
* Copyright (C) 1999-2002 Lauris Kaplinski
9
* Copyright (C) 1999 Raph Levien
11
* Released under GNU GPL, read the file 'COPYING' for more information
22
#include <glib/gstrfuncs.h>
23
#include <libnr/nr-matrix-fns.h>
24
#include <libnr/nr-matrix-ops.h>
25
#include <2geom/transforms.h>
26
#include <2geom/angle.h>
27
#include <libnr/nr-convert2geom.h>
29
#include "preferences.h"
32
# define M_PI 3.14159265358979323846
36
sp_svg_transform_read(gchar const *str, Geom::Matrix *transform)
44
if (str == NULL) return false;
46
Geom::Matrix a(Geom::identity());
50
/* skip initial whitespace */
51
while (g_ascii_isspace (str[idx])) idx++;
54
for (key_len = 0; key_len < sizeof (keyword); key_len++) {
58
if (g_ascii_isalpha (c) || c == '-') {
59
keyword[key_len] = str[idx++];
64
if (key_len >= sizeof (keyword)) return false;
65
keyword[key_len] = '\0';
68
while (g_ascii_isspace (str[idx])) idx++;
70
if (str[idx] != '(') return false;
73
for (n_args = 0; ; n_args++) {
78
while (g_ascii_isspace (str[idx])) idx++;
80
if (g_ascii_isdigit (c) || c == '+' || c == '-' || c == '.') {
81
if (n_args == sizeof (args) / sizeof (args[0])) return false; /* Too many args */
82
args[n_args] = g_ascii_strtod (str + idx, &end_ptr);
84
//printf("took %d chars from '%s' to make %f\n",
89
idx = end_ptr - (char *) str;
91
while (g_ascii_isspace (str[idx])) idx++;
93
/* skip optional comma */
94
if (str[idx] == ',') idx++;
95
} else if (c == ')') {
103
/* ok, have parsed keyword and args, now modify the transform */
104
if (!strcmp (keyword, "matrix")) {
105
if (n_args != 6) return false;
106
a = (*((Geom::Matrix *) &(args)[0])) * a;
107
} else if (!strcmp (keyword, "translate")) {
110
} else if (n_args != 2) {
113
a = Geom::Translate(args[0], args[1]) * a;
114
} else if (!strcmp (keyword, "scale")) {
117
} else if (n_args != 2) {
120
a = Geom::Scale(args[0], args[1]) * a;
121
} else if (!strcmp (keyword, "rotate")) {
122
if (n_args != 1 && n_args != 3) {
125
Geom::Rotate const rot(Geom::deg_to_rad(args[0]));
127
a = ( Geom::Translate(-args[1], -args[2])
129
* Geom::Translate(args[1], args[2])
134
} else if (!strcmp (keyword, "skewX")) {
135
if (n_args != 1) return false;
136
a = ( Geom::Matrix(1, 0,
137
tan(args[0] * M_PI / 180.0), 1,
140
} else if (!strcmp (keyword, "skewY")) {
141
if (n_args != 1) return false;
142
a = ( Geom::Matrix(1, tan(args[0] * M_PI / 180.0),
147
return false; /* unknown keyword */
149
/* Skip trailing whitespace */
150
while (g_ascii_isspace (str[idx])) idx++;
157
#define EQ(a,b) (fabs ((a) - (b)) < 1e-9)
160
sp_svg_transform_write(Geom::Matrix const &transform)
162
Inkscape::Preferences *prefs = Inkscape::Preferences::get();
164
double e = 0.000001 * transform.descrim();
165
int prec = prefs->getInt("/options/svgoutput/numericprecision", 8);
166
int min_exp = prefs->getInt("/options/svgoutput/minimumexponent", -8);
168
/* fixme: We could use t1 * t1 + t2 * t2 here instead */
169
if ( Geom::are_near(transform[1], 0.0, e) && Geom::are_near (transform[2], 0.0, e)) {
170
if (Geom::are_near (transform[4], 0.0, e) && Geom::are_near (transform[5], 0.0, e)) {
171
if (Geom::are_near (transform[0], 1.0, e) && Geom::are_near (transform[3], 1.0, e)) {
172
/* We are more or less identity */
175
/* We are more or less scale */
178
strcpy (c + p, "scale(");
180
p += sp_svg_number_write_de (c + p, transform[0], prec, min_exp);
182
p += sp_svg_number_write_de (c + p, transform[3], prec, min_exp);
185
g_assert( p <= sizeof(c) );
189
if (Geom::are_near (transform[0], 1.0, e) && Geom::are_near (transform[3], 1.0, e)) {
190
/* We are more or less translate */
193
strcpy (c + p, "translate(");
195
p += sp_svg_number_write_de (c + p, transform[4], prec, min_exp);
197
p += sp_svg_number_write_de (c + p, transform[5], prec, min_exp);
200
g_assert( p <= sizeof(c) );
205
strcpy (c + p, "matrix(");
207
p += sp_svg_number_write_de (c + p, transform[0], prec, min_exp);
209
p += sp_svg_number_write_de (c + p, transform[1], prec, min_exp);
211
p += sp_svg_number_write_de (c + p, transform[2], prec, min_exp);
213
p += sp_svg_number_write_de (c + p, transform[3], prec, min_exp);
215
p += sp_svg_number_write_de (c + p, transform[4], prec, min_exp);
217
p += sp_svg_number_write_de (c + p, transform[5], prec, min_exp);
220
g_assert( p <= sizeof(c) );
227
strcpy (c + p, "matrix(");
229
p += sp_svg_number_write_de (c + p, transform[0], prec, min_exp);
231
p += sp_svg_number_write_de (c + p, transform[1], prec, min_exp);
233
p += sp_svg_number_write_de (c + p, transform[2], prec, min_exp);
235
p += sp_svg_number_write_de (c + p, transform[3], prec, min_exp);
237
p += sp_svg_number_write_de (c + p, transform[4], prec, min_exp);
239
p += sp_svg_number_write_de (c + p, transform[5], prec, min_exp);
242
g_assert( p <= sizeof(c) );
249
sp_svg_transform_write(Geom::Matrix const *transform)
251
return sp_svg_transform_write(*transform);
257
c-file-style:"stroustrup"
258
c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
263
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :