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//
16// Terminal markers generator (arrowhead/arrowtail)
17//
18//----------------------------------------------------------------------------
19
20#include "agg_vcgen_markers_term.h"
21
22namespace agg
23{
24
25    //------------------------------------------------------------------------
26    void vcgen_markers_term::remove_all()
27    {
28        m_markers.remove_all();
29    }
30
31
32    //------------------------------------------------------------------------
33    void vcgen_markers_term::add_vertex(double x, double y, unsigned cmd)
34    {
35        if(is_move_to(cmd))
36        {
37            if(m_markers.size() & 1)
38            {
39                // Initial state, the first coordinate was added.
40                // If two of more calls of start_vertex() occures
41                // we just modify the last one.
42                m_markers.modify_last(coord_type(x, y));
43            }
44            else
45            {
46                m_markers.add(coord_type(x, y));
47            }
48        }
49        else
50        {
51            if(is_vertex(cmd))
52            {
53                if(m_markers.size() & 1)
54                {
55                    // Initial state, the first coordinate was added.
56                    // Add three more points, 0,1,1,0
57                    m_markers.add(coord_type(x, y));
58                    m_markers.add(m_markers[m_markers.size() - 1]);
59                    m_markers.add(m_markers[m_markers.size() - 3]);
60                }
61                else
62                {
63                    if(m_markers.size())
64                    {
65                        // Replace two last points: 0,1,1,0 -> 0,1,2,1
66                        m_markers[m_markers.size() - 1] = m_markers[m_markers.size() - 2];
67                        m_markers[m_markers.size() - 2] = coord_type(x, y);
68                    }
69                }
70            }
71        }
72    }
73
74
75    //------------------------------------------------------------------------
76    void vcgen_markers_term::rewind(unsigned path_id)
77    {
78        m_curr_id = path_id * 2;
79        m_curr_idx = m_curr_id;
80    }
81
82
83    //------------------------------------------------------------------------
84    unsigned vcgen_markers_term::vertex(double* x, double* y)
85    {
86        if(m_curr_id > 2 || m_curr_idx >= m_markers.size())
87        {
88            return path_cmd_stop;
89        }
90        const coord_type& c = m_markers[m_curr_idx];
91        *x = c.x;
92        *y = c.y;
93        if(m_curr_idx & 1)
94        {
95            m_curr_idx += 3;
96            return path_cmd_line_to;
97        }
98        ++m_curr_idx;
99        return path_cmd_move_to;
100    }
101
102
103}
104