new files that implement the text rendering that used to live in

2002-10-09  Sven Neumann  <sven@gimp.org>

	* app/core/gimpimage-text.[ch]: new files that implement the text
	rendering that used to live in gimptexttool.[ch].

	* app/core/Makefile.am
	* app/core/core-types.h
	* app/tools/gimptexttool.[ch]
	* tools/pdbgen/Makefile.am
	* tools/pdbgen/pdb/text_tool.pdb: changed accordingly.

	* tools/pdbgen/enums.pl
	* app/pdb/text_tool_cmds.c: regenerated.
This commit is contained in:
Sven Neumann 2002-10-09 14:00:26 +00:00 committed by Sven Neumann
parent 0ab91f9413
commit 29a75b7b07
11 changed files with 311 additions and 247 deletions

View File

@ -1,3 +1,17 @@
2002-10-09 Sven Neumann <sven@gimp.org>
* app/core/gimpimage-text.[ch]: new files that implement the text
rendering that used to live in gimptexttool.[ch].
* app/core/Makefile.am
* app/core/core-types.h
* app/tools/gimptexttool.[ch]
* tools/pdbgen/Makefile.am
* tools/pdbgen/pdb/text_tool.pdb: changed accordingly.
* tools/pdbgen/enums.pl
* app/pdb/text_tool_cmds.c: regenerated.
2002-10-09 Michael Natterer <mitch@gimp.org>
* app/gui/menus.c: some shortcut changes: Ctrl+Shift+S is now

View File

@ -115,6 +115,8 @@ libappcore_a_sources = \
gimpimage-resize.h \
gimpimage-scale.c \
gimpimage-scale.h \
gimpimage-text.c \
gimpimage-text.h \
gimpimage-undo.c \
gimpimage-undo.h \
gimpimagefile.c \

View File

