fixed a few bugs in remove_font_transforms filter
[swftools.git] / lib / devices / bbox.c
1 /* bbox.c
2
3    Part of the swftools package.
4
5    Copyright (c) 2006 Matthias Kramm <kramm@quiss.org> 
6  
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
20
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <stdarg.h>
24 #ifndef WIN32
25 #include <unistd.h>
26 #endif
27 #include <memory.h>
28 #include <string.h>
29 #include "../types.h"
30 #include "../mem.h"
31 #include "../gfxdevice.h"
32 #include "../gfxtools.h"
33
34 typedef struct _internal {
35     gfxbbox_t bbox;
36     int do_graphics;
37     int do_text;
38 } internal_t;
39
40 void measuregfxline(internal_t*i, gfxline_t*line)
41 {
42     gfxbbox_t b = gfxline_getbbox(line);
43     if(b.xmin==0 && b.ymin==0 && b.xmax==0 && b.ymax==0) {
44         return;
45     }
46     i->bbox = gfxbbox_expand_to_point(i->bbox, b.xmin, b.ymin);
47     i->bbox = gfxbbox_expand_to_point(i->bbox, b.xmax, b.ymax);
48 }
49
50 int bbox_setparameter(gfxdevice_t*dev, const char*key, const char*value)
51 {
52     internal_t*i = (internal_t*)dev->internal;
53     if(!strcmp(key, "graphics")) {
54         i->do_graphics = atoi(value);
55         return 1;
56     } else if(!strcmp(key, "text")) {
57         i->do_text = atoi(value);
58         return 1;
59     }
60     return 0;
61 }
62
63 void bbox_startpage(gfxdevice_t*dev, int width, int height)
64 {
65     internal_t*i = (internal_t*)dev->internal;
66     i->bbox.xmin = 0;
67     i->bbox.ymin = 0;
68     i->bbox.xmax = 0;
69     i->bbox.ymax = 0;
70 }
71
72 void bbox_startclip(gfxdevice_t*dev, gfxline_t*line)
73 {
74     internal_t*i = (internal_t*)dev->internal;
75 }
76
77 void bbox_endclip(gfxdevice_t*dev)
78 {
79     internal_t*i = (internal_t*)dev->internal;
80 }
81
82 void bbox_stroke(gfxdevice_t*dev, gfxline_t*line, gfxcoord_t width, gfxcolor_t*color, gfx_capType cap_style, gfx_joinType joint_style, gfxcoord_t miterLimit)
83 {
84     internal_t*i = (internal_t*)dev->internal;
85     if(i->do_graphics)
86         measuregfxline(i, line);
87 }
88
89 void bbox_fill(gfxdevice_t*dev, gfxline_t*line, gfxcolor_t*color)
90 {
91     internal_t*i = (internal_t*)dev->internal;
92     if(i->do_graphics)
93         measuregfxline(i, line);
94 }
95
96 void bbox_fillbitmap(gfxdevice_t*dev, gfxline_t*line, gfximage_t*img, gfxmatrix_t*matrix, gfxcxform_t*cxform)
97 {
98     internal_t*i = (internal_t*)dev->internal;
99     if(i->do_graphics)
100         measuregfxline(i, line);
101 }
102
103 void bbox_fillgradient(gfxdevice_t*dev, gfxline_t*line, gfxgradient_t*gradient, gfxgradienttype_t type, gfxmatrix_t*matrix)
104 {
105     internal_t*i = (internal_t*)dev->internal;
106     if(i->do_graphics)
107         measuregfxline(i, line);
108 }
109
110 void bbox_addfont(gfxdevice_t*dev, gfxfont_t*font)
111 {
112     internal_t*i = (internal_t*)dev->internal;
113 }
114
115 void bbox_drawchar(gfxdevice_t*dev, gfxfont_t*font, int glyphnr, gfxcolor_t*color, gfxmatrix_t*matrix)
116 {
117     internal_t*i = (internal_t*)dev->internal;
118     if(!font)
119         return;
120
121     if(i->do_text) {
122         gfxglyph_t*glyph = &font->glyphs[glyphnr];
123         gfxline_t*line2 = gfxline_clone(glyph->line);
124         gfxline_transform(line2, matrix);
125         measuregfxline(i, line2);
126         gfxline_free(line2);
127     }
128 }
129
130 void bbox_drawlink(gfxdevice_t*dev, gfxline_t*line, const char*action)
131 {
132     internal_t*i = (internal_t*)dev->internal;
133 }
134
135 void bbox_endpage(gfxdevice_t*dev)
136 {
137     internal_t*i = (internal_t*)dev->internal;
138 }
139
140 gfxresult_t* bbox_finish(gfxdevice_t*dev)
141 {
142     free(dev->internal);dev->internal = 0;
143     return 0;
144 }
145
146 gfxbbox_t gfxdevice_bbox_getbbox(gfxdevice_t*dev)
147 {
148     internal_t*i = (internal_t*)dev->internal;
149     return i->bbox;
150 }
151
152 void gfxdevice_bbox_init(gfxdevice_t*dev)
153 {
154     internal_t*i = (internal_t*)rfx_calloc(sizeof(internal_t));
155     memset(dev, 0, sizeof(gfxdevice_t));
156
157     dev->name = "bbox";
158
159     dev->internal = i;
160
161     dev->setparameter = bbox_setparameter;
162     dev->startpage = bbox_startpage;
163     dev->startclip = bbox_startclip;
164     dev->endclip = bbox_endclip;
165     dev->stroke = bbox_stroke;
166     dev->fill = bbox_fill;
167     dev->fillbitmap = bbox_fillbitmap;
168     dev->fillgradient = bbox_fillgradient;
169     dev->addfont = bbox_addfont;
170     dev->drawchar = bbox_drawchar;
171     dev->drawlink = bbox_drawlink;
172     dev->endpage = bbox_endpage;
173     dev->finish = bbox_finish;
174
175     i->do_graphics = 1;
176     i->do_text = 1;
177 }
178