~ubuntu-branches/ubuntu/hoary/gimp/hoary

« back to all changes in this revision

Viewing changes to app/text/gimptextlayout.c

  • Committer: Bazaar Package Importer
  • Author(s): Sebastien Bacher
  • Date: 2005-04-04 14:51:23 UTC
  • Revision ID: james.westby@ubuntu.com-20050404145123-9py049eeelfymur8
Tags: upstream-2.2.2
ImportĀ upstreamĀ versionĀ 2.2.2

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 "text-types.h"
 
28
 
 
29
#include "core/gimpimage.h"
 
30
#include "core/gimpunit.h"
 
31
 
 
32
#include "gimptext.h"
 
33
#include "gimptext-private.h"
 
34
#include "gimptextlayout.h"
 
35
 
 
36
 
 
37
static void   gimp_text_layout_class_init  (GimpTextLayoutClass *klass);
 
38
static void   gimp_text_layout_init        (GimpTextLayout      *layout);
 
39
static void   gimp_text_layout_finalize    (GObject             *object);
 
40
 
 
41
static void   gimp_text_layout_position    (GimpTextLayout      *layout);
 
42
 
 
43
static PangoContext * gimp_text_get_pango_context (GimpText     *text,
 
44
                                                   gdouble       xres,
 
45
                                                   gdouble       yres);
 
46
 
 
47
static gint   gimp_text_layout_pixel_size         (Gimp         *gimp,
 
48
                                                   gdouble       value,
 
49
                                                   GimpUnit      unit,
 
50
                                                   gdouble       res);
 
51
static gint   gimp_text_layout_point_size         (Gimp         *gimp,
 
52
                                                   gdouble       value,
 
53
                                                   GimpUnit      unit,
 
54
                                                   gdouble       res);
 
55
 
 
56
 
 
57
static GObjectClass * parent_class = NULL;
 
58
 
 
59
 
 
60
GType
 
61
gimp_text_layout_get_type (void)
 
62
{
 
63
  static GType layout_type = 0;
 
64
 
 
65
  if (! layout_type)
 
66
    {
 
67
      static const GTypeInfo layout_info =
 
68
      {
 
69
        sizeof (GimpTextLayoutClass),
 
70
        (GBaseInitFunc) NULL,
 
71
        (GBaseFinalizeFunc) NULL,
 
72
        (GClassInitFunc) gimp_text_layout_class_init,
 
73
        NULL,           /* class_finalize */
 
74
        NULL,           /* class_data     */
 
75
        sizeof (GimpTextLayout),
 
76
        0,              /* n_preallocs    */
 
77
        (GInstanceInitFunc) gimp_text_layout_init,
 
78
      };
 
79
 
 
80
      layout_type = g_type_register_static (G_TYPE_OBJECT,
 
81
                                            "GimpTextLayout",
 
82
                                            &layout_info, 0);
 
83
    }
 
84
 
 
85
  return layout_type;
 
86
}
 
87
 
 
88
static void
 
89
gimp_text_layout_class_init (GimpTextLayoutClass *klass)
 
90
{
 
91
  GObjectClass *object_class;
 
92
 
 
93
  object_class = G_OBJECT_CLASS (klass);
 
94
 
 
95
  parent_class = g_type_class_peek_parent (klass);
 
96
 
 
97
  object_class->finalize = gimp_text_layout_finalize;
 
98
}
 
99
 
 
100
static void
 
101
gimp_text_layout_init (GimpTextLayout *layout)
 
102
{
 
103
  layout->text   = NULL;
 
104
  layout->layout = NULL;
 
105
}
 
106
 
 
107
static void
 
108
gimp_text_layout_finalize (GObject *object)
 
109
{
 
110
  GimpTextLayout *layout;
 
111
 
 
112
  layout = GIMP_TEXT_LAYOUT (object);
 
113
 
 
114
  if (layout->text)
 
115
    {
 
116
      g_object_unref (layout->text);
 
117
      layout->text = NULL;
 
118
    }
 
119
  if (layout->layout)
 
120
    {
 
121
      g_object_unref (layout->layout);
 
122
      layout->layout = NULL;
 
123
    }
 
124
 
 
125
  G_OBJECT_CLASS (parent_class)->finalize (object);
 
126
}
 
