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. */
27 #include "art_vpath.h"
28 #include "art_vpath_dash.h"
31 /* Return the length of the largest subpath within vpath */
33 art_vpath_dash_max_subpath (const ArtVpath *vpath)
41 for (i = 0; vpath[i].code != ART_END; i++)
43 if (vpath[i].code == ART_MOVETO || vpath[i].code == ART_MOVETO_OPEN)
45 if (i - start > max_subpath)
46 max_subpath = i - start;
50 if (i - start > max_subpath)
51 max_subpath = i - start;
57 * art_vpath_dash: Add dash style to vpath.
58 * @vpath: Original vpath.
61 * Creates a new vpath that is the result of applying dash style @dash
64 * This implementation has two known flaws:
66 * First, it adds a spurious break at the beginning of the vpath. The
67 * only way I see to resolve this flaw is to run the state forward one
68 * dash break at the beginning, and fix up by looping back to the
69 * first dash break at the end. This is doable but of course adds some
72 * Second, it does not suppress output points that are within epsilon
75 * Return value: Newly created vpath.
78 art_vpath_dash (const ArtVpath *vpath, const ArtVpathDash *dash)
83 int n_result, n_result_max;
88 /* state while traversing dasharray - offset is offset of current dash
89 value, toggle is 0 for "off" and 1 for "on", and phase is the distance
90 in, >= 0, < dash->dash[offset]. */
95 int offset_init, toggle_init;
98 max_subpath = art_vpath_dash_max_subpath (vpath);
99 dists = art_new (double, max_subpath);
103 result = art_new (ArtVpath, n_result_max);
105 /* determine initial values of dash state */
108 phase_init = dash->offset;
109 while (phase_init >= dash->dash[offset_init])
111 toggle_init = !toggle_init;
112 phase_init -= dash->dash[offset_init];
114 if (offset_init == dash->n_dash)
118 for (start = 0; vpath[start].code != ART_END; start = end)
120 for (end = start + 1; vpath[end].code == ART_LINETO; end++);
121 /* subpath is [start..end) */
123 for (i = start; i < end - 1; i++)
127 dx = vpath[i + 1].x - vpath[i].x;
128 dy = vpath[i + 1].y - vpath[i].y;
129 dists[i - start] = sqrt (dx * dx + dy * dy);
130 total_dist += dists[i - start];
132 if (total_dist <= dash->dash[offset_init] - phase_init)
134 /* subpath fits entirely within first dash */
137 for (i = start; i < end; i++)
138 art_vpath_add_point (&result, &n_result, &n_result_max,
139 vpath[i].code, vpath[i].x, vpath[i].y);
144 /* subpath is composed of at least one dash - thus all
145 generated pieces are open */
149 offset = offset_init;
150 toggle = toggle_init;
154 art_vpath_add_point (&result, &n_result, &n_result_max,
155 ART_MOVETO_OPEN, vpath[i].x, vpath[i].y);
158 if (dists[i - start] - dist > dash->dash[offset] - phase)
160 /* dash boundary is next */
164 dist += dash->dash[offset] - phase;
165 a = dist / dists[i - start];
166 x = vpath[i].x + a * (vpath[i + 1].x - vpath[i].x);
167 y = vpath[i].y + a * (vpath[i + 1].y - vpath[i].y);
168 art_vpath_add_point (&result, &n_result, &n_result_max,
169 toggle ? ART_LINETO : ART_MOVETO_OPEN,
171 /* advance to next dash */
175 if (offset == dash->n_dash)
180 /* end of line in vpath is next */
181 phase += dists[i - start] - dist;
185 art_vpath_add_point (&result, &n_result, &n_result_max,
186 ART_LINETO, vpath[i].x, vpath[i].y);
192 art_vpath_add_point (&result, &n_result, &n_result_max,