summaryrefslogtreecommitdiff
path: root/xc/lib/font/bitmap/snfread.c
blob: 51f454524ff2946f434c0994c8f647b771dd4040 (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
472
473
474
475
476
477
478
479
480
481
482
483
484
/* $Xorg: snfread.c,v 1.3 2000/08/17 19:46:35 cpqbld Exp $ */
/************************************************************************
Copyright 1989 by Digital Equipment Corporation, Maynard, Massachusetts.

                        All Rights Reserved

Permission to use, copy, modify, and distribute this software and its
documentation for any purpose and without fee is hereby granted,
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 name of Digital not be
used in advertising or publicity pertaining to distribution of the
software without specific, written prior permission.

DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
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 1994, 1998  The Open Group

All Rights Reserved.

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.

*/
/* $XFree86: xc/lib/font/bitmap/snfread.c,v 1.8 2001/01/17 19:43:27 dawes Exp $ */

#ifndef FONTMODULE
#include <ctype.h>
#endif

#include "fntfilst.h"
#include "bitmap.h"
#include "snfstr.h"

static void snfUnloadFont(FontPtr pFont);

static int
snfReadCharInfo(FontFilePtr file, CharInfoPtr charInfo, char *base)
{
    snfCharInfoRec snfCharInfo;

#define Width(m)    ((m).rightSideBearing - (m).leftSideBearing)
#define Height(m)   ((m).ascent + (m).descent)

    if (FontFileRead(file, (char *) &snfCharInfo, sizeof snfCharInfo) !=
	    sizeof(snfCharInfo)) {
	return BadFontName;
    }
    charInfo->metrics = snfCharInfo.metrics;
    if (snfCharInfo.exists)
	charInfo->bits = base + snfCharInfo.byteOffset;
    else
	charInfo->bits = 0;
    return Successful;
}

static int
snfReadxCharInfo(FontFilePtr file, xCharInfo *charInfo)
{
    snfCharInfoRec snfCharInfo;

    if (FontFileRead(file, (char *) &snfCharInfo, sizeof snfCharInfo) !=
	    sizeof(snfCharInfo)) {
	return BadFontName;
    }
    *charInfo = snfCharInfo.metrics;
    return Successful;
}

static void
snfCopyInfo(snfFontInfoPtr snfInfo, FontInfoPtr pFontInfo)
{
    pFontInfo->firstCol = snfInfo->firstCol;
    pFontInfo->lastCol = snfInfo->lastCol;
    pFontInfo->firstRow = snfInfo->firstRow;
    pFontInfo->lastRow = snfInfo->lastRow;
    pFontInfo->defaultCh = snfInfo->chDefault;
    pFontInfo->noOverlap = snfInfo->noOverlap;
    pFontInfo->terminalFont = snfInfo->terminalFont;
    pFontInfo->constantMetrics = snfInfo->constantMetrics;
    pFontInfo->constantWidth = snfInfo->constantWidth;
    pFontInfo->inkInside = snfInfo->inkInside;
    pFontInfo->inkMetrics = snfInfo->inkMetrics;
    pFontInfo->allExist = snfInfo->allExist;
    pFontInfo->drawDirection = snfInfo->drawDirection;
    pFontInfo->anamorphic = FALSE;
    pFontInfo->cachable = TRUE;
    pFontInfo->maxOverlap = 0;
    pFontInfo->minbounds = snfInfo->minbounds.metrics;
    pFontInfo->maxbounds = snfInfo->maxbounds.metrics;
    pFontInfo->fontAscent = snfInfo->fontAscent;
    pFontInfo->fontDescent = snfInfo->fontDescent;
    pFontInfo->nprops = snfInfo->nProps;
}

static int
snfReadProps(snfFontInfoPtr snfInfo, FontInfoPtr pFontInfo, FontFilePtr file)
{
    char       *strings;
    FontPropPtr pfp;
    snfFontPropPtr psnfp;
    char       *propspace;
    int         bytestoalloc;
    int         i;

    bytestoalloc = snfInfo->nProps * sizeof(snfFontPropRec) +
	BYTESOFSTRINGINFO(snfInfo);
    propspace = (char *) xalloc(bytestoalloc);
    if (!propspace)
	return AllocError;

    if (FontFileRead(file, propspace, bytestoalloc) != bytestoalloc) {
	xfree(propspace);
	return BadFontName;
    }
    psnfp = (snfFontPropPtr) propspace;

    strings = propspace + BYTESOFPROPINFO(snfInfo);

    for (i = 0, pfp = pFontInfo->props; i < snfInfo->nProps; i++, pfp++, psnfp++) {
	pfp->name = MakeAtom(&strings[psnfp->name],
			     (unsigned) strlen(&strings[psnfp->name]), 1);
	pFontInfo->isStringProp[i] = psnfp->indirect;
	if (psnfp->indirect)
	    pfp->value = (INT32) MakeAtom(&strings[psnfp->value],
			       (unsigned) strlen(&strings[psnfp->value]), 1);
	else
	    pfp->value = psnfp->value;
    }

    xfree(propspace);
    return Successful;
}

static int
snfReadHeader(snfFontInfoPtr snfInfo, FontFilePtr file)
{
    if (FontFileRead(file, (char *) snfInfo, sizeof *snfInfo) != sizeof *snfInfo)
	return BadFontName;

    if (snfInfo->version1 != FONT_FILE_VERSION ||
	    snfInfo->version2 != FONT_FILE_VERSION)
	return BadFontName;
    return Successful;
}

static int  snf_set;
static int  snf_bit, snf_byte, snf_glyph, snf_scan;

void
SnfSetFormat (int bit, int byte, int glyph, int scan)
{
    snf_bit = bit;
    snf_byte = byte;
    snf_glyph = glyph;
    snf_scan = scan;
    snf_set = 1;
}

static void
SnfGetFormat (int *bit, int *byte, int *glyph, int *scan)
{
    if (!snf_set)
	FontDefaultFormat (&snf_bit, &snf_byte, &snf_glyph, &snf_scan);
    *bit = snf_bit;
    *byte = snf_byte;
    *glyph = snf_glyph;
    *scan = snf_scan;
}

int
snfReadFont(FontPtr pFont, FontFilePtr file, 
	    int bit, int byte, int glyph, int scan)
{
    snfFontInfoRec fi;
    unsigned    bytestoalloc;
    int         i, j;
    char       *fontspace;
    BitmapFontPtr  bitmapFont;
    int         num_chars;
    int         bitmapsSize;
    int         ret;
    int         metrics_off;
    int         encoding_off;
    int         props_off;
    int         isStringProp_off;
    int         ink_off;
    char	*bitmaps;
    int		def_bit, def_byte, def_glyph, def_scan;

    ret = snfReadHeader(&fi, file);
    if (ret != Successful)
	return ret;

    SnfGetFormat (&def_bit, &def_byte, &def_glyph, &def_scan);

    /*
     * we'll allocate one chunk of memory and split it among the various parts
     * of the font:
     * 
     * BitmapFontRec CharInfoRec's Glyphs Encoding DIX Properties Ink CharInfoRec's
     *
     * If the glyphpad is not the same as the font file, then the glyphs
     * are allocated separately, to be later realloc'ed when we know
     * how big to make them.
     */

    bitmapsSize = BYTESOFGLYPHINFO(&fi);
    num_chars = n2dChars(&fi);
    bytestoalloc = sizeof(BitmapFontRec);	/* bitmapFont */
    metrics_off = bytestoalloc;
    bytestoalloc += num_chars * sizeof(CharInfoRec);	/* metrics */
    encoding_off = bytestoalloc;
    bytestoalloc += NUM_SEGMENTS(num_chars) * sizeof(CharInfoPtr**);	
                                                /* encoding */
    props_off = bytestoalloc;
    bytestoalloc += fi.nProps * sizeof(FontPropRec);	/* props */
    isStringProp_off = bytestoalloc;
    bytestoalloc += fi.nProps * sizeof(char);	/* isStringProp */
    bytestoalloc = (bytestoalloc + 3) & ~3;
    ink_off = bytestoalloc;
    if (fi.inkMetrics)
	bytestoalloc += num_chars * sizeof(xCharInfo);	/* ink_metrics */

    fontspace = (char *) xalloc(bytestoalloc);
    if (!fontspace)
	return AllocError;

    bitmaps = (char *) xalloc (bitmapsSize);
    if (!bitmaps)
    {
	xfree (fontspace);
	return AllocError;
    }
    /*
     * now fix up pointers
     */

    bitmapFont = (BitmapFontPtr) fontspace;
    bitmapFont->num_chars = num_chars;
    bitmapFont->metrics = (CharInfoPtr) (fontspace + metrics_off);
    bitmapFont->encoding = (CharInfoPtr **) (fontspace + encoding_off);
    bitmapFont->bitmaps = bitmaps;
    bitmapFont->pDefault = NULL;
    bitmapFont->bitmapExtra = NULL;
    pFont->info.props = (FontPropPtr) (fontspace + props_off);
    pFont->info.isStringProp = (char *) (fontspace + isStringProp_off);
    if (fi.inkMetrics)
	bitmapFont->ink_metrics = (xCharInfo *) (fontspace + ink_off);
    else
	bitmapFont->ink_metrics = 0;

    /*
     * read the CharInfo
     */

    ret = Successful;
    memset(bitmapFont->encoding, 0, 
           NUM_SEGMENTS(num_chars)*sizeof(CharInfoPtr*));
    for (i = 0; ret == Successful && i < num_chars; i++) {
	ret = snfReadCharInfo(file, &bitmapFont->metrics[i], bitmaps);
	if (bitmapFont->metrics[i].bits) {
            if (!bitmapFont->encoding[SEGMENT_MAJOR(i)]) {
                bitmapFont->encoding[SEGMENT_MAJOR(i)]=
                    (CharInfoPtr*)xcalloc(BITMAP_FONT_SEGMENT_SIZE,
                                          sizeof(CharInfoPtr));
                if (!bitmapFont->encoding[SEGMENT_MAJOR(i)]) {
                    ret = AllocError;
                    break;
                }
            }
            ACCESSENCODINGL(bitmapFont->encoding,i) = &bitmapFont->metrics[i];
        }
    }

    if (ret != Successful) {
	xfree(bitmaps);
        if(bitmapFont->encoding) {
            for(j=0; j<SEGMENT_MAJOR(i); j++)
                xfree(bitmapFont->encoding[i]);
        }
	xfree(fontspace);
	return ret;
    }
    /*
     * read the glyphs
     */

    if (FontFileRead(file, bitmaps, bitmapsSize) != bitmapsSize) {
	xfree(bitmaps);
	xfree(fontspace);
	return BadFontName;
    }

    if (def_bit != bit)
	BitOrderInvert((unsigned char *)bitmaps, bitmapsSize);
    if ((def_byte == def_bit) != (bit == byte)) {
	switch (bit == byte ? def_scan : scan) {
	case 1:
	    break;
	case 2:
	    TwoByteSwap((unsigned char *)bitmaps, bitmapsSize);
	    break;
	case 4:
	    FourByteSwap((unsigned char *)bitmaps, bitmapsSize);
	    break;
	}
    }
    if (def_glyph != glyph) {
	char	    *padbitmaps;
	int         sizepadbitmaps;
	int	    sizechar;
	CharInfoPtr metric;

	sizepadbitmaps = 0;
	metric = bitmapFont->metrics;
	for (i = 0; i < num_chars; i++)
	{
	    if (metric->bits)
		sizepadbitmaps += BYTES_FOR_GLYPH(metric,glyph);
	    metric++;
	}
	padbitmaps = (char *) xalloc(sizepadbitmaps);
	if (!padbitmaps) {
	    xfree (bitmaps);
	    xfree (fontspace);
	    return AllocError;
	}
	metric = bitmapFont->metrics;
	bitmapFont->bitmaps = padbitmaps;
	for (i = 0; i < num_chars; i++) {
	    sizechar = RepadBitmap(metric->bits, padbitmaps,
			       def_glyph, glyph,
			       metric->metrics.rightSideBearing -
			       metric->metrics.leftSideBearing,
			       metric->metrics.ascent + metric->metrics.descent);
	    metric->bits = padbitmaps;
	    padbitmaps += sizechar;
	    metric++;
	}
	xfree(bitmaps);
    }

    /* now read and atom'ize properties */

    ret = snfReadProps(&fi, &pFont->info, file);
    if (ret != Successful) {
	xfree(fontspace);
	return ret;
    }
    snfCopyInfo(&fi, &pFont->info);

    /* finally, read the ink metrics if the exist */

    if (fi.inkMetrics) {
	ret = Successful;
	ret = snfReadxCharInfo(file, &pFont->info.ink_minbounds);
	ret = snfReadxCharInfo(file, &pFont->info.ink_maxbounds);
	for (i = 0; ret == Successful && i < num_chars; i++)
	    ret = snfReadxCharInfo(file, &bitmapFont->ink_metrics[i]);
	if (ret != Successful) {
	    xfree(fontspace);
	    return ret;
	}
    } else {
	pFont->info.ink_minbounds = pFont->info.minbounds;
	pFont->info.ink_maxbounds = pFont->info.maxbounds;
    }

    if (pFont->info.defaultCh != (unsigned short) NO_SUCH_CHAR) {
	unsigned int r,
	            c,
	            cols;

	r = pFont->info.defaultCh >> 8;
	c = pFont->info.defaultCh & 0xFF;
	if (pFont->info.firstRow <= r && r <= pFont->info.lastRow &&
		pFont->info.firstCol <= c && c <= pFont->info.lastCol) {
	    cols = pFont->info.lastCol - pFont->info.firstCol + 1;
	    r = r - pFont->info.firstRow;
	    c = c - pFont->info.firstCol;
	    bitmapFont->pDefault = &bitmapFont->metrics[r * cols + c];
	}
    }
    bitmapFont->bitmapExtra = (BitmapExtraPtr) 0;
    pFont->fontPrivate = (pointer) bitmapFont;
    pFont->get_glyphs = bitmapGetGlyphs;
    pFont->get_metrics = bitmapGetMetrics;
    pFont->unload_font = snfUnloadFont;
    pFont->unload_glyphs = NULL;
    pFont->bit = bit;
    pFont->byte = byte;
    pFont->glyph = glyph;
    pFont->scan = scan;
    return Successful;
}

int
snfReadFontInfo(FontInfoPtr pFontInfo, FontFilePtr file)
{
    int         ret;
    snfFontInfoRec fi;
    int         bytestoskip;
    int         num_chars;

    ret = snfReadHeader(&fi, file);
    if (ret != Successful)
	return ret;
    snfCopyInfo(&fi, pFontInfo);

    pFontInfo->props = (FontPropPtr) xalloc(fi.nProps * sizeof(FontPropRec));
    if (!pFontInfo->props)
	return AllocError;
    pFontInfo->isStringProp = (char *) xalloc(fi.nProps * sizeof(char));
    if (!pFontInfo->isStringProp) {
	xfree(pFontInfo->props);
	return AllocError;
    }
    num_chars = n2dChars(&fi);
    bytestoskip = num_chars * sizeof(snfCharInfoRec);	/* charinfos */
    bytestoskip += BYTESOFGLYPHINFO(&fi);
    (void)FontFileSkip(file, bytestoskip);

    ret = snfReadProps(&fi, pFontInfo, file);
    if (ret != Successful) {
	xfree(pFontInfo->props);
	xfree(pFontInfo->isStringProp);
	return ret;
    }
    if (fi.inkMetrics) {
	ret = snfReadxCharInfo(file, &pFontInfo->ink_minbounds);
	if (ret != Successful) {
	    xfree(pFontInfo->props);
	    xfree(pFontInfo->isStringProp);
	    return ret;
	}
	ret = snfReadxCharInfo(file, &pFontInfo->ink_maxbounds);
	if (ret != Successful) {
	    xfree(pFontInfo->props);
	    xfree(pFontInfo->isStringProp);
	    return ret;
	}
    } else {
	pFontInfo->ink_minbounds = pFontInfo->minbounds;
	pFontInfo->ink_maxbounds = pFontInfo->maxbounds;
    }
    return Successful;

}

static void
snfUnloadFont(FontPtr pFont)
{
    BitmapFontPtr   bitmapFont;

    bitmapFont = (BitmapFontPtr) pFont->fontPrivate;
    xfree (bitmapFont->bitmaps);
    xfree (bitmapFont);
    DestroyFontRec (pFont);
}