summaryrefslogtreecommitdiff
path: root/docs/reference/gdk/tmpl/pango_interaction.sgml
blob: 16ee04a5f7d886d34c03642c37f65bf61bdd5e0e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
<!-- ##### SECTION Title ##### -->
Pango Interaction

<!-- ##### SECTION Short_Description ##### -->
Using Pango in GDK

<!-- ##### SECTION Long_Description ##### -->
<para>
Pango is the text layout system used by GDK and GTK+. The functions
and types in this section are used to render Pango objects to GDK.
drawables, and also extend the set of Pango attributes to include
stippling and embossing.
</para>
<para>
Creating a #PangoLayout object is the first step in rendering text,
and requires getting a handle to a #PangoContext. For GTK+ programs,
you'll usually want to use gtk_widget_get_context(), or
gtk_widget_create_pango_layout(), rather than using the lowlevel
gdk_pango_context_get_for_screen(). Once you have a #PangoLayout, you
can set the text and attributes of it with Pango functions like
pango_layout_set_text() and get its size with pango_layout_get_size().
(Note that Pango uses a fixed point system internally, so converting
between Pango units and pixels using <link
linkend="PANGO-SCALE-CAPS">PANGO_SCALE</link> or the PANGO_PIXELS() macro.)
</para>
<para>
Rendering a Pango layout is done most simply with gdk_draw_layout();
you can also draw pieces of the layout with gdk_draw_layout() or
gdk_draw_glyphs(). #GdkPangoRenderer is a subclass of #PangoRenderer
that is used internally to implement these functions. Using it
directly or subclassing it can be useful in some cases. See the
#GdkPangoRenderer documentation for details.
</para>
<example id="rotated-example">
<title>Using #GdkPangoRenderer to draw transformed text</title>
<!-- Note that this example is basically the same as
     demos/gtk-demo/rotated_text.c -->
<programlisting>
#define RADIUS 100
#define N_WORDS 10
#define FONT "Sans Bold 18"
  
GdkScreen *screen = gdk_drawable_get_screen (drawable);
PangoRenderer *renderer;
GdkGC *gc;

PangoMatrix matrix = PANGO_MATRIX_INIT;
PangoContext *context;
PangoLayout *layout;
PangoFontDescription *desc;

double device_radius;
int width, height;
int i;

/* Get the default renderer for the screen, and set it up for drawing  */
renderer = gdk_pango_renderer_get_default (screen);
gdk_pango_renderer_set_drawable (GDK_PANGO_RENDERER (renderer), drawable);

gc = gdk_gc_new (drawable);
gdk_pango_renderer_set_gc (GDK_PANGO_RENDERER (renderer), gc);

/* Set up a transformation matrix so that the user space coordinates for
 * where we are drawing are [-RADIUS, RADIUS], [-RADIUS, RADIUS]
 * We first center, then change the scale */
gdk_drawable_get_size (drawable, &amp;width, &amp;height);
device_radius = MIN (width, height) / 2.;

pango_matrix_translate (&amp;matrix,
                        device_radius + (width - 2 * device_radius) / 2,
                        device_radius + (height - 2 * device_radius) / 2);
pango_matrix_scale (&amp;matrix, device_radius / RADIUS, device_radius / RADIUS);

/* Create a PangoLayout, set the font and text */
context = gdk_pango_context_get_for_screen (screen);
layout = pango_layout_new (context);
pango_layout_set_text (layout, "Text", -1);
desc = pango_font_description_from_string (FONT);
pango_layout_set_font_description (layout, desc);
pango_font_description_free (desc);

/* Draw the layout N_WORDS times in a circle */
for (i = 0; i &lt; N_WORDS; i++)
  {
    GdkColor color;
    PangoMatrix rotated_matrix = matrix;
    int width, height;
    double angle = (360. * i) / N_WORDS;

    /* Gradient from red at angle == 60 to blue at angle == 300 */
    color.red   = 65535 * (1 + cos ((angle - 60) * M_PI / 180.)) / 2;
    color.green = 0;
    color.blue  = 65535  - color.red;
    
    gdk_pango_renderer_set_override_color (GDK_PANGO_RENDERER (renderer),
                                           PANGO_RENDER_PART_FOREGROUND, &amp;color);
                                             
    pango_matrix_rotate (&amp;rotated_matrix, angle);

    pango_context_set_matrix (context, &amp;rotated_matrix);
    
    /* Inform Pango to re-layout the text with the new transformation matrix */
    pango_layout_context_changed (layout);
    
    pango_layout_get_size (layout, &amp;width, &amp;height);
    pango_renderer_draw_layout (renderer, layout,
                                - width / 2, - RADIUS * PANGO_SCALE);
  }

