merge the formfield patch from ooo-build
[ooovba.git] / agg / source / agg_vcgen_dash.cpp
blobf7f7710222b201386395488b820d73739e086184
1 //----------------------------------------------------------------------------
2 // Anti-Grain Geometry - Version 2.3
3 // Copyright (C) 2002-2005 Maxim Shemanarev (http://www.antigrain.com)
4 //
5 // Permission to copy, use, modify, sell and distribute this software
6 // is granted provided this copyright notice appears in all copies.
7 // This software is provided "as is" without express or implied
8 // warranty, and with no claim as to its suitability for any purpose.
9 //
10 //----------------------------------------------------------------------------
11 // Contact: mcseem@antigrain.com
12 // mcseemagg@yahoo.com
13 // http://www.antigrain.com
14 //----------------------------------------------------------------------------
16 // Line dash generator
18 //----------------------------------------------------------------------------
20 #include <math.h>
21 #include "agg_vcgen_dash.h"
22 #include "agg_shorten_path.h"
24 namespace agg
27 //------------------------------------------------------------------------
28 vcgen_dash::vcgen_dash() :
29 m_total_dash_len(0.0),
30 m_num_dashes(0),
31 m_dash_start(0.0),
32 m_shorten(0.0),
33 m_curr_dash_start(0.0),
34 m_curr_dash(0),
35 m_src_vertices(),
36 m_closed(0),
37 m_status(initial),
38 m_src_vertex(0)
44 //------------------------------------------------------------------------
45 void vcgen_dash::remove_all_dashes()
47 m_total_dash_len = 0.0;
48 m_num_dashes = 0;
49 m_curr_dash_start = 0.0;
50 m_curr_dash = 0;
54 //------------------------------------------------------------------------
55 void vcgen_dash::add_dash(double dash_len, double gap_len)
57 if(m_num_dashes < max_dashes)
59 m_total_dash_len += dash_len + gap_len;
60 m_dashes[m_num_dashes++] = dash_len;
61 m_dashes[m_num_dashes++] = gap_len;
66 //------------------------------------------------------------------------
67 void vcgen_dash::dash_start(double ds)
69 m_dash_start = ds;
70 calc_dash_start(fabs(ds));
74 //------------------------------------------------------------------------
75 void vcgen_dash::calc_dash_start(double ds)
77 m_curr_dash = 0;
78 m_curr_dash_start = 0.0;
79 while(ds > 0.0)
81 if(ds > m_dashes[m_curr_dash])
83 ds -= m_dashes[m_curr_dash];
84 ++m_curr_dash;
85 m_curr_dash_start = 0.0;
86 if(m_curr_dash >= m_num_dashes) m_curr_dash = 0;
88 else
90 m_curr_dash_start = ds;
91 ds = 0.0;
97 //------------------------------------------------------------------------
98 void vcgen_dash::remove_all()
100 m_status = initial;
101 m_src_vertices.remove_all();
102 m_closed = 0;
106 //------------------------------------------------------------------------
107 void vcgen_dash::add_vertex(double x, double y, unsigned cmd)
109 m_status = initial;
110 if(is_move_to(cmd))
112 m_src_vertices.modify_last(vertex_dist(x, y));
114 else
116 if(is_vertex(cmd))
118 m_src_vertices.add(vertex_dist(x, y));
120 else
122 m_closed = get_close_flag(cmd);
128 //------------------------------------------------------------------------
129 void vcgen_dash::rewind(unsigned)
131 if(m_status == initial)
133 m_src_vertices.close(m_closed != 0);
134 shorten_path(m_src_vertices, m_shorten, m_closed);
136 m_status = ready;
137 m_src_vertex = 0;
141 //------------------------------------------------------------------------
142 unsigned vcgen_dash::vertex(double* x, double* y)
144 unsigned cmd = path_cmd_move_to;
145 while(!is_stop(cmd))
147 switch(m_status)
149 case initial:
150 rewind(0);
152 case ready:
153 if(m_num_dashes < 2 || m_src_vertices.size() < 2)
155 cmd = path_cmd_stop;
156 break;
158 m_status = polyline;
159 m_src_vertex = 1;
160 m_v1 = &m_src_vertices[0];
161 m_v2 = &m_src_vertices[1];
162 m_curr_rest = m_v1->dist;
163 *x = m_v1->x;
164 *y = m_v1->y;
165 if(m_dash_start >= 0.0) calc_dash_start(m_dash_start);
166 return path_cmd_move_to;
168 case polyline:
170 double dash_rest = m_dashes[m_curr_dash] - m_curr_dash_start;
172 unsigned _cmd = (m_curr_dash & 1) ?
173 path_cmd_move_to :
174 path_cmd_line_to;
176 if(m_curr_rest > dash_rest)
178 m_curr_rest -= dash_rest;
179 ++m_curr_dash;
180 if(m_curr_dash >= m_num_dashes) m_curr_dash = 0;
181 m_curr_dash_start = 0.0;
182 *x = m_v2->x - (m_v2->x - m_v1->x) * m_curr_rest / m_v1->dist;
183 *y = m_v2->y - (m_v2->y - m_v1->y) * m_curr_rest / m_v1->dist;
185 else
187 m_curr_dash_start += m_curr_rest;
188 *x = m_v2->x;
189 *y = m_v2->y;
190 ++m_src_vertex;
191 m_v1 = m_v2;
192 m_curr_rest = m_v1->dist;
193 if(m_closed)
195 if(m_src_vertex > m_src_vertices.size())
197 m_status = stop;
199 else
201 m_v2 = &m_src_vertices
203 (m_src_vertex >= m_src_vertices.size()) ? 0 :
204 m_src_vertex
208 else
210 if(m_src_vertex >= m_src_vertices.size())
212 m_status = stop;
214 else
216 m_v2 = &m_src_vertices[m_src_vertex];
220 return _cmd;
223 // statement unreachable
224 //break;
226 case stop:
227 cmd = path_cmd_stop;
228 break;
232 return path_cmd_stop;