+ add stroke-dashoffset property

+ replace <CssParameter name='xxx'>yyy</CssParameter>
   element with 'xxx'=yyy attribute
This commit is contained in:
Artem Pavlenko 2010-04-09 18:46:25 +00:00
parent 1acc288349
commit 5fcc311764
4 changed files with 769 additions and 791 deletions

View file

@ -31,46 +31,47 @@
namespace mapnik
{
using std::pair;
using std::vector;
typedef vector<pair<float,float> > dash_array;
using std::pair;
using std::vector;
typedef vector<pair<double,double> > dash_array;
// if you add new tokens, don't forget to add them to the corresponding
// string array in the cpp file.
enum line_cap_enum
{
// if you add new tokens, don't forget to add them to the corresponding
// string array in the cpp file.
enum line_cap_enum
{
BUTT_CAP,
SQUARE_CAP,
ROUND_CAP,
line_cap_enum_MAX
};
};
DEFINE_ENUM( line_cap_e, line_cap_enum );
DEFINE_ENUM( line_cap_e, line_cap_enum );
// if you add new tokens, don't forget to add them to the corresponding
// string array in the cpp file.
enum line_join_enum
{
// if you add new tokens, don't forget to add them to the corresponding
// string array in the cpp file.
enum line_join_enum
{
MITER_JOIN,
MITER_REVERT_JOIN,
ROUND_JOIN,
BEVEL_JOIN,
line_join_enum_MAX
};
};
DEFINE_ENUM( line_join_e, line_join_enum );
DEFINE_ENUM( line_join_e, line_join_enum );
class MAPNIK_DECL stroke
{
class MAPNIK_DECL stroke
{
color c_;
float width_;
float opacity_; // 0.0 - 1.0
double width_;
double opacity_; // 0.0 - 1.0
line_cap_e line_cap_;
line_join_e line_join_;
dash_array dash_;
public:
double dash_offset_;
public:
explicit stroke();
stroke(color const& c, float width=1.0);
stroke(color const& c, double width=1.0);
stroke(stroke const& other);
stroke& operator=(const stroke& rhs);
@ -78,11 +79,11 @@ namespace mapnik
color const& get_color() const;
float get_width() const;
void set_width(float w);
void set_opacity(float opacity);
double get_width() const;
void set_width(double w);
void set_opacity(double opacity);
float get_opacity() const;
double get_opacity() const;
void set_line_cap(line_cap_e line_cap);
line_cap_e get_line_cap() const;
@ -90,14 +91,16 @@ namespace mapnik
void set_line_join(line_join_e line_join);
line_join_e get_line_join() const;
void add_dash(float dash,float gap);
void add_dash(double dash,double gap);
bool has_dash() const;
void set_dash_offset(double offset);
double dash_offset() const;
dash_array const& get_dash_array() const;
private:
private:
void swap(const stroke& other) throw();
};
};
}
#endif //STROKE_HPP

View file