@ -51,6 +51,11 @@ typedef enum /*< pdb-skip >*/
ORIENTATION_VERTICAL
} InternalOrientationType;
typedef enum
{
PIXELS,
POINTS
} SizeType;
typedef enum /*< pdb-skip >*/ /*< skip >*/
{

239
app/core/gimpimage-text.c Normal file
View File

@ -0,0 +1,239 @@
/* The GIMP -- an image manipulation program
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
*
* 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"
#include <glib-object.h>
#include <pango/pangoft2.h>
#include "core-types.h"
#include "base/pixel-region.h"
#include "base/tile-manager.h"
#include "paint-funcs/paint-funcs.h"
#include "gimpdrawable.h"
#include "gimpimage.h"
#include "gimpimage-mask.h"
#include "gimpimage-text.h"
#include "gimplayer.h"
#include "gimplayer-floating-sel.h"
#include "undo.h"
#include "libgimp/gimpintl.h"
GimpLayer *
text_render (GimpImage *gimage,
GimpDrawable *drawable,
gint text_x,
gint text_y,
const gchar *fontname,
const gchar *text,
gint border,
gint antialias)
{
PangoFontDescription *font_desc;
PangoContext *context;
PangoLayout *layout;
PangoRectangle ink;
PangoRectangle logical;
GimpImageType layer_type;
GimpLayer *layer = NULL;
gdouble xres;
gdouble yres;
g_return_val_if_fail (fontname != NULL, FALSE);
g_return_val_if_fail (text != NULL, FALSE);
if (border < 0)
border = 0;
/* determine the layer type */
if (drawable)
layer_type = gimp_drawable_type_with_alpha (drawable);
else
layer_type = gimp_image_base_type_with_alpha (gimage);
font_desc = pango_font_description_from_string (fontname);
g_return_val_if_fail (font_desc != NULL, NULL);
if (!font_desc)
return NULL;
gimp_image_get_resolution (gimage, &xres, &yres);
context = pango_ft2_get_context (xres, yres);
layout = pango_layout_new (context);
pango_layout_set_font_description (layout, font_desc);
pango_font_description_free (font_desc);
pango_layout_set_text (layout, text, -1);
pango_layout_get_pixel_extents (layout, &ink, &logical);
g_print ("ink rect: %d x %d @ %d, %d\n",
ink.width, ink.height, ink.x, ink.y);
g_print ("logical rect: %d x %d @ %d, %d\n",
logical.width, logical.height, logical.x, logical.y);
if (ink.width > 0 && ink.height > 0)
{
TileManager *mask;
PixelRegion textPR;
PixelRegion maskPR;
FT_Bitmap bitmap;
guchar *black = NULL;
guchar color[MAX_CHANNELS];
gint width;
gint height;
gint y;
bitmap.width = ink.width;
bitmap.rows = ink.height;
bitmap.pitch = ink.width;
if (bitmap.pitch & 3)
bitmap.pitch += 4 - (bitmap.pitch & 3);
bitmap.buffer = g_malloc0 (bitmap.rows * bitmap.pitch);
pango_ft2_render_layout (&bitmap, layout, - ink.x, - ink.y);
width = ink.width + 2 * border;
height = ink.height + 2 * border;
mask = tile_manager_new (width, height, 1);
pixel_region_init (&maskPR, mask, 0, 0, width, height, TRUE);
if (border)
black = g_malloc0 (width);
for (y = 0; y < border; y++)
pixel_region_set_row (&maskPR, 0, y, width, black);
for (; y < height - border; y++)
{
if (border)
{
pixel_region_set_row (&maskPR, 0, y, border, black);
pixel_region_set_row (&maskPR, width - border, y, border, black);
}
pixel_region_set_row (&maskPR, border, y, bitmap.width,
bitmap.buffer + (y - border) * bitmap.pitch);
}
for (; y < height; y++)
pixel_region_set_row (&maskPR, 0, y, width, black);
g_free (black);
g_free (bitmap.buffer);
layer = gimp_layer_new (gimage, width, height, layer_type,
_("Text Layer"),
GIMP_OPACITY_OPAQUE, GIMP_NORMAL_MODE);
/* color the layer buffer */
gimp_image_get_foreground (gimage, drawable, color);
color[GIMP_DRAWABLE (layer)->bytes - 1] = OPAQUE_OPACITY;
pixel_region_init (&textPR, GIMP_DRAWABLE (layer)->tiles,
0, 0, width, height, TRUE);
color_region (&textPR, color);
/* apply the text mask */
pixel_region_init (&textPR, GIMP_DRAWABLE (layer)->tiles,
0, 0, width, height, TRUE);
pixel_region_init (&maskPR, mask,
0, 0, width, height, FALSE);
apply_mask_to_region (&textPR, &maskPR, OPAQUE_OPACITY);
/* Start a group undo */
undo_push_group_start (gimage, TEXT_UNDO_GROUP);
/* Set the layer offsets */
GIMP_DRAWABLE (layer)->offset_x = text_x;
GIMP_DRAWABLE (layer)->offset_y = text_y;
/* If there is a selection mask clear it--
* this might not always be desired, but in general,
* it seems like the correct behavior.
*/
if (! gimp_image_mask_is_empty (gimage))
gimp_image_mask_clear (gimage);
/* If the drawable is NULL, create a new layer */
if (drawable == NULL)
gimp_image_add_layer (gimage, layer, -1);
/* Otherwise, instantiate the text as the new floating selection */
else
floating_sel_attach (layer, drawable);
/* end the group undo */
undo_push_group_end (gimage);
tile_manager_destroy (mask);
}
g_object_unref (layout);
g_object_unref (context);
return layer;
}
gboolean
text_get_extents (const gchar *fontname,
const gchar *text,
gint *width,
gint *height,
gint *ascent,
gint *descent)
{
PangoFontDescription *font_desc;
PangoContext *context;
PangoLayout *layout;
PangoRectangle rect;
g_return_val_if_fail (fontname != NULL, FALSE);
g_return_val_if_fail (text != NULL, FALSE);
font_desc = pango_font_description_from_string (fontname);
if (!font_desc)
return FALSE;
/* FIXME: resolution */
context = pango_ft2_get_context (72.0, 72.0);
layout = pango_layout_new (context);
pango_layout_set_font_description (layout, font_desc);
pango_font_description_free (font_desc);
pango_layout_set_text (layout, text, -1);
pango_layout_get_pixel_extents (layout, &rect, NULL);
if (width)
*width = rect.width;
if (height)
*height = rect.height;
if (ascent)
*ascent = -rect.y;
if (descent)
*descent = rect.height + rect.y;
g_object_unref (layout);
g_object_unref (context);
return TRUE;
}

