Commit 366beeb2 authored by Sven Neumann's avatar Sven Neumann Committed by Sven Neumann
Browse files

app/core/gimpobject.[ch] improved the code that trims the text before

2003-02-05  Sven Neumann  <sven@gimp.org>

	* app/core/gimpobject.[ch]
	* app/text/gimptextlayer.c: improved the code that trims the text
	before setting it as layer name and moved it to GimpObject as
	gimp_object_set_name_safe().

	* app/text/gimptext.[ch]: removed fixed_height and gravity again.

	* app/text/gimptextlayout.c: much simpler positioning that has the
	advantage that it actually works.

	* libgimpproxy/gimpobject.[ch]: this crap was regenerated.
parent aa9f82d1
2003-02-05 Sven Neumann <sven@gimp.org>
* app/core/gimpobject.[ch]
* app/text/gimptextlayer.c: improved the code that trims the text
before setting it as layer name and moved it to GimpObject as
gimp_object_set_name_safe().
* app/text/gimptext.[ch]: removed fixed_height and gravity again.
* app/text/gimptextlayout.c: much simpler positioning that has the
advantage that it actually works.
* libgimpproxy/gimpobject.[ch]: this crap was regenerated.
2003-02-05 Michael Natterer <mitch@gimp.org>
 
Made GimpToolOptions a GimpContext subclass and objectified
......@@ -95,7 +109,7 @@
hbox of radio buttons with icons from an enum type.
 
* app/widgets/gimppropwidgets.[ch]: added a property widget
constructors for the new enum_stock_box.
constructor for the new enum_stock_box.
 
2003-02-05 Sven Neumann <sven@gimp.org>
 
