2010-06-01 15:31:08 +02:00
|
|
|
/*****************************************************************************
|
|
|
|
*
|
|
|
|
* This file is part of Mapnik (c++ mapping toolkit)
|
|
|
|
*
|
|
|
|
* Copyright (C) 2010 Artem Pavlenko
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*
|
|
|
|
*****************************************************************************/
|
|
|
|
//$Id$
|
|
|
|
|
|
|
|
#include <mapnik/agg_renderer.hpp>
|
2010-06-13 14:03:42 +02:00
|
|
|
#include <mapnik/agg_rasterizer.hpp>
|
2011-09-01 02:43:46 +02:00
|
|
|
#include <mapnik/expression_evaluator.hpp>
|
2010-06-01 15:31:08 +02:00
|
|
|
#include <mapnik/image_util.hpp>
|
2011-01-26 02:18:40 +01:00
|
|
|
#include <mapnik/marker_cache.hpp>
|
2010-06-08 12:16:22 +02:00
|
|
|
#include <mapnik/svg/svg_converter.hpp>
|
|
|
|
#include <mapnik/svg/svg_renderer.hpp>
|
2010-06-14 18:38:02 +02:00
|
|
|
#include <mapnik/svg/svg_path_adapter.hpp>
|
2011-09-01 02:43:46 +02:00
|
|
|
#include <mapnik/expression_evaluator.hpp>
|
2010-06-01 15:31:08 +02:00
|
|
|
|
|
|
|
#include "agg_basics.h"
|
|
|
|
#include "agg_rendering_buffer.h"
|
|
|
|
#include "agg_scanline_u.h"
|
|
|
|
|
2011-05-10 23:09:54 +02:00
|
|
|
// boost
|
|
|
|
#include <boost/make_shared.hpp>
|
|
|
|
|
2010-06-01 15:31:08 +02:00
|
|
|
namespace mapnik {
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
void agg_renderer<T>::process(shield_symbolizer const& sym,
|
2010-06-02 13:03:30 +02:00
|
|
|
Feature const& feature,
|
|
|
|
proj_transform const& prj_trans)
|
2010-06-01 15:31:08 +02:00
|
|
|
{
|
2010-11-03 14:19:15 +01:00
|
|
|
typedef coord_transform2<CoordTransform,geometry_type> path_type;
|
2010-08-11 05:25:15 +02:00
|
|
|
|
2011-02-28 14:17:46 +01:00
|
|
|
|
|
|
|
text_placement_info_ptr placement_options = sym.get_placement_options()->get_placement_info();
|
|
|
|
placement_options->next();
|
|
|
|
placement_options->next_position_only();
|
|
|
|
|
2010-06-01 15:31:08 +02:00
|
|
|
UnicodeString text;
|
|
|
|
if( sym.get_no_text() )
|
2010-06-02 13:03:30 +02:00
|
|
|
text = UnicodeString( " " ); // TODO: fix->use 'space' as the text to render
|
2010-06-01 15:31:08 +02:00
|
|
|
else
|
|
|
|
{
|
2010-06-02 13:03:30 +02:00
|
|
|
expression_ptr name_expr = sym.get_name();
|
|
|
|
if (!name_expr) return;
|
|
|
|
value_type result = boost::apply_visitor(evaluate<Feature,value_type>(feature),*name_expr);
|
|
|
|
text = result.to_unicode();
|
2010-06-01 15:31:08 +02:00
|
|
|
}
|
|
|
|
|
2010-09-18 21:10:18 +02:00
|
|
|
if ( sym.get_text_transform() == UPPERCASE)
|
2010-06-01 15:31:08 +02:00
|
|
|
{
|
2010-06-02 13:03:30 +02:00
|
|
|
text = text.toUpper();
|
2010-06-01 15:31:08 +02:00
|
|
|
}
|
2010-09-18 21:10:18 +02:00
|
|
|
else if ( sym.get_text_transform() == LOWERCASE)
|
2010-06-01 15:31:08 +02:00
|
|
|
{
|
2010-06-02 13:03:30 +02:00
|
|
|
text = text.toLower();
|
2010-06-01 15:31:08 +02:00
|
|
|
}
|
2011-02-21 21:43:26 +01:00
|
|
|
else if ( sym.get_text_transform() == CAPITALIZE)
|
|
|
|
{
|
|
|
|
text = text.toTitle(NULL);
|
|
|
|
}
|
2010-06-01 15:31:08 +02:00
|
|
|
|
2010-06-10 16:12:28 +02:00
|
|
|
agg::trans_affine tr;
|
|
|
|
boost::array<double,6> const& m = sym.get_transform();
|
|
|
|
tr.load_from(&m[0]);
|
|
|
|
|
2010-06-01 15:31:08 +02:00
|
|
|
std::string filename = path_processor_type::evaluate( *sym.get_filename(), feature);
|
2011-01-26 02:18:40 +01:00
|
|
|
boost::optional<mapnik::marker_ptr> marker;
|
|
|
|
if ( !filename.empty() )
|
2010-06-01 15:31:08 +02:00
|
|
|
{
|
2011-01-26 02:18:40 +01:00
|
|
|
marker = marker_cache::instance()->find(filename, true);
|
2010-06-03 14:35:51 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-05-10 23:09:54 +02:00
|
|
|
marker.reset(boost::make_shared<mapnik::marker>());
|
2011-01-26 02:18:40 +01:00
|
|
|
}
|
2010-06-03 14:35:51 +02:00
|
|
|
|
2011-09-25 19:58:13 +02:00
|
|
|
|
2011-01-26 02:18:40 +01:00
|
|
|
if (text.length() > 0 && marker)
|
|
|
|
{
|
2011-09-25 19:58:13 +02:00
|
|
|
int w = (*marker)->width();
|
|
|
|
int h = (*marker)->height();
|
|
|
|
|
|
|
|
double px0 = - 0.5 * w;
|
|
|
|
double py0 = - 0.5 * h;
|
|
|
|
double px1 = 0.5 * w;
|
|
|
|
double py1 = 0.5 * h;
|
|
|
|
double px2 = px1;
|
|
|
|
double py2 = py0;
|
|
|
|
double px3 = px0;
|
|
|
|
double py3 = py1;
|
|
|
|
tr.transform(&px0,&py0);
|
|
|
|
tr.transform(&px1,&py1);
|
|
|
|
tr.transform(&px2,&py2);
|
|
|
|
tr.transform(&px3,&py3);
|
|
|
|
box2d<double> label_ext (px0, py0, px1, py1);
|
|
|
|
label_ext.expand_to_include(px2, py2);
|
|
|
|
label_ext.expand_to_include(px3, py3);
|
|
|
|
|
2011-01-26 02:18:40 +01:00
|
|
|
face_set_ptr faces;
|
|
|
|
|
|
|
|
if (sym.get_fontset().size() > 0)
|
2010-06-03 14:35:51 +02:00
|
|
|
{
|
2011-01-26 02:18:40 +01:00
|
|
|
faces = font_manager_.get_face_set(sym.get_fontset());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
faces = font_manager_.get_face_set(sym.get_face_name());
|
|
|
|
}
|
2010-06-01 15:31:08 +02:00
|
|
|
|
2011-01-26 02:18:40 +01:00
|
|
|
stroker_ptr strk = font_manager_.get_stroker();
|
|
|
|
if (strk && faces->size() > 0)
|
|
|
|
{
|
|
|
|
text_renderer<T> ren(pixmap_, faces, *strk);
|
2010-06-01 15:31:08 +02:00
|
|
|
|
2011-01-26 02:18:40 +01:00
|
|
|
ren.set_pixel_size(sym.get_text_size() * scale_factor_);
|
|
|
|
ren.set_fill(sym.get_fill());
|
|
|
|
ren.set_halo_fill(sym.get_halo_fill());
|
|
|
|
ren.set_halo_radius(sym.get_halo_radius() * scale_factor_);
|
|
|
|
ren.set_opacity(sym.get_text_opacity());
|
|
|
|
|
|
|
|
placement_finder<label_collision_detector4> finder(detector_);
|
2010-06-03 14:35:51 +02:00
|
|
|
|
2011-01-26 02:18:40 +01:00
|
|
|
string_info info(text);
|
2010-06-03 14:35:51 +02:00
|
|
|
|
2011-01-26 02:18:40 +01:00
|
|
|
faces->get_string_info(info);
|
2010-06-03 14:35:51 +02:00
|
|
|
|
2011-01-26 02:18:40 +01:00
|
|
|
metawriter_with_properties writer = sym.get_metawriter();
|
2010-06-03 14:35:51 +02:00
|
|
|
|
2011-01-26 02:18:40 +01:00
|
|
|
for (unsigned i = 0; i < feature.num_geometries(); ++i)
|
|
|
|
{
|
|
|
|
geometry_type const& geom = feature.get_geometry(i);
|
|
|
|
if (geom.num_points() > 0 )
|
2010-06-03 14:35:51 +02:00
|
|
|
{
|
2011-01-26 02:18:40 +01:00
|
|
|
path_type path(t_,geom,prj_trans);
|
2010-06-03 14:35:51 +02:00
|
|
|
|
2011-01-26 02:18:40 +01:00
|
|
|
label_placement_enum how_placed = sym.get_label_placement();
|
2011-02-01 09:13:08 +01:00
|
|
|
if (how_placed == POINT_PLACEMENT || how_placed == VERTEX_PLACEMENT || how_placed == INTERIOR_PLACEMENT)
|
2011-01-26 02:18:40 +01:00
|
|
|
{
|
|
|
|
// for every vertex, try and place a shield/text
|
|
|
|
geom.rewind(0);
|
2011-05-30 03:16:21 +02:00
|
|
|
placement text_placement(info, sym, scale_factor_, w, h, false);
|
2011-01-26 02:18:40 +01:00
|
|
|
text_placement.avoid_edges = sym.get_avoid_edges();
|
|
|
|
text_placement.allow_overlap = sym.get_allow_overlap();
|
2011-05-06 02:33:49 +02:00
|
|
|
if (writer.first)
|
|
|
|
text_placement.collect_extents =true; // needed for inmem metawriter
|
2011-01-26 02:18:40 +01:00
|
|
|
position const& pos = sym.get_displacement();
|
|
|
|
position const& shield_pos = sym.get_shield_displacement();
|
|
|
|
for( unsigned jj = 0; jj < geom.num_points(); jj++ )
|
2010-06-02 13:03:30 +02:00
|
|
|
{
|
2011-01-26 02:18:40 +01:00
|
|
|
double label_x;
|
|
|
|
double label_y;
|
|
|
|
double z=0.0;
|
|
|
|
|
|
|
|
if( how_placed == VERTEX_PLACEMENT )
|
|
|
|
geom.vertex(&label_x,&label_y); // by vertex
|
2011-02-01 09:13:08 +01:00
|
|
|
else if( how_placed == INTERIOR_PLACEMENT )
|
|
|
|
geom.label_interior_position(&label_x,&label_y);
|
2011-01-26 02:18:40 +01:00
|
|
|
else
|
|
|
|
geom.label_position(&label_x, &label_y); // by middle of line or by point
|
|
|
|
prj_trans.backward(label_x,label_y, z);
|
|
|
|
t_.forward(&label_x,&label_y);
|
|
|
|
|
|
|
|
label_x += boost::get<0>(shield_pos);
|
|
|
|
label_y += boost::get<1>(shield_pos);
|
|
|
|
|
2011-05-30 03:16:21 +02:00
|
|
|
finder.find_point_placement( text_placement, placement_options,
|
|
|
|
label_x, label_y, 0.0,
|
2011-01-26 02:18:40 +01:00
|
|
|
sym.get_line_spacing(),
|
2011-05-30 03:16:21 +02:00
|
|
|
sym.get_character_spacing());
|
2011-01-26 02:18:40 +01:00
|
|
|
|
|
|
|
// check to see if image overlaps anything too, there is only ever 1 placement found for points and verticies
|
|
|
|
if( text_placement.placements.size() > 0)
|
2010-06-02 13:03:30 +02:00
|
|
|
{
|
2011-01-26 02:18:40 +01:00
|
|
|
double x = floor(text_placement.placements[0].starting_x);
|
|
|
|
double y = floor(text_placement.placements[0].starting_y);
|
|
|
|
int px;
|
|
|
|
int py;
|
2011-09-25 19:58:13 +02:00
|
|
|
|
2011-01-26 02:18:40 +01:00
|
|
|
if( !sym.get_unlock_image() )
|
2010-06-02 13:03:30 +02:00
|
|
|
{
|
2011-01-26 02:18:40 +01:00
|
|
|
// center image at text center position
|
|
|
|
// remove displacement from image label
|
|
|
|
double lx = x - boost::get<0>(pos);
|
|
|
|
double ly = y - boost::get<1>(pos);
|
2011-05-10 23:05:40 +02:00
|
|
|
px=int(floor(lx - (0.5 * w))) + 1;
|
|
|
|
py=int(floor(ly - (0.5 * h))) + 1;
|
2011-09-25 19:58:13 +02:00
|
|
|
label_ext.re_center(lx,ly);
|
2011-01-26 02:18:40 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{ // center image at reference location
|
|
|
|
px=int(floor(label_x - 0.5 * w));
|
|
|
|
py=int(floor(label_y - 0.5 * h));
|
2011-09-25 19:58:13 +02:00
|
|
|
label_ext.re_center(label_x,label_y);
|
2011-01-26 02:18:40 +01:00
|
|
|
}
|
2011-09-25 19:58:13 +02:00
|
|
|
|
2011-01-26 02:18:40 +01:00
|
|
|
if ( sym.get_allow_overlap() || detector_.has_placement(label_ext) )
|
|
|
|
{
|
|
|
|
render_marker(px,py,**marker,tr,sym.get_opacity());
|
|
|
|
|
|
|
|
box2d<double> dim = ren.prepare_glyphs(&text_placement.placements[0]);
|
|
|
|
ren.render(x,y);
|
|
|
|
detector_.insert(label_ext);
|
|
|
|
finder.update_detector(text_placement);
|
|
|
|
if (writer.first) {
|
2011-09-25 19:58:13 +02:00
|
|
|
writer.first->add_box(label_ext, feature, t_, writer.second);
|
2011-01-26 02:18:40 +01:00
|
|
|
writer.first->add_text(text_placement, faces, feature, t_, writer.second);
|
2010-06-03 14:35:51 +02:00
|
|
|
}
|
2010-06-02 13:03:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-01-26 02:18:40 +01:00
|
|
|
}
|
2010-06-02 13:03:30 +02:00
|
|
|
|
2011-01-26 02:18:40 +01:00
|
|
|
else if (geom.num_points() > 1 && how_placed == LINE_PLACEMENT)
|
|
|
|
{
|
2011-09-25 19:58:13 +02:00
|
|
|
placement text_placement(info, sym, scale_factor_, label_ext.width(), label_ext.height(), true);
|
|
|
|
|
2011-01-26 02:18:40 +01:00
|
|
|
text_placement.avoid_edges = sym.get_avoid_edges();
|
2011-05-30 03:16:21 +02:00
|
|
|
finder.find_point_placements<path_type>(text_placement, placement_options, path);
|
2011-01-26 02:18:40 +01:00
|
|
|
|
|
|
|
position const& pos = sym.get_displacement();
|
|
|
|
for (unsigned int ii = 0; ii < text_placement.placements.size(); ++ ii)
|
2010-06-02 13:03:30 +02:00
|
|
|
{
|
2011-01-26 02:18:40 +01:00
|
|
|
double x = floor(text_placement.placements[ii].starting_x);
|
|
|
|
double y = floor(text_placement.placements[ii].starting_y);
|
|
|
|
|
|
|
|
double lx = x - boost::get<0>(pos);
|
|
|
|
double ly = y - boost::get<1>(pos);
|
2011-05-10 23:05:40 +02:00
|
|
|
int px=int(floor(lx - (0.5*w))) + 1;
|
|
|
|
int py=int(floor(ly - (0.5*h))) + 1;
|
2011-01-26 02:18:40 +01:00
|
|
|
|
|
|
|
render_marker(px,py,**marker,tr,sym.get_opacity());
|
|
|
|
|
2011-09-25 19:58:13 +02:00
|
|
|
if (writer.first) writer.first->add_box(label_ext, feature, t_, writer.second);
|
2011-01-26 02:18:40 +01:00
|
|
|
|
|
|
|
box2d<double> dim = ren.prepare_glyphs(&text_placement.placements[ii]);
|
|
|
|
ren.render(x,y);
|
2010-06-02 13:03:30 +02:00
|
|
|
}
|
2011-01-26 02:18:40 +01:00
|
|
|
finder.update_detector(text_placement);
|
|
|
|
if (writer.first) writer.first->add_text(text_placement, faces, feature, t_, writer.second);
|
2010-06-02 13:03:30 +02:00
|
|
|
}
|
|
|
|
}
|
2010-06-01 15:31:08 +02:00
|
|
|
}
|
2010-06-02 13:03:30 +02:00
|
|
|
}
|
2010-06-01 15:31:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template void agg_renderer<image_32>::process(shield_symbolizer const&,
|
2010-06-02 13:03:30 +02:00
|
|
|
Feature const&,
|
|
|
|
proj_transform const&);
|
2010-06-01 15:31:08 +02:00
|
|
|
|
|
|
|
}
|