+ add support for background-image attribute

( background image is repeated both vertically
   and horizontally )
This commit is contained in:
Artem Pavlenko 2010-07-19 11:10:03 +00:00
parent 4bf42fe2d7
commit 8b8e247e62
5 changed files with 68 additions and 6 deletions

View file

@ -74,6 +74,7 @@ private:
std::string srs_;
int buffer_size_;
boost::optional<color> background_;
boost::optional<std::string> background_image_;
std::map<std::string,feature_type_style> styles_;
std::map<std::string,metawriter_ptr> metawriters_;
std::map<std::string,font_set> 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<color> 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<std::string> const& background_image() const;
/*! \brief Set buffer size
* @param buffer_size Buffer size in pixels.
*/

View file

@ -120,8 +120,32 @@ agg_renderer<T>::agg_renderer(Map const& m, T & pixmap, double scale_factor, uns
detector_(box2d<double>(-m.buffer_size(), -m.buffer_size(), m.width() + m.buffer_size() ,m.height() + m.buffer_size())),
ras_ptr(new rasterizer)
{
boost::optional<color> bg = m.background();
boost::optional<color> const& bg = m.background();
if (bg) pixmap_.set_background(*bg);
boost::optional<std::string> const& image_filename = m.background_image();
if (image_filename)
{
boost::optional<mapnik::image_ptr> 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<x_steps;++x)
{
for (unsigned y=0;y<y_steps;++y)
{
pixmap_.set_rectangle_alpha2(*(*bg_image), x*w, y*h, 1.0f);
}
}
}
}
}
#ifdef MAPNIK_DEBUG
std::clog << "scale=" << m.scale() << "\n";
#endif

View file

@ -171,10 +171,17 @@ void map_parser::parse_map( Map & map, ptree const & pt )
try
{
optional<color> bgcolor = get_opt_attr<color>(map_node, "bgcolor");
if (bgcolor) {
if (bgcolor)
{
map.set_background( * bgcolor );
}
optional<std::string> image_filename = get_opt_attr<string>(map_node, "background-image");
if (image_filename)
{
map.set_background_image(*image_filename);
}
map.set_srs( get_attr(map_node, "srs", map.srs() ));
optional<unsigned> buffer_size = get_opt_attr<unsigned>(map_node,"buffer_size");

View file

@ -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<std::string> 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();

View file

@ -699,12 +699,19 @@ void serialize_map(ptree & pt, Map const & map, bool explicit_defaults)
set_attr( map_node, "srs", map.srs() );
optional<color> c = map.background();
optional<color> const& c = map.background();
if ( c )
{
set_attr( map_node, "bgcolor", * c );
}
optional<std::string> 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)
{