Merge pull request #3003 from mapnik/svg-parser-errors

SVG parser refactoring and improvements
This commit is contained in:
Artem Pavlenko 2015-07-31 17:06:30 +02:00
commit 727341f41a
16 changed files with 1177 additions and 665 deletions

2
.gitmodules vendored
View file

@ -5,4 +5,4 @@
[submodule "test/data-visual"]
path = test/data-visual
url = https://github.com/mapnik/test-data-visual.git
branch = master
branch = master

View file

@ -395,7 +395,7 @@ opts.AddVariables(
BoolVariable('FULL_LIB_PATH', 'Embed the full and absolute path to libmapnik when linking ("install_name" on OS X/rpath on Linux)', 'True'),
BoolVariable('ENABLE_SONAME', 'Embed a soname in libmapnik on Linux', 'True'),
EnumVariable('THREADING','Set threading support','multi', ['multi','single']),
EnumVariable('XMLPARSER','Set xml parser','libxml2', ['libxml2','ptree']),
EnumVariable('XMLPARSER','Set xml parser','ptree', ['libxml2','ptree']),
BoolVariable('DEMO', 'Compile demo c++ application', 'True'),
BoolVariable('PGSQL2SQLITE', 'Compile and install a utility to convert postgres tables to sqlite', 'False'),
BoolVariable('SHAPEINDEX', 'Compile and install a utility to generate shapefile indexes in the custom format (.index) Mapnik supports', 'True'),
@ -1266,18 +1266,19 @@ if not preconfigured:
# libxml2 should be optional but is currently not
# https://github.com/mapnik/mapnik/issues/913
if env.get('XML2_LIBS') or env.get('XML2_INCLUDES'):
REQUIRED_LIBSHEADERS.insert(0,['libxml2','libxml/parser.h',True,'C'])
if env.get('XML2_INCLUDES'):
inc_path = env['XML2_INCLUDES']
env.AppendUnique(CPPPATH = fix_path(inc_path))
if env.get('XML2_LIBS'):
lib_path = env['XML2_LIBS']
env.AppendUnique(LIBPATH = fix_path(lib_path))
elif conf.parse_config('XML2_CONFIG',checks='--cflags'):
env['HAS_LIBXML2'] = True
else:
env['MISSING_DEPS'].append('libxml2')
if env.get('XMLPARSER') and env['XMLPARSER'] == 'libxml2':
if env.get('XML2_LIBS') or env.get('XML2_INCLUDES'):
OPTIONAL_LIBSHEADERS.insert(0,['libxml2','libxml/parser.h',True,'C'])
if env.get('XML2_INCLUDES'):
inc_path = env['XML2_INCLUDES']
env.AppendUnique(CPPPATH = fix_path(inc_path))
if env.get('XML2_LIBS'):
lib_path = env['XML2_LIBS']
env.AppendUnique(LIBPATH = fix_path(lib_path))
elif conf.parse_config('XML2_CONFIG',checks='--cflags'):
env['HAS_LIBXML2'] = True
else:
env['MISSING_DEPS'].append('libxml2')
if not env['HOST']:
if conf.CheckHasDlfcn():

View file

@ -55,5 +55,5 @@ run test_offset_converter 10 1000
--threads 1
./benchmark/out/test_quad_tree \
--iterations 10000 \
--iterations 1000 \
--threads 10

View file

@ -60,7 +60,6 @@ function install_mason_deps() {
install freetype 2.5.5 libfreetype &
install harfbuzz 0.9.40 libharfbuzz &
install jpeg_turbo 1.4.0 libjpeg &
install libxml2 2.9.2 libxml2 &
install libpng 1.6.17 libpng &
install webp 0.4.2 libwebp &
install icu 54.1 &
@ -117,8 +116,6 @@ PG_INCLUDES = '${MASON_LINKED_REL}/include'
PG_LIBS = '${MASON_LINKED_REL}/lib'
FREETYPE_INCLUDES = '${MASON_LINKED_REL}/include/freetype2'
FREETYPE_LIBS = '${MASON_LINKED_REL}/lib'
XML2_INCLUDES = '${MASON_LINKED_REL}/include/libxml2'
XML2_LIBS = '${MASON_LINKED_REL}/lib'
SVG_RENDERER = True
CAIRO_INCLUDES = '${MASON_LINKED_REL}/include'
CAIRO_LIBS = '${MASON_LINKED_REL}/lib'

View file

@ -79,8 +79,9 @@ class MAPNIK_DECL gradient
public:
gradient();
gradient(gradient const& other);
gradient& operator=(const gradient& rhs);
gradient(gradient && other);
gradient& operator=(gradient rhs);
bool operator==(gradient const& other) const;
void set_gradient_type(gradient_e grad);
gradient_e get_gradient_type() const;
@ -100,7 +101,7 @@ public:
void get_control_points(double &x1, double &y1, double &x2, double &y2) const;
private:
void swap(const gradient& other) throw();
void swap(gradient& other) throw();
};
}

View file

