1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
|
/* GIMP - The GNU Image Manipulation Program
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
*
* GimpText
* Copyright (C) 2002-2004 Sven Neumann <sven@gimp.org>
*
* Some of this code was copied from Pango (pangox-fontmap.c)
* and was originally written by Owen Taylor <otaylor@redhat.com>.
*
* 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 3 of the License, or
* (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
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "config.h"
#include <stdlib.h>
#include <string.h>
#include <gio/gio.h>
#include "libgimpbase/gimpbase.h"
#include "text-types.h"
#include "gimptext.h"
#include "gimptext-xlfd.h"
#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
{
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
};
static gchar * gimp_text_get_xlfd_field (const gchar *fontname,
gint field_num,
gchar *buffer);
static gchar * launder_font_name (gchar *name);
/**
* 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 launder_font_name (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) / 10.0;
*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_DEBUG
g_printerr ("XLFD: %s font: %s\n", xlfd, font ? font : "(null)");
#endif
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
*
* 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
* %NULL, the field is longer than XFLD_MAX_FIELD_LEN or it contains
* just an asterisk.
**/
static gchar *
gimp_text_get_xlfd_field (const gchar *fontname,
gint field_num,
gchar *buffer)
{
const gchar *t1, *t2;
gchar *p;
gint countdown, num_dashes;
gsize len;
if (!fontname)
return NULL;
/* we assume this is a valid fontname...that is, it has 14 fields */
for (t1 = fontname, countdown = field_num; *t1 && (countdown >= 0); t1++)
if (*t1 == '-')
countdown--;
num_dashes = (field_num == XLFD_CHARSET) ? 2 : 1;
for (t2 = t1; *t2; t2++)
{
if (*t2 == '-' && --num_dashes == 0)
break;
}
if (t2 > t1)
{
/* Check we don't overflow the buffer */
len = (gsize) t2 - (gsize) 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. */
for (p = buffer; *p; p++)
*p = g_ascii_tolower (*p);
}
else
{
return NULL;
}
return buffer;
}
/* 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;
}
|