Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion enable/gcbench/pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ def __init__(self, size, *args, **kw):
def save(self, filename, *args, **kw):
# Reportlab is a bit silly
self.gc._filename = filename
super().save()
super().save(filename, *args, **kw)
22 changes: 15 additions & 7 deletions kiva/abstract_graphics_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,19 +84,22 @@ def set_line_cap(self, line_cap):
"""

@abstractmethod
def set_line_dash(self, line_dash):
def set_line_dash(self, line_dash, phase=0):
""" Set the dash style to use when stroking a path

Parameters
----------
line_dash
An even-lengthed tuple of floats that represents
the width of each dash and gap in the dash pattern.
phase : float
Specifies how many units into the dash pattern to start.
"""

@abstractmethod
def set_fill_color(self, color):
""" Set the color used to fill the region bounded by a path
""" Set the color used to fill the region bounded by a path or when
drawing text.

Parameters
----------
Expand All @@ -108,10 +111,13 @@ def set_fill_color(self, color):

@abstractmethod
def get_fill_color(self):
""" Get the color used to fill the region bounded by a path """
""" Get the color used to fill the region bounded by a path or when
drawing text.
"""

@abstractmethod
def linear_gradient(self, x1, y1, x2, y2, stops, spread_method, units):
def linear_gradient(self, x1, y1, x2, y2, stops, spread_method,
units="userSpaceOnUse"):
""" Modify the fill color to be a linear gradient

Parameters
Expand All @@ -137,7 +143,8 @@ def linear_gradient(self, x1, y1, x2, y2, stops, spread_method, units):
"""

@abstractmethod
def radial_gradient(self, cx, cy, r, fx, fy, stops, spread_method, units):
def radial_gradient(self, cx, cy, r, fx, fy, stops, spread_method,
units="userSpaceOnUse"):
""" Modify the fill color to be a radial gradient

Parameters
Expand Down Expand Up @@ -493,7 +500,7 @@ def get_text_position(self):
""" Get the current point where text will be drawn """

@abstractmethod
def show_text(self, text):
def show_text(self, text, point=None):
""" Draw the specified string at the current point """

@abstractmethod
Expand Down Expand Up @@ -656,7 +663,8 @@ 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):
def draw_path_at_points(self, point_array, compiled_path,
draw_mode=FILL_STROKE):
""" Draw a compiled path at a collection of points

The starting point of the paths are specified by the points,
Expand Down
15 changes: 11 additions & 4 deletions kiva/basecore2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -811,11 +811,13 @@ def get_fill_color(self, color):
"""
return self.state.fill_color

def linear_gradient(self, x1, y1, x2, y2, stops, spread_method, units):
def linear_gradient(self, x1, y1, x2, y2, stops, spread_method,
units="userSpaceOnUse"):
""" Modify the fill color to be a linear gradient """
pass

def radial_gradient(self, cx, cy, r, fx, fy, stops, spread_method, units):
def radial_gradient(self, cx, cy, r, fx, fy, stops, spread_method,
units="userSpaceOnUse"):
""" Modify the fill color to be a linear gradient """
pass

Expand Down Expand Up @@ -1003,15 +1005,20 @@ def get_text_matrix(self):
"""
return self.state.text_matrix.copy()

def show_text(self, text):
def show_text(self, text, point=None):
""" Draws text on the device at the current text position.

This calls the device dependent device_show_text() method to
do all the heavy lifting.

It is not clear yet how this should affect the current point.
"""
self.device_show_text(text)
if point is not None:
with self:
self.set_text_position(*point)
self.device_show_text(text)
else:
self.device_show_text(text)

def show_text_tanslate(self, text, dx, dy):
""" Draws text at the specified offset. """
Expand Down
8 changes: 6 additions & 2 deletions kiva/cairo.py
Original file line number Diff line number Diff line change
Expand Up @@ -1082,11 +1082,15 @@ def get_text_matrix(self):
"""
return copy.copy(self.text_matrix)

def show_text(self, text, point=(0.0, 0.0)):
def show_text(self, text, point=None):
""" Draws text on the device at the current text position.
Leaves the current point unchanged.
"""
self.show_text_at_point(text, point[0], point[1])
if point is not None:
x, y = point
else:
x, y = 0.0, 0.0
self.show_text_at_point(text, x, y)

def show_glyphs(self):
"""
Expand Down
10 changes: 7 additions & 3 deletions kiva/celiagg.py
Original file line number Diff line number Diff line change
Expand Up @@ -608,15 +608,15 @@ def normalize_image(img):
# Drawing Text
# ----------------------------------------------------------------

def select_font(self, name, size, textEncoding):
def select_font(self, face_name, size=12, style='regular', encoding=None):
""" Set the font for the current graphics context.
"""
self.font = agg.Font(name, size)
self.set_font(Font(face_name, size=size, style=style))

def set_font(self, font):
""" Set the font for the current graphics context.
"""
self.select_font(font.findfont(), font.size, None)
self.font = agg.Font(font.findfont(), font.size)

def set_font_size(self, size):
""" Set the font size for the current graphics context.
Expand All @@ -631,6 +631,10 @@ def set_character_spacing(self, spacing):
msg = "set_character_spacing not implemented on celiagg yet."
raise NotImplementedError(msg)

def get_character_spacing(self):
msg = "get_character_spacing not implemented on celiagg yet."
raise NotImplementedError(msg)

