2003-01-31 17:22:42 +08:00
|
|
|
/* The GIMP -- an image manipulation program
|
|
|
|
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
|
|
|
|
*
|
|
|
|
* GimpText
|
|
|
|
* Copyright (C) 2002-2003 Sven Neumann <sven@gimp.org>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (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
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
2003-02-01 09:44:35 +08:00
|
|
|
#include <string.h>
|
|
|
|
|
2003-01-31 17:22:42 +08:00
|
|
|
#include <glib-object.h>
|
|
|
|
|
|
|
|
#include "text-types.h"
|
|
|
|
|
|
|
|
#include "base/pixel-region.h"
|
|
|
|
#include "base/tile-manager.h"
|
|
|
|
|
|
|
|
#include "paint-funcs/paint-funcs.h"
|
|
|
|
|
2003-02-11 21:52:47 +08:00
|
|
|
#include "config/gimpconfig.h"
|
|
|
|
|
2003-01-31 17:22:42 +08:00
|
|
|
#include "core/gimpimage.h"
|
|
|
|
|
|
|
|
#include "gimptext.h"
|
|
|
|
#include "gimptextlayer.h"
|
2003-02-05 16:29:12 +08:00
|
|
|
#include "gimptextlayout.h"
|
2003-01-31 17:22:42 +08:00
|
|
|
|
|
|
|
|
2003-02-11 21:52:47 +08:00
|
|
|
static void gimp_text_layer_class_init (GimpTextLayerClass *klass);
|
|
|
|
static void gimp_text_layer_init (GimpTextLayer *layer);
|
|
|
|
static void gimp_text_layer_dispose (GObject *object);
|
2003-01-31 17:22:42 +08:00
|
|
|
|
2003-02-11 21:52:47 +08:00
|
|
|
static gsize gimp_text_layer_get_memsize (GimpObject *object);
|
|
|
|
static TempBuf * gimp_text_layer_get_preview (GimpViewable *viewable,
|
|
|
|
gint width,
|
|
|
|
gint height);
|
|
|
|
static GimpItem * gimp_text_layer_duplicate (GimpItem *item,
|
|
|
|
GType new_type,
|
|
|
|
gboolean add_alpha);
|
2003-01-31 17:22:42 +08:00
|
|
|
|
2003-02-11 21:52:47 +08:00
|
|
|
static void gimp_text_layer_notify_text (GimpTextLayer *layer);
|
|
|
|
static gboolean gimp_text_layer_idle_render (GimpTextLayer *layer);
|
|
|
|
static gboolean gimp_text_layer_render (GimpTextLayer *layer);
|
|
|
|
static void gimp_text_layer_render_layout (GimpTextLayer *layer,
|
|
|
|
GimpTextLayout *layout);
|
2003-01-31 17:22:42 +08:00
|
|
|
|
|
|
|
|
|
|
|
static GimpLayerClass *parent_class = NULL;
|
|
|
|
|
|
|
|
|
|
|
|
GType
|
|
|
|
gimp_text_layer_get_type (void)
|
|
|
|
{
|
|
|
|
static GType layer_type = 0;
|
|
|
|
|
|
|
|
if (! layer_type)
|
|
|
|
{
|
|
|
|
static const GTypeInfo layer_info =
|
|
|
|
{
|
|
|
|
sizeof (GimpTextLayerClass),
|
|
|
|
(GBaseInitFunc) NULL,
|
|
|
|
(GBaseFinalizeFunc) NULL,
|
|
|
|
(GClassInitFunc) gimp_text_layer_class_init,
|
|
|
|
NULL, /* class_finalize */
|
|
|
|
NULL, /* class_data */
|
|
|
|
sizeof (GimpTextLayer),
|
|
|
|
0, /* n_preallocs */
|
|
|
|
(GInstanceInitFunc) gimp_text_layer_init,
|
|
|
|
};
|
|
|
|
|
|
|
|
layer_type = g_type_register_static (GIMP_TYPE_LAYER,
|
|
|
|
"GimpTextLayer",
|
|
|
|
&layer_info, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
return layer_type;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gimp_text_layer_class_init (GimpTextLayerClass *klass)
|
|
|
|
{
|
|
|
|
GObjectClass *object_class;
|
|
|
|
GimpObjectClass *gimp_object_class;
|
|
|
|
GimpViewableClass *viewable_class;
|
2003-02-11 21:52:47 +08:00
|
|
|
GimpItemClass *item_class;
|
2003-01-31 17:22:42 +08:00
|
|
|
|
|
|
|
object_class = G_OBJECT_CLASS (klass);
|
|
|
|
gimp_object_class = GIMP_OBJECT_CLASS (klass);
|
|
|
|
viewable_class = GIMP_VIEWABLE_CLASS (klass);
|
2003-02-11 21:52:47 +08:00
|
|
|
item_class = GIMP_ITEM_CLASS (klass);
|
2003-01-31 17:22:42 +08:00
|
|
|
|
|
|
|
parent_class = g_type_class_peek_parent (klass);
|
|
|
|
|
2003-02-27 02:08:26 +08:00
|
|
|
object_class->dispose = gimp_text_layer_dispose;
|
2003-01-31 17:22:42 +08:00
|
|
|
|
2003-02-27 02:08:26 +08:00
|
|
|
gimp_object_class->get_memsize = gimp_text_layer_get_memsize;
|
2003-01-31 17:22:42 +08:00
|
|
|
|
2003-03-27 20:29:54 +08:00
|
|
|
viewable_class->default_stock_id = "gimp-text-layer";
|
2003-02-27 02:08:26 +08:00
|
|
|
viewable_class->get_preview = gimp_text_layer_get_preview;
|
2003-02-11 21:52:47 +08:00
|
|
|
|
2003-02-27 02:08:26 +08:00
|
|
|
item_class->duplicate = gimp_text_layer_duplicate;
|
2003-01-31 17:22:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gimp_text_layer_init (GimpTextLayer *layer)
|
|
|
|
{
|
2003-01-31 21:36:27 +08:00
|
|
|
layer->text = NULL;
|
2003-01-31 17:22:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2003-02-06 02:23:58 +08:00
|
|
|
gimp_text_layer_dispose (GObject *object)
|
2003-01-31 17:22:42 +08:00
|
|
|
{
|
|
|
|
GimpTextLayer *layer;
|
|
|
|
|
|
|
|
layer = GIMP_TEXT_LAYER (object);
|
|
|
|
|
2003-02-06 02:23:58 +08:00
|
|
|
if (layer->idle_render_id)
|
|
|
|
{
|
|
|
|
g_source_remove (layer->idle_render_id);
|
|
|
|
layer->idle_render_id = 0;
|
|
|
|
}
|
2003-01-31 17:22:42 +08:00
|
|
|
if (layer->text)
|
|
|
|
{
|
|
|
|
g_object_unref (layer->text);
|
|
|
|
layer->text = NULL;
|
|
|
|
}
|
|
|
|
|
2003-02-06 02:23:58 +08:00
|
|
|
G_OBJECT_CLASS (parent_class)->dispose (object);
|
2003-01-31 17:22:42 +08:00
|
|
|
}
|
|
|
|
|
2003-02-11 21:52:47 +08:00
|
|
|
static gsize
|
|
|
|
gimp_text_layer_get_memsize (GimpObject *object)
|
|
|
|
{
|
|
|
|
GimpTextLayer *text_layer;
|
|
|
|
gsize memsize = 0;
|
|
|
|
|
|
|
|
text_layer = GIMP_TEXT_LAYER (object);
|
|
|
|
|
|
|
|
if (text_layer->text)
|
|
|
|
{
|
|
|
|
memsize += sizeof (GimpText);
|
|
|
|
|
|
|
|
if (text_layer->text->text)
|
|
|
|
memsize += strlen (text_layer->text->text);
|
|
|
|
}
|
|
|
|
|
|
|
|
return memsize + GIMP_OBJECT_CLASS (parent_class)->get_memsize (object);
|
|
|
|
}
|
|
|
|
|
|
|
|
static TempBuf *
|
|
|
|
gimp_text_layer_get_preview (GimpViewable *viewable,
|
|
|
|
gint width,
|
|
|
|
gint height)
|
|
|
|
{
|
|
|
|
return GIMP_VIEWABLE_CLASS (parent_class)->get_preview (viewable,
|
|
|
|
width, height);
|
|
|
|
}
|
|
|
|
|
|
|
|
static GimpItem *
|
|
|
|
gimp_text_layer_duplicate (GimpItem *item,
|
|
|
|
GType new_type,
|
|
|
|
gboolean add_alpha)
|
|
|
|
{
|
|
|
|
GimpTextLayer *text_layer;
|
|
|
|
GimpItem *new_item;
|
|
|
|
GimpTextLayer *new_text_layer;
|
|
|
|
|
|
|
|
g_return_val_if_fail (g_type_is_a (new_type, GIMP_TYPE_DRAWABLE), NULL);
|
|
|
|
|
|
|
|
new_item = GIMP_ITEM_CLASS (parent_class)->duplicate (item, new_type,
|
|
|
|
add_alpha);
|
|
|
|
|
|
|
|
if (! GIMP_IS_TEXT_LAYER (new_item))
|
|
|
|
return new_item;
|
|
|
|
|
|
|
|
text_layer = GIMP_TEXT_LAYER (item);
|
|
|
|
new_text_layer = GIMP_TEXT_LAYER (new_item);
|
|
|
|
|
|
|
|
new_text_layer->text =
|
|
|
|
GIMP_TEXT (gimp_config_duplicate (G_OBJECT (text_layer->text)));
|
|
|
|
|
|
|
|
g_signal_connect_object (new_text_layer->text, "notify",
|
|
|
|
G_CALLBACK (gimp_text_layer_notify_text),
|
|
|
|
new_text_layer, G_CONNECT_SWAPPED);
|
|
|
|
|
|
|
|
return new_item;
|
|
|
|
}
|
|
|
|
|
2003-01-31 17:22:42 +08:00
|
|
|
GimpLayer *
|
2003-01-31 21:36:27 +08:00
|
|
|
gimp_text_layer_new (GimpImage *image,
|
2003-01-31 17:22:42 +08:00
|
|
|
GimpText *text)
|
|
|
|
{
|
|
|
|
GimpTextLayer *layer;
|
|
|
|
|
2003-01-31 21:36:27 +08:00
|
|
|
g_return_val_if_fail (GIMP_IS_IMAGE (image), NULL);
|
2003-01-31 17:22:42 +08:00
|
|
|
g_return_val_if_fail (GIMP_IS_TEXT (text), NULL);
|
|
|
|
|
|
|
|
if (!text->text)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
layer = g_object_new (GIMP_TYPE_TEXT_LAYER, NULL);
|
|
|
|
|
2003-02-01 02:57:10 +08:00
|
|
|
gimp_drawable_configure (GIMP_DRAWABLE (layer),
|
|
|
|
image,
|
|
|
|
0, 0, 0, 0,
|
|
|
|
gimp_image_base_type_with_alpha (image),
|
|
|
|
NULL);
|
2003-01-31 17:22:42 +08:00
|
|
|
|
2003-02-01 00:37:03 +08:00
|
|
|
layer->text = g_object_ref (text);
|
|
|
|
|
2003-02-08 01:01:38 +08:00
|
|
|
if (! gimp_text_layer_render (layer))
|
2003-01-31 17:22:42 +08:00
|
|
|
{
|
|
|
|
g_object_unref (layer);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2003-02-06 02:23:58 +08:00
|
|
|
g_signal_connect_object (text, "notify",
|
|
|
|
G_CALLBACK (gimp_text_layer_notify_text),
|
|
|
|
layer, G_CONNECT_SWAPPED);
|
|
|
|
|
2003-01-31 17:22:42 +08:00
|
|
|
return GIMP_LAYER (layer);
|
|
|
|
}
|
|
|
|
|
2003-02-01 00:37:03 +08:00
|
|
|
GimpText *
|
|
|
|
gimp_text_layer_get_text (GimpTextLayer *layer)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (GIMP_IS_TEXT_LAYER (layer), NULL);
|
|
|
|
|
|
|
|
return layer->text;
|
|
|
|
}
|
|
|
|
|
2003-02-06 02:23:58 +08:00
|
|
|
static void
|
|
|
|
gimp_text_layer_notify_text (GimpTextLayer *layer)
|
|
|
|
{
|
|
|
|
if (layer->idle_render_id)
|
2003-02-08 01:01:38 +08:00
|
|
|
g_source_remove (layer->idle_render_id);
|
2003-02-06 02:23:58 +08:00
|
|
|
|
|
|
|
layer->idle_render_id =
|
|
|
|
g_idle_add_full (G_PRIORITY_LOW,
|
|
|
|
(GSourceFunc) gimp_text_layer_idle_render, layer,
|
|
|
|
NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gimp_text_layer_idle_render (GimpTextLayer *layer)
|
|
|
|
{
|
|
|
|
layer->idle_render_id = 0;
|
|
|
|
|
|
|
|
gimp_text_layer_render (layer);
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2003-01-31 21:36:27 +08:00
|
|
|
static gboolean
|
|
|
|
gimp_text_layer_render (GimpTextLayer *layer)
|
2003-01-31 17:22:42 +08:00
|
|
|
{
|
2003-02-01 02:57:10 +08:00
|
|
|
GimpImage *image;
|
|
|
|
GimpDrawable *drawable;
|
2003-02-05 16:29:12 +08:00
|
|
|
GimpTextLayout *layout;
|
|
|
|
gint width;
|
|
|
|
gint height;
|
2003-01-31 17:22:42 +08:00
|
|
|
|
2003-02-08 01:01:38 +08:00
|
|
|
image = gimp_item_get_image (GIMP_ITEM (layer));
|
|
|
|
drawable = GIMP_DRAWABLE (layer);
|
2003-01-31 17:22:42 +08:00
|
|
|
|
2003-02-05 16:29:12 +08:00
|
|
|
layout = gimp_text_layout_new (layer->text, image);
|
|
|
|
|
|
|
|
gimp_text_layout_get_size (layout, &width, &height);
|
|
|
|
|
2003-02-08 01:01:38 +08:00
|
|
|
if (gimp_text_layout_get_size (layout, &width, &height))
|
2003-01-31 21:36:27 +08:00
|
|
|
{
|
2003-02-08 01:01:38 +08:00
|
|
|
if (width != gimp_drawable_width (drawable) ||
|
|
|
|
height != gimp_drawable_height (drawable))
|
|
|
|
{
|
|
|
|
gimp_drawable_update (drawable,
|
|
|
|
0, 0,
|
|
|
|
gimp_drawable_width (drawable),
|
|
|
|
gimp_drawable_height (drawable));
|
|
|
|
|
|
|
|
drawable->width = width;
|
|
|
|
drawable->height = height;
|
|
|
|
|
|
|
|
if (drawable->tiles)
|
|
|
|
tile_manager_destroy (drawable->tiles);
|
|
|
|
|
|
|
|
drawable->tiles = tile_manager_new (width, height, drawable->bytes);
|
|
|
|
|
|
|
|
gimp_viewable_size_changed (GIMP_VIEWABLE (layer));
|
|
|
|
}
|
2003-01-31 21:36:27 +08:00
|
|
|
}
|
2003-02-01 02:57:10 +08:00
|
|
|
|
2003-02-05 22:49:48 +08:00
|
|
|
gimp_object_set_name_safe (GIMP_OBJECT (layer), layer->text->text);
|
2003-01-31 21:36:27 +08:00
|
|
|
|
2003-02-05 16:29:12 +08:00
|
|
|
gimp_text_layer_render_layout (layer, layout);
|
2003-01-31 21:36:27 +08:00
|
|
|
g_object_unref (layout);
|
2003-01-31 17:22:42 +08:00
|
|
|
|
2003-02-01 00:37:03 +08:00
|
|
|
gimp_image_flush (image);
|
|
|
|
|
2003-02-08 01:01:38 +08:00
|
|
|
return (width > 0 && height > 0);
|
2003-01-31 17:22:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2003-02-05 16:29:12 +08:00
|
|
|
gimp_text_layer_render_layout (GimpTextLayer *layer,
|
|
|
|
GimpTextLayout *layout)
|
2003-01-31 17:22:42 +08:00
|
|
|
{
|
|
|
|
GimpDrawable *drawable = GIMP_DRAWABLE (layer);
|
|
|
|
TileManager *mask;
|
|
|
|
PixelRegion textPR;
|
|
|
|
PixelRegion maskPR;
|
|
|
|
gint width;
|
|
|
|
gint height;
|
|
|
|
|
|
|
|
gimp_drawable_fill (drawable, &layer->text->color);
|
|
|
|
|
|
|
|
width = gimp_drawable_width (drawable);
|
|
|
|
height = gimp_drawable_height (drawable);
|
|
|
|
|
2003-02-08 01:01:38 +08:00
|
|
|
mask = gimp_text_layout_render (layout, width, height);
|
2003-01-31 17:22:42 +08:00
|
|
|
|
2003-02-08 01:01:38 +08:00
|
|
|
pixel_region_init (&textPR, drawable->tiles, 0, 0, width, height, TRUE);
|
|
|
|
pixel_region_init (&maskPR, mask, 0, 0, width, height, FALSE);
|
2003-01-31 17:22:42 +08:00
|
|
|
|
|
|
|
apply_mask_to_region (&textPR, &maskPR, OPAQUE_OPACITY);
|
|
|
|
|
|
|
|
tile_manager_destroy (mask);
|
2003-02-08 01:01:38 +08:00
|
|
|
|
|
|
|
gimp_drawable_update (drawable, 0, 0, width, height);
|
2003-01-31 17:22:42 +08:00
|
|
|
}
|