Hello there!

I've written a small patch based on the gimp-2.4.0-rc2 sources which 
implements a new gimp-text-fontname and gimp-text-get-extents-fontname 
function for the PDB. In fact, I wrote this because I needed the 
possibilities of adjusting letter/line spacing and indentation as well as the 
possibilities of directly getting access to hinting and auto-hinting in my 
gimp-ruby-scripts.

As I'm totally new to gimp development I don't really know if this was the 
right place to implement, so I just wrote two new 
functions "gimp-text-fontname-new" and "gimp-text-get-extents-fontname-new" 
and put them just in the same files where I found the other old equivalent 
functions. I hope my C knowledge wasn't that bad, but as gimp sources were 
really well arranged, it didn't take me too long to get used to the syntax.

I would be happy about some feedback because I urgently need this feature in 
the gimp.

Best regards
Marcus

diff -Naur gimp-2.4.0-rc2/app/pdb/text_tool_cmds.c gimp-2.4.0-rc2.new/app/pdb/text_tool_cmds.c
--- gimp-2.4.0-rc2/app/pdb/text_tool_cmds.c	2007-08-31 15:38:00.000000000 +0200
+++ gimp-2.4.0-rc2.new/app/pdb/text_tool_cmds.c	2007-09-26 18:47:49.000000000 +0200
@@ -96,6 +96,76 @@
 }
 
 static GValueArray *
+text_fontname_invoker_new (GimpProcedure     *procedure,
+                           Gimp              *gimp,
+                           GimpContext       *context,
+                           GimpProgress      *progress,
+                           const GValueArray *args)
+{
+  gboolean success = TRUE;
+  GValueArray *return_vals;
+  GimpImage *image;
+  GimpDrawable *drawable;
+  gdouble x;
+  gdouble y;
+  const gchar *text;
+  gint32 border;
+  gboolean antialias;
+  gdouble size;
+  gint32 size_type;
+  const gchar *fontname;
+  gboolean autohint;
+  gboolean hinting;
+  gdouble indent;
+  gdouble line_spacing;
+  gdouble letter_spacing;
+  GimpLayer *text_layer = NULL;
+
+  image = gimp_value_get_image (&args->values[0], gimp);
+  drawable = gimp_value_get_drawable (&args->values[1], gimp);
+  x = g_value_get_double (&args->values[2]);
+  y = g_value_get_double (&args->values[3]);
+  text = g_value_get_string (&args->values[4]);
+  border = g_value_get_int (&args->values[5]);
+  antialias = g_value_get_boolean (&args->values[6]);
+  size = g_value_get_double (&args->values[7]);
+  size_type = g_value_get_enum (&args->values[8]);
+  fontname = g_value_get_string (&args->values[9]);
+  hinting = g_value_get_boolean (&args->values[10]);
+  autohint = g_value_get_boolean (&args->values[11]);
+  indent = g_value_get_double (&args->values[12]);
+  line_spacing = g_value_get_double (&args->values[13]);
+  letter_spacing = g_value_get_double (&args->values[14]);
+  
+  
+
+  if (success)
+    {
+      if (drawable && ! gimp_item_is_attached (GIMP_ITEM (drawable)))
+        success = FALSE;
+
+      if (success)
+        {
+          gchar *real_fontname = g_strdup_printf ("%s %d", fontname, (gint) size);
+
+          text_layer = text_render_new (image, drawable, context,
+                                        x, y, real_fontname, text,
+                                        border, antialias, hinting, autohint,
+                                        indent, line_spacing, letter_spacing);
+
+          g_free (real_fontname);
+        }
+    }
+
+  return_vals = gimp_procedure_get_return_values (procedure, success);
+
+  if (success)
+    gimp_value_set_layer (&return_vals->values[1], text_layer);
+
+  return return_vals;
+}
+
+static GValueArray *
 text_get_extents_fontname_invoker (GimpProcedure     *procedure,
                                    Gimp              *gimp,
                                    GimpContext       *context,
@@ -143,6 +213,64 @@
 }
 
 static GValueArray *
