Debian Codegraph @

texk/dvipsk/output.c

raw
1 /*
2 * These routines do most of the communicating with the printer.
3 *
4 * LINELENGTH tells the maximum line length to send out. It's been
5 * reduced to 72 because sometimes PostScript files are included in
6 * mail messages and many mailers mutilate longer lines.
7 */
8 #define LINELENGTH (72)
9 #include "dvips.h" /* The copyright notice in that file is included too! */
10 #include <ctype.h>
11 #include <stdlib.h>
12
13 #ifdef OS2
14 #ifdef _MSC_VER
15 #define popen(pcmd, pmode) _popen(pcmd, pmode)
16 #define pclose(pstream) _pclose(pstream)
17 #endif
18 #endif
19 #if defined(WIN32) && defined(KPATHSEA)
20 #undef fopen
21 #undef popen
22 #undef pclose
23 extern FILE *generic_fsyscp_fopen(const char *name, const char *mode);
24 #define fopen(file, fmode) generic_fsyscp_fopen(file, fmode)
25 #define popen(pcmd, pmode) fsyscp_popen(pcmd, pmode)
26 #define pclose(pstream) _pclose(pstream)
27 #endif
28
29 #ifdef __DJGPP__
30 #include <unistd.h> /* for `isatty' */
31 #endif
32
33 /* UCS -> UTF-8 */
34 #define UCStoUTF8B1(x) (0xc0 + (((x) >> 6) & 0x1f))
35 #define UCStoUTF8B2(x) (0x80 + (((x) ) & 0x3f))
36
37 #define UCStoUTF8C1(x) (0xe0 + (((x) >> 12) & 0x0f))
38 #define UCStoUTF8C2(x) (0x80 + (((x) >> 6) & 0x3f))
39 #define UCStoUTF8C3(x) (0x80 + (((x) ) & 0x3f))
40
41 #define UCStoUTF8D1(x) (0xf0 + (((x) >> 18) & 0x07))
42 #define UCStoUTF8D2(x) (0x80 + (((x) >> 12) & 0x3f))
43 #define UCStoUTF8D3(x) (0x80 + (((x) >> 6) & 0x3f))
44 #define UCStoUTF8D4(x) (0x80 + (((x) ) & 0x3f))
45
46 /* UTF-32 over U+FFFF -> UTF-16 surrogate pair */
47 #define UTF32toUTF16HS(x) (0xd800 + (((x-0x10000) >> 10) & 0x3ff))
48 #define UTF32toUTF16LS(x) (0xdc00 + ( x & 0x3ff))
49
50 /*
51 * The external declarations:
52 */
53 #include "protos.h"
54
55 char preamblecomment[256]; /* usually "TeX output ..." */
56 integer rdir = 0, fdir = 0;
57 /*
58 * We need a few statics to take care of things.
59 */
60 static void chrcmd(char c);
61 static void tell_needed_fonts(void);
62 static void print_composefont(void);
63 static void setdir(int d);
64 static int JIStoSJIS(int c);
65
66 static Boolean any_dir = 0; /* did we output any direction commands? */
67 static Boolean jflag = 0;
68 static integer rhh, rvv;
69 static int instring;
70 static Boolean lastspecial = 1;
71 static shalfword d;
72 static Boolean popened = 0;
73 static int lastfont;
74 static char strbuffer[LINELENGTH + 20], *strbp = strbuffer;
75 static struct papsiz *finpapsiz;
76 static struct papsiz defpapsiz = {
77 0, 40258437L, 52099154L, "letter", ""
78 };
79 #ifdef CREATIONDATE
80 #if (!defined(VMS) && !defined(MSDOS) && !(defined(OS2) && defined(_MSC_VER)) && !defined(ATARIST))
81 /* VAXC/MSDOS don't like/need this !! */
82 #include <sys/types.h>
83 #ifndef WIN32
84 #include <sys/time.h> /* time(), at least on BSD Unix */
85 #endif
86 #endif
87 #include <time.h> /* asctime() and localtime(), at least on BSD Unix */
88 static time_t jobtime;
89
90 #if defined(_MSC_VER)
91 #define strtoll _strtoi64
92 #endif
93
94 #define INVALID_EPOCH_VALUE ((time_t) -1)
95
96 static time_t
97 get_unique_time_if_given(void)
98 {
99 const char *source_date_epoch;
100 int64_t epoch;
101 char *endptr;
102 time_t ret = INVALID_EPOCH_VALUE;
103
104 source_date_epoch = getenv("SOURCE_DATE_EPOCH");
105 if (source_date_epoch) {
106 errno = 0;
107 epoch = strtoll(source_date_epoch, &endptr, 10);
108 if (!(epoch < 0 || *endptr != '\0' || errno != 0)) {
109 ret = (time_t) epoch;
110 #if defined(_MSC_VER)
111 if (ret > 32535291599ULL)
112 ret = 32535291599ULL;
113 #endif
114 }
115 }
116 return ret;
117 }
118 #endif /* CREATIONDATE */
119
120 /*
121 * This routine copies a file down the pipe. Search path uses the
122 * header path.
123 *
124 * We add code to handle the case of MS-DOS font files.
125 *
126 * Format: 80 {01,02} four byte length in littleendian order data
127 * repeated possibly multiple times.
128 */
129 static const char *hxdata = "0123456789ABCDEF";
130 static int infigure;
131 static char possibleDSCLine[81],
132 *dscLinePointer = possibleDSCLine, *dscLineEnd = possibleDSCLine + 80;
133 void
134 copyfile_general(const char *s, struct header_list *cur_header)
135 {
136 FILE *f = NULL;
137 int c, prevc = '\n';
138 long len;
139 /* begin DOS EPS code */
140 int doseps = 0;
141 unsigned long dosepsbegin, dosepsend = 0;
142 int removingBytes = 0;
143 const char *scanForEnd = 0;
144 int scanningFont = 0;
145
146 /* end DOS EPS code */
147 #if defined(VMCMS) || defined (MVSXA)
148 register char *lastdirsep;
149 register char *trunc_s;
150 trunc_s = s;
151 #endif
152 dscLinePointer = possibleDSCLine;
153
154 switch (infigure) {
155 case 1:
156 /*
157 * Look in headerpath too, just in case. This allows common header
158 * or figure files to be installed in the .../ps directory.
159 */
160 f = search(figpath, s, READBIN);
161 #if defined(WIN32)
162 if (f == 0 && file_system_codepage != win32_codepage) {
163 int tmpcp = file_system_codepage;
164 file_system_codepage = win32_codepage;
165 f = search(figpath, s, READBIN);
166 file_system_codepage = tmpcp;
167 }
168 #endif
169 if (f == 0)
170 f = search(headerpath, s, READBIN);
171 #if defined(VMCMS) || defined (MVSXA)
172 lastdirsep = strrchr(s, '/');
173 if ( NULL != lastdirsep ) trunc_s = lastdirsep + 1;
174 #ifdef VMCMS
175 sprintf(errbuf,
176 "Couldn't find figure file %s with CMS name %s; continuing.", s, trunc_s);
177 if (secure == 2) {
178 strcat(errbuf, "\nNote that an absolute path or a relative path with .. are denied in -R2 mode.");
179 }
180 #else /* ! VMCMS */
181 sprintf(errbuf,
182 "Couldn't find figure file %s with MVS name %s; continuing.", s, trunc_s);
183 if (secure == 2) {
184 strcat(errbuf, "\nNote that an absolute path or a relative path with .. are denied in -R2 mode.");
185 }
186 #endif /* ! VMCMS */
187 #else /* ! (VMCMS || MVSXA) */
188 sprintf(errbuf, "Could not find figure file %.500s; continuing.", s);
189 if (secure == 2) {
190 strcat(errbuf, "\nNote that an absolute path or a relative path with .. are denied in -R2 mode.");
191 }
192 #endif /* !(VMCMS || MVSXA) */
193 break;
194 #ifndef VMCMS
195 #ifndef MVSXA
196 #ifndef VMS
197 #if !defined(MSDOS) || defined(__DJGPP__)
198 #ifndef ATARIST
199 #ifndef __THINK__
200 case 2:
201 #ifdef SECURE
202 sprintf(errbuf, "<%.500s>: Tick filename execution disabled", s);
203 #else
204 #ifdef OS2
205 if (_osmode == OS2_MODE) {
206 #endif
207 if (secure == 0) {
208 sprintf(errbuf, "Execution of <%.500s> failed ", s);
209 f = popen(s, "r");
210 if (f != 0)
211 SET_BINARY(fileno(f));
212 }
213 else {
214 sprintf(errbuf,"Secure mode is %d so execute <%.500s> will not run",
215 secure, s);
216 }
217 #ifdef OS2
218 }
219 #endif
220 #endif
221 break;
222 #endif
223 #endif
224 #endif
225 #endif
226 #endif
227 #endif
228 default:
229 f = search(headerpath, s, READBIN);
230 if(cur_header && (cur_header->precode || cur_header->postcode)) {
231 if(f==NULL)
232 f = search(figpath, s, READBIN);
233 }
234 sprintf(errbuf, "! Could not find header file %.500s.", s);
235 if (secure == 2) {
236 strcat(errbuf, "\nNote that an absolute path or a relative path with .. are denied in -R2 mode.");
237 }
238 break;
239 }
240 if (f==NULL) {
241 found_problems = 1; /* continue, but eventually exit unsuccessfully */
242 error(errbuf);
243 } else {
244 if (! quiet) {
245 #if defined(VMCMS) || defined (MVSXA)
246 if (strlen(s) + prettycolumn > STDOUTSIZE) {
247 #else
248 if (strlen(realnameoffile) + prettycolumn > STDOUTSIZE) {
249 #endif
250 fprintf(stderr, "\n");
251 prettycolumn = 0;
252 }
253 #if defined(VMCMS) || defined (MVSXA)
254 fprintf(stderr, "<%s>", trunc_s);
255 #else
256 fprintf_str(stderr, "<%s>", realnameoffile);
257 #endif
258 fflush(stderr);
259 #if defined(VMCMS) || defined (MVSXA)
260 prettycolumn += 2 + strlen(s);
261 #else
262 prettycolumn += 2 + strlen(realnameoffile);
263 #endif
264 }
265 if (linepos != 0)
266 putc('\n', bitfile);
267 /*
268 * Suggested by Andrew Trevorrow; don't ever BeginFont a file ending in .enc
269 */
270 if (infont && strstr(s,".enc"))
271 infont = 0;
272 if (! disablecomments) {
273 if (infigure)
274 fprintf(bitfile, "%%%%BeginDocument: %s\n", s);
275 else if (infont)
276 fprintf(bitfile, "%%%%BeginFont: %s\n", infont);
277 #ifdef HPS
278 else if (noprocset) {}
279 #endif
280 else
281 fprintf(bitfile, "%%%%BeginProcSet: %s 0 0\n", s);
282 }
283 if (cur_header && cur_header->precode) {
284 fprintf(bitfile, "%s\n", cur_header->precode);
285 free(cur_header->precode);
286 }
287 c = getc(f);
288 if (c == 0x80) {
289 while (1) {
290 c = getc(f);
291 switch(c) {
292 case 1:
293 case 2:
294 len = getc(f);
295 len += getc(f) * 256L;
296 len += getc(f) * 65536L;
297 len += getc(f) * 256L * 65536;
298 if (c == 1) {
299 while (len > 0) {
300 c = getc(f);
301 if (c == EOF) {
302 error("premature EOF in MS-DOS font file");
303 len = 0;
304 } else {
305 if (c == '\r') { /* Mac- or DOS-style text file */
306 putc('\n', bitfile);
307 if ((c = getc(f)) == '\n') /* DOS-style text */
308 len--; /* consume, but don't generate NL */
309 else
310 ungetc(c, f);
311 }
312 else
313 putc(c, bitfile);
314 len--;
315 }
316 }
317 } else {
318 putc('\n', bitfile);
319 prevc = 0;
320 while (len > 0) {
321 c = getc(f);
322 if (c == EOF) {
323 error("premature EOF in MS-DOS font file");
324 len = 0;
325 } else {
326 putc(hxdata[c >> 4], bitfile);
327 putc(hxdata[c & 15], bitfile);
328 len--;
329 prevc += 2;
330 if (prevc >= 76) {
331 putc('\n', bitfile);
332 prevc = 0;
333 }
334 }
335 }
336 }
337 break;
338 case 3:
339 goto msdosdone;
340 default:
341 error("saw type other than 1, 2, or 3 in MS-DOS font file");
342 break;
343 }
344 c = getc(f);
345 if (c == EOF)
346 break;
347 if (c != 0x80) {
348 error("saw non-MSDOS header in MSDOS font file");
349 break;
350 }
351 }
352 msdosdone:
353 prevc = 0;
354 } else {
355 /* begin DOS EPS code */
356 if (c == 'E'+0x80) {
357 if ((getc(f)=='P'+0x80) && (getc(f)=='S'+0x80)
358 && (getc(f)=='F'+0x80)) {
359 doseps = 1;
360 dosepsbegin = getc(f);
361 dosepsbegin += getc(f) * 256L;
362 dosepsbegin += getc(f) * 65536L;
363 dosepsbegin += getc(f) * 256L * 65536;
364 dosepsend = getc(f);
365 dosepsend += getc(f) * 256L;
366 dosepsend += getc(f) * 65536L;
367 dosepsend += getc(f) * 256L * 65536;
368 fseek(f, dosepsbegin, 0);
369 c = getc(f);
370 dosepsend--;
371 }
372 else {
373 rewind(f);
374 c = getc(f);
375 }
376 }
377 /* end DOS EPS code */
378 if (c != EOF) {
379 while (1) {
380 if (c == '\n') { /* end or beginning of line; check DSC */
381 *dscLinePointer = 0; /* make sure we terminate!
382 * might be a new empty line! */
383 if (strncmp(possibleDSCLine, "%%BeginBinary:", 14) == 0 ||
384 strncmp(possibleDSCLine, "%%BeginData:", 12) == 0 ||
385 strncmp(possibleDSCLine, "%%BeginFont:", 12) == 0) {
386 integer size = 0;
387 char *p = possibleDSCLine;
388 *dscLinePointer = 0;
389 *dscLineEnd = 0;
390 if (scanForEnd == 0 && removecomments) {
391 fputs(possibleDSCLine, bitfile);
392 putc('\n', bitfile);
393 }
394
395 if (strncmp(possibleDSCLine, "%%BeginFont:", 12) == 0) {
396
397 /* Theoretically, we could wait until we see
398 the "currentfile eexec" and possibly even
399 check that the following data really looks
400 like binary before we begin verbatim
401 copying, but that would complicate the code
402 beyond our present needs. If we were going
403 to do that much work, this entire chunk of
404 code should probably be
405 rewritten. [[email protected], 2007/08/20] */
406
407 scanForEnd = "%%EndFont";
408 scanningFont = 1;
409 } else {
410 scanningFont = 0;
411
412 scanForEnd = 0;
413 while (*p != ':')
414 p++;
415 p++;
416 while (*p && *p <= ' ')
417 p++;
418 if ('0' > *p || *p > '9') {
419 /*
420 * No byte count! We need to scan for end binary
421 * or end data, and hope we get it right. Really
422 * the file is malformed.
423 */
424 scanForEnd = "Yes";
425 }
426 while ('0' <= *p && *p <= '9') {
427 size = size * 10 + *p - '0';
428 p++;
429 }
430 while (*p && *p <= ' ')
431 p++;
432 if (*p == 'h' || *p == 'H')
433 /*
434 * Illustrator 8 and 9 have bogus byte counts
435 * for hex data. But if it is hex, we assume
436 * that it is safe to use ASCII scanning, so
437 * we do so.
438 */
439 scanForEnd = "Yes";
440 while (*p > ' ') /* ignore Hex/Binary/ASCII */
441 p++;
442 while (*p && *p <= ' ')
443 p++;
444 putc(c, bitfile);
445 if (c == '\r') { /* maybe we have a DOS-style text file */
446 c = getc(f);
447 if (c == '\n') {
448 putc(c, bitfile);
449 dosepsend--;
450 } else
451 ungetc(c, f);
452 }
453 if (scanForEnd != 0) {
454 if (strncmp(possibleDSCLine, "%%BeginBinary", 13) == 0)
455 scanForEnd = "%%EndBinary";
456 else
457 scanForEnd = "%%EndData";
458 }
459 }
460 if (scanForEnd == 0) {
461 if (strncmp(p, "lines", 5) != 0 &&
462 strncmp(p, "Lines", 5) != 0) {
463 for (; size>0; size--) {
464 c = getc(f);
465 dosepsend--;
466 if (c == EOF)
467 error(
468 "! premature end of file in binary section");
469 putc(c, bitfile);
470 }
471 } else {
472 /*
473 * Count both newlines and returns, and when either
474 * goes over the count, we are done.
475 */
476 int newlines=0, returns=0;
477 while (newlines < size && returns < size) {
478 c = getc(f);
479 dosepsend--;
480 if (c == EOF)
481 error(
482 "! premature end of file in binary section");
483 putc(c, bitfile);
484 if (c == '\n')
485 newlines++;
486 else if (c == '\r')
487 returns++;
488 }
489 /*
490 * If we've seen precisely one too few newlines,
491 * and the next char is a newline, include it too.
492 */
493 if (returns == newlines + 1) {
494 if ((c = getc(f)) == '\n') {
495 putc(c, bitfile);
496 dosepsend--;
497 } else {
498 ungetc(c, f);
499 }
500 }
501 }
502 c = getc(f);
503 dosepsend--;
504 if (c == '\n' || c == '\r') {
505 putc(c, bitfile);
506 if (c == '\r') { /* DOS-style text file? */
507 c = getc(f);
508 dosepsend--;
509 if (c == '\n') {
510 putc(c, bitfile);
511 c = getc(f);
512 dosepsend--;
513 }
514 } else {
515 c = getc(f);
516 dosepsend--;
517 }
518 }
519 if (c != '%') {
520 /* try to find %%EndBinary or %%EndData anywhere
521 in the rest of the file, and pretend it
522 worked; this works around various Illustrator
523 bugs. -tgr, 14 June 2003 */
524 const char *m1 = "%%EndData";
525 const char *m2 = "%%EndBinary";
526 const char *p1 = m1;
527 const char *p2 = m2;
528 error(
529 " expected to see %%EndBinary at end of data; struggling on");
530 while (1) {
531 putc(c, bitfile);
532 if (c == '\r' || c == '\n') {
533 if (c == '\r') { /* DOS-style text file? */
534 c = getc(f);
535 if (c != '\n')
536 ungetc(c, f);
537 else
538 dosepsend--;
539 }
540 break;
541 }
542 c = getc(f);
543 dosepsend--;
544 if (c == EOF)
545 error(
546 "! premature end of file in binary section");
547 /*
548 * By the way, this code can be fooled by things like %%%EndBinary
549 * or even %%EndBi%%EndBinary, but this isn't valid DSC anyway.
550 * This comment is mainly here to prevent anyone else from emulating
551 * this code when doing stream-based substring matching.
552 */
553 if (c == *p1) {
554 p1++;
555 if (*p1 == 0)
556 break;
557 } else {
558 p1 = m1;
559 }
560 if (c == *p2) {
561 p2++;
562 if (*p2 == 0)
563 break;
564 } else {
565 p2 = m2;
566 }
567 }
568 }
569 while (1) {
570 putc(c, bitfile);
571 if (c == '\r' || c == '\n') {
572 if (c == '\r') { /* DOS-style text file? */
573 c = getc(f);
574 if (c != '\n')
575 ungetc(c, f);
576 else {
577 putc(c, bitfile);
578 dosepsend--;
579 }
580 }
581 break;
582 }
583 c = getc(f);
584 removingBytes = 0;
585 dosepsend--;
586 if (c == EOF)
587 error(
588 "! premature end of file in binary section");
589 }
590 c = getc(f);
591 dosepsend--;
592 }
593 } else if (scanForEnd && strncmp(possibleDSCLine, scanForEnd,
594 strlen(scanForEnd))==0) {
595 scanForEnd = 0;
596 scanningFont = 0;
597 }
598 dscLinePointer = possibleDSCLine;
599 } else if (dscLinePointer < dscLineEnd) {
600 *dscLinePointer++ = c;
601 if (removecomments && scanForEnd == 0 &&
602 c == '%' && dscLinePointer == possibleDSCLine + 1) {
603 /* % is first char */
604 c = getc(f);
605 if (c == '%' || c == '!')
606 removingBytes = 1;
607 if (c != EOF)
608 ungetc(c, f);
609 c = '%';
610 }
611 }
612 #if defined(VMCMS) || defined (MVSXA)
613 if (c != 0x37 || scanningFont) {
614 #else
615 if (c != 4 || scanningFont) {
616 #endif
617 if (!removingBytes)
618 putc(c, bitfile);
619 }
620 prevc = c;
621 /* begin DOS EPS code */
622 if (doseps && (dosepsend <= 0))
623 break; /* stop at end of DOS EPS PostScript section */
624 /* end DOS EPS code */
625 c = getc(f);
626 dosepsend--;
627 if (c == EOF)
628 break;
629 else if (c == '\r' && ! scanningFont) {
630 c = getc(f);
631 if (c == '\n') { /* DOS-style text file? */
632 if (!removingBytes) putc('\r', bitfile);
633 dosepsend--;
634 } else
635 ungetc(c, f);
636 c = '\n';
637 }
638 if (prevc == '\n')
639 removingBytes = 0;
640 }
641 }
642 }
643 if (prevc != '\n')
644 putc('\n', bitfile);
645 linepos = 0;
646 #ifndef VMCMS
647 #ifndef MVSXA
648 #ifndef VMS
649 #if !defined(MSDOS) || defined(__DJGPP__)
650 #ifndef ATARIST
651 #ifndef __THINK__
652 if (infigure == 2)
653 #ifdef OS2
654 {
655 if (_osmode == OS2_MODE)
656 pclose(f);
657 }
658 #else
659 pclose(f);
660 #endif
661 else
662 #endif
663 #endif
664 #endif
665 #endif
666 #endif
667 #endif
668 fclose(f);
669 if (cur_header && cur_header->postcode) {
670 fprintf(bitfile, "\n%s", cur_header->postcode);
671 free(cur_header->postcode);
672 }
673 if (!disablecomments) {
674 if (infigure)
675 fprintf(bitfile, "\n%%%%EndDocument\n");
676 else if (infont)
677 fprintf(bitfile, "\n%%%%EndFont\n");
678 #ifdef HPS
679 else if (noprocset) {}
680 #endif
681 else
682 fprintf(bitfile, "\n%%%%EndProcSet\n");
683 }
684 }
685 }
686
687 void
688 copyfile(const char *s)
689 {
690 copyfile_general(s, NULL);
691 }
692
693 /*
694 * For included PostScript graphics, we use the above routine, but
695 * with no fatal error message.
696 */
697 void
698 figcopyfile(char *s, int systemtype)
699 {
700 infigure = systemtype ? 2 : 1;
701 copyfile(s);
702 infigure = 0;
703 }
704 /*
705 * This next routine writes out a `special' character. In this case,
706 * we simply put it out, since any special character terminates the
707 * preceding token.
708 */
709 void
710 specialout(char c)
711 {
712 if (linepos >= LINELENGTH) {
713 putc('\n', bitfile);
714 linepos = 0;
715 }
716 putc(c, bitfile);
717 linepos++;
718 lastspecial = 1;
719 }
720
721 void
722 stringend(void)
723 {
724 if (linepos + instring >= LINELENGTH - 2) {
725 putc('\n', bitfile);
726 linepos = 0;
727 }
728 putc('(', bitfile);
729 *strbp = 0;
730 fputs(strbuffer, bitfile);
731 putc(')', bitfile);
732 linepos += instring + 2;
733 lastspecial = 1;
734 instring = 0;
735 strbp = strbuffer;
736 }
737
738 #ifdef SHIFTLOWCHARS
739 /*
740 * moving chars 0-32 and 127 to higher positions
741 * is desirable when using some fonts
742 */
743 int
744 T1Char(int c)
745 {
746 int tmpchr = c;
747 if (shiftlowchars && curfnt->resfont) {
748 if ((tmpchr <= 0x20)&&(tmpchr>=0)) {
749 if (tmpchr < 0x0A) {
750 tmpchr += 0xA1;
751 }
752 else {
753 tmpchr += 0xA3;
754 }
755 }
756 else if (tmpchr == 0x7F) {
757 tmpchr = 0xC4;
758 }
759 }
760 if (curfnt->chardesc[tmpchr].flags2 & EXISTS)
761 tmpchr = c;
762 return tmpchr;
763 }
764 #endif
765
766 void
767 scout(unsigned char c) /* string character out */
768 {
769 /*
770 * Is there room in the buffer? LINELENGTH-6 is used because we
771 * need room for (, ), and a possible four-byte string \000, for
772 * instance. If it is too long, we send out the string.
773 */
774 jflag = 0;
775 if (instring > LINELENGTH-6) {
776 stringend();
777 chrcmd('p');
778 }
779 #ifdef SHIFTLOWCHARS
780 c=T1Char(c);
781 #endif
782 /* changed next line to hex representation for VMCMS port
783 if (c<' ' || c > 126 || c=='%' ) {
784 */
785 if ( c<0x20 || c>= 0x7F || c==0x25 ) {
786 *strbp++ = '\\';
787 *strbp++ = '0' + ((c >> 6) & 3);
788 *strbp++ = '0' + ((c >> 3) & 7);
789 *strbp++ = '0' + (c & 7);
790 instring += 4;
791 } else {
792 #if defined(VMCMS) || defined (MVSXA)
793 c = ascii2ebcdic[c];
794 #endif
795 if (c == '(' || c == ')' || c == '\\') {
796 *strbp++ = '\\';
797 *strbp++ = c;
798 instring += 2;
799 } else {
800 *strbp++ = c;
801 instring++;
802 }
803 }
804 }
805
806 static void
807 scout2(int c)
808 {
809 char s[64];
810
811 sprintf(s, "<%04x>p", c);
812 cmdout(s);
813 }
814
815 static void
816 jscout(int c, char *fs) /* string character out */
817 {
818 char s[64];
819
820 if (!dir) {
821 numout(hh);
822 numout(vv);
823 } else {
824 numout(vv);
825 numout(-hh);
826 }
827 if (strstr(fs,"-UTF32-")!=NULL) {
828 snprintf(s, sizeof(s), "a<%08x>p", c);
829 } else if (strstr(fs,"-UTF8-")!=NULL) {
830 if (c<0x80) {
831 snprintf(s, sizeof(s), "a<%02x>p", c);
832 } else if (c<0x800) {
833 snprintf(s, sizeof(s), "a<%02x%02x>p", UCStoUTF8B1(c), UCStoUTF8B2(c));
834 } else if (c<0x10000) {
835 snprintf(s, sizeof(s), "a<%02x%02x%02x>p", UCStoUTF8C1(c),
836 UCStoUTF8C2(c), UCStoUTF8C3(c));
837 } else if (c<0x110000) {
838 snprintf(s, sizeof(s), "a<%02x%02x%02x%02x>p", UCStoUTF8D1(c),
839 UCStoUTF8D2(c), UCStoUTF8D3(c), UCStoUTF8D4(c));
840 } else {
841 error("warning: Illegal code value.");
842 }
843 } else if (c>0xffff && strstr(fs,"-UTF16-")!=NULL) {
844 snprintf(s, sizeof(s), "a<%04x%04x>p",
845 UTF32toUTF16HS(c), UTF32toUTF16LS(c));
846 } else {
847 if ((strstr(fs,"-RKSJ-")!=NULL)) c = JIStoSJIS(c);
848 snprintf(s, sizeof(s), "a<%04x>p", c);
849 }
850 cmdout(s);
851 instring = 0;
852 jflag = 1;
853 strbuffer[0] = '\0';
854 }
855
856 void
857 cmdout(const char *s)
858 {
859 int l;
860
861 /* hack added by dorab */
862 if (instring && !jflag) {
863 stringend();
864 chrcmd('p');
865 }
866 l = strlen(s);
867 if ((! lastspecial && linepos >= LINELENGTH - 20) ||
868 linepos + l >= LINELENGTH) {
869 putc('\n', bitfile);
870 linepos = 0;
871 } else if (! lastspecial) {
872 putc(' ', bitfile);
873 linepos++;
874 }
875 fputs(s, bitfile);
876 linepos += l;
877 lastspecial = 0;
878 }
879
880 void psnameout(const char *s) {
881 // we lead with a special, so we don't need the space.
882 lastspecial = 1 ;
883 cmdout(s) ;
884 }
885
886 void pslineout(const char *s) {
887 fputs(s, bitfile) ;
888 fprintf(bitfile, "\n");
889 linepos = 0;
890 }
891
892 static void
893 chrcmd(char c)
894 {
895 if ((! lastspecial && linepos >= LINELENGTH - 20) ||
896 linepos + 2 > LINELENGTH) {
897 putc('\n', bitfile);
898 linepos = 0;
899 } else if (! lastspecial) {
900 putc(' ', bitfile);
901 linepos++;
902 }
903 putc(c, bitfile);
904 linepos++;
905 lastspecial = 0;
906 }
907
908 void
909 floatout(float n)
910 {
911 char buf[50];
912
913 snprintf(buf, sizeof(buf), "%.2f", n);
914 cmdout(buf);
915 }
916
917 void
918 doubleout(double n)
919 {
920 char buf[50];
921
922 snprintf(buf, sizeof(buf), "%g", n);
923 cmdout(buf);
924 }
925
926 void
927 numout(integer n)
928 {
929 char buf[50];
930
931 #ifdef SHORTINT
932 snprintf(buf, sizeof(buf), "%ld", n);
933 #else
934 snprintf(buf, sizeof(buf), "%d", n);
935 #endif
936 cmdout(buf);
937 }
938
939 void
940 mhexout(register unsigned char *p,
941 register long len)
942 {
943 register const char *hexchar = hxdata;
944 register int n, k;
945
946 while (len > 0) {
947 if (linepos > LINELENGTH - 2) {
948 putc('\n', bitfile);
949 linepos = 0;
950 }
951 k = (LINELENGTH - linepos) >> 1;
952 if (k > len)
953 k = len;
954 len -= k;
955 linepos += (k << 1);
956 while (k--) {
957 n = *p++;
958 putc(hexchar[n >> 4], bitfile);
959 putc(hexchar[n & 15], bitfile);
960 }
961 }
962 }
963
964 static void
965 fontout(halfword n)
966 {
967 char buf[6];
968
969 if (instring && !jflag) {
970 stringend();
971 chrcmd('p');
972 }
973 makepsname(buf, n);
974 cmdout(buf);
975
976 lastfont = curfnt->psname;
977 }
978
979 void
980 hvpos(void)
981 {
982 if (!dir) {
983 if (rvv != vv || jflag) {
984 if (instring) {
985 stringend();
986 numout(hh);
987 numout(vv);
988 chrcmd('y');
989 } else if (rhh != hh || jflag) {
990 numout(hh);
991 numout(vv);
992 chrcmd('a') ;
993 } else { /* hard to get this case, but it's there when you need it! */
994 numout(vv - rvv);
995 chrcmd('x');
996 }
997 rvv = vv;
998 } else if (rhh != hh || jflag) {
999 if (instring) {
1000 stringend();
1001 if (hh - rhh < 5 && rhh - hh < 5) {
1002 #if defined(VMCMS) || defined (MVSXA) /* should replace 'p' in non-VMCMS, non-MVSXA line as well */
1003 chrcmd(ascii2ebcdic[(char)(112 + hh - rhh)]);
1004 #else
1005 chrcmd((char)('p' + hh - rhh));
1006 #endif
1007 } else if (hh - rhh < d + 5 && rhh - hh < 5 - d) {
1008 #if defined(VMCMS) || defined (MVSXA) /* should replace 'g' in non-VMCMS, non-MVSXA line as well */
1009 chrcmd(ascii2ebcdic[(char)(103 + hh - rhh - d)]);
1010 #else
1011 chrcmd((char)('g' + hh - rhh - d));
1012 #endif
1013 d = hh - rhh;
1014 } else {
1015 numout(hh - rhh);
1016 chrcmd('b');
1017 d = hh - rhh;
1018 }
1019 } else {
1020 numout(hh - rhh);
1021 chrcmd('w');
1022 }
1023 }
1024 rhh = hh;
1025 } else {
1026 if (rhh != hh || jflag) {
1027 if (instring) {
1028 stringend();
1029 numout(vv);
1030 numout(-hh);
1031 chrcmd('y');
1032 } else if (rvv != vv || jflag) {
1033 numout(vv);
1034 numout(-hh);
1035 chrcmd('a');
1036 } else { /* hard to get this case, but it's there when you need it! */
1037 numout(rhh - hh);
1038 chrcmd('x');
1039 }
1040 rhh = hh;
1041 } else if (rvv != vv || jflag) {
1042 if (instring) {
1043 stringend();
1044 if (vv - rvv < 5 && rvv - vv < 5) {
1045 #if defined(VMCMS) || defined (MVSXA) /* should replace 'p' in non-VMCMS, non-MVSXA line as well */
1046 chrcmd(ascii2ebcdic[(char)(112 + vv - rvv)]);
1047 #else
1048 chrcmd((char)('p' + vv - rvv));
1049 #endif
1050 } else if (vv - rvv < d + 5 && rvv - vv < 5 - d) {
1051 #if defined(VMCMS) || defined (MVSXA) /* should replace 'g' in non-VMCMS, non-MVSXA line as well */
1052 chrcmd(ascii2ebcdic[(char)(103 + vv - rvv - d)]);
1053 #else
1054 chrcmd((char)('g' + vv - rvv - d));
1055 #endif
1056 d = vv - rvv;
1057 } else {
1058 numout(vv - rvv);
1059 chrcmd('b');
1060 d = vv - rvv;
1061 }
1062 } else {
1063 numout(vv - rvv);
1064 chrcmd('w');
1065 }
1066 }
1067 rvv = vv;
1068 }
1069 }
1070
1071 /*
1072 * initprinter opens the bitfile and writes the initialization sequence
1073 * to it.
1074 */
1075 void
1076 newline(void)
1077 {
1078 if (linepos != 0) {
1079 fprintf(bitfile, "\n");
1080 linepos = 0;
1081 }
1082 lastspecial = 1;
1083 }
1084
1085 void
1086 nlcmdout(const char *s)
1087 {
1088 newline();
1089 cmdout(s);
1090 newline();
1091 }
1092 /*
1093 * Is the dimension close enough for a match? We use 5bp
1094 * as a match; this is 65536*72.27*5/72 or 328909 scaled points.
1095 */
1096 static int
1097 indelta(integer i)
1098 {
1099 if (i < 0)
1100 i = -i;
1101 return (i <= 328909);
1102 }
1103 /*
1104 * A case-irrelevant string compare.
1105 */
1106 static int
1107 mlower(int c)
1108 {
1109 if ('A' <= c && c <= 'Z')
1110 return c - 'A' + 'a';
1111 else
1112 return c;
1113 }
1114 static int
1115 ncstrcmp(const char *a, const char *b)
1116 {
1117 while (*a && (*a == *b ||
1118 mlower(*a) == mlower(*b)))
1119 a++, b++;
1120 if (*a == 0 && *b == 0)
1121 return 0;
1122 else
1123 return 1;
1124 }
1125 /*
1126 * Find the paper size.
1127 */
1128 static void
1129 findpapersize(void) {
1130 if (finpapsiz == 0) {
1131 struct papsiz *ps;
1132 struct papsiz *fps = 0;
1133 int ih, iv, it;
1134 int mindiff = 0x7fffffff;
1135
1136 if (tryepsf && !landscape) {
1137 finpapsiz = &defpapsiz;
1138 hpapersize = defpapsiz.xsize;
1139 vpapersize = defpapsiz.ysize;
1140 return;
1141 }
1142 if (cropmarks) {
1143 /*
1144 * If user wanted crop marks, we increase the size of the page by
1145 * a half inch all around.
1146 */
1147 if (hpapersize == 0 || vpapersize == 0) {
1148 error(
1149 "warning: -k crop marks wanted, but no paper size specified; using default");
1150 if (landscape) {
1151 hpapersize = defpapsiz.ysize;
1152 vpapersize = defpapsiz.xsize;
1153 } else {
1154 hpapersize = defpapsiz.xsize;
1155 vpapersize = defpapsiz.ysize;
1156 }
1157 }
1158 hpapersize += 2368143L;
1159 vpapersize += 2368143L;
1160 add_header(CROPHEADER);
1161 }
1162 if (paperfmt && *paperfmt) {
1163 for (ps = papsizes; ps; ps = ps->next)
1164 if (ncstrcmp(paperfmt, ps->name)==0)
1165 finpapsiz = ps;
1166 if (finpapsiz == 0)
1167 error("no match for papersize");
1168 }
1169 if (finpapsiz == 0 && hpapersize > 0 && vpapersize > 0) {
1170 for (ps=papsizes; ps; ps = ps->next) {
1171 ih = ps->xsize-hpapersize;
1172 iv = ps->ysize-vpapersize;
1173 if (ih < 0) ih = -ih;
1174 if (iv < 0) iv = -iv;
1175 it = ih;
1176 if (it < iv) it = iv;
1177 if (it < mindiff) {
1178 mindiff = it;
1179 fps = ps;
1180 }
1181 }
1182 if (indelta(mindiff))
1183 landscape = 0;
1184 else
1185 fps = 0;
1186 mindiff = 0x7fffffff;
1187 if (fps == 0) {
1188 if (landscaperotate) {
1189 for (ps=papsizes; ps; ps = ps->next) {
1190 iv = ps->ysize-hpapersize;
1191 ih = ps->xsize-vpapersize;
1192 if (ih < 0) ih = -ih;
1193 if (iv < 0) iv = -iv;
1194 it = ih;
1195 if (it < iv) it = iv;
1196 if (it < mindiff) {
1197 mindiff = it;
1198 fps = ps;
1199 }
1200 }
1201 }
1202 if (indelta(mindiff))
1203 landscape = 1;
1204 else
1205 fps = 0;
1206 if (fps == 0) {
1207 for (ps=papsizes; ps; ps = ps->next) {
1208 if (ps->ysize == 0 && ps->xsize == 0) {
1209 fps = ps;
1210 break;
1211 }
1212 }
1213 if (fps == 0) {
1214 landscape = (hpapersize > vpapersize);
1215 error(
1216 "no match for special paper size found; using default");
1217 }
1218 }
1219 }
1220 finpapsiz = fps;
1221 }
1222 if (finpapsiz == 0) {
1223 if (papsizes)
1224 finpapsiz = papsizes;
1225 else
1226 finpapsiz = &defpapsiz;
1227 /*
1228 * But change xsize/ysize to match so bounding box works.
1229 */
1230 if (hpapersize && vpapersize) {
1231 if (landscape) {
1232 finpapsiz->ysize = hpapersize;
1233 finpapsiz->xsize = vpapersize;
1234 } else {
1235 finpapsiz->xsize = hpapersize;
1236 finpapsiz->ysize = vpapersize;
1237 }
1238 }
1239 }
1240 /*
1241 * Here, there was no papersize special. We set the paper size from
1242 * the selected paper format. If the selected paper format has no
1243 * sizes, we use the defaults.
1244 */
1245 if (hpapersize == 0 || vpapersize == 0) {
1246 if (finpapsiz->xsize == 0 || finpapsiz->ysize == 0) {
1247 finpapsiz->xsize = defpapsiz.xsize;
1248 finpapsiz->ysize = defpapsiz.ysize;
1249 }
1250 if (landscape) {
1251 vpapersize = finpapsiz->xsize;
1252 hpapersize = finpapsiz->ysize;
1253 } else {
1254 hpapersize = finpapsiz->xsize;
1255 vpapersize = finpapsiz->ysize;
1256 }
1257 /*
1258 * Here, there was a papersize special, but the selected paper
1259 * format has 0 0 for sizes. We set the sizes here so that the
1260 * bounding box works.
1261 */
1262 } else if (finpapsiz->xsize == 0 || finpapsiz->ysize == 0) {
1263 finpapsiz->xsize = hpapersize;
1264 finpapsiz->ysize = vpapersize;
1265 /*
1266 * Here, the user specified a size with -t, and there was a
1267 * papersize special, and its sizes were greater than zero.
1268 * We make sure the sizes are okay. Note that the user must have
1269 * specified landscape if this is desired.
1270 */
1271 } else if (paperfmt && *paperfmt) {
1272 if (landscape) {
1273 if (!indelta(vpapersize - finpapsiz->xsize) ||
1274 !indelta(hpapersize - finpapsiz->ysize)) {
1275 if (vpapersize > finpapsiz->xsize ||
1276 hpapersize > finpapsiz->ysize)
1277 error("warning: -t selected paper may be too small");
1278 else
1279 error("note: -t selected paper may be too large");
1280 }
1281 } else {
1282 if (!indelta(hpapersize - finpapsiz->xsize) ||
1283 !indelta(vpapersize - finpapsiz->ysize)) {
1284 if (hpapersize > finpapsiz->xsize ||
1285 vpapersize > finpapsiz->ysize)
1286 error("warning: -t selected paper may be too small");
1287 else
1288 error("note: -t selected paper may be too large");
1289 }
1290 }
1291 }
1292 }
1293 }
1294 /*
1295 * Convert scaled points to PostScript points. This is the same
1296 * as return (i * 72 / (65536 * 72.27)), which is the same as
1297 * dividing by 65781.76, but we want to round up.
1298 */
1299 static int
1300 topoints(integer i)
1301 {
1302 i += 65780L;
1303 return (i / 6578176L)*100 + (i % 6578176) * 100 / 6578176;
1304 }
1305 /*
1306 * Send out the special paper stuff. If `hed' is non-zero, only
1307 * send out lines starting with `!' else send all other lines out.
1308 */
1309 static void
1310 paperspec(const char *s, int hed)
1311 {
1312 int sendit;
1313
1314 while (*s) {
1315 s++;
1316 if (*s == '\0')
1317 return;
1318 if (*s == '!') {
1319 s++;
1320 while (*s == ' ') s++;
1321 sendit = hed;
1322 } else
1323 sendit = ! hed;
1324 if (sendit) {
1325 while (*s && *s != '\n')
1326 putc(*s++, bitfile);
1327 putc('\n', bitfile);
1328 } else {
1329 while (*s && *s != '\n')
1330 s++;
1331 }
1332 }
1333 }
1334 static char *
1335 epsftest(integer bop)
1336 {
1337 if (tryepsf && paperfmt == 0 && *iname) {
1338 findbb(bop+44);
1339 return nextstring;
1340 }
1341 return 0;
1342 }
1343 static char *isepsf = 0;
1344 static int endprologsent;
1345 void
1346 open_output(void) {
1347 FILE * pf = NULL;
1348 if (*oname != 0) {
1349 /*
1350 * We check to see if the first character is a exclamation
1351 * point, and popen if so.
1352 */
1353 if (*oname == '!' || *oname == '|') {
1354 #if defined(MSDOS) && !defined(__DJGPP__) || defined(VMS) || defined(VMCMS) || defined(MVSXA) || defined(__THINK__) || defined(ATARIST)
1355 error("! can't open output pipe");
1356 #else
1357 #ifdef OS2
1358 if (_osmode != OS2_MODE) {
1359 error("! can't open output pipe");
1360 } else {
1361 #endif
1362 #ifdef __DJGPP__
1363 /* Feature: if they pipe to "lpr" and there's no executable by
1364 that name anywhere in sight, write to local printer instead.
1365
1366 We do this up front, before even trying to popen, because on
1367 MS-DOS popen always succeeds for writing (it only opens a
1368 temporary file), and by the time we get to actually run the
1369 (possibly nonexistent) program in pclose, it's too late to
1370 fall back.
1371
1372 We don't use kpathsea functions here because they don't
1373 know about DOS-specific executable extensions, while we
1374 want to be able to find "lpr.exe", "lpr.com", "lpr.bat" etc. */
1375 extern char *__dosexec_find_on_path(const char *,
1376 char **, char *);
1377 extern char **environ;
1378 char *p = oname + 1;
1379 char found[FILENAME_MAX];
1380
1381 while (ISSPACE(*p))
1382 p++;
1383 if (strncmp(p, "lpr", 3) == 0 && (ISSPACE(p[3]) || p[3] == '\0')
1384 && !__dosexec_find_on_path(oname+1, (char **)0, found)
1385 && !__dosexec_find_on_path(oname+1, environ, found))
1386 pf = fopen("PRN", "w");
1387 #endif
1388 if (pf == NULL && (pf = popen(oname+1, "w")) != NULL) {
1389 popened = 1;
1390 SET_BINARY(fileno(pf));
1391 }
1392 if (pf == NULL)
1393 error("! couldn't open output pipe");
1394 bitfile = pf;
1395 #ifdef OS2
1396 }
1397 #endif
1398 #endif /* MSDOS && !__DJGPP__ || VMS || ... */
1399 } else {
1400 if ((bitfile=fopen(oname,"w"))==NULL)
1401 error("! couldn't open PostScript file");
1402 SET_BINARY(fileno(bitfile));
1403 }
1404 } else {
1405 bitfile = stdout;
1406 }
1407
1408 /* Even PostScript output may include binary characters, so switch
1409 bitfile to binary mode. */
1410 if (O_BINARY && !isatty(fileno(bitfile)))
1411 SET_BINARY(fileno(bitfile));
1412 }
1413 void
1414 initprinter(sectiontype *sect)
1415 {
1416 int n = sect->numpages * pagecopies * collatedcopies;
1417 #ifdef HPS
1418 if (!HPS_FLAG)
1419 #endif
1420 open_output();
1421
1422 findpapersize();
1423 if (disablecomments) {
1424 fprintf(bitfile,
1425 "%%!PS (but not EPSF; comments have been disabled)\n");
1426 fprintf(stderr, "Warning: no %%%%Page comments generated.\n");
1427 } else {
1428 if (multiplesects) {
1429 fprintf(bitfile,
1430 "%%!PS (but not EPSF because of memory limits)\n");
1431 fprintf(stderr, "Warning: no %%%%Page comments generated.\n");
1432 } else {
1433 isepsf = epsftest(sect->bos);
1434 if (isepsf)
1435 fprintf(bitfile, "%%!PS-Adobe-2.0 EPSF-2.0\n");
1436 else
1437 fprintf(bitfile, "%%!PS-Adobe-2.0\n");
1438 }
1439 if (tryepsf && isepsf == 0)
1440 error("We tried, but couldn't make it EPSF.");
1441 fprintf(bitfile, "%%%%Creator: %s\n", banner + 8);
1442 if (*titlename)
1443 fprintf(bitfile, "%%%%Title: %s\n", titlename);
1444 else if (*iname)
1445 fprintf(bitfile, "%%%%Title: %s\n", xbasename(iname));
1446 #ifdef CREATIONDATE
1447 jobtime = get_unique_time_if_given();
1448 if (jobtime == INVALID_EPOCH_VALUE) {
1449 jobtime = time(0);
1450 }
1451 fprintf(bitfile, "%%%%CreationDate: %s", asctime(gmtime(&jobtime)));
1452 #endif
1453 if (! isepsf) {
1454 /*
1455 * Normally, we wouldn't want to add that second field
1456 * indicating that the page order is reversed, as per page
1457 * 644 of the Red book. But we have to, for many existing
1458 * spoolers.
1459 */
1460 fprintf(bitfile, "%%%%Pages: %d%s\n", (sepfiles ? n : totalpages),
1461 (reverse?" -1":""));
1462 fprintf(bitfile, "%%%%PageOrder: %sscend\n", reverse?"De":"A");
1463 }
1464 if (landscape) {
1465 fprintf(bitfile, "%%%%Orientation: Landscape\n");
1466 fprintf(bitfile, "%%%%BoundingBox: 0 0 %d %d\n",
1467 topoints(finpapsiz->xsize), topoints(finpapsiz->ysize));
1468 } else if (isepsf)
1469 fprintf(bitfile, "%%%%BoundingBox: %s\n", isepsf);
1470 else
1471 fprintf(bitfile, "%%%%BoundingBox: 0 0 %d %d\n",
1472 topoints(finpapsiz->xsize), topoints(finpapsiz->ysize));
1473 tell_needed_fonts();
1474 paperspec(finpapsiz->specdat, 1);
1475 fprintf(bitfile, "%%%%EndComments\n");
1476 /*
1477 * If we encode Type 3 fonts with an encoding vector, this can cause
1478 * Distiller's autoorientation to get confused. We remedy this by
1479 * emitting underdocumented ViewingOrientation comments right after
1480 * EndComments. Known defect: if a user "flips" the landscape to be
1481 * 180 degrees using one of the \special{} commands available, the
1482 * document will be rendered in the viewer upside down. (But only
1483 * with bitmap font encoding enabled and bitmapped fonts actually used.)
1484 * --tgr, 29 February 2020.
1485 */
1486 if (encodetype3 && bitmapfontseen) {
1487 fprintf(bitfile, "%%%%BeginDefaults\n") ;
1488 if (landscape) {
1489 fprintf(bitfile, "%%%%ViewingOrientation: 0 -1 1 0\n") ;
1490 } else {
1491 fprintf(bitfile, "%%%%ViewingOrientation: 1 0 0 1\n") ;
1492 }
1493 fprintf(bitfile, "%%%%EndDefaults\n") ;
1494 }
1495 }
1496 {
1497 int i, len;
1498 char *p;
1499
1500 /*
1501 * Here, too, we have to be careful not to exceed the line length
1502 * limitation, if possible.
1503 */
1504 fprintf(bitfile, "%%DVIPSWebPage: %s\n", banner2);
1505 fprintf(bitfile, "%%DVIPSCommandLine:");
1506 len = 18;
1507 for (i=0; i<gargc; i++) {
1508 p = gargv[i];
1509 while (*p > ' ')
1510 p++;
1511 if (*p)
1512 len += 2;
1513 len += strlen(gargv[i]) + 1;
1514 if (len > LINELENGTH) {
1515 fprintf(bitfile, "\n%%+");
1516 len = strlen(gargv[i]) + 3;
1517 if (*p)
1518 len += 2;
1519 }
1520 fprintf(bitfile, (*p ? " \"%s\"" : " %s"), gargv[i]);
1521 }
1522 fprintf(bitfile, "\n%%DVIPSParameters: dpi=%d", actualdpi);
1523 if (actualdpi != vactualdpi)
1524 fprintf(bitfile, "x%d", vactualdpi);
1525 if (compressed)
1526 fprintf(bitfile, ", compressed");
1527 if (removecomments)
1528 fprintf(bitfile, ", comments removed");
1529 fputc('\n', bitfile);
1530 }
1531 #if defined(VMCMS) || defined (MVSXA) /* convert preamblecomment to ebcdic so we can read it */
1532 {
1533 int i;
1534 for ( i=0; preamblecomment[i]; i++ )
1535 preamblecomment[i] = ascii2ebcdic[preamblecomment[i]];
1536 }
1537 #endif
1538 fprintf(bitfile, "%%DVIPSSource: %s\n", preamblecomment);
1539 linepos = 0;
1540 endprologsent = 0;
1541 if (safetyenclose)
1542 fprintf(bitfile, "/SafetyEnclosure save def\n");
1543 print_composefont();
1544 if (! headers_off)
1545 send_headers();
1546 }
1547 void
1548 setup(void) {
1549 newline();
1550 if (endprologsent == 0 && !disablecomments) {
1551 fprintf(bitfile, "%%%%EndProlog\n");
1552 fprintf(bitfile, "%%%%BeginSetup\n");
1553 if (vactualdpi == actualdpi)
1554 fprintf(bitfile, "%%%%Feature: *Resolution %ddpi\n",
1555 actualdpi);
1556 else
1557 fprintf(bitfile, "%%%%Feature: *Resolution %dx%ddpi\n",
1558 actualdpi, vactualdpi);
1559 if (multiplesects && *(finpapsiz->specdat)) {
1560 fprintf(bitfile, "TeXDict begin\n");
1561 paperspec(finpapsiz->specdat, 0);
1562 fprintf(bitfile, "end\n");
1563 }
1564 if (manualfeed)
1565 fprintf(bitfile, "%%%%Feature: *ManualFeed True\n");
1566 #ifdef HPS
1567 if (!HPS_FLAG)
1568 #endif
1569 if (multiplesects)
1570 fprintf(bitfile, "%%%%EndSetup\n");
1571 }
1572 if (multiplesects && ! disablecomments)
1573 fprintf(bitfile, "%%DVIPSBeginSection\n");
1574 cmdout("TeXDict");
1575 cmdout("begin");
1576 if (endprologsent || disablecomments || multiplesects == 0) {
1577 fprintf(bitfile, "\n");
1578 paperspec(finpapsiz->specdat, 0);
1579 }
1580 if (manualfeed) cmdout("@manualfeed");
1581 if (landscape) cmdout("@landscape");
1582 if (numcopies != 1) {
1583 numout((integer)numcopies);
1584 cmdout("@copies");
1585 }
1586 cmdout("end");
1587 if (endprologsent == 0 && !disablecomments) {
1588 newline();
1589 endprologsent = 1;
1590 #ifdef HPS
1591 if (!HPS_FLAG)
1592 #endif
1593 if (! multiplesects)
1594 fprintf(bitfile, "%%%%EndSetup\n");
1595 }
1596 #ifdef HPS
1597 if (HPS_FLAG) {
1598 fclose(bitfile);
1599 set_bitfile("body.tmp",0);
1600 }
1601 #endif
1602 }
1603 /*
1604 * cleanprinter is the antithesis of the above routine.
1605 */
1606 void
1607 cleanprinter(void)
1608 {
1609 fprintf(bitfile, "\n");
1610 fprintf(bitfile, "userdict /end-hook known{end-hook}if\n");
1611 if (safetyenclose)
1612 fprintf(bitfile, "SafetyEnclosure restore\n");
1613 if (!disablecomments)
1614 fprintf(bitfile, "%%%%EOF\n");
1615 if (sendcontrolD)
1616 putc(4, bitfile);
1617 if (ferror(bitfile))
1618 error("Problems with file writing; probably disk full.");
1619 #if !defined(MSDOS) || defined(__DJGPP__)
1620 #ifndef VMS
1621 #ifndef MVSXA
1622 #ifndef VMCMS
1623 #ifndef __THINK__
1624 #ifndef ATARIST
1625 #ifdef OS2
1626 if (_osmode == OS2_MODE)
1627 #endif
1628 if (popened)
1629 pclose(bitfile);
1630 #endif
1631 #endif
1632 #endif
1633 #endif
1634 #endif
1635 #endif
1636 if (popened == 0)
1637 fclose(bitfile);
1638 bitfile = NULL;
1639 }
1640
1641 /* this tells dvips that it has no clue where it is. */
1642 static int thispage = 0;
1643 static integer rulex, ruley;
1644 void
1645 psflush(void) {
1646 rulex = ruley = rhh = rvv = -314159265;
1647 lastfont = -1;
1648 }
1649 /*
1650 * pageinit initializes the output variables.
1651 */
1652 void
1653 pageinit(void)
1654 {
1655 psflush();
1656 newline();
1657 thispage++;
1658 if (!disablecomments) {
1659 if (multiplesects)
1660 #ifdef SHORTINT
1661 fprintf(bitfile, "%%DVIPSSectionPage: %ld\n", pagenum);
1662 else if (! isepsf)
1663 fprintf(bitfile, "%%%%Page: %ld %d\n", pagenum, thispage);
1664 #else
1665 fprintf(bitfile, "%%DVIPSSectionPage: %d\n", pagenum);
1666 else if (! isepsf)
1667 fprintf(bitfile, "%%%%Page: %d %d\n", pagenum, thispage);
1668 #endif
1669 }
1670 linepos = 0;
1671 cmdout("TeXDict");
1672 cmdout("begin");
1673 #ifdef HPS
1674 if (HPS_FLAG) {
1675 cmdout("HPSdict");
1676 cmdout("begin");
1677 }
1678 #endif
1679 if (landscape) cmdout("@landscape");
1680 numout((integer)pagenum);
1681 numout((integer)thispage-1);
1682 cmdout("bop");
1683 d = 0;
1684 }
1685
1686
1687
1688 /*
1689 * This routine ends a page.
1690 */
1691 void
1692 pageend(void)
1693 {
1694 if (instring) {
1695 stringend();
1696 chrcmd('p');
1697 }
1698 if (dir)
1699 cmdout("-90 rotate");
1700 if (any_dir)
1701 cmdout("dyy");
1702 cmdout("eop");
1703 cmdout("end");
1704 #ifdef HPS
1705 if (HPS_FLAG)
1706 cmdout("end");
1707 #endif
1708 }
1709
1710 /*
1711 * drawrule draws a rule at the specified position.
1712 * It does nothing to save/restore the current position,
1713 * or even draw the current string. (Rules are normally
1714 * set below the baseline anyway, so this saves us on
1715 * output size almost always.)
1716 */
1717 void
1718 drawrule(integer rw, integer rh)
1719 {
1720 if (!dir) {
1721 numout((integer)hh);
1722 numout((integer)vv);
1723 } else {
1724 numout((integer)vv);
1725 numout((integer)-hh);
1726 }
1727 if (rw == rulex && rh == ruley)
1728 chrcmd('V');
1729 else {
1730 numout((integer)rw);
1731 numout((integer)rh);
1732 chrcmd('v');
1733 rulex = rw;
1734 ruley = rh;
1735 }
1736 }
1737
1738 /*
1739 * drawchar draws a character at the specified position.
1740 */
1741 void
1742 drawchar(chardesctype *c, int cc)
1743 {
1744 if (rdir != dir || fdir != curfnt->dir) {
1745 if (curfnt->dir == 9)
1746 setdir(dir+2);
1747 else
1748 setdir(dir);
1749 rdir = dir;
1750 fdir = curfnt->dir;
1751 }
1752
1753 if (curfnt->iswide == 0 && curfnt->codewidth == 2) {
1754 Boolean savejflag = jflag;
1755 jflag = 1;
1756 hvpos();
1757 jflag = savejflag;
1758 if (lastfont != curfnt->psname)
1759 fontout(curfnt->psname);
1760 scout2(cc);
1761 }
1762 else if (curfnt->iswide) {
1763 if (lastfont != curfnt->psname)
1764 fontout(curfnt->psname);
1765 jscout(cc, curfnt->resfont->PSname);
1766 }
1767 else {
1768 if (jflag) {
1769 if (!dir){
1770 numout(hh);
1771 numout(vv);
1772 }
1773 else {
1774 numout(vv);
1775 numout(-hh);
1776 }
1777 chrcmd('a');
1778 rhh = hh;
1779 rvv = vv;
1780 }
1781 else hvpos();
1782 if (lastfont != curfnt->psname)
1783 fontout(curfnt->psname);
1784 scout((unsigned char)cc);
1785 }
1786 if (!dir)
1787 rhh = hh + c->pixelwidth; /* rvv = rv */
1788 else
1789 rvv = vv + c->pixelwidth; /* rhh = rh */
1790 }
1791 /*
1792 * This routine sends out the document fonts comment.
1793 */
1794 static void
1795 tell_needed_fonts(void) {
1796 struct header_list *hl = ps_fonts_used;
1797 char *q;
1798 int roomleft = -1;
1799
1800 if (hl == 0)
1801 return;
1802 while (0 != (q=get_name(&hl))) {
1803 if ((int)strlen(q) >= roomleft) {
1804 if (roomleft != -1) {
1805 fprintf(bitfile, "\n%%%%+");
1806 roomleft = LINELENGTH - 3;
1807 } else {
1808 fprintf(bitfile, "%%%%DocumentFonts:");
1809 roomleft = LINELENGTH - 16;
1810 }
1811 }
1812 fprintf(bitfile, " %s", q);
1813 roomleft -= strlen(q) + 1;
1814 }
1815 fprintf(bitfile, "\n");
1816 }
1817
1818 static void print_composefont(void)
1819 {
1820 struct header_list *hl = ps_fonts_used;
1821 int len;
1822 char *q, *p;
1823
1824 if (hl == 0)
1825 return;
1826 while (0 != (q=get_name(&hl))) {
1827 len = strlen(q);
1828 if(len > 11 && (!strncmp(q+len-10, "Identity-H", 10) ||
1829 !strncmp(q+len-10, "Identity-V", 10))) {
1830 fprintf(bitfile, "%%%%BeginFont: %s\n", q);
1831 fprintf(bitfile, "/%s ", q);
1832 fprintf(bitfile, "/%s ", q+len-10);
1833 fprintf(bitfile, "[/");
1834 for(p=q; p <= q+len-12; p++)
1835 fprintf(bitfile, "%c", *p);
1836 fprintf(bitfile, "] composefont pop\n");
1837 fprintf(bitfile, "%%%%EndFont\n");
1838 }
1839 }
1840 }
1841
1842 static void setdir(int d)
1843 {
1844 if (instring) {
1845 stringend();
1846 chrcmd('p');
1847 }
1848 switch(d) {
1849 case 1 :
1850 cmdout("dyt");
1851 break;
1852 case 2 :
1853 cmdout("dty");
1854 break;
1855 case 3 :
1856 cmdout("dtt");
1857 break;
1858 default :
1859 cmdout("dyy");
1860 break;
1861 }
1862 linepos += 4;
1863 any_dir = 1;
1864 }
1865
1866 void cmddir(void)
1867 {
1868 if (dir != rdir) {
1869 if (dir)
1870 cmdout("90 rotate");
1871 else
1872 cmdout("-90 rotate");
1873 rdir = dir;
1874 }
1875 }
1876
1877 static int JIStoSJIS(int c)
1878 {
1879 int high, low;
1880 int nh,nl;
1881
1882 high = (c>>8) & 0xff;
1883 low = c & 0xff;
1884 nh = ((high-0x21)>>1) + 0x81;
1885 if (nh>0x9f)
1886 nh += 0x40;
1887 if (high & 1) {
1888 nl = low + 0x1f;
1889 if (low>0x5f)
1890 nl++;
1891 }
1892 else
1893 nl = low + 0x7e;
1894 if (((nh >= 0x81 && nh <= 0x9f) || (nh >= 0xe0 && nh <= 0xfc)) &&
1895 (nl >= 0x40 && nl <= 0xfc && nl != 0x7f))
1896 return((nh<<8) | nl);
1897 else
1898 return(0x813f);
1899 }