2006-12-10 05:33:38 +08:00
|
|
|
/* GIMP - The GNU Image Manipulation Program
|
2003-06-25 03:45:55 +08:00
|
|
|
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
|
|
|
|
*
|
|
|
|
* GimpText
|
2004-01-27 02:49:33 +08:00
|
|
|
* Copyright (C) 2002-2004 Sven Neumann <sven@gimp.org>
|
2003-06-25 03:45:55 +08:00
|
|
|
*
|
2003-06-25 19:43:36 +08:00
|
|
|
* Some of this code was copied from Pango (pangox-fontmap.c)
|
|
|
|
* and was originally written by Owen Taylor <otaylor@redhat.com>.
|
|
|
|
*
|
2009-01-18 06:28:01 +08:00
|
|
|
* This program is free software: you can redistribute it and/or modify
|
2003-06-25 03:45:55 +08:00
|
|
|
* it under the terms of the GNU General Public License as published by
|
2009-01-18 06:28:01 +08:00
|
|
|
* the Free Software Foundation; either version 3 of the License, or
|
2003-06-25 03:45:55 +08:00
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2018-07-12 05:27:07 +08:00
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2003-06-25 03:45:55 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2013-10-15 07:58:39 +08:00
|
|
|
#include <gio/gio.h>
|
2003-06-25 03:45:55 +08:00
|
|
|
|
|
|
|
#include "libgimpbase/gimpbase.h"
|
|
|
|
|
2004-09-27 20:30:04 +08:00
|
|
|
#include "text-types.h"
|
2003-06-25 03:45:55 +08:00
|
|
|
|
2003-06-25 19:43:36 +08:00
|
|
|
#include "gimptext.h"
|
|
|
|
#include "gimptext-xlfd.h"
|
2003-06-25 03:45:55 +08:00
|
|
|
|
|
|
|
|
|
|
|
#define XLFD_MAX_FIELD_LEN 64
|
|
|
|
|
|
|
|
/* These are the field numbers in the X Logical Font Description fontnames,
|
|
|
|
e.g. -adobe-courier-bold-o-normal--25-180-100-100-m-150-iso8859-1 */
|
|
|
|
enum
|
|
|
|
{
|
2004-01-27 02:49:33 +08:00
|
|
|
XLFD_FOUNDRY = 0,
|
|
|
|
XLFD_FAMILY = 1,
|
|
|
|
XLFD_WEIGHT = 2,
|
|
|
|
XLFD_SLANT = 3,
|
|
|
|
XLFD_SET_WIDTH = 4,
|
|
|
|
XLFD_ADD_STYLE = 5,
|
|
|
|
XLFD_PIXELS = 6,
|
|
|
|
XLFD_POINTS = 7,
|
|
|
|
XLFD_RESOLUTION_X = 8,
|
|
|
|
XLFD_RESOLUTION_Y = 9,
|
|
|
|
XLFD_SPACING = 10,
|
|
|
|
XLFD_AVERAGE_WIDTH = 11,
|
|
|
|
XLFD_CHARSET = 12,
|
|
|
|
XLFD_NUM_FIELDS
|
2003-06-25 03:45:55 +08:00
|
|
|
};
|
|
|
|
|
2003-06-25 19:43:36 +08:00
|
|
|
static gchar * gimp_text_get_xlfd_field (const gchar *fontname,
|
|
|
|
gint field_num,
|
|
|
|
gchar *buffer);
|
2005-02-10 13:33:01 +08:00
|
|
|
static gchar * launder_font_name (gchar *name);
|
2003-06-25 19:43:36 +08:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gimp_text_font_name_from_xlfd:
|
|
|
|
* @xlfd: X Logical Font Description
|
2004-01-27 02:49:33 +08:00
|
|
|
*
|
2003-06-25 19:43:36 +08:00
|
|
|
* Attempts to extract a meaningful font name from the "family",
|
|
|
|
* "weight", "slant" and "stretch" fields of an X Logical Font
|
|
|
|
* Description.
|
2004-01-27 02:49:33 +08:00
|
|
|
*
|
2019-08-03 06:10:14 +08:00
|
|
|
* Returns: a newly allocated string.
|
2003-06-25 19:43:36 +08:00
|
|
|
**/
|
|
|
|
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;
|
2003-06-25 03:45:55 +08:00
|
|
|
|
2005-02-10 13:33:01 +08:00
|
|
|
return launder_font_name (g_strconcat (fields[0], " ",
|
|
|
|
fields[1], " ",
|
|
|
|
fields[2], " ",
|
|
|
|
fields[3], NULL));
|
2003-06-25 19:43:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
2004-01-27 02:49:33 +08:00
|
|
|
*
|
2003-06-25 19:43:36 +08:00
|
|
|
* Attempts to extract the font size from an X Logical Font
|
|
|
|
* Description.
|
2004-01-27 02:49:33 +08:00
|
|
|
*
|
2019-08-03 06:10:14 +08:00
|
|
|
* Returns: %TRUE on success, %FALSE otherwise.
|
2003-06-25 19:43:36 +08:00
|
|
|
**/
|
|
|
|
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)
|
|
|
|
{
|
2004-01-27 02:49:33 +08:00
|
|
|
*size = atoi (field) / 10.0;
|
2003-06-25 19:43:36 +08:00
|
|
|
*size_unit = GIMP_UNIT_POINT;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gimp_text_set_font_from_xlfd:
|
|
|
|
* @text: a #GimpText object
|
|
|
|
* @xlfd: X Logical Font Description
|
2004-01-27 02:49:33 +08:00
|
|
|
*
|
2003-06-25 19:43:36 +08:00
|
|
|
* 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);
|
|
|
|
|
2004-01-27 02:49:33 +08:00
|
|
|
#if GIMP_TEXT_DEBUG
|
|
|
|
g_printerr ("XLFD: %s font: %s\n", xlfd, font ? font : "(null)");
|
|
|
|
#endif
|
|
|
|
|
2003-06-25 19:43:36 +08:00
|
|
|
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:
|
2003-06-25 03:45:55 +08:00
|
|
|
* @fontname: an XLFD fontname
|
|
|
|
* @field_num: field index
|
|
|
|
* @buffer: buffer of at least XLFD_MAX_FIELD_LEN chars
|
|
|
|
*
|
|
|
|
* Fills the buffer with the specified field from the X Logical Font
|
|
|
|
* Description name, and returns it. Note: For the charset field, we
|
|
|
|
* also return the encoding, e.g. 'iso8859-1'.
|
|
|
|
*
|
|
|
|
* This function is basically copied from pangox-fontmap.c.
|
|
|
|
*
|
|
|
|
* Returns: a pointer to the filled buffer or %NULL if fontname is
|
2003-06-25 19:43:36 +08:00
|
|
|
* %NULL, the field is longer than XFLD_MAX_FIELD_LEN or it contains
|
2018-04-26 02:31:11 +08:00
|
|
|
* just an asterisk.
|
2003-06-25 03:45:55 +08:00
|
|
|
**/
|
|
|
|
static gchar *
|
|
|
|
gimp_text_get_xlfd_field (const gchar *fontname,
|
|
|
|
gint field_num,
|
|
|
|
gchar *buffer)
|
|
|
|
{
|
|
|
|
const gchar *t1, *t2;
|
|
|
|
gchar *p;
|
2004-01-27 02:49:33 +08:00
|
|
|
gint countdown, num_dashes;
|
|
|
|
gsize len;
|
2003-06-25 03:45:55 +08:00
|
|
|
|
|
|
|
if (!fontname)
|
|
|
|
return NULL;
|
2004-01-27 02:49:33 +08:00
|
|
|
|
2003-06-25 03:45:55 +08:00
|
|
|
/* we assume this is a valid fontname...that is, it has 14 fields */
|
|
|
|
|
2004-01-27 02:49:33 +08:00
|
|
|
for (t1 = fontname, countdown = field_num; *t1 && (countdown >= 0); t1++)
|
|
|
|
if (*t1 == '-')
|
2003-06-25 03:45:55 +08:00
|
|
|
countdown--;
|
2004-01-27 02:49:33 +08:00
|
|
|
|
2003-06-25 03:45:55 +08:00
|
|
|
num_dashes = (field_num == XLFD_CHARSET) ? 2 : 1;
|
2004-01-27 02:49:33 +08:00
|
|
|
|
2003-06-25 03:45:55 +08:00
|
|
|
for (t2 = t1; *t2; t2++)
|
2004-01-27 02:49:33 +08:00
|
|
|
{
|
2003-06-25 03:45:55 +08:00
|
|
|
if (*t2 == '-' && --num_dashes == 0)
|
2005-02-10 13:33:01 +08:00
|
|
|
break;
|
2003-06-25 03:45:55 +08:00
|
|
|
}
|
2004-01-27 02:49:33 +08:00
|
|
|
|
|
|
|
if (t2 > t1)
|
2003-06-25 03:45:55 +08:00
|
|
|
{
|
|
|
|
/* Check we don't overflow the buffer */
|
2004-01-27 02:49:33 +08:00
|
|
|
len = (gsize) t2 - (gsize) t1;
|
2003-06-25 03:45:55 +08:00
|
|
|
if (len > XLFD_MAX_FIELD_LEN - 1)
|
2005-02-10 13:33:01 +08:00
|
|
|
return NULL;
|
2003-06-25 19:43:36 +08:00
|
|
|
|
|
|
|
if (*t1 == '*')
|
|
|
|
return NULL;
|
|
|
|
|
2019-08-15 05:52:38 +08:00
|
|
|
g_strlcpy (buffer, t1, len);
|
2004-01-27 02:49:33 +08:00
|
|
|
|
2003-06-25 03:45:55 +08:00
|
|
|
/* Convert to lower case. */
|
|
|
|
for (p = buffer; *p; p++)
|
2005-02-10 13:33:01 +08:00
|
|
|
*p = g_ascii_tolower (*p);
|
2003-06-25 03:45:55 +08:00
|
|
|
}
|
|
|
|
else
|
2004-01-27 02:49:33 +08:00
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
2003-06-25 03:45:55 +08:00
|
|
|
|
2003-06-25 19:43:36 +08:00
|
|
|
return buffer;
|
2003-06-25 03:45:55 +08:00
|
|
|
}
|
2005-02-10 13:33:01 +08:00
|
|
|
|
|
|
|
/* Guard against font names that end in numbers being interpreted as a
|
|
|
|
* font size in pango font descriptions
|
|
|
|
*/
|
|
|
|
static gchar *
|
|
|
|
launder_font_name (gchar *name)
|
|
|
|
{
|
|
|
|
gchar *laundered_name;
|
|
|
|
gchar last_char;
|
|
|
|
|
|
|
|
last_char = name[strlen (name) - 1];
|
|
|
|
|
|
|
|
if (g_ascii_isdigit (last_char) || last_char == '.')
|
|
|
|
{
|
|
|
|
laundered_name = g_strconcat (name, ",", NULL);
|
|
|
|
g_free (name);
|
|
|
|
|
|
|
|
return laundered_name;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return name;
|
|
|
|
}
|