gimptextlayer.c 25.4 KB
Newer Older
1
/* GIMP - The GNU Image Manipulation Program
2 3
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
4
 * GimpTextLayer
5
 * Copyright (C) 2002-2004  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 3 of the License, or
10 11 12 13 14 15 16 17
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
18
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19 20 21 22
 */

#include "config.h"

Manish Singh's avatar
Manish Singh committed
23 24
#include <string.h>

25
#include <cairo.h>
26
#include <gegl.h>
27
#include <gdk-pixbuf/gdk-pixbuf.h>
28
#include <pango/pangocairo.h>
29

30
#include "libgimpbase/gimpbase.h"
31
#include "libgimpcolor/gimpcolor.h"
32
#include "libgimpconfig/gimpconfig.h"
33

34 35
#include "text-types.h"

36
#include "gegl/gimp-babl.h"
37
#include "gegl/gimp-gegl-loops.h"
38
#include "gegl/gimp-gegl-utils.h"
39

40
#include "core/gimp.h"
41
#include "core/gimp-utils.h"
42
#include "core/gimpcontext.h"
43
#include "core/gimpcontainer.h"
44
#include "core/gimpdatafactory.h"
45
#include "core/gimpimage.h"
46
#include "core/gimpimage-color-profile.h"
47
#include "core/gimpimage-undo.h"
48
#include "core/gimpimage-undo-push.h"
49
#include "core/gimpitemtree.h"
50
#include "core/gimpparasitelist.h"
51 52 53

#include "gimptext.h"
#include "gimptextlayer.h"
54
#include "gimptextlayer-transform.h"
55
#include "gimptextlayout.h"
56
#include "gimptextlayout-render.h"
57

58
#include "gimp-intl.h"
59 60


61 62 63
enum
{
  PROP_0,
64
  PROP_TEXT,
65 66 67 68
  PROP_AUTO_RENAME,
  PROP_MODIFIED
};

69

70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
static void       gimp_text_layer_finalize       (GObject           *object);
static void       gimp_text_layer_get_property   (GObject           *object,
                                                  guint              property_id,
                                                  GValue            *value,
                                                  GParamSpec        *pspec);
static void       gimp_text_layer_set_property   (GObject           *object,
                                                  guint              property_id,
                                                  const GValue      *value,
                                                  GParamSpec        *pspec);

static gint64     gimp_text_layer_get_memsize    (GimpObject        *object,
                                                  gint64            *gui_size);

static GimpItem * gimp_text_layer_duplicate      (GimpItem          *item,
                                                  GType              new_type);
static gboolean   gimp_text_layer_rename         (GimpItem          *item,
                                                  const gchar       *new_name,
                                                  const gchar       *undo_desc,
                                                  GError           **error);

static void       gimp_text_layer_set_buffer     (GimpDrawable      *drawable,
                                                  gboolean           push_undo,
                                                  const gchar       *undo_desc,
                                                  GeglBuffer        *buffer,
                                                  gint               offset_x,
                                                  gint               offset_y);
static void       gimp_text_layer_push_undo      (GimpDrawable      *drawable,
                                                  const gchar       *undo_desc,
                                                  GeglBuffer        *buffer,
                                                  gint               x,
                                                  gint               y,
                                                  gint               width,
                                                  gint               height);

104 105 106 107 108 109 110 111 112
static void       gimp_text_layer_convert_type   (GimpLayer         *layer,
                                                  GimpImage         *dest_image,
                                                  const Babl        *new_format,
                                                  GimpColorProfile  *dest_profile,
                                                  GeglDitherMethod   layer_dither_type,
                                                  GeglDitherMethod   mask_dither_type,
                                                  gboolean           push_undo,
                                                  GimpProgress      *progress);

113 114 115 116
static void       gimp_text_layer_text_changed   (GimpTextLayer     *layer);
static gboolean   gimp_text_layer_render         (GimpTextLayer     *layer);
static void       gimp_text_layer_render_layout  (GimpTextLayer     *layer,
                                                  GimpTextLayout    *layout);
117 118


119
G_DEFINE_TYPE (GimpTextLayer, gimp_text_layer, GIMP_TYPE_LAYER)
120

