1 /* PSPP - computes sample statistics.
2 Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
3 Written by Ben Pfaff <blp@gnu.org>.
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 This program is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 /*this #if encloses the remainder of the file. */
35 #if TIME_WITH_SYS_TIME
47 #include "bitvector.h"
62 optimize-text-size not implemented.
64 Line buffering is the only possibility; page buffering should also
69 Should add a field to give a file that has a list of fonts
72 Should add an option that tells the driver it can emit %%Include:'s.
74 Should have auto-encode=true stream-edit or whatever to allow
75 addition to list of encodings.
77 Should align fonts of different sizes along their baselines (see
80 /* PostScript driver options: (defaults listed first)
84 data=clean7bit|clean8bit|binary
87 paper-size=letter (see "papersize" file)
88 orientation=portrait|landscape
97 prologue-file=ps-prologue
99 encoding-file=ps-encodings
100 auto-encode=true|false
106 line-style=thick|double
112 optimize-text-size=1|0|2
113 optimize-line-size=1|0
114 max-fonts-simult=0 Max # of fonts in printer memory at once (0=infinite)
117 /* The number of `psus' (PostScript driver UnitS) per inch. Although
118 this is a #define, the value is expected never to change. If it
119 does, review all uses. */
122 /* Magic numbers for PostScript and EPSF drivers. */
132 OTN_PORTRAIT, /* Portrait. */
133 OTN_LANDSCAPE /* Landscape. */
136 /* Output options. */
139 OPO_MIRROR_HORZ = 001, /* 1=Mirror across a horizontal axis. */
140 OPO_MIRROR_VERT = 002, /* 1=Mirror across a vertical axis. */
141 OPO_ROTATE_180 = 004, /* 1=Rotate the page 180 degrees. */
142 OPO_COLOR = 010, /* 1=Enable color. */
143 OPO_HEADERS = 020, /* 1=Draw headers at top of page. */
144 OPO_AUTO_ENCODE = 040, /* 1=Add encodings semi-intelligently. */
145 OPO_DOUBLE_LINE = 0100 /* 1=Double lines instead of thick lines. */
148 /* Data allowed in output. */
151 ODA_CLEAN7BIT, /* 0x09, 0x0a, 0x0d, 0x1b...0x7e */
152 ODA_CLEAN8BIT, /* 0x09, 0x0a, 0x0d, 0x1b...0xff */
153 ODA_BINARY, /* 0x00...0xff */
157 /* Types of lines for purpose of caching. */
160 horz, /* Single horizontal. */
161 dbl_horz, /* Double horizontal. */
162 spl_horz, /* Special horizontal. */
163 vert, /* Single vertical. */
164 dbl_vert, /* Double vertical. */
165 spl_vert, /* Special vertical. */
172 int ind; /* Independent var. Don't reorder. */
173 int mdep; /* Maximum number of dependent var pairs. */
174 int ndep; /* Current number of dependent var pairs. */
175 int dep[1][2]; /* Dependent var pairs. */
178 /* Contents of ps_driver_ext.loaded. */
181 char *dit; /* Font Groff name. */
182 struct font_desc *font; /* Font descriptor. */
185 /* Combines a font with a font size for benefit of generated code. */
188 struct font_entry *font; /* Font. */
189 int size; /* Font size. */
190 int index; /* PostScript index. */
193 /* A font encoding. */
196 char *filename; /* Normalized filename of this encoding. */
197 int index; /* Index value. */
200 /* PostScript output driver extension record. */
203 /* User parameters. */
204 int orientation; /* OTN_PORTRAIT or OTN_LANDSCAPE. */
205 int output_options; /* OPO_*. */
206 int data; /* ODA_*. */
208 int left_margin; /* Left margin in psus. */
209 int right_margin; /* Right margin in psus. */
210 int top_margin; /* Top margin in psus. */
211 int bottom_margin; /* Bottom margin in psus. */
213 char eol[3]; /* End of line--CR, LF, or CRLF. */
215 char *font_dir; /* Font directory relative to font path. */
216 char *prologue_fn; /* Prologue's filename relative to font dir. */
217 char *desc_fn; /* DESC filename relative to font dir. */
218 char *encoding_fn; /* Encoding's filename relative to font dir. */
220 char *prop_family; /* Default proportional font family. */
221 char *fixed_family; /* Default fixed-pitch font family. */
222 int font_size; /* Default font size (psus). */
224 int line_gutter; /* Space around lines. */
225 int line_space; /* Space between lines. */
226 int line_width; /* Width of lines. */
227 int line_width_thick; /* Width of thick lines. */
229 int text_opt; /* Text optimization level. */
230 int line_opt; /* Line optimization level. */
231 int max_fonts; /* Max # of simultaneous fonts (0=infinite). */
233 /* Internal state. */
234 struct file_ext file; /* Output file. */
235 int page_number; /* Current page number. */
236 int file_page_number; /* Page number in this file. */
237 int w, l; /* Paper size. */
238 struct hsh_table *lines[n_line_types]; /* Line buffers. */
240 struct font_entry *prop; /* Default Roman proportional font. */
241 struct font_entry *fixed; /* Default Roman fixed-pitch font. */
242 struct hsh_table *loaded; /* Fonts in memory. */
244 struct hsh_table *combos; /* Combinations of fonts with font sizes. */
245 struct ps_font_combo *last_font; /* PostScript selected font. */
246 int next_combo; /* Next font combo position index. */
248 struct hsh_table *encodings;/* Set of encodings. */
249 int next_encoding; /* Next font encoding index. */
251 /* Currently selected font. */
252 struct font_entry *current; /* Current font. */
253 char *family; /* Font family. */
254 int size; /* Size in psus. */
258 /* Transform logical y-ordinate Y into a page ordinate. */
259 #define YT(Y) (this->length - (Y))
262 static int postopen (struct file_ext *);
263 static int preclose (struct file_ext *);
264 static void draw_headers (struct outp_driver *this);
266 static int compare_font_entry (const void *, const void *, void *param);
267 static unsigned hash_font_entry (const void *, void *param);
268 static void free_font_entry (void *, void *foo);
269 static struct font_entry *load_font (struct outp_driver *, const char *dit);
270 static void init_fonts (void);
271 static void done_fonts (void);
273 static void dump_lines (struct outp_driver *this);
275 static void read_ps_encodings (struct outp_driver *this);
276 static int compare_ps_encoding (const void *pa, const void *pb, void *foo);
277 static unsigned hash_ps_encoding (const void *pa, void *foo);
278 static void free_ps_encoding (void *a, void *foo);
279 static void add_encoding (struct outp_driver *this, char *filename);
280 static struct ps_encoding *default_encoding (struct outp_driver *this);
282 static int compare_ps_combo (const void *pa, const void *pb, void *foo);
283 static unsigned hash_ps_combo (const void *pa, void *foo);
284 static void free_ps_combo (void *a, void *foo);
286 static char *quote_ps_name (char *dest, const char *string);
287 static char *quote_ps_string (char *dest, const char *string);
289 /* Driver initialization. */
292 ps_open_global (struct outp_class *this UNUSED)
300 ps_close_global (struct outp_class *this UNUSED)
308 ps_font_sizes (struct outp_class *this UNUSED, int *n_valid_sizes)
310 /* Allow fonts up to 1" in height. */
311 static int valid_sizes[] =
314 assert (n_valid_sizes != NULL);
320 ps_preopen_driver (struct outp_driver *this)
322 struct ps_driver_ext *x;
326 assert (this->driver_open == 0);
327 msg (VM (1), _("PostScript driver initializing as `%s'..."), this->name);
329 this->ext = x = xmalloc (sizeof (struct ps_driver_ext));
331 this->horiz = this->vert = 1;
332 this->width = this->length = 0;
334 x->orientation = OTN_PORTRAIT;
335 x->output_options = OPO_COLOR | OPO_HEADERS | OPO_AUTO_ENCODE;
336 x->data = ODA_CLEAN7BIT;
338 x->left_margin = x->right_margin =
339 x->top_margin = x->bottom_margin = PSUS / 2;
341 strcpy (x->eol, "\n");
344 x->prologue_fn = NULL;
346 x->encoding_fn = NULL;
348 x->prop_family = NULL;
349 x->fixed_family = NULL;
350 x->font_size = PSUS * 10 / 72;
352 x->line_gutter = PSUS / 144;
353 x->line_space = PSUS / 144;
354 x->line_width = PSUS / 144;
355 x->line_width_thick = PSUS / 48;
361 x->file.filename = NULL;
364 x->file.sequence_no = &x->page_number;
365 x->file.param = this;
366 x->file.postopen = postopen;
367 x->file.preclose = preclose;
371 x->file_page_number = 0;
372 for (i = 0; i < n_line_types; i++)
383 x->encodings = hsh_create (31, compare_ps_encoding, hash_ps_encoding,
384 free_ps_encoding, NULL);
385 x->next_encoding = 0;
395 ps_postopen_driver (struct outp_driver *this)
397 struct ps_driver_ext *x = this->ext;
399 assert (this->driver_open == 0);
401 if (this->width == 0)
403 this->width = PSUS * 17 / 2; /* Defaults to 8.5"x11". */
404 this->length = PSUS * 11;
407 if (x->text_opt == -1)
408 x->text_opt = (this->device & OUTP_DEV_SCREEN) ? 0 : 1;
409 if (x->line_opt == -1)
410 x->line_opt = (this->device & OUTP_DEV_SCREEN) ? 0 : 1;
414 if (x->orientation == OTN_LANDSCAPE)
416 int temp = this->width;
417 this->width = this->length;
420 this->width -= x->left_margin + x->right_margin;
421 this->length -= x->top_margin + x->bottom_margin;
422 if (x->output_options & OPO_HEADERS)
424 this->length -= 3 * x->font_size;
425 x->top_margin += 3 * x->font_size;
427 if (NULL == x->file.filename)
428 x->file.filename = xstrdup ("pspp.ps");
430 if (x->font_dir == NULL)
431 x->font_dir = xstrdup ("devps");
432 if (x->prologue_fn == NULL)
433 x->prologue_fn = xstrdup ("ps-prologue");
434 if (x->desc_fn == NULL)
435 x->desc_fn = xstrdup ("DESC");
436 if (x->encoding_fn == NULL)
437 x->encoding_fn = xstrdup ("ps-encodings");
439 if (x->prop_family == NULL)
440 x->prop_family = xstrdup ("H");
441 if (x->fixed_family == NULL)
442 x->fixed_family = xstrdup ("C");
444 read_ps_encodings (this);
449 if (this->length / x->font_size < 15)
451 msg (SE, _("PostScript driver: The defined page is not long "
452 "enough to hold margins and headers, plus least 15 "
453 "lines of the default fonts. In fact, there's only "
454 "room for %d lines of each font at the default size "
455 "of %d.%03d points."),
456 this->length / x->font_size,
457 x->font_size / 1000, x->font_size % 1000);
461 this->driver_open = 1;
462 msg (VM (2), _("%s: Initialization complete."), this->name);
468 ps_close_driver (struct outp_driver *this)
470 struct ps_driver_ext *x = this->ext;
474 assert (this->driver_open == 1);
475 msg (VM (2), _("%s: Beginning closing..."), this->name);
477 fn_close_ext (&x->file);
478 free (x->file.filename);
480 free (x->prologue_fn);
482 free (x->encoding_fn);
483 free (x->prop_family);
484 free (x->fixed_family);
486 for (i = 0; i < n_line_types; i++)
487 hsh_destroy (x->lines[i]);
488 hsh_destroy (x->encodings);
489 hsh_destroy (x->combos);
490 hsh_destroy (x->loaded);
493 this->driver_open = 0;
494 msg (VM (3), _("%s: Finished closing."), this->name);
499 /* font_entry comparison function for hash tables. */
501 compare_font_entry (const void *a, const void *b, void *foobar UNUSED)
503 return strcmp (((struct font_entry *) a)->dit, ((struct font_entry *) b)->dit);
506 /* font_entry hash function for hash tables. */
508 hash_font_entry (const void *fe_, void *foobar UNUSED)
510 const struct font_entry *fe = fe_;
511 return hsh_hash_string (fe->dit);
514 /* font_entry destructor function for hash tables. */
516 free_font_entry (void *pa, void *foo UNUSED)
518 struct font_entry *a = pa;
523 /* Generic option types. */
533 /* All the options that the PostScript driver supports. */
534 static struct outp_option option_tab[] =
537 {"output-file", 1, 0},
538 {"paper-size", 2, 0},
539 {"orientation", 3, 0},
540 {"color", boolean_arg, 0},
542 {"auto-encode", boolean_arg, 5},
543 {"headers", boolean_arg, 1},
544 {"left-margin", pos_int_arg, 0},
545 {"right-margin", pos_int_arg, 1},
546 {"top-margin", pos_int_arg, 2},
547 {"bottom-margin", pos_int_arg, 3},
548 {"font-dir", string_arg, 0},
549 {"prologue-file", string_arg, 1},
550 {"device-file", string_arg, 2},
551 {"encoding-file", string_arg, 3},
552 {"prop-font-family", string_arg, 5},
553 {"fixed-font-family", string_arg, 6},
554 {"font-size", pos_int_arg, 4},
555 {"optimize-text-size", nonneg_int_arg, 0},
556 {"optimize-line-size", nonneg_int_arg, 1},
557 {"max-fonts-simult", nonneg_int_arg, 2},
559 {"line-style", 7, 0},
560 {"line-width", dimension_arg, 2},
561 {"line-gutter", dimension_arg, 3},
562 {"line-width", dimension_arg, 4},
563 {"line-width-thick", dimension_arg, 5},
567 static struct outp_option_info option_info;
570 ps_option (struct outp_driver *this, const char *key, const struct string *val)
572 struct ps_driver_ext *x = this->ext;
574 char *value = ds_c_str (val);
576 cat = outp_match_keyword (key, option_tab, &option_info, &subcat);
581 msg (SE, _("Unknown configuration parameter `%s' for PostScript device "
585 free (x->file.filename);
586 x->file.filename = xstrdup (value);
589 outp_get_paper_size (value, &this->width, &this->length);
592 if (!strcmp (value, "portrait"))
593 x->orientation = OTN_PORTRAIT;
594 else if (!strcmp (value, "landscape"))
595 x->orientation = OTN_LANDSCAPE;
597 msg (SE, _("Unknown orientation `%s'. Valid orientations are "
598 "`portrait' and `landscape'."), value);
601 if (!strcmp (value, "clean7bit") || !strcmp (value, "Clean7Bit"))
602 x->data = ODA_CLEAN7BIT;
603 else if (!strcmp (value, "clean8bit")
604 || !strcmp (value, "Clean8Bit"))
605 x->data = ODA_CLEAN8BIT;
606 else if (!strcmp (value, "binary") || !strcmp (value, "Binary"))
607 x->data = ODA_BINARY;
609 msg (SE, _("Unknown value for `data'. Valid values are `clean7bit', "
610 "`clean8bit', and `binary'."));
613 if (!strcmp (value, "lf"))
614 strcpy (x->eol, "\n");
615 else if (!strcmp (value, "crlf"))
616 strcpy (x->eol, "\r\n");
618 msg (SE, _("Unknown value for `line-ends'. Valid values are `lf' and "
622 if (!strcmp (value, "thick"))
623 x->output_options &= ~OPO_DOUBLE_LINE;
624 else if (!strcmp (value, "double"))
625 x->output_options |= OPO_DOUBLE_LINE;
627 msg (SE, _("Unknown value for `line-style'. Valid values are `thick' "
635 if (!strcmp (value, "on") || !strcmp (value, "true")
636 || !strcmp (value, "yes") || atoi (value))
638 else if (!strcmp (value, "off") || !strcmp (value, "false")
639 || !strcmp (value, "no") || !strcmp (value, "0"))
643 msg (SE, _("Boolean value expected for %s."), key);
655 mask = OPO_MIRROR_HORZ;
658 mask = OPO_MIRROR_VERT;
661 mask = OPO_ROTATE_180;
664 mask = OPO_AUTO_ENCODE;
671 x->output_options |= mask;
673 x->output_options &= ~mask;
682 arg = strtol (value, &tail, 0);
683 if (arg < 1 || errno == ERANGE || *tail)
685 msg (SE, _("Positive integer required as value for `%s'."), key);
688 if ((subcat == 4 || subcat == 5) && arg < 1000)
690 msg (SE, _("Default font size must be at least 1 point (value "
691 "of 1000 for key `%s')."), key);
697 x->left_margin = arg;
700 x->right_margin = arg;
706 x->bottom_margin = arg;
718 int dimension = outp_evaluate_dimension (value, NULL);
722 msg (SE, _("Value for `%s' must be a dimension of positive "
723 "length (i.e., `1in')."), key);
729 x->line_width = dimension;
732 x->line_gutter = dimension;
735 x->line_width = dimension;
738 x->line_width_thick = dimension;
754 dest = &x->prologue_fn;
760 dest = &x->encoding_fn;
763 dest = &x->prop_family;
766 dest = &x->fixed_family;
774 *dest = xstrdup (value);
783 arg = strtol (value, &tail, 0);
784 if (arg < 0 || errno == ERANGE || *tail)
786 msg (SE, _("Nonnegative integer required as value for `%s'."), key);
810 /* Looks for a PostScript font file or config file in all the
811 appropriate places. Returns the filename on success, NULL on
813 /* PORTME: Filename operations. */
815 find_ps_file (struct outp_driver *this, const char *name)
817 struct ps_driver_ext *x = this->ext;
820 /* x->font_dir + name: "devps/ps-encodings". */
823 /* Usually equal to groff_font_path. */
826 /* Final filename. */
830 basename = local_alloc (strlen (x->font_dir) + 1 + strlen (name) + 1);
831 cp = stpcpy (basename, x->font_dir);
832 *cp++ = DIR_SEPARATOR;
835 /* Decide on search path. */
837 const char *pre_pathname;
839 pre_pathname = getenv ("STAT_GROFF_FONT_PATH");
840 if (pre_pathname == NULL)
841 pre_pathname = getenv ("GROFF_FONT_PATH");
842 if (pre_pathname == NULL)
843 pre_pathname = groff_font_path;
844 pathname = fn_tilde_expand (pre_pathname);
847 /* Search all possible places for the file. */
848 fn = fn_search_path (basename, pathname, NULL);
850 fn = fn_search_path (basename, config_path, NULL);
852 fn = fn_search_path (name, pathname, NULL);
854 fn = fn_search_path (name, config_path, NULL);
856 local_free (basename);
863 /* Hash table comparison function for ps_encoding's. */
865 compare_ps_encoding (const void *pa, const void *pb, void *foo UNUSED)
867 const struct ps_encoding *a = pa;
868 const struct ps_encoding *b = pb;
870 return strcmp (a->filename, b->filename);
873 /* Hash table hash function for ps_encoding's. */
875 hash_ps_encoding (const void *pa, void *foo UNUSED)
877 const struct ps_encoding *a = pa;
879 return hsh_hash_string (a->filename);
882 /* Hash table free function for ps_encoding's. */
884 free_ps_encoding (void *pa, void *foo UNUSED)
886 struct ps_encoding *a = pa;
892 /* Iterates through the list of encodings used for this driver
893 instance, reads each of them from disk, and writes them as
894 PostScript code to the output file. */
896 output_encodings (struct outp_driver *this)
898 struct ps_driver_ext *x = this->ext;
900 struct hsh_iterator iter;
901 struct ps_encoding *pe;
903 struct string line, buf;
905 ds_init (&line, 128);
907 for (pe = hsh_first (x->encodings, &iter); pe != NULL;
908 pe = hsh_next (x->encodings, &iter))
912 msg (VM (1), _("%s: %s: Opening PostScript font encoding..."),
913 this->name, pe->filename);
915 f = fopen (pe->filename, "r");
918 msg (IE, _("PostScript driver: Cannot open encoding file `%s': %s. "
919 "Substituting ISOLatin1Encoding for missing encoding."),
920 pe->filename, strerror (errno));
921 fprintf (x->file.file, "/E%x ISOLatin1Encoding def%s",
926 struct file_locator where;
928 const char *tab[256];
935 const char *notdef = ".notdef";
939 for (i = 0; i < 256; i++)
942 where.filename = pe->filename;
943 where.line_number = 0;
944 err_push_file_locator (&where);
946 while (ds_get_config_line (f, &buf, &where))
953 pschar = strtok_r (ds_c_str (&buf), " \t\r\n", &sp);
954 code = strtok_r (NULL, " \t\r\n", &sp);
955 if (*pschar == 0 || *code == 0)
957 code_val = strtol (code, &fubar, 0);
960 msg (IS, _("PostScript driver: Invalid numeric format."));
963 if (code_val < 0 || code_val > 255)
965 msg (IS, _("PostScript driver: Codes must be between 0 "
966 "and 255. (%d is not allowed.)"), code_val);
969 tab[code_val] = local_alloc (strlen (pschar) + 1);
970 strcpy ((char *) (tab[code_val]), pschar);
972 err_pop_file_locator (&where);
975 ds_printf (&line, "/E%x[", pe->index);
976 for (i = 0; i < 257; i++)
982 quote_ps_name (temp, tab[i]);
983 if (tab[i] != notdef)
987 strcpy (temp, "]def");
989 if (ds_length (&line) + strlen (temp) > 70)
991 ds_puts (&line, x->eol);
992 fputs (ds_c_str (&line), x->file.file);
995 ds_puts (&line, temp);
997 ds_puts (&line, x->eol);
998 fputs (ds_c_str (&line), x->file.file);
1000 if (fclose (f) == EOF)
1001 msg (MW, _("PostScript driver: Error closing encoding file `%s'."),
1004 msg (VM (2), _("%s: PostScript font encoding read successfully."),
1012 /* Finds the ps_encoding in THIS that corresponds to the file with
1013 name NORM_FILENAME, which must have previously been normalized with
1014 normalize_filename(). */
1015 static struct ps_encoding *
1016 get_encoding (struct outp_driver *this, const char *norm_filename)
1018 struct ps_driver_ext *x = this->ext;
1019 struct ps_encoding *pe;
1021 pe = (struct ps_encoding *) hsh_find (x->encodings, (void *) &norm_filename);
1025 /* Searches the filesystem for an encoding file with name FILENAME;
1026 returns its malloc'd, normalized name if found, otherwise NULL. */
1028 find_encoding_file (struct outp_driver *this, char *filename)
1032 if (filename == NULL)
1034 while (isspace ((unsigned char) *filename))
1036 for (cp = filename; *cp && !isspace ((unsigned char) *cp); cp++)
1042 temp = find_ps_file (this, filename);
1046 filename = fn_normalize (temp);
1047 assert (filename != NULL);
1053 /* Adds the encoding represented by the not-necessarily-normalized
1054 file FILENAME to the list of encodings, if it exists and is not
1055 already in the list. */
1057 add_encoding (struct outp_driver *this, char *filename)
1059 struct ps_driver_ext *x = this->ext;
1060 struct ps_encoding **pe;
1062 filename = find_encoding_file (this, filename);
1066 pe = (struct ps_encoding **) hsh_probe (x->encodings, &filename);
1072 *pe = xmalloc (sizeof **pe);
1073 (*pe)->filename = filename;
1074 (*pe)->index = x->next_encoding++;
1077 /* Finds the file on disk that contains the list of encodings to
1078 include in the output file, then adds those encodings to the list
1081 read_ps_encodings (struct outp_driver *this)
1083 struct ps_driver_ext *x = this->ext;
1085 /* Encodings file. */
1086 char *encoding_fn; /* `ps-encodings' filename. */
1090 struct file_locator where;
1092 /* It's okay if there's no list of encodings; not everyone cares. */
1093 encoding_fn = find_ps_file (this, x->encoding_fn);
1094 if (encoding_fn == NULL)
1098 msg (VM (1), _("%s: %s: Opening PostScript encoding list file."),
1099 this->name, encoding_fn);
1100 f = fopen (encoding_fn, "r");
1103 msg (IE, _("Opening %s: %s."), encoding_fn, strerror (errno));
1107 where.filename = encoding_fn;
1108 where.line_number = 0;
1109 err_push_file_locator (&where);
1111 ds_init (&line, 128);
1115 if (!ds_get_config_line (f, &line, &where))
1118 msg (ME, _("Reading %s: %s."), encoding_fn, strerror (errno));
1122 add_encoding (this, line.string);
1126 err_pop_file_locator (&where);
1128 if (-1 == fclose (f))
1129 msg (MW, _("Closing %s: %s."), encoding_fn, strerror (errno));
1131 msg (VM (2), _("%s: PostScript encoding list file read successfully."), this->name);
1134 /* Creates a default encoding for driver D that can be substituted for
1135 an unavailable encoding. */
1136 struct ps_encoding *
1137 default_encoding (struct outp_driver *d)
1139 struct ps_driver_ext *x = d->ext;
1140 static struct ps_encoding *enc;
1144 enc = xmalloc (sizeof *enc);
1145 enc->filename = xstrdup (_("<<default encoding>>"));
1146 enc->index = x->next_encoding++;
1151 /* Basic file operations. */
1153 /* Variables for the prologue. */
1160 static struct ps_variable *ps_var_tab;
1162 /* Searches ps_var_tab for a ps_variable with key KEY, and returns the
1163 associated value. */
1165 ps_get_var (const char *key)
1167 struct ps_variable *v;
1169 for (v = ps_var_tab; v->key; v++)
1170 if (!strcmp (key, v->key))
1175 /* Writes the PostScript prologue to file F. */
1177 postopen (struct file_ext *f)
1179 static struct ps_variable dict[] =
1181 {"bounding-box", 0},
1190 {"scale-factor", 0},
1192 {"paper-length", 0},
1196 {"line-width-thick", 0},
1201 char boundbox[INT_DIGITS * 4 + 4];
1205 char scaling[INT_DIGITS + 5];
1209 char paper_width[INT_DIGITS + 1];
1210 char paper_length[INT_DIGITS + 1];
1211 char left_margin[INT_DIGITS + 1];
1212 char top_margin[INT_DIGITS + 1];
1213 char line_width[INT_DIGITS + 1];
1214 char line_width_thick[INT_DIGITS + 1];
1216 struct outp_driver *this = f->param;
1217 struct ps_driver_ext *x = this->ext;
1219 char *prologue_fn = find_ps_file (this, x->prologue_fn);
1220 FILE *prologue_file;
1223 size_t buf_size = 0;
1225 x->loaded = hsh_create (31, compare_font_entry, hash_font_entry,
1226 free_font_entry, NULL);
1229 char *font_name = local_alloc (2 + max (strlen (x->prop_family),
1230 strlen (x->fixed_family)));
1232 strcpy (stpcpy (font_name, x->prop_family), "R");
1233 x->prop = load_font (this, font_name);
1235 strcpy (stpcpy (font_name, x->fixed_family), "R");
1236 x->fixed = load_font (this, font_name);
1238 local_free(font_name);
1241 x->current = x->prop;
1242 x->family = xstrdup (x->prop_family);
1243 x->size = x->font_size;
1246 int *h = this->horiz_line_width, *v = this->vert_line_width;
1248 this->cp_x = this->cp_y = 0;
1249 this->font_height = x->font_size;
1251 struct char_metrics *metric;
1253 metric = font_get_char_metrics (x->prop->font, '0');
1254 this->prop_em_width = ((metric
1255 ? metric->width : x->prop->font->space_width)
1256 * x->font_size / 1000);
1258 metric = font_get_char_metrics (x->fixed->font, '0');
1259 this->fixed_width = ((metric
1260 ? metric->width : x->fixed->font->space_width)
1261 * x->font_size / 1000);
1265 h[1] = v[1] = 2 * x->line_gutter + x->line_width;
1266 if (x->output_options & OPO_DOUBLE_LINE)
1267 h[2] = v[2] = 2 * x->line_gutter + 2 * x->line_width + x->line_space;
1269 h[2] = v[2] = 2 * x->line_gutter + x->line_width_thick;
1270 h[3] = v[3] = 2 * x->line_gutter + x->line_width;
1275 for (i = 0; i < (1 << OUTP_L_COUNT); i++)
1279 /* Maximum width of any line type so far. */
1282 for (bit = 0; bit < OUTP_L_COUNT; bit++)
1283 if ((i & (1 << bit)) && h[bit] > max)
1285 this->horiz_line_spacing[i] = this->vert_line_spacing[i] = max;
1290 if (x->output_options & OPO_AUTO_ENCODE)
1292 /* It's okay if this is done more than once since add_encoding()
1293 is idempotent over identical encodings. */
1294 add_encoding (this, x->prop->font->encoding);
1295 add_encoding (this, x->fixed->font->encoding);
1298 x->file_page_number = 0;
1301 if (prologue_fn == NULL)
1303 msg (IE, _("Cannot find PostScript prologue. The use of `-vv' "
1304 "on the command line is suggested as a debugging aid."));
1308 msg (VM (1), _("%s: %s: Opening PostScript prologue..."),
1309 this->name, prologue_fn);
1310 prologue_file = fopen (prologue_fn, "rb");
1311 if (prologue_file == NULL)
1313 fclose (prologue_file);
1315 msg (IE, "%s: %s", prologue_fn, strerror (errno));
1319 sprintf (boundbox, "0 0 %d %d",
1320 x->w / (PSUS / 72) + (x->w % (PSUS / 72) > 0),
1321 x->l / (PSUS / 72) + (x->l % (PSUS / 72) > 0));
1322 dict[0].value = boundbox;
1324 dict[1].value = (char *) version;
1326 curtime = time (NULL);
1327 loctime = localtime (&curtime);
1328 dict[2].value = asctime (loctime);
1329 cp = strchr (dict[2].value, '\n');
1336 dict[3].value = "Clean7Bit";
1339 dict[3].value = "Clean8Bit";
1342 dict[3].value = "Binary";
1348 if (x->orientation == OTN_PORTRAIT)
1349 dict[4].value = "Portrait";
1351 dict[4].value = "Landscape";
1353 /* PORTME: Determine username, net address. */
1355 dict[5].value = getenv ("LOGNAME");
1357 dict[5].value = getlogin ();
1359 dict[5].value = _("nobody");
1361 if (gethostname (host, 128) == -1)
1363 if (errno == ENAMETOOLONG)
1366 strcpy (host, _("nowhere"));
1368 dict[6].value = host;
1369 #else /* !HAVE_UNISTD_H */
1370 dict[5].value = _("nobody");
1371 dict[6].value = _("nowhere");
1372 #endif /* !HAVE_UNISTD_H */
1374 cp = stpcpy (p = local_alloc (288), "font ");
1375 quote_ps_string (cp, x->prop->font->internal_name);
1378 cp = stpcpy (p = local_alloc (288), "font ");
1379 quote_ps_string (cp, x->fixed->font->internal_name);
1382 sprintf (scaling, "%.3f", PSUS / 72.0);
1383 dict[9].value = scaling;
1385 sprintf (paper_width, "%g", x->w / (PSUS / 72.0));
1386 dict[10].value = paper_width;
1388 sprintf (paper_length, "%g", x->l / (PSUS / 72.0));
1389 dict[11].value = paper_length;
1391 sprintf (left_margin, "%d", x->left_margin);
1392 dict[12].value = left_margin;
1394 sprintf (top_margin, "%d", x->top_margin);
1395 dict[13].value = top_margin;
1397 sprintf (line_width, "%d", x->line_width);
1398 dict[14].value = line_width;
1400 sprintf (line_width, "%d", x->line_width_thick);
1401 dict[15].value = line_width_thick;
1403 getl_location (&dict[17].value, NULL);
1404 if (dict[17].value == NULL)
1405 dict[17].value = "<stdin>";
1409 dict[16].value = cp = local_alloc (strlen (dict[17].value) + 30);
1410 sprintf (cp, "PSPP (%s)", dict[17].value);
1414 dict[16].value = local_alloc (strlen (outp_title) + 1);
1415 strcpy ((char *) (dict[16].value), outp_title);
1419 while (-1 != getline (&buf, &buf_size, prologue_file))
1425 cp = strstr (buf, "!eps");
1428 if (this->class->magic == MAGIC_PS)
1435 cp = strstr (buf, "!ps");
1438 if (this->class->magic == MAGIC_EPSF)
1443 if (strstr (buf, "!!!"))
1448 if (!strncmp (buf, "!encodings", 10))
1449 output_encodings (this);
1453 beg = buf2 = fn_interp_vars (buf, ps_get_var);
1454 len = strlen (buf2);
1455 while (isspace ((unsigned char) *beg))
1457 if (beg[len - 1] == '\n')
1459 if (beg[len - 1] == '\r')
1461 fwrite (beg, len, 1, f->file);
1462 fputs (x->eol, f->file);
1466 if (ferror (f->file))
1467 msg (IE, _("Reading `%s': %s."), prologue_fn, strerror (errno));
1468 fclose (prologue_file);
1473 local_free (dict[7].value);
1474 local_free (dict[8].value);
1475 local_free (dict[16].value);
1477 if (ferror (f->file))
1480 msg (VM (2), _("%s: PostScript prologue read successfully."), this->name);
1484 msg (VM (1), _("%s: Error reading PostScript prologue."), this->name);
1488 /* Writes the string STRING to buffer DEST (of at least 288
1489 characters) as a PostScript name object. Returns a pointer
1490 to the null terminator of the resultant string. */
1492 quote_ps_name (char *dest, const char *string)
1496 for (sp = string; *sp; sp++)
1497 switch (*(unsigned char *) sp)
1570 for (sp = string; *sp && dp < &dest[256]; sp++)
1572 sprintf (dp, "%02x", *(unsigned char *) sp);
1575 return stpcpy (dp, ">cvn");
1579 return stpcpy (&dest[1], string);
1582 /* Adds the string STRING to buffer DEST as a PostScript quoted
1583 string; returns a pointer to the null terminator added. Will not
1584 add more than 235 characters. */
1586 quote_ps_string (char *dest, const char *string)
1588 const char *sp = string;
1592 for (; *sp && dp < &dest[235]; sp++)
1594 dp = stpcpy (dp, "\\(");
1595 else if (*sp == ')')
1596 dp = stpcpy (dp, "\\)");
1597 else if (*sp < 32 || *((unsigned char *) sp) > 127)
1598 dp = spprintf (dp, "\\%3o", *sp);
1601 return stpcpy (dp, ")");
1604 /* Writes the PostScript epilogue to file F. */
1606 preclose (struct file_ext *f)
1608 struct outp_driver *this = f->param;
1609 struct ps_driver_ext *x = this->ext;
1610 struct hsh_iterator iter;
1611 struct font_entry *fe;
1616 "%%%%DocumentNeededResources:%s"),
1617 x->eol, x->file_page_number, x->eol, x->eol);
1619 for (fe = hsh_first (x->loaded, &iter); fe != NULL;
1620 fe = hsh_next (x->loaded, &iter))
1624 cp = stpcpy (buf, "%%+ font ");
1625 cp = quote_ps_string (cp, fe->font->internal_name);
1626 strcpy (cp, x->eol);
1627 fputs (buf, f->file);
1630 hsh_destroy (x->loaded);
1632 hsh_destroy (x->combos);
1634 x->last_font = NULL;
1637 fprintf (f->file, "%%EOF%s", x->eol);
1638 if (ferror (f->file))
1644 ps_open_page (struct outp_driver *this)
1646 struct ps_driver_ext *x = this->ext;
1648 assert (this->driver_open && !this->page_open);
1651 if (!fn_open_ext (&x->file))
1654 msg (ME, _("PostScript output driver: %s: %s"), x->file.filename,
1658 x->file_page_number++;
1660 hsh_destroy (x->combos);
1661 x->combos = hsh_create (31, compare_ps_combo, hash_ps_combo,
1662 free_ps_combo, NULL);
1663 x->last_font = NULL;
1666 fprintf (x->file.file,
1668 "%%%%BeginPageSetup%s"
1669 "/pg save def 0.001 dup scale%s",
1670 x->page_number, x->file_page_number, x->eol,
1674 if (x->orientation == OTN_LANDSCAPE)
1675 fprintf (x->file.file,
1676 "%d 0 translate 90 rotate%s",
1679 if (x->bottom_margin != 0 || x->left_margin != 0)
1680 fprintf (x->file.file,
1681 "%d %d translate%s",
1682 x->left_margin, x->bottom_margin, x->eol);
1684 fprintf (x->file.file,
1685 "/LW %d def/TW %d def %d setlinewidth%s"
1686 "%%%%EndPageSetup%s",
1687 x->line_width, x->line_width_thick, x->line_width, x->eol,
1690 if (!ferror (x->file.file))
1692 this->page_open = 1;
1693 if (x->output_options & OPO_HEADERS)
1694 draw_headers (this);
1699 return !ferror (x->file.file);
1703 ps_close_page (struct outp_driver *this)
1705 struct ps_driver_ext *x = this->ext;
1707 assert (this->driver_open && this->page_open);
1712 fprintf (x->file.file,
1717 this->page_open = 0;
1718 return !ferror (x->file.file);
1722 ps_submit (struct outp_driver *this UNUSED, struct som_entity *s)
1736 /* qsort() comparison function for int tuples. */
1738 int_2_compare (const void *a_, const void *b_)
1743 return *a < *b ? -1 : *a > *b;
1746 /* Hash table comparison function for cached lines. */
1748 compare_line (const void *a_, const void *b_, void *foo UNUSED)
1750 const struct line_form *a = a_;
1751 const struct line_form *b = b_;
1753 return a->ind < b->ind ? -1 : a->ind > b->ind;
1756 /* Hash table hash function for cached lines. */
1758 hash_line (const void *pa, void *foo UNUSED)
1760 const struct line_form *a = pa;
1765 /* Hash table free function for cached lines. */
1767 free_line (void *pa, void *foo UNUSED)
1772 /* Writes PostScript code to draw a line from (x1,y1) to (x2,y2) to
1774 #define dump_line(x1, y1, x2, y2) \
1775 fprintf (ext->file.file, "%d %d %d %d L%s", \
1776 x1, YT (y1), x2, YT (y2), ext->eol)
1778 /* Write PostScript code to draw a thick line from (x1,y1) to (x2,y2)
1779 to the output file. */
1780 #define dump_thick_line(x1, y1, x2, y2) \
1781 fprintf (ext->file.file, "%d %d %d %d TL%s", \
1782 x1, YT (y1), x2, YT (y2), ext->eol)
1784 /* Writes a line of type TYPE to THIS driver's output file. The line
1785 (or its center, in the case of double lines) has its independent
1786 axis coordinate at IND; it extends from DEP1 to DEP2 on the
1789 dump_fancy_line (struct outp_driver *this, int type, int ind, int dep1, int dep2)
1791 struct ps_driver_ext *ext = this->ext;
1792 int ofs = ext->line_space / 2 + ext->line_width / 2;
1797 dump_line (dep1, ind, dep2, ind);
1800 if (ext->output_options & OPO_DOUBLE_LINE)
1802 dump_line (dep1, ind - ofs, dep2, ind - ofs);
1803 dump_line (dep1, ind + ofs, dep2, ind + ofs);
1806 dump_thick_line (dep1, ind, dep2, ind);
1811 dump_line (ind, dep1, ind, dep2);
1814 if (ext->output_options & OPO_DOUBLE_LINE)
1816 dump_line (ind - ofs, dep1, ind - ofs, dep2);
1817 dump_line (ind + ofs, dep1, ind + ofs, dep2);
1820 dump_thick_line (ind, dep1, ind, dep2);
1831 /* Writes all the cached lines to the output file, then clears the
1834 dump_lines (struct outp_driver *this)
1836 struct ps_driver_ext *x = this->ext;
1838 struct hsh_iterator iter;
1841 for (type = 0; type < n_line_types; type++)
1843 struct line_form *line;
1845 if (x->lines[type] == NULL)
1848 for (line = hsh_first (x->lines[type], &iter); line != NULL;
1849 line = hsh_next (x->lines[type], &iter))
1852 int lo = INT_MIN, hi;
1854 qsort (line->dep, line->ndep, sizeof *line->dep, int_2_compare);
1855 lo = line->dep[0][0];
1856 hi = line->dep[0][1];
1857 for (i = 1; i < line->ndep; i++)
1858 if (line->dep[i][0] <= hi + 1)
1860 int min_hi = line->dep[i][1];
1866 dump_fancy_line (this, type, line->ind, lo, hi);
1867 lo = line->dep[i][0];
1868 hi = line->dep[i][1];
1870 dump_fancy_line (this, type, line->ind, lo, hi);
1873 hsh_destroy (x->lines[type]);
1874 x->lines[type] = NULL;
1878 /* (Same args as dump_fancy_line()). Either dumps the line directly
1879 to the output file, or adds it to the cache, depending on the
1880 user-selected line optimization mode. */
1882 line (struct outp_driver *this, int type, int ind, int dep1, int dep2)
1884 struct ps_driver_ext *ext = this->ext;
1885 struct line_form **f;
1887 assert (dep2 >= dep1);
1888 if (ext->line_opt == 0)
1890 dump_fancy_line (this, type, ind, dep1, dep2);
1894 if (ext->lines[type] == NULL)
1895 ext->lines[type] = hsh_create (31, compare_line, hash_line,
1897 f = (struct line_form **) hsh_probe (ext->lines[type], &ind);
1900 *f = xmalloc (sizeof **f + sizeof (int[15][2]));
1904 (*f)->dep[0][0] = dep1;
1905 (*f)->dep[0][1] = dep2;
1908 if ((*f)->ndep >= (*f)->mdep)
1911 *f = xrealloc (*f, (sizeof **f + sizeof (int[2]) * ((*f)->mdep - 1)));
1913 (*f)->dep[(*f)->ndep][0] = dep1;
1914 (*f)->dep[(*f)->ndep][1] = dep2;
1919 ps_line_horz (struct outp_driver *this, const struct rect *r,
1920 const struct color *c UNUSED, int style)
1922 /* Must match output.h:OUTP_L_*. */
1923 static const int types[OUTP_L_COUNT] =
1924 {-1, horz, dbl_horz, spl_horz};
1926 int y = (r->y1 + r->y2) / 2;
1928 assert (this->driver_open && this->page_open);
1929 assert (style >= 0 && style < OUTP_L_COUNT);
1930 style = types[style];
1932 line (this, style, y, r->x1, r->x2);
1936 ps_line_vert (struct outp_driver *this, const struct rect *r,
1937 const struct color *c UNUSED, int style)
1939 /* Must match output.h:OUTP_L_*. */
1940 static const int types[OUTP_L_COUNT] =
1941 {-1, vert, dbl_vert, spl_vert};
1943 int x = (r->x1 + r->x2) / 2;
1945 assert (this->driver_open && this->page_open);
1946 assert (style >= 0 && style < OUTP_L_COUNT);
1947 style = types[style];
1949 line (this, style, x, r->y1, r->y2);
1952 #define L (style->l != OUTP_L_NONE)
1953 #define R (style->r != OUTP_L_NONE)
1954 #define T (style->t != OUTP_L_NONE)
1955 #define B (style->b != OUTP_L_NONE)
1958 ps_line_intersection (struct outp_driver *this, const struct rect *r,
1959 const struct color *c UNUSED,
1960 const struct outp_styles *style)
1962 struct ps_driver_ext *ext = this->ext;
1964 int x = (r->x1 + r->x2) / 2;
1965 int y = (r->y1 + r->y2) / 2;
1966 int ofs = (ext->line_space + ext->line_width) / 2;
1967 int x1 = x - ofs, x2 = x + ofs;
1968 int y1 = y - ofs, y2 = y + ofs;
1970 assert (this->driver_open && this->page_open);
1971 assert (!((style->l != style->r && style->l != OUTP_L_NONE
1972 && style->r != OUTP_L_NONE)
1973 || (style->t != style->b && style->t != OUTP_L_NONE
1974 && style->b != OUTP_L_NONE)));
1976 switch ((style->l | style->r) | ((style->t | style->b) << 8))
1978 case (OUTP_L_SINGLE) | (OUTP_L_SINGLE << 8):
1979 case (OUTP_L_SINGLE) | (OUTP_L_NONE << 8):
1980 case (OUTP_L_NONE) | (OUTP_L_SINGLE << 8):
1982 line (this, horz, y, r->x1, x);
1984 line (this, horz, y, x, r->x2);
1986 line (this, vert, x, r->y1, y);
1988 line (this, vert, x, y, r->y2);
1990 case (OUTP_L_SINGLE) | (OUTP_L_DOUBLE << 8):
1991 case (OUTP_L_NONE) | (OUTP_L_DOUBLE << 8):
1993 line (this, horz, y, r->x1, x1);
1995 line (this, horz, y, x2, r->x2);
1997 line (this, dbl_vert, x, r->y1, y);
1999 line (this, dbl_vert, x, y, r->y2);
2000 if ((L && R) && !(T && B))
2001 line (this, horz, y, x1, x2);
2003 case (OUTP_L_DOUBLE) | (OUTP_L_SINGLE << 8):
2004 case (OUTP_L_DOUBLE) | (OUTP_L_NONE << 8):
2006 line (this, dbl_horz, y, r->x1, x);
2008 line (this, dbl_horz, y, x, r->x2);
2010 line (this, vert, x, r->y1, y);
2012 line (this, vert, x, y, r->y2);
2013 if ((T && B) && !(L && R))
2014 line (this, vert, x, y1, y2);
2016 case (OUTP_L_DOUBLE) | (OUTP_L_DOUBLE << 8):
2018 line (this, dbl_horz, y, r->x1, x);
2020 line (this, dbl_horz, y, x, r->x2);
2022 line (this, dbl_vert, x, r->y1, y);
2024 line (this, dbl_vert, x, y, r->y2);
2026 line (this, vert, x1, y1, y2);
2028 line (this, vert, x2, y1, y2);
2030 line (this, horz, y1, x1, x2);
2032 line (this, horz, y2, x1, x2);
2040 ps_box (struct outp_driver *this UNUSED, const struct rect *r UNUSED,
2041 const struct color *bord UNUSED, const struct color *fill UNUSED)
2043 assert (this->driver_open && this->page_open);
2047 ps_polyline_begin (struct outp_driver *this UNUSED,
2048 const struct color *c UNUSED)
2050 assert (this->driver_open && this->page_open);
2053 ps_polyline_point (struct outp_driver *this UNUSED, int x UNUSED, int y UNUSED)
2055 assert (this->driver_open && this->page_open);
2058 ps_polyline_end (struct outp_driver *this UNUSED)
2060 assert (this->driver_open && this->page_open);
2063 /* Returns the width of string S for THIS driver. */
2065 text_width (struct outp_driver *this, char *s)
2067 struct outp_text text;
2069 text.options = OUTP_T_JUST_LEFT;
2070 ls_init (&text.s, s, strlen (s));
2071 this->class->text_metrics (this, &text);
2075 /* Write string S at location (X,Y) with width W for THIS driver. */
2077 out_text_plain (struct outp_driver *this, char *s, int x, int y, int w)
2079 struct outp_text text;
2081 text.options = OUTP_T_JUST_LEFT | OUTP_T_HORZ | OUTP_T_VERT;
2082 ls_init (&text.s, s, strlen (s));
2084 text.v = this->font_height;
2087 this->class->text_draw (this, &text);
2090 /* Draw top of page headers for THIS driver. */
2092 draw_headers (struct outp_driver *this)
2094 struct ps_driver_ext *ext = this->ext;
2096 struct font_entry *old_current = ext->current;
2097 char *old_family = xstrdup (ext->family); /* FIXME */
2098 int old_size = ext->size;
2100 int fh = this->font_height;
2103 fprintf (ext->file.file, "%d %d %d %d GB%s",
2104 0, YT (y), this->width, YT (y + 2 * fh + ext->line_gutter),
2106 this->class->text_set_font_family (this, "T");
2108 y += ext->line_width + ext->line_gutter;
2114 sprintf (buf, _("%s - Page %d"), curdate, ext->page_number);
2115 rh_width = text_width (this, buf);
2117 out_text_plain (this, buf, this->width - this->prop_em_width - rh_width,
2120 if (outp_title && outp_subtitle)
2121 out_text_plain (this, outp_title, this->prop_em_width, y,
2122 this->width - 3 * this->prop_em_width - rh_width);
2130 char *string = outp_subtitle ? outp_subtitle : outp_title;
2132 sprintf (buf, "%s - %s", version, host_system);
2133 rh_width = text_width (this, buf);
2135 out_text_plain (this, buf, this->width - this->prop_em_width - rh_width,
2139 out_text_plain (this, string, this->prop_em_width, y,
2140 this->width - 3 * this->prop_em_width - rh_width);
2145 ext->current = old_current;
2147 ext->family = old_family;
2148 ext->size = old_size;
2155 ps_text_set_font_by_name (struct outp_driver *this, const char *dit)
2157 struct ps_driver_ext *x = this->ext;
2158 struct font_entry *fe;
2160 assert (this->driver_open && this->page_open);
2162 /* Short-circuit common fonts. */
2163 if (!strcmp (dit, "PROP"))
2165 x->current = x->prop;
2166 x->size = x->font_size;
2169 else if (!strcmp (dit, "FIXED"))
2171 x->current = x->fixed;
2172 x->size = x->font_size;
2176 /* Find font_desc corresponding to Groff name dit. */
2177 fe = hsh_find (x->loaded, &dit);
2179 fe = load_font (this, dit);
2184 ps_text_set_font_by_position (struct outp_driver *this, int pos)
2186 struct ps_driver_ext *x = this->ext;
2189 assert (this->driver_open && this->page_open);
2191 /* Determine font name by suffixing position string to font family
2196 dit = local_alloc (strlen (x->family) + 3);
2197 cp = stpcpy (dit, x->family);
2219 /* Find font_desc corresponding to Groff name dit. */
2221 struct font_entry *fe = hsh_find (x->loaded, &dit);
2223 fe = load_font (this, dit);
2231 ps_text_set_font_family (struct outp_driver *this, const char *s)
2233 struct ps_driver_ext *x = this->ext;
2235 assert (this->driver_open && this->page_open);
2238 x->family = xstrdup (s);
2242 ps_text_get_font_name (struct outp_driver *this)
2244 struct ps_driver_ext *x = this->ext;
2246 assert (this->driver_open && this->page_open);
2247 return x->current->font->name;
2251 ps_text_get_font_family (struct outp_driver *this)
2253 struct ps_driver_ext *x = this->ext;
2255 assert (this->driver_open && this->page_open);
2260 ps_text_set_size (struct outp_driver *this, int size)
2262 struct ps_driver_ext *x = this->ext;
2264 assert (this->driver_open && this->page_open);
2265 x->size = PSUS / 72000 * size;
2270 ps_text_get_size (struct outp_driver *this, int *em_width)
2272 struct ps_driver_ext *x = this->ext;
2274 assert (this->driver_open && this->page_open);
2276 *em_width = (x->current->font->space_width * x->size) / 1000;
2277 return x->size / (PSUS / 72000);
2280 /* An output character. */
2283 struct font_entry *font; /* Font of character. */
2284 int size; /* Size of character. */
2285 int x, y; /* Location of character. */
2286 unsigned char ch; /* Character. */
2287 char separate; /* Must be separate from previous char. */
2290 /* Hash table comparison function for ps_combo structs. */
2292 compare_ps_combo (const void *pa, const void *pb, void *foo UNUSED)
2294 const struct ps_font_combo *a = pa;
2295 const struct ps_font_combo *b = pb;
2297 return !((a->font == b->font) && (a->size == b->size));
2300 /* Hash table hash function for ps_combo structs. */
2302 hash_ps_combo (const void *pa, void *foo UNUSED)
2304 const struct ps_font_combo *a = pa;
2305 unsigned name_hash = hsh_hash_string (a->font->font->internal_name);
2306 return name_hash ^ hsh_hash_int (a->size);
2309 /* Hash table free function for ps_combo structs. */
2311 free_ps_combo (void *a, void *foo UNUSED)
2316 /* Causes PostScript code to be output that switches to the font
2317 CP->FONT and font size CP->SIZE. The first time a particular
2318 font/size combination is used on a particular page, this involves
2319 outputting PostScript code to load the font. */
2321 switch_font (struct outp_driver *this, const struct output_char *cp)
2323 struct ps_driver_ext *ext = this->ext;
2324 struct ps_font_combo srch, **fc;
2326 srch.font = cp->font;
2327 srch.size = cp->size;
2329 fc = (struct ps_font_combo **) hsh_probe (ext->combos, &srch);
2332 fprintf (ext->file.file, "F%x%s", (*fc)->index, ext->eol);
2337 struct ps_encoding *encoding;
2340 *fc = xmalloc (sizeof **fc);
2341 (*fc)->font = cp->font;
2342 (*fc)->size = cp->size;
2343 (*fc)->index = ext->next_combo++;
2345 filename = find_encoding_file (this, cp->font->font->encoding);
2348 encoding = get_encoding (this, filename);
2353 msg (IE, _("PostScript driver: Cannot find encoding `%s' for "
2354 "PostScript font `%s'."), cp->font->font->encoding,
2355 cp->font->font->internal_name);
2356 encoding = default_encoding (this);
2359 if (cp->font != ext->fixed && cp->font != ext->prop)
2361 bp = stpcpy (buf, "%%IncludeResource: font ");
2362 bp = quote_ps_string (bp, cp->font->font->internal_name);
2363 bp = stpcpy (bp, ext->eol);
2368 bp = spprintf (bp, "/F%x E%x %d", (*fc)->index, encoding->index,
2370 bp = quote_ps_name (bp, cp->font->font->internal_name);
2371 sprintf (bp, " SF%s", ext->eol);
2372 fputs (buf, ext->file.file);
2374 ext->last_font = *fc;
2377 /* (write_text) Writes the accumulated line buffer to the output
2379 #define output_line() \
2382 lp = stpcpy (lp, ext->eol); \
2384 fputs (line, ext->file.file); \
2389 /* (write_text) Adds the string representing number X to the line
2390 buffer, flushing the buffer to disk beforehand if necessary. */
2391 #define put_number(X) \
2394 int n = nsprintf (number, "%d", X); \
2395 if (n + lp > &line[75]) \
2397 lp = stpcpy (lp, number); \
2401 /* Outputs PostScript code to THIS driver's output file to display the
2402 characters represented by the output_char's between CP and END,
2403 using the associated outp_text T to determine formatting. WIDTH is
2404 the width of the output region; WIDTH_LEFT is the amount of the
2405 WIDTH that is not taken up by text (so it can be used to determine
2408 write_text (struct outp_driver *this,
2409 const struct output_char *cp, const struct output_char *end,
2410 struct outp_text *t, int width UNUSED, int width_left)
2412 struct ps_driver_ext *ext = this->ext;
2417 char number[INT_DIGITS + 1];
2421 switch (t->options & OUTP_T_JUST_MASK)
2423 case OUTP_T_JUST_LEFT:
2426 case OUTP_T_JUST_RIGHT:
2429 case OUTP_T_JUST_CENTER:
2430 ofs = width_left / 2;
2441 int x = cp->x + ofs;
2442 int y = cp->y + (cp->font->font->ascent * cp->size / 1000);
2444 if (ext->last_font == NULL
2445 || cp->font != ext->last_font->font
2446 || cp->size != ext->last_font->size)
2447 switch_font (this, cp);
2453 static unsigned char literal_chars[ODA_COUNT][32] =
2455 {0x00, 0x00, 0x00, 0xf8, 0xff, 0xfc, 0xff, 0xff,
2456 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f,
2457 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2458 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2460 {0x00, 0x00, 0x00, 0xf8, 0xff, 0xfc, 0xff, 0xff,
2461 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
2462 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
2463 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
2465 {0x7e, 0xd6, 0xff, 0xfb, 0xff, 0xfc, 0xff, 0xff,
2466 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
2467 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
2468 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
2472 if (TEST_BIT (literal_chars[ext->data], cp->ch))
2478 lp = stpcpy (lp, "\\(");
2481 lp = stpcpy (lp, "\\)");
2484 lp = spprintf (lp, "\\%03o", cp->ch);
2489 while (cp < end && lp < &line[70] && cp->separate == 0);
2497 put_number (YT (y));
2508 if (lp >= &line[70])
2518 /* Displays the text in outp_text T, if DRAW is nonzero; or, merely
2519 determine the text metrics, if DRAW is zero. */
2521 text (struct outp_driver *this, struct outp_text *t, int draw)
2523 struct ps_driver_ext *ext = this->ext;
2526 struct output_char *buf; /* Output buffer. */
2527 struct output_char *buf_end; /* End of output buffer. */
2528 struct output_char *buf_loc; /* Current location in output buffer. */
2531 struct font_entry *old_current = ext->current;
2532 char *old_family = xstrdup (ext->family); /* FIXME */
2533 int old_size = ext->size;
2538 /* Current location. */
2541 /* Keeping track of what's left over. */
2542 int width; /* Width available for characters. */
2543 int width_left, height_left; /* Width, height left over. */
2544 int max_height; /* Tallest character on this line so far. */
2546 /* Previous character. */
2549 /* Information about location of previous space. */
2550 char *space_char; /* Character after space. */
2551 struct output_char *space_buf_loc; /* Buffer location after space. */
2552 int space_width_left; /* Width of characters before space. */
2554 /* Name of the current character. */
2555 const char *char_name;
2556 char local_char_name[2] = {0, 0};
2558 local_char_name[0] = local_char_name[1] = 0;
2560 buf = local_alloc (sizeof *buf * 128);
2561 buf_end = &buf[128];
2564 assert (!ls_null_p (&t->s));
2565 cp = ls_c_str (&t->s);
2566 end = ls_end (&t->s);
2574 width = width_left = (t->options & OUTP_T_HORZ) ? t->h : INT_MAX;
2575 height_left = (t->options & OUTP_T_VERT) ? t->v : INT_MAX;
2579 space_buf_loc = NULL;
2580 space_width_left = 0;
2583 if (!width || !height_left)
2588 struct char_metrics *metric;
2594 /* Set char_name to the name of the character or ligature at
2596 local_char_name[0] = *cp;
2597 char_name = local_char_name;
2598 if (ext->current->font->ligatures && *cp == 'f')
2607 lig = LIG_fi, char_name = "fi";
2610 lig = LIG_fl, char_name = "fl";
2617 lig = LIG_ffi, char_name = "ffi";
2620 lig = LIG_ffl, char_name = "ffl";
2623 lig = LIG_ff, char_name = "ff";
2627 if ((lig & ext->current->font->ligatures) == 0)
2629 local_char_name[0] = *cp; /* 'f' */
2630 char_name = local_char_name;
2633 else if (*cp == '\n')
2637 write_text (this, buf, buf_loc, t, width, width_left);
2644 height_left -= max_height;
2650 /* FIXME: when we're page buffering it will be necessary to
2651 set separate to 1. */
2654 cp += strlen (char_name);
2656 /* Figure out what size this character is, and what kern
2657 adjustment we need. */
2658 cur_char = font_char_name_to_index (char_name);
2659 metric = font_get_char_metrics (ext->current->font, cur_char);
2662 static struct char_metrics m;
2664 m.width = ext->current->font->space_width;
2665 m.code = *char_name;
2667 kern_amt = font_get_kern_adjust (ext->current->font, prev_char,
2671 kern_amt = (kern_amt * ext->size / 1000);
2674 char_width = metric->width * ext->size / 1000;
2676 /* Record the current status if this is a space character. */
2677 if (cur_char == space_index && buf_loc > buf)
2680 space_buf_loc = buf_loc;
2681 space_width_left = width_left;
2684 /* Drop down to a new line if there's no room left on this
2686 if (char_width + kern_amt > width_left)
2688 /* Regress to previous space, if any. */
2692 width_left = space_width_left;
2693 buf_loc = space_buf_loc;
2698 write_text (this, buf, buf_loc, t, width, width_left);
2705 height_left -= max_height;
2713 /* FIXME: when we're page buffering it will be
2714 necessary to set separate to 1. */
2719 if (ext->size > max_height)
2720 max_height = ext->size;
2721 if (max_height > height_left)
2724 /* Actually draw the character. */
2727 if (buf_loc >= buf_end)
2729 int buf_len = buf_end - buf;
2733 struct output_char *new_buf;
2735 new_buf = xmalloc (sizeof *new_buf * 256);
2736 memcpy (new_buf, buf, sizeof *new_buf * 128);
2737 buf_loc = new_buf + 128;
2738 buf_end = new_buf + 256;
2744 buf = xrealloc (buf, sizeof *buf * buf_len * 2);
2745 buf_loc = buf + buf_len;
2746 buf_end = buf + buf_len * 2;
2751 buf_loc->font = ext->current;
2752 buf_loc->size = ext->size;
2755 buf_loc->ch = metric->code;
2756 buf_loc->separate = separate;
2761 /* Prepare for next iteration. */
2762 width_left -= char_width + kern_amt;
2763 prev_char = cur_char;
2765 height_left -= max_height;
2766 if (buf_loc > buf && draw)
2767 write_text (this, buf, buf_loc, t, width, width_left);
2770 if (!(t->options & OUTP_T_HORZ))
2771 t->h = INT_MAX - width_left;
2772 if (!(t->options & OUTP_T_VERT))
2773 t->v = INT_MAX - height_left;
2775 t->v -= height_left;
2776 if (buf_end - buf == 128)
2780 ext->current = old_current;
2782 ext->family = old_family;
2783 ext->size = old_size;
2787 ps_text_metrics (struct outp_driver *this, struct outp_text *t)
2789 assert (this->driver_open && this->page_open);
2794 ps_text_draw (struct outp_driver *this, struct outp_text *t)
2796 assert (this->driver_open && this->page_open);
2802 /* Translate a filename to a font. */
2803 struct filename2font
2805 char *filename; /* Normalized filename. */
2806 struct font_desc *font;
2809 /* Table of `filename2font's. */
2810 static struct hsh_table *ps_fonts;
2812 /* Hash table comparison function for filename2font structs. */
2814 compare_filename2font (const void *a, const void *b, void *param UNUSED)
2816 return strcmp (((struct filename2font *) a)->filename,
2817 ((struct filename2font *) b)->filename);
2820 /* Hash table hash function for filename2font structs. */
2822 hash_filename2font (const void *f2f_, void *param UNUSED)
2824 const struct filename2font *f2f = f2f_;
2825 return hsh_hash_string (f2f->filename);
2828 /* Initializes the global font list by creating the hash table for
2829 translation of filenames to font_desc structs. */
2833 ps_fonts = hsh_create (31, compare_filename2font, hash_filename2font,
2840 hsh_destroy (ps_fonts);
2843 /* Loads the font having Groff name DIT into THIS driver instance.
2844 Specifically, adds it into the THIS driver's `loaded' hash
2846 static struct font_entry *
2847 load_font (struct outp_driver *this, const char *dit)
2849 struct ps_driver_ext *x = this->ext;
2850 char *filename1, *filename2;
2852 struct font_entry *fe;
2854 filename1 = find_ps_file (this, dit);
2856 filename1 = xstrdup (dit);
2857 filename2 = fn_normalize (filename1);
2860 entry = hsh_probe (ps_fonts, &filename2);
2863 struct filename2font *f2f;
2864 struct font_desc *f = groff_read_font (filename2);
2871 f = default_font ();
2874 f2f = xmalloc (sizeof *f2f);
2875 f2f->filename = filename2;
2882 fe = xmalloc (sizeof *fe);
2883 fe->dit = xstrdup (dit);
2884 fe->font = ((struct filename2font *) * entry)->font;
2885 *hsh_probe (x->loaded, &dit) = fe;
2891 ps_chart_initialise (struct outp_driver *this UNUSED, struct chart *ch)
2896 struct ps_driver_ext *x = this->ext;
2897 char page_size[128];
2899 int x_origin, y_origin;
2901 ch->file = tmpfile ();
2902 if (ch->file == NULL)
2908 size = this->width < this->length ? this->width : this->length;
2909 x_origin = x->left_margin + (size - this->width) / 2;
2910 y_origin = x->bottom_margin + (size - this->length) / 2;
2912 snprintf (page_size, sizeof page_size,
2913 "a,xsize=%.3f,ysize=%.3f,xorigin=%.3f,yorigin=%.3f",
2914 (double) size / PSUS, (double) size / PSUS,
2915 (double) x_origin / PSUS, (double) y_origin / PSUS);
2917 ch->pl_params = pl_newplparams ();
2918 pl_setplparam (ch->pl_params, "PAGESIZE", page_size);
2919 ch->lp = pl_newpl_r ("ps", NULL, ch->file, stderr, ch->pl_params);
2924 ps_chart_finalise (struct outp_driver *this UNUSED, struct chart *ch UNUSED)
2927 struct ps_driver_ext *x = this->ext;
2929 static int doc_num = 0;
2931 if (this->page_open)
2933 this->class->close_page (this);
2934 this->page_open = 0;
2936 this->class->open_page (this);
2937 fprintf (x->file.file,
2939 "%d %d translate 1000 dup scale%s"
2941 "/showpage { } def%s"
2942 "0 setgray 0 setlinecap 1 setlinewidth%s"
2943 "0 setlinejoin 10 setmiterlimit [ ] 0 setdash newpath clear%s"
2944 "%%%%BeginDocument: %d%s",
2946 -x->left_margin, -x->bottom_margin, x->eol,
2954 while (fwrite (buf, 1, fread (buf, 1, sizeof buf, ch->file), x->file.file))
2958 fprintf (x->file.file,
2965 this->class->close_page (this);
2966 this->page_open = 0;
2970 /* PostScript driver class. */
2971 struct outp_class postscript_class =
2993 ps_line_intersection,
3000 ps_text_set_font_by_name,
3001 ps_text_set_font_by_position,
3002 ps_text_set_font_family,
3003 ps_text_get_font_name,
3004 ps_text_get_font_family,
3010 ps_chart_initialise,
3014 /* EPSF driver class. FIXME: Probably doesn't work right. */
3015 struct outp_class epsf_class =
3037 ps_line_intersection,
3044 ps_text_set_font_by_name,
3045 ps_text_set_font_by_position,
3046 ps_text_set_font_family,
3047 ps_text_get_font_name,
3048 ps_text_get_font_family,
3054 ps_chart_initialise,
3059 #endif /* NO_POSTSCRIPT */