Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 96ff114

Browse files
authored
Merge pull request #19918 from QuLogic/contour-cleanup
Cleanup some consistency in contour extensions
2 parents f17e491 + ace6442 commit 96ff114

File tree

5 files changed

+39
-51
lines changed

5 files changed

+39
-51
lines changed

src/_contour.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,6 @@
1111
#include <algorithm>
1212

1313

14-
// 'kind' codes.
15-
#define MOVETO 1
16-
#define LINETO 2
17-
#define CLOSEPOLY 79
18-
1914
// Point indices from current quad index.
2015
#define POINT_SW (quad)
2116
#define POINT_SE (quad+1)

src/_path.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1138,15 +1138,14 @@ bool __convert_to_string(PathIterator &path,
11381138
double last_x = 0.0;
11391139
double last_y = 0.0;
11401140

1141-
const int sizes[] = { 1, 1, 2, 3 };
11421141
int size = 0;
11431142
unsigned code;
11441143

11451144
while ((code = path.vertex(&x[0], &y[0])) != agg::path_cmd_stop) {
1146-
if (code == 0x4f) {
1145+
if (code == CLOSEPOLY) {
11471146
buffer += codes[4];
11481147
} else if (code < 5) {
1149-
size = sizes[code - 1];
1148+
size = NUM_VERTICES[code];
11501149

11511150
for (int i = 1; i < size; ++i) {
11521151
unsigned subcode = path.vertex(&x[i], &y[i]);

src/ft2font.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -209,10 +209,10 @@ ft_outline_move_to(FT_Vector const* to, void* user)
209209
ft_outline_decomposer* d = reinterpret_cast<ft_outline_decomposer*>(user);
210210
if (d->codes) {
211211
if (d->index) {
212-
// Appending ENDPOLY is important to make patheffects work.
212+
// Appending CLOSEPOLY is important to make patheffects work.
213213
*(d->vertices++) = 0;
214214
*(d->vertices++) = 0;
215-
*(d->codes++) = ENDPOLY;
215+
*(d->codes++) = CLOSEPOLY;
216216
}
217217
*(d->vertices++) = to->x / 64.;
218218
*(d->vertices++) = to->y / 64.;
@@ -292,7 +292,7 @@ FT2Font::get_path()
292292
"FT_Outline_Decompose failed with error 0x%x", error);
293293
return NULL;
294294
}
295-
if (!decomposer.index) { // Don't append ENDPOLY to null glyphs.
295+
if (!decomposer.index) { // Don't append CLOSEPOLY to null glyphs.
296296
npy_intp vertices_dims[2] = { 0, 2 };
297297
numpy::array_view<double, 2> vertices(vertices_dims);
298298
npy_intp codes_dims[1] = { 0 };
@@ -315,7 +315,7 @@ FT2Font::get_path()
315315
}
316316
*(decomposer.vertices++) = 0;
317317
*(decomposer.vertices++) = 0;
318-
*(decomposer.codes++) = ENDPOLY;
318+
*(decomposer.codes++) = CLOSEPOLY;
319319
return Py_BuildValue("NN", vertices.pyobj(), codes.pyobj());
320320
}
321321

src/mplutils.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,14 @@ inline double mpl_round(double v)
4040
return (double)(int)(v + ((v >= 0.0) ? 0.5 : -0.5));
4141
}
4242

43+
// 'kind' codes for paths.
4344
enum {
4445
STOP = 0,
4546
MOVETO = 1,
4647
LINETO = 2,
4748
CURVE3 = 3,
4849
CURVE4 = 4,
49-
ENDPOLY = 0x4f
50+
CLOSEPOLY = 0x4f
5051
};
5152

5253
const size_t NUM_VERTICES[] = { 1, 1, 1, 2, 3, 1 };

src/tri/_tri.cpp

Lines changed: 31 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,12 @@
77
*/
88
#define NO_IMPORT_ARRAY
99

10+
#include "../mplutils.h"
1011
#include "_tri.h"
1112

1213
#include <algorithm>
1314
#include <set>
1415

15-
#define MOVETO 1
16-
#define LINETO 2
17-
#define CLOSEPOLY 79
18-
1916

2017
TriEdge::TriEdge()
2118
: tri(-1), edge(-1)
@@ -629,14 +626,12 @@ PyObject* TriContourGenerator::contour_line_to_segs_and_kinds(const Contour& con
629626
npy_intp npoints = static_cast<npy_intp>(contour_line.size());
630627

631628
npy_intp segs_dims[2] = {npoints, 2};
632-
PyArrayObject* segs = (PyArrayObject*)PyArray_SimpleNew(
633-
2, segs_dims, NPY_DOUBLE);
634-
double* segs_ptr = (double*)PyArray_DATA(segs);
629+
numpy::array_view<double, 2> segs(segs_dims);
630+
double* segs_ptr = segs.data();
635631

636632
npy_intp codes_dims[1] = {npoints};
637-
PyArrayObject* codes = (PyArrayObject*)PyArray_SimpleNew(
638-
1, codes_dims, NPY_UBYTE);
639-
unsigned char* codes_ptr = (unsigned char*)PyArray_DATA(codes);
633+
numpy::array_view<unsigned char, 1> codes(codes_dims);
634+
unsigned char* codes_ptr = codes.data();
640635

641636
for (ContourLine::const_iterator it = contour_line.begin();
642637
it != contour_line.end(); ++it) {
@@ -650,24 +645,21 @@ PyObject* TriContourGenerator::contour_line_to_segs_and_kinds(const Contour& con
650645
contour_line.front() == contour_line.back())
651646
*(codes_ptr-1) = CLOSEPOLY;
652647

653-
if (PyList_SetItem(vertices_list, i, (PyObject*)segs) ||
654-
PyList_SetItem(codes_list, i, (PyObject*)codes)) {
655-
Py_XDECREF(segs);
656-
Py_XDECREF(codes);
657-
PyErr_SetString(PyExc_RuntimeError,
658-
"Unable to set contour segments and kind codes");
659-
return NULL;
660-
}
648+
PyList_SET_ITEM(vertices_list, i, segs.pyobj());
649+
PyList_SET_ITEM(codes_list, i, codes.pyobj());
661650
}
662651

663652
PyObject* result = PyTuple_New(2);
664-
if (PyTuple_SetItem(result, 0, (PyObject*)vertices_list) ||
665-
PyTuple_SetItem(result, 1, (PyObject*)codes_list)) {
666-
Py_XDECREF(result);
667-
PyErr_SetString(PyExc_RuntimeError,
668-
"Unable to set contour segments and kind codes");
669-
return NULL;
653+
if (result == 0) {
654+
Py_XDECREF(vertices_list);
655+
Py_XDECREF(codes_list);
656+
throw std::runtime_error("Failed to create Python tuple");
670657
}
658+
659+
// No error checking here as filling in a brand new pre-allocated result.
660+
PyTuple_SET_ITEM(result, 0, vertices_list);
661+
PyTuple_SET_ITEM(result, 1, codes_list);
662+
671663
return result;
672664
}
673665

@@ -697,15 +689,13 @@ PyObject* TriContourGenerator::contour_to_segs_and_kinds(const Contour& contour)
697689

698690
// Create segs array for point coordinates.
699691
npy_intp segs_dims[2] = {n_points, 2};
700-
PyArrayObject* segs = (PyArrayObject*)PyArray_SimpleNew(
701-
2, segs_dims, NPY_DOUBLE);
702-
double* segs_ptr = (double*)PyArray_DATA(segs);
692+
numpy::array_view<double, 2> segs(segs_dims);
693+
double* segs_ptr = segs.data();
703694

704695
// Create kinds array for code types.
705696
npy_intp codes_dims[1] = {n_points};
706-
PyArrayObject* codes = (PyArrayObject*)PyArray_SimpleNew(
707-
1, codes_dims, NPY_UBYTE);
708-
unsigned char* codes_ptr = (unsigned char*)PyArray_DATA(codes);
697+
numpy::array_view<unsigned char, 1> codes(codes_dims);
698+
unsigned char* codes_ptr = codes.data();
709699

710700
for (line = contour.begin(); line != contour.end(); ++line) {
711701
for (point = line->begin(); point != line->end(); point++) {
@@ -725,21 +715,24 @@ PyObject* TriContourGenerator::contour_to_segs_and_kinds(const Contour& contour)
725715
throw std::runtime_error("Failed to create Python list");
726716
}
727717

728-
if (PyList_Append(vertices_list, (PyObject*)segs) ||
729-
PyList_Append(codes_list, (PyObject*)codes)) {
718+
if (PyList_Append(vertices_list, segs.pyobj_steal()) ||
719+
PyList_Append(codes_list, codes.pyobj_steal())) {
730720
Py_XDECREF(vertices_list);
731721
Py_XDECREF(codes_list);
732722
throw std::runtime_error("Unable to add contour to vertices and codes lists");
733723
}
734724

735725
PyObject* result = PyTuple_New(2);
736-
if (PyTuple_SetItem(result, 0, (PyObject*)vertices_list) ||
737-
PyTuple_SetItem(result, 1, (PyObject*)codes_list)) {
738-
Py_XDECREF(result);
739-
PyErr_SetString(PyExc_RuntimeError,
740-
"Unable to set contour segments and kinds");
741-
return NULL;
726+
if (result == 0) {
727+
Py_XDECREF(vertices_list);
728+
Py_XDECREF(codes_list);
729+
throw std::runtime_error("Failed to create Python tuple");
742730
}
731+
732+
// No error checking here as filling in a brand new pre-allocated tuple.
733+
PyTuple_SET_ITEM(result, 0, vertices_list);
734+
PyTuple_SET_ITEM(result, 1, codes_list);
735+
743736
return result;
744737
}
745738

0 commit comments

Comments
 (0)