summaryrefslogtreecommitdiff
path: root/app/showfont/showfont.c
blob: b1d1ff75873cb704674d383d6e51c1f798765d80 (plain)
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
/* $XConsortium: showfont.c,v 1.13 94/04/17 20:44:07 gildea Exp $ */
/*
 * Copyright 1990 Network Computing Devices;
 * Portions Copyright 1987 by Digital Equipment Corporation and the
 *
 * 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, and that the names of Network Computing Devices or Digital
 * not be used in advertising or publicity pertaining to distribution
 * of the software without specific, written prior permission.
 *
 * NETWORK COMPUTING DEVICES AND DIGITAL DISCLAIM ALL WARRANTIES WITH
 * REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL NETWORK COMPUTING DEVICES
 * OR DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
 * THIS SOFTWARE.
 */
/*

Copyright (c) 1987  X Consortium

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

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 X CONSORTIUM 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 X Consortium 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 X Consortium.

*/
/* $XFree86: xc/programs/showfont/showfont.c,v 1.3 2000/02/18 12:20:17 tsi Exp $ */

#include	<stdio.h>
#include	<stdlib.h>
#include	<string.h>
#include	<ctype.h>
#include	<X11/fonts/FSlib.h>

/*
 * the equivalent of showsnf
 */

#define	GLWIDTHBYTESPADDED(bits,nbytes) \
	((nbytes) == 1 ? (((bits)+7)>>3)        /* pad to 1 byte */ \
	:(nbytes) == 2 ? ((((bits)+15)>>3)&~1)  /* pad to 2 bytes */ \
	:(nbytes) == 4 ? ((((bits)+31)>>3)&~3)  /* pad to 4 bytes */ \
	:(nbytes) == 8 ? ((((bits)+63)>>3)&~7)  /* pad to 8 bytes */ \
	: 0)

int         byteorder = MSBFirst; /* -LSB or -MSB */
int         bitorder = MSBFirst; /* -lsb or -msb */
int         bitmap_pad = 0;	/* -bitmap_pad: ImageRect bitmap format */
int         scan_pad = 8;	/* -pad: ScanlinePad */
int         scan_unit = 8;	/* -unit: ScanlineUnit */
int         first_ch = 0;	/* -start: first character*/
int         end_ch = ~0;	/* -end: end character */
const char *ProgramName;
Bool	    no_props = False;	/* -noprops: don't show font properties */
Bool        extents_only = False; /* -extents_only */

FSServer   *svr;

/* set from bitmap_pad to ImageRectMin, ImageMaxWidth, or ImageMax */
int	    bitmap_format;	

static      FSBitmapFormat
make_format(void)
{
    FSBitmapFormat format;

    format = 0;
    /* set up format */
    switch (scan_pad) {
    case 8:
	format |= BitmapFormatScanlinePad8;
	break;
    case 16:
	format |= BitmapFormatScanlinePad16;
	break;
    case 32:
	format |= BitmapFormatScanlinePad32;
	break;
    case 64:
	format |= BitmapFormatScanlinePad64;
	break;
    default:
	fprintf(stderr, "bogus scanline pad value: %d\n", scan_pad);
	break;
    }
    switch (scan_unit) {
    case 8:
	format |= BitmapFormatScanlineUnit8;
	break;
    case 16:
	format |= BitmapFormatScanlineUnit16;
	break;
    case 32:
	format |= BitmapFormatScanlineUnit32;
	break;
    case 64:
	format |= BitmapFormatScanlineUnit64;
	break;
    default:
	fprintf(stderr, "bogus scanline unit value: %d\n", scan_unit);
	break;
    }
    switch (bitmap_pad) {
    case 0:
	bitmap_format = BitmapFormatImageRectMin;
	break;
    case 1:
	bitmap_format = BitmapFormatImageRectMaxWidth;
	break;
    case 2:
	bitmap_format = BitmapFormatImageRectMax;
	break;
    default:
	fprintf(stderr, "bogus bitmap pad value: %d\n", bitmap_pad);
	break;
    }
    format |= bitmap_format;

    format |= (bitorder == MSBFirst) ? BitmapFormatBitOrderMSB :
	BitmapFormatBitOrderLSB;
    format |= (byteorder == MSBFirst) ? BitmapFormatByteOrderMSB :
	BitmapFormatByteOrderLSB;

    return format;
}

static void
show_char_info(FSXCharInfo *ci)
{
    printf("Left: %-3d    Right: %-3d    Ascent: %-3d    Descent: %-3d    Width: %d\n",
	   ci->left, ci->right, ci->ascent, ci->descent, ci->width);
}

