1 /* GDK - The GIMP Drawing Kit
2 * Copyright (C) 2000 Red Hat, Inc.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library 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.
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 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library 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.
23 #include "gdkprivate.h"
25 #define GDK_INFO_KEY "gdk-info"
27 typedef struct _GdkPangoContextInfo GdkPangoContextInfo;
29 struct _GdkPangoContextInfo
31 GdkColormap *colormap;
34 static void gdk_pango_get_item_properties (PangoItem *item,
35 PangoUnderline *uline,
36 PangoAttrColor *fg_color,
38 PangoAttrColor *bg_color,
42 gdk_pango_context_destroy (GdkPangoContextInfo *info)
44 gdk_colormap_unref (info->colormap);
48 static GdkPangoContextInfo *
49 gdk_pango_context_get_info (PangoContext *context, gboolean create)
51 GdkPangoContextInfo *info = pango_context_get_data (context, GDK_INFO_KEY);
54 info = g_new (GdkPangoContextInfo, 1);
55 info->colormap = NULL;
57 pango_context_set_data (context, GDK_INFO_KEY,
58 info, (GDestroyNotify)gdk_pango_context_destroy);
65 gdk_pango_get_gc (PangoContext *context,
66 PangoAttrColor *fg_color,
69 GdkPangoContextInfo *info;
70 GdkColormap *colormap;
73 g_return_val_if_fail (context != NULL, NULL);
75 info = gdk_pango_context_get_info (context, FALSE);
77 if (info && info->colormap)
78 colormap = info->colormap;
80 colormap = gdk_colormap_get_system();
82 /* FIXME. FIXME. FIXME. Only works for true color */
84 color.red = fg_color->red;
85 color.green = fg_color->green;
86 color.blue = fg_color->blue;
88 if (gdk_colormap_alloc_color (colormap, &color, FALSE, TRUE))
90 GdkGC *result = gdk_gc_new (gdk_parent_root);
91 gdk_gc_copy (result, base_gc);
92 gdk_gc_set_foreground (result, &color);
97 return gdk_gc_ref (base_gc);
101 gdk_pango_free_gc (PangoContext *context,
108 gdk_pango_context_set_colormap (PangoContext *context,
109 GdkColormap *colormap)
111 GdkPangoContextInfo *info;
113 g_return_if_fail (context != NULL);
115 info = gdk_pango_context_get_info (context, TRUE);
116 g_return_if_fail (info != NULL);
118 if (info->colormap != colormap)
121 gdk_colormap_unref (info->colormap);
123 info->colormap = colormap;
126 gdk_colormap_ref (info->colormap);
132 * gdk_draw_layout_line:
133 * @drawable: the drawable on which to draw the line
134 * @gc: base graphics to use
135 * @x: the x position of start of string (in pixels)
136 * @y: the y position of baseline (in pixels)
137 * @line: a #PangoLayoutLine
139 * Render a #PangoLayoutLine onto an GDK drawable
142 gdk_draw_layout_line (GdkDrawable *drawable,
146 PangoLayoutLine *line)
148 GSList *tmp_list = line->runs;
149 PangoRectangle overall_rect;
150 PangoRectangle logical_rect;
151 PangoRectangle ink_rect;
152 PangoContext *context;
155 g_return_if_fail (drawable != NULL);
156 g_return_if_fail (gc != NULL);
157 g_return_if_fail (line != NULL);
159 if (GDK_DRAWABLE_DESTROYED (drawable))
162 context = pango_layout_get_context (line->layout);
164 pango_layout_line_get_extents (line,NULL, &overall_rect);
168 PangoUnderline uline = PANGO_UNDERLINE_NONE;
169 PangoLayoutRun *run = tmp_list->data;
170 PangoAttrColor fg_color, bg_color;
171 gboolean fg_set, bg_set;
174 tmp_list = tmp_list->next;
176 gdk_pango_get_item_properties (run->item, &uline, &fg_color, &fg_set, &bg_color, &bg_set);
179 fg_gc = gdk_pango_get_gc (context, &fg_color, gc);
183 if (uline == PANGO_UNDERLINE_NONE)
184 pango_glyph_string_extents (run->glyphs, run->item->analysis.font,
185 NULL, &logical_rect);
187 pango_glyph_string_extents (run->glyphs, run->item->analysis.font,
188 &ink_rect, &logical_rect);
192 GdkGC *bg_gc = gdk_pango_get_gc (context, &bg_color, gc);
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);
200 gdk_pango_free_gc (context, bg_gc);
203 gdk_draw_glyphs (drawable, fg_gc, run->item->analysis.font,
204 x + x_off / PANGO_SCALE, y, run->glyphs);
208 case PANGO_UNDERLINE_NONE:
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);
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);
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);
228 gdk_pango_free_gc (context, fg_gc);
230 x_off += logical_rect.width;
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
242 * Render a #PangoLayout onto a GDK drawable
245 gdk_draw_layout (GdkDrawable *drawable,
251 PangoRectangle logical_rect;
253 PangoAlignment align;
257 gboolean first = FALSE;
259 g_return_if_fail (drawable != NULL);
260 g_return_if_fail (gc != NULL);
261 g_return_if_fail (layout != NULL);
263 if (GDK_DRAWABLE_DESTROYED (drawable))
266 g_return_if_fail (layout != NULL);
268 indent = pango_layout_get_indent (layout);
269 width = pango_layout_get_width (layout);
270 align = pango_layout_get_alignment (layout);
272 if (width == -1 && align != PANGO_ALIGN_LEFT)
274 pango_layout_get_extents (layout, NULL, &logical_rect);
275 width = logical_rect.width;
278 tmp_list = pango_layout_get_lines (layout);
281 PangoLayoutLine *line = tmp_list->data;
284 pango_layout_line_get_extents (line, NULL, &logical_rect);
286 if (width != 1 && align == PANGO_ALIGN_RIGHT)
287 x_offset = width - logical_rect.width;
288 else if (width != 1 && align == PANGO_ALIGN_CENTER)
289 x_offset = (width - logical_rect.width) / 2;
297 if (align == PANGO_ALIGN_LEFT)
309 if (align == PANGO_ALIGN_LEFT)
316 gdk_draw_layout_line (drawable, gc,
317 x + x_offset / PANGO_SCALE, y + (y_offset - logical_rect.y) / PANGO_SCALE,
320 y_offset += logical_rect.height;
321 tmp_list = tmp_list->next;
326 gdk_pango_get_item_properties (PangoItem *item,
327 PangoUnderline *uline,
328 PangoAttrColor *fg_color,
330 PangoAttrColor *bg_color,
333 GSList *tmp_list = item->extra_attrs;
343 PangoAttribute *attr = tmp_list->data;
345 switch (attr->klass->type)
347 case PANGO_ATTR_UNDERLINE:
349 *uline = ((PangoAttrInt *)attr)->value;
352 case PANGO_ATTR_FOREGROUND:
354 *fg_color = *((PangoAttrColor *)attr);
360 case PANGO_ATTR_BACKGROUND:
362 *bg_color = *((PangoAttrColor *)attr);
371 tmp_list = tmp_list->next;