Skip to content
Browse files

Use pango for rendering TextElement

  • Loading branch information...
1 parent 9c08a49 commit 2c009941ae308174dc8828aaff4ac84eb169b72e @amolenaar committed
Showing with 17 additions and 5 deletions.
  1. +17 −5 gaphor/diagram/textelement.py
View
22 gaphor/diagram/textelement.py
@@ -5,12 +5,12 @@
import math
+import cairo, pango, pangocairo
from gaphor.diagram.style import Style
from gaphor.diagram.style import ALIGN_CENTER, ALIGN_TOP
from gaphas.geometry import distance_rectangle_point, Rectangle
-from gaphas.util import text_extents, text_align, text_multiline, \
- text_set_font
+
DEFAULT_TEXT_FONT = 'sans 10'
@@ -25,6 +25,14 @@ def swap(list, el1, el2):
list[i2] = el1
+def text_extents(cr, text, font=None, multiline=False):
+ cr = pangocairo.CairoContext(cr)
+ layout = cr.create_layout()
+ layout.set_font_description(pango.FontDescription(font))
+ layout.set_text(text)
+ return layout.get_pixel_size()
+
+
class EditableTextSupport(object):
"""
Editable text support to allow display and edit text parts of a diagram
@@ -385,9 +393,13 @@ def draw(self, context):
width, height = bounds.width, bounds.height
cr = context.cairo
- text_set_font(cr, self._style.font)
- text_multiline(cr, x, y, self.text)
- cr.stroke()
+ if isinstance(cr, cairo.Context):
+ cr = pangocairo.CairoContext(context.cairo)
+ cr.move_to(x, y)
+ layout = cr.create_layout()
+ layout.set_font_description(pango.FontDescription(self._style.font))
+ layout.set_text(self.text)
+ cr.show_layout(layout)
if self.editable and (context.hovered or context.focused):
cr.save()
cr.set_source_rgb(0.6, 0.6, 0.6)

0 comments on commit 2c00994

Please sign in to comment.
Something went wrong with that request. Please try again.