Debian Codegraph @

texk/xdvipsk/sfntload.c

raw
1 /*
2 * Loads a SFNT tables based font file. It marks the characters as undefined.
3 */
4 #include "xdvips.h" /* The copyright notice in that file is included too! */
5 #ifdef KPATHSEA
6 #include <kpathsea/c-pathmx.h>
7 #include <kpathsea/concatn.h>
8 #include <kpathsea/variable.h>
9 #ifdef WIN32
10 #define getcwd _getcwd
11 #endif
12 #else
13 /* Be sure we have `isascii'. */
14 #ifndef WIN32
15 #if !(defined(HAVE_DECL_ISASCII) && HAVE_DECL_ISASCII)
16 #define isascii(c) (((c) & ~0x7f) == 0)
17 #endif
18 #else
19 #define getcwd _getcwd
20 #endif
21 #endif
22 /*
23 * The external declarations:
24 */
25 #include "protos.h"
26
27 #include "mem.h"
28 #include "error.h"
29 #include "dpxfile.h"
30 #include "sfnt.h"
31 #include "cmap.h"
32
33 #include "tt_aux.h"
34 #include "tt_cmap.h"
35 #include "tt_table.h"
36
37 #include "cff.h"
38
39
40 #ifndef KPATHSEA
41 char *expandfilename(const char *src)
42 {
43 const char *s, *var_end;
44 const char *st = NULL;
45 unsigned len;
46 char *var, *ret;
47
48 s = src;
49 if (*s=='$') {
50 s++;
51 var_end = s;
52 do {
53 var_end++;
54 } while ((isascii(*var_end) && isalnum((unsigned char)*var_end)) || *var_end == '_');
55 st = var_end;
56 var_end--; /* had to go one past */
57 len = var_end - s + 1;
58 var = mymalloc(len + 1);
59 strncpy(var, s, len);
60 var[len] = 0;
61 s = getenv(var);
62 free(var);
63 if ( s && *s ) {
64 ret = mymalloc(strlen(s) + strlen(st) + 1);
65 strcpy(ret,s);
66 strcat(ret,st);
67 return ret;
68 }
69 }
70 else {
71 ret = mymalloc(strlen(s) + 1);
72 strcpy(ret,s);
73 return ret;
74 }
75
76 return NULL;
77 }
78 #endif
79
80 static long
81 scale(long what, unsigned short unitsPerEm)
82 {
83 return(((what / unitsPerEm) << 20) +
84 (((what % unitsPerEm) << 20) + 500) / unitsPerEm);
85 }
86
87 #define MAXFILENAME 2056
88 FILE *lookup_font_file(const char *fpath, char **p_real_fpath, int *p_is_dfont)
89 {
90 FILE *fp = NULL;
91 char *real_fpath = NULL;
92 char *fpath_exp = NULL;
93 char *fname_ptr;
94
95 if (p_is_dfont)
96 *p_is_dfont = 0;
97
98 #ifdef KPATHSEA
99 fpath_exp = kpse_var_expand(fpath);
100 #else
101 fpath_exp = expandfilename(fpath);
102 #endif
103 if (fpath_exp)
104 {
105 if ((fpath_exp[0] == '.') && (fpath_exp[1] == '/'))
106 {
107 char cwd[MAXFILENAME + 1];
108 if (getcwd(cwd, MAXFILENAME))
109 {
110 strcat(cwd, "/");
111 strncat(cwd, fpath_exp + 2, MAXFILENAME - strlen(cwd));
112 free(fpath_exp);
113 fpath_exp = strdup(cwd);
114 }
115 }
116
117 if ((real_fpath = dpx_find_dfont_file(fpath_exp)) != NULL &&
118 (fp = fopen(real_fpath, "rb")) != NULL)
119 {
120 if (p_is_dfont)
121 *p_is_dfont = 1;
122 }
123 else
124 {
125 if (real_fpath)
126 free(real_fpath);
127 real_fpath = NULL;
128
129 if (((real_fpath = dpx_find_opentype_file(fpath_exp)) == NULL
130 && (real_fpath = dpx_find_truetype_file(fpath_exp)) == NULL)
131 || (fp = fopen(real_fpath, "rb")) == NULL)
132 {
133 if (real_fpath)
134 free(real_fpath);
135 real_fpath = NULL;
136
137 fname_ptr = fpath_exp + strlen(fpath_exp);
138 while ((fname_ptr > fpath_exp) && !IS_DIR_SEP(*(fname_ptr - 1)))
139 --fname_ptr;
140 if (fname_ptr > fpath_exp)
141 {
142 if ((real_fpath = dpx_find_dfont_file(fname_ptr)) != NULL &&
143 (fp = fopen(real_fpath, "rb")) != NULL)
144 {
145 if (p_is_dfont)
146 *p_is_dfont = 1;
147 }
148 else
149 {
150 if (real_fpath)
151 free(real_fpath);
152 real_fpath = NULL;
153
154 if (((real_fpath = dpx_find_opentype_file(fname_ptr)) != NULL
155 || (real_fpath = dpx_find_truetype_file(fname_ptr)) != NULL))
156 fp = fopen(real_fpath, "rb");
157 }
158 }
159 }
160 }
161 }
162
163 if (fpath_exp)
164 free(fpath_exp);
165
166 if (p_real_fpath)
167 *p_real_fpath = real_fpath;
168 else
169 if (real_fpath)
170 free(real_fpath);
171
172 return (fp);
173 }
174
175 int sfntload(fontdesctype *curfnt)
176 {
177 FILE *fp;
178 char *path;
179 sfnt *sfont;
180 unsigned offset = 0;
181 int is_dfont = 0, index = 0;
182 char fontname[256];
183 int glyphCount;
184 int length, n, k, no_of_chars;
185 struct tt_head_table *head;
186 struct tt_hhea_table *hhea;
187 struct tt_maxp_table *maxp;
188 struct tt_longMetrics *metrics;
189 tt_cmap *ttcmap;
190 CMap *cmap;
191 cff_font *cffont = NULL;
192 halfword gid, cid;
193 chardesctype *cd;
194 char *d;
195
196 if (curfnt->resfont->Fontfile == NULL)
197 {
198 char *msg = concatn("! No font file name for ", curfnt->resfont->PSname, NULL);
199 error(msg);
200 free(msg);
201 }
202
203 if (!(fp = lookup_font_file(curfnt->resfont->Fontfile, &path, &is_dfont)))
204 {
205 char *msg = concatn("! Cannot proceed without the font: ", curfnt->resfont->Fontfile, NULL);
206 error(msg);
207 }
208 if ( path ) free(path);
209 d = curfnt->resfont->specialinstructions;
210 if (is_dfont) {
211 if ( d && *d ) {
212 char *tmp = strstr(d,"index=");
213 if ( tmp )
214 sscanf(tmp + 6,"%d",&index);
215 }
216 sfont = dfont_open(fp, index);
217 }
218 else
219 sfont = sfnt_open(fp);
220 if (sfont->type == SFNT_TYPE_TTC) {
221 if ( d && *d ) {
222 char *tmp = strstr(d,"index=");
223 if ( tmp )
224 sscanf(tmp + 6,"%d",&index);
225 }
226 offset = ttc_read_offset(sfont, index);
227 if (offset == 0) {
228 ERROR("Invalid TTC index for font %s",curfnt->resfont->PSname);
229 }
230 curfnt->resfont->otftype = TTC_font;
231 }
232 else if (sfont->type == SFNT_TYPE_DFONT) {
233 offset = sfont->offset;
234 curfnt->resfont->otftype = DFONT_font;
235 }
236 else if (sfont->type == SFNT_TYPE_POSTSCRIPT)
237 curfnt->resfont->otftype = PostScript_font;
238 else
239 curfnt->resfont->otftype = TrueType_font;
240 curfnt->resfont->index = index;
241 if (sfnt_read_table_directory(sfont, offset) < 0) {
242 ERROR("Could not read OpenType/TrueType table directory for font %s",curfnt->resfont->PSname);
243 }
244 length = tt_get_ps_fontname(sfont, fontname, 255);
245 if (length < 1) {
246 length = MIN(strlen(curfnt->resfont->PSname), 255);
247 strncpy(fontname, curfnt->resfont->PSname, length);
248 }
249 fontname[length] = '\0';
250 k = length;
251 for (n = 0; n < length; n++) {
252 if (fontname[n] == 0) {
253 memmove(fontname + n, fontname + n + 1, length - n - 1);
254 k--;
255 }
256 }
257 fontname[k] = '\0';
258 if (strlen(fontname) == 0)
259 ERROR("Can't find valid fontname for \"%s\".", curfnt->resfont->Fontfile);
260 maxp = tt_read_maxp_table(sfont);
261 if ( maxp ) {
262 glyphCount = maxp->numGlyphs;
263 RELEASE(maxp);
264 }
265 else
266 glyphCount = 1000;
267 head = tt_read_head_table(sfont);
268 hhea = tt_read_hhea_table(sfont);
269 sfnt_locate_table(sfont, "hmtx");
270 metrics = tt_read_longMetrics(sfont, glyphCount, hhea->numOfLongHorMetrics,hhea->numOfExSideBearings);
271 if ( metrics == NULL )
272 ERROR("Can't read valid fontname for \"%s\" metric.", curfnt->resfont->Fontfile);
273 if (curfnt->resfont->otftype == PostScript_font) {
274 offset = sfnt_find_table_pos(sfont, "CFF ");
275 cffont = cff_open(sfont->stream, offset, 0);
276 if (!cffont)
277 ERROR("Could not open CFF font for font %s", curfnt->resfont->Fontfile);
278 cff_read_charsets(cffont);
279 }
280 curfnt->resfont->luamap_idx = LuaMap_cache_find(curfnt->resfont->Vectfile ? curfnt->resfont->Vectfile : curfnt->resfont->PSname);
281 if (curfnt->resfont->luamap_idx == -1) {
282 ttcmap = tt_cmap_read(sfont, 3, 10); /* Microsoft UCS4 */
283 if (!ttcmap) {
284 ttcmap = tt_cmap_read(sfont, 3, 1); /* Microsoft UCS2 */
285 if (!ttcmap) {
286 ttcmap = tt_cmap_read(sfont, 0, 3); /* Unicode 2.0 or later */
287 if (!ttcmap) {
288 ttcmap = tt_cmap_read(sfont, 3, 0); /* Windows Symbol encoding */
289 if (!ttcmap) {
290 ttcmap = tt_cmap_read(sfont, 1, 0); /* Mac encoding */
291 if (!ttcmap) {
292 ERROR("Unable to read cmap table for font %s",curfnt->resfont->PSname);
293 }
294 }
295 }
296 }
297 }
298 curfnt->resfont->cmap_fmt = ttcmap->format;
299 if ( ttcmap->format >= 12 )
300 no_of_chars = MAX_CODE;
301 else
302 no_of_chars = MAX_2BYTES_CODE;
303 curfnt->maxchars=no_of_chars;
304 curfnt->iswide = 1;
305 curfnt->codewidth = 2;
306 curfnt->dir = 0;
307 for (n = 1; n < no_of_chars; n++) {
308 gid = tt_cmap_lookup(ttcmap, n);
309 if (cffont && (cffont->flag & FONTTYPE_CIDFONT) && (gid > 0)) {
310 cid = cff_charsets_lookup_inverse(cffont, gid);
311 }
312 else
313 cid = gid;
314 if (gid > 0) {
315 int li = scalewidth(scale(metrics[gid].advance, head->unitsPerEm), curfnt->scaledsize);
316 cd = add_chardesc(curfnt, n);
317 cd->TFMwidth = li;
318 cd->pixelwidth = ((integer)(conv*li + 0.5));
319 cd->cid = cid;
320 cd->flags = (curfnt->resfont ? EXISTS : 0);
321 cd->flags2 = EXISTS;
322 }
323 }
324 tt_cmap_release(ttcmap);
325 }
326 else {
327 luamaptype *map, *current, *tmp;
328 map = LuaMap_cache_get(curfnt->resfont->luamap_idx);
329 curfnt->maxchars = MAX_CODE;
330 curfnt->iswide = 1;
331 curfnt->codewidth = 2;
332 curfnt->dir = 0;
333 HASH_ITER(hh, map, current, tmp) {
334 gid = current->gid;
335 // if (cffont && (cffont->flag & FONTTYPE_CIDFONT) && (gid > 0)) {
336 // cid = cff_charsets_lookup_inverse(cffont, gid);
337 // }
338 // else
339 cid = gid;
340 if (gid > 0) {
341 int li = scalewidth(scale(metrics[gid].advance, head->unitsPerEm), curfnt->scaledsize);
342 cd = add_chardesc(curfnt, current->charcode);
343 cd->TFMwidth = li;
344 cd->pixelwidth = ((integer)(conv*li + 0.5));
345 cd->cid = cid;
346 cd->flags = (curfnt->resfont ? EXISTS : 0);
347 cd->flags2 = EXISTS;
348 }
349 }
350 }
351 curfnt->loaded = 1;
352 free(metrics);
353 free(hhea);
354 free(head);
355 if (cffont)
356 cff_close(cffont);
357 sfnt_close(sfont);
358 fclose(fp);
359 return glyphCount;
360 }