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
)) ;
113 (*outputFunc
)(outputStream
, line
, ((char *)file
+ len
) - line
);
117 char *FoFiType1::getNextLine(char *line
) {
118 while (line
< (char *)file
+ len
&& *line
!= '\x0a' && *line
!= '\x0d') {
121 if (line
< (char *)file
+ len
&& *line
== '\x0d') {
124 if (line
< (char *)file
+ len
&& *line
== '\x0a') {
127 if (line
>= (char *)file
+ len
) {
133 void FoFiType1::parse() {
134 char *line
, *line1
, *p
, *p2
;
139 for (i
= 1, line
= (char *)file
;
140 i
<= 100 && line
&& (!name
|| !encoding
);
144 if (!name
&& !strncmp(line
, "/FontName", 9)) {
145 strncpy(buf
, line
, 255);
147 if ((p
= strchr(buf
+9, '/')) &&
148 (p
= strtok(p
+1, " \t\n\r"))) {
149 name
= copyString(p
);
151 line
= getNextLine(line
);
154 } else if (!encoding
&&
155 !strncmp(line
, "/Encoding StandardEncoding def", 30)) {
156 encoding
= fofiType1StandardEncoding
;
157 } else if (!encoding
&&
158 !strncmp(line
, "/Encoding 256 array", 19)) {
159 encoding
= (char **)gmalloc(256 * sizeof(char *));
160 for (j
= 0; j
< 256; ++j
) {
163 line
= getNextLine(line
);
164 for (j
= 0; j
< 300 && line
; ++j
) {
165 line1
= getNextLine(line
);
166 if ((n
= line1
- line
) > 255) {
169 strncpy(buf
, line
, n
);
171 for (p
= buf
; *p
== ' ' || *p
== '\t'; ++p
) ;
172 if (!strncmp(p
, "dup", 3)) {
173 for (p
+= 3; *p
== ' ' || *p
== '\t'; ++p
) ;
174 for (p2
= p
; *p2
>= '0' && *p2
<= '9'; ++p2
) ;
178 if ((code
= atoi(p
)) < 256) {
180 for (p
= p2
; *p
== ' ' || *p
== '\t'; ++p
) ;
183 for (p2
= p
; *p2
&& *p2
!= ' ' && *p2
!= '\t'; ++p2
) ;
185 encoding
[code
] = copyString(p
);
190 if (strtok(buf
, " \t") &&
191 (p
= strtok(NULL
, " \t\n\r")) && !strcmp(p
, "def")) {
197 //~ check for getinterval/putinterval junk
200 line
= getNextLine(line
);