agg_rasterizer_outline.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. //----------------------------------------------------------------------------
  2. // Anti-Grain Geometry - Version 2.4
  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_INCLUDED
  16. #define AGG_RASTERIZER_OUTLINE_INCLUDED
  17. #include "agg_basics.h"
  18. namespace agg
  19. {
  20. //======================================================rasterizer_outline
  21. template<class Renderer> class rasterizer_outline
  22. {
  23. public:
  24. explicit rasterizer_outline(Renderer& ren) :
  25. m_ren(&ren),
  26. m_start_x(0),
  27. m_start_y(0),
  28. m_vertices(0)
  29. {}
  30. void attach(Renderer& ren) { m_ren = &ren; }
  31. //--------------------------------------------------------------------
  32. void move_to(int x, int y)
  33. {
  34. m_vertices = 1;
  35. m_ren->move_to(m_start_x = x, m_start_y = y);
  36. }
  37. //--------------------------------------------------------------------
  38. void line_to(int x, int y)
  39. {
  40. ++m_vertices;
  41. m_ren->line_to(x, y);
  42. }
  43. //--------------------------------------------------------------------
  44. void move_to_d(double x, double y)
  45. {
  46. move_to(m_ren->coord(x), m_ren->coord(y));
  47. }
  48. //--------------------------------------------------------------------
  49. void line_to_d(double x, double y)
  50. {
  51. line_to(m_ren->coord(x), m_ren->coord(y));
  52. }
  53. //--------------------------------------------------------------------
  54. void close()
  55. {
  56. if(m_vertices > 2)
  57. {
  58. line_to(m_start_x, m_start_y);
  59. }
  60. m_vertices = 0;
  61. }
  62. //--------------------------------------------------------------------
  63. void add_vertex(double x, double y, unsigned cmd)
  64. {
  65. if(is_move_to(cmd))
  66. {
  67. move_to_d(x, y);
  68. }
  69. else
  70. {
  71. if(is_end_poly(cmd))
  72. {
  73. if(is_closed(cmd)) close();
  74. }
  75. else
  76. {
  77. line_to_d(x, y);
  78. }
  79. }
  80. }
  81. //--------------------------------------------------------------------
  82. template<class VertexSource>
  83. void add_path(VertexSource& vs, unsigned path_id=0)
  84. {
  85. double x;
  86. double y;
  87. unsigned cmd;
  88. vs.rewind(path_id);
  89. while(!is_stop(cmd = vs.vertex(&x, &y)))
  90. {
  91. add_vertex(x, y, cmd);
  92. }
  93. }
  94. //--------------------------------------------------------------------
  95. template<class VertexSource, class ColorStorage, class PathId>
  96. void render_all_paths(VertexSource& vs,
  97. const ColorStorage& colors,
  98. const PathId& path_id,
  99. unsigned num_paths)
  100. {
  101. for(unsigned i = 0; i < num_paths; i++)
  102. {
  103. m_ren->line_color(colors[i]);
  104. add_path(vs, path_id[i]);
  105. }
  106. }
  107. //--------------------------------------------------------------------
  108. template<class Ctrl> void render_ctrl(Ctrl& c)
  109. {
  110. unsigned i;
  111. for(i = 0; i < c.num_paths(); i++)
  112. {
  113. m_ren->line_color(c.color(i));
  114. add_path(c, i);
  115. }
  116. }
  117. private:
  118. Renderer* m_ren;
  119. int m_start_x;
  120. int m_start_y;
  121. unsigned m_vertices;
  122. };
  123. }
  124. #endif