2010-05-28 16:34:34 +00:00
|
|
|
/*****************************************************************************
|
|
|
|
*
|
|
|
|
* This file is part of Mapnik (c++ mapping toolkit)
|
|
|
|
*
|
2011-10-23 13:04:25 +00:00
|
|
|
* Copyright (C) 2011 Artem Pavlenko
|
2010-05-28 16:34:34 +00:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*
|
|
|
|
*****************************************************************************/
|
|
|
|
|
2012-04-08 00:20:56 +00:00
|
|
|
#include <mapnik/debug.hpp>
|
2010-05-28 16:34:34 +00:00
|
|
|
#include <mapnik/color_factory.hpp>
|
2010-05-28 16:34:58 +00:00
|
|
|
#include <mapnik/svg/svg_parser.hpp>
|
|
|
|
#include <mapnik/svg/svg_path_parser.hpp>
|
2012-03-11 22:24:28 +00:00
|
|
|
#include <mapnik/config_error.hpp>
|
2010-05-28 16:34:34 +00:00
|
|
|
|
2010-06-13 13:45:59 +00:00
|
|
|
#include "agg_ellipse.h"
|
|
|
|
#include "agg_rounded_rect.h"
|
2011-01-26 01:18:40 +00:00
|
|
|
#include "agg_span_gradient.h"
|
2010-06-13 13:45:59 +00:00
|
|
|
|
2010-05-28 16:34:34 +00:00
|
|
|
#include <boost/spirit/include/qi.hpp>
|
2011-01-26 01:18:40 +00:00
|
|
|
#include <boost/spirit/include/phoenix_core.hpp>
|
|
|
|
#include <boost/spirit/include/phoenix_operator.hpp>
|
2010-05-28 16:34:34 +00:00
|
|
|
#include <boost/fusion/include/std_pair.hpp>
|
|
|
|
#include <boost/foreach.hpp>
|
2011-01-26 01:18:40 +00:00
|
|
|
#include <boost/algorithm/string/predicate.hpp>
|
2010-05-28 16:34:34 +00:00
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <stdexcept>
|
|
|
|
#include <vector>
|
2013-01-15 21:32:09 +00:00
|
|
|
#include <cstring>
|
2010-05-28 16:34:34 +00:00
|
|
|
|
2013-03-13 01:09:59 +00:00
|
|
|
// xml2
|
|
|
|
#include <libxml/xmlreader.h>
|
|
|
|
|
|
|
|
|
2010-05-28 16:34:34 +00:00
|
|
|
namespace mapnik { namespace svg {
|
|
|
|
|
2013-03-13 01:09:59 +00:00
|
|
|
bool parse_reader(svg_parser & parser,xmlTextReaderPtr reader);
|
|
|
|
void process_node(svg_parser & parser,xmlTextReaderPtr reader);
|
|
|
|
void start_element(svg_parser & parser,xmlTextReaderPtr reader);
|
|
|
|
void end_element(svg_parser & parser,xmlTextReaderPtr reader);
|
|
|
|
void parse_path(svg_parser & parser,xmlTextReaderPtr reader);
|
|
|
|
void parse_dimensions(svg_parser & parser,xmlTextReaderPtr reader);
|
|
|
|
void parse_polygon(svg_parser & parser,xmlTextReaderPtr reader);
|
|
|
|
void parse_polyline(svg_parser & parser,xmlTextReaderPtr reader);
|
|
|
|
void parse_line(svg_parser & parser,xmlTextReaderPtr reader);
|
|
|
|
void parse_rect(svg_parser & parser,xmlTextReaderPtr reader);
|
|
|
|
void parse_circle(svg_parser & parser,xmlTextReaderPtr reader);
|
|
|
|
void parse_ellipse(svg_parser & parser,xmlTextReaderPtr reader);
|
|
|
|
void parse_linear_gradient(svg_parser & parser,xmlTextReaderPtr reader);
|
|
|
|
void parse_radial_gradient(svg_parser & parser,xmlTextReaderPtr reader);
|
|
|
|
bool parse_common_gradient(svg_parser & parser,xmlTextReaderPtr reader);
|
|
|
|
void parse_gradient_stop(svg_parser & parser,xmlTextReaderPtr reader);
|
|
|
|
void parse_pattern(svg_parser & parser,xmlTextReaderPtr reader);
|
|
|
|
void parse_attr(svg_parser & parser,xmlTextReaderPtr reader);
|
|
|
|
void parse_attr(svg_parser & parser,const xmlChar * name, const xmlChar * value );
|
2010-05-28 16:34:34 +00:00
|
|
|
|
2012-04-11 13:00:34 +00:00
|
|
|
typedef std::vector<std::pair<double, agg::rgba8> > color_lookup_type;
|
2011-01-26 01:18:40 +00:00
|
|
|
|
2012-04-11 13:00:34 +00:00
|
|
|
namespace qi = boost::spirit::qi;
|
2010-05-28 16:34:34 +00:00
|
|
|
|
2012-04-11 13:00:34 +00:00
|
|
|
typedef std::vector<std::pair<std::string, std::string> > pairs_type;
|
2010-05-28 16:34:34 +00:00
|
|
|
|
2012-04-11 13:00:34 +00:00
|
|
|
template <typename Iterator,typename SkipType>
|
|
|
|
struct key_value_sequence_ordered
|
|
|
|
: qi::grammar<Iterator, pairs_type(), SkipType>
|
|
|
|
{
|
|
|
|
key_value_sequence_ordered()
|
|
|
|
: key_value_sequence_ordered::base_type(query)
|
2010-06-02 11:03:30 +00:00
|
|
|
{
|
2012-04-11 13:00:34 +00:00
|
|
|
query = pair >> *( qi::lit(';') >> pair);
|
|
|
|
pair = key >> -(':' >> value);
|
|
|
|
key = qi::char_("a-zA-Z_") >> *qi::char_("a-zA-Z_0-9-");
|
|
|
|
value = +(qi::char_ - qi::lit(';'));
|
|
|
|
}
|
2012-02-02 01:53:35 +00:00
|
|
|
|
2012-04-11 13:00:34 +00:00
|
|
|
qi::rule<Iterator, pairs_type(), SkipType> query;
|
|
|
|
qi::rule<Iterator, std::pair<std::string, std::string>(), SkipType> pair;
|
|
|
|
qi::rule<Iterator, std::string(), SkipType> key, value;
|
|
|
|
};
|
2010-05-28 16:34:34 +00:00
|
|
|
|
|
|
|
agg::rgba8 parse_color(const char* str)
|
|
|
|
{
|
|
|
|
mapnik::color c(100,100,100);
|
|
|
|
try
|
|
|
|
{
|
2012-08-29 17:44:04 +00:00
|
|
|
c = mapnik::parse_color(str);
|
2010-05-28 16:34:34 +00:00
|
|
|
}
|
2013-03-23 00:58:33 +00:00
|
|
|
catch (mapnik::config_error const& ex)
|
2010-05-28 16:34:34 +00:00
|
|
|
{
|
2012-04-09 22:51:04 +00:00
|
|
|
MAPNIK_LOG_ERROR(svg_parser) << ex.what();
|
2010-05-28 16:34:34 +00:00
|
|
|
}
|
|
|
|
return agg::rgba8(c.red(), c.green(), c.blue(), c.alpha());
|
|
|
|
}
|
|
|
|
|
|
|
|
double parse_double(const char* str)
|
|
|
|
{
|
|
|
|
using namespace boost::spirit::qi;
|
|
|
|
double val = 0.0;
|
2013-01-15 21:32:09 +00:00
|
|
|
parse(str, str + std::strlen(str),double_,val);
|
2012-02-02 01:53:35 +00:00
|
|
|
return val;
|
2010-05-28 16:34:34 +00:00
|
|
|
}
|
|
|
|
|
2011-01-26 01:18:40 +00:00
|
|
|
/*
|
|
|
|
* parse a double that might end with a %
|
|
|
|
* if it does then set the ref bool true and divide the result by 100
|
|
|
|
*/
|
|
|
|
double parse_double_optional_percent(const char* str, bool &percent)
|
|
|
|
{
|
|
|
|
using namespace boost::spirit::qi;
|
|
|
|
using boost::phoenix::ref;
|
2011-07-09 00:28:41 +00:00
|
|
|
using qi::_1;
|
2011-01-26 01:18:40 +00:00
|
|
|
|
|
|
|
double val = 0.0;
|
|
|
|
char unit='\0';
|
2013-01-15 21:32:09 +00:00
|
|
|
parse(str, str + std::strlen(str),double_[ref(val)=_1] >> *char_('%')[ref(unit)=_1]);
|
2011-01-26 01:18:40 +00:00
|
|
|
if (unit =='%')
|
|
|
|
{
|
|
|
|
percent = true;
|
|
|
|
val/=100.0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
percent = false;
|
|
|
|
}
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
2010-05-28 16:34:34 +00:00
|
|
|
bool parse_style (const char* str, pairs_type & v)
|
|
|
|
{
|
|
|
|
using namespace boost::spirit::qi;
|
|
|
|
typedef boost::spirit::ascii::space_type skip_type;
|
|
|
|
key_value_sequence_ordered<const char*, skip_type> kv_parser;
|
2013-01-15 21:32:09 +00:00
|
|
|
return phrase_parse(str, str + std::strlen(str), kv_parser, skip_type(), v);
|
2010-05-28 16:34:34 +00:00
|
|
|
}
|
|
|
|
|
2013-03-13 01:09:59 +00:00
|
|
|
bool parse_reader(svg_parser & parser, xmlTextReaderPtr reader)
|
2012-07-10 15:59:35 +00:00
|
|
|
{
|
|
|
|
int ret = xmlTextReaderRead(reader);
|
|
|
|
try {
|
|
|
|
while (ret == 1)
|
2010-06-02 11:03:30 +00:00
|
|
|
{
|
2013-03-13 01:09:59 +00:00
|
|
|
process_node(parser,reader);
|
2012-07-10 15:59:35 +00:00
|
|
|
ret = xmlTextReaderRead(reader);
|
2010-06-02 11:03:30 +00:00
|
|
|
}
|
2012-07-10 15:59:35 +00:00
|
|
|
}
|
|
|
|
catch (std::exception const& ex)
|
|
|
|
{
|
2010-06-02 11:03:30 +00:00
|
|
|
xmlFreeTextReader(reader);
|
2012-07-10 15:59:35 +00:00
|
|
|
throw;
|
|
|
|
}
|
|
|
|
xmlFreeTextReader(reader);
|
|
|
|
if (ret != 0)
|
|
|
|
{
|
|
|
|
// parsing failure
|
|
|
|
return false;
|
2010-05-28 16:34:34 +00:00
|
|
|
}
|
2012-07-10 15:59:35 +00:00
|
|
|
return true;
|
2010-05-28 16:34:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-03-13 01:09:59 +00:00
|
|
|
void start_element(svg_parser & parser, xmlTextReaderPtr reader)
|
2010-05-28 16:34:34 +00:00
|
|
|
{
|
|
|
|
const xmlChar *name;
|
2012-02-02 01:53:35 +00:00
|
|
|
name = xmlTextReaderConstName(reader);
|
|
|
|
|
2012-04-11 13:00:34 +00:00
|
|
|
if (xmlStrEqual(name, BAD_CAST "defs"))
|
2010-06-11 14:09:05 +00:00
|
|
|
{
|
|
|
|
if (xmlTextReaderIsEmptyElement(reader) == 0)
|
2013-03-13 01:09:59 +00:00
|
|
|
parser.is_defs_ = true;
|
2010-06-11 14:09:05 +00:00
|
|
|
}
|
2011-01-26 01:18:40 +00:00
|
|
|
// the gradient tags *should* be in defs, but illustrator seems not to put them in there so
|
|
|
|
// accept them anywhere
|
|
|
|
else if (xmlStrEqual(name, BAD_CAST "linearGradient"))
|
|
|
|
{
|
2013-03-13 01:09:59 +00:00
|
|
|
parse_linear_gradient(parser,reader);
|
2011-01-26 01:18:40 +00:00
|
|
|
}
|
|
|
|
else if (xmlStrEqual(name, BAD_CAST "radialGradient"))
|
|
|
|
{
|
2013-03-13 01:09:59 +00:00
|
|
|
parse_radial_gradient(parser,reader);
|
2011-01-26 01:18:40 +00:00
|
|
|
}
|
|
|
|
else if (xmlStrEqual(name, BAD_CAST "stop"))
|
|
|
|
{
|
2013-03-13 01:09:59 +00:00
|
|
|
parse_gradient_stop(parser,reader);
|
2011-01-26 01:18:40 +00:00
|
|
|
}
|
2013-03-13 01:09:59 +00:00
|
|
|
if ( !parser.is_defs_ )
|
2011-01-26 01:18:40 +00:00
|
|
|
{
|
2012-04-11 13:00:34 +00:00
|
|
|
|
|
|
|
if (xmlStrEqual(name, BAD_CAST "g"))
|
|
|
|
{
|
2013-07-10 21:25:34 +00:00
|
|
|
if (xmlTextReaderIsEmptyElement(reader) == 0)
|
|
|
|
{
|
|
|
|
parser.path_.push_attr();
|
|
|
|
parse_attr(parser,reader);
|
|
|
|
}
|
2012-04-11 13:00:34 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-03-13 01:09:59 +00:00
|
|
|
parser.path_.push_attr();
|
|
|
|
parse_attr(parser,reader);
|
|
|
|
if (parser.path_.display())
|
2012-04-11 13:00:34 +00:00
|
|
|
{
|
|
|
|
if (xmlStrEqual(name, BAD_CAST "path"))
|
|
|
|
{
|
2013-03-13 01:09:59 +00:00
|
|
|
parse_path(parser,reader);
|
2012-04-11 13:00:34 +00:00
|
|
|
}
|
|
|
|
else if (xmlStrEqual(name, BAD_CAST "polygon") )
|
|
|
|
{
|
2013-03-13 01:09:59 +00:00
|
|
|
parse_polygon(parser,reader);
|
2012-04-11 13:00:34 +00:00
|
|
|
}
|
|
|
|
else if (xmlStrEqual(name, BAD_CAST "polyline"))
|
|
|
|
{
|
2013-03-13 01:09:59 +00:00
|
|
|
parse_polyline(parser,reader);
|
2012-04-11 13:00:34 +00:00
|
|
|
}
|
|
|
|
else if (xmlStrEqual(name, BAD_CAST "line"))
|
|
|
|
{
|
2013-03-13 01:09:59 +00:00
|
|
|
parse_line(parser,reader);
|
2012-04-11 13:00:34 +00:00
|
|
|
}
|
|
|
|
else if (xmlStrEqual(name, BAD_CAST "rect"))
|
|
|
|
{
|
2013-03-13 01:09:59 +00:00
|
|
|
parse_rect(parser,reader);
|
2012-04-11 13:00:34 +00:00
|
|
|
}
|
|
|
|
else if (xmlStrEqual(name, BAD_CAST "circle"))
|
|
|
|
{
|
2013-03-13 01:09:59 +00:00
|
|
|
parse_circle(parser,reader);
|
2012-04-11 13:00:34 +00:00
|
|
|
}
|
|
|
|
else if (xmlStrEqual(name, BAD_CAST "ellipse"))
|
|
|
|
{
|
2013-03-13 01:09:59 +00:00
|
|
|
parse_ellipse(parser,reader);
|
2012-04-11 13:00:34 +00:00
|
|
|
}
|
2013-03-09 01:40:19 +00:00
|
|
|
else if (xmlStrEqual(name, BAD_CAST "svg"))
|
|
|
|
{
|
2013-03-13 01:09:59 +00:00
|
|
|
parse_dimensions(parser,reader);
|
2013-03-09 01:40:19 +00:00
|
|
|
}
|
2012-04-11 13:00:34 +00:00
|
|
|
#ifdef MAPNIK_LOG
|
2012-04-11 13:00:34 +00:00
|
|
|
else if (!xmlStrEqual(name, BAD_CAST "svg"))
|
|
|
|
{
|
2012-04-11 13:00:34 +00:00
|
|
|
MAPNIK_LOG_WARN(svg_parser) << "svg_parser: Unhandled svg element=" << name;
|
2012-04-11 13:00:34 +00:00
|
|
|
}
|
2012-03-22 23:41:44 +00:00
|
|
|
#endif
|
2012-04-11 13:00:34 +00:00
|
|
|
}
|
2013-03-13 01:09:59 +00:00
|
|
|
parser.path_.pop_attr();
|
2012-04-11 13:00:34 +00:00
|
|
|
}
|
|
|
|
}
|
2010-05-28 16:34:34 +00:00
|
|
|
}
|
|
|
|
|
2013-03-13 01:09:59 +00:00
|
|
|
void end_element(svg_parser & parser, xmlTextReaderPtr reader)
|
2010-05-28 16:34:34 +00:00
|
|
|
{
|
|
|
|
const xmlChar *name;
|
2012-02-02 01:53:35 +00:00
|
|
|
name = xmlTextReaderConstName(reader);
|
2012-04-11 13:00:34 +00:00
|
|
|
|
|
|
|
|
2013-03-13 01:09:59 +00:00
|
|
|
if (!parser.is_defs_ && xmlStrEqual(name, BAD_CAST "g"))
|
2010-05-28 16:34:34 +00:00
|
|
|
{
|
2013-03-13 01:09:59 +00:00
|
|
|
parser.path_.pop_attr();
|
2010-05-28 16:34:34 +00:00
|
|
|
}
|
2010-06-11 14:09:05 +00:00
|
|
|
else if (xmlStrEqual(name, BAD_CAST "defs"))
|
|
|
|
{
|
2013-03-13 01:09:59 +00:00
|
|
|
parser.is_defs_ = false;
|
2010-06-11 14:09:05 +00:00
|
|
|
}
|
2011-01-26 01:18:40 +00:00
|
|
|
else if ((xmlStrEqual(name, BAD_CAST "linearGradient")) || (xmlStrEqual(name, BAD_CAST "radialGradient")))
|
|
|
|
{
|
2013-03-13 01:09:59 +00:00
|
|
|
parser.gradient_map_[parser.temporary_gradient_.first] = parser.temporary_gradient_.second;
|
2011-01-26 01:18:40 +00:00
|
|
|
}
|
2012-04-11 13:00:34 +00:00
|
|
|
|
2010-05-28 16:34:34 +00:00
|
|
|
}
|
|
|
|
|
2013-03-13 01:09:59 +00:00
|
|
|
void process_node(svg_parser & parser, xmlTextReaderPtr reader)
|
|
|
|
{
|
|
|
|
int node_type = xmlTextReaderNodeType(reader);
|
|
|
|
switch (node_type)
|
|
|
|
{
|
|
|
|
case 1: //start element
|
|
|
|
start_element(parser,reader);
|
|
|
|
break;
|
|
|
|
case 15:// end element
|
|
|
|
end_element(parser,reader);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void parse_attr(svg_parser & parser, const xmlChar * name, const xmlChar * value )
|
2010-05-28 16:34:34 +00:00
|
|
|
{
|
|
|
|
if (xmlStrEqual(name, BAD_CAST "transform"))
|
|
|
|
{
|
2010-06-02 11:03:30 +00:00
|
|
|
agg::trans_affine tr;
|
2010-06-14 13:18:17 +00:00
|
|
|
mapnik::svg::parse_transform((const char*) value,tr);
|
2013-03-13 01:09:59 +00:00
|
|
|
parser.path_.transform().premultiply(tr);
|
2010-05-28 16:34:34 +00:00
|
|
|
}
|
|
|
|
else if (xmlStrEqual(name, BAD_CAST "fill"))
|
|
|
|
{
|
2010-06-02 11:03:30 +00:00
|
|
|
if (xmlStrEqual(value, BAD_CAST "none"))
|
|
|
|
{
|
2013-03-13 01:09:59 +00:00
|
|
|
parser.path_.fill_none();
|
2010-06-02 11:03:30 +00:00
|
|
|
}
|
2011-01-26 01:18:40 +00:00
|
|
|
else if (boost::starts_with((const char*)value, "url(#"))
|
|
|
|
{
|
|
|
|
// see if we have a known gradient fill
|
|
|
|
std::string id = std::string((const char*)&value[5]);
|
|
|
|
// get rid of the trailing )
|
|
|
|
id.erase(id.end()-1);
|
2013-03-13 01:09:59 +00:00
|
|
|
if (parser.gradient_map_.count(id) > 0)
|
2011-01-26 01:18:40 +00:00
|
|
|
{
|
2013-03-13 01:09:59 +00:00
|
|
|
parser.path_.add_fill_gradient(parser.gradient_map_[id]);
|
2011-01-26 01:18:40 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-04-09 22:51:04 +00:00
|
|
|
MAPNIK_LOG_ERROR(svg_parser) << "Failed to find gradient fill: " << id;
|
2011-01-26 01:18:40 +00:00
|
|
|
}
|
|
|
|
}
|
2010-06-02 11:03:30 +00:00
|
|
|
else
|
|
|
|
{
|
2013-03-13 01:09:59 +00:00
|
|
|
parser.path_.fill(parse_color((const char*) value));
|
2010-06-02 11:03:30 +00:00
|
|
|
}
|
2010-05-28 16:34:34 +00:00
|
|
|
}
|
|
|
|
else if (xmlStrEqual(name, BAD_CAST "fill-opacity"))
|
|
|
|
{
|
2013-03-13 01:09:59 +00:00
|
|
|
parser.path_.fill_opacity(parse_double((const char*) value));
|
2010-05-28 16:34:34 +00:00
|
|
|
}
|
|
|
|
else if (xmlStrEqual(name, BAD_CAST "fill-rule"))
|
|
|
|
{
|
2010-06-02 11:03:30 +00:00
|
|
|
if (xmlStrEqual(value, BAD_CAST "evenodd"))
|
|
|
|
{
|
2013-03-13 01:09:59 +00:00
|
|
|
parser.path_.even_odd(true);
|
2010-06-02 11:03:30 +00:00
|
|
|
}
|
2010-05-28 16:34:34 +00:00
|
|
|
}
|
|
|
|
else if (xmlStrEqual(name, BAD_CAST "stroke"))
|
|
|
|
{
|
2010-06-02 11:03:30 +00:00
|
|
|
if (xmlStrEqual(value, BAD_CAST "none"))
|
|
|
|
{
|
2013-03-13 01:09:59 +00:00
|
|
|
parser.path_.stroke_none();
|
2010-06-02 11:03:30 +00:00
|
|
|
}
|
2011-01-26 01:18:40 +00:00
|
|
|
else if (boost::starts_with((const char*)value, "url(#"))
|
|
|
|
{
|
|
|
|
// see if we have a known gradient fill
|
|
|
|
std::string id = std::string((const char*)&value[5]);
|
|
|
|
// get rid of the trailing )
|
|
|
|
id.erase(id.end()-1);
|
2013-03-13 01:09:59 +00:00
|
|
|
if (parser.gradient_map_.count(id) > 0)
|
2011-01-26 01:18:40 +00:00
|
|
|
{
|
2013-03-13 01:09:59 +00:00
|
|
|
parser.path_.add_stroke_gradient(parser.gradient_map_[id]);
|
2011-01-26 01:18:40 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-04-09 22:51:04 +00:00
|
|
|
MAPNIK_LOG_ERROR(svg_parser) << "Failed to find gradient fill: " << id;
|
2011-01-26 01:18:40 +00:00
|
|
|
}
|
|
|
|
}
|
2010-06-02 11:03:30 +00:00
|
|
|
else
|
|
|
|
{
|
2013-03-13 01:09:59 +00:00
|
|
|
parser.path_.stroke(parse_color((const char*) value));
|
2010-06-02 11:03:30 +00:00
|
|
|
}
|
2010-05-28 16:34:34 +00:00
|
|
|
}
|
|
|
|
else if (xmlStrEqual(name, BAD_CAST "stroke-width"))
|
|
|
|
{
|
2013-03-13 01:09:59 +00:00
|
|
|
parser.path_.stroke_width(parse_double((const char*)value));
|
2010-05-28 16:34:34 +00:00
|
|
|
}
|
|
|
|
else if (xmlStrEqual(name, BAD_CAST "stroke-opacity"))
|
|
|
|
{
|
2013-03-13 01:09:59 +00:00
|
|
|
parser.path_.stroke_opacity(parse_double((const char*)value));
|
2010-05-28 16:34:34 +00:00
|
|
|
}
|
|
|
|
else if(xmlStrEqual(name,BAD_CAST "stroke-width"))
|
|
|
|
{
|
2013-03-13 01:09:59 +00:00
|
|
|
parser.path_.stroke_width(parse_double((const char*) value));
|
2010-05-28 16:34:34 +00:00
|
|
|
}
|
|
|
|
else if(xmlStrEqual(name,BAD_CAST "stroke-linecap"))
|
|
|
|
{
|
2012-02-02 01:53:35 +00:00
|
|
|
if(xmlStrEqual(value,BAD_CAST "butt"))
|
2013-03-13 01:09:59 +00:00
|
|
|
parser.path_.line_cap(agg::butt_cap);
|
2012-02-02 01:53:35 +00:00
|
|
|
else if(xmlStrEqual(value,BAD_CAST "round"))
|
2013-03-13 01:09:59 +00:00
|
|
|
parser.path_.line_cap(agg::round_cap);
|
2012-02-02 01:53:35 +00:00
|
|
|
else if(xmlStrEqual(value,BAD_CAST "square"))
|
2013-03-13 01:09:59 +00:00
|
|
|
parser.path_.line_cap(agg::square_cap);
|
2010-05-28 16:34:34 +00:00
|
|
|
}
|
|
|
|
else if(xmlStrEqual(name,BAD_CAST "stroke-linejoin"))
|
|
|
|
{
|
2012-02-02 01:53:35 +00:00
|
|
|
if(xmlStrEqual(value,BAD_CAST "miter"))
|
2013-03-13 01:09:59 +00:00
|
|
|
parser.path_.line_join(agg::miter_join);
|
2012-02-02 01:53:35 +00:00
|
|
|
else if(xmlStrEqual(value,BAD_CAST "round"))
|
2013-03-13 01:09:59 +00:00
|
|
|
parser.path_.line_join(agg::round_join);
|
2012-02-02 01:53:35 +00:00
|
|
|
else if(xmlStrEqual(value,BAD_CAST "bevel"))
|
2013-03-13 01:09:59 +00:00
|
|
|
parser.path_.line_join(agg::bevel_join);
|
2010-05-28 16:34:34 +00:00
|
|
|
}
|
|
|
|
else if(xmlStrEqual(name,BAD_CAST "stroke-miterlimit"))
|
|
|
|
{
|
2013-03-13 01:09:59 +00:00
|
|
|
parser.path_.miter_limit(parse_double((const char*)value));
|
2010-05-28 16:34:34 +00:00
|
|
|
}
|
2012-02-02 01:53:35 +00:00
|
|
|
|
2010-05-28 16:34:34 +00:00
|
|
|
else if(xmlStrEqual(name, BAD_CAST "opacity"))
|
|
|
|
{
|
2010-06-02 11:03:30 +00:00
|
|
|
double opacity = parse_double((const char*)value);
|
2013-03-13 01:09:59 +00:00
|
|
|
parser.path_.opacity(opacity);
|
2010-05-28 16:34:34 +00:00
|
|
|
}
|
2011-01-26 01:18:40 +00:00
|
|
|
else if (xmlStrEqual(name, BAD_CAST "visibility"))
|
|
|
|
{
|
2013-03-13 01:09:59 +00:00
|
|
|
parser.path_.visibility(!xmlStrEqual(value, BAD_CAST "hidden"));
|
2011-01-26 01:18:40 +00:00
|
|
|
}
|
2012-04-09 11:27:19 +00:00
|
|
|
else if (xmlStrEqual(name, BAD_CAST "display") && xmlStrEqual(value, BAD_CAST "none"))
|
|
|
|
{
|
2013-03-13 01:09:59 +00:00
|
|
|
parser.path_.display(false);
|
2012-04-09 11:27:19 +00:00
|
|
|
}
|
2010-05-28 16:34:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-03-13 01:09:59 +00:00
|
|
|
void parse_attr(svg_parser & parser, xmlTextReaderPtr reader)
|
2010-05-28 16:34:34 +00:00
|
|
|
{
|
|
|
|
const xmlChar *name, *value;
|
2012-04-11 13:00:34 +00:00
|
|
|
|
|
|
|
if (xmlTextReaderMoveToFirstAttribute(reader) == 1)
|
2010-05-28 16:34:34 +00:00
|
|
|
{
|
2012-04-11 13:00:34 +00:00
|
|
|
do
|
2010-06-02 11:03:30 +00:00
|
|
|
{
|
2012-04-11 13:00:34 +00:00
|
|
|
name = xmlTextReaderConstName(reader);
|
|
|
|
value = xmlTextReaderConstValue(reader);
|
|
|
|
|
|
|
|
if (xmlStrEqual(name, BAD_CAST "style"))
|
2010-06-02 11:03:30 +00:00
|
|
|
{
|
2012-04-11 13:00:34 +00:00
|
|
|
typedef std::vector<std::pair<std::string,std::string> > cont_type;
|
|
|
|
typedef cont_type::value_type value_type;
|
|
|
|
cont_type vec;
|
|
|
|
parse_style((const char*)value, vec);
|
|
|
|
BOOST_FOREACH(value_type kv , vec )
|
|
|
|
{
|
2013-03-13 01:09:59 +00:00
|
|
|
parse_attr(parser,BAD_CAST kv.first.c_str(),BAD_CAST kv.second.c_str());
|
2012-04-11 13:00:34 +00:00
|
|
|
}
|
2012-02-02 01:53:35 +00:00
|
|
|
}
|
2012-04-11 13:00:34 +00:00
|
|
|
else
|
|
|
|
{
|
2013-03-13 01:09:59 +00:00
|
|
|
parse_attr(parser,name,value);
|
2012-04-11 13:00:34 +00:00
|
|
|
}
|
|
|
|
} while(xmlTextReaderMoveToNextAttribute(reader) == 1);
|
2010-05-28 16:34:34 +00:00
|
|
|
}
|
2012-04-11 13:00:34 +00:00
|
|
|
xmlTextReaderMoveToElement(reader);
|
2010-05-28 16:34:34 +00:00
|
|
|
}
|
2013-03-09 01:40:19 +00:00
|
|
|
|
2013-03-13 01:09:59 +00:00
|
|
|
void parse_dimensions(svg_parser & parser, xmlTextReaderPtr reader)
|
2013-03-09 01:40:19 +00:00
|
|
|
{
|
|
|
|
xmlChar *value;
|
|
|
|
double width = 0;
|
|
|
|
double height = 0;
|
|
|
|
value = xmlTextReaderGetAttribute(reader, BAD_CAST "width");
|
|
|
|
if (value)
|
|
|
|
{
|
|
|
|
width = parse_double((const char*)value);
|
|
|
|
xmlFree(value);
|
|
|
|
}
|
|
|
|
xmlChar *value2;
|
|
|
|
value2 = xmlTextReaderGetAttribute(reader, BAD_CAST "width");
|
|
|
|
if (value2)
|
|
|
|
{
|
|
|
|
height = parse_double((const char*)value2);
|
|
|
|
xmlFree(value2);
|
|
|
|
}
|
2013-03-13 01:09:59 +00:00
|
|
|
parser.path_.set_dimensions(width,height);
|
2013-03-09 01:40:19 +00:00
|
|
|
|
|
|
|
}
|
2013-03-13 01:09:59 +00:00
|
|
|
void parse_path(svg_parser & parser, xmlTextReaderPtr reader)
|
2010-05-28 16:34:34 +00:00
|
|
|
{
|
2012-04-11 13:00:34 +00:00
|
|
|
xmlChar *value;
|
2010-05-28 16:34:34 +00:00
|
|
|
|
|
|
|
value = xmlTextReaderGetAttribute(reader, BAD_CAST "d");
|
2012-02-02 01:53:35 +00:00
|
|
|
if (value)
|
2010-05-28 16:34:34 +00:00
|
|
|
{
|
2012-06-17 03:54:14 +00:00
|
|
|
// d="" (empty paths) are valid
|
2013-01-15 21:32:09 +00:00
|
|
|
if (std::strlen((const char*)value) < 1)
|
2010-06-02 11:03:30 +00:00
|
|
|
{
|
2012-04-11 13:44:19 +00:00
|
|
|
xmlFree(value);
|
2012-06-17 03:54:14 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-03-13 01:09:59 +00:00
|
|
|
parser.path_.begin_path();
|
2012-06-17 03:54:14 +00:00
|
|
|
|
2013-03-13 01:09:59 +00:00
|
|
|
if (!mapnik::svg::parse_path((const char*) value, parser.path_))
|
2012-04-11 18:45:56 +00:00
|
|
|
{
|
2012-06-17 03:54:14 +00:00
|
|
|
xmlFree(value);
|
|
|
|
xmlChar *id_value;
|
|
|
|
id_value = xmlTextReaderGetAttribute(reader, BAD_CAST "id");
|
|
|
|
if (id_value)
|
|
|
|
{
|
|
|
|
std::string id_string((const char *) id_value);
|
|
|
|
xmlFree(id_value);
|
|
|
|
throw std::runtime_error(std::string("unable to parse invalid svg <path> with id '") + id_string + "'");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw std::runtime_error("unable to parse invalid svg <path>");
|
|
|
|
}
|
2012-04-11 18:45:56 +00:00
|
|
|
}
|
2013-03-13 01:09:59 +00:00
|
|
|
parser.path_.end_path();
|
2012-06-17 03:54:14 +00:00
|
|
|
xmlFree(value);
|
2010-06-02 11:03:30 +00:00
|
|
|
}
|
2010-05-28 16:34:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-13 01:09:59 +00:00
|
|
|
void parse_polygon(svg_parser & parser, xmlTextReaderPtr reader)
|
2010-05-28 16:34:34 +00:00
|
|
|
{
|
2012-04-11 13:00:34 +00:00
|
|
|
xmlChar *value;
|
2012-02-02 01:53:35 +00:00
|
|
|
|
2010-05-28 16:34:34 +00:00
|
|
|
value = xmlTextReaderGetAttribute(reader, BAD_CAST "points");
|
2012-02-02 01:53:35 +00:00
|
|
|
if (value)
|
2010-05-28 16:34:34 +00:00
|
|
|
{
|
2013-03-13 01:09:59 +00:00
|
|
|
parser.path_.begin_path();
|
|
|
|
if (!mapnik::svg::parse_points((const char*) value, parser.path_))
|
2010-06-02 11:03:30 +00:00
|
|
|
{
|
2012-04-11 13:44:19 +00:00
|
|
|
xmlFree(value);
|
2012-04-11 18:45:56 +00:00
|
|
|
throw std::runtime_error("Failed to parse <polygon>");
|
2010-06-02 11:03:30 +00:00
|
|
|
}
|
2013-03-13 01:09:59 +00:00
|
|
|
parser.path_.close_subpath();
|
|
|
|
parser.path_.end_path();
|
2012-04-11 13:00:34 +00:00
|
|
|
xmlFree(value);
|
2010-05-28 16:34:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-13 01:09:59 +00:00
|
|
|
void parse_polyline(svg_parser & parser, xmlTextReaderPtr reader)
|
2010-05-28 16:34:34 +00:00
|
|
|
{
|
2012-04-11 13:00:34 +00:00
|
|
|
xmlChar *value;
|
2012-02-02 01:53:35 +00:00
|
|
|
|
2010-05-28 16:34:34 +00:00
|
|
|
value = xmlTextReaderGetAttribute(reader, BAD_CAST "points");
|
2012-02-02 01:53:35 +00:00
|
|
|
if (value)
|
2010-05-28 16:34:34 +00:00
|
|
|
{
|
2013-03-13 01:09:59 +00:00
|
|
|
parser.path_.begin_path();
|
|
|
|
if (!mapnik::svg::parse_points((const char*) value, parser.path_))
|
2010-06-02 11:03:30 +00:00
|
|
|
{
|
2012-04-11 13:44:19 +00:00
|
|
|
xmlFree(value);
|
2012-04-11 18:45:56 +00:00
|
|
|
throw std::runtime_error("Failed to parse <polygon>");
|
2010-06-02 11:03:30 +00:00
|
|
|
}
|
2012-02-02 01:53:35 +00:00
|
|
|
|
2013-03-13 01:09:59 +00:00
|
|
|
parser.path_.end_path();
|
2012-04-11 13:00:34 +00:00
|
|
|
xmlFree(value);
|
2010-05-28 16:34:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-13 01:09:59 +00:00
|
|
|
void parse_line(svg_parser & parser, xmlTextReaderPtr reader)
|
2010-05-28 16:34:34 +00:00
|
|
|
{
|
2012-04-11 13:00:34 +00:00
|
|
|
xmlChar *value;
|
2010-05-28 16:34:34 +00:00
|
|
|
double x1 = 0.0;
|
|
|
|
double y1 = 0.0;
|
|
|
|
double x2 = 0.0;
|
|
|
|
double y2 = 0.0;
|
2012-02-02 01:53:35 +00:00
|
|
|
|
2010-05-28 16:34:34 +00:00
|
|
|
value = xmlTextReaderGetAttribute(reader, BAD_CAST "x1");
|
2012-04-11 13:00:34 +00:00
|
|
|
if (value)
|
|
|
|
{
|
|
|
|
x1 = parse_double((const char*)value);
|
|
|
|
xmlFree(value);
|
|
|
|
}
|
|
|
|
|
2010-05-28 16:34:34 +00:00
|
|
|
value = xmlTextReaderGetAttribute(reader, BAD_CAST "y1");
|
2012-04-11 13:00:34 +00:00
|
|
|
if (value)
|
|
|
|
{
|
|
|
|
y1 = parse_double((const char*)value);
|
|
|
|
xmlFree(value);
|
|
|
|
}
|
|
|
|
|
2010-05-28 16:34:34 +00:00
|
|
|
value = xmlTextReaderGetAttribute(reader, BAD_CAST "x2");
|
2012-04-11 13:00:34 +00:00
|
|
|
if (value)
|
|
|
|
{
|
|
|
|
x2 = parse_double((const char*)value);
|
|
|
|
xmlFree(value);
|
|
|
|
}
|
|
|
|
|
2010-05-28 16:34:34 +00:00
|
|
|
value = xmlTextReaderGetAttribute(reader, BAD_CAST "y2");
|
2012-04-11 13:00:34 +00:00
|
|
|
if (value)
|
|
|
|
{
|
|
|
|
y2 = parse_double((const char*)value);
|
|
|
|
xmlFree(value);
|
|
|
|
}
|
|
|
|
|
2013-03-13 01:09:59 +00:00
|
|
|
parser.path_.begin_path();
|
|
|
|
parser.path_.move_to(x1, y1);
|
|
|
|
parser.path_.line_to(x2, y2);
|
|
|
|
parser.path_.end_path();
|
2012-02-02 01:53:35 +00:00
|
|
|
|
2010-05-28 16:34:34 +00:00
|
|
|
}
|
|
|
|
|
2013-03-13 01:09:59 +00:00
|
|
|
void parse_circle(svg_parser & parser, xmlTextReaderPtr reader)
|
2010-05-28 16:34:34 +00:00
|
|
|
{
|
2012-04-11 13:00:34 +00:00
|
|
|
xmlChar *value;
|
2010-05-28 16:34:34 +00:00
|
|
|
double cx = 0.0;
|
|
|
|
double cy = 0.0;
|
|
|
|
double r = 0.0;
|
|
|
|
value = xmlTextReaderGetAttribute(reader, BAD_CAST "cx");
|
2012-04-11 13:00:34 +00:00
|
|
|
if (value)
|
|
|
|
{
|
|
|
|
cx = parse_double((const char*)value);
|
|
|
|
xmlFree(value);
|
|
|
|
}
|
|
|
|
|
2010-05-28 16:34:34 +00:00
|
|
|
value = xmlTextReaderGetAttribute(reader, BAD_CAST "cy");
|
2012-04-11 13:00:34 +00:00
|
|
|
if (value)
|
|
|
|
{
|
|
|
|
cy = parse_double((const char*)value);
|
|
|
|
xmlFree(value);
|
|
|
|
}
|
|
|
|
|
2010-05-28 16:34:34 +00:00
|
|
|
value = xmlTextReaderGetAttribute(reader, BAD_CAST "r");
|
2012-04-11 13:00:34 +00:00
|
|
|
if (value)
|
|
|
|
{
|
|
|
|
r = parse_double((const char*)value);
|
|
|
|
xmlFree(value);
|
|
|
|
}
|
|
|
|
|
2013-03-13 01:09:59 +00:00
|
|
|
parser.path_.begin_path();
|
2012-02-02 01:53:35 +00:00
|
|
|
|
2010-05-28 16:34:34 +00:00
|
|
|
if(r != 0.0)
|
|
|
|
{
|
2010-06-02 11:03:30 +00:00
|
|
|
if(r < 0.0) throw std::runtime_error("parse_circle: Invalid radius");
|
2010-06-13 13:45:59 +00:00
|
|
|
agg::ellipse c(cx, cy, r, r);
|
2013-03-13 01:09:59 +00:00
|
|
|
parser.path_.storage().concat_path(c);
|
2010-05-28 16:34:34 +00:00
|
|
|
}
|
2012-02-02 01:53:35 +00:00
|
|
|
|
2013-03-13 01:09:59 +00:00
|
|
|
parser.path_.end_path();
|
2010-05-28 16:34:34 +00:00
|
|
|
}
|
|
|
|
|
2013-03-13 01:09:59 +00:00
|
|
|
void parse_ellipse(svg_parser & parser, xmlTextReaderPtr reader)
|
2010-05-28 16:34:34 +00:00
|
|
|
{
|
2012-04-11 13:00:34 +00:00
|
|
|
xmlChar *value;
|
2010-05-28 16:34:34 +00:00
|
|
|
double cx = 0.0;
|
|
|
|
double cy = 0.0;
|
|
|
|
double rx = 0.0;
|
|
|
|
double ry = 0.0;
|
2012-02-02 01:53:35 +00:00
|
|
|
|
2010-05-28 16:34:34 +00:00
|
|
|
value = xmlTextReaderGetAttribute(reader, BAD_CAST "cx");
|
2012-04-11 13:00:34 +00:00
|
|
|
if (value)
|
|
|
|
{
|
|
|
|
cx = parse_double((const char*)value);
|
|
|
|
xmlFree(value);
|
|
|
|
}
|
|
|
|
|
2010-05-28 16:34:34 +00:00
|
|
|
value = xmlTextReaderGetAttribute(reader, BAD_CAST "cy");
|
2012-04-11 13:00:34 +00:00
|
|
|
if (value)
|
|
|
|
{
|
|
|
|
cy = parse_double((const char*)value);
|
|
|
|
xmlFree(value);
|
|
|
|
}
|
|
|
|
|
2010-05-28 16:34:34 +00:00
|
|
|
value = xmlTextReaderGetAttribute(reader, BAD_CAST "rx");
|
2012-04-11 13:00:34 +00:00
|
|
|
if (value)
|
|
|
|
{
|
|
|
|
rx = parse_double((const char*)value);
|
|
|
|
xmlFree(value);
|
|
|
|
}
|
|
|
|
|
2010-05-28 16:34:34 +00:00
|
|
|
value = xmlTextReaderGetAttribute(reader, BAD_CAST "ry");
|
2012-04-11 13:00:34 +00:00
|
|
|
if (value)
|
|
|
|
{
|
|
|
|
ry = parse_double((const char*)value);
|
|
|
|
xmlFree(value);
|
|
|
|
}
|
|
|
|
|
2013-03-13 01:09:59 +00:00
|
|
|
parser.path_.begin_path();
|
2012-02-02 01:53:35 +00:00
|
|
|
|
2010-05-28 16:34:34 +00:00
|
|
|
if(rx != 0.0 && ry != 0.0)
|
|
|
|
{
|
2010-06-02 11:03:30 +00:00
|
|
|
if(rx < 0.0) throw std::runtime_error("parse_ellipse: Invalid rx");
|
|
|
|
if(ry < 0.0) throw std::runtime_error("parse_ellipse: Invalid ry");
|
2010-06-13 13:45:59 +00:00
|
|
|
agg::ellipse c(cx, cy, rx, ry);
|
2013-03-13 01:09:59 +00:00
|
|
|
parser.path_.storage().concat_path(c);
|
2010-05-28 16:34:34 +00:00
|
|
|
}
|
2012-02-02 01:53:35 +00:00
|
|
|
|
2013-03-13 01:09:59 +00:00
|
|
|
parser.path_.end_path();
|
2012-04-11 13:00:34 +00:00
|
|
|
|
2010-05-28 16:34:34 +00:00
|
|
|
}
|
|
|
|
|
2013-03-13 01:09:59 +00:00
|
|
|
void parse_rect(svg_parser & parser, xmlTextReaderPtr reader)
|
2010-05-28 16:34:34 +00:00
|
|
|
{
|
2012-04-11 13:00:34 +00:00
|
|
|
xmlChar *value;
|
2010-05-28 16:34:34 +00:00
|
|
|
double x = 0.0;
|
|
|
|
double y = 0.0;
|
|
|
|
double w = 0.0;
|
|
|
|
double h = 0.0;
|
|
|
|
double rx = 0.0;
|
|
|
|
double ry = 0.0;
|
2012-02-02 01:53:35 +00:00
|
|
|
|
2010-05-28 16:34:34 +00:00
|
|
|
value = xmlTextReaderGetAttribute(reader, BAD_CAST "x");
|
2012-04-11 13:00:34 +00:00
|
|
|
if (value)
|
|
|
|
{
|
|
|
|
x = parse_double((const char*)value);
|
|
|
|
xmlFree(value);
|
|
|
|
}
|
|
|
|
|
2010-05-28 16:34:34 +00:00
|
|
|
value = xmlTextReaderGetAttribute(reader, BAD_CAST "y");
|
2012-04-11 13:00:34 +00:00
|
|
|
if (value)
|
|
|
|
{
|
|
|
|
y = parse_double((const char*)value);
|
|
|
|
xmlFree(value);
|
|
|
|
}
|
|
|
|
|
2010-05-28 16:34:34 +00:00
|
|
|
value = xmlTextReaderGetAttribute(reader, BAD_CAST "width");
|
2012-04-11 13:00:34 +00:00
|
|
|
if (value)
|
|
|
|
{
|
|
|
|
w = parse_double((const char*)value);
|
|
|
|
xmlFree(value);
|
|
|
|
}
|
2010-05-28 16:34:34 +00:00
|
|
|
value = xmlTextReaderGetAttribute(reader, BAD_CAST "height");
|
2012-04-11 13:00:34 +00:00
|
|
|
if (value)
|
|
|
|
{
|
|
|
|
h = parse_double((const char*)value);
|
|
|
|
xmlFree(value);
|
|
|
|
}
|
2012-02-02 01:53:35 +00:00
|
|
|
|
2010-06-13 13:45:59 +00:00
|
|
|
bool rounded = true;
|
2010-05-28 16:34:34 +00:00
|
|
|
value = xmlTextReaderGetAttribute(reader, BAD_CAST "rx");
|
2012-04-11 13:00:34 +00:00
|
|
|
if (value)
|
|
|
|
{
|
|
|
|
rx = parse_double((const char*)value);
|
|
|
|
xmlFree(value);
|
|
|
|
}
|
2010-06-13 13:45:59 +00:00
|
|
|
else rounded = false;
|
2012-02-02 01:53:35 +00:00
|
|
|
|
2010-05-28 16:34:34 +00:00
|
|
|
value = xmlTextReaderGetAttribute(reader, BAD_CAST "ry");
|
2010-06-13 13:45:59 +00:00
|
|
|
if (value)
|
|
|
|
{
|
|
|
|
ry = parse_double((const char*)value);
|
2012-02-02 01:53:35 +00:00
|
|
|
if (!rounded)
|
2010-06-13 13:45:59 +00:00
|
|
|
{
|
|
|
|
rx = ry;
|
|
|
|
rounded = true;
|
|
|
|
}
|
2012-04-11 13:00:34 +00:00
|
|
|
xmlFree(value);
|
2010-06-13 13:45:59 +00:00
|
|
|
}
|
2012-02-02 01:53:35 +00:00
|
|
|
else if (rounded)
|
2010-06-13 13:45:59 +00:00
|
|
|
{
|
|
|
|
ry = rx;
|
|
|
|
}
|
2012-02-02 01:53:35 +00:00
|
|
|
|
2010-05-28 16:34:34 +00:00
|
|
|
if(w != 0.0 && h != 0.0)
|
|
|
|
{
|
2010-06-02 11:03:30 +00:00
|
|
|
if(w < 0.0) throw std::runtime_error("parse_rect: Invalid width");
|
|
|
|
if(h < 0.0) throw std::runtime_error("parse_rect: Invalid height");
|
|
|
|
if(rx < 0.0) throw std::runtime_error("parse_rect: Invalid rx");
|
|
|
|
if(ry < 0.0) throw std::runtime_error("parse_rect: Invalid ry");
|
2013-03-13 01:09:59 +00:00
|
|
|
parser.path_.begin_path();
|
2012-04-11 13:00:34 +00:00
|
|
|
|
2010-06-13 13:45:59 +00:00
|
|
|
if(rounded)
|
2010-06-02 11:03:30 +00:00
|
|
|
{
|
2010-06-13 13:45:59 +00:00
|
|
|
agg::rounded_rect r;
|
|
|
|
r.rect(x,y,x+w,y+h);
|
|
|
|
r.radius(rx,ry);
|
2013-03-13 01:09:59 +00:00
|
|
|
parser.path_.storage().concat_path(r);
|
2010-06-02 11:03:30 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-03-13 01:09:59 +00:00
|
|
|
parser.path_.move_to(x, y);
|
|
|
|
parser.path_.line_to(x + w, y);
|
|
|
|
parser.path_.line_to(x + w, y + h);
|
|
|
|
parser.path_.line_to(x, y + h);
|
|
|
|
parser.path_.close_subpath();
|
2010-06-02 11:03:30 +00:00
|
|
|
}
|
2013-03-13 01:09:59 +00:00
|
|
|
parser.path_.end_path();
|
2010-05-28 16:34:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-26 01:18:40 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* <stop
|
2012-02-02 01:53:35 +00:00
|
|
|
style="stop-color:#ffffff;stop-opacity:1;"
|
|
|
|
offset="1"
|
|
|
|
id="stop3763" />
|
|
|
|
*/
|
2013-03-13 01:09:59 +00:00
|
|
|
void parse_gradient_stop(svg_parser & parser, xmlTextReaderPtr reader)
|
2011-01-26 01:18:40 +00:00
|
|
|
{
|
2012-04-11 13:00:34 +00:00
|
|
|
xmlChar *value;
|
2011-01-26 01:18:40 +00:00
|
|
|
|
|
|
|
double offset = 0.0;
|
|
|
|
mapnik::color stop_color;
|
|
|
|
double opacity = 1.0;
|
|
|
|
|
|
|
|
value = xmlTextReaderGetAttribute(reader, BAD_CAST "offset");
|
2012-04-11 13:00:34 +00:00
|
|
|
if (value)
|
|
|
|
{
|
|
|
|
offset = parse_double((const char*)value);
|
|
|
|
xmlFree(value);
|
|
|
|
}
|
2011-01-26 01:18:40 +00:00
|
|
|
|
|
|
|
value = xmlTextReaderGetAttribute(reader, BAD_CAST "style");
|
|
|
|
if (value)
|
|
|
|
{
|
|
|
|
typedef std::vector<std::pair<std::string,std::string> > cont_type;
|
|
|
|
typedef cont_type::value_type value_type;
|
|
|
|
cont_type vec;
|
|
|
|
parse_style((const char*)value, vec);
|
|
|
|
|
|
|
|
BOOST_FOREACH(value_type kv , vec )
|
|
|
|
{
|
|
|
|
if (kv.first == "stop-color")
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2012-08-29 17:44:04 +00:00
|
|
|
stop_color = mapnik::parse_color(kv.second.c_str());
|
2011-01-26 01:18:40 +00:00
|
|
|
}
|
2013-03-23 00:58:33 +00:00
|
|
|
catch (mapnik::config_error const& ex)
|
2011-01-26 01:18:40 +00:00
|
|
|
{
|
2012-04-09 22:51:04 +00:00
|
|
|
MAPNIK_LOG_ERROR(svg_parser) << ex.what();
|
2011-01-26 01:18:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (kv.first == "stop-opacity")
|
|
|
|
{
|
|
|
|
opacity = parse_double(kv.second.c_str());
|
|
|
|
}
|
|
|
|
}
|
2012-04-11 13:00:34 +00:00
|
|
|
xmlFree(value);
|
2011-01-26 01:18:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
value = xmlTextReaderGetAttribute(reader, BAD_CAST "stop-color");
|
|
|
|
if (value)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2012-08-29 17:44:04 +00:00
|
|
|
stop_color = mapnik::parse_color((const char *) value);
|
2011-01-26 01:18:40 +00:00
|
|
|
}
|
2013-03-23 00:58:33 +00:00
|
|
|
catch (mapnik::config_error const& ex)
|
2011-01-26 01:18:40 +00:00
|
|
|
{
|
2012-04-09 22:51:04 +00:00
|
|
|
MAPNIK_LOG_ERROR(svg_parser) << ex.what();
|
2011-01-26 01:18:40 +00:00
|
|
|
}
|
2012-04-11 13:00:34 +00:00
|
|
|
xmlFree(value);
|
2011-01-26 01:18:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
value = xmlTextReaderGetAttribute(reader, BAD_CAST "stop-opacity");
|
|
|
|
if (value)
|
|
|
|
{
|
|
|
|
opacity = parse_double((const char *) value);
|
2012-04-11 13:00:34 +00:00
|
|
|
xmlFree(value);
|
2011-01-26 01:18:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
stop_color.set_alpha(opacity*255);
|
|
|
|
|
2013-03-13 01:09:59 +00:00
|
|
|
parser.temporary_gradient_.second.add_stop(offset, stop_color);
|
2011-01-26 01:18:40 +00:00
|
|
|
|
2012-04-09 22:51:04 +00:00
|
|
|
/*
|
|
|
|
MAPNIK_LOG_DEBUG(svg_parser) << "\tFound Stop: " << offset << " "
|
|
|
|
<< (unsigned)stop_color.red() << " "
|
|
|
|
<< (unsigned)stop_color.green() << " "
|
|
|
|
<< (unsigned)stop_color.blue() << " "
|
|
|
|
<< (unsigned)stop_color.alpha();
|
|
|
|
*/
|
2011-01-26 01:18:40 +00:00
|
|
|
}
|
|
|
|
|
2013-03-13 01:09:59 +00:00
|
|
|
bool parse_common_gradient(svg_parser & parser, xmlTextReaderPtr reader)
|
2011-01-26 01:18:40 +00:00
|
|
|
{
|
2012-04-11 13:00:34 +00:00
|
|
|
xmlChar *value;
|
2011-01-26 01:18:40 +00:00
|
|
|
|
|
|
|
std::string id;
|
|
|
|
value = xmlTextReaderGetAttribute(reader, BAD_CAST "id");
|
|
|
|
if (value)
|
|
|
|
{
|
|
|
|
// start a new gradient
|
|
|
|
gradient new_grad;
|
|
|
|
id = std::string((const char *) value);
|
2013-03-13 01:09:59 +00:00
|
|
|
parser.temporary_gradient_ = std::make_pair(id, new_grad);
|
2012-04-11 13:00:34 +00:00
|
|
|
xmlFree(value);
|
2011-01-26 01:18:40 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// no point without an ID
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// check if we should inherit from another tag
|
|
|
|
value = xmlTextReaderGetAttribute(reader, BAD_CAST "xlink:href");
|
2012-04-11 13:00:34 +00:00
|
|
|
if (value)
|
2011-01-26 01:18:40 +00:00
|
|
|
{
|
2012-04-11 13:00:34 +00:00
|
|
|
if (value[0] == '#')
|
2011-01-26 01:18:40 +00:00
|
|
|
{
|
2012-04-11 13:00:34 +00:00
|
|
|
std::string linkid = (const char *) &value[1];
|
2013-03-13 01:09:59 +00:00
|
|
|
if (parser.gradient_map_.count(linkid))
|
2012-04-11 13:00:34 +00:00
|
|
|
{
|
2013-03-13 01:09:59 +00:00
|
|
|
parser.temporary_gradient_.second = parser.gradient_map_[linkid];
|
2012-04-11 13:00:34 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-04-11 13:00:34 +00:00
|
|
|
MAPNIK_LOG_ERROR(svg_parser) << "Failed to find linked gradient " << linkid;
|
2012-04-11 13:00:34 +00:00
|
|
|
}
|
2011-01-26 01:18:40 +00:00
|
|
|
}
|
2012-04-11 13:00:34 +00:00
|
|
|
xmlFree(value);
|
2011-01-26 01:18:40 +00:00
|
|
|
}
|
2012-04-11 13:00:34 +00:00
|
|
|
|
2011-01-26 01:18:40 +00:00
|
|
|
value = xmlTextReaderGetAttribute(reader, BAD_CAST "gradientUnits");
|
2012-04-11 13:00:34 +00:00
|
|
|
if (value)
|
2011-01-26 01:18:40 +00:00
|
|
|
{
|
2012-04-11 13:00:34 +00:00
|
|
|
if (xmlStrEqual(value, BAD_CAST "userSpaceOnUse"))
|
|
|
|
{
|
2013-03-13 01:09:59 +00:00
|
|
|
parser.temporary_gradient_.second.set_units(USER_SPACE_ON_USE);
|
2012-04-11 13:00:34 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-03-13 01:09:59 +00:00
|
|
|
parser.temporary_gradient_.second.set_units(OBJECT_BOUNDING_BOX);
|
2012-04-11 13:00:34 +00:00
|
|
|
}
|
|
|
|
xmlFree(value);
|
2011-01-26 01:18:40 +00:00
|
|
|
}
|
2012-04-11 13:00:34 +00:00
|
|
|
|
2011-01-26 01:18:40 +00:00
|
|
|
value = xmlTextReaderGetAttribute(reader, BAD_CAST "gradientTransform");
|
|
|
|
if (value)
|
|
|
|
{
|
|
|
|
agg::trans_affine tr;
|
|
|
|
mapnik::svg::parse_transform((const char*) value,tr);
|
2013-03-13 01:09:59 +00:00
|
|
|
parser.temporary_gradient_.second.set_transform(tr);
|
2012-04-11 13:00:34 +00:00
|
|
|
xmlFree(value);
|
2011-01-26 01:18:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* <radialGradient
|
2012-02-02 01:53:35 +00:00
|
|
|
collect="always"
|
|
|
|
xlink:href="#linearGradient3759"
|
|
|
|
id="radialGradient3765"
|
|
|
|
cx="-1.2957155"
|
|
|
|
cy="-21.425594"
|
|
|
|
fx="-1.2957155"
|
|
|
|
fy="-21.425594"
|
|
|
|
r="5.1999998"
|
|
|
|
gradientUnits="userSpaceOnUse" />
|
|
|
|
*/
|
2013-03-13 01:09:59 +00:00
|
|
|
void parse_radial_gradient(svg_parser & parser, xmlTextReaderPtr reader)
|
2011-01-26 01:18:40 +00:00
|
|
|
{
|
2013-03-13 01:09:59 +00:00
|
|
|
if (!parse_common_gradient(parser,reader))
|
2011-01-26 01:18:40 +00:00
|
|
|
return;
|
|
|
|
|
2012-04-11 13:00:34 +00:00
|
|
|
xmlChar *value;
|
2011-01-26 01:18:40 +00:00
|
|
|
double cx = 0.5;
|
|
|
|
double cy = 0.5;
|
|
|
|
double fx = 0.0;
|
|
|
|
double fy = 0.0;
|
|
|
|
double r = 0.5;
|
|
|
|
bool has_percent=true;
|
|
|
|
|
|
|
|
value = xmlTextReaderGetAttribute(reader, BAD_CAST "cx");
|
2012-04-11 13:00:34 +00:00
|
|
|
if (value)
|
|
|
|
{
|
|
|
|
cx = parse_double_optional_percent((const char*)value, has_percent);
|
|
|
|
xmlFree(value);
|
|
|
|
}
|
2011-01-26 01:18:40 +00:00
|
|
|
|
|
|
|
value = xmlTextReaderGetAttribute(reader, BAD_CAST "cy");
|
2012-04-11 13:00:34 +00:00
|
|
|
if (value)
|
|
|
|
{
|
|
|
|
cy = parse_double_optional_percent((const char*)value, has_percent);
|
|
|
|
xmlFree(value);
|
|
|
|
}
|
2011-01-26 01:18:40 +00:00
|
|
|
|
|
|
|
value = xmlTextReaderGetAttribute(reader, BAD_CAST "fx");
|
|
|
|
if (value)
|
2012-04-11 13:00:34 +00:00
|
|
|
{
|
2011-01-26 01:18:40 +00:00
|
|
|
fx = parse_double_optional_percent((const char*)value, has_percent);
|
2012-04-11 13:00:34 +00:00
|
|
|
xmlFree(value);
|
|
|
|
}
|
2011-01-26 01:18:40 +00:00
|
|
|
else
|
|
|
|
fx = cx;
|
|
|
|
|
|
|
|
value = xmlTextReaderGetAttribute(reader, BAD_CAST "fy");
|
|
|
|
if (value)
|
2012-04-11 13:00:34 +00:00
|
|
|
{
|
2011-01-26 01:18:40 +00:00
|
|
|
fy = parse_double_optional_percent((const char*)value, has_percent);
|
2012-04-11 13:00:34 +00:00
|
|
|
xmlFree(value);
|
|
|
|
}
|
2011-01-26 01:18:40 +00:00
|
|
|
else
|
|
|
|
fy = cy;
|
|
|
|
|
|
|
|
value = xmlTextReaderGetAttribute(reader, BAD_CAST "r");
|
2012-04-11 13:00:34 +00:00
|
|
|
if (value)
|
|
|
|
{
|
|
|
|
r = parse_double_optional_percent((const char*)value, has_percent);
|
|
|
|
xmlFree(value);
|
|
|
|
}
|
2011-01-26 01:18:40 +00:00
|
|
|
// this logic for detecting %'s will not support mixed coordinates.
|
2013-03-13 01:09:59 +00:00
|
|
|
if (has_percent && parser.temporary_gradient_.second.get_units() == USER_SPACE_ON_USE)
|
2011-01-26 01:18:40 +00:00
|
|
|
{
|
2013-03-13 01:09:59 +00:00
|
|
|
parser.temporary_gradient_.second.set_units(USER_SPACE_ON_USE_BOUNDING_BOX);
|
2011-01-26 01:18:40 +00:00
|
|
|
}
|
|
|
|
|
2013-03-13 01:09:59 +00:00
|
|
|
parser.temporary_gradient_.second.set_gradient_type(RADIAL);
|
|
|
|
parser.temporary_gradient_.second.set_control_points(fx,fy,cx,cy,r);
|
2011-01-26 01:18:40 +00:00
|
|
|
// add this here in case we have no end tag, will be replaced if we do
|
2013-03-13 01:09:59 +00:00
|
|
|
parser.gradient_map_[parser.temporary_gradient_.first] = parser.temporary_gradient_.second;
|
2011-01-26 01:18:40 +00:00
|
|
|
|
2012-04-09 22:51:04 +00:00
|
|
|
//MAPNIK_LOG_DEBUG(svg_parser) << "Found Radial Gradient: " << " " << cx << " " << cy << " " << fx << " " << fy << " " << r;
|
2011-01-26 01:18:40 +00:00
|
|
|
}
|
|
|
|
|
2013-03-13 01:09:59 +00:00
|
|
|
void parse_linear_gradient(svg_parser & parser, xmlTextReaderPtr reader)
|
2011-01-26 01:18:40 +00:00
|
|
|
{
|
2013-03-13 01:09:59 +00:00
|
|
|
if (!parse_common_gradient(parser,reader))
|
2011-01-26 01:18:40 +00:00
|
|
|
return;
|
|
|
|
|
2012-04-11 13:00:34 +00:00
|
|
|
xmlChar *value;
|
2011-01-26 01:18:40 +00:00
|
|
|
double x1 = 0.0;
|
|
|
|
double x2 = 1.0;
|
|
|
|
double y1 = 0.0;
|
|
|
|
double y2 = 1.0;
|
|
|
|
|
|
|
|
bool has_percent=true;
|
|
|
|
value = xmlTextReaderGetAttribute(reader, BAD_CAST "x1");
|
2012-04-11 13:00:34 +00:00
|
|
|
if (value)
|
|
|
|
{
|
|
|
|
x1 = parse_double_optional_percent((const char*)value, has_percent);
|
|
|
|
xmlFree(value);
|
|
|
|
}
|
2011-01-26 01:18:40 +00:00
|
|
|
|
|
|
|
value = xmlTextReaderGetAttribute(reader, BAD_CAST "x2");
|
2012-04-11 13:00:34 +00:00
|
|
|
if (value)
|
|
|
|
{
|
|
|
|
x2 = parse_double_optional_percent((const char*)value, has_percent);
|
|
|
|
xmlFree(value);
|
|
|
|
}
|
2011-01-26 01:18:40 +00:00
|
|
|
|
|
|
|
value = xmlTextReaderGetAttribute(reader, BAD_CAST "y1");
|
2012-04-11 13:00:34 +00:00
|
|
|
if (value)
|
|
|
|
{
|
|
|
|
y1 = parse_double_optional_percent((const char*)value, has_percent);
|
|
|
|
xmlFree(value);
|
|
|
|
}
|
|
|
|
|
2011-01-26 01:18:40 +00:00
|
|
|
value = xmlTextReaderGetAttribute(reader, BAD_CAST "y2");
|
2012-04-11 13:00:34 +00:00
|
|
|
if (value)
|
|
|
|
{
|
|
|
|
y2 = parse_double_optional_percent((const char*)value, has_percent);
|
|
|
|
xmlFree(value);
|
|
|
|
}
|
2011-01-26 01:18:40 +00:00
|
|
|
// this logic for detecting %'s will not support mixed coordinates.
|
2013-03-13 01:09:59 +00:00
|
|
|
if (has_percent && parser.temporary_gradient_.second.get_units() == USER_SPACE_ON_USE)
|
2011-01-26 01:18:40 +00:00
|
|
|
{
|
2013-03-13 01:09:59 +00:00
|
|
|
parser.temporary_gradient_.second.set_units(USER_SPACE_ON_USE_BOUNDING_BOX);
|
2011-01-26 01:18:40 +00:00
|
|
|
}
|
|
|
|
|
2013-03-13 01:09:59 +00:00
|
|
|
parser.temporary_gradient_.second.set_gradient_type(LINEAR);
|
|
|
|
parser.temporary_gradient_.second.set_control_points(x1,y1,x2,y2);
|
2011-01-26 01:18:40 +00:00
|
|
|
// add this here in case we have no end tag, will be replaced if we do
|
2013-03-13 01:09:59 +00:00
|
|
|
parser.gradient_map_[parser.temporary_gradient_.first] = parser.temporary_gradient_.second;
|
2011-01-26 01:18:40 +00:00
|
|
|
|
2012-04-09 22:51:04 +00:00
|
|
|
//MAPNIK_LOG_DEBUG(svg_parser) << "Found Linear Gradient: " << "(" << x1 << " " << y1 << "),(" << x2 << " " << y2 << ")";
|
2011-01-26 01:18:40 +00:00
|
|
|
}
|
|
|
|
|
2013-03-13 01:09:59 +00:00
|
|
|
void parse_pattern(svg_parser & parser, xmlTextReaderPtr reader)
|
2011-01-26 01:18:40 +00:00
|
|
|
{
|
|
|
|
//const xmlChar *value;
|
|
|
|
}
|
|
|
|
|
2013-03-13 01:09:59 +00:00
|
|
|
svg_parser::svg_parser(svg_converter<svg_path_adapter,
|
|
|
|
agg::pod_bvector<mapnik::svg::path_attributes> > & path)
|
|
|
|
: path_(path),
|
|
|
|
is_defs_(false) {}
|
|
|
|
|
|
|
|
svg_parser::~svg_parser() {}
|
|
|
|
|
|
|
|
void svg_parser::parse(std::string const& filename)
|
|
|
|
{
|
|
|
|
xmlTextReaderPtr reader = xmlNewTextReaderFilename(filename.c_str());
|
|
|
|
if (reader == NULL)
|
|
|
|
{
|
|
|
|
MAPNIK_LOG_ERROR(svg_parser) << "Unable to open '" << filename << "'";
|
|
|
|
}
|
|
|
|
else if (!parse_reader(*this,reader))
|
|
|
|
{
|
|
|
|
MAPNIK_LOG_ERROR(svg_parser) << "Unable to parse '" << filename << "'";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void svg_parser::parse_from_string(std::string const& svg)
|
|
|
|
{
|
|
|
|
xmlTextReaderPtr reader = xmlReaderForMemory(svg.c_str(),svg.size(),NULL,NULL,
|
|
|
|
(XML_PARSE_NOBLANKS | XML_PARSE_NOCDATA | XML_PARSE_NOERROR | XML_PARSE_NOWARNING));
|
|
|
|
if (reader == NULL)
|
|
|
|
{
|
|
|
|
MAPNIK_LOG_ERROR(svg_parser) << "Unable to parse '" << svg << "'";
|
|
|
|
}
|
|
|
|
else if (!parse_reader(*this,reader))
|
|
|
|
{
|
|
|
|
MAPNIK_LOG_ERROR(svg_parser) << "Unable to parse '" << svg << "'";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-05-28 16:34:34 +00:00
|
|
|
}}
|