#ifndef lint static char sccsid[] = "@(#)rdadobefnt.c 9.2 88/01/19 Copyright 1984 Sun Micro"; #endif /* * Copyright (c) 1984 by Sun Microsystems, Inc. */ /*- Read ASCII coded font files as produced by Adobe Systems Inc. rdadobefnt.c, Tue Jun 4 16:41:19 1985 James Gosling, Sun Microsystems */ #include #include #ifdef REF #include #include #endif #include "fract.h" #include "font.h" #include "NameMap.h" static char *linep; static char *linee; static char *filee; static char *token; static char *filename; static lineno; static struct font AdobeFont; static int NAdobeGlyphs, AdobeSize; struct AdobeGlyphs { char *name; struct wglyph glyph; int slot; } *AdobeGlyphs; unsigned char ReverseBitsInByte[256] = { 0x00, 0x80, 0x40, 0xc0, 0x20, 0xa0, 0x60, 0xe0, 0x10, 0x90, 0x50, 0xd0, 0x30, 0xb0, 0x70, 0xf0, 0x08, 0x88, 0x48, 0xc8, 0x28, 0xa8, 0x68, 0xe8, 0x18, 0x98, 0x58, 0xd8, 0x38, 0xb8, 0x78, 0xf8, 0x04, 0x84, 0x44, 0xc4, 0x24, 0xa4, 0x64, 0xe4, 0x14, 0x94, 0x54, 0xd4, 0x34, 0xb4, 0x74, 0xf4, 0x0c, 0x8c, 0x4c, 0xcc, 0x2c, 0xac, 0x6c, 0xec, 0x1c, 0x9c, 0x5c, 0xdc, 0x3c, 0xbc, 0x7c, 0xfc, 0x02, 0x82, 0x42, 0xc2, 0x22, 0xa2, 0x62, 0xe2, 0x12, 0x92, 0x52, 0xd2, 0x32, 0xb2, 0x72, 0xf2, 0x0a, 0x8a, 0x4a, 0xca, 0x2a, 0xaa, 0x6a, 0xea, 0x1a, 0x9a, 0x5a, 0xda, 0x3a, 0xba, 0x7a, 0xfa, 0x06, 0x86, 0x46, 0xc6, 0x26, 0xa6, 0x66, 0xe6, 0x16, 0x96, 0x56, 0xd6, 0x36, 0xb6, 0x76, 0xf6, 0x0e, 0x8e, 0x4e, 0xce, 0x2e, 0xae, 0x6e, 0xee, 0x1e, 0x9e, 0x5e, 0xde, 0x3e, 0xbe, 0x7e, 0xfe, 0x01, 0x81, 0x41, 0xc1, 0x21, 0xa1, 0x61, 0xe1, 0x11, 0x91, 0x51, 0xd1, 0x31, 0xb1, 0x71, 0xf1, 0x09, 0x89, 0x49, 0xc9, 0x29, 0xa9, 0x69, 0xe9, 0x19, 0x99, 0x59, 0xd9, 0x39, 0xb9, 0x79, 0xf9, 0x05, 0x85, 0x45, 0xc5, 0x25, 0xa5, 0x65, 0xe5, 0x15, 0x95, 0x55, 0xd5, 0x35, 0xb5, 0x75, 0xf5, 0x0d, 0x8d, 0x4d, 0xcd, 0x2d, 0xad, 0x6d, 0xed, 0x1d, 0x9d, 0x5d, 0xdd, 0x3d, 0xbd, 0x7d, 0xfd, 0x03, 0x83, 0x43, 0xc3, 0x23, 0xa3, 0x63, 0xe3, 0x13, 0x93, 0x53, 0xd3, 0x33, 0xb3, 0x73, 0xf3, 0x0b, 0x8b, 0x4b, 0xcb, 0x2b, 0xab, 0x6b, 0xeb, 0x1b, 0x9b, 0x5b, 0xdb, 0x3b, 0xbb, 0x7b, 0xfb, 0x07, 0x87, 0x47, 0xc7, 0x27, 0xa7, 0x67, 0xe7, 0x17, 0x97, 0x57, 0xd7, 0x37, 0xb7, 0x77, 0xf7, 0x0f, 0x8f, 0x4f, 0xcf, 0x2f, 0xaf, 0x6f, 0xef, 0x1f, 0x9f, 0x5f, 0xdf, 0x3f, 0xbf, 0x7f, 0xff, }; static char * getline() { lineno++; while (linee < filee && (*linee == '\r' || *linee == '\n' || *linee == 0)) linee++; linep = linee; while (linee < filee && *linee != '\n' && *linee != '\r' && *linee) linee++; return linep < filee ? linep : 0; } static char * gettoken() { while (linep < linee && *linep <= ' ') linep++; if (linep >= linee) { token = 0; return 0; } token = linep; while (linep < linee && *linep > ' ') linep++; return token; } static tokenis(s) register char *s; { register char *p = token; if (p == 0) return 0; for (; p < linep && *s && (isupper(*p) ? tolower(*p) == *s : *p == *s); p++, s++); return *s == 0 && p == linep; } static foldedeq(p, s) register char *s, *p; { while (*p && *s && (isupper(*p) ? tolower(*p) == *s : *p == *s)) p++, s++; return *s == 0 && *p == 0; } static getint(base) { register n = 0; register neg = 0; gettoken(); if (*token == '-') neg++, token++; if (token[0] == '0' && token[1] == 'x') base = 16, token += 2; while (token < linep && isxdigit(*token)) { n = n * base + (isdigit(*token) ? *token - '0' : (*token & 07) + 9); token++; } return neg ? -n : n; } static LookupName(name, len, table, tsize) register char *name; struct NameMap *table; { register struct NameMap *lo, *hi, *mid; lo = &table[0]; hi = &table[tsize - 1]; while (lo <= hi) { register dif; mid = (hi - lo) / 2 + lo; if ((dif = name[0] - mid->name[0]) == 0) if (len < mid->namesize) { if ((dif = strncmp(name, mid->name, len)) == 0) dif = -1; } else if ((dif = strncmp(name, mid->name, mid->namesize)) == 0) if (len == mid->namesize) return mid - table; else dif = 1; if (dif < 0) hi = mid - 1; else lo = mid + 1; } return -1; } static syntax() { fprintf(stderr, "Syntax error in file %s at line %d:\n %.*s\n", filename, lineno, linee - token, token); exit(0); } #define Cname(n,i) "n", sizeof "n"-1, i static struct NameMap keywords[] = { Cname(bbx, 1), Cname(bitmap, 2), Cname(chars, 3), Cname(comment, 4), Cname(dwidth, 5), Cname(encoding, 6), Cname(endchar, 7), Cname(endfont, 8), Cname(font, 9), Cname(fontboundingbox, 10), Cname(size, 11), Cname(startchar, 12), Cname(startfont, 13), Cname(swidth, 14) }; static struct NameMap encodings[] = { Cname(adobe, (int) AdobeEncoding), /* The Adobe version of ASCII */ Cname(adobesymbol, (int) AdobeSymbolEncoding), Cname(ascii, (int) ASCIIencoding), /* 8 bit western European ASCII */ Cname(cyrillic, (int) CyrillicEncoding), Cname(devenageri, (int) DevenageriEncoding), Cname(hangul, (int) HangulEncoding), Cname(jisc6226, (int) JISC6226Encoding), /* Japanese Industrial * Standard Kanjii */ }; static enum FontEncoding getencoding() { register i; gettoken(); i = LookupName(token, linep - token, encodings, sizeof encodings / sizeof encodings[0]); return i >= 0 ? (enum FontEncoding) encodings[i].index : ASCIIencoding; } static lowercase() { register char *p = token; while (p < linep) { if (isupper(*p)) *p = tolower(*p); p++; } } LookupSymname() { register index; switch (AdobeFont.encoding) { default: index = LookupName(token, linep - token, AdobeMap, AdobeMapSize); if (index >= 0) index = AdobeMap[index].index; break; case AdobeSymbolEncoding: index = LookupName(token, linep - token, AdobeSymbolMap, AdobeSymbolMapSize); if (index >= 0) index = AdobeSymbolMap[index].index; break; } return index; } struct font * cst_readadobefont(file, len, name) char *file; char *name; { register struct AdobeGlyphs *p = 0; struct wglyph wg; struct glyph g; int maxindex = 0; char *formatversion = "0.0"; filee = file + len; linee = file; filename = name; lineno = 0; NAdobeGlyphs = 0; if (AdobeSize == 0) { AdobeSize = 256; AdobeGlyphs = (struct AdobeGlyphs *) snoopalloc("rdadobefnt.c", sizeof(struct AdobeGlyphs) * AdobeSize); } AdobeFont.left_to_right = 0; AdobeFont.narrow = 0; AdobeFont.type = LRbitmapFont; AdobeFont.encoding = AdobeEncoding; AdobeFont.printermatched = 0; AdobeFont.name = 0; AdobeFont.matrix[0][0] = 0; AdobeFont.matrix[0][1] = 0; AdobeFont.matrix[1][0] = 0; AdobeFont.matrix[1][1] = 0; AdobeFont.comment = 0; while (getline()) { if (gettoken() == 0) continue; lowercase(); { register index = LookupName(token, linep - token, keywords, sizeof keywords / sizeof keywords[0]); if (index < 0) syntax(); switch (keywords[index].index) { case 1: /* bbx */ g.size.x = getint(10); g.size.y = getint(10); g.origin.x = -getint(10); g.origin.y = g.size.y + getint(10); break; case 2: /* bitmap */ g.length = sizeof(struct glyph) + (g.size.x + 15) / 16 * g.size.y * 2; p->glyph.glyph = (struct glyph *) snoopalloc("rdadobefnt.c", g.length); *p->glyph.glyph = g; { register unsigned short *bits = ((struct generic_glyph *) p->glyph.glyph)->bits; register int i = 0; while (i < g.size.y) { register n = ((g.size.x + 15) >> 4); register k; register unsigned short w; if (getline() == 0) break; while (n-- != 0) { k = 4; w = 0; while (k-- != 0) if (*linep < ' ') w <<= 4; else if (isdigit(*linep)) w = (w << 4) + *linep++ - '0'; else w = (w << 4) + (*linep++ & 7) + 9; #ifdef LITTLEENDIAN *bits++ = (ReverseBitsInByte[w & 0xFF] << 8) | ReverseBitsInByte[w >> 8]; #else *bits++ = w; #endif } i++; } } break; case 3: /* chars */ break; case 4: /* comment */ if (gettoken()) if (tokenis("encoding")) AdobeFont.encoding = getencoding(); else if (tokenis("printermatched")) AdobeFont.printermatched = 1; else if (tokenis("copyright") || AdobeFont.comment == 0) { AdobeFont.comment = token; *linee = 0; } break; case 5: /* dwidth */ p->glyph.width.x = fracti(getint(10)); p->glyph.width.y = fracti(getint(10)); break; case 6: /* encoding */ if (p->slot < 0) { p->slot = getint(10); if (p->slot > maxindex) maxindex = p->slot; } break; case 7: /* endchar */ break; case 8: /* endfont */ break; case 9: /* font */ if (AdobeFont.name == 0 /* strcmp(formatversion,"1.4") > 0 */ && gettoken()) { register char *p = linep; AdobeFont.name = token; do *p-- = 0; while (isdigit(*p)); if (foldedeq(AdobeFont.name, "symbol")) AdobeFont.encoding = AdobeSymbolEncoding; } break; case 10: /* fontboundingbox */ break; case 11: /* size */ AdobeFont.matrix[0][0] = fracti(getint(10)); AdobeFont.matrix[1][1] = AdobeFont.matrix[0][0]; break; case 12: /* startchar */ if (NAdobeGlyphs >= AdobeSize) { AdobeSize = (AdobeSize * 3) / 2; AdobeGlyphs = (struct AdobeGlyphs *) snooprealloc ("rdadobefnt.c", AdobeGlyphs, sizeof(struct AdobeGlyphs) * AdobeSize); } p = &AdobeGlyphs[NAdobeGlyphs++]; if (gettoken()) { p->name = token; *linee = 0; } else p->name = 0; if (p->name) { p->slot = LookupSymname(); if (p->slot > maxindex) maxindex = p->slot; } else p->slot = -1; g.length = 0; p->glyph.width.x = 0; p->glyph.width.y = 0; g.origin.x = 0; g.origin.y = 0; g.size.x = 0; g.size.y = 0; p->glyph.glyph = 0; break; case 13: /* startfont */ if (gettoken()) { formatversion = token; *linep = 0; } break; case 14: /* swidth */ break; } } } { register char *s; register i; #ifndef SYSVREF extern char *rindex(); #endif if ((s = rindex(name, '/')) == 0) s = name; i = strlen(s); if (strncmp(s + i - 5, ".bits", 5) == 0) { register char *d = (char *) snoopalloc("rdadobefnt.c", i - 4); register p = i - 5; while (isdigit(s[p - 1])) p--; AdobeFont.name = d; strncpy(d, s, p); d[p] = 0; if (foldedeq(AdobeFont.name, "symbol")) AdobeFont.encoding = AdobeSymbolEncoding; } } { register struct font *f; register i; i = sizeof(struct font) + (maxindex + 1) * sizeof(struct wglyph); f = (struct font *) snoopalloc("rdadobefnt.c", i); bzero(f, i); *f = AdobeFont; f->nglyphs = maxindex + 1; for (i = NAdobeGlyphs; --i >= 0;) if (AdobeGlyphs[i].slot >= 0) f->glyphs[AdobeGlyphs[i].slot] = AdobeGlyphs[i].glyph; return f; } } static struct NameMap metrickeywords[] = { Cname(c, 1), Cname(comment, 2), Cname(font, 4), Cname(fontbbox, 3), Cname(fontboundingbox, 3), Cname(fontname, 4), Cname(fullname, 5), Cname(notice, 6), Cname(startfontmetrics, 7), }; struct font * cst_readfontmetrics(file, len, name) char *file; char *name; { register struct AdobeGlyphs *p = 0; struct wglyph wg; int maxindex = 0; filee = file + len; linee = file; filename = name; lineno = 0; NAdobeGlyphs = 0; if (AdobeSize == 0) { AdobeSize = 256; AdobeGlyphs = (struct AdobeGlyphs *) snoopalloc("rdadobefnt.c", sizeof(struct AdobeGlyphs) * AdobeSize); } bzero(&AdobeFont, sizeof AdobeFont); AdobeFont.type = PrinterWidthFont; AdobeFont.encoding = AdobeEncoding; AdobeFont.printermatched = 1; while (getline()) { if (gettoken() == 0) continue; lowercase(); #define expecting(s) gettoken(); if(!tokenis(s)) syntax(); { register index = LookupName(token, linep - token, metrickeywords, sizeof metrickeywords / sizeof metrickeywords[0]); if (index >= 0) switch (metrickeywords[index].index) { case 1: /* c */ { int code, lcode, width; char *chname; register struct AdobeGlyphs *p; code = getint(10); expecting(";"); expecting("wx"); width = getint(10); expecting(";"); expecting("n"); chname = gettoken(); lcode = LookupSymname(); if (lcode >= 0) code = lcode; if (NAdobeGlyphs >= AdobeSize) { AdobeSize = (AdobeSize * 3) / 2; AdobeGlyphs = (struct AdobeGlyphs *) snooprealloc ("rdadobefnt.c", AdobeGlyphs, sizeof(struct AdobeGlyphs) * AdobeSize); } p = &AdobeGlyphs[NAdobeGlyphs++]; p->name = chname; *linep++ = 0; p->slot = code; if (code > maxindex) maxindex = code; p->glyph.glyph = 0; p->glyph.width.y = 0; p->glyph.width.x = fraction(width, 1000); } break; case 2: /* comment */ case 6: /* notice */ if (gettoken()) if (tokenis("encoding")) AdobeFont.encoding = getencoding(); else if (tokenis("copyright") || AdobeFont.comment == 0) { AdobeFont.comment = token; *linee = 0; } case 7: /* startfontmetrics */ break; case 3: /* fontbbox */ { int llx, lly, urx, ury; llx = getint(10); lly = getint(10); urx = getint(10); ury = getint(10); AdobeFont.size.x = urx - llx; AdobeFont.size.y = ury - lly; AdobeFont.origin.x = -llx; AdobeFont.origin.y = ury; } break; case 4: /* fontname */ case 5: /* fullname */ if (AdobeFont.name == 0 && gettoken()) { register len = linep - token; AdobeFont.name = (char *) snoopalloc("rdadobefnt.c", len + 1); strncpy(AdobeFont.name, token, len); AdobeFont.name[len] = 0; if (foldedeq(AdobeFont.name, "symbol")) AdobeFont.encoding = AdobeSymbolEncoding; } break; } } } { register struct font *f; register i; i = sizeof(struct font) + (maxindex + 1) * sizeof(struct wglyph); f = (struct font *) snoopalloc("rdadobefnt.c", i); bzero(f, i); *f = AdobeFont; f->nglyphs = maxindex + 1; for (i = NAdobeGlyphs; --i >= 0;) if (AdobeGlyphs[i].slot >= 0) f->glyphs[AdobeGlyphs[i].slot] = AdobeGlyphs[i].glyph; return f; } }