@ -1258,59 +1258,43 @@ namespace mapnik
try
{
stroke strk;
ptree::const_iterator cssIter = sym.begin();
ptree::const_iterator endCss = sym.end();
for(; cssIter != endCss; ++cssIter)
// stroke color
optional<color> c= get_opt_attr<color>(sym, "stroke");
if (c) strk.set_color(*c);
// stroke-width
optional<double> width = get_opt_attr<double>(sym, "stroke-width");
if (width) strk.set_width(*width);
// stroke-opacity
optional<double> opacity = get_opt_attr<double>(sym, "stroke-opacity");
if (opacity) strk.set_opacity(*opacity);
// stroke-linejoin
optional<line_join_e> line_join = get_opt_attr<line_join_e>(sym, "stroke-linejoin");
if (line_join) strk.set_line_join(*line_join);
// stroke-linecap
optional<line_cap_e> line_cap = get_opt_attr<line_cap_e>(sym, "stroke-linecap");
if (line_cap) strk.set_line_cap(*line_cap);
// stroke-dashaffset
optional<double> offset = get_opt_attr<double>(sym, "stroke-dashoffet");
if (offset) strk.set_dash_offset(*offset);
// stroke-dasharray
optional<string> str = get_opt_attr<string>(sym,"stroke-dasharray");
if (str)
{
ptree::value_type const& css_tag = *cssIter;
ptree const & css = cssIter->second;
if (css_tag.first == "CssParameter")
{
std::string css_name = get_attr<string>(css, "name");
if (css_name == "stroke")
{
color c = get_css<color>(css, css_name);
strk.set_color(c);
}
else if (css_name == "stroke-width")
{
float width = get_css<float>(css, css_name);
strk.set_width(width);
}
else if (css_name == "stroke-opacity")
{
float opacity = get_css<float>(css, css_name);
strk.set_opacity(opacity);
}
else if (css_name == "stroke-linejoin")
{
line_join_e line_join = get_css<line_join_e>(css, css_name);
strk.set_line_join( line_join );
}
else if (css_name == "stroke-linecap")
{
line_cap_e line_cap = get_css<line_cap_e>(css, css_name);
strk.set_line_cap( line_cap );
}
else if (css_name == "stroke-dasharray")
{
tokenizer<> tok ( css.data() );
std::vector<float> dash_array;
tokenizer<> tok (*str);
std::vector<double> dash_array;
tokenizer<>::iterator itr = tok.begin();
for (; itr != tok.end(); ++itr)
{
try
{
float f = boost::lexical_cast<float>(*itr);
double f = boost::lexical_cast<double>(*itr);
dash_array.push_back(f);
}
catch ( boost::bad_lexical_cast &)
{
throw config_error(std::string("Failed to parse CSS ") +
"parameter '" + css_name + "'. Expected a " +
"list of floats but got '" + css.data() + "'");
throw config_error(std::string("Failed to parse dasharray ") +
"'. Expected a " +
"list of floats but got '" + (*str) + "'");
}
}
if (dash_array.size())
@ -1323,7 +1307,7 @@ namespace mapnik
dash_array.push_back(dash_array[i]);
}
}
std::vector<float>::const_iterator pos = dash_array.begin();
std::vector<double>::const_iterator pos = dash_array.begin();
while (pos != dash_array.end())
{
strk.add_dash(*pos,*(pos + 1));
@ -1331,19 +1315,6 @@ namespace mapnik
}
}
}
else
{
throw config_error(std::string("Failed to parse unknown CSS ") +
"parameter '" + css_name + "'");
}
}
else if (css_tag.first != "<xmlcomment>" &&
css_tag.first != "<xmlattr>" )
{
throw config_error(std::string("Unknown child node. ") +
"Expected 'CssParameter' but got '" + css_tag.first + "'");
}
}
rule.append(line_symbolizer(strk));
}
catch (const config_error & ex)

View file

