2012-02-15 21:35:54 +01:00
|
|
|
/*****************************************************************************
|
|
|
|
*
|
|
|
|
* This file is part of Mapnik (c++ mapping toolkit)
|
|
|
|
*
|
|
|
|
* Copyright (C) 2012 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
|
|
|
|
*
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
// mapnik
|
2012-04-10 00:51:04 +02:00
|
|
|
#include <mapnik/debug.hpp>
|
2012-08-28 02:58:49 +02:00
|
|
|
#include <mapnik/formatting/expression_format.hpp>
|
2012-02-15 21:35:54 +01:00
|
|
|
#include <mapnik/ptree_helpers.hpp>
|
|
|
|
#include <mapnik/expression_string.hpp>
|
|
|
|
#include <mapnik/expression_evaluator.hpp>
|
|
|
|
#include <mapnik/text_properties.hpp>
|
|
|
|
#include <mapnik/feature.hpp>
|
2012-03-11 23:24:28 +01:00
|
|
|
#include <mapnik/xml_node.hpp>
|
2012-02-15 21:35:54 +01:00
|
|
|
|
2013-01-04 03:06:07 +01:00
|
|
|
// boost
|
|
|
|
#include <boost/property_tree/ptree.hpp>
|
|
|
|
|
2012-02-15 21:35:54 +01:00
|
|
|
|
2012-04-10 00:51:04 +02:00
|
|
|
namespace mapnik {
|
|
|
|
namespace formatting {
|
|
|
|
|
2012-02-15 21:35:54 +01:00
|
|
|
using boost::property_tree::ptree;
|
|
|
|
void expression_format::to_xml(boost::property_tree::ptree &xml) const
|
|
|
|
{
|
2012-03-12 02:12:58 +01:00
|
|
|
ptree &new_node = xml.push_back(ptree::value_type("ExpressionFormat", ptree()))->second;
|
2012-02-15 21:35:54 +01:00
|
|
|
if (face_name) set_attr(new_node, "face-name", to_expression_string(*face_name));
|
|
|
|
if (text_size) set_attr(new_node, "size", to_expression_string(*text_size));
|
2012-05-29 22:53:39 +02:00
|
|
|
if (character_spacing) set_attr(new_node, "character-spacing", to_expression_string(*character_spacing));
|
2012-02-15 21:35:54 +01:00
|
|
|
if (line_spacing) set_attr(new_node, "line-spacing", to_expression_string(*line_spacing));
|
|
|
|
if (text_opacity) set_attr(new_node, "opacity", to_expression_string(*text_opacity));
|
|
|
|
if (wrap_before) set_attr(new_node, "wrap-before", to_expression_string(*wrap_before));
|
|
|
|
if (wrap_char) set_attr(new_node, "wrap-character", to_expression_string(*wrap_char));
|
|
|
|
if (fill) set_attr(new_node, "fill", to_expression_string(*fill));
|
|
|
|
if (halo_fill) set_attr(new_node, "halo-fill", to_expression_string(*halo_fill));
|
|
|
|
if (halo_radius) set_attr(new_node, "halo-radius", to_expression_string(*halo_radius));
|
|
|
|
if (child_) child_->to_xml(new_node);
|
|
|
|
}
|
|
|
|
|
2012-03-07 02:23:16 +01:00
|
|
|
node_ptr expression_format::from_xml(xml_node const& xml)
|
2012-02-15 21:35:54 +01:00
|
|
|
{
|
|
|
|
expression_format *n = new expression_format();
|
|
|
|
node_ptr np(n);
|
|
|
|
|
|
|
|
node_ptr child = node::from_xml(xml);
|
|
|
|
n->set_child(child);
|
|
|
|
|
|
|
|
n->face_name = get_expression(xml, "face-name");
|
|
|
|
n->text_size = get_expression(xml, "size");
|
|
|
|
n->character_spacing = get_expression(xml, "character-spacing");
|
|
|
|
n->line_spacing = get_expression(xml, "line-spacing");
|
2012-09-04 22:17:09 +02:00
|
|
|
n->text_opacity = get_expression(xml, "opacity");
|
2012-02-15 21:35:54 +01:00
|
|
|
n->wrap_before = get_expression(xml, "wrap-before");
|
|
|
|
n->wrap_char = get_expression(xml, "wrap-character");
|
|
|
|
n->fill = get_expression(xml, "fill");
|
|
|
|
n->halo_fill = get_expression(xml, "halo-fill");
|
|
|
|
n->halo_radius = get_expression(xml, "halo-radius");
|
|
|
|
return np;
|
|
|
|
}
|
|
|
|
|
2012-03-07 02:23:16 +01:00
|
|
|
expression_ptr expression_format::get_expression(xml_node const& xml, std::string name)
|
2012-02-15 21:35:54 +01:00
|
|
|
{
|
2012-07-04 20:59:36 +02:00
|
|
|
boost::optional<expression_ptr> tmp = xml.get_opt_attr<expression_ptr>(name);
|
|
|
|
if (tmp) return *tmp;
|
2012-02-15 21:35:54 +01:00
|
|
|
return expression_ptr();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-01-22 02:05:33 +01:00
|
|
|
void expression_format::apply(char_properties const& p, feature_impl const& feature, processed_text &output) const
|
2012-02-15 21:35:54 +01:00
|
|
|
{
|
|
|
|
char_properties new_properties = p;
|
|
|
|
if (face_name) new_properties.face_name =
|
2013-01-04 08:54:04 +01:00
|
|
|
boost::apply_visitor(evaluate<feature_impl,value_type>(feature), *face_name).to_string();
|
2012-02-15 21:35:54 +01:00
|
|
|
if (text_size) new_properties.text_size =
|
2013-01-04 08:54:04 +01:00
|
|
|
boost::apply_visitor(evaluate<feature_impl,value_type>(feature), *text_size).to_double();
|
2012-02-15 21:35:54 +01:00
|
|
|
if (character_spacing) new_properties.character_spacing =
|
2013-01-04 08:54:04 +01:00
|
|
|
boost::apply_visitor(evaluate<feature_impl,value_type>(feature), *character_spacing).to_double();
|
2012-02-15 21:35:54 +01:00
|
|
|
if (line_spacing) new_properties.line_spacing =
|
2013-01-04 08:54:04 +01:00
|
|
|
boost::apply_visitor(evaluate<feature_impl,value_type>(feature), *line_spacing).to_double();
|
2012-02-15 21:35:54 +01:00
|
|
|
if (text_opacity) new_properties.text_opacity =
|
2013-01-04 08:54:04 +01:00
|
|
|
boost::apply_visitor(evaluate<feature_impl,value_type>(feature), *text_opacity).to_double();
|
2012-02-15 21:35:54 +01:00
|
|
|
if (wrap_before) new_properties.wrap_before =
|
2013-01-04 08:54:04 +01:00
|
|
|
boost::apply_visitor(evaluate<feature_impl,value_type>(feature), *wrap_before).to_bool();
|
2012-02-15 21:35:54 +01:00
|
|
|
if (wrap_char) new_properties.wrap_char =
|
2013-01-04 08:54:04 +01:00
|
|
|
boost::apply_visitor(evaluate<feature_impl,value_type>(feature), *character_spacing).to_unicode()[0];
|
2012-02-15 21:35:54 +01:00
|
|
|
// if (fill) new_properties.fill =
|
2013-01-04 08:54:04 +01:00
|
|
|
// boost::apply_visitor(evaluate<feature_impl,value_type>(feature), *fill).to_color();
|
2012-02-15 21:35:54 +01:00
|
|
|
// if (halo_fill) new_properties.halo_fill =
|
2013-01-04 08:54:04 +01:00
|
|
|
// boost::apply_visitor(evaluate<feature_impl,value_type>(feature), *halo_fill).to_color();
|
2012-02-15 21:35:54 +01:00
|
|
|
if (halo_radius) new_properties.halo_radius =
|
2013-01-04 08:54:04 +01:00
|
|
|
boost::apply_visitor(evaluate<feature_impl,value_type>(feature), *halo_radius).to_double();
|
2012-02-15 21:35:54 +01:00
|
|
|
|
|
|
|
if (child_) {
|
|
|
|
child_->apply(new_properties, feature, output);
|
|
|
|
} else {
|
2012-04-10 00:51:04 +02:00
|
|
|
MAPNIK_LOG_WARN(expression) << "Useless format: No text to format";
|
2012-02-15 21:35:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void expression_format::set_child(node_ptr child)
|
|
|
|
{
|
|
|
|
child_ = child;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
node_ptr expression_format::get_child() const
|
|
|
|
{
|
|
|
|
return child_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void expression_format::add_expressions(expression_set &output) const
|
|
|
|
{
|
|
|
|
if (child_) child_->add_expressions(output);
|
|
|
|
output.insert(face_name);
|
|
|
|
output.insert(text_size);
|
|
|
|
output.insert(character_spacing);
|
|
|
|
output.insert(line_spacing);
|
|
|
|
output.insert(text_opacity);
|
|
|
|
output.insert(wrap_before);
|
|
|
|
output.insert(wrap_char);
|
|
|
|
output.insert(fill);
|
|
|
|
output.insert(halo_fill);
|
|
|
|
output.insert(halo_radius);
|
|
|
|
}
|
|
|
|
|
|
|
|
} //ns formatting
|
|
|
|
} //ns mapnik
|