File: polyops.c

package info (click to toggle)
swftools 0.9.2%2Bds1-3
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 9,488 kB
  • sloc: ansic: 122,576; sh: 8,494; cpp: 8,020; yacc: 2,260; lisp: 904; makefile: 581; python: 304
file content (443 lines) | stat: -rw-r--r-- 12,616 bytes parent folder | download | duplicates (3)
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
/* polyops.c

   Part of the swftools package.

   Copyright (c) 2008 Matthias Kramm <kramm@quiss.org> 
 
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */

#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#ifndef WIN32
#include <unistd.h>
#endif
#include <memory.h>
#include <assert.h>
#include <string.h>
#include <math.h>
#include "../mem.h"
#include "../gfxdevice.h"
#include "../gfxtools.h"
#include "../gfxpoly.h"
#include "../log.h"
#include "polyops.h"

typedef struct _clip {
    gfxpoly_t*poly;
    int openclips;
    struct _clip*next;
} clip_t;

typedef struct _internal {
    gfxdevice_t*out;
    clip_t*clip;
    gfxpoly_t*polyunion;
    
    int good_polygons;
    int bad_polygons;
} internal_t;

static int verbose = 0;

static void dbg(char*format, ...)
{
    if(!verbose)
	return;
    char buf[1024];
    int l;
    va_list arglist;
    va_start(arglist, format);
    vsnprintf(buf, sizeof(buf)-1, format, arglist);
    va_end(arglist);
    l = strlen(buf);
    while(l && buf[l-1]=='\n') {
	buf[l-1] = 0;
	l--;
    }
    printf("(device-polyops) %s\n", buf);
    fflush(stdout);
}

int polyops_setparameter(struct _gfxdevice*dev, const char*key, const char*value)
{
    dbg("polyops_setparameter");
    internal_t*i = (internal_t*)dev->internal;
    if(i->out) return i->out->setparameter(i->out,key,value);
    else return 0;
}

void polyops_startpage(struct _gfxdevice*dev, int width, int height)
{
    dbg("polyops_startpage");
    internal_t*i = (internal_t*)dev->internal;
    if(i->out) i->out->startpage(i->out,width,height);
}

void polyops_startclip(struct _gfxdevice*dev, gfxline_t*line)
{
    dbg("polyops_startclip");
    internal_t*i = (internal_t*)dev->internal;

    gfxpoly_t* oldclip = i->clip?i->clip->poly:0;
    gfxpoly_t* poly = gfxpoly_from_fill(line, DEFAULT_GRID);
    if(poly) 
        i->good_polygons++;
    else
        i->bad_polygons++;

    gfxpoly_t* currentclip = 0;
    int type = 0;

    /* we can't rely on gfxpoly actually being able to convert
       a gfxline into a gfxpoly- for polygons which are too
       complex or just degenerate, this might fail. So handle
       all the cases where polygon conversion or intersection
       might go awry 
       UPDATE: this is not needed anymore. The new gfxpoly
       implementation is stable enough so it always returns
       a valid result. Still, it's good practice.
     */
    if(!poly && !oldclip) {
	i->out->startclip(i->out,line);
	currentclip = 0;
	type = 1;
    } else if(!poly && oldclip) {
	gfxline_t*oldclipline = gfxline_from_gfxpoly(oldclip);
	i->out->startclip(i->out,oldclipline);
	i->out->startclip(i->out,line);
	currentclip = 0;
	type = 2;
    } else if(poly && oldclip) {
	gfxpoly_t*intersection = gfxpoly_intersect(poly, oldclip);
	if(intersection) {
            i->good_polygons++;
	    // this case is what usually happens 
	    gfxpoly_destroy(poly);poly=0;
	    currentclip = intersection;
	    type = 0;
	} else {
            i->bad_polygons++;
	    gfxline_t*oldclipline = gfxline_from_gfxpoly(oldclip);
	    i->out->startclip(i->out, oldclipline);
	    currentclip = poly;
	    type = 1;
	}
    } else if(poly && !oldclip) {
	currentclip = poly;
	type = 0;
    }

    clip_t*n = i->clip;
    i->clip = (clip_t*)rfx_calloc(sizeof(clip_t));
    i->clip->next = n;
    i->clip->poly = currentclip;
    i->clip->openclips = type;
}

