Debian Codegraph @

texk/dvipsk/download.c

raw
1 /*
2 * Code to download a font definition at the beginning of a section.
3 * hacked by SPQR 8.95 to allow for non-partial downloading
4 *
5 */
6 #include "dvips.h" /* The copyright notice in that file is included too! */
7 #ifndef DOWNLOAD_USING_PDFTEX
8 #define DOWNLOAD_USING_PDFTEX
9 #endif
10 #ifndef DOWNLOAD_USING_PDFTEX
11 #include "t1part.h"
12 #endif
13 #define DVIPS
14 /*
15 * The external declarations:
16 */
17 #include "protos.h"
18
19 static unsigned char dummyend[8] = { 252 };
20
21 /*
22 * We have a routine that downloads an individual character.
23 */
24 static int lastccout;
25 quarterword *unpack_bb(chardesctype *c, integer *cwidth, integer *cheight,
26 integer *xoff, integer *yoff) {
27 quarterword *p = c->packptr;
28 halfword cmd = *p++;
29 if (cmd & 4) {
30 if ((cmd & 7) == 7) {
31 *cwidth = getlong(p);
32 *cheight = getlong(p + 4);
33 *xoff = getlong(p + 8);
34 *yoff = getlong(p + 12);
35 p += 16;
36 } else {
37 *cwidth = p[0] * 256 + p[1];
38 *cheight = p[2] * 256 + p[3];
39 *xoff = p[4] * 256 + p[5];
40 if (*xoff > 32767)
41 *xoff -= 65536 ;
42 *yoff = p[6] * 256 + p[7];
43 if (*xoff > 32767)
44 *xoff -= 65536 ;
45 p += 8;
46 }
47 } else {
48 *cwidth = *p++;
49 *cheight = *p++;
50 *xoff = *p++;
51 *yoff = *p++;
52 if (*xoff > 127)
53 *xoff -= 256;
54 if (*yoff > 127)
55 *yoff -= 256;
56 }
57 return p ;
58 }
59 static void
60 downchar(chardesctype *c, shalfword cc)
61 {
62 register long i, j;
63 integer cheight, cwidth;
64 register long k;
65 register quarterword *p;
66 register halfword cmd;
67 integer xoff, yoff;
68 halfword wwidth = 0;
69 register long len;
70 int smallchar;
71
72 cmd = *(c->packptr) ;
73 p = unpack_bb(c, &cwidth, &cheight, &xoff, &yoff) ;
74 if (c->flags & BIGCHAR)
75 smallchar = 0;
76 else
77 smallchar = 5;
78 if (compressed) {
79 len = getlong(p);
80 p += 4;
81 } else {
82 wwidth = (cwidth + 15) / 16;
83 i = 2 * cheight * (long)wwidth;
84 if (i <= 0)
85 i = 2;
86 i += smallchar;
87 if (mbytesleft < i) {
88 if (mbytesleft >= RASTERCHUNK)
89 (void) free((char *) mraster);
90 if (RASTERCHUNK > i) {
91 mraster = (quarterword *)mymalloc((integer)RASTERCHUNK);
92 mbytesleft = RASTERCHUNK;
93 } else {
94 k = i + i / 4;
95 mraster = (quarterword *)mymalloc((integer)k);
96 mbytesleft = k;
97 }
98 }
99 k = i;
100 while (k > 0)
101 mraster[--k] = 0;
102 unpack(p, (halfword *)mraster, cwidth, cheight, cmd);
103 p = mraster;
104 len = i - smallchar;
105 }
106 if (cheight == 0 || cwidth == 0 || len == 0) {
107 cwidth = 1;
108 cheight = 1;
109 wwidth = 1;
110 len = 2;
111 if (compressed)
112 p = dummyend; /* CMD(END); see repack.c */
113 else
114 mraster[0] = 0;
115 }
116 if (smallchar) {
117 p[len] = cwidth;
118 p[len + 1] = cheight;
119 p[len + 2] = xoff + 128;
120 p[len + 3] = yoff + 128;
121 p[len + 4] = c->pixelwidth;
122 } else
123 /*
124 * Now we actually send out the data.
125 */
126 specialout('[');
127 if (compressed) {
128 specialout('<');
129 mhexout(p, len + smallchar);
130 specialout('>');
131 } else {
132 i = (cwidth + 7) / 8;
133 if (i * cheight > 65520) {
134 long bc = 0;
135 specialout('<');
136 for (j=0; j<cheight; j++) {
137 if (bc + i > 65520) {
138 specialout('>');
139 specialout('<');
140 bc = 0;
141 }
142 mhexout(p, i);
143 bc += i;
144 p += 2*wwidth;
145 }
146 specialout('>');
147 } else {
148 specialout('<');
149 if (2 * wwidth == i)
150 mhexout(p, ((long)cheight) * i + smallchar);
151 else {
152 for (j=0; j<cheight; j++) {
153 mhexout(p, i);
154 p += 2*wwidth;
155 }
156 if (smallchar)
157 mhexout(p, (long)smallchar);
158 }
159 specialout('>');
160 }
161 }
162 if (smallchar == 0) {
163 numout((integer)cwidth);
164 numout((integer)cheight);
165 numout((integer)xoff + 128); /* not all these casts needed. */
166 numout((integer)yoff + 128);
167 numout((integer)(c->pixelwidth));
168 }
169 if (lastccout + 1 == cc) {
170 cmdout("I");
171 } else {
172 numout((integer)cc);
173 cmdout("D");
174 }
175 lastccout = cc;
176 }
177 /*
178 * Output the literal name of the font change command with PostScript index n
179 */
180 static char goodnames[] =
181 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
182 void
183 makepsname(register char *s, register int n)
184 {
185 n--;
186 *s++ = 'F' + n / (sizeof(goodnames)-1);
187 *s++ = goodnames[n % (sizeof(goodnames)-1)];
188 *s++ = 0;
189 }
190 void
191 lfontout(int n)
192 {
193 char buf[10];
194 char *b = buf;
195 *b++ = '/';
196 makepsname(b, n);
197 cmdout(buf);
198 }
199
200 /*
201 * And the download procedure.
202 */
203 void
204 download(charusetype *p, int psfont)
205 {
206 double scale;
207 int seq;
208 register int b, i;
209 register halfword bit;
210 register chardesctype *c;
211 int cc, maxcc = -1, numcc;
212 double fontscale;
213 char name[10];
214 lastccout = -5;
215 name[0] = '/';
216 makepsname(name + 1, psfont);
217 curfnt = p->fd;
218 curfnt->psname = psfont;
219 if (curfnt->resfont) {
220 struct resfont *rf = curfnt->resfont;
221 int non_empty=0;
222 for (b=0; b<16; b++)
223 if(p->bitmap[b] !=0)
224 non_empty =1;
225 if(non_empty==0 && curfnt->iswide == 0 && curfnt->codewidth==1)
226 return;
227 cmdout(name);
228 /* following code re-arranged - Rob Hutchings 1992Apr02 */
229 c = curfnt->chardesc + 255;
230 cc = 255;
231 numcc = 0;
232 i = 0;
233 for (b=15; b>=0; b--) {
234 for (bit=1; bit; bit<<=1) {
235 if (p->bitmap[b] & bit) {
236 if (i > 0) {
237 numout((integer)i);
238 specialout('[');
239 i = 0;
240 }
241 numout((integer)c->pixelwidth);
242 c->flags |= EXISTS;
243 numcc++;
244 } else {
245 i++;
246 c->flags &= ~EXISTS;
247 }
248 c--;
249 cc--;
250 }
251 }
252 if (i > 0) {
253 numout((integer)i);
254 specialout('[');
255 }
256 specialout ('{');
257 if (rf->specialinstructions)
258 cmdout(rf->specialinstructions);
259 specialout ('}');
260 numout((integer)numcc);
261 /*
262 * This code has been bogus for a long time. The fix is
263 * straightforward. The input, curfnt->scaledsize, is the
264 * desired size of the font in dvi units. The output is the
265 * fontscale, which is the height of the font in output units
266 * which are simply pixels. Thus, all we need to do is multiply
267 * by the default generic conv, which properly takes into
268 * account magnification, num/den, output resolution, and so on.
269 * Sorry this bug has been in here so long. -tgr
270 */
271 fontscale = curfnt->scaledsize * conv;
272 sprintf(nextstring, "%g", fontscale);
273 cmdout(nextstring);
274 strcpy(nextstring, "/");
275 strcat(nextstring, rf->PSname);
276 cmdout(nextstring);
277 /* end of changed section - Rob */
278 cmdout("rf");
279 rf->sent = 1;
280 return;
281 }
282 /*
283 * Here we calculate the largest character actually used, and
284 * send it as a parameter to df.
285 */
286 cc = 0;
287 numcc = 0;
288 for (b=0; b<16; b++) {
289 for (bit=32768; bit!=0; bit>>=1) {
290 if (p->bitmap[b] & bit) {
291 maxcc = cc;
292 numcc++;
293 }
294 cc++;
295 }
296 }
297 if (numcc <= 0)
298 return;
299 fontscale = ((double)(curfnt->scaledsize)) / 65536.0;
300 fontscale *= (mag/1000.0);
301 newline();
302 fprintf(bitfile, "%%DVIPSBitmapFont: %s %s %g %d\n", name+1, curfnt->name,
303 fontscale, numcc);
304 scale = fontscale * DPI / 72.0 ;
305 seq = -1 ;
306 if (encodetype3)
307 seq = downloadbmencoding(curfnt->name, scale, curfnt) ;
308 cmdout(name);
309 numout((integer)numcc);
310 numout((integer)maxcc + 1);
311 /*
312 * If we need to scale the font, we say so by using dfs
313 * instead of df, and we give it a scale factor. We also
314 * scale the character widths; this is ugly, but scaling
315 * fonts is ugly, and this is the best we can probably do.
316 */
317 if (curfnt->dpi != curfnt->loadeddpi) {
318 numout((integer)curfnt->dpi);
319 numout((integer)curfnt->loadeddpi);
320 if (curfnt->alreadyscaled == 0) {
321 for (b=0, c=curfnt->chardesc; b<256; b++, c++)
322 c->pixelwidth = (c->pixelwidth *
323 (long)curfnt->dpi * 2 + curfnt->loadeddpi) / (2 * curfnt->loadeddpi);
324 curfnt->alreadyscaled = 1;
325 }
326 cmdout("dfs");
327 } else
328 cmdout("df");
329 c = curfnt->chardesc;
330 cc = 0;
331 for (b=0; b<16; b++) {
332 for (bit=32768; bit; bit>>=1) {
333 if (p->bitmap[b] & bit) {
334 downchar(c, (shalfword)cc);
335 c->flags |= EXISTS;
336 } else
337 c->flags &= ~EXISTS;
338 c++;
339 cc++;
340 }
341 }
342 cmdout("E");
343 newline() ;
344 if (seq >= 0)
345 finishbitmapencoding(name, scale) ;
346 fprintf(bitfile, "%%EndDVIPSBitmapFont\n");
347 }
348
349 #ifdef DOWNLOAD_USING_PDFTEX
350 /*
351 * Magic code to deal with PostScript font partial downloading.
352 * We track the encodings we've seen so far and keep them in these
353 * structures. We rely on writet1 to load the encodings for us.
354 */
355 static struct seenEncodings {
356 struct seenEncodings *next;
357 const char *name;
358 char **glyphs;
359 } *seenEncodings;
360
361 /*
362 * Load a new encoding and return the array of glyphs as a vector.
363 * Linear search.
364 */
365 static char **getEncoding(char *encoding) {
366 struct seenEncodings *p = seenEncodings;
367 while (p != 0)
368 if (strcmp(encoding, p->name) == 0)
369 break;
370 else
371 p = p->next;
372 if (p == 0) {
373 p = (struct seenEncodings *)mymalloc(sizeof(struct seenEncodings));
374 p->next = seenEncodings;
375 seenEncodings = p;
376 p->name = xstrdup(encoding);
377 p->glyphs = load_enc_file(encoding);
378 }
379 return p->glyphs;
380 }
381 /*
382 * When partially downloading a type 1 font, sometimes the font uses
383 * the built-in encoding (which we don't know at this point) so we want
384 * to subset it by character code. Sometimes the font uses a different
385 * encoding, so we want to subset it by glyph. These routines manage
386 * accumulating all the glyph names for a particular font from all
387 * the various uses into a single, slash-delimited string for writet1().
388 *
389 * If no glyphs have been added, extraGlyphs is null. In all cases,
390 * the memory allocated for this is in extraGlyphSpace.
391 */
392 static char *extraGlyphs = 0;
393 static char *extraGlyphSpace = 0;
394 static int extraGlyphSize = 0;
395 static int glyphSizeUsed = 0;
396 /*
397 * We want to make sure we pass in null or "/" but never "".
398 */
399 static void clearExtraGlyphList(void) {
400 glyphSizeUsed = 0;
401 extraGlyphs = 0;
402 }
403 /*
404 * Add the glyph name; make sure it hasn't been added already.
405 * We do this by adding it, so we can get the // around it,
406 * and *then* see if it's already there and if it is un-add it.
407 */
408 static void addGlyph(const char *glyphName) {
409 int len = strlen(glyphName);
410 char *startOfAdd = 0;
411 if (len + glyphSizeUsed + 3 > extraGlyphSize) {
412 extraGlyphSize = 2 * (extraGlyphSize + len + 100);
413 extraGlyphSpace = (char *) xrealloc(extraGlyphSpace, extraGlyphSize);
414 }
415 extraGlyphs = extraGlyphSpace;
416 if (glyphSizeUsed == 0) {
417 startOfAdd = extraGlyphs + glyphSizeUsed;
418 extraGlyphs[glyphSizeUsed++] = '/'; /* leading / */
419 } else {
420 startOfAdd = extraGlyphs + glyphSizeUsed - 1;
421 }
422 strcpy(extraGlyphs + glyphSizeUsed, glyphName);
423 glyphSizeUsed += len;
424 extraGlyphs[glyphSizeUsed++] = '/'; /* trailing / */
425 extraGlyphs[glyphSizeUsed] = 0;
426 if (strstr(extraGlyphs, startOfAdd) != startOfAdd) { /* already there! */
427 glyphSizeUsed = startOfAdd - extraGlyphs + 1; /* kill the second copy */
428 extraGlyphs[glyphSizeUsed] = 0;
429 }
430 }
431 #endif
432
433 /*
434 * Download a PostScript font, using partial font downloading if
435 * necessary.
436 */
437 static void
438 downpsfont(charusetype *p, charusetype *all)
439 {
440 static unsigned char grid[256];
441 int GridCount;
442 register int b;
443 register halfword bit;
444 register chardesctype *c;
445 struct resfont *rf;
446 int cc;
447 int j;
448
449 curfnt = p->fd;
450 rf = curfnt->resfont;
451 if (rf == 0 || rf->Fontfile == NULL)
452 return;
453 for (; all->fd; all++)
454 if (all->fd->resfont &&
455 strcmp(rf->PSname, all->fd->resfont->PSname) == 0)
456 break;
457 if (all != p)
458 return;
459 if (rf->sent == 2) /* sent as header, from a PS file */
460 return;
461 for (j=0; downloadedpsnames[j] && j < unused_top_of_psnames; j++) {
462 if (strcmp (downloadedpsnames[j], rf->PSname) == 0)
463 return;
464 }
465 if (all->fd == 0)
466 error("! internal error in downpsfont");
467 if (!partialdownload) {
468 infont = all->fd->resfont->PSname;
469 copyfile(all->fd->resfont->Fontfile);
470 infont = 0;
471 return;
472 }
473 for (cc=0; cc<256; cc++)
474 grid[cc] = 0;
475 #ifdef DOWNLOAD_USING_PDFTEX
476 clearExtraGlyphList();
477 #endif
478 for (; all->fd; all++) {
479 if (all->fd->resfont == 0 ||
480 strcmp(rf->PSname, all->fd->resfont->PSname))
481 continue;
482 curfnt = all->fd;
483 #ifdef DOWNLOAD_USING_PDFTEX
484 if (curfnt->resfont->Vectfile) {
485 char **glyphs = getEncoding(curfnt->resfont->Vectfile);
486 c = curfnt->chardesc + 255;
487 cc = 255;
488 for (b=15; b>=0; b--) {
489 for (bit=1; bit; bit<<=1) {
490 if (all->bitmap[b] & bit) {
491 addGlyph(glyphs[cc]);
492 }
493 c--;
494 cc--;
495 }
496 }
497 } else {
498 #endif
499 c = curfnt->chardesc + 255;
500 cc = 255;
501 for (b=15; b>=0; b--) {
502 for (bit=1; bit; bit<<=1) {
503 if (all->bitmap[b] & bit) {
504 grid[cc]=1;
505 }
506 c--;
507 cc--;
508 }
509 }
510 #ifdef DOWNLOAD_USING_PDFTEX
511 }
512 #endif
513 }
514
515 for (GridCount=0,cc=0; cc<256; cc++) {
516 /* tmpgrid[cc]=grid[cc]; */
517 if(grid[cc] ==1) {
518 GridCount++;
519 }
520 }
521 if(GridCount!=0
522 #ifdef DOWNLOAD_USING_PDFTEX
523 || extraGlyphs
524 #endif
525 ) {
526 newline();
527 if (! disablecomments)
528 fprintf(bitfile, "%%%%BeginFont: %s\n", rf->PSname);
529 #ifdef DOWNLOAD_USING_PDFTEX
530 if (!t1_subset_2(rf->Fontfile, grid, extraGlyphs))
531 #else
532 if(FontPart(bitfile, rf->Fontfile, rf->Vectfile) < 0)
533 #endif
534 exit(1);
535 if (!quiet) {
536 if (strlen(realnameoffile) + prettycolumn > STDOUTSIZE) {
537 fprintf(stderr, "\n");
538 prettycolumn = 0;
539 }
540 fprintf_str(stderr, "<%s>", realnameoffile);
541 prettycolumn += strlen(realnameoffile) + 2;
542 }
543 if (! disablecomments)
544 fprintf(bitfile, "%%%%EndFont \n");
545 }
546 }
547
548 void
549 dopsfont(sectiontype *fs)
550 {
551 charusetype *cu;
552
553 cu = (charusetype *) (fs + 1);
554 #ifdef DOWNLOAD_USING_PDFTEX
555 while (cu->fd) {
556 if (cu->psfused)
557 cu->fd->psflag = EXISTS;
558 downpsfont(cu++, (charusetype *)(fs + 1));
559 }
560 #else
561 line = getmem(BUFSIZ);
562 tmpline=line;
563 while (cu->fd) {
564 if (cu->psfused)
565 cu->fd->psflag = EXISTS;
566 downpsfont(cu++, (charusetype *)(fs + 1));
567 }
568 loadbase = ~FLG_LOAD_BASE;
569 FirstCharB=UnDefineChars(FirstCharB);
570 free(tmpline);
571 #endif
572 }