121
#define parent_class gimp_text_layer_parent_class
122 123 124 125 126


static void
gimp_text_layer_class_init (GimpTextLayerClass *klass)
{
127 128 129 130 131
  GObjectClass      *object_class      = G_OBJECT_CLASS (klass);
  GimpObjectClass   *gimp_object_class = GIMP_OBJECT_CLASS (klass);
  GimpViewableClass *viewable_class    = GIMP_VIEWABLE_CLASS (klass);
  GimpItemClass     *item_class        = GIMP_ITEM_CLASS (klass);
  GimpDrawableClass *drawable_class    = GIMP_DRAWABLE_CLASS (klass);
132
  GimpLayerClass    *layer_class       = GIMP_LAYER_CLASS (klass);
133

134 135 136
  object_class->finalize            = gimp_text_layer_finalize;
  object_class->get_property        = gimp_text_layer_get_property;
  object_class->set_property        = gimp_text_layer_set_property;
137

138
  gimp_object_class->get_memsize    = gimp_text_layer_get_memsize;
139

140
  viewable_class->default_icon_name = "gimp-text-layer";
141

142 143
  item_class->duplicate             = gimp_text_layer_duplicate;
  item_class->rename                = gimp_text_layer_rename;
144 145

#if 0
146 147 148 149
  item_class->scale                 = gimp_text_layer_scale;
  item_class->flip                  = gimp_text_layer_flip;
  item_class->rotate                = gimp_text_layer_rotate;
  item_class->transform             = gimp_text_layer_transform;
150
#endif
151

152 153 154 155 156 157 158 159 160 161 162
  item_class->default_name          = _("Text Layer");
  item_class->rename_desc           = _("Rename Text Layer");
  item_class->translate_desc        = _("Move Text Layer");
  item_class->scale_desc            = _("Scale Text Layer");
  item_class->resize_desc           = _("Resize Text Layer");
  item_class->flip_desc             = _("Flip Text Layer");
  item_class->rotate_desc           = _("Rotate Text Layer");
  item_class->transform_desc        = _("Transform Text Layer");

  drawable_class->set_buffer        = gimp_text_layer_set_buffer;
  drawable_class->push_undo         = gimp_text_layer_push_undo;
163

164 165
  layer_class->convert_type         = gimp_text_layer_convert_type;

166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
  GIMP_CONFIG_PROP_OBJECT (object_class, PROP_TEXT,
                           "text",
                           NULL, NULL,
                           GIMP_TYPE_TEXT,
                           GIMP_PARAM_STATIC_STRINGS);

  GIMP_CONFIG_PROP_BOOLEAN (object_class, PROP_AUTO_RENAME,
                            "auto-rename",
                            NULL, NULL,
                            TRUE,
                            GIMP_PARAM_STATIC_STRINGS);

  GIMP_CONFIG_PROP_BOOLEAN (object_class, PROP_MODIFIED,
                            "modified",
                            NULL, NULL,
                            FALSE,
                            GIMP_PARAM_STATIC_STRINGS);
183 184 185 186 187
}

static void
gimp_text_layer_init (GimpTextLayer *layer)
{
188 189
  layer->text          = NULL;
  layer->text_parasite = NULL;
190 191 192 193 194 195 196
}

static void
gimp_text_layer_finalize (GObject *object)
{
  GimpTextLayer *layer = GIMP_TEXT_LAYER (object);

197
  g_clear_object (&layer->text);
198

199
  G_OBJECT_CLASS (parent_class)->finalize (object);
200 201
}

202 203 204 205 206 207 208 209 210 211
static void
gimp_text_layer_get_property (GObject      *object,
                              guint         property_id,
                              GValue       *value,
                              GParamSpec   *pspec)
{
  GimpTextLayer *text_layer = GIMP_TEXT_LAYER (object);

  switch (property_id)
    {
212 213 214
    case PROP_TEXT:
      g_value_set_object (value, text_layer->text);
      break;
215 216 217 218 219 220
    case PROP_AUTO_RENAME:
      g_value_set_boolean (value, text_layer->auto_rename);
      break;
    case PROP_MODIFIED:
      g_value_set_boolean (value, text_layer->modified);
      break;
221

222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
      break;
    }
}

