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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
|
/** \file font.c
* Font selection and loading.
*/
/* XTrkCad - Model Railroad CAD
* Copyright (C) 2005 Dave Bullis
*
* 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
* (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, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <stdlib.h>
#ifdef HAVE_MALLOC_H
#include <malloc.h>
#endif
#define GTK_DISABLE_SINGLE_INCLUDES
#define GDK_DISABLE_DEPRECATED
#define GTK_DISABLE_DEPRECATED
#define GSEAL_ENABLE
#include <gtk/gtk.h>
#include <gdk/gdk.h>
#include "wlib.h"
#include "gtkint.h"
#include "i18n.h"
/*
* Macro for debug purposes. Possible debug macro values:
*
* 0 - no messages to console (use this value when building in release mode)
* 1 - send errors
* 2 - send details
* 3 - send more details
*/
#define WLIB_FONT_DEBUG 0
static gchar sampleText[] = "AbCdE0129!@$&()[]{}";
static GtkWidget *fontSelectionDialog;
/*****************************************************************************
* FONT HANDLERS
*/
#define FW_MEDIUM (0)
#define FW_BOLD (1)
#define FS_REGULAR (0)
#define FS_ITALIC (1)
/* absoluteFontSize was introduced to keep the font size information synchron
* between the Dt.size of ctext.c and it's drop list on the status bar and
* the font size coming from the gtk font dialog which is located in this file */
int absoluteFontSize = 18;
struct wFont_t {
PangoFontDescription *fontDescription;
};
static wFont_p standardFonts[F_HELV-F_TIMES+1][2][2];
static wFont_p curFont = NULL;
/**
* Callback for font selection dialog
*
* \param fontSelectionDialog IN dialog
* \param response IN response code from dialog
* \param data IN unused
* \return
*/
static void fontSelectionDialogCallback(GtkFontSelectionDialog
*fontSelectionDialog, gint response, gpointer data)
{
if (response == GTK_RESPONSE_APPLY || response == GTK_RESPONSE_OK) {
gchar *fontName;
fontName = gtk_font_selection_dialog_get_font_name(fontSelectionDialog);
wPrefSetString("font", "name", fontName);
pango_font_description_free(curFont->fontDescription);
curFont->fontDescription = pango_font_description_from_string(fontName);
absoluteFontSize = (pango_font_description_get_size(
curFont->fontDescription))/PANGO_SCALE;
#if WLIB_FONT_DEBUG >= 2
fprintf(stderr, "new font selection:\n");
fprintf(stderr, " font name \"%s\"\n", fontName);
fprintf(stderr, " font size is %d\n",
pango_font_description_get_size(curFont->fontDescription)/PANGO_SCALE);
fprintf(stderr, " font size is absolute %d\n",
pango_font_description_get_size_is_absolute(curFont->fontDescription));
#endif
g_free(fontName);
}
if (response == GTK_RESPONSE_OK || response == GTK_RESPONSE_CANCEL) {
gtk_widget_hide(GTK_WIDGET(fontSelectionDialog));
}
}
static wBool_t fontInitted = FALSE;
static wBool_t fontInit()
{
const char *fontNames[] = {
"times 18",
"times italic 18",
"times bold 18",
"times bold italic 18",
"helvetica 18",
"helvetica oblique 18",
"helvetica bold 18",
"helvetica bold oblique 18",
};
int s = 0;
int i, j, k;
for (i = F_TIMES; i <= F_HELV; ++i) {
for (j = FW_MEDIUM; j <= FW_BOLD; ++j) {
for (k = FS_REGULAR; k <= FS_ITALIC; ++k) {
PangoFontDescription *fontDescription = pango_font_description_from_string(
fontNames[s++]);
wFont_p standardFont = (wFont_p) malloc(sizeof(struct wFont_t));
standardFont->fontDescription = fontDescription;
standardFonts[i-F_TIMES][j][k] = standardFont;
}
}
}
if (curFont == NULL) {
curFont = (wFont_p) malloc(sizeof(struct wFont_t));
if (curFont == NULL) {
return FALSE;
}
const char *fontName = wPrefGetString("font", "name");
curFont->fontDescription = pango_font_description_from_string(
fontName ? fontName : "helvetica 18");
absoluteFontSize = (int) PANGO_PIXELS(pango_font_description_get_size(
curFont->fontDescription));
}
fontInitted = TRUE;
return TRUE;
}
static double fontFactor = 1.0;
#define FONTSIZE_TO_PANGOSIZE(fs) ((gint) ((fs) * (fontFactor) + .5))
/**
* Create a Pango layout with a specified font and font size
*
* \param widget IN
* \param cairo IN cairo context
* \param fp IN font
* \param fs IN size
* \param s IN ???
* \param width_p OUT width of layout
* \param height_p OUT height of layout
* \param ascent_p OUT ascent of layout
* \param descent_p OUT descent of layout
* \return the created Pango layout
*/
PangoLayout *wlibFontCreatePangoLayout(GtkWidget *widget,
void *cairo,
wFont_p fp,
wFontSize_t fs,
const char *s,
wDrawPix_t *width_p,
wDrawPix_t *height_p,
wDrawPix_t *ascent_p,
wDrawPix_t *descent_p,
wDrawPix_t *baseline_p)
{
if (!fontInitted) {
fontInit();
}
PangoLayout *layout = NULL;
gchar *utf8 = wlibConvertInput(s);
/* RPH -- pango_cairo_create_layout() is missing in CentOS 4.8.
CentOS 4.8 only has GTK 2.4.13 and Pango 1.6.0 and does not have
libpangocairo at all.
pango_cairo_create_layout() was introduced with Pango 1.10. */
#if PANGO_VERSION_MAJOR >= 1 && PANGO_VERSION_MINOR >= 10
if (cairo != NULL) {
layout = pango_cairo_create_layout((cairo_t *) cairo);
pango_layout_set_text(layout, utf8, -1);
} else
#endif
layout = gtk_widget_create_pango_layout(widget, utf8);
PangoFontDescription *fontDescription = (fp ? fp : curFont)->fontDescription;
PangoContext *context;
PangoFontMetrics *metrics;
/* set attributes */
pango_font_description_set_size(fontDescription,
FONTSIZE_TO_PANGOSIZE(fs) * PANGO_SCALE);
pango_layout_set_font_description(layout, fontDescription);
/* get layout measures */
gint width_i, height_i;
pango_layout_get_size(layout, &width_i, &height_i);
*width_p = width_i / PANGO_SCALE;
*height_p = height_i / PANGO_SCALE;
context = gtk_widget_create_pango_context(widget);
metrics = pango_context_get_metrics(context, fontDescription,
pango_context_get_language(context));
*baseline_p = pango_layout_get_baseline(layout) / PANGO_SCALE;
*ascent_p = pango_font_metrics_get_ascent(metrics) / PANGO_SCALE;
*descent_p = pango_font_metrics_get_descent(metrics) / PANGO_SCALE;
pango_font_metrics_unref(metrics);
g_object_unref(context);
#if WLIB_FONT_DEBUG >= 3
fprintf(stderr, "font layout created:\n");
fprintf(stderr, " widget: %p\n", widget);
//fprintf(stderr, " font description:%p\n", fp);
fprintf(stderr, " font size: %f\n", fs);
fprintf(stderr, " layout text: \"%s\" (utf8)\n", utf8);
fprintf(stderr, " layout width: %d\n", *width_p);
fprintf(stderr, " layout height: %d\n", *height_p);
fprintf(stderr, " layout ascent: %d (pixels)\n", *ascent_p);
fprintf(stderr, " layout descent: %d (pixels)\n", *descent_p);
#endif
return layout;
}
/**
* Destroy a previously allocated Pango layout
* \return
*/
void wlibFontDestroyPangoLayout(PangoLayout *layout)
{
g_object_ref_sink(layout);
g_object_unref(layout);
}
/**
* Initialize font data
* \return
*/
void wInitializeFonts()
{
if (!fontInitted) {
fontInit();
}
}
/**
* Initialize and run the font dialog
*
* \param title IN dialog box title
* \return
*/
void wSelectFont(
const char * title)
{
if (!fontInitted) {
fontInit();
}
if (fontSelectionDialog == NULL) {
fontSelectionDialog = gtk_font_selection_dialog_new(_("Font Select"));
gtk_window_set_position(GTK_WINDOW(fontSelectionDialog), GTK_WIN_POS_MOUSE);
gtk_window_set_modal(GTK_WINDOW(fontSelectionDialog), TRUE);
gtk_font_selection_dialog_set_preview_text(GTK_FONT_SELECTION_DIALOG(
fontSelectionDialog), sampleText);
g_signal_connect(G_OBJECT(fontSelectionDialog), "response",
G_CALLBACK(fontSelectionDialogCallback), NULL);
g_signal_connect(G_OBJECT(fontSelectionDialog), "destroy",
G_CALLBACK(gtk_widget_destroyed), &fontSelectionDialog);
}
gtk_window_set_title(GTK_WINDOW(fontSelectionDialog), title);
if (curFont != NULL) {
gchar *fontName;
/* the curFont description contains the latest font info
* which is depended on the current scale
* overwrite it with the absoluteFontSize */
pango_font_description_set_size(curFont->fontDescription,
FONTSIZE_TO_PANGOSIZE(absoluteFontSize) * PANGO_SCALE);
fontName = pango_font_description_to_string(curFont->fontDescription);
gtk_font_selection_dialog_set_font_name(GTK_FONT_SELECTION_DIALOG(
fontSelectionDialog), fontName);
g_free(fontName);
}
gtk_widget_show(fontSelectionDialog);
}
/**
* Get the current font
*
* \return describe the return value
*/
static wFont_p wlibSelectedFont(void)
{
if (!fontInitted) {
fontInit();
}
return curFont;
}
/**
* Get the default font size
*
* \return describe the return value
*/
wFontSize_t wSelectedFontSize(void)
{
if (!fontInitted) {
fontInit();
}
#if WLIB_FONT_DEBUG >= 3
fprintf(stderr, "the font size of current font description is: %d\n",
pango_font_description_get_size(curFont->fontDescription)/PANGO_SCALE);
fprintf(stderr, "the font size of absoluteFontSize is: %d\n",absoluteFontSize);
#endif
return absoluteFontSize;
}
/**
* Set the default font size
*
* \param size IN font size
* \return describe the return value
*/
void wSetSelectedFontSize(wFontSize_t size)
{
absoluteFontSize = size;
}
/**
* get the Pango font description as a string from a font definition.
* If the font definition is NULL, a default font is return. This is
* the current font if one is set. If not the first font from the font
* list is returned.
*
* \param fp IN the font definition
* \return the font description
*/
const char *wlibFontTranslate(wFont_p fp)
{
static gchar *fontName = NULL;
if (fontName != NULL) {
g_free(fontName);
}
if (!fontInitted) {
fontInit();
}
if (fp == NULL) {
fp = wlibSelectedFont();
}
if (fp == NULL) {
fp = standardFonts[0][FW_MEDIUM][FS_REGULAR];
}
fontName = pango_font_description_to_string(fp->fontDescription);
#if WLIB_FONT_DEBUG >= 2
fprintf(stderr, "font translation: ");
fprintf(stderr, " \"%s\"\n", fontName);
#endif
return (const char *) fontName;
}
/**
* Return description for one of the standard fonts
*
* \return
*/
wFont_p wStandardFont(int face, wBool_t bold, wBool_t italic)
{
if (!fontInitted) {
fontInit();
}
return standardFonts[face-F_TIMES][bold][italic];
}
|