From ab5ce64b165d95eb52ab2c98d3431abfd9a8afdd Mon Sep 17 00:00:00 2001 From: Dane Springmeyer Date: Tue, 21 Aug 2012 15:59:31 -0700 Subject: [PATCH] python: add properties to mapnik.Stroke to match xml/svg spec - refs #1427 --- bindings/python/mapnik_stroke.cpp | 40 ++++++++++++++++++++++++++----- tests/python_tests/object_test.py | 10 ++++++++ 2 files changed, 44 insertions(+), 6 deletions(-) diff --git a/bindings/python/mapnik_stroke.cpp b/bindings/python/mapnik_stroke.cpp index eba5afe5a..104bb7b69 100644 --- a/bindings/python/mapnik_stroke.cpp +++ b/bindings/python/mapnik_stroke.cpp @@ -32,10 +32,9 @@ using namespace mapnik; namespace { using namespace boost::python; -list get_dashes_list(const stroke& stroke) +list get_dashes_list(stroke const& stroke) { list l; - if (stroke.has_dash()) { mapnik::dash_array const& dash = stroke.get_dash_array(); mapnik::dash_array::const_iterator iter = dash.begin(); @@ -44,9 +43,23 @@ list get_dashes_list(const stroke& stroke) l.append(make_tuple(iter->first, iter->second)); } } - return l; } + +void set_dasharray(stroke & stroke, list const& l) +{ + for (int i=0; i(l[i]); + if (len(dash) == 2) + { + double d1 = extract(dash[0]); + double d2 = extract(dash[1]); + stroke.add_dash(d1,d2); + } + } +} + } void export_stroke () @@ -101,22 +114,37 @@ void export_stroke () .add_property("line_cap", &stroke::get_line_cap, &stroke::set_line_cap, - "Gets or sets the line cap of this stroke.\n") + "Gets or sets the line cap of this stroke. (alias of linecap)\n") + .add_property("linecap", + &stroke::get_line_cap, + &stroke::set_line_cap, + "Gets or sets the linecap of this stroke.\n") .add_property("line_join", &stroke::get_line_join, &stroke::set_line_join, - "Returns the line join mode of this stroke.\n") + "Returns the line join mode of this stroke. (alias of linejoin)\n") + .add_property("linejoin", + &stroke::get_line_join, + &stroke::set_line_join, + "Returns the linejoin mode of this stroke.\n") .add_property("miterlimit", &stroke::get_miterlimit, &stroke::set_miterlimit, "Returns the miterlimit mode of this stroke.\n") - // todo consider providing a single get/set property .def("add_dash",&stroke::add_dash, (arg("length"),arg("gap")), "Adds a dash segment to the dash patterns of this stroke.\n") .def("get_dashes", get_dashes_list, "Returns the list of dash segments for this stroke.\n") + .add_property("dasharray", + get_dashes_list, + set_dasharray, + "Gets or sets dasharray string of this stroke. (alternate property to add_dash/get_dashes)\n") .add_property("dash_offset", + &stroke::dash_offset, + &stroke::set_dash_offset, + "Gets or sets dash offset of this stroke. (alias of dashoffet)\n") + .add_property("dashoffset", &stroke::dash_offset, &stroke::set_dash_offset, "Gets or sets dash offset of this stroke.\n") diff --git a/tests/python_tests/object_test.py b/tests/python_tests/object_test.py index cb1a3fe5a..2e794da66 100644 --- a/tests/python_tests/object_test.py +++ b/tests/python_tests/object_test.py @@ -30,6 +30,16 @@ def test_line_symbolizer_stroke_reference(): eq_(l.stroke.opacity,1.0) assert_almost_equal(l.stroke.width,0.1) +# https://github.com/mapnik/mapnik/issues/1427 +def test_stroke_dash_api(): + stroke = mapnik.Stroke() + dashes = [(1.0,1.0)] + stroke.dasharray = dashes + eq_(stroke.dasharray, dashes) + stroke.add_dash(.1,.1) + dashes.append((.1,.1)) + eq_(stroke.dasharray, dashes) + # https://github.com/mapnik/mapnik/issues/1420 def test_text_symbolizer_init(): s = mapnik.TextSymbolizer()