From 8b8e247e6222e3370cbd2126ab970cd22c61c287 Mon Sep 17 00:00:00 2001 From: Artem Pavlenko Date: Mon, 19 Jul 2010 11:10:03 +0000 Subject: [PATCH] + add support for background-image attribute ( background image is repeated both vertically and horizontally ) --- include/mapnik/map.hpp | 16 ++++++++++++++-- src/agg/agg_renderer.cpp | 26 +++++++++++++++++++++++++- src/load_map.cpp | 11 +++++++++-- src/map.cpp | 12 ++++++++++++ src/save_map.cpp | 9 ++++++++- 5 files changed, 68 insertions(+), 6 deletions(-) diff --git a/include/mapnik/map.hpp b/include/mapnik/map.hpp index 9a0c2735e..c97dbc5b9 100644 --- a/include/mapnik/map.hpp +++ b/include/mapnik/map.hpp @@ -74,6 +74,7 @@ private: std::string srs_; int buffer_size_; boost::optional background_; + boost::optional background_image_; std::map styles_; std::map metawriters_; std::map fontsets_; @@ -299,13 +300,24 @@ public: * @param c Background color. */ void set_background(const color& c); - + /*! \brief Get the map background color * @return Background color as boost::optional * object */ boost::optional const& background() const; - + + /*! \brief Set the map background image filename. + * @param c Background image filename. + */ + void set_background_image(std::string const& image_filename); + + /*! \brief Get the map background image + * @return Background image path as std::string + * object + */ + boost::optional const& background_image() const; + /*! \brief Set buffer size * @param buffer_size Buffer size in pixels. */ diff --git a/src/agg/agg_renderer.cpp b/src/agg/agg_renderer.cpp index eec56b9a1..17c1a6fe7 100644 --- a/src/agg/agg_renderer.cpp +++ b/src/agg/agg_renderer.cpp @@ -120,8 +120,32 @@ agg_renderer::agg_renderer(Map const& m, T & pixmap, double scale_factor, uns detector_(box2d(-m.buffer_size(), -m.buffer_size(), m.width() + m.buffer_size() ,m.height() + m.buffer_size())), ras_ptr(new rasterizer) { - boost::optional bg = m.background(); + boost::optional const& bg = m.background(); if (bg) pixmap_.set_background(*bg); + + boost::optional const& image_filename = m.background_image(); + if (image_filename) + { + boost::optional bg_image = mapnik::image_cache::instance()->find(*image_filename,true); + if (bg_image) + { + int w = (*bg_image)->width(); + int h = (*bg_image)->height(); + if ( w > 0 && h > 0) + { + // repeat background-image in both x,y + unsigned x_steps = unsigned(std::ceil(width_/double(w))); + unsigned y_steps = unsigned(std::ceil(height_/double(h))); + for (unsigned x=0;x bgcolor = get_opt_attr(map_node, "bgcolor"); - if (bgcolor) { + if (bgcolor) + { map.set_background( * bgcolor ); } - + + optional image_filename = get_opt_attr(map_node, "background-image"); + if (image_filename) + { + map.set_background_image(*image_filename); + } + map.set_srs( get_attr(map_node, "srs", map.srs() )); optional buffer_size = get_opt_attr(map_node,"buffer_size"); diff --git a/src/map.cpp b/src/map.cpp index 0d3d31222..1685e446e 100644 --- a/src/map.cpp +++ b/src/map.cpp @@ -80,6 +80,7 @@ Map::Map(const Map& rhs) srs_(rhs.srs_), buffer_size_(rhs.buffer_size_), background_(rhs.background_), + background_image_(rhs.background_image_), styles_(rhs.styles_), metawriters_(rhs.metawriters_), layers_(rhs.layers_), @@ -94,6 +95,7 @@ Map& Map::operator=(const Map& rhs) srs_=rhs.srs_; buffer_size_ = rhs.buffer_size_; background_=rhs.background_; + background_image_=rhs.background_image_; styles_=rhs.styles_; metawriters_ = rhs.metawriters_; layers_=rhs.layers_; @@ -319,6 +321,16 @@ void Map::set_background(const color& c) background_ = c; } +boost::optional const& Map::background_image() const +{ + return background_image_; +} + +void Map::set_background_image(std::string const& image_filename) +{ + background_image_ = image_filename; +} + void Map::zoom(double factor) { coord2d center = currentExtent_.center(); diff --git a/src/save_map.cpp b/src/save_map.cpp index ad5fa83df..5b0d8fba4 100644 --- a/src/save_map.cpp +++ b/src/save_map.cpp @@ -699,12 +699,19 @@ void serialize_map(ptree & pt, Map const & map, bool explicit_defaults) set_attr( map_node, "srs", map.srs() ); - optional c = map.background(); + optional const& c = map.background(); if ( c ) { set_attr( map_node, "bgcolor", * c ); } + optional const& image_filename = map.background_image(); + if ( image_filename ) + { + set_attr( map_node, "background-image", *image_filename ); + } + + unsigned buffer_size = map.buffer_size(); if ( buffer_size || explicit_defaults) {