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)
|
|
|
|
*
|
|
|
|
* Copyright (C) 2010 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 <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>
|
2011-01-26 02:19:01 +01:00
|
|
|
|
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/graphics.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>
|
|
|
|
|
|
|
|
#include <boost/algorithm/string.hpp>
|
|
|
|
#include <boost/filesystem/operations.hpp>
|
|
|
|
#include <boost/program_options.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"
|
|
|
|
|
2012-04-11 19:17:03 +02:00
|
|
|
#include <libxml/parser.h> // for xmlInitParser(), xmlCleanupParser()
|
|
|
|
|
2011-01-26 02:19:01 +01:00
|
|
|
|
2011-11-14 04:54:32 +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;
|
2012-07-24 02:52:26 +02:00
|
|
|
int return_value = 0;
|
2011-01-26 10:14:57 +01:00
|
|
|
std::vector<std::string> svg_files;
|
2012-04-11 21:35:07 +02:00
|
|
|
mapnik::logger logger;
|
|
|
|
logger.set_severity(mapnik::logger::error);
|
2011-11-14 04:54:32 +01:00
|
|
|
|
2011-01-26 02:19:01 +01:00
|
|
|
try
|
|
|
|
{
|
|
|
|
po::options_description desc("svg2png utility");
|
|
|
|
desc.add_options()
|
|
|
|
("help,h", "produce usage message")
|
|
|
|
("version,V","print version string")
|
|
|
|
("verbose,v","verbose output")
|
2012-04-11 19:41:50 +02:00
|
|
|
("open","automatically open the file after rendering (os x only)")
|
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
|
|
|
;
|
2011-11-14 04:54:32 +01:00
|
|
|
|
2011-01-26 02:19:01 +01:00
|
|
|
po::positional_options_description p;
|
|
|
|
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"))
|
|
|
|
{
|
2012-04-11 19:41:50 +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;
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2011-01-26 02:19:01 +01:00
|
|
|
if (vm.count("svg"))
|
|
|
|
{
|
2011-01-26 10:14:57 +01:00
|
|
|
svg_files=vm["svg"].as< std::vector<std::string> >();
|
2011-01-26 02:19:01 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::clog << "please provide an svg file!" << std::endl;
|
|
|
|
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())
|
|
|
|
{
|
|
|
|
std::clog << "no svg files to render" << std::endl;
|
|
|
|
return 0;
|
|
|
|
}
|
2012-04-11 21:35:07 +02:00
|
|
|
|
|
|
|
xmlInitParser();
|
|
|
|
|
2011-01-26 10:14:57 +01:00
|
|
|
while (itr != svg_files.end())
|
|
|
|
{
|
|
|
|
std::string svg_name (*itr++);
|
2012-04-11 19:41:50 +02:00
|
|
|
if (verbose)
|
|
|
|
{
|
|
|
|
std::clog << "found: " << svg_name << "\n";
|
|
|
|
}
|
2011-11-14 04:54:32 +01:00
|
|
|
|
2012-04-11 19:41:50 +02:00
|
|
|
boost::optional<mapnik::marker_ptr> marker_ptr =
|
2012-09-07 17:23:03 +02:00
|
|
|
mapnik::marker_cache::instance().find(svg_name, false);
|
2012-04-11 21:35:07 +02:00
|
|
|
if (!marker_ptr)
|
|
|
|
{
|
|
|
|
std::clog << "svg2png error: could not open: '" << svg_name << "'\n";
|
2012-07-24 02:52:26 +02:00
|
|
|
return_value = -1;
|
2012-04-11 21:35:07 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
mapnik::marker marker = **marker_ptr;
|
|
|
|
if (!marker.is_vector())
|
2012-04-11 19:41:50 +02:00
|
|
|
{
|
2012-04-11 21:35:07 +02:00
|
|
|
std::clog << "svg2png error: '" << svg_name << "' is not a valid vector!\n";
|
2012-07-24 02:52:26 +02:00
|
|
|
return_value = -1;
|
2012-04-11 21:35:07 +02:00
|
|
|
continue;
|
|
|
|
}
|
2011-02-09 07:50:27 +01:00
|
|
|
|
2012-10-03 23:00:48 +02:00
|
|
|
typedef agg::pixfmt_rgba32_pre pixfmt;
|
2012-04-11 21:35:07 +02:00
|
|
|
typedef agg::renderer_base<pixfmt> renderer_base;
|
|
|
|
typedef agg::renderer_scanline_aa_solid<renderer_base> renderer_solid;
|
|
|
|
agg::rasterizer_scanline_aa<> ras_ptr;
|
|
|
|
agg::scanline_u8 sl;
|
|
|
|
|
|
|
|
double opacity = 1;
|
|
|
|
int w = marker.width();
|
|
|
|
int h = marker.height();
|
|
|
|
if (verbose)
|
|
|
|
{
|
|
|
|
std::clog << "found width of '" << w << "' and height of '" << h << "'\n";
|
2011-01-26 02:19:01 +01:00
|
|
|
}
|
2012-07-31 01:37:34 +02:00
|
|
|
// 10 pixel buffer to avoid edge clipping of 100% svg's
|
|
|
|
mapnik::image_32 im(w+10,h+10);
|
|
|
|
agg::rendering_buffer buf(im.raw_data(), im.width(), im.height(), im.width() * 4);
|
2012-04-11 21:35:07 +02:00
|
|
|
pixfmt pixf(buf);
|
|
|
|
renderer_base renb(pixf);
|
|
|
|
|
|
|
|
mapnik::box2d<double> const& bbox = (*marker.get_vector_data())->bounding_box();
|
|
|
|
mapnik::coord<double,2> c = bbox.center();
|
|
|
|
// center the svg marker on '0,0'
|
|
|
|
agg::trans_affine mtx = agg::trans_affine_translation(-c.x,-c.y);
|
|
|
|
// render the marker at the center of the marker box
|
2012-07-31 01:37:34 +02:00
|
|
|
mtx.translate(0.5 * im.width(), 0.5 * im.height());
|
2012-04-11 21:35:07 +02:00
|
|
|
|
|
|
|
mapnik::svg::vertex_stl_adapter<mapnik::svg::svg_path_storage> stl_storage((*marker.get_vector_data())->source());
|
|
|
|
mapnik::svg::svg_path_adapter svg_path(stl_storage);
|
2012-08-28 02:58:49 +02:00
|
|
|
mapnik::svg::svg_renderer_agg<mapnik::svg::svg_path_adapter,
|
2012-04-11 21:35:07 +02:00
|
|
|
agg::pod_bvector<mapnik::svg::path_attributes>,
|
|
|
|
renderer_solid,
|
2012-10-03 23:00:48 +02:00
|
|
|
agg::pixfmt_rgba32_pre > svg_renderer_this(svg_path,
|
2012-04-11 21:35:07 +02:00
|
|
|
(*marker.get_vector_data())->attributes());
|
|
|
|
|
|
|
|
svg_renderer_this.render(ras_ptr, sl, renb, mtx, opacity, bbox);
|
|
|
|
|
|
|
|
boost::algorithm::ireplace_last(svg_name,".svg",".png");
|
2012-10-03 23:00:48 +02:00
|
|
|
im.demultiply();
|
2012-04-11 21:35:07 +02:00
|
|
|
mapnik::save_to_file<mapnik::image_data_32>(im.data(),svg_name,"png");
|
|
|
|
if (auto_open)
|
|
|
|
{
|
|
|
|
std::ostringstream s;
|
2012-07-24 02:41:52 +02:00
|
|
|
#ifdef DARWIN
|
2012-04-11 21:35:07 +02:00
|
|
|
s << "open " << svg_name;
|
2012-07-24 02:41:52 +02:00
|
|
|
#else
|
|
|
|
s << "xdg-open " << svg_name;
|
|
|
|
#endif
|
2012-07-24 02:52:26 +02:00
|
|
|
int ret = system(s.str().c_str());
|
|
|
|
if (ret != 0)
|
|
|
|
return_value = ret;
|
2011-01-26 02:19:01 +01:00
|
|
|
}
|
2012-04-11 21:35:07 +02:00
|
|
|
std::clog << "rendered to: " << svg_name << "\n";
|
2011-01-26 10:14:57 +01:00
|
|
|
}
|
2011-01-26 02:19:01 +01:00
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
std::clog << "Exception of unknown type!" << std::endl;
|
2012-04-11 19:17:03 +02:00
|
|
|
xmlCleanupParser();
|
2011-01-26 02:19:01 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2011-01-26 10:14:57 +01:00
|
|
|
|
2012-04-11 19:17:03 +02:00
|
|
|
// only call this once, on exit
|
|
|
|
// to make sure valgrind output is clean
|
|
|
|
// http://xmlsoft.org/xmlmem.html
|
|
|
|
xmlCleanupParser();
|
2012-07-24 02:52:26 +02:00
|
|
|
return return_value;
|
2011-01-26 02:19:01 +01:00
|
|
|
}
|