static void
gimp_text_layer_set_property (GObject      *object,
                              guint         property_id,
                              const GValue *value,
                              GParamSpec   *pspec)
{
  GimpTextLayer *text_layer = GIMP_TEXT_LAYER (object);

  switch (property_id)
    {
238 239 240
    case PROP_TEXT:
      gimp_text_layer_set_text (text_layer, g_value_get_object (value));
      break;
241 242 243 244 245 246
    case PROP_AUTO_RENAME:
      text_layer->auto_rename = g_value_get_boolean (value);
      break;
    case PROP_MODIFIED:
      text_layer->modified = g_value_get_boolean (value);
      break;
247

248 249 250 251 252 253
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
      break;
    }
}

254
static gint64
Michael Natterer's avatar
Michael Natterer committed
255
gimp_text_layer_get_memsize (GimpObject *object,
256
                             gint64     *gui_size)
257
{
258 259
  GimpTextLayer *text_layer = GIMP_TEXT_LAYER (object);
  gint64         memsize    = 0;
260

261 262
  memsize += gimp_object_get_memsize (GIMP_OBJECT (text_layer->text),
                                      gui_size);
263

Michael Natterer's avatar
Michael Natterer committed
264 265
  return memsize + GIMP_OBJECT_CLASS (parent_class)->get_memsize (object,
                                                                  gui_size);
266 267 268 269
}

static GimpItem *
gimp_text_layer_duplicate (GimpItem *item,
270
                           GType     new_type)
271
{
272
  GimpItem *new_item;
273 274 275

  g_return_val_if_fail (g_type_is_a (new_type, GIMP_TYPE_DRAWABLE), NULL);

276
  new_item = GIMP_ITEM_CLASS (parent_class)->duplicate (item, new_type);
277

278 279 280 281
  if (GIMP_IS_TEXT_LAYER (new_item))
    {
      GimpTextLayer *layer     = GIMP_TEXT_LAYER (item);
      GimpTextLayer *new_layer = GIMP_TEXT_LAYER (new_item);
282

283
      gimp_config_sync (G_OBJECT (layer), G_OBJECT (new_layer), 0);
284

285 286 287
      if (layer->text)
        {
          GimpText *text = gimp_config_duplicate (GIMP_CONFIG (layer->text));
288

289
          gimp_text_layer_set_text (new_layer, text);
290

291 292
          g_object_unref (text);
        }
293

294 295 296
      /*  this is just the parasite name, not a pointer to the parasite  */
      if (layer->text_parasite)
        new_layer->text_parasite = layer->text_parasite;
297 298
    }

299 300 301
  return new_item;
}

302
static gboolean
303 304 305 306
gimp_text_layer_rename (GimpItem     *item,
                        const gchar  *new_name,
                        const gchar  *undo_desc,
                        GError      **error)
307
{
308
  if (GIMP_ITEM_CLASS (parent_class)->rename (item, new_name, undo_desc, error))
309
    {
310
      g_object_set (item, "auto-rename", FALSE, NULL);
311 312 313

      return TRUE;
    }
314

315
  return FALSE;
316 317
}

318
static void
319 320 321 322 323 324
gimp_text_layer_set_buffer (GimpDrawable *drawable,
                            gboolean      push_undo,
                            const gchar  *undo_desc,
                            GeglBuffer   *buffer,
                            gint          offset_x,
                            gint          offset_y)
325
{
326 327
  GimpTextLayer *layer = GIMP_TEXT_LAYER (drawable);
  GimpImage     *image = gimp_item_get_image (GIMP_ITEM (layer));
328 329

  if (push_undo && ! layer->modified)
330
    gimp_image_undo_group_start (image, GIMP_UNDO_GROUP_DRAWABLE_MOD,
331 332
                                 undo_desc);

333 334 335 336
  GIMP_DRAWABLE_CLASS (parent_class)->set_buffer (drawable,
                                                  push_undo, undo_desc,
                                                  buffer,
                                                  offset_x, offset_y);
337 338 339

  if (push_undo && ! layer->modified)
    {
340
      gimp_image_undo_push_text_layer_modified (image, NULL, layer);
341 342 343

      g_object_set (drawable, "modified", TRUE, NULL);

344
      gimp_image_undo_group_end (image);
345
    }
346 347
}

