Ticket #381: further_cairo_rendering_abstraction.patch
| File further_cairo_rendering_abstraction.patch, 6.6 kB (added by springmeyer, 8 months ago) |
|---|
-
src/image_util.cpp
35 35 #include <mapnik/memory.hpp> 36 36 #include <mapnik/image_view.hpp> 37 37 38 #ifdef HAVE_CAIRO 39 #include <mapnik/cairo_renderer.hpp> 40 #endif 41 38 42 // stl 39 43 #include <string> 40 44 #include <iostream> … … 103 107 } 104 108 } 105 109 else throw ImageWriterException("unknown file type: " + type); 106 } 110 } 111 else throw ImageWriterException("Could not write file to " + filename ); 107 112 } 113 108 114 109 115 template <typename T> 110 116 void save_to_file(T const& image,std::string const& filename) … … 112 118 std::string type = type_from_filename(filename); 113 119 save_to_file<T>(image,filename,type); 114 120 } 115 116 121 122 123 #if defined(HAVE_CAIRO) 124 125 void save_to_cairo_file(mapnik::Map const& map, std::string const& filename) 126 { 127 std::string type = type_from_filename(filename); 128 save_to_cairo_file(map,filename,type); 129 } 130 131 void save_to_cairo_file(mapnik::Map const& map, 132 std::string const& filename, 133 std::string const& type) 134 { 135 std::ofstream file (filename.c_str(), std::ios::out|std::ios::trunc|std::ios::binary); 136 if (file) 137 { 138 Cairo::RefPtr<Cairo::Surface> surface; 139 if (type == "pdf") 140 surface = Cairo::PdfSurface::create(filename, map.getWidth(),map.getHeight()); 141 else if (type == "svg") 142 surface = Cairo::SvgSurface::create(filename, map.getWidth(),map.getHeight()); 143 else if (type == "ps") 144 surface = Cairo::PsSurface::create(filename, map.getWidth(),map.getHeight()); 145 else if (type == "ARGB32") 146 surface = Cairo::ImageSurface::create(Cairo::FORMAT_ARGB32, map.getWidth(),map.getHeight()); 147 else if (type == "RGB24") 148 surface = Cairo::ImageSurface::create(Cairo::FORMAT_RGB24, map.getWidth(),map.getHeight()); 149 else 150 throw ImageWriterException("unknown file type: " + type); 151 Cairo::RefPtr<Cairo::Context> context = Cairo::Context::create(surface); 152 153 if (type == "ARGB32" | type == "RGB24") 154 { 155 context->set_antialias(Cairo::ANTIALIAS_NONE); 156 } 157 158 mapnik::cairo_renderer<Cairo::Context> ren(map, context); 159 ren.apply(); 160 161 if (type == "ARGB32" | type == "RGB24") 162 { 163 surface->write_to_png(filename); 164 } 165 surface->finish(); 166 } 167 } 168 169 #endif 170 117 171 template void save_to_file<ImageData32>(ImageData32 const&, 118 172 std::string const&, 119 173 std::string const&); 120 174 121 175 template void save_to_file<ImageData32>(ImageData32 const&, 122 176 std::string const&); 123 177 -
include/mapnik/image_util.hpp
27 27 28 28 // mapnik 29 29 #include <mapnik/config.hpp> 30 #include <mapnik/map.hpp> 30 31 #include <mapnik/graphics.hpp> 31 32 32 33 // boost … … 53 54 } 54 55 }; 55 56 57 MAPNIK_DECL void save_to_cairo_file(mapnik::Map const& map, 58 std::string const& filename, 59 std::string const& type); 60 56 61 template <typename T> 57 62 MAPNIK_DECL void save_to_file(T const& image, 58 63 std::string const& filename, … … 91 96 return boost::algorithm::iends_with(filename,std::string(".tif")) || 92 97 boost::algorithm::iends_with(filename,std::string(".tiff")); 93 98 } 94 99 100 inline bool is_pdf (std::string const& filename) 101 { 102 return boost::algorithm::iends_with(filename,std::string(".pdf")); 103 } 104 105 inline bool is_svg (std::string const& filename) 106 { 107 return boost::algorithm::iends_with(filename,std::string(".svg")); 108 } 109 110 inline bool is_ps (std::string const& filename) 111 { 112 return boost::algorithm::iends_with(filename,std::string(".ps")); 113 } 114 95 115 inline std::string type_from_filename(std::string const& filename) 96 116 { 97 117 if (is_png(filename)) return "png"; 98 118 if (is_jpeg(filename)) return "jpeg"; 99 119 if (is_tiff(filename)) return "tiff"; 120 if (is_pdf(filename)) return "pdf"; 121 if (is_svg(filename)) return "svg"; 122 if (is_ps(filename)) return "ps"; 100 123 return "unknown"; 101 124 } 102 125 -
bindings/python/mapnik_python.cpp
194 194 const std::string& filename, 195 195 const std::string& format) 196 196 { 197 mapnik::Image32 image(map.getWidth(),map.getHeight()); 198 render(map,image,0,0); 199 mapnik::save_to_file(image,filename,format); 197 if (format == "pdf" | format == "svg" | format =="ps" | format == "ARGB32" | format == "RGB24") 198 { 199 #if defined(HAVE_CAIRO) 200 mapnik::save_to_cairo_file(map,filename,format); 201 #else 202 throw mapnik::ImageWriterException("Cairo backend not available, cannot write to format: " + format); 203 #endif 204 } 205 else 206 { 207 mapnik::Image32 image(map.getWidth(),map.getHeight()); 208 render(map,image,0,0); 209 mapnik::save_to_file(image,filename,format); 210 } 200 211 } 201 212 202 void render_to_file2(const mapnik::Map& map, 203 const std::string& filename) 213 void render_to_file2(const mapnik::Map& map,const std::string& filename) 204 214 { 205 mapnik::Image32 image(map.getWidth(),map.getHeight()); 206 render(map,image,0,0); 207 mapnik::save_to_file(image,filename); 215 std::string format = mapnik::guess_type(filename); 216 if (format == "pdf" | format == "svg" | format =="ps") 217 { 218 #if defined(HAVE_CAIRO) 219 mapnik::save_to_cairo_file(map,filename,format); 220 #else 221 throw mapnik::ImageWriterException("Cairo backend not available, cannot write to format: " + format); 222 #endif 223 } 224 else 225 { 226 mapnik::Image32 image(map.getWidth(),map.getHeight()); 227 render(map,image,0,0); 228 mapnik::save_to_file(image,filename); 229 } 208 230 } 209 231 210 211 232 double scale_denominator(mapnik::Map const &map, bool geographic) 212 233 { 213 234 return mapnik::scale_denominator(map, geographic);
