Issue #9987: text related functions crash using string for font name.

- Fonctions were renamed: s/gimp_text_fontname/gimp_text_font/ and
  s/gimp_text_get_extents_fontname/gimp_text_get_extents_font/
- The size_type arguments were removed. Even in 2.10, this argument was marked
  as "dead" and ignored. It was only kept for API compatibility.
- The font name (string) was replaced by a GimpFont argument.

gimp_text_font() is easily tested in the Python console with:

> Gimp.text_font(Gimp.list_images()[0], None, 10, 40, "Hello World!", 1.0, True, 100, Gimp.context_get_font())

And gimp_text_get_extents_font() with:

> Gimp.text_get_extents_font("Hello World!", 100, Gimp.context_get_font())
This commit is contained in:
Jehan 2023-10-02 20:38:18 +02:00
parent 0a77a8492f
commit 24a85eebd6
8 changed files with 150 additions and 212 deletions

View File

@ -35,6 +35,7 @@
#include "core/gimpimage.h"
#include "core/gimplayer.h"
#include "core/gimpparamspecs.h"
#include "text/gimpfont.h"
#include "text/gimptext-compat.h"
#include "gimppdb.h"
@ -44,12 +45,12 @@
static GimpValueArray *
text_fontname_invoker (GimpProcedure *procedure,
Gimp *gimp,
GimpContext *context,
GimpProgress *progress,
const GimpValueArray *args,
GError **error)
text_font_invoker (GimpProcedure *procedure,
Gimp *gimp,
GimpContext *context,
GimpProgress *progress,
const GimpValueArray *args,
GError **error)
{
gboolean success = TRUE;
GimpValueArray *return_vals;
@ -61,7 +62,7 @@ text_fontname_invoker (GimpProcedure *procedure,
gint border;
gboolean antialias;
gdouble size;
const gchar *fontname;
GimpFont *font;
GimpLayer *text_layer = NULL;
image = g_value_get_object (gimp_value_array_index (args, 0));
@ -72,7 +73,7 @@ text_fontname_invoker (GimpProcedure *procedure,
border = g_value_get_int (gimp_value_array_index (args, 5));
antialias = g_value_get_boolean (gimp_value_array_index (args, 6));
size = g_value_get_double (gimp_value_array_index (args, 7));
fontname = g_value_get_string (gimp_value_array_index (args, 9));
font = g_value_get_object (gimp_value_array_index (args, 8));
if (success)
{
@ -83,15 +84,9 @@ text_fontname_invoker (GimpProcedure *procedure,
success = FALSE;
if (success)
{
gchar *real_fontname = g_strdup_printf ("%s %d", fontname, (gint) size);
text_layer = text_render (image, drawable, context,
x, y, real_fontname, text,
border, antialias);
g_free (real_fontname);
}
text_layer = text_render (image, drawable, context,
x, y, font, size, text,
border, antialias);
}
return_vals = gimp_procedure_get_return_values (procedure, success,
@ -104,18 +99,18 @@ text_fontname_invoker (GimpProcedure *procedure,
}
static GimpValueArray *
text_get_extents_fontname_invoker (GimpProcedure *procedure,
Gimp *gimp,
GimpContext *context,
GimpProgress *progress,
const GimpValueArray *args,
GError **error)
text_get_extents_font_invoker (GimpProcedure *procedure,
Gimp *gimp,
GimpContext *context,
GimpProgress *progress,
const GimpValueArray *args,
GError **error)
{
gboolean success = TRUE;
GimpValueArray *return_vals;
const gchar *text;
gdouble size;
const gchar *fontname;
GimpFont *font;
gint width = 0;
gint height = 0;
gint ascent = 0;
@ -123,18 +118,14 @@ text_get_extents_fontname_invoker (GimpProcedure *procedure,
text = g_value_get_string (gimp_value_array_index (args, 0));
size = g_value_get_double (gimp_value_array_index (args, 1));
fontname = g_value_get_string (gimp_value_array_index (args, 3));
font = g_value_get_object (gimp_value_array_index (args, 2));
if (success)
{
gchar *real_fontname = g_strdup_printf ("%s %d", fontname, (gint) size);
success = text_get_extents (gimp,
real_fontname, text,
font, size, text,
&width, &height,
&ascent, &descent);
g_free (real_fontname);
}
return_vals = gimp_procedure_get_return_values (procedure, success,
@ -157,14 +148,15 @@ register_text_tool_procs (GimpPDB *pdb)
GimpProcedure *procedure;
/*
* gimp-text-fontname
* gimp-text-font
*/
procedure = gimp_procedure_new (text_fontname_invoker);
procedure = gimp_procedure_new (text_font_invoker);
gimp_object_set_static_name (GIMP_OBJECT (procedure),
"gimp-text-fontname");
"gimp-text-font");
gimp_procedure_set_static_help (procedure,
"Add text at the specified location as a floating selection or a new layer.",
"This tool requires a fontname matching an installed PangoFT2 font. You can specify the fontsize in units of pixels or points, and the appropriate metric is specified using the size_type argument. The x and y parameters together control the placement of the new text by specifying the upper left corner of the text bounding box. If the specified drawable parameter is valid, the text will be created as a floating selection attached to the drawable. If the drawable parameter is not valid (%NULL), the text will appear as a new layer. Finally, a border can be specified around the final rendered text. The border is measured in pixels. Parameter size-type is not used and is currently ignored. If you need to display a font in points, divide the size in points by 72.0 and multiply it by the image's vertical resolution.",
"The x and y parameters together control the placement of the new text by specifying the upper left corner of the text bounding box. If the specified drawable parameter is valid, the text will be created as a floating selection attached to the drawable. If the drawable parameter is not valid (%NULL), the text will appear as a new layer. Finally, a border can be specified around the final rendered text. The border is measured in pixels.\n"
"The size is always in pixels. If you need to display a font in points, divide the size in points by 72.0 and multiply it by the image's vertical resolution.",
NULL);
gimp_procedure_set_static_attribution (procedure,
"Martin Edlman & Sven Neumann",
@ -216,23 +208,15 @@ register_text_tool_procs (GimpPDB *pdb)
gimp_procedure_add_argument (procedure,
g_param_spec_double ("size",
"size",
"The size of text in either pixels or points",
"The size of text in pixels",
0, G_MAXDOUBLE, 0,
GIMP_PARAM_READWRITE));
gimp_procedure_add_argument (procedure,
g_param_spec_enum ("size-type",
"size type",
"The units of specified size",
GIMP_TYPE_SIZE_TYPE,
GIMP_PIXELS,
GIMP_PARAM_READWRITE));
gimp_procedure_add_argument (procedure,
gimp_param_spec_string ("fontname",
"fontname",
"The name of the font",
FALSE, FALSE, FALSE,
NULL,
GIMP_PARAM_READWRITE));
gimp_param_spec_font ("font",
"font",
"The font",
FALSE,
GIMP_PARAM_READWRITE));
gimp_procedure_add_return_value (procedure,
gimp_param_spec_layer ("text-layer",
"text layer",
@ -243,14 +227,15 @@ register_text_tool_procs (GimpPDB *pdb)
g_object_unref (procedure);
/*
* gimp-text-get-extents-fontname
* gimp-text-get-extents-font
*/
procedure = gimp_procedure_new (text_get_extents_fontname_invoker);
procedure = gimp_procedure_new (text_get_extents_font_invoker);
gimp_object_set_static_name (GIMP_OBJECT (procedure),
"gimp-text-get-extents-fontname");
"gimp-text-get-extents-font");
gimp_procedure_set_static_help (procedure,
"Get extents of the bounding box for the specified text.",
"This tool returns the width and height of a bounding box for the specified text string with the specified font information. Ascent and descent for the specified font are returned as well. Parameter size-type is not used and is currently ignored. If you need to display a font in points, divide the size in points by 72.0 and multiply it by the vertical resolution of the image you are taking into account.",
"This tool returns the width and height of a bounding box for the specified text string with the specified font information. Ascent and descent for the specified font are returned as well.\n"
"The size is always in pixels. If you need to display a font in points, divide the size in points by 72.0 and multiply it by the vertical resolution of the image you are taking into account.",
NULL);
gimp_procedure_set_static_attribution (procedure,
"Martin Edlman & Sven Neumann",
@ -270,19 +255,11 @@ register_text_tool_procs (GimpPDB *pdb)
0, G_MAXDOUBLE, 0,
GIMP_PARAM_READWRITE));
gimp_procedure_add_argument (procedure,
g_param_spec_enum ("size-type",
"size type",
"The units of specified size",
GIMP_TYPE_SIZE_TYPE,
GIMP_PIXELS,
GIMP_PARAM_READWRITE));
gimp_procedure_add_argument (procedure,
gimp_param_spec_string ("fontname",
"fontname",
"The name of the font",
FALSE, FALSE, FALSE,
NULL,
GIMP_PARAM_READWRITE));
gimp_param_spec_font ("font",
"font",
"The name of the font",
FALSE,
GIMP_PARAM_READWRITE));
gimp_procedure_add_return_value (procedure,
g_param_spec_int ("width",
"width",

View File

@ -38,6 +38,7 @@
#include "core/gimpimage-undo.h"
#include "core/gimplayer-floating-selection.h"
#include "gimpfont.h"
#include "gimptext.h"
#include "gimptext-compat.h"
#include "gimptextlayer.h"
@ -51,24 +52,22 @@ text_render (GimpImage *image,
GimpContext *context,
gint text_x,
gint text_y,
const gchar *fontname,
GimpFont *font,
gdouble font_size,
const gchar *text,
gint border,
gboolean antialias)
{
PangoFontDescription *desc;
GimpText *gtext;
GimpLayer *layer;
GimpRGB color;
gchar *font;
gdouble size;
GimpText *gtext;
GimpLayer *layer;
GimpRGB color;
g_return_val_if_fail (GIMP_IS_IMAGE (image), NULL);
g_return_val_if_fail (drawable == NULL || GIMP_IS_DRAWABLE (drawable), NULL);
g_return_val_if_fail (drawable == NULL ||
gimp_item_is_attached (GIMP_ITEM (drawable)), NULL);
g_return_val_if_fail (GIMP_IS_CONTEXT (context), NULL);
g_return_val_if_fail (fontname != NULL, NULL);
g_return_val_if_fail (GIMP_IS_FONT (font), NULL);
g_return_val_if_fail (text != NULL, NULL);
if (! gimp_data_factory_data_wait (image->gimp->font_factory))
@ -77,27 +76,17 @@ text_render (GimpImage *image,
if (border < 0)
border = 0;
desc = pango_font_description_from_string (fontname);
size = PANGO_PIXELS (pango_font_description_get_size (desc));
pango_font_description_unset_fields (desc, PANGO_FONT_MASK_SIZE);
font = pango_font_description_to_string (desc);
pango_font_description_free (desc);
gimp_context_get_foreground (context, &color);
gtext = g_object_new (GIMP_TYPE_TEXT,
"text", text,
"font", font,
"font-size", size,
"font-size", font_size,
"antialias", antialias,
"border", border,
"color", &color,
NULL);
g_free (font);
layer = gimp_text_layer_new (image, gtext);
g_object_unref (gtext);
@ -138,7 +127,8 @@ text_render (GimpImage *image,
gboolean
text_get_extents (Gimp *gimp,
const gchar *fontname,
GimpFont *font,
gdouble font_size,
const gchar *text,
gint *width,
gint *height,
@ -150,9 +140,10 @@ text_get_extents (Gimp *gimp,
PangoLayout *layout;
PangoFontMap *fontmap;
PangoRectangle rect;
gchar *real_fontname;
g_return_val_if_fail (GIMP_IS_GIMP (gimp), FALSE);
g_return_val_if_fail (fontname != NULL, FALSE);
g_return_val_if_fail (GIMP_IS_FONT (font), NULL);
g_return_val_if_fail (text != NULL, FALSE);
if (! gimp_data_factory_data_wait (gimp->font_factory))
@ -171,9 +162,11 @@ text_get_extents (Gimp *gimp,
layout = pango_layout_new (context);
g_object_unref (context);
font_desc = pango_font_description_from_string (fontname);
real_fontname = g_strdup_printf ("%s %d", gimp_font_get_lookup_name (font), (gint) font_size);
font_desc = pango_font_description_from_string (real_fontname);
pango_layout_set_font_description (layout, font_desc);
pango_font_description_free (font_desc);
g_free (real_fontname);
pango_layout_set_text (layout, text, -1);

View File

@ -29,12 +29,14 @@ GimpLayer * text_render (GimpImage *image,
GimpContext *context,
gint text_x,
gint text_y,
const gchar *fontname,
GimpFont *font,
gdouble font_size,
const gchar *text,
gint border,
gboolean antialias);
gboolean text_get_extents (Gimp *gimp,
const gchar *fontname,
GimpFont *font,
gdouble font_size,
const gchar *text,
gint *width,
gint *height,

View File

@ -855,8 +855,8 @@ EXPORTS
gimp_smudge
gimp_smudge_default
gimp_temp_file
gimp_text_fontname
gimp_text_get_extents_fontname
gimp_text_font
gimp_text_get_extents_font
gimp_text_layer_get_antialias
gimp_text_layer_get_base_direction
gimp_text_layer_get_by_id

View File

@ -37,7 +37,7 @@
/**
* gimp_text_fontname:
* gimp_text_font:
* @image: The image.
* @drawable: (nullable): The affected drawable: (%NULL for a new text layer).
* @x: The x coordinate for the left of the text bounding box.
@ -45,41 +45,36 @@
* @text: The text to generate (in UTF-8 encoding).
* @border: The size of the border.
* @antialias: Antialiasing.
* @size: The size of text in either pixels or points.
* @size_type: The units of specified size.
* @fontname: The name of the font.
* @size: The size of text in pixels.
* @font: The font.
*
* Add text at the specified location as a floating selection or a new
* layer.
*
* This tool requires a fontname matching an installed PangoFT2 font.
* You can specify the fontsize in units of pixels or points, and the
* appropriate metric is specified using the size_type argument. The x
* and y parameters together control the placement of the new text by
* specifying the upper left corner of the text bounding box. If the
* specified drawable parameter is valid, the text will be created as a
* floating selection attached to the drawable. If the drawable
* parameter is not valid (%NULL), the text will appear as a new layer.
* Finally, a border can be specified around the final rendered text.
* The border is measured in pixels. Parameter size-type is not used
* and is currently ignored. If you need to display a font in points,
* divide the size in points by 72.0 and multiply it by the image's
* vertical resolution.
* The x and y parameters together control the placement of the new
* text by specifying the upper left corner of the text bounding box.
* If the specified drawable parameter is valid, the text will be
* created as a floating selection attached to the drawable. If the
* drawable parameter is not valid (%NULL), the text will appear as a
* new layer. Finally, a border can be specified around the final
* rendered text. The border is measured in pixels.
* The size is always in pixels. If you need to display a font in
* points, divide the size in points by 72.0 and multiply it by the
* image's vertical resolution.
*
* Returns: (nullable) (transfer none):
* The new text layer or %NULL if no layer was created.
**/
GimpLayer *
gimp_text_fontname (GimpImage *image,
GimpDrawable *drawable,
gdouble x,
gdouble y,
const gchar *text,
gint border,
gboolean antialias,
gdouble size,
GimpSizeType size_type,
const gchar *fontname)
gimp_text_font (GimpImage *image,
GimpDrawable *drawable,
gdouble x,
gdouble y,
const gchar *text,
gint border,
gboolean antialias,
gdouble size,
GimpFont *font)
{
GimpValueArray *args;
GimpValueArray *return_vals;
@ -94,12 +89,11 @@ gimp_text_fontname (GimpImage *image,
G_TYPE_INT, border,
G_TYPE_BOOLEAN, antialias,
G_TYPE_DOUBLE, size,
GIMP_TYPE_SIZE_TYPE, size_type,
G_TYPE_STRING, fontname,
GIMP_TYPE_FONT, font,
G_TYPE_NONE);
return_vals = gimp_pdb_run_procedure_array (gimp_get_pdb (),
"gimp-text-fontname",
"gimp-text-font",
args);
gimp_value_array_unref (args);
@ -112,11 +106,10 @@ gimp_text_fontname (GimpImage *image,
}
/**
* gimp_text_get_extents_fontname:
* gimp_text_get_extents_font:
* @text: The text to generate (in UTF-8 encoding).
* @size: The size of text in either pixels or points.
* @size_type: The units of specified size.
* @fontname: The name of the font.
* @font: The name of the font.
* @width: (out): The width of the specified font.
* @height: (out): The height of the specified font.
* @ascent: (out): The ascent of the specified font.
@ -126,23 +119,21 @@ gimp_text_fontname (GimpImage *image,
*
* This tool returns the width and height of a bounding box for the
* specified text string with the specified font information. Ascent
* and descent for the specified font are returned as well. Parameter
* size-type is not used and is currently ignored. If you need to
* display a font in points, divide the size in points by 72.0 and
* multiply it by the vertical resolution of the image you are taking
* into account.
* and descent for the specified font are returned as well.
* The size is always in pixels. If you need to display a font in
* points, divide the size in points by 72.0 and multiply it by the
* vertical resolution of the image you are taking into account.
*
* Returns: TRUE on success.
**/
gboolean
gimp_text_get_extents_fontname (const gchar *text,
gdouble size,
GimpSizeType size_type,
const gchar *fontname,
gint *width,
gint *height,
gint *ascent,
gint *descent)
gimp_text_get_extents_font (const gchar *text,
gdouble size,
GimpFont *font,
gint *width,
gint *height,
gint *ascent,
gint *descent)
{
GimpValueArray *args;
GimpValueArray *return_vals;
@ -151,12 +142,11 @@ gimp_text_get_extents_fontname (const gchar *text,
args = gimp_value_array_new_from_types (NULL,
G_TYPE_STRING, text,
G_TYPE_DOUBLE, size,
GIMP_TYPE_SIZE_TYPE, size_type,
G_TYPE_STRING, fontname,
GIMP_TYPE_FONT, font,
G_TYPE_NONE);
return_vals = gimp_pdb_run_procedure_array (gimp_get_pdb (),
"gimp-text-get-extents-fontname",
"gimp-text-get-extents-font",
args);
gimp_value_array_unref (args);

View File

@ -32,24 +32,22 @@ G_BEGIN_DECLS
/* For information look into the C source or the html documentation */
GimpLayer* gimp_text_fontname (GimpImage *image,
GimpDrawable *drawable,
gdouble x,
gdouble y,
const gchar *text,
gint border,
gboolean antialias,
gdouble size,
GimpSizeType size_type,
const gchar *fontname);
gboolean gimp_text_get_extents_fontname (const gchar *text,
gdouble size,
GimpSizeType size_type,
const gchar *fontname,
gint *width,
gint *height,
gint *ascent,
gint *descent);
GimpLayer* gimp_text_font (GimpImage *image,
GimpDrawable *drawable,
gdouble x,
gdouble y,
const gchar *text,
gint border,
gboolean antialias,
gdouble size,
GimpFont *font);
gboolean gimp_text_get_extents_font (const gchar *text,
gdouble size,
GimpFont *font,
gint *width,
gint *height,
gint *ascent,
gint *descent);
G_END_DECLS

View File

@ -16,24 +16,21 @@
# "Perlized" from C source by Manish Singh <yosh@gimp.org>
sub text_fontname {
sub text_font {
$blurb = <<'BLURB';
Add text at the specified location as a floating selection or a new layer.
BLURB
$help = <<'HELP';
This tool requires a fontname matching an installed PangoFT2 font.
You can specify the fontsize in units of pixels
or points, and the appropriate metric is specified using the size_type
argument. The x and y parameters together control the placement of the new
text by specifying the upper left corner of the text bounding box. If the
specified drawable parameter is valid, the text will be created as a floating
selection attached to the drawable. If the drawable parameter is not valid
(%NULL), the text will appear as a new layer. Finally, a border can be specified
around the final rendered text. The border is measured in pixels. Parameter
size-type is not used and is currently ignored. If you need to display a font
in points, divide the size in points by 72.0 and multiply it by the image's
vertical resolution.
The x and y parameters together control the placement of the new text by
specifying the upper left corner of the text bounding box. If the specified
drawable parameter is valid, the text will be created as a floating selection
attached to the drawable. If the drawable parameter is not valid (%NULL), the
text will appear as a new layer. Finally, a border can be specified around the
final rendered text. The border is measured in pixels.
The size is always in pixels. If you need to display a font in points, divide
the size in points by 72.0 and multiply it by the image's vertical resolution.
HELP
&std_pdb_misc;
@ -57,11 +54,9 @@ HELP
{ name => 'antialias', type => 'boolean',
desc => 'Antialiasing' },
{ name => 'size', type => '0 < float',
desc => 'The size of text in either pixels or points' },
{ name => 'size_type', type => 'enum GimpSizeType', dead => 1,
desc => 'The units of specified size' },
{ name => 'fontname', type => 'string',
desc => 'The name of the font' }
desc => 'The size of text in pixels' },
{ name => 'font', type => 'font',
desc => 'The font' }
);
@outargs = (
@ -79,29 +74,24 @@ HELP
success = FALSE;
if (success)
{
gchar *real_fontname = g_strdup_printf ("%s %d", fontname, (gint) size);
text_layer = text_render (image, drawable, context,
x, y, real_fontname, text,
border, antialias);
g_free (real_fontname);
}
text_layer = text_render (image, drawable, context,
x, y, font, size, text,
border, antialias);
}
CODE
);
}
sub text_get_extents_fontname {
sub text_get_extents_font {
$blurb = 'Get extents of the bounding box for the specified text.';
$help = <<'HELP';
This tool returns the width and height of a bounding box for the specified text
string with the specified font information. Ascent and descent for the
specified font are returned as well. Parameter size-type is not used and is
currently ignored. If you need to display a font in points, divide the
size in points by 72.0 and multiply it by the vertical resolution of the
specified font are returned as well.
The size is always in pixels. If you need to display a font in points, divide
the size in points by 72.0 and multiply it by the vertical resolution of the
image you are taking into account.
HELP
@ -114,9 +104,7 @@ HELP
desc => 'The text to generate (in UTF-8 encoding)' },
{ name => 'size', type => '0 < float',
desc => 'The size of text in either pixels or points' },
{ name => 'size_type', type => 'enum GimpSizeType', dead => 1,
desc => 'The units of specified size' },
{ name => 'fontname', type => 'string',
{ name => 'font', type => 'font',
desc => 'The name of the font' }
);
@ -134,14 +122,10 @@ HELP
%invoke = (
code => <<'CODE'
{
gchar *real_fontname = g_strdup_printf ("%s %d", fontname, (gint) size);
success = text_get_extents (gimp,
real_fontname, text,
font, size, text,
&width, &height,
&ascent, &descent);
g_free (real_fontname);
}
CODE
);
@ -152,8 +136,8 @@ CODE
"text/gimptext-compat.h"
"gimppdb-utils.h");
@procs = qw(text_fontname
text_get_extents_fontname);
@procs = qw(text_font
text_get_extents_font);
%exports = (app => [@procs], lib => [@procs]);

View File

@ -778,9 +778,6 @@ draw_number (GimpLayer *layer,
GimpImage *image;
GimpLayer *text_layer;
gint text_width, text_height, text_ascent, descent;
gchar *fontname;
fontname = gimp_resource_get_name (GIMP_RESOURCE (font));
g_snprintf (buf, sizeof (buf), "%d", num);
@ -801,11 +798,9 @@ draw_number (GimpLayer *layer,
if ((k & 1) == 0)
delta = -delta;
success = gimp_text_get_extents_fontname (buf,
height + delta, GIMP_PIXELS,
fontname,
&text_width, &text_height,
&text_ascent, &descent);
success = gimp_text_get_extents_font (buf, height + delta, font,
&text_width, &text_height,
&text_ascent, &descent);
if (success)
{
@ -814,11 +809,10 @@ draw_number (GimpLayer *layer,
}
}
text_layer = gimp_text_fontname (image, GIMP_DRAWABLE (layer),
x, y + descent / 2,
buf, 1, FALSE,
height, GIMP_PIXELS,
fontname);
text_layer = gimp_text_font (image, GIMP_DRAWABLE (layer),
x, y + descent / 2,
buf, 1, FALSE,
height, font);
if (! text_layer)
g_message ("draw_number: Error in drawing text\n");