diff --git a/enable/gcbench/pdf.py b/enable/gcbench/pdf.py index dc46af88b..df26ad187 100644 --- a/enable/gcbench/pdf.py +++ b/enable/gcbench/pdf.py @@ -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) diff --git a/kiva/abstract_graphics_context.py b/kiva/abstract_graphics_context.py index 0404979ec..0daa702ea 100644 --- a/kiva/abstract_graphics_context.py +++ b/kiva/abstract_graphics_context.py @@ -84,7 +84,7 @@ 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 @@ -92,11 +92,14 @@ def set_line_dash(self, line_dash): 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 ---------- @@ -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 @@ -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 @@ -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 @@ -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, diff --git a/kiva/basecore2d.py b/kiva/basecore2d.py index 28f740722..fbc9a7201 100644 --- a/kiva/basecore2d.py +++ b/kiva/basecore2d.py @@ -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 @@ -1003,7 +1005,7 @@ 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 @@ -1011,7 +1013,12 @@ def show_text(self, text): 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. """ diff --git a/kiva/cairo.py b/kiva/cairo.py index 491d62845..35a41656a 100644 --- a/kiva/cairo.py +++ b/kiva/cairo.py @@ -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): """ diff --git a/kiva/celiagg.py b/kiva/celiagg.py index c88829400..a2f40949a 100644 --- a/kiva/celiagg.py +++ b/kiva/celiagg.py @@ -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. @@ -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] diff --git a/kiva/pdf.py b/kiva/pdf.py index bf1fd7422..73ce2eeb6 100644 --- a/kiva/pdf.py +++ b/kiva/pdf.py @@ -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) @@ -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): @@ -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: @@ -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 - def set_text_position(self, x, y): """ """ @@ -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): """ diff --git a/kiva/qpainter.py b/kiva/qpainter.py index 1f92700b7..4a4ad3104 100644 --- a/kiva/qpainter.py +++ b/kiva/qpainter.py @@ -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): """ @@ -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()) + 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). diff --git a/kiva/quartz/ABCGI.pyx b/kiva/quartz/ABCGI.pyx index fdf1aebd6..2351acaa0 100644 --- a/kiva/quartz/ABCGI.pyx +++ b/kiva/quartz/ABCGI.pyx @@ -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: @@ -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): """ """