| 1 |
//======================================================================== |
| 2 |
// |
| 3 |
// FoFiType1.cc |
| 4 |
// |
| 5 |
// Copyright 1999-2003 Glyph & Cog, LLC |
| 6 |
// |
| 7 |
//======================================================================== |
| 8 |
|
| 9 |
#include <aconf.h> |
| 10 |
|
| 11 |
#include <stdlib.h> |
| 12 |
#include <string.h> |
| 13 |
#include "gmem.h" |
| 14 |
#include "gmempp.h" |
| 15 |
#include "FoFiEncodings.h" |
| 16 |
#include "FoFiType1.h" |
| 17 |
|
| 18 |
//------------------------------------------------------------------------ |
| 19 |
// FoFiType1 |
| 20 |
//------------------------------------------------------------------------ |
| 21 |
|
| 22 |
FoFiType1 *FoFiType1::make(char *fileA, int lenA) { |
| 23 |
return new FoFiType1(fileA, lenA, gFalse); |
| 24 |
} |
| 25 |
|
| 26 |
FoFiType1 *FoFiType1::load(char *fileName) { |
| 27 |
char *fileA; |
| 28 |
int lenA; |
| 29 |
|
| 30 |
if (!(fileA = FoFiBase::readFile(fileName, &lenA))) { |
| 31 |
return NULL; |
| 32 |
} |
| 33 |
return new FoFiType1(fileA, lenA, gTrue); |
| 34 |
} |
| 35 |
|
| 36 |
FoFiType1::FoFiType1(char *fileA, int lenA, GBool freeFileDataA): |
| 37 |
FoFiBase(fileA, lenA, freeFileDataA) |
| 38 |
{ |
| 39 |
name = NULL; |
| 40 |
encoding = NULL; |
| 41 |
fontMatrix[0] = 0.001; |
| 42 |
fontMatrix[1] = 0; |
| 43 |
fontMatrix[2] = 0; |
| 44 |
fontMatrix[3] = 0.001; |
| 45 |
fontMatrix[4] = 0; |
| 46 |
fontMatrix[5] = 0; |
| 47 |
parsed = gFalse; |
| 48 |
undoPFB(); |
| 49 |
} |
| 50 |
|
| 51 |
FoFiType1::~FoFiType1() { |
| 52 |
int i; |
| 53 |
|
| 54 |
if (name) { |
| 55 |
gfree(name); |
| 56 |
} |
| 57 |
if (encoding && encoding != (char **)fofiType1StandardEncoding) { |
| 58 |
for (i = 0; i < 256; ++i) { |
| 59 |
gfree(encoding[i]); |
| 60 |
} |
| 61 |
gfree(encoding); |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
char *FoFiType1::getName() { |
| 66 |
if (!parsed) { |
| 67 |
parse(); |
| 68 |
} |
| 69 |
return name; |
| 70 |
} |
| 71 |
|
| 72 |
char **FoFiType1::getEncoding() { |
| 73 |
if (!parsed) { |
| 74 |
parse(); |
| 75 |
} |
| 76 |
return encoding; |
| 77 |
} |
| 78 |
|
| 79 |
void FoFiType1::getFontMatrix(double *mat) { |
| 80 |
int i; |
| 81 |
|
| 82 |
if (!parsed) { |
| 83 |
parse(); |
| 84 |
} |
| 85 |
for (i = 0; i < 6; ++i) { |
| 86 |
mat[i] = fontMatrix[i]; |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
void FoFiType1::writeEncoded(const char **newEncoding, |
| 91 |
FoFiOutputFunc outputFunc, void *outputStream) { |
| 92 |
char buf[512]; |
| 93 |
char *line, *line2, *p; |
| 94 |
int i; |
| 95 |
|
| 96 |
// copy everything up to the encoding |
| 97 |
for (line = (char *)file; |
| 98 |
line && line + 9 <= (char *)file + len && strncmp(line, "/Encoding", 9); |
| 99 |
line = getNextLine(line)) ; |
| 100 |
if (!line) { |
| 101 |
// no encoding - just copy the whole font file |
| 102 |
(*outputFunc)(outputStream, (char *)file, len); |
| 103 |
return; |
| 104 |
} |
| 105 |
(*outputFunc)(outputStream, (char *)file, (int)(line - (char *)file)); |
| 106 |
|
| 107 |
// write the new encoding |
| 108 |
(*outputFunc)(outputStream, "/Encoding 256 array\n", 20); |
| 109 |
(*outputFunc)(outputStream, |
| 110 |
"0 1 255 {1 index exch /.notdef put} for\n", 40); |
| 111 |
for (i = 0; i < 256; ++i) { |
| 112 |
if (newEncoding[i]) { |
| 113 |
snprintf(buf, sizeof(buf), "dup %d /%s put\n", i, newEncoding[i]); |
| 114 |
(*outputFunc)(outputStream, buf, (int)strlen(buf)); |
| 115 |
} |
| 116 |
} |
| 117 |
(*outputFunc)(outputStream, "readonly def\n", 13); |
| 118 |
|
| 119 |
// find the end of the encoding data |
| 120 |
//~ this ought to parse PostScript tokens |
| 121 |
if (line + 30 <= (char *)file + len && |
| 122 |
!strncmp(line, "/Encoding StandardEncoding def", 30)) { |
| 123 |
line = getNextLine(line); |
| 124 |
} else { |
| 125 |
// skip "/Encoding" + one whitespace char, |
| 126 |
// then look for 'def' preceded by PostScript whitespace |
| 127 |
p = line + 10; |
| 128 |
line = NULL; |
| 129 |
for (; p < (char *)file + len; ++p) { |
| 130 |
if ((*p == ' ' || *p == '\t' || *p == '\x0a' || |
| 131 |
*p == '\x0d' || *p == '\x0c' || *p == '\0') && |
| 132 |
p + 4 <= (char *)file + len && |
| 133 |
!strncmp(p + 1, "def", 3)) { |
| 134 |
line = p + 4; |
| 135 |
break; |
| 136 |
} |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
// some fonts have two /Encoding entries in their dictionary, so we |
| 141 |
// check for a second one here |
| 142 |
if (line) { |
| 143 |
for (line2 = line, i = 0; |
| 144 |
i < 20 && line2 && line2 + 9 <= (char *)file + len && |
| 145 |
strncmp(line2, "/Encoding", 9); |
| 146 |
line2 = getNextLine(line2), ++i) ; |
| 147 |
if (i < 20 && line2) { |
| 148 |
(*outputFunc)(outputStream, line, (int)(line2 - line)); |
| 149 |
if (line2 + 30 <= (char *)file + len && |
| 150 |
!strncmp(line2, "/Encoding StandardEncoding def", 30)) { |
| 151 |
line = getNextLine(line2); |
| 152 |
} else { |
| 153 |
// skip "/Encoding", |
| 154 |
// then look for 'def' preceded by PostScript whitespace |
| 155 |
p = line2 + 9; |
| 156 |
line = NULL; |
| 157 |
for (; p < (char *)file + len; ++p) { |
| 158 |
if ((*p == ' ' || *p == '\t' || *p == '\x0a' || |
| 159 |
*p == '\x0d' || *p == '\x0c' || *p == '\0') && |
| 160 |
p + 4 <= (char *)file + len && |
| 161 |
!strncmp(p + 1, "def", 3)) { |
| 162 |
line = p + 4; |
| 163 |
break; |
| 164 |
} |
| 165 |
} |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
// copy everything after the encoding |
| 170 |
if (line) { |
| 171 |
(*outputFunc)(outputStream, line, (int)(((char *)file + len) - line)); |
| 172 |
} |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
char *FoFiType1::getNextLine(char *line) { |
| 177 |
while (line < (char *)file + len && *line != '\x0a' && *line != '\x0d') { |
| 178 |
++line; |
| 179 |
} |
| 180 |
if (line < (char *)file + len && *line == '\x0d') { |
| 181 |
++line; |
| 182 |
} |
| 183 |
if (line < (char *)file + len && *line == '\x0a') { |
| 184 |
++line; |
| 185 |
} |
| 186 |
if (line >= (char *)file + len) { |
| 187 |
return NULL; |
| 188 |
} |
| 189 |
return line; |
| 190 |
} |
| 191 |
|
| 192 |
void FoFiType1::parse() { |
| 193 |
char *line, *line1, *p, *p2; |
| 194 |
char buf[256]; |
| 195 |
char c; |
| 196 |
unsigned int code; |
| 197 |
int n, base, i, j; |
| 198 |
GBool gotMatrix, startsWithDup, endsWithDup; |
| 199 |
|
| 200 |
gotMatrix = gFalse; |
| 201 |
for (i = 1, line = (char *)file; |
| 202 |
i <= 100 && line && (!name || !encoding || !gotMatrix); |
| 203 |
++i) { |
| 204 |
|
| 205 |
// get font name |
| 206 |
if (!name && line + 9 <= (char *)file + len && |
| 207 |
!strncmp(line, "/FontName", 9)) { |
| 208 |
n = 255; |
| 209 |
if (line + n > (char *)file + len) { |
| 210 |
n = (int)(((char *)file + len) - line); |
| 211 |
} |
| 212 |
strncpy(buf, line, n); |
| 213 |
buf[n] = '\0'; |
| 214 |
if ((p = strchr(buf+9, '/')) && |
| 215 |
(p = strtok(p+1, " \t\n\r"))) { |
| 216 |
name = copyString(p); |
| 217 |
} |
| 218 |
line = getNextLine(line); |
| 219 |
|
| 220 |
// get encoding |
| 221 |
} else if (!encoding && line + 30 <= (char *)file + len && |
| 222 |
!strncmp(line, "/Encoding StandardEncoding def", 30)) { |
| 223 |
encoding = (char **)fofiType1StandardEncoding; |
| 224 |
} else if (!encoding && line + 19 <= (char *)file + len && |
| 225 |
!strncmp(line, "/Encoding 256 array", 19)) { |
| 226 |
encoding = (char **)gmallocn(256, sizeof(char *)); |
| 227 |
for (j = 0; j < 256; ++j) { |
| 228 |
encoding[j] = NULL; |
| 229 |
} |
| 230 |
for (j = 0, line = getNextLine(line); |
| 231 |
j < 300 && line && (line1 = getNextLine(line)); |
| 232 |
++j, line = line1) { |
| 233 |
if ((n = (int)(line1 - line)) > 255) { |
| 234 |
n = 255; |
| 235 |
} |
| 236 |
strncpy(buf, line, n); |
| 237 |
buf[n] = '\0'; |
| 238 |
for (p = buf; *p == ' ' || *p == '\t'; ++p) ; |
| 239 |
endsWithDup = !strncmp(line - 4, "dup\x0a", 4) || |
| 240 |
!strncmp(line - 5, "dup\x0d", 4); |
| 241 |
startsWithDup = !strncmp(p, "dup", 3); |
| 242 |
if (endsWithDup || startsWithDup) { |
| 243 |
if (startsWithDup) { |
| 244 |
p += 3; |
| 245 |
} |
| 246 |
while (1) { |
| 247 |
for (; *p == ' ' || *p == '\t'; ++p) ; |
| 248 |
code = 0; |
| 249 |
if (*p == '8' && p[1] == '#') { |
| 250 |
base = 8; |
| 251 |
p += 2; |
| 252 |
} else if (*p >= '0' && *p <= '9') { |
| 253 |
base = 10; |
| 254 |
} else { |
| 255 |
break; |
| 256 |
} |
| 257 |
for (; *p >= '0' && *p < '0' + base; ++p) { |
| 258 |
code = code * base + (*p - '0'); |
| 259 |
} |
| 260 |
for (; *p == ' ' || *p == '\t'; ++p) ; |
| 261 |
if (*p != '/') { |
| 262 |
break; |
| 263 |
} |
| 264 |
++p; |
| 265 |
for (p2 = p; *p2 && *p2 != ' ' && *p2 != '\t'; ++p2) ; |
| 266 |
if (code < 256) { |
| 267 |
c = *p2; |
| 268 |
*p2 = '\0'; |
| 269 |
gfree(encoding[code]); |
| 270 |
encoding[code] = copyString(p); |
| 271 |
*p2 = c; |
| 272 |
} |
| 273 |
for (p = p2; *p == ' ' || *p == '\t'; ++p) ; |
| 274 |
if (strncmp(p, "put", 3)) { |
| 275 |
break; |
| 276 |
} |
| 277 |
for (p += 3; *p == ' ' || *p == '\t'; ++p) ; |
| 278 |
if (strncmp(p, "dup", 3)) { |
| 279 |
break; |
| 280 |
} |
| 281 |
p += 3; |
| 282 |
} |
| 283 |
} else { |
| 284 |
if (strtok(buf, " \t") && |
| 285 |
(p = strtok(NULL, " \t\n\r")) && !strcmp(p, "def")) { |
| 286 |
break; |
| 287 |
} |
| 288 |
} |
| 289 |
} |
| 290 |
//~ check for getinterval/putinterval junk |
| 291 |
|
| 292 |
} else if (!gotMatrix && line + 11 <= (char *)file + len && |
| 293 |
!strncmp(line, "/FontMatrix", 11)) { |
| 294 |
n = 255; |
| 295 |
if (line + 11 + n > (char *)file + len) { |
| 296 |
n = (int)(((char *)file + len) - (line + 11)); |
| 297 |
} |
| 298 |
strncpy(buf, line + 11, n); |
| 299 |
buf[n] = '\0'; |
| 300 |
if ((p = strchr(buf, '['))) { |
| 301 |
++p; |
| 302 |
if ((p2 = strchr(p, ']'))) { |
| 303 |
*p2 = '\0'; |
| 304 |
for (j = 0; j < 6; ++j) { |
| 305 |
if ((p = strtok(j == 0 ? p : (char *)NULL, " \t\n\r"))) { |
| 306 |
fontMatrix[j] = atof(p); |
| 307 |
} else { |
| 308 |
break; |
| 309 |
} |
| 310 |
} |
| 311 |
} |
| 312 |
} |
| 313 |
gotMatrix = gTrue; |
| 314 |
|
| 315 |
} else { |
| 316 |
line = getNextLine(line); |
| 317 |
} |
| 318 |
} |
| 319 |
|
| 320 |
parsed = gTrue; |
| 321 |
} |
| 322 |
|
| 323 |
// Undo the PFB encoding, i.e., remove the PFB headers. |
| 324 |
void FoFiType1::undoPFB() { |
| 325 |
GBool ok; |
| 326 |
Guchar *file2; |
| 327 |
int pos1, pos2, type; |
| 328 |
Guint segLen; |
| 329 |
|
| 330 |
ok = gTrue; |
| 331 |
if (getU8(0, &ok) != 0x80 || !ok) { |
| 332 |
return; |
| 333 |
} |
| 334 |
file2 = (Guchar *)gmalloc(len); |
| 335 |
pos1 = pos2 = 0; |
| 336 |
while (getU8(pos1, &ok) == 0x80 && ok) { |
| 337 |
type = getU8(pos1 + 1, &ok); |
| 338 |
if (type < 1 || type > 2 || !ok) { |
| 339 |
break; |
| 340 |
} |
| 341 |
segLen = getU32LE(pos1 + 2, &ok); |
| 342 |
pos1 += 6; |
| 343 |
if (!ok || !checkRegion(pos1, segLen)) { |
| 344 |
break; |
| 345 |
} |
| 346 |
memcpy(file2 + pos2, file + pos1, segLen); |
| 347 |
pos1 += segLen; |
| 348 |
pos2 += segLen; |
| 349 |
} |
| 350 |
if (freeFileData) { |
| 351 |
gfree(fileData); |
| 352 |
} |
| 353 |
file = fileData = file2; |
| 354 |
freeFileData = gTrue; |
| 355 |
len = pos2; |
| 356 |
} |