optionally allow GIMP_UNIT_PIXEL as value for GimpUnit params.

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

	* app/config/gimpconfig-params.[ch]: optionally allow GIMP_UNIT_PIXEL
	as value for GimpUnit params.

	* app/core/gimpimage-text.[ch]
	* app/core/gimptext.[ch]
	* app/tools/gimptexttool.c: moved some code around.
This commit is contained in:
Sven Neumann 2002-10-10 17:07:46 +00:00 committed by Sven Neumann
parent 920096588f
commit ce5aa0c929
11 changed files with 213 additions and 113 deletions

View file

@ -1,3 +1,12 @@
2002-10-10 Sven Neumann <sven@gimp.org>
* app/config/gimpconfig-params.[ch]: optionally allow GIMP_UNIT_PIXEL
as value for GimpUnit params.
* app/core/gimpimage-text.[ch]
* app/core/gimptext.[ch]
* app/tools/gimptexttool.c: moved some code around.
2002-10-10 Michael Natterer <mitch@gimp.org>
* app/gui/palette-import-dialog.c: s/"new_import"/"New Import"/.

View file

@ -337,6 +337,7 @@ GParamSpec *
gimp_param_spec_unit (const gchar *name,
const gchar *nick,
const gchar *blurb,
gboolean allow_pixels,
GimpUnit default_value,
GParamFlags flags)
{
@ -346,7 +347,7 @@ gimp_param_spec_unit (const gchar *name,
name, nick, blurb, flags);
pspec->default_value = default_value;
pspec->minimum = GIMP_UNIT_INCH;
pspec->minimum = allow_pixels ? GIMP_UNIT_PIXEL : GIMP_UNIT_INCH;
pspec->maximum = G_MAXINT;
return G_PARAM_SPEC (pspec);

View file

@ -76,6 +76,7 @@ GType gimp_param_unit_get_type (void) G_GNUC_CONST;
GParamSpec * gimp_param_spec_unit (const gchar *name,
const gchar *nick,
const gchar *blurb,
gboolean allow_pixels,
GimpUnit default_value,
GParamFlags flags);
@ -135,7 +136,7 @@ GParamSpec * gimp_param_spec_unit (const gchar *name,
#define GIMP_CONFIG_INSTALL_PROP_UNIT(class, id, name, default)\
g_object_class_install_property (class, id,\
gimp_param_spec_unit (name, NULL, NULL,\
default,\
FALSE, default,\
GIMP_CONFIG_PARAM_FLAGS))

View file

@ -21,6 +21,8 @@
#include <glib-object.h>
#include <pango/pangoft2.h>
#include "libgimpbase/gimpbase.h"
#include "core-types.h"
#include "base/pixel-region.h"
@ -34,6 +36,7 @@
#include "gimpimage-text.h"
#include "gimplayer.h"
#include "gimplayer-floating-sel.h"
#include "gimptext.h"
#include "undo.h"
@ -41,49 +44,60 @@
GimpLayer *
text_render (GimpImage *gimage,
GimpDrawable *drawable,
gint text_x,
gint text_y,
const gchar *fontname,
const gchar *text,
gint border,
gint antialias)
gimp_image_text_render (GimpImage *gimage,
GimpText *text)
{
PangoFontDescription *font_desc;
PangoContext *context;
PangoLayout *layout;
PangoRectangle ink;
PangoRectangle logical;
GimpImageType layer_type;
GimpLayer *layer = NULL;
gdouble factor;
gdouble xres;
gdouble yres;
gint size;
gint border;
g_return_val_if_fail (fontname != NULL, FALSE);
g_return_val_if_fail (text != NULL, FALSE);
g_return_val_if_fail (GIMP_IS_IMAGE (gimage), NULL);
g_return_val_if_fail (GIMP_IS_TEXT (text), NULL);
if (border < 0)
border = 0;
if (!text->str)
return NULL;
/* determine the layer type */
if (drawable)
layer_type = gimp_drawable_type_with_alpha (drawable);
else
layer_type = gimp_image_base_type_with_alpha (gimage);
gimp_image_get_resolution (gimage, &xres, &yres);
font_desc = pango_font_description_from_string (fontname);
switch (text->unit)
{
case GIMP_UNIT_PIXEL:
size = PANGO_SCALE * text->size;
border = text->border;
break;
default:
factor = gimp_unit_get_factor (text->unit);
g_return_val_if_fail (factor > 0.0, NULL);
size = (gdouble) PANGO_SCALE * text->size * yres / factor;
border = text->border * yres / factor;
break;
}
font_desc = pango_font_description_from_string (text->font);
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);
if (size > 1)
pango_font_description_set_size (font_desc, size);
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_set_text (layout, text->str, -1);
pango_layout_get_pixel_extents (layout, &ink, &logical);
@ -91,7 +105,13 @@ text_render (GimpImage *gimage,
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 > 8192 || ink.height > 8192)
{
g_message ("Uh, oh, insane text size.");
return NULL;
}
if (ink.width > 0 && ink.height > 0)
{
TileManager *mask;
@ -141,12 +161,14 @@ text_render (GimpImage *gimage,
g_free (black);
g_free (bitmap.buffer);
layer = gimp_layer_new (gimage, width, height, layer_type,
layer = gimp_layer_new (gimage,
width, height,
gimp_image_base_type_with_alpha (gimage),
_("Text Layer"),
GIMP_OPACITY_OPAQUE, GIMP_NORMAL_MODE);
/* color the layer buffer */
gimp_image_get_foreground (gimage, drawable, color);
gimp_image_get_foreground (gimage, GIMP_DRAWABLE (layer), color);
color[GIMP_DRAWABLE (layer)->bytes - 1] = OPAQUE_OPACITY;
pixel_region_init (&textPR, GIMP_DRAWABLE (layer)->tiles,
0, 0, width, height, TRUE);
@ -159,30 +181,6 @@ text_render (GimpImage *gimage,
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);
}
@ -192,6 +190,67 @@ text_render (GimpImage *gimage,
return layer;
}
GimpLayer *
text_render (GimpImage *gimage,
GimpDrawable *drawable,
gint text_x,
gint text_y,
const gchar *fontname,
const gchar *text,
gint border,
gint antialias)
{
GimpText *gtext;
GimpLayer *layer;
g_return_val_if_fail (fontname != NULL, FALSE);
g_return_val_if_fail (text != NULL, FALSE);
if (border < 0)
border = 0;
gtext = GIMP_TEXT (g_object_new (GIMP_TYPE_TEXT,
"text", text,
"font", fontname,
"border", border,
"unit", GIMP_UNIT_PIXEL,
NULL));
gtext->size = -1;
layer = gimp_image_text_render (gimage, gtext);
g_object_unref (gtext);
if (!layer)
return NULL;
/* 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);
return layer;
}
gboolean
text_get_extents (const gchar *fontname,
const gchar *text,

View file

@ -19,6 +19,12 @@
#ifndef __GIMP_IMAGE_TEXT_H__
#define __GIMP_IMAGE_TEXT_H__
GimpLayer * gimp_image_text_render (GimpImage *image,
GimpText *text);
/* convenience functions using the old API */
GimpLayer * text_render (GimpImage *gimage,
GimpDrawable *drawable,
gint text_x,

View file

@ -32,7 +32,8 @@ enum
PROP_TEXT,
PROP_FONT,
PROP_SIZE,
PROP_SIZE_UNIT,
PROP_BORDER,
PROP_UNIT,
PROP_LETTER_SPACING,
PROP_LINE_SPACING
};
@ -98,23 +99,28 @@ gimp_text_class_init (GimpTextClass *klass)
g_object_class_install_property (object_class, PROP_FONT, param_spec);
param_spec = g_param_spec_double ("size", NULL, NULL,
18.0, 0.0, G_MAXFLOAT,
0.0, G_MAXFLOAT, 18.0,
G_PARAM_CONSTRUCT | G_PARAM_WRITABLE);
g_object_class_install_property (object_class, PROP_SIZE, param_spec);
param_spec = gimp_param_spec_unit ("size-unit", NULL, NULL,
GIMP_UNIT_PIXEL,
param_spec = g_param_spec_double ("border", NULL, NULL,
0.0, G_MAXFLOAT, 0.0,
G_PARAM_CONSTRUCT | G_PARAM_WRITABLE);
g_object_class_install_property (object_class, PROP_BORDER, param_spec);
param_spec = gimp_param_spec_unit ("unit", NULL, NULL,
TRUE, GIMP_UNIT_PIXEL,
G_PARAM_CONSTRUCT | G_PARAM_WRITABLE);
g_object_class_install_property (object_class, PROP_SIZE_UNIT, param_spec);
g_object_class_install_property (object_class, PROP_UNIT, param_spec);
param_spec = g_param_spec_double ("letter-spacing", NULL, NULL,
1.0, 0.0, 64.0,
0.0, 64.0, 1.0,
G_PARAM_CONSTRUCT | G_PARAM_WRITABLE);
g_object_class_install_property (object_class,
PROP_LETTER_SPACING, param_spec);
param_spec = g_param_spec_double ("line-spacing", NULL, NULL,
1.0, 0.0, 64.0,
0.0, 64.0, 1.0,
G_PARAM_CONSTRUCT | G_PARAM_WRITABLE);
g_object_class_install_property (object_class,
PROP_LINE_SPACING, param_spec);
@ -160,8 +166,11 @@ gimp_text_set_property (GObject *object,
case PROP_SIZE:
text->size = g_value_get_double (value);
break;
case PROP_SIZE_UNIT:
text->size_unit = g_value_get_enum (value);
case PROP_BORDER:
text->border = g_value_get_double (value);
break;
case PROP_UNIT:
text->unit = g_value_get_int (value);
break;
case PROP_LETTER_SPACING:
text->letter_spacing = g_value_get_double (value);

View file

@ -36,7 +36,8 @@ struct _GimpText
gchar *str;
gchar *font;
gdouble size;
GimpUnit size_unit;
gdouble border;
GimpUnit unit;
gdouble letter_spacing;
gdouble line_spacing;
};

View file

@ -32,7 +32,8 @@ enum
PROP_TEXT,
PROP_FONT,
PROP_SIZE,
PROP_SIZE_UNIT,
PROP_BORDER,
PROP_UNIT,
PROP_LETTER_SPACING,
PROP_LINE_SPACING
};
@ -98,23 +99,28 @@ gimp_text_class_init (GimpTextClass *klass)
g_object_class_install_property (object_class, PROP_FONT, param_spec);
param_spec = g_param_spec_double ("size", NULL, NULL,
18.0, 0.0, G_MAXFLOAT,
0.0, G_MAXFLOAT, 18.0,
G_PARAM_CONSTRUCT | G_PARAM_WRITABLE);
g_object_class_install_property (object_class, PROP_SIZE, param_spec);
param_spec = gimp_param_spec_unit ("size-unit", NULL, NULL,
GIMP_UNIT_PIXEL,
param_spec = g_param_spec_double ("border", NULL, NULL,
0.0, G_MAXFLOAT, 0.0,
G_PARAM_CONSTRUCT | G_PARAM_WRITABLE);
g_object_class_install_property (object_class, PROP_BORDER, param_spec);
param_spec = gimp_param_spec_unit ("unit", NULL, NULL,
TRUE, GIMP_UNIT_PIXEL,
G_PARAM_CONSTRUCT | G_PARAM_WRITABLE);
g_object_class_install_property (object_class, PROP_SIZE_UNIT, param_spec);
g_object_class_install_property (object_class, PROP_UNIT, param_spec);
param_spec = g_param_spec_double ("letter-spacing", NULL, NULL,
1.0, 0.0, 64.0,
0.0, 64.0, 1.0,
G_PARAM_CONSTRUCT | G_PARAM_WRITABLE);
g_object_class_install_property (object_class,
PROP_LETTER_SPACING, param_spec);
param_spec = g_param_spec_double ("line-spacing", NULL, NULL,
1.0, 0.0, 64.0,
0.0, 64.0, 1.0,
G_PARAM_CONSTRUCT | G_PARAM_WRITABLE);
g_object_class_install_property (object_class,
PROP_LINE_SPACING, param_spec);
@ -160,8 +166,11 @@ gimp_text_set_property (GObject *object,
case PROP_SIZE:
text->size = g_value_get_double (value);
break;
case PROP_SIZE_UNIT:
text->size_unit = g_value_get_enum (value);
case PROP_BORDER:
text->border = g_value_get_double (value);
break;
case PROP_UNIT:
text->unit = g_value_get_int (value);
break;
case PROP_LETTER_SPACING:
text->letter_spacing = g_value_get_double (value);

View file

@ -36,7 +36,8 @@ struct _GimpText
gchar *str;
gchar *font;
gdouble size;
GimpUnit size_unit;
gdouble border;
GimpUnit unit;
gdouble letter_spacing;
gdouble line_spacing;
};

View file

@ -24,14 +24,16 @@
#include <gtk/gtk.h>
#include <pango/pangoft2.h>
#include "libgimpbase/gimpbase.h"
#include "libgimpwidgets/gimpwidgets.h"
#include "tools-types.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/gimptext.h"
#include "core/gimptoolinfo.h"
#include "widgets/gimpfontselection.h"
@ -44,6 +46,7 @@
#include "tool_options.h"
#include "gimprc.h"
#include "undo.h"
#include "libgimp/gimpintl.h"
@ -314,58 +317,58 @@ text_tool_cursor_update (GimpTool *tool,
static void
text_tool_render (GimpTextTool *text_tool)
{
TextOptions *options;
GimpDisplay *gdisp;
PangoFontDescription *font_desc;
gchar *fontname;
gchar *text;
gdouble border;
gdouble size;
gdouble factor;
TextOptions *options;
GimpImage *gimage;
GimpText *text;
GimpLayer *layer;
const gchar *font;
options = (TextOptions *) GIMP_TOOL (text_tool)->tool_info->tool_options;
gimage = text_tool->gdisp->gimage;
gdisp = text_tool->gdisp;
font_desc = gimp_font_selection_get_font_desc
(GIMP_FONT_SELECTION (options->font_selection));
font = gimp_font_selection_get_fontname (GIMP_FONT_SELECTION (options->font_selection));
if (!font_desc)
if (!font)
{
g_message (_("No font chosen or font invalid."));
return;
}
text = GIMP_TEXT (g_object_new (GIMP_TYPE_TEXT,
"text", ("No, you can't change this text.\n"
"Please DON'T report this bug."),
"font", font,
"size", options->size,
"border", options->border,
"unit", options->unit,
"letter-spacing", options->letter_spacing,
"line-spacing", options->line_spacing,
NULL));
size = options->size;
border = options->border;
layer = gimp_image_text_render (gimage, text);
switch (options->unit)
{
case GIMP_UNIT_PIXEL:
break;
default:
factor = (gdisp->gimage->xresolution /
gimp_unit_get_factor (options->unit));
size *= factor;
border *= factor;
break;
}
g_object_unref (text);
pango_font_description_set_size (font_desc, PANGO_SCALE * MAX (1, size));
fontname = pango_font_description_to_string (font_desc);
pango_font_description_free (font_desc);
if (!layer)
return;
text = ("No, you can't change this text.\n"
"Please DON'T report this bug."); /* FIXME */
undo_push_group_start (gimage, TEXT_UNDO_GROUP);
text_render (gdisp->gimage, gimp_image_active_drawable (gdisp->gimage),
text_tool->click_x, text_tool->click_y,
fontname, text, border,
TRUE /* antialias */);
GIMP_DRAWABLE (layer)->offset_x = text_tool->click_x;
GIMP_DRAWABLE (layer)->offset_y = text_tool->click_y;
g_free (fontname);
/* 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);
gimp_image_flush (gdisp->gimage);
floating_sel_attach (layer, gimp_image_active_drawable (gimage));
undo_push_group_end (gimage);
gimp_image_flush (gimage);
}
/* tool options stuff */
@ -513,8 +516,8 @@ text_tool_options_reset (GimpToolOptions *tool_options)
g_object_get_data (G_OBJECT (spinbutton), "set_digits");
}
gtk_adjustment_set_value (GTK_ADJUSTMENT (options->line_spacing_w),
options->line_spacing_d);
gtk_adjustment_set_value (GTK_ADJUSTMENT (options->letter_spacing_w),
options->letter_spacing_d);
gtk_adjustment_set_value (GTK_ADJUSTMENT (options->line_spacing_w),
options->line_spacing_d);
}

View file

@ -76,6 +76,7 @@ GType gimp_param_unit_get_type (void) G_GNUC_CONST;
GParamSpec * gimp_param_spec_unit (const gchar *name,
const gchar *nick,
const gchar *blurb,
gboolean allow_pixels,
GimpUnit default_value,
GParamFlags flags);
@ -135,7 +136,7 @@ GParamSpec * gimp_param_spec_unit (const gchar *name,
#define GIMP_CONFIG_INSTALL_PROP_UNIT(class, id, name, default)\
g_object_class_install_property (class, id,\
gimp_param_spec_unit (name, NULL, NULL,\
default,\
FALSE, default,\
GIMP_CONFIG_PARAM_FLAGS))