Fix test cases.

This commit is contained in:
Daniel Patterson 2015-06-17 20:50:01 -07:00
parent 51a50fdf92
commit 192495ce21
3 changed files with 40 additions and 25 deletions

View file

@ -45,7 +45,7 @@ namespace mapnik { namespace svg {
~svg_parser(); ~svg_parser();
void parse(std::string const& filename); void parse(std::string const& filename);
void parse_from_string(std::string const& svg); void parse_from_string(std::string const& svg);
template <typename T> void parse_from_stream(boost::iostreams::stream<T> const& svg_stream); template <typename T> void parse_from_stream(boost::iostreams::stream<T> &svg_stream);
svg_converter_type & path_; svg_converter_type & path_;
bool is_defs_; bool is_defs_;
std::map<std::string, gradient> gradient_map_; std::map<std::string, gradient> gradient_map_;

View file

@ -57,6 +57,7 @@
namespace mapnik { namespace svg { namespace mapnik { namespace svg {
bool parse_reader(svg_parser & parser,xmlTextReaderPtr reader);
void process_node(svg_parser & parser,xmlTextReaderPtr reader); void process_node(svg_parser & parser,xmlTextReaderPtr reader);
void start_element(svg_parser & parser,xmlTextReaderPtr reader); void start_element(svg_parser & parser,xmlTextReaderPtr reader);
void end_element(svg_parser & parser,xmlTextReaderPtr reader); void end_element(svg_parser & parser,xmlTextReaderPtr reader);
@ -1091,34 +1092,37 @@ void svg_parser::parse_from_string(std::string const& svg)
} }
} }
template <typename T> int _xmlInputReadCallback(void * stream, char * buffer, int len) {
int _xmlInputReadCallback(boost::iostreams::stream<T> * stream, char * buffer, int len) { std::istream *s = reinterpret_cast<std::istream *>(stream);
stream->read(buffer,len); s->read(buffer,len);
return stream->gcount(); return s->gcount();
} }
template <typename T> template <typename T>
int _xmlInputCloseCallback(boost::iostreams::stream<T> * stream) int _xmlInputCloseCallback(void * stream)
{ {
if(stream->close()) boost::iostreams::stream<T> *s = reinterpret_cast<boost::iostreams::stream<T> *>(stream);
{ try {
return 0; s->close();
return 0;
}
catch (...) {
return -1;
} }
return -1;
} }
template <typename T> template <typename T>
void svg_parser::parse_from_stream(boost::iostreams::stream<T> const& svg_stream) void svg_parser::parse_from_stream(boost::iostreams::stream<T> &svg_stream)
{ {
xmlTextReaderPtr reader = xmlReaderForIO(_xmlInputReadCallback<T>, xmlTextReaderPtr reader = xmlReaderForIO(_xmlInputReadCallback,
_xmlInputCloseCallback<T>, _xmlInputCloseCallback<T>,
reinterpret_cast<const void *>(&svg_stream), reinterpret_cast<void *>(&svg_stream),
nullptr, nullptr,
nullptr, nullptr,
(XML_PARSE_NOBLANKS | XML_PARSE_NOCDATA | XML_PARSE_NOERROR | XML_PARSE_NOWARNING)); (XML_PARSE_NOBLANKS | XML_PARSE_NOCDATA | XML_PARSE_NOERROR | XML_PARSE_NOWARNING));
if (reader == nullptr) if (reader == nullptr)
{ {
MAPNIK_LOG_ERROR(svg_parser) << "Unable to parse SVG"; MAPNIK_LOG_ERROR(svg_parser) << "Unable to parse SVG XML";
} }
else if (!parse_reader(*this,reader)) else if (!parse_reader(*this,reader))
{ {
@ -1126,9 +1130,7 @@ void svg_parser::parse_from_stream(boost::iostreams::stream<T> const& svg_stream
} }
} }
template void svg_parser::parse_from_stream(boost::iostreams::stream<boost::iostreams::file_source> const& svg_stream); template void svg_parser::parse_from_stream(boost::iostreams::stream<boost::iostreams::file_source> & svg_stream);
template void svg_parser::parse_from_stream(boost::iostreams::stream<boost::iostreams::array_source> const& svg_stream); template void svg_parser::parse_from_stream(boost::iostreams::stream<boost::iostreams::array_source> & svg_stream);
}} }}

View file

@ -26,31 +26,44 @@ SECTION("svg_invalid")
{ {
std::string imagedata = "<svg/svg>"; std::string imagedata = "<svg/svg>";
std::unique_ptr<mapnik::image_reader> reader(mapnik::get_image_reader(imagedata.c_str(), imagedata.length())); std::unique_ptr<mapnik::image_reader> reader(mapnik::get_image_reader(imagedata.c_str(), imagedata.length()));
CHECK_FALSE(reader.get()); CHECK(reader.get());
unsigned width = reader->width();
unsigned height = reader->height();
CHECK(width == 0);
CHECK(height == 0);
} }
SECTION("svg_empty") SECTION("svg_empty")
{ {
std::string imagedata = "<svg></svg>"; std::string imagedata = "<svg></svg>";
std::unique_ptr<mapnik::image_reader> reader(mapnik::get_image_reader(imagedata.c_str(), imagedata.length())); std::unique_ptr<mapnik::image_reader> reader(mapnik::get_image_reader(imagedata.c_str(), imagedata.length()));
CHECK_FALSE(reader.get()); CHECK(reader.get());
unsigned width = reader->width();
unsigned height = reader->height();
CHECK(width == 0);
CHECK(height == 0);
} }
SECTION("svg_blank") SECTION("svg_blank")
{ {
std::string imagedata = "<svg width='100' height='100'><g id='a'><ellipse fill='#FFFFFF' stroke='#000000' stroke-width='4' cx='50' cy='50' rx='25' ry='25'></g></svg>"; std::string imagedata = "<svg width='100' height='100'><g id='a'><ellipse fill='#FFFFFF' stroke='#000000' stroke-width='4' cx='50' cy='50' rx='25' ry='25'/></g></svg>";
std::unique_ptr<mapnik::image_reader> reader(mapnik::get_image_reader(imagedata.c_str(), imagedata.length())); std::unique_ptr<mapnik::image_reader> reader(mapnik::get_image_reader(imagedata.c_str(), imagedata.length()));
CHECK(reader.get()); CHECK(reader.get());
unsigned width = reader->width(); unsigned width = reader->width();
unsigned height = reader->height(); unsigned height = reader->height();
CHECK(width == 100); CHECK(width == 100);
CHECK(height == 100); CHECK(height == 100);
mapnik::image_any im = reader->read(0,0,width,height); mapnik::image_any im = reader->read(0,0,width,height);
mapnik::image_rgba8 raw = im.get<mapnik::image_rgba8>(); mapnik::image_rgba8 raw = im.get<mapnik::image_rgba8>();
std::string pngdata = mapnik::save_to_string<mapnik::image_rgba8>(raw,"png");
CHECK(pngdata.length() == 1270 )
} // END SECTION } // END SECTION
} // END TEST CASE } // END TEST CASE