static void
show_glyphs(
    Font        fid,
    FSXFontInfoHeader *hdr,
    Bool        show_all,
    FSChar2b    first,
    FSChar2b    last)
{
    FSXCharInfo *extents;
    int         err,
                ch,
                start,
                end;
    int         offset = 0;
    unsigned char *glyphs;
    FSOffset   *offsets;
    int         scanpad;
    int         r,
                b;
    FSBitmapFormat format;
    FSChar2b    chars[2];
    int         num_chars;

    if (show_all) {
	num_chars = 0;
    } else {
	chars[0] = first;
	chars[1] = last;
	num_chars = 2;
    }
    FSQueryXExtents16(svr, fid, True, chars, num_chars, &extents);

    if (!extents_only) {
	format = make_format();
	err = FSQueryXBitmaps16(svr, fid, format, True, chars, num_chars,
				&offsets, &glyphs);

	if (err != FSSuccess) {
	    fprintf(stderr, "QueryGlyphs failed\n");
	    exit(1);
	}
    }
    start = first.low + (first.high << 8);
    end = last.low + (last.high << 8);

    scanpad = scan_pad >> 3;

    for (ch = 0; ch <= (end - start); ch++) {
	int         bottom,
	            bpr,
	            charwidth;

	printf("char #%d", ch + start);
	if ((ch + start >= 0) && (ch + start <= 127) && isprint(ch + start))
	    printf(" '%c'\n", (char) (ch + start));
	else
	    printf(" 0x%04x\n", ch + start);
	show_char_info(&extents[ch]);
	if (extents_only)
	    continue;
	if (offset != offsets[ch].position)
	    fprintf(stderr, "offset mismatch: expected %d, got %d\n",
		    offset, offsets[ch].position);
	switch (bitmap_format) {
	case BitmapFormatImageRectMin:
	    bottom = extents[ch].descent + extents[ch].ascent;
	    charwidth = extents[ch].right - extents[ch].left;
	    break;
	case BitmapFormatImageRectMaxWidth:
	    bottom = extents[ch].descent + extents[ch].ascent;
	    charwidth = hdr->max_bounds.right - hdr->min_bounds.left;
	    break;
	case BitmapFormatImageRectMax:
	    bottom = hdr->max_bounds.ascent +
		hdr->max_bounds.descent;
	    charwidth = hdr->max_bounds.right - hdr->min_bounds.left;
	    break;
	default:
	    bottom = 0;
	    charwidth = 0;
	}

	if (extents[ch].left == 0 &&
	    extents[ch].right == 0 &&
	    extents[ch].width == 0 &&
	    extents[ch].ascent == 0 &&
	    extents[ch].descent == 0)
	{
	    printf ("Nonexistent character\n");
	    continue;
	}
	bpr = GLWIDTHBYTESPADDED(charwidth, scanpad);
	if (offsets[ch].length != bottom * bpr) {
	    fprintf (stderr, "length mismatch: expected %d (%dx%d), got %d\n",
			 bottom * bpr, bpr, bottom, offsets[ch].length);
	}
	offset = offsets[ch].position;
	for (r = 0; r < bottom; r++) {
	    unsigned char *row = glyphs + offset;

	    for (b = 0; b < charwidth; b++) {
		putchar((row[b >> 3] &
			 (1 << (7 - (b & 7)))) ? '#' : '-');
	    }
	    putchar('\n');
	    offset += bpr;
	}
    }
    FSFree((char *) extents);
    if (!extents_only) {
	FSFree((char *) offsets);
	FSFree((char *) glyphs);
    }
}

static void
show_props(
    FSPropInfo *pi,
    FSPropOffset *po,
    unsigned char *pd)
{
    int         i;
    char        buf[512];
    int         num_props;

    num_props = pi->num_offsets;
    for (i = 0; i < num_props; i++, po++) {
	strncpy(buf, (char *) (pd + po->name.position), po->name.length);
	buf[po->name.length] = '\0';
	printf("%s\t", buf);
	switch (po->type) {
	case PropTypeString:
	    strncpy(buf, (char *)(pd + po->value.position), po->value.length);
	    buf[po->value.length] = '\0';
	    printf("%s\n", buf);
	    break;
	case PropTypeUnsigned:
	    printf("%lu\n", (unsigned long) po->value.position);
	    break;
	case PropTypeSigned:
	    printf("%ld\n", (long) po->value.position);
	    break;
	default:
	    fprintf(stderr, "bogus property\n");
	    break;
	}
    }
}

static void
show_info(
    Font        fid,
    FSXFontInfoHeader *hdr,
    FSChar2b   *first,
    FSChar2b   *last)
{
    FSPropInfo  pi;
    FSPropOffset *po;
    unsigned char *pd;

    FSQueryXInfo(svr, fid, hdr, &pi, &po, &pd);
    printf("Direction: %s\n", (hdr->draw_direction == LeftToRightDrawDirection)
	   ? "Left to Right" : "Right to Left");
    *first = hdr->char_range.min_char;
    *last = hdr->char_range.max_char;
    printf("Range:	%d to %d\n",
	   first->low + (first->high << 8),
	   last->low + (last->high << 8));
    if (hdr->flags & FontInfoAllCharsExist)
	printf("All chars exist\n");
    printf("Default char: %d\n",
	   hdr->default_char.low + (hdr->default_char.high << 8));
    printf("Min bounds: \n");
    show_char_info(&hdr->min_bounds);
    printf("Max bounds: \n");
    show_char_info(&hdr->max_bounds);
    printf("Font Ascent: %d  Font Descent: %d\n",
	   hdr->font_ascent, hdr->font_descent);

    if (!no_props)
	show_props(&pi, po, pd);
    FSFree((char *) po);
    FSFree((char *) pd);
}