127
 
 
128
 
 
129
GimpTextLayout *
 
130
gimp_text_layout_new (GimpText  *text,
 
131
                      GimpImage *image)
 
132
{
 
133
  GimpTextLayout       *layout;
 
134
  PangoContext         *context;
 
135
  PangoFontDescription *font_desc;
 
136
  PangoAlignment        alignment = PANGO_ALIGN_LEFT;
 
137
  gdouble               xres, yres;
 
138
  gint                  size;
 
139
 
 
140
  g_return_val_if_fail (GIMP_IS_TEXT (text), NULL);
 
141
  g_return_val_if_fail (GIMP_IS_IMAGE (image), NULL);
 
142
 
 
143
  font_desc = pango_font_description_from_string (text->font);
 
144
  g_return_val_if_fail (font_desc != NULL, NULL);
 
145
  if (!font_desc)
 
146
    return NULL;
 
147
 
 
148
  gimp_image_get_resolution (image, &xres, &yres);
 
149
 
 
150
  size = gimp_text_layout_point_size (image->gimp,
 
151
                                      text->font_size,
 
152
                                      text->unit,
 
153
                                      yres);
 
154
 
 
155
  pango_font_description_set_size (font_desc, MAX (1, size));
 
156
 
 
157
  context = gimp_text_get_pango_context (text, xres, yres);
 
158
 
 
159
  layout = g_object_new (GIMP_TYPE_TEXT_LAYOUT, NULL);
 
160
  layout->text   = g_object_ref (text);
 
161
  layout->layout = pango_layout_new (context);
 
162
 
 
163
  g_object_unref (context);
 
164
 
 
165
  pango_layout_set_font_description (layout->layout, font_desc);
 
166
  pango_font_description_free (font_desc);
 
167
 
 
168
  if (text->text)
 
169
    pango_layout_set_text (layout->layout, text->text, -1);
 
170
  else
 
171
    pango_layout_set_text (layout->layout, NULL, 0);
 
172
 
 
173
  switch (text->justify)
 
174
    {
 
175
    case GIMP_TEXT_JUSTIFY_LEFT:
 
176
      alignment = PANGO_ALIGN_LEFT;
 
177
      break;
 
178
    case GIMP_TEXT_JUSTIFY_RIGHT:
 
179
      alignment = PANGO_ALIGN_RIGHT;
 
180
      break;
 
181
    case GIMP_TEXT_JUSTIFY_CENTER:
 
182
      alignment = PANGO_ALIGN_CENTER;
 
183
      break;
 
184
    case GIMP_TEXT_JUSTIFY_FILL:
 
185
      /* FIXME: This doesn't work since the implementation is missing
 
186
         at the Pango level.
 
187
       */
 
188
      alignment = PANGO_ALIGN_LEFT;
 
189
      pango_layout_set_justify (layout->layout, TRUE);
 
190
      break;
 
191
    }
 
192
 
 
193
  pango_layout_set_alignment (layout->layout, alignment);
 
194
 
 
195
  switch (text->box_mode)
 
196
    {
 
197
    case GIMP_TEXT_BOX_DYNAMIC:
 
198
      break;
 
199
    case GIMP_TEXT_BOX_FIXED:
 
200
      pango_layout_set_width (layout->layout,
 
201
                              gimp_text_layout_pixel_size (image->gimp,
 
202
                                                           text->box_width,
 
203
                                                           text->box_unit,
 
204
                                                           xres));
 
205
      break;
 
206
    }
 
207
 
 
208
  pango_layout_set_indent (layout->layout,
 
209
                           gimp_text_layout_pixel_size (image->gimp,
 
210
                                                        text->indent,
 
211
                                                        text->unit,
 
212
                                                        xres));
 
213
  pango_layout_set_spacing (layout->layout,
 
214
                            gimp_text_layout_pixel_size (image->gimp,
 
215
                                                         text->line_spacing,
 
216
                                                         text->unit,
 
217
                                                         yres));
 
218
  gimp_text_layout_position (layout);
 
219
 
 
220
  switch (text->box_mode)
 
221
    {
 
222
    case GIMP_TEXT_BOX_DYNAMIC:
 
223
      break;
 
224
    case GIMP_TEXT_BOX_FIXED:
 
225
      layout->extents.height =
 
226
        PANGO_PIXELS (gimp_text_layout_pixel_size (image->gimp,
 
227
                                                   text->box_height,
 
228
                                                   text->box_unit,
 
229
                                                   yres));
 
230
      break;
 
231
    }
 
232
 
 
233
  return layout;
 
234
}
 