......
......@@ -237,6 +237,56 @@ gimp_object_set_name (GimpObject *object,
gimp_object_name_changed (object);
}
/* A safe version of gimp_object_set_name() that takes care
* of newlines and overly long names.
*/
#define MAX_NAME_LEN 32
void
gimp_object_set_name_safe (GimpObject *object,
const gchar *name)
{
gchar *newline;
gsize len;
g_return_if_fail (GIMP_IS_OBJECT (object));
if (name)
{
newline = strchr (name, '\n');
if (newline)
len = newline - name + 1;
else
len = strlen (name);
if (len > MAX_NAME_LEN)
newline = NULL;
if (newline || len > MAX_NAME_LEN)
{
gchar *safe_name;
len = MIN (len, MAX_NAME_LEN);
safe_name = g_new (gchar, len + 4);
memcpy (safe_name, name, len);
if (newline)
safe_name[len-1] = ' ';
g_strlcpy (safe_name + len, "...", 4);
gimp_object_set_name (object, safe_name);
g_free (safe_name);
return;
}
}
gimp_object_set_name (object, name);
}
const gchar *
gimp_object_get_name (const GimpObject *object)
{
......
......@@ -55,6 +55,8 @@ GType gimp_object_get_type (void) G_GNUC_CONST;
void gimp_object_set_name (GimpObject *object,
const gchar *name);
const gchar * gimp_object_get_name (const GimpObject *object);
void gimp_object_set_name_safe (GimpObject *object,
const gchar *name);
void gimp_object_name_changed (GimpObject *object);
gsize gimp_object_get_memsize (GimpObject *object);
......
......@@ -44,13 +44,11 @@ enum
PROP_FONT_SIZE,
PROP_FONT_SIZE_UNIT,
PROP_COLOR,
PROP_FIXED_WIDTH,
PROP_JUSTIFICATION,
PROP_INDENTATION,
PROP_LINE_SPACING,
PROP_LETTER_SPACING,
PROP_FIXED_WIDTH,
PROP_FIXED_HEIGHT,
PROP_GRAVITY,
PROP_BORDER
};
......@@ -142,6 +140,10 @@ gimp_text_class_init (GimpTextClass *klass)
"color", NULL,
&black,
0);
GIMP_CONFIG_INSTALL_PROP_INT (object_class, PROP_FIXED_WIDTH,
"fixed-width", NULL,
0, GIMP_MAX_IMAGE_SIZE, 0,
0);
GIMP_CONFIG_INSTALL_PROP_ENUM (object_class, PROP_JUSTIFICATION,
"justify",
NULL,
......@@ -163,18 +165,6 @@ gimp_text_class_init (GimpTextClass *klass)
"letter-spacing", NULL,
-8192.0, 8192.0, 0.0,
0);
GIMP_CONFIG_INSTALL_PROP_INT (object_class, PROP_FIXED_WIDTH,
"fixed-width", NULL,
0, GIMP_MAX_IMAGE_SIZE, 0,
0);
GIMP_CONFIG_INSTALL_PROP_INT (object_class, PROP_FIXED_HEIGHT,
"fixed-height", NULL,
0, GIMP_MAX_IMAGE_SIZE, 0,
0);
GIMP_CONFIG_INSTALL_PROP_ENUM (object_class, PROP_GRAVITY,
"gravity", NULL,
GIMP_TYPE_GRAVITY_TYPE, GIMP_GRAVITY_NONE,
0);
/* border does only exist to implement the old text API */
param_spec = g_param_spec_int ("border", NULL, NULL,
......@@ -228,6 +218,9 @@ gimp_text_get_property (GObject *object,
case PROP_COLOR:
g_value_set_boxed (value, &text->color);
break;
case PROP_FIXED_WIDTH:
g_value_set_int (value, text->fixed_width);
break;
case PROP_JUSTIFICATION:
g_value_set_enum (value, text->justify);
break;
......@@ -240,15 +233,6 @@ gimp_text_get_property (GObject *object,
case PROP_LETTER_SPACING:
g_value_set_double (value, text->letter_spacing);
break;
case PROP_FIXED_WIDTH:
g_value_set_int (value, text->fixed_width);
break;
case PROP_FIXED_HEIGHT:
g_value_set_int (value, text->fixed_height);
break;
case PROP_GRAVITY:
g_value_set_enum (value, text->gravity);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
break;
......@@ -284,6 +268,9 @@ gimp_text_set_property (GObject *object,
color = g_value_get_boxed (value);
text->color = *color;
break;
case PROP_FIXED_WIDTH:
text->fixed_width = g_value_get_int (value);
break;
case PROP_JUSTIFICATION:
text->justify = g_value_get_enum (value);
break;
......@@ -296,19 +283,8 @@ gimp_text_set_property (GObject *object,
case PROP_LETTER_SPACING:
text->letter_spacing = g_value_get_double (value);
break;
case PROP_FIXED_WIDTH:
text->fixed_width = g_value_get_int (value);
break;
case PROP_FIXED_HEIGHT:
text->fixed_height = g_value_get_int (value);
break;
case PROP_GRAVITY:
text->gravity = g_value_get_enum (value);
break;
case PROP_BORDER:
text->border = g_value_get_int (value);
if (text->border > 0)
text->gravity = GIMP_GRAVITY_CENTER;
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
......
......@@ -46,8 +46,6 @@ struct _GimpText
gdouble line_spacing;
gdouble letter_spacing;
gint fixed_width;
gint fixed_height;
GimpGravityType gravity;
/* for historical reasons, don't use */
gint border;
......
......@@ -205,8 +205,6 @@ gimp_text_layer_render (GimpTextLayer *layer)
GimpTextLayout *layout;
gint width;
gint height;
gchar *name;
gchar *newline;
image = gimp_item_get_image (GIMP_ITEM (layer));
......@@ -242,17 +240,7 @@ gimp_text_layer_render (GimpTextLayer *layer)
gimp_viewable_size_changed (GIMP_VIEWABLE (layer));
}
newline = strchr (layer->text->text, '\n');
if (newline)
{
name = g_strndup (layer->text->text, newline - layer->text->text);
gimp_object_set_name (GIMP_OBJECT (layer), name);
g_free (name);
}
else
{
gimp_object_set_name (GIMP_OBJECT (layer), layer->text->text);
}
gimp_object_set_name_safe (GIMP_OBJECT (layer), layer->text->text);
gimp_text_layer_render_layout (layer, layout);
g_object_unref (layout);
......
......@@ -289,123 +289,52 @@ gimp_text_layout_render (GimpTextLayout *layout)
static void
gimp_text_layout_position (GimpTextLayout *layout)
{
GimpText *text;
PangoRectangle ink;
PangoRectangle logical;
GimpGravityType gravity;
gint x1, y1;
gint x2, y2;
gboolean fixed;
layout->extents.x = 0;
layout->extents.x = 0;
layout->extents.width = 0;
layout->extents.height = 0;
text = layout->text;
fixed = (text->fixed_width > 1 && text->fixed_height > 1);
pango_layout_get_pixel_extents (layout->layout, &ink, &logical);
#ifdef VERBOSE
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);
#endif
if (!fixed)
{
if (ink.width < 1 || ink.height < 1)
return;
/* sanity checks for insane font sizes */
if (ink.width > 8192) ink.width = 8192;
if (ink.height > 8192) ink.height = 8192;
}
if (ink.width < 1 || ink.height < 1)
return;
x1 = MIN (0, logical.x);
y1 = MIN (0, logical.y);
x2 = MAX (ink.x + ink.width, logical.x + logical.width);
y2 = MAX (ink.y + ink.height, logical.y + logical.height);
layout->extents.width = fixed ? text->fixed_width : x2 - x1;
layout->extents.height = fixed ? text->fixed_height : y2 - y1;
gravity = text->gravity;
layout->extents.width = x2 - x1;
layout->extents.height = y2 - y1;
/* border should only be used by the compatibility API */
if (text->border)
{
fixed = TRUE;
gravity = GIMP_GRAVITY_CENTER;
layout->extents.width += 2 * text->border;
layout->extents.height += 2 * text->border;
}
layout->extents.x = 0;
layout->extents.y = 0;
if (!fixed)
return;
if (gravity == GIMP_GRAVITY_NONE)
if (layout->text->border > 0)
{
switch (pango_layout_get_alignment (layout->layout))
{
case PANGO_ALIGN_LEFT:
gravity = GIMP_GRAVITY_NORTH_WEST;
break;
case PANGO_ALIGN_CENTER:
gravity = GIMP_GRAVITY_NORTH;
break;
case PANGO_ALIGN_RIGHT:
gravity = GIMP_GRAVITY_NORTH_EAST;
break;
}
}
gint border = layout->text->border;
switch (gravity)
{
case GIMP_GRAVITY_NONE:
case GIMP_GRAVITY_NORTH_WEST:
case GIMP_GRAVITY_SOUTH_WEST:
case GIMP_GRAVITY_WEST:
break;
case GIMP_GRAVITY_CENTER:
case GIMP_GRAVITY_NORTH:
case GIMP_GRAVITY_SOUTH:
layout->extents.x += (layout->extents.width - logical.width) / 2;
break;
case GIMP_GRAVITY_NORTH_EAST:
case GIMP_GRAVITY_SOUTH_EAST:
case GIMP_GRAVITY_EAST:
layout->extents.x += (layout->extents.width - logical.width);
break;
layout->extents.x += border;
layout->extents.y += border;
layout->extents.width += 2 * border;
layout->extents.height += 2 * border;
}
switch (text->gravity)
{
case GIMP_GRAVITY_NONE:
case GIMP_GRAVITY_NORTH:
case GIMP_GRAVITY_NORTH_WEST:
case GIMP_GRAVITY_NORTH_EAST:
break;
case GIMP_GRAVITY_CENTER:
case GIMP_GRAVITY_WEST:
case GIMP_GRAVITY_EAST:
layout->extents.y += (layout->extents.height - logical.height) / 2;
break;
case GIMP_GRAVITY_SOUTH:
case GIMP_GRAVITY_SOUTH_WEST:
case GIMP_GRAVITY_SOUTH_EAST:
layout->extents.y += (layout->extents.height - logical.height);
break;
}
#ifdef VERBOSE
g_print ("layout extents: %d x %d @ %d, %d\n",
layout->extents.width, layout->extents.height,
layout->extents.x, layout->extents.y);
#endif
}
......
......@@ -242,6 +242,56 @@ gimp_object_set_name (GimpObject *object,
gimp_object_name_changed (object);
}
/* A safe version of gimp_object_set_name() that takes care
* of newlines and overly long names.
*/
#define MAX_NAME_LEN 32
void
gimp_object_set_name_safe (GimpObject *object,
const gchar *name)
{
gchar *newline;
gsize len;
g_return_if_fail (GIMP_IS_OBJECT (object));
if (name)
{
newline = strchr (name, '\n');
if (newline)
len = newline - name + 1;
else
len = strlen (name);
if (len > MAX_NAME_LEN)
newline = NULL;
if (newline || len > MAX_NAME_LEN)
{
gchar *safe_name;
len = MIN (len, MAX_NAME_LEN);
safe_name = g_new (gchar, len + 4);
memcpy (safe_name, name, len);
if (newline)
safe_name[len-1] = ' ';
g_strlcpy (safe_name + len, "...", 4);
gimp_object_set_name (object, safe_name);
g_free (safe_name);
return;
}
}
gimp_object_set_name (object, name);
}
const gchar *
gimp_object_get_name (const GimpObject *object)
{
......
......@@ -60,6 +60,8 @@ GType gimp_object_get_type (void) G_GNUC_CONST;
void gimp_object_set_name (GimpObject *object,
const gchar *name);
const gchar * gimp_object_get_name (const GimpObject *object);
void gimp_object_set_name_safe (GimpObject *object,
const gchar *name);
void gimp_object_name_changed (GimpObject *object);
gsize gimp_object_get_memsize (GimpObject *object);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment