2010-03-18 21:04:35 +01:00
|
|
|
#include <mapnik/glyph_symbolizer.hpp>
|
2010-03-18 21:05:08 +01:00
|
|
|
#include <mapnik/expression_evaluator.hpp>
|
2010-03-18 21:04:35 +01:00
|
|
|
|
|
|
|
namespace mapnik
|
|
|
|
{
|
2010-03-18 21:05:08 +01:00
|
|
|
|
2010-03-22 12:21:17 +01:00
|
|
|
static const char * angle_mode_strings[] = {
|
|
|
|
"azimuth",
|
|
|
|
"trigonometric",
|
|
|
|
""
|
|
|
|
};
|
2010-06-19 07:27:11 +02:00
|
|
|
|
2011-04-01 08:24:57 +02:00
|
|
|
IMPLEMENT_ENUM( angle_mode_e, angle_mode_strings )
|
2010-03-22 12:21:17 +01:00
|
|
|
|
2010-03-18 21:05:08 +01:00
|
|
|
text_path_ptr glyph_symbolizer::get_text_path(face_set_ptr const& faces,
|
|
|
|
Feature const& feature) const
|
|
|
|
{
|
|
|
|
// Try to evaulate expressions against feature
|
|
|
|
UnicodeString char_ = eval_char(feature);
|
|
|
|
double angle = eval_angle(feature);
|
|
|
|
|
2010-03-19 10:33:25 +01:00
|
|
|
// calculate displacement so glyph is rotated along center (default pivot is
|
2010-03-18 21:05:08 +01:00
|
|
|
// lowerbottom corner)
|
|
|
|
string_info info(char_);
|
|
|
|
faces->get_string_info(info);
|
2010-03-19 10:33:25 +01:00
|
|
|
|
|
|
|
// XXX: Perhaps this limitation can be overcomed?
|
2010-03-18 21:05:08 +01:00
|
|
|
if (info.num_characters() != 1)
|
2010-03-18 21:04:35 +01:00
|
|
|
{
|
2010-03-18 21:05:08 +01:00
|
|
|
throw config_error("'char' length must be exactly 1");
|
|
|
|
}
|
|
|
|
|
|
|
|
character_info ci = info.at(0);
|
2010-08-10 14:05:38 +02:00
|
|
|
font_face_set::dimension_t cdim = faces->character_dimensions(ci.character);
|
|
|
|
double cwidth = static_cast<double>(cdim.width)/2.0;
|
|
|
|
double cheight = static_cast<double>(cdim.height)/2.0;
|
2010-03-18 21:05:08 +01:00
|
|
|
double xoff = cwidth*cos(angle) - cheight*sin(angle);
|
|
|
|
double yoff = cwidth*sin(angle) + cheight*cos(angle);
|
|
|
|
|
|
|
|
// Create text path and add character with displacement and angle
|
|
|
|
text_path_ptr path_ptr = text_path_ptr(new text_path());
|
|
|
|
path_ptr->add_node(ci.character, -xoff, -yoff, angle);
|
|
|
|
return path_ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
UnicodeString glyph_symbolizer::eval_char(Feature const& feature) const
|
|
|
|
{
|
|
|
|
expression_ptr expr = get_char();
|
2010-03-19 10:33:25 +01:00
|
|
|
if (!expr)
|
|
|
|
throw config_error("No 'char' expression");
|
|
|
|
value_type result = boost::apply_visitor(
|
|
|
|
evaluate<Feature,value_type>(feature),
|
|
|
|
*expr
|
|
|
|
);
|
2010-03-22 12:21:17 +01:00
|
|
|
#ifdef MAPNIK_DEBUG
|
2010-03-24 19:02:10 +01:00
|
|
|
std::clog << "char_result=" << result.to_string() << "\n";
|
2010-03-22 12:21:17 +01:00
|
|
|
#endif
|
2010-03-18 21:05:08 +01:00
|
|
|
return result.to_unicode();
|
|
|
|
}
|
|
|
|
|
|
|
|
double glyph_symbolizer::eval_angle(Feature const& feature) const
|
|
|
|
{
|
|
|
|
double angle = 0.0;
|
|
|
|
expression_ptr expr = get_angle();
|
|
|
|
if (expr) {
|
|
|
|
value_type result = boost::apply_visitor(
|
|
|
|
evaluate<Feature,value_type>(feature),
|
|
|
|
*expr
|
|
|
|
);
|
2010-03-22 12:21:17 +01:00
|
|
|
#ifdef MAPNIK_DEBUG
|
|
|
|
std::clog << "angle_result=" << result.to_string() << "\n";
|
|
|
|
#endif
|
2010-03-18 21:05:08 +01:00
|
|
|
angle = result.to_double();
|
|
|
|
// normalize to first rotation in case an expression has made it go past
|
|
|
|
angle = std::fmod(angle, 360);
|
|
|
|
angle *= (M_PI/180); // convert to radians
|
2010-03-22 12:21:17 +01:00
|
|
|
if (get_angle_mode()==AZIMUTH) {
|
2010-03-18 21:05:08 +01:00
|
|
|
// angle is an azimuth, convert into trigonometric angle
|
|
|
|
angle = std::atan2(std::cos(angle), std::sin(angle));
|
|
|
|
}
|
|
|
|
if (angle<0)
|
|
|
|
angle += 2*M_PI;
|
|
|
|
}
|
|
|
|
return angle;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned glyph_symbolizer::eval_size(Feature const& feature) const
|
|
|
|
{
|
|
|
|
expression_ptr expr = get_size();
|
|
|
|
if (!expr) throw config_error("No 'size' expression");
|
|
|
|
value_type result = boost::apply_visitor(
|
|
|
|
evaluate<Feature,value_type>(feature),
|
|
|
|
*expr
|
|
|
|
);
|
2010-03-22 12:21:17 +01:00
|
|
|
#ifdef MAPNIK_DEBUG
|
2010-03-24 19:02:10 +01:00
|
|
|
std::clog << "size_result=" << result.to_string() << "\n";
|
2010-03-22 12:21:17 +01:00
|
|
|
#endif
|
2011-10-27 14:24:50 +02:00
|
|
|
float size = static_cast<float>(result.to_double());
|
2010-03-22 12:21:17 +01:00
|
|
|
#ifdef MAPNIK_DEBUG
|
2010-03-24 19:02:10 +01:00
|
|
|
std::clog << "size=" << size << "\n";
|
2010-03-22 12:21:17 +01:00
|
|
|
#endif
|
|
|
|
return size;
|
2010-03-18 21:05:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
color glyph_symbolizer::eval_color(Feature const& feature) const
|
|
|
|
{
|
|
|
|
raster_colorizer_ptr colorizer = get_colorizer();
|
2010-03-24 19:02:10 +01:00
|
|
|
if (colorizer)
|
|
|
|
{
|
2010-03-18 21:05:08 +01:00
|
|
|
expression_ptr value_expr = get_value();
|
2010-03-24 19:02:10 +01:00
|
|
|
if (!value_expr)
|
2010-06-02 13:03:30 +02:00
|
|
|
{
|
2010-03-18 21:05:08 +01:00
|
|
|
throw config_error(
|
|
|
|
"Must define a 'value' expression to use a colorizer"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
value_type value_result = boost::apply_visitor(
|
|
|
|
evaluate<Feature,value_type>(feature),
|
|
|
|
*value_expr
|
|
|
|
);
|
2010-03-22 12:21:17 +01:00
|
|
|
#ifdef MAPNIK_DEBUG
|
|
|
|
std::clog << "value_result=" << value_result.to_string() << "\n";
|
|
|
|
#endif
|
2010-03-18 21:05:08 +01:00
|
|
|
return colorizer->get_color((float)value_result.to_double());
|
2010-03-24 19:02:10 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-03-18 21:05:08 +01:00
|
|
|
expression_ptr color_expr = get_color();
|
2010-03-24 19:02:10 +01:00
|
|
|
if (color_expr)
|
2010-06-02 13:03:30 +02:00
|
|
|
{
|
2010-03-18 21:05:08 +01:00
|
|
|
value_type color_result = boost::apply_visitor(
|
|
|
|
evaluate<Feature,value_type>(feature),
|
|
|
|
*color_expr
|
|
|
|
);
|
2010-03-22 12:21:17 +01:00
|
|
|
#ifdef MAPNIK_DEBUG
|
|
|
|
std::clog << "color_result=" << color_result.to_string() << "\n";
|
|
|
|
#endif
|
2010-03-18 21:05:08 +01:00
|
|
|
return color(color_result.to_string());
|
2010-03-24 19:02:10 +01:00
|
|
|
}
|
2010-06-02 13:03:30 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
return color(0,0,0); // black
|
2010-03-18 21:05:08 +01:00
|
|
|
}
|
2010-03-18 21:04:35 +01:00
|
|
|
}
|
|
|
|
}
|
2010-03-18 21:05:08 +01:00
|
|
|
|
|
|
|
} // end mapnik namespace
|