Debian Codegraph @

demos/gtk-demo/rotated_text.c

raw
1 /* Pango/Rotated Text
2 *
3 * This demo shows how to use PangoCairo to draw rotated and transformed
4 * text. The right pane shows a rotated GtkLabel widget.
5 *
6 * In both cases, a custom PangoCairo shape renderer is installed to draw
7 * a red heard using cairo drawing operations instead of the Unicode heart
8 * character.
9 */
10
11 #include <gtk/gtk.h>
12 #include <string.h>
13
14 #define HEART "♥"
15 const char text[] = "I ♥ GTK+";
16
17 static void
18 fancy_shape_renderer (cairo_t *cr,
19 PangoAttrShape *attr,
20 gboolean do_path,
21 gpointer data)
22 {
23 double x, y;
24 cairo_get_current_point (cr, &x, &y);
25 cairo_translate (cr, x, y);
26
27 cairo_scale (cr,
28 (double) attr->ink_rect.width / PANGO_SCALE,
29 (double) attr->ink_rect.height / PANGO_SCALE);
30
31 switch (GPOINTER_TO_UINT (attr->data))
32 {
33 case 0x2665: /* U+2665 BLACK HEART SUIT */
34 {
35 cairo_move_to (cr, .5, .0);
36 cairo_line_to (cr, .9, -.4);
37 cairo_curve_to (cr, 1.1, -.8, .5, -.9, .5, -.5);
38 cairo_curve_to (cr, .5, -.9, -.1, -.8, .1, -.4);
39 cairo_close_path (cr);
40 }
41 break;
42 }
43
44 if (!do_path) {
45 cairo_set_source_rgb (cr, 1., 0., 0.);
46 cairo_fill (cr);
47 }
48 }
49
50 PangoAttrList *
51 create_fancy_attr_list_for_layout (PangoLayout *layout)
52 {
53 PangoAttrList *attrs;
54 PangoFontMetrics *metrics;
55 int ascent;
56 PangoRectangle ink_rect, logical_rect;
57 const char *p;
58
59 /* Get font metrics and prepare fancy shape size */
60 metrics = pango_context_get_metrics (pango_layout_get_context (layout),
61 pango_layout_get_font_description (layout),
62 NULL);
63 ascent = pango_font_metrics_get_ascent (metrics);
64 logical_rect.x = 0;
65 logical_rect.width = ascent;
66 logical_rect.y = -ascent;
67 logical_rect.height = ascent;
68 ink_rect = logical_rect;
69 pango_font_metrics_unref (metrics);
70
71 /* Set fancy shape attributes for all hearts */
72 attrs = pango_attr_list_new ();
73 for (p = text; (p = strstr (p, HEART)); p += strlen (HEART))
74 {
75 PangoAttribute *attr;
76
77 attr = pango_attr_shape_new_with_data (&ink_rect,
78 &logical_rect,
79 GUINT_TO_POINTER (g_utf8_get_char (p)),
80 NULL, NULL);
81
82 attr->start_index = p - text;
83 attr->end_index = attr->start_index + strlen (HEART);
84
85 pango_attr_list_insert (attrs, attr);
86 }
87
88 return attrs;
89 }
90
91 static gboolean
92 rotated_text_draw (GtkWidget *widget,
93 cairo_t *cr,
94 gpointer data)
95 {
96 #define RADIUS 150
97 #define N_WORDS 5
98 #define FONT "Serif 18"
99
100 PangoContext *context;
101 PangoLayout *layout;
102 PangoFontDescription *desc;
103
104 cairo_pattern_t *pattern;
105
106 PangoAttrList *attrs;
107
108 double device_radius;
109 int width, height;
110 int i;
111
112 /* Create a cairo context and set up a transformation matrix so that the user
113 * space coordinates for the centered square where we draw are [-RADIUS, RADIUS],
114 * [-RADIUS, RADIUS].
115 * We first center, then change the scale. */
116 width = gtk_widget_get_allocated_width (widget);
117 height = gtk_widget_get_allocated_height (widget);
118 device_radius = MIN (width, height) / 2.;
119 cairo_translate (cr,
120 device_radius + (width - 2 * device_radius) / 2,
121 device_radius + (height - 2 * device_radius) / 2);
122 cairo_scale (cr, device_radius / RADIUS, device_radius / RADIUS);
123
124 /* Create and a subtle gradient source and use it. */
125 pattern = cairo_pattern_create_linear (-RADIUS, -RADIUS, RADIUS, RADIUS);
126 cairo_pattern_add_color_stop_rgb (pattern, 0., .5, .0, .0);
127 cairo_pattern_add_color_stop_rgb (pattern, 1., .0, .0, .5);
128 cairo_set_source (cr, pattern);
129
130 /* Create a PangoContext and set up our shape renderer */
131 context = gtk_widget_create_pango_context (widget);
132 pango_cairo_context_set_shape_renderer (context,
133 fancy_shape_renderer,
134 NULL, NULL);
135
136 /* Create a PangoLayout, set the text, font, and attributes */
137 layout = pango_layout_new (context);
138 pango_layout_set_text (layout, text, -1);
139 desc = pango_font_description_from_string (FONT);
140 pango_layout_set_font_description (layout, desc);
141
142 attrs = create_fancy_attr_list_for_layout (layout);
143 pango_layout_set_attributes (layout, attrs);
144 pango_attr_list_unref (attrs);
145
146 /* Draw the layout N_WORDS times in a circle */
147 for (i = 0; i < N_WORDS; i++)
148 {
149 int width, height;
150
151 /* Inform Pango to re-layout the text with the new transformation matrix */
152 pango_cairo_update_layout (cr, layout);
153
154 pango_layout_get_pixel_size (layout, &width, &height);
155 cairo_move_to (cr, - width / 2, - RADIUS * .9);
156 pango_cairo_show_layout (cr, layout);
157
158 /* Rotate for the next turn */
159 cairo_rotate (cr, G_PI*2 / N_WORDS);
160 }
161
162 /* free the objects we created */
163 pango_font_description_free (desc);
164 g_object_unref (layout);
165 g_object_unref (context);
166 cairo_pattern_destroy (pattern);
167
168 return FALSE;
169 }
170
171 GtkWidget *
172 do_rotated_text (GtkWidget *do_widget)
173 {
174 static GtkWidget *window = NULL;
175
176 if (!window)
177 {
178 GtkWidget *box;
179 GtkWidget *drawing_area;
180 GtkWidget *label;
181 PangoLayout *layout;
182 PangoAttrList *attrs;
183
184 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
185 gtk_window_set_screen (GTK_WINDOW (window),
186 gtk_widget_get_screen (do_widget));
187 gtk_window_set_title (GTK_WINDOW (window), "Rotated Text");
188 gtk_window_set_default_size (GTK_WINDOW (window), 4 * RADIUS, 2 * RADIUS);
189 g_signal_connect (window, "destroy",
190 G_CALLBACK (gtk_widget_destroyed), &window);
191
192 box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
193 gtk_box_set_homogeneous (GTK_BOX (box), TRUE);
194 gtk_container_add (GTK_CONTAINER (window), box);
195
196 /* Add a drawing area */
197 drawing_area = gtk_drawing_area_new ();
198 gtk_container_add (GTK_CONTAINER (box), drawing_area);
199 gtk_style_context_add_class (gtk_widget_get_style_context (drawing_area),
200 GTK_STYLE_CLASS_VIEW);
201
202 g_signal_connect (drawing_area, "draw",
203 G_CALLBACK (rotated_text_draw), NULL);
204
205 /* And a label */
206 label = gtk_label_new (text);
207 gtk_container_add (GTK_CONTAINER (box), label);
208
209 gtk_label_set_angle (GTK_LABEL (label), 45);
210
211 /* Set up fancy stuff on the label */
212 layout = gtk_label_get_layout (GTK_LABEL (label));
213 pango_cairo_context_set_shape_renderer (pango_layout_get_context (layout),
214 fancy_shape_renderer,
215 NULL, NULL);
216 attrs = create_fancy_attr_list_for_layout (layout);
217 gtk_label_set_attributes (GTK_LABEL (label), attrs);
218 pango_attr_list_unref (attrs);
219 }
220
221 if (!gtk_widget_get_visible (window))
222 gtk_widget_show_all (window);
223 else
224 gtk_widget_destroy (window);
225
226 return window;
227 }