4 static ArtVpath* gfxline_to_ArtVpath(gfxline_t*line)
11 /* factor which determines into how many line fragments a spline is converted */
12 double subfraction = 2.4;//0.3
16 if(l2->type == gfx_moveTo) {
18 } if(l2->type == gfx_lineTo) {
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);
32 vec = art_new (ArtVpath, len+1);
37 if(l2->type == gfx_moveTo) {
38 vec[pos].code = ART_MOVETO;
43 } else if(l2->type == gfx_lineTo) {
44 vec[pos].code = ART_LINETO;
49 } else if(l2->type == gfx_splineTo) {
51 int parts = (int)(sqrt(fabs(l2->x-2*l2->sx+x) + fabs(l2->y-2*l2->sy+y))*subfraction);
53 for(i=0;i<=parts;i++) {
54 double t = (double)i/(double)parts;
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);
66 vec[pos].code = ART_END;
68 // Spot adjacent identical points
73 double dx = vec[j].x - vec[j-1].x;
74 double dy = vec[j].y - vec[j-1].y;
75 double d = dx*dx + dy*dy;
76 if ((vec[j-1].x == vec[j].x)
77 && (vec[j-1].y == vec[j].y))
79 // adjacent identical points; remove one
80 memcpy(&(vec[j]), &(vec[j + 1]), sizeof(vec[j]) * (pos - j));
91 // Check for further non-adjacent identical points. We don't want any
92 // points other than the first and last points to exactly match.
94 // If we do find matching points, move the second point slightly. This
95 // currently moves the duplicate 2% towards the midpoint of its neighbours
96 // (easier to calculate than 2% down the perpendicular to the line joining
97 // the neighbours) but limiting the change to 0.1 twips to avoid visual
98 // problems when the shapes are large. Note that there is no minimum
99 // change: if the neighbouring points are colinear and equally spaced,
100 // e.g. they were generated as part of a straight spline, then the
101 // duplicate point may not actually move.
103 // The scan for duplicates algorithm is quadratic in the number of points:
104 // there's probably a better method but the numbers of points is generally
105 // small so this will do for now.
108 for(; i < (pos - 1); ++i)
110 for (j = 0; j < i; ++j)
112 if ((vec[i].x == vec[j].x)
113 && (vec[i].y == vec[j].y))
115 // points match; shuffle point
116 double dx = (vec[i-1].x + vec[i+1].x - (vec[i].x * 2.0)) / 100.0;
117 double dy = (vec[i-1].y + vec[i+1].y - (vec[i].y * 2.0)) / 100.0;
118 double dxxyy = (dx*dx) + (dy*dy);
121 // This is more than 0.1 twip's distance; scale down
122 double dscale = sqrt(dxxyy) * 10.0;
137 static void show_path(ArtSVP*path)
140 printf("Segments: %d\n", path->n_segs);
141 for(t=0;t<path->n_segs;t++) {
142 ArtSVPSeg* seg = &path->segs[t];
143 printf("Segment %d: %d points, %s, BBox: (%f,%f,%f,%f)\n",
144 t, seg->n_points, seg->dir==0?"UP ":"DOWN",
145 seg->bbox.x0, seg->bbox.y0, seg->bbox.x1, seg->bbox.y1);
147 for(p=0;p<seg->n_points;p++) {
148 ArtPoint* point = &seg->points[p];
149 printf(" (%f,%f)\n", point->x, point->y);
155 static ArtSVP* gfxfillToSVP(gfxline_t*line, int perturb)
157 ArtVpath* vec = gfxline_to_ArtVpath(line);
159 ArtVpath* vec2 = art_vpath_perturb(vec);
163 ArtSVP *svp = art_svp_from_vpath(vec);
167 static ArtSVP* boxToSVP(double x1, double y1,double x2, double y2)
169 ArtVpath *vec = art_new (ArtVpath, 5+1);
170 vec[0].code = ART_MOVETO;
173 vec[1].code = ART_LINETO;
176 vec[2].code = ART_LINETO;
179 vec[3].code = ART_LINETO;
182 vec[4].code = ART_LINETO;
185 vec[5].code = ART_END;
188 ArtSVP *svp = art_svp_from_vpath(vec);
193 static ArtSVP* gfxstrokeToSVP(gfxline_t*line, gfxcoord_t width, gfx_capType cap_style, gfx_joinType joint_style, double miterLimit)
195 ArtVpath* vec = gfxline_to_ArtVpath(line);
196 ArtSVP *svp = art_svp_vpath_stroke (vec,
197 (joint_style==gfx_joinMiter)?ART_PATH_STROKE_JOIN_MITER:
198 ((joint_style==gfx_joinRound)?ART_PATH_STROKE_JOIN_ROUND:
199 ((joint_style==gfx_joinBevel)?ART_PATH_STROKE_JOIN_BEVEL:ART_PATH_STROKE_JOIN_BEVEL)),
200 (cap_style==gfx_capButt)?ART_PATH_STROKE_CAP_BUTT:
201 ((cap_style==gfx_capRound)?ART_PATH_STROKE_CAP_ROUND:
202 ((cap_style==gfx_capSquare)?ART_PATH_STROKE_CAP_SQUARE:ART_PATH_STROKE_CAP_SQUARE)),
204 miterLimit, //miter_limit
211 static gfxline_t* SVPtogfxline(ArtSVP*svp)
216 for(t=0;t<svp->n_segs;t++) {
217 size += svp->segs[t].n_points + 1;
219 gfxline_t* lines = (gfxline_t*)rfx_alloc(sizeof(gfxline_t)*size);
221 for(t=0;t<svp->n_segs;t++) {
222 ArtSVPSeg* seg = &svp->segs[t];
224 for(p=0;p<seg->n_points;p++) {
225 lines[pos].type = p==0?gfx_moveTo:gfx_lineTo;
226 ArtPoint* point = &seg->points[p];
227 lines[pos].x = point->x;
228 lines[pos].y = point->y;
229 lines[pos].next = &lines[pos+1];
234 lines[pos-1].next = 0;