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. */
37 #include "bitvector.h"
52 #define _(msgid) gettext (msgid)
56 optimize-text-size not implemented.
58 Line buffering is the only possibility; page buffering should also
63 Should add a field to give a file that has a list of fonts
66 Should add an option that tells the driver it can emit %%Include:'s.
68 Should have auto-encode=true stream-edit or whatever to allow
69 addition to list of encodings.
71 Should align fonts of different sizes along their baselines (see
74 /* PostScript driver options: (defaults listed first)
78 data=clean7bit|clean8bit|binary
81 paper-size=letter (see "papersize" file)
82 orientation=portrait|landscape
91 prologue-file=ps-prologue
93 encoding-file=ps-encodings
94 auto-encode=true|false
100 line-style=thick|double
106 optimize-text-size=1|0|2
107 optimize-line-size=1|0
108 max-fonts-simult=0 Max # of fonts in printer memory at once (0=infinite)
111 /* The number of `psus' (PostScript driver UnitS) per inch. Although
112 this is a #define, the value is expected never to change. If it
113 does, review all uses. */
116 /* Magic numbers for PostScript and EPSF drivers. */
126 OTN_PORTRAIT, /* Portrait. */
127 OTN_LANDSCAPE /* Landscape. */
130 /* Output options. */
133 OPO_MIRROR_HORZ = 001, /* 1=Mirror across a horizontal axis. */
134 OPO_MIRROR_VERT = 002, /* 1=Mirror across a vertical axis. */
135 OPO_ROTATE_180 = 004, /* 1=Rotate the page 180 degrees. */
136 OPO_COLOR = 010, /* 1=Enable color. */
137 OPO_HEADERS = 020, /* 1=Draw headers at top of page. */
138 OPO_AUTO_ENCODE = 040, /* 1=Add encodings semi-intelligently. */
139 OPO_DOUBLE_LINE = 0100 /* 1=Double lines instead of thick lines. */
142 /* Data allowed in output. */
145 ODA_CLEAN7BIT, /* 0x09, 0x0a, 0x0d, 0x1b...0x7e */
146 ODA_CLEAN8BIT, /* 0x09, 0x0a, 0x0d, 0x1b...0xff */
147 ODA_BINARY, /* 0x00...0xff */
151 /* Types of lines for purpose of caching. */
154 horz, /* Single horizontal. */
155 dbl_horz, /* Double horizontal. */
156 spl_horz, /* Special horizontal. */
157 vert, /* Single vertical. */
158 dbl_vert, /* Double vertical. */
159 spl_vert, /* Special vertical. */
166 int ind; /* Independent var. Don't reorder. */
167 int mdep; /* Maximum number of dependent var pairs. */
168 int ndep; /* Current number of dependent var pairs. */
169 int dep[1][2]; /* Dependent var pairs. */
172 /* Contents of ps_driver_ext.loaded. */
175 char *dit; /* Font Groff name. */
176 struct font_desc *font; /* Font descriptor. */
179 /* Combines a font with a font size for benefit of generated code. */
182 struct font_entry *font; /* Font. */
183 int size; /* Font size. */
184 int index; /* PostScript index. */
187 /* A font encoding. */
190 char *filename; /* Normalized filename of this encoding. */
191 int index; /* Index value. */
194 /* PostScript output driver extension record. */
197 /* User parameters. */
198 int orientation; /* OTN_PORTRAIT or OTN_LANDSCAPE. */
199 int output_options; /* OPO_*. */
200 int data; /* ODA_*. */
202 int left_margin; /* Left margin in psus. */
203 int right_margin; /* Right margin in psus. */
204 int top_margin; /* Top margin in psus. */
205 int bottom_margin; /* Bottom margin in psus. */
207 char eol[3]; /* End of line--CR, LF, or CRLF. */
209 char *font_dir; /* Font directory relative to font path. */
210 char *prologue_fn; /* Prologue's filename relative to font dir. */
211 char *desc_fn; /* DESC filename relative to font dir. */
212 char *encoding_fn; /* Encoding's filename relative to font dir. */
214 char *prop_family; /* Default proportional font family. */
215 char *fixed_family; /* Default fixed-pitch font family. */
216 int font_size; /* Default font size (psus). */
218 int line_gutter; /* Space around lines. */
219 int line_space; /* Space between lines. */
220 int line_width; /* Width of lines. */
221 int line_width_thick; /* Width of thick lines. */
223 int text_opt; /* Text optimization level. */
224 int line_opt; /* Line optimization level. */
225 int max_fonts; /* Max # of simultaneous fonts (0=infinite). */
227 /* Internal state. */
228 struct file_ext file; /* Output file. */
229 int page_number; /* Current page number. */
230 int file_page_number; /* Page number in this file. */
231 int w, l; /* Paper size. */
232 struct hsh_table *lines[n_line_types]; /* Line buffers. */
234 struct font_entry *prop; /* Default Roman proportional font. */
235 struct font_entry *fixed; /* Default Roman fixed-pitch font. */
236 struct hsh_table *loaded; /* Fonts in memory. */
238 struct hsh_table *combos; /* Combinations of fonts with font sizes. */
239 struct ps_font_combo *last_font; /* PostScript selected font. */
240 int next_combo; /* Next font combo position index. */
242 struct hsh_table *encodings;/* Set of encodings. */
243 int next_encoding; /* Next font encoding index. */
245 /* Currently selected font. */
246 struct font_entry *current; /* Current font. */
247 char *family; /* Font family. */
248 int size; /* Size in psus. */
252 /* Transform logical y-ordinate Y into a page ordinate. */
253 #define YT(Y) (this->length - (Y))
256 static int postopen (struct file_ext *);
257 static int preclose (struct file_ext *);
258 static void draw_headers (struct outp_driver *this);
260 static int compare_font_entry (const void *, const void *, void *param);
261 static unsigned hash_font_entry (const void *, void *param);
262 static void free_font_entry (void *, void *foo);
263 static struct font_entry *load_font (struct outp_driver *, const char *dit);
264 static void init_fonts (void);
265 static void done_fonts (void);
267 static void dump_lines (struct outp_driver *this);
269 static void read_ps_encodings (struct outp_driver *this);
270 static int compare_ps_encoding (const void *pa, const void *pb, void *foo);
271 static unsigned hash_ps_encoding (const void *pa, void *foo);
272 static void free_ps_encoding (void *a, void *foo);
273 static void add_encoding (struct outp_driver *this, char *filename);
274 static struct ps_encoding *default_encoding (struct outp_driver *this);
276 static int compare_ps_combo (const void *pa, const void *pb, void *foo);
277 static unsigned hash_ps_combo (const void *pa, void *foo);
278 static void free_ps_combo (void *a, void *foo);
280 static char *quote_ps_name (char *dest, const char *string);
281 static char *quote_ps_string (char *dest, const char *string);
283 /* Driver initialization. */
286 ps_open_global (struct outp_class *this UNUSED)
294 ps_close_global (struct outp_class *this UNUSED)
302 ps_font_sizes (struct outp_class *this UNUSED, int *n_valid_sizes)
304 /* Allow fonts up to 1" in height. */
305 static int valid_sizes[] =
308 assert (n_valid_sizes != NULL);
314 ps_preopen_driver (struct outp_driver *this)
316 struct ps_driver_ext *x;
320 assert (this->driver_open == 0);
321 msg (VM (1), _("PostScript driver initializing as `%s'..."), this->name);
323 this->ext = x = xmalloc (sizeof (struct ps_driver_ext));
325 this->horiz = this->vert = 1;
326 this->width = this->length = 0;
328 x->orientation = OTN_PORTRAIT;
329 x->output_options = OPO_COLOR | OPO_HEADERS | OPO_AUTO_ENCODE;
330 x->data = ODA_CLEAN7BIT;
332 x->left_margin = x->right_margin =
333 x->top_margin = x->bottom_margin = PSUS / 2;
335 strcpy (x->eol, "\n");
338 x->prologue_fn = NULL;
340 x->encoding_fn = NULL;
342 x->prop_family = NULL;
343 x->fixed_family = NULL;
344 x->font_size = PSUS * 10 / 72;
346 x->line_gutter = PSUS / 144;
347 x->line_space = PSUS / 144;
348 x->line_width = PSUS / 144;
349 x->line_width_thick = PSUS / 48;
355 x->file.filename = NULL;
358 x->file.sequence_no = &x->page_number;
359 x->file.param = this;
360 x->file.postopen = postopen;
361 x->file.preclose = preclose;
365 x->file_page_number = 0;
366 for (i = 0; i < n_line_types; i++)
377 x->encodings = hsh_create (31, compare_ps_encoding, hash_ps_encoding,
378 free_ps_encoding, NULL);
379 x->next_encoding = 0;
389 ps_postopen_driver (struct outp_driver *this)
391 struct ps_driver_ext *x = this->ext;
393 assert (this->driver_open == 0);
395 if (this->width == 0)
397 this->width = PSUS * 17 / 2; /* Defaults to 8.5"x11". */
398 this->length = PSUS * 11;
401 if (x->text_opt == -1)
402 x->text_opt = (this->device & OUTP_DEV_SCREEN) ? 0 : 1;
403 if (x->line_opt == -1)
404 x->line_opt = (this->device & OUTP_DEV_SCREEN) ? 0 : 1;
408 if (x->orientation == OTN_LANDSCAPE)
410 int temp = this->width;
411 this->width = this->length;
414 this->width -= x->left_margin + x->right_margin;
415 this->length -= x->top_margin + x->bottom_margin;
416 if (x->output_options & OPO_HEADERS)
418 this->length -= 3 * x->font_size;
419 x->top_margin += 3 * x->font_size;
421 if (NULL == x->file.filename)
422 x->file.filename = xstrdup ("pspp.ps");
424 if (x->font_dir == NULL)
425 x->font_dir = xstrdup ("devps");
426 if (x->prologue_fn == NULL)
427 x->prologue_fn = xstrdup ("ps-prologue");
428 if (x->desc_fn == NULL)
429 x->desc_fn = xstrdup ("DESC");
430 if (x->encoding_fn == NULL)
431 x->encoding_fn = xstrdup ("ps-encodings");
433 if (x->prop_family == NULL)
434 x->prop_family = xstrdup ("H");
435 if (x->fixed_family == NULL)
436 x->fixed_family = xstrdup ("C");
438 read_ps_encodings (this);
443 if (this->length / x->font_size < 15)
445 msg (SE, _("PostScript driver: The defined page is not long "
446 "enough to hold margins and headers, plus least 15 "
447 "lines of the default fonts. In fact, there's only "
448 "room for %d lines of each font at the default size "
449 "of %d.%03d points."),
450 this->length / x->font_size,
451 x->font_size / 1000, x->font_size % 1000);
455 this->driver_open = 1;
456 msg (VM (2), _("%s: Initialization complete."), this->name);
462 ps_close_driver (struct outp_driver *this)
464 struct ps_driver_ext *x = this->ext;
468 assert (this->driver_open == 1);
469 msg (VM (2), _("%s: Beginning closing..."), this->name);
471 fn_close_ext (&x->file);
472 free (x->file.filename);
474 free (x->prologue_fn);
476 free (x->encoding_fn);
477 free (x->prop_family);
478 free (x->fixed_family);
480 for (i = 0; i < n_line_types; i++)
481 hsh_destroy (x->lines[i]);
482 hsh_destroy (x->encodings);
483 hsh_destroy (x->combos);
484 hsh_destroy (x->loaded);
487 this->driver_open = 0;
488 msg (VM (3), _("%s: Finished closing."), this->name);
493 /* font_entry comparison function for hash tables. */
495 compare_font_entry (const void *a, const void *b, void *foobar UNUSED)
497 return strcmp (((struct font_entry *) a)->dit, ((struct font_entry *) b)->dit);
500 /* font_entry hash function for hash tables. */
502 hash_font_entry (const void *fe_, void *foobar UNUSED)
504 const struct font_entry *fe = fe_;
505 return hsh_hash_string (fe->dit);
508 /* font_entry destructor function for hash tables. */
510 free_font_entry (void *pa, void *foo UNUSED)
512 struct font_entry *a = pa;
517 /* Generic option types. */
527 /* All the options that the PostScript driver supports. */
528 static struct outp_option option_tab[] =
531 {"output-file", 1, 0},
532 {"paper-size", 2, 0},
533 {"orientation", 3, 0},
534 {"color", boolean_arg, 0},
536 {"auto-encode", boolean_arg, 5},
537 {"headers", boolean_arg, 1},
538 {"left-margin", pos_int_arg, 0},
539 {"right-margin", pos_int_arg, 1},
540 {"top-margin", pos_int_arg, 2},
541 {"bottom-margin", pos_int_arg, 3},
542 {"font-dir", string_arg, 0},
543 {"prologue-file", string_arg, 1},
544 {"device-file", string_arg, 2},
545 {"encoding-file", string_arg, 3},
546 {"prop-font-family", string_arg, 5},
547 {"fixed-font-family", string_arg, 6},
548 {"font-size", pos_int_arg, 4},
549 {"optimize-text-size", nonneg_int_arg, 0},
550 {"optimize-line-size", nonneg_int_arg, 1},
551 {"max-fonts-simult", nonneg_int_arg, 2},
553 {"line-style", 7, 0},
554 {"line-width", dimension_arg, 2},
555 {"line-gutter", dimension_arg, 3},
556 {"line-width", dimension_arg, 4},
557 {"line-width-thick", dimension_arg, 5},
561 static struct outp_option_info option_info;
564 ps_option (struct outp_driver *this, const char *key, const struct string *val)
566 struct ps_driver_ext *x = this->ext;
568 char *value = ds_c_str (val);
570 cat = outp_match_keyword (key, option_tab, &option_info, &subcat);
575 msg (SE, _("Unknown configuration parameter `%s' for PostScript device "
579 free (x->file.filename);
580 x->file.filename = xstrdup (value);
583 outp_get_paper_size (value, &this->width, &this->length);
586 if (!strcmp (value, "portrait"))
587 x->orientation = OTN_PORTRAIT;
588 else if (!strcmp (value, "landscape"))
589 x->orientation = OTN_LANDSCAPE;
591 msg (SE, _("Unknown orientation `%s'. Valid orientations are "
592 "`portrait' and `landscape'."), value);
595 if (!strcmp (value, "clean7bit") || !strcmp (value, "Clean7Bit"))
596 x->data = ODA_CLEAN7BIT;
597 else if (!strcmp (value, "clean8bit")
598 || !strcmp (value, "Clean8Bit"))
599 x->data = ODA_CLEAN8BIT;
600 else if (!strcmp (value, "binary") || !strcmp (value, "Binary"))
601 x->data = ODA_BINARY;
603 msg (SE, _("Unknown value for `data'. Valid values are `clean7bit', "
604 "`clean8bit', and `binary'."));
607 if (!strcmp (value, "lf"))
608 strcpy (x->eol, "\n");
609 else if (!strcmp (value, "crlf"))
610 strcpy (x->eol, "\r\n");
612 msg (SE, _("Unknown value for `line-ends'. Valid values are `lf' and "
616 if (!strcmp (value, "thick"))
617 x->output_options &= ~OPO_DOUBLE_LINE;
618 else if (!strcmp (value, "double"))
619 x->output_options |= OPO_DOUBLE_LINE;
621 msg (SE, _("Unknown value for `line-style'. Valid values are `thick' "
629 if (!strcmp (value, "on") || !strcmp (value, "true")
630 || !strcmp (value, "yes") || atoi (value))
632 else if (!strcmp (value, "off") || !strcmp (value, "false")
633 || !strcmp (value, "no") || !strcmp (value, "0"))
637 msg (SE, _("Boolean value expected for %s."), key);
649 mask = OPO_MIRROR_HORZ;
652 mask = OPO_MIRROR_VERT;
655 mask = OPO_ROTATE_180;
658 mask = OPO_AUTO_ENCODE;
665 x->output_options |= mask;
667 x->output_options &= ~mask;
676 arg = strtol (value, &tail, 0);
677 if (arg < 1 || errno == ERANGE || *tail)
679 msg (SE, _("Positive integer required as value for `%s'."), key);
682 if ((subcat == 4 || subcat == 5) && arg < 1000)
684 msg (SE, _("Default font size must be at least 1 point (value "
685 "of 1000 for key `%s')."), key);
691 x->left_margin = arg;
694 x->right_margin = arg;
700 x->bottom_margin = arg;
712 int dimension = outp_evaluate_dimension (value, NULL);
716 msg (SE, _("Value for `%s' must be a dimension of positive "
717 "length (i.e., `1in')."), key);
723 x->line_width = dimension;
726 x->line_gutter = dimension;
729 x->line_width = dimension;
732 x->line_width_thick = dimension;
748 dest = &x->prologue_fn;
754 dest = &x->encoding_fn;
757 dest = &x->prop_family;
760 dest = &x->fixed_family;
768 *dest = xstrdup (value);
777 arg = strtol (value, &tail, 0);
778 if (arg < 0 || errno == ERANGE || *tail)
780 msg (SE, _("Nonnegative integer required as value for `%s'."), key);
804 /* Looks for a PostScript font file or config file in all the
805 appropriate places. Returns the filename on success, NULL on
807 /* PORTME: Filename operations. */
809 find_ps_file (struct outp_driver *this, const char *name)
811 struct ps_driver_ext *x = this->ext;
814 /* x->font_dir + name: "devps/ps-encodings". */
817 /* Usually equal to groff_font_path. */
820 /* Final filename. */
824 basename = local_alloc (strlen (x->font_dir) + 1 + strlen (name) + 1);
825 cp = stpcpy (basename, x->font_dir);
826 *cp++ = DIR_SEPARATOR;
829 /* Decide on search path. */
831 const char *pre_pathname;
833 pre_pathname = getenv ("STAT_GROFF_FONT_PATH");
834 if (pre_pathname == NULL)
835 pre_pathname = getenv ("GROFF_FONT_PATH");
836 if (pre_pathname == NULL)
837 pre_pathname = groff_font_path;
838 pathname = fn_tilde_expand (pre_pathname);
841 /* Search all possible places for the file. */
842 fn = fn_search_path (basename, pathname, NULL);
844 fn = fn_search_path (basename, config_path, NULL);
846 fn = fn_search_path (name, pathname, NULL);
848 fn = fn_search_path (name, config_path, NULL);
850 local_free (basename);
857 /* Hash table comparison function for ps_encoding's. */
859 compare_ps_encoding (const void *pa, const void *pb, void *foo UNUSED)
861 const struct ps_encoding *a = pa;
862 const struct ps_encoding *b = pb;
864 return strcmp (a->filename, b->filename);
867 /* Hash table hash function for ps_encoding's. */
869 hash_ps_encoding (const void *pa, void *foo UNUSED)
871 const struct ps_encoding *a = pa;
873 return hsh_hash_string (a->filename);
876 /* Hash table free function for ps_encoding's. */
878 free_ps_encoding (void *pa, void *foo UNUSED)
880 struct ps_encoding *a = pa;
886 /* Iterates through the list of encodings used for this driver
887 instance, reads each of them from disk, and writes them as
888 PostScript code to the output file. */
890 output_encodings (struct outp_driver *this)
892 struct ps_driver_ext *x = this->ext;
894 struct hsh_iterator iter;
895 struct ps_encoding *pe;
897 struct string line, buf;
899 ds_init (&line, 128);
901 for (pe = hsh_first (x->encodings, &iter); pe != NULL;
902 pe = hsh_next (x->encodings, &iter))
906 msg (VM (1), _("%s: %s: Opening PostScript font encoding..."),
907 this->name, pe->filename);
909 f = fopen (pe->filename, "r");
912 msg (IE, _("PostScript driver: Cannot open encoding file `%s': %s. "
913 "Substituting ISOLatin1Encoding for missing encoding."),
914 pe->filename, strerror (errno));
915 fprintf (x->file.file, "/E%x ISOLatin1Encoding def%s",
920 struct file_locator where;
922 const char *tab[256];
929 const char *notdef = ".notdef";
933 for (i = 0; i < 256; i++)
936 where.filename = pe->filename;
937 where.line_number = 0;
938 err_push_file_locator (&where);
940 while (ds_get_config_line (f, &buf, &where))
947 pschar = strtok_r (ds_c_str (&buf), " \t\r\n", &sp);
948 code = strtok_r (NULL, " \t\r\n", &sp);
949 if (*pschar == 0 || *code == 0)
951 code_val = strtol (code, &fubar, 0);
954 msg (IS, _("PostScript driver: Invalid numeric format."));
957 if (code_val < 0 || code_val > 255)
959 msg (IS, _("PostScript driver: Codes must be between 0 "
960 "and 255. (%d is not allowed.)"), code_val);
963 tab[code_val] = local_alloc (strlen (pschar) + 1);
964 strcpy ((char *) (tab[code_val]), pschar);
966 err_pop_file_locator (&where);
969 ds_printf (&line, "/E%x[", pe->index);
970 for (i = 0; i < 257; i++)
976 quote_ps_name (temp, tab[i]);
977 if (tab[i] != notdef)
981 strcpy (temp, "]def");
983 if (ds_length (&line) + strlen (temp) > 70)
985 ds_puts (&line, x->eol);
986 fputs (ds_c_str (&line), x->file.file);
989 ds_puts (&line, temp);
991 ds_puts (&line, x->eol);
992 fputs (ds_c_str (&line), x->file.file);
994 if (fclose (f) == EOF)
995 msg (MW, _("PostScript driver: Error closing encoding file `%s'."),
998 msg (VM (2), _("%s: PostScript font encoding read successfully."),
1006 /* Finds the ps_encoding in THIS that corresponds to the file with
1007 name NORM_FILENAME, which must have previously been normalized with
1008 normalize_filename(). */
1009 static struct ps_encoding *
1010 get_encoding (struct outp_driver *this, const char *norm_filename)
1012 struct ps_driver_ext *x = this->ext;
1013 struct ps_encoding *pe;
1015 pe = (struct ps_encoding *) hsh_find (x->encodings, (void *) &norm_filename);
1019 /* Searches the filesystem for an encoding file with name FILENAME;
1020 returns its malloc'd, normalized name if found, otherwise NULL. */
1022 find_encoding_file (struct outp_driver *this, char *filename)
1026 if (filename == NULL)
1028 while (isspace ((unsigned char) *filename))
1030 for (cp = filename; *cp && !isspace ((unsigned char) *cp); cp++)
1036 temp = find_ps_file (this, filename);
1040 filename = fn_normalize (temp);
1041 assert (filename != NULL);
1047 /* Adds the encoding represented by the not-necessarily-normalized
1048 file FILENAME to the list of encodings, if it exists and is not
1049 already in the list. */
1051 add_encoding (struct outp_driver *this, char *filename)
1053 struct ps_driver_ext *x = this->ext;
1054 struct ps_encoding **pe;
1056 filename = find_encoding_file (this, filename);
1060 pe = (struct ps_encoding **) hsh_probe (x->encodings, &filename);
1066 *pe = xmalloc (sizeof **pe);
1067 (*pe)->filename = filename;
1068 (*pe)->index = x->next_encoding++;
1071 /* Finds the file on disk that contains the list of encodings to
1072 include in the output file, then adds those encodings to the list
1075 read_ps_encodings (struct outp_driver *this)
1077 struct ps_driver_ext *x = this->ext;
1079 /* Encodings file. */
1080 char *encoding_fn; /* `ps-encodings' filename. */
1084 struct file_locator where;
1086 /* It's okay if there's no list of encodings; not everyone cares. */
1087 encoding_fn = find_ps_file (this, x->encoding_fn);
1088 if (encoding_fn == NULL)
1092 msg (VM (1), _("%s: %s: Opening PostScript encoding list file."),
1093 this->name, encoding_fn);
1094 f = fopen (encoding_fn, "r");
1097 msg (IE, _("Opening %s: %s."), encoding_fn, strerror (errno));
1101 where.filename = encoding_fn;
1102 where.line_number = 0;
1103 err_push_file_locator (&where);
1105 ds_init (&line, 128);
1109 if (!ds_get_config_line (f, &line, &where))
1112 msg (ME, _("Reading %s: %s."), encoding_fn, strerror (errno));
1116 add_encoding (this, line.string);
1120 err_pop_file_locator (&where);
1122 if (-1 == fclose (f))
1123 msg (MW, _("Closing %s: %s."), encoding_fn, strerror (errno));
1125 msg (VM (2), _("%s: PostScript encoding list file read successfully."), this->name);
1128 /* Creates a default encoding for driver D that can be substituted for
1129 an unavailable encoding. */
1130 struct ps_encoding *
1131 default_encoding (struct outp_driver *d)
1133 struct ps_driver_ext *x = d->ext;
1134 static struct ps_encoding *enc;
1138 enc = xmalloc (sizeof *enc);
1139 enc->filename = xstrdup (_("<<default encoding>>"));
1140 enc->index = x->next_encoding++;
1145 /* Basic file operations. */
1147 /* Variables for the prologue. */
1154 static struct ps_variable *ps_var_tab;
1156 /* Searches ps_var_tab for a ps_variable with key KEY, and returns the
1157 associated value. */
1159 ps_get_var (const char *key)
1161 struct ps_variable *v;
1163 for (v = ps_var_tab; v->key; v++)
1164 if (!strcmp (key, v->key))
1169 /* Writes the PostScript prologue to file F. */
1171 postopen (struct file_ext *f)
1173 static struct ps_variable dict[] =
1175 {"bounding-box", 0},
1184 {"scale-factor", 0},
1186 {"paper-length", 0},
1190 {"line-width-thick", 0},
1195 char boundbox[INT_DIGITS * 4 + 4];
1199 char scaling[INT_DIGITS + 5];
1203 char paper_width[INT_DIGITS + 1];
1204 char paper_length[INT_DIGITS + 1];
1205 char left_margin[INT_DIGITS + 1];
1206 char top_margin[INT_DIGITS + 1];
1207 char line_width[INT_DIGITS + 1];
1208 char line_width_thick[INT_DIGITS + 1];
1210 struct outp_driver *this = f->param;
1211 struct ps_driver_ext *x = this->ext;
1213 char *prologue_fn = find_ps_file (this, x->prologue_fn);
1214 FILE *prologue_file;
1217 size_t buf_size = 0;
1219 x->loaded = hsh_create (31, compare_font_entry, hash_font_entry,
1220 free_font_entry, NULL);
1223 char *font_name = local_alloc (2 + max (strlen (x->prop_family),
1224 strlen (x->fixed_family)));
1226 strcpy (stpcpy (font_name, x->prop_family), "R");
1227 x->prop = load_font (this, font_name);
1229 strcpy (stpcpy (font_name, x->fixed_family), "R");
1230 x->fixed = load_font (this, font_name);
1232 local_free(font_name);
1235 x->current = x->prop;
1236 x->family = xstrdup (x->prop_family);
1237 x->size = x->font_size;
1240 int *h = this->horiz_line_width, *v = this->vert_line_width;
1242 this->cp_x = this->cp_y = 0;
1243 this->font_height = x->font_size;
1245 struct char_metrics *metric;
1247 metric = font_get_char_metrics (x->prop->font, '0');
1248 this->prop_em_width = ((metric
1249 ? metric->width : x->prop->font->space_width)
1250 * x->font_size / 1000);
1252 metric = font_get_char_metrics (x->fixed->font, '0');
1253 this->fixed_width = ((metric
1254 ? metric->width : x->fixed->font->space_width)
1255 * x->font_size / 1000);
1259 h[1] = v[1] = 2 * x->line_gutter + x->line_width;
1260 if (x->output_options & OPO_DOUBLE_LINE)
1261 h[2] = v[2] = 2 * x->line_gutter + 2 * x->line_width + x->line_space;
1263 h[2] = v[2] = 2 * x->line_gutter + x->line_width_thick;
1264 h[3] = v[3] = 2 * x->line_gutter + x->line_width;
1269 for (i = 0; i < (1 << OUTP_L_COUNT); i++)
1273 /* Maximum width of any line type so far. */
1276 for (bit = 0; bit < OUTP_L_COUNT; bit++)
1277 if ((i & (1 << bit)) && h[bit] > max)
1279 this->horiz_line_spacing[i] = this->vert_line_spacing[i] = max;
1284 if (x->output_options & OPO_AUTO_ENCODE)
1286 /* It's okay if this is done more than once since add_encoding()
1287 is idempotent over identical encodings. */
1288 add_encoding (this, x->prop->font->encoding);
1289 add_encoding (this, x->fixed->font->encoding);
1292 x->file_page_number = 0;
1295 if (prologue_fn == NULL)
1297 msg (IE, _("Cannot find PostScript prologue. The use of `-vv' "
1298 "on the command line is suggested as a debugging aid."));
1302 msg (VM (1), _("%s: %s: Opening PostScript prologue..."),
1303 this->name, prologue_fn);
1304 prologue_file = fopen (prologue_fn, "rb");
1305 if (prologue_file == NULL)
1307 fclose (prologue_file);
1309 msg (IE, "%s: %s", prologue_fn, strerror (errno));
1313 sprintf (boundbox, "0 0 %d %d",
1314 x->w / (PSUS / 72) + (x->w % (PSUS / 72) > 0),
1315 x->l / (PSUS / 72) + (x->l % (PSUS / 72) > 0));
1316 dict[0].value = boundbox;
1318 dict[1].value = (char *) version;
1320 curtime = time (NULL);
1321 loctime = localtime (&curtime);
1322 dict[2].value = asctime (loctime);
1323 cp = strchr (dict[2].value, '\n');
1330 dict[3].value = "Clean7Bit";
1333 dict[3].value = "Clean8Bit";
1336 dict[3].value = "Binary";
1342 if (x->orientation == OTN_PORTRAIT)
1343 dict[4].value = "Portrait";
1345 dict[4].value = "Landscape";
1347 /* PORTME: Determine username, net address. */
1349 dict[5].value = getenv ("LOGNAME");
1351 dict[5].value = getlogin ();
1353 dict[5].value = _("nobody");
1355 if (gethostname (host, 128) == -1)
1357 if (errno == ENAMETOOLONG)
1360 strcpy (host, _("nowhere"));
1362 dict[6].value = host;
1363 #else /* !HAVE_UNISTD_H */
1364 dict[5].value = _("nobody");
1365 dict[6].value = _("nowhere");
1366 #endif /* !HAVE_UNISTD_H */
1368 cp = stpcpy (p = local_alloc (288), "font ");
1369 quote_ps_string (cp, x->prop->font->internal_name);
1372 cp = stpcpy (p = local_alloc (288), "font ");
1373 quote_ps_string (cp, x->fixed->font->internal_name);
1376 sprintf (scaling, "%.3f", PSUS / 72.0);
1377 dict[9].value = scaling;
1379 sprintf (paper_width, "%g", x->w / (PSUS / 72.0));
1380 dict[10].value = paper_width;
1382 sprintf (paper_length, "%g", x->l / (PSUS / 72.0));
1383 dict[11].value = paper_length;
1385 sprintf (left_margin, "%d", x->left_margin);
1386 dict[12].value = left_margin;
1388 sprintf (top_margin, "%d", x->top_margin);
1389 dict[13].value = top_margin;
1391 sprintf (line_width, "%d", x->line_width);
1392 dict[14].value = line_width;
1394 sprintf (line_width, "%d", x->line_width_thick);
1395 dict[15].value = line_width_thick;
1397 getl_location (&dict[17].value, NULL);
1398 if (dict[17].value == NULL)
1399 dict[17].value = "<stdin>";
1403 dict[16].value = cp = local_alloc (strlen (dict[17].value) + 30);
1404 sprintf (cp, "PSPP (%s)", dict[17].value);
1408 dict[16].value = local_alloc (strlen (outp_title) + 1);
1409 strcpy ((char *) (dict[16].value), outp_title);
1413 while (-1 != getline (&buf, &buf_size, prologue_file))
1419 cp = strstr (buf, "!eps");
1422 if (this->class->magic == MAGIC_PS)
1429 cp = strstr (buf, "!ps");
1432 if (this->class->magic == MAGIC_EPSF)
1437 if (strstr (buf, "!!!"))
1442 if (!strncmp (buf, "!encodings", 10))
1443 output_encodings (this);
1447 beg = buf2 = fn_interp_vars (buf, ps_get_var);
1448 len = strlen (buf2);
1449 while (isspace ((unsigned char) *beg))
1451 if (beg[len - 1] == '\n')
1453 if (beg[len - 1] == '\r')
1455 fwrite (beg, len, 1, f->file);
1456 fputs (x->eol, f->file);
1460 if (ferror (f->file))
1461 msg (IE, _("Reading `%s': %s."), prologue_fn, strerror (errno));
1462 fclose (prologue_file);
1467 local_free (dict[7].value);
1468 local_free (dict[8].value);
1469 local_free (dict[16].value);
1471 if (ferror (f->file))
1474 msg (VM (2), _("%s: PostScript prologue read successfully."), this->name);
1478 msg (VM (1), _("%s: Error reading PostScript prologue."), this->name);
1482 /* Writes the string STRING to buffer DEST (of at least 288
1483 characters) as a PostScript name object. Returns a pointer
1484 to the null terminator of the resultant string. */
1486 quote_ps_name (char *dest, const char *string)
1490 for (sp = string; *sp; sp++)
1491 switch (*(unsigned char *) sp)
1564 for (sp = string; *sp && dp < &dest[256]; sp++)
1566 sprintf (dp, "%02x", *(unsigned char *) sp);
1569 return stpcpy (dp, ">cvn");
1573 return stpcpy (&dest[1], string);
1576 /* Adds the string STRING to buffer DEST as a PostScript quoted
1577 string; returns a pointer to the null terminator added. Will not
1578 add more than 235 characters. */
1580 quote_ps_string (char *dest, const char *string)
1582 const char *sp = string;
1586 for (; *sp && dp < &dest[235]; sp++)
1588 dp = stpcpy (dp, "\\(");
1589 else if (*sp == ')')
1590 dp = stpcpy (dp, "\\)");
1591 else if (*sp < 32 || *((unsigned char *) sp) > 127)
1592 dp = spprintf (dp, "\\%3o", *sp);
1595 return stpcpy (dp, ")");
1598 /* Writes the PostScript epilogue to file F. */
1600 preclose (struct file_ext *f)
1602 struct outp_driver *this = f->param;
1603 struct ps_driver_ext *x = this->ext;
1604 struct hsh_iterator iter;
1605 struct font_entry *fe;
1610 "%%%%DocumentNeededResources:%s"),
1611 x->eol, x->file_page_number, x->eol, x->eol);
1613 for (fe = hsh_first (x->loaded, &iter); fe != NULL;
1614 fe = hsh_next (x->loaded, &iter))
1618 cp = stpcpy (buf, "%%+ font ");
1619 cp = quote_ps_string (cp, fe->font->internal_name);
1620 strcpy (cp, x->eol);
1621 fputs (buf, f->file);
1624 hsh_destroy (x->loaded);
1626 hsh_destroy (x->combos);
1628 x->last_font = NULL;
1631 fprintf (f->file, "%%EOF%s", x->eol);
1632 if (ferror (f->file))
1638 ps_open_page (struct outp_driver *this)
1640 struct ps_driver_ext *x = this->ext;
1642 assert (this->driver_open && !this->page_open);
1645 if (!fn_open_ext (&x->file))
1648 msg (ME, _("PostScript output driver: %s: %s"), x->file.filename,
1652 x->file_page_number++;
1654 hsh_destroy (x->combos);
1655 x->combos = hsh_create (31, compare_ps_combo, hash_ps_combo,
1656 free_ps_combo, NULL);
1657 x->last_font = NULL;
1660 fprintf (x->file.file,
1662 "%%%%BeginPageSetup%s"
1663 "/pg save def 0.001 dup scale%s",
1664 x->page_number, x->file_page_number, x->eol,
1668 if (x->orientation == OTN_LANDSCAPE)
1669 fprintf (x->file.file,
1670 "%d 0 translate 90 rotate%s",
1673 if (x->bottom_margin != 0 || x->left_margin != 0)
1674 fprintf (x->file.file,
1675 "%d %d translate%s",
1676 x->left_margin, x->bottom_margin, x->eol);
1678 fprintf (x->file.file,
1679 "/LW %d def/TW %d def %d setlinewidth%s"
1680 "%%%%EndPageSetup%s",
1681 x->line_width, x->line_width_thick, x->line_width, x->eol,
1684 if (!ferror (x->file.file))
1686 this->page_open = 1;
1687 if (x->output_options & OPO_HEADERS)
1688 draw_headers (this);
1693 return !ferror (x->file.file);
1697 ps_close_page (struct outp_driver *this)
1699 struct ps_driver_ext *x = this->ext;
1701 assert (this->driver_open && this->page_open);
1706 fprintf (x->file.file,
1711 this->page_open = 0;
1712 return !ferror (x->file.file);
1716 ps_submit (struct outp_driver *this UNUSED, struct som_entity *s)
1730 /* qsort() comparison function for int tuples. */
1732 int_2_compare (const void *a_, const void *b_)
1737 return *a < *b ? -1 : *a > *b;
1740 /* Hash table comparison function for cached lines. */
1742 compare_line (const void *a_, const void *b_, void *foo UNUSED)
1744 const struct line_form *a = a_;
1745 const struct line_form *b = b_;
1747 return a->ind < b->ind ? -1 : a->ind > b->ind;
1750 /* Hash table hash function for cached lines. */
1752 hash_line (const void *pa, void *foo UNUSED)
1754 const struct line_form *a = pa;
1759 /* Hash table free function for cached lines. */
1761 free_line (void *pa, void *foo UNUSED)
1766 /* Writes PostScript code to draw a line from (x1,y1) to (x2,y2) to
1768 #define dump_line(x1, y1, x2, y2) \
1769 fprintf (ext->file.file, "%d %d %d %d L%s", \
1770 x1, YT (y1), x2, YT (y2), ext->eol)
1772 /* Write PostScript code to draw a thick line from (x1,y1) to (x2,y2)
1773 to the output file. */
1774 #define dump_thick_line(x1, y1, x2, y2) \
1775 fprintf (ext->file.file, "%d %d %d %d TL%s", \
1776 x1, YT (y1), x2, YT (y2), ext->eol)
1778 /* Writes a line of type TYPE to THIS driver's output file. The line
1779 (or its center, in the case of double lines) has its independent
1780 axis coordinate at IND; it extends from DEP1 to DEP2 on the
1783 dump_fancy_line (struct outp_driver *this, int type, int ind, int dep1, int dep2)
1785 struct ps_driver_ext *ext = this->ext;
1786 int ofs = ext->line_space / 2 + ext->line_width / 2;
1791 dump_line (dep1, ind, dep2, ind);
1794 if (ext->output_options & OPO_DOUBLE_LINE)
1796 dump_line (dep1, ind - ofs, dep2, ind - ofs);
1797 dump_line (dep1, ind + ofs, dep2, ind + ofs);
1800 dump_thick_line (dep1, ind, dep2, ind);
1805 dump_line (ind, dep1, ind, dep2);
1808 if (ext->output_options & OPO_DOUBLE_LINE)
1810 dump_line (ind - ofs, dep1, ind - ofs, dep2);
1811 dump_line (ind + ofs, dep1, ind + ofs, dep2);
1814 dump_thick_line (ind, dep1, ind, dep2);
1825 /* Writes all the cached lines to the output file, then clears the
1828 dump_lines (struct outp_driver *this)
1830 struct ps_driver_ext *x = this->ext;
1832 struct hsh_iterator iter;
1835 for (type = 0; type < n_line_types; type++)
1837 struct line_form *line;
1839 if (x->lines[type] == NULL)
1842 for (line = hsh_first (x->lines[type], &iter); line != NULL;
1843 line = hsh_next (x->lines[type], &iter))
1846 int lo = INT_MIN, hi;
1848 qsort (line->dep, line->ndep, sizeof *line->dep, int_2_compare);
1849 lo = line->dep[0][0];
1850 hi = line->dep[0][1];
1851 for (i = 1; i < line->ndep; i++)
1852 if (line->dep[i][0] <= hi + 1)
1854 int min_hi = line->dep[i][1];
1860 dump_fancy_line (this, type, line->ind, lo, hi);
1861 lo = line->dep[i][0];
1862 hi = line->dep[i][1];
1864 dump_fancy_line (this, type, line->ind, lo, hi);
1867 hsh_destroy (x->lines[type]);
1868 x->lines[type] = NULL;
1872 /* (Same args as dump_fancy_line()). Either dumps the line directly
1873 to the output file, or adds it to the cache, depending on the
1874 user-selected line optimization mode. */
1876 line (struct outp_driver *this, int type, int ind, int dep1, int dep2)
1878 struct ps_driver_ext *ext = this->ext;
1879 struct line_form **f;
1881 assert (dep2 >= dep1);
1882 if (ext->line_opt == 0)
1884 dump_fancy_line (this, type, ind, dep1, dep2);
1888 if (ext->lines[type] == NULL)
1889 ext->lines[type] = hsh_create (31, compare_line, hash_line,
1891 f = (struct line_form **) hsh_probe (ext->lines[type], &ind);
1894 *f = xmalloc (sizeof **f + sizeof (int[15][2]));
1898 (*f)->dep[0][0] = dep1;
1899 (*f)->dep[0][1] = dep2;
1902 if ((*f)->ndep >= (*f)->mdep)
1905 *f = xrealloc (*f, (sizeof **f + sizeof (int[2]) * ((*f)->mdep - 1)));
1907 (*f)->dep[(*f)->ndep][0] = dep1;
1908 (*f)->dep[(*f)->ndep][1] = dep2;
1913 ps_line_horz (struct outp_driver *this, const struct rect *r,
1914 const struct color *c UNUSED, int style)
1916 /* Must match output.h:OUTP_L_*. */
1917 static const int types[OUTP_L_COUNT] =
1918 {-1, horz, dbl_horz, spl_horz};
1920 int y = (r->y1 + r->y2) / 2;
1922 assert (this->driver_open && this->page_open);
1923 assert (style >= 0 && style < OUTP_L_COUNT);
1924 style = types[style];
1926 line (this, style, y, r->x1, r->x2);
1930 ps_line_vert (struct outp_driver *this, const struct rect *r,
1931 const struct color *c UNUSED, int style)
1933 /* Must match output.h:OUTP_L_*. */
1934 static const int types[OUTP_L_COUNT] =
1935 {-1, vert, dbl_vert, spl_vert};
1937 int x = (r->x1 + r->x2) / 2;
1939 assert (this->driver_open && this->page_open);
1940 assert (style >= 0 && style < OUTP_L_COUNT);
1941 style = types[style];
1943 line (this, style, x, r->y1, r->y2);
1946 #define L (style->l != OUTP_L_NONE)
1947 #define R (style->r != OUTP_L_NONE)
1948 #define T (style->t != OUTP_L_NONE)
1949 #define B (style->b != OUTP_L_NONE)
1952 ps_line_intersection (struct outp_driver *this, const struct rect *r,
1953 const struct color *c UNUSED,
1954 const struct outp_styles *style)
1956 struct ps_driver_ext *ext = this->ext;
1958 int x = (r->x1 + r->x2) / 2;
1959 int y = (r->y1 + r->y2) / 2;
1960 int ofs = (ext->line_space + ext->line_width) / 2;
1961 int x1 = x - ofs, x2 = x + ofs;
1962 int y1 = y - ofs, y2 = y + ofs;
1964 assert (this->driver_open && this->page_open);
1965 assert (!((style->l != style->r && style->l != OUTP_L_NONE
1966 && style->r != OUTP_L_NONE)
1967 || (style->t != style->b && style->t != OUTP_L_NONE
1968 && style->b != OUTP_L_NONE)));
1970 switch ((style->l | style->r) | ((style->t | style->b) << 8))
1972 case (OUTP_L_SINGLE) | (OUTP_L_SINGLE << 8):
1973 case (OUTP_L_SINGLE) | (OUTP_L_NONE << 8):
1974 case (OUTP_L_NONE) | (OUTP_L_SINGLE << 8):
1976 line (this, horz, y, r->x1, x);
1978 line (this, horz, y, x, r->x2);
1980 line (this, vert, x, r->y1, y);
1982 line (this, vert, x, y, r->y2);
1984 case (OUTP_L_SINGLE) | (OUTP_L_DOUBLE << 8):
1985 case (OUTP_L_NONE) | (OUTP_L_DOUBLE << 8):
1987 line (this, horz, y, r->x1, x1);
1989 line (this, horz, y, x2, r->x2);
1991 line (this, dbl_vert, x, r->y1, y);
1993 line (this, dbl_vert, x, y, r->y2);
1994 if ((L && R) && !(T && B))
1995 line (this, horz, y, x1, x2);
1997 case (OUTP_L_DOUBLE) | (OUTP_L_SINGLE << 8):
1998 case (OUTP_L_DOUBLE) | (OUTP_L_NONE << 8):
2000 line (this, dbl_horz, y, r->x1, x);
2002 line (this, dbl_horz, y, x, r->x2);
2004 line (this, vert, x, r->y1, y);
2006 line (this, vert, x, y, r->y2);
2007 if ((T && B) && !(L && R))
2008 line (this, vert, x, y1, y2);
2010 case (OUTP_L_DOUBLE) | (OUTP_L_DOUBLE << 8):
2012 line (this, dbl_horz, y, r->x1, x);
2014 line (this, dbl_horz, y, x, r->x2);
2016 line (this, dbl_vert, x, r->y1, y);
2018 line (this, dbl_vert, x, y, r->y2);
2020 line (this, vert, x1, y1, y2);
2022 line (this, vert, x2, y1, y2);
2024 line (this, horz, y1, x1, x2);
2026 line (this, horz, y2, x1, x2);
2034 ps_box (struct outp_driver *this UNUSED, const struct rect *r UNUSED,
2035 const struct color *bord UNUSED, const struct color *fill UNUSED)
2037 assert (this->driver_open && this->page_open);
2041 ps_polyline_begin (struct outp_driver *this UNUSED,
2042 const struct color *c UNUSED)
2044 assert (this->driver_open && this->page_open);
2047 ps_polyline_point (struct outp_driver *this UNUSED, int x UNUSED, int y UNUSED)
2049 assert (this->driver_open && this->page_open);
2052 ps_polyline_end (struct outp_driver *this UNUSED)
2054 assert (this->driver_open && this->page_open);
2057 /* Returns the width of string S for THIS driver. */
2059 text_width (struct outp_driver *this, char *s)
2061 struct outp_text text;
2063 text.options = OUTP_T_JUST_LEFT;
2064 ls_init (&text.s, s, strlen (s));
2065 this->class->text_metrics (this, &text);
2069 /* Write string S at location (X,Y) with width W for THIS driver. */
2071 out_text_plain (struct outp_driver *this, char *s, int x, int y, int w)
2073 struct outp_text text;
2075 text.options = OUTP_T_JUST_LEFT | OUTP_T_HORZ | OUTP_T_VERT;
2076 ls_init (&text.s, s, strlen (s));
2078 text.v = this->font_height;
2081 this->class->text_draw (this, &text);
2084 /* Draw top of page headers for THIS driver. */
2086 draw_headers (struct outp_driver *this)
2088 struct ps_driver_ext *ext = this->ext;
2090 struct font_entry *old_current = ext->current;
2091 char *old_family = xstrdup (ext->family); /* FIXME */
2092 int old_size = ext->size;
2094 int fh = this->font_height;
2097 fprintf (ext->file.file, "%d %d %d %d GB%s",
2098 0, YT (y), this->width, YT (y + 2 * fh + ext->line_gutter),
2100 this->class->text_set_font_family (this, "T");
2102 y += ext->line_width + ext->line_gutter;
2108 sprintf (buf, _("%s - Page %d"), curdate, ext->page_number);
2109 rh_width = text_width (this, buf);
2111 out_text_plain (this, buf, this->width - this->prop_em_width - rh_width,
2114 if (outp_title && outp_subtitle)
2115 out_text_plain (this, outp_title, this->prop_em_width, y,
2116 this->width - 3 * this->prop_em_width - rh_width);
2124 char *string = outp_subtitle ? outp_subtitle : outp_title;
2126 sprintf (buf, "%s - %s", version, host_system);
2127 rh_width = text_width (this, buf);
2129 out_text_plain (this, buf, this->width - this->prop_em_width - rh_width,
2133 out_text_plain (this, string, this->prop_em_width, y,
2134 this->width - 3 * this->prop_em_width - rh_width);
2139 ext->current = old_current;
2141 ext->family = old_family;
2142 ext->size = old_size;
2149 ps_text_set_font_by_name (struct outp_driver *this, const char *dit)
2151 struct ps_driver_ext *x = this->ext;
2152 struct font_entry *fe;
2154 assert (this->driver_open && this->page_open);
2156 /* Short-circuit common fonts. */
2157 if (!strcmp (dit, "PROP"))
2159 x->current = x->prop;
2160 x->size = x->font_size;
2163 else if (!strcmp (dit, "FIXED"))
2165 x->current = x->fixed;
2166 x->size = x->font_size;
2170 /* Find font_desc corresponding to Groff name dit. */
2171 fe = hsh_find (x->loaded, &dit);
2173 fe = load_font (this, dit);
2178 ps_text_set_font_by_position (struct outp_driver *this, int pos)
2180 struct ps_driver_ext *x = this->ext;
2183 assert (this->driver_open && this->page_open);
2185 /* Determine font name by suffixing position string to font family
2190 dit = local_alloc (strlen (x->family) + 3);
2191 cp = stpcpy (dit, x->family);
2213 /* Find font_desc corresponding to Groff name dit. */
2215 struct font_entry *fe = hsh_find (x->loaded, &dit);
2217 fe = load_font (this, dit);
2225 ps_text_set_font_family (struct outp_driver *this, const char *s)
2227 struct ps_driver_ext *x = this->ext;
2229 assert (this->driver_open && this->page_open);
2232 x->family = xstrdup (s);
2236 ps_text_get_font_name (struct outp_driver *this)
2238 struct ps_driver_ext *x = this->ext;
2240 assert (this->driver_open && this->page_open);
2241 return x->current->font->name;
2245 ps_text_get_font_family (struct outp_driver *this)
2247 struct ps_driver_ext *x = this->ext;
2249 assert (this->driver_open && this->page_open);
2254 ps_text_set_size (struct outp_driver *this, int size)
2256 struct ps_driver_ext *x = this->ext;
2258 assert (this->driver_open && this->page_open);
2259 x->size = PSUS / 72000 * size;
2264 ps_text_get_size (struct outp_driver *this, int *em_width)
2266 struct ps_driver_ext *x = this->ext;
2268 assert (this->driver_open && this->page_open);
2270 *em_width = (x->current->font->space_width * x->size) / 1000;
2271 return x->size / (PSUS / 72000);
2274 /* An output character. */
2277 struct font_entry *font; /* Font of character. */
2278 int size; /* Size of character. */
2279 int x, y; /* Location of character. */
2280 unsigned char ch; /* Character. */
2281 char separate; /* Must be separate from previous char. */
2284 /* Hash table comparison function for ps_combo structs. */
2286 compare_ps_combo (const void *pa, const void *pb, void *foo UNUSED)
2288 const struct ps_font_combo *a = pa;
2289 const struct ps_font_combo *b = pb;
2291 return !((a->font == b->font) && (a->size == b->size));
2294 /* Hash table hash function for ps_combo structs. */
2296 hash_ps_combo (const void *pa, void *foo UNUSED)
2298 const struct ps_font_combo *a = pa;
2299 unsigned name_hash = hsh_hash_string (a->font->font->internal_name);
2300 return name_hash ^ hsh_hash_int (a->size);
2303 /* Hash table free function for ps_combo structs. */
2305 free_ps_combo (void *a, void *foo UNUSED)
2310 /* Causes PostScript code to be output that switches to the font
2311 CP->FONT and font size CP->SIZE. The first time a particular
2312 font/size combination is used on a particular page, this involves
2313 outputting PostScript code to load the font. */
2315 switch_font (struct outp_driver *this, const struct output_char *cp)
2317 struct ps_driver_ext *ext = this->ext;
2318 struct ps_font_combo srch, **fc;
2320 srch.font = cp->font;
2321 srch.size = cp->size;
2323 fc = (struct ps_font_combo **) hsh_probe (ext->combos, &srch);
2326 fprintf (ext->file.file, "F%x%s", (*fc)->index, ext->eol);
2331 struct ps_encoding *encoding;
2334 *fc = xmalloc (sizeof **fc);
2335 (*fc)->font = cp->font;
2336 (*fc)->size = cp->size;
2337 (*fc)->index = ext->next_combo++;
2339 filename = find_encoding_file (this, cp->font->font->encoding);
2342 encoding = get_encoding (this, filename);
2347 msg (IE, _("PostScript driver: Cannot find encoding `%s' for "
2348 "PostScript font `%s'."), cp->font->font->encoding,
2349 cp->font->font->internal_name);
2350 encoding = default_encoding (this);
2353 if (cp->font != ext->fixed && cp->font != ext->prop)
2355 bp = stpcpy (buf, "%%IncludeResource: font ");
2356 bp = quote_ps_string (bp, cp->font->font->internal_name);
2357 bp = stpcpy (bp, ext->eol);
2362 bp = spprintf (bp, "/F%x E%x %d", (*fc)->index, encoding->index,
2364 bp = quote_ps_name (bp, cp->font->font->internal_name);
2365 sprintf (bp, " SF%s", ext->eol);
2366 fputs (buf, ext->file.file);
2368 ext->last_font = *fc;
2371 /* (write_text) Writes the accumulated line buffer to the output
2373 #define output_line() \
2376 lp = stpcpy (lp, ext->eol); \
2378 fputs (line, ext->file.file); \
2383 /* (write_text) Adds the string representing number X to the line
2384 buffer, flushing the buffer to disk beforehand if necessary. */
2385 #define put_number(X) \
2388 int n = nsprintf (number, "%d", X); \
2389 if (n + lp > &line[75]) \
2391 lp = stpcpy (lp, number); \
2395 /* Outputs PostScript code to THIS driver's output file to display the
2396 characters represented by the output_char's between CP and END,
2397 using the associated outp_text T to determine formatting. WIDTH is
2398 the width of the output region; WIDTH_LEFT is the amount of the
2399 WIDTH that is not taken up by text (so it can be used to determine
2402 write_text (struct outp_driver *this,
2403 const struct output_char *cp, const struct output_char *end,
2404 struct outp_text *t, int width UNUSED, int width_left)
2406 struct ps_driver_ext *ext = this->ext;
2411 char number[INT_DIGITS + 1];
2415 switch (t->options & OUTP_T_JUST_MASK)
2417 case OUTP_T_JUST_LEFT:
2420 case OUTP_T_JUST_RIGHT:
2423 case OUTP_T_JUST_CENTER:
2424 ofs = width_left / 2;
2435 int x = cp->x + ofs;
2436 int y = cp->y + (cp->font->font->ascent * cp->size / 1000);
2438 if (ext->last_font == NULL
2439 || cp->font != ext->last_font->font
2440 || cp->size != ext->last_font->size)
2441 switch_font (this, cp);
2447 static unsigned char literal_chars[ODA_COUNT][32] =
2449 {0x00, 0x00, 0x00, 0xf8, 0xff, 0xfc, 0xff, 0xff,
2450 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f,
2451 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2452 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2454 {0x00, 0x00, 0x00, 0xf8, 0xff, 0xfc, 0xff, 0xff,
2455 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
2456 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
2457 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
2459 {0x7e, 0xd6, 0xff, 0xfb, 0xff, 0xfc, 0xff, 0xff,
2460 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
2461 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
2462 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
2466 if (TEST_BIT (literal_chars[ext->data], cp->ch))
2472 lp = stpcpy (lp, "\\(");
2475 lp = stpcpy (lp, "\\)");
2478 lp = spprintf (lp, "\\%03o", cp->ch);
2483 while (cp < end && lp < &line[70] && cp->separate == 0);
2491 put_number (YT (y));
2502 if (lp >= &line[70])
2512 /* Displays the text in outp_text T, if DRAW is nonzero; or, merely
2513 determine the text metrics, if DRAW is zero. */
2515 text (struct outp_driver *this, struct outp_text *t, int draw)
2517 struct ps_driver_ext *ext = this->ext;
2520 struct output_char *buf; /* Output buffer. */
2521 struct output_char *buf_end; /* End of output buffer. */
2522 struct output_char *buf_loc; /* Current location in output buffer. */
2525 struct font_entry *old_current = ext->current;
2526 char *old_family = xstrdup (ext->family); /* FIXME */
2527 int old_size = ext->size;
2532 /* Current location. */
2535 /* Keeping track of what's left over. */
2536 int width; /* Width available for characters. */
2537 int width_left, height_left; /* Width, height left over. */
2538 int max_height; /* Tallest character on this line so far. */
2540 /* Previous character. */
2543 /* Information about location of previous space. */
2544 char *space_char; /* Character after space. */
2545 struct output_char *space_buf_loc; /* Buffer location after space. */
2546 int space_width_left; /* Width of characters before space. */
2548 /* Name of the current character. */
2549 const char *char_name;
2550 char local_char_name[2] = {0, 0};
2552 local_char_name[0] = local_char_name[1] = 0;
2554 buf = local_alloc (sizeof *buf * 128);
2555 buf_end = &buf[128];
2558 assert (!ls_null_p (&t->s));
2559 cp = ls_c_str (&t->s);
2560 end = ls_end (&t->s);
2568 width = width_left = (t->options & OUTP_T_HORZ) ? t->h : INT_MAX;
2569 height_left = (t->options & OUTP_T_VERT) ? t->v : INT_MAX;
2573 space_buf_loc = NULL;
2574 space_width_left = 0;
2577 if (!width || !height_left)
2582 struct char_metrics *metric;
2588 /* Set char_name to the name of the character or ligature at
2590 local_char_name[0] = *cp;
2591 char_name = local_char_name;
2592 if (ext->current->font->ligatures && *cp == 'f')
2601 lig = LIG_fi, char_name = "fi";
2604 lig = LIG_fl, char_name = "fl";
2611 lig = LIG_ffi, char_name = "ffi";
2614 lig = LIG_ffl, char_name = "ffl";
2617 lig = LIG_ff, char_name = "ff";
2621 if ((lig & ext->current->font->ligatures) == 0)
2623 local_char_name[0] = *cp; /* 'f' */
2624 char_name = local_char_name;
2627 else if (*cp == '\n')
2631 write_text (this, buf, buf_loc, t, width, width_left);
2638 height_left -= max_height;
2644 /* FIXME: when we're page buffering it will be necessary to
2645 set separate to 1. */
2648 cp += strlen (char_name);
2650 /* Figure out what size this character is, and what kern
2651 adjustment we need. */
2652 cur_char = font_char_name_to_index (char_name);
2653 metric = font_get_char_metrics (ext->current->font, cur_char);
2656 static struct char_metrics m;
2658 m.width = ext->current->font->space_width;
2659 m.code = *char_name;
2661 kern_amt = font_get_kern_adjust (ext->current->font, prev_char,
2665 kern_amt = (kern_amt * ext->size / 1000);
2668 char_width = metric->width * ext->size / 1000;
2670 /* Record the current status if this is a space character. */
2671 if (cur_char == space_index && buf_loc > buf)
2674 space_buf_loc = buf_loc;
2675 space_width_left = width_left;
2678 /* Drop down to a new line if there's no room left on this
2680 if (char_width + kern_amt > width_left)
2682 /* Regress to previous space, if any. */
2686 width_left = space_width_left;
2687 buf_loc = space_buf_loc;
2692 write_text (this, buf, buf_loc, t, width, width_left);
2699 height_left -= max_height;
2707 /* FIXME: when we're page buffering it will be
2708 necessary to set separate to 1. */
2713 if (ext->size > max_height)
2714 max_height = ext->size;
2715 if (max_height > height_left)
2718 /* Actually draw the character. */
2721 if (buf_loc >= buf_end)
2723 int buf_len = buf_end - buf;
2727 struct output_char *new_buf;
2729 new_buf = xmalloc (sizeof *new_buf * 256);
2730 memcpy (new_buf, buf, sizeof *new_buf * 128);
2731 buf_loc = new_buf + 128;
2732 buf_end = new_buf + 256;
2738 buf = xrealloc (buf, sizeof *buf * buf_len * 2);
2739 buf_loc = buf + buf_len;
2740 buf_end = buf + buf_len * 2;
2745 buf_loc->font = ext->current;
2746 buf_loc->size = ext->size;
2749 buf_loc->ch = metric->code;
2750 buf_loc->separate = separate;
2755 /* Prepare for next iteration. */
2756 width_left -= char_width + kern_amt;
2757 prev_char = cur_char;
2759 height_left -= max_height;
2760 if (buf_loc > buf && draw)
2761 write_text (this, buf, buf_loc, t, width, width_left);
2764 if (!(t->options & OUTP_T_HORZ))
2765 t->h = INT_MAX - width_left;
2766 if (!(t->options & OUTP_T_VERT))
2767 t->v = INT_MAX - height_left;
2769 t->v -= height_left;
2770 if (buf_end - buf == 128)
2774 ext->current = old_current;
2776 ext->family = old_family;
2777 ext->size = old_size;
2781 ps_text_metrics (struct outp_driver *this, struct outp_text *t)
2783 assert (this->driver_open && this->page_open);
2788 ps_text_draw (struct outp_driver *this, struct outp_text *t)
2790 assert (this->driver_open && this->page_open);
2796 /* Translate a filename to a font. */
2797 struct filename2font
2799 char *filename; /* Normalized filename. */
2800 struct font_desc *font;
2803 /* Table of `filename2font's. */
2804 static struct hsh_table *ps_fonts;
2806 /* Hash table comparison function for filename2font structs. */
2808 compare_filename2font (const void *a, const void *b, void *param UNUSED)
2810 return strcmp (((struct filename2font *) a)->filename,
2811 ((struct filename2font *) b)->filename);
2814 /* Hash table hash function for filename2font structs. */
2816 hash_filename2font (const void *f2f_, void *param UNUSED)
2818 const struct filename2font *f2f = f2f_;
2819 return hsh_hash_string (f2f->filename);
2822 /* Initializes the global font list by creating the hash table for
2823 translation of filenames to font_desc structs. */
2827 ps_fonts = hsh_create (31, compare_filename2font, hash_filename2font,
2834 hsh_destroy (ps_fonts);
2837 /* Loads the font having Groff name DIT into THIS driver instance.
2838 Specifically, adds it into the THIS driver's `loaded' hash
2840 static struct font_entry *
2841 load_font (struct outp_driver *this, const char *dit)
2843 struct ps_driver_ext *x = this->ext;
2844 char *filename1, *filename2;
2846 struct font_entry *fe;
2848 filename1 = find_ps_file (this, dit);
2850 filename1 = xstrdup (dit);
2851 filename2 = fn_normalize (filename1);
2854 entry = hsh_probe (ps_fonts, &filename2);
2857 struct filename2font *f2f;
2858 struct font_desc *f = groff_read_font (filename2);
2865 f = default_font ();
2868 f2f = xmalloc (sizeof *f2f);
2869 f2f->filename = filename2;
2876 fe = xmalloc (sizeof *fe);
2877 fe->dit = xstrdup (dit);
2878 fe->font = ((struct filename2font *) * entry)->font;
2879 *hsh_probe (x->loaded, &dit) = fe;
2885 ps_chart_initialise (struct outp_driver *this UNUSED, struct chart *ch)
2890 struct ps_driver_ext *x = this->ext;
2891 char page_size[128];
2893 int x_origin, y_origin;
2895 ch->file = tmpfile ();
2896 if (ch->file == NULL)
2902 size = this->width < this->length ? this->width : this->length;
2903 x_origin = x->left_margin + (size - this->width) / 2;
2904 y_origin = x->bottom_margin + (size - this->length) / 2;
2906 snprintf (page_size, sizeof page_size,
2907 "a,xsize=%.3f,ysize=%.3f,xorigin=%.3f,yorigin=%.3f",
2908 (double) size / PSUS, (double) size / PSUS,
2909 (double) x_origin / PSUS, (double) y_origin / PSUS);
2911 ch->pl_params = pl_newplparams ();
2912 pl_setplparam (ch->pl_params, "PAGESIZE", page_size);
2913 ch->lp = pl_newpl_r ("ps", NULL, ch->file, stderr, ch->pl_params);
2918 ps_chart_finalise (struct outp_driver *this UNUSED, struct chart *ch UNUSED)
2921 struct ps_driver_ext *x = this->ext;
2923 static int doc_num = 0;
2925 if (this->page_open)
2927 this->class->close_page (this);
2928 this->page_open = 0;
2930 this->class->open_page (this);
2931 fprintf (x->file.file,
2933 "%d %d translate 1000 dup scale%s"
2935 "/showpage { } def%s"
2936 "0 setgray 0 setlinecap 1 setlinewidth%s"
2937 "0 setlinejoin 10 setmiterlimit [ ] 0 setdash newpath clear%s"
2938 "%%%%BeginDocument: %d%s",
2940 -x->left_margin, -x->bottom_margin, x->eol,
2948 while (fwrite (buf, 1, fread (buf, 1, sizeof buf, ch->file), x->file.file))
2952 fprintf (x->file.file,
2959 this->class->close_page (this);
2960 this->page_open = 0;
2964 /* PostScript driver class. */
2965 struct outp_class postscript_class =
2987 ps_line_intersection,
2994 ps_text_set_font_by_name,
2995 ps_text_set_font_by_position,
2996 ps_text_set_font_family,
2997 ps_text_get_font_name,
2998 ps_text_get_font_family,
3004 ps_chart_initialise,
3008 /* EPSF driver class. FIXME: Probably doesn't work right. */
3009 struct outp_class epsf_class =
3031 ps_line_intersection,
3038 ps_text_set_font_by_name,
3039 ps_text_set_font_by_position,
3040 ps_text_set_font_family,
3041 ps_text_get_font_name,
3042 ps_text_get_font_family,
3048 ps_chart_initialise,
3053 #endif /* NO_POSTSCRIPT */