simplify the svg2png command line tool

This commit is contained in:
Dane Springmeyer 2011-02-09 06:50:27 +00:00
parent ec2090d8a8
commit af8e859d66

View file

@ -23,7 +23,7 @@
#include <iostream> #include <iostream>
#include <sstream> #include <sstream>
#include <vector> #include <vector>
#include <string> //#include <string>
#include <mapnik/marker.hpp> #include <mapnik/marker.hpp>
#include <mapnik/marker_cache.hpp> #include <mapnik/marker_cache.hpp>
@ -33,11 +33,9 @@
#include <mapnik/svg/svg_renderer.hpp> #include <mapnik/svg/svg_renderer.hpp>
#include <mapnik/svg/svg_path_attributes.hpp> #include <mapnik/svg/svg_path_attributes.hpp>
#include <boost/tokenizer.hpp>
#include <boost/algorithm/string.hpp> #include <boost/algorithm/string.hpp>
#include <boost/filesystem/operations.hpp> #include <boost/filesystem/operations.hpp>
#include <boost/program_options.hpp> #include <boost/program_options.hpp>
#include <boost/array.hpp>
#include "agg_rasterizer_scanline_aa.h" #include "agg_rasterizer_scanline_aa.h"
#include "agg_basics.h" #include "agg_basics.h"
@ -113,52 +111,45 @@ int main (int argc,char** argv)
mapnik::marker marker = **marker_ptr; mapnik::marker marker = **marker_ptr;
if (marker.is_vector()) { if (marker.is_vector()) {
int width_ = marker.width();
int height_ = marker.height();
mapnik::image_32 pixmap_(width_,height_);
agg::rasterizer_scanline_aa<> ras_ptr;
typedef agg::pixfmt_rgba32_plain pixfmt; typedef agg::pixfmt_rgba32_plain pixfmt;
typedef agg::renderer_base<pixfmt> renderer_base; typedef agg::renderer_base<pixfmt> renderer_base;
agg::rasterizer_scanline_aa<> ras_ptr;
ras_ptr.reset();
ras_ptr.gamma(agg::gamma_linear());
agg::scanline_u8 sl; agg::scanline_u8 sl;
agg::rendering_buffer buf(pixmap_.raw_data(), width_, height_, width_ * 4);
double opacity = 1;
double scale_factor_ = .95;
int w = marker.width();
int h = marker.height();
mapnik::image_32 im(w,h);
agg::rendering_buffer buf(im.raw_data(), w, h, w * 4);
pixfmt pixf(buf); pixfmt pixf(buf);
renderer_base renb(pixf); renderer_base renb(pixf);
mapnik::box2d<double> const& bbox = (*marker.get_vector_data())->bounding_box(); mapnik::box2d<double> const& bbox = (*marker.get_vector_data())->bounding_box();
double x1 = bbox.minx(); mapnik::coord<double,2> c = bbox.center();
double y1 = bbox.miny(); // center the svg marker on '0,0'
double x2 = bbox.maxx(); agg::trans_affine mtx = agg::trans_affine_translation(-c.x,-c.y);
double y2 = bbox.maxy(); // apply symbol transformation to get to map space
mtx *= agg::trans_affine_scaling(scale_factor_);
agg::trans_affine recenter = agg::trans_affine_translation(0.5*(marker.width()-(x1+x2)),0.5*(marker.height()-(y1+y2))); // render the marker at the center of the marker box
mtx.translate(0.5 * w, 0.5 * h);
mapnik::svg::vertex_stl_adapter<mapnik::svg::svg_path_storage> stl_storage((*marker.get_vector_data())->source()); 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); mapnik::svg::svg_path_adapter svg_path(stl_storage);
mapnik::svg::svg_renderer<mapnik::svg::svg_path_adapter, mapnik::svg::svg_renderer<mapnik::svg::svg_path_adapter,
agg::pod_bvector<mapnik::svg::path_attributes> > svg_renderer_this(svg_path, agg::pod_bvector<mapnik::svg::path_attributes> > svg_renderer_this(svg_path,
(*marker.get_vector_data())->attributes()); (*marker.get_vector_data())->attributes());
agg::trans_affine tr;
agg::trans_affine mtx = recenter * tr;
double scale_factor_ = 1;
double opacity = 1;
mtx *= agg::trans_affine_scaling(scale_factor_);
svg_renderer_this.render(ras_ptr, sl, renb, mtx, opacity, bbox); svg_renderer_this.render(ras_ptr, sl, renb, mtx, opacity, bbox);
boost::algorithm::ireplace_last(svg_name,".svg",".png"); boost::algorithm::ireplace_last(svg_name,".svg",".png");
mapnik::save_to_file<mapnik::image_data_32>(pixmap_.data(),svg_name,"png"); mapnik::save_to_file<mapnik::image_data_32>(im.data(),svg_name,"png");
std::ostringstream s; std::ostringstream s;
s << "open " << svg_name; s << "open " << svg_name;
system(s.str().c_str()); system(s.str().c_str());
} }
} }
} }
} }
catch (...) catch (...)
{ {