update dev300-m58
[ooovba.git] / agg / inc / agg_rasterizer_outline_aa.h
blobce543ed7f39117ca04483c99ffd17f3dab56dad5
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 //----------------------------------------------------------------------------
15 #ifndef AGG_RASTERIZER_OUTLINE_AA_INCLUDED
16 #define AGG_RASTERIZER_OUTLINE_AA_INCLUDED
18 #include "agg_basics.h"
19 #include "agg_line_aa_basics.h"
20 #include "agg_vertex_sequence.h"
22 namespace agg
25 //-------------------------------------------------------------------------
26 inline bool cmp_dist_start(int d) { return d > 0; }
27 inline bool cmp_dist_end(int d) { return d <= 0; }
31 //-----------------------------------------------------------line_aa_vertex
32 // Vertex (x, y) with the distance to the next one. The last vertex has
33 // the distance between the last and the first points
34 struct line_aa_vertex
36 int x;
37 int y;
38 int len;
40 line_aa_vertex() {}
41 line_aa_vertex(int x_, int y_) :
42 x(x_),
43 y(y_),
44 len(0)
48 bool operator () (const line_aa_vertex& val)
50 double dx = val.x - x;
51 double dy = val.y - y;
52 return (len = int(sqrt(dx * dx + dy * dy))) >
53 (line_subpixel_size + line_subpixel_size / 2);
60 //=======================================================rasterizer_outline_aa
61 template<class Renderer> class rasterizer_outline_aa
63 private:
64 //------------------------------------------------------------------------
65 struct draw_vars
67 unsigned idx;
68 int x1, y1, x2, y2;
69 line_parameters curr, next;
70 int lcurr, lnext;
71 int xb1, yb1, xb2, yb2;
72 unsigned flags;
75 void draw(draw_vars& dv, unsigned start, unsigned end);
77 public:
78 typedef line_aa_vertex vertex_type;
79 typedef vertex_sequence<vertex_type, 6> vertex_storage_type;
81 rasterizer_outline_aa(Renderer& ren) :
82 m_ren(ren),
83 m_accurate_join(m_ren.accurate_join_only()),
84 m_round_cap(false),
85 m_start_x(0),
86 m_start_y(0)
90 //------------------------------------------------------------------------
91 void accurate_join(bool v)
93 m_accurate_join = m_ren.accurate_join_only() ? true : v;
95 bool accurate_join() const { return m_accurate_join; }
97 //------------------------------------------------------------------------
98 void round_cap(bool v) { m_round_cap = v; }
99 bool round_cap() const { return m_round_cap; }
101 //------------------------------------------------------------------------
102 void move_to(int x, int y)
104 m_src_vertices.modify_last(vertex_type(m_start_x = x, m_start_y = y));
107 //------------------------------------------------------------------------
108 void line_to(int x, int y)
110 m_src_vertices.add(vertex_type(x, y));
113 //------------------------------------------------------------------------
114 void move_to_d(double x, double y)
116 move_to(line_coord(x), line_coord(y));
119 //------------------------------------------------------------------------
120 void line_to_d(double x, double y)
122 line_to(line_coord(x), line_coord(y));
125 //------------------------------------------------------------------------
126 void render(bool close_polygon);
128 //------------------------------------------------------------------------
129 void add_vertex(double x, double y, unsigned cmd)
131 if(is_move_to(cmd))
133 render(false);
134 move_to_d(x, y);
136 else
138 if(is_end_poly(cmd))
140 render(is_closed(cmd));
141 if(is_closed(cmd))
143 move_to(m_start_x, m_start_y);
146 else
148 line_to_d(x, y);
153 //------------------------------------------------------------------------
154 template<class VertexSource>
155 void add_path(VertexSource& vs, unsigned id=0)
157 double x;
158 double y;
160 unsigned cmd;
161 vs.rewind(id);
162 while(!is_stop(cmd = vs.vertex(&x, &y)))
164 add_vertex(x, y, cmd);
166 render(false);
170 //------------------------------------------------------------------------
171 template<class VertexSource, class ColorStorage, class PathId>
172 void render_all_paths(VertexSource& vs,
173 const ColorStorage& colors,
174 const PathId& id,
175 unsigned num_paths)
177 for(unsigned i = 0; i < num_paths; i++)
179 m_ren.color(colors[i]);
180 add_path(vs, id[i]);
185 //------------------------------------------------------------------------
186 template<class Ctrl> void render_ctrl(Ctrl& c)
188 unsigned i;
189 for(i = 0; i < c.num_paths(); i++)
191 m_ren.color(c.color(i));
192 add_path(c, i);
196 private:
197 rasterizer_outline_aa(const rasterizer_outline_aa<Renderer>&);
198 const rasterizer_outline_aa<Renderer>& operator =
199 (const rasterizer_outline_aa<Renderer>&);
201 Renderer& m_ren;
202 vertex_storage_type m_src_vertices;
203 bool m_accurate_join;
204 bool m_round_cap;
205 int m_start_x;
206 int m_start_y;
216 //----------------------------------------------------------------------------
217 template<class Renderer>
218 void rasterizer_outline_aa<Renderer>::draw(draw_vars& dv, unsigned start, unsigned end)
220 unsigned i;
221 const vertex_storage_type::value_type* v;
223 for(i = start; i < end; i++)
225 switch(dv.flags)
227 case 0: m_ren.line3(dv.curr, dv.xb1, dv.yb1, dv.xb2, dv.yb2); break;
228 case 1: m_ren.line2(dv.curr, dv.xb2, dv.yb2); break;
229 case 2: m_ren.line1(dv.curr, dv.xb1, dv.yb1); break;
230 case 3: m_ren.line0(dv.curr); break;
233 dv.x1 = dv.x2;
234 dv.y1 = dv.y2;
235 dv.lcurr = dv.lnext;
236 dv.lnext = m_src_vertices[dv.idx].len;
238 ++dv.idx;
239 if(dv.idx >= m_src_vertices.size()) dv.idx = 0;
241 v = &m_src_vertices[dv.idx];
242 dv.x2 = v->x;
243 dv.y2 = v->y;
245 dv.curr = dv.next;
246 dv.next = line_parameters(dv.x1, dv.y1, dv.x2, dv.y2, dv.lnext);
247 dv.xb1 = dv.xb2;
248 dv.yb1 = dv.yb2;
250 if(m_accurate_join)
252 dv.flags = 0;
254 else
256 dv.flags >>= 1;
257 dv.flags |= ((dv.curr.diagonal_quadrant() ==
258 dv.next.diagonal_quadrant()) << 1);
261 if((dv.flags & 2) == 0)
263 bisectrix(dv.curr, dv.next, &dv.xb2, &dv.yb2);
271 //----------------------------------------------------------------------------
272 template<class Renderer>
273 void rasterizer_outline_aa<Renderer>::render(bool close_polygon)
275 m_src_vertices.close(close_polygon);
276 draw_vars dv;
277 const vertex_storage_type::value_type* v;
278 int x1;
279 int y1;
280 int x2;
281 int y2;
282 int lprev;
284 if(close_polygon)
286 if(m_src_vertices.size() >= 3)
288 dv.idx = 2;
290 v = &m_src_vertices[m_src_vertices.size() - 1];
291 x1 = v->x;
292 y1 = v->y;
293 lprev = v->len;
295 v = &m_src_vertices[0];
296 x2 = v->x;
297 y2 = v->y;
298 dv.lcurr = v->len;
299 line_parameters prev(x1, y1, x2, y2, lprev);
301 v = &m_src_vertices[1];
302 dv.x1 = v->x;
303 dv.y1 = v->y;
304 dv.lnext = v->len;
305 dv.curr = line_parameters(x2, y2, dv.x1, dv.y1, dv.lcurr);
307 v = &m_src_vertices[dv.idx];
308 dv.x2 = v->x;
309 dv.y2 = v->y;
310 dv.next = line_parameters(dv.x1, dv.y1, dv.x2, dv.y2, dv.lnext);
312 dv.xb1 = 0;
313 dv.yb1 = 0;
314 dv.xb2 = 0;
315 dv.yb2 = 0;
317 if(m_accurate_join)
319 dv.flags = 0;
321 else
323 dv.flags =
324 (prev.diagonal_quadrant() == dv.curr.diagonal_quadrant()) |
325 ((dv.curr.diagonal_quadrant() == dv.next.diagonal_quadrant()) << 1);
328 if((dv.flags & 1) == 0)
330 bisectrix(prev, dv.curr, &dv.xb1, &dv.yb1);
333 if((dv.flags & 2) == 0)
335 bisectrix(dv.curr, dv.next, &dv.xb2, &dv.yb2);
337 draw(dv, 0, m_src_vertices.size());
340 else
342 switch(m_src_vertices.size())
344 case 0:
345 case 1:
346 break;
348 case 2:
350 v = &m_src_vertices[0];
351 x1 = v->x;
352 y1 = v->y;
353 lprev = v->len;
354 v = &m_src_vertices[1];
355 x2 = v->x;
356 y2 = v->y;
357 line_parameters lp(x1, y1, x2, y2, lprev);
358 if(m_round_cap)
360 m_ren.semidot(cmp_dist_start, x1, y1, x1 + (y2 - y1), y1 - (x2 - x1));
362 m_ren.line3(lp,
363 x1 + (y2 - y1),
364 y1 - (x2 - x1),
365 x2 + (y2 - y1),
366 y2 - (x2 - x1));
367 if(m_round_cap)
369 m_ren.semidot(cmp_dist_end, x2, y2, x2 + (y2 - y1), y2 - (x2 - x1));
372 break;
374 case 3:
376 int x3, y3;
377 int lnext;
378 v = &m_src_vertices[0];
379 x1 = v->x;
380 y1 = v->y;
381 lprev = v->len;
382 v = &m_src_vertices[1];
383 x2 = v->x;
384 y2 = v->y;
385 lnext = v->len;
386 v = &m_src_vertices[2];
387 x3 = v->x;
388 y3 = v->y;
389 line_parameters lp1(x1, y1, x2, y2, lprev);
390 line_parameters lp2(x2, y2, x3, y3, lnext);
391 bisectrix(lp1, lp2, &dv.xb1, &dv.yb1);
393 if(m_round_cap)
395 m_ren.semidot(cmp_dist_start, x1, y1, x1 + (y2 - y1), y1 - (x2 - x1));
397 m_ren.line3(lp1,
398 x1 + (y2 - y1),
399 y1 - (x2 - x1),
400 dv.xb1,
401 dv.yb1);
403 m_ren.line3(lp2,
404 dv.xb1,
405 dv.yb1,
406 x3 + (y3 - y2),
407 y3 - (x3 - x2));
408 if(m_round_cap)
410 m_ren.semidot(cmp_dist_end, x3, y3, x3 + (y3 - y2), y3 - (x3 - x2));
413 break;
415 default:
417 dv.idx = 3;
419 v = &m_src_vertices[0];
420 x1 = v->x;
421 y1 = v->y;
422 lprev = v->len;
424 v = &m_src_vertices[1];
425 x2 = v->x;
426 y2 = v->y;
427 dv.lcurr = v->len;
428 line_parameters prev(x1, y1, x2, y2, lprev);
430 v = &m_src_vertices[2];
431 dv.x1 = v->x;
432 dv.y1 = v->y;
433 dv.lnext = v->len;
434 dv.curr = line_parameters(x2, y2, dv.x1, dv.y1, dv.lcurr);
436 v = &m_src_vertices[dv.idx];
437 dv.x2 = v->x;
438 dv.y2 = v->y;
439 dv.next = line_parameters(dv.x1, dv.y1, dv.x2, dv.y2, dv.lnext);
441 dv.xb1 = 0;
442 dv.yb1 = 0;
443 dv.xb2 = 0;
444 dv.yb2 = 0;
446 if(m_accurate_join)
448 dv.flags = 0;
450 else
452 dv.flags =
453 (prev.diagonal_quadrant() == dv.curr.diagonal_quadrant()) |
454 ((dv.curr.diagonal_quadrant() == dv.next.diagonal_quadrant()) << 1);
457 if((dv.flags & 1) == 0)
459 bisectrix(prev, dv.curr, &dv.xb1, &dv.yb1);
460 m_ren.line3(prev,
461 x1 + (y2 - y1),
462 y1 - (x2 - x1),
463 dv.xb1,
464 dv.yb1);
466 else
468 m_ren.line1(prev,
469 x1 + (y2 - y1),
470 y1 - (x2 - x1));
472 if(m_round_cap)
474 m_ren.semidot(cmp_dist_start, x1, y1, x1 + (y2 - y1), y1 - (x2 - x1));
476 if((dv.flags & 2) == 0)
478 bisectrix(dv.curr, dv.next, &dv.xb2, &dv.yb2);
481 draw(dv, 1, m_src_vertices.size() - 2);
483 if((dv.flags & 1) == 0)
485 m_ren.line3(dv.curr,
486 dv.xb1,
487 dv.yb1,
488 dv.curr.x2 + (dv.curr.y2 - dv.curr.y1),
489 dv.curr.y2 - (dv.curr.x2 - dv.curr.x1));
491 else
493 m_ren.line2(dv.curr,
494 dv.curr.x2 + (dv.curr.y2 - dv.curr.y1),
495 dv.curr.y2 - (dv.curr.x2 - dv.curr.x1));
497 if(m_round_cap)
499 m_ren.semidot(cmp_dist_end, dv.curr.x2, dv.curr.y2,
500 dv.curr.x2 + (dv.curr.y2 - dv.curr.y1),
501 dv.curr.y2 - (dv.curr.x2 - dv.curr.x1));
505 break;
508 m_src_vertices.remove_all();
515 #endif