+text_get_extents_fontname_invoker_new (GimpProcedure     *procedure,
+                                       Gimp              *gimp,
+                                       GimpContext       *context,
+                                       GimpProgress      *progress,
+                                       const GValueArray *args)
+{
+  gboolean success = TRUE;
+  GValueArray *return_vals;
+  GimpImage *image;
+  const gchar *text;
+  gdouble size;
+  gint32 size_type;
+  const gchar *fontname;
+  gboolean antialias;
+  gboolean hinting;
+  gboolean autohint;
+  gdouble indent;
+  gdouble line_spacing;
+  gdouble letter_spacing;
+  gint32 width = 0;
+  gint32 height = 0;
+
+  image = gimp_value_get_image (&args->values[0], gimp);
+  text = g_value_get_string (&args->values[1]);
+  size = g_value_get_double (&args->values[2]);
+  size_type = g_value_get_enum (&args->values[3]);
+  fontname = g_value_get_string (&args->values[4]);
+  antialias = g_value_get_boolean (&args->values[5]);
+  hinting = g_value_get_boolean (&args->values[6]);
+  autohint = g_value_get_boolean (&args->values[7]);
+  indent = g_value_get_double (&args->values[8]);
+  line_spacing = g_value_get_double(&args->values[9]);
+  letter_spacing = g_value_get_double(&args->values[10]);
+
+  if (success)
+    {
+      gchar *real_fontname = g_strdup_printf ("%s %d", fontname, (gint) size);
+
+      success = text_get_extents_new (image, real_fontname, text,
+                                      antialias, hinting, autohint,
+                                      indent, line_spacing, letter_spacing, 
+                                      &width, &height);
+
+      g_free (real_fontname);
+    }
+
+  return_vals = gimp_procedure_get_return_values (procedure, success);
+
+  if (success)
+    {
+      g_value_set_int (&return_vals->values[1], width);
+      g_value_set_int (&return_vals->values[2], height);
+    }
+
+  return return_vals;
+}
+
+static GValueArray *
 text_invoker (GimpProcedure     *procedure,
               Gimp              *gimp,
               GimpContext       *context,
@@ -364,6 +492,123 @@
   gimp_pdb_register_procedure (pdb, procedure);
   g_object_unref (procedure);
 
+
+  /*
+   * gimp-text-fontname-new
+   */
+  procedure = gimp_procedure_new (text_fontname_invoker_new);
+  gimp_object_set_static_name (GIMP_OBJECT (procedure), "gimp-text-fontname-new");
+  gimp_procedure_set_static_strings (procedure,
+                                     "gimp-text-fontname-new",
+                                     "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 (-1), the text will appear as a new layer. Aa 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. You can also enable Antialiasing and Hinting and Autohinting, likewise to the Text-Tool. Indenting, Line- and Letter-Spacing are also supported.",
+                                     "Marcus Heese",
+                                     "Spencer Kimball & Peter Mattis",
+                                     "2007",
+                                     NULL);
+  gimp_procedure_add_argument (procedure,
+                               gimp_param_spec_image_id ("image",
+                                                         "image",
+                                                         "The image",
+                                                         pdb->gimp, FALSE,
+                                                         GIMP_PARAM_READWRITE));
+  gimp_procedure_add_argument (procedure,
+                               gimp_param_spec_drawable_id ("drawable",
+                                                            "drawable",
+                                                            "The affected drawable: (-1 for a new text layer)",
+                                                            pdb->gimp, TRUE,
+                                                            GIMP_PARAM_READWRITE));
+  gimp_procedure_add_argument (procedure,
+                               g_param_spec_double ("x",
+                                                    "x",
+                                                    "The x coordinate for the left of the text bounding box",
+                                                    -G_MAXDOUBLE, G_MAXDOUBLE, 0,
+                                                    GIMP_PARAM_READWRITE));
+  gimp_procedure_add_argument (procedure,
+                               g_param_spec_double ("y",
+                                                    "y",
+                                                    "The y coordinate for the top of the text bounding box",
+                                                    -G_MAXDOUBLE, G_MAXDOUBLE, 0,
+                                                    GIMP_PARAM_READWRITE));
+  gimp_procedure_add_argument (procedure,
+                               gimp_param_spec_string ("text",
+                                                       "text",
+                                                       "The text to generate (in UTF-8 encoding)",
+                                                       FALSE, FALSE, FALSE,
+                                                       NULL,
+                                                       GIMP_PARAM_READWRITE));
+  gimp_procedure_add_argument (procedure,
+                               gimp_param_spec_int32 ("border",
+                                                      "border",
+                                                      "The size of the border",
+                                                      -1, G_MAXINT32, -1,
+                                                      GIMP_PARAM_READWRITE));
+  gimp_procedure_add_argument (procedure,
+                               g_param_spec_boolean ("antialias",
+                                                     "antialias",
+                                                     "Antialiasing",
+                                                     FALSE,
+                                                     GIMP_PARAM_READWRITE));
+  gimp_procedure_add_argument (procedure,
+                               g_param_spec_double ("size",
+                                                    "size",
+                                                    "The size of text in either pixels or points",
+                                                    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_procedure_add_argument(procedure,
+                              g_param_spec_boolean ("hinting",
+			                               "hinting",
+						       "Hinting",
+						       TRUE,
+						       GIMP_PARAM_READWRITE));
+  gimp_procedure_add_argument(procedure,
+                              g_param_spec_boolean ("autohint",
+			                               "autohint",
+						       "Auto-Hinting",
+						       FALSE,
+						       GIMP_PARAM_READWRITE));
+  gimp_procedure_add_argument(procedure,
+                              g_param_spec_double ("indent",
+			                              "indent",
+						      "Einzug der ersten Zeile",
+						      -G_MAXDOUBLE, G_MAXDOUBLE, 0,
+						      GIMP_PARAM_READWRITE));
+  gimp_procedure_add_argument(procedure,
+                              g_param_spec_double ("line-spacing",
+			                              "line spacing",
+						      "Abstand zwischen den Zeilen",
+						      -G_MAXDOUBLE, G_MAXDOUBLE, 0,
+						      GIMP_PARAM_READWRITE));
+  gimp_procedure_add_argument(procedure,
+                              g_param_spec_double ("letter-spacing",
+			                              "letter spacing",
+						      "Abstand zwischen den Buchstaben",
+						      -G_MAXDOUBLE, G_MAXDOUBLE, 0,
+						      GIMP_PARAM_READWRITE));
+  gimp_procedure_add_return_value (procedure,
+                                   gimp_param_spec_layer_id ("text-layer",
+                                                             "text layer",
+                                                             "The new text layer or -1 if no layer was created.",
+                                                             pdb->gimp, FALSE,
+                                                             GIMP_PARAM_READWRITE));
+  gimp_pdb_register_procedure (pdb, procedure);
+  g_object_unref (procedure);
+
+
   /*
    * gimp-text-get-extents-fontname
    */
@@ -432,6 +677,103 @@
   g_object_unref (procedure);
 
   /*
+   * gimp-text-get-extents-fontname-new
+   */
+  procedure = gimp_procedure_new (text_get_extents_fontname_invoker_new);
+  gimp_object_set_static_name (GIMP_OBJECT (procedure), "gimp-text-get-extents-fontname-new");
+  gimp_procedure_set_static_strings (procedure,
+                                     "gimp-text-get-extents-fontname-new",
+                                     "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.",
+                                     "Marcus Heese",
+                                     "Spencer Kimball & Peter Mattis",
+                                     "2007",
+                                     NULL);
+  gimp_procedure_add_argument (procedure,
+                               gimp_param_spec_image_id ("image",
+                                                         "image",
+                                                         "The image",
+                                                         pdb->gimp, FALSE,
+                                                         GIMP_PARAM_READWRITE));
+  gimp_procedure_add_argument (procedure,
+                               gimp_param_spec_string ("text",
+                                                       "text",
+                                                       "The text to generate (in UTF-8 encoding)",
+                                                       FALSE, FALSE, FALSE,
+                                                       NULL,
+                                                       GIMP_PARAM_READWRITE));
+  gimp_procedure_add_argument (procedure,
+                               g_param_spec_double ("size",
+                                                    "size",
+                                                    "The size of text in either pixels or points",
+                                                    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_procedure_add_argument(procedure,
+                              g_param_spec_boolean ("antialias",
+                                                       "antialias",
+                                                       "Antialiasing",
+                                                       TRUE,
+                                                       GIMP_PARAM_READWRITE));
+  gimp_procedure_add_argument(procedure,
+                              g_param_spec_boolean ("hinting",
+                                                       "hinting",
+                                                       "Hinting",
+                                                       TRUE,
+                                                       GIMP_PARAM_READWRITE));
+  gimp_procedure_add_argument(procedure,
+                              g_param_spec_boolean ("autohint",
+                                                       "autohint",
+                                                       "Auto-Hinting",
+                                                       FALSE,
+                                                       GIMP_PARAM_READWRITE));
+  gimp_procedure_add_argument(procedure,
+                              g_param_spec_double ("indent",
+                                                      "indent",
+                                                      "Einzug der ersten Zeile",
+                                                      -G_MAXDOUBLE, G_MAXDOUBLE, 0,
+                                                      GIMP_PARAM_READWRITE));
+  gimp_procedure_add_argument(procedure,
+                              g_param_spec_double ("line-spacing",
+                                                      "line spacing",
+                                                      "Abstand zwischen den Zeilen",
+                                                      -G_MAXDOUBLE, G_MAXDOUBLE, 0,
+                                                      GIMP_PARAM_READWRITE));
+  gimp_procedure_add_argument(procedure,
+                              g_param_spec_double ("letter-spacing",
+                                                      "letter spacing",
+                                                      "Abstand zwischen den Buchstaben",
+                                                      -G_MAXDOUBLE, G_MAXDOUBLE, 0,
+                                                      GIMP_PARAM_READWRITE));			    
+  gimp_procedure_add_return_value (procedure,
+                                   gimp_param_spec_int32 ("width",
+                                                          "width",
+                                                          "The width of the specified font",
+                                                          G_MININT32, G_MAXINT32, 0,
+                                                          GIMP_PARAM_READWRITE));
+  gimp_procedure_add_return_value (procedure,
+                                   gimp_param_spec_int32 ("height",
+                                                          "height",
+                                                          "The height of the specified font",
+                                                          G_MININT32, G_MAXINT32, 0,
+                                                          GIMP_PARAM_READWRITE));
+  gimp_pdb_register_procedure (pdb, procedure);
+  g_object_unref (procedure);
+
+  /*
    * gimp-text
    */
   procedure = gimp_procedure_new (text_invoker);
diff -Naur gimp-2.4.0-rc2/app/text/gimptext-compat.c gimp-2.4.0-rc2.new/app/text/gimptext-compat.c
--- gimp-2.4.0-rc2/app/text/gimptext-compat.c	2007-08-31 15:37:53.000000000 +0200
+++ gimp-2.4.0-rc2.new/app/text/gimptext-compat.c	2007-09-26 18:39:52.000000000 +0200
@@ -42,6 +42,7 @@
 #include "gimptext.h"
 #include "gimptext-compat.h"
 #include "gimptextlayer.h"
+#include "gimptextlayout.h"
 
 #include "gimp-intl.h"
 
@@ -131,6 +132,102 @@
   return layer;
 }
 
+GimpLayer *
+text_render_new (GimpImage    *image,
+                 GimpDrawable *drawable,
+                 GimpContext  *context,
+                 gint          text_x,
+                 gint          text_y,
+                 const gchar  *fontname,
+                 const gchar  *text,
+                 gint          border,
+                 gboolean      antialias,
+                 gboolean      hinting,
+                 gboolean      autohint,
+                 gdouble       indent,
+                 gdouble       line_spacing,
+                 gdouble       letter_spacing)
+
+{
+  PangoFontDescription *desc;
+  GimpText             *gtext;
+  GimpLayer            *layer;
+  GimpRGB               color;
+  gchar                *font;
+  gdouble               size;
+
+  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 (text != NULL, NULL);
+
+  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 = gimp_font_util_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,
+                        "antialias",      antialias,
+                        "border",         border,
+                        "color",          &color,
+			"hinting",        hinting,
+			"autohint",       autohint,
+			"indent",         indent,
+			"line-spacing",   line_spacing,
+			"letter-spacing", letter_spacing,
+                        NULL);
+
+  g_free (font);
+
+  layer = gimp_text_layer_new (image, gtext);
+
+  g_object_unref (gtext);
+
+  if (!layer)
+    return NULL;
+
+  /*  Start a group undo  */
+  gimp_image_undo_group_start (image, GIMP_UNDO_GROUP_TEXT,
+                               _("Add Text Layer"));
+
+  /*  Set the layer offsets  */
+  GIMP_ITEM (layer)->offset_x = text_x;
+  GIMP_ITEM (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_channel_is_empty (gimp_image_get_mask (image)))
+    gimp_channel_clear (gimp_image_get_mask (image), NULL, TRUE);
+
+  /*  If the drawable is NULL, create a new layer  */
+  if (drawable == NULL)
+    gimp_image_add_layer (image, layer, -1);
+  /*  Otherwise, instantiate the text as the new floating selection */
+  else
+    floating_sel_attach (layer, drawable);
+
+  /*  end the group undo  */
+  gimp_image_undo_group_end (image);
+
+  return layer;
+}
+
 gboolean
 text_get_extents (const gchar *fontname,
                   const gchar *text,
@@ -191,3 +288,59 @@
 
   return TRUE;
 }
+
+gboolean
+text_get_extents_new (GimpImage   *image,
+                      const gchar *fontname,
+                      const gchar *text,
+		      gboolean    antialias,
+		      gboolean    hinting,
+		      gboolean    autohint,
+		      gdouble     indent,
+		      gdouble     line_spacing,
+		      gdouble     letter_spacing,
+                      gint        *width,
+                      gint        *height)
+{
+  PangoFontDescription *desc;
+  GimpText             *gtext;
+  GimpTextLayout       *gtextlayout;
+  gchar                *font;
+  gdouble               size;
+  gint                  tmp_width;
+  gint                  tmp_height;
+  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 = gimp_font_util_pango_font_description_to_string (desc);
+
+  pango_font_description_free (desc);
+
+  gtext = g_object_new (GIMP_TYPE_TEXT,
+                        "text",           text,
+                        "font",           font,
+                        "font-size",      size,
+			"antialias",      antialias,
+			"hinting",        hinting,
+			"autohint",      autohint,
+                        "indent",         indent,
+                        "line-spacing",   line_spacing,
+                        "letter-spacing", letter_spacing,
+                        NULL);
+
+  gtextlayout = gimp_text_layout_new(gtext, image);
+  if(!gimp_text_layout_get_size(gtextlayout, &tmp_width, &tmp_height)) {
+    *width = 0;
+    *height = 0;
+  } else {
+    *width = (gdouble) tmp_width;
+    *height = (gdouble) tmp_height;
+  }
+
+  g_free (font);
+  g_object_unref (gtext);
+  g_object_unref (gtextlayout);
+
+  return TRUE;
+}
diff -Naur gimp-2.4.0-rc2/app/text/gimptext-compat.h gimp-2.4.0-rc2.new/app/text/gimptext-compat.h
--- gimp-2.4.0-rc2/app/text/gimptext-compat.h	2007-08-31 15:37:53.000000000 +0200
+++ gimp-2.4.0-rc2.new/app/text/gimptext-compat.h	2007-09-26 18:37:05.000000000 +0200
@@ -34,12 +34,36 @@
                               const gchar  *text,
                               gint          border,
                               gboolean      antialias);
+GimpLayer * text_render_new  (GimpImage    *image,
+                              GimpDrawable *drawable,
+                              GimpContext  *context,
+                              gint          text_x,
+                              gint          text_y,
+                              const gchar  *fontname,
+                              const gchar  *text,
+                              gint          border,
+                              gboolean      antialias,
+			      gboolean      hinting,
+			      gboolean      autohint,
+			      gdouble       indent,
+			      gdouble       line_spacing,
+			      gdouble       letter_spacing);
 gboolean    text_get_extents (const gchar  *fontname,
                               const gchar  *text,
                               gint         *width,
                               gint         *height,
                               gint         *ascent,
                               gint         *descent);
-
+gboolean    text_get_extents_new (GimpImage    *image,
+                                  const gchar  *fontname,
+                                  const gchar  *text,
+				  gboolean     antialias,
+				  gboolean     hinting,
+				  gboolean     autohint,
+				  gdouble      indent,
+				  gdouble      line_spacing,
+				  gdouble      letter_spacing,
+                                  gint         *width,
+                                  gint         *height);
 
 #endif /* __GIMP_TEXT_COMPAT_H__ */
_______________________________________________
Gimp-developer mailing list
Gimp-developer@lists.XCF.Berkeley.EDU
https://lists.XCF.Berkeley.EDU/mailman/listinfo/gimp-developer

Reply via email to