37
app/core/gimpimage-text.h Normal file
View File

@ -0,0 +1,37 @@
/* The GIMP -- an image manipulation program
* Copyright (C) 1995 Spencer Kimball and Peter Mattisbvf
*
* 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.
*/
#ifndef __GIMP_IMAGE_TEXT_H__
#define __GIMP_IMAGE_TEXT_H__
GimpLayer * text_render (GimpImage *gimage,
GimpDrawable *drawable,
gint text_x,
gint text_y,
const gchar *fontname,
const gchar *text,
gint border,
gint antialias);
gboolean text_get_extents (const gchar *fontname,
const gchar *text,
gint *width,
gint *height,
gint *ascent,
gint *descent);
#endif /* __GIMP_IMAGE_TEXT_H__ */

View File

@ -23,18 +23,18 @@
#include <stdio.h>
#include <string.h>
#include <gtk/gtk.h>
#include <glib-object.h>
#include "libgimpbase/gimpbasetypes.h"
#include "pdb-types.h"
#include "tools/tools-types.h"
#include "procedural_db.h"
#include "core/core-types.h"
#include "core/gimpdrawable.h"
#include "core/gimpimage-text.h"
#include "core/gimpimage.h"
#include "core/gimplayer.h"
#include "tools/gimptexttool.h"
#include "libgimpbase/gimpbase.h"

View File