void polyops_endclip(struct _gfxdevice*dev)
{
    dbg("polyops_endclip");
    internal_t*i = (internal_t*)dev->internal;

    if(!i->clip) {
	msg("<error> endclip without startclip (in: polyops)\n");
	return;
    }

    clip_t*old = i->clip;
    i->clip = i->clip->next;
    if(old->poly) {
	gfxpoly_destroy(old->poly);old->poly = 0;
    }
    int t;
    for(t=0;t<old->openclips;t++)
	i->out->endclip(i->out);

    old->next = 0;free(old);
}

static void addtounion(struct _gfxdevice*dev, gfxpoly_t*poly)
{
    internal_t*i = (internal_t*)dev->internal;
    if(poly && i->polyunion) {
	gfxpoly_t*old = i->polyunion;
	gfxpoly_t*newpoly = gfxpoly_union(poly,i->polyunion);
	i->polyunion = newpoly;
	gfxpoly_destroy(old);
    }
}

static gfxline_t* handle_poly(gfxdevice_t*dev, gfxpoly_t*poly, char*ok)
{
    internal_t*i = (internal_t*)dev->internal;
    if(i->clip && i->clip->poly) {
	gfxpoly_t*old = poly;
	if(poly) {
	    poly = gfxpoly_intersect(poly, i->clip->poly);
	    gfxpoly_destroy(old);
	}
    }

    if(poly) 
        i->good_polygons++;
    else
        i->bad_polygons++;

    addtounion(dev, poly);
    gfxline_t*gfxline = 0;
    if(poly) {
	// this is the case where everything went right
	gfxline_t*line = gfxline_from_gfxpoly(poly);
	gfxpoly_destroy(poly);
        *ok = 1;
	return line;
    } else {
	if(i->clip && i->clip->poly) {
	    /* convert current clipping from a polygon to an
	       actual "startclip" written to the output */
	    assert(i->clip->openclips <= 1);
	    gfxline_t*clipline = gfxline_from_gfxpoly(i->clip->poly);
	    i->out->startclip(i->out, clipline);
	    gfxline_free(clipline);
	    gfxpoly_destroy(i->clip->poly);i->clip->poly = 0;
	    i->clip->openclips++;
	    return 0;
	} else {
	    return 0;
	}
    }
}

void polyops_stroke(struct _gfxdevice*dev, gfxline_t*line, gfxcoord_t width, gfxcolor_t*color, gfx_capType cap_style, gfx_joinType joint_style, gfxcoord_t miterLimit)
{
    dbg("polyops_stroke");
    internal_t*i = (internal_t*)dev->internal;

    gfxpoly_t* poly = gfxpoly_from_stroke(line, width, cap_style, joint_style, miterLimit, DEFAULT_GRID);
    char ok = 0;
    gfxline_t*line2 = handle_poly(dev, poly, &ok);

    if(ok) {
	if(i->out && line2) i->out->fill(i->out, line2, color);
	gfxline_free(line2);
    } else {
        msg("<error> ..");
	if(i->out) i->out->stroke(i->out, line, width, color, cap_style, joint_style, miterLimit);
    }
}

void polyops_fill(struct _gfxdevice*dev, gfxline_t*line, gfxcolor_t*color)
{
    dbg("polyops_fill");
    internal_t*i = (internal_t*)dev->internal;

    gfxpoly_t*poly = gfxpoly_from_fill(line, DEFAULT_GRID);
    char ok = 0;
    gfxline_t*line2 = handle_poly(dev, poly, &ok);

    if(ok) {
	if(i->out && line2) i->out->fill(i->out, line2, color);
	gfxline_free(line2);
    } else {
	if(i->out) i->out->fill(i->out, line, color);
    }
}

