Skip to content

Support odd-length dash patterns in Agg. #17444

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions lib/matplotlib/tests/test_lines.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,3 +217,9 @@ def test_marker_as_markerstyle():

assert_array_equal(line2.get_marker().vertices, triangle1.vertices)
assert_array_equal(line3.get_marker().vertices, triangle1.vertices)


@check_figures_equal()
def test_odd_dashes(fig_test, fig_ref):
fig_test.add_subplot().plot([1, 2], dashes=[1, 2, 3])
fig_ref.add_subplot().plot([1, 2], dashes=[1, 2, 3, 1, 2, 3])
16 changes: 7 additions & 9 deletions src/py_converters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,6 @@ int convert_dashes(PyObject *dashobj, void *dashesp)
PyObject *dash_offset_obj = NULL;
double dash_offset = 0.0;
PyObject *dashes_seq = NULL;
Py_ssize_t nentries;

if (!PyArg_ParseTuple(dashobj, "OO:dashes", &dash_offset_obj, &dashes_seq)) {
return 0;
Expand Down Expand Up @@ -256,18 +255,17 @@ int convert_dashes(PyObject *dashobj, void *dashesp)
return 0;
}

nentries = PySequence_Size(dashes_seq);
if (nentries % 2 != 0) {
PyErr_Format(PyExc_ValueError, "dashes sequence must have an even number of elements");
return 0;
}
Py_ssize_t nentries = PySequence_Size(dashes_seq);
// If the dashpattern has odd length, iterate through it twice (in
// accordance with the pdf/ps/svg specs).
Py_ssize_t dash_pattern_length = (nentries % 2) ? 2 * nentries : nentries;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a comment what this is doing and why? It's hard to understand if one doesn't know the context.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done


for (Py_ssize_t i = 0; i < nentries; ++i) {
for (Py_ssize_t i = 0; i < dash_pattern_length; ++i) {
PyObject *item;
double length;
double skip;

item = PySequence_GetItem(dashes_seq, i);
item = PySequence_GetItem(dashes_seq, i % nentries);
if (item == NULL) {
return 0;
}
Expand All @@ -280,7 +278,7 @@ int convert_dashes(PyObject *dashobj, void *dashesp)

++i;

item = PySequence_GetItem(dashes_seq, i);
item = PySequence_GetItem(dashes_seq, i % nentries);
if (item == NULL) {
return 0;
}
Expand Down