1 //========================================================================
5 // Copyright 1999-2003 Glyph & Cog, LLC
7 //========================================================================
11 #ifdef USE_GCC_PRAGMAS
12 #pragma implementation
18 #include "FoFiEncodings.h"
19 #include "FoFiType1.h"
21 //------------------------------------------------------------------------
23 //------------------------------------------------------------------------
25 FoFiType1 *FoFiType1::make(char *fileA, int lenA) {
26 return new FoFiType1(fileA, lenA, gFalse);
29 FoFiType1 *FoFiType1::load(char *fileName) {
33 if (!(fileA = FoFiBase::readFile(fileName, &lenA))) {
36 return new FoFiType1(fileA, lenA, gTrue);
39 FoFiType1::FoFiType1(char *fileA, int lenA, GBool freeFileDataA):
40 FoFiBase(fileA, lenA, freeFileDataA)
47 FoFiType1::~FoFiType1() {
53 if (encoding && encoding != fofiType1StandardEncoding) {
54 for (i = 0; i < 256; ++i) {
61 char *FoFiType1::getName() {
68 char **FoFiType1::getEncoding() {
75 void FoFiType1::writeEncoded(char **newEncoding,
76 FoFiOutputFunc outputFunc, void *outputStream) {
81 // copy everything up to the encoding
82 for (line = (char *)file;
83 line && strncmp(line, "/Encoding", 9);
84 line = getNextLine(line)) ;
86 // no encoding - just copy the whole font file
87 (*outputFunc)(outputStream, (char *)file, len);
90 (*outputFunc)(outputStream, (char *)file, line - (char *)file);
92 // write the new encoding
93 (*outputFunc)(outputStream, "/Encoding 256 array\n", 20);
94 (*outputFunc)(outputStream,
95 "0 1 255 {1 index exch /.notdef put} for\n", 40);
96 for (i = 0; i < 256; ++i) {
98 sprintf(buf, "dup %d /%s put\n", i, newEncoding[i]);
99 (*outputFunc)(outputStream, buf, strlen(buf));
102 (*outputFunc)(outputStream, "readonly def\n", 13);
104 // copy everything after the encoding
105 if (!strncmp(line, "/Encoding StandardEncoding def", 30)) {
106 line = getNextLine(line);
108 for (line = getNextLine(line);
109 line && strncmp(line, "readonly def", 12);
110 line = getNextLine(line)) ;
112 line = getNextLine(line);
116 (*outputFunc)(outputStream, line, ((char *)file + len) - line);
120 char *FoFiType1::getNextLine(char *line) {
121 while (line < (char *)file + len && *line != '\x0a' && *line != '\x0d') {
124 if (line < (char *)file + len && *line == '\x0d') {
127 if (line < (char *)file + len && *line == '\x0a') {
130 if (line >= (char *)file + len) {
136 void FoFiType1::parse() {
137 char *line, *line1, *p, *p2;
142 for (i = 1, line = (char *)file;
143 i <= 100 && line && (!name || !encoding);
147 if (!name && !strncmp(line, "/FontName", 9)) {
148 strncpy(buf, line, 255);
150 if ((p = strchr(buf+9, '/')) &&
151 (p = strtok(p+1, " \t\n\r"))) {
152 name = copyString(p);
154 line = getNextLine(line);
157 } else if (!encoding &&
158 !strncmp(line, "/Encoding StandardEncoding def", 30)) {
159 encoding = fofiType1StandardEncoding;
160 } else if (!encoding &&
161 !strncmp(line, "/Encoding 256 array", 19)) {
162 encoding = (char **)gmallocn(256, sizeof(char *));
163 for (j = 0; j < 256; ++j) {
166 for (j = 0, line = getNextLine(line);
167 j < 300 && line && (line1 = getNextLine(line));
169 if ((n = line1 - line) > 255) {
172 strncpy(buf, line, n);
174 for (p = buf; *p == ' ' || *p == '\t'; ++p) ;
175 if (!strncmp(p, "dup", 3)) {
176 for (p += 3; *p == ' ' || *p == '\t'; ++p) ;
177 for (p2 = p; *p2 >= '0' && *p2 <= '9'; ++p2) ;
181 if ((code = atoi(p)) < 256) {
183 for (p = p2; *p == ' ' || *p == '\t'; ++p) ;
186 for (p2 = p; *p2 && *p2 != ' ' && *p2 != '\t'; ++p2) ;
188 encoding[code] = copyString(p);
193 p = strtok(buf, " \t\n\r");
196 if (!strcmp(p, "def")) break;
197 if (!strcmp(p, "readonly")) break;
198 // the spec does not says this but i'm mantaining old xpdf behaviour that accepts "foo def" as end of the encoding array
199 p = strtok(buf, " \t\n\r");
200 if (p && !strcmp(p, "def")) break;
204 //~ check for getinterval/putinterval junk
207 line = getNextLine(line);