summaryrefslogtreecommitdiff
path: root/third_party/agg23/agg_vcgen_dash.cpp
blob: cfeab0764ec5282ca494a100105ff84e9e0ee4f7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178

//----------------------------------------------------------------------------
// Anti-Grain Geometry - Version 2.3
// Copyright (C) 2002-2005 Maxim Shemanarev (http://www.antigrain.com)
//
// Permission to copy, use, modify, sell and distribute this software
// is granted provided this copyright notice appears in all copies.
// This software is provided "as is" without express or implied
// warranty, and with no claim as to its suitability for any purpose.
//
//----------------------------------------------------------------------------
// Contact: mcseem@antigrain.com
//          mcseemagg@yahoo.com
//          http://www.antigrain.com
//----------------------------------------------------------------------------
//
// Line dash generator
//
//----------------------------------------------------------------------------

#include "agg_shorten_path.h"
#include "agg_vcgen_dash.h"
#include "core/fxcrt/fx_basic.h"

namespace agg
{
vcgen_dash::vcgen_dash() :
    m_total_dash_len(0),
    m_num_dashes(0),
    m_dash_start(0),
    m_shorten(0),
    m_curr_dash_start(0),
    m_curr_dash(0),
    m_src_vertices(),
    m_closed(0),
    m_status(initial),
    m_src_vertex(0)
{
}
void vcgen_dash::remove_all_dashes()
{
    m_total_dash_len = 0;
    m_num_dashes = 0;
    m_curr_dash_start = 0;
    m_curr_dash = 0;
}
void vcgen_dash::add_dash(float dash_len, float gap_len)
{
    if(m_num_dashes < max_dashes) {
        m_total_dash_len += dash_len + gap_len;
        m_dashes[m_num_dashes++] = dash_len;
        m_dashes[m_num_dashes++] = gap_len;
    }
}
void vcgen_dash::dash_start(float ds)
{
    m_dash_start = ds;
    calc_dash_start(FXSYS_fabs(ds));
}
void vcgen_dash::calc_dash_start(float ds)
{
    m_curr_dash = 0;
    m_curr_dash_start = 0;
    while(ds > 0) {
        if(ds > m_dashes[m_curr_dash]) {
            ds -= m_dashes[m_curr_dash];
            ++m_curr_dash;
            m_curr_dash_start = 0;
            if(m_curr_dash >= m_num_dashes) {
                m_curr_dash = 0;
            }
        } else {
            m_curr_dash_start = ds;
            ds = 0;
        }
    }
}
void vcgen_dash::remove_all()
{
    m_status = initial;
    m_src_vertices.remove_all();
    m_closed = 0;
}
void vcgen_dash::add_vertex(float x, float y, unsigned cmd)
{
    m_status = initial;
    if(is_move_to(cmd)) {
        m_src_vertices.modify_last(vertex_dist(x, y));
    } else {
        if(is_vertex(cmd)) {
            m_src_vertices.add(vertex_dist(x, y));
        } else {
            m_closed = get_close_flag(cmd);
        }
    }
}
void vcgen_dash::rewind(unsigned)
{
    if(m_status == initial) {
        m_src_vertices.close(m_closed != 0);
        shorten_path(m_src_vertices, m_shorten, m_closed);
    }
    m_status = ready;
    m_src_vertex = 0;
}
unsigned vcgen_dash::vertex(float* x, float* y)
{
    unsigned cmd = path_cmd_move_to;
    while(!is_stop(cmd)) {
        switch(m_status) {
            case initial:
                rewind(0);
            case ready:
                if(m_num_dashes < 2 || m_src_vertices.size() < 2) {
                    cmd = path_cmd_stop;
                    break;
                }
                m_status = polyline;
                m_src_vertex = 1;
                m_v1 = &m_src_vertices[0];
                m_v2 = &m_src_vertices[1];
                m_curr_rest = m_v1->dist;
                *x = m_v1->x;
                *y = m_v1->y;
                if(m_dash_start >= 0) {
                    calc_dash_start(m_dash_start);
                }
                return path_cmd_move_to;
            case polyline: {
                    float dash_rest = m_dashes[m_curr_dash] - m_curr_dash_start;
                    unsigned cmd = (m_curr_dash & 1) ?
                                   path_cmd_move_to :
                                   path_cmd_line_to;
                    if(m_curr_rest > dash_rest) {
                        m_curr_rest -= dash_rest;
                        ++m_curr_dash;
                        if(m_curr_dash >= m_num_dashes) {
                            m_curr_dash = 0;
                        }
                        m_curr_dash_start = 0;
                        *x = m_v2->x - (m_v2->x - m_v1->x) * m_curr_rest / m_v1->dist;
                        *y = m_v2->y - (m_v2->y - m_v1->y) * m_curr_rest / m_v1->dist;
                    } else {
                        m_curr_dash_start += m_curr_rest;
                        *x = m_v2->x;
                        *y = m_v2->y;
                        ++m_src_vertex;
                        m_v1 = m_v2;
                        m_curr_rest = m_v1->dist;
                        if(m_closed) {
                            if(m_src_vertex > m_src_vertices.size()) {
                                m_status = stop;
                            } else {
                                m_v2 = &m_src_vertices
                                       [
                                           (m_src_vertex >= m_src_vertices.size()) ? 0 :
                                           m_src_vertex
                                       ];
                            }
                        } else {
                            if(m_src_vertex >= m_src_vertices.size()) {
                                m_status = stop;
                            } else {
                                m_v2 = &m_src_vertices[m_src_vertex];
                            }
                        }
                    }
                    return cmd;
                }
                break;
            case stop:
                cmd = path_cmd_stop;
                break;
        }
    }
    return path_cmd_stop;
}
}