235
 
 
236
gboolean
 
237
gimp_text_layout_get_size (GimpTextLayout *layout,
 
238
                           gint           *width,
 
239
                           gint           *height)
 
240
{
 
241
  g_return_val_if_fail (GIMP_IS_TEXT_LAYOUT (layout), FALSE);
 
242
 
 
243
  if (width)
 
244
    *width = layout->extents.width;
 
245
  if (height)
 
246
    *height = layout->extents.height;
 
247
 
 
248
  return (layout->extents.width > 0 && layout->extents.height > 0);
 
249
}
 
250
 
 
251
void
 
252
gimp_text_layout_get_offsets (GimpTextLayout *layout,
 
253
                              gint           *x,
 
254
                              gint           *y)
 
255
{
 
256
  g_return_if_fail (GIMP_IS_TEXT_LAYOUT (layout));
 
257
 
 
258
  if (x)
 
259
    *x = layout->extents.x;
 
260
  if (y)
 
261
    *y = layout->extents.y;
 
262
}
 
263
 
 
264
static void
 
265
gimp_text_layout_position (GimpTextLayout *layout)
 
266
{
 
267
  PangoRectangle  ink;
 
268
  PangoRectangle  logical;
 
269
  gint            x1, y1;
 
270
  gint            x2, y2;
 
271
 
 
272
  layout->extents.x      = 0;
 
273
  layout->extents.x      = 0;
 
274
  layout->extents.width  = 0;
 
275
  layout->extents.height = 0;
 
276
 
 
277
  pango_layout_get_pixel_extents (layout->layout, &ink, &logical);
 
278
 
 
279
#ifdef VERBOSE
 
280
  g_print ("ink rect: %d x %d @ %d, %d\n",
 
281
           ink.width, ink.height, ink.x, ink.y);
 
282
  g_print ("logical rect: %d x %d @ %d, %d\n",
 
283
           logical.width, logical.height, logical.x, logical.y);
 
284
#endif
 
285
 
 
286
  if (ink.width < 1 || ink.height < 1)
 
287
    return;
 
288
 
 
289
  x1 = MIN (ink.x, logical.x);
 
290
  y1 = MIN (ink.y, logical.y);
 
291
  x2 = MAX (ink.x + ink.width,  logical.x + logical.width);
 
292
  y2 = MAX (ink.y + ink.height, logical.y + logical.height);
 
293
 
 
294
  layout->extents.x      = - x1;
 
295
  layout->extents.y      = - y1;
 
296
  layout->extents.width  = x2 - x1;
 
297
  layout->extents.height = y2 - y1;
 
298
 
 
299
  if (layout->text->border > 0)
 
300
    {
 
301
      gint border = layout->text->border;
 
302
 
 
303
      layout->extents.x      += border;
 
304
      layout->extents.y      += border;
 
305
      layout->extents.width  += 2 * border;
 
306
      layout->extents.height += 2 * border;
 
307
    }
 
308
 
 
309
#ifdef VERBOSE
 
310
  g_print ("layout extents: %d x %d @ %d, %d\n",
 
311
           layout->extents.width, layout->extents.height,
 
312
           layout->extents.x, layout->extents.y);
 
313
#endif
 
314
}
 
315
 
 
316
 
 
317
static void
 
318
gimp_text_ft2_subst_func (FcPattern *pattern,
 
319
                          gpointer   data)
 
320
{
 
321
  GimpText *text = GIMP_TEXT (data);
 
322
 
 
323
  FcPatternAddBool (pattern, FC_HINTING,   text->hinting);
 
324
  FcPatternAddBool (pattern, FC_AUTOHINT,  text->autohint);
 
325
  FcPatternAddBool (pattern, FC_ANTIALIAS, text->antialias);
 
326
}
 
