From 2f144d6ccb6ff791dab2e021f81e820996b769da Mon Sep 17 00:00:00 2001 From: Hermann Kraus Date: Sat, 4 Feb 2012 00:19:20 +0100 Subject: [PATCH] Python bindings for formating::text_node. --- bindings/python/mapnik/__init__.py | 2 + bindings/python/mapnik_text_placement.cpp | 82 +++++++++++++++++++++-- 2 files changed, 79 insertions(+), 5 deletions(-) diff --git a/bindings/python/mapnik/__init__.py b/bindings/python/mapnik/__init__.py index d093c78a8..8a6b2ae7e 100644 --- a/bindings/python/mapnik/__init__.py +++ b/bindings/python/mapnik/__init__.py @@ -612,6 +612,7 @@ __all__ = [ 'FontEngine', 'FontSet', 'FormatingNode', + 'FormatingTextNode', 'Geometry2d', 'Image', 'ImageView', @@ -630,6 +631,7 @@ __all__ = [ 'PointSymbolizer', 'PolygonPatternSymbolizer', 'PolygonSymbolizer', + 'ProcessedText', 'ProjTransform', 'Projection', 'Query', diff --git a/bindings/python/mapnik_text_placement.cpp b/bindings/python/mapnik_text_placement.cpp index 87ef68055..976677ba4 100644 --- a/bindings/python/mapnik_text_placement.cpp +++ b/bindings/python/mapnik_text_placement.cpp @@ -58,14 +58,57 @@ void set_displacement(text_symbolizer_properties &t, boost::python::tuple arg) t.displacement = std::make_pair(x, y); } +/* boost.python documentation doesn't relly tell you how to do it. +But this helps: +http://www.gamedev.net/topic/446225-inheritance-in-boostpython/ +*/ + struct NodeWrap: formating::node, wrapper { + NodeWrap() : formating::node(), wrapper() + { + + } + void apply(char_properties const& p, Feature const& feature, processed_text &output) const { - this->get_override("apply")(); + std::cout << "Calling apply\n"; + if (!this->get_override("apply")) { + std::cout << "Warning: No apply function found!\n"; + } + this->get_override("apply")(ptr(&p), ptr(&feature), ptr(&output)); } }; + +struct TextNodeWrap: formating::text_node, wrapper +{ + + TextNodeWrap(expression_ptr text) : formating::text_node(text), wrapper() + { + + } + + virtual void apply(char_properties const& p, Feature const& feature, processed_text &output) const + { + if(override func_apply = this->get_override("apply")) + { + std::cout << "got override!" << &feature << "\n"; + func_apply(ptr(&p), ptr(&feature), ptr(&output)); + } + else + { + formating::text_node::apply(p, feature, output); + } + } + + void default_apply(char_properties const& p, Feature const& feature, processed_text &output) const + { + formating::text_node::apply(p, feature, output); + } + +}; + struct TextPlacementsWrap: text_placements, wrapper { text_placement_info_ptr get_placement_info(double scale_factor_, dimension_type dim, @@ -175,15 +218,21 @@ void export_text_placement() /* from_xml, to_xml operate on mapnik's internal XML tree and don't make sense in python.*/ ; - class_, boost::noncopyable>("TextPlacements") + class_, + boost::noncopyable> + ("TextPlacements") .def_readwrite("defaults", &text_placements::properties) .def("get_placement_info", pure_virtual(&text_placements::get_placement_info)) /* TODO: get_all expressions. */ ; register_ptr_to_python >(); - class_, boost::noncopyable>("TextPlacementInfo", - init()) + class_, + boost::noncopyable> + ("TextPlacementInfo", + init()) .def("next", pure_virtual(&text_placement_info::next)) .def("get_actual_label_spacing", &text_placement_info::get_actual_label_spacing) .def("get_actual_minimum_distance", &text_placement_info::get_actual_minimum_distance) @@ -199,11 +248,34 @@ void export_text_placement() // .def_readwrite("placements", &text_placement_info::placements) ; + class_, + boost::noncopyable>("ProcessedText", no_init); + register_ptr_to_python >(); //TODO: Python namespace - class_("FormatingNode") + class_, + boost::noncopyable> + ("FormatingNode") .def("apply", pure_virtual(&formating::node::apply)) ; + register_ptr_to_python >(); + + class_, + bases, + boost::noncopyable>( + "FormatingTextNode", init() ) + /* Apply and add_expressions are automatically inherited. */ + .def("apply", &TextNodeWrap::apply)//, &TextNodeWrap::default_apply + .add_property("text", + &formating::text_node::get_text, + &formating::text_node::set_text) + ; + + register_ptr_to_python >(); + }