static void
usage(void)
{
    printf("%s: [-server servername] [-extents_only] [-noprops] [-lsb] [-msb] [-LSB] [-MSB] [-unit #] [-pad #] [-bitmap_pad value] [-start first_char] [-end last_char] -fn fontname\n", ProgramName);
    exit(0);
}

int
main(int argc, char **argv)
{
    char *servername = "localhost:7100"; /* -server: font server name */
    char *fontname = NULL; /* -fn: font name */
    int         i;
    Font        fid,
                dummy;
    FSBitmapFormat format;
    FSBitmapFormatMask fmask;
    FSChar2b    first,
                last;
    FSXFontInfoHeader hdr;
    Bool        show_all = True;

    ProgramName = argv[0];

    for (i = 1; i < argc; i++) {
	if (!strncmp(argv[i], "-se", 3)) {
	    if (++i < argc)
		servername = argv[i];
	    else
		usage();
	} else if (!strncmp(argv[i], "-ext", 4)) {
	    extents_only = True;
	} else if (!strncmp(argv[i], "-noprops", 7)) {
	    no_props = True;
	} else if (!strncmp(argv[i], "-lsb", 4)) {
	    bitorder = LSBFirst;
	} else if (!strncmp(argv[i], "-msb", 4)) {
	    bitorder = MSBFirst;
	} else if (!strncmp(argv[i], "-LSB", 4)) {
	    byteorder = LSBFirst;
	} else if (!strncmp(argv[i], "-MSB", 4)) {
	    byteorder = MSBFirst;
	} else if (!strncmp(argv[i], "-p", 2)) {
	    if (++i < argc)
		scan_pad = atoi(argv[i]);
	    else
		usage();
	} else if (!strncmp(argv[i], "-u", 2)) {
	    if (++i < argc)
		scan_unit = atoi(argv[i]);
	    else
		usage();
	} else if (!strncmp(argv[i], "-b", 2)) {
	    if (++i < argc)
		bitmap_pad = atoi(argv[i]);
	    else
		usage();
	} else if (!strncmp(argv[i], "-st", 3)) {
	    if (++i < argc)
		first_ch = atoi(argv[i]);
	    else
		usage();
	} else if (!strncmp(argv[i], "-e", 2)) {
	    if (++i < argc)
		end_ch = atoi(argv[i]);
	    else
		usage();
	} else if (!strncmp(argv[i], "-f", 2)) {
	    if (++i < argc)
		fontname = argv[i];
	    else
		usage();
	} else
	    usage();
    }
    if (fontname == NULL)
	usage();

    if (first_ch != 0 && end_ch != ~0 && end_ch < first_ch) {
	fprintf(stderr,
		"bad character range -- end (%d) is less than start (%d)\n",
		end_ch, first_ch);
	exit(1);
    }
    if ((svr = FSOpenServer(servername)) == NULL) {
	if(FSServerName(servername) != NULL)
		fprintf(stderr, "can't open server \"%s\"\n", FSServerName(servername));
	else
		fprintf(stderr, "can't open server \"\"\n");
	exit(1);
    }
    format = make_format();
    fmask = (BitmapFormatMaskByte | BitmapFormatMaskBit |
	     BitmapFormatMaskImageRectangle | BitmapFormatMaskScanLinePad |
	     BitmapFormatMaskScanLineUnit);
    fid = FSOpenBitmapFont(svr, format, fmask, fontname, &dummy);
    if (fid) {
	printf("opened font %s\n", fontname);
	show_info(fid, &hdr, &first, &last);
	if (first_ch != 0 &&
		((unsigned)first_ch >= (first.low + (first.high << 8)))) {
	    first.low = first_ch & 0xff;
	    first.high = first_ch >> 8;
	    show_all = False;
	}
	if (end_ch != ~0 &&
		((unsigned)end_ch <= (last.low + (last.high << 8)))) {
	    last.low = end_ch & 0xff;
	    last.high = end_ch >> 8;
	    show_all = False;
	}
	/* make sure the range is legal */
	if ((first.high > last.high) || (first.high == last.high &&
					 first.low > last.low)) {
	    last = first;
	    fprintf(stderr,
		    "adjusting range -- specifed first char is after end\n");
	}
	show_glyphs(fid, &hdr, show_all, first, last);
	FSCloseFont(svr, fid);
    } else {
	fprintf(stderr, "couldn't get font %s\n", fontname);
	FSCloseServer(svr);
	exit(1);
    }
    FSCloseServer(svr);
    exit(0);
}