Skip to content

Commit

Permalink
Fix the rendering of polygons in basecore2d-based backends (#987)
Browse files Browse the repository at this point in the history
* Fix the rendering of polygons in basecore2d-based backends

This:

- consistently uses Nx2 numpy arrays to store path points
- doesn't create a new subpath after a LINES function

A driveby fix gives a default implementation for show_text_at_point().

* Clean-up comments.

* Fix some unused imports.

* Some of the unused imports are in fact used.

* Improve the situation of line_state_equal a bit.

* Ensure all subpath arguments are numpy arrays.
  • Loading branch information
corranwebster authored Aug 12, 2022
1 parent a71a938 commit de1170a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 26 deletions.
42 changes: 21 additions & 21 deletions kiva/basecore2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"""
import numpy as np
from numpy import alltrue, array, asarray, concatenate, float64, pi, shape
from numpy import alltrue, array, asarray, float64, pi

from .constants import (
CAP_BUTT, CAP_ROUND, CAP_SQUARE, CLOSE, CONCAT_CTM, EOF_FILL_STROKE,
Expand All @@ -40,7 +40,7 @@
TRANSLATE_CTM,
)
from .abstract_graphics_context import AbstractGraphicsContext
from .line_state import LineState, line_state_equal
from .line_state import LineState, line_state_equal # noqa: F401
from .graphics_state import GraphicsState
from .fonttools import Font
import kiva.affine as affine
Expand Down Expand Up @@ -504,6 +504,8 @@ def line_set(self, starts, ends):
Starts and ends should have the same length.
The current point is moved to the last point in 'ends'.
"""
starts = asarray(starts)
ends = asarray(ends)
self._new_subpath()
for i in range(min(len(starts), len(ends))):
self.active_subpath.append((POINT, starts[i]))
Expand Down Expand Up @@ -1054,7 +1056,7 @@ def show_glyphs(self):
def show_text_at_point(self, text, x, y):
"""
"""
pass
self.show_text(text, (x, y))

def show_glyphs_at_point(self):
"""
Expand Down Expand Up @@ -1129,18 +1131,20 @@ def draw_path(self, mode=FILL_STROKE):
for func, args in subpath:
if func == POINT:
self.draw_subpath(mode)
self.add_point_to_subpath(args)
self.add_point_to_subpath(args.reshape(1, 2))
self.first_point = args
elif func == LINE:
self.add_point_to_subpath(args)
self.add_point_to_subpath(args.reshape(1, 2))
elif func == LINES:
self.draw_subpath(mode)
# add all points in list to subpath.
self.add_point_to_subpath(args)
self.first_point = args[0]
elif func == CLOSE:
self.add_point_to_subpath(self.first_point)
self.draw_subpath(mode)
if self.first_point is not None:
self.add_point_to_subpath(
self.first_point.reshape(1, 2)
)
self.draw_subpath(mode)
elif func == RECT:
self.draw_subpath(mode)
self.device_draw_rect(
Expand All @@ -1149,7 +1153,7 @@ def draw_path(self, mode=FILL_STROKE):
elif func in ctm_funcs:
self.device_transform_device_ctm(func, args)
else:
print("oops:", func)
raise RuntimeError(f"Unrecognised subpath term: {func}")
# finally, draw any remaining paths.
self.draw_subpath(mode)

Expand Down Expand Up @@ -1236,22 +1240,18 @@ def add_point_to_subpath(self, pt):

def clear_subpath_points(self):
self.draw_points = []
self.first_point = None

def get_subpath_points(self, debug=0):
""" Gets the points that are in the current path.
def get_subpath_points(self, debug=False):
""" Gets the points that are in the current subpath as an Nx2 array.
The first entry in the draw_points list may actually
be an array. If this is true, the other points are
converted to an array and concatenated with the first
The draw_points attribute holds the current set of points as a
list of Nx2 arrays.
"""
if self.draw_points and len(shape(self.draw_points[0])) > 1:
first_points = self.draw_points[0]
other_points = asarray(self.draw_points[1:])
if len(other_points):
pts = concatenate((first_points, other_points), 0)
else:
pts = first_points
if self.draw_points:
pts = np.vstack(self.draw_points)
else:
# list is empty, convert to an empty array
pts = asarray(self.draw_points)
return pts

Expand Down
11 changes: 6 additions & 5 deletions kiva/tests/test_basecore2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from kiva import affine
from kiva import basecore2d
from kiva import constants
from kiva.line_state import line_state_equal


class TestIsFullyTransparent(unittest.TestCase):
Expand Down Expand Up @@ -70,31 +71,31 @@ def test_color_on_copy(self):
ls1 = self.create_ls()
ls2 = ls1.copy()
ls1.line_color[1] = 10
self.assertTrue(not basecore2d.line_state_equal(ls1, ls2))
self.assertTrue(not line_state_equal(ls1, ls2))

def test_dash_on_copy(self):
ls1 = self.create_ls()
ls2 = ls1.copy()
ls1.line_dash[1][0] = 10
self.assertTrue(not basecore2d.line_state_equal(ls1, ls2))
self.assertTrue(not line_state_equal(ls1, ls2))

def test_cmp_for_different_length_dash_patterns(self):
ls1 = self.create_ls()
ls2 = ls1.copy()
ls1.line_dash = (ls1.line_dash[0], array([10, 10, 10, 10]))
self.assertTrue(not basecore2d.line_state_equal(ls1, ls2))
self.assertTrue(not line_state_equal(ls1, ls2))

def test_cmp(self):
ls1 = self.create_ls()
ls2 = ls1.copy()
self.assertTrue(basecore2d.line_state_equal(ls1, ls2))
self.assertTrue(line_state_equal(ls1, ls2))

# line_dash no longer allowed to be none.
# def test_cmp_with_dash_as_none(self):
# ls1 = self.create_ls()
# ls2 = ls1.copy()
# #ls1.line_dash = None
# assert(not basecore2d.line_state_equal(ls1,ls2))
# assert(not line_state_equal(ls1,ls2))


class GraphicsContextTestCase(unittest.TestCase):
Expand Down

0 comments on commit de1170a

Please sign in to comment.