From 99560b23ab297a7b3a7a4ed33732fd4e01d9b124 Mon Sep 17 00:00:00 2001 From: Dane Springmeyer Date: Sat, 23 May 2015 11:08:51 -0700 Subject: [PATCH] add svg_renderer to visual tests --- test/visual/renderer.hpp | 56 +++++++++++++++++++++++++++++++++++++--- test/visual/runner.cpp | 3 ++- test/visual/runner.hpp | 3 ++- 3 files changed, 56 insertions(+), 6 deletions(-) diff --git a/test/visual/renderer.hpp b/test/visual/renderer.hpp index 58cb4cd72..d3b9cda77 100644 --- a/test/visual/renderer.hpp +++ b/test/visual/renderer.hpp @@ -28,6 +28,7 @@ // stl #include #include +#include // mapnik #include @@ -36,6 +37,9 @@ #include #include #endif +#if defined(SVG_RENDERER) +#include +#endif // boost #include @@ -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 }; #endif +#if defined(SVG_RENDERER) +struct svg_renderer : renderer_base +{ + 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 output_stream_iterator(ss); + mapnik::svg_renderer> 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(stream.rdbuf()),(std::istreambuf_iterator())); + 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 { 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(); } diff --git a/test/visual/runner.cpp b/test/visual/runner.cpp index 64b475f0c..ed8ef25b6 100644 --- a/test/visual/runner.cpp +++ b/test/visual/runner.cpp @@ -61,7 +61,8 @@ runner::runner(runner::path_type const & styles_dir, reference_dir_(reference_dir), jobs_(jobs), renderers_{ renderer(output_dir_, reference_dir_, overwrite), - renderer(output_dir_, reference_dir_, overwrite)/*, + renderer(output_dir_, reference_dir_, overwrite), + renderer(output_dir_, reference_dir_, overwrite)/*, renderer(output_dir_, reference_dir_, overwrite)*/ } { } diff --git a/test/visual/runner.hpp b/test/visual/runner.hpp index 848249d8a..c47642466 100644 --- a/test/visual/runner.hpp +++ b/test/visual/runner.hpp @@ -36,7 +36,8 @@ namespace visual_tests class runner { using renderer_type = mapnik::util::variant, - renderer/*, + renderer, + renderer/*, renderer*/>; using path_type = boost::filesystem::path; using files_iterator = std::vector::const_iterator;