From 234a272540e55443326dd87e5071e3c19bd3bbe9 Mon Sep 17 00:00:00 2001 From: Gonzalo Odiard Date: Thu, 26 Jul 2012 13:37:30 +0000 Subject: Text tool ported to cairo This patch reorganize a little the text management code, to do the font_properties private to Area. Signed-off-by: Gonzalo Odiard --- diff --git a/Area.py b/Area.py index c9503f0..19f6146 100644 --- a/Area.py +++ b/Area.py @@ -158,7 +158,8 @@ class Area(gtk.DrawingArea): 'fill': True, 'cairo_stroke_color': (0.0, 0.0, 0.0, 0.3), 'cairo_fill_color': (0.0, 0.0, 0.0, 0.3), - 'vertices': 6.0} + 'vertices': 6.0, + 'font_description': 'Sans 12'} self.desenha = False self._selmove = False @@ -173,9 +174,9 @@ class Area(gtk.DrawingArea): self.keep_aspect_ratio = False self.keep_shape_ratio = False - self.font_description = pango.FontDescription() - self.font_description.set_family('Sans') - self.font_description.set_size(12) + self._font_description = None + self.set_font_description( + pango.FontDescription(self.tool['font_description'])) # selection properties self.clear_selection() @@ -190,6 +191,14 @@ class Area(gtk.DrawingArea): self.x_cursor = 0 self.y_cursor = 0 + def set_font_description(self, fd): + self._font_description = fd + self.activity.textview.modify_font(fd) + self.tool['font_description'] = str(fd) + + def get_font_description(self): + return self._font_description + def _get_stamp_size(self): """Set the stamp initial size, based on the display DPI.""" return zoom(44) @@ -284,13 +293,13 @@ class Area(gtk.DrawingArea): context = self.window.cairo_create() if self.desenha: - #logging.error('Expose use temp canvas') + logging.error('Expose use temp canvas') # Paint the canvas in the widget: # TODO: clipping context.set_source_surface(self.temp_canvas) context.paint() else: - #logging.error('Expose use drawing canvas') + logging.error('Expose use drawing canvas') # TODO: clipping context.set_source_surface(self.drawing_canvas) context.paint() @@ -345,11 +354,14 @@ class Area(gtk.DrawingArea): coords = int(event.x), int(event.y) # text + design_mode = True if self.tool['name'] == 'text': self.d.text(widget, event) + design_mode = False # This fixes a bug that made the text viewer get stuck in the canvas elif self.text_in_progress: + design_mode = False try: # This works for a gtk.Entry text = self.activity.textview.get_text() @@ -371,7 +383,6 @@ class Area(gtk.DrawingArea): if self.tool['name'] == 'picker': self.pick_color(x, y) - design_mode = True if state & gtk.gdk.BUTTON1_MASK: #Handle with the left button click event. if self.tool['name'] == 'eraser': diff --git a/Desenho.py b/Desenho.py index cdcc674..19f561d 100644 --- a/Desenho.py +++ b/Desenho.py @@ -70,6 +70,7 @@ import math import gc import gobject import cairo +import pangocairo RESIZE_DELAY = 500 # The time to wait for the resize operation to be # executed, after the resize controls are pressed. @@ -558,22 +559,32 @@ class Desenho: else: widget.text_in_progress = False - try: - # This works for a gtk.Entry - text = widget.activity.textview.get_text() - except AttributeError: - # This works for a gtk.TextView - buf = widget.activity.textview.get_buffer() - start, end = buf.get_bounds() - text = buf.get_text(start, end) + buf = widget.activity.textview.get_buffer() + start, end = buf.get_bounds() + text = buf.get_text(start, end) + + textview = widget.activity.textview + tv_layout = textview.create_pango_layout(text) + + ctx = widget.drawing_ctx + + ctx.save() + ctx.new_path() + pango_context = pangocairo.CairoContext(ctx) + pango_context.set_source_rgba(*widget.tool['cairo_stroke_color']) + + pango_layout = pango_context.create_layout() + pango_layout.set_font_description(widget.get_font_description()) + pango_layout.set_text(unicode(text)) - layout = widget.activity.textview.create_pango_layout(text) + tv_alloc = textview.get_allocation() + pango_context.move_to(tv_alloc.x, tv_alloc.y) + pango_context.show_layout(pango_layout) + ctx.stroke() + ctx.restore() - widget.pixmap.draw_layout(widget.gc_brush, - widget.oldx, widget.oldy, layout) - widget.pixmap_temp.draw_layout(widget.gc, - widget.oldx, widget.oldy, layout) widget.activity.textview.hide() + widget.drawing_canvas.flush() try: widget.activity.textview.set_text('') diff --git a/OficinaActivity.py b/OficinaActivity.py index 16484b6..59cb28d 100644 --- a/OficinaActivity.py +++ b/OficinaActivity.py @@ -90,14 +90,14 @@ class OficinaActivity(activity.Activity): self.fixed.modify_bg(gtk.STATE_NORMAL, style.COLOR_WHITE.get_gdk_color()) + self.textview = gtk.TextView() + self.fixed.put(self.textview, 0, 0) + # These attributes are used in other classes, so they should be public self.area = Area(self) self.area.show() self.fixed.put(self.area, 0, 0) - self.textview = gtk.TextView() - self.fixed.put(self.textview, 0, 0) - sw = gtk.ScrolledWindow() sw.show() sw.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC) diff --git a/toolbox.py b/toolbox.py index 1ed9f53..7ccfdb4 100644 --- a/toolbox.py +++ b/toolbox.py @@ -592,24 +592,11 @@ class TextToolbar(gtk.Toolbar): self._italic.show() self._italic.connect('clicked', self.__italic_bt_cb) - """ - self._text_color = ButtonFillColor(activity) - item = gtk.ToolItem() - item.add(self._text_color) - self.insert(item, -1) - """ - separator = gtk.SeparatorToolItem() separator.set_draw(True) self.insert(separator, -1) - """ - self._font_size_icon = Icon(icon_name="format-text-size", - icon_size=gtk.ICON_SIZE_LARGE_TOOLBAR) - tool_item = gtk.ToolItem() - tool_item.add(self._font_size_icon) - self.insert(tool_item, -1) - """ + fd = activity.area.get_font_description() self._font_size_combo = gtk.combo_box_new_text() self._font_sizes = ['8', '10', '12', '14', '16', '20', @@ -618,7 +605,7 @@ class TextToolbar(gtk.Toolbar): self.__font_size_changed_cb) for i, s in enumerate(self._font_sizes): self._font_size_combo.append_text(s) - if int(s) == activity.area.font_description.get_size(): + if int(s) == fd.get_size(): self._font_size_combo.set_active(i) tool_item = ToolComboBox(self._font_size_combo) @@ -627,39 +614,39 @@ class TextToolbar(gtk.Toolbar): self._font_combo = FontComboBox() self._fonts_changed_id = self._font_combo.connect('changed', self.__font_changed_cb) - font_name = activity.area.font_description.get_family() + font_name = fd.get_family() self._font_combo.set_font_name(font_name) tool_item = ToolComboBox(self._font_combo) self.insert(tool_item, -1) self.show_all() def __bold_bt_cb(self, button): - activity = self._activity + fd = self._activity.area.get_font_description() if button.get_active(): - activity.area.font_description.set_weight(pango.WEIGHT_BOLD) + fd.set_weight(pango.WEIGHT_BOLD) else: - activity.area.font_description.set_weight(pango.WEIGHT_NORMAL) - activity.textview.modify_font(activity.area.font_description) + fd.set_weight(pango.WEIGHT_NORMAL) + self._activity.area.set_font_description(fd) def __italic_bt_cb(self, button): - activity = self._activity + fd = self._activity.area.get_font_description() if button.get_active(): - activity.area.font_description.set_style(pango.STYLE_ITALIC) + fd.set_style(pango.STYLE_ITALIC) else: - activity.area.font_description.set_style(pango.STYLE_NORMAL) - activity.textview.modify_font(activity.area.font_description) + fd.set_style(pango.STYLE_NORMAL) + self._activity.area.set_font_description(fd) def __font_size_changed_cb(self, combo): - activity = self._activity + fd = self._activity.area.get_font_description() value = self.get_active_text(combo) - activity.area.font_description.set_size(int(value) * pango.SCALE) - activity.textview.modify_font(activity.area.font_description) + fd.set_size(int(value) * pango.SCALE) + self._activity.area.set_font_description(fd) def __font_changed_cb(self, combo): - activity = self._activity + fd = self._activity.area.get_font_description() font_name = combo.get_font_name() - activity.area.font_description.set_family(font_name) - activity.textview.modify_font(activity.area.font_description) + fd.set_family(font_name) + self._activity.area.set_font_description(fd) def get_active_text(self, combobox): model = combobox.get_model() -- cgit v0.9.1