2b375b85c5cc42c5854a7c8593df2f84d91b47c5
[swftools.git] / lib / devices / artsutils.c
1 #include <assert.h>
2 #include <math.h>
3
4 static ArtVpath* gfxline_to_ArtVpath(gfxline_t*line)
5 {
6     ArtVpath *vec = NULL;
7     int pos=0,len=0;
8     gfxline_t*l2;
9     double x=0,y=0;
10
11     /* factor which determines into how many line fragments a spline is converted */
12     double subfraction = 2.4;//0.3
13
14     l2 = line;
15     while(l2) {
16         if(l2->type == gfx_moveTo) {
17             pos ++;
18         } if(l2->type == gfx_lineTo) {
19             pos ++;
20         } if(l2->type == gfx_splineTo) {
21             int parts = (int)(sqrt(fabs(l2->x-2*l2->sx+x) + fabs(l2->y-2*l2->sy+y))*subfraction);
22             if(!parts) parts = 1;
23             pos += parts + 1;
24         }
25         x = l2->x;
26         y = l2->y;
27         l2 = l2->next;
28     }
29     pos++;
30     len = pos;
31
32     vec = art_new (ArtVpath, len+1);
33
34     pos = 0;
35     l2 = line;
36     while(l2) {
37         if(l2->type == gfx_moveTo) {
38             vec[pos].code = ART_MOVETO;
39             vec[pos].x = l2->x;
40             vec[pos].y = l2->y;
41             pos++; 
42             assert(pos<=len);
43         } else if(l2->type == gfx_lineTo) {
44             vec[pos].code = ART_LINETO;
45             vec[pos].x = l2->x;
46             vec[pos].y = l2->y;
47             pos++; 
48             assert(pos<=len);
49         } else if(l2->type == gfx_splineTo) {
50             int i;
51             int parts = (int)(sqrt(fabs(l2->x-2*l2->sx+x) + fabs(l2->y-2*l2->sy+y))*subfraction);
52             double stepsize = parts?1.0/parts:0;
53             for(i=0;i<=parts;i++) {
54                 double t = (double)i*stepsize;
55                 vec[pos].code = ART_LINETO;
56                 vec[pos].x = l2->x*t*t + 2*l2->sx*t*(1-t) + x*(1-t)*(1-t);
57                 vec[pos].y = l2->y*t*t + 2*l2->sy*t*(1-t) + y*(1-t)*(1-t);
58                 pos++;
59                 assert(pos<=len);
60             }
61         }
62         x = l2->x;
63         y = l2->y;
64         l2 = l2->next;
65     }
66     vec[pos].code = ART_END;
67     
68     /* fix "dotted" lines */
69     int t;
70     char linepending=0;
71     for(t=0;vec[t].code!=ART_END;t++) {
72         if(t>0 && vec[t-1].code==ART_MOVETO && vec[t].code==ART_LINETO 
73                 && vec[t+1].code!=ART_LINETO
74             && vec[t-1].x == vec[t].x
75             && vec[t-1].y == vec[t].y) {
76             vec[t].x += 0.01;
77         }
78         if(vec[t].code==ART_MOVETO)
79             linepending=0;
80         x = vec[t].x;
81         y = vec[t].y;
82     }
83
84     // Spot adjacent identical points
85     t = 1;
86     while(t < pos)
87     {
88         if ((vec[t-1].x == vec[t].x) && (vec[t-1].y == vec[t].y)) {
89             // adjacent identical points; remove one
90             memcpy(&(vec[t]), &(vec[t + 1]), sizeof(vec[t]) * (pos - t));
91             pos--;
92         } else {
93             t++;
94         }
95     }
96
97     /* adjacency remover disabled for now, pending code inspection */
98     return vec;
99
100     // Check for further non-adjacent identical points. We don't want any
101     // points other than the first and last points to exactly match.
102     //
103     // If we do find matching points, move the second point slightly. This
104     // currently moves the duplicate 2% towards the midpoint of its neighbours
105     // (easier to calculate than 2% down the perpendicular to the line joining
106     // the neighbours) but limiting the change to 0.1 twips to avoid visual
107     // problems when the shapes are large. Note that there is no minimum
108     // change: if the neighbouring points are colinear and equally spaced,
109     // e.g. they were generated as part of a straight spline, then the
110     // duplicate point may not actually move.
111     //
112     // The scan for duplicates algorithm is quadratic in the number of points:
113     // there's probably a better method but the numbers of points is generally
114     // small so this will do for now.
115     {
116         int i = 1, j;
117         for(; i < (pos - 1); ++i)
118         {
119             for (j = 0; j < i; ++j)
120             {
121                 if ((vec[i].x == vec[j].x)
122                     && (vec[i].y == vec[j].y))
123                 {
124                     // points match; shuffle point
125                     double dx = (vec[i-1].x + vec[i+1].x - (vec[i].x * 2.0)) / 100.0;
126                     double dy = (vec[i-1].y + vec[i+1].y - (vec[i].y * 2.0)) / 100.0;
127                     double dxxyy = (dx*dx) + (dy*dy);
128                     if (dxxyy > 0.01)
129                     {
130                         // This is more than 0.1 twip's distance; scale down
131                         double dscale = sqrt(dxxyy) * 10.0;
132                         dx /= dscale;
133                         dy /= dscale;
134                     };
135                     vec[i].x += dx;
136                     vec[i].y += dy;
137                     break;
138                 }
139             }
140         }
141     }
142
143     return vec;
144 }
145
146 static void show_path(ArtSVP*path)
147 {
148     int t;
149     printf("Segments: %d\n", path->n_segs);
150     for(t=0;t<path->n_segs;t++) {
151         ArtSVPSeg* seg = &path->segs[t];
152         printf("Segment %d: %d points, %s, BBox: (%f,%f,%f,%f)\n", 
153                 t, seg->n_points, seg->dir==0?"UP  ":"DOWN",
154                 seg->bbox.x0, seg->bbox.y0, seg->bbox.x1, seg->bbox.y1);
155         int p;
156         for(p=0;p<seg->n_points;p++) {
157             ArtPoint* point = &seg->points[p];
158             printf("        (%f,%f)\n", point->x, point->y);
159         }
160     }
161     printf("\n");
162 }
163
164 static ArtSVP* gfxfillToSVP(gfxline_t*line, int perturb)
165 {
166     ArtVpath* vec = gfxline_to_ArtVpath(line);
167     if(perturb) {
168         ArtVpath* vec2 = art_vpath_perturb(vec);
169         free(vec);
170         vec = vec2;
171     }
172     ArtSVP *svp = art_svp_from_vpath(vec);
173     free(vec);
174
175     // We need to make sure that the SVP we now have bounds an area (i.e. the
176     // source line wound anticlockwise) rather than excludes an area (i.e. the
177     // line wound clockwise). It seems that PDF (or xpdf) is less strict about
178     // this for bitmaps than it is for fill areas.
179     //
180     // To check this, we'll sum the cross products of all pairs of adjacent
181     // lines. If the result is positive, the direction is correct; if not, we
182     // need to reverse the sense of the SVP generated. The easiest way to do
183     // this is to flip the up/down flags of all the segments.
184     //
185     // This is approximate; since the gfxline_t structure can contain any
186     // combination of moveTo, lineTo and splineTo in any order, not all pairs
187     // of lines in the shape that share a point need be described next to each
188     // other in the sequence. For ease, we'll consider only pairs of lines
189     // stored as lineTos and splineTos without intervening moveTos.
190     //
191     // TODO is this a valid algorithm? My vector maths is rusty.
192     //
193     // It may be more correct to instead reverse the line before we feed it
194     // into gfxfilltoSVP. However, this seems equivalent and is easier to
195     // implement!
196     double total_cross_product = 0.0;
197     gfxline_t* cursor = line;
198     if (cursor != NULL)
199     {
200         double x_last = cursor->x;
201         double y_last = cursor->y;
202         cursor = cursor->next;
203
204         while((cursor != NULL) && (cursor->next != NULL))
205         {
206             if (((cursor->type == gfx_lineTo) || (cursor->type == gfx_splineTo))
207                 && ((cursor->next->type == gfx_lineTo) || (cursor->next->type == gfx_splineTo)))
208             {
209                 // Process these lines
210                 //
211                 // In this space (x right, y down) the cross-product is
212                 // (x1 * y0) - (x0 * y1)
213                 double x0 = cursor->x - x_last;
214                 double y0 = cursor->y - y_last;
215                 double x1 = cursor->next->x - cursor->x;
216                 double y1 = cursor->next->y - cursor->y;
217                 total_cross_product += (x1 * y0) - (x0 * y1);
218             }
219
220             x_last = cursor->x;
221             y_last = cursor->y;
222             cursor = cursor->next;
223         }
224     }
225     if (total_cross_product < 0.0)
226     {
227         int i = 0;
228         for(; i < svp->n_segs; ++i)
229         {
230             if (svp->segs[i].dir != 0)
231                 svp->segs[i].dir = 0;
232             else
233                 svp->segs[i].dir = 1;
234         }
235     }
236     return svp;
237 }
238 static ArtSVP* boxToSVP(double x1, double y1,double x2, double y2)
239 {
240     ArtVpath *vec = art_new (ArtVpath, 5+1);
241     vec[0].code = ART_MOVETO;
242     vec[0].x = x1;
243     vec[0].y = y1;
244     vec[1].code = ART_LINETO;
245     vec[1].x = x1;
246     vec[1].y = y2;
247     vec[2].code = ART_LINETO;
248     vec[2].x = x2;
249     vec[2].y = y2;
250     vec[3].code = ART_LINETO;
251     vec[3].x = x2;
252     vec[3].y = y1;
253     vec[4].code = ART_LINETO;
254     vec[4].x = x1;
255     vec[4].y = y1;
256     vec[5].code = ART_END;
257     vec[5].x = 0;
258     vec[5].y = 0;
259     ArtSVP *svp = art_svp_from_vpath(vec);
260     free(vec);
261     return svp;
262 }
263
264 static ArtSVP* gfxstrokeToSVP(gfxline_t*line, gfxcoord_t width, gfx_capType cap_style, gfx_joinType joint_style, double miterLimit)
265 {
266     ArtVpath* vec = gfxline_to_ArtVpath(line);
267
268     ArtSVP *svp = art_svp_vpath_stroke (vec,
269                         (joint_style==gfx_joinMiter)?ART_PATH_STROKE_JOIN_MITER:
270                         ((joint_style==gfx_joinRound)?ART_PATH_STROKE_JOIN_ROUND:
271                          ((joint_style==gfx_joinBevel)?ART_PATH_STROKE_JOIN_BEVEL:ART_PATH_STROKE_JOIN_BEVEL)),
272                         (cap_style==gfx_capButt)?ART_PATH_STROKE_CAP_BUTT:
273                         ((cap_style==gfx_capRound)?ART_PATH_STROKE_CAP_ROUND:
274                          ((cap_style==gfx_capSquare)?ART_PATH_STROKE_CAP_SQUARE:ART_PATH_STROKE_CAP_SQUARE)),
275                         width, //line_width
276                         miterLimit, //miter_limit
277                         0.05 //flatness
278                         );
279     free(vec);
280     return svp;
281 }
282
283 static gfxline_t* SVPtogfxline(ArtSVP*svp)
284 {
285     int size = 0;
286     int t;
287     int pos = 0;
288     for(t=0;t<svp->n_segs;t++) {
289         size += svp->segs[t].n_points + 1;
290     }
291     gfxline_t* lines = (gfxline_t*)rfx_alloc(sizeof(gfxline_t)*size);
292
293     for(t=0;t<svp->n_segs;t++) {
294         ArtSVPSeg* seg = &svp->segs[t];
295         int p;
296         for(p=0;p<seg->n_points;p++) {
297             lines[pos].type = p==0?gfx_moveTo:gfx_lineTo;
298             ArtPoint* point = &seg->points[p];
299             lines[pos].x = point->x;
300             lines[pos].y = point->y;
301             lines[pos].next = &lines[pos+1];
302             pos++;
303         }
304     }
305     if(pos) {
306         lines[pos-1].next = 0;
307         return lines;
308     } else {
309         return 0;
310     }
311 }
312