@ -41,13 +41,13 @@
namespace mapnik
{
using boost::property_tree::ptree;
using boost::optional;
using boost::property_tree::ptree;
using boost::optional;
void serialize_raster_colorizer(ptree & sym_node,
void serialize_raster_colorizer(ptree & sym_node,
raster_colorizer_ptr const& colorizer,
bool explicit_defaults)
{
{
ptree & col_node = sym_node.push_back(
ptree::value_type("RasterColorizer", ptree() ))->second;
@ -65,11 +65,11 @@ namespace mapnik
}
}
}
}
class serialize_symbolizer : public boost::static_visitor<>
{
public:
class serialize_symbolizer : public boost::static_visitor<>
{
public:
serialize_symbolizer( ptree & r , bool explicit_defaults):
rule_(r),
explicit_defaults_(explicit_defaults) {}
@ -129,6 +129,10 @@ namespace mapnik
}
set_css( sym_node, "stroke-dasharray", os.str() );
}
if ( strk.dash_offset() != dfl.dash_offset() || explicit_defaults_ )
{
set_css( sym_node, "stroke-dashoffset", strk.dash_offset());
}
}
void operator () ( const line_pattern_symbolizer & sym )
@ -327,7 +331,7 @@ namespace mapnik
}
private:
private:
serialize_symbolizer();
void add_image_attributes(ptree & node, const symbolizer_with_image & sym)
{
@ -452,10 +456,10 @@ namespace mapnik
}
ptree & rule_;
bool explicit_defaults_;
};
};
void serialize_rule( ptree & style_node, const rule_type & rule, bool explicit_defaults)
{
void serialize_rule( ptree & style_node, const rule_type & rule, bool explicit_defaults)
{
ptree & rule_node = style_node.push_back(
ptree::value_type("Rule", ptree() ))->second;
@ -506,10 +510,10 @@ namespace mapnik
rule_type::symbolizers::const_iterator end = rule.get_symbolizers().end();
serialize_symbolizer serializer( rule_node, explicit_defaults);
std::for_each( begin, end , boost::apply_visitor( serializer ));
}
}
void serialize_style( ptree & map_node, Map::const_style_iterator style_it, bool explicit_defaults )
{
void serialize_style( ptree & map_node, Map::const_style_iterator style_it, bool explicit_defaults )
{
const feature_type_style & style = style_it->second;
const std::string & name = style_it->first;
@ -525,10 +529,10 @@ namespace mapnik
serialize_rule( style_node, * it , explicit_defaults);
}
}
}
void serialize_fontset( ptree & map_node, Map::const_fontset_iterator fontset_it )
{
void serialize_fontset( ptree & map_node, Map::const_fontset_iterator fontset_it )
{
const font_set & fontset = fontset_it->second;
const std::string & name = fontset_it->first;
@ -546,10 +550,10 @@ namespace mapnik
set_attr(font_node, "face_name", *it);
}
}
}
void serialize_datasource( ptree & layer_node, datasource_ptr datasource)
{
void serialize_datasource( ptree & layer_node, datasource_ptr datasource)
{
ptree & datasource_node = layer_node.push_back(
ptree::value_type("Datasource", ptree()))->second;
@ -564,10 +568,10 @@ namespace mapnik
param_node.put_value( it->second );
}
}
}
void serialize_layer( ptree & map_node, const layer & layer, bool explicit_defaults )
{
void serialize_layer( ptree & map_node, const layer & layer, bool explicit_defaults )
{
ptree & layer_node = map_node.push_back(
ptree::value_type("Layer", ptree()))->second;
if ( layer.name() != "" )
@ -629,10 +633,10 @@ namespace mapnik
{
serialize_datasource( layer_node, datasource );
}
}
}
void serialize_map(ptree & pt, Map const & map, bool explicit_defaults)
{
void serialize_map(ptree & pt, Map const & map, bool explicit_defaults)
{
ptree & map_node = pt.push_back(ptree::value_type("Map", ptree() ))->second;
@ -671,22 +675,22 @@ namespace mapnik
{
serialize_layer( map_node, layers[i], explicit_defaults );
}
}
}
void save_map(Map const & map, std::string const& filename, bool explicit_defaults)
{
void save_map(Map const & map, std::string const& filename, bool explicit_defaults)
{
ptree pt;
serialize_map(pt,map,explicit_defaults);
write_xml(filename,pt,std::locale(),boost::property_tree::xml_writer_make_settings(' ',4));
}
}
std::string save_map_to_string(Map const & map, bool explicit_defaults)
{
std::string save_map_to_string(Map const & map, bool explicit_defaults)
{
ptree pt;
serialize_map(pt,map,explicit_defaults);
std::ostringstream ss;
write_xml(ss,pt,boost::property_tree::xml_writer_make_settings(' ',4));
return ss.str();
}
}
}