summaryrefslogtreecommitdiff
path: root/core/src/fxge/agg/agg23/fx_agg_vcgen_stroke.cpp
blob: 425ac45acade838bc7e60daebc415e88036a288d (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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214

//----------------------------------------------------------------------------
// XYQ: 2006-01-22 Copied from AGG project.
// TODO: This file uses intensive floating point operations, so it's NOT suitable
// for platforms like Symbian OS. We need to change to FIX format.
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
// 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
//----------------------------------------------------------------------------
//
// Stroke generator
//
//----------------------------------------------------------------------------
#include "../../../../include/fxcrt/fx_basic.h"
#include "agg_vcgen_stroke.h"
namespace agg
{

vcgen_stroke::vcgen_stroke() :
    m_src_vertices(),
    m_out_vertices(),
    m_width(0.5f),
    m_miter_limit(4 * 1.0f),
    m_inner_miter_limit(1.0f + 1.0f / 100),
    m_approx_scale(1.0f),
    m_line_cap(butt_cap),
    m_line_join(miter_join),
    m_inner_join(inner_miter),
    m_closed(0),
    m_status(initial),
    m_src_vertex(0),
    m_out_vertex(0)
{
}
void vcgen_stroke::remove_all()
{
    m_src_vertices.remove_all();
    m_closed = 0;
    m_status = initial;
}
void vcgen_stroke::add_vertex(FX_FLOAT x, FX_FLOAT y, unsigned cmd)
{
    m_status = initial;
    if(is_move_to(cmd)) {
        m_src_vertices.modify_last(vertex_dist_cmd(x, y, cmd));
    } else {
        if(is_vertex(cmd)) {
            m_src_vertices.add(vertex_dist_cmd(x, y, cmd));
        } else {
            m_closed = get_close_flag(cmd);
        }
    }
}
static inline void calc_butt_cap(FX_FLOAT* cap,
                                 const vertex_dist& v0,
                                 const vertex_dist& v1,
                                 FX_FLOAT len,
                                 FX_FLOAT width)
{
    FX_FLOAT dx = FXSYS_MulDiv(v1.y - v0.y, width, len);
    FX_FLOAT dy = FXSYS_MulDiv(v1.x - v0.x, width, len);
    cap[0] = v0.x - dx;
    cap[1] = v0.y + dy;
    cap[2] = v0.x + dx;
    cap[3] = v0.y - dy;
}
void vcgen_stroke::rewind(unsigned)
{
    if(m_status == initial) {
        m_src_vertices.close(m_closed != 0);
        if(m_src_vertices.size() < 3) {
            m_closed = 0;
        }
    }
    m_status = ready;
    m_src_vertex = 0;
    m_out_vertex = 0;
}
unsigned vcgen_stroke::vertex(FX_FLOAT* x, FX_FLOAT* y)
{
    unsigned cmd = path_cmd_line_to;
    line_join_e curj;
    while(!is_stop(cmd)) {
        switch(m_status) {
            case initial:
                rewind(0);
            case ready:
                if(m_src_vertices.size() < 2 + unsigned(m_closed != 0)) {
                    cmd = path_cmd_stop;
                    break;
                }
                m_status = m_closed ? outline1 : cap1;
                cmd = path_cmd_move_to;
                m_src_vertex = 0;
                m_out_vertex = 0;
                break;
            case cap1:
                stroke_calc_cap(m_out_vertices,
                                m_src_vertices[0],
                                m_src_vertices[1],
                                m_src_vertices[0].dist,
                                m_line_cap,
                                m_width,
                                m_approx_scale);
                m_src_vertex = 1;
                m_prev_status = outline1;
                m_status = out_vertices;
                m_out_vertex = 0;
                break;
            case cap2:
                stroke_calc_cap(m_out_vertices,
                                m_src_vertices[m_src_vertices.size() - 1],
                                m_src_vertices[m_src_vertices.size() - 2],
                                m_src_vertices[m_src_vertices.size() - 2].dist,
                                m_line_cap,
                                m_width,
                                m_approx_scale);
                m_prev_status = outline2;
                m_status = out_vertices;
                m_out_vertex = 0;
                break;
            case outline1:
                if(m_closed) {
                    if(m_src_vertex >= m_src_vertices.size()) {
                        m_prev_status = close_first;
                        m_status = end_poly1;
                        break;
                    }
                } else {
                    if(m_src_vertex >= m_src_vertices.size() - 1) {
                        m_status = cap2;
                        break;
                    }
                }
                curj = m_src_vertices[m_src_vertex].cmd & path_flags_jr ? miter_join_round : m_line_join;
                stroke_calc_join(m_out_vertices,
                                 m_src_vertices.prev(m_src_vertex),
                                 m_src_vertices.curr(m_src_vertex),
                                 m_src_vertices.next(m_src_vertex),
                                 m_src_vertices.prev(m_src_vertex).dist,
                                 m_src_vertices.curr(m_src_vertex).dist,
                                 m_width,
                                 curj,
                                 m_inner_join,
                                 m_miter_limit,
                                 m_inner_miter_limit,
                                 m_approx_scale);
                ++m_src_vertex;
                m_prev_status = m_status;
                m_status = out_vertices;
                m_out_vertex = 0;
                break;
            case close_first:
                m_status = outline2;
                cmd = path_cmd_move_to;
            case outline2:
                if(m_src_vertex <= unsigned(m_closed == 0)) {
                    m_status = end_poly2;
                    m_prev_status = stop;
                    break;
                }
                --m_src_vertex;
                curj = m_src_vertices[m_src_vertex].cmd & path_flags_jr ? miter_join_round : m_line_join;
                stroke_calc_join(m_out_vertices,
                                 m_src_vertices.next(m_src_vertex),
                                 m_src_vertices.curr(m_src_vertex),
                                 m_src_vertices.prev(m_src_vertex),
                                 m_src_vertices.curr(m_src_vertex).dist,
                                 m_src_vertices.prev(m_src_vertex).dist,
                                 m_width,
                                 curj,
                                 m_inner_join,
                                 m_miter_limit,
                                 m_inner_miter_limit,
                                 m_approx_scale);
                m_prev_status = m_status;
                m_status = out_vertices;
                m_out_vertex = 0;
                break;
            case out_vertices:
                if(m_out_vertex >= m_out_vertices.size()) {
                    m_status = m_prev_status;
                } else {
                    const point_type& c = m_out_vertices[m_out_vertex++];
                    *x = c.x;
                    *y = c.y;
                    return cmd;
                }
                break;
            case end_poly1:
                m_status = m_prev_status;
                return path_cmd_end_poly | path_flags_close | path_flags_ccw;
            case end_poly2:
                m_status = m_prev_status;
                return path_cmd_end_poly | path_flags_close | path_flags_cw;
            case stop:
                cmd = path_cmd_stop;
                break;
        }
    }
    return cmd;
}
}