@ -254,10 +254,10 @@ void apply_convolution_3x3(Src const& src_view, Dst & dst_view, Filter const& fi
typename Src::x_iterator dst_it = dst_view.row_begin(0);
// top row
for (std::size_t x = 0 ; x < static_cast<std::size_t>(src_view.width()); ++x)
for (std::ptrdiff_t x = 0 ; x < src_view.width(); ++x)
{
(*dst_it)[3] = src_loc[loc11][3]; // Dst.a = Src.a
for (std::size_t i = 0; i < 3; ++i)
for (std::ptrdiff_t i = 0; i < 3; ++i)
{
bits32f p[9];
@ -275,7 +275,7 @@ void apply_convolution_3x3(Src const& src_view, Dst & dst_view, Filter const& fi
p[6] = src_loc[loc02][i];
}
if ( x == static_cast<std::size_t>(src_view.width())-1)
if ( x == (src_view.width())-1)
{
p[5] = p[4];
p[8] = p[7];
@ -296,15 +296,15 @@ void apply_convolution_3x3(Src const& src_view, Dst & dst_view, Filter const& fi
++dst_it;
}
// carrige-return
src_loc += point2<std::ptrdiff_t>(-static_cast<std::size_t>(src_view.width()),1);
src_loc += point2<std::ptrdiff_t>(-src_view.width(),1);
// 1... height-1 rows
for (std::size_t y = 1; y<static_cast<std::size_t>(src_view.height())-1; ++y)
for (std::ptrdiff_t y = 1; y < src_view.height()-1; ++y)
{
for (std::size_t x = 0; x < static_cast<std::size_t>(src_view.width()); ++x)
for (std::ptrdiff_t x = 0; x < src_view.width(); ++x)
{
(*dst_it)[3] = src_loc[loc11][3]; // Dst.a = Src.a
for (std::size_t i = 0; i < 3; ++i)
for (std::ptrdiff_t i = 0; i < 3; ++i)
{
bits32f p[9];
@ -325,7 +325,7 @@ void apply_convolution_3x3(Src const& src_view, Dst & dst_view, Filter const& fi
p[6] = src_loc[loc02][i];
}
if ( x == static_cast<std::size_t>(src_view.width()) - 1)
if ( x == (src_view.width()) - 1)
{
p[2] = p[1];
p[5] = p[4];
@ -343,15 +343,15 @@ void apply_convolution_3x3(Src const& src_view, Dst & dst_view, Filter const& fi
++src_loc.x();
}
// carrige-return
src_loc += point2<std::ptrdiff_t>(-static_cast<std::size_t>(src_view.width()),1);
src_loc += point2<std::ptrdiff_t>(-src_view.width(),1);
}
// bottom row
//src_loc = src_view.xy_at(0,static_cast<std::size_t>(src_view.height())-1);
for (std::size_t x = 0 ; x < static_cast<std::size_t>(src_view.width()); ++x)
//src_loc = src_view.xy_at(0,src_view.height()-1);
for (std::ptrdiff_t x = 0 ; x < src_view.width(); ++x)
{
(*dst_it)[3] = src_loc[loc11][3]; // Dst.a = Src.a
for (std::size_t i = 0; i < 3; ++i)
for (std::ptrdiff_t i = 0; i < 3; ++i)
{
bits32f p[9];
@ -369,7 +369,7 @@ void apply_convolution_3x3(Src const& src_view, Dst & dst_view, Filter const& fi
p[3] = src_loc[loc01][i];
}
if ( x == static_cast<std::size_t>(src_view.width())-1)
if ( x == (src_view.width())-1)
{
p[2] = p[1];
p[5] = p[4];
@ -431,10 +431,10 @@ void apply_filter(Src & src, color_to_alpha const& op)
double cr = static_cast<double>(op.color.red())/255.0;
double cg = static_cast<double>(op.color.green())/255.0;
double cb = static_cast<double>(op.color.blue())/255.0;
for (std::size_t y=0; y<static_cast<std::size_t>(src_view.height()); ++y)
for (std::ptrdiff_t y = 0; y < src_view.height(); ++y)
{
rgba8_view_t::x_iterator src_it = src_view.row_begin(static_cast<long>(y));
for (std::size_t x=0; x<static_cast<std::size_t>(src_view.width()); ++x)
for (std::ptrdiff_t x = 0; x < src_view.width(); ++x)
{
uint8_t & r = get_color(src_it[x], red_t());
uint8_t & g = get_color(src_it[x], green_t());
@ -485,17 +485,17 @@ template <typename Src>
void apply_filter(Src & src, colorize_alpha const& op)
{
using namespace boost::gil;
std::size_t size = op.size();
std::ptrdiff_t size = op.size();
if (op.size() == 1)
{
// no interpolation if only one stop
mapnik::filter::color_stop const& stop = op[0];
mapnik::color const& c = stop.color;
rgba8_view_t src_view = rgba8_view(src);
for (std::size_t y=0; y<static_cast<std::size_t>(src_view.height()); ++y)
for (std::ptrdiff_t y = 0; y < src_view.height(); ++y)
{
rgba8_view_t::x_iterator src_it = src_view.row_begin(static_cast<long>(y));
for (std::size_t x=0; x<static_cast<std::size_t>(src_view.width()); ++x)
for (std::ptrdiff_t x = 0; x < src_view.width(); ++x)
{
uint8_t & r = get_color(src_it[x], red_t());
uint8_t & g = get_color(src_it[x], green_t());
@ -533,10 +533,10 @@ void apply_filter(Src & src, colorize_alpha const& op)
if (grad_lut.build_lut())
{
rgba8_view_t src_view = rgba8_view(src);
for (std::size_t y=0; y<static_cast<std::size_t>(src_view.height()); ++y)
for (std::ptrdiff_t y = 0; y < src_view.height(); ++y)
{
rgba8_view_t::x_iterator src_it = src_view.row_begin(static_cast<long>(y));
for (std::size_t x=0; x<static_cast<std::size_t>(src_view.width()); ++x)
for (std::ptrdiff_t x = 0; x < src_view.width(); ++x)
{
uint8_t & r = get_color(src_it[x], red_t());
uint8_t & g = get_color(src_it[x], green_t());
@ -598,10 +598,10 @@ void apply_filter(Src & src, scale_hsla const& transform)
if (tinting || set_alpha)
{
rgba8_view_t src_view = rgba8_view(src);
for (std::size_t y=0; y<static_cast<std::size_t>(src_view.height()); ++y)
for (std::ptrdiff_t y = 0; y < src_view.height(); ++y)
{
rgba8_view_t::x_iterator src_it = src_view.row_begin(static_cast<long>(y));
for (std::size_t x=0; x<static_cast<std::size_t>(src_view.width()); ++x)
for (std::ptrdiff_t x = 0; x < src_view.width(); ++x)
{
uint8_t & r = get_color(src_it[x], red_t());
uint8_t & g = get_color(src_it[x], green_t());
@ -681,10 +681,10 @@ void apply_filter(Src & src, gray const& /*op*/)
rgba8_view_t src_view = rgba8_view(src);
for (std::size_t y=0; y<static_cast<std::size_t>(src_view.height()); ++y)
for (std::ptrdiff_t y = 0; y < src_view.height(); ++y)
{
rgba8_view_t::x_iterator src_it = src_view.row_begin(static_cast<long>(y));
for (std::size_t x=0; x<static_cast<std::size_t>(src_view.width()); ++x)
for (std::ptrdiff_t x = 0; x < src_view.width(); ++x)
{
// formula taken from boost/gil/color_convert.hpp:rgb_to_luminance
uint8_t & r = get_color(src_it[x], red_t());
@ -699,7 +699,7 @@ void apply_filter(Src & src, gray const& /*op*/)
template <typename Src, typename Dst>
void x_gradient_impl(Src const& src_view, Dst const& dst_view)
{
for (std::size_t y=0; y<static_cast<std::size_t>(src_view.height()); ++y)
for (std::ptrdiff_t y = 0; y < src_view.height(); ++y)
{
typename Src::x_iterator src_it = src_view.row_begin(static_cast<long>(y));
typename Dst::x_iterator dst_it = dst_view.row_begin(static_cast<long>(y));
@ -708,13 +708,13 @@ void x_gradient_impl(Src const& src_view, Dst const& dst_view)
dst_it[0][1] = 128 + (src_it[0][1] - src_it[1][1]) / 2;
dst_it[0][2] = 128 + (src_it[0][2] - src_it[1][2]) / 2;
dst_it[dst_view.width()-1][0] = 128 + (src_it[static_cast<std::size_t>(src_view.width())-2][0] - src_it[static_cast<std::size_t>(src_view.width())-1][0]) / 2;
dst_it[dst_view.width()-1][1] = 128 + (src_it[static_cast<std::size_t>(src_view.width())-2][1] - src_it[static_cast<std::size_t>(src_view.width())-1][1]) / 2;
dst_it[dst_view.width()-1][2] = 128 + (src_it[static_cast<std::size_t>(src_view.width())-2][2] - src_it[static_cast<std::size_t>(src_view.width())-1][2]) / 2;
dst_it[dst_view.width()-1][0] = 128 + (src_it[(src_view.width())-2][0] - src_it[(src_view.width())-1][0]) / 2;
dst_it[dst_view.width()-1][1] = 128 + (src_it[(src_view.width())-2][1] - src_it[(src_view.width())-1][1]) / 2;
dst_it[dst_view.width()-1][2] = 128 + (src_it[(src_view.width())-2][2] - src_it[(src_view.width())-1][2]) / 2;
dst_it[0][3] = dst_it[static_cast<std::size_t>(src_view.width())-1][3] = 255;
dst_it[0][3] = dst_it[(src_view.width())-1][3] = 255;
for (std::size_t x=1; x<static_cast<std::size_t>(src_view.width())-1; ++x)
for (std::ptrdiff_t x = 1; x < src_view.width()-1; ++x)
{
dst_it[x][0] = 128 + (src_it[x-1][0] - src_it[x+1][0]) / 2;
dst_it[x][1] = 128 + (src_it[x-1][1] - src_it[x+1][1]) / 2;
@ -746,10 +746,10 @@ void apply_filter(Src & src, invert const& /*op*/)
rgba8_view_t src_view = rgba8_view(src);
for (std::size_t y=0; y<static_cast<std::size_t>(src_view.height()); ++y)
for (std::ptrdiff_t y = 0; y < src_view.height(); ++y)
{
rgba8_view_t::x_iterator src_it = src_view.row_begin(static_cast<long>(y));
for (std::size_t x=0; x<static_cast<std::size_t>(src_view.width()); ++x)
for (std::ptrdiff_t x = 0; x < src_view.width(); ++x)
{
// we only work with premultiplied source,
// thus all color values must be <= alpha

View file

@ -38,15 +38,18 @@ namespace mapnik { namespace svg {
class MAPNIK_DECL svg_parser : private util::noncopyable
{
using error_message_container = std::vector<std::string> ;
public:
explicit svg_parser(svg_converter_type & path);
~svg_parser();
void parse(std::string const& filename);
void parse_from_string(std::string const& svg);
error_message_container const& error_messages() const;
bool parse(std::string const& filename);
bool parse_from_string(std::string const& svg);
svg_converter_type & path_;
bool is_defs_;
std::map<std::string, gradient> gradient_map_;
std::pair<std::string, gradient> temporary_gradient_;
error_message_container error_messages_;
};
}}

View file

@ -0,0 +1,54 @@
/*****************************************************************************
*
* This file is part of Mapnik (c++ mapping toolkit)
*
* Copyright (C) 2015 Artem Pavlenko
*
* 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
*
*****************************************************************************/
#ifndef MAPNIK_SVG_PARSER_EXCEPTION_HPP
#define MAPNIK_SVG_PARSER_EXCEPTION_HPP
// mapnik
#include <mapnik/config.hpp>
#include <exception>
// stl
#include <map>
namespace mapnik { namespace svg {
class MAPNIK_DECL svg_parser_exception : public std::exception
{
public:
svg_parser_exception(std::string const& message)
: message_(message) {}
~svg_parser_exception() throw() {}
virtual const char* what() const throw()
{
return message_.c_str();
}
private:
std::string message_;
};
}}
#endif // MAPNIK_SVG_PARSER_EXCEPTION_HPP

View file

@ -84,7 +84,8 @@ if '-DHAVE_WEBP' in env['CPPDEFINES']:
lib_env['LIBS'].append('webp')
enabled_imaging_libraries.append('webp_reader.cpp')
lib_env['LIBS'].append('xml2')
if env['XMLPARSER'] == 'libxml2' and env['HAS_LIBXML2']:
lib_env['LIBS'].append('xml2')
if '-DBOOST_REGEX_HAS_ICU' in env['CPPDEFINES']:
lib_env['LIBS'].append('icui18n')

View file

@ -66,13 +66,36 @@ gradient::gradient(gradient const& other)
units_(other.units_),
gradient_type_(other.gradient_type_) {}
gradient & gradient::operator=(const gradient& rhs)
gradient::gradient(gradient && other)
: transform_(std::move(other.transform_)),
x1_(std::move(other.x1_)),
y1_(std::move(other.y1_)),
x2_(std::move(other.x2_)),
y2_(std::move(other.y2_)),
r_(std::move(other.r_)),
stops_(std::move(other.stops_)),
units_(std::move(other.units_)),
gradient_type_(std::move(other.gradient_type_)) {}
gradient & gradient::operator=(gradient rhs)
{
gradient tmp(rhs);
swap(tmp);
swap(rhs);
return *this;
}
bool gradient::operator==(gradient const& other) const
{
return transform_ == other.transform_ &&
x1_ == other.x1_ &&
y1_ == other.y1_ &&
x2_ == other.x2_ &&
y2_ == other.y2_ &&
r_ == other.r_ &&
std::equal(stops_.begin(),stops_.end(), other.stops_.begin()),
units_ == other.units_ &&
gradient_type_ == other.gradient_type_;
}
void gradient::set_gradient_type(gradient_e grad)
{
gradient_type_=grad;
@ -108,7 +131,7 @@ void gradient::add_stop(double offset,mapnik::color const& c)
bool gradient::has_stop() const
{
return ! stops_.empty();
return !stops_.empty();
}
stop_array const& gradient::get_stop_array() const
@ -116,13 +139,17 @@ stop_array const& gradient::get_stop_array() const
return stops_;
}
void gradient::swap(const gradient& other) throw()
void gradient::swap(gradient& other) throw()
{
gradient_type_=other.gradient_type_;
stops_=other.stops_;
units_=other.units_;
transform_=other.transform_;
other.get_control_points(x1_,y1_,x2_,y2_,r_);
std::swap(gradient_type_, other.gradient_type_);
std::swap(stops_, other.stops_);
std::swap(units_, other.units_);
std::swap(transform_, other.transform_);
std::swap(x1_, other.x1_);
std::swap(y1_, other.y1_);
std::swap(x2_, other.x2_);
std::swap(y2_, other.y2_);
std::swap(r_, other.r_);
}
void gradient::set_control_points(double x1, double y1, double x2, double y2, double r)

View file

@ -175,7 +175,15 @@ std::shared_ptr<mapnik::marker const> marker_cache::find(std::string const& uri,
svg_path_adapter svg_path(stl_storage);
svg_converter_type svg(svg_path, marker_path->attributes());
svg_parser p(svg);
p.parse_from_string(known_svg_string);
if (!p.parse_from_string(known_svg_string))
{
for (auto const& msg : p.error_messages())
{
MAPNIK_LOG_ERROR(marker_cache) << "SVG PARSING ERROR:\"" << msg << "\"";
}
return std::make_shared<mapnik::marker const>(mapnik::marker_null());
}
//svg.arrange_orientations();
double lox,loy,hix,hiy;
svg.bounding_rect(&lox, &loy, &hix, &hiy);
@ -207,7 +215,16 @@ std::shared_ptr<mapnik::marker const> marker_cache::find(std::string const& uri,
svg_path_adapter svg_path(stl_storage);
svg_converter_type svg(svg_path, marker_path->attributes());
svg_parser p(svg);
p.parse(uri);
if (!p.parse(uri))
{
for (auto const& msg : p.error_messages())
{
MAPNIK_LOG_ERROR(marker_cache) << "SVG PARSING ERROR:\"" << msg << "\"";
}
return std::make_shared<mapnik::marker const>(mapnik::marker_null());
}
//svg.arrange_orientations();
double lox,loy,hix,hiy;
svg.bounding_rect(&lox, &loy, &hix, &hiy);

File diff suppressed because it is too large Load diff

View file

@ -1,9 +1,11 @@
#ifndef TEST_MEMORY_CLEANUP
#define TEST_MEMORY_CLEANUP
#if defined(HAVE_LIBXML2)
#include <libxml/parser.h>
#include <libxml/entities.h>
#include <libxml/globals.h>
#endif
#if defined(HAVE_CAIRO)
#include <cairo.h>
@ -21,6 +23,7 @@ inline void run_cleanup()
// only call this once, on exit
// to make sure valgrind output is clean
// http://xmlsoft.org/xmlmem.html
#if defined(HAVE_LIBXML2)
xmlCleanupCharEncodingHandlers();
xmlCleanupEncodingAliases();
xmlCleanupGlobals();
@ -29,6 +32,7 @@ inline void run_cleanup()
xmlCleanupInputCallbacks();
xmlCleanupOutputCallbacks();
xmlCleanupMemory();
#endif
#if defined(HAVE_CAIRO)
// http://cairographics.org/manual/cairo-Error-handling.html#cairo-debug-reset-static-data
@ -45,9 +49,9 @@ inline void run_cleanup()
#endif
// https://trac.osgeo.org/proj/wiki/ProjAPI#EnvironmentFunctions
pj_deallocate_grids();
#endif
#endif
}
}
#endif
#endif

@ -1 +1 @@
Subproject commit 5038563194e75b95af155a337ce48f6478534022
Subproject commit d0a23b2a512d2ea83f08a9c1dc50e9b9b4a08dd5

View file

@ -27,11 +27,15 @@
#include <mapnik/marker.hpp>
#include <mapnik/marker_cache.hpp>
#include <mapnik/vertex.hpp>
#include <mapnik/svg/svg_parser.hpp>
#include <mapnik/svg/svg_storage.hpp>
#include <mapnik/svg/svg_converter.hpp>
#include <mapnik/svg/svg_path_adapter.hpp>
#include <mapnik/svg/svg_renderer_agg.hpp>
#include <mapnik/svg/svg_path_attributes.hpp>
#include <libxml/parser.h> // for xmlInitParser(), xmlCleanupParser()
#include <cmath>
#include <fstream>
#include <streambuf>
namespace detail {
@ -51,7 +55,207 @@ struct vertex_equal
TEST_CASE("SVG parser") {
xmlInitParser();
SECTION("SVG i/o")
{
std::string svg_name("FAIL");
std::shared_ptr<mapnik::marker const> marker = mapnik::marker_cache::instance().find(svg_name, false);
REQUIRE(marker);
REQUIRE(marker->is<mapnik::marker_null>());
}
SECTION("SVG::parse i/o")
{
std::string svg_name("FAIL");
using namespace mapnik::svg;
mapnik::svg_storage_type path;
vertex_stl_adapter<svg_path_storage> stl_storage(path.source());
svg_path_adapter svg_path(stl_storage);
svg_converter_type svg(svg_path, path.attributes());
svg_parser p(svg);
if (!p.parse(svg_name))
{
auto const& errors = p.error_messages();
REQUIRE(errors.size() == 1);
REQUIRE(errors[0] == "Unable to open 'FAIL'");
}
}
SECTION("SVG::parse_from_string syntax error")
{
std::string svg_name("./test/data/svg/invalid.svg");
std::ifstream in(svg_name.c_str());
std::string svg_str((std::istreambuf_iterator<char>(in)),
std::istreambuf_iterator<char>());
using namespace mapnik::svg;
mapnik::svg_storage_type path;
vertex_stl_adapter<svg_path_storage> stl_storage(path.source());
svg_path_adapter svg_path(stl_storage);
svg_converter_type svg(svg_path, path.attributes());
svg_parser p(svg);
if (!p.parse_from_string(svg_str))
{
auto const& errors = p.error_messages();
REQUIRE(errors.size() == 1);
REQUIRE(errors[0] == "Unable to parse '<?xml version=\"1.0\"?>\n<svg width=\"12cm\" height=\"4cm\" viewBox=\"0 0 1200 400\"\nxmlns=\"http://www.w3.org/2000/svg\" version=\"1.2\" baseProfile=\"tiny\">\n'");
}
}
SECTION("SVG::parse_from_string syntax error")
{
std::string svg_name("./test/data/svg/invalid.svg");
using namespace mapnik::svg;
mapnik::svg_storage_type path;
vertex_stl_adapter<svg_path_storage> stl_storage(path.source());
svg_path_adapter svg_path(stl_storage);
svg_converter_type svg(svg_path, path.attributes());
svg_parser p(svg);
if (!p.parse(svg_name))
{
auto const& errors = p.error_messages();
REQUIRE(errors.size() == 1);
REQUIRE(errors[0] == "svg_parser::parse - Unable to parse './test/data/svg/invalid.svg'");
}
}
SECTION("SVG parser color <fail>")
{
std::string svg_name("./test/data/svg/color_fail.svg");
std::ifstream in(svg_name.c_str());
std::string svg_str((std::istreambuf_iterator<char>(in)),
std::istreambuf_iterator<char>());
using namespace mapnik::svg;
mapnik::svg_storage_type path;
vertex_stl_adapter<svg_path_storage> stl_storage(path.source());
svg_path_adapter svg_path(stl_storage);
svg_converter_type svg(svg_path, path.attributes());
svg_parser p(svg);
if (!p.parse_from_string(svg_str))
{
auto const& errors = p.error_messages();
REQUIRE(errors.size() == 3);
REQUIRE(errors[0] == "Failed to parse color: \"fail\"");
REQUIRE(errors[1] == "Failed to parse double: \"fail\"");
REQUIRE(errors[2] == "Failed to parse color: \"fail\"");
}
}
SECTION("SVG - cope with erroneous geometries")
{
std::string svg_name("./test/data/svg/errors.svg");
std::ifstream in(svg_name.c_str());
std::string svg_str((std::istreambuf_iterator<char>(in)),
std::istreambuf_iterator<char>());
using namespace mapnik::svg;
mapnik::svg_storage_type path;
vertex_stl_adapter<svg_path_storage> stl_storage(path.source());
svg_path_adapter svg_path(stl_storage);
svg_converter_type svg(svg_path, path.attributes());
svg_parser p(svg);
if (!p.parse_from_string(svg_str))
{
auto const& errors = p.error_messages();
REQUIRE(errors.size() == 14);
REQUIRE(errors[0] == "parse_rect: Invalid width");
REQUIRE(errors[1] == "Failed to parse double: \"FAIL\"");
REQUIRE(errors[2] == "parse_rect: Invalid height");
REQUIRE(errors[3] == "parse_rect: Invalid rx");
REQUIRE(errors[4] == "parse_rect: Invalid ry");
REQUIRE(errors[5] == "unable to parse invalid svg <path>");
REQUIRE(errors[6] == "unable to parse invalid svg <path> with id 'fail-path'");
REQUIRE(errors[7] == "unable to parse invalid svg <path> with id 'fail-path'");
REQUIRE(errors[8] == "parse_circle: Invalid radius");
REQUIRE(errors[9] == "Failed to parse <polygon> 'points'");
REQUIRE(errors[10] == "Failed to parse <polyline> 'points'");
REQUIRE(errors[11] == "parse_ellipse: Invalid rx");
REQUIRE(errors[12] == "parse_ellipse: Invalid ry");
REQUIRE(errors[13] == "parse_rect: Invalid height");
}
}
SECTION("SVG parser double % <fail>")
{
std::string svg_name("./test/data/svg/gradient-radial-error.svg");
std::ifstream in(svg_name.c_str());
std::string svg_str((std::istreambuf_iterator<char>(in)),
std::istreambuf_iterator<char>());
using namespace mapnik::svg;
mapnik::svg_storage_type path;
vertex_stl_adapter<svg_path_storage> stl_storage(path.source());
svg_path_adapter svg_path(stl_storage);
svg_converter_type svg(svg_path, path.attributes());
svg_parser p(svg);
if (!p.parse_from_string(svg_str))
{
auto const& errors = p.error_messages();
REQUIRE(errors.size() == 1);
REQUIRE(errors[0] == "Failed to parse double (optional %) from FAIL");
}
}
SECTION("SVG parser display=none")
{
std::string svg_name("./test/data/svg/invisible.svg");
std::shared_ptr<mapnik::marker const> marker = mapnik::marker_cache::instance().find(svg_name, false);
REQUIRE(marker);
REQUIRE(marker->is<mapnik::marker_svg>());
mapnik::marker_svg const& svg = mapnik::util::get<mapnik::marker_svg>(*marker);
auto bbox = svg.bounding_box();
REQUIRE(bbox == mapnik::box2d<double>(0, 0, 1, 1));
auto storage = svg.get_data();
REQUIRE(storage);
mapnik::svg::vertex_stl_adapter<mapnik::svg::svg_path_storage> stl_storage(storage->source());
mapnik::svg::svg_path_adapter path(stl_storage);
double x,y;
REQUIRE(path.vertex(&x,&y) == mapnik::SEG_END);
}
SECTION("SVG parser stroke-linecap=square")
{
std::string svg_name("./test/data/svg/stroke-linecap-square.svg");
std::shared_ptr<mapnik::marker const> marker = mapnik::marker_cache::instance().find(svg_name, false);
REQUIRE(marker);
REQUIRE(marker->is<mapnik::marker_svg>());
mapnik::marker_svg const& svg = mapnik::util::get<mapnik::marker_svg>(*marker);
auto bbox = svg.bounding_box();
REQUIRE(bbox == mapnik::box2d<double>(5, 60, 220, 60));
auto storage = svg.get_data();
REQUIRE(storage);
mapnik::svg::vertex_stl_adapter<mapnik::svg::svg_path_storage> stl_storage(storage->source());
mapnik::svg::svg_path_adapter path(stl_storage);
auto const& attrs = storage->attributes();
agg::line_cap_e expected_cap(agg::square_cap);
REQUIRE(attrs.size() == 1 );
REQUIRE(attrs[0].line_cap == expected_cap);
double x,y;
unsigned cmd;
std::vector<std::tuple<double,double,unsigned>> vec;
while ((cmd = path.vertex(&x,&y)) != mapnik::SEG_END)
{
vec.emplace_back(x, y, cmd);
}
std::vector<std::tuple<double,double,unsigned>> expected = { std::make_tuple(5, 60, 1),
std::make_tuple(220, 60, 2) };
REQUIRE(std::equal(expected.begin(),expected.end(), vec.begin()));
}
SECTION("SVG <rect>")
{
//<rect width="20" height="15" style="fill:rgb(0,0,255);stroke-width:1;stroke:rgb(0,0,0)" />
@ -129,6 +333,71 @@ TEST_CASE("SVG parser") {
REQUIRE(std::equal(expected.begin(),expected.end(), vec.begin(),detail::vertex_equal<3>()));
}
SECTION("SVG rounded <rect>s missing rx or ry")
{
std::string svg_name("./test/data/svg/rounded_rect-missing-one-radius.svg");
std::shared_ptr<mapnik::marker const> marker = mapnik::marker_cache::instance().find(svg_name, false);
REQUIRE(marker);
REQUIRE(marker->is<mapnik::marker_svg>());
mapnik::marker_svg const& svg = mapnik::util::get<mapnik::marker_svg>(*marker);
auto bbox = svg.bounding_box();
REQUIRE(bbox == mapnik::box2d<double>(0, 0, 20, 15));
auto storage = svg.get_data();
REQUIRE(storage);
mapnik::svg::vertex_stl_adapter<mapnik::svg::svg_path_storage> stl_storage(storage->source());
mapnik::svg::svg_path_adapter path(stl_storage);
double x,y;
unsigned cmd;
std::vector<std::tuple<double,double,unsigned>> vec;
while ((cmd = path.vertex(&x,&y)) != mapnik::SEG_END)
{
vec.emplace_back(x, y, cmd);
}
std::vector<std::tuple<double,double,unsigned>> expected = {std::make_tuple(0, 5,1),
std::make_tuple(0.481856, 2.85842,2),
std::make_tuple(1.83455, 1.12961,2),
std::make_tuple(3.79736, 0.146789,2),
std::make_tuple(5, 0,2),
std::make_tuple(15, 0,2),
std::make_tuple(17.1416, 0.481856,2),
std::make_tuple(18.8704, 1.83455,2),
std::make_tuple(19.8532, 3.79736,2),
std::make_tuple(20, 5,2),
std::make_tuple(20, 10,2),
std::make_tuple(19.5181, 12.1416,2),
std::make_tuple(18.1654, 13.8704,2),
std::make_tuple(16.2026, 14.8532,2),
std::make_tuple(15, 15,2),
std::make_tuple(5, 15,2),
std::make_tuple(2.85842, 14.5181,2),
std::make_tuple(1.12961, 13.1654,2),
std::make_tuple(0.146789, 11.2026,2),
std::make_tuple(0, 10,2),
std::make_tuple(0, 10,95)};
REQUIRE(std::equal(expected.begin(),expected.end(), vec.begin(),detail::vertex_equal<3>()));
}
SECTION("SVG beveled <rect>")
{
std::string svg_name("./test/data/svg/stroke-linejoin-bevel.svg");
std::shared_ptr<mapnik::marker const> marker = mapnik::marker_cache::instance().find(svg_name, false);
REQUIRE(marker);
REQUIRE(marker->is<mapnik::marker_svg>());
mapnik::marker_svg const& svg = mapnik::util::get<mapnik::marker_svg>(*marker);
auto bbox = svg.bounding_box();
REQUIRE(bbox == mapnik::box2d<double>(10, 10, 30, 25));
auto storage = svg.get_data();
REQUIRE(storage);
mapnik::svg::vertex_stl_adapter<mapnik::svg::svg_path_storage> stl_storage(storage->source());
auto const& attrs = storage->attributes();
agg::line_join_e expected_join(agg::bevel_join);
REQUIRE(attrs.size() == 1 );
REQUIRE(attrs[0].line_join == expected_join);
}
SECTION("SVG <line>")
{
//
@ -147,17 +416,11 @@ TEST_CASE("SVG parser") {
unsigned cmd;
std::vector<std::tuple<double,double,unsigned>> vec;
std::size_t num_vertices = path.total_vertices();
//std::cerr << "Num vertices = " << num_vertices << std::endl;
//std::cerr << "{";
for (std::size_t i = 0; i < num_vertices; ++i)
{
cmd = path.vertex(&x,&y);
vec.emplace_back(x, y, cmd);
//if (vec.size() > 1) std::cerr << ",";
//std::cerr << std::setprecision(6) << "std::make_tuple(" << x << ", " << y << ", " << cmd << ")";
}
//std::cerr << "}" << std::endl;
std::vector<std::tuple<double,double,unsigned>> expected = {std::make_tuple(1, 1, 1),
std::make_tuple(1199, 1, 2),
std::make_tuple(1199, 399, 2),
@ -291,5 +554,205 @@ TEST_CASE("SVG parser") {
REQUIRE(std::equal(expected.begin(),expected.end(), vec.begin()));
}
xmlCleanupParser();
SECTION("SVG <gradient>")
{
//
std::string svg_name("./test/data/svg/gradient.svg");
std::shared_ptr<mapnik::marker const> marker = mapnik::marker_cache::instance().find(svg_name, false);
REQUIRE(marker);
REQUIRE(marker->is<mapnik::marker_svg>());
mapnik::marker_svg const& svg = mapnik::util::get<mapnik::marker_svg>(*marker);
auto bbox = svg.bounding_box();
REQUIRE(bbox == mapnik::box2d<double>(1.0,1.0,799.0,599.0));
auto storage = svg.get_data();
REQUIRE(storage);
mapnik::svg::vertex_stl_adapter<mapnik::svg::svg_path_storage> stl_storage(storage->source());
mapnik::svg::svg_path_adapter path(stl_storage);
double x,y;
unsigned cmd;
std::vector<std::tuple<double,double,unsigned>> vec;
std::size_t num_vertices = path.total_vertices();
for (std::size_t i = 0; i < num_vertices; ++i)
{
cmd = path.vertex(&x,&y);
vec.emplace_back(x, y, cmd);
}
std::vector<std::tuple<double,double,unsigned>> expected = {std::make_tuple(1, 1, 1),
std::make_tuple(799, 1, 2),
std::make_tuple(799, 599, 2),
std::make_tuple(1, 599, 2),
std::make_tuple(1, 1, 79),
std::make_tuple(0, 0, 0),
std::make_tuple(100, 100, 1),
std::make_tuple(700, 100, 2),
std::make_tuple(700, 300, 2),
std::make_tuple(100, 300, 2),
std::make_tuple(100, 100, 79),
std::make_tuple(0, 0, 0),
std::make_tuple(100, 320, 1),
std::make_tuple(700, 320, 2),
std::make_tuple(700, 520, 2),
std::make_tuple(100, 520, 2),
std::make_tuple(100, 320, 79)};
REQUIRE(std::equal(expected.begin(),expected.end(), vec.begin()));
}
SECTION("SVG missing <gradient> def")
{
std::string svg_name("./test/data/svg/gradient-nodef.svg");
using namespace mapnik::svg;
mapnik::svg_storage_type path;
vertex_stl_adapter<svg_path_storage> stl_storage(path.source());
svg_path_adapter svg_path(stl_storage);
svg_converter_type svg(svg_path, path.attributes());
svg_parser p(svg);
REQUIRE(!p.parse(svg_name));
auto const& errors = p.error_messages();
REQUIRE(errors.size() == 2);
REQUIRE(errors[0] == "Failed to find gradient fill: MyGradient");
REQUIRE(errors[1] == "Failed to find gradient stroke: MyGradient");
}
SECTION("SVG missing <gradient> id")
{
std::string svg_name("./test/data/svg/gradient-no-id.svg");
std::ifstream in(svg_name.c_str());
std::string svg_str((std::istreambuf_iterator<char>(in)),
std::istreambuf_iterator<char>());
using namespace mapnik::svg;
mapnik::svg_storage_type path;
vertex_stl_adapter<svg_path_storage> stl_storage(path.source());
svg_path_adapter svg_path(stl_storage);
svg_converter_type svg(svg_path, path.attributes());
svg_parser p(svg);
if (!p.parse_from_string(svg_str))
{
auto const& errors = p.error_messages();
REQUIRE(errors.size() == 2);
REQUIRE(errors[0] == "Failed to find gradient fill: MyGradient");
REQUIRE(errors[1] == "Failed to find gradient stroke: MyGradient");
}
}
SECTION("SVG missing <gradient> inheritance")
{
//
std::string svg_name("./test/data/svg/gradient-inherit.svg");
std::shared_ptr<mapnik::marker const> marker = mapnik::marker_cache::instance().find(svg_name, false);
REQUIRE(marker);
REQUIRE(marker->is<mapnik::marker_svg>());
mapnik::marker_svg const& svg = mapnik::util::get<mapnik::marker_svg>(*marker);
auto bbox = svg.bounding_box();
REQUIRE(bbox == mapnik::box2d<double>(1.0,1.0,699.0,199.0));
auto storage = svg.get_data();
REQUIRE(storage);
mapnik::svg::vertex_stl_adapter<mapnik::svg::svg_path_storage> stl_storage(storage->source());
auto const& attrs = storage->attributes();
REQUIRE(attrs.size() == 3 );
REQUIRE(attrs[1].fill_gradient == attrs[2].fill_gradient);
mapnik::svg::svg_path_adapter path(stl_storage);
double x,y;
unsigned cmd;
std::vector<std::tuple<double,double,unsigned>> vec;
std::size_t num_vertices = path.total_vertices();
for (std::size_t i = 0; i < num_vertices; ++i)
{
cmd = path.vertex(&x,&y);
vec.emplace_back(x, y, cmd);
}
std::vector<std::tuple<double,double,unsigned>> expected = {std::make_tuple(1, 1, 1),
std::make_tuple(699, 1, 2),
std::make_tuple(699, 199, 2),
std::make_tuple(1, 199, 2),
std::make_tuple(1, 1, 79),
std::make_tuple(0, 0, 0),
std::make_tuple(100, 50, 1),
std::make_tuple(300, 50, 2),
std::make_tuple(300, 150, 2),
std::make_tuple(100, 150, 2),
std::make_tuple(100, 50, 79),
std::make_tuple(0, 0, 0),
std::make_tuple(400, 50, 1),
std::make_tuple(600, 50, 2),
std::make_tuple(600, 150, 2),
std::make_tuple(400, 150, 2),
std::make_tuple(400, 50, 79)};
REQUIRE(std::equal(expected.begin(),expected.end(), vec.begin()));
}
SECTION("SVG <gradient> with transformations")
{
//
std::string svg_name("./test/data/svg/gradient-transform.svg");
std::shared_ptr<mapnik::marker const> marker = mapnik::marker_cache::instance().find(svg_name, false);
REQUIRE(marker);
REQUIRE(marker->is<mapnik::marker_svg>());
mapnik::marker_svg const& svg = mapnik::util::get<mapnik::marker_svg>(*marker);
auto bbox = svg.bounding_box();
REQUIRE(bbox == mapnik::box2d<double>(1.0,1.0,799.0,599.0));
auto storage = svg.get_data();
REQUIRE(storage);
auto const& attrs = storage->attributes();
REQUIRE(attrs.size() == 3 );
REQUIRE(attrs[1].fill_gradient == attrs[2].fill_gradient);
REQUIRE(attrs[1].fill_gradient.get_gradient_type() == mapnik::RADIAL);
agg::trans_affine transform;
transform *= agg::trans_affine_translation(240,155);
REQUIRE(attrs[1].fill_gradient.get_transform() == transform);
}
SECTION("SVG <gradient> with xlink:href")
{
std::string svg_name("./test/data/svg/gradient-xhref.svg");
std::shared_ptr<mapnik::marker const> marker = mapnik::marker_cache::instance().find(svg_name, false);
REQUIRE(marker);
REQUIRE(marker->is<mapnik::marker_svg>());
mapnik::marker_svg const& svg = mapnik::util::get<mapnik::marker_svg>(*marker);
auto bbox = svg.bounding_box();
REQUIRE(bbox == mapnik::box2d<double>(20,20,460,230));
auto storage = svg.get_data();
REQUIRE(storage);
auto const& attrs = storage->attributes();
REQUIRE(attrs.size() == 2 );
REQUIRE(attrs[0].fill_gradient.get_gradient_type() == mapnik::LINEAR);
REQUIRE(attrs[1].fill_gradient.get_gradient_type() == mapnik::LINEAR);
REQUIRE(attrs[1].fill_gradient.has_stop());
}
SECTION("SVG <gradient> with radial percents")
{
std::string svg_name("./test/data/svg/gradient-radial-percents.svg");
std::shared_ptr<mapnik::marker const> marker = mapnik::marker_cache::instance().find(svg_name, false);
REQUIRE(marker);
REQUIRE(marker->is<mapnik::marker_svg>());
mapnik::marker_svg const& svg = mapnik::util::get<mapnik::marker_svg>(*marker);
auto bbox = svg.bounding_box();
REQUIRE(bbox == mapnik::box2d<double>(0,0,200,200));
auto storage = svg.get_data();
REQUIRE(storage);
double x1, x2, y1, y2, r;
auto const& attrs = storage->attributes();
REQUIRE(attrs.size() == 1 );
REQUIRE(attrs[0].fill_gradient.get_gradient_type() == mapnik::RADIAL);
REQUIRE(attrs[0].fill_gradient.has_stop());
attrs[0].fill_gradient.get_control_points(x1, y1, x2, y2, r);
REQUIRE(x1 == 0);
REQUIRE(y1 == 0.25);
REQUIRE(x2 == 0.10);
REQUIRE(y2 == 0.10);
REQUIRE(r == 0.75);
}
}

View file

@ -48,8 +48,6 @@
#include "agg_pixfmt_rgba.h"
#include "agg_scanline_u.h"
#include <libxml/parser.h> // for xmlInitParser(), xmlCleanupParser()
struct main_marker_visitor
{
main_marker_visitor(std::string const& svg_name,
@ -59,18 +57,6 @@ struct main_marker_visitor
verbose_(verbose),
auto_open_(auto_open) {}
int operator() (mapnik::marker_null const&)
{
std::clog << "svg2png error: '" << svg_name_ << "' is not a valid vector!\n";
return -1;
}
int operator() (mapnik::marker_rgba8 const&)
{
std::clog << "svg2png error: '" << svg_name_ << "' is not a valid vector!\n";
return -1;
}
int operator() (mapnik::marker_svg const& marker)
{
using pixfmt = agg::pixfmt_rgba32_pre;
@ -130,6 +116,14 @@ struct main_marker_visitor
return status;
}
// default
template <typename T>
int operator() (T const&)
{
std::clog << "svg2png error: '" << svg_name_ << "' is not a valid vector!\n";
return -1;
}
private:
std::string const& svg_name_;
bool verbose_;
@ -202,8 +196,6 @@ int main (int argc,char** argv)
return 0;
}
xmlInitParser();
while (itr != svg_files.end())
{
std::string svg_name (*itr++);
@ -217,16 +209,15 @@ int main (int argc,char** argv)
status = mapnik::util::apply_visitor(visitor, *marker);
}
}
catch (std::exception const& ex)
{
std::clog << "Exception caught:" << ex.what() << std::endl;
return -1;
}
catch (...)
{
std::clog << "Exception of unknown type!" << std::endl;
xmlCleanupParser();
return -1;
}
// only call this once, on exit
// to make sure valgrind output is clean
// http://xmlsoft.org/xmlmem.html
xmlCleanupParser();
return status;
}