1
/* The GIMP -- an image manipulation program
2
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
5
* Copyright (C) 2002-2003 Sven Neumann <sven@gimp.org>
7
* This program is free software; you can redistribute it and/or modify
8
* it under the terms of the GNU General Public License as published by
9
* the Free Software Foundation; either version 2 of the License, or
10
* (at your option) any later version.
12
* This program is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
* GNU General Public License for more details.
17
* You should have received a copy of the GNU General Public License
18
* along with this program; if not, write to the Free Software
19
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24
#include <glib-object.h>
25
#include <pango/pangoft2.h>
27
#include "libgimpcolor/gimpcolor.h"
29
#include "text-types.h"
31
#include "core/gimp.h"
32
#include "core/gimpchannel.h"
33
#include "core/gimpcontext.h"
34
#include "core/gimpimage.h"
35
#include "core/gimpdrawable.h"
36
#include "core/gimpimage.h"
37
#include "core/gimpimage-undo.h"
38
#include "core/gimplayer-floating-sel.h"
40
#include "gimpfont-utils.h"
43
#include "gimptext-compat.h"
44
#include "gimptextlayer.h"
46
#include "gimp-intl.h"
50
text_render (GimpImage *gimage,
51
GimpDrawable *drawable,
55
const gchar *fontname,
60
PangoFontDescription *desc;
67
g_return_val_if_fail (GIMP_IS_IMAGE (gimage), NULL);
68
g_return_val_if_fail (drawable == NULL || GIMP_IS_DRAWABLE (drawable), NULL);
69
g_return_val_if_fail (drawable == NULL ||
70
gimp_item_is_attached (GIMP_ITEM (drawable)), NULL);
71
g_return_val_if_fail (GIMP_IS_CONTEXT (context), NULL);
72
g_return_val_if_fail (fontname != NULL, NULL);
73
g_return_val_if_fail (text != NULL, NULL);
78
desc = pango_font_description_from_string (fontname);
79
size = PANGO_PIXELS (pango_font_description_get_size (desc));
81
pango_font_description_unset_fields (desc, PANGO_FONT_MASK_SIZE);
82
font = gimp_font_util_pango_font_description_to_string (desc);
84
pango_font_description_free (desc);
86
gimp_context_get_foreground (context, &color);
88
gtext = g_object_new (GIMP_TYPE_TEXT,
92
"antialias", antialias,
99
layer = gimp_text_layer_new (gimage, gtext);
101
g_object_unref (gtext);
106
/* Start a group undo */
107
gimp_image_undo_group_start (gimage, GIMP_UNDO_GROUP_TEXT,
108
_("Add Text Layer"));
110
/* Set the layer offsets */
111
GIMP_ITEM (layer)->offset_x = text_x;
112
GIMP_ITEM (layer)->offset_y = text_y;
114
/* If there is a selection mask clear it--
115
* this might not always be desired, but in general,
116
* it seems like the correct behavior.
118
if (! gimp_channel_is_empty (gimp_image_get_mask (gimage)))
119
gimp_channel_clear (gimp_image_get_mask (gimage), NULL, TRUE);
121
/* If the drawable is NULL, create a new layer */
122
if (drawable == NULL)
123
gimp_image_add_layer (gimage, layer, -1);
124
/* Otherwise, instantiate the text as the new floating selection */
126
floating_sel_attach (layer, drawable);
128
/* end the group undo */
129
gimp_image_undo_group_end (gimage);
135
text_get_extents (const gchar *fontname,
142
PangoFontDescription *font_desc;
143
PangoContext *context;
145
PangoFontMap *fontmap;
148
g_return_val_if_fail (fontname != NULL, FALSE);
149
g_return_val_if_fail (text != NULL, FALSE);
151
/* FIXME: resolution */
152
fontmap = pango_ft2_font_map_new ();
153
pango_ft2_font_map_set_resolution (PANGO_FT2_FONT_MAP (fontmap), 72.0, 72.0);
154
context = pango_ft2_font_map_create_context (PANGO_FT2_FONT_MAP (fontmap));
155
g_object_unref (fontmap);
157
layout = pango_layout_new (context);
158
g_object_unref (context);
160
font_desc = pango_font_description_from_string (fontname);
161
pango_layout_set_font_description (layout, font_desc);
162
pango_font_description_free (font_desc);
164
pango_layout_set_text (layout, text, -1);
166
pango_layout_get_pixel_extents (layout, NULL, &rect);
171
*height = rect.height;
173
if (ascent || descent)
175
PangoLayoutIter *iter;
176
PangoLayoutLine *line;
178
iter = pango_layout_get_iter (layout);
179
line = pango_layout_iter_get_line (iter);
180
pango_layout_iter_free (iter);
182
pango_layout_line_get_pixel_extents (line, NULL, &rect);
185
*ascent = PANGO_ASCENT (rect);
187
*descent = - PANGO_DESCENT (rect);
190
g_object_unref (layout);