summaryrefslogtreecommitdiff
path: root/svg/svgtransform.c
blob: 280be4d9627177568d7c852de63c5747b0a61316 (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
/* Copyright (C) 2008 Artifex Software, Inc.
   All Rights Reserved.

   This software is provided AS-IS with no warranty, either express or
   implied.

   This software is distributed under license and may not be copied, modified
   or distributed except as expressly authorized under the terms of that
   license.  Refer to licensing information at http://www.artifex.com/
   or contact Artifex Software, Inc.,  7 Mt. Lassen  Drive - Suite A-134,
   San Rafael, CA  94903, U.S.A., +1(415)492-9861, for further information.
*/

/* SVG interpreter - coordinate transformations */

#include "ghostsvg.h"

int svg_parse_transform(svg_context_t *ctx, char *str)
{
    char keyword[20];
    int keywordlen;
    char number[20];
    int numberlen;
    float args[6];
    int nargs;

    nargs = 0;
    keywordlen = 0;

    while (*str)
    {
        keywordlen = 0;
        nargs = 0;

        /*
         * Parse keyword and opening parenthesis.
         */

        while (svg_is_whitespace(*str))
            str ++;

        if (*str == 0)
            break;

        while (svg_is_alpha(*str) && keywordlen < sizeof(keyword) - 1)
            keyword[keywordlen++] = *str++;
        keyword[keywordlen] = 0;

        if (keywordlen == 0)
            return gs_throw(-1, "syntax error in transform attribute - no keyword");

        while (svg_is_whitespace(*str))
            str ++;

        if (*str != '(')
            return gs_throw(-1, "syntax error in transform attribute - no open paren");
        str ++;

        while (svg_is_whitespace(*str))
            str ++;

        /*
         * Parse list of numbers until closing parenthesis
         */

        while (nargs < 6)
        {
            numberlen = 0;

            while (svg_is_digit(*str) && numberlen < sizeof(number) - 1)
                number[numberlen++] = *str++;
            number[numberlen] = 0;

            args[nargs++] = atof(number);

            while (svg_is_whitespace_or_comma(*str))
                str ++;

            if (*str == ')')
            {
                str ++;
                break;
            }

            if (*str == 0)
                return gs_throw(-1, "syntax error in transform attribute - no close paren");
        }

        while (svg_is_whitespace_or_comma(*str))
            str ++;

        /*
         * Execute the transform.
         */

        if (!strcmp(keyword, "matrix"))
        {
            gs_matrix mtx;

            if (nargs != 6)
                return gs_throw1(-1, "wrong number of arguments to matrix(): %d", nargs);

            mtx.xx = args[0];
            mtx.xy = args[1];
            mtx.yx = args[2];
            mtx.yy = args[3];
            mtx.tx = args[4];
            mtx.ty = args[5];

            gs_concat(ctx->pgs, &mtx);
        }

        else if (!strcmp(keyword, "translate"))
        {
            if (nargs != 2)
                return gs_throw1(-1, "wrong number of arguments to translate(): %d", nargs);
            gs_translate(ctx->pgs, args[0], args[1]);
        }

        else if (!strcmp(keyword, "scale"))
        {
            if (nargs == 1)
                gs_scale(ctx->pgs, args[0], args[0]);
            else if (nargs == 2)
                gs_scale(ctx->pgs, args[0], args[1]);
            else
                return gs_throw1(-1, "wrong number of arguments to scale(): %d", nargs);
        }

        else if (!strcmp(keyword, "rotate"))
        {
            if (nargs != 1)
                return gs_throw1(-1, "wrong number of arguments to rotate(): %d", nargs);
            gs_rotate(ctx->pgs, args[0]);
        }

        else if (!strcmp(keyword, "skewX"))
        {
            gs_matrix mtx;

            if (nargs != 1)
                return gs_throw1(-1, "wrong number of arguments to skewX(): %d", nargs);

            mtx.xx = 1.0;
            mtx.xy = 0.0;
            mtx.yx = tan(args[0] * 0.0174532925);
            mtx.yy = 1.0;
            mtx.tx = 0.0;
            mtx.ty = 0.0;

            gs_concat(ctx->pgs, &mtx);
        }

        else if (!strcmp(keyword, "skewY"))
        {
            gs_matrix mtx;

            if (nargs != 1)
                return gs_throw1(-1, "wrong number of arguments to skewY(): %d", nargs);

            mtx.xx = 1.0;
            mtx.xy = tan(args[0] * 0.0174532925);
            mtx.yx = 0.0;
            mtx.yy = 1.0;
            mtx.tx = 0.0;
            mtx.ty = 0.0;

            gs_concat(ctx->pgs, &mtx);
        }

        else
        {
            return gs_throw1(-1, "unknown transform function: %d", keyword);
        }
    }

    return 0;
}