@ -29,16 +29,9 @@
#include "tools-types.h"
#include "base/pixel-region.h"
#include "base/tile-manager.h"
#include "paint-funcs/paint-funcs.h"
#include "core/gimpchannel.h"
#include "core/gimpimage.h"
#include "core/gimpimage-mask.h"
#include "core/gimpimage-text.h"
#include "core/gimplayer.h"
#include "core/gimplayer-floating-sel.h"
#include "core/gimptoolinfo.h"
#include "widgets/gimpfontselection.h"
@ -50,7 +43,6 @@
#include "tool_options.h"
#include "gimprc.h"
#include "undo.h"
#include "libgimp/gimpintl.h"
@ -367,205 +359,6 @@ text_tool_render (GimpTextTool *text_tool)
gimp_image_flush (gdisp->gimage);
}
GimpLayer *
text_render (GimpImage *gimage,
GimpDrawable *drawable,
gint text_x,
gint text_y,
const gchar *fontname,
const gchar *text,
gint border,
gint antialias)
{
PangoFontDescription *font_desc;
PangoContext *context;
PangoLayout *layout;
PangoRectangle ink;
PangoRectangle logical;
GimpImageType layer_type;
GimpLayer *layer = NULL;
gdouble xres;
gdouble yres;
g_return_val_if_fail (fontname != NULL, FALSE);
g_return_val_if_fail (text != NULL, FALSE);
if (border < 0)
border = 0;
/* determine the layer type */
if (drawable)
layer_type = gimp_drawable_type_with_alpha (drawable);
else
layer_type = gimp_image_base_type_with_alpha (gimage);
font_desc = pango_font_description_from_string (fontname);
g_return_val_if_fail (font_desc != NULL, NULL);
if (!font_desc)
return NULL;
gimp_image_get_resolution (gimage, &xres, &yres);
context = pango_ft2_get_context (xres, yres);
layout = pango_layout_new (context);
pango_layout_set_font_description (layout, font_desc);
pango_font_description_free (font_desc);
pango_layout_set_text (layout, text, -1);
pango_layout_get_pixel_extents (layout, &ink, &logical);
g_print ("ink rect: %d x %d @ %d, %d\n",
ink.width, ink.height, ink.x, ink.y);
g_print ("logical rect: %d x %d @ %d, %d\n",
logical.width, logical.height, logical.x, logical.y);
if (ink.width > 0 && ink.height > 0)
{
TileManager *mask;
PixelRegion textPR;
PixelRegion maskPR;
FT_Bitmap bitmap;
guchar *black = NULL;
guchar color[MAX_CHANNELS];
gint width;
gint height;
gint y;
bitmap.width = ink.width;
bitmap.rows = ink.height;
bitmap.pitch = ink.width;
if (bitmap.pitch & 3)
bitmap.pitch += 4 - (bitmap.pitch & 3);
bitmap.buffer = g_malloc0 (bitmap.rows * bitmap.pitch);
pango_ft2_render_layout (&bitmap, layout, - ink.x, - ink.y);
width = ink.width + 2 * border;
height = ink.height + 2 * border;
mask = tile_manager_new (width, height, 1);
pixel_region_init (&maskPR, mask, 0, 0, width, height, TRUE);
if (border)
black = g_malloc0 (width);
for (y = 0; y < border; y++)
pixel_region_set_row (&maskPR, 0, y, width, black);
for (; y < height - border; y++)
{
if (border)
{
pixel_region_set_row (&maskPR, 0, y, border, black);
pixel_region_set_row (&maskPR, width - border, y, border, black);
}
pixel_region_set_row (&maskPR, border, y, bitmap.width,
bitmap.buffer + (y - border) * bitmap.pitch);
}
for (; y < height; y++)
pixel_region_set_row (&maskPR, 0, y, width, black);
g_free (black);
g_free (bitmap.buffer);
layer = gimp_layer_new (gimage, width, height, layer_type,
_("Text Layer"),
GIMP_OPACITY_OPAQUE, GIMP_NORMAL_MODE);
/* color the layer buffer */
gimp_image_get_foreground (gimage, drawable, color);
color[GIMP_DRAWABLE (layer)->bytes - 1] = OPAQUE_OPACITY;
pixel_region_init (&textPR, GIMP_DRAWABLE (layer)->tiles,
0, 0, width, height, TRUE);
color_region (&textPR, color);
/* apply the text mask */
pixel_region_init (&textPR, GIMP_DRAWABLE (layer)->tiles,
0, 0, width, height, TRUE);
pixel_region_init (&maskPR, mask,
0, 0, width, height, FALSE);
apply_mask_to_region (&textPR, &maskPR, OPAQUE_OPACITY);
/* Start a group undo */
undo_push_group_start (gimage, TEXT_UNDO_GROUP);
/* Set the layer offsets */
GIMP_DRAWABLE (layer)->offset_x = text_x;
GIMP_DRAWABLE (layer)->offset_y = text_y;
/* If there is a selection mask clear it--
* this might not always be desired, but in general,
* it seems like the correct behavior.
*/
if (! gimp_image_mask_is_empty (gimage))
gimp_image_mask_clear (gimage);
/* If the drawable is NULL, create a new layer */
if (drawable == NULL)
gimp_image_add_layer (gimage, layer, -1);
/* Otherwise, instantiate the text as the new floating selection */
else
floating_sel_attach (layer, drawable);
/* end the group undo */
undo_push_group_end (gimage);
tile_manager_destroy (mask);
}
g_object_unref (layout);
g_object_unref (context);
return layer;
}
gboolean
text_get_extents (const gchar *fontname,
const gchar *text,
gint *width,
gint *height,
gint *ascent,
gint *descent)
{
PangoFontDescription *font_desc;
PangoContext *context;
PangoLayout *layout;
PangoRectangle rect;
g_return_val_if_fail (fontname != NULL, FALSE);
g_return_val_if_fail (text != NULL, FALSE);
font_desc = pango_font_description_from_string (fontname);
if (!font_desc)
return FALSE;
/* FIXME: resolution */
context = pango_ft2_get_context (72.0, 72.0);
layout = pango_layout_new (context);
pango_layout_set_font_description (layout, font_desc);
pango_font_description_free (font_desc);
pango_layout_set_text (layout, text, -1);
pango_layout_get_pixel_extents (layout, &rect, NULL);
if (width)
*width = rect.width;
if (height)
*height = rect.height;
if (ascent)
*ascent = -rect.y;
if (descent)
*descent = rect.height + rect.y;
g_object_unref (layout);
g_object_unref (context);
return TRUE;
}
/* tool options stuff */
static GimpToolOptions *

