From 81c62c58657b19bdc82825e6936c52a81f1c9cc3 Mon Sep 17 00:00:00 2001 From: Sven Neumann Date: Wed, 25 Jun 2003 11:43:36 +0000 Subject: [PATCH] added convenience function gimp_text_set_font_from_xlfd(). Improved and 2003-06-25 Sven Neumann * app/text/gimptext-xlfd.[ch]: added convenience function gimp_text_set_font_from_xlfd(). Improved and documented XLFD parsing. * app/text/gimptext-parasite.c: use the new function. --- ChangeLog | 7 ++ app/text/gimptext-parasite.c | 15 +-- app/text/gimptext-xlfd.c | 223 +++++++++++++++++++++++++---------- app/text/gimptext-xlfd.h | 2 + 4 files changed, 172 insertions(+), 75 deletions(-) diff --git a/ChangeLog b/ChangeLog index 74c692150f..a7f8cc8f56 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +2003-06-25 Sven Neumann + + * app/text/gimptext-xlfd.[ch]: added convenience function + gimp_text_set_font_from_xlfd(). Improved and documented XLFD parsing. + + * app/text/gimptext-parasite.c: use the new function. + 2003-06-25 Michael Natterer * modules/cdisplay_colorblind.c (cdisplay_colorblind_load_state) diff --git a/app/text/gimptext-parasite.c b/app/text/gimptext-parasite.c index afc96d92d1..ef8928433b 100644 --- a/app/text/gimptext-parasite.c +++ b/app/text/gimptext-parasite.c @@ -130,12 +130,9 @@ gimp_text_from_gdyntext_parasite (const GimpParasite *parasite) GimpTextJustification justify; const gchar *str; gchar *text = NULL; - gchar *font = NULL; gchar **params; gboolean antialias; gdouble spacing; - gdouble size; - GimpUnit unit; GimpRGB rgb; glong color; gint i; @@ -182,27 +179,17 @@ gimp_text_from_gdyntext_parasite (const GimpParasite *parasite) color = strtol (params[COLOR], NULL, 16); gimp_rgba_set_uchar (&rgb, color >> 16, color >> 8, color, 255); - font = gimp_text_font_name_from_xlfd (params[XLFD]); - retval = g_object_new (GIMP_TYPE_TEXT, "text", text, "antialias", antialias, "justify", justify, "line-spacing", spacing, "color", &rgb, - "font", font, NULL); - if (gimp_text_font_size_from_xlfd (params[XLFD], &size, &unit)) - { - g_object_set (retval, - "font-size", size, - "font-size-unit", unit, - NULL); - } + gimp_text_set_font_from_xlfd (GIMP_TEXT (retval), params[XLFD]); cleanup: - g_free (font); g_free (text); g_strfreev (params); diff --git a/app/text/gimptext-xlfd.c b/app/text/gimptext-xlfd.c index 494e548ae6..f2eabec603 100644 --- a/app/text/gimptext-xlfd.c +++ b/app/text/gimptext-xlfd.c @@ -4,6 +4,9 @@ * GimpText * Copyright (C) 2002-2003 Sven Neumann * + * Some of this code was copied from Pango (pangox-fontmap.c) + * and was originally written by Owen Taylor . + * * 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 @@ -28,12 +31,12 @@ #include "libgimpbase/gimpbase.h" +#include "text/text-types.h" + +#include "gimptext.h" #include "gimptext-xlfd.h" -/* Most of this code was copied from pangox-fontmap.c */ - - #define XLFD_MAX_FIELD_LEN 64 /* These are the field numbers in the X Logical Font Description fontnames, @@ -56,8 +59,155 @@ enum XLFD_NUM_FIELDS }; +static gchar * gimp_text_get_xlfd_field (const gchar *fontname, + gint field_num, + gchar *buffer); -/** gimp_text_get_xlfd_field() + +/** + * gimp_text_font_name_from_xlfd: + * @xlfd: X Logical Font Description + * + * Attempts to extract a meaningful font name from the "family", + * "weight", "slant" and "stretch" fields of an X Logical Font + * Description. + * + * Return value: a newly allocated string. + **/ +gchar * +gimp_text_font_name_from_xlfd (const gchar *xlfd) +{ + gchar *fields[4]; + gchar buffers[4][XLFD_MAX_FIELD_LEN]; + gint i = 0; + + /* family */ + fields[i] = gimp_text_get_xlfd_field (xlfd, XLFD_FAMILY, buffers[i]); + if (fields[i]) + i++; + + /* weight */ + fields[i] = gimp_text_get_xlfd_field (xlfd, XLFD_WEIGHT, buffers[i]); + if (fields[i] && strcmp (fields[i], "medium")) + i++; + + /* slant */ + fields[i] = gimp_text_get_xlfd_field (xlfd, XLFD_SLANT, buffers[i]); + if (fields[i]) + { + switch (*fields[i]) + { + case 'i': + strcpy (buffers[i], "italic"); + i++; + break; + case 'o': + strcpy (buffers[i], "oblique"); + i++; + break; + case 'r': + break; + } + } + + /* stretch */ + fields[i] = gimp_text_get_xlfd_field (xlfd, XLFD_SET_WIDTH, buffers[i]); + if (fields[i] && strcmp (fields[i], "normal")) + i++; + + if (i < 4) + fields[i] = NULL; + + return g_strconcat (fields[0], " ", + fields[1], " ", + fields[2], " ", + fields[3], NULL); +} + +/** + * gimp_text_font_size_from_xlfd: + * @xlfd: X Logical Font Description + * @size: return location for the font size + * @size_unit: return location for the font size unit + * + * Attempts to extract the font size from an X Logical Font + * Description. + * + * Return value: %TRUE on success, %FALSE otherwise. + **/ +gboolean +gimp_text_font_size_from_xlfd (const gchar *xlfd, + gdouble *size, + GimpUnit *size_unit) +{ + gchar buffer[XLFD_MAX_FIELD_LEN]; + gchar *field; + + if (!xlfd) + return FALSE; + + field = gimp_text_get_xlfd_field (xlfd, XLFD_PIXELS, buffer); + if (field) + { + *size = atoi (field); + *size_unit = GIMP_UNIT_PIXEL; + return TRUE; + } + + field = gimp_text_get_xlfd_field (xlfd, XLFD_POINTS, buffer); + if (field) + { + *size = atoi (field); + *size_unit = GIMP_UNIT_POINT; + return TRUE; + } + + return FALSE; +} + +/** + * gimp_text_set_font_from_xlfd: + * @text: a #GimpText object + * @xlfd: X Logical Font Description + * + * Attempts to extract font name and font size from @xlfd and sets + * them on the #GimpText object. + **/ +void +gimp_text_set_font_from_xlfd (GimpText *text, + const gchar *xlfd) +{ + gchar *font; + gdouble size; + GimpUnit size_unit; + + g_return_if_fail (GIMP_IS_TEXT (text)); + + if (!xlfd) + return; + + font = gimp_text_font_name_from_xlfd (xlfd); + + if (gimp_text_font_size_from_xlfd (xlfd, &size, &size_unit)) + { + g_object_set (text, + "font-size", size, + "font-size-unit", size_unit, + font ? "font" : NULL, font, + NULL); + } + else if (font) + { + g_object_set (text, + "font", font, + NULL); + } + + g_free (font); +} + +/** + * gimp_text_get_xlfd_field: * @fontname: an XLFD fontname * @field_num: field index * @buffer: buffer of at least XLFD_MAX_FIELD_LEN chars @@ -69,7 +219,8 @@ enum * This function is basically copied from pangox-fontmap.c. * * Returns: a pointer to the filled buffer or %NULL if fontname is - * %NULL or the field is longer than XFLD_MAX_FIELD_LEN. + * %NULL, the field is longer than XFLD_MAX_FIELD_LEN or it contains + * just an asteriks. **/ static gchar * gimp_text_get_xlfd_field (const gchar *fontname, @@ -104,6 +255,10 @@ gimp_text_get_xlfd_field (const gchar *fontname, len = (long) t2 - (long) t1; if (len > XLFD_MAX_FIELD_LEN - 1) return NULL; + + if (*t1 == '*') + return NULL; + strncpy (buffer, t1, len); buffer[len] = 0; /* Convert to lower case. */ @@ -111,61 +266,7 @@ gimp_text_get_xlfd_field (const gchar *fontname, *p = g_ascii_tolower (*p); } else - strcpy(buffer, "(nil)"); - + return NULL; + return buffer; } - -gchar * -gimp_text_font_name_from_xlfd (const gchar *xlfd) -{ - gchar *fields[4]; - gchar buffers[4][XLFD_MAX_FIELD_LEN]; - gint i, j; - - for (i = 0, j = 0; i < 4; i++) - { - fields[j] = gimp_text_get_xlfd_field (xlfd, XLFD_FAMILY + i, buffers[i]); - - if (fields[j] && *fields[j] == '*') - fields[j] = NULL; - - if (fields[j]) - j++; - } - - return g_strconcat (fields[0], " ", - fields[1], " ", - fields[2], " ", - fields[3], NULL); -} - -gboolean -gimp_text_font_size_from_xlfd (const gchar *xlfd, - gdouble *size, - GimpUnit *size_unit) -{ - gchar buffer[XLFD_MAX_FIELD_LEN]; - gchar *field; - - if (!xlfd) - return FALSE; - - field = gimp_text_get_xlfd_field (xlfd, XLFD_PIXELS, buffer); - if (field && *field != '*') - { - *size = atoi (field); - *size_unit = GIMP_UNIT_PIXEL; - return TRUE; - } - - field = gimp_text_get_xlfd_field (xlfd, XLFD_POINTS, buffer); - if (field && *field != '*') - { - *size = atoi (field); - *size_unit = GIMP_UNIT_POINT; - return TRUE; - } - - return FALSE; -} diff --git a/app/text/gimptext-xlfd.h b/app/text/gimptext-xlfd.h index c4e10487d6..17fa68b038 100644 --- a/app/text/gimptext-xlfd.h +++ b/app/text/gimptext-xlfd.h @@ -30,6 +30,8 @@ gboolean gimp_text_font_size_from_xlfd (const gchar *xlfd, gdouble *size, GimpUnit *size_unit); +void gimp_text_set_font_from_xlfd (GimpText *text, + const gchar *xlfd); #endif /* __GIMP_TEXT_COMPAT_H__ */