2 * Convert ASCII to PostScript.
3 * Copyright (c) 1995-2002 Markku Rossi.
5 * Author: Markku Rossi <mtr@iki.fi>
9 * This file is part of GNU Enscript.
11 * Enscript is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
16 * Enscript is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with Enscript. If not, see <http://www.gnu.org/licenses/>.
28 * Types and definitions.
31 /* Values for token flags. */
34 #define F_EPSF_CENTER 0x01
35 #define F_EPSF_RIGHT 0x02
36 #define M_EPSF_JUSTIFICATION 0x03
38 #define F_EPSF_NO_CPOINT_UPDATE_X 0x04
39 #define F_EPSF_NO_CPOINT_UPDATE_Y 0x08
41 #define F_EPSF_ABSOLUTE_X 0x10
42 #define F_EPSF_ABSOLUTE_Y 0x20
44 #define F_EPSF_SCALE_X 0x40
45 #define F_EPSF_SCALE_Y 0x80
48 /* Predicate to check if we are at the correct slice. */
49 #define CORRECT_SLICE() (slicing == 0 || current_slice == slice)
51 /* Predicates for the current body font. */
53 /* Is character <ch> printable. */
54 #define ISPRINT(ch) (font_ctype[(unsigned char) (ch)] != ' ')
56 /* Does character <ch> exist in current body font? */
57 #define EXISTS(ch) (font_ctype[(unsigned char) (ch)] == '*')
60 #define RESOURCE_LINE_WIDTH 75
84 /* Special escape tokens. */
103 /* Token structure. */
108 double new_x; /* Current point x after this token. */
109 double new_y; /* Current point y after this token. */
110 int new_col; /* Line column after this token. */
118 double x; /* x-offset */
119 double y; /* y-offset */
120 double w; /* width */
121 double h; /* height */
124 int llx, lly, urx, ury; /* Bounding box. */
127 unsigned int skipbuf_len;
128 unsigned int skipbuf_pos;
129 FILE *fp; /* File from which eps image is read. */
130 int pipe; /* Is <fp> opened to pipe? */
138 InputEncoding encoding;
144 typedef struct gs_token_st Token;
148 * Prototypes for static functions.
151 static void get_next_token ___P ((InputStream *is, double linestart,
152 double linepos, unsigned int col,
153 double linew, Token *token));
155 static void dump_ps_page_header ___P ((char *fname, int empty));
157 static void dump_ps_page_trailer ();
159 static void dump_empty_page ();
162 * Recognize a EPS file described by <token>. Returns 1 if file was a
163 * valid EPS file or 0 otherwise. File is accepted if it starts with
164 * the PostScript magic `%!' and it has a valid `%%BoundingBox' DSC
167 static int recognize_eps_file ___P ((Token *token));
170 * Insert EPS file described by <token> to the output stream.
172 static void paste_epsf ___P ((Token *token));
175 * Check if InputStream <is> contains a file which can be passed
176 * through without any modifications. Returns 1 if file was passed or
179 static int do_pass_through ___P ((char *fname, InputStream *is));
182 * Read one float dimension from InputStream <is>. If <units> is
183 * true, number can be followed by an optional unit specifier. If
184 * <horizontal> is true, dimension is horizontal, otherwise it is
185 * vertical (this is used to find out how big `line' units are).
187 static double read_float ___P ((InputStream *is, int units, int horizontal));
190 * Print linenumber <linenum> to the beginning of the current line.
191 * Current line start is specified by point (x, y).
193 static void print_line_number ___P ((double x, double y, double space,
194 double margin, unsigned int linenum));
196 /* Send PostScript to the output file. */
197 #define OUTPUT(body) \
205 /* Divert output to tmp file so the total page count can be counted. */
206 static void divert ();
208 /* Paste diverted data to the output and patch the total page counts. */
209 static void undivert ();
212 * Handle two-side printing related binding options. This function is
213 * called once for each even-numbered page.
215 static void handle_two_side_options ();
221 unsigned int current_pagenum = 0; /* The number of the current page. */
222 unsigned int total_pages_in_file;
223 unsigned int input_filenum = 0;
224 unsigned int current_file_linenum;
225 int first_pagenum_for_file;
226 char *fname = NULL; /* The name of the current input file. */
233 /* Have we dumped PS header? */
234 static int ps_header_dumped = 0;
237 static FILE *divertfp = NULL;
239 /* Current output() file. */
240 static FILE *cofp = NULL;
242 /* To print or not to print, that's a question. */
243 static int do_print = 1;
245 /* Is ^@font{}-defined font active? */
246 static int user_fontp = 0;
248 /* The user ^@font{}-defined font. */
249 static char user_font_name[256];
250 static FontPoint user_font_pt;
251 static InputEncoding user_font_encoding;
253 /* Is ^@color{}-defined color active? */
254 static int user_colorp = 0;
256 /* The user ^@color{}-defined color. */
257 static Color user_color;
259 /* Is ^@bgcolor{}-defined color active? */
260 static int user_bgcolorp = 0;
262 /* The user ^@bgcolor{}-defined color. */
263 static Color user_bgcolor;
265 /* The last linenumber printed by print_line_number(). */
266 static unsigned int print_line_number_last;
268 /* Registers to store X-coordinates with the ^@savex{} escape.
269 Initially these are uninitialized. */
270 static double xstore[256];
282 /* Dump PS header only once. */
283 if (ps_header_dumped)
285 ps_header_dumped = 1;
291 OUTPUT ((cofp, "%s\n", output_first_line));
292 OUTPUT ((cofp, "%%%%BoundingBox: %d %d %d %d\n", media->llx, media->lly,
293 media->urx, media->ury));
294 OUTPUT ((cofp, "%%%%Title: %s\n", title));
295 OUTPUT ((cofp, "%%%%For: %s\n", passwd->pw_gecos));
296 OUTPUT ((cofp, "%%%%Creator: %s\n", version_string));
297 OUTPUT ((cofp, "%%%%CreationDate: %s\n", date_string));
298 OUTPUT ((cofp, "%%%%Orientation: %s\n",
299 ((nup > 1) && nup_landscape)
300 || ((nup == 1) && landscape) ? "Landscape" : "Portrait"));
301 OUTPUT ((cofp, "%%%%Pages: (atend)\n"));
302 OUTPUT ((cofp, "%%%%DocumentMedia: %s %d %d 0 () ()\n",
303 media->name, media->w, media->h));
304 OUTPUT ((cofp, "%%%%DocumentNeededResources: (atend)\n"));
306 if (count_key_value_set (pagedevice) > 0)
307 OUTPUT ((cofp, "%%%%LanguageLevel: 2\n"));
309 OUTPUT ((cofp, "%%%%EndComments\n"));
313 * Procedure Definitions.
316 OUTPUT ((cofp, "%%%%BeginProlog\n"));
319 OUTPUT ((cofp, "%%%%BeginResource: procset Enscript-Prolog %s\n",
321 if (!paste_file ("enscript", ".pro"))
322 FATAL ((stderr, _("couldn't find prolog \"%s\": %s\n"), "enscript.pro",
324 OUTPUT ((cofp, "%%%%EndResource\n"));
326 /* Encoding vector. */
327 OUTPUT ((cofp, "%%%%BeginResource: procset Enscript-Encoding-%s %s\n",
328 encoding_name, ps_version_string));
329 if (!paste_file (encoding_name, ".enc"))
330 FATAL ((stderr, _("couldn't find encoding file \"%s.enc\": %s\n"),
331 encoding_name, strerror (errno)));
332 OUTPUT ((cofp, "%%%%EndResource\n"));
334 OUTPUT ((cofp, "%%%%EndProlog\n"));
341 OUTPUT ((cofp, "%%%%BeginSetup\n"));
343 /* Download fonts. */
344 for (got = strhash_get_first (download_fonts, &cp, &j, (void **) &cp2); got;
345 got = strhash_get_next (download_fonts, &cp, &j, (void **) &cp2))
348 /* For each required font, emit %%IncludeResouce comment. */
349 for (got = strhash_get_first (res_fonts, &cp, &j, (void **) &cp2); got;
350 got = strhash_get_next (res_fonts, &cp, &j, (void **) &cp2))
351 OUTPUT ((cofp, "%%%%IncludeResource: font %s\n", cp));
353 OUTPUT ((cofp, "/HFpt_w %g def\n", HFpt.w));
354 OUTPUT ((cofp, "/HFpt_h %g def\n", HFpt.h));
357 /* Select our fonts. */
359 /* Header font HF. */
360 OUTPUT ((cofp, "/%s /HF-gs-font MF\n", HFname));
362 "/HF /HF-gs-font findfont [HFpt_w 0 0 HFpt_h 0 0] makefont def\n"));
364 /* Our default typing font F. */
365 OUTPUT ((cofp, "/%s /F-gs-font MF\n", Fname));
366 OUTPUT ((cofp, "/F-gs-font %g %g SF\n", Fpt.w, Fpt.h));
369 if (underlay != NULL)
371 OUTPUT ((cofp, "/ul_str (%s) def\n", underlay));
372 OUTPUT ((cofp, "/ul_w_ptsize %g def\n", ul_ptsize.w));
373 OUTPUT ((cofp, "/ul_h_ptsize %g def\n", ul_ptsize.h));
374 OUTPUT ((cofp, "/ul_gray %g def\n", ul_gray));
375 OUTPUT ((cofp, "/ul_x %g def\n", ul_x));
376 OUTPUT ((cofp, "/ul_y %g def\n", ul_y));
377 OUTPUT ((cofp, "/ul_angle %g def\n", ul_angle));
378 OUTPUT ((cofp, "/ul_style %d def\n", ul_style));
379 OUTPUT ((cofp, "/%s /F-ul-font MF\n", ul_font));
380 OUTPUT ((cofp, "/ul_font /F-ul-font findfont \
381 [ul_w_ptsize 0 0 ul_h_ptsize 0 0] makefont def\n"));
384 /* Number of copies. */
385 OUTPUT ((cofp, "/#copies %d def\n", num_copies));
389 OUTPUT ((cofp, "true page_prefeed\n"));
391 /* Statusdict definitions. */
392 if (count_key_value_set (statusdict) > 0)
394 OUTPUT ((cofp, "%% Statustdict definitions:\nstatusdict begin\n "));
396 for (got = strhash_get_first (statusdict, &cp, &j, (void **) &cp2); got;
397 got = strhash_get_next (statusdict, &cp, &j, (void **) &cp2))
399 j = strlen (cp) + 1 + strlen (cp2) + 1;
400 if (i + j > RESOURCE_LINE_WIDTH)
402 OUTPUT ((cofp, "\n "));
405 OUTPUT ((cofp, "%s %s ", cp2, cp));
408 OUTPUT ((cofp, "\nend\n"));
411 /* Page device definitions. */
413 (count_key_value_set (pagedevice) > 0 || generate_PageSize))
415 OUTPUT ((cofp, "%% Pagedevice definitions:\n"));
416 OUTPUT ((cofp, "gs_languagelevel 1 gt {\n <<\n "));
419 for (got = strhash_get_first (pagedevice, &cp, &j, (void **) &cp2); got;
420 got = strhash_get_next (pagedevice, &cp, &j, (void **) &cp2))
422 j = strlen (cp2) + 1 + strlen (cp) + 2;
423 if (i + j > RESOURCE_LINE_WIDTH)
425 OUTPUT ((cofp, "\n "));
428 OUTPUT ((cofp, "/%s %s ", cp, cp2));
432 if (generate_PageSize)
434 if (i + 21 > RESOURCE_LINE_WIDTH)
436 OUTPUT ((cofp, "\n "));
439 OUTPUT ((cofp, "/PageSize [%d %d] ", media->w, media->h));
443 OUTPUT ((cofp, "\n >> setpagedevice\n} if\n"));
447 * Dump header procset. Header must come after all font inclusions
448 * and enscript's dynamic state definition.
450 if (header != HDR_NONE)
453 if (header == HDR_SIMPLE)
456 hdr = fancy_header_name;
458 OUTPUT ((cofp, "%%%%BeginResource: procset Enscript-Header-%s %s\n",
459 hdr, ps_version_string));
460 if (!paste_file (hdr, ".hdr"))
462 _("couldn't find header definition file \"%s.hdr\": %s\n"),
463 hdr, strerror (errno)));
464 OUTPUT ((cofp, "%%%%EndResource\n"));
468 * Count output width and height here; we can't do it earlier because
469 * header might have just allocated some extra space.
471 d_output_w = d_page_w;
472 d_output_h = d_page_h - d_header_h - d_footer_h;
474 /* Dump our current dynamic state. */
475 OUTPUT ((cofp, "/d_page_w %d def\n", d_page_w));
476 OUTPUT ((cofp, "/d_page_h %d def\n", d_page_h));
478 OUTPUT ((cofp, "/d_header_x %d def\n", 0));
479 OUTPUT ((cofp, "/d_header_y %d def\n", d_output_h + d_footer_h));
480 OUTPUT ((cofp, "/d_header_w %d def\n", d_header_w));
481 OUTPUT ((cofp, "/d_header_h %d def\n", d_header_h));
483 OUTPUT ((cofp, "/d_footer_x %d def\n", 0));
484 OUTPUT ((cofp, "/d_footer_y %d def\n", 0));
485 OUTPUT ((cofp, "/d_footer_w %d def\n", d_header_w));
486 OUTPUT ((cofp, "/d_footer_h %d def\n", d_footer_h));
488 OUTPUT ((cofp, "/d_output_w %d def\n", d_output_w));
489 OUTPUT ((cofp, "/d_output_h %d def\n", d_output_h));
490 OUTPUT ((cofp, "/cols %d def\n", num_columns));
492 OUTPUT ((cofp, "%%%%EndSetup\n"));
502 unsigned int nup_subpage;
504 if (!ps_header_dumped)
505 /* No header, let's be consistent and forget trailer also. */
508 /* The possible pending N-up showpage. */
509 nup_subpage = (total_pages - 1) % nup;
510 if (nup > 1 && nup_subpage + 1 != nup)
511 /* N-up showpage missing. */
512 OUTPUT ((cofp, "_R\nS\n"));
516 OUTPUT ((cofp, "%%%%Trailer\n"));
519 OUTPUT ((cofp, "false page_prefeed\n"));
521 OUTPUT ((cofp, "%%%%Pages: %d\n", total_pages));
523 /* Document needed resources. */
526 OUTPUT ((cofp, "%%%%DocumentNeededResources: font "));
527 i = 32; /* length of the previous string. */
528 for (got = strhash_get_first (res_fonts, &cp, &j, &value); got;
529 got = strhash_get_next (res_fonts, &cp, &j, &value))
531 if (i + strlen (cp) + 1 > RESOURCE_LINE_WIDTH)
533 OUTPUT ((cofp, "\n%%%%+ font "));
534 i = 9; /* length of the previous string. */
536 OUTPUT ((cofp, "%s ", cp));
537 i += strlen (cp) + 1;
539 OUTPUT ((cofp, "\n%%%%EOF\n"));
544 process_file (char *fname_arg, InputStream *is, int is_toc)
549 double linewidth; /* Line width in points. */
553 unsigned int line_column;
554 unsigned int current_linenum;
555 double linenumber_space = 0;
556 double linenumber_margin = 0;
558 int reuse_last_token = 0;
559 unsigned int current_slice = 1;
560 int last_wrapped_line = -1;
561 int last_spaced_file_linenum = -1;
562 int save_current_pagenum;
567 fname = xstrdup (fname_arg);
569 /* Init page number and line counters. */
570 if (!continuous_page_numbers)
572 total_pages_in_file = 0;
573 current_file_linenum = start_line_number;
576 * Count possible line number spaces. This should be enought for 99999
579 linenumber_space = CHAR_WIDTH ('0') * 5 + 1.0;
580 linenumber_margin = CHAR_WIDTH (':') + CHAR_WIDTH ('m');
582 /* We got a new input file. */
585 /* We haven't printed any line numbers yet. */
586 print_line_number_last = (unsigned int) -1;
588 if (pass_through || output_language_pass_through)
589 if (do_pass_through (fname, is))
593 /* We have work to do, let's give header a chance to dump itself. */
597 * Align files to the file_align boundary, this is handy for two-side
600 while ((total_pages % file_align) != 0)
606 MESSAGE (1, (stderr, _("processing file \"%s\"...\n"), fname));
608 linewidth = d_output_w / num_columns - 2 * d_output_x_margin
611 /* Save the current running page number for possible toc usage. */
612 first_pagenum_for_file = total_pages + 1;
615 * Divert our output to a temp file. We will re-process it
616 * afterwards to patch, for example, the number of pages in the
621 /* Process this input file. */
624 /* Start a new page. */
627 for (col = 0; !done && col < num_columns; col++)
629 /* Move to the beginning of the column <col>. */
630 lx = x = col * d_output_w / (float) num_columns + d_output_x_margin
632 lineend = lx + linewidth;
634 ly = y = d_footer_h + d_output_h - d_output_y_margin - LINESKIP;
640 if (line_numbers && line_column == 0
641 && (current_file_linenum != last_spaced_file_linenum))
643 /* Forward x by the amount needed by our line numbers. */
644 x += linenumber_space + linenumber_margin;
645 last_spaced_file_linenum = current_file_linenum;
649 if (!reuse_last_token)
650 get_next_token (is, lx, x, line_column, lineend, &token);
651 reuse_last_token = 0;
654 * Page header printing is delayed to this point because
655 * we want to handle files ending with a newline character
656 * with care. If the last newline would cause a pagebreak,
657 * otherwise we would print page header to the non-existent
658 * next page and that would be ugly ;)
661 if (token.type == tEOF)
668 * Now we know that we are going to make marks to this page
669 * => print page header.
677 total_pages_in_file++;
679 /* Check page ranges. */
680 if (page_ranges == NULL)
685 for (pr = page_ranges; pr; pr = pr->next)
687 if (pr->odd || pr->even)
689 if ((pr->odd && (current_pagenum % 2) == 1)
690 || (pr->even && (current_pagenum % 2) == 0))
698 if (pr->start <= current_pagenum
699 && current_pagenum <= pr->end)
713 save_current_pagenum = current_pagenum;
715 current_pagenum = toc_pagenum;
718 dump_ps_page_header (fname, 0);
722 current_pagenum = save_current_pagenum;
725 /* Print line highlight. */
726 if (line_column == 0 && line_highlight_gray < 1.0)
727 OUTPUT ((cofp, "%g %g %g %g %g line_highlight\n",
728 lx, (y - baselineskip
729 + (font_bbox_lly * Fpt.h / UNITS_PER_POINT)),
730 linewidth, Fpt.h + baselineskip,
731 line_highlight_gray));
733 /* Print line numbers if needed. */
734 if (line_numbers && line_column == 0 && token.type != tFORMFEED)
735 print_line_number (lx, y, linenumber_space, linenumber_margin,
736 current_file_linenum);
738 /* Check rest of tokens. */
742 switch (formfeed_type)
744 case FORMFEED_COLUMN:
752 case FORMFEED_HCOLUMN:
754 * Advance y-coordinate to the next even
755 * `horizontal_column_height' position.
760 current_row = (ly - y) / horizontal_column_height;
761 y = ly - (current_row + 1) * horizontal_column_height;
763 /* Check the end of the page. */
764 if (y < d_footer_h + d_output_y_margin)
772 if (CORRECT_SLICE ())
776 OUTPUT ((cofp, "%g %g %g %g %g (%s) bgs\n", x, y,
777 Fpt.h + baselineskip,
779 - (font_bbox_lly * Fpt.h / UNITS_PER_POINT),
783 else if (user_bgcolorp)
785 OUTPUT ((cofp, "%g %g %g %g %g %g %g (%s) bgcs\n",
786 x, y, Fpt.h + baselineskip,
788 - (font_bbox_lly * Fpt.h / UNITS_PER_POINT),
796 OUTPUT ((cofp, "%g %g M\n(%s) s\n", x, y,
801 line_column = token.new_col;
804 case tCARRIAGE_RETURN:
805 /* Just reset the x-coordinate. */
806 x = col * d_output_w / (float) num_columns
807 + d_output_x_margin + line_indent;
812 case tWRAPPED_NEWLINE:
813 if (token.type == tNEWLINE)
815 current_file_linenum++;
824 /* Mark wrapped line marks. */
825 switch (mark_wrapped_lines_style)
832 OUTPUT ((cofp, "%g %g M (+) s\n", x, y));
836 /* Print some fancy graphics. */
838 "%g %g %g %g %d wrapped_line_mark\n",
840 mark_wrapped_lines_style));
845 * For wrapped newlines, decrement y only if
846 * we are not slicing the input.
851 /* Count the wrapped lines here. */
852 if (!slicing || current_slice > slice)
853 if (current_file_linenum != last_wrapped_line)
856 num_truncated_lines++;
857 last_wrapped_line = current_file_linenum;
862 if (current_linenum >= lines_per_page
863 || y < d_footer_h + d_output_y_margin)
866 x = col * d_output_w / (float) num_columns
867 + d_output_x_margin + line_indent;
872 /* Count current point movement. */
874 if (token.flags & F_EPSF_ABSOLUTE_Y)
878 token.new_y += token.u.epsf.y - token.u.epsf.h;
880 if (token.flags & F_EPSF_ABSOLUTE_X)
884 token.new_x += token.u.epsf.x;
888 /* Justification flags overwrite <x_ofs>. */
889 if (token.flags & F_EPSF_CENTER)
890 token.new_x = lx + (linewidth - token.u.epsf.w) / 2;
891 if (token.flags & F_EPSF_RIGHT)
892 token.new_x = lx + (linewidth - token.u.epsf.w);
894 /* Check if eps file does not fit to this column. */
895 if ((token.flags & F_EPSF_NO_CPOINT_UPDATE_Y) == 0
896 && token.new_y < d_footer_h + d_output_y_margin)
898 if (current_linenum == 0)
901 * At the beginning of the column, warn user
904 MESSAGE (0, (stderr, _("EPS file \"%s\" is too \
906 token.u.epsf.filename));
910 /* Must start a new column. */
911 reuse_last_token = 1;
917 if (CORRECT_SLICE ())
920 /* Update current point? */
921 if (!(token.flags & F_EPSF_NO_CPOINT_UPDATE_Y))
923 if (!(token.flags & F_EPSF_NO_CPOINT_UPDATE_X))
924 x = token.new_x + token.u.epsf.w;
926 if (y < d_footer_h + d_output_y_margin)
931 /* Select a new current font. */
932 if (line_column == 0)
936 /* Check for possible line skip change. */
937 if (token.u.font.name[0] == '\0')
938 newh = default_Fpt.h;
940 newh = token.u.font.size.h;
944 /* We need a different line skip value. */
948 * We must check for page overflow after we have
953 MESSAGE (2, (stderr, "^@font="));
954 if (token.u.font.name[0] == '\0')
956 /* Select the default font. */
957 Fpt.w = default_Fpt.w;
958 Fpt.h = default_Fpt.h;
959 Fname = default_Fname;
960 encoding = default_Fencoding;
961 OUTPUT ((cofp, "/F-gs-font %g %g SF\n", Fpt.w, Fpt.h));
966 strhash_put (res_fonts, token.u.font.name,
967 strlen (token.u.font.name) + 1,
969 if (token.u.font.encoding == default_Fencoding)
970 OUTPUT ((cofp, "/%s %g %g SUF\n", token.u.font.name,
971 token.u.font.size.w, token.u.font.size.h));
972 else if (token.u.font.encoding == ENC_PS)
973 OUTPUT ((cofp, "/%s %g %g SUF_PS\n", token.u.font.name,
974 token.u.font.size.w, token.u.font.size.h));
977 _("user font encoding can be only the system's default or `ps'")));
979 strcpy (user_font_name, token.u.font.name);
980 user_font_pt.w = token.u.font.size.w;
981 user_font_pt.h = token.u.font.size.h;
982 user_font_encoding = token.u.font.encoding;
985 Fpt.w = user_font_pt.w;
986 Fpt.h = user_font_pt.h;
987 Fname = user_font_name;
988 encoding = user_font_encoding;
990 MESSAGE (2, (stderr, "%s %g/%gpt\n", Fname, Fpt.w, Fpt.h));
994 * Check for page overflow in that case that we were
995 * at the first column and font were changed to a bigger
998 if (y < d_footer_h + d_output_y_margin)
1003 /* Select a new color. */
1004 MESSAGE (2, (stderr, "^@color{%f %f %f}\n",
1008 if (token.u.color.r == token.u.color.g
1009 && token.u.color.g == token.u.color.b
1010 && token.u.color.b == 0.0)
1012 /* Select the default color (black). */
1013 OUTPUT ((cofp, "0 setgray\n"));
1018 OUTPUT ((cofp, "%g %g %g setrgbcolor\n",
1023 user_color.r = token.u.color.r;
1024 user_color.g = token.u.color.g;
1025 user_color.b = token.u.color.b;
1031 /* Select a new background color. */
1032 MESSAGE (2, (stderr, "^@bgcolor{%f %f %f}\n",
1037 if (token.u.color.r == token.u.color.g
1038 && token.u.color.g == token.u.color.b
1039 && token.u.color.b == 1.0)
1041 /* Select the default bgcolor (white). */
1046 user_bgcolor.r = token.u.color.r;
1047 user_bgcolor.g = token.u.color.g;
1048 user_bgcolor.b = token.u.color.b;
1055 fname = xstrdup (token.u.filename);
1058 case tSETPAGENUMBER:
1059 current_pagenum = token.u.i - 1;
1063 if (current_linenum >= token.u.i)
1068 xstore[(unsigned char) token.u.i] = x;
1072 x = xstore[(unsigned char) token.u.i];
1076 OUTPUT ((cofp, "%g %g M\n%s\n", x, y, token.u.str));
1077 xfree (token.u.str);
1082 FATAL ((stderr, "process_file(): got illegal token %d",
1088 ; /* ULTRIX's cc needs this line. */
1093 dump_ps_page_trailer ();
1097 * Reset print flag to true so all the required document trailers
1098 * etc. get printed properly.
1102 /* Undivert our output from the temp file to our output stream. */
1105 /* Table of contents? */
1109 int save_total_pages = total_pages;
1111 /* use first pagenum in file for toc */
1112 total_pages = first_pagenum_for_file;
1114 cp = format_user_string ("TOC", toc_fmt_string);
1115 fprintf (toc_fp, "%s\n", cp);
1118 total_pages = save_total_pages;
1129 /* Check if character <ch> fits to current line. */
1130 #define FITS_ON_LINE(ch) ((linepos + CHAR_WIDTH (ch) < linew) || col == 0)
1132 /* Is line buffer empty? */
1133 #define BUFFER_EMPTY() (bufpos == 0)
1135 /* Unconditionally append character <ch> to the line buffer. */
1136 #define APPEND_CHAR(ch) \
1138 if (bufpos >= buflen) \
1141 buffer = xrealloc (buffer, buflen); \
1143 buffer[bufpos++] = ch; \
1147 * Copy character <ch> (it fits to this line) to output buffer and
1148 * update current point counters.
1153 linepos += CHAR_WIDTH (ch); \
1157 #define UNEMIT(ch) \
1159 linepos -= CHAR_WIDTH (ch); \
1163 #define ISSPACE(ch) ((ch) == ' ' || (ch) == '\t')
1164 #define ISOCTAL(ch) ('0' <= (ch) && (ch) <= '7')
1166 /* Read one special escape from input <fp>. */
1171 SpecialEscape escape;
1174 {"comment", ESC_COMMENT},
1177 {"color", ESC_COLOR},
1178 {"bgcolor", ESC_BGCOLOR},
1179 {"newpage", ESC_NEWPAGE},
1181 {"setfilename", ESC_SETFILENAME},
1182 {"setpagenumber", ESC_SETPAGENUMBER},
1183 {"shade", ESC_SHADE},
1184 {"bggray", ESC_BGGRAY},
1185 {"escape", ESC_ESCAPE},
1186 {"savex", ESC_SAVEX},
1187 {"loadx", ESC_LOADX},
1193 read_special_escape (InputStream *is, Token *token)
1200 /* Get escape name. */
1201 for (i = 0; i < sizeof (escname) - 1 && (ch = is_getc (is)) != EOF; i++)
1213 /* Lookup escape. */
1214 for (e = 0; escapes[e].name; e++)
1215 if (strcmp (escname, escapes[e].name) == 0)
1217 if (escapes[e].name == NULL)
1218 FATAL ((stderr, _("unknown special escape: %s"), escname));
1221 * The epsf escape takes optional arguments so it must be handled
1224 if (escapes[e].escape == ESC_EPSF)
1231 token->u.epsf.x = 0.0;
1232 token->u.epsf.y = 0.0;
1233 token->u.epsf.h = 0.0;
1234 token->u.epsf.pipe = 0;
1240 while ((ch = is_getc (is)) != EOF && ch != ']')
1244 case 'c': /* center justification */
1245 token->flags &= ~M_EPSF_JUSTIFICATION;
1246 token->flags |= F_EPSF_CENTER;
1249 case 'n': /* no current point update */
1250 /* Check the next character. */
1255 token->flags |= F_EPSF_NO_CPOINT_UPDATE_X;
1259 token->flags |= F_EPSF_NO_CPOINT_UPDATE_Y;
1264 token->flags |= F_EPSF_NO_CPOINT_UPDATE_X;
1265 token->flags |= F_EPSF_NO_CPOINT_UPDATE_Y;
1270 case 'r': /* right justification */
1271 token->flags &= ~M_EPSF_JUSTIFICATION;
1272 token->flags |= F_EPSF_RIGHT;
1276 case 's': /* scale */
1277 /* Check the next character. */
1282 token->flags |= F_EPSF_SCALE_X;
1283 token->u.epsf.xscale = read_float (is, 0, 1);
1287 token->flags |= F_EPSF_SCALE_Y;
1288 token->u.epsf.yscale = read_float (is, 0, 0);
1293 token->flags |= F_EPSF_SCALE_X;
1294 token->flags |= F_EPSF_SCALE_Y;
1295 token->u.epsf.xscale = token->u.epsf.yscale
1296 = read_float (is, 0, 1);
1301 case 'x': /* x-position */
1302 token->u.epsf.x = read_float (is, 1, 1);
1304 /* Check the next character. */
1309 token->flags |= F_EPSF_ABSOLUTE_X;
1318 case 'y': /* y-position */
1319 token->u.epsf.y = - read_float (is, 1, 0);
1321 /* Check the next character. */
1326 token->flags |= F_EPSF_ABSOLUTE_Y;
1335 case 'h': /* height */
1336 token->u.epsf.h = read_float (is, 1, 0);
1344 FATAL ((stderr, _("illegal option %c for ^@epsf escape"),
1350 _("malformed ^@epsf escape: no ']' after options")));
1356 /* Read filename. */
1357 for (i = 0; (ch = is_getc (is)) != EOF && ch != '}'; i++)
1359 token->u.epsf.filename[i] = ch;
1360 if (i + 1 >= sizeof (token->u.epsf.filename))
1362 _("too long file name for ^@epsf escape:\n%.*s"),
1363 i, token->u.epsf.filename));
1366 FATAL ((stderr, _("unexpected EOF while scanning ^@epsf escape")));
1368 token->u.epsf.filename[i] = '\0';
1369 token->type = tEPSF;
1372 FATAL ((stderr, _("malformed ^@epsf escape: no '{' found")));
1375 * Now we have a valid epsf-token in <token>. Let's read BoundingBox
1376 * and do some calculations.
1378 if (!recognize_eps_file (token))
1379 /* Recognize eps has already printed error message so we are done. */
1380 token->type = tNONE;
1383 /* Some fixups for x and y dimensions. */
1384 token->u.epsf.y += LINESKIP - 1;
1385 if (token->u.epsf.h != 0.0)
1386 token->u.epsf.h -= 1.0;
1388 /* Count picture's width and height. */
1390 pw = token->u.epsf.urx - token->u.epsf.llx;
1391 ph = token->u.epsf.ury - token->u.epsf.lly;
1393 /* The default scale. */
1394 if (token->u.epsf.h == 0.0)
1397 scale = token->u.epsf.h / ph;
1399 if ((token->flags & F_EPSF_SCALE_X) == 0)
1400 token->u.epsf.xscale = scale;
1401 if ((token->flags & F_EPSF_SCALE_Y) == 0)
1402 token->u.epsf.yscale = scale;
1404 pw *= token->u.epsf.xscale;
1405 ph *= token->u.epsf.yscale;
1407 token->u.epsf.w = pw;
1408 token->u.epsf.h = ph;
1411 else if (escapes[e].escape == ESC_COMMENT)
1413 /* Comment the rest of this line. */
1414 while ((ch = is_getc (is)) != EOF && ch != nl)
1416 token->type = tNONE;
1424 * Handle the rest of the escapes.
1427 /* Read argument. */
1430 FATAL ((stderr, _("malformed %s escape: no '{' found"),
1435 (ch = is_getc (is)) != EOF && (parenlevel > 0 || ch != '}'); i++)
1443 if (i + 1 >= sizeof (buf))
1444 FATAL ((stderr, _("too long argument for %s escape:\n%.*s"),
1445 escapes[i].name, i, buf));
1449 /* And now handle the escape. */
1450 switch (escapes[e].escape)
1453 strcpy (token->u.font.name, buf);
1455 /* Check for the default font. */
1456 if (strcmp (token->u.font.name, "default") == 0)
1457 token->u.font.name[0] = '\0';
1460 if (!parse_font_spec (token->u.font.name, &cp,
1461 &token->u.font.size,
1462 &token->u.font.encoding))
1463 FATAL ((stderr, _("malformed font spec for ^@font escape: %s"),
1464 token->u.font.name));
1466 strcpy (token->u.font.name, cp);
1469 token->type = tFONT;
1474 /* Check for the default color. */
1475 if (strcmp (buf, "default") == 0)
1479 if (escapes[e].escape == ESC_BGCOLOR)
1482 token->u.color.r = val;
1483 token->u.color.g = val;
1484 token->u.color.b = val;
1490 got = sscanf (buf, "%g %g %g",
1499 _("malformed color spec for ^@%s escape: %s"),
1500 escapes[e].escape == ESC_COLOR
1501 ? "color" : "bgcolor",
1506 token->u.color.g = token->u.color.b = token->u.color.r;
1510 /* Got all three components. */
1514 if (escapes[e].escape == ESC_COLOR)
1515 token->type = tCOLOR;
1517 token->type = tBGCOLOR;
1521 line_highlight_gray = atof (buf);
1522 if (line_highlight_gray < 0.0 || line_highlight_gray > 1.0)
1523 FATAL ((stderr, _("invalid value for ^@shade escape: %s"), buf));
1525 token->type = tNONE;
1529 bggray = atof (buf);
1530 if (bggray < 0.0 || bggray > 1.0)
1531 FATAL ((stderr, _("invalid value for ^@bggray escape: %s"), buf));
1533 token->type = tNONE;
1537 if (strcmp (buf, "default") == 0)
1538 escape_char = default_escape_char;
1540 escape_char = atoi (buf);
1541 token->type = tNONE;
1544 case ESC_SETFILENAME:
1545 strcpy (token->u.filename, buf);
1546 token->type = tSETFILENAME;
1549 case ESC_SETPAGENUMBER:
1550 token->u.i = atoi (buf);
1551 token->type = tSETPAGENUMBER;
1556 token->u.i = 1; /* The default is the first line. */
1558 token->u.i = atoi (buf);
1559 token->type = tNEWPAGE;
1563 token->type = tSAVEX;
1564 token->u.i = atoi (buf);
1568 token->type = tLOADX;
1569 token->u.i = atoi (buf);
1573 token->u.str = xstrdup (buf);
1586 /* Get next token from input file <fp>. */
1588 get_next_token (InputStream *is, double linestart, double linepos,
1589 unsigned int col, double linew, Token *token)
1591 static unsigned char *buffer = NULL; /* output buffer */
1592 static unsigned int buflen = 0; /* output buffer's length */
1593 unsigned int bufpos = 0; /* current position in output buffer */
1597 static int pending_token = tNONE;
1598 unsigned int original_col = col;
1600 if (pending_token != tNONE)
1602 token->type = pending_token;
1603 pending_token = tNONE;
1616 if (BUFFER_EMPTY ())
1628 * One of these is the newline character and the other one
1629 * is carriage return.
1633 /* The newline character. */
1634 if (BUFFER_EMPTY ())
1636 token->type = tNEWLINE;
1647 /* The carriage return character. */
1648 if (BUFFER_EMPTY ())
1650 token->type = tCARRIAGE_RETURN;
1664 i = tabsize - (col % tabsize);
1667 if (FITS_ON_LINE (' '))
1678 /* Proportional font. */
1680 double grid = tabsize * CHAR_WIDTH (' ');
1683 /* Move linepos to the next multiple of <grid>. */
1684 linepos = (((int) ((linepos - linestart) / grid) + 1) * grid
1686 if (linepos >= linew)
1694 if (BUFFER_EMPTY ())
1696 if (interpret_formfeed)
1697 token->type = tFORMFEED;
1699 token->type = tNEWLINE;
1710 /* Handle special escapes. */
1711 if (special_escapes && ch == escape_char)
1713 if (BUFFER_EMPTY ())
1715 /* Interpret special escapes. */
1716 read_special_escape (is, token);
1717 if (token->type != tNONE)
1721 * Got tNONE special escape => read_special_escape()
1722 * has already done what was needed. Just read more.
1734 /* Handle backspace character. */
1737 if (BUFFER_EMPTY () || !EXISTS (buffer[bufpos - 1]))
1738 linepos -= CHAR_WIDTH ('m');
1740 linepos -= CHAR_WIDTH (buffer[bufpos - 1]);
1746 /* Check normal characters. */
1749 if (FITS_ON_LINE (ch))
1752 * Print control characters (and optionally
1753 * characters greater than 127) in the escaped form
1754 * so PostScript interpreter will not hang on them.
1756 if (ch < 040 || (clean_7bit && ch >= 0200))
1760 sprintf (buf, "\\%03o", ch);
1761 for (i = 0; buf[i]; i++)
1762 APPEND_CHAR (buf[i]);
1764 /* Update current point counters manually. */
1765 linepos += CHAR_WIDTH (ch);
1768 else if (ch == '(' || ch == ')' || ch == '\\')
1770 /* These must be quoted in PostScript strings. */
1783 else if (ISPRINT (ch))
1785 /* Printable, but do not exists in this font. */
1786 if (FITS_ON_LINE ('?'))
1789 if (missing_chars[ch]++ == 0)
1790 num_missing_chars++;
1804 * Non-printable and does not exist in current font, print
1805 * it in the format specified by non_printable_format.
1808 if (non_printable_chars[ch]++ == 0)
1809 num_non_printable_chars++;
1811 switch (non_printable_format)
1817 case NPF_QUESTIONMARK:
1832 sprintf (buf, "\\%03o", ch);
1837 for (i = 0; buf[i]; i++)
1838 len += CHAR_WIDTH (buf[i]);
1840 if (linepos + len < linew || col == 0)
1843 for (i = 0; buf[i]; i++)
1846 APPEND_CHAR ('\\'); /* Escape '\\' characters. */
1862 /* Check for wrapped line. */
1863 if (done == DONE_WRAP)
1865 /* This line is too long. */
1867 if (line_end == LE_TRUNCATE)
1869 /* Truncate this line. */
1870 while ((ch = is_getc (is)) != EOF && ch != nl)
1873 else if (!BUFFER_EMPTY () && line_end == LE_WORD_WRAP)
1877 if (ISSPACE (buffer[bufpos - 1]))
1879 /* Skip all whitespace from the end of the wrapped line. */
1880 while ((w = is_getc (is)) != EOF && ISSPACE (w))
1886 /* Find the previous word boundary for the wrap. */
1887 for (w = bufpos - 1; w >= 0 && !ISSPACE (buffer[w]); w--)
1890 if (w > 0 || original_col > 0)
1893 * Ok, we found a word boundary. Now we must unemit
1894 * characters from the buffer to the intput stream.
1897 * - bufpos is unsigned integer variable
1898 * - some characters are escaped with '\\'
1899 * - some characters are printed in octal notation
1905 /* Check for '(', ')' and '\\'. */
1907 && (buffer[bufpos] == '('
1908 || buffer[bufpos] == ')'
1909 || buffer[bufpos] == '\\')
1910 && buffer[bufpos - 1] == '\\')
1912 is_ungetc (buffer[bufpos], is);
1913 UNEMIT (buffer[bufpos]);
1916 /* Check the octal notations "\\%03o". */
1917 else if (bufpos - 2 > w
1918 && ISOCTAL (buffer[bufpos])
1919 && ISOCTAL (buffer[bufpos - 1])
1920 && ISOCTAL (buffer[bufpos - 2])
1921 && buffer[bufpos - 3] == '\\')
1926 * It is a potential octal character. Now we
1927 * must process the buffer from the beginning
1928 * and see if `bufpos - 3' really starts a character.
1930 for (ti = w; ti < bufpos - 3; ti++)
1932 if (buffer[ti] == '\\')
1934 if (ISOCTAL (buffer[ti + 1]))
1939 tti < 3 && ISOCTAL (buffer[ti + 1]);
1944 /* Simple escape. */
1950 * If <ti> is equal to <bufpos - 3>, we found
1951 * an octal character, otherwise the leading
1952 * backslash at <bufpos - 3> belongs to the
1953 * previous character.
1955 if (ti == bufpos - 3)
1959 tch = (((buffer[bufpos - 2] - '0') << 6)
1960 + ((buffer[bufpos - 1] - '0') << 3)
1961 + (buffer[bufpos] - '0'));
1962 is_ungetc (tch, is);
1967 /* Normal character. */
1972 /* Normal character, just unget it. */
1974 is_ungetc (buffer[bufpos], is);
1975 UNEMIT (buffer[bufpos]);
1985 if (line_end == LE_TRUNCATE)
1988 num_truncated_lines++;
1989 pending_token = tNEWLINE;
1992 pending_token = tWRAPPED_NEWLINE;
1995 pending_token = tEOF;
1999 token->type = tSTRING;
2000 token->u.str = (char *) buffer;
2001 token->new_x = linepos;
2002 token->new_col = col;
2007 dump_ps_page_header (char *fname, int empty)
2014 unsigned int nup_subpage;
2016 /* The N-up printing sub-page. */
2017 nup_subpage = (total_pages - 1) % nup;
2019 /* Create fdir and ftail. */
2020 ftail = strrchr (fname, '/');
2024 ftail = strrchr (fname, '\\');
2035 strncpy (buf, fname, ftail - fname);
2036 buf[ftail - fname] = '\0';
2041 /* N-up printing is active. */
2044 if (nup_subpage == 0)
2046 /* This is a real page start. */
2051 OUTPUT ((cofp, "%%%%Page: (%d-%d) %d\n", current_pagenum,
2052 current_pagenum + nup - 1, total_pages / nup + 1));
2056 OUTPUT ((cofp, "%%%%Page: (%s:%3d-%3d) %d\n", ftail,
2057 current_pagenum, current_pagenum + nup - 1,
2058 total_pages / nup + 1));
2063 OUTPUT ((cofp, "%%%%BeginPageSetup\n_S\n"));
2065 if ((total_pages / nup + 1) % 2 == 0)
2066 /* Two-side binding options for the even pages. */
2067 handle_two_side_options ();
2069 #define PRINT_BOUNDING_BOXES 0
2071 #if PRINT_BOUNDING_BOXES
2073 "%d %d moveto %d %d lineto %d %d lineto %d %d lineto closepath stroke\n",
2074 media->llx, media->lly, media->llx, media->ury,
2075 media->urx, media->ury, media->urx, media->lly));
2081 OUTPUT ((cofp, "90 rotate\n%d %d translate\n",
2082 media->lly, -media->urx));
2084 OUTPUT ((cofp, "%d %d translate\n", media->llx, media->lly));
2089 OUTPUT ((cofp, "90 rotate\n%d %d translate\n",
2090 media->lly, -media->llx));
2092 OUTPUT ((cofp, "%d %d translate\n", media->llx, media->ury));
2097 /* Page start comment. */
2101 OUTPUT ((cofp, "%sPage: (%d) %d\n", cstr, current_pagenum, total_pages));
2105 OUTPUT ((cofp, "%sPage: (%s:%3d) %d\n", cstr, ftail, current_pagenum,
2114 OUTPUT ((cofp, "%sBeginPageSetup\n_S\n", cstr));
2120 OUTPUT ((cofp, "%% N-up sub-page %d/%d\n", nup_subpage + 1, nup));
2125 xm = nup_subpage % nup_columns;
2126 ym = nup_subpage / nup_columns;
2130 xm = nup_subpage / nup_rows;
2131 ym = nup_subpage % nup_rows;
2134 OUTPUT ((cofp, "%d %d translate\n",
2135 xm * (nup_width + nup_xpad),
2136 ym * (nup_height + nup_ypad)));
2142 xm = nup_subpage / nup_rows;
2143 ym = nup_subpage % nup_rows;
2147 xm = nup_subpage % nup_columns;
2148 ym = nup_subpage / nup_columns;
2151 OUTPUT ((cofp, "%d %d translate\n",
2152 xm * (nup_width + nup_xpad),
2153 -((int) (ym * (nup_height + nup_ypad) + nup_height))));
2155 OUTPUT ((cofp, "%g dup scale\n", nup_scale));
2157 /* And finally, the real page setup. */
2159 OUTPUT ((cofp, "90 rotate\n%d %d translate\n", 0, -d_page_h));
2163 /* No N-up printing. */
2165 if (total_pages % 2 == 0)
2166 /* Two-side binding options for the even pages. */
2167 handle_two_side_options ();
2170 OUTPUT ((cofp, "90 rotate\n%d %d translate\n",
2171 media->lly, -media->urx));
2173 OUTPUT ((cofp, "%d %d translate\n", media->llx, media->lly));
2176 /* Some constants etc. */
2177 OUTPUT ((cofp, "/pagenum %d def\n", current_pagenum));
2179 cp = escape_string (fname);
2180 OUTPUT ((cofp, "/fname (%s) def\n", cp));
2183 cp = escape_string (buf);
2184 OUTPUT ((cofp, "/fdir (%s) def\n", cp));
2187 cp = escape_string (ftail);
2188 OUTPUT ((cofp, "/ftail (%s) def\n", cp));
2191 /* Do we have a pending ^@font{} font? */
2194 if (encoding == default_Fencoding)
2195 OUTPUT ((cofp, "/%s %g %g SUF\n", Fname, Fpt.w, Fpt.h));
2197 /* This must be the case. */
2198 OUTPUT ((cofp, "/%s %g %g SUF_PS\n", Fname, Fpt.w, Fpt.h));
2201 /* Dump user defined strings. */
2202 if (count_key_value_set (user_strings) > 0)
2204 OUTPUT ((cofp, "%% User defined strings:\n"));
2205 for (got = strhash_get_first (user_strings, &cp, &i, (void **) &cp2);
2207 got = strhash_get_next (user_strings, &cp, &i, (void **) &cp2))
2209 cp2 = format_user_string ("%Format", cp2);
2210 OUTPUT ((cofp, "/%s (%s) def\n", cp, cp2));
2215 /* User supplied header? */
2220 char *h_right = NULL;
2222 h_left = format_user_string ("page header", page_header);
2223 h_center = strchr (h_left, '|');
2229 h_right = strchr (h_center, '|');
2237 OUTPUT ((cofp, "/user_header_p true def\n"));
2238 OUTPUT ((cofp, "/user_header_left_str (%s) def\n", h_left));
2239 OUTPUT ((cofp, "/user_header_center_str (%s) def\n",
2240 h_center ? h_center : ""));
2241 OUTPUT ((cofp, "/user_header_right_str (%s) def\n",
2242 h_right ? h_right : ""));
2246 OUTPUT ((cofp, "/user_header_p false def\n"));
2248 /* User supplied footer? */
2253 char *f_right = NULL;
2255 f_left = format_user_string ("page footer", page_footer);
2256 f_center = strchr (f_left, '|');
2262 f_right = strchr (f_center, '|');
2270 OUTPUT ((cofp, "/user_footer_p true def\n"));
2271 OUTPUT ((cofp, "/user_footer_left_str (%s) def\n", f_left));
2272 OUTPUT ((cofp, "/user_footer_center_str (%s) def\n",
2273 f_center ? f_center : ""));
2274 OUTPUT ((cofp, "/user_footer_right_str (%s) def\n",
2275 f_right ? f_right : ""));
2279 OUTPUT ((cofp, "/user_footer_p false def\n"));
2281 OUTPUT ((cofp, "%%%%EndPageSetup\n"));
2284 * Mark standard page decorations.
2289 /* Highlight bars. */
2291 OUTPUT ((cofp, "%d %f %d %f highlight_bars\n", highlight_bars,
2292 LINESKIP, d_output_y_margin, highlight_bar_gray));
2295 if (underlay != NULL)
2297 if (ul_position_p || ul_angle_p)
2298 OUTPUT ((cofp, "user_underlay\n"));
2300 OUTPUT ((cofp, "underlay\n"));
2304 if (num_columns > 1 && (header == HDR_FANCY || borders))
2305 OUTPUT ((cofp, "column_lines\n"));
2307 /* Borders around columns. */
2309 OUTPUT ((cofp, "column_borders\n"));
2319 OUTPUT ((cofp, "do_header\n"));
2324 /* Do we have a pending ^@color{} color? */
2326 OUTPUT ((cofp, "%g %g %g setrgbcolor\n", user_color.r, user_color.g,
2332 dump_ps_page_trailer ()
2334 unsigned int nup_subpage = (total_pages - 1) % nup;
2336 OUTPUT ((cofp, "_R\n"));
2340 if (nup_subpage + 1 == nup)
2341 /* Real end of page. */
2342 OUTPUT ((cofp, "_R\nS\n"));
2345 OUTPUT ((cofp, "S\n"));
2354 unsigned int nup_subpage = (total_pages - 1) % nup;
2356 if (nup_subpage == 0)
2358 /* Real start of the page, must do it the harder way. */
2359 dump_ps_page_header ("", 1);
2360 OUTPUT ((cofp, "_R\n"));
2363 OUTPUT ((cofp, "%%Page: (-) %d\n", total_pages));
2365 if (nup_subpage + 1 == nup)
2366 /* This is the last page on this sheet, dump us. */
2367 OUTPUT ((cofp, "_R\nS\n"));
2370 OUTPUT ((cofp, "%%%%Page: (-) %d\nS\n", total_pages));
2375 recognize_eps_file (Token *token)
2382 float llx, lly, urx, ury;
2384 MESSAGE (2, (stderr, "^@epsf=\"%s\"\n", token->u.epsf.filename));
2386 i = strlen (token->u.epsf.filename);
2388 /* Read EPS data from file. */
2389 filename = tilde_subst (token->u.epsf.filename);
2391 token->u.epsf.fp = fopen (filename, "rb");
2394 if (token->u.epsf.fp == NULL)
2396 if (token->u.epsf.filename[0] != '/')
2398 /* Name is not absolute, let's lookup path. */
2401 ctx.name = token->u.epsf.filename;
2403 ctx.fullname = buffer_alloc ();
2405 if (pathwalk (libpath, file_lookup, &ctx))
2406 token->u.epsf.fp = fopen (buffer_ptr (ctx.fullname), "rb");
2408 buffer_free (ctx.fullname);
2410 if (token->u.epsf.fp == NULL)
2412 MESSAGE (0, (stderr, _("couldn't open EPS file \"%s\": %s\n"),
2413 token->u.epsf.filename, strerror (errno)));
2418 /* Find BoundingBox DSC comment. */
2422 token->u.epsf.skipbuf = NULL;
2423 token->u.epsf.skipbuf_len = 0;
2424 token->u.epsf.skipbuf_pos = 0;
2426 while (fgets (buf, sizeof (buf), token->u.epsf.fp))
2430 /* Append data to the skip buffer. */
2432 if (i + token->u.epsf.skipbuf_pos >= token->u.epsf.skipbuf_len)
2434 token->u.epsf.skipbuf_len += 8192;
2435 token->u.epsf.skipbuf = xrealloc (token->u.epsf.skipbuf,
2436 token->u.epsf.skipbuf_len);
2438 memcpy (token->u.epsf.skipbuf + token->u.epsf.skipbuf_pos, buf, i);
2439 token->u.epsf.skipbuf_pos += i;
2441 /* Check the "%!" magic cookie. */
2444 if (buf[0] != '%' || buf[1] != '!')
2448 _("EPS file \"%s\" does not start with \"%%!\" magic\n"),
2449 token->u.epsf.filename));
2454 #define BB_DSC "%%BoundingBox:"
2456 if (strncmp (buf, BB_DSC, strlen (BB_DSC)) == 0)
2458 i = sscanf (buf + strlen (BB_DSC), "%f %f %f %f",
2459 &llx, &lly, &urx, &ury);
2464 /* Skip possible whitespace. */
2465 for (i = strlen (BB_DSC);
2466 buf[i] && (buf[i] == ' ' || buf[i] == '\t');
2469 #define BB_DSC_ATEND "(atend)"
2470 if (strncmp (buf + i, BB_DSC_ATEND, strlen (BB_DSC_ATEND)) != 0)
2472 /* No, this BoundingBox comment is corrupted. */
2473 MESSAGE (0, (stderr, _("EPS file \"%s\" contains malformed \
2474 %%%%BoundingBox row:\n\"%.*s\"\n"),
2475 token->u.epsf.filename, strlen (buf) - 1, buf));
2481 /* It was a valid EPS file. */
2483 /* We store bounding box in int format. */
2484 token->u.epsf.llx = llx;
2485 token->u.epsf.lly = lly;
2486 token->u.epsf.urx = urx;
2487 token->u.epsf.ury = ury;
2495 /* Check that we found the BoundingBox comment. */
2498 MESSAGE (0, (stderr, _("EPS file \"%s\" is not a valid EPS file\n"),
2499 token->u.epsf.filename));
2500 if (token->u.epsf.pipe)
2501 pclose (token->u.epsf.fp);
2503 fclose (token->u.epsf.fp);
2504 xfree (token->u.epsf.skipbuf);
2508 MESSAGE (2, (stderr, "BoundingBox: %d %d %d %d\n",
2509 token->u.epsf.llx, token->u.epsf.lly,
2510 token->u.epsf.urx, token->u.epsf.ury));
2517 paste_epsf (Token *token)
2522 /* EPSF import header. */
2523 OUTPUT ((cofp, "BeginEPSF\n"));
2524 OUTPUT ((cofp, "%g %g translate\n", token->new_x, token->new_y));
2525 OUTPUT ((cofp, "%g %g scale\n", token->u.epsf.xscale, token->u.epsf.yscale));
2526 OUTPUT ((cofp, "%d %d translate\n", -token->u.epsf.llx,
2527 -token->u.epsf.lly));
2528 OUTPUT ((cofp, "%d %d %d %d Box clip newpath\n",
2529 token->u.epsf.llx - 1,
2530 token->u.epsf.lly - 1,
2531 token->u.epsf.urx - token->u.epsf.llx + 2,
2532 token->u.epsf.ury - token->u.epsf.lly + 2));
2533 OUTPUT ((cofp, "%%%%BeginDocument: %s%s\n", token->u.epsf.filename,
2534 token->u.epsf.pipe ? "|" : ""));
2538 /* Dump skip buffer. */
2539 fwrite (token->u.epsf.skipbuf, 1, token->u.epsf.skipbuf_pos, cofp);
2542 while ((i = fread (buf, 1, sizeof (buf), token->u.epsf.fp)) != 0)
2543 fwrite (buf, 1, i, cofp);
2546 /* Add a newline to keep comments correct */
2547 OUTPUT ((cofp, "\n"));
2549 /* EPSF import trailer. */
2550 OUTPUT ((cofp, "%%%%EndDocument\nEndEPSF\n"));
2553 if (token->u.epsf.pipe)
2554 pclose (token->u.epsf.fp);
2556 fclose (token->u.epsf.fp);
2557 xfree (token->u.epsf.skipbuf);
2562 read_float (InputStream *is, int units, int horizontal)
2568 for (i = 0; (i < sizeof (buf) - 1
2569 && (ch = is_getc (is)) != EOF
2570 && ISNUMBERDIGIT (ch));
2585 case 'c': /* centimeters */
2589 case 'p': /* PostScript points */
2592 case 'i': /* inches */
2600 case 'l': /* lines or characters */
2602 val *= CHAR_WIDTH ('m');
2613 /* Magics used to recognize different pass-through files. */
2617 unsigned int magiclen;
2620 } pass_through_magics[] =
2622 {"%!", 2, "PostScript", -2},
2623 {"\004%!", 3, "PostScript", -2},
2624 {"\033E", 2, "PCL", -2},
2625 {"\033%", 2, "PCL", -2},
2631 do_pass_through (char *fname, InputStream *is)
2634 unsigned long saved_pos = is->bufpos;
2637 if (output_language_pass_through)
2640 _("passing through all input files for output language `%s'\n"),
2645 * Try to recognize pass-through files.
2648 for (i = 0; pass_through_magics[i].magic; i++)
2650 for (j = 0; j < pass_through_magics[i].magiclen; j++)
2654 || ch != (unsigned char) pass_through_magics[i].magic[j])
2658 if (j >= pass_through_magics[i].magiclen)
2659 /* The <i>th one matched. */
2663 * Try the next one, but first, seek the input stream to its
2666 is->bufpos = saved_pos;
2669 /* Did we find any? */
2670 if (pass_through_magics[i].magic == NULL)
2674 /* Yes, it really is a pass-through file. Now do the pass through. */
2676 is->bufpos += pass_through_magics[i].revert_delta;
2678 if (ps_header_dumped)
2680 /* A pass-through file between normal ASCII files, obey DSC. */
2683 * XXX I don't know how to handle PCL files... Let's hope none
2684 * mixes them with the normal ASCII files.
2688 "%%%%Page: (%s) -1\n_S\n%%%%BeginDocument: %s\n",
2692 MESSAGE (1, (stderr, _("passing through %s file \"%s\"\n"),
2693 pass_through_magics[i].name, fname));
2696 /* And now, do the actual pass-through. */
2699 /* Note: this will be written directly to the <ofp>. */
2700 fwrite (is->buf + is->bufpos, 1, is->data_in_buf - is->bufpos, ofp);
2701 is->bufpos = is->data_in_buf;
2703 /* Read more data to the input buffer. */
2709 if (!output_language_pass_through)
2711 if (ps_header_dumped)
2713 * XXX How to end a PCL file mixed between ASCII files?
2715 OUTPUT ((cofp, "%%%%EndDocument\n_R\n"));
2723 print_line_number (double x, double y, double space, double margin,
2724 unsigned int linenum)
2729 char *saved_Fname = "";
2730 FontPoint saved_Fpt;
2731 InputEncoding saved_Fencoding;
2736 /* Do not print linenumbers for wrapped lines. */
2737 if (linenum == print_line_number_last)
2739 print_line_number_last = linenum;
2743 /* Re-select our default typing font. */
2744 saved_Fname = Fname;
2745 saved_Fpt.w = Fpt.w;
2746 saved_Fpt.h = Fpt.h;
2747 saved_Fencoding = encoding;
2749 Fname = default_Fname;
2750 Fpt.w = default_Fpt.w;
2751 Fpt.h = default_Fpt.h;
2752 encoding = default_Fencoding;
2754 OUTPUT ((cofp, "/F-gs-font %g %g SF\n", Fpt.w, Fpt.h));
2758 /* Count linenumber string length. */
2759 sprintf (buf, "%d", linenum);
2760 for (i = 0; buf[i]; i++)
2761 len += CHAR_WIDTH (buf[i]);
2763 /* Print line numbers. */
2764 OUTPUT ((cofp, "%g %g M (%s:) s\n", x + space - len, y, buf));
2768 /* Switch back to the user font. */
2769 Fname = saved_Fname;
2770 Fpt.w = saved_Fpt.w;
2771 Fpt.h = saved_Fpt.h;
2772 encoding = saved_Fencoding;
2774 OUTPUT ((cofp, "/%s %g %g SUF\n", Fname, Fpt.w, Fpt.h));
2781 * The name of the divert file, shared between divert() and undivert()
2784 static char divertfname[512];
2789 assert (divertfp == NULL);
2791 /* Open divert file. */
2793 divertfp = tmpfile ();
2794 if (divertfp == NULL)
2795 FATAL ((stderr, _("couldn't create temporary divert file: %s"),
2809 assert (divertfp != NULL);
2811 if (fseek (divertfp, 0, SEEK_SET) != 0)
2812 FATAL ((stderr, _("couldn't rewind divert file: %s"), strerror (errno)));
2814 while (fgets (buf, sizeof (buf), divertfp))
2816 if (strncmp (buf, "%%BeginDocument", 15) == 0)
2818 else if (strncmp (buf, "%%EndDocument", 13) == 0)
2823 if (strncmp (buf, "% User defined strings", 22) == 0)
2826 while (fgets (buf, sizeof (buf), divertfp))
2828 if (strncmp (buf, "%%EndPageSetup", 14) == 0)
2831 /* Patch total pages to the user defined strings. */
2832 cp = strchr (buf, '\001');
2837 fprintf (ofp, "%d", total_pages_in_file);
2838 fputs (cp + 1, ofp);
2857 handle_two_side_options ()
2859 if (rotate_even_pages)
2860 /* Rotate page 180 degrees. */
2861 OUTPUT ((cofp, "180 rotate\n%d %d translate\n",
2862 -media->w, -media->h));
2864 if (swap_even_page_margins)
2865 OUTPUT ((cofp, "%d 0 translate\n",
2866 -(media->llx - (media->w - media->urx))));