add svg_renderer to visual tests

This commit is contained in:
Dane Springmeyer 2015-05-23 11:08:51 -07:00
parent 9a697a7dd6
commit 99560b23ab
3 changed files with 56 additions and 6 deletions

View File

@ -28,6 +28,7 @@
// stl
#include <sstream>
#include <iomanip>
#include <fstream>
// mapnik
#include <mapnik/map.hpp>
@ -36,6 +37,9 @@
#include <mapnik/cairo/cairo_renderer.hpp>
#include <mapnik/cairo/cairo_image_util.hpp>
#endif
#if defined(SVG_RENDERER)
#include <mapnik/svg/output/svg_renderer.hpp>
#endif
// boost
#include <boost/filesystem.hpp>
@ -48,6 +52,8 @@ struct renderer_base
{
using image_type = ImageType;
static constexpr const char * ext = ".png";
unsigned compare(image_type const & actual, boost::filesystem::path const& reference) const
{
return compare_images(actual, reference.string());
@ -92,6 +98,47 @@ struct cairo_renderer : renderer_base<mapnik::image_rgba8>
};
#endif
#if defined(SVG_RENDERER)
struct svg_renderer : renderer_base<std::string>
{
static constexpr const char * name = "svg";
static constexpr const char * ext = ".svg";
image_type render(mapnik::Map const & map, double scale_factor) const
{
std::stringstream ss;
std::ostream_iterator<char> output_stream_iterator(ss);
mapnik::svg_renderer<std::ostream_iterator<char>> ren(map, output_stream_iterator, scale_factor);
ren.apply();
return ss.str();
}
unsigned compare(image_type const & actual, boost::filesystem::path const& reference) const
{
std::ifstream stream(reference.string().c_str(),std::ios_base::in|std::ios_base::binary);
if (!stream.is_open())
{
throw std::runtime_error("could not open: '" + reference.string() + "'");
}
std::string expected(std::istreambuf_iterator<char>(stream.rdbuf()),(std::istreambuf_iterator<char>()));
stream.close();
return std::fabs(actual.size() - expected.size());
}
void save(image_type const & image, boost::filesystem::path const& path) const
{
std::ofstream file(path.string().c_str(), std::ios::out | std::ios::trunc | std::ios::binary);
if (!file) {
throw std::runtime_error((std::string("cannot open file for writing file ") + path.string()).c_str());
} else {
file << image;
file.close();
}
}
};
#endif
struct grid_renderer : renderer_base<mapnik::image_gray8>
{
static constexpr const char * name = "grid";
@ -116,7 +163,7 @@ public:
result test(std::string const & name, mapnik::Map const & map, double scale_factor) const
{
typename Renderer::image_type image(ren.render(map, scale_factor));
boost::filesystem::path reference = reference_dir / image_file_name(name, map.width(), map.height(), scale_factor, true);
boost::filesystem::path reference = reference_dir / image_file_name(name, map.width(), map.height(), scale_factor, true, Renderer::ext);
bool reference_exists = boost::filesystem::exists(reference);
result res;
@ -131,7 +178,7 @@ public:
if (res.diff)
{
boost::filesystem::create_directories(output_dir);
boost::filesystem::path path = output_dir / image_file_name(name, map.width(), map.height(), scale_factor);
boost::filesystem::path path = output_dir / image_file_name(name, map.width(), map.height(), scale_factor, false, Renderer::ext);
res.actual_image_path = path;
res.state = STATE_FAIL;
ren.save(image, path);
@ -151,12 +198,13 @@ private:
double width,
double height,
double scale_factor,
bool reference=false) const
bool reference,
std::string const& ext) const
{
std::stringstream s;
s << test_name << '-' << width << '-' << height << '-'
<< std::fixed << std::setprecision(1) << scale_factor
<< '-' << Renderer::name << (reference ? "-reference" : "") << ".png";
<< '-' << Renderer::name << (reference ? "-reference" : "") << ext;
return s.str();
}

View File

@ -61,7 +61,8 @@ runner::runner(runner::path_type const & styles_dir,
reference_dir_(reference_dir),
jobs_(jobs),
renderers_{ renderer<agg_renderer>(output_dir_, reference_dir_, overwrite),
renderer<cairo_renderer>(output_dir_, reference_dir_, overwrite)/*,
renderer<cairo_renderer>(output_dir_, reference_dir_, overwrite),
renderer<svg_renderer>(output_dir_, reference_dir_, overwrite)/*,
renderer<grid_renderer>(output_dir_, reference_dir_, overwrite)*/ }
{
}

View File

@ -36,7 +36,8 @@ namespace visual_tests
class runner
{
using renderer_type = mapnik::util::variant<renderer<agg_renderer>,
renderer<cairo_renderer>/*,
renderer<cairo_renderer>,
renderer<svg_renderer>/*,
renderer<grid_renderer>*/>;
using path_type = boost::filesystem::path;
using files_iterator = std::vector<path_type>::const_iterator;