Skip to content

Convert Kiva constants to Python Enums #953

New issue

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

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

Already on GitHub? Sign in to your account

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/source/kiva/state_ex.py
Original file line number Diff line number Diff line change
@@ -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))
Expand Down
10 changes: 5 additions & 5 deletions docs/source/kiva_tutorial/tutorial.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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
Expand Down Expand Up @@ -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()
Expand All @@ -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
Expand Down Expand Up @@ -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()

Expand Down
12 changes: 5 additions & 7 deletions docs/source/kiva_tutorial/tutorial_advanced.py
Original file line number Diff line number Diff line change
@@ -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


Expand Down Expand Up @@ -92,15 +90,15 @@ 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()
wire_connection_path.move_to(0,0)
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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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()

Expand Down
24 changes: 12 additions & 12 deletions enable/savage/svg/backends/kiva/renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Comment on lines +416 to +418
Copy link
Member

Choose a reason for hiding this comment

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

We should perhaps note in the FontStyle declaration that it's used as a bitmask?

return Font(font_name, style=kiva_style)

@classmethod
Expand Down
26 changes: 13 additions & 13 deletions enable/tests/trait_defs/test_kiva_font_trait.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand All @@ -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)

Expand All @@ -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):
Expand Down
30 changes: 15 additions & 15 deletions enable/trait_defs/kiva_font_trait.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}


Expand All @@ -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)

Expand Down
22 changes: 11 additions & 11 deletions enable/trait_defs/ui/kiva_font_editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
}


Expand Down Expand Up @@ -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)
Expand Down
1 change: 0 additions & 1 deletion kiva/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,4 @@
"""
from kiva._version import full_version as __version__

from .constants import *
from .fonttools import Font
26 changes: 6 additions & 20 deletions kiva/abstract_graphics_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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,
Expand Down
Loading