-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcid_read.c
214 lines (164 loc) · 4.92 KB
/
cid_read.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/*
cid_read -- read CID files
Copyright (C) 1999 Dieter Baron
This file is part of ttftot42, to use TrueType fonts in PostScript.
The author can be contacted at <dillo@giga.or.at>
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
3. The name of the author may not be used to endorse or promote
products derived from this software without specific prior
written permission.
THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS
OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "t42.h"
#include "cid.h"
#define get_short(p) ((*(p))+=2, (*((*(p))-2)<<8)+(*((*(p))-1)))
static int get_long(unsigned char **p);
static char *get_string(unsigned char **p);
static char *read_portion(FILE *f);
struct cid *
cid_read(char *fname)
{
struct cid *cid;
struct cid_cmap *cmap;
unsigned char b[6], *p, *q;
int i, j;
FILE *f;
if ((f=fopen(fname, "rb")) == NULL) {
/* error: can't open file */
return NULL;
}
if (fread(b, 1, 6, f) != 6) {
/* error: read error or file size < pre-header */
fclose(f);
return NULL;
}
if (strncmp(b, "CID0", 4) != 0) {
/* error: not a cid file */
fclose(f);
return NULL;
}
if (b[4] != 1) {
/* error: major version incompatibility */
return NULL;
}
if (b[5] > 0) {
/* warning: minor version incompatibility */
}
cid = cid_new();
cid->v_major = 1;
cid->v_minor = 0;
if ((q=p=read_portion(f)) == NULL) {
/* error: read error */
cid_free(cid);
fclose(f);
return NULL;
}
cid->registry = get_string(&p);
cid->ordering = get_string(&p);
cid->supplement = get_short(&p);
cid->nsupl = cid->supplement+1;
cid->supl_ncid = (int *)xmalloc(sizeof(int)*cid->nsupl);
for (i=0; i<cid->nsupl; i++)
cid->supl_ncid[i] = get_long(&p);
cid->ncmap = get_short(&p);
cid->cmap = (struct cid_cmap *)xmalloc(sizeof(struct cid_cmap)*cid->ncmap);
free(q);
for (i=0; i<cid->ncmap; i++) {
if ((q=p=read_portion(f)) == NULL) {
/* error: read error */
cid->ncmap = i;
cid_free(cid);
fclose(f);
return NULL;
}
cmap = cid->cmap+i;
cmap->pid = get_short(&p);
cmap->eid = get_short(&p);
cmap->nvert = get_long(&p);
cmap->vert = (struct cid_feature *)xmalloc(sizeof(struct cid_feature)
* cmap->nvert);
for (i=0; i<cmap->nvert; i++) {
cmap->vert[i].script = get_long(&p);
cmap->vert[i].language = get_long(&p);
cmap->vert[i].feature = get_long(&p);
}
cmap->nfeature = get_long(&p);
cmap->feature = (struct cid_feature *)xmalloc(sizeof(struct
cid_feature)
* cmap->nfeature);
for (i=0; i<cmap->nfeature; i++) {
cmap->feature[i].script = get_long(&p);
cmap->feature[i].language = get_long(&p);
cmap->feature[i].feature = get_long(&p);
}
cmap->code = (struct cid_code *)xmalloc(sizeof(struct cid_code));
cmap->code->nchar = get_long(&p);
cmap->code->ndata = cmap->code->size = get_long(&p);
cmap->code->data = (unsigned short *)xmalloc(sizeof(unsigned short)
* cmap->code->ndata);
for (j=0; j<cmap->code->ndata; j++)
cmap->code->data[j] = get_short(&p);
free(q);
}
fclose(f);
return cid;
}
static int
get_long(unsigned char **p)
{
int i;
i = (**p << 24) + (*((*p)+1) << 16) + (*((*p)+2) << 8) + (*((*p)+3));
*p += 4;
return i;
}
static char *
get_string(unsigned char **p)
{
int l;
char *s;
l = get_short(p);
s = (char *)xmalloc(l);
memcpy(s, *p, l);
*p += l;
return s;
}
static char *
read_portion(FILE *f)
{
int len;
unsigned char *data, b[4];
if (fread(b, 1, 4, f) != 4) {
/* error: read error */
return NULL;
}
data = b;
len = get_long(&data);
data = (char *)xmalloc(len);
if (fread(data, 1, len, f) != len) {
/* error: read error */
free(data);
return NULL;
}
return data;
}