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/>.
30 * Types and definitions.
33 /* Values for token flags. */
36 #define F_EPSF_CENTER 0x01
37 #define F_EPSF_RIGHT 0x02
38 #define M_EPSF_JUSTIFICATION 0x03
40 #define F_EPSF_NO_CPOINT_UPDATE_X 0x04
41 #define F_EPSF_NO_CPOINT_UPDATE_Y 0x08
43 #define F_EPSF_ABSOLUTE_X 0x10
44 #define F_EPSF_ABSOLUTE_Y 0x20
46 #define F_EPSF_SCALE_X 0x40
47 #define F_EPSF_SCALE_Y 0x80
50 /* Predicate to check if we are at the correct slice. */
51 #define CORRECT_SLICE() (slicing == 0 || current_slice == slice)
53 /* Predicates for the current body font. */
55 /* Is character <ch> printable. */
56 #define ISPRINT(ch) (font_ctype[(unsigned char) (ch)] != ' ')
58 /* Does character <ch> exist in current body font? */
59 #define EXISTS(ch) (font_ctype[(unsigned char) (ch)] == '*')
62 #define RESOURCE_LINE_WIDTH 75
86 /* Special escape tokens. */
105 /* Token structure. */
110 double new_x; /* Current point x after this token. */
111 double new_y; /* Current point y after this token. */
112 int new_col; /* Line column after this token. */
120 double x; /* x-offset */
121 double y; /* y-offset */
122 double w; /* width */
123 double h; /* height */
126 int llx, lly, urx, ury; /* Bounding box. */
127 char filename[PATH_MAX];
129 unsigned int skipbuf_len;
130 unsigned int skipbuf_pos;
131 FILE *fp; /* File from which eps image is read. */
132 int pipe; /* Is <fp> opened to pipe? */
140 InputEncoding encoding;
142 char filename[PATH_MAX];
146 typedef struct gs_token_st Token;
150 * Prototypes for static functions.
153 static void get_next_token ___P ((InputStream *is, double linestart,
154 double linepos, unsigned int col,
155 double linew, Token *token));
157 static void dump_ps_page_header ___P ((char *fname, int empty));
159 static void dump_ps_page_trailer ();
161 static void dump_empty_page ();
164 * Recognize a EPS file described by <token>. Returns 1 if file was a
165 * valid EPS file or 0 otherwise. File is accepted if it starts with
166 * the PostScript magic `%!' and it has a valid `%%BoundingBox' DSC
169 static int recognize_eps_file ___P ((Token *token));
172 * Insert EPS file described by <token> to the output stream.
174 static void paste_epsf ___P ((Token *token));
177 * Check if InputStream <is> contains a file which can be passed
178 * through without any modifications. Returns 1 if file was passed or
181 static int do_pass_through ___P ((char *fname, InputStream *is));
184 * Read one float dimension from InputStream <is>. If <units> is
185 * true, number can be followed by an optional unit specifier. If
186 * <horizontal> is true, dimension is horizontal, otherwise it is
187 * vertical (this is used to find out how big `line' units are).
189 static double read_float ___P ((InputStream *is, int units, int horizontal));
192 * Print linenumber <linenum> to the beginning of the current line.
193 * Current line start is specified by point (x, y).
195 static void print_line_number ___P ((double x, double y, double space,
196 double margin, unsigned int linenum));
198 /* Send PostScript to the output file. */
199 #define OUTPUT(body) \
207 /* Divert output to tmp file so the total page count can be counted. */
208 static void divert ();
210 /* Paste diverted data to the output and patch the total page counts. */
211 static void undivert ();
214 * Handle two-side printing related binding options. This function is
215 * called once for each even-numbered page.
217 static void handle_two_side_options ();
223 unsigned int current_pagenum = 0; /* The number of the current page. */
224 unsigned int total_pages_in_file;
225 unsigned int input_filenum = 0;
226 unsigned int current_file_linenum;
227 int first_pagenum_for_file;
228 char *fname = NULL; /* The name of the current input file. */
235 /* Have we dumped PS header? */
236 static int ps_header_dumped = 0;
239 static FILE *divertfp = NULL;
241 /* Current output() file. */
242 static FILE *cofp = NULL;
244 /* To print or not to print, that's a question. */
245 static int do_print = 1;
247 /* Is ^@font{}-defined font active? */
248 static int user_fontp = 0;
250 /* The user ^@font{}-defined font. */
251 static char user_font_name[PATH_MAX];
252 static FontPoint user_font_pt;
253 static InputEncoding user_font_encoding;
255 /* Is ^@color{}-defined color active? */
256 static int user_colorp = 0;
258 /* The user ^@color{}-defined color. */
259 static Color user_color;
261 /* Is ^@bgcolor{}-defined color active? */
262 static int user_bgcolorp = 0;
264 /* The user ^@bgcolor{}-defined color. */
265 static Color user_bgcolor;
267 /* The last linenumber printed by print_line_number(). */
268 static unsigned int print_line_number_last;
270 /* Registers to store X-coordinates with the ^@savex{} escape.
271 Initially these are uninitialized. */
272 static double xstore[256];
283 char *ps_version_string; /* Version string for PS procsets. */
286 /* Dump PS header only once. */
287 if (ps_header_dumped)
289 ps_header_dumped = 1;
291 /* Create version string. */
292 ps_version_string = xstrdup (VERSION);
293 cp = strrchr (ps_version_string, '.');
300 OUTPUT ((cofp, "%s\n", output_first_line));
301 OUTPUT ((cofp, "%%%%BoundingBox: %d %d %d %d\n", media->llx, media->lly,
302 media->urx, media->ury));
303 OUTPUT ((cofp, "%%%%Title: %s\n", title));
304 OUTPUT ((cofp, "%%%%For: %s\n", passwd->pw_gecos));
305 OUTPUT ((cofp, "%%%%Creator: %s\n", PACKAGE_STRING));
306 OUTPUT ((cofp, "%%%%CreationDate: %s\n", date_string));
307 OUTPUT ((cofp, "%%%%Orientation: %s\n",
308 ((nup > 1) && nup_landscape)
309 || ((nup == 1) && landscape) ? "Landscape" : "Portrait"));
310 OUTPUT ((cofp, "%%%%Pages: (atend)\n"));
311 OUTPUT ((cofp, "%%%%DocumentMedia: %s %d %d 0 () ()\n",
312 media->name, media->w, media->h));
313 OUTPUT ((cofp, "%%%%DocumentNeededResources: (atend)\n"));
315 if (count_key_value_set (pagedevice) > 0)
316 OUTPUT ((cofp, "%%%%LanguageLevel: 2\n"));
318 OUTPUT ((cofp, "%%%%EndComments\n"));
322 * Procedure Definitions.
325 OUTPUT ((cofp, "%%%%BeginProlog\n"));
328 OUTPUT ((cofp, "%%%%BeginResource: procset Enscript-Prolog %s\n",
330 if (!paste_file ("enscript", ".pro"))
331 FATAL ((stderr, _("couldn't find prolog \"%s\": %s\n"), "enscript.pro",
333 OUTPUT ((cofp, "%%%%EndResource\n"));
335 /* Encoding vector. */
336 OUTPUT ((cofp, "%%%%BeginResource: procset Enscript-Encoding-%s %s\n",
337 encoding_name, ps_version_string));
338 if (!paste_file (encoding_name, ".enc"))
339 FATAL ((stderr, _("couldn't find encoding file \"%s.enc\": %s\n"),
340 encoding_name, strerror (errno)));
341 OUTPUT ((cofp, "%%%%EndResource\n"));
343 OUTPUT ((cofp, "%%%%EndProlog\n"));
350 OUTPUT ((cofp, "%%%%BeginSetup\n"));
352 /* Download fonts. */
353 for (got = strhash_get_first (download_fonts, &cp, &j, (void **) &cp2); got;
354 got = strhash_get_next (download_fonts, &cp, &j, (void **) &cp2))
357 /* For each required font, emit %%IncludeResouce comment. */
358 for (got = strhash_get_first (res_fonts, &cp, &j, (void **) &cp2); got;
359 got = strhash_get_next (res_fonts, &cp, &j, (void **) &cp2))
360 OUTPUT ((cofp, "%%%%IncludeResource: font %s\n", cp));
362 OUTPUT ((cofp, "/HFpt_w %g def\n", HFpt.w));
363 OUTPUT ((cofp, "/HFpt_h %g def\n", HFpt.h));
366 /* Select our fonts. */
368 /* Header font HF. */
369 OUTPUT ((cofp, "/%s /HF-gs-font MF\n", HFname));
371 "/HF /HF-gs-font findfont [HFpt_w 0 0 HFpt_h 0 0] makefont def\n"));
373 /* Our default typing font F. */
374 OUTPUT ((cofp, "/%s /F-gs-font MF\n", Fname));
375 OUTPUT ((cofp, "/F-gs-font %g %g SF\n", Fpt.w, Fpt.h));
378 if (underlay != NULL)
380 OUTPUT ((cofp, "/ul_str (%s) def\n", underlay));
381 OUTPUT ((cofp, "/ul_w_ptsize %g def\n", ul_ptsize.w));
382 OUTPUT ((cofp, "/ul_h_ptsize %g def\n", ul_ptsize.h));
383 OUTPUT ((cofp, "/ul_gray %g def\n", ul_gray));
384 OUTPUT ((cofp, "/ul_x %g def\n", ul_x));
385 OUTPUT ((cofp, "/ul_y %g def\n", ul_y));
386 OUTPUT ((cofp, "/ul_angle %g def\n", ul_angle));
387 OUTPUT ((cofp, "/ul_style %d def\n", ul_style));
388 OUTPUT ((cofp, "/%s /F-ul-font MF\n", ul_font));
389 OUTPUT ((cofp, "/ul_font /F-ul-font findfont \
390 [ul_w_ptsize 0 0 ul_h_ptsize 0 0] makefont def\n"));
393 /* Number of copies. */
394 OUTPUT ((cofp, "/#copies %d def\n", num_copies));
398 OUTPUT ((cofp, "true page_prefeed\n"));
400 /* Statusdict definitions. */
401 if (count_key_value_set (statusdict) > 0)
403 OUTPUT ((cofp, "%% Statustdict definitions:\nstatusdict begin\n "));
405 for (got = strhash_get_first (statusdict, &cp, &j, (void **) &cp2); got;
406 got = strhash_get_next (statusdict, &cp, &j, (void **) &cp2))
408 j = strlen (cp) + 1 + strlen (cp2) + 1;
409 if (i + j > RESOURCE_LINE_WIDTH)
411 OUTPUT ((cofp, "\n "));
414 OUTPUT ((cofp, "%s %s ", cp2, cp));
417 OUTPUT ((cofp, "\nend\n"));
420 /* Page device definitions. */
422 (count_key_value_set (pagedevice) > 0 || generate_PageSize))
424 OUTPUT ((cofp, "%% Pagedevice definitions:\n"));
425 OUTPUT ((cofp, "gs_languagelevel 1 gt {\n <<\n "));
428 for (got = strhash_get_first (pagedevice, &cp, &j, (void **) &cp2); got;
429 got = strhash_get_next (pagedevice, &cp, &j, (void **) &cp2))
431 j = strlen (cp2) + 1 + strlen (cp) + 2;
432 if (i + j > RESOURCE_LINE_WIDTH)
434 OUTPUT ((cofp, "\n "));
437 OUTPUT ((cofp, "/%s %s ", cp, cp2));
441 if (generate_PageSize)
443 if (i + 21 > RESOURCE_LINE_WIDTH)
445 OUTPUT ((cofp, "\n "));
448 OUTPUT ((cofp, "/PageSize [%d %d] ", media->w, media->h));
452 OUTPUT ((cofp, "\n >> setpagedevice\n} if\n"));
456 * Dump header procset. Header must come after all font inclusions
457 * and enscript's dynamic state definition.
459 if (header != HDR_NONE)
462 if (header == HDR_SIMPLE)
465 hdr = fancy_header_name;
467 OUTPUT ((cofp, "%%%%BeginResource: procset Enscript-Header-%s %s\n",
468 hdr, ps_version_string));
469 if (!paste_file (hdr, ".hdr"))
471 _("couldn't find header definition file \"%s.hdr\": %s\n"),
472 hdr, strerror (errno)));
473 OUTPUT ((cofp, "%%%%EndResource\n"));
477 * Count output width and height here; we can't do it earlier because
478 * header might have just allocated some extra space.
480 d_output_w = d_page_w;
481 d_output_h = d_page_h - d_header_h - d_footer_h;
483 /* Dump our current dynamic state. */
484 OUTPUT ((cofp, "/d_page_w %d def\n", d_page_w));
485 OUTPUT ((cofp, "/d_page_h %d def\n", d_page_h));
487 OUTPUT ((cofp, "/d_header_x %d def\n", 0));
488 OUTPUT ((cofp, "/d_header_y %d def\n", d_output_h + d_footer_h));
489 OUTPUT ((cofp, "/d_header_w %d def\n", d_header_w));
490 OUTPUT ((cofp, "/d_header_h %d def\n", d_header_h));
492 OUTPUT ((cofp, "/d_footer_x %d def\n", 0));
493 OUTPUT ((cofp, "/d_footer_y %d def\n", 0));
494 OUTPUT ((cofp, "/d_footer_w %d def\n", d_header_w));
495 OUTPUT ((cofp, "/d_footer_h %d def\n", d_footer_h));
497 OUTPUT ((cofp, "/d_output_w %d def\n", d_output_w));
498 OUTPUT ((cofp, "/d_output_h %d def\n", d_output_h));
499 OUTPUT ((cofp, "/cols %d def\n", num_columns));
501 OUTPUT ((cofp, "%%%%EndSetup\n"));
511 unsigned int nup_subpage;
513 if (!ps_header_dumped)
514 /* No header, let's be consistent and forget trailer also. */
517 /* The possible pending N-up showpage. */
518 nup_subpage = (total_pages - 1) % nup;
519 if (nup > 1 && nup_subpage + 1 != nup)
520 /* N-up showpage missing. */
521 OUTPUT ((cofp, "_R\nS\n"));
525 OUTPUT ((cofp, "%%%%Trailer\n"));
528 OUTPUT ((cofp, "false page_prefeed\n"));
530 OUTPUT ((cofp, "%%%%Pages: %d\n", total_pages));
532 /* Document needed resources. */
535 OUTPUT ((cofp, "%%%%DocumentNeededResources: font "));
536 i = 32; /* length of the previous string. */
537 for (got = strhash_get_first (res_fonts, &cp, &j, &value); got;
538 got = strhash_get_next (res_fonts, &cp, &j, &value))
540 if (i + strlen (cp) + 1 > RESOURCE_LINE_WIDTH)
542 OUTPUT ((cofp, "\n%%%%+ font "));
543 i = 9; /* length of the previous string. */
545 OUTPUT ((cofp, "%s ", cp));
546 i += strlen (cp) + 1;
548 OUTPUT ((cofp, "\n%%%%EOF\n"));
553 process_file (char *fname_arg, InputStream *is, int is_toc)
558 double linewidth; /* Line width in points. */
562 unsigned int line_column;
563 unsigned int current_linenum;
564 double linenumber_space = 0;
565 double linenumber_margin = 0;
567 int reuse_last_token = 0;
568 unsigned int current_slice = 1;
569 int last_wrapped_line = -1;
570 int last_spaced_file_linenum = -1;
571 int save_current_pagenum;
576 fname = xstrdup (fname_arg);
578 /* Init page number and line counters. */
579 if (!continuous_page_numbers)
581 total_pages_in_file = 0;
582 current_file_linenum = start_line_number;
585 * Count possible line number spaces. This should be enought for 99999
588 linenumber_space = FNT_CHAR_WIDTH ('0') * 5 + 1.0;
589 linenumber_margin = FNT_CHAR_WIDTH (':') + FNT_CHAR_WIDTH ('m');
591 /* We got a new input file. */
594 /* We haven't printed any line numbers yet. */
595 print_line_number_last = (unsigned int) -1;
597 if (pass_through || output_language_pass_through)
598 if (do_pass_through (fname, is))
602 /* We have work to do, let's give header a chance to dump itself. */
606 * Align files to the file_align boundary, this is handy for two-side
609 while ((total_pages % file_align) != 0)
615 MESSAGE (1, (stderr, _("processing file \"%s\"...\n"), fname));
617 linewidth = d_output_w / num_columns - 2 * d_output_x_margin
620 /* Save the current running page number for possible toc usage. */
621 first_pagenum_for_file = total_pages + 1;
624 * Divert our output to a temp file. We will re-process it
625 * afterwards to patch, for example, the number of pages in the
630 /* Process this input file. */
633 /* Start a new page. */
636 for (col = 0; !done && col < num_columns; col++)
638 /* Move to the beginning of the column <col>. */
639 lx = x = col * d_output_w / (float) num_columns + d_output_x_margin
641 lineend = lx + linewidth;
643 ly = y = d_footer_h + d_output_h - d_output_y_margin - LINESKIP;
649 if (line_numbers && line_column == 0
650 && (current_file_linenum != last_spaced_file_linenum))
652 /* Forward x by the amount needed by our line numbers. */
653 x += linenumber_space + linenumber_margin;
654 last_spaced_file_linenum = current_file_linenum;
658 if (!reuse_last_token)
659 get_next_token (is, lx, x, line_column, lineend, &token);
660 reuse_last_token = 0;
663 * Page header printing is delayed to this point because
664 * we want to handle files ending with a newline character
665 * with care. If the last newline would cause a pagebreak,
666 * otherwise we would print page header to the non-existent
667 * next page and that would be ugly ;)
670 if (token.type == tEOF)
677 * Now we know that we are going to make marks to this page
678 * => print page header.
686 total_pages_in_file++;
688 /* Check page ranges. */
689 if (page_ranges == NULL)
694 for (pr = page_ranges; pr; pr = pr->next)
696 if (pr->odd || pr->even)
698 if ((pr->odd && (current_pagenum % 2) == 1)
699 || (pr->even && (current_pagenum % 2) == 0))
707 if (pr->start <= current_pagenum
708 && current_pagenum <= pr->end)
722 save_current_pagenum = current_pagenum;
724 current_pagenum = toc_pagenum;
727 dump_ps_page_header (fname, 0);
731 current_pagenum = save_current_pagenum;
734 /* Print line highlight. */
735 if (line_column == 0 && line_highlight_gray < 1.0)
736 OUTPUT ((cofp, "%g %g %g %g %g line_highlight\n",
737 lx, (y - baselineskip
738 + (font_bbox_lly * Fpt.h / UNITS_PER_POINT)),
739 linewidth, Fpt.h + baselineskip,
740 line_highlight_gray));
742 /* Print line numbers if needed. */
743 if (line_numbers && line_column == 0 && token.type != tFORMFEED)
744 print_line_number (lx, y, linenumber_space, linenumber_margin,
745 current_file_linenum);
747 /* Check rest of tokens. */
751 switch (formfeed_type)
753 case FORMFEED_COLUMN:
761 case FORMFEED_HCOLUMN:
763 * Advance y-coordinate to the next even
764 * `horizontal_column_height' position.
769 current_row = (ly - y) / horizontal_column_height;
770 y = ly - (current_row + 1) * horizontal_column_height;
772 /* Check the end of the page. */
773 if (y < d_footer_h + d_output_y_margin)
781 if (CORRECT_SLICE ())
785 OUTPUT ((cofp, "%g %g %g %g %g (%s) bgs\n", x, y,
786 Fpt.h + baselineskip,
788 - (font_bbox_lly * Fpt.h / UNITS_PER_POINT),
792 else if (user_bgcolorp)
794 OUTPUT ((cofp, "%g %g %g %g %g %g %g (%s) bgcs\n",
795 x, y, Fpt.h + baselineskip,
797 - (font_bbox_lly * Fpt.h / UNITS_PER_POINT),
805 OUTPUT ((cofp, "%g %g M\n(%s) s\n", x, y,
810 line_column = token.new_col;
813 case tCARRIAGE_RETURN:
814 /* Just reset the x-coordinate. */
815 x = col * d_output_w / (float) num_columns
816 + d_output_x_margin + line_indent;
821 case tWRAPPED_NEWLINE:
822 if (token.type == tNEWLINE)
824 current_file_linenum++;
833 /* Mark wrapped line marks. */
834 switch (mark_wrapped_lines_style)
841 OUTPUT ((cofp, "%g %g M (+) s\n", x, y));
845 /* Print some fancy graphics. */
847 "%g %g %g %g %d wrapped_line_mark\n",
849 mark_wrapped_lines_style));
854 * For wrapped newlines, decrement y only if
855 * we are not slicing the input.
860 /* Count the wrapped lines here. */
861 if (!slicing || current_slice > slice)
862 if (current_file_linenum != last_wrapped_line)
865 num_truncated_lines++;
866 last_wrapped_line = current_file_linenum;
871 if (current_linenum >= lines_per_page
872 || y < d_footer_h + d_output_y_margin)
875 x = col * d_output_w / (float) num_columns
876 + d_output_x_margin + line_indent;
881 /* Count current point movement. */
883 if (token.flags & F_EPSF_ABSOLUTE_Y)
887 token.new_y += token.u.epsf.y - token.u.epsf.h;
889 if (token.flags & F_EPSF_ABSOLUTE_X)
893 token.new_x += token.u.epsf.x;
897 /* Justification flags overwrite <x_ofs>. */
898 if (token.flags & F_EPSF_CENTER)
899 token.new_x = lx + (linewidth - token.u.epsf.w) / 2;
900 if (token.flags & F_EPSF_RIGHT)
901 token.new_x = lx + (linewidth - token.u.epsf.w);
903 /* Check if eps file does not fit to this column. */
904 if ((token.flags & F_EPSF_NO_CPOINT_UPDATE_Y) == 0
905 && token.new_y < d_footer_h + d_output_y_margin)
907 if (current_linenum == 0)
910 * At the beginning of the column, warn user
913 MESSAGE (0, (stderr, _("EPS file \"%s\" is too \
915 token.u.epsf.filename));
919 /* Must start a new column. */
920 reuse_last_token = 1;
926 if (CORRECT_SLICE ())
929 /* Update current point? */
930 if (!(token.flags & F_EPSF_NO_CPOINT_UPDATE_Y))
932 if (!(token.flags & F_EPSF_NO_CPOINT_UPDATE_X))
933 x = token.new_x + token.u.epsf.w;
935 if (y < d_footer_h + d_output_y_margin)
940 /* Select a new current font. */
941 if (line_column == 0)
945 /* Check for possible line skip change. */
946 if (token.u.font.name[0] == '\0')
947 newh = default_Fpt.h;
949 newh = token.u.font.size.h;
953 /* We need a different line skip value. */
957 * We must check for page overflow after we have
962 MESSAGE (2, (stderr, "^@font="));
963 if (token.u.font.name[0] == '\0')
965 /* Select the default font. */
966 Fpt.w = default_Fpt.w;
967 Fpt.h = default_Fpt.h;
968 Fname = default_Fname;
969 encoding = default_Fencoding;
970 OUTPUT ((cofp, "/F-gs-font %g %g SF\n", Fpt.w, Fpt.h));
975 strhash_put (res_fonts, token.u.font.name,
976 strlen (token.u.font.name) + 1,
978 if (token.u.font.encoding == default_Fencoding)
979 OUTPUT ((cofp, "/%s %g %g SUF\n", token.u.font.name,
980 token.u.font.size.w, token.u.font.size.h));
981 else if (token.u.font.encoding == ENC_PS)
982 OUTPUT ((cofp, "/%s %g %g SUF_PS\n", token.u.font.name,
983 token.u.font.size.w, token.u.font.size.h));
986 _("user font encoding can be only the system's default or `ps'")));
988 memset (user_font_name, 0, sizeof(user_font_name));
989 strncpy (user_font_name, token.u.font.name, sizeof(user_font_name) - 1);
990 user_font_pt.w = token.u.font.size.w;
991 user_font_pt.h = token.u.font.size.h;
992 user_font_encoding = token.u.font.encoding;
995 Fpt.w = user_font_pt.w;
996 Fpt.h = user_font_pt.h;
997 Fname = user_font_name;
998 encoding = user_font_encoding;
1000 MESSAGE (2, (stderr, "%s %g/%gpt\n", Fname, Fpt.w, Fpt.h));
1004 * Check for page overflow in that case that we were
1005 * at the first column and font were changed to a bigger
1008 if (y < d_footer_h + d_output_y_margin)
1013 /* Select a new color. */
1014 MESSAGE (2, (stderr, "^@color{%f %f %f}\n",
1018 if (token.u.color.r == token.u.color.g
1019 && token.u.color.g == token.u.color.b
1020 && token.u.color.b == 0.0)
1022 /* Select the default color (black). */
1023 OUTPUT ((cofp, "0 setgray\n"));
1028 OUTPUT ((cofp, "%g %g %g setrgbcolor\n",
1033 user_color.r = token.u.color.r;
1034 user_color.g = token.u.color.g;
1035 user_color.b = token.u.color.b;
1041 /* Select a new background color. */
1042 MESSAGE (2, (stderr, "^@bgcolor{%f %f %f}\n",
1047 if (token.u.color.r == token.u.color.g
1048 && token.u.color.g == token.u.color.b
1049 && token.u.color.b == 1.0)
1051 /* Select the default bgcolor (white). */
1056 user_bgcolor.r = token.u.color.r;
1057 user_bgcolor.g = token.u.color.g;
1058 user_bgcolor.b = token.u.color.b;
1065 fname = xstrdup (token.u.filename);
1068 case tSETPAGENUMBER:
1069 current_pagenum = token.u.i - 1;
1073 if (current_linenum >= token.u.i)
1078 xstore[(unsigned char) token.u.i] = x;
1082 x = xstore[(unsigned char) token.u.i];
1086 OUTPUT ((cofp, "%g %g M\n%s\n", x, y, token.u.str));
1087 xfree (token.u.str);
1092 FATAL ((stderr, "process_file(): got illegal token %d",
1098 ; /* ULTRIX's cc needs this line. */
1103 dump_ps_page_trailer ();
1107 * Reset print flag to true so all the required document trailers
1108 * etc. get printed properly.
1112 /* Undivert our output from the temp file to our output stream. */
1115 /* Table of contents? */
1119 int save_total_pages = total_pages;
1121 /* use first pagenum in file for toc */
1122 total_pages = first_pagenum_for_file;
1124 cp = format_user_string ("TOC", toc_fmt_string);
1125 fprintf (toc_fp, "%s\n", cp);
1128 total_pages = save_total_pages;
1139 /* Check if character <ch> fits to current line. */
1140 #define FITS_ON_LINE(ch) ((linepos + FNT_CHAR_WIDTH (ch) < linew) || col == 0)
1142 /* Is line buffer empty? */
1143 #define BUFFER_EMPTY() (bufpos == 0)
1145 /* Unconditionally append character <ch> to the line buffer. */
1146 #define APPEND_CHAR(ch) \
1148 if (bufpos >= buflen) \
1151 buffer = xrealloc (buffer, buflen); \
1153 buffer[bufpos++] = ch; \
1157 * Copy character <ch> (it fits to this line) to output buffer and
1158 * update current point counters.
1163 linepos += FNT_CHAR_WIDTH (ch); \
1167 #define UNEMIT(ch) \
1169 linepos -= FNT_CHAR_WIDTH (ch); \
1173 #define ISSPACE(ch) ((ch) == ' ' || (ch) == '\t')
1174 #define ISOCTAL(ch) ('0' <= (ch) && (ch) <= '7')
1176 /* Read one special escape from input <fp>. */
1181 SpecialEscape escape;
1184 {"comment", ESC_COMMENT},
1187 {"color", ESC_COLOR},
1188 {"bgcolor", ESC_BGCOLOR},
1189 {"newpage", ESC_NEWPAGE},
1191 {"setfilename", ESC_SETFILENAME},
1192 {"setpagenumber", ESC_SETPAGENUMBER},
1193 {"shade", ESC_SHADE},
1194 {"bggray", ESC_BGGRAY},
1195 {"escape", ESC_ESCAPE},
1196 {"savex", ESC_SAVEX},
1197 {"loadx", ESC_LOADX},
1203 read_special_escape (InputStream *is, Token *token)
1210 /* Get escape name. */
1211 for (i = 0; i < sizeof (escname) - 1 && (ch = is_getc (is)) != EOF; i++)
1223 /* Lookup escape. */
1224 for (e = 0; escapes[e].name; e++)
1225 if (strcmp (escname, escapes[e].name) == 0)
1227 if (escapes[e].name == NULL)
1228 FATAL ((stderr, _("unknown special escape: %s"), escname));
1231 * The epsf escape takes optional arguments so it must be handled
1234 if (escapes[e].escape == ESC_EPSF)
1241 token->u.epsf.x = 0.0;
1242 token->u.epsf.y = 0.0;
1243 token->u.epsf.h = 0.0;
1244 token->u.epsf.pipe = 0;
1250 while ((ch = is_getc (is)) != EOF && ch != ']')
1254 case 'c': /* center justification */
1255 token->flags &= ~M_EPSF_JUSTIFICATION;
1256 token->flags |= F_EPSF_CENTER;
1259 case 'n': /* no current point update */
1260 /* Check the next character. */
1265 token->flags |= F_EPSF_NO_CPOINT_UPDATE_X;
1269 token->flags |= F_EPSF_NO_CPOINT_UPDATE_Y;
1274 token->flags |= F_EPSF_NO_CPOINT_UPDATE_X;
1275 token->flags |= F_EPSF_NO_CPOINT_UPDATE_Y;
1280 case 'r': /* right justification */
1281 token->flags &= ~M_EPSF_JUSTIFICATION;
1282 token->flags |= F_EPSF_RIGHT;
1286 case 's': /* scale */
1287 /* Check the next character. */
1292 token->flags |= F_EPSF_SCALE_X;
1293 token->u.epsf.xscale = read_float (is, 0, 1);
1297 token->flags |= F_EPSF_SCALE_Y;
1298 token->u.epsf.yscale = read_float (is, 0, 0);
1303 token->flags |= F_EPSF_SCALE_X;
1304 token->flags |= F_EPSF_SCALE_Y;
1305 token->u.epsf.xscale = token->u.epsf.yscale
1306 = read_float (is, 0, 1);
1311 case 'x': /* x-position */
1312 token->u.epsf.x = read_float (is, 1, 1);
1314 /* Check the next character. */
1319 token->flags |= F_EPSF_ABSOLUTE_X;
1328 case 'y': /* y-position */
1329 token->u.epsf.y = - read_float (is, 1, 0);
1331 /* Check the next character. */
1336 token->flags |= F_EPSF_ABSOLUTE_Y;
1345 case 'h': /* height */
1346 token->u.epsf.h = read_float (is, 1, 0);
1354 FATAL ((stderr, _("illegal option %c for ^@epsf escape"),
1360 _("malformed ^@epsf escape: no ']' after options")));
1366 /* Read filename. */
1367 for (i = 0; (ch = is_getc (is)) != EOF && ch != '}'; i++)
1369 token->u.epsf.filename[i] = ch;
1370 if (i + 1 >= sizeof (token->u.epsf.filename))
1372 _("too long file name for ^@epsf escape:\n%.*s"),
1373 i, token->u.epsf.filename));
1376 FATAL ((stderr, _("unexpected EOF while scanning ^@epsf escape")));
1378 token->u.epsf.filename[i] = '\0';
1379 token->type = tEPSF;
1382 FATAL ((stderr, _("malformed ^@epsf escape: no '{' found")));
1385 * Now we have a valid epsf-token in <token>. Let's read BoundingBox
1386 * and do some calculations.
1388 if (!recognize_eps_file (token))
1389 /* Recognize eps has already printed error message so we are done. */
1390 token->type = tNONE;
1393 /* Some fixups for x and y dimensions. */
1394 token->u.epsf.y += LINESKIP - 1;
1395 if (token->u.epsf.h != 0.0)
1396 token->u.epsf.h -= 1.0;
1398 /* Count picture's width and height. */
1400 pw = token->u.epsf.urx - token->u.epsf.llx;
1401 ph = token->u.epsf.ury - token->u.epsf.lly;
1403 /* The default scale. */
1404 if (token->u.epsf.h == 0.0)
1407 scale = token->u.epsf.h / ph;
1409 if ((token->flags & F_EPSF_SCALE_X) == 0)
1410 token->u.epsf.xscale = scale;
1411 if ((token->flags & F_EPSF_SCALE_Y) == 0)
1412 token->u.epsf.yscale = scale;
1414 pw *= token->u.epsf.xscale;
1415 ph *= token->u.epsf.yscale;
1417 token->u.epsf.w = pw;
1418 token->u.epsf.h = ph;
1421 else if (escapes[e].escape == ESC_COMMENT)
1423 /* Comment the rest of this line. */
1424 while ((ch = is_getc (is)) != EOF && ch != nl)
1426 token->type = tNONE;
1434 * Handle the rest of the escapes.
1437 /* Read argument. */
1440 FATAL ((stderr, _("malformed %s escape: no '{' found"),
1445 (ch = is_getc (is)) != EOF && (parenlevel > 0 || ch != '}'); i++)
1453 if (i + 1 >= sizeof (buf))
1454 FATAL ((stderr, _("too long argument for %s escape:\n%.*s"),
1455 escapes[e].name, i, buf));
1459 /* And now handle the escape. */
1460 switch (escapes[e].escape)
1463 memset (token->u.font.name, 0, sizeof(token->u.font.name));
1464 strncpy (token->u.font.name, buf, sizeof(token->u.font.name) - 1);
1466 /* Check for the default font. */
1467 if (strcmp (token->u.font.name, "default") == 0)
1468 token->u.font.name[0] = '\0';
1471 if (!parse_font_spec (token->u.font.name, &cp,
1472 &token->u.font.size,
1473 &token->u.font.encoding))
1474 FATAL ((stderr, _("malformed font spec for ^@font escape: %s"),
1475 token->u.font.name));
1477 memset (token->u.font.name, 0, sizeof(token->u.font.name));
1478 strncpy (token->u.font.name, cp, sizeof(token->u.font.name) - 1);
1481 token->type = tFONT;
1486 /* Check for the default color. */
1487 if (strcmp (buf, "default") == 0)
1491 if (escapes[e].escape == ESC_BGCOLOR)
1494 token->u.color.r = val;
1495 token->u.color.g = val;
1496 token->u.color.b = val;
1502 got = sscanf (buf, "%g %g %g",
1511 _("malformed color spec for ^@%s escape: %s"),
1512 escapes[e].escape == ESC_COLOR
1513 ? "color" : "bgcolor",
1518 token->u.color.g = token->u.color.b = token->u.color.r;
1522 /* Got all three components. */
1526 if (escapes[e].escape == ESC_COLOR)
1527 token->type = tCOLOR;
1529 token->type = tBGCOLOR;
1533 line_highlight_gray = atof (buf);
1534 if (line_highlight_gray < 0.0 || line_highlight_gray > 1.0)
1535 FATAL ((stderr, _("invalid value for ^@shade escape: %s"), buf));
1537 token->type = tNONE;
1541 bggray = atof (buf);
1542 if (bggray < 0.0 || bggray > 1.0)
1543 FATAL ((stderr, _("invalid value for ^@bggray escape: %s"), buf));
1545 token->type = tNONE;
1549 if (strcmp (buf, "default") == 0)
1550 escape_char = default_escape_char;
1552 escape_char = atoi (buf);
1553 token->type = tNONE;
1556 case ESC_SETFILENAME:
1557 memset (token->u.filename, 0, sizeof(token->u.filename));
1558 strncpy (token->u.filename, buf, sizeof(token->u.filename) - 1);
1559 token->type = tSETFILENAME;
1562 case ESC_SETPAGENUMBER:
1563 token->u.i = atoi (buf);
1564 token->type = tSETPAGENUMBER;
1569 token->u.i = 1; /* The default is the first line. */
1571 token->u.i = atoi (buf);
1572 token->type = tNEWPAGE;
1576 token->type = tSAVEX;
1577 token->u.i = atoi (buf);
1581 token->type = tLOADX;
1582 token->u.i = atoi (buf);
1586 token->u.str = xstrdup (buf);
1599 /* Get next token from input file <fp>. */
1601 get_next_token (InputStream *is, double linestart, double linepos,
1602 unsigned int col, double linew, Token *token)
1604 static unsigned char *buffer = NULL; /* output buffer */
1605 static unsigned int buflen = 0; /* output buffer's length */
1606 unsigned int bufpos = 0; /* current position in output buffer */
1610 static int pending_token = tNONE;
1611 unsigned int original_col = col;
1613 if (pending_token != tNONE)
1615 token->type = pending_token;
1616 pending_token = tNONE;
1629 if (BUFFER_EMPTY ())
1641 * One of these is the newline character and the other one
1642 * is carriage return.
1646 /* The newline character. */
1647 if (BUFFER_EMPTY ())
1649 token->type = tNEWLINE;
1660 /* The carriage return character. */
1661 if (BUFFER_EMPTY ())
1663 token->type = tCARRIAGE_RETURN;
1677 i = tabsize - (col % tabsize);
1680 if (FITS_ON_LINE (' '))
1691 /* Proportional font. */
1693 double grid = tabsize * FNT_CHAR_WIDTH (' ');
1696 /* Move linepos to the next multiple of <grid>. */
1697 linepos = (((int) ((linepos - linestart) / grid) + 1) * grid
1699 if (linepos >= linew)
1707 if (BUFFER_EMPTY ())
1709 if (interpret_formfeed)
1710 token->type = tFORMFEED;
1712 token->type = tNEWLINE;
1723 /* Handle special escapes. */
1724 if (special_escapes && ch == escape_char)
1726 if (BUFFER_EMPTY ())
1728 /* Interpret special escapes. */
1729 read_special_escape (is, token);
1730 if (token->type != tNONE)
1734 * Got tNONE special escape => read_special_escape()
1735 * has already done what was needed. Just read more.
1747 /* Handle backspace character. */
1750 if (BUFFER_EMPTY () || !EXISTS (buffer[bufpos - 1]))
1751 linepos -= FNT_CHAR_WIDTH ('m');
1753 linepos -= FNT_CHAR_WIDTH (buffer[bufpos - 1]);
1759 /* Check normal characters. */
1762 if (FITS_ON_LINE (ch))
1765 * Print control characters (and optionally
1766 * characters greater than 127) in the escaped form
1767 * so PostScript interpreter will not hang on them.
1769 if (ch < 040 || (clean_7bit && ch >= 0200))
1773 sprintf (buf, "\\%03o", ch);
1774 for (i = 0; buf[i]; i++)
1775 APPEND_CHAR (buf[i]);
1777 /* Update current point counters manually. */
1778 linepos += FNT_CHAR_WIDTH (ch);
1781 else if (ch == '(' || ch == ')' || ch == '\\')
1783 /* These must be quoted in PostScript strings. */
1796 else if (ISPRINT (ch))
1798 /* Printable, but do not exists in this font. */
1799 if (FITS_ON_LINE ('?'))
1802 if (missing_chars[ch]++ == 0)
1803 num_missing_chars++;
1817 * Non-printable and does not exist in current font, print
1818 * it in the format specified by non_printable_format.
1821 if (non_printable_chars[ch]++ == 0)
1822 num_non_printable_chars++;
1824 switch (non_printable_format)
1830 case NPF_QUESTIONMARK:
1845 sprintf (buf, "\\%03o", ch);
1850 for (i = 0; buf[i]; i++)
1851 len += FNT_CHAR_WIDTH (buf[i]);
1853 if (linepos + len < linew || col == 0)
1856 for (i = 0; buf[i]; i++)
1859 APPEND_CHAR ('\\'); /* Escape '\\' characters. */
1875 /* Check for wrapped line. */
1876 if (done == DONE_WRAP)
1878 /* This line is too long. */
1880 if (line_end == LE_TRUNCATE)
1882 /* Truncate this line. */
1883 while ((ch = is_getc (is)) != EOF && ch != nl)
1886 else if (!BUFFER_EMPTY () && line_end == LE_WORD_WRAP)
1890 if (ISSPACE (buffer[bufpos - 1]))
1892 /* Skip all whitespace from the end of the wrapped line. */
1893 while ((w = is_getc (is)) != EOF && ISSPACE (w))
1899 /* Find the previous word boundary for the wrap. */
1900 for (w = bufpos - 1; w >= 0 && !ISSPACE (buffer[w]); w--)
1903 if (w > 0 || original_col > 0)
1906 * Ok, we found a word boundary. Now we must unemit
1907 * characters from the buffer to the intput stream.
1910 * - bufpos is unsigned integer variable
1911 * - some characters are escaped with '\\'
1912 * - some characters are printed in octal notation
1918 /* Check for '(', ')' and '\\'. */
1920 && (buffer[bufpos] == '('
1921 || buffer[bufpos] == ')'
1922 || buffer[bufpos] == '\\')
1923 && buffer[bufpos - 1] == '\\')
1925 is_ungetc (buffer[bufpos], is);
1926 UNEMIT (buffer[bufpos]);
1929 /* Check the octal notations "\\%03o". */
1930 else if (bufpos - 2 > w
1931 && ISOCTAL (buffer[bufpos])
1932 && ISOCTAL (buffer[bufpos - 1])
1933 && ISOCTAL (buffer[bufpos - 2])
1934 && buffer[bufpos - 3] == '\\')
1939 * It is a potential octal character. Now we
1940 * must process the buffer from the beginning
1941 * and see if `bufpos - 3' really starts a character.
1943 for (ti = w; ti < bufpos - 3; ti++)
1945 if (buffer[ti] == '\\')
1947 if (ISOCTAL (buffer[ti + 1]))
1952 tti < 3 && ISOCTAL (buffer[ti + 1]);
1957 /* Simple escape. */
1963 * If <ti> is equal to <bufpos - 3>, we found
1964 * an octal character, otherwise the leading
1965 * backslash at <bufpos - 3> belongs to the
1966 * previous character.
1968 if (ti == bufpos - 3)
1972 tch = (((buffer[bufpos - 2] - '0') << 6)
1973 + ((buffer[bufpos - 1] - '0') << 3)
1974 + (buffer[bufpos] - '0'));
1975 is_ungetc (tch, is);
1980 /* Normal character. */
1985 /* Normal character, just unget it. */
1987 is_ungetc (buffer[bufpos], is);
1988 UNEMIT (buffer[bufpos]);
1998 if (line_end == LE_TRUNCATE)
2001 num_truncated_lines++;
2002 pending_token = tNEWLINE;
2005 pending_token = tWRAPPED_NEWLINE;
2008 pending_token = tEOF;
2012 token->type = tSTRING;
2013 token->u.str = (char *) buffer;
2014 token->new_x = linepos;
2015 token->new_col = col;
2020 dump_ps_page_header (char *fname, int empty)
2022 char *dirc, *basec, *fdir, *ftail;
2026 unsigned int nup_subpage;
2028 /* The N-up printing sub-page. */
2029 nup_subpage = (total_pages - 1) % nup;
2031 /* Split fname into fdir and ftail. */
2032 dirc = strdup(fname);
2033 basec = strdup(fname);
2034 fdir = dirname(dirc);
2035 ftail = basename(basec);
2039 /* N-up printing is active. */
2042 if (nup_subpage == 0)
2044 /* This is a real page start. */
2049 OUTPUT ((cofp, "%%%%Page: (%d-%d) %d\n", current_pagenum,
2050 current_pagenum + nup - 1, total_pages / nup + 1));
2054 OUTPUT ((cofp, "%%%%Page: (%s:%3d-%3d) %d\n", ftail,
2055 current_pagenum, current_pagenum + nup - 1,
2056 total_pages / nup + 1));
2061 OUTPUT ((cofp, "%%%%BeginPageSetup\n_S\n"));
2063 if ((total_pages / nup + 1) % 2 == 0)
2064 /* Two-side binding options for the even pages. */
2065 handle_two_side_options ();
2067 #define PRINT_BOUNDING_BOXES 0
2069 #if PRINT_BOUNDING_BOXES
2071 "%d %d moveto %d %d lineto %d %d lineto %d %d lineto closepath stroke\n",
2072 media->llx, media->lly, media->llx, media->ury,
2073 media->urx, media->ury, media->urx, media->lly));
2079 OUTPUT ((cofp, "90 rotate\n%d %d translate\n",
2080 media->lly, -media->urx));
2082 OUTPUT ((cofp, "%d %d translate\n", media->llx, media->lly));
2087 OUTPUT ((cofp, "90 rotate\n%d %d translate\n",
2088 media->lly, -media->llx));
2090 OUTPUT ((cofp, "%d %d translate\n", media->llx, media->ury));
2095 /* Page start comment. */
2099 OUTPUT ((cofp, "%sPage: (%d) %d\n", cstr, current_pagenum, total_pages));
2103 OUTPUT ((cofp, "%sPage: (%s:%3d) %d\n", cstr, ftail, current_pagenum,
2112 OUTPUT ((cofp, "%sBeginPageSetup\n_S\n", cstr));
2118 OUTPUT ((cofp, "%% N-up sub-page %d/%d\n", nup_subpage + 1, nup));
2123 xm = nup_subpage % nup_columns;
2124 ym = nup_subpage / nup_columns;
2128 xm = nup_subpage / nup_rows;
2129 ym = nup_subpage % nup_rows;
2132 OUTPUT ((cofp, "%d %d translate\n",
2133 xm * (nup_width + nup_xpad),
2134 ym * (nup_height + nup_ypad)));
2140 xm = nup_subpage / nup_rows;
2141 ym = nup_subpage % nup_rows;
2145 xm = nup_subpage % nup_columns;
2146 ym = nup_subpage / nup_columns;
2149 OUTPUT ((cofp, "%d %d translate\n",
2150 xm * (nup_width + nup_xpad),
2151 -((int) (ym * (nup_height + nup_ypad) + nup_height))));
2153 OUTPUT ((cofp, "%g dup scale\n", nup_scale));
2155 /* And finally, the real page setup. */
2157 OUTPUT ((cofp, "90 rotate\n%d %d translate\n", 0, -d_page_h));
2161 /* No N-up printing. */
2163 if (total_pages % 2 == 0)
2164 /* Two-side binding options for the even pages. */
2165 handle_two_side_options ();
2168 OUTPUT ((cofp, "90 rotate\n%d %d translate\n",
2169 media->lly, -media->urx));
2171 OUTPUT ((cofp, "%d %d translate\n", media->llx, media->lly));
2174 /* Some constants etc. */
2175 OUTPUT ((cofp, "/pagenum %d def\n", current_pagenum));
2177 cp = escape_string (fname);
2178 OUTPUT ((cofp, "/fname (%s) def\n", cp));
2181 cp = escape_string (fdir);
2182 OUTPUT ((cofp, "/fdir (%s) def\n", cp));
2186 cp = escape_string (ftail);
2187 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, (int)(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 *= FNT_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 = 0;
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 += FNT_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()
2787 assert (divertfp == NULL);
2789 /* Open divert file. */
2791 divertfp = tmpfile ();
2792 if (divertfp == NULL)
2793 FATAL ((stderr, _("couldn't create temporary divert file: %s"),
2807 assert (divertfp != NULL);
2809 if (fseek (divertfp, 0, SEEK_SET) != 0)
2810 FATAL ((stderr, _("couldn't rewind divert file: %s"), strerror (errno)));
2812 while (fgets (buf, sizeof (buf), divertfp))
2814 if (strncmp (buf, "%%BeginDocument", 15) == 0)
2816 else if (strncmp (buf, "%%EndDocument", 13) == 0)
2821 if (strncmp (buf, "% User defined strings", 22) == 0)
2824 while (fgets (buf, sizeof (buf), divertfp))
2826 if (strncmp (buf, "%%EndPageSetup", 14) == 0)
2829 /* Patch total pages to the user defined strings. */
2830 cp = strchr (buf, '\001');
2835 fprintf (ofp, "%d", total_pages_in_file);
2836 fputs (cp + 1, ofp);
2855 handle_two_side_options ()
2857 if (rotate_even_pages)
2858 /* Rotate page 180 degrees. */
2859 OUTPUT ((cofp, "180 rotate\n%d %d translate\n",
2860 -media->w, -media->h));
2862 if (swap_even_page_margins)
2863 OUTPUT ((cofp, "%d 0 translate\n",
2864 -(media->llx - (media->w - media->urx))));