Permalink
Browse files

Fixed text for comments.

  • Loading branch information...
amolenaar committed Jul 13, 2011
1 parent b247800 commit 51b4b7952404c5caf98bedfc3400ca947b5d79ec
Showing with 46 additions and 22 deletions.
  1. +14 −7 gaphor/diagram/comment.py
  2. +32 −15 gaphor/diagram/textelement.py
View
@@ -5,13 +5,17 @@
from gaphor import UML
from elementitem import ElementItem
from gaphas.item import NW
-from gaphas.util import text_multiline, text_extents
+from textelement import text_multiline, text_extents
class CommentItem(ElementItem):
__uml__ = UML.Comment
+ __style__ = {
+ 'font': 'sans 10'
+ }
+
EAR=15
OFFSET=5
@@ -32,11 +36,13 @@ def pre_update(self, context):
if not self.subject:
return
cr = context.cairo
- w, h = text_extents(cr, self.subject.body, multiline=True, padding=2)
- e2 = self.EAR * 2
- self.min_width = w + e2 + self.OFFSET
- self.min_height = h + e2
+ e = self.EAR
+ o = self.OFFSET
+ w, h = text_extents(cr, self.subject.body, self.style.font, width=self.width - e)
+ self.min_width = w + e + o * 2
+ self.min_height = h + o * 2
ElementItem.pre_update(self, context)
+ print self.width, self.height, self.min_width, self.min_height
def draw(self, context):
@@ -60,9 +66,10 @@ def draw(self, context):
line_to(w, oy + ear)
c.stroke()
if self.subject.body:
+ off = self.OFFSET
# Do not print empty string, since cairo-win32 can't handle it.
- text_multiline(c, self.EAR, self.EAR, self.subject.body, padding=2)
+ text_multiline(c, off, off, self.subject.body, self.style.font, self.width - ear, self.height)
#c.move_to(10, 15)
#c.show_text(self.subject.body)
-# vim:sw=4
+# vim:sw=4:et:ai
@@ -25,36 +25,45 @@ def swap(list, el1, el2):
list[i2] = el1
-def text_extents(cr, text, font=None, multiline=False):
- if not multiline:
- text = text.replace('\n', ' ')
+def _text_layout(cr, text, font, width):
cr = pangocairo.CairoContext(cr)
layout = cr.create_layout()
- layout.set_font_description(pango.FontDescription(font))
+ if font:
+ layout.set_font_description(pango.FontDescription(font))
layout.set_text(text)
+ layout.set_width(int(width * pango.SCALE))
+ #layout.set_height(height)
+ return layout
+
+
+def text_extents(cr, text, font=None, width=-1, height=-1):
+ if not text:
+ return 0, 0
+ layout = _text_layout(cr, text, font, width)
return layout.get_pixel_size()
-def text_align(cr, x, y, text, font, align_x=0, align_y=0, padding_x=0, padding_y=0):
+def text_align(cr, x, y, text, font, width=-1, height=-1,
+ 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)
font - The font to render in
- align_x - -1 (top), 0 (middle), 1 (bottom)
- align_y - -1 (left), 0 (center), 1 (right)
+ width
+ height
+ 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
"""
if not isinstance(cr, cairo.Context):
return
+ if not text:
+ return
+
+ layout = _text_layout(cr, text, font, width)
- cr = pangocairo.CairoContext(cr)
- layout = cr.create_layout()
- print 'align with font', font
- if font:
- layout.set_font_description(font)
- layout.set_text(text)
w, h = layout.get_pixel_size()
if align_x == 0:
@@ -74,6 +83,14 @@ def text_align(cr, x, y, text, font, align_x=0, align_y=0, padding_x=0, padding_
cr.show_layout(layout)
+def text_center(cr, x, y, text, font):
+ text_align(cr, x, y, text, font=font, align_x=0, align_y=0)
+
+
+def text_multiline(cr, x, y, text, font, width=-1, height=-1):
+ text_align(cr, x, y, text, font=font, width=width, height=height, align_x=1, align_y=1)
+
+
class EditableTextSupport(object):
"""
Editable text support to allow display and edit text parts of a diagram
@@ -188,7 +205,7 @@ def _set_text_sizes(self, context, texts):
"""
cr = context.cairo
for txt in texts:
- w, h = text_extents(cr, txt.text, font=txt.style.font, multiline=True)
+ w, h = text_extents(cr, txt.text, font=txt.style.font)
txt.bounds.width = max(15, w)
txt.bounds.height = max(10, h)
@@ -434,7 +451,7 @@ def draw(self, context):
width, height = bounds.width, bounds.height
cr = context.cairo
- if isinstance(cr, cairo.Context):
+ if isinstance(cr, cairo.Context) and self.text:
cr = pangocairo.CairoContext(context.cairo)
cr.move_to(x, y)
layout = cr.create_layout()

0 comments on commit 51b4b79

Please sign in to comment.