2011-01-26 02:19:01 +01:00
|
|
|
/*****************************************************************************
|
2011-11-14 04:54:32 +01:00
|
|
|
*
|
2011-01-26 02:19:01 +01:00
|
|
|
* This file is part of Mapnik (c++ mapping toolkit)
|
|
|
|
*
|
2021-01-05 15:39:07 +01:00
|
|
|
* Copyright (C) 2021 Artem Pavlenko
|
2011-01-26 02:19:01 +01:00
|
|
|
*
|
|
|
|
* 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 <iostream>
|
2011-01-26 10:14:57 +01:00
|
|
|
#include <sstream>
|
2011-01-26 02:19:01 +01:00
|
|
|
#include <vector>
|
2012-04-11 19:41:50 +02:00
|
|
|
#include <string>
|
2020-01-23 16:48:16 +01:00
|
|
|
#include <cmath>
|
2011-01-26 02:19:01 +01:00
|
|
|
|
2022-02-02 17:20:29 +01:00
|
|
|
#include <mapnik/mapnik.hpp>
|
2012-04-11 19:41:50 +02:00
|
|
|
#include <mapnik/version.hpp>
|
2012-04-11 21:35:07 +02:00
|
|
|
#include <mapnik/debug.hpp>
|
2011-01-26 02:19:01 +01:00
|
|
|
#include <mapnik/marker.hpp>
|
|
|
|
#include <mapnik/marker_cache.hpp>
|
|
|
|
#include <mapnik/image_util.hpp>
|
|
|
|
#include <mapnik/svg/svg_path_adapter.hpp>
|
2012-08-28 02:58:49 +02:00
|
|
|
#include <mapnik/svg/svg_renderer_agg.hpp>
|
2011-01-26 02:19:01 +01:00
|
|
|
#include <mapnik/svg/svg_path_attributes.hpp>
|
|
|
|
|
2020-11-19 15:30:30 +01:00
|
|
|
#include <mapnik/warning.hpp>
|
|
|
|
MAPNIK_DISABLE_WARNING_PUSH
|
2015-11-08 02:53:09 +01:00
|
|
|
#include <mapnik/warning_ignore.hpp>
|
2011-01-26 02:19:01 +01:00
|
|
|
#include <boost/algorithm/string.hpp>
|
|
|
|
#include <boost/program_options.hpp>
|
2020-11-19 15:30:30 +01:00
|
|
|
MAPNIK_DISABLE_WARNING_POP
|
2011-01-26 02:19:01 +01:00
|
|
|
|
2020-11-19 15:30:30 +01:00
|
|
|
#include <mapnik/warning.hpp>
|
|
|
|
MAPNIK_DISABLE_WARNING_PUSH
|
2016-03-10 01:43:00 +01:00
|
|
|
#include <mapnik/warning_ignore_agg.hpp>
|
2011-01-26 02:19:01 +01:00
|
|
|
#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"
|
2020-11-19 15:30:30 +01:00
|
|
|
MAPNIK_DISABLE_WARNING_POP
|
2016-03-10 01:43:00 +01:00
|
|
|
|
2015-02-19 19:19:01 +01:00
|
|
|
struct main_marker_visitor
|
|
|
|
{
|
2022-11-23 17:18:18 +01:00
|
|
|
main_marker_visitor(std::string const& svg_name, double scale_factor, double opacity, bool verbose, bool auto_open)
|
2022-11-25 14:54:41 +01:00
|
|
|
: svg_name_(svg_name)
|
|
|
|
, scale_factor_(scale_factor)
|
|
|
|
, opacity_(opacity)
|
|
|
|
, verbose_(verbose)
|
|
|
|
, auto_open_(auto_open)
|
2022-01-26 20:41:37 +01:00
|
|
|
{}
|
|
|
|
|
|
|
|
int operator()(mapnik::marker_svg const& marker) const
|
2015-02-19 19:19:01 +01:00
|
|
|
{
|
2022-11-18 16:17:58 +01:00
|
|
|
#if 1
|
|
|
|
using color_type = agg::rgba8;
|
|
|
|
using order_type = agg::order_rgba;
|
2022-11-25 14:54:41 +01:00
|
|
|
using blender_type = agg::comp_op_adaptor_rgba_pre<color_type, order_type>; // comp blender
|
2022-11-18 16:17:58 +01:00
|
|
|
using buf_type = agg::rendering_buffer;
|
|
|
|
using pixfmt = agg::pixfmt_custom_blend_rgba<blender_type, buf_type>;
|
|
|
|
using renderer_base = agg::renderer_base<pixfmt>;
|
|
|
|
using renderer_solid = agg::renderer_scanline_aa_solid<renderer_base>;
|
|
|
|
#else
|
2015-02-19 19:19:01 +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-11-18 16:17:58 +01:00
|
|
|
#endif
|
2015-02-19 19:19:01 +01:00
|
|
|
agg::rasterizer_scanline_aa<> ras_ptr;
|
|
|
|
agg::scanline_u8 sl;
|
|
|
|
|
2017-06-23 16:17:29 +02:00
|
|
|
double w, h;
|
|
|
|
std::tie(w, h) = marker.dimensions();
|
2020-02-05 16:06:11 +01:00
|
|
|
if (w == 0 || h == 0)
|
|
|
|
{
|
|
|
|
if (verbose_)
|
|
|
|
{
|
|
|
|
std::clog << "Invalid SVG dimensions: " << w << "," << h << " using svgBBOX()" << std::endl;
|
|
|
|
}
|
|
|
|
auto b = marker.bounding_box();
|
|
|
|
w = b.width();
|
|
|
|
h = b.height();
|
|
|
|
}
|
2020-01-21 11:33:04 +01:00
|
|
|
|
2020-01-22 11:45:15 +01:00
|
|
|
double svg_width = w * scale_factor_;
|
|
|
|
double svg_height = h * scale_factor_;
|
2020-01-21 11:33:04 +01:00
|
|
|
|
2021-04-15 16:53:04 +02:00
|
|
|
int output_width = std::max(1, static_cast<int>(std::round(svg_width)));
|
|
|
|
int output_height = std::max(1, static_cast<int>(std::round(svg_height)));
|
2015-02-19 19:19:01 +01:00
|
|
|
if (verbose_)
|
|
|
|
{
|
2020-02-05 16:06:11 +01:00
|
|
|
std::clog << "SVG width of '" << w << "' and height of '" << h << "'\n";
|
2020-01-21 11:33:04 +01:00
|
|
|
std::clog << "Output image dimensions:[" << output_width << "," << output_height << "]" << std::endl;
|
2015-02-19 19:19:01 +01:00
|
|
|
}
|
2020-01-22 11:49:10 +01:00
|
|
|
mapnik::image_rgba8 im(output_width, output_height, true, true);
|
2015-05-04 12:49:11 +02:00
|
|
|
agg::rendering_buffer buf(im.bytes(), im.width(), im.height(), im.row_size());
|
2015-02-19 19:19:01 +01:00
|
|
|
pixfmt pixf(buf);
|
|
|
|
renderer_base renb(pixf);
|
|
|
|
|
2021-04-12 15:16:09 +02:00
|
|
|
mapnik::box2d<double> bbox = {0, 0, svg_width, svg_height};
|
2020-01-21 11:33:04 +01:00
|
|
|
// center the svg marker on '0,0'
|
|
|
|
agg::trans_affine mtx = agg::trans_affine_translation(-0.5 * w, -0.5 * h);
|
|
|
|
// Scale the image
|
|
|
|
mtx.scale(scale_factor_);
|
|
|
|
// render the marker at the center of the marker box
|
|
|
|
mtx.translate(0.5 * svg_width, 0.5 * svg_height);
|
|
|
|
|
2015-02-19 19:19:01 +01:00
|
|
|
mapnik::svg::vertex_stl_adapter<mapnik::svg::svg_path_storage> stl_storage(marker.get_data()->source());
|
|
|
|
mapnik::svg::svg_path_adapter svg_path(stl_storage);
|
2022-11-18 16:17:58 +01:00
|
|
|
mapnik::svg::renderer_agg<mapnik::svg_path_adapter, mapnik::svg_attribute_type, renderer_solid, pixfmt>
|
2022-11-25 14:54:41 +01:00
|
|
|
svg_renderer_this(svg_path, marker.get_data()->svg_group());
|
2015-02-19 19:19:01 +01:00
|
|
|
|
2022-11-23 17:18:18 +01:00
|
|
|
svg_renderer_this.render(ras_ptr, sl, renb, mtx, opacity_, bbox);
|
2015-02-19 19:19:01 +01:00
|
|
|
|
2015-07-20 12:05:26 +02:00
|
|
|
std::string png_name(svg_name_);
|
2022-01-26 20:41:37 +01:00
|
|
|
boost::algorithm::ireplace_last(png_name, ".svg", ".png");
|
2015-02-19 19:19:01 +01:00
|
|
|
demultiply_alpha(im);
|
2022-01-26 20:41:37 +01:00
|
|
|
mapnik::save_to_file<mapnik::image_rgba8>(im, png_name, "png");
|
2015-07-20 12:05:26 +02:00
|
|
|
int status = 0;
|
2015-02-19 19:19:01 +01:00
|
|
|
if (auto_open_)
|
|
|
|
{
|
|
|
|
std::ostringstream s;
|
2024-03-07 10:09:56 +01:00
|
|
|
#ifdef __APPLE__
|
2015-07-20 12:05:26 +02:00
|
|
|
s << "open " << png_name;
|
2015-02-19 19:19:01 +01:00
|
|
|
#else
|
2015-07-20 12:05:26 +02:00
|
|
|
s << "xdg-open " << png_name;
|
2015-02-19 19:19:01 +01:00
|
|
|
#endif
|
2015-10-19 19:01:33 +02:00
|
|
|
int ret = std::system(s.str().c_str());
|
2015-02-19 19:19:01 +01:00
|
|
|
if (ret != 0)
|
2015-07-20 12:05:26 +02:00
|
|
|
status = ret;
|
2015-02-19 19:19:01 +01:00
|
|
|
}
|
2015-07-20 14:51:24 +02:00
|
|
|
std::clog << "rendered to: " << png_name << "\n";
|
2015-07-20 12:05:26 +02:00
|
|
|
return status;
|
2015-02-19 19:19:01 +01:00
|
|
|
}
|
|
|
|
|
2015-07-16 13:06:52 +02:00
|
|
|
// default
|
2022-01-26 20:41:37 +01:00
|
|
|
template<typename T>
|
|
|
|
int operator()(T const&) const
|
2015-07-16 13:06:52 +02:00
|
|
|
{
|
2017-05-26 09:24:31 +02:00
|
|
|
std::clog << "svg2png error: failed to process '" << svg_name_ << "'\n";
|
2015-07-21 09:46:38 +02:00
|
|
|
return -1;
|
2015-07-16 13:06:52 +02:00
|
|
|
}
|
|
|
|
|
2015-02-19 19:19:01 +01:00
|
|
|
private:
|
2016-01-27 14:50:17 +01:00
|
|
|
std::string svg_name_;
|
2022-11-23 17:18:18 +01:00
|
|
|
double scale_factor_;
|
|
|
|
double opacity_;
|
2015-02-19 19:19:01 +01:00
|
|
|
bool verbose_;
|
|
|
|
bool auto_open_;
|
|
|
|
};
|
2011-01-26 02:19:01 +01:00
|
|
|
|
2022-11-24 11:25:09 +01:00
|
|
|
void check_opacity_range(double opacity)
|
|
|
|
{
|
|
|
|
using namespace boost::program_options;
|
|
|
|
if (opacity < 0.0 || opacity > 1.0)
|
|
|
|
{
|
|
|
|
throw invalid_option_value(std::to_string(opacity));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-26 20:41:37 +01:00
|
|
|
int main(int argc, char** argv)
|
2011-01-26 02:19:01 +01:00
|
|
|
{
|
|
|
|
namespace po = boost::program_options;
|
2011-11-14 04:54:32 +01:00
|
|
|
|
2012-04-11 21:35:07 +02:00
|
|
|
bool verbose = false;
|
|
|
|
bool auto_open = false;
|
2017-05-25 14:30:34 +02:00
|
|
|
bool strict = false;
|
2015-07-20 12:05:26 +02:00
|
|
|
int status = 0;
|
2011-01-26 10:14:57 +01:00
|
|
|
std::vector<std::string> svg_files;
|
2022-02-02 17:20:29 +01:00
|
|
|
mapnik::setup();
|
2014-08-28 22:59:48 +02:00
|
|
|
mapnik::logger::instance().set_severity(mapnik::logger::error);
|
2022-11-24 11:25:09 +01:00
|
|
|
double scale_factor;
|
|
|
|
double opacity;
|
2020-01-21 11:33:04 +01:00
|
|
|
std::string usage = "Usage: svg2png [options] <svg-file(s)>";
|
2011-01-26 02:19:01 +01:00
|
|
|
try
|
|
|
|
{
|
|
|
|
po::options_description desc("svg2png utility");
|
2022-01-26 20:41:37 +01:00
|
|
|
// clang-format off
|
2011-01-26 02:19:01 +01:00
|
|
|
desc.add_options()
|
|
|
|
("help,h", "produce usage message")
|
|
|
|
("version,V","print version string")
|
|
|
|
("verbose,v","verbose output")
|
2017-05-26 09:24:31 +02:00
|
|
|
("open,o","automatically open the file after rendering (os x only)")
|
|
|
|
("strict,s","enables strict SVG parsing")
|
2022-11-24 11:25:09 +01:00
|
|
|
("scale-factor", po::value<double>()->default_value(1.0), "provide scaling factor (default: 1.0)")
|
|
|
|
("opacity", po::value<double>()->default_value(1.0)->notifier(&check_opacity_range), "top level opacity (default: 1.0)")
|
2011-01-26 10:14:57 +01:00
|
|
|
("svg",po::value<std::vector<std::string> >(),"svg file to read")
|
2011-01-26 02:19:01 +01:00
|
|
|
;
|
2022-01-26 20:41:37 +01:00
|
|
|
// clang-format on
|
2011-01-26 02:19:01 +01:00
|
|
|
po::positional_options_description p;
|
2017-05-25 14:30:34 +02:00
|
|
|
p.add("svg", -1);
|
2011-11-14 04:54:32 +01:00
|
|
|
po::variables_map vm;
|
2011-01-26 02:19:01 +01:00
|
|
|
po::store(po::command_line_parser(argc, argv).options(desc).positional(p).run(), vm);
|
|
|
|
po::notify(vm);
|
2011-02-09 07:50:27 +01:00
|
|
|
|
2011-01-26 02:19:01 +01:00
|
|
|
if (vm.count("version"))
|
|
|
|
{
|
2017-05-25 14:30:34 +02:00
|
|
|
std::clog << "version " << MAPNIK_VERSION_STRING << std::endl;
|
2011-01-26 02:19:01 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2011-11-14 04:54:32 +01:00
|
|
|
if (vm.count("help"))
|
2011-01-26 02:19:01 +01:00
|
|
|
{
|
|
|
|
std::clog << desc << std::endl;
|
2020-01-21 11:33:04 +01:00
|
|
|
std::clog << usage << std::endl;
|
2011-01-26 02:19:01 +01:00
|
|
|
return 1;
|
|
|
|
}
|
2012-04-11 19:41:50 +02:00
|
|
|
|
2011-11-14 04:54:32 +01:00
|
|
|
if (vm.count("verbose"))
|
2011-01-26 02:19:01 +01:00
|
|
|
{
|
|
|
|
verbose = true;
|
|
|
|
}
|
2011-02-09 07:50:27 +01:00
|
|
|
|
2012-04-11 19:41:50 +02:00
|
|
|
if (vm.count("open"))
|
|
|
|
{
|
|
|
|
auto_open = true;
|
|
|
|
}
|
|
|
|
|
2017-05-25 14:30:34 +02:00
|
|
|
if (vm.count("strict"))
|
|
|
|
{
|
|
|
|
strict = true;
|
|
|
|
}
|
2020-01-21 11:33:04 +01:00
|
|
|
if (vm.count("scale-factor"))
|
|
|
|
{
|
|
|
|
scale_factor = vm["scale-factor"].as<double>();
|
|
|
|
}
|
2022-11-23 17:18:18 +01:00
|
|
|
if (vm.count("opacity"))
|
|
|
|
{
|
|
|
|
opacity = vm["opacity"].as<double>();
|
|
|
|
}
|
2011-01-26 02:19:01 +01:00
|
|
|
if (vm.count("svg"))
|
|
|
|
{
|
2022-01-26 20:41:37 +01:00
|
|
|
svg_files = vm["svg"].as<std::vector<std::string>>();
|
2011-01-26 02:19:01 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-01-21 11:33:04 +01:00
|
|
|
std::clog << usage << std::endl;
|
2011-01-26 02:19:01 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2011-01-26 10:14:57 +01:00
|
|
|
std::vector<std::string>::const_iterator itr = svg_files.begin();
|
|
|
|
if (itr == svg_files.end())
|
|
|
|
{
|
2020-01-21 11:33:04 +01:00
|
|
|
std::clog << usage << std::endl;
|
2011-01-26 10:14:57 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
while (itr != svg_files.end())
|
|
|
|
{
|
2022-01-26 20:41:37 +01:00
|
|
|
std::string svg_name(*itr++);
|
2012-04-11 19:41:50 +02:00
|
|
|
if (verbose)
|
|
|
|
{
|
|
|
|
std::clog << "found: " << svg_name << "\n";
|
|
|
|
}
|
2022-01-26 20:41:37 +01:00
|
|
|
std::shared_ptr<mapnik::marker const> marker =
|
|
|
|
mapnik::marker_cache::instance().find(svg_name, false, strict);
|
2022-11-23 17:18:18 +01:00
|
|
|
main_marker_visitor visitor(svg_name, scale_factor, opacity, verbose, auto_open);
|
2015-07-20 12:05:26 +02:00
|
|
|
status = mapnik::util::apply_visitor(visitor, *marker);
|
2011-01-26 10:14:57 +01:00
|
|
|
}
|
2022-11-10 16:57:38 +01:00
|
|
|
}
|
|
|
|
catch (std::exception const& ex)
|
2015-07-29 16:09:33 +02:00
|
|
|
{
|
|
|
|
std::clog << "Exception caught:" << ex.what() << std::endl;
|
|
|
|
return -1;
|
2022-11-10 16:57:38 +01:00
|
|
|
}
|
|
|
|
catch (...)
|
2011-01-26 02:19:01 +01:00
|
|
|
{
|
|
|
|
std::clog << "Exception of unknown type!" << std::endl;
|
|
|
|
return -1;
|
|
|
|
}
|
2015-07-20 12:05:26 +02:00
|
|
|
return status;
|
2011-01-26 02:19:01 +01:00
|
|
|
}
|