Debian Codegraph @

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