def set_text_drawing_mode(self, mode):
try:
tmode = text_modes[mode]
Expand Down
35 changes: 15 additions & 20 deletions kiva/pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@
join_style[constants.JOIN_BEVEL] = 2
join_style[constants.JOIN_MITER] = 0

font_styles = {}
font_styles["regular"] = ""
font_styles["bold"] = "-Bold"
font_styles["italic"] = "-Oblique"
font_styles["bold italic"] = "-BoldOblique"

# stroke, fill, mode
path_mode = {}
path_mode[constants.FILL_STROKE] = (1, 1, canvas.FILL_NON_ZERO)
Expand Down Expand Up @@ -612,9 +618,10 @@ def draw_image(self, img, rect=None):
# Drawing Text
# ----------------------------------------------------------------

def select_font(self, name, size, textEncoding):
def select_font(self, name, size, style="regular", encoding=None):
""" PDF ignores the Encoding variable.
"""
name += font_styles.get(style, "")
self.gc.setFont(name, size)

def set_font(self, font):
Expand All @@ -625,6 +632,8 @@ def set_font(self, font):
if face_name == "":
face_name = "Helvetica"

# Apply the style as a suffix to the face name
face_name += font_styles.get(font.style, "")
try:
self.gc.setFont(face_name, font.size)
except KeyError:
Expand All @@ -647,20 +656,6 @@ def set_font_size(self, size):
font = self.gc._fontname
self.gc.setFont(font, size)

def set_character_spacing(self):
"""
"""
pass

def get_character_spacing(self):
""" Get the current font """
raise NotImplementedError

def set_text_drawing_mode(self):
"""
"""
pass
Comment on lines -650 to -662
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Just to clarify, why did these get removed?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

They're covered by GraphicsContextBase


def set_text_position(self, x, y):
"""
"""
Expand All @@ -683,22 +678,22 @@ def get_text_matrix(self):
a, b, c, d, tx, ty = self.gc._textMatrix
return affine.affine_from_values(a, b, c, d, tx, ty)

def show_text(self, text, x=None, y=None):
def show_text(self, text, point=None):
""" Draws text on the device at current text position.

This is also used for showing text at a particular point
specified by x and y.
specified by ``point``.

This ignores the text matrix for now.
"""
if x and y:
pass
if point:
x, y = point
else:
x, y = self.text_xy
self.gc.drawString(x, y, text)

def show_text_at_point(self, text, x, y):
self.show_text(text, x, y)
self.show_text(text, point=(x, y))

def show_glyphs(self):
"""
Expand Down
24 changes: 17 additions & 7 deletions kiva/qpainter.py
Original file line number Diff line number Diff line change
Expand Up @@ -644,16 +644,22 @@ def set_font_size(self, size):
self.gc.setFont(font)

def set_character_spacing(self, spacing):
"""
""" Set the spacing between characters when drawing text
"""
font = self.gc.font()
font.setLetterSpacing(QtGui.QFont.AbsoluteSpacing, spacing)
self.gc.setFont(font)

def set_text_drawing_mode(self):
def get_character_spacing(self):
""" Get the spacing between characters when drawing text
"""
font = self.gc.font()
return font.letterSpacing()

def set_text_drawing_mode(self, mode):
""" Set the drawing mode to use with text
"""
pass
raise NotImplementedError()

def set_text_position(self, x, y):
"""
Expand Down Expand Up @@ -690,10 +696,14 @@ def show_text(self, text, point=None):
unflip_trans.translate(0, self._height)
unflip_trans.scale(1.0, -1.0)

self.gc.save()
self.gc.setTransform(unflip_trans, True)
self.gc.drawText(QtCore.QPointF(pos[0], self._flip_y(pos[1])), text)
self.gc.restore()
# Make some temporary modifications to the state
with self:
# Kiva uses the fill color for text
brush = self.gc.brush()
self.gc.setPen(brush.color())
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

verified this with the benchmark 👍

self.gc.setTransform(unflip_trans, True)
pos = QtCore.QPointF(pos[0], self._flip_y(pos[1]))
self.gc.drawText(pos, text)

def show_text_at_point(self, text, x, y):
""" Draw text at some point (x, y).
Expand Down
14 changes: 10 additions & 4 deletions kiva/quartz/ABCGI.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -787,8 +787,8 @@ cdef class CGContext:
# Drawing Text
#----------------------------------------------------------------

def select_font(self, object name, float size, style='regular'):
"""
def select_font(self, object name, float size, style='regular', encoding=None):
""" Select a font to use.
"""
key = (name, size, style)
if key not in self.font_cache:
Expand Down Expand Up @@ -826,13 +826,19 @@ cdef class CGContext:
self.current_font = self.font_cache[key]

def set_character_spacing(self, float spacing):
""" Set the spacing between characters when drawing text
"""
"""

# XXX: Perhaps this should be handled by the kerning attribute
# in the attributed string that is used to make a line of text?
CGContextSetCharacterSpacing(self.context, spacing)

def get_character_spacing(self):
""" Get the current spacing between characters when drawing text
"""
# XXX: There is no "get" counterpart for CGContextSetCharacterSpacing
msg = "get_character_spacing not implemented for Quartz yet."
raise NotImplementedError(msg)

def set_text_drawing_mode(self, object mode):
"""
"""
Expand Down