mapnik/test/unit/svg/svg_renderer_test.cpp

109 lines
4 KiB
C++
Raw Normal View History

/*****************************************************************************
*
* This file is part of Mapnik (c++ mapping toolkit)
*
2021-01-05 15:39:07 +01:00
* Copyright (C) 2021 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
*
*****************************************************************************/
#include "catch.hpp"
#include <mapnik/debug.hpp>
#include <mapnik/marker.hpp>
#include <mapnik/marker_cache.hpp>
#include <mapnik/image_util.hpp>
#include <mapnik/svg/svg_parser.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 <boost/range/combine.hpp>
#include <mapnik/warning.hpp>
MAPNIK_DISABLE_WARNING_PUSH
#include <mapnik/warning_ignore_agg.hpp>
#include "agg_rasterizer_scanline_aa.h"
#include "agg_basics.h"
#include "agg_rendering_buffer.h"
#include "agg_renderer_base.h"
#include "agg_pixfmt_rgba.h"
#include "agg_scanline_u.h"
MAPNIK_DISABLE_WARNING_POP
2022-01-26 23:25:53 +01:00
namespace {
mapnik::image_rgba8 render_svg(std::string const& filename, double scale_factor)
{
2022-01-26 23:25:53 +01:00
using pixfmt = agg::pixfmt_rgba32_pre;
using renderer_base = agg::renderer_base<pixfmt>;
using renderer_solid = agg::renderer_scanline_aa_solid<renderer_base>;
2022-01-26 23:25:53 +01:00
agg::rasterizer_scanline_aa<> ras_ptr;
agg::scanline_u8 sl;
std::shared_ptr<mapnik::marker const> marker = mapnik::marker_cache::instance().find(filename, false);
mapnik::marker_svg const& svg = mapnik::util::get<mapnik::marker_svg>(*marker);
double svg_width, svg_height;
std::tie(svg_width, svg_height) = svg.dimensions();
int image_width = static_cast<int>(std::round(svg_width));
int image_height = static_cast<int>(std::round(svg_height));
2022-01-26 23:25:53 +01:00
mapnik::image_rgba8 im(image_width, image_height, true, true);
agg::rendering_buffer buf(im.bytes(), im.width(), im.height(), im.row_size());
pixfmt pixf(buf);
renderer_base renb(pixf);
2022-01-26 23:25:53 +01:00
agg::trans_affine mtx = agg::trans_affine_translation(-0.5 * svg_width, -0.5 * svg_height);
mtx.scale(scale_factor);
mtx.translate(0.5 * svg_width, 0.5 * svg_height);
2022-01-26 23:25:53 +01:00
mapnik::svg::vertex_stl_adapter<mapnik::svg::svg_path_storage> stl_storage(svg.get_data()->source());
mapnik::svg::svg_path_adapter svg_path(stl_storage);
mapnik::svg::
renderer_agg<mapnik::svg_path_adapter, mapnik::svg_attribute_type, renderer_solid, agg::pixfmt_rgba32_pre>
renderer(svg_path, svg.get_data()->attributes());
double opacity = 1.0;
renderer.render(ras_ptr, sl, renb, mtx, opacity, {0, 0, svg_width, svg_height});
return im;
}
bool equal(mapnik::image_rgba8 const& im1, mapnik::image_rgba8 const& im2)
{
if (im1.width() != im2.width() || im1.height() != im2.height())
return false;
2022-01-26 23:25:53 +01:00
for (auto tup : boost::combine(im1, im2))
{
2022-01-26 23:25:53 +01:00
if (boost::get<0>(tup) != boost::get<1>(tup))
return false;
}
return true;
}
2022-01-26 23:25:53 +01:00
} // namespace
2022-01-26 23:25:53 +01:00
TEST_CASE("SVG renderer")
{
SECTION("SVG octocat inline/css")
{
double scale_factor = 1.0;
std::string octocat_inline("./test/data/svg/octocat.svg");
std::string octocat_css("./test/data/svg/octocat-css.svg");
auto image1 = render_svg(octocat_inline, 1.0);
auto image2 = render_svg(octocat_css, 1.0);
REQUIRE(equal(image1, image2));
}
}