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
|
/*
* $Xorg: xfd.c,v 1.4 2001/02/09 02:05:42 xorgcvs Exp $
*
*
Copyright 1989, 1998 The Open Group
Permission to use, copy, modify, distribute, and sell this software and its
documentation for any purpose is hereby granted without fee, provided that
the above copyright notice appear in all copies and that both that
copyright notice and this permission notice appear in supporting
documentation.
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Except as contained in this notice, the name of The Open Group shall not be
used in advertising or otherwise to promote the sale, use or other dealings
in this Software without prior written authorization from The Open Group.
* *
* Author: Jim Fulton, MIT X Consortium
*/
#include <X11/Intrinsic.h>
#include <X11/StringDefs.h>
#include <X11/Xos.h>
#include <X11/Xatom.h>
#include <X11/Shell.h>
#include <X11/Xaw/Cardinals.h>
#include <X11/Xaw/Paned.h>
#include <X11/Xaw/Box.h>
#include <X11/Xaw/Form.h>
#include <X11/Xaw/Command.h>
#include <stdio.h>
#include "grid.h"
char *ProgramName;
static XrmOptionDescRec xfd_options[] = {
{"-fn", "*grid.font", XrmoptionSepArg, (caddr_t) NULL },
{"-start", "*startChar", XrmoptionSepArg, (caddr_t) NULL },
{"-box", "*grid.boxChars", XrmoptionNoArg, (caddr_t) "on" },
{"-bc", "*grid.boxColor", XrmoptionSepArg, (caddr_t) NULL },
{"-center", "*grid.centerChars", XrmoptionNoArg, (caddr_t) "on" },
{"-rows", "*grid.cellRows", XrmoptionSepArg, (caddr_t) NULL },
{"-columns", "*grid.cellColumns", XrmoptionSepArg, (caddr_t) NULL },
};
static void do_quit(), do_next(), do_prev();
static void change_page (), set_button_state ();
static char *get_font_name();
static void SelectChar();
static XtActionsRec xfd_actions[] = {
{ "Quit", do_quit },
{ "Prev", do_prev },
{ "Next", do_next },
};
static Atom wm_delete_window;
Widget quitButton, prevButton, nextButton;
#define DEF_SELECT_FORMAT "character 0x%02x%02x (%u,%u) (%#o,%#o)"
#define DEF_METRICS_FORMAT "width %d; left %d, right %d; ascent %d, descent %d (font %d, %d)"
#define DEF_RANGE_FORMAT "range: 0x%02x%02x (%u,%u) thru 0x%02x%02x (%u,%u)"
#define DEF_START_FORMAT "upper left: 0x%04x (%d,%d)"
#define DEF_NOCHAR_FORMAT "no such character 0x%02x%02x (%u,%u) (%#o,%#o)"
static struct _xfd_resources {
char *select_format;
char *metrics_format;
char *range_format;
char *start_format;
char *nochar_format;
} xfd_resources;
#define Offset(field) XtOffsetOf(struct _xfd_resources, field)
static XtResource Resources[] = {
{ "selectFormat", "SelectFormat", XtRString, sizeof(char *),
Offset(select_format), XtRString, DEF_SELECT_FORMAT },
{ "metricsFormat", "MetricsFormat", XtRString, sizeof(char *),
Offset(metrics_format), XtRString, DEF_METRICS_FORMAT },
{ "rangeFormat", "RangeFormat", XtRString, sizeof(char *),
Offset(range_format), XtRString, DEF_RANGE_FORMAT },
{ "startFormat", "StartFormat", XtRString, sizeof(char *),
Offset(start_format), XtRString, DEF_START_FORMAT },
{ "nocharFormat", "NocharFormat", XtRString, sizeof(char *),
Offset(nochar_format), XtRString, DEF_NOCHAR_FORMAT },
};
#undef Offset
usage()
{
fprintf (stderr, "usage: %s [-options ...] -fn font\n\n", ProgramName);
fprintf (stderr, "where options include:\n");
fprintf (stderr,
" -display dpy X server to contact\n");
fprintf (stderr,
" -geometry geom size and location of window\n");
fprintf (stderr,
" -start num first character to show\n");
fprintf (stderr,
" -box show a box around each character\n");
fprintf (stderr,
" -center center each character inside its grid\n");
fprintf (stderr,
" -rows number number of rows in grid\n");
fprintf (stderr,
" -columns number number of columns in grid\n");
fprintf (stderr, "\n");
exit (1);
}
static Widget selectLabel, metricsLabel, rangeLabel, startLabel, fontGrid;
static Boolean fontConversionFailed = False;
static void (*oldWarningHandler)();
static void CatchFontConversionWarning();
main (argc, argv)
int argc;
char **argv;
{
XtAppContext xtcontext;
Widget toplevel, pane, toplabel, box, form;
char buf[256];
Arg av[10];
Cardinal i;
static XtCallbackRec cb[2] = { { SelectChar, NULL }, { NULL, NULL } };
XFontStruct *fs;
char *fontname;
ProgramName = argv[0];
toplevel = XtAppInitialize (&xtcontext, "Xfd",
xfd_options, XtNumber(xfd_options),
&argc, argv, NULL, NULL, 0);
if (argc != 1) usage ();
XtAppAddActions (xtcontext, xfd_actions, XtNumber (xfd_actions));
XtOverrideTranslations
(toplevel, XtParseTranslationTable ("<Message>WM_PROTOCOLS: Quit()"));
XtGetApplicationResources (toplevel, (XtPointer) &xfd_resources, Resources,
XtNumber (Resources), NULL, ZERO);
/* pane wrapping everything */
pane = XtCreateManagedWidget ("pane", panedWidgetClass, toplevel,
NULL, ZERO);
/* font name */
toplabel = XtCreateManagedWidget ("fontname", labelWidgetClass, pane,
NULL, ZERO);
/* button box */
box = XtCreateManagedWidget ("box", boxWidgetClass, pane, NULL, ZERO);
quitButton = XtCreateManagedWidget ("quit", commandWidgetClass, box,
NULL, ZERO);
prevButton = XtCreateManagedWidget ("prev", commandWidgetClass, box,
NULL, ZERO);
nextButton = XtCreateManagedWidget ("next", commandWidgetClass, box,
NULL, ZERO);
/* and labels in which to put information */
selectLabel = XtCreateManagedWidget ("select", labelWidgetClass,
pane, NULL, ZERO);
metricsLabel = XtCreateManagedWidget ("metrics", labelWidgetClass,
pane, NULL, ZERO);
rangeLabel = XtCreateManagedWidget ("range", labelWidgetClass, pane,
NULL, ZERO);
startLabel = XtCreateManagedWidget ("start", labelWidgetClass, pane,
NULL, ZERO);
/* form in which to draw */
form = XtCreateManagedWidget ("form", formWidgetClass, pane, NULL, ZERO);
i = 0;
XtSetArg (av[i], XtNtop, XtChainTop); i++;
XtSetArg (av[i], XtNbottom, XtChainBottom); i++;
XtSetArg (av[i], XtNleft, XtChainLeft); i++;
XtSetArg (av[i], XtNright, XtChainRight); i++;
XtSetArg (av[i], XtNcallback, cb); i++;
oldWarningHandler = XtAppSetWarningMsgHandler(xtcontext,
CatchFontConversionWarning);
fontGrid = XtCreateManagedWidget ("grid", fontgridWidgetClass, form,
av, i);
XtAppSetWarningMsgHandler(xtcontext, oldWarningHandler);
/* set the label at the top to tell us which font this is */
i = 0;
XtSetArg (av[i], XtNfont, &fs); i++;
XtGetValues (fontGrid, av, i);
if (!fs || fontConversionFailed) {
fprintf (stderr, "%s: no font to display\n", ProgramName);
exit (1);
}
fontname = get_font_name (XtDisplay(toplevel), fs);
if (!fontname) fontname = "unknown font!";
i = 0;
XtSetArg (av[i], XtNlabel, fontname); i++;
XtSetValues (toplabel, av, i);
i = 0;
XtSetArg (av[i], XtNlabel, buf); i++;
sprintf (buf, xfd_resources.range_format,
fs->min_byte1, fs->min_char_or_byte2,
fs->min_byte1, fs->min_char_or_byte2,
fs->max_byte1, fs->max_char_or_byte2,
fs->max_byte1, fs->max_char_or_byte2);
XtSetValues (rangeLabel, av, i);
XtRealizeWidget (toplevel);
wm_delete_window = XInternAtom(XtDisplay(toplevel), "WM_DELETE_WINDOW",
False);
(void) XSetWMProtocols (XtDisplay(toplevel), XtWindow(toplevel),
&wm_delete_window, 1);
change_page (0);
XtAppMainLoop (xtcontext);
}
/*ARGSUSED*/
static void SelectChar (w, closure, data)
Widget w;
XtPointer closure, data;
{
FontGridCharRec *p = (FontGridCharRec *) data;
XFontStruct *fs = p->thefont;
unsigned n = ((((unsigned) p->thechar.byte1) << 8) |
((unsigned) p->thechar.byte2));
int direction, fontascent, fontdescent;
XCharStruct metrics;
char buf[256];
Arg arg;
XtSetArg (arg, XtNlabel, buf);
if ((!fs->min_byte1 && !fs->max_byte1) ?
(n < fs->min_char_or_byte2 || n > fs->max_char_or_byte2) :
(p->thechar.byte1 < fs->min_byte1 || p->thechar.byte1 > fs->max_byte1 ||
p->thechar.byte2 < fs->min_char_or_byte2 ||
p->thechar.byte2 > fs->max_char_or_byte2)) {
sprintf (buf, xfd_resources.nochar_format,
(unsigned) p->thechar.byte1, (unsigned) p->thechar.byte2,
(unsigned) p->thechar.byte1, (unsigned) p->thechar.byte2,
(unsigned) p->thechar.byte1, (unsigned) p->thechar.byte2);
XtSetValues (selectLabel, &arg, ONE);
buf[0] = '\0';
XtSetValues (metricsLabel, &arg, ONE);
return;
}
XTextExtents16 (fs, &p->thechar, 1, &direction, &fontascent, &fontdescent,
&metrics);
sprintf (buf, xfd_resources.select_format,
(unsigned) p->thechar.byte1, (unsigned) p->thechar.byte2,
(unsigned) p->thechar.byte1, (unsigned) p->thechar.byte2,
(unsigned) p->thechar.byte1, (unsigned) p->thechar.byte2);
XtSetValues (selectLabel, &arg, ONE);
sprintf (buf, xfd_resources.metrics_format,
metrics.width, metrics.lbearing, metrics.rbearing,
metrics.ascent, metrics.descent, fontascent, fontdescent);
XtSetValues (metricsLabel, &arg, ONE);
return;
}
/*ARGSUSED*/
static void do_quit (w, event, params, num_params)
Widget w;
XEvent *event;
String *params;
Cardinal *num_params;
{
exit (0);
}
static void change_page (page)
int page;
{
Dimension oldstart, newstart;
int ncols, nrows;
char buf[256];
Arg arg;
arg.name = XtNstartChar;
GetFontGridCellDimensions (fontGrid, &oldstart, &ncols, &nrows);
if (page) {
Dimension start = (oldstart +
((long) ncols) * ((long) nrows) * ((long) page));
arg.value = (XtArgVal) start;
XtSetValues (fontGrid, &arg, ONE);
}
/* find out what it got set to */
arg.value = (XtArgVal) &newstart;
XtGetValues (fontGrid, &arg, ONE);
/* if not paging, then initialize it, else only do it actually changed */
if (!page || newstart != oldstart) {
unsigned int row = (unsigned int) ((newstart >> 8) & 0xff);
unsigned int col = (unsigned int) (newstart & 0xff);
XtSetArg (arg, XtNlabel, buf);
sprintf (buf, xfd_resources.start_format, newstart, row, col);
XtSetValues (startLabel, &arg, ONE);
}
set_button_state ();
return;
}
static void set_button_state ()
{
Bool prevvalid, nextvalid;
Arg arg;
GetPrevNextStates (fontGrid, &prevvalid, &nextvalid);
arg.name = XtNsensitive;
arg.value = (XtArgVal) (prevvalid ? TRUE : FALSE);
XtSetValues (prevButton, &arg, ONE);
arg.value = (XtArgVal) (nextvalid ? TRUE : FALSE);
XtSetValues (nextButton, &arg, ONE);
}
/* ARGSUSED */
static void do_prev (w, event, params, num_params)
Widget w;
XEvent *event;
String *params;
Cardinal *num_params;
{
change_page (-1);
}
/* ARGSUSED */
static void do_next (w, event, params, num_params)
Widget w;
XEvent *event;
String *params;
Cardinal *num_params;
{
change_page (1);
}
static char *get_font_name (dpy, fs)
Display *dpy;
XFontStruct *fs;
{
register XFontProp *fp;
register int i;
Atom fontatom = XInternAtom (dpy, "FONT", False);
for (i = 0, fp = fs->properties; i < fs->n_properties; i++, fp++) {
if (fp->name == fontatom) {
return (XGetAtomName (dpy, fp->card32));
}
}
return NULL;
}
static void CatchFontConversionWarning(name, type, class, defaultp, params, np)
String name, type, class, defaultp, *params;
Cardinal *np;
{
if (np && *np > 1 &
strcmp(name, "conversionError") == 0 &&
strcmp(type, "string") == 0 &&
strcmp(class, "XtToolkitError") == 0 &&
strcmp(params[1], "FontStruct") == 0) fontConversionFailed = True;
(*oldWarningHandler)(name, type, class, defaultp, params, np);
}
|