View File

@ -19,17 +19,8 @@
#ifndef __GIMP_TEXT_TOOL_H__
#define __GIMP_TEXT_TOOL_H__
#include "libgimptool/gimptool.h"
#define SUPERSAMPLE 3
typedef enum
{
PIXELS,
POINTS
} SizeType;
#define GIMP_TYPE_TEXT_TOOL (gimp_text_tool_get_type ())
#define GIMP_TEXT_TOOL(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GIMP_TYPE_TEXT_TOOL, GimpTextTool))
@ -63,20 +54,4 @@ void gimp_text_tool_register (GimpToolRegisterCallback callback,
GType gimp_text_tool_get_type (void) G_GNUC_CONST;
gboolean text_get_extents (const gchar *fontname,
const gchar *text,
gint *width,
gint *height,
gint *ascent,
gint *descent);
GimpLayer * text_render (GimpImage *gimage,
GimpDrawable *drawable,
gint text_x,
gint text_y,
const gchar *fontname,
const gchar *text,
gint border,
gint antialias);
#endif /* __GIMP_TEXT_TOOL_H__ */

View File

@ -60,8 +60,7 @@ enum_headers = \
../../app/core/core-enums.h \
../../app/core/core-types.h \
../../app/paint/paint-enums.h \
../../app/plug-in/plug-in-types.h \
../../app/tools/gimptexttool.h
../../app/plug-in/plug-in-types.h
pdb_scripts = \
pdbgen.pl \

View File

@ -355,6 +355,13 @@ package Gimp::CodeGen::enums;
VERTICAL => '1',
UNKNOWN => '2' }
},
SizeType =>
{ contig => 1,
header => 'core/core-types.h',
symbols => [ qw(PIXELS POINTS) ],
mapping => { PIXELS => '0',
POINTS => '1' }
},
GimpCloneType =>
{ contig => 1,
header => 'paint/paint-enums.h',
@ -410,13 +417,6 @@ package Gimp::CodeGen::enums;
mapping => { GIMP_RUN_INTERACTIVE => '0',
GIMP_RUN_NONINTERACTIVE => '1',
GIMP_RUN_WITH_LAST_VALS => '2' }
},
SizeType =>
{ contig => 1,
header => 'tools/gimptexttool.h',
symbols => [ qw(PIXELS POINTS) ],
mapping => { PIXELS => '0',
POINTS => '1' }
}
);

View File

@ -245,8 +245,8 @@ HELP
);
}
@headers = qw("tools/tools-types.h" "tools/gimptexttool.h"
"libgimpbase/gimpbase.h" <stdio.h> <string.h>);
@headers = qw("libgimpbase/gimpbase.h"
"core/gimpimage-text.h" <stdio.h> <string.h>);
$extra{app}->{code} = <<'CODE';