]> Pileus Git - ~andy/gtk/blob - gdk/gdkpango.c
applied patch from Andreas Persenius <ndap@swipnet.se> that updates the
[~andy/gtk] / gdk / gdkpango.c
1 /* GDK - The GIMP Drawing Kit
2  * Copyright (C) 2000 Red Hat, Inc. 
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 #include "gdkcolor.h"
21 #include "gdkgc.h"
22 #include "gdkpango.h"
23 #include "gdkprivate.h"
24
25 #define GDK_INFO_KEY "gdk-info"
26
27 typedef struct _GdkPangoContextInfo GdkPangoContextInfo;
28
29 struct _GdkPangoContextInfo
30 {
31   GdkColormap *colormap;
32 };
33
34 static void gdk_pango_get_item_properties (PangoItem      *item,
35                                            PangoUnderline *uline,
36                                            PangoAttrColor *fg_color,
37                                            gboolean       *fg_set,
38                                            PangoAttrColor *bg_color,
39                                            gboolean       *bg_set);
40
41 static void
42 gdk_pango_context_destroy (GdkPangoContextInfo *info)
43 {
44   gdk_colormap_unref (info->colormap);
45   g_free (info);
46 }
47
48 static GdkPangoContextInfo *
49 gdk_pango_context_get_info (PangoContext *context, gboolean create)
50 {
51   GdkPangoContextInfo *info =
52     g_object_get_qdata (G_OBJECT (context),
53                         g_quark_try_string (GDK_INFO_KEY));
54   if (!info && create)
55     {
56       info = g_new (GdkPangoContextInfo, 1);
57       info->colormap = NULL;
58
59       g_object_set_qdata_full (G_OBJECT (context),
60                                g_quark_from_static_string (GDK_INFO_KEY),
61                                info, (GDestroyNotify)gdk_pango_context_destroy);
62     }
63
64   return info;
65 }
66
67 static GdkGC *
68 gdk_pango_get_gc (PangoContext   *context,
69                   PangoAttrColor *fg_color,
70                   GdkGC          *base_gc)
71 {
72   GdkPangoContextInfo *info;
73   GdkColormap *colormap;
74   GdkColor color;
75   
76   g_return_val_if_fail (context != NULL, NULL);
77
78   info = gdk_pango_context_get_info (context, FALSE);
79
80   if (info && info->colormap)
81     colormap = info->colormap;
82   else
83     colormap = gdk_colormap_get_system();
84
85   /* FIXME. FIXME. FIXME. Only works for true color */
86
87   color.red = fg_color->red;
88   color.green = fg_color->green;
89   color.blue = fg_color->blue;
90   
91   if (gdk_colormap_alloc_color (colormap, &color, FALSE, TRUE))
92     {
93       GdkGC *result = gdk_gc_new (gdk_parent_root);
94       gdk_gc_copy (result, base_gc);
95       gdk_gc_set_foreground (result, &color);
96
97       return result;
98     }
99   else
100     return gdk_gc_ref (base_gc);
101 }
102
103 static void
104 gdk_pango_free_gc (PangoContext *context,
105                    GdkGC        *gc)
106 {
107   gdk_gc_unref (gc);
108 }
109
110 void
111 gdk_pango_context_set_colormap (PangoContext *context,
112                                 GdkColormap  *colormap)
113 {
114   GdkPangoContextInfo *info;
115   
116   g_return_if_fail (context != NULL);
117
118   info = gdk_pango_context_get_info (context, TRUE);
119   g_return_if_fail (info != NULL);
120   
121   if (info->colormap != colormap)
122     {
123       if (info->colormap)
124         gdk_colormap_unref (info->colormap);
125
126       info->colormap = colormap;
127       
128       if (info->colormap)
129         gdk_colormap_ref (info->colormap);
130     }
131 }
132
133
134 /**
135  * gdk_draw_layout_line:
136  * @drawable:  the drawable on which to draw the line
137  * @gc:        base graphics to use
138  * @x:         the x position of start of string (in pixels)
139  * @y:         the y position of baseline (in pixels)
140  * @line:      a #PangoLayoutLine
141  *
142  * Render a #PangoLayoutLine onto an GDK drawable
143  */
144 void 
145 gdk_draw_layout_line (GdkDrawable      *drawable,
146                       GdkGC            *gc,
147                       gint              x, 
148                       gint              y,
149                       PangoLayoutLine  *line)
150 {
151   GSList *tmp_list = line->runs;
152   PangoRectangle overall_rect;
153   PangoRectangle logical_rect;
154   PangoRectangle ink_rect;
155   PangoContext *context;
156   gint x_off = 0;
157
158   g_return_if_fail (drawable != NULL);
159   g_return_if_fail (gc != NULL);
160   g_return_if_fail (line != NULL);
161
162   context = pango_layout_get_context (line->layout);
163   
164   pango_layout_line_get_extents (line,NULL, &overall_rect);
165   
166   while (tmp_list)
167     {
168       PangoUnderline uline = PANGO_UNDERLINE_NONE;
169       PangoLayoutRun *run = tmp_list->data;
170       PangoAttrColor fg_color, bg_color;
171       gboolean fg_set, bg_set;
172       GdkGC *fg_gc;
173       
174       tmp_list = tmp_list->next;
175
176       gdk_pango_get_item_properties (run->item, &uline, &fg_color, &fg_set, &bg_color, &bg_set);
177
178       if (fg_set)
179         fg_gc = gdk_pango_get_gc (context, &fg_color, gc);
180       else
181         fg_gc = gc;
182
183       if (uline == PANGO_UNDERLINE_NONE)
184         pango_glyph_string_extents (run->glyphs, run->item->analysis.font,
185                                     NULL, &logical_rect);
186       else
187         pango_glyph_string_extents (run->glyphs, run->item->analysis.font,
188                                     &ink_rect, &logical_rect);
189
190       if (bg_set)
191         {
192           GdkGC *bg_gc = gdk_pango_get_gc (context, &bg_color, gc);
193
194           gdk_draw_rectangle (drawable, bg_gc, TRUE,
195                               x + (x_off + logical_rect.x) / PANGO_SCALE,
196                               y + overall_rect.y / PANGO_SCALE,
197                               logical_rect.width / PANGO_SCALE,
198                               overall_rect.height / PANGO_SCALE);
199
200           gdk_pango_free_gc (context, bg_gc);
201         }
202
203       gdk_draw_glyphs (drawable, fg_gc, run->item->analysis.font,
204                       x + x_off / PANGO_SCALE, y, run->glyphs);
205
206       switch (uline)
207         {
208         case PANGO_UNDERLINE_NONE:
209           break;
210         case PANGO_UNDERLINE_DOUBLE:
211           gdk_draw_line (drawable, fg_gc,
212                          x + (x_off + ink_rect.x) / PANGO_SCALE - 1, y + 4,
213                          x + (x_off + ink_rect.x + ink_rect.width) / PANGO_SCALE, y + 4);
214           /* Fall through */
215         case PANGO_UNDERLINE_SINGLE:
216           gdk_draw_line (drawable, fg_gc,
217                          x + (x_off + ink_rect.x) / PANGO_SCALE - 1, y + 2,
218                          x + (x_off + ink_rect.x + ink_rect.width) / PANGO_SCALE, y + 2);
219           break;
220         case PANGO_UNDERLINE_LOW:
221           gdk_draw_line (drawable, fg_gc,
222                          x + (x_off + ink_rect.x) / PANGO_SCALE - 1, y + (ink_rect.y + ink_rect.height) / PANGO_SCALE + 2,
223                          x + (x_off + ink_rect.x + ink_rect.width) / PANGO_SCALE, y + (ink_rect.y + ink_rect.height) / PANGO_SCALE + 2);
224           break;
225         }
226
227       if (fg_set)
228         gdk_pango_free_gc (context, fg_gc);
229       
230       x_off += logical_rect.width;
231     }
232 }
233
234 /**
235  * gdk_draw_layout:
236  * @drawable:  the drawable on which to draw string
237  * @gc:        base graphics context to use
238  * @x:         the X position of the left of the layout (in pixels)
239  * @y:         the Y position of the top of the layout (in pixels)
240  * @layout:    a #PangoLayout
241  *
242  * Render a #PangoLayout onto a GDK drawable
243  */
244 void 
245 gdk_draw_layout (GdkDrawable     *drawable,
246                  GdkGC           *gc,
247                  int              x, 
248                  int              y,
249                  PangoLayout     *layout)
250 {
251   PangoRectangle logical_rect;
252   GSList *tmp_list;
253   PangoAlignment align;
254   gint indent;
255   gint width;
256   gint y_offset = 0;
257   gboolean first = FALSE;
258   
259   g_return_if_fail (drawable != NULL);
260   g_return_if_fail (gc != NULL);
261   g_return_if_fail (layout != NULL);
262
263   indent = pango_layout_get_indent (layout);
264   width = pango_layout_get_width (layout);
265   align = pango_layout_get_alignment (layout);
266
267   if (width == -1 && align != PANGO_ALIGN_LEFT)
268     {
269       pango_layout_get_extents (layout, NULL, &logical_rect);
270       width = logical_rect.width;
271     }
272   
273   tmp_list = pango_layout_get_lines (layout);
274   while (tmp_list)
275     {
276       PangoLayoutLine *line = tmp_list->data;
277       int x_offset;
278       
279       pango_layout_line_get_extents (line, NULL, &logical_rect);
280
281       if (width != 1 && align == PANGO_ALIGN_RIGHT)
282         x_offset = width - logical_rect.width;
283       else if (width != 1 && align == PANGO_ALIGN_CENTER)
284         x_offset = (width - logical_rect.width) / 2;
285       else
286         x_offset = 0;
287
288       if (first)
289         {
290           if (indent > 0)
291             {
292               if (align == PANGO_ALIGN_LEFT)
293                 x_offset += indent;
294               else
295                 x_offset -= indent;
296             }
297
298           first = FALSE;
299         }
300       else
301         {
302           if (indent < 0)
303             {
304               if (align == PANGO_ALIGN_LEFT)
305                 x_offset -= indent;
306               else
307                 x_offset += indent;
308             }
309         }
310           
311       gdk_draw_layout_line (drawable, gc,
312                             x + x_offset / PANGO_SCALE, y + (y_offset - logical_rect.y) / PANGO_SCALE,
313                             line);
314
315       y_offset += logical_rect.height;
316       tmp_list = tmp_list->next;
317     }
318 }
319
320 static void
321 gdk_pango_get_item_properties (PangoItem      *item,
322                                PangoUnderline *uline,
323                                PangoAttrColor *fg_color,
324                                gboolean       *fg_set,
325                                PangoAttrColor *bg_color,
326                                gboolean       *bg_set)
327 {
328   GSList *tmp_list = item->extra_attrs;
329
330   if (fg_set)
331     *fg_set = FALSE;
332   
333   if (bg_set)
334     *bg_set = FALSE;
335   
336   while (tmp_list)
337     {
338       PangoAttribute *attr = tmp_list->data;
339
340       switch (attr->klass->type)
341         {
342         case PANGO_ATTR_UNDERLINE:
343           if (uline)
344             *uline = ((PangoAttrInt *)attr)->value;
345           break;
346           
347         case PANGO_ATTR_FOREGROUND:
348           if (fg_color)
349             *fg_color = *((PangoAttrColor *)attr);
350           if (fg_set)
351             *fg_set = TRUE;
352           
353           break;
354           
355         case PANGO_ATTR_BACKGROUND:
356           if (bg_color)
357             *bg_color = *((PangoAttrColor *)attr);
358           if (bg_set)
359             *bg_set = TRUE;
360           
361           break;
362           
363         default:
364           break;
365         }
366       tmp_list = tmp_list->next;
367     }
368 }
369