Permalink
Browse files

Attributes and Operations render with Pango.

  • Loading branch information...
1 parent 2c00994 commit f429e37d53c854c077863af018ec16ed400789af @amolenaar committed Jun 27, 2011
@@ -11,7 +11,7 @@
# are connected to the same Class, the head_end property is connected to the
# tail end and visa versa.
-from gaphas.util import text_extents, text_align, text_multiline
+from gaphas.util import text_extents, text_multiline
from gaphas.state import reversible_property
from gaphas import Item
from gaphas.geometry import Rectangle, distance_point_point_fast
@@ -2,13 +2,14 @@
Diagram item with compartments.
"""
-import cairo
+import cairo, pango, pangocairo
from gaphas.state import observed, reversible_property
from gaphor import UML
from gaphor.diagram.diagramitem import DiagramItem
from gaphor.diagram.nameditem import NamedItem
-from gaphas.util import text_extents, text_set_font, text_align, text_underline
+from textelement import text_extents
+from gaphas.util import text_align
class FeatureItem(object):
@@ -77,13 +78,18 @@ def render(self):
def draw(self, context):
cr = context.cairo
- if self.font:
- text_set_font(cr, self.font)
- if hasattr(self.subject, 'isStatic') and self.subject.isStatic:
- text_underline(cr, 0, 0, self.render() or '')
- else:
- text_align(cr, 0, 0, self.render(), align_x=1, align_y=1)
-
+ if isinstance(cr, cairo.Context):
+ cr = pangocairo.CairoContext(cr)
+ layout = cr.create_layout()
+ layout.set_font_description(pango.FontDescription(self.font))
+ layout.set_text(self.render() or '')
+
+ if hasattr(self.subject, 'isStatic') and self.subject.isStatic:
+ attrlist = pango.AttrList()
+ attrlist.insert(pango.AttrUnderline(pango.UNDERLINE_SINGLE,
+ 2, -1))
+ layout.set_attributes(attrlist)
+ cr.show_layout(layout)
class Compartment(list):
@@ -577,4 +583,4 @@ def item_at(self, x, y):
return None
-# vim:sw=4:et
+# vi:ai:sw=4:et
@@ -6,7 +6,6 @@
import gaphas
from gaphor import UML
-from gaphas.util import text_extents, text_align
from diagramitem import DiagramItem
from interfaces import IConnect
@@ -10,7 +10,7 @@
from gaphor.diagram.nameditem import NamedItem
from gaphor.diagram.style import ALIGN_CENTER, ALIGN_BOTTOM
-from gaphas.util import text_extents, text_multiline
+
DEFAULT_UPPER_BOUND = '*'
@@ -26,13 +26,32 @@ def swap(list, el1, el2):
def text_extents(cr, text, font=None, multiline=False):
+ if not multiline:
+ text = text.replace('\n', ' ')
cr = pangocairo.CairoContext(cr)
layout = cr.create_layout()
layout.set_font_description(pango.FontDescription(font))
layout.set_text(text)
return layout.get_pixel_size()
+def text_align(cr, x, y, text, align_x=0, align_y=0, padding_x=0, padding_y=0):
+ """
+ Draw text relative to (x, y).
+ x, y - coordinates
+ text - text to print (utf8)
+ align_x - -1 (top), 0 (middle), 1 (bottom)
+ align_y - -1 (left), 0 (center), 1 (right)
+ padding_x - padding (extra offset), always > 0
+ padding_y - padding (extra offset), always > 0
+ """
+ cr = pangocairo.CairoContext(cr)
+ layout = cr.create_layout()
+ layout.set_font_description(pango.FontDescription(self._style.font))
+ layout.set_text(self.text)
+ cr.show_layout(layout)
+
+
class EditableTextSupport(object):
"""
Editable text support to allow display and edit text parts of a diagram
@@ -6,7 +6,8 @@
from gaphor import UML
from gaphor.diagram.classifier import ClassifierItem
from gaphor.diagram.style import ALIGN_CENTER, ALIGN_MIDDLE
-from gaphas.util import text_align, text_extents, path_ellipse
+from textelement import text_extents
+from gaphas.util import path_ellipse
class UseCaseItem(ClassifierItem):
"""
View
@@ -11,5 +11,5 @@ verbosity=2
rednose=1
detailed-errors=1
#with-coverage=1
-cover-package=gaphor
+cover-package=gaphor/diagram

0 comments on commit f429e37

Please sign in to comment.