348
static void
349 350
gimp_text_layer_push_undo (GimpDrawable *drawable,
                           const gchar  *undo_desc,
351
                           GeglBuffer   *buffer,
352 353 354 355
                           gint          x,
                           gint          y,
                           gint          width,
                           gint          height)
356
{
357 358
  GimpTextLayer *layer = GIMP_TEXT_LAYER (drawable);
  GimpImage     *image = gimp_item_get_image (GIMP_ITEM (layer));
359 360

  if (! layer->modified)
361
    gimp_image_undo_group_start (image, GIMP_UNDO_GROUP_DRAWABLE, undo_desc);
362 363

  GIMP_DRAWABLE_CLASS (parent_class)->push_undo (drawable, undo_desc,
364
                                                 buffer,
365 366 367 368
                                                 x, y, width, height);

  if (! layer->modified)
    {
369
      gimp_image_undo_push_text_layer_modified (image, NULL, layer);
370 371 372

      g_object_set (drawable, "modified", TRUE, NULL);

373
      gimp_image_undo_group_end (image);
374
    }
375 376
}

377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
static void
gimp_text_layer_convert_type (GimpLayer         *layer,
                              GimpImage         *dest_image,
                              const Babl        *new_format,
                              GimpColorProfile  *dest_profile,
                              GeglDitherMethod   layer_dither_type,
                              GeglDitherMethod   mask_dither_type,
                              gboolean           push_undo,
                              GimpProgress      *progress)
{
  GimpTextLayer *text_layer = GIMP_TEXT_LAYER (layer);
  GimpImage     *image      = gimp_item_get_image (GIMP_ITEM (text_layer));

  if (! text_layer->text   ||
      text_layer->modified ||
      layer_dither_type != GEGL_DITHER_NONE)
    {
      GIMP_LAYER_CLASS (parent_class)->convert_type (layer, dest_image,
                                                     new_format,
                                                     dest_profile,
                                                     layer_dither_type,
                                                     mask_dither_type,
                                                     push_undo,
                                                     progress);
    }
  else
    {
      if (push_undo)
        gimp_image_undo_push_text_layer_convert (image, NULL, text_layer);

      text_layer->convert_format = new_format;

      gimp_text_layer_render (text_layer);

      text_layer->convert_format = NULL;
    }
}

415

416 417
/*  public functions  */

418 419 420 421 422 423 424 425 426
/**
 * gimp_text_layer_new:
 * @image: the #GimpImage the layer should belong to
 * @text: a #GimpText object
 *
 * Creates a new text layer.
 *
 * Return value: a new #GimpTextLayer or %NULL in case of a problem
 **/
427
GimpLayer *
428
gimp_text_layer_new (GimpImage *image,
429
                     GimpText  *text)
430
{
431
  GimpTextLayer *layer;
432

433
  g_return_val_if_fail (GIMP_IS_IMAGE (image), NULL);
434
  g_return_val_if_fail (GIMP_IS_TEXT (text), NULL);
435

436
  if (! text->text && ! text->markup)
437
    return NULL;
438

439 440 441 442 443 444
  layer =
    GIMP_TEXT_LAYER (gimp_drawable_new (GIMP_TYPE_TEXT_LAYER,
                                        image, NULL,
                                        0, 0, 1, 1,
                                        gimp_image_get_layer_format (image,
                                                                     TRUE)));
445

446 447 448 449
  gimp_layer_set_mode (GIMP_LAYER (layer),
                       gimp_image_get_default_new_layer_mode (image),
                       FALSE);

450
  gimp_text_layer_set_text (layer, text);
451

452
  if (! gimp_text_layer_render (layer))
453 454 455 456
    {
      g_object_unref (layer);
      return NULL;
    }
457

458 459
  return GIMP_LAYER (layer);
}
460

