1 /* Libart_LGPL - library of basic graphic primitives
2 * Copyright (C) 1999-2000 Raph Levien
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
20 /* Apply a dash style to a vector path. */
23 #include "art_vpath_dash.h"
30 #include "art_vpath.h"
33 /* Return the length of the largest subpath within vpath */
35 art_vpath_dash_max_subpath (const ArtVpath *vpath)
43 for (i = 0; vpath[i].code != ART_END; i++)
45 if (vpath[i].code == ART_MOVETO || vpath[i].code == ART_MOVETO_OPEN)
47 if (i - start > max_subpath)
48 max_subpath = i - start;
52 if (i - start > max_subpath)
53 max_subpath = i - start;
59 * art_vpath_dash: Add dash style to vpath.
60 * @vpath: Original vpath.
63 * Creates a new vpath that is the result of applying dash style @dash
66 * This implementation has two known flaws:
68 * First, it adds a spurious break at the beginning of the vpath. The
69 * only way I see to resolve this flaw is to run the state forward one
70 * dash break at the beginning, and fix up by looping back to the
71 * first dash break at the end. This is doable but of course adds some
74 * Second, it does not suppress output points that are within epsilon
77 * Return value: Newly created vpath.
80 art_vpath_dash (const ArtVpath *vpath, const ArtVpathDash *dash)
85 int n_result, n_result_max;
90 /* state while traversing dasharray - offset is offset of current dash
91 value, toggle is 0 for "off" and 1 for "on", and phase is the distance
92 in, >= 0, < dash->dash[offset]. */
97 int offset_init, toggle_init;
100 max_subpath = art_vpath_dash_max_subpath (vpath);
101 dists = art_new (double, max_subpath);
105 result = art_new (ArtVpath, n_result_max);
107 /* determine initial values of dash state */
110 phase_init = dash->offset;
111 while (phase_init >= dash->dash[offset_init])
113 toggle_init = !toggle_init;
114 phase_init -= dash->dash[offset_init];
116 if (offset_init == dash->n_dash)
120 for (start = 0; vpath[start].code != ART_END; start = end)
122 for (end = start + 1; vpath[end].code == ART_LINETO; end++);
123 /* subpath is [start..end) */
125 for (i = start; i < end - 1; i++)
129 dx = vpath[i + 1].x - vpath[i].x;
130 dy = vpath[i + 1].y - vpath[i].y;
131 dists[i - start] = sqrt (dx * dx + dy * dy);
132 total_dist += dists[i - start];
134 if (total_dist <= dash->dash[offset_init] - phase_init)
136 /* subpath fits entirely within first dash */
139 for (i = start; i < end; i++)
140 art_vpath_add_point (&result, &n_result, &n_result_max,
141 vpath[i].code, vpath[i].x, vpath[i].y);
146 /* subpath is composed of at least one dash - thus all
147 generated pieces are open */
151 offset = offset_init;
152 toggle = toggle_init;
156 art_vpath_add_point (&result, &n_result, &n_result_max,
157 ART_MOVETO_OPEN, vpath[i].x, vpath[i].y);
160 if (dists[i - start] - dist > dash->dash[offset] - phase)
162 /* dash boundary is next */
166 dist += dash->dash[offset] - phase;
167 a = dist / dists[i - start];
168 x = vpath[i].x + a * (vpath[i + 1].x - vpath[i].x);
169 y = vpath[i].y + a * (vpath[i + 1].y - vpath[i].y);
170 art_vpath_add_point (&result, &n_result, &n_result_max,
171 toggle ? ART_LINETO : ART_MOVETO_OPEN,
173 /* advance to next dash */
177 if (offset == dash->n_dash)
182 /* end of line in vpath is next */
183 phase += dists[i - start] - dist;
187 art_vpath_add_point (&result, &n_result, &n_result_max,
188 ART_LINETO, vpath[i].x, vpath[i].y);
194 art_vpath_add_point (&result, &n_result, &n_result_max,