[gtk+/rendering-cleanup: 49/95] label: Draw all text using PangoCairo
- From: Benjamin Otte <otte src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gtk+/rendering-cleanup: 49/95] label: Draw all text using PangoCairo
- Date: Sat, 7 Aug 2010 00:42:47 +0000 (UTC)
commit 61fc02f74704e1f2745c4c1479f086d172f1dd9b
Author: Benjamin Otte <otte redhat com>
Date: Wed Jul 21 14:23:22 2010 +0200
label: Draw all text using PangoCairo
This includes the addition of a "small" helper function,
_gtk_pango_fill_layout() that ignores color information. This
functionality is not available inside Pango and until that happens, we
need this fix. The bug is filed at:
https://bugzilla.gnome.org/show_bug.cgi?id=624917
gtk/Makefile.am | 2 +
gtk/gtklabel.c | 49 ++++++++----
gtk/gtkpango.c | 235 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
gtk/gtkpango.h | 47 +++++++++++
tests/testgtk.c | 10 +++
5 files changed, 326 insertions(+), 17 deletions(-)
---
diff --git a/gtk/Makefile.am b/gtk/Makefile.am
index 3695375..f0d6498 100644
--- a/gtk/Makefile.am
+++ b/gtk/Makefile.am
@@ -373,6 +373,7 @@ gtk_private_h_sources = \
gtkkeyhash.h \
gtkmnemonichash.h \
gtkmountoperationprivate.h \
+ gtkpango.h \
gtkpathbar.h \
gtkplugprivate.h \
gtkprintoperation-private.h\
@@ -518,6 +519,7 @@ gtk_base_c_sources = \
gtkorientable.c \
gtkpagesetup.c \
gtkpaned.c \
+ gtkpango.c \
gtkpapersize.c \
gtkpathbar.c \
gtkplug.c \
diff --git a/gtk/gtklabel.c b/gtk/gtklabel.c
index d712cc6..9caa49e 100644
--- a/gtk/gtklabel.c
+++ b/gtk/gtklabel.c
@@ -33,6 +33,7 @@
#include "gtkdnd.h"
#include "gtkmain.h"
#include "gtkmarshalers.h"
+#include "gtkpango.h"
#include "gtkwindow.h"
#include "gdk/gdkkeysyms.h"
#include "gtkclipboard.h"
@@ -4095,6 +4096,7 @@ gtk_label_expose (GtkWidget *widget,
gint range[2];
cairo_region_t *clip;
GtkStateType state;
+ cairo_t *cr;
range[0] = info->selection_anchor;
range[1] = info->selection_end;
@@ -4116,21 +4118,23 @@ gtk_label_expose (GtkWidget *widget,
* region
*/
- gdk_gc_set_clip_region (widget->style->black_gc, clip);
+ cr = gdk_cairo_create (event->window);
+ gdk_cairo_region (cr, clip);
+ cairo_clip (cr);
state = GTK_STATE_SELECTED;
if (!gtk_widget_has_focus (widget))
state = GTK_STATE_ACTIVE;
- gdk_draw_layout_with_colors (widget->window,
- widget->style->black_gc,
- x, y,
- priv->layout,
- &widget->style->text[state],
- &widget->style->base[state]);
+ gdk_cairo_set_source_color (cr, &widget->style->base[state]);
+ cairo_paint (cr);
- gdk_gc_set_clip_region (widget->style->black_gc, NULL);
+ gdk_cairo_set_source_color (cr, &widget->style->text[state]);
+ cairo_move_to (cr, x, y);
+ _gtk_pango_fill_layout (cr, priv->layout);
+
+ cairo_destroy (cr);
cairo_region_destroy (clip);
}
else if (info)
@@ -4151,16 +4155,26 @@ gtk_label_expose (GtkWidget *widget,
focus_link = gtk_label_get_focus_link (label);
active_link = info->active_link;
+
if (active_link)
{
+ cairo_t *cr;
+
range[0] = active_link->start;
range[1] = active_link->end;
+ cr = gdk_cairo_create (event->window);
+
+ gdk_cairo_region (cr, event->region);
+ cairo_clip (cr);
+
clip = gdk_pango_layout_get_clip_region (priv->layout,
x, y,
range,
1);
- gdk_gc_set_clip_region (widget->style->black_gc, clip);
+ gdk_cairo_region (cr, clip);
+ cairo_clip (cr);
+ cairo_region_destroy (clip);
gtk_label_get_link_colors (widget, &link_color, &visited_link_color);
if (active_link->visited)
@@ -4171,17 +4185,18 @@ gtk_label_expose (GtkWidget *widget,
base_color = &widget->style->base[GTK_STATE_ACTIVE];
else
base_color = &widget->style->base[GTK_STATE_PRELIGHT];
- gdk_draw_layout_with_colors (widget->window,
- widget->style->black_gc,
- x, y,
- priv->layout,
- text_color,
- base_color);
+
+ gdk_cairo_set_source_color (cr, base_color);
+ cairo_paint (cr);
+
+ gdk_cairo_set_source_color (cr, text_color);
+ cairo_move_to (cr, x, y);
+ _gtk_pango_fill_layout (cr, priv->layout);
+
gdk_color_free (link_color);
gdk_color_free (visited_link_color);
- gdk_gc_set_clip_region (widget->style->black_gc, NULL);
- cairo_region_destroy (clip);
+ cairo_destroy (cr);
}
if (focus_link && gtk_widget_has_focus (widget))
diff --git a/gtk/gtkpango.c b/gtk/gtkpango.c
new file mode 100644
index 0000000..4435c7e
--- /dev/null
+++ b/gtk/gtkpango.c
@@ -0,0 +1,235 @@
+/* gtktextdisplay.c - display layed-out text
+ *
+ * Copyright (c) 2010 Red Hat, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free
+ * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+/*
+ * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
+ * file for a list of people on the GTK+ Team. See the ChangeLog
+ * files for a list of changes. These files are distributed with
+ * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
+ */
+
+#include "config.h"
+#include <pango/pangocairo.h>
+#include "gtkintl.h"
+
+#define GTK_TYPE_FILL_LAYOUT_RENDERER (_gtk_fill_layout_renderer_get_type())
+#define GTK_FILL_LAYOUT_RENDERER(object) (G_TYPE_CHECK_INSTANCE_CAST ((object), GTK_TYPE_TEXT_RENDERER, GtkFillLayoutRenderer))
+#define GTK_IS_TEXT_RENDERER(object) (G_TYPE_CHECK_INSTANCE_TYPE ((object), GTK_TYPE_TEXT_RENDERER))
+#define GTK_FILL_LAYOUT_RENDERER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_TEXT_RENDERER, GtkFillLayoutRendererClass))
+#define GTK_IS_TEXT_RENDERER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_TEXT_RENDERER))
+#define GTK_FILL_LAYOUT_RENDERER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_TEXT_RENDERER, GtkFillLayoutRendererClass))
+
+typedef struct _GtkFillLayoutRenderer GtkFillLayoutRenderer;
+typedef struct _GtkFillLayoutRendererClass GtkFillLayoutRendererClass;
+
+struct _GtkFillLayoutRenderer
+{
+ PangoRenderer parent_instance;
+
+ cairo_t *cr;
+};
+
+struct _GtkFillLayoutRendererClass
+{
+ PangoRendererClass parent_class;
+};
+
+G_DEFINE_TYPE (GtkFillLayoutRenderer, _gtk_fill_layout_renderer, PANGO_TYPE_RENDERER)
+
+static void
+gtk_fill_layout_renderer_draw_glyphs (PangoRenderer *renderer,
+ PangoFont *font,
+ PangoGlyphString *glyphs,
+ int x,
+ int y)
+{
+ GtkFillLayoutRenderer *text_renderer = GTK_FILL_LAYOUT_RENDERER (renderer);
+
+ cairo_move_to (text_renderer->cr, (double)x / PANGO_SCALE, (double)y / PANGO_SCALE);
+ pango_cairo_show_glyph_string (text_renderer->cr, font, glyphs);
+}
+
+static void
+gtk_fill_layout_renderer_draw_glyph_item (PangoRenderer *renderer,
+ const char *text,
+ PangoGlyphItem *glyph_item,
+ int x,
+ int y)
+{
+ GtkFillLayoutRenderer *text_renderer = GTK_FILL_LAYOUT_RENDERER (renderer);
+
+ cairo_move_to (text_renderer->cr, (double)x / PANGO_SCALE, (double)y / PANGO_SCALE);
+ pango_cairo_show_glyph_item (text_renderer->cr, text, glyph_item);
+}
+
+static void
+gtk_fill_layout_renderer_draw_rectangle (PangoRenderer *renderer,
+ PangoRenderPart part,
+ int x,
+ int y,
+ int width,
+ int height)
+{
+ GtkFillLayoutRenderer *text_renderer = GTK_FILL_LAYOUT_RENDERER (renderer);
+ double cur_x, cur_y;
+
+ if (part == PANGO_RENDER_PART_BACKGROUND)
+ return;
+
+ cairo_rectangle (text_renderer->cr,
+ (double)x / PANGO_SCALE, (double)y / PANGO_SCALE,
+ (double)width / PANGO_SCALE, (double)height / PANGO_SCALE);
+ cairo_fill (text_renderer->cr);
+}
+
+static void
+gtk_fill_layout_renderer_draw_trapezoid (PangoRenderer *renderer,
+ PangoRenderPart part,
+ double y1_,
+ double x11,
+ double x21,
+ double y2,
+ double x12,
+ double x22)
+{
+ GtkFillLayoutRenderer *text_renderer = GTK_FILL_LAYOUT_RENDERER (renderer);
+ cairo_matrix_t matrix;
+ cairo_t *cr;
+ double x, y;
+
+ cr = text_renderer->cr;
+
+ cairo_save (cr);
+
+ /* use identity scale, but keep translation */
+ cairo_get_matrix (cr, &matrix);
+ matrix.xx = matrix.yy = 1;
+ matrix.xy = matrix.yx = 0;
+ cairo_set_matrix (cr, &matrix);
+
+ cairo_move_to (cr, x11, y1_);
+ cairo_line_to (cr, x21, y1_);
+ cairo_line_to (cr, x22, y2);
+ cairo_line_to (cr, x12, y2);
+ cairo_close_path (cr);
+
+ cairo_fill (cr);
+
+ cairo_restore (cr);
+}
+
+static void
+gtk_fill_layout_renderer_draw_error_underline (PangoRenderer *renderer,
+ int x,
+ int y,
+ int width,
+ int height)
+{
+ GtkFillLayoutRenderer *text_renderer = GTK_FILL_LAYOUT_RENDERER (renderer);
+
+ pango_cairo_show_error_underline (text_renderer->cr,
+ (double)x / PANGO_SCALE, (double)y / PANGO_SCALE,
+ (double)width / PANGO_SCALE, (double)height / PANGO_SCALE);
+}
+
+static void
+gtk_fill_layout_renderer_draw_shape (PangoRenderer *renderer,
+ PangoAttrShape *attr,
+ int x,
+ int y)
+{
+ GtkFillLayoutRenderer *text_renderer = GTK_FILL_LAYOUT_RENDERER (renderer);
+ cairo_t *cr = text_renderer->cr;
+ PangoLayout *layout;
+ PangoCairoShapeRendererFunc shape_renderer;
+ gpointer shape_renderer_data;
+
+ layout = pango_renderer_get_layout (renderer);
+
+ if (!layout)
+ return;
+
+ shape_renderer = pango_cairo_context_get_shape_renderer (pango_layout_get_context (layout),
+ &shape_renderer_data);
+
+ if (!shape_renderer)
+ return;
+
+ cairo_save (cr);
+
+ cairo_move_to (cr, (double)x / PANGO_SCALE, (double)y / PANGO_SCALE);
+
+ shape_renderer (cr, attr, FALSE, shape_renderer_data);
+
+ cairo_restore (cr);
+}
+
+static void
+gtk_fill_layout_renderer_finalize (GObject *object)
+{
+ G_OBJECT_CLASS (_gtk_fill_layout_renderer_parent_class)->finalize (object);
+}
+
+static void
+_gtk_fill_layout_renderer_init (GtkFillLayoutRenderer *renderer)
+{
+}
+
+static void
+_gtk_fill_layout_renderer_class_init (GtkFillLayoutRendererClass *klass)
+{
+ GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+ PangoRendererClass *renderer_class = PANGO_RENDERER_CLASS (klass);
+
+ renderer_class->draw_glyphs = gtk_fill_layout_renderer_draw_glyphs;
+ renderer_class->draw_glyph_item = gtk_fill_layout_renderer_draw_glyph_item;
+ renderer_class->draw_rectangle = gtk_fill_layout_renderer_draw_rectangle;
+ renderer_class->draw_trapezoid = gtk_fill_layout_renderer_draw_trapezoid;
+ renderer_class->draw_error_underline = gtk_fill_layout_renderer_draw_error_underline;
+ renderer_class->draw_shape = gtk_fill_layout_renderer_draw_shape;
+
+ object_class->finalize = gtk_fill_layout_renderer_finalize;
+}
+
+void
+_gtk_pango_fill_layout (cairo_t *cr,
+ PangoLayout *layout)
+{
+ static GtkFillLayoutRenderer *renderer = NULL;
+ gboolean has_current_point;
+ double current_x, current_y;
+
+ has_current_point = cairo_has_current_point (cr);
+ cairo_get_current_point (cr, ¤t_x, ¤t_y);
+
+ if (renderer == NULL)
+ renderer = g_object_new (GTK_TYPE_FILL_LAYOUT_RENDERER, NULL);
+
+ cairo_save (cr);
+ cairo_translate (cr, current_x, current_y);
+
+ renderer->cr = cr;
+ pango_renderer_draw_layout (PANGO_RENDERER (renderer), layout, 0, 0);
+
+ cairo_restore (cr);
+
+ if (has_current_point)
+ cairo_move_to (cr, current_x, current_y);
+}
+
diff --git a/gtk/gtkpango.h b/gtk/gtkpango.h
new file mode 100644
index 0000000..d02c7a6
--- /dev/null
+++ b/gtk/gtkpango.h
@@ -0,0 +1,47 @@
+/* GTK - The GIMP Toolkit
+ * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+/*
+ * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
+ * file for a list of people on the GTK+ Team. See the ChangeLog
+ * files for a list of changes. These files are distributed with
+ * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
+ */
+
+#if !defined (__GTK_H_INSIDE__) && !defined (GTK_COMPILATION)
+#error "Only <gtk/gtk.h> can be included directly."
+#endif
+
+#ifndef __GTK_PANGO_H__
+#define __GTK_PANGO_H__
+
+
+#include <pango/pangocairo.h>
+
+
+G_BEGIN_DECLS
+
+void
+_gtk_pango_fill_layout (cairo_t *cr,
+ PangoLayout *layout);
+
+
+G_END_DECLS
+
+#endif /* __GTK_PANGO_H__ */
diff --git a/tests/testgtk.c b/tests/testgtk.c
index a7ad14b..178b0fd 100644
--- a/tests/testgtk.c
+++ b/tests/testgtk.c
@@ -2159,18 +2159,21 @@ void create_labels (GtkWidget *widget)
frame = gtk_frame_new ("Multi-line Label");
label = gtk_label_new ("This is a Multi-line label.\nSecond line\nThird line");
gtk_label_set_ellipsize (GTK_LABEL (label), PANGO_ELLIPSIZE_END);
+ gtk_label_set_selectable (GTK_LABEL (label), TRUE);
gtk_container_add (GTK_CONTAINER (frame), label);
gtk_box_pack_start (GTK_BOX (vbox), frame, FALSE, FALSE, 0);
frame = gtk_frame_new ("Left Justified Label");
label = gtk_label_new ("This is a Left-Justified\nMulti-line label.\nThird line");
gtk_label_set_ellipsize (GTK_LABEL (label), PANGO_ELLIPSIZE_MIDDLE);
+ gtk_label_set_selectable (GTK_LABEL (label), TRUE);
gtk_label_set_justify (GTK_LABEL (label), GTK_JUSTIFY_LEFT);
gtk_container_add (GTK_CONTAINER (frame), label);
gtk_box_pack_start (GTK_BOX (vbox), frame, FALSE, FALSE, 0);
frame = gtk_frame_new ("Right Justified Label");
gtk_label_set_ellipsize (GTK_LABEL (label), PANGO_ELLIPSIZE_START);
+ gtk_label_set_selectable (GTK_LABEL (label), TRUE);
label = gtk_label_new ("This is a Right-Justified\nMulti-line label.\nFourth line, (j/k)");
gtk_label_set_justify (GTK_LABEL (label), GTK_JUSTIFY_RIGHT);
gtk_container_add (GTK_CONTAINER (frame), label);
@@ -2186,12 +2189,14 @@ void create_labels (GtkWidget *widget)
"Chinese (Traditional) <span lang=\"zh-tw\">\345\205\203\346\260\243 \351\226\213\347\231\274</span>\n"
"Japanese <span lang=\"ja\">\345\205\203\346\260\227 \351\226\213\347\231\272</span>");
gtk_label_set_justify (GTK_LABEL (label), GTK_JUSTIFY_LEFT);
+ gtk_label_set_selectable (GTK_LABEL (label), TRUE);
gtk_container_add (GTK_CONTAINER (frame), label);
gtk_box_pack_start (GTK_BOX (vbox), frame, FALSE, FALSE, 0);
frame = gtk_frame_new ("Bidirection Label");
label = gtk_label_new ("\342\200\217Arabic \330\247\331\204\330\263\331\204\330\247\331\205 \330\271\331\204\331\212\331\203\331\205\n"
"\342\200\217Hebrew \327\251\327\234\327\225\327\235");
+ gtk_label_set_selectable (GTK_LABEL (label), TRUE);
gtk_container_add (GTK_CONTAINER (frame), label);
gtk_box_pack_start (GTK_BOX (vbox), frame, FALSE, FALSE, 0);
@@ -2199,6 +2204,7 @@ void create_labels (GtkWidget *widget)
label = gtk_label_new ("Some <a href=\"http://en.wikipedia.org/wiki/Text\" title=\"plain text\">text</a> may be marked up\n"
"as hyperlinks, which can be clicked\n"
"or activated via <a href=\"keynav\">keynav</a>");
+ gtk_label_set_selectable (GTK_LABEL (label), TRUE);
gtk_label_set_use_markup (GTK_LABEL (label), TRUE);
gtk_container_add (GTK_CONTAINER (frame), label);
gtk_box_pack_start (GTK_BOX (vbox), frame, FALSE, FALSE, 0);
@@ -2214,6 +2220,7 @@ void create_labels (GtkWidget *widget)
"The sixth sheik's six sheep's sick.\n"\
" It supports multiple paragraphs correctly, and correctly adds "\
"many extra spaces. ");
+ gtk_label_set_selectable (GTK_LABEL (label), TRUE);
gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
gtk_container_add (GTK_CONTAINER (frame), label);
@@ -2227,6 +2234,7 @@ void create_labels (GtkWidget *widget)
" This is a new paragraph.\n"\
" This is another newer, longer, better paragraph. It is coming to an end, "\
"unfortunately.");
+ gtk_label_set_selectable (GTK_LABEL (label), TRUE);
gtk_label_set_justify (GTK_LABEL (label), GTK_JUSTIFY_FILL);
gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
gtk_container_add (GTK_CONTAINER (frame), label);
@@ -2235,6 +2243,7 @@ void create_labels (GtkWidget *widget)
frame = gtk_frame_new ("Underlined label");
label = gtk_label_new ("This label is underlined!\n"
"This one is underlined (\343\201\223\343\202\223\343\201\253\343\201\241\343\201\257) in quite a funky fashion");
+ gtk_label_set_selectable (GTK_LABEL (label), TRUE);
gtk_label_set_justify (GTK_LABEL (label), GTK_JUSTIFY_LEFT);
gtk_label_set_pattern (GTK_LABEL (label), "_________________________ _ _________ _ _____ _ __ __ ___ ____ _____");
gtk_container_add (GTK_CONTAINER (frame), label);
@@ -2261,6 +2270,7 @@ void create_labels (GtkWidget *widget)
"la <big>la <big>la <big>la <big>la</big></big></big></big>\n"
"but this _word is <span foreground=\"purple\"><big>purple</big></span>\n"
"<span underline=\"double\">We like <sup>superscript</sup> and <sub>subscript</sub> too</span>");
+ gtk_label_set_selectable (GTK_LABEL (label), TRUE);
g_assert (gtk_label_get_mnemonic_keyval (GTK_LABEL (label)) == GDK_s);
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]