461
void
462 463 464 465
gimp_text_layer_set_text (GimpTextLayer *layer,
                          GimpText      *text)
{
  g_return_if_fail (GIMP_IS_TEXT_LAYER (layer));
466 467 468 469 470 471 472 473
  g_return_if_fail (text == NULL || GIMP_IS_TEXT (text));

  if (layer->text == text)
    return;

  if (layer->text)
    {
      g_signal_handlers_disconnect_by_func (layer->text,
474
                                            G_CALLBACK (gimp_text_layer_text_changed),
475 476
                                            layer);

477
      g_clear_object (&layer->text);
478 479 480 481 482 483
    }

  if (text)
    {
      layer->text = g_object_ref (text);

484 485
      g_signal_connect_object (text, "changed",
                               G_CALLBACK (gimp_text_layer_text_changed),
486 487
                               layer, G_CONNECT_SWAPPED);
    }
488

489
  g_object_notify (G_OBJECT (layer), "text");
490
  gimp_viewable_invalidate_preview (GIMP_VIEWABLE (layer));
491 492
}

493 494 495 496
GimpText *
gimp_text_layer_get_text (GimpTextLayer *layer)
{
  g_return_val_if_fail (GIMP_IS_TEXT_LAYER (layer), NULL);
497

498 499 500
  return layer->text;
}

501 502 503 504 505
void
gimp_text_layer_set (GimpTextLayer *layer,
                     const gchar   *undo_desc,
                     const gchar   *first_property_name,
                     ...)
506
{
507 508 509
  GimpImage *image;
  GimpText  *text;
  va_list    var_args;
510

511
  g_return_if_fail (gimp_item_is_text_layer (GIMP_ITEM (layer)));
512
  g_return_if_fail (gimp_item_is_attached (GIMP_ITEM (layer)));
513 514 515 516 517

  text = gimp_text_layer_get_text (layer);
  if (! text)
    return;

518 519
  image = gimp_item_get_image (GIMP_ITEM (layer));

520 521 522 523 524 525 526
  gimp_image_undo_group_start (image, GIMP_UNDO_GROUP_TEXT, undo_desc);

  g_object_freeze_notify (G_OBJECT (layer));

  if (layer->modified)
    {
      gimp_image_undo_push_text_layer_modified (image, NULL, layer);
527 528 529 530 531 532 533 534 535 536

      /*  pass copy_tiles = TRUE so we not only ref the tiles; after
       *  being a text layer again, undo doesn't care about the
       *  layer's pixels any longer because they are generated, so
       *  changing the text would happily overwrite the layer's
       *  pixels, changing the pixels on the undo stack too without
       *  any chance to ever undo again.
       */
      gimp_image_undo_push_drawable_mod (image, NULL,
                                         GIMP_DRAWABLE (layer), TRUE);
537
    }
538

539
  gimp_image_undo_push_text_layer (image, undo_desc, layer, NULL);
540

541 542 543 544 545
  va_start (var_args, first_property_name);

  g_object_set_valist (G_OBJECT (text), first_property_name, var_args);

  va_end (var_args);
546

547 548 549 550 551
  g_object_set (layer, "modified", FALSE, NULL);

  g_object_thaw_notify (G_OBJECT (layer));

  gimp_image_undo_group_end (image);
552
}
553

554 555 556 557 558 559 560
/**
 * gimp_text_layer_discard:
 * @layer: a #GimpTextLayer
 *
 * Discards the text information. This makes @layer behave like a
 * normal layer.
 */
561
void
562
gimp_text_layer_discard (GimpTextLayer *layer)
563 564
{
  g_return_if_fail (GIMP_IS_TEXT_LAYER (layer));
565
  g_return_if_fail (gimp_item_is_attached (GIMP_ITEM (layer)));
566

567 568 569 570 571
  if (! layer->text)
    return;

  gimp_image_undo_push_text_layer (gimp_item_get_image (GIMP_ITEM (layer)),
                                   _("Discard Text Information"),
572
                                   layer, NULL);
573 574 575 576

  gimp_text_layer_set_text (layer, NULL);
}

