sync trunk with new cairo support added previously to 0.7 branch in r1531

This commit is contained in:
Dane Springmeyer 2010-01-11 18:55:30 +00:00
parent e5673e239b
commit 067fdb884d
4 changed files with 169 additions and 37 deletions

View file

@ -195,20 +195,41 @@ void render_tile_to_file(const mapnik::Map& map,
void render_to_file1(const mapnik::Map& map, void render_to_file1(const mapnik::Map& map,
const std::string& filename, const std::string& filename,
const std::string& format) const std::string& format)
{
if (format == "pdf" || format == "svg" || format =="ps" || format == "ARGB32" || format == "RGB24")
{
#if defined(HAVE_CAIRO)
mapnik::save_to_cairo_file(map,filename,format);
#else
throw mapnik::ImageWriterException("Cairo backend not available, cannot write to format: " + format);
#endif
}
else
{ {
mapnik::image_32 image(map.getWidth(),map.getHeight()); mapnik::image_32 image(map.getWidth(),map.getHeight());
render(map,image,0,0); render(map,image,0,0);
mapnik::save_to_file(image,filename,format); mapnik::save_to_file(image,filename,format);
} }
}
void render_to_file2(const mapnik::Map& map, void render_to_file2(const mapnik::Map& map,const std::string& filename)
const std::string& filename) {
std::string format = mapnik::guess_type(filename);
if (format == "pdf" || format == "svg" || format =="ps")
{
#if defined(HAVE_CAIRO)
mapnik::save_to_cairo_file(map,filename,format);
#else
throw mapnik::ImageWriterException("Cairo backend not available, cannot write to format: " + format);
#endif
}
else
{ {
mapnik::image_32 image(map.getWidth(),map.getHeight()); mapnik::image_32 image(map.getWidth(),map.getHeight());
render(map,image,0,0); render(map,image,0,0);
mapnik::save_to_file(image,filename); mapnik::save_to_file(image,filename);
} }
}
double scale_denominator(mapnik::Map const &map, bool geographic) double scale_denominator(mapnik::Map const &map, bool geographic)
{ {

View file

@ -23,12 +23,20 @@
# Import everything. In this case this is safe, in more complex systems, you # Import everything. In this case this is safe, in more complex systems, you
# will want to be more selective. # will want to be more selective.
import sys
try: try:
import mapnik import mapnik
except: except:
print '\n\nThe mapnik library and python bindings must have been compiled and \ print '\n\nThe mapnik library and python bindings must have been compiled and \
installed successfully before running this script.\n\n' installed successfully before running this script.\n\n'
raise sys.exit(1)
try:
import cairo
HAS_PYCAIRO_MODULE = True
except ImportError:
HAS_PYCAIRO_MODULE = False
# Instanciate a map, giving it a width and height. Remember: the word "map" is # Instanciate a map, giving it a width and height. Remember: the word "map" is
# reserved in Python! :) # reserved in Python! :)
@ -317,38 +325,55 @@ im = mapnik.Image(m.width,m.height)
mapnik.render(m, im) mapnik.render(m, im)
# Save image to files # Save image to files
images = [] images_ = []
im.save('demo.png', 'png') # true-colour RGBA im.save('demo.png', 'png') # true-colour RGBA
images.append('demo.png') images_.append('demo.png')
im.save('demo256.png', 'png256') # save to palette based (max 256 colours) png im.save('demo256.png', 'png256') # save to palette based (max 256 colours) png
images.append('demo256.png') images_.append('demo256.png')
im.save('demo.jpg', 'jpeg') im.save('demo_high.jpg', 'jpeg100')
images.append('demo.jpg') images_.append('demo_high.jpg')
im.save('demo_low.jpg', 'jpeg50')
images_.append('demo_low.jpg')
# Render cairo examples # Render cairo examples
if mapnik.has_pycairo(): if HAS_PYCAIRO_MODULE and mapnik.has_pycairo():
import cairo
svg_surface = cairo.SVGSurface('demo.svg', m.width,m.height) svg_surface = cairo.SVGSurface('demo.svg', m.width,m.height)
mapnik.render(m, svg_surface) mapnik.render(m, svg_surface)
svg_surface.finish() svg_surface.finish()
images.append('demo.svg') images_.append('demo.svg')
pdf_surface = cairo.PDFSurface('demo.pdf', m.width,m.height) pdf_surface = cairo.PDFSurface('demo.pdf', m.width,m.height)
mapnik.render(m, pdf_surface) mapnik.render(m, pdf_surface)
images.append('demo.pdf') images_.append('demo.pdf')
pdf_surface.finish() pdf_surface.finish()
postscript_surface = cairo.PSSurface('demo.ps', m.width,m.height) postscript_surface = cairo.PSSurface('demo.ps', m.width,m.height)
mapnik.render(m, postscript_surface) mapnik.render(m, postscript_surface)
images.append('demo.ps') images_.append('demo.ps')
postscript_surface.finish() postscript_surface.finish()
else:
print '\n\nSkipping cairo examples as Mapnik Pycairo support not available'
print "\n\n", len(images), "maps have been rendered in the current directory:" else:
for image in images: print '\n\nPycairo not available...',
print "-", image if mapnik.has_cairo():
print ' will render Cairo formats using alternative method'
mapnik.render_to_file(m,'demo.pdf')
images_.append('demo.pdf')
mapnik.render_to_file(m,'demo.ps')
images_.append('demo.ps')
mapnik.render_to_file(m,'demo.svg')
images_.append('demo.svg')
mapnik.render_to_file(m,'demo_cairo_rgb.png','RGB24')
images_.append('demo_cairo_rgb.png')
mapnik.render_to_file(m,'demo_cairo_argb.png','ARGB32')
images_.append('demo_cairo_argb.png')
print "\n\n", len(images_), "maps have been rendered in the current directory:"
for im_ in images_:
print "-", im_
print "\n\nHave a look!\n\n" print "\n\nHave a look!\n\n"
mapnik.save_map(m,"map.xml") mapnik.save_map(m,"map.xml")

View file

@ -27,6 +27,7 @@
// mapnik // mapnik
#include <mapnik/config.hpp> #include <mapnik/config.hpp>
#include <mapnik/map.hpp>
#include <mapnik/graphics.hpp> #include <mapnik/graphics.hpp>
// boost // boost
@ -55,6 +56,10 @@ namespace mapnik {
} }
}; };
MAPNIK_DECL void save_to_cairo_file(mapnik::Map const& map,
std::string const& filename,
std::string const& type);
template <typename T> template <typename T>
MAPNIK_DECL void save_to_file(T const& image, MAPNIK_DECL void save_to_file(T const& image,
std::string const& filename, std::string const& filename,
@ -94,12 +99,31 @@ namespace mapnik {
boost::algorithm::iends_with(filename,std::string(".tiff")); boost::algorithm::iends_with(filename,std::string(".tiff"));
} }
inline bool is_pdf (std::string const& filename)
{
return boost::algorithm::iends_with(filename,std::string(".pdf"));
}
inline bool is_svg (std::string const& filename)
{
return boost::algorithm::iends_with(filename,std::string(".svg"));
}
inline bool is_ps (std::string const& filename)
{
return boost::algorithm::iends_with(filename,std::string(".ps"));
}
inline boost::optional<std::string> type_from_filename(std::string const& filename) inline boost::optional<std::string> type_from_filename(std::string const& filename)
{ {
typedef boost::optional<std::string> result_type; typedef boost::optional<std::string> result_type;
if (is_png(filename)) return result_type("png"); if (is_png(filename)) return result_type("png");
if (is_jpeg(filename)) return result_type("jpeg"); if (is_jpeg(filename)) return result_type("jpeg");
if (is_tiff(filename)) return result_type("tiff"); if (is_tiff(filename)) return result_type("tiff");
if (is_pdf(filename)) return result_type("pdf");
if (is_svg(filename)) return result_type("svg");
if (is_ps(filename)) return result_type("ps");
return result_type(); return result_type();
} }

View file

@ -35,6 +35,10 @@ extern "C"
#include <mapnik/memory.hpp> #include <mapnik/memory.hpp>
#include <mapnik/image_view.hpp> #include <mapnik/image_view.hpp>
#ifdef HAVE_CAIRO
#include <mapnik/cairo_renderer.hpp>
#endif
// stl // stl
#include <string> #include <string>
#include <iostream> #include <iostream>
@ -104,8 +108,10 @@ namespace mapnik
} }
else throw ImageWriterException("unknown file type: " + type); else throw ImageWriterException("unknown file type: " + type);
} }
else throw ImageWriterException("Could not write file to " + filename );
} }
template <typename T> template <typename T>
void save_to_file(T const& image,std::string const& filename) void save_to_file(T const& image,std::string const& filename)
{ {
@ -116,6 +122,62 @@ namespace mapnik
} }
} }
#if defined(HAVE_CAIRO)
// TODO - move to separate cairo_io.hpp
void save_to_cairo_file(mapnik::Map const& map, std::string const& filename)
{
boost::optional<std::string> type = type_from_filename(filename);
if (type)
{
save_to_cairo_file(map,filename,*type);
}
}
void save_to_cairo_file(mapnik::Map const& map,
std::string const& filename,
std::string const& type)
{
std::ofstream file (filename.c_str(), std::ios::out|std::ios::trunc|std::ios::binary);
if (file)
{
Cairo::RefPtr<Cairo::Surface> surface;
unsigned width = map.getWidth();
unsigned height = map.getHeight();
if (type == "pdf")
surface = Cairo::PdfSurface::create(filename,width,height);
else if (type == "svg")
surface = Cairo::SvgSurface::create(filename,width,height);
else if (type == "ps")
surface = Cairo::PsSurface::create(filename,width,height);
else if (type == "ARGB32")
surface = Cairo::ImageSurface::create(Cairo::FORMAT_ARGB32,width,height);
else if (type == "RGB24")
surface = Cairo::ImageSurface::create(Cairo::FORMAT_RGB24,width,height);
else
throw ImageWriterException("unknown file type: " + type);
Cairo::RefPtr<Cairo::Context> context = Cairo::Context::create(surface);
// TODO - expose as user option
/*
if (type == "ARGB32" || type == "RGB24")
{
context->set_antialias(Cairo::ANTIALIAS_NONE);
}
*/
mapnik::cairo_renderer<Cairo::Context> ren(map, context);
ren.apply();
if (type == "ARGB32" || type == "RGB24")
{
surface->write_to_png(filename);
}
surface->finish();
}
}
#endif
template void save_to_file<image_data_32>(image_data_32 const&, template void save_to_file<image_data_32>(image_data_32 const&,
std::string const&, std::string const&,