/* Clean up default renderer, since it is shared */
gdk_pango_renderer_set_override_color (GDK_PANGO_RENDERER (renderer),
                                       PANGO_RENDER_PART_FOREGROUND, NULL);
gdk_pango_renderer_set_drawable (GDK_PANGO_RENDERER (renderer), NULL);
gdk_pango_renderer_set_gc (GDK_PANGO_RENDERER (renderer), NULL);

/* free the objects we created */
g_object_unref (layout);
g_object_unref (context);
g_object_unref (gc);
</programlisting>
</example>
<figure>
  <title>Output of <xref linkend="rotated-example"/></title>
  <graphic fileref="rotated-text.png" format="PNG"/>
</figure>

<!-- ##### SECTION See_Also ##### -->
<para>

</para>

<!-- ##### STRUCT GdkPangoRenderer ##### -->
<para>

</para>


<!-- ##### ARG GdkPangoRenderer:screen ##### -->
<para>

</para>

<!-- ##### STRUCT GdkPangoRendererClass ##### -->
<para>

</para>


<!-- ##### FUNCTION gdk_pango_renderer_new ##### -->
<para>

</para>

@screen: 
@Returns: 


<!-- ##### FUNCTION gdk_pango_renderer_get_default ##### -->
<para>

</para>

@screen: 
@Returns: 


<!-- ##### FUNCTION gdk_pango_renderer_set_drawable ##### -->
<para>

</para>

@gdk_renderer: 
@drawable: 


<!-- ##### FUNCTION gdk_pango_renderer_set_gc ##### -->
<para>

</para>

@gdk_renderer: 
@gc: 


<!-- ##### FUNCTION gdk_pango_renderer_set_stipple ##### -->
<para>

</para>

@gdk_renderer: 
@part: 
@stipple: 


<!-- ##### FUNCTION gdk_pango_renderer_set_override_color ##### -->
<para>

</para>

@gdk_renderer: 
@part: 
@color: 


<!-- ##### FUNCTION gdk_pango_context_get ##### -->
<para>

</para>

@Returns: 


<!-- ##### FUNCTION gdk_pango_context_get_for_screen ##### -->
<para>

</para>

@screen: 
@Returns: 


<!-- ##### FUNCTION gdk_pango_context_set_colormap ##### -->
<para>

</para>

@context: 
@colormap: 


<!-- ##### STRUCT GdkPangoAttrEmbossed ##### -->
<para>
A Pango text attribute containing a embossed bitmap to be used when
rendering the text.
</para>

@attr: the #PangoAttribute.
@embossed: the embossed bitmap.

<!-- ##### STRUCT GdkPangoAttrStipple ##### -->
<para>
A Pango text attribute containing a stipple bitmap to be used when
rendering the text.
</para>

@attr: the #PangoAttribute.
@stipple: the stipple bitmap.

<!-- ##### FUNCTION gdk_pango_attr_embossed_new ##### -->
<para>

</para>

@embossed: 
@Returns: 


<!-- ##### FUNCTION gdk_pango_attr_stipple_new ##### -->
<para>

</para>

@stipple: 
@Returns: 


<!-- ##### FUNCTION gdk_pango_layout_get_clip_region ##### -->
<para>

</para>

@layout: 
@x_origin: 
@y_origin: 
@index_ranges: 
@n_ranges: 
@Returns: 


<!-- ##### FUNCTION gdk_pango_layout_line_get_clip_region ##### -->
<para>

</para>

@line: 
@x_origin: 
@y_origin: 
@index_ranges: 
@n_ranges: 
@Returns: