Complete support for formatting types.

This commit is contained in:
Hermann Kraus 2012-02-19 02:03:25 +01:00
parent 76cdd7a254
commit 6fe0637425
3 changed files with 97 additions and 14 deletions

View file

@ -617,9 +617,10 @@ __all__ = [
'FontEngine',
'FontSet',
'FormattingNode',
'FormattingTextNode',
'FormattingFormatNode',
'FormattingListNode',
'FormattingText',
'FormattingFormat',
'FormattingList',
'FormattingExpressionFormat',
'Geometry2d',
'Image',
'ImageView',

View file

@ -28,6 +28,7 @@
#include <mapnik/formatting/text.hpp>
#include <mapnik/formatting/list.hpp>
#include <mapnik/formatting/format.hpp>
#include <mapnik/formatting/expression.hpp>
#include <mapnik/processed_text.hpp>
#include <mapnik/expression_string.hpp>
#include <mapnik/text_symbolizer.hpp>
@ -160,6 +161,27 @@ struct FormatNodeWrap: formatting::format_node, wrapper<formatting::format_node>
}
};
struct ExprFormatWrap: formatting::expression_format, wrapper<formatting::expression_format>
{
virtual void apply(char_properties const& p, Feature const& feature, processed_text &output) const
{
if(override o = this->get_override("apply"))
{
python_block_auto_unblock b;
o(ptr(&p), ptr(&feature), ptr(&output));
}
else
{
formatting::expression_format::apply(p, feature, output);
}
}
void default_apply(char_properties const& p, Feature const& feature, processed_text &output) const
{
formatting::expression_format::apply(p, feature, output);
}
};
struct ListNodeWrap: formatting::list_node, wrapper<formatting::list_node>
{
//Default constructor
@ -195,6 +217,36 @@ struct ListNodeWrap: formatting::list_node, wrapper<formatting::list_node>
{
formatting::list_node::apply(p, feature, output);
}
inline void IndexError(){
PyErr_SetString(PyExc_IndexError, "Index out of range");
throw_error_already_set();
}
unsigned get_length()
{
return children_.size();
}
formatting::node_ptr get_item(int i)
{
if (i<0) i+= children_.size();
if (i<children_.size()) return children_[i];
IndexError();
return formatting::node_ptr(); //Avoid compiler warning
}
void set_item(int i, formatting::node_ptr ptr)
{
if (i<0) i+= children_.size();
if (i<children_.size()) children_[i] = ptr;
IndexError();
}
void append(formatting::node_ptr ptr)
{
children_.push_back(ptr);
}
};
struct TextPlacementsWrap: text_placements, wrapper<text_placements>
@ -421,7 +473,7 @@ void export_text_placement()
boost::shared_ptr<TextNodeWrap>,
bases<formatting::node>,
boost::noncopyable>
("FormattingTextNode", init<expression_ptr>())
("FormattingText", init<expression_ptr>())
.def(init<std::string>())
.def("apply", &formatting::text_node::apply, &TextNodeWrap::default_apply)
.add_property("text",
@ -435,7 +487,7 @@ void export_text_placement()
boost::shared_ptr<FormatNodeWrap>,
bases<formatting::node>,
boost::noncopyable>
("FormattingFormatNode")
("FormattingFormat")
.def_readwrite_optional("text_size", &formatting::format_node::text_size)
.def_readwrite_optional("face_name", &formatting::format_node::face_name)
.def_readwrite_optional("character_spacing", &formatting::format_node::character_spacing)
@ -458,11 +510,39 @@ void export_text_placement()
boost::shared_ptr<ListNodeWrap>,
bases<formatting::node>,
boost::noncopyable>
("FormattingListNode", init<>())
("FormattingList", init<>())
.def(init<list>())
.def("append", &formatting::list_node::push_back)
.def("apply", &formatting::list_node::apply, &ListNodeWrap::default_apply)
.def("__len__", &ListNodeWrap::get_length)
.def("__getitem__", &ListNodeWrap::get_item)
.def("__setitem__", &ListNodeWrap::set_item)
.def("append", &ListNodeWrap::append)
;
register_ptr_to_python<boost::shared_ptr<formatting::list_node> >();
class_<ExprFormatWrap,
boost::shared_ptr<ExprFormatWrap>,
bases<formatting::node>,
boost::noncopyable>
("FormattingExpressionFormat")
.def_readwrite("text_size", &formatting::expression_format::text_size)
.def_readwrite("face_name", &formatting::expression_format::face_name)
.def_readwrite("character_spacing", &formatting::expression_format::character_spacing)
.def_readwrite("line_spacing", &formatting::expression_format::line_spacing)
.def_readwrite("text_opacity", &formatting::expression_format::text_opacity)
.def_readwrite("wrap_char", &formatting::expression_format::wrap_char)
.def_readwrite("wrap_before", &formatting::expression_format::wrap_before)
.def_readwrite("fill", &formatting::expression_format::fill)
.def_readwrite("halo_fill", &formatting::expression_format::halo_fill)
.def_readwrite("halo_radius", &formatting::expression_format::halo_radius)
.def("apply", &formatting::expression_format::apply, &ExprFormatWrap::default_apply)
.add_property("child",
&formatting::expression_format::get_child,
&formatting::expression_format::set_child)
;
register_ptr_to_python<boost::shared_ptr<formatting::expression_format> >();
//TODO: registry
}

View file

@ -74,23 +74,25 @@ m.layers.append(layer)
bbox = mapnik.Box2d(-0.05, -0.01, 0.95, 0.01)
m.zoom_to_box(bbox)
formatnode = mapnik.FormattingFormatNode()
formatnode.child = mapnik.FormattingTextNode("[name]")
formatnode = mapnik.FormattingFormat()
formatnode.child = mapnik.FormattingText("[name]")
formatnode.fill = mapnik.Color("green")
format_trees = [
('TextNode', mapnik.FormattingTextNode("[name]")),
('TextNode', mapnik.FormattingText("[name]")),
('MyText', MyText()),
('IfElse', IfElse("[nr] != '5'",
mapnik.FormattingTextNode("[name]"),
mapnik.FormattingTextNode("'SPECIAL!'"))),
mapnik.FormattingText("[name]"),
mapnik.FormattingText("'SPECIAL!'"))),
('Format', formatnode),
('List', mapnik.FormattingListNode([
mapnik.FormattingTextNode("[name]+'\n'"),
('List', mapnik.FormattingList([
mapnik.FormattingText("[name]+'\n'"),
MyText()
]))
])
)
]
for format_tree in format_trees:
text.placements.defaults.format_tree = format_tree[1]
mapnik.render_to_file(m, 'python-%s.png' % format_tree[0], 'png')