1 /* $XConsortium: snfread.c,v 1.17 94/04/17 20:17:16 gildea Exp $ */
2 /************************************************************************
3 Copyright 1989 by Digital Equipment Corporation, Maynard, Massachusetts.
7 Permission to use, copy, modify, and distribute this software and its
8 documentation for any purpose and without fee is hereby granted,
9 provided that the above copyright notice appear in all copies and that
10 both that copyright notice and this permission notice appear in
11 supporting documentation, and that the name of Digital not be
12 used in advertising or publicity pertaining to distribution of the
13 software without specific, written prior permission.
15 DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
16 ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
17 DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
18 ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
19 WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
20 ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
23 ************************************************************************/
27 Copyright (c) 1994 X Consortium
29 Permission is hereby granted, free of charge, to any person obtaining
30 a copy of this software and associated documentation files (the
31 "Software"), to deal in the Software without restriction, including
32 without limitation the rights to use, copy, modify, merge, publish,
33 distribute, sublicense, and/or sell copies of the Software, and to
34 permit persons to whom the Software is furnished to do so, subject to
35 the following conditions:
37 The above copyright notice and this permission notice shall be included
38 in all copies or substantial portions of the Software.
40 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
41 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
42 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
43 IN NO EVENT SHALL THE X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR
44 OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
45 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
46 OTHER DEALINGS IN THE SOFTWARE.
48 Except as contained in this notice, the name of the X Consortium shall
49 not be used in advertising or otherwise to promote the sale, use or
50 other dealings in this Software without prior written authorization
51 from the X Consortium.
60 static void snfUnloadFont();
63 snfReadCharInfo(file, charInfo, base)
68 snfCharInfoRec snfCharInfo;
70 #define Width(m) ((m).rightSideBearing - (m).leftSideBearing)
71 #define Height(m) ((m).ascent + (m).descent)
73 if (FontFileRead(file, (char *) &snfCharInfo, sizeof snfCharInfo) !=
74 sizeof(snfCharInfo)) {
77 charInfo->metrics = snfCharInfo.metrics;
78 if (snfCharInfo.exists)
79 charInfo->bits = base + snfCharInfo.byteOffset;
86 snfReadxCharInfo(file, charInfo)
90 snfCharInfoRec snfCharInfo;
92 if (FontFileRead(file, (char *) &snfCharInfo, sizeof snfCharInfo) !=
93 sizeof(snfCharInfo)) {
96 *charInfo = snfCharInfo.metrics;
101 snfCopyInfo(snfInfo, pFontInfo)
102 snfFontInfoPtr snfInfo;
103 FontInfoPtr pFontInfo;
105 pFontInfo->firstCol = snfInfo->firstCol;
106 pFontInfo->lastCol = snfInfo->lastCol;
107 pFontInfo->firstRow = snfInfo->firstRow;
108 pFontInfo->lastRow = snfInfo->lastRow;
109 pFontInfo->defaultCh = snfInfo->chDefault;
110 pFontInfo->noOverlap = snfInfo->noOverlap;
111 pFontInfo->terminalFont = snfInfo->terminalFont;
112 pFontInfo->constantMetrics = snfInfo->constantMetrics;
113 pFontInfo->constantWidth = snfInfo->constantWidth;
114 pFontInfo->inkInside = snfInfo->inkInside;
115 pFontInfo->inkMetrics = snfInfo->inkMetrics;
116 pFontInfo->allExist = snfInfo->allExist;
117 pFontInfo->drawDirection = snfInfo->drawDirection;
118 pFontInfo->anamorphic = FALSE;
119 pFontInfo->cachable = TRUE;
120 pFontInfo->maxOverlap = 0;
121 pFontInfo->minbounds = snfInfo->minbounds.metrics;
122 pFontInfo->maxbounds = snfInfo->maxbounds.metrics;
123 pFontInfo->fontAscent = snfInfo->fontAscent;
124 pFontInfo->fontDescent = snfInfo->fontDescent;
125 pFontInfo->nprops = snfInfo->nProps;
129 snfReadProps(snfInfo, pFontInfo, file)
130 snfFontInfoPtr snfInfo;
131 FontInfoPtr pFontInfo;
136 snfFontPropPtr psnfp;
141 bytestoalloc = snfInfo->nProps * sizeof(snfFontPropRec) +
142 BYTESOFSTRINGINFO(snfInfo);
143 propspace = (char *) xalloc(bytestoalloc);
147 if (FontFileRead(file, propspace, bytestoalloc) != bytestoalloc) {
151 psnfp = (snfFontPropPtr) propspace;
153 strings = propspace + BYTESOFPROPINFO(snfInfo);
155 for (i = 0, pfp = pFontInfo->props; i < snfInfo->nProps; i++, pfp++, psnfp++) {
156 pfp->name = MakeAtom(&strings[psnfp->name],
157 (unsigned) strlen(&strings[psnfp->name]), 1);
158 pFontInfo->isStringProp[i] = psnfp->indirect;
160 pfp->value = (INT32) MakeAtom(&strings[psnfp->value],
161 (unsigned) strlen(&strings[psnfp->value]), 1);
163 pfp->value = psnfp->value;
171 snfReadHeader(snfInfo, file)
172 snfFontInfoPtr snfInfo;
175 if (FontFileRead(file, (char *) snfInfo, sizeof *snfInfo) != sizeof *snfInfo)
178 if (snfInfo->version1 != FONT_FILE_VERSION ||
179 snfInfo->version2 != FONT_FILE_VERSION)
185 snfReadFont(pFont, file, bit, byte, glyph, scan)
194 unsigned bytestoalloc;
197 BitmapFontPtr bitmapFont;
204 int isStringProp_off;
207 int def_bit, def_byte, def_glyph, def_scan;
209 ret = snfReadHeader(&fi, file);
210 if (ret != Successful)
213 SnfGetFormat (&def_bit, &def_byte, &def_glyph, &def_scan);
216 * we'll allocate one chunk of memory and split it among the various parts
219 * BitmapFontRec CharInfoRec's Glyphs Encoding DIX Properties Ink CharInfoRec's
221 * If the glyphpad is not the same as the font file, then the glyphs
222 * are allocated separately, to be later realloc'ed when we know
223 * how big to make them.
226 bitmapsSize = BYTESOFGLYPHINFO(&fi);
227 num_chars = n2dChars(&fi);
228 bytestoalloc = sizeof(BitmapFontRec); /* bitmapFont */
229 metrics_off = bytestoalloc;
230 bytestoalloc += num_chars * sizeof(CharInfoRec); /* metrics */
231 encoding_off = bytestoalloc;
232 bytestoalloc += num_chars * sizeof(CharInfoPtr); /* encoding */
233 props_off = bytestoalloc;
234 bytestoalloc += fi.nProps * sizeof(FontPropRec); /* props */
235 isStringProp_off = bytestoalloc;
236 bytestoalloc += fi.nProps * sizeof(char); /* isStringProp */
237 bytestoalloc = (bytestoalloc + 3) & ~3;
238 ink_off = bytestoalloc;
240 bytestoalloc += num_chars * sizeof(xCharInfo); /* ink_metrics */
242 fontspace = (char *) xalloc(bytestoalloc);
246 bitmaps = (char *) xalloc (bitmapsSize);
253 * now fix up pointers
256 bitmapFont = (BitmapFontPtr) fontspace;
257 bitmapFont->num_chars = num_chars;
258 bitmapFont->metrics = (CharInfoPtr) (fontspace + metrics_off);
259 bitmapFont->encoding = (CharInfoPtr *) (fontspace + encoding_off);
260 bitmapFont->bitmaps = bitmaps;
261 bitmapFont->pDefault = NULL;
262 bitmapFont->bitmapExtra = NULL;
263 pFont->info.props = (FontPropPtr) (fontspace + props_off);
264 pFont->info.isStringProp = (char *) (fontspace + isStringProp_off);
266 bitmapFont->ink_metrics = (xCharInfo *) (fontspace + ink_off);
268 bitmapFont->ink_metrics = 0;
275 for (i = 0; ret == Successful && i < num_chars; i++) {
276 ret = snfReadCharInfo(file, &bitmapFont->metrics[i], bitmaps);
277 if (bitmapFont->metrics[i].bits)
278 bitmapFont->encoding[i] = &bitmapFont->metrics[i];
280 bitmapFont->encoding[i] = 0;
283 if (ret != Successful) {
292 if (FontFileRead(file, (char *) bitmaps, bitmapsSize) != bitmapsSize) {
299 BitOrderInvert(bitmaps, bitmapsSize);
300 if ((def_byte == def_bit) != (bit == byte)) {
301 switch (bit == byte ? def_scan : scan) {
305 TwoByteSwap(bitmaps, bitmapsSize);
308 FourByteSwap(bitmaps, bitmapsSize);
312 if (def_glyph != glyph) {
319 metric = bitmapFont->metrics;
320 for (i = 0; i < num_chars; i++)
323 sizepadbitmaps += BYTES_FOR_GLYPH(metric,glyph);
326 padbitmaps = (char *) xalloc(sizepadbitmaps);
332 metric = bitmapFont->metrics;
333 bitmapFont->bitmaps = padbitmaps;
334 for (i = 0; i < num_chars; i++) {
335 sizechar = RepadBitmap(metric->bits, padbitmaps,
337 metric->metrics.rightSideBearing -
338 metric->metrics.leftSideBearing,
339 metric->metrics.ascent + metric->metrics.descent);
340 metric->bits = padbitmaps;
341 padbitmaps += sizechar;
347 /* now read and atom'ize properties */
349 ret = snfReadProps(&fi, &pFont->info, file);
350 if (ret != Successful) {
354 snfCopyInfo(&fi, &pFont->info);
356 /* finally, read the ink metrics if the exist */
360 ret = snfReadxCharInfo(file, &pFont->info.ink_minbounds);
361 ret = snfReadxCharInfo(file, &pFont->info.ink_maxbounds);
362 for (i = 0; ret == Successful && i < num_chars; i++)
363 ret = snfReadxCharInfo(file, &bitmapFont->ink_metrics[i]);
364 if (ret != Successful) {
369 pFont->info.ink_minbounds = pFont->info.minbounds;
370 pFont->info.ink_maxbounds = pFont->info.maxbounds;
373 if (pFont->info.defaultCh != (unsigned short) NO_SUCH_CHAR) {
378 r = pFont->info.defaultCh >> 8;
379 c = pFont->info.defaultCh & 0xFF;
380 if (pFont->info.firstRow <= r && r <= pFont->info.lastRow &&
381 pFont->info.firstCol <= c && c <= pFont->info.lastCol) {
382 cols = pFont->info.lastCol - pFont->info.firstCol + 1;
383 r = r - pFont->info.firstRow;
384 c = c - pFont->info.firstCol;
385 bitmapFont->pDefault = &bitmapFont->metrics[r * cols + c];
388 bitmapFont->bitmapExtra = (BitmapExtraPtr) 0;
389 pFont->fontPrivate = (pointer) bitmapFont;
390 pFont->get_glyphs = bitmapGetGlyphs;
391 pFont->get_metrics = bitmapGetMetrics;
392 pFont->unload_font = snfUnloadFont;
393 pFont->unload_glyphs = NULL;
396 pFont->glyph = glyph;
402 snfReadFontInfo(pFontInfo, file)
403 FontInfoPtr pFontInfo;
411 ret = snfReadHeader(&fi, file);
412 if (ret != Successful)
414 snfCopyInfo(&fi, pFontInfo);
416 pFontInfo->props = (FontPropPtr) xalloc(fi.nProps * sizeof(FontPropRec));
417 if (!pFontInfo->props)
419 pFontInfo->isStringProp = (char *) xalloc(fi.nProps * sizeof(char));
420 if (!pFontInfo->isStringProp) {
421 xfree(pFontInfo->props);
424 num_chars = n2dChars(&fi);
425 bytestoskip = num_chars * sizeof(snfCharInfoRec); /* charinfos */
426 bytestoskip += BYTESOFGLYPHINFO(&fi);
427 FontFileSkip(file, bytestoskip);
429 ret = snfReadProps(&fi, pFontInfo, file);
430 if (ret != Successful) {
431 xfree(pFontInfo->props);
432 xfree(pFontInfo->isStringProp);
436 ret = snfReadxCharInfo(file, &pFontInfo->ink_minbounds);
437 if (ret != Successful) {
438 xfree(pFontInfo->props);
439 xfree(pFontInfo->isStringProp);
442 ret = snfReadxCharInfo(file, &pFontInfo->ink_maxbounds);
443 if (ret != Successful) {
444 xfree(pFontInfo->props);
445 xfree(pFontInfo->isStringProp);
449 pFontInfo->ink_minbounds = pFontInfo->minbounds;
450 pFontInfo->ink_maxbounds = pFontInfo->maxbounds;
460 BitmapFontPtr bitmapFont;
462 bitmapFont = (BitmapFontPtr) pFont->fontPrivate;
463 xfree (bitmapFont->bitmaps);
465 xfree (pFont->devPrivates);
470 static int snf_bit, snf_byte, snf_glyph, snf_scan;
472 SnfSetFormat (bit, byte, glyph, scan)
473 int bit, byte, glyph, scan;
482 SnfGetFormat (bit, byte, glyph, scan)
483 int *bit, *byte, *glyph, *scan;
486 FontDefaultFormat (&snf_bit, &snf_byte, &snf_glyph, &snf_scan);