| 1 |
//---------------------------------------------------------------------------- |
| 2 |
// Anti-Grain Geometry - Version 2.3 |
| 3 |
// Copyright (C) 2002-2005 Maxim Shemanarev (http://www.antigrain.com) |
| 4 |
// Copyright (c) 2008 Rene Rebe <[email protected]> [arc parser code] |
| 5 |
// Copyright (c) 2017 John Horigan <[email protected]> [align_subpath()] |
| 6 |
// |
| 7 |
// Permission to copy, use, modify, sell and distribute this software |
| 8 |
// is granted provided this copyright notice appears in all copies. |
| 9 |
// This software is provided "as is" without express or implied |
| 10 |
// warranty, and with no claim as to its suitability for any purpose. |
| 11 |
// |
| 12 |
//---------------------------------------------------------------------------- |
| 13 |
// Contact: [email protected] |
| 14 |
// [email protected] |
| 15 |
// http://www.antigrain.com |
| 16 |
//---------------------------------------------------------------------------- |
| 17 |
// |
| 18 |
// SVG path renderer. |
| 19 |
// |
| 20 |
//---------------------------------------------------------------------------- |
| 21 |
#ifndef AGG_SVG_PATH_RENDERER_INCLUDED |
| 22 |
#define AGG_SVG_PATH_RENDERER_INCLUDED |
| 23 |
|
| 24 |
#include "agg_path_storage.h" |
| 25 |
#include "agg_conv_transform.h" |
| 26 |
#include "agg_conv_stroke.h" |
| 27 |
#include "agg_conv_contour.h" |
| 28 |
#include "agg_conv_curve.h" |
| 29 |
#include "agg_color_rgba.h" |
| 30 |
#include "agg_renderer_scanline.h" |
| 31 |
#include "agg_bounding_rect.h" |
| 32 |
#include "agg_rasterizer_scanline_aa.h" |
| 33 |
#include "agg_svg_path_tokenizer.h" |
| 34 |
|
| 35 |
namespace agg |
| 36 |
{ |
| 37 |
namespace svg |
| 38 |
{ |
| 39 |
template<class VertexSource> class conv_count |
| 40 |
{ |
| 41 |
public: |
| 42 |
conv_count(VertexSource& vs) : m_source(&vs), m_count(0) {} |
| 43 |
|
| 44 |
void count(unsigned n) { m_count = n; } |
| 45 |
unsigned count() const { return m_count; } |
| 46 |
|
| 47 |
void rewind(unsigned path_id) { m_source->rewind(path_id); } |
| 48 |
unsigned vertex(double* x, double* y) |
| 49 |
{ |
| 50 |
++m_count; |
| 51 |
return m_source->vertex(x, y); |
| 52 |
} |
| 53 |
|
| 54 |
private: |
| 55 |
VertexSource* m_source; |
| 56 |
unsigned m_count; |
| 57 |
}; |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
//============================================================================ |
| 63 |
// Basic path attributes |
| 64 |
struct path_attributes |
| 65 |
{ |
| 66 |
unsigned index; |
| 67 |
rgba8 fill_color; |
| 68 |
rgba8 stroke_color; |
| 69 |
bool fill_flag; |
| 70 |
bool stroke_flag; |
| 71 |
bool even_odd_flag; |
| 72 |
line_join_e line_join; |
| 73 |
line_cap_e line_cap; |
| 74 |
double miter_limit; |
| 75 |
double stroke_width; |
| 76 |
trans_affine transform; |
| 77 |
|
| 78 |
// Empty constructor |
| 79 |
path_attributes() : |
| 80 |
index(0), |
| 81 |
fill_color(rgba(0,0,0)), |
| 82 |
stroke_color(rgba(0,0,0)), |
| 83 |
fill_flag(true), |
| 84 |
stroke_flag(false), |
| 85 |
even_odd_flag(false), |
| 86 |
line_join(miter_join), |
| 87 |
line_cap(butt_cap), |
| 88 |
miter_limit(4.0), |
| 89 |
stroke_width(1.0), |
| 90 |
transform() |
| 91 |
{ |
| 92 |
} |
| 93 |
|
| 94 |
// Copy constructor |
| 95 |
path_attributes(const path_attributes& attr) : |
| 96 |
index(attr.index), |
| 97 |
fill_color(attr.fill_color), |
| 98 |
stroke_color(attr.stroke_color), |
| 99 |
fill_flag(attr.fill_flag), |
| 100 |
stroke_flag(attr.stroke_flag), |
| 101 |
even_odd_flag(attr.even_odd_flag), |
| 102 |
line_join(attr.line_join), |
| 103 |
line_cap(attr.line_cap), |
| 104 |
miter_limit(attr.miter_limit), |
| 105 |
stroke_width(attr.stroke_width), |
| 106 |
transform(attr.transform) |
| 107 |
{ |
| 108 |
} |
| 109 |
|
| 110 |
// Copy constructor with new index value |
| 111 |
path_attributes(const path_attributes& attr, unsigned idx) : |
| 112 |
index(idx), |
| 113 |
fill_color(attr.fill_color), |
| 114 |
stroke_color(attr.stroke_color), |
| 115 |
fill_flag(attr.fill_flag), |
| 116 |
stroke_flag(attr.stroke_flag), |
| 117 |
even_odd_flag(attr.even_odd_flag), |
| 118 |
line_join(attr.line_join), |
| 119 |
line_cap(attr.line_cap), |
| 120 |
miter_limit(attr.miter_limit), |
| 121 |
stroke_width(attr.stroke_width), |
| 122 |
transform(attr.transform) |
| 123 |
{ |
| 124 |
} |
| 125 |
}; |
| 126 |
|
| 127 |
|
| 128 |
//============================================================================ |
| 129 |
// Path container and renderer. |
| 130 |
class path_renderer |
| 131 |
{ |
| 132 |
public: |
| 133 |
typedef pod_bvector<path_attributes> attr_storage; |
| 134 |
|
| 135 |
typedef conv_curve<path_storage> curved; |
| 136 |
typedef conv_count<curved> curved_count; |
| 137 |
|
| 138 |
typedef conv_stroke<curved_count> curved_stroked; |
| 139 |
typedef conv_transform<curved_stroked> curved_stroked_trans; |
| 140 |
|
| 141 |
typedef conv_transform<curved_count> curved_trans; |
| 142 |
typedef conv_contour<curved_trans> curved_trans_contour; |
| 143 |
|
| 144 |
path_renderer(); |
| 145 |
|
| 146 |
void remove_all(); |
| 147 |
|
| 148 |
// Use these functions as follows: |
| 149 |
// begin_path() when the XML tag <path> comes ("start_element" handler) |
| 150 |
// parse_path() on "d=" tag attribute |
| 151 |
// end_path() when parsing of the entire tag is done. |
| 152 |
void begin_path(); |
| 153 |
void parse_path(path_tokenizer& tok); |
| 154 |
void end_path(); |
| 155 |
|
| 156 |
// The following functions are essentially a "reflection" of |
| 157 |
// the respective SVG path commands. |
| 158 |
void move_to(double x, double y, bool rel=false); // M, m |
| 159 |
void line_to(double x, double y, bool rel=false); // L, l |
| 160 |
void hline_to(double x, bool rel=false); // H, h |
| 161 |
void vline_to(double y, bool rel=false); // V, v |
| 162 |
void curve3(double x1, double y1, // Q, q |
| 163 |
double x, double y, bool rel=false); |
| 164 |
void curve3(double x, double y, bool rel=false); // T, t |
| 165 |
void curve4(double x1, double y1, // C, c |
| 166 |
double x2, double y2, |
| 167 |
double x, double y, bool rel=false); |
| 168 |
void curve4(double x2, double y2, // S, s |
| 169 |
double x, double y, bool rel=false); |
| 170 |
void arc(double rx, double ry, double angle, // A, a |
| 171 |
bool large_arc_flag, bool sweep_flag, double x, double y, |
| 172 |
bool rel=false); |
| 173 |
void close_subpath(); // Z, z |
| 174 |
void align_subpath(unsigned start_idx); |
| 175 |
|
| 176 |
// template<class VertexSource> |
| 177 |
// void add_path(VertexSource& vs, |
| 178 |
// unsigned path_id = 0, |
| 179 |
// bool solid_path = true) |
| 180 |
// { |
| 181 |
// m_storage.add_path(vs, path_id, solid_path); |
| 182 |
// } |
| 183 |
|
| 184 |
|
| 185 |
unsigned vertex_count() const { return m_curved_count.count(); } |
| 186 |
|
| 187 |
|
| 188 |
// Call these functions on <g> tag (start_element, end_element respectively) |
| 189 |
void push_attr(); |
| 190 |
void pop_attr(); |
| 191 |
|
| 192 |
// Attribute setting functions. |
| 193 |
void fill(const rgba8& f); |
| 194 |
void stroke(const rgba8& s); |
| 195 |
void even_odd(bool flag); |
| 196 |
void stroke_width(double w); |
| 197 |
void fill_none(); |
| 198 |
void stroke_none(); |
| 199 |
void fill_opacity(double op); |
| 200 |
void stroke_opacity(double op); |
| 201 |
void line_join(line_join_e join); |
| 202 |
void line_cap(line_cap_e cap); |
| 203 |
void miter_limit(double ml); |
| 204 |
trans_affine& transform(); |
| 205 |
|
| 206 |
// Make all polygons CCW-oriented |
| 207 |
void arrange_orientations() |
| 208 |
{ |
| 209 |
m_storage.arrange_orientations_all_paths(path_flags_ccw); |
| 210 |
} |
| 211 |
|
| 212 |
// Expand all polygons |
| 213 |
void expand(double value) |
| 214 |
{ |
| 215 |
m_curved_trans_contour.width(value); |
| 216 |
} |
| 217 |
|
| 218 |
unsigned operator [](unsigned idx) |
| 219 |
{ |
| 220 |
m_transform = m_attr_storage[idx].transform; |
| 221 |
return m_attr_storage[idx].index; |
| 222 |
} |
| 223 |
|
| 224 |
void bounding_rect(double* x1, double* y1, double* x2, double* y2) |
| 225 |
{ |
| 226 |
agg::conv_transform<agg::path_storage> trans(m_storage, m_transform); |
| 227 |
agg::bounding_rect(trans, *this, 0, m_attr_storage.size(), x1, y1, x2, y2); |
| 228 |
} |
| 229 |
|
| 230 |
// Rendering. One can specify two additional parameters: |
| 231 |
// trans_affine and opacity. They can be used to transform the whole |
| 232 |
// image and/or to make it translucent. |
| 233 |
template<class Rasterizer, class Scanline, class Renderer> |
| 234 |
void render(Rasterizer& ras, |
| 235 |
Scanline& sl, |
| 236 |
Renderer& ren, |
| 237 |
const trans_affine& mtx, |
| 238 |
const rect_i& cb, |
| 239 |
double opacity=1.0) |
| 240 |
{ |
| 241 |
unsigned i; |
| 242 |
|
| 243 |
ras.clip_box(cb.x1, cb.y1, cb.x2, cb.y2); |
| 244 |
m_curved_count.count(0); |
| 245 |
|
| 246 |
for(i = 0; i < m_attr_storage.size(); i++) |
| 247 |
{ |
| 248 |
const path_attributes& attr = m_attr_storage[i]; |
| 249 |
m_transform = attr.transform; |
| 250 |
m_transform *= mtx; |
| 251 |
double scl = m_transform.scale(); |
| 252 |
//m_curved.approximation_method(curve_inc); |
| 253 |
m_curved.approximation_scale(scl); |
| 254 |
m_curved.angle_tolerance(0.0); |
| 255 |
|
| 256 |
rgba8 color; |
| 257 |
|
| 258 |
if(attr.fill_flag) |
| 259 |
{ |
| 260 |
ras.reset(); |
| 261 |
ras.filling_rule(attr.even_odd_flag ? fill_even_odd : fill_non_zero); |
| 262 |
if(fabs(m_curved_trans_contour.width()) < 0.0001) |
| 263 |
{ |
| 264 |
ras.add_path(m_curved_trans, attr.index); |
| 265 |
} |
| 266 |
else |
| 267 |
{ |
| 268 |
m_curved_trans_contour.miter_limit(attr.miter_limit); |
| 269 |
ras.add_path(m_curved_trans_contour, attr.index); |
| 270 |
} |
| 271 |
|
| 272 |
color = attr.fill_color; |
| 273 |
color.opacity(color.opacity() * opacity); |
| 274 |
ren.color(color); |
| 275 |
agg::render_scanlines(ras, sl, ren); |
| 276 |
} |
| 277 |
|
| 278 |
if(attr.stroke_flag) |
| 279 |
{ |
| 280 |
m_curved_stroked.width(attr.stroke_width); |
| 281 |
//m_curved_stroked.line_join((attr.line_join == miter_join) ? miter_join_round : attr.line_join); |
| 282 |
m_curved_stroked.line_join(attr.line_join); |
| 283 |
m_curved_stroked.line_cap(attr.line_cap); |
| 284 |
m_curved_stroked.miter_limit(attr.miter_limit); |
| 285 |
m_curved_stroked.inner_join(inner_round); |
| 286 |
m_curved_stroked.approximation_scale(scl); |
| 287 |
|
| 288 |
// If the *visual* line width is considerable we |
| 289 |
// turn on processing of curve cusps. |
| 290 |
//--------------------- |
| 291 |
if(attr.stroke_width * scl > 1.0) |
| 292 |
{ |
| 293 |
m_curved.angle_tolerance(0.2); |
| 294 |
} |
| 295 |
ras.reset(); |
| 296 |
ras.filling_rule(fill_non_zero); |
| 297 |
ras.add_path(m_curved_stroked_trans, attr.index); |
| 298 |
color = attr.stroke_color; |
| 299 |
color.opacity(color.opacity() * opacity); |
| 300 |
ren.color(color); |
| 301 |
agg::render_scanlines(ras, sl, ren); |
| 302 |
} |
| 303 |
} |
| 304 |
} |
| 305 |
|
| 306 |
private: |
| 307 |
path_attributes& cur_attr(); |
| 308 |
|
| 309 |
path_storage m_storage; |
| 310 |
attr_storage m_attr_storage; |
| 311 |
attr_storage m_attr_stack; |
| 312 |
trans_affine m_transform; |
| 313 |
|
| 314 |
curved m_curved; |
| 315 |
curved_count m_curved_count; |
| 316 |
|
| 317 |
curved_stroked m_curved_stroked; |
| 318 |
curved_stroked_trans m_curved_stroked_trans; |
| 319 |
|
| 320 |
curved_trans m_curved_trans; |
| 321 |
curved_trans_contour m_curved_trans_contour; |
| 322 |
}; |
| 323 |
|
| 324 |
} |
| 325 |
} |
| 326 |
|
| 327 |
#endif |