577
gboolean
578
gimp_item_is_text_layer (GimpItem *item)
579
{
580 581 582
  return (GIMP_IS_TEXT_LAYER (item)    &&
          GIMP_TEXT_LAYER (item)->text &&
          GIMP_TEXT_LAYER (item)->modified == FALSE);
583 584 585
}


586 587
/*  private functions  */

588 589 590 591 592 593 594 595 596
static const Babl *
gimp_text_layer_get_format (GimpTextLayer *layer)
{
  if (layer->convert_format)
    return layer->convert_format;

  return gimp_drawable_get_format (GIMP_DRAWABLE (layer));
}

597
static void
598
gimp_text_layer_text_changed (GimpTextLayer *layer)
599
{
600 601
  /*  If the text layer was created from a parasite, it's time to
   *  remove that parasite now.
602 603 604
   */
  if (layer->text_parasite)
    {
605 606 607 608 609
      /*  Don't push an undo because the parasite only exists temporarily
       *  while the text layer is loaded from XCF.
       */
      gimp_item_parasite_detach (GIMP_ITEM (layer), layer->text_parasite,
                                 FALSE);
610 611 612
      layer->text_parasite = NULL;
    }

613
  gimp_text_layer_render (layer);
614 615 616
}

static gboolean
617
gimp_text_layer_render (GimpTextLayer *layer)
618
{
619
  GimpDrawable   *drawable;
620 621
  GimpItem       *item;
  GimpImage      *image;
622
  GimpContainer  *container;
623
  GimpTextLayout *layout;
624 625
  gdouble         xres;
  gdouble         yres;
626 627
  gint            width;
  gint            height;
628
  GError         *error = NULL;
629

630 631 632
  if (! layer->text)
    return FALSE;

633 634 635 636
  drawable  = GIMP_DRAWABLE (layer);
  item      = GIMP_ITEM (layer);
  image     = gimp_item_get_image (item);
  container = gimp_data_factory_get_container (image->gimp->font_factory);
637

638
  gimp_data_factory_data_wait (image->gimp->font_factory);
639

640
  if (gimp_container_is_empty (container))
641
    {
642
      gimp_message_literal (image->gimp, NULL, GIMP_MESSAGE_ERROR,
643 644
                            _("Due to lack of any fonts, "
                              "text functionality is not available."));
645 646 647
      return FALSE;
    }

648 649
  gimp_image_get_resolution (image, &xres, &yres);

650 651 652 653 654 655
  layout = gimp_text_layout_new (layer->text, xres, yres, &error);
  if (error)
    {
      gimp_message_literal (image->gimp, NULL, GIMP_MESSAGE_ERROR, error->message);
      g_error_free (error);
    }
656

657 658
  g_object_freeze_notify (G_OBJECT (drawable));

659
  if (gimp_text_layout_get_size (layout, &width, &height) &&
660
      (width  != gimp_item_get_width  (item) ||
661 662 663
       height != gimp_item_get_height (item) ||
       gimp_text_layer_get_format (layer) !=
       gimp_drawable_get_format (drawable)))
664
    {
665
      GeglBuffer *new_buffer;
666 667 668 669 670 671
      GimpItem   *item;
      gint        oldwidth;
      gint        newwidth;

      item = GIMP_ITEM (drawable);
      oldwidth = gimp_item_get_width (item);
672

673
      new_buffer = gegl_buffer_new (GEGL_RECTANGLE (0, 0, width, height),
674
                                    gimp_text_layer_get_format (layer));
675
      gimp_drawable_set_buffer (drawable, FALSE, NULL, new_buffer);
676 677 678 679 680 681 682 683 684 685

      newwidth = gimp_item_get_width(item);
      if (layer->text->box_mode == GIMP_TEXT_BOX_DYNAMIC &&
          oldwidth != newwidth &&
          (layer->text->base_dir == GIMP_TEXT_DIRECTION_TTB_RTL ||
           layer->text->base_dir == GIMP_TEXT_DIRECTION_TTB_RTL_UPRIGHT))
        {
          gimp_item_translate (item, oldwidth - newwidth, 0, FALSE);
        }

686
      g_object_unref (new_buffer);
687

688
      if (gimp_layer_get_mask (GIMP_LAYER (layer)))
689
        {
690 691
          GimpLayerMask *mask = gimp_layer_get_mask (GIMP_LAYER (layer));

692 693 694 695 696
          static GimpContext *unused_eek = NULL;

          if (! unused_eek)
            unused_eek = gimp_context_new (image->gimp, "eek", NULL);

697 698 699
          gimp_item_resize (GIMP_ITEM (mask),
                            unused_eek, GIMP_FILL_TRANSPARENT,
                            width, height, 0, 0);
700
        }
701
    }
702

703
  if (layer->auto_rename)
704 705
    {
      GimpItem *item = GIMP_ITEM (layer);
706 707 708 709 710 711 712 713 714 715 716 717 718
      gchar    *name = NULL;

      if (layer->text->text)
        {
          name = gimp_utf8_strtrim (layer->text->text, 30);
        }
      else if (layer->text->markup)
        {
          gchar *tmp = gimp_markup_extract_text (layer->text->markup);
          name = gimp_utf8_strtrim (tmp, 30);
          g_free (tmp);
        }

719 720 721 722 723
      if (! name || ! name[0])
        {
          g_free (name);
          name = g_strdup (_("Empty Text Layer"));
        }
724 725

      if (gimp_item_is_attached (item))
726 727 728 729 730
        {
          gimp_item_tree_rename_item (gimp_item_get_tree (item), item,
                                      name, FALSE, NULL);
          g_free (name);
        }
731
      else
732 733 734
        {
          gimp_object_take_name (GIMP_OBJECT (layer), name);
        }
735
    }
736

737 738
  if (width > 0 && height > 0)
    gimp_text_layer_render_layout (layer, layout);
739

740
  g_object_unref (layout);
741

742 743
  g_object_thaw_notify (G_OBJECT (drawable));

744
  return (width > 0 && height > 0);
745 746 747
}