void polyops_fillbitmap(struct _gfxdevice*dev, gfxline_t*line, gfximage_t*img, gfxmatrix_t*matrix, gfxcxform_t*cxform)
{
    dbg("polyops_fillbitmap");
    internal_t*i = (internal_t*)dev->internal;
    
    gfxpoly_t*poly = gfxpoly_from_fill(line, DEFAULT_GRID);
    char ok = 0;
    gfxline_t*line2 = handle_poly(dev, poly, &ok);

    if(ok) {
	if(i->out && line2) i->out->fillbitmap(i->out, line2, img, matrix, cxform);
	gfxline_free(line2);
    } else {
	if(i->out) i->out->fillbitmap(i->out, line, img, matrix, cxform);
    }
}

void polyops_fillgradient(struct _gfxdevice*dev, gfxline_t*line, gfxgradient_t*gradient, gfxgradienttype_t type, gfxmatrix_t*matrix)
{
    dbg("polyops_fillgradient");
    internal_t*i = (internal_t*)dev->internal;
    
    gfxpoly_t*poly = gfxpoly_from_fill(line, DEFAULT_GRID);
    char ok = 0;
    gfxline_t*line2 = handle_poly(dev, poly, &ok);

    if(ok) {
	if(i->out && line2) i->out->fillgradient(i->out, line2, gradient, type, matrix);
	gfxline_free(line2);
    } else {
	if(i->out) i->out->fillgradient(i->out, line, gradient, type, matrix);
    }
}

void polyops_addfont(struct _gfxdevice*dev, gfxfont_t*font)
{
    dbg("polyops_addfont");
    internal_t*i = (internal_t*)dev->internal;
    if(i->out) i->out->addfont(i->out, font);
}

void polyops_drawchar(struct _gfxdevice*dev, gfxfont_t*font, int glyphnr, gfxcolor_t*color, gfxmatrix_t*matrix)
{
    dbg("polyops_drawchar");
    if(!font)
	return;
    internal_t*i = (internal_t*)dev->internal;
    gfxline_t*glyph = gfxline_clone(font->glyphs[glyphnr].line);
    gfxline_transform(glyph, matrix);

    if(i->clip && i->clip->poly) {
	gfxbbox_t bbox = gfxline_getbbox(glyph);
	gfxpoly_t*dummybox = gfxpoly_createbox(bbox.xmin,bbox.ymin,bbox.xmax,bbox.ymax, DEFAULT_GRID);
	gfxline_t*dummybox2 = gfxline_from_gfxpoly(dummybox);
	bbox = gfxline_getbbox(dummybox2);
	gfxline_free(dummybox2);

        char ok=0;
	gfxline_t*gfxline = handle_poly(dev, dummybox, &ok);
	if(ok) {
	    gfxbbox_t bbox2 = gfxline_getbbox(gfxline);
	    double w = bbox2.xmax - bbox2.xmin;
	    double h = bbox2.ymax - bbox2.ymin;
	    if(fabs((bbox.xmax - bbox.xmin) - w) > DEFAULT_GRID*2 ||
	       fabs((bbox.ymax - bbox.ymin) - h) > DEFAULT_GRID*2) {
		/* notable change in character size: character was clipped 
		   TODO: how to deal with diagonal cuts?
		 */
		msg("<trace> Character %d was clipped: (%f,%f,%f,%f) -> (%f,%f,%f,%f)",
			glyphnr, 
			bbox.xmin,bbox.ymin,bbox.xmax,bbox.ymax,
			bbox2.xmin,bbox2.ymin,bbox2.xmax,bbox2.ymax);
		polyops_fill(dev, glyph, color);
	    } else {
		if(i->out) i->out->drawchar(i->out, font, glyphnr, color, matrix);
	    }
	} else {
	    if(i->out) i->out->drawchar(i->out, font, glyphnr, color, matrix);
	}
	gfxline_free(gfxline);
    } else {
	if(i->out) i->out->drawchar(i->out, font, glyphnr, color, matrix);
    }
    
    gfxline_free(glyph);
}

