diff --git a/docs/source/kiva/state_ex.py b/docs/source/kiva/state_ex.py index 73ac5e22c..0153a950e 100644 --- a/docs/source/kiva/state_ex.py +++ b/docs/source/kiva/state_ex.py @@ -1,5 +1,5 @@ import math -from kiva import CAP_ROUND, CAP_SQUARE, JOIN_ROUND +from kiva.constants import CAP_ROUND, CAP_SQUARE, JOIN_ROUND from kiva.image import GraphicsContext gc = GraphicsContext((600, 600)) diff --git a/docs/source/kiva_tutorial/tutorial.py b/docs/source/kiva_tutorial/tutorial.py index dfeb3d195..2535ec05f 100644 --- a/docs/source/kiva_tutorial/tutorial.py +++ b/docs/source/kiva_tutorial/tutorial.py @@ -2,7 +2,7 @@ import numpy as np -from kiva.api import CAP_ROUND, CIRCLE_MARKER, Font, STROKE +from kiva.api import DrawMode, Font, LineCap, Marker from kiva.image import GraphicsContext, CompiledPath gc = GraphicsContext((600, 300)) @@ -22,7 +22,7 @@ [400., 200.], [550., 130.] ]) -gc.draw_marker_at_points(points, 4.0, CIRCLE_MARKER) +gc.draw_marker_at_points(points, 4.0, Marker.CIRCLE) gc.save("images/step_2.png") # step 3) Ammeter and Voltmeter @@ -65,7 +65,7 @@ with gc: gc.set_stroke_color((1., 1., 1., 1.)) gc.set_line_width(2) - gc.draw_path_at_points(resistor_locations, clear_resistor_path, STROKE) + gc.draw_path_at_points(resistor_locations, clear_resistor_path, DrawMode.STROKE) #step 4) resistors resistor_path = CompiledPath() @@ -74,7 +74,7 @@ for x, y in resistor_path_points: resistor_path.line_to(x,y) resistor_path.line_to(80, 0) -gc.draw_path_at_points(resistor_locations, resistor_path, STROKE) +gc.draw_path_at_points(resistor_locations, resistor_path, DrawMode.STROKE) gc.save("images/step_45.png") # step 6) switch @@ -130,7 +130,7 @@ (8, -28), ] gc.set_line_width(8) - gc.set_line_cap(CAP_ROUND) + gc.set_line_cap(LineCap.ROUND) gc.line_set(thick_starts, thick_ends) gc.stroke_path() diff --git a/docs/source/kiva_tutorial/tutorial_advanced.py b/docs/source/kiva_tutorial/tutorial_advanced.py index c638ef5c2..cedd57670 100644 --- a/docs/source/kiva_tutorial/tutorial_advanced.py +++ b/docs/source/kiva_tutorial/tutorial_advanced.py @@ -1,8 +1,6 @@ from math import tau -import numpy as np - -from kiva.api import CAP_ROUND, CIRCLE_MARKER, FILL, Font, STROKE +from kiva.api import LineCap, Marker, DrawMode, Font from kiva.image import GraphicsContext, CompiledPath @@ -92,7 +90,7 @@ def draw_wire_connections_at_points(gc, points): """ if hasattr(gc, 'draw_marker_at_points'): - gc.draw_marker_at_points(points, 4.0, CIRCLE_MARKER) + gc.draw_marker_at_points(points, 4.0, Marker.CIRCLE) else: wire_connection_path = CompiledPath() @@ -100,7 +98,7 @@ def draw_wire_connections_at_points(gc, points): wire_connection_path.arc(0, 0, 4, 0, tau) if hasattr(gc, 'draw_path_at_points'): - gc.draw_path_at_points(points, wire_connection_path, FILL) + gc.draw_path_at_points(points, wire_connection_path, DrawMode.FILL) else: for point in points: with gc: @@ -145,7 +143,7 @@ def draw_resistors_at_points(gc, points, resistor_path): """ if hasattr(gc, 'draw_path_at_points'): - gc.draw_path_at_points(points, resistor_path, STROKE) + gc.draw_path_at_points(points, resistor_path, DrawMode.STROKE) else: for point in points: with gc: @@ -230,7 +228,7 @@ def draw_battery(gc, location): thick_starts = [(-8, -10), (-8, -28)] thick_ends = [(8, -10), (8, -28)] gc.set_line_width(8) - gc.set_line_cap(CAP_ROUND) + gc.set_line_cap(LineCap.ROUND) gc.line_set(thick_starts, thick_ends) gc.stroke_path() diff --git a/enable/savage/svg/backends/kiva/renderer.py b/enable/savage/svg/backends/kiva/renderer.py index c50677290..ad2f737c5 100644 --- a/enable/savage/svg/backends/kiva/renderer.py +++ b/enable/savage/svg/backends/kiva/renderer.py @@ -100,8 +100,8 @@ class Pen(object): def __init__(self, color): # fixme: what format is the color passed in? int or float self.color = color - self.cap = constants.CAP_BUTT - self.join = constants.JOIN_MITER + self.cap = constants.LineCap.BUTT + self.join = constants.LineJoin.MITER self.width = 1 self.dasharray = None self.dashoffset = 0.0 @@ -348,18 +348,18 @@ class Renderer(NullRenderer): TransparentPen = Pen((1.0, 1.0, 1.0, 0.0)) caps = { - "butt": constants.CAP_BUTT, - "round": constants.CAP_ROUND, - "square": constants.CAP_SQUARE, + "butt": constants.LineCap.BUTT, + "round": constants.LineCap.ROUND, + "square": constants.LineCap.SQUARE, } joins = { - "miter": constants.JOIN_MITER, - "round": constants.JOIN_ROUND, - "bevel": constants.JOIN_BEVEL, + "miter": constants.LineJoin.MITER, + "round": constants.LineJoin.ROUND, + "bevel": constants.LineJoin.BEVEL, } - fill_rules = {"nonzero": constants.FILL, "evenodd": constants.EOF_FILL} + fill_rules = {"nonzero": constants.DrawMode.FILL, "evenodd": constants.DrawMode.EOF_FILL} def __init__(self): pass @@ -408,14 +408,14 @@ def getCurrentPoint(cls, path): @classmethod def getFont(cls, font_name="Arial"): - kiva_style = constants.NORMAL + kiva_style = constants.FontStyle.NORMAL if "-" in font_name: font_name, style = font_name.split("-", 2) style = style.lower() if "bold" in style: - kiva_style += constants.BOLD + kiva_style |= constants.FontStyle.BOLD if "italic" in style: - kiva_style += constants.ITALIC + kiva_style |= constants.FontStyle.ITALIC return Font(font_name, style=kiva_style) @classmethod diff --git a/enable/tests/trait_defs/test_kiva_font_trait.py b/enable/tests/trait_defs/test_kiva_font_trait.py index 6653c36d1..f1316ca2f 100644 --- a/enable/tests/trait_defs/test_kiva_font_trait.py +++ b/enable/tests/trait_defs/test_kiva_font_trait.py @@ -27,33 +27,33 @@ class TestKivaFont(unittest.TestCase): def test_validate_str(self): expected_outcomes = {} - expected_outcomes[""] = Font(size=10, family=constants.DEFAULT) + expected_outcomes[""] = Font(size=10, family=constants.FontFamily.DEFAULT) for weight, kiva_weight in WEIGHTS.items(): expected_outcomes[weight] = Font( - weight=kiva_weight, size=10, family=constants.DEFAULT) + weight=kiva_weight, size=10, family=constants.FontFamily.DEFAULT) for style, kiva_style in STYLES.items(): expected_outcomes[style] = Font( - style=kiva_style, size=10, family=constants.DEFAULT) + style=kiva_style, size=10, family=constants.FontFamily.DEFAULT) expected_outcomes["underline"] = Font( - underline=True, size=10, family=constants.DEFAULT) + underline=True, size=10, family=constants.FontFamily.DEFAULT) - expected_outcomes["18"] = Font(size=18, family=constants.DEFAULT) - expected_outcomes["18 pt"] = Font(size=18, family=constants.DEFAULT) - expected_outcomes["18 point"] = Font(size=18, family=constants.DEFAULT) + expected_outcomes["18"] = Font(size=18, family=constants.FontFamily.DEFAULT) + expected_outcomes["18 pt"] = Font(size=18, family=constants.FontFamily.DEFAULT) + expected_outcomes["18 point"] = Font(size=18, family=constants.FontFamily.DEFAULT) for family, kiva_family in FAMILIES.items(): expected_outcomes[family] = Font(family=kiva_family, size=10) expected_outcomes["Courier"] = Font( - "Courier", size=10, family=constants.DEFAULT) + "Courier", size=10, family=constants.FontFamily.DEFAULT) expected_outcomes["Comic Sans"] = Font( - "Comic Sans", size=10, family=constants.DEFAULT) + "Comic Sans", size=10, family=constants.FontFamily.DEFAULT) expected_outcomes["18 pt Bold Italic Underline Comic Sans script"] = Font( # noqa: E501 - "Comic Sans", 18, constants.SCRIPT, weight=constants.WEIGHT_BOLD, - style=constants.ITALIC, underline=True, + "Comic Sans", 18, constants.FontFamily.SCRIPT, weight=constants.FontWeight.BOLD, + style=constants.FontStyle.ITALIC, underline=True, ) for name, expected in expected_outcomes.items(): @@ -76,7 +76,7 @@ def test_validate_font(self): self.assertIs(result, font) def test_validate_pyface_font(self): - font = Font("Comic Sans", 18, constants.DEFAULT) + font = Font("Comic Sans", 18, constants.FontFamily.DEFAULT) pyface_font = PyfaceFont(family=["Comic Sans"], size=18) example = FontExample(font=pyface_font) @@ -90,7 +90,7 @@ def test_font_trait_default(self): example = FontExample() self.assertIsInstance(example.font, Font) - self.assertEqual(example.font, Font(size=12, family=constants.SWISS)) + self.assertEqual(example.font, Font(size=12, family=constants.FontFamily.SWISS)) def test_font_trait_none(self): with self.assertRaises(TraitError): diff --git a/enable/trait_defs/kiva_font_trait.py b/enable/trait_defs/kiva_font_trait.py index cdcde72e1..11107f303 100644 --- a/enable/trait_defs/kiva_font_trait.py +++ b/enable/trait_defs/kiva_font_trait.py @@ -24,19 +24,19 @@ #: Mapping from Pyface Font generic family names to corresponding constants. pyface_family_to_kiva_family = { - 'default': kc.DEFAULT, - 'fantasy': kc.DECORATIVE, - 'decorative': kc.DECORATIVE, - 'serif': kc.ROMAN, - 'roman': kc.ROMAN, - 'cursive': kc.SCRIPT, - 'script': kc.SCRIPT, - 'sans-serif': kc.SWISS, - 'swiss': kc.SWISS, - 'monospace': kc.MODERN, - 'modern': kc.MODERN, - 'typewriter': kc.TELETYPE, - 'teletype': kc.TELETYPE, + 'default': kc.FontFamily.DEFAULT, + 'fantasy': kc.FontFamily.DECORATIVE, + 'decorative': kc.FontFamily.DECORATIVE, + 'serif': kc.FontFamily.ROMAN, + 'roman': kc.FontFamily.ROMAN, + 'cursive': kc.FontFamily.SCRIPT, + 'script': kc.FontFamily.SCRIPT, + 'sans-serif': kc.FontFamily.SWISS, + 'swiss': kc.FontFamily.SWISS, + 'monospace': kc.FontFamily.MODERN, + 'modern': kc.FontFamily.MODERN, + 'typewriter': kc.FontFamily.TELETYPE, + 'teletype': kc.FontFamily.TELETYPE, } @@ -62,10 +62,10 @@ def pyface_font_to_font(font): family = pyface_family_to_kiva_family[face] break else: - family = kc.DEFAULT + family = kc.FontFamily.DEFAULT size = int(font.size) weight = font.weight_ - style = kc.NORMAL if font.style == 'normal' else kc.ITALIC + style = kc.FontStyle.NORMAL if font.style == 'normal' else kc.FontStyle.ITALIC underline = 'underline' in font.decorations return Font(face_name, size, family, weight, style, underline) diff --git a/enable/trait_defs/ui/kiva_font_editor.py b/enable/trait_defs/ui/kiva_font_editor.py index b11f5e9de..ceb24add6 100644 --- a/enable/trait_defs/ui/kiva_font_editor.py +++ b/enable/trait_defs/ui/kiva_font_editor.py @@ -22,16 +22,16 @@ #: A mapping of Kiva weight constants to strings. WEIGHTS = { - kc.WEIGHT_THIN: ' Thin', - kc.WEIGHT_EXTRALIGHT: ' Extra-light', - kc.WEIGHT_LIGHT: ' Light', - kc.WEIGHT_NORMAL: '', - kc.WEIGHT_MEDIUM: ' Medium', - kc.WEIGHT_SEMIBOLD: ' Semi-bold', - kc.WEIGHT_BOLD: ' Bold', - kc.WEIGHT_EXTRABOLD: ' Extra-bold', - kc.WEIGHT_HEAVY: ' Heavy', - kc.WEIGHT_EXTRAHEAVY: ' Extra-heavy', + kc.FontWeight.THIN: ' Thin', + kc.FontWeight.EXTRALIGHT: ' Extra-light', + kc.FontWeight.LIGHT: ' Light', + kc.FontWeight.NORMAL: '', + kc.FontWeight.MEDIUM: ' Medium', + kc.FontWeight.SEMIBOLD: ' Semi-bold', + kc.FontWeight.BOLD: ' Bold', + kc.FontWeight.EXTRABOLD: ' Extra-bold', + kc.FontWeight.HEAVY: ' Heavy', + kc.FontWeight.EXTRAHEAVY: ' Extra-heavy', } @@ -123,7 +123,7 @@ def button_clicked(self, event): font = Font( face_name=pyface_font.family[0], weight=pyface_font.weight_, - style=kc.ITALIC if pyface_font.style == 'italic' else kc.NORMAL, # noqa: E501 + style=kc.FontStyle.ITALIC if pyface_font.style == 'italic' else kc.FontStyle.NORMAL, # noqa: E501 size=int(pyface_font.size), ) self.update_object(font) diff --git a/kiva/__init__.py b/kiva/__init__.py index bc09b909e..b37b1f5d1 100644 --- a/kiva/__init__.py +++ b/kiva/__init__.py @@ -13,5 +13,4 @@ """ from kiva._version import full_version as __version__ -from .constants import * from .fonttools import Font diff --git a/kiva/abstract_graphics_context.py b/kiva/abstract_graphics_context.py index 7e1307dba..94bf08533 100644 --- a/kiva/abstract_graphics_context.py +++ b/kiva/abstract_graphics_context.py @@ -9,7 +9,7 @@ # Thanks for using Enthought open source! from abc import ABCMeta, abstractmethod -from .constants import FILL_STROKE, SQUARE_MARKER +from .constants import DrawMode, Marker, TextMode class AbstractGraphicsContext(object, metaclass=ABCMeta): @@ -426,18 +426,12 @@ def eof_fill_path(self): """ @abstractmethod - def draw_path(self, draw_mode=FILL_STROKE): + def draw_path(self, draw_mode: DrawMode = DrawMode.FILL_STROKE): """ Draw the current path with the specified mode - - Parameters - ---------- - draw_mode - One of ``FILL``, ``EOF_FILL``, ``STROKE``, ``FILL_STROKE``, - or ``EOF_FILL_STROKE``. Each is defined in :py:mod:`kiva.api`. """ @abstractmethod - def draw_rect(self, rect, draw_mode=FILL_STROKE): + def draw_rect(self, rect, draw_mode: DrawMode = DrawMode.FILL_STROKE): """ Draw a rectangle with the specified mode Parameters @@ -468,16 +462,8 @@ def draw_image(self, image, rect=None): # ------------------------------------------- @abstractmethod - def set_text_drawing_mode(self, draw_mode): + def set_text_drawing_mode(self, draw_mode: TextMode): """ Set the drawing mode to use with text - - Parameters - ---------- - draw_mode - Allowed values are ``TEXT_FILL``, ``TEXT_STROKE``, - ``TEXT_FILL_STROKE``, ``TEXT_INVISIBLE``, ``TEXT_FILL_CLIP``, - ``TEXT_STROKE_CLIP``, ``TEXT_FILL_STROKE_CLIP``, or - ``TEXT_CLIP``. Each is defined in :py:mod:`kiva.api`. """ @abstractmethod @@ -645,7 +631,7 @@ class EnhancedAbstractGraphicsContext(AbstractGraphicsContext): """ ABC for graphics contexts which provide additional methods """ @abstractmethod - def draw_marker_at_points(self, point_array, size, marker=SQUARE_MARKER): + def draw_marker_at_points(self, point_array, size, marker: Marker = Marker.SQUARE): """ Draw a marker at a collection of points Parameters @@ -669,7 +655,7 @@ def draw_marker_at_points(self, point_array, size, marker=SQUARE_MARKER): @abstractmethod def draw_path_at_points(self, point_array, compiled_path, - draw_mode=FILL_STROKE): + draw_mode: DrawMode = DrawMode.FILL_STROKE): """ Draw a compiled path at a collection of points The starting point of the paths are specified by the points, diff --git a/kiva/agg/tests/test_join_stroke_path.py b/kiva/agg/tests/test_join_stroke_path.py index b769213ef..49165fc4b 100644 --- a/kiva/agg/tests/test_join_stroke_path.py +++ b/kiva/agg/tests/test_join_stroke_path.py @@ -34,7 +34,7 @@ from numpy import array from kiva.agg import GraphicsContextArray -import kiva +from kiva import constants from .test_utils import Utils @@ -71,8 +71,8 @@ def _test_alias_miter(self): """ antialias = False width = 3 - line_cap = kiva.CAP_BUTT - line_join = kiva.JOIN_MITER + line_cap = constants.LineCap.BUTT + line_join = constants.LineJoin.MITER gc = self.helper(antialias, width, line_cap, line_join) @@ -89,8 +89,8 @@ def _test_alias_bevel(self): """ antialias = False width = 3 - line_cap = kiva.CAP_BUTT - line_join = kiva.JOIN_BEVEL + line_cap = constants.LineCap.BUTT + line_join = constants.LineJoin.BEVEL gc = self.helper(antialias, width, line_cap, line_join) actual = gc.bmp_array[:, :, 0] @@ -110,8 +110,8 @@ def _test_alias_round(self): """ antialias = False width = 3 - line_cap = kiva.CAP_BUTT - line_join = kiva.JOIN_ROUND + line_cap = constants.LineCap.BUTT + line_join = constants.LineJoin.ROUND gc = self.helper(antialias, width, line_cap, line_join) actual = gc.bmp_array[:, :, 0] @@ -123,8 +123,8 @@ def test_antialias_miter(self): """ antialias = True width = 3 - line_cap = kiva.CAP_BUTT - line_join = kiva.JOIN_MITER + line_cap = constants.LineCap.BUTT + line_join = constants.LineJoin.MITER gc = self.helper(antialias, width, line_cap, line_join) @@ -148,8 +148,8 @@ def test_antialias_miter(self): def test_antialias_bevel(self): antialias = True width = 3 - line_cap = kiva.CAP_BUTT - line_join = kiva.JOIN_BEVEL + line_cap = constants.LineCap.BUTT + line_join = constants.LineJoin.BEVEL gc = self.helper(antialias, width, line_cap, line_join) actual = gc.bmp_array[:, :, 0] @@ -174,8 +174,8 @@ def test_antialias_round(self): """ antialias = True width = 3 - line_cap = kiva.CAP_BUTT - line_join = kiva.JOIN_ROUND + line_cap = constants.LineCap.BUTT + line_join = constants.LineJoin.ROUND gc = self.helper(antialias, width, line_cap, line_join) actual = gc.bmp_array[:, :, 0] diff --git a/kiva/agg/tests/test_stroke_path.py b/kiva/agg/tests/test_stroke_path.py index ee4f91d18..9a9792c02 100644 --- a/kiva/agg/tests/test_stroke_path.py +++ b/kiva/agg/tests/test_stroke_path.py @@ -62,7 +62,7 @@ from numpy import array from kiva.agg import GraphicsContextArray -import kiva +from kiva import constants from .test_utils import Utils @@ -125,8 +125,8 @@ def test_alias_width_two_outline_aa(self): gc.set_stroke_color((0.0, 0.0, 0.0)) # black gc.set_antialias(False) gc.set_line_width(2) - gc.set_line_cap(kiva.CAP_ROUND) - gc.set_line_join(kiva.JOIN_MITER) + gc.set_line_cap(constants.LineCap.ROUND) + gc.set_line_join(constants.LineJoin.MITER) gc.stroke_path() @@ -154,8 +154,8 @@ def test_alias_width_two_scanline_aa(self): gc.set_stroke_color((0.0, 0.0, 0.0)) # black gc.set_antialias(False) gc.set_line_width(2) - gc.set_line_cap(kiva.CAP_ROUND) - gc.set_line_join(kiva.JOIN_BEVEL) + gc.set_line_cap(constants.LineCap.ROUND) + gc.set_line_join(constants.LineJoin.BEVEL) gc.stroke_path() @@ -210,8 +210,8 @@ def test_alias_cap_round(self): gc.set_stroke_color((0.0, 0.0, 0.0)) # black gc.set_antialias(False) gc.set_line_width(2) - gc.set_line_cap(kiva.CAP_ROUND) - gc.set_line_join(kiva.JOIN_MITER) + gc.set_line_cap(constants.LineCap.ROUND) + gc.set_line_join(constants.LineJoin.MITER) gc.stroke_path() @@ -238,12 +238,12 @@ def test_alias_cap_round_equality(self): """ antialias = False width = 2 - cap = kiva.CAP_ROUND + cap = constants.LineCap.ROUND # join=miter allows the faster outline path through C++ code. - gc1 = self.cap_equality_helper(antialias, width, cap, kiva.JOIN_MITER) + gc1 = self.cap_equality_helper(antialias, width, cap, constants.LineJoin.MITER) # join=bevel forces the scanline path through C++ code. - gc2 = self.cap_equality_helper(antialias, width, cap, kiva.JOIN_BEVEL) + gc2 = self.cap_equality_helper(antialias, width, cap, constants.LineJoin.BEVEL) # Instead of testing against a known desired value, we are simply # testing for equality... @@ -266,8 +266,8 @@ def test_alias_cap_square(self): gc.set_stroke_color((0.0, 0.0, 0.0)) # black gc.set_antialias(False) gc.set_line_width(2) - gc.set_line_cap(kiva.CAP_SQUARE) - gc.set_line_join(kiva.JOIN_MITER) + gc.set_line_cap(constants.LineCap.SQUARE) + gc.set_line_join(constants.LineJoin.MITER) gc.stroke_path() @@ -291,12 +291,12 @@ def test_alias_cap_butt_equality(self): """ antialias = False width = 2 - cap = kiva.CAP_BUTT + cap = constants.LineCap.BUTT # join=miter allows the faster outline path through C++ code. - gc1 = self.cap_equality_helper(antialias, width, cap, kiva.JOIN_MITER) + gc1 = self.cap_equality_helper(antialias, width, cap, constants.LineJoin.MITER) # join=bevel forces the scanline path through C++ code. - gc2 = self.cap_equality_helper(antialias, width, cap, kiva.JOIN_BEVEL) + gc2 = self.cap_equality_helper(antialias, width, cap, constants.LineJoin.BEVEL) # Instead of testing against a known desired value, we are simply # testing for equality... @@ -308,12 +308,12 @@ def test_alias_cap_square_equality(self): """ antialias = False width = 2 - cap = kiva.CAP_SQUARE + cap = constants.LineCap.SQUARE # join=miter allows the faster outline path through C++ code. - gc1 = self.cap_equality_helper(antialias, width, cap, kiva.JOIN_MITER) + gc1 = self.cap_equality_helper(antialias, width, cap, constants.LineJoin.MITER) # join=bevel forces the scanline path through C++ code. - gc2 = self.cap_equality_helper(antialias, width, cap, kiva.JOIN_BEVEL) + gc2 = self.cap_equality_helper(antialias, width, cap, constants.LineJoin.BEVEL) # Instead of testing against a known desired value, we are simply # testing for equality... @@ -343,8 +343,8 @@ def test_antialias_width_one(self): gc.set_stroke_color((0.0, 0.0, 0.0)) # black gc.set_antialias(True) gc.set_line_width(1) - gc.set_line_cap(kiva.CAP_BUTT) - gc.set_line_join(kiva.JOIN_MITER) + gc.set_line_cap(constants.LineCap.BUTT) + gc.set_line_join(constants.LineJoin.MITER) gc.stroke_path() @@ -377,8 +377,8 @@ def test_antialias_width_slower_path(self): gc.set_stroke_color((0.0, 0.0, 0.0)) # black gc.set_antialias(True) gc.set_line_width(1) - gc.set_line_cap(kiva.CAP_BUTT) - gc.set_line_join(kiva.JOIN_BEVEL) + gc.set_line_cap(constants.LineCap.BUTT) + gc.set_line_join(constants.LineJoin.BEVEL) gc.stroke_path() @@ -414,8 +414,8 @@ def test_curve_to(self): gc.set_stroke_color((0.0, 0.0, 0.0)) # black gc.set_antialias(True) gc.set_line_width(1) - gc.set_line_cap(kiva.CAP_BUTT) - gc.set_line_join(kiva.JOIN_MITER) + gc.set_line_cap(constants.LineCap.BUTT) + gc.set_line_join(constants.LineJoin.MITER) gc.stroke_path() diff --git a/kiva/api.py b/kiva/api.py index 1f861a042..5694f5cb0 100644 --- a/kiva/api.py +++ b/kiva/api.py @@ -139,6 +139,8 @@ tsr_factor, trs_factor, transform_point, transform_points, IDENTITY ) from .constants import ( + LineCap, LineJoin, DrawMode, FontStyle, FontFamily, FontWeight, + TextMode, PathPrimitive, CTM, Marker, NO_DASH, CAP_ROUND, CAP_BUTT, CAP_SQUARE, JOIN_ROUND, JOIN_BEVEL, JOIN_MITER, FILL, EOF_FILL, STROKE, FILL_STROKE, EOF_FILL_STROKE, diff --git a/kiva/basecore2d.py b/kiva/basecore2d.py index 25dbd0b59..49a3fd420 100644 --- a/kiva/basecore2d.py +++ b/kiva/basecore2d.py @@ -31,12 +31,7 @@ from numpy import array, asarray, float64, pi from .constants import ( - CAP_BUTT, CAP_ROUND, CAP_SQUARE, CLOSE, CONCAT_CTM, EOF_FILL_STROKE, - EOF_FILL, FILL_STROKE, FILL, JOIN_BEVEL, JOIN_MITER, JOIN_ROUND, LINE, - LINES, LOAD_CTM, NO_DASH, POINT, RECT, ROTATE_CTM, SCALE_CTM, STROKE, - TEXT_CLIP, TEXT_FILL_CLIP, TEXT_FILL_STROKE_CLIP, TEXT_FILL_STROKE, - TEXT_FILL, TEXT_INVISIBLE, TEXT_OUTLINE, TEXT_STROKE_CLIP, TEXT_STROKE, - TRANSLATE_CTM, + LineCap, DrawMode, PathPrimitive, CTM, LineJoin, NO_DASH, TextMode, ) from .abstract_graphics_context import AbstractGraphicsContext from .arc_conversion import arc_to_tangent_points @@ -54,11 +49,11 @@ def is_point(tup): - return tup[0] == POINT + return tup[0] == PathPrimitive.POINT def is_line(tup): - return tup[0] == LINE + return tup[0] == PathPrimitive.LINE def is_fully_transparent(color): @@ -157,7 +152,7 @@ def scale_ctm(self, sx, sy): The new scale factor for the y axis """ self.state.ctm = affine.scale(self.state.ctm, sx, sy) - self.active_subpath.append((SCALE_CTM, (sx, sy))) + self.active_subpath.append((CTM.SCALE, (sx, sy))) self.path_transform_indices.append(len(self.active_subpath) - 1) def translate_ctm(self, tx, ty): @@ -171,7 +166,7 @@ def translate_ctm(self, tx, ty): The distance to move in the y direction """ self.state.ctm = affine.translate(self.state.ctm, tx, ty) - self.active_subpath.append((TRANSLATE_CTM, (tx, ty))) + self.active_subpath.append((CTM.TRANSLATE, (tx, ty))) self.path_transform_indices.append(len(self.active_subpath) - 1) def rotate_ctm(self, angle): @@ -183,7 +178,7 @@ def rotate_ctm(self, angle): the angle, in radians, to rotate the coordinate system """ self.state.ctm = affine.rotate(self.state.ctm, angle) - self.active_subpath.append((ROTATE_CTM, (angle,))) + self.active_subpath.append((CTM.ROTATE, (angle,))) self.path_transform_indices.append(len(self.active_subpath) - 1) def concat_ctm(self, transform): @@ -196,7 +191,7 @@ def concat_ctm(self, transform): the current coordinate matrix. """ self.state.ctm = affine.concat(self.state.ctm, transform) - self.active_subpath.append((CONCAT_CTM, (transform,))) + self.active_subpath.append((CTM.CONCAT, (transform,))) self.path_transform_indices.append(len(self.active_subpath) - 1) def get_ctm(self): @@ -208,7 +203,7 @@ def set_ctm(self, transform): """ Returns the current coordinate transform matrix. """ self.state.ctm = transform - self.active_subpath.append((LOAD_CTM, (transform,))) + self.active_subpath.append((CTM.LOAD, (transform,))) self.path_transform_indices.append(len(self.active_subpath) - 1) # ---------------------------------------------------------------- @@ -228,7 +223,7 @@ def save_state(self): def restore_state(self): """ Restores the previous graphics state. """ self.state = self.state_stack.pop(-1) - self.active_subpath.append((LOAD_CTM, (self.state.ctm,))) + self.active_subpath.append((CTM.LOAD, (self.state.ctm,))) self.path_transform_indices.append(len(self.active_subpath) - 1) # ---------------------------------------------------------------- @@ -288,17 +283,17 @@ def set_line_width(self, width): """ self.state.line_state.line_width = width - def set_line_join(self, style): + def set_line_join(self, style: LineJoin): """ Sets the style for joining lines in a drawing. Parameters ---------- style : join_style The line joining style. The available - styles are JOIN_ROUND, JOIN_BEVEL, JOIN_MITER. + styles are Join.ROUND, Join.BEVEL, Join.MITER. """ - if style not in (JOIN_ROUND, JOIN_BEVEL, JOIN_MITER): + if not isinstance(style, LineJoin): msg = "Invalid line join style. See documentation for valid styles" raise ValueError(msg) self.state.line_state.line_join = style @@ -322,17 +317,17 @@ def set_miter_limit(self, limit): """ self.state.miter_limit = limit - def set_line_cap(self, style): + def set_line_cap(self, style: LineCap): """ Specifies the style of endings to put on line ends. Parameters ---------- style : cap_style The line cap style to use. Available styles - are CAP_ROUND, CAP_BUTT, CAP_SQUARE. + are Cap.ROUND, Cap.BUTT, Cap.SQUARE. """ - if style not in (CAP_ROUND, CAP_BUTT, CAP_SQUARE): + if not isinstance(style, LineCap): msg = "Invalid line cap style. See documentation for valid styles" raise ValueError(msg) self.state.line_state.line_cap = style @@ -457,7 +452,7 @@ def move_to(self, x, y): pt = array((x, y), dtype=float64) self.state.current_point = pt - self.active_subpath.append((POINT, pt)) + self.active_subpath.append((PathPrimitive.POINT, pt)) def line_to(self, x, y): """ Adds a line from the current point to the given point (x, y). @@ -472,7 +467,7 @@ def line_to(self, x, y): """ pt = array((x, y), dtype=float64) self.state.current_point = pt - self.active_subpath.append((LINE, pt)) + self.active_subpath.append((PathPrimitive.LINE, pt)) def lines(self, points): """ Adds a series of lines as a new subpath. @@ -487,7 +482,7 @@ def lines(self, points): """ self._new_subpath() pts = points - self.active_subpath.append((LINES, pts)) + self.active_subpath.append((PathPrimitive.LINES, pts)) self.state.current_point = points[-1] def line_set(self, starts, ends): @@ -507,8 +502,8 @@ def line_set(self, starts, ends): ends = asarray(ends) self._new_subpath() for i in range(min(len(starts), len(ends))): - self.active_subpath.append((POINT, starts[i])) - self.active_subpath.append((LINE, ends[i])) + self.active_subpath.append((PathPrimitive.POINT, starts[i])) + self.active_subpath.append((PathPrimitive.LINE, ends[i])) self.state.current_point = ends[i] def rect(self, x, y, sx, sy): @@ -535,7 +530,7 @@ def close_path(self, tag=None): Currently starts a new subpath -- is this what we want? """ - self.active_subpath.append((CLOSE, (tag,))) + self.active_subpath.append((PathPrimitive.CLOSE, (tag,))) self._new_subpath() def curve_to(self, x_ctrl1, y_ctrl1, x_ctrl2, y_ctrl2, x_to, y_to): @@ -575,7 +570,7 @@ def curve_to(self, x_ctrl1, y_ctrl1, x_ctrl2, y_ctrl2, x_to, y_to): y0*u3 + 3*(y_ctrl1*t*u2 + y_ctrl2*t2*u) + y_to*t3, ] ) - self.active_subpath.append((LINES, pts)) + self.active_subpath.append((PathPrimitive.LINES, pts)) self.state.current_point = pts[-1] def quad_curve_to(self, x_ctrl, y_ctrl, x_to, y_to): @@ -636,7 +631,7 @@ def arc(self, x, y, radius, start_angle, end_angle, cw=False): theta = np.linspace(start_angle, end_angle, n) pts = radius * np.column_stack([np.cos(theta), np.sin(theta)]) pts += np.array([x, y]) - self.active_subpath.append((LINES, pts)) + self.active_subpath.append((PathPrimitive.LINES, pts)) self.state.current_point = pts[-1] def arc_to(self, x1, y1, x2, y2, radius): @@ -967,43 +962,10 @@ def get_character_spacing(self): """ Gets the amount of additional spacing between text characters. """ return self.state.character_spacing - def set_text_drawing_mode(self, mode): + def set_text_drawing_mode(self, mode: TextMode): """ Specifies whether text is drawn filled or outlined or both. - - Parameters - ---------- - - mode - determines how text is drawn to the screen. If - a CLIP flag is set, the font outline is added to the - clipping path. Possible values: - - TEXT_FILL - fill the text - TEXT_STROKE - paint the outline - TEXT_FILL_STROKE - fill and outline - TEXT_INVISIBLE - paint it invisibly ?? - TEXT_FILL_CLIP - fill and add outline clipping path - TEXT_STROKE_CLIP - outline and add outline to clipping path - TEXT_FILL_STROKE_CLIP - fill, outline, and add to clipping path - TEXT_CLIP - add text outline to clipping path - - Note: - wxPython currently ignores all but the INVISIBLE flag. """ - text_modes = ( - TEXT_FILL, TEXT_STROKE, TEXT_FILL_STROKE, TEXT_INVISIBLE, - TEXT_FILL_CLIP, TEXT_STROKE_CLIP, TEXT_FILL_STROKE_CLIP, TEXT_CLIP, - TEXT_OUTLINE, - ) - if mode not in text_modes: + if not isinstance(mode, TextMode): msg = ( "Invalid text drawing mode. See documentation for valid " + "modes" @@ -1099,80 +1061,57 @@ def get_empty_path(self): pass def stroke_path(self): - self.draw_path(mode=STROKE) + self.draw_path(mode=DrawMode.STROKE) def fill_path(self): - self.draw_path(mode=FILL) + self.draw_path(mode=DrawMode.FILL) def eof_fill_path(self): - self.draw_path(mode=EOF_FILL) + self.draw_path(mode=DrawMode.EOF_FILL) - def draw_path(self, mode=FILL_STROKE): - """ Walks through all the drawing subpaths and draw each element. + def draw_path(self, mode: DrawMode = DrawMode.FILL_STROKE): + """Walk through all the drawing subpaths and draw each element. - Each subpath is drawn separately. - - Parameters - ---------- - mode - Specifies how the subpaths are drawn. The default is - FILL_STROKE. The following are valid values. - - FILL - Paint the path using the nonzero winding rule - to determine the regions for painting. - EOF_FILL - Paint the path using the even-odd fill rule. - STROKE - Draw the outline of the path with the - current width, end caps, etc settings. - FILL_STROKE - First fill the path using the nonzero - winding rule, then stroke the path. - EOF_FILL_STROKE - First fill the path using the even-odd - fill method, then stroke the path. + Each subpath is drawn separately. """ - # --------------------------------------------------------------------- - # FILL AND STROKE settings are handled by setting the alpha value of + # FILL and STROKE settings are handled by setting the alpha value of # the line and fill colors to zero (transparent) if stroke or fill # is not needed. - # --------------------------------------------------------------------- old_line_alpha = self.state.line_state.line_color[3] old_fill_alpha = self.state.fill_color[3] - if mode not in [STROKE, FILL_STROKE, EOF_FILL_STROKE]: + if not (mode & DrawMode.STROKE): self.state.line_state.line_color[3] = 0.0 - if mode not in [FILL, EOF_FILL, FILL_STROKE, EOF_FILL_STROKE]: + if not (mode & (DrawMode.FILL | DrawMode.EOF_FILL)): self.state.fill_color[3] = 0.0 self.device_update_line_state() self.device_update_fill_state() ctm_funcs = ( - SCALE_CTM, ROTATE_CTM, TRANSLATE_CTM, CONCAT_CTM, LOAD_CTM, + CTM.SCALE, CTM.ROTATE, CTM.TRANSLATE, CTM.CONCAT, CTM.LOAD, ) for subpath in self.path: # reset the current point for drawing. self.clear_subpath_points() for func, args in subpath: - if func == POINT: + if func == PathPrimitive.POINT: self.draw_subpath(mode) self.add_point_to_subpath(args.reshape(1, 2)) self.first_point = args - elif func == LINE: + elif func == PathPrimitive.LINE: self.add_point_to_subpath(args.reshape(1, 2)) - elif func == LINES: + elif func == PathPrimitive.LINES: # add all points in list to subpath. self.add_point_to_subpath(args) self.first_point = args[0] - elif func == CLOSE: + elif func == PathPrimitive.CLOSE: 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: + elif func == PathPrimitive.RECT: self.draw_subpath(mode) self.device_draw_rect( args[0], args[1], args[2], args[3], mode @@ -1205,17 +1144,17 @@ def device_transform_device_ctm(self, func, args): Many implementations will just use this function. Others can benefit from overriding the method and using hardware acceleration. """ - if func == SCALE_CTM: + if func == CTM.SCALE: self.device_ctm = affine.scale(self.device_ctm, args[0], args[1]) - elif func == ROTATE_CTM: + elif func == CTM.ROTATE: self.device_ctm = affine.rotate(self.device_ctm, args[0]) - elif func == TRANSLATE_CTM: + elif func == CTM.TRANSLATE: self.device_ctm = affine.translate( self.device_ctm, args[0], args[1] ) - elif func == CONCAT_CTM: + elif func == CTM.CONCAT: self.device_ctm = affine.concat(self.device_ctm, args[0]) - elif func == LOAD_CTM: + elif func == CTM.LOAD: self.device_ctm = args[0].copy() def device_draw_rect(self, x, y, sx, sy, mode): @@ -1281,7 +1220,7 @@ def get_subpath_points(self, debug=False): pts = asarray(self.draw_points) return pts - def draw_subpath(self, mode): + def draw_subpath(self, mode: DrawMode): """ Fills and strokes the point path. After the path is drawn, the subpath point list is diff --git a/kiva/blend2d.py b/kiva/blend2d.py index 85a80785f..e2f3fd4f9 100644 --- a/kiva/blend2d.py +++ b/kiva/blend2d.py @@ -15,21 +15,21 @@ import numpy as np from kiva.abstract_graphics_context import AbstractGraphicsContext -import kiva.constants as constants +from kiva import constants from kiva.fonttools import Font # These are the symbols that a backend has to define. __all__ = ["CompiledPath", "Font", "font_metrics_provider", "GraphicsContext"] cap_style = { - constants.CAP_BUTT: blend2d.StrokeCap.CAP_BUTT, - constants.CAP_ROUND: blend2d.StrokeCap.CAP_ROUND, - constants.CAP_SQUARE: blend2d.StrokeCap.CAP_SQUARE, + constants.LineCap.BUTT: blend2d.StrokeCap.CAP_BUTT, + constants.LineCap.ROUND: blend2d.StrokeCap.CAP_ROUND, + constants.LineCap.SQUARE: blend2d.StrokeCap.CAP_SQUARE, } join_style = { - constants.JOIN_ROUND: blend2d.StrokeJoin.JOIN_ROUND, - constants.JOIN_BEVEL: blend2d.StrokeJoin.JOIN_BEVEL, - constants.JOIN_MITER: blend2d.StrokeJoin.JOIN_MITER_BEVEL, + constants.LineJoin.ROUND: blend2d.StrokeJoin.JOIN_ROUND, + constants.LineJoin.BEVEL: blend2d.StrokeJoin.JOIN_BEVEL, + constants.LineJoin.MITER: blend2d.StrokeJoin.JOIN_MITER_BEVEL, } gradient_spread_modes = { "pad": blend2d.ExtendMode.PAD, @@ -43,10 +43,10 @@ # map used in select_font font_styles = { - "regular": (constants.WEIGHT_NORMAL, constants.NORMAL), - "bold": (constants.WEIGHT_BOLD, constants.NORMAL), - "italic": (constants.WEIGHT_NORMAL, constants.ITALIC), - "bold italic": (constants.WEIGHT_BOLD, constants.ITALIC), + "regular": (constants.FontWeight.NORMAL, constants.FontStyle.NORMAL), + "bold": (constants.FontWeight.BOLD, constants.FontStyle.NORMAL), + "italic": (constants.FontWeight.NORMAL, constants.FontStyle.ITALIC), + "bold italic": (constants.FontWeight.BOLD, constants.FontStyle.ITALIC), } class GraphicsContext(object): @@ -67,7 +67,7 @@ def __init__(self, size, *args, **kwargs): self.font = None self._kiva_font = None self.text_pos = (0, 0) - self.text_drawing_mode = constants.TEXT_FILL + self.text_drawing_mode = constants.TextMode.FILL # flip y / HiDPI self.base_scale = kwargs.pop("base_pixel_scale", 1) @@ -161,7 +161,7 @@ def set_line_width(self, width): """ Set the width of the pen used to stroke a path """ self.gc.set_stroke_width(width) - def set_line_join(self, style): + def set_line_join(self, style: constants.LineJoin): """ Set the style of join to use a path corners """ try: @@ -174,11 +174,11 @@ def set_line_join(self, style): def set_miter_limit(self, limit): """ Set the limit at which mitered joins are flattened. - Only applicable when the line join type is set to ``JOIN_MITER``. + Only applicable when the line join type is set to ``Join.MITER``. """ self.gc.set_stroke_miter_limit(limit) - def set_line_cap(self, style): + def set_line_cap(self, style: constants.LineCap): """ Set the style of cap to use a path ends """ try: @@ -267,13 +267,13 @@ def rects(self, rects): for rect in rects: self.path.add_rect(rect) - def draw_rect(self, rect, mode=constants.FILL_STROKE): + def draw_rect(self, rect, mode = constants.DrawMode.FILL_STROKE): """ Draw a rect. """ rect = blend2d.Rect(*rect) - if mode in (constants.FILL, constants.FILL_STROKE): + if mode & constants.DrawMode.FILL: self.gc.fill_rect(rect) - if mode in (constants.STROKE, constants.FILL_STROKE): + if mode & constants.DrawMode.STROKE: self.gc.stroke_rect(rect) def add_path(self, path): @@ -505,14 +505,14 @@ def get_character_spacing(self): msg = "get_character_spacing not implemented on blend2d yet." raise NotImplementedError(msg) - def set_text_drawing_mode(self, mode): + def set_text_drawing_mode(self, mode: constants.TextMode): """ Set the drawing mode to use with text """ supported_modes = { - constants.TEXT_FILL, - constants.TEXT_STROKE, - constants.TEXT_FILL_STROKE, - constants.TEXT_INVISIBLE, + constants.TextMode.FILL, + constants.TextMode.STROKE, + constants.TextMode.FILL_STROKE, + constants.TextMode.INVISIBLE, } if mode not in supported_modes: raise NotImplementedError() @@ -542,7 +542,7 @@ def show_text(self, text, point=None): if self.font is None: raise RuntimeError("show_text called before setting a font!") - if self.text_drawing_mode == constants.TEXT_INVISIBLE: + if self.text_drawing_mode == constants.TextMode.INVISIBLE: # XXX: This is probably more sophisticated in practice return @@ -559,9 +559,9 @@ def show_text(self, text, point=None): self.gc.translate(0, self._height) self.gc.scale(1.0, -1.0) pos = (pos[0], flip_y(pos[1])) - if mode in (constants.TEXT_FILL, constants.TEXT_FILL_STROKE): + if mode in (constants.TextMode.FILL, constants.TextMode.FILL_STROKE): self.gc.fill_text(pos, self.font, text) - if mode in (constants.TEXT_STROKE, constants.TEXT_FILL_STROKE): + if mode in (constants.TextMode.STROKE, constants.TextMode.FILL_STROKE): self.gc.stroke_text(pos, self.font, text) def show_text_at_point(self, text, x, y): @@ -618,12 +618,15 @@ def clear(self, clear_color=(1.0, 1.0, 1.0, 1.0)): self.gc.set_fill_style(clear_color) self.gc.fill_all() - def draw_path(self, mode=constants.FILL_STROKE): + def draw_path( + self, + mode: constants.DrawMode = constants.DrawMode.FILL_STROKE, + ): """ Draw the current path with the specified mode """ - if mode in (constants.FILL, constants.FILL_STROKE): + if mode & constants.DrawMode.FILL: self.gc.fill_path(self.path) - if mode in (constants.STROKE, constants.FILL_STROKE): + if mode & constants.DrawMode.STROKE: self.gc.stroke_path(self.path) self.begin_path() @@ -632,13 +635,22 @@ def get_empty_path(self): """ return CompiledPath() - def draw_path_at_points(self, points, path, mode=constants.FILL_STROKE): + def draw_path_at_points( + self, + points, + path, + mode: constants.DrawMode = constants.DrawMode.FILL_STROKE, + ): """ Draw a path object at many different points. """ raise NotImplementedError() - def draw_marker_at_points(self, points_array, size, - marker=constants.SQUARE_MARKER): + def draw_marker_at_points( + self, + points_array, + size, + marker: constants.Marker = constants.Marker.SQUARE + ): """ Draw a marker at a collection of points """ raise NotImplementedError() diff --git a/kiva/cairo.py b/kiva/cairo.py index cfe0faaea..8e8602b57 100644 --- a/kiva/cairo.py +++ b/kiva/cairo.py @@ -27,15 +27,15 @@ line_join = { - constants.JOIN_BEVEL: cairo.LINE_JOIN_BEVEL, - constants.JOIN_MITER: cairo.LINE_JOIN_MITER, - constants.JOIN_ROUND: cairo.LINE_JOIN_ROUND, + constants.LineJoin.BEVEL: cairo.LINE_JOIN_BEVEL, + constants.LineJoin.MITER: cairo.LINE_JOIN_MITER, + constants.LineJoin.ROUND: cairo.LINE_JOIN_ROUND, } line_cap = { - constants.CAP_BUTT: cairo.LINE_CAP_BUTT, - constants.CAP_ROUND: cairo.LINE_CAP_ROUND, - constants.CAP_SQUARE: cairo.LINE_CAP_SQUARE, + constants.LineCap.BUTT: cairo.LINE_CAP_BUTT, + constants.LineCap.ROUND: cairo.LINE_CAP_ROUND, + constants.LineCap.SQUARE: cairo.LINE_CAP_SQUARE, } font_slant = { @@ -60,24 +60,24 @@ text_draw_modes = { "FILL": ( - constants.TEXT_FILL, - constants.TEXT_FILL_CLIP, - constants.TEXT_FILL_STROKE, - constants.TEXT_FILL_STROKE_CLIP, + constants.TextMode.FILL, + constants.TextMode.FILL_CLIP, + constants.TextMode.FILL_STROKE, + constants.TextMode.FILL_STROKE_CLIP, ), "STROKE": ( - constants.TEXT_FILL_STROKE, - constants.TEXT_FILL_STROKE_CLIP, - constants.TEXT_STROKE, - constants.TEXT_STROKE_CLIP, + constants.TextMode.FILL_STROKE, + constants.TextMode.FILL_STROKE_CLIP, + constants.TextMode.STROKE, + constants.TextMode.STROKE_CLIP, ), "CLIP": ( - constants.TEXT_CLIP, - constants.TEXT_FILL_CLIP, - constants.TEXT_FILL_STROKE_CLIP, - constants.TEXT_STROKE_CLIP, + constants.TextMode.CLIP, + constants.TextMode.FILL_CLIP, + constants.TextMode.FILL_STROKE_CLIP, + constants.TextMode.STROKE_CLIP, ), - "INVISIBLE": constants.TEXT_INVISIBLE, + "INVISIBLE": constants.TextMode.INVISIBLE, } @@ -195,7 +195,7 @@ def __init__(self): self.fill_color = [1, 1, 1] self.stroke_color = [1, 1, 1] self.alpha = 1.0 - self.text_drawing_mode = constants.TEXT_FILL + self.text_drawing_mode = constants.TextMode.FILL self.has_gradient = False # not implemented yet... @@ -245,7 +245,7 @@ def clear(self, color=(1, 1, 1)): else: self._ctx.set_source_rgb(*color) self.rect(0, 0, self.width(), self.height()) - self.draw_path(constants.FILL) + self.draw_path(constants.DrawMode.FILL) self.restore_state() def height(self): @@ -354,7 +354,7 @@ def set_line_width(self, width): """ self._ctx.set_line_width(width) - def set_line_join(self, style): + def set_line_join(self, style: constants.LineJoin): """ Sets the style for joining lines in a drawing. Parameters @@ -387,7 +387,7 @@ def set_miter_limit(self, limit): """ self._ctx.set_miter_limit(limit) - def set_line_cap(self, style): + def set_line_cap(self, style: constants.LineCap): """ Specifies the style of endings to put on line ends. Parameters @@ -1031,7 +1031,7 @@ def set_character_spacing(self, spacing): """ self.state.character_spacing = spacing - def set_text_drawing_mode(self, mode): + def set_text_drawing_mode(self, mode: constants.TextMode): """ Specifies whether text is drawn filled or outlined or both. Parameters @@ -1062,14 +1062,7 @@ def set_text_drawing_mode(self, mode): Note: wxPython currently ignores all but the INVISIBLE flag. """ - text_modes = ( - constants.TEXT_FILL, constants.TEXT_STROKE, - constants.TEXT_FILL_STROKE, constants.TEXT_INVISIBLE, - constants.TEXT_FILL_CLIP, constants.TEXT_STROKE_CLIP, - constants.TEXT_FILL_STROKE_CLIP, constants.TEXT_CLIP, - constants.TEXT_OUTLINE, - ) - if mode not in text_modes: + if not isinstance(mode, constants.TextMode): msg = ( "Invalid text drawing mode. See documentation for valid modes" ) @@ -1152,7 +1145,7 @@ def show_glyphs_at_point(self): # Painting paths (drawing and filling contours) # ---------------------------------------------------------------- - def draw_path(self, mode=constants.FILL_STROKE): + def draw_path(self, mode=constants.DrawMode.FILL_STROKE): """ Walks through all the drawing subpaths and draw each element. Each subpath is drawn separately. @@ -1180,20 +1173,20 @@ def draw_path(self, mode=constants.FILL_STROKE): """ ctx = self._ctx fr = ctx.get_fill_rule() - if mode in [constants.EOF_FILL, constants.EOF_FILL_STROKE]: + if mode in [constants.DrawMode.EOF_FILL, constants.DrawMode.EOF_FILL_STROKE]: ctx.set_fill_rule(cairo.FILL_RULE_EVEN_ODD) else: ctx.set_fill_rule(cairo.FILL_RULE_WINDING) - if mode in [constants.FILL, constants.EOF_FILL]: + if mode in [constants.DrawMode.FILL, constants.DrawMode.EOF_FILL]: if not self.state.has_gradient: self._set_source_color(self.state.fill_color) ctx.fill() - elif mode == constants.STROKE: + elif mode == constants.DrawMode.STROKE: if not self.state.has_gradient: self._set_source_color(self.state.stroke_color) ctx.stroke() - elif mode in [constants.FILL_STROKE, constants.EOF_FILL_STROKE]: + elif mode in [constants.DrawMode.FILL_STROKE, constants.DrawMode.EOF_FILL_STROKE]: if not self.state.has_gradient: self._set_source_color(self.state.fill_color) ctx.fill_preserve() diff --git a/kiva/celiagg.py b/kiva/celiagg.py index 57589d941..dc5e83d83 100644 --- a/kiva/celiagg.py +++ b/kiva/celiagg.py @@ -18,7 +18,7 @@ import numpy as np from kiva.abstract_graphics_context import AbstractGraphicsContext -import kiva.constants as constants +from kiva import constants from kiva.fonttools import Font from kiva.marker_renderer import MarkerRenderer @@ -26,44 +26,44 @@ __all__ = ["CompiledPath", "Font", "font_metrics_provider", "GraphicsContext"] cap_style = { - constants.CAP_ROUND: agg.LineCap.CapRound, - constants.CAP_SQUARE: agg.LineCap.CapSquare, - constants.CAP_BUTT: agg.LineCap.CapButt, + constants.LineCap.ROUND: agg.LineCap.CapRound, + constants.LineCap.SQUARE: agg.LineCap.CapSquare, + constants.LineCap.BUTT: agg.LineCap.CapButt, } join_style = { - constants.JOIN_ROUND: agg.LineJoin.JoinRound, - constants.JOIN_BEVEL: agg.LineJoin.JoinBevel, - constants.JOIN_MITER: agg.LineJoin.JoinMiter, + constants.LineJoin.ROUND: agg.LineJoin.JoinRound, + constants.LineJoin.BEVEL: agg.LineJoin.JoinBevel, + constants.LineJoin.MITER: agg.LineJoin.JoinMiter, } draw_modes = { - constants.FILL: agg.DrawingMode.DrawFill, - constants.EOF_FILL: agg.DrawingMode.DrawEofFill, - constants.STROKE: agg.DrawingMode.DrawStroke, - constants.FILL_STROKE: agg.DrawingMode.DrawFillStroke, - constants.EOF_FILL_STROKE: agg.DrawingMode.DrawEofFillStroke, + constants.DrawMode.FILL: agg.DrawingMode.DrawFill, + constants.DrawMode.EOF_FILL: agg.DrawingMode.DrawEofFill, + constants.DrawMode.STROKE: agg.DrawingMode.DrawStroke, + constants.DrawMode.FILL_STROKE: agg.DrawingMode.DrawFillStroke, + constants.DrawMode.EOF_FILL_STROKE: agg.DrawingMode.DrawEofFillStroke, } font_weights = { - constants.WEIGHT_THIN: agg.FontWeight.Thin, - constants.WEIGHT_EXTRALIGHT: agg.FontWeight.ExtraLight, - constants.WEIGHT_LIGHT: agg.FontWeight.Light, - constants.WEIGHT_NORMAL: agg.FontWeight.Regular, - constants.WEIGHT_MEDIUM: agg.FontWeight.Medium, - constants.WEIGHT_SEMIBOLD: agg.FontWeight.SemiBold, - constants.WEIGHT_BOLD: agg.FontWeight.Bold, - constants.WEIGHT_EXTRABOLD: agg.FontWeight.ExtraBold, - constants.WEIGHT_HEAVY: agg.FontWeight.Heavy, - constants.WEIGHT_EXTRAHEAVY: agg.FontWeight.Heavy, + constants.FontWeight.THIN: agg.FontWeight.Thin, + constants.FontWeight.EXTRALIGHT: agg.FontWeight.ExtraLight, + constants.FontWeight.LIGHT: agg.FontWeight.Light, + constants.FontWeight.NORMAL: agg.FontWeight.Regular, + constants.FontWeight.MEDIUM: agg.FontWeight.Medium, + constants.FontWeight.SEMIBOLD: agg.FontWeight.SemiBold, + constants.FontWeight.BOLD: agg.FontWeight.Bold, + constants.FontWeight.EXTRABOLD: agg.FontWeight.ExtraBold, + constants.FontWeight.HEAVY: agg.FontWeight.Heavy, + constants.FontWeight.EXTRAHEAVY: agg.FontWeight.Heavy, } text_modes = { - constants.TEXT_FILL: agg.TextDrawingMode.TextDrawRaster, - constants.TEXT_STROKE: agg.TextDrawingMode.TextDrawStroke, - constants.TEXT_FILL_STROKE: agg.TextDrawingMode.TextDrawFillStroke, - constants.TEXT_INVISIBLE: agg.TextDrawingMode.TextDrawInvisible, - constants.TEXT_FILL_CLIP: agg.TextDrawingMode.TextDrawFillClip, - constants.TEXT_STROKE_CLIP: agg.TextDrawingMode.TextDrawStrokeClip, - constants.TEXT_FILL_STROKE_CLIP: agg.TextDrawingMode.TextDrawFillStrokeClip, # noqa - constants.TEXT_CLIP: agg.TextDrawingMode.TextDrawClip, - constants.TEXT_OUTLINE: agg.TextDrawingMode.TextDrawStroke, + constants.TextMode.FILL: agg.TextDrawingMode.TextDrawRaster, + constants.TextMode.STROKE: agg.TextDrawingMode.TextDrawStroke, + constants.TextMode.FILL_STROKE: agg.TextDrawingMode.TextDrawFillStroke, + constants.TextMode.INVISIBLE: agg.TextDrawingMode.TextDrawInvisible, + constants.TextMode.FILL_CLIP: agg.TextDrawingMode.TextDrawFillClip, + constants.TextMode.STROKE_CLIP: agg.TextDrawingMode.TextDrawStrokeClip, + constants.TextMode.FILL_STROKE_CLIP: agg.TextDrawingMode.TextDrawFillStrokeClip, # noqa + constants.TextMode.CLIP: agg.TextDrawingMode.TextDrawClip, + constants.TextMode.OUTLINE: agg.TextDrawingMode.TextDrawStroke, } gradient_coord_modes = { 'userSpaceOnUse': agg.GradientUnits.UserSpace, @@ -95,10 +95,10 @@ # map used in select_font font_styles = { - "regular": (constants.WEIGHT_NORMAL, constants.NORMAL), - "bold": (constants.WEIGHT_BOLD, constants.NORMAL), - "italic": (constants.WEIGHT_NORMAL, constants.ITALIC), - "bold italic": (constants.WEIGHT_BOLD, constants.ITALIC), + "regular": (constants.FontWeight.NORMAL, constants.FontStyle.NORMAL), + "bold": (constants.FontWeight.BOLD, constants.FontStyle.NORMAL), + "italic": (constants.FontWeight.NORMAL, constants.FontStyle.ITALIC), + "bold italic": (constants.FontWeight.BOLD, constants.FontStyle.ITALIC), } class GraphicsContext(object): @@ -392,7 +392,11 @@ def rects(self, rects): """ self.path.rects(rects) - def draw_rect(self, rect, mode=constants.FILL_STROKE): + def draw_rect( + self, + rect, + mode: constants.DrawMode = constants.DrawMode.FILL_STROKE, + ): """ Draw a rect. """ @@ -834,7 +838,10 @@ def clear_rect(self, rect): def clear(self, clear_color=(1.0, 1.0, 1.0, 1.0)): self.gc.clear(*clear_color) - def draw_path(self, mode=constants.FILL_STROKE): + def draw_path( + self, + mode: constants.DrawMode = constants.DrawMode.FILL_STROKE, + ): """ Walk through all the drawing subpaths and draw each element. Each subpath is drawn separately. @@ -854,7 +861,12 @@ def get_empty_path(self): """ return CompiledPath() - def draw_path_at_points(self, points, path, mode=constants.FILL_STROKE): + def draw_path_at_points( + self, + points, + path, + mode: constants.DrawMode = constants.DrawMode.FILL_STROKE, + ): """ Draw a path object at many different points. """ self.canvas_state.drawing_mode = draw_modes[mode] @@ -867,8 +879,11 @@ def draw_path_at_points(self, points, path, mode=constants.FILL_STROKE): fill=self.fill_paint, ) - def draw_marker_at_points(self, points_array, size, - marker=constants.SQUARE_MARKER): + def draw_marker_at_points( + self, + points_array, + size, + marker: constants.Marker = constants.Marker.SQUARE): """ Draw a marker at a collection of points """ # Apply the current transform diff --git a/kiva/constants.py b/kiva/constants.py index 3b9588bf1..3a94bc3f8 100644 --- a/kiva/constants.py +++ b/kiva/constants.py @@ -9,6 +9,8 @@ # Thanks for using Enthought open source! """ Constants used by core2d drawing engine. """ +from enum import IntEnum, IntFlag + from numpy import array # -------------------------------------------------------------------- @@ -21,17 +23,29 @@ # Line Cap Constants # -------------------------------------------------------------------- -CAP_ROUND = 0 -CAP_BUTT = 1 -CAP_SQUARE = 2 +class LineCap(IntEnum): + "Line cap styles." + ROUND = 0 + BUTT = 1 + SQUARE = 2 + +CAP_ROUND = LineCap.ROUND +CAP_BUTT = LineCap.BUTT +CAP_SQUARE = LineCap.SQUARE # -------------------------------------------------------------------- # Line Join Constants # -------------------------------------------------------------------- -JOIN_ROUND = 0 -JOIN_BEVEL = 1 -JOIN_MITER = 2 +class LineJoin(IntEnum): + "Line join styles." + ROUND = 0 + BEVEL = 1 + MITER = 2 + +JOIN_ROUND = LineJoin.ROUND +JOIN_BEVEL = LineJoin.BEVEL +JOIN_MITER = LineJoin.MITER # -------------------------------------------------------------------- # Path Drawing Mode Constants @@ -41,59 +55,155 @@ # C version. # -------------------------------------------------------------------- -FILL = 1 -EOF_FILL = 2 -STROKE = 4 -FILL_STROKE = 5 -EOF_FILL_STROKE = 6 +class DrawMode(IntFlag): + """Drawing mode flags. + + FILL + Paint the path using the nonzero winding rule + to determine the regions for painting. + + EOF_FILL + Paint the path using the even-odd fill rule. + + STROKE + Draw the outline of the path with the + current width, end caps, etc settings. + + Note that FILL and EOF_FILL are mutually exclusive, so modes 3 + and 7 aren't valid. + + Outlines are stroked after any filling is performed. + """ + FILL = 1 + EOF_FILL = 2 + STROKE = 4 + FILL_STROKE = 5 + EOF_FILL_STROKE = 6 + +FILL = DrawMode.FILL +EOF_FILL = DrawMode.EOF_FILL +STROKE = DrawMode.STROKE +FILL_STROKE = DrawMode.FILL_STROKE +EOF_FILL_STROKE = DrawMode.EOF_FILL_STROKE # ----------------------------------------------------------------------------- # Font Constants # ----------------------------------------------------------------------------- -NORMAL = 0 -BOLD = 1 -ITALIC = 2 -BOLD_ITALIC = 3 +class FontStyle(IntFlag): + """Basic font styles.""" + NORMAL = 0 + BOLD = 1 + ITALIC = 2 + +NORMAL = FontStyle.NORMAL +BOLD = FontStyle.BOLD +ITALIC = FontStyle.ITALIC +BOLD_ITALIC = FontStyle.BOLD | FontStyle.ITALIC # convenience sets for styles bold_styles = {BOLD, BOLD_ITALIC} italic_styles = {ITALIC, BOLD_ITALIC} # Font families, as defined by the Windows API, and their CSS equivalents -DEFAULT = 0 -SWISS = 1 # Sans-serif -ROMAN = 2 # Serif -MODERN = 3 # Monospace -DECORATIVE = 4 # Fantasy -SCRIPT = 5 # Cursive -TELETYPE = 6 +class FontFamily(IntEnum): + """Standard font family names""" + DEFAULT = 0 + SWISS = 1 # Sans-serif + ROMAN = 2 # Serif + MODERN = 3 # Monospace + DECORATIVE = 4 # Fantasy + SCRIPT = 5 # Cursive + TELETYPE = 6 + +DEFAULT = FontFamily.DEFAULT +SWISS = FontFamily.SWISS +ROMAN = FontFamily.ROMAN +MODERN = FontFamily.MODERN +DECORATIVE = FontFamily.DECORATIVE +SCRIPT = FontFamily.SCRIPT +TELETYPE = FontFamily.TELETYPE + # Font weight constants -WEIGHT_THIN = 100 -WEIGHT_EXTRALIGHT = 200 -WEIGHT_LIGHT = 300 -WEIGHT_NORMAL = 400 -WEIGHT_MEDIUM = 500 -WEIGHT_SEMIBOLD = 600 -WEIGHT_BOLD = 700 -WEIGHT_EXTRABOLD = 800 -WEIGHT_HEAVY = 900 -WEIGHT_EXTRAHEAVY = 1000 +class FontWeight(IntEnum): + """Font weights""" + THIN = 100 + EXTRALIGHT = 200 + LIGHT = 300 + NORMAL = 400 + MEDIUM = 500 + SEMIBOLD = 600 + BOLD = 700 + EXTRABOLD = 800 + HEAVY = 900 + EXTRAHEAVY = 1000 + +WEIGHT_THIN = FontWeight.THIN +WEIGHT_EXTRALIGHT = FontWeight.EXTRALIGHT +WEIGHT_LIGHT = FontWeight.LIGHT +WEIGHT_NORMAL = FontWeight.NORMAL +WEIGHT_MEDIUM = FontWeight.MEDIUM +WEIGHT_SEMIBOLD = FontWeight.SEMIBOLD +WEIGHT_BOLD = FontWeight.BOLD +WEIGHT_EXTRABOLD = FontWeight.EXTRABOLD +WEIGHT_HEAVY = FontWeight.HEAVY +WEIGHT_EXTRAHEAVY = FontWeight.EXTRAHEAVY # ----------------------------------------------------------------------------- # Text Drawing Mode Constants # ----------------------------------------------------------------------------- -TEXT_FILL = 0 -TEXT_STROKE = 1 -TEXT_FILL_STROKE = 2 -TEXT_INVISIBLE = 3 -TEXT_FILL_CLIP = 4 -TEXT_STROKE_CLIP = 5 -TEXT_FILL_STROKE_CLIP = 6 -TEXT_CLIP = 7 -TEXT_OUTLINE = 8 +class TextMode(IntEnum): + """Text drawing mode. + + Determines how text is drawn to the screen. If a CLIP flag is set, the + font outline is added to the clipping path. Possible values: + + FILL + fill the text + + STROKE + paint the outline + + FILL_STROKE + fill and outline + + INVISIBLE + paint it invisibly ?? + + FILL_CLIP + fill and add outline clipping path + + STROKE_CLIP + outline and add outline to clipping path + + FILL_STROKE_CLIP + fill, outline, and add to clipping path + + CLIP + add text outline to clipping path + + """ + FILL = 0 + STROKE = 1 + FILL_STROKE = 2 + INVISIBLE = 3 + FILL_CLIP = 4 + STROKE_CLIP = 5 + FILL_STROKE_CLIP = 6 + CLIP = 7 + OUTLINE = 8 + +TEXT_FILL = TextMode.FILL +TEXT_STROKE = TextMode.STROKE +TEXT_FILL_STROKE = TextMode.FILL_STROKE +TEXT_INVISIBLE = TextMode.INVISIBLE +TEXT_FILL_CLIP = TextMode.FILL_CLIP +TEXT_STROKE_CLIP = TextMode.STROKE_CLIP +TEXT_FILL_STROKE_CLIP = TextMode.FILL_STROKE_CLIP +TEXT_CLIP = TextMode.CLIP +TEXT_OUTLINE = TextMode.OUTLINE # ----------------------------------------------------------------------------- # Subpath Drawing Primitive Constants @@ -101,27 +211,53 @@ # Used by the drawing state machine to determine what object to draw. # ----------------------------------------------------------------------------- -POINT = 0 -LINE = 1 -LINES = 2 -RECT = 3 -CLOSE = 4 -CURVE_TO = 5 -QUAD_CURVE_TO = 6 -ARC = 7 -ARC_TO = 8 +class PathPrimitive(IntEnum): + """Subpath drawing primitive constants + + Used by the drawing state machine to determine what object to draw. + """ + POINT = 0 + LINE = 1 + LINES = 2 + RECT = 3 + CLOSE = 4 + CURVE_TO = 5 + QUAD_CURVE_TO = 6 + ARC = 7 + ARC_TO = 8 + +POINT = PathPrimitive.POINT +LINE = PathPrimitive.LINE +LINES = PathPrimitive.LINES +RECT = PathPrimitive.RECT +CLOSE = PathPrimitive.CLOSE +CURVE_TO = PathPrimitive.CURVE_TO +QUAD_CURVE_TO = PathPrimitive.QUAD_CURVE_TO +ARC = PathPrimitive.ARC +ARC_TO = PathPrimitive.ARC_TO # ----------------------------------------------------------------------------- # Subpath CTM Constants -# # ----------------------------------------------------------------------------- -SCALE_CTM = 5 -TRANSLATE_CTM = 6 -ROTATE_CTM = 7 -CONCAT_CTM = 8 -LOAD_CTM = 9 +class CTM(IntEnum): + """Subpath CTM Constants + + These are added so its possible for OpenGL to do the matrix transformations + on the data (its much faster than doing it with NumPy). + """ + SCALE = 5 + TRANSLATE = 6 + ROTATE = 7 + CONCAT = 8 + LOAD = 9 + +SCALE_CTM = CTM.SCALE +TRANSLATE_CTM = CTM.TRANSLATE +ROTATE_CTM = CTM.ROTATE +CONCAT_CTM = CTM.CONCAT +LOAD_CTM = CTM.LOAD # ----------------------------------------------------------------------------- @@ -134,14 +270,35 @@ # Note that draw_marker_at_points takes a marker name as a string. # ----------------------------------------------------------------------------- -NO_MARKER = 0 -SQUARE_MARKER = 1 -DIAMOND_MARKER = 2 -CIRCLE_MARKER = 3 -CROSSED_CIRCLE_MARKER = 4 -CROSS_MARKER = 5 -TRIANGLE_MARKER = 6 -INVERTED_TRIANGLE_MARKER = 7 -PLUS_MARKER = 8 -DOT_MARKER = 9 -PIXEL_MARKER = 10 +class Marker(IntEnum): + """Marker Types + + These are the marker types for draw_marker_at_points. Some backends + (like Agg) have fast implementations for these; other backends manually + construct the paths representing these markers. + + Note that draw_marker_at_points takes a marker name as a string. + """ + NONE = 0 + SQUARE = 1 + DIAMOND = 2 + CIRCLE = 3 + CROSSED_CIRCLE = 4 + CROSS = 5 + TRIANGLE = 6 + INVERTED_TRIANGLE = 7 + PLUS = 8 + DOT = 9 + PIXEL = 10 + +NO_MARKER = Marker.NONE +SQUARE_MARKER = Marker.SQUARE +DIAMOND_MARKER = Marker.DIAMOND +CIRCLE_MARKER = Marker.CIRCLE +CROSSED_CIRCLE_MARKER = Marker.CROSSED_CIRCLE +CROSS_MARKER = Marker.CROSS +TRIANGLE_MARKER = Marker.TRIANGLE +INVERTED_TRIANGLE_MARKER = Marker.INVERTED_TRIANGLE +PLUS_MARKER = Marker.PLUS +DOT_MARKER = Marker.DOT +PIXEL_MARKER = Marker.PIXEL diff --git a/kiva/examples/kiva/Kiva Explorer.ipynb b/kiva/examples/kiva/Kiva Explorer.ipynb index 38bc5b3db..e5345b98b 100644 --- a/kiva/examples/kiva/Kiva Explorer.ipynb +++ b/kiva/examples/kiva/Kiva Explorer.ipynb @@ -45,8 +45,8 @@ "\n", " gc.set_stroke_color((0.0, 0.0, 1.0, 1.0))\n", " gc.set_line_width(7)\n", - " gc.set_line_join(constants.JOIN_ROUND)\n", - " gc.set_line_cap(constants.CAP_ROUND)\n", + " gc.set_line_join(constants.LineJoin.ROUND)\n", + " gc.set_line_cap(constants.LineCap.ROUND)\n", " gc.rect(100, 400, 50, 50)\n", " gc.stroke_path()\n", "\n", diff --git a/kiva/examples/kiva/agg/benchmark.py b/kiva/examples/kiva/agg/benchmark.py index 3d28eb036..c555ae829 100644 --- a/kiva/examples/kiva/agg/benchmark.py +++ b/kiva/examples/kiva/agg/benchmark.py @@ -15,7 +15,7 @@ from numpy import array, shape, arange, transpose, sin, cos, zeros, pi from scipy import stats -import kiva +from kiva import constants from kiva import agg @@ -86,11 +86,11 @@ def benchmark_draw_path_flags(cycles=10, n_pts=1000, sz=(1000, 1000)): x[:] = arange(0, width, interval) flags = [ - kiva.FILL, - kiva.EOF_FILL, - kiva.STROKE, - kiva.FILL_STROKE, - kiva.EOF_FILL_STROKE, + constants.FILL, + constants.EOF_FILL, + constants.STROKE, + constants.FILL_STROKE, + constants.EOF_FILL_STROKE, ] for flag in flags: diff --git a/kiva/examples/kiva/agg/dash.py b/kiva/examples/kiva/agg/dash.py index 118e5b26f..723ce7f66 100644 --- a/kiva/examples/kiva/agg/dash.py +++ b/kiva/examples/kiva/agg/dash.py @@ -11,8 +11,8 @@ from numpy import array -import kiva from kiva import agg +from kiva import constants def dash(sz=(1000, 1000)): @@ -26,7 +26,7 @@ def dash(sz=(1000, 1000)): phase = width * 2.5 pattern = width * array((5, 5)) gc.set_line_dash(pattern, phase) - gc.set_line_cap(kiva.CAP_BUTT) + gc.set_line_cap(constants.CAP_BUTT) t1 = perf_counter() gc.move_to(10, 10) gc.line_to(sz[0] - 10, sz[1] - 10) diff --git a/kiva/examples/kiva/agg/simple.py b/kiva/examples/kiva/agg/simple.py index d95886a81..630e5c2f5 100644 --- a/kiva/examples/kiva/agg/simple.py +++ b/kiva/examples/kiva/agg/simple.py @@ -24,8 +24,8 @@ print(gc.bmp_array[:7, :7, 0]) gc.clear() -gc.set_line_cap(constants.CAP_SQUARE) -gc.set_line_join(constants.JOIN_MITER) +gc.set_line_cap(constants.LineCap.SQUARE) +gc.set_line_join(constants.LineJoin.MITER) gc.set_fill_color((0, 0, 1)) # gc.rect(0,0,5,5) gc.rect(0.5, 0.5, 5.0, 5.0) diff --git a/kiva/examples/kiva/agg/star1.py b/kiva/examples/kiva/agg/star1.py index f64bf6af6..1e2ee42b1 100644 --- a/kiva/examples/kiva/agg/star1.py +++ b/kiva/examples/kiva/agg/star1.py @@ -7,7 +7,7 @@ # is also available online at http://www.enthought.com/licenses/BSD.txt # # Thanks for using Enthought open source! -import kiva +from kiva import constants from kiva import agg @@ -35,6 +35,6 @@ def add_star(gc): gc.translate_ctm(0, -100) add_star(gc) gc.set_fill_color((0.0, 0.0, 1.0)) -gc.draw_path(kiva.EOF_FILL_STROKE) +gc.draw_path(constants.DrawMode.EOF_FILL_STROKE) gc.save("star1.bmp") diff --git a/kiva/examples/kiva/compiled_path.py b/kiva/examples/kiva/compiled_path.py index ce8b137a5..500f5ed29 100644 --- a/kiva/examples/kiva/compiled_path.py +++ b/kiva/examples/kiva/compiled_path.py @@ -18,7 +18,7 @@ from enable.api import ConstraintsContainer from enable.examples._example_support import DemoFrame, demo_main from enable.primitives.image import Image -from kiva.api import STROKE +from kiva.api import DrawMode from kiva.image import GraphicsContext, CompiledPath @@ -49,7 +49,7 @@ def compiled_path(): gc = GraphicsContext((300, 600)) gc.set_stroke_color((0, 0, 1, 1)) - gc.draw_path_at_points(locs, path, STROKE) + gc.draw_path_at_points(locs, path, DrawMode.STROKE) with tempfile.NamedTemporaryFile(suffix=".jpg") as fid: gc.save(fid.name) diff --git a/kiva/examples/kiva/dash.py b/kiva/examples/kiva/dash.py index bed736413..d4f00e94b 100644 --- a/kiva/examples/kiva/dash.py +++ b/kiva/examples/kiva/dash.py @@ -15,7 +15,7 @@ from enable.api import ConstraintsContainer from enable.examples._example_support import DemoFrame, demo_main from enable.primitives.image import Image -from kiva import constants +from kiva.constants import LineCap from kiva.agg import GraphicsContextArray @@ -30,7 +30,7 @@ def dash(sz=(1000, 1000)): phase = width * 2.5 pattern = width * numpy.array((5, 5)) gc.set_line_dash(pattern, phase) - gc.set_line_cap(constants.CAP_BUTT) + gc.set_line_cap(LineCap.BUTT) t1 = perf_counter() gc.move_to(10, 10) gc.line_to(sz[0] - 10, sz[1] - 10) diff --git a/kiva/examples/kiva/gradient.py b/kiva/examples/kiva/gradient.py index b31fe3d21..6a485d99b 100644 --- a/kiva/examples/kiva/gradient.py +++ b/kiva/examples/kiva/gradient.py @@ -14,7 +14,7 @@ from enable.api import ConstraintsContainer from enable.examples._example_support import DemoFrame, demo_main from enable.primitives.image import Image -from kiva import constants +from kiva.constants import DrawMode from kiva.image import GraphicsContext @@ -31,7 +31,7 @@ def draw(gc): gc.linear_gradient( 50, 25, 150, 125, array([starting_color, ending_color]), "pad" ) - gc.draw_path(constants.FILL) + gc.draw_path(DrawMode.FILL) # vertical top to bottom with gc: @@ -39,14 +39,14 @@ def draw(gc): gc.linear_gradient( 0, 200, 0, 150, array([starting_color, ending_color]), "pad" ) - gc.draw_path(constants.FILL) + gc.draw_path(DrawMode.FILL) # horizontal left to right with gc: gc.rect(50, 200, 150, 50) gc.linear_gradient( 50, 0, 150, 0, array([starting_color, ending_color]), "pad" ) - gc.draw_path(constants.FILL) + gc.draw_path(DrawMode.FILL) # vertical bottom to top with gc: @@ -54,14 +54,14 @@ def draw(gc): gc.linear_gradient( 0, 275, 0, 325, array([starting_color, ending_color]), "pad" ) - gc.draw_path(constants.FILL) + gc.draw_path(DrawMode.FILL) # horizontal right to left with gc: gc.rect(50, 325, 150, 50) gc.linear_gradient( 200, 0, 100, 0, array([starting_color, ending_color]), "pad" ) - gc.draw_path(constants.FILL) + gc.draw_path(DrawMode.FILL) # radial with gc: @@ -69,7 +69,7 @@ def draw(gc): gc.radial_gradient( 325, 75, 50, 325, 75, array([starting_color, ending_color]), "pad" ) - gc.draw_path(constants.FILL) + gc.draw_path(DrawMode.FILL) # radial with focal point in upper left with gc: @@ -79,7 +79,7 @@ def draw(gc): array([starting_color, ending_color]), "pad", ) - gc.draw_path(constants.FILL) + gc.draw_path(DrawMode.FILL) # radial with focal point in bottom right with gc: @@ -89,7 +89,7 @@ def draw(gc): array([starting_color, ending_color]), "pad", ) - gc.draw_path(constants.FILL) + gc.draw_path(DrawMode.FILL) def gradient(): diff --git a/kiva/examples/kiva/simple.py b/kiva/examples/kiva/simple.py index ace0e225d..1910bef85 100644 --- a/kiva/examples/kiva/simple.py +++ b/kiva/examples/kiva/simple.py @@ -12,7 +12,7 @@ from enable.api import ConstraintsContainer from enable.examples._example_support import DemoFrame, demo_main from enable.primitives.image import Image -from kiva import constants +from kiva.constants import LineCap, LineJoin from kiva.image import GraphicsContext @@ -20,8 +20,8 @@ def simple(): gc = GraphicsContext((100, 100)) gc.clear() - gc.set_line_cap(constants.CAP_SQUARE) - gc.set_line_join(constants.JOIN_MITER) + gc.set_line_cap(LineCap.SQUARE) + gc.set_line_join(LineJoin.MITER) gc.set_stroke_color((1, 0, 0)) gc.set_fill_color((0, 0, 1)) gc.rect(0, 0, 30, 30) diff --git a/kiva/examples/kiva/star1.py b/kiva/examples/kiva/star1.py index 909b8ae04..dcd5de469 100644 --- a/kiva/examples/kiva/star1.py +++ b/kiva/examples/kiva/star1.py @@ -15,7 +15,7 @@ from enable.api import ConstraintsContainer, Component, ComponentEditor from enable.examples._example_support import demo_main, DemoFrame from enable.primitives.image import Image -from kiva import constants +from kiva.constants import DrawMode from kiva.image import GraphicsContext @@ -44,7 +44,7 @@ def stars(): gc.translate_ctm(0, -100) add_star(gc) gc.set_fill_color((0.0, 0.0, 1.0)) - gc.draw_path(constants.EOF_FILL_STROKE) + gc.draw_path(DrawMode.EOF_FILL_STROKE) with tempfile.NamedTemporaryFile(suffix=".bmp") as fid: gc.save(fid.name) image = Image.from_file( diff --git a/kiva/fonttools/font.py b/kiva/fonttools/font.py index abaf2efba..ddb5e1756 100644 --- a/kiva/fonttools/font.py +++ b/kiva/fonttools/font.py @@ -14,44 +14,41 @@ import warnings from kiva.constants import ( - BOLD, DECORATIVE, DEFAULT, ITALIC, MODERN, NORMAL, ROMAN, SCRIPT, SWISS, - TELETYPE, WEIGHT_BOLD, WEIGHT_EXTRABOLD, WEIGHT_EXTRAHEAVY, - WEIGHT_EXTRALIGHT, WEIGHT_HEAVY, WEIGHT_LIGHT, WEIGHT_MEDIUM, - WEIGHT_NORMAL, WEIGHT_SEMIBOLD, WEIGHT_THIN, bold_styles, italic_styles, + FontFamily, FontWeight, FontStyle, bold_styles, italic_styles, ) from kiva.fonttools._query import FontQuery from kiva.fonttools.font_manager import default_font_manager FAMILIES = { - 'default': DEFAULT, - 'cursive': SCRIPT, - 'decorative': DECORATIVE, - 'fantasy': DECORATIVE, - 'modern': MODERN, - 'monospace': MODERN, - 'roman': ROMAN, - 'sans-serif': SWISS, - 'script': SCRIPT, - 'serif': ROMAN, - 'swiss': SWISS, - 'teletype': TELETYPE, - 'typewriter': TELETYPE, + 'default': FontFamily.DEFAULT, + 'cursive': FontFamily.SCRIPT, + 'decorative': FontFamily.DECORATIVE, + 'fantasy': FontFamily.DECORATIVE, + 'modern': FontFamily.MODERN, + 'monospace': FontFamily.MODERN, + 'roman': FontFamily.ROMAN, + 'sans-serif': FontFamily.SWISS, + 'script': FontFamily.SCRIPT, + 'serif': FontFamily.ROMAN, + 'swiss': FontFamily.SWISS, + 'teletype': FontFamily.TELETYPE, + 'typewriter': FontFamily.TELETYPE, } WEIGHTS = { - 'thin': WEIGHT_THIN, - 'extra-light': WEIGHT_EXTRALIGHT, - 'light': WEIGHT_LIGHT, - 'regular': WEIGHT_NORMAL, - 'medium': WEIGHT_MEDIUM, - 'semi-bold': WEIGHT_SEMIBOLD, - 'bold': WEIGHT_BOLD, - 'extra-bold': WEIGHT_EXTRABOLD, - 'heavy': WEIGHT_HEAVY, - 'extra-heavy': WEIGHT_EXTRAHEAVY + 'thin': FontWeight.THIN, + 'extra-light': FontWeight.EXTRALIGHT, + 'light': FontWeight.LIGHT, + 'regular': FontWeight.NORMAL, + 'medium': FontWeight.MEDIUM, + 'semi-bold': FontWeight.SEMIBOLD, + 'bold': FontWeight.BOLD, + 'extra-bold': FontWeight.EXTRABOLD, + 'heavy': FontWeight.HEAVY, + 'extra-heavy': FontWeight.EXTRAHEAVY } STYLES = { - 'italic': ITALIC, - 'oblique': ITALIC, + 'italic': FontStyle.ITALIC, + 'oblique': FontStyle.ITALIC, } DECORATIONS = {'underline'} NOISE = {'pt', 'point', 'px', 'family'} @@ -117,10 +114,10 @@ def simple_parser(description): CSS font definition. """ face = [] - family = DEFAULT + family = FontFamily.DEFAULT size = None - weight = WEIGHT_NORMAL - style = NORMAL + weight = FontWeight.NORMAL + style = FontStyle.NORMAL underline = False for word in description.split(): lower_word = word.casefold() @@ -181,18 +178,18 @@ class Font(object): # Maps the constants for font families to names to use when searching for # fonts. familymap = { - DEFAULT: "serif", - SWISS: "sans-serif", - ROMAN: "serif", - MODERN: "sans-serif", - DECORATIVE: "fantasy", - SCRIPT: "cursive", - TELETYPE: "monospace", + FontFamily.DEFAULT: "serif", + FontFamily.SWISS: "sans-serif", + FontFamily.ROMAN: "serif", + FontFamily.MODERN: "sans-serif", + FontFamily.DECORATIVE: "fantasy", + FontFamily.SCRIPT: "cursive", + FontFamily.TELETYPE: "monospace", } - def __init__(self, face_name="", size=12, family=SWISS, - weight=WEIGHT_NORMAL, style=NORMAL, underline=0, - encoding=DEFAULT): + def __init__(self, face_name="", size=12, family=FontFamily.SWISS, + weight=FontWeight.NORMAL, style=FontStyle.NORMAL, underline=0, + encoding=FontFamily.DEFAULT): if not isinstance(face_name, str): raise RuntimeError( f"Expected face name to be a str, got {face_name!r}") @@ -225,7 +222,7 @@ def __init__(self, face_name="", size=12, family=SWISS, # correct the style and weight if needed (can be removed in Enable 7) self.weight = self._get_weight() - self.style = style & ~BOLD + self.style = style & ~FontStyle.BOLD def findfont(self, language=None): """ Returns the file name and face index of the font that most closely @@ -269,7 +266,7 @@ def is_bold(self): than medium. """ weight = self._get_weight() - return (weight > WEIGHT_MEDIUM) + return (weight > FontWeight.MEDIUM) def _make_font_query(self): """ Returns a FontQuery object that encapsulates our font properties. @@ -296,21 +293,21 @@ def _get_weight(self): Note: this is a temporary method that will be removed in Enable 7. """ - if self.weight == BOLD: + if self.weight == FontStyle.BOLD: warnings.warn( - "Use WEIGHT_BOLD instead of BOLD for Font.weight", + "Use FontWeight.BOLD instead of FontStyle.BOLD for Font.weight", DeprecationWarning ) - return WEIGHT_BOLD + return FontWeight.BOLD elif self.style in bold_styles: warnings.warn( - "Set Font.weight to WEIGHT_BOLD instead of Font.style to " + "Set Font.weight to FontWeight.BOLD instead of Font.style to " "BOLD or BOLD_STYLE", DeprecationWarning ) # if weight is default, and style is bold, report as bold - if self.weight == WEIGHT_NORMAL: - return WEIGHT_BOLD + if self.weight == FontWeight.NORMAL: + return FontWeight.BOLD return self.weight diff --git a/kiva/fonttools/tests/test_font.py b/kiva/fonttools/tests/test_font.py index 21d424b6c..0c81c6dab 100644 --- a/kiva/fonttools/tests/test_font.py +++ b/kiva/fonttools/tests/test_font.py @@ -14,8 +14,8 @@ import unittest from kiva.constants import ( - BOLD, BOLD_ITALIC, DECORATIVE, DEFAULT, ITALIC, MODERN, NORMAL, ROMAN, - SCRIPT, TELETYPE, WEIGHT_BOLD, WEIGHT_LIGHT, WEIGHT_NORMAL, SWISS, + BOLD_ITALIC, FontFamily, FontStyle, FontWeight, DECORATIVE, DEFAULT, + MODERN, ROMAN, SCRIPT, TELETYPE, SWISS, ) from kiva.fonttools._constants import font_family_aliases, preferred_fonts from kiva.fonttools.tests._testing import patch_global_font_manager @@ -78,21 +78,21 @@ def test_find_font_for_language(self): def test_str_to_font(self): # Simple from_str = str_to_font("modern 10") - from_ctor = Font(family=MODERN, size=10) + from_ctor = Font(family=FontFamily.MODERN, size=10) self.assertEqual(from_ctor, from_str) # Some complexity from_str = str_to_font("roman bold italic 12") - from_ctor = Font(family=ROMAN, weight=WEIGHT_BOLD, style=ITALIC, size=12) + from_ctor = Font(family=FontFamily.ROMAN, weight=FontWeight.BOLD, style=FontStyle.ITALIC, size=12) self.assertEqual(from_ctor, from_str) # Lots of complexity from_str = str_to_font("Times roman bold italic underline 72") from_ctor = Font( "Times", - family=ROMAN, - weight=WEIGHT_BOLD, - style=ITALIC, + family=FontFamily.ROMAN, + weight=FontWeight.BOLD, + style=FontStyle.ITALIC, size=72, underline=1, ) @@ -102,9 +102,9 @@ def test_str_to_font(self): from_str = str_to_font("Times roman light italic underline 72") from_ctor = Font( "Times", - family=ROMAN, - weight=WEIGHT_LIGHT, - style=ITALIC, + family=FontFamily.ROMAN, + weight=FontWeight.LIGHT, + style=FontStyle.ITALIC, size=72, underline=1, ) @@ -127,46 +127,46 @@ def test_is_bold_true(self): def test_weight_warnings(self): # Don't use BOLD as a weight with self.assertWarns(DeprecationWarning): - font = Font(weight=BOLD) - self.assertEqual(font.weight, WEIGHT_BOLD) + font = Font(weight=FontStyle.BOLD) + self.assertEqual(font.weight, FontWeight.BOLD) # Don't use BOLD as a style with self.assertWarns(DeprecationWarning): - font = Font(style=BOLD) - self.assertEqual(font.weight, WEIGHT_BOLD) - self.assertEqual(font.style, NORMAL) + font = Font(style=FontStyle.BOLD) + self.assertEqual(font.weight, FontWeight.BOLD) + self.assertEqual(font.style, FontStyle.NORMAL) # Don't use BOLD_ITALIC as a style with self.assertWarns(DeprecationWarning): font = Font(style=BOLD_ITALIC) - self.assertEqual(font.weight, WEIGHT_BOLD) - self.assertEqual(font.style, ITALIC) + self.assertEqual(font.weight, FontWeight.BOLD) + self.assertEqual(font.style, FontStyle.ITALIC) # Ignore BOLD style if weight is not normal with self.assertWarns(DeprecationWarning): - font = Font(style=BOLD, weight=WEIGHT_LIGHT) - self.assertEqual(font.weight, WEIGHT_LIGHT) - self.assertEqual(font.style, NORMAL) + font = Font(style=FontStyle.BOLD, weight=FontWeight.LIGHT) + self.assertEqual(font.weight, FontWeight.LIGHT) + self.assertEqual(font.style, FontStyle.NORMAL) with self.assertWarns(DeprecationWarning): - font = Font(style=BOLD_ITALIC, weight=WEIGHT_LIGHT) - self.assertEqual(font.weight, WEIGHT_LIGHT) - self.assertEqual(font.style, ITALIC) + font = Font(style=BOLD_ITALIC, weight=FontWeight.LIGHT) + self.assertEqual(font.weight, FontWeight.LIGHT) + self.assertEqual(font.style, FontStyle.ITALIC) def test_font_query_warnings(self): # Don't use BOLD as a weight font = Font() - font.weight = BOLD + font.weight = FontStyle.BOLD with self.assertWarns(DeprecationWarning): query = font._make_font_query() - self.assertEqual(query.get_weight(), WEIGHT_BOLD) + self.assertEqual(query.get_weight(), FontWeight.BOLD) # Don't use BOLD as a style font = Font() - font.style = BOLD + font.style = FontStyle.BOLD with self.assertWarns(DeprecationWarning): query = font._make_font_query() - self.assertEqual(query.get_weight(), WEIGHT_BOLD) + self.assertEqual(query.get_weight(), FontWeight.BOLD) self.assertEqual(query.get_style(), "normal") # Don't use BOLD_ITALIC as a style @@ -174,24 +174,24 @@ def test_font_query_warnings(self): font.style = BOLD_ITALIC with self.assertWarns(DeprecationWarning): query = font._make_font_query() - self.assertEqual(query.get_weight(), WEIGHT_BOLD) + self.assertEqual(query.get_weight(), FontWeight.BOLD) self.assertEqual(query.get_style(), "italic") # Ignore BOLD style if weight is not normal font = Font() - font.weight = WEIGHT_LIGHT - font.style = BOLD + font.weight = FontWeight.LIGHT + font.style = FontStyle.BOLD with self.assertWarns(DeprecationWarning): query = font._make_font_query() - self.assertEqual(query.get_weight(), WEIGHT_LIGHT) + self.assertEqual(query.get_weight(), FontWeight.LIGHT) self.assertEqual(query.get_style(), "normal") font = Font() - font.weight = WEIGHT_LIGHT + font.weight = FontWeight.LIGHT font.style = BOLD_ITALIC with self.assertWarns(DeprecationWarning): query = font._make_font_query() - self.assertEqual(query.get_weight(), WEIGHT_LIGHT) + self.assertEqual(query.get_weight(), FontWeight.LIGHT) self.assertEqual(query.get_style(), "italic") def test_family_queries(self): @@ -220,10 +220,10 @@ def test_empty(self): properties, { 'face_name': "", - 'family': DEFAULT, + 'family': FontFamily.DEFAULT, 'size': 10, - 'weight': WEIGHT_NORMAL, - 'style': NORMAL, + 'weight': FontWeight.NORMAL, + 'style': FontStyle.NORMAL, 'underline': False, }, ) @@ -235,10 +235,10 @@ def test_typical(self): properties, { 'face_name': "Helvetica", - 'family': SWISS, + 'family': FontFamily.SWISS, 'size': 10, - 'weight': WEIGHT_BOLD, - 'style': ITALIC, + 'weight': FontWeight.BOLD, + 'style': FontStyle.ITALIC, 'underline': True, }, ) @@ -251,10 +251,10 @@ def test_noise(self): properties, { 'face_name': "", - 'family': DEFAULT, + 'family': FontFamily.DEFAULT, 'size': 10, - 'weight': WEIGHT_NORMAL, - 'style': NORMAL, + 'weight': FontWeight.NORMAL, + 'style': FontStyle.NORMAL, 'underline': False, }, ) @@ -269,8 +269,8 @@ def test_generic_families(self): 'face_name': "", 'family': constant, 'size': 10, - 'weight': WEIGHT_NORMAL, - 'style': NORMAL, + 'weight': FontWeight.NORMAL, + 'style': FontStyle.NORMAL, 'underline': False, }, ) @@ -283,10 +283,10 @@ def test_size(self): properties, { 'face_name': "", - 'family': DEFAULT, + 'family': FontFamily.DEFAULT, 'size': size, - 'weight': WEIGHT_NORMAL, - 'style': NORMAL, + 'weight': FontWeight.NORMAL, + 'style': FontStyle.NORMAL, 'underline': False, }, ) @@ -299,10 +299,10 @@ def test_weight(self): properties, { 'face_name': "", - 'family': DEFAULT, + 'family': FontFamily.DEFAULT, 'size': 10, 'weight': constant, - 'style': NORMAL, + 'style': FontStyle.NORMAL, 'underline': False, }, ) @@ -315,9 +315,9 @@ def test_style(self): properties, { 'face_name': "", - 'family': DEFAULT, + 'family': FontFamily.DEFAULT, 'size': 10, - 'weight': WEIGHT_NORMAL, + 'weight': FontWeight.NORMAL, 'style': constant, 'underline': False, }, @@ -336,10 +336,10 @@ def test_decorations(self): properties, { 'face_name': "", - 'family': DEFAULT, + 'family': FontFamily.DEFAULT, 'size': 10, - 'weight': WEIGHT_NORMAL, - 'style': NORMAL, + 'weight': FontWeight.NORMAL, + 'style': FontStyle.NORMAL, 'underline': 'underline' in decorations, }, ) diff --git a/kiva/graphics_state.py b/kiva/graphics_state.py index ac05aedd4..1df97b2aa 100644 --- a/kiva/graphics_state.py +++ b/kiva/graphics_state.py @@ -17,7 +17,7 @@ from numpy import array, float64 -from .constants import CAP_ROUND, JOIN_MITER, TEXT_FILL +from .constants import LineCap, LineJoin, TextMode from .fonttools import Font from .line_state import LineState import kiva.affine as affine @@ -98,8 +98,8 @@ def __init__(self): # Line state default values. line_color = array((0.0, 0.0, 0.0, 1.0)) line_width = 1 - line_cap = CAP_ROUND - line_join = JOIN_MITER + line_cap = LineCap.ROUND + line_join = LineJoin.MITER line_dash = (0, array([0])) # This will draw a solid line # FIXME: This is a very wierd class. The following code is here to @@ -123,7 +123,7 @@ def __init__(self): self.miter_limit = 1.0 self.flatness = 1.0 self.character_spacing = 0.0 - self.text_drawing_mode = TEXT_FILL + self.text_drawing_mode = TextMode.FILL self.alpha = 1.0 def copy(self): diff --git a/kiva/pdf.py b/kiva/pdf.py index 96c9b7bd6..92029c110 100644 --- a/kiva/pdf.py +++ b/kiva/pdf.py @@ -34,21 +34,20 @@ from .arc_conversion import arc_to_tangent_points from .basecore2d import GraphicsContextBase from .line_state import is_dashed -from .constants import FILL, STROKE, EOF_FILL -import kiva.constants as constants +from kiva import constants import kiva.affine as affine from kiva.fonttools.font import Font cap_style = {} -cap_style[constants.CAP_ROUND] = 1 -cap_style[constants.CAP_SQUARE] = 2 -cap_style[constants.CAP_BUTT] = 0 +cap_style[constants.LineCap.ROUND] = 1 +cap_style[constants.LineCap.SQUARE] = 2 +cap_style[constants.LineCap.BUTT] = 0 join_style = {} -join_style[constants.JOIN_ROUND] = 1 -join_style[constants.JOIN_BEVEL] = 2 -join_style[constants.JOIN_MITER] = 0 +join_style[constants.LineJoin.ROUND] = 1 +join_style[constants.LineJoin.BEVEL] = 2 +join_style[constants.LineJoin.MITER] = 0 font_styles = {} font_styles["regular"] = "" @@ -58,11 +57,11 @@ # stroke, fill, mode path_mode = {} -path_mode[constants.FILL_STROKE] = (1, 1, canvas.FILL_NON_ZERO) -path_mode[constants.FILL] = (0, 1, canvas.FILL_NON_ZERO) -path_mode[constants.EOF_FILL] = (0, 1, canvas.FILL_EVEN_ODD) -path_mode[constants.STROKE] = (1, 0, canvas.FILL_NON_ZERO) -path_mode[constants.EOF_FILL_STROKE] = (1, 1, canvas.FILL_EVEN_ODD) +path_mode[constants.DrawMode.FILL_STROKE] = (1, 1, canvas.FILL_NON_ZERO) +path_mode[constants.DrawMode.FILL] = (0, 1, canvas.FILL_NON_ZERO) +path_mode[constants.DrawMode.EOF_FILL] = (0, 1, canvas.FILL_EVEN_ODD) +path_mode[constants.DrawMode.STROKE] = (1, 0, canvas.FILL_NON_ZERO) +path_mode[constants.DrawMode.EOF_FILL_STROKE] = (1, 1, canvas.FILL_EVEN_ODD) # fixme: I believe this can be implemented but for now, it is not. @@ -371,7 +370,7 @@ def rect(self, *args): self.current_pdf_path.rect(*args) self.current_point = (args[0], args[1]) - def draw_rect(self, rect, mode=constants.FILL_STROKE): + def draw_rect(self, rect, mode: constants.DrawMode = constants.DrawMode.FILL_STROKE): self.rect(rect) self.draw_path(mode) self.current_point = (rect[0], rect[1]) @@ -719,17 +718,17 @@ def clear(self): def stroke_path(self): """ """ - self.draw_path(mode=STROKE) + self.draw_path(mode=constants.DrawMode.STROKE) def fill_path(self): """ """ - self.draw_path(mode=FILL) + self.draw_path(mode=constants.DrawMode.FILL) def eof_fill_path(self): """ """ - self.draw_path(mode=EOF_FILL) + self.draw_path(mode=constants.DrawMode.EOF_FILL) def stroke_rect(self, rect): """ @@ -763,7 +762,7 @@ def clear_rect(self, rect): msg = "clear_rect not implemented on PDF yet." raise NotImplementedError(msg) - def draw_path(self, mode=constants.FILL_STROKE): + def draw_path(self, mode: constants.DrawMode = constants.DrawMode.FILL_STROKE): """ Walks through all the drawing subpaths and draw each element. Each subpath is drawn separately. diff --git a/kiva/ps.py b/kiva/ps.py index fb7b3ccfd..a4bcf3633 100644 --- a/kiva/ps.py +++ b/kiva/ps.py @@ -25,11 +25,7 @@ # Local, relative Kiva imports from . import affine from . import basecore2d -from . import constants -from .constants import ( - CONCAT_CTM, EOF_FILL, EOF_FILL_STROKE, FILL, FILL_STROKE, LOAD_CTM, - ROTATE_CTM, SCALE_CTM, STROKE, TRANSLATE_CTM, -) +from .constants import LineCap, CTM, DrawMode, LineJoin # This backend does not have compiled paths, yet. CompiledPath = None @@ -60,15 +56,15 @@ def default_filter(kw1): line_cap_map = { - constants.CAP_BUTT: 0, - constants.CAP_ROUND: 1, - constants.CAP_SQUARE: 2, + LineCap.BUTT: 0, + LineCap.ROUND: 1, + LineCap.SQUARE: 2, } line_join_map = { - constants.JOIN_MITER: 0, - constants.JOIN_ROUND: 1, - constants.JOIN_BEVEL: 2, + LineJoin.MITER: 0, + LineJoin.ROUND: 1, + LineJoin.BEVEL: 2, } font_map = {"Arial": "Helvetica"} @@ -90,11 +86,11 @@ def default_filter(kw1): _clip_counter = 0 fill_stroke_map = { - FILL_STROKE: ("fill", "stroke"), - EOF_FILL_STROKE: ("eofill", "stroke"), - FILL: ("fill", None), - STROKE: ("stroke", None), - EOF_FILL: ("eofill", None), + DrawMode.FILL_STROKE: ("fill", "stroke"), + DrawMode.EOF_FILL_STROKE: ("eofill", "stroke"), + DrawMode.FILL: ("fill", None), + DrawMode.STROKE: ("stroke", None), + DrawMode.EOF_FILL: ("eofill", None), } @@ -152,7 +148,7 @@ def device_show_text(self, text): "%3.3f %3.3f %3.3f %3.3f rectclip\n" % self.state.clipping_path ) self.contents.write("gsave\n") - self.device_transform_device_ctm(LOAD_CTM, [m]) + self.device_transform_device_ctm(CTM.LOAD, [m]) self.contents.write("%3.3f %3.3f moveto\n" % (0, 0)) r, g, b, a = self.state.line_color self.contents.write("%1.3f %1.3f %1.3f setrgbcolor\n" % (r, g, b)) @@ -237,20 +233,20 @@ def device_draw_image(self, img, rect): self.contents.write("grestore\n") def device_transform_device_ctm(self, func, args): - if func == LOAD_CTM: + if func == CTM.LOAD: self.contents.write("initmatrix\n") - func = CONCAT_CTM + func = CTM.CONCAT - if func == SCALE_CTM: + if func == CTM.SCALE: sx, sy = args self.contents.write("%.3f %.3f scale\n" % (sx, sy)) - elif func == ROTATE_CTM: + elif func == CTM.ROTATE: r, = args self.contents.write("%.3f rotate\n" % r) - elif func == TRANSLATE_CTM: + elif func == CTM.TRANSLATE: tx, ty = args self.contents.write("%.3f %.3f translate\n" % (tx, ty)) - elif func == CONCAT_CTM: + elif func == CTM.CONCAT: m, = args self.contents.write( "[%.3f %.3f %.3f %.3f %.3f %.3f] concat\n" diff --git a/kiva/qpainter.py b/kiva/qpainter.py index f4a0323c4..287811b86 100644 --- a/kiva/qpainter.py +++ b/kiva/qpainter.py @@ -20,27 +20,27 @@ from .abstract_graphics_context import AbstractGraphicsContext from .arc_conversion import arc_to_tangent_points from .fonttools import Font -import kiva.constants as constants +from kiva import constants # These are the symbols that a backend has to define. __all__ = ["CompiledPath", "Font", "font_metrics_provider", "GraphicsContext"] cap_style = {} -cap_style[constants.CAP_ROUND] = QtCore.Qt.RoundCap -cap_style[constants.CAP_SQUARE] = QtCore.Qt.SquareCap -cap_style[constants.CAP_BUTT] = QtCore.Qt.FlatCap +cap_style[constants.LineCap.ROUND] = QtCore.Qt.RoundCap +cap_style[constants.LineCap.SQUARE] = QtCore.Qt.SquareCap +cap_style[constants.LineCap.BUTT] = QtCore.Qt.FlatCap join_style = {} -join_style[constants.JOIN_ROUND] = QtCore.Qt.RoundJoin -join_style[constants.JOIN_BEVEL] = QtCore.Qt.BevelJoin -join_style[constants.JOIN_MITER] = QtCore.Qt.MiterJoin +join_style[constants.LineJoin.ROUND] = QtCore.Qt.RoundJoin +join_style[constants.LineJoin.BEVEL] = QtCore.Qt.BevelJoin +join_style[constants.LineJoin.MITER] = QtCore.Qt.MiterJoin draw_modes = {} -draw_modes[constants.FILL] = QtCore.Qt.OddEvenFill -draw_modes[constants.EOF_FILL] = QtCore.Qt.WindingFill -draw_modes[constants.STROKE] = 0 -draw_modes[constants.FILL_STROKE] = QtCore.Qt.OddEvenFill -draw_modes[constants.EOF_FILL_STROKE] = QtCore.Qt.WindingFill +draw_modes[constants.DrawMode.FILL] = QtCore.Qt.OddEvenFill +draw_modes[constants.DrawMode.EOF_FILL] = QtCore.Qt.WindingFill +draw_modes[constants.DrawMode.STROKE] = 0 +draw_modes[constants.DrawMode.FILL_STROKE] = QtCore.Qt.OddEvenFill +draw_modes[constants.DrawMode.EOF_FILL_STROKE] = QtCore.Qt.WindingFill font_family_to_style_hint = { constants.DEFAULT: QtGui.QFont.StyleHint.AnyStyle, @@ -53,29 +53,31 @@ } font_styles = {} -font_styles["regular"] = constants.NORMAL -font_styles["bold"] = constants.NORMAL -font_styles["italic"] = constants.ITALIC -font_styles["bold italic"] = constants.ITALIC +font_styles["regular"] = constants.FontStyle.NORMAL +font_styles["bold"] = constants.FontStyle.NORMAL +font_styles["italic"] = constants.FontStyle.ITALIC +font_styles["bold italic"] = constants.FontStyle.ITALIC font_weights = { - "regular": constants.WEIGHT_NORMAL, - "bold": constants.WEIGHT_BOLD, - "italic": constants.WEIGHT_NORMAL, - "bold italic": constants.WEIGHT_BOLD, + "regular": constants.FontWeight.NORMAL, + "bold": constants.FontWeight.BOLD, + "italic": constants.FontWeight.NORMAL, + "bold italic": constants.FontWeight.BOLD, } weight_to_qt_weight = { - constants.WEIGHT_THIN: QtGui.QFont.Weight.Thin, - constants.WEIGHT_EXTRALIGHT: QtGui.QFont.Weight.ExtraLight, - constants.WEIGHT_LIGHT: QtGui.QFont.Weight.Light, - constants.WEIGHT_NORMAL: QtGui.QFont.Weight.Normal, - constants.WEIGHT_MEDIUM: QtGui.QFont.Weight.Medium, - constants.WEIGHT_SEMIBOLD: QtGui.QFont.Weight.DemiBold, - constants.WEIGHT_BOLD: QtGui.QFont.Weight.Bold, - constants.WEIGHT_EXTRABOLD: QtGui.QFont.Weight.ExtraBold, - constants.WEIGHT_HEAVY: QtGui.QFont.Weight.Black, - constants.WEIGHT_EXTRAHEAVY: 99 if is_qt5 else QtGui.QFont.Weight.Black, + constants.FontWeight.THIN: QtGui.QFont.Weight.Thin, + constants.FontWeight.EXTRALIGHT: QtGui.QFont.Weight.ExtraLight, + constants.FontWeight.LIGHT: QtGui.QFont.Weight.Light, + constants.FontWeight.NORMAL: QtGui.QFont.Weight.Normal, + constants.FontWeight.MEDIUM: QtGui.QFont.Weight.Medium, + constants.FontWeight.SEMIBOLD: QtGui.QFont.Weight.DemiBold, + constants.FontWeight.BOLD: QtGui.QFont.Weight.Bold, + constants.FontWeight.EXTRABOLD: QtGui.QFont.Weight.ExtraBold, + constants.FontWeight.HEAVY: QtGui.QFont.Weight.Black, + constants.FontWeight.EXTRAHEAVY: ( + 99 if is_qt5 else QtGui.QFont.Weight.Black + ), } gradient_coord_modes = {} @@ -396,16 +398,16 @@ def rects(self, rects): """ self.path.rects(rects) - def draw_rect(self, rect, mode=constants.FILL_STROKE): + def draw_rect(self, rect, mode=constants.DrawMode.FILL_STROKE): """ Draw a rect. """ rect = QtCore.QRectF(*rect) - if mode == constants.STROKE: + if mode == constants.DrawMode.STROKE: save_brush = self.gc.brush() self.gc.setBrush(QtGui.QBrush(QtCore.Qt.NoBrush)) self.gc.drawRect(rect) self.gc.setBrush(save_brush) - elif mode in [constants.FILL, constants.EOF_FILL]: + elif mode in [constants.DrawMode.FILL, constants.DrawMode.EOF_FILL]: self.gc.fillRect(rect, self.gc.brush()) else: self.gc.fillRect(rect, self.gc.brush()) @@ -651,8 +653,8 @@ def draw_image(self, img, rect=None): def select_font(self, name, size, style="regular", encoding=None): """ Set the font for the current graphics context. """ - style = font_styles.get(style, constants.NORMAL) - weight = font_weights.get(style, constants.WEIGHT_NORMAL) + style = font_styles.get(style, constants.FontStyle.NORMAL) + weight = font_weights.get(style, constants.FontWeight.NORMAL) font = Font(name, size=size, style=style, weight=weight) self.set_font(font) @@ -845,14 +847,14 @@ def clear(self, clear_color=(1.0, 1.0, 1.0, 1.0)): self.gc.setBackground(QtGui.QBrush(QtGui.QColor.fromRgbF(r, g, b, a))) self.gc.eraseRect(QtCore.QRectF(0, 0, self.width(), self.height())) - def draw_path(self, mode=constants.FILL_STROKE): + def draw_path(self, mode=constants.DrawMode.FILL_STROKE): """ Walk through all the drawing subpaths and draw each element. Each subpath is drawn separately. """ - if mode == constants.STROKE: + if mode == constants.DrawMode.STROKE: self.stroke_path() - elif mode in [constants.FILL, constants.EOF_FILL]: + elif mode in [constants.DrawMode.FILL, constants.DrawMode.EOF_FILL]: mode = draw_modes[mode] self.path.path.setFillRule(mode) self.fill_path() @@ -867,11 +869,11 @@ def get_empty_path(self): """ return CompiledPath() - def draw_path_at_points(self, points, path, mode=constants.FILL_STROKE): + def draw_path_at_points(self, points, path, mode=constants.DrawMode.FILL_STROKE): # set up drawing state and function - if mode == constants.STROKE: + if mode == constants.DrawMode.STROKE: draw_func = partial(self.gc.strokePath, path.path, self.gc.pen()) - elif mode in [constants.FILL, constants.EOF_FILL]: + elif mode in [constants.DrawMode.FILL, constants.DrawMode.EOF_FILL]: mode = draw_modes[mode] path.path.setFillRule(mode) draw_func = partial(self.gc.fillPath, path.path, self.gc.brush()) diff --git a/kiva/quartz/ABCGI.pyx b/kiva/quartz/ABCGI.pyx index ffd85fa5b..a980904b2 100644 --- a/kiva/quartz/ABCGI.pyx +++ b/kiva/quartz/ABCGI.pyx @@ -144,33 +144,33 @@ import numpy from kiva import constants cap_style = {} -cap_style[constants.CAP_ROUND] = kCGLineCapRound -cap_style[constants.CAP_SQUARE] = kCGLineCapSquare -cap_style[constants.CAP_BUTT] = kCGLineCapButt +cap_style[constants.LineCap.ROUND] = kCGLineCapRound +cap_style[constants.LineCap.SQUARE] = kCGLineCapSquare +cap_style[constants.LineCap.BUTT] = kCGLineCapButt join_style = {} -join_style[constants.JOIN_ROUND] = kCGLineJoinRound -join_style[constants.JOIN_BEVEL] = kCGLineJoinBevel -join_style[constants.JOIN_MITER] = kCGLineJoinMiter +join_style[constants.LineJoin.ROUND] = kCGLineJoinRound +join_style[constants.LineJoin.BEVEL] = kCGLineJoinBevel +join_style[constants.LineJoin.MITER] = kCGLineJoinMiter draw_modes = {} -draw_modes[constants.FILL] = kCGPathFill -draw_modes[constants.EOF_FILL] = kCGPathEOFill -draw_modes[constants.STROKE] = kCGPathStroke -draw_modes[constants.FILL_STROKE] = kCGPathFillStroke -draw_modes[constants.EOF_FILL_STROKE] = kCGPathEOFillStroke +draw_modes[constants.DrawMode.FILL] = kCGPathFill +draw_modes[constants.DrawMode.EOF_FILL] = kCGPathEOFill +draw_modes[constants.DrawMode.STROKE] = kCGPathStroke +draw_modes[constants.DrawMode.FILL_STROKE] = kCGPathFillStroke +draw_modes[constants.DrawMode.EOF_FILL_STROKE] = kCGPathEOFillStroke text_modes = {} -text_modes[constants.TEXT_FILL] = kCGTextFill -text_modes[constants.TEXT_STROKE] = kCGTextStroke -text_modes[constants.TEXT_FILL_STROKE] = kCGTextFillStroke -text_modes[constants.TEXT_INVISIBLE] = kCGTextInvisible -text_modes[constants.TEXT_FILL_CLIP] = kCGTextFillClip -text_modes[constants.TEXT_STROKE_CLIP] = kCGTextStrokeClip -text_modes[constants.TEXT_FILL_STROKE_CLIP] = kCGTextFillStrokeClip -text_modes[constants.TEXT_CLIP] = kCGTextClip +text_modes[constants.TextMode.FILL] = kCGTextFill +text_modes[constants.TextMode.STROKE] = kCGTextStroke +text_modes[constants.TextMode.FILL_STROKE] = kCGTextFillStroke +text_modes[constants.TextMode.INVISIBLE] = kCGTextInvisible +text_modes[constants.TextMode.FILL_CLIP] = kCGTextFillClip +text_modes[constants.TextMode.STROKE_CLIP] = kCGTextStrokeClip +text_modes[constants.TextMode.FILL_STROKE_CLIP] = kCGTextFillStrokeClip +text_modes[constants.TextMode.CLIP] = kCGTextClip # this last one doesn't exist in Quartz -text_modes[constants.TEXT_OUTLINE] = kCGTextStroke +text_modes[constants.TextMode.OUTLINE] = kCGTextStroke cdef class CGContext cdef class CGContextInABox(CGContext) @@ -804,10 +804,10 @@ cdef class CGContext: """ style = { - constants.NORMAL: 'regular', - constants.BOLD: 'bold', - constants.ITALIC: 'italic', - constants.BOLD_ITALIC: 'bold italic', + constants.FontStyle.NORMAL: 'regular', + constants.FontStyle.BOLD: 'bold', + constants.FontStyle.ITALIC: 'italic', + constants.FontStyle.BOLD | constants.FontStyle.ITALIC: 'bold italic', }[font.is_bold() | font.style] if font.face_name: name = font.face_name @@ -1026,7 +1026,7 @@ cdef class CGContext: """ CGContextClearRect(self.context, CGRectMakeFromPython(rect)) - def draw_path(self, object mode=constants.FILL_STROKE): + def draw_path(self, object mode=constants.DrawMode.FILL_STROKE): """ Walk through all the drawing subpaths and draw each element. Each subpath is drawn separately. @@ -1035,7 +1035,7 @@ cdef class CGContext: cg_mode = draw_modes[mode] CGContextDrawPath(self.context, cg_mode) - def draw_rect(self, rect, object mode=constants.FILL_STROKE): + def draw_rect(self, rect, object mode=constants.DrawMode.FILL_STROKE): """ Draw a rectangle with the given mode. """ @@ -1053,7 +1053,7 @@ cdef class CGContext: return CGMutablePath() def draw_path_at_points(self, points, CGMutablePath marker not None, - object mode=constants.FILL_STROKE): + object mode=constants.DrawMode.FILL_STROKE): cdef int i cdef int n @@ -2422,27 +2422,27 @@ cdef class _Markers: path : CGMutablePath """ - if marker_type == constants.NO_MARKER: + if marker_type == constants.Marker.NO: return CGMutablePath() - elif marker_type == constants.SQUARE_MARKER: + elif marker_type == constants.Marker.SQUARE: return self.square(size) - elif marker_type == constants.DIAMOND_MARKER: + elif marker_type == constants.Marker.DIAMOND: return self.diamond(size) - elif marker_type == constants.CIRCLE_MARKER: + elif marker_type == constants.Marker.CIRCLE: return self.circle(size) - elif marker_type == constants.CROSSED_CIRCLE_MARKER: + elif marker_type == constants.Marker.CROSSED_CIRCLE: raise NotImplementedError - elif marker_type == constants.CROSS_MARKER: + elif marker_type == constants.Marker.CROSS: return self.cross(size) - elif marker_type == constants.TRIANGLE_MARKER: + elif marker_type == constants.Marker.TRIANGLE: raise NotImplementedError - elif marker_type == constants.INVERTED_TRIANGLE_MARKER: + elif marker_type == constants.Marker.INVERTED_TRIANGLE: raise NotImplementedError - elif marker_type == constants.PLUS_MARKER: + elif marker_type == constants.Marker.PLUS: raise NotImplementedError - elif marker_type == constants.DOT_MARKER: + elif marker_type == constants.Marker.DOT: raise NotImplementedError - elif marker_type == constants.PIXEL_MARKER: + elif marker_type == constants.Marker.PIXEL: raise NotImplementedError diff --git a/kiva/svg.py b/kiva/svg.py index a3071cfde..0f7702b47 100644 --- a/kiva/svg.py +++ b/kiva/svg.py @@ -42,7 +42,6 @@ from . import affine from . import basecore2d from . import constants -from .constants import FILL, FILL_STROKE, EOF_FILL_STROKE, EOF_FILL, STROKE def _strpoints(points): @@ -68,15 +67,15 @@ def default_filter(kw1): line_cap_map = { - constants.CAP_ROUND: "round", - constants.CAP_SQUARE: "square", - constants.CAP_BUTT: "butt", + constants.LineCap.ROUND: "round", + constants.LineCap.SQUARE: "square", + constants.LineCap.BUTT: "butt", } line_join_map = { - constants.JOIN_ROUND: "round", - constants.JOIN_BEVEL: "bevel", - constants.JOIN_MITER: "miter", + constants.LineJoin.ROUND: "round", + constants.LineJoin.BEVEL: "bevel", + constants.LineJoin.MITER: "miter", } font_style_map = { constants.NORMAL: "normal", @@ -307,15 +306,15 @@ def device_draw_image(self, img, rect): def device_fill_points(self, points, mode): points = self._fixpoints(points) - if mode in (FILL, FILL_STROKE, EOF_FILL_STROKE): + if mode in (constants.DrawMode.FILL, constants.DrawMode.FILL_STROKE, constants.DrawMode.EOF_FILL_STROKE): fill = self._color(self.state.fill_color) else: fill = "none" - if mode in (STROKE, FILL_STROKE, EOF_FILL_STROKE): + if mode in (constants.DrawMode.STROKE, constants.DrawMode.FILL_STROKE, constants.DrawMode.EOF_FILL_STROKE): stroke = self._color(self.state.line_color) else: stroke = "none" - if mode in (EOF_FILL_STROKE, EOF_FILL): + if mode in (constants.DrawMode.EOF_FILL_STROKE, constants.DrawMode.EOF_FILL): rule = "evenodd" else: rule = "nonzero" @@ -330,7 +329,7 @@ def device_fill_points(self, points, mode): clip = None a, b, c, d, tx, ty = affine.affine_params(self.get_ctm()) transform = "matrix(%(a)f,%(b)f,%(c)f,%(d)f,%(tx)f,%(ty)f)" % locals() - if mode == STROKE: + if mode == constants.DrawMode.STROKE: opacity = "%1.3f" % self.state.line_color[-1] self._emit( "polyline", diff --git a/kiva/tests/test_basecore2d.py b/kiva/tests/test_basecore2d.py index b415f47f8..43c32a9ad 100644 --- a/kiva/tests/test_basecore2d.py +++ b/kiva/tests/test_basecore2d.py @@ -51,8 +51,8 @@ class LineStateTestCase(unittest.TestCase): def create_ls(self): color = array([0, 0, 0, 1]) width = 2 - join = basecore2d.JOIN_MITER - cap = basecore2d.CAP_ROUND + join = constants.LineJoin.MITER + cap = constants.LineCap.ROUND phase = 0 pattern = array([5, 5]) dash = (phase, pattern) @@ -191,13 +191,13 @@ def test_state_line_width(self): def test_state_line_join(self): gc = basecore2d.GraphicsContextBase() # defaults to JOIN_MITER - self.assertEqual(gc.state.line_state.line_join, constants.JOIN_MITER) - gc.set_line_join(constants.JOIN_BEVEL) + self.assertEqual(gc.state.line_state.line_join, constants.LineJoin.MITER) + gc.set_line_join(constants.LineJoin.BEVEL) gc.save_state() - gc.set_line_join(constants.JOIN_ROUND) - self.assertEqual(gc.state.line_state.line_join, constants.JOIN_ROUND) + gc.set_line_join(constants.LineJoin.ROUND) + self.assertEqual(gc.state.line_state.line_join, constants.LineJoin.ROUND) gc.restore_state() - self.assertEqual(gc.state.line_state.line_join, constants.JOIN_BEVEL) + self.assertEqual(gc.state.line_state.line_join, constants.LineJoin.BEVEL) # set_line_join should fail if one attempts to set a bad value. self.assertRaises(ValueError, gc.set_line_join, (100,)) @@ -215,13 +215,13 @@ def test_state_miter_limit(self): def test_state_line_cap(self): gc = basecore2d.GraphicsContextBase() # defaults to CAP_ROUND - self.assertEqual(gc.state.line_state.line_cap, constants.CAP_ROUND) - gc.set_line_cap(constants.CAP_BUTT) + self.assertEqual(gc.state.line_state.line_cap, constants.LineCap.ROUND) + gc.set_line_cap(constants.LineCap.BUTT) gc.save_state() - gc.set_line_cap(constants.CAP_SQUARE) - self.assertEqual(gc.state.line_state.line_cap, constants.CAP_SQUARE) + gc.set_line_cap(constants.LineCap.SQUARE) + self.assertEqual(gc.state.line_state.line_cap, constants.LineCap.SQUARE) gc.restore_state() - self.assertEqual(gc.state.line_state.line_cap, constants.CAP_BUTT) + self.assertEqual(gc.state.line_state.line_cap, constants.LineCap.BUTT) # set_line_cap should fail if one attempts to set a bad value. self.assertRaises(ValueError, gc.set_line_cap, (100,)) @@ -319,13 +319,13 @@ def test_state_character_spacing(self): def test_state_text_drawing_mode(self): gc = basecore2d.GraphicsContextBase() # defaults to None - self.assertEqual(gc.state.text_drawing_mode, constants.TEXT_FILL) - gc.set_text_drawing_mode(constants.TEXT_OUTLINE) + self.assertEqual(gc.state.text_drawing_mode, constants.TextMode.FILL) + gc.set_text_drawing_mode(constants.TextMode.OUTLINE) gc.save_state() - gc.set_text_drawing_mode(constants.TEXT_CLIP) - self.assertEqual(gc.state.text_drawing_mode, constants.TEXT_CLIP) + gc.set_text_drawing_mode(constants.TextMode.CLIP) + self.assertEqual(gc.state.text_drawing_mode, constants.TextMode.CLIP) gc.restore_state() - self.assertEqual(gc.state.text_drawing_mode, constants.TEXT_OUTLINE) + self.assertEqual(gc.state.text_drawing_mode, constants.TextMode.OUTLINE) # try an unacceptable value. self.assertRaises(ValueError, gc.set_text_drawing_mode, (10,)) diff --git a/kiva/tests/test_marker_rendering.py b/kiva/tests/test_marker_rendering.py index b2c2edf99..330be235e 100644 --- a/kiva/tests/test_marker_rendering.py +++ b/kiva/tests/test_marker_rendering.py @@ -104,7 +104,7 @@ def test_transformation(self): gc.transform(1.0, 1.0, 0.0, 0.0, 110, 110) points = np.array([[0.0, 0.0]]) buffer.fill(255) - gc.draw_markers(points, 5, constants.SQUARE_MARKER, fill, stroke) + gc.draw_markers(points, 5, constants.Marker.SQUARE, fill, stroke) # Transformed the point _out_ of the bounds. We expect nothing drawn all_white = (np.sum(buffer == [255, 255, 255]) == buffer.size) self.assertTrue(all_white) @@ -112,7 +112,7 @@ def test_transformation(self): # Scale past the bounds gc.transform(2.0, 2.0, 0.0, 0.0, 0.0, 0.0) points = np.array([[90.0, 90.0]]) - gc.draw_markers(points, 5, constants.SQUARE_MARKER, fill, stroke) + gc.draw_markers(points, 5, constants.Marker.SQUARE, fill, stroke) # Transformed the point _out_ of the bounds. We expect nothing drawn all_white = (np.sum(buffer == [255, 255, 255]) == buffer.size) self.assertTrue(all_white) @@ -126,17 +126,17 @@ def test_bad_arguments(self): # Input array shape checking with self.assertRaises(ValueError): - gc.draw_markers(fill, 5, constants.PLUS_MARKER, fill, stroke) + gc.draw_markers(fill, 5, constants.Marker.PLUS, fill, stroke) with self.assertRaises(ValueError): - gc.draw_markers(points, 5, constants.PLUS_MARKER, fill[:2], stroke) + gc.draw_markers(points, 5, constants.Marker.PLUS, fill[:2], stroke) with self.assertRaises(ValueError): - gc.draw_markers(points, 5, constants.PLUS_MARKER, fill, stroke[:2]) + gc.draw_markers(points, 5, constants.Marker.PLUS, fill, stroke[:2]) # Argument type coercions with self.assertRaises(TypeError): gc.draw_markers(points, 5, "plus", fill, stroke) with self.assertRaises(TypeError): - gc.draw_markers(points, [5], constants.PLUS_MARKER, fill, stroke) + gc.draw_markers(points, [5], constants.Marker.PLUS, fill, stroke) # Finally, check that drawing a bad marker ID returns False self.assertFalse(gc.draw_markers(points, 5, 500, fill, stroke)) diff --git a/kiva/trait_defs/tests/test_kiva_font_trait.py b/kiva/trait_defs/tests/test_kiva_font_trait.py index 9b85aa230..12dd8bfe3 100644 --- a/kiva/trait_defs/tests/test_kiva_font_trait.py +++ b/kiva/trait_defs/tests/test_kiva_font_trait.py @@ -21,19 +21,19 @@ # Mapping of strings to valid Kiva font families: font_families = { - "default": constants.DEFAULT, - "decorative": constants.DECORATIVE, - "roman": constants.ROMAN, - "script": constants.SCRIPT, - "swiss": constants.SWISS, - "modern": constants.MODERN, + "default": constants.FontFamily.DEFAULT, + "decorative": constants.FontFamily.DECORATIVE, + "roman": constants.FontFamily.ROMAN, + "script": constants.FontFamily.SCRIPT, + "swiss": constants.FontFamily.SWISS, + "modern": constants.FontFamily.MODERN, } # Mapping of strings to Kiva font styles: -font_styles = {"italic": constants.ITALIC} +font_styles = {"italic": constants.FontStyle.ITALIC} # Mapping of strings to Kiva font weights: -font_weights = {"bold": constants.WEIGHT_BOLD} +font_weights = {"bold": constants.FontWeight.BOLD} class FontExample(HasTraits): @@ -44,28 +44,28 @@ class TestKivaFont(unittest.TestCase): def test_validate_str(self): expected_outcomes = {} - expected_outcomes[""] = Font(size=10, family=constants.DEFAULT) + expected_outcomes[""] = Font(size=10, family=constants.FontFamily.DEFAULT) for weight, kiva_weight in font_weights.items(): - expected_outcomes[weight] = Font(weight=kiva_weight, size=10, family=constants.DEFAULT) + expected_outcomes[weight] = Font(weight=kiva_weight, size=10, family=constants.FontFamily.DEFAULT) for style, kiva_style in font_styles.items(): - expected_outcomes[style] = Font(style=kiva_style, size=10, family=constants.DEFAULT) + expected_outcomes[style] = Font(style=kiva_style, size=10, family=constants.FontFamily.DEFAULT) - expected_outcomes["underline"] = Font(underline=True, size=10, family=constants.DEFAULT) + expected_outcomes["underline"] = Font(underline=True, size=10, family=constants.FontFamily.DEFAULT) - expected_outcomes["18"] = Font(size=18, family=constants.DEFAULT) - expected_outcomes["18 pt"] = Font(size=18, family=constants.DEFAULT) - expected_outcomes["18 point"] = Font(size=18, family=constants.DEFAULT) + expected_outcomes["18"] = Font(size=18, family=constants.FontFamily.DEFAULT) + expected_outcomes["18 pt"] = Font(size=18, family=constants.FontFamily.DEFAULT) + expected_outcomes["18 point"] = Font(size=18, family=constants.FontFamily.DEFAULT) for family, kiva_family in font_families.items(): expected_outcomes[family] = Font(family=kiva_family, size=10) - expected_outcomes["Courier"] = Font("Courier", size=10, family=constants.DEFAULT) - expected_outcomes["Comic Sans"] = Font("Comic Sans", size=10, family=constants.DEFAULT) + expected_outcomes["Courier"] = Font("Courier", size=10, family=constants.FontFamily.DEFAULT) + expected_outcomes["Comic Sans"] = Font("Comic Sans", size=10, family=constants.FontFamily.DEFAULT) expected_outcomes["18 pt Bold Italic Underline Comic Sans script"] = Font( - "Comic Sans", 18, constants.SCRIPT, weight=constants.WEIGHT_BOLD, - style=constants.ITALIC, underline=True, + "Comic Sans", 18, constants.FontFamily.SCRIPT, weight=constants.FontWeight.BOLD, + style=constants.FontStyle.ITALIC, underline=True, ) for name, expected in expected_outcomes.items(): @@ -91,7 +91,7 @@ def test_font_trait_default(self): example = FontExample() self.assertIsInstance(example.font, Font) - self.assertEqual(example.font, Font(size=12, family=constants.MODERN)) + self.assertEqual(example.font, Font(size=12, family=constants.FontFamily.MODERN)) def test_font_trait_none(self): with self.assertRaises(TraitError): diff --git a/kiva/trait_defs/ui/wx/kiva_font_editor.py b/kiva/trait_defs/ui/wx/kiva_font_editor.py index a6c98ee12..44ea807e6 100644 --- a/kiva/trait_defs/ui/wx/kiva_font_editor.py +++ b/kiva/trait_defs/ui/wx/kiva_font_editor.py @@ -59,12 +59,12 @@ def to_wx_font(self, editor): else wx.FONTSTYLE_NORMAL ) family = { - kc.DEFAULT: wx.FONTFAMILY_DEFAULT, - kc.DECORATIVE: wx.FONTFAMILY_DECORATIVE, - kc.ROMAN: wx.FONTFAMILY_ROMAN, - kc.SCRIPT: wx.FONTFAMILY_SCRIPT, - kc.SWISS: wx.FONTFAMILY_SWISS, - kc.MODERN: wx.FONTFAMILY_MODERN, + kc.FontFamily.DEFAULT: wx.FONTFAMILY_DEFAULT, + kc.FontFamily.DECORATIVE: wx.FONTFAMILY_DECORATIVE, + kc.FontFamily.ROMAN: wx.FONTFAMILY_ROMAN, + kc.FontFamily.SCRIPT: wx.FONTFAMILY_SCRIPT, + kc.FontFamily.SWISS: wx.FONTFAMILY_SWISS, + kc.FontFamily.MODERN: wx.FONTFAMILY_MODERN, }.get(font.family, wx.FONTFAMILY_SWISS) return wx.Font( @@ -90,18 +90,18 @@ def from_wx_font(self, font): return Font( size=font.GetPointSize(), family={ - wx.FONTFAMILY_DEFAULT: kc.DEFAULT, - wx.FONTFAMILY_DECORATIVE: kc.DECORATIVE, - wx.FONTFAMILY_ROMAN: kc.ROMAN, - wx.FONTFAMILY_SCRIPT: kc.SCRIPT, - wx.FONTFAMILY_SWISS: kc.SWISS, - wx.FONTFAMILY_MODERN: kc.MODERN, - }.get(font.GetFamily(), kc.SWISS), + wx.FONTFAMILY_DEFAULT: kc.FontFamily.DEFAULT, + wx.FONTFAMILY_DECORATIVE: kc.FontFamily.DECORATIVE, + wx.FONTFAMILY_ROMAN: kc.FontFamily.ROMAN, + wx.FONTFAMILY_SCRIPT: kc.FontFamily.SCRIPT, + wx.FONTFAMILY_SWISS: kc.FontFamily.SWISS, + wx.FONTFAMILY_MODERN: kc.FontFamily.MODERN, + }.get(font.GetFamily(), kc.FontFamily.SWISS), weight=wx_weight_to_weight[font.GetWeight()], style=( # XXX: treat wx.FONTSTYLE_OBLIQUE as italic for now - kc.NORMAL if font.GetStyle() == wx.FONTSTYLE_NORMAL - else kc.ITALIC + kc.FontStyle.NORMAL if font.GetStyle() == wx.FONTSTYLE_NORMAL + else kc.FontStyle.ITALIC ), underline=font.GetUnderlined() - 0, # convert Bool to an int type face_name=font.GetFaceName(),