~ubuntu-branches/ubuntu/maverick/gimp/maverick-updates

« back to all changes in this revision

Viewing changes to app/text/gimptext-compat.c

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Holbach
  • Date: 2005-12-09 19:44:52 UTC
  • Revision ID: james.westby@ubuntu.com-20051209194452-yggpemjlofpjqyf4
Tags: upstream-2.2.9
ImportĀ upstreamĀ versionĀ 2.2.9

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* The GIMP -- an image manipulation program
 
2
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 
3
 *
 
4
 * GimpText
 
5
 * Copyright (C) 2002-2003  Sven Neumann <sven@gimp.org>
 
6
 *
 
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.
 
11
 *
 
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.
 
16
 *
 
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.
 
20
 */
 
21
 
 
22
#include "config.h"
 
23
 
 
24
#include <glib-object.h>
 
25
#include <pango/pangoft2.h>
 
26
 
 
27
#include "libgimpcolor/gimpcolor.h"
 
28
 
 
29
#include "text-types.h"
 
30
 
 
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"
 
39
 
 
40
#include "gimpfont-utils.h"
 
41
 
 
42
#include "gimptext.h"
 
43
#include "gimptext-compat.h"
 
44
#include "gimptextlayer.h"
 
45
 
 
46
#include "gimp-intl.h"
 
47
 
 
48
 
 
49
GimpLayer *
 
50
text_render (GimpImage    *gimage,
 
51
             GimpDrawable *drawable,
 
52
             GimpContext  *context,
 
53
             gint          text_x,
 
54
             gint          text_y,
 
55
             const gchar  *fontname,
 
56
             const gchar  *text,
 
57
             gint          border,
 
58
             gboolean      antialias)
 
59
{
 
60
  PangoFontDescription *desc;
 
61
  GimpText             *gtext;
 
62
  GimpLayer            *layer;
 
63
  GimpRGB               color;
 
64
  gchar                *font;
 
65
  gdouble               size;
 
66
 
 
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);
 
74
 
 
75
  if (border < 0)
 
76
    border = 0;
 
77
 
 
78
  desc = pango_font_description_from_string (fontname);
 
79
  size = PANGO_PIXELS (pango_font_description_get_size (desc));
 
80
 
 
81
  pango_font_description_unset_fields (desc, PANGO_FONT_MASK_SIZE);
 
82
  font = gimp_font_util_pango_font_description_to_string (desc);
 
83
 
 
84
  pango_font_description_free (desc);
 
85
 
 
86
  gimp_context_get_foreground (context, &color);
 
87
 
 
88
  gtext = g_object_new (GIMP_TYPE_TEXT,
 
89
                        "text",      text,
 
90
                        "font",      font,
 
91
                        "font-size", size,
 
92
                        "antialias", antialias,
 
93
                        "border",    border,
 
94
                        "color",     &color,
 
95
                        NULL);
 
96
 
 
97
  g_free (font);
 
98
 
 
99
  layer = gimp_text_layer_new (gimage, gtext);
 
100
 
 
101
  g_object_unref (gtext);
 
102
 
 
103
  if (!layer)
 
104
    return NULL;
 
105
 
 
106
  /*  Start a group undo  */
 
107
  gimp_image_undo_group_start (gimage, GIMP_UNDO_GROUP_TEXT,
 
108
                               _("Add Text Layer"));
 
109
 
 
110
  /*  Set the layer offsets  */
 
111
  GIMP_ITEM (layer)->offset_x = text_x;
 
112
  GIMP_ITEM (layer)->offset_y = text_y;
 
113
 
 
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.
 
117
   */
 
118
  if (! gimp_channel_is_empty (gimp_image_get_mask (gimage)))
 
119
    gimp_channel_clear (gimp_image_get_mask (gimage), NULL, TRUE);
 
120
 
 
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 */
 
125
  else
 
126
    floating_sel_attach (layer, drawable);
 
127
 
 
128
  /*  end the group undo  */
 
129
  gimp_image_undo_group_end (gimage);
 
130
 
 
131
  return layer;
 
132
}
 
133
 
 
134
gboolean
 
135
text_get_extents (const gchar *fontname,
 
136
                  const gchar *text,
 
137
                  gint        *width,
 
138
                  gint        *height,
 
139
                  gint        *ascent,
 
140
                  gint        *descent)
 
141
{
 
142
  PangoFontDescription *font_desc;
 
143
  PangoContext         *context;
 
144
  PangoLayout          *layout;
 
145
  PangoFontMap         *fontmap;
 
146
  PangoRectangle        rect;
 
147
 
 
148
  g_return_val_if_fail (fontname != NULL, FALSE);
 
149
  g_return_val_if_fail (text != NULL, FALSE);
 
150
 
 
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);
 
156
 
 
157
  layout = pango_layout_new (context);
 
158
  g_object_unref (context);
 
159
 
 
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);
 
163
 
 
164
  pango_layout_set_text (layout, text, -1);
 
165
 
 
166
  pango_layout_get_pixel_extents (layout, NULL, &rect);
 
167
 
 
168
  if (width)
 
169
    *width = rect.width;
 
170
  if (height)
 
171
    *height = rect.height;
 
172
 
 
173
  if (ascent || descent)
 
174
    {
 
175
      PangoLayoutIter *iter;
 
176
      PangoLayoutLine *line;
 
177
 
 
178
      iter = pango_layout_get_iter (layout);
 
179
      line = pango_layout_iter_get_line (iter);
 
180
      pango_layout_iter_free (iter);
 
181
 
 
182
      pango_layout_line_get_pixel_extents (line, NULL, &rect);
 
183
 
 
184
      if (ascent)
 
185
        *ascent = PANGO_ASCENT (rect);
 
186
      if (descent)
 
187
        *descent = - PANGO_DESCENT (rect);
 
188
    }
 
189
 
 
190
  g_object_unref (layout);
 
191
 
 
192
  return TRUE;
 
193
}