summaryrefslogtreecommitdiff
path: root/met/metgstate.c
blob: 4b633455c00921a74db4dae9e4cbb4c8707377cd (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
/* Portions Copyright (C) 2001 artofcode LLC.
   Portions Copyright (C) 1996, 2001 Artifex Software Inc.
   Portions Copyright (C) 1988, 2000 Aladdin Enterprises.
   This software is based in part on the work of the Independent JPEG Group.
   All Rights Reserved.

   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., 101 Lucas Valley Road #110,
   San Rafael, CA  94903, (415)492-9861, for further information. */
/*$Id: */

/* metgstate.c */
/* metro graphics state operators */

#include "std.h"
#include "metsimple.h"
#include "metstate.h"
#include "metgstate.h"
#include "metutil.h"
#include "gxstate.h" /* for gs_state_copy_reason_t */
#include "string_.h"
#include "ctype_.h"

typedef struct metgstate_s {
    char *Stroke;
    char *Fill;
    ST_ZeroOne Opacity;
    char *FillRule;
    bool isClosed;
    bool charpathmode;
    char *CharFill;
    met_path_child_t child;
    void *VisualBrush;
    gs_memory_t *pmem;
    met_state_t *ms;
} metgstate_t;

private void *
met_gstate_alloc(gs_memory_t *pmem)
{
    metgstate_t *pmg = (metgstate_t *)gs_alloc_bytes(pmem,
                               sizeof(metgstate_t),
                               "met_gstate_alloc");
    memset(pmg, 0, sizeof(metgstate_t));
    pmg->pmem = pmem;
    return pmg;
}

private int
met_gstate_copy_for(void *pto, void *pfrom, gs_state_copy_reason_t reason)
{
    /* reason is ignored for now */
    metgstate_t *pt = pto;
    metgstate_t *pf = pfrom;

    /* free old prior to deep copy */
    gs_free_object(pf->pmem, pt->Stroke, "met_gstate_free");
    gs_free_object(pf->pmem, pt->Fill, "met_gstate_free");
    gs_free_object(pf->pmem, pt->FillRule, "met_gstate_free");
    gs_free_object(pf->pmem, pt->CharFill, "met_gstate_free");

    /* struct copy */
    *pt = *pf;
    
    /* deep copy */    
    pt->Stroke = met_strdup(pf->pmem, pf->Stroke, "met_gstate_copy_for");
    pt->Fill = met_strdup(pf->pmem, pf->Fill, "met_gstate_copy_for");
    pt->FillRule = met_strdup(pf->pmem, pf->FillRule, "met_gstate_copy_for");
    pt->CharFill = met_strdup(pf->pmem, pf->CharFill, "met_gstate_copy_for");

    return 0;
}

private void
met_gstate_free(void *pold, gs_memory_t *pmem)
{
    metgstate_t *pmetgs = (metgstate_t *)pold;
    gs_free_object(pmem, pmetgs->Stroke, "met_gstate_free");
    gs_free_object(pmem, pmetgs->Fill, "met_gstate_free");
    gs_free_object(pmem, pmetgs->FillRule, "met_gstate_free");
    gs_free_object(pmem, pmetgs->CharFill, "met_gstate_free");
    // TODO free gradientstops
    gs_free_object(pmem, pmetgs, "met_gstate_free");
}

private const ST_RscRefColor met_pattern = "xxx";

ST_RscRefColor
met_currentstrokecolor(gs_state *pgs)
{
    metgstate_t *pmg = gs_state_client_data(pgs);
    return pmg->Stroke;
}
    
void
met_setstrokecolor(gs_state *pgs, const ST_RscRefColor color)
{
    metgstate_t *pmg = gs_state_client_data(pgs);

    gs_free_object(pmg->pmem, pmg->Stroke, "met_setstrokecolor");
    pmg->Stroke = met_strdup(pmg->pmem, color, "met_setstrokecolor");
}

ST_RscRefColor
met_currentfillcolor(gs_state *pgs)
{
    metgstate_t *pmg = gs_state_client_data(pgs);
    return pmg->Fill;
}
    
void
met_setfillcolor(gs_state *pgs, const ST_RscRefColor color)
{
    metgstate_t *pmg = gs_state_client_data(pgs);
    gs_free_object(pmg->pmem, pmg->Fill, "met_setfillcolor");
    pmg->Fill = met_strdup(pmg->pmem, color, "met_setfillcolor");
}

void
met_setpatternstroke(gs_state *pgs)
{
    met_setstrokecolor(pgs, met_pattern);
}

void
met_setpatternfill(gs_state *pgs)
{
    met_setfillcolor(pgs, met_pattern);
}


void
met_setfillrule(gs_state *pgs, const ST_FillRule fill)
{
    metgstate_t *pmg = gs_state_client_data(pgs);
    gs_free_object(pmg->pmem, pmg->FillRule, "met_setfillrulecolor");
    pmg->FillRule = met_strdup(pmg->pmem, fill, "met_setfillrulecolor");
}

ST_FillRule
met_currentfillrule(gs_state *pgs)
{
    metgstate_t *pmg = gs_state_client_data(pgs);
    return pmg->FillRule;
}    

void
met_setopacity(gs_state *pgs, const ST_ZeroOne opacity)
{
    metgstate_t *pmg = gs_state_client_data(pgs);
    pmg->Opacity = opacity;
}

ST_ZeroOne
met_currentopacity(gs_state *pgs)
{
    metgstate_t *pmg = gs_state_client_data(pgs);
    return pmg->Opacity;
}

met_path_t
met_currentpathtype(gs_state *pgs) 
{ 
    if (met_currentstrokecolor(pgs) && met_currentfillcolor(pgs))
        return met_stroke_and_fill;
    if (met_currentfillcolor(pgs))
        return met_fill_only;
    if (met_currentstrokecolor(pgs))
        return met_stroke_only;
    return met_path_undefined;
}

/* if set all paths should be closed */
void 
met_setclosepath(gs_state *pgs, bool close)
{
    metgstate_t *pmg = gs_state_client_data(pgs);
    pmg->isClosed = close;
}

bool met_currentclosepath(gs_state *pgs)
{
    metgstate_t *pmg = gs_state_client_data(pgs);
    return pmg->isClosed;
}   

bool met_currenteofill(gs_state *pgs)
{
    /* if not set even odd */
    if (!met_currentfillrule(pgs))
        return true;

    return (!strcasecmp(met_currentfillrule(pgs), "EvenOdd"));
}

met_path_child_t met_currentpathchild(gs_state *pgs)
{
    metgstate_t *pmg = gs_state_client_data(pgs);
    return pmg->child;
}

void met_setpathchild(gs_state *pgs, met_path_child_t child)
{
    metgstate_t *pmg = gs_state_client_data(pgs);
    pmg->child = child;
}

bool
met_iscolorpattern(gs_state *pgs, ST_RscRefColor color)
{
    if (!color)
        return false;
    return !strcmp(color, met_pattern);
}

void
met_setcharpathmode(gs_state *pgs, bool mode)
{
    metgstate_t *pmg = gs_state_client_data(pgs);
    pmg->charpathmode = mode;
}

bool
met_currentcharpathmode(gs_state *pgs)
{
    metgstate_t *pmg = gs_state_client_data(pgs);
    return pmg->charpathmode;
}

void
met_setcharfillcolor(gs_state *pgs, ST_RscRefColor color)
{
    metgstate_t *pmg = gs_state_client_data(pgs);
    pmg->CharFill = color;
}

ST_RscRefColor met_currentcharfillcolor(gs_state *pgs)
{
    metgstate_t *pmg = gs_state_client_data(pgs);
    return pmg->CharFill;
}

private metgstate_t *
client_data(gs_state *pgs)
{
    return gs_state_client_data(pgs);
}

/* nb should be a resource type */
void
met_setvisualbrush(gs_state *pgs, void *vb)
{
    client_data(pgs)->VisualBrush = vb;
}
    
void *met_currentvisualbrush(gs_state *pgs)
{
    return client_data(pgs)->VisualBrush;
}

private const gs_state_client_procs met_gstate_procs = {
    met_gstate_alloc,
    0,
    met_gstate_free,
    met_gstate_copy_for
};


/* constructor for gstate */
void
met_gstate_init(gs_state *pgs, gs_memory_t *pmem, met_state_t *ms)
{
    metgstate_t *pmg = met_gstate_alloc(pmem);
    pmg->Stroke = 0;
    pmg->Opacity = 0;
    pmg->FillRule = 0;
    pmg->isClosed = 0;
    pmg->charpathmode = 0;
    pmg->CharFill = 0;
    pmg->child = met_none;
    pmg->pmem = pmem;
    pmg->VisualBrush = 0;
    pmg->ms = ms;
    gs_state_set_client(pgs, pmg, &met_gstate_procs, false);
}

met_state_t *
met_state_from_gstate(gs_state *pgs)
{
    return client_data(pgs)->ms;
}