327
 
 
328
static PangoContext *
 
329
gimp_text_get_pango_context (GimpText *text,
 
330
                             gdouble   xres,
 
331
                             gdouble   yres)
 
332
{
 
333
  PangoContext    *context;
 
334
  PangoFT2FontMap *fontmap;
 
335
 
 
336
  fontmap = PANGO_FT2_FONT_MAP (pango_ft2_font_map_new ());
 
337
 
 
338
  pango_ft2_font_map_set_resolution (fontmap, xres, yres);
 
339
 
 
340
  pango_ft2_font_map_set_default_substitute (fontmap,
 
341
                                             gimp_text_ft2_subst_func,
 
342
                                             g_object_ref (text),
 
343
                                             (GDestroyNotify) g_object_unref);
 
344
 
 
345
  context = pango_ft2_font_map_create_context (fontmap);
 
346
  g_object_unref (fontmap);
 
347
 
 
348
  /*  Workaround for bug #143542 (PangoFT2Fontmap leak),
 
349
   *  see also bug #148997 (Text layer rendering leaks font file descriptor):
 
350
   *
 
351
   *  Calling pango_ft2_font_map_substitute_changed() causes the
 
352
   *  font_map cache to be flushed, thereby removing the circular
 
353
   *  reference that causes the leak.
 
354
   */
 
355
  g_object_weak_ref (G_OBJECT (context),
 
356
                     (GWeakNotify) pango_ft2_font_map_substitute_changed,
 
357
                     fontmap);
 
358
 
 
359
  if (text->language)
 
360
    pango_context_set_language (context,
 
361
                                pango_language_from_string (text->language));
 
362
 
 
363
  switch (text->base_dir)
 
364
    {
 
365
    case GIMP_TEXT_DIRECTION_LTR:
 
366
      pango_context_set_base_dir (context, PANGO_DIRECTION_LTR);
 
367
      break;
 
368
    case GIMP_TEXT_DIRECTION_RTL:
 
369
      pango_context_set_base_dir (context, PANGO_DIRECTION_RTL);
 
370
      break;
 
371
    }
 
372
 
 
373
  return context;
 
374
}
 
375
 
 
376
static gint
 
377
gimp_text_layout_pixel_size (Gimp     *gimp,
 
378
                             gdouble   value,
 
379
                             GimpUnit  unit,
 
380
                             gdouble   res)
 
381
{
 
382
  gdouble factor;
 
383
 
 
384
  switch (unit)
 
385
    {
 
386
    case GIMP_UNIT_PIXEL:
 
387
      return PANGO_SCALE * value;
 
388
 
 
389
    default:
 
390
      factor = _gimp_unit_get_factor (gimp, unit);
 
391
      g_return_val_if_fail (factor > 0.0, 0);
 
392
 
 
393
      return PANGO_SCALE * value * res / factor;
 
394
    }
 
395
}
 
396
 
 
397
static gint
 
398
gimp_text_layout_point_size (Gimp     *gimp,
 
399
                             gdouble   value,
 
400
                             GimpUnit  unit,
 
401
                             gdouble   res)
 
402
{
 
403
  gdouble factor;
 
404
 
 
405
  switch (unit)
 
406
    {
 
407
    case GIMP_UNIT_POINT:
 
408
      return PANGO_SCALE * value;
 
409
 
 
410
    case GIMP_UNIT_PIXEL:
 
411
      g_return_val_if_fail (res > 0.0, 0);
 
412
      return (PANGO_SCALE * value *
 
413
              _gimp_unit_get_factor (gimp, GIMP_UNIT_POINT) / res);
 
414
 
 
415
    default:
 
416
      factor = _gimp_unit_get_factor (gimp, unit);
 
417
      g_return_val_if_fail (factor > 0.0, 0);
 
418
 
 
419
      return (PANGO_SCALE * value *
 
420
              _gimp_unit_get_factor (gimp, GIMP_UNIT_POINT) / factor);
 
421
    }
 
422
}