void polyops_drawlink(struct _gfxdevice*dev, gfxline_t*line, const char*action, const char*text)
{
    dbg("polyops_drawlink");
    internal_t*i = (internal_t*)dev->internal;
    if(i->out) i->out->drawlink(i->out, line, action, text);
}

void polyops_endpage(struct _gfxdevice*dev)
{
    dbg("polyops_endpage");
    internal_t*i = (internal_t*)dev->internal;
    if(i->out) i->out->endpage(i->out);
}

gfxresult_t* polyops_finish(struct _gfxdevice*dev)
{
    dbg("polyops_finish");
    internal_t*i = (internal_t*)dev->internal;

    if(i->polyunion) {
	gfxpoly_destroy(i->polyunion);i->polyunion=0;
    } else {
        if(i->bad_polygons) {
            msg("<notice> --flatten success rate: %.1f%% (%d failed polygons)", i->good_polygons*100.0 / (i->good_polygons + i->bad_polygons), i->bad_polygons);
        }
    }
    gfxdevice_t*out = i->out;
    free(i);memset(dev, 0, sizeof(gfxdevice_t));
    if(out) {
	return out->finish(out);
    } else {
	return 0;
    }
}

gfxline_t*gfxdevice_union_getunion(struct _gfxdevice*dev)
{
    internal_t*i = (internal_t*)dev->internal;
    return gfxline_from_gfxpoly(i->polyunion);
}

void gfxdevice_removeclippings_init(gfxdevice_t*dev, gfxdevice_t*out)
{
    dbg("gfxdevice_removeclippings_init");
    internal_t*i = (internal_t*)rfx_calloc(sizeof(internal_t));
    memset(dev, 0, sizeof(gfxdevice_t));
    
    dev->name = "removeclippings";

    dev->internal = i;

    dev->setparameter = polyops_setparameter;
    dev->startpage = polyops_startpage;
    dev->startclip = polyops_startclip;
    dev->endclip = polyops_endclip;
    dev->stroke = polyops_stroke;
    dev->fill = polyops_fill;
    dev->fillbitmap = polyops_fillbitmap;
    dev->fillgradient = polyops_fillgradient;
    dev->addfont = polyops_addfont;
    dev->drawchar = polyops_drawchar;
    dev->drawlink = polyops_drawlink;
    dev->endpage = polyops_endpage;
    dev->finish = polyops_finish;

    i->out = out;
    i->polyunion = 0;
}

void gfxdevice_union_init(gfxdevice_t*dev,gfxdevice_t*out)
{
    dbg("gfxdevice_getunion_init");
    internal_t*i = (internal_t*)rfx_calloc(sizeof(internal_t));
    memset(dev, 0, sizeof(gfxdevice_t));
    
    dev->name = "union";

    dev->internal = i;

    dev->setparameter = polyops_setparameter;
    dev->startpage = polyops_startpage;
    dev->startclip = polyops_startclip;
    dev->endclip = polyops_endclip;
    dev->stroke = polyops_stroke;
    dev->fill = polyops_fill;
    dev->fillbitmap = polyops_fillbitmap;
    dev->fillgradient = polyops_fillgradient;
    dev->addfont = polyops_addfont;
    dev->drawchar = polyops_drawchar;
    dev->drawlink = polyops_drawlink;
    dev->endpage = polyops_endpage;
    dev->finish = polyops_finish;

    i->out = out;
    /* create empty polygon */
    i->polyunion = gfxpoly_from_stroke(0, 0, gfx_capButt, gfx_joinMiter, 0, DEFAULT_GRID);
}