1 //----------------------------------------------------------------------------
2 // Anti-Grain Geometry - Version 2.3
3 // Copyright (C) 2002-2005 Maxim Shemanarev (http://www.antigrain.com)
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.
10 //----------------------------------------------------------------------------
11 // Contact: mcseem@antigrain.com
12 // mcseemagg@yahoo.com
13 // http://www.antigrain.com
14 //----------------------------------------------------------------------------
16 // Line dash generator
18 //----------------------------------------------------------------------------
21 #include "agg_vcgen_dash.h"
22 #include "agg_shorten_path.h"
27 //------------------------------------------------------------------------
28 vcgen_dash::vcgen_dash() :
29 m_total_dash_len(0.0),
33 m_curr_dash_start(0.0),
44 //------------------------------------------------------------------------
45 void vcgen_dash::remove_all_dashes()
47 m_total_dash_len
= 0.0;
49 m_curr_dash_start
= 0.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
)
70 calc_dash_start(fabs(ds
));
74 //------------------------------------------------------------------------
75 void vcgen_dash::calc_dash_start(double ds
)
78 m_curr_dash_start
= 0.0;
81 if(ds
> m_dashes
[m_curr_dash
])
83 ds
-= m_dashes
[m_curr_dash
];
85 m_curr_dash_start
= 0.0;
86 if(m_curr_dash
>= m_num_dashes
) m_curr_dash
= 0;
90 m_curr_dash_start
= ds
;
97 //------------------------------------------------------------------------
98 void vcgen_dash::remove_all()
101 m_src_vertices
.remove_all();
106 //------------------------------------------------------------------------
107 void vcgen_dash::add_vertex(double x
, double y
, unsigned cmd
)
112 m_src_vertices
.modify_last(vertex_dist(x
, y
));
118 m_src_vertices
.add(vertex_dist(x
, y
));
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
);
141 //------------------------------------------------------------------------
142 unsigned vcgen_dash::vertex(double* x
, double* y
)
144 unsigned cmd
= path_cmd_move_to
;
153 if(m_num_dashes
< 2 || m_src_vertices
.size() < 2)
160 m_v1
= &m_src_vertices
[0];
161 m_v2
= &m_src_vertices
[1];
162 m_curr_rest
= m_v1
->dist
;
165 if(m_dash_start
>= 0.0) calc_dash_start(m_dash_start
);
166 return path_cmd_move_to
;
170 double dash_rest
= m_dashes
[m_curr_dash
] - m_curr_dash_start
;
172 unsigned _cmd
= (m_curr_dash
& 1) ?
176 if(m_curr_rest
> dash_rest
)
178 m_curr_rest
-= dash_rest
;
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
;
187 m_curr_dash_start
+= m_curr_rest
;
192 m_curr_rest
= m_v1
->dist
;
195 if(m_src_vertex
> m_src_vertices
.size())
201 m_v2
= &m_src_vertices
203 (m_src_vertex
>= m_src_vertices
.size()) ? 0 :
210 if(m_src_vertex
>= m_src_vertices
.size())
216 m_v2
= &m_src_vertices
[m_src_vertex
];
223 // statement unreachable
232 return path_cmd_stop
;