static void
748
gimp_text_layer_render_layout (GimpTextLayer  *layer,
749
                               GimpTextLayout *layout)
750
{
751 752 753 754 755 756 757 758 759 760
  GimpDrawable       *drawable = GIMP_DRAWABLE (layer);
  GimpItem           *item     = GIMP_ITEM (layer);
  GimpImage          *image    = gimp_item_get_image (item);
  GeglBuffer         *buffer;
  GimpColorTransform *transform;
  cairo_t            *cr;
  cairo_surface_t    *surface;
  gint                width;
  gint                height;
  cairo_status_t      status;
761

762
  g_return_if_fail (gimp_drawable_has_alpha (drawable));
763

764 765
  width  = gimp_item_get_width  (item);
  height = gimp_item_get_height (item);
766

767
  surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, width, height);
768 769 770 771 772 773 774 775 776 777 778 779
  status = cairo_surface_status (surface);

  if (status != CAIRO_STATUS_SUCCESS)
    {
      GimpImage *image = gimp_item_get_image (item);

      gimp_message_literal (image->gimp, NULL, GIMP_MESSAGE_ERROR,
                            _("Your text cannot be rendered. It is likely too big. "
                              "Please make it shorter or use a smaller font."));
      cairo_surface_destroy (surface);
      return;
    }
780

781
  cr = cairo_create (surface);
782
  gimp_text_layout_render (layout, cr, layer->text->base_dir, FALSE);
783
  cairo_destroy (cr);
784

785
  cairo_surface_flush (surface);
786

787
  buffer = gimp_cairo_surface_create_buffer (surface);
788

789 790 791 792 793 794 795 796 797 798 799 800
  transform = gimp_image_get_color_transform_from_srgb_u8 (image);

  if (transform)
    {
      gimp_color_transform_process_buffer (transform,
                                           buffer,
                                           NULL,
                                           gimp_drawable_get_buffer (drawable),
                                           NULL);
    }
  else
    {
801 802
      gimp_gegl_buffer_copy (buffer, NULL, GEGL_ABYSS_NONE,
                             gimp_drawable_get_buffer (drawable), NULL);
803
    }
804

805
  g_object_unref (buffer);
806
  cairo_surface_destroy (surface);
807 808

  gimp_drawable_update (drawable, 0, 0, width, height);
809
}