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., 59 Temple Place - Suite 330, Boston, MA
20 /* AIX requires this to be the first thing in the file. */
23 #define alloca __builtin_alloca
31 #ifndef alloca /* predefined by HP cc +Olibcalls */
38 /*this #if encloses the remainder of the file. */
51 #if TIME_WITH_SYS_TIME
63 #include "bitvector.h"
77 optimize-text-size not implemented.
79 Line buffering is the only possibility; page buffering should also
84 Should add a field to give a file that has a list of fonts
87 Should add an option that tells the driver it can emit %%Include:'s.
89 Should have auto-encode=true stream-edit or whatever to allow
90 addition to list of encodings.
92 Should align fonts of different sizes along their baselines (see
95 /* PostScript driver options: (defaults listed first)
99 data=clean7bit|clean8bit|binary
102 paper-size=letter (see "papersize" file)
103 orientation=portrait|landscape
112 prologue-file=ps-prologue
114 encoding-file=ps-encodings
115 auto-encode=true|false
121 line-style=thick|double
127 optimize-text-size=1|0|2
128 optimize-line-size=1|0
129 max-fonts-simult=0 Max # of fonts in printer memory at once (0=infinite)
132 /* The number of `psus' (PostScript driver UnitS) per inch. Although
133 this is a #define, the value is expected never to change. If it
134 does, review all uses. */
137 /* Magic numbers for PostScript and EPSF drivers. */
147 OTN_PORTRAIT, /* Portrait. */
148 OTN_LANDSCAPE /* Landscape. */
151 /* Output options. */
154 OPO_MIRROR_HORZ = 001, /* 1=Mirror across a horizontal axis. */
155 OPO_MIRROR_VERT = 002, /* 1=Mirror across a vertical axis. */
156 OPO_ROTATE_180 = 004, /* 1=Rotate the page 180 degrees. */
157 OPO_COLOR = 010, /* 1=Enable color. */
158 OPO_HEADERS = 020, /* 1=Draw headers at top of page. */
159 OPO_AUTO_ENCODE = 040, /* 1=Add encodings semi-intelligently. */
160 OPO_DOUBLE_LINE = 0100 /* 1=Double lines instead of thick lines. */
163 /* Data allowed in output. */
166 ODA_CLEAN7BIT, /* 0x09, 0x0a, 0x0d, 0x1b...0x7e */
167 ODA_CLEAN8BIT, /* 0x09, 0x0a, 0x0d, 0x1b...0xff */
168 ODA_BINARY, /* 0x00...0xff */
172 /* Types of lines for purpose of caching. */
175 horz, /* Single horizontal. */
176 dbl_horz, /* Double horizontal. */
177 spl_horz, /* Special horizontal. */
178 vert, /* Single vertical. */
179 dbl_vert, /* Double vertical. */
180 spl_vert, /* Special vertical. */
187 int ind; /* Independent var. Don't reorder. */
188 int mdep; /* Maximum number of dependent var pairs. */
189 int ndep; /* Current number of dependent var pairs. */
190 int dep[1][2]; /* Dependent var pairs. */
193 /* Contents of ps_driver_ext.loaded. */
196 char *dit; /* Font Groff name. */
197 struct font_desc *font; /* Font descriptor. */
200 /* Combines a font with a font size for benefit of generated code. */
203 struct font_entry *font; /* Font. */
204 int size; /* Font size. */
205 int index; /* PostScript index. */
208 /* A font encoding. */
211 char *filename; /* Normalized filename of this encoding. */
212 int index; /* Index value. */
215 /* PostScript output driver extension record. */
218 /* User parameters. */
219 int orientation; /* OTN_PORTRAIT or OTN_LANDSCAPE. */
220 int output_options; /* OPO_*. */
221 int data; /* ODA_*. */
223 int left_margin; /* Left margin in psus. */
224 int right_margin; /* Right margin in psus. */
225 int top_margin; /* Top margin in psus. */
226 int bottom_margin; /* Bottom margin in psus. */
228 char eol[3]; /* End of line--CR, LF, or CRLF. */
230 char *font_dir; /* Font directory relative to font path. */
231 char *prologue_fn; /* Prologue's filename relative to font dir. */
232 char *desc_fn; /* DESC filename relative to font dir. */
233 char *encoding_fn; /* Encoding's filename relative to font dir. */
235 char *prop_family; /* Default proportional font family. */
236 char *fixed_family; /* Default fixed-pitch font family. */
237 int font_size; /* Default font size (psus). */
239 int line_gutter; /* Space around lines. */
240 int line_space; /* Space between lines. */
241 int line_width; /* Width of lines. */
242 int line_width_thick; /* Width of thick lines. */
244 int text_opt; /* Text optimization level. */
245 int line_opt; /* Line optimization level. */
246 int max_fonts; /* Max # of simultaneous fonts (0=infinite). */
248 /* Internal state. */
249 struct file_ext file; /* Output file. */
250 int page_number; /* Current page number. */
251 int file_page_number; /* Page number in this file. */
252 int w, l; /* Paper size. */
253 struct hsh_table *lines[n_line_types]; /* Line buffers. */
255 struct font_entry *prop; /* Default Roman proportional font. */
256 struct font_entry *fixed; /* Default Roman fixed-pitch font. */
257 struct hsh_table *loaded; /* Fonts in memory. */
259 struct hsh_table *combos; /* Combinations of fonts with font sizes. */
260 struct ps_font_combo *last_font; /* PostScript selected font. */
261 int next_combo; /* Next font combo position index. */
263 struct hsh_table *encodings;/* Set of encodings. */
264 int next_encoding; /* Next font encoding index. */
266 /* Currently selected font. */
267 struct font_entry *current; /* Current font. */
268 char *family; /* Font family. */
269 int size; /* Size in psus. */
273 /* Transform logical y-ordinate Y into a page ordinate. */
274 #define YT(Y) (this->length - (Y))
277 static int postopen (struct file_ext *);
278 static int preclose (struct file_ext *);
279 static void draw_headers (struct outp_driver *this);
281 static int compare_font_entry (const void *, const void *, void *param);
282 static unsigned hash_font_entry (const void *, void *param);
283 static void free_font_entry (void *, void *foo);
284 static struct font_entry *load_font (struct outp_driver *, const char *dit);
285 static void init_fonts (void);
287 static void dump_lines (struct outp_driver *this);
289 static void read_ps_encodings (struct outp_driver *this);
290 static int compare_ps_encoding (const void *pa, const void *pb, void *foo);
291 static unsigned hash_ps_encoding (const void *pa, void *foo);
292 static void free_ps_encoding (void *a, void *foo);
293 static void add_encoding (struct outp_driver *this, char *filename);
294 static struct ps_encoding *default_encoding (struct outp_driver *this);
296 static int compare_ps_combo (const void *pa, const void *pb, void *foo);
297 static unsigned hash_ps_combo (const void *pa, void *foo);
298 static void free_ps_combo (void *a, void *foo);
300 static char *quote_ps_name (char *dest, const char *string);
301 static char *quote_ps_string (char *dest, const char *string);
303 /* Driver initialization. */
306 ps_open_global (struct outp_class *this unused)
314 ps_close_global (struct outp_class *this unused)
320 ps_font_sizes (struct outp_class *this unused, int *n_valid_sizes)
322 /* Allow fonts up to 1" in height. */
323 static int valid_sizes[] =
326 assert (n_valid_sizes != NULL);
332 ps_preopen_driver (struct outp_driver *this)
334 struct ps_driver_ext *x;
338 assert (this->driver_open == 0);
339 msg (VM (1), _("PostScript driver initializing as `%s'..."), this->name);
341 this->ext = x = xmalloc (sizeof (struct ps_driver_ext));
343 this->horiz = this->vert = 1;
344 this->width = this->length = 0;
346 x->orientation = OTN_PORTRAIT;
347 x->output_options = OPO_COLOR | OPO_HEADERS | OPO_AUTO_ENCODE;
348 x->data = ODA_CLEAN7BIT;
350 x->left_margin = x->right_margin =
351 x->top_margin = x->bottom_margin = PSUS / 2;
353 strcpy (x->eol, "\n");
356 x->prologue_fn = NULL;
358 x->encoding_fn = NULL;
360 x->prop_family = NULL;
361 x->fixed_family = NULL;
362 x->font_size = PSUS * 10 / 72;
364 x->line_gutter = PSUS / 144;
365 x->line_space = PSUS / 144;
366 x->line_width = PSUS / 144;
367 x->line_width_thick = PSUS / 48;
373 x->file.filename = NULL;
376 x->file.sequence_no = &x->page_number;
377 x->file.param = this;
378 x->file.postopen = postopen;
379 x->file.preclose = preclose;
383 x->file_page_number = 0;
384 for (i = 0; i < n_line_types; i++)
395 x->encodings = hsh_create (31, compare_ps_encoding, hash_ps_encoding,
396 free_ps_encoding, NULL);
397 x->next_encoding = 0;
407 ps_postopen_driver (struct outp_driver *this)
409 struct ps_driver_ext *x = this->ext;
411 assert (this->driver_open == 0);
413 if (this->width == 0)
415 this->width = PSUS * 17 / 2; /* Defaults to 8.5"x11". */
416 this->length = PSUS * 11;
419 if (x->text_opt == -1)
420 x->text_opt = (this->device & OUTP_DEV_SCREEN) ? 0 : 1;
421 if (x->line_opt == -1)
422 x->line_opt = (this->device & OUTP_DEV_SCREEN) ? 0 : 1;
426 if (x->orientation == OTN_LANDSCAPE)
428 int temp = this->width;
429 this->width = this->length;
432 this->width -= x->left_margin + x->right_margin;
433 this->length -= x->top_margin + x->bottom_margin;
434 if (x->output_options & OPO_HEADERS)
436 this->length -= 3 * x->font_size;
437 x->top_margin += 3 * x->font_size;
439 if (NULL == x->file.filename)
440 x->file.filename = xstrdup ("pspp.ps");
442 if (x->font_dir == NULL)
443 x->font_dir = xstrdup ("devps");
444 if (x->prologue_fn == NULL)
445 x->prologue_fn = xstrdup ("ps-prologue");
446 if (x->desc_fn == NULL)
447 x->desc_fn = xstrdup ("DESC");
448 if (x->encoding_fn == NULL)
449 x->encoding_fn = xstrdup ("ps-encodings");
451 if (x->prop_family == NULL)
452 x->prop_family = xstrdup ("H");
453 if (x->fixed_family == NULL)
454 x->fixed_family = xstrdup ("C");
456 read_ps_encodings (this);
461 if (this->length / x->font_size < 15)
463 msg (SE, _("PostScript driver: The defined page is not long "
464 "enough to hold margins and headers, plus least 15 "
465 "lines of the default fonts. In fact, there's only "
466 "room for %d lines of each font at the default size "
467 "of %d.%03d points."),
468 this->length / x->font_size,
469 x->font_size / 1000, x->font_size % 1000);
473 this->driver_open = 1;
474 msg (VM (2), _("%s: Initialization complete."), this->name);
480 ps_close_driver (struct outp_driver *this)
482 struct ps_driver_ext *x = this->ext;
486 assert (this->driver_open == 1);
487 msg (VM (2), _("%s: Beginning closing..."), this->name);
489 fn_close_ext (&x->file);
490 free (x->file.filename);
492 free (x->prologue_fn);
494 free (x->encoding_fn);
495 free (x->prop_family);
496 free (x->fixed_family);
498 for (i = 0; i < n_line_types; i++)
499 hsh_destroy (x->lines[i]);
500 hsh_destroy (x->encodings);
501 hsh_destroy (x->combos);
502 hsh_destroy (x->loaded);
505 this->driver_open = 0;
506 msg (VM (3), _("%s: Finished closing."), this->name);
511 /* font_entry comparison function for hash tables. */
513 compare_font_entry (const void *a, const void *b, void *foobar unused)
515 return strcmp (((struct font_entry *) a)->dit, ((struct font_entry *) b)->dit);
518 /* font_entry hash function for hash tables. */
520 hash_font_entry (const void *a, void *foobar unused)
522 return hashpjw (((struct font_entry *) a)->dit);
525 /* font_entry destructor function for hash tables. */
527 free_font_entry (void *pa, void *foo unused)
529 struct font_entry *a = pa;
534 /* Generic option types. */
544 /* All the options that the PostScript driver supports. */
545 static struct outp_option option_tab[] =
548 {"output-file", 1, 0},
549 {"paper-size", 2, 0},
550 {"orientation", 3, 0},
551 {"color", boolean_arg, 0},
553 {"auto-encode", boolean_arg, 5},
554 {"headers", boolean_arg, 1},
555 {"left-margin", pos_int_arg, 0},
556 {"right-margin", pos_int_arg, 1},
557 {"top-margin", pos_int_arg, 2},
558 {"bottom-margin", pos_int_arg, 3},
559 {"font-dir", string_arg, 0},
560 {"prologue-file", string_arg, 1},
561 {"device-file", string_arg, 2},
562 {"encoding-file", string_arg, 3},
563 {"prop-font-family", string_arg, 5},
564 {"fixed-font-family", string_arg, 6},
565 {"font-size", pos_int_arg, 4},
566 {"optimize-text-size", nonneg_int_arg, 0},
567 {"optimize-line-size", nonneg_int_arg, 1},
568 {"max-fonts-simult", nonneg_int_arg, 2},
570 {"line-style", 7, 0},
571 {"line-width", dimension_arg, 2},
572 {"line-gutter", dimension_arg, 3},
573 {"line-width", dimension_arg, 4},
574 {"line-width-thick", dimension_arg, 5},
578 static struct outp_option_info option_info;
581 ps_option (struct outp_driver *this, const char *key, const struct string *val)
583 struct ps_driver_ext *x = this->ext;
585 char *value = ds_value (val);
587 cat = outp_match_keyword (key, option_tab, &option_info, &subcat);
592 msg (SE, _("Unknown configuration parameter `%s' for PostScript device "
596 free (x->file.filename);
597 x->file.filename = xstrdup (value);
600 outp_get_paper_size (value, &this->width, &this->length);
603 if (!strcmp (value, "portrait"))
604 x->orientation = OTN_PORTRAIT;
605 else if (!strcmp (value, "landscape"))
606 x->orientation = OTN_LANDSCAPE;
608 msg (SE, _("Unknown orientation `%s'. Valid orientations are "
609 "`portrait' and `landscape'."), value);
612 if (!strcmp (value, "clean7bit") || !strcmp (value, "Clean7Bit"))
613 x->data = ODA_CLEAN7BIT;
614 else if (!strcmp (value, "clean8bit")
615 || !strcmp (value, "Clean8Bit"))
616 x->data = ODA_CLEAN8BIT;
617 else if (!strcmp (value, "binary") || !strcmp (value, "Binary"))
618 x->data = ODA_BINARY;
620 msg (SE, _("Unknown value for `data'. Valid values are `clean7bit', "
621 "`clean8bit', and `binary'."));
624 if (!strcmp (value, "lf"))
625 strcpy (x->eol, "\n");
626 else if (!strcmp (value, "crlf"))
627 strcpy (x->eol, "\r\n");
629 msg (SE, _("Unknown value for `line-ends'. Valid values are `lf' and "
633 if (!strcmp (value, "thick"))
634 x->output_options &= ~OPO_DOUBLE_LINE;
635 else if (!strcmp (value, "double"))
636 x->output_options |= OPO_DOUBLE_LINE;
638 msg (SE, _("Unknown value for `line-style'. Valid values are `thick' "
646 if (!strcmp (value, "on") || !strcmp (value, "true")
647 || !strcmp (value, "yes") || atoi (value))
649 else if (!strcmp (value, "off") || !strcmp (value, "false")
650 || !strcmp (value, "no") || !strcmp (value, "0"))
654 msg (SE, _("Boolean value expected for %s."), key);
666 mask = OPO_MIRROR_HORZ;
669 mask = OPO_MIRROR_VERT;
672 mask = OPO_ROTATE_180;
675 mask = OPO_AUTO_ENCODE;
681 x->output_options |= mask;
683 x->output_options &= ~mask;
692 arg = strtol (value, &tail, 0);
693 if (arg < 1 || errno == ERANGE || *tail)
695 msg (SE, _("Positive integer required as value for `%s'."), key);
698 if ((subcat == 4 || subcat == 5) && arg < 1000)
700 msg (SE, _("Default font size must be at least 1 point (value "
701 "of 1000 for key `%s')."), key);
707 x->left_margin = arg;
710 x->right_margin = arg;
716 x->bottom_margin = arg;
728 int dimension = outp_evaluate_dimension (value, NULL);
732 msg (SE, _("Value for `%s' must be a dimension of positive "
733 "length (i.e., `1in')."), key);
739 x->line_width = dimension;
742 x->line_gutter = dimension;
745 x->line_width = dimension;
748 x->line_width_thick = dimension;
764 dest = &x->prologue_fn;
770 dest = &x->encoding_fn;
773 dest = &x->prop_family;
776 dest = &x->fixed_family;
783 *dest = xstrdup (value);
792 arg = strtol (value, &tail, 0);
793 if (arg < 0 || errno == ERANGE || *tail)
795 msg (SE, _("Nonnegative integer required as value for `%s'."), key);
819 /* Looks for a PostScript font file or config file in all the
820 appropriate places. Returns the filename on success, NULL on
822 /* PORTME: Filename operations. */
824 find_ps_file (struct outp_driver *this, const char *name)
826 struct ps_driver_ext *x = this->ext;
829 /* x->font_dir + name: "devps/ps-encodings". */
832 /* Usually equal to groff_font_path. */
835 /* Final filename. */
839 basename = local_alloc (strlen (x->font_dir) + 1 + strlen (name) + 1);
840 cp = stpcpy (basename, x->font_dir);
841 *cp++ = DIR_SEPARATOR;
844 /* Decide on search path. */
846 const char *pre_pathname;
848 pre_pathname = getenv ("STAT_GROFF_FONT_PATH");
849 if (pre_pathname == NULL)
850 pre_pathname = getenv ("GROFF_FONT_PATH");
851 if (pre_pathname == NULL)
852 pre_pathname = groff_font_path;
853 pathname = fn_tilde_expand (pre_pathname);
856 /* Search all possible places for the file. */
857 fn = fn_search_path (basename, pathname, NULL);
859 fn = fn_search_path (basename, config_path, NULL);
861 fn = fn_search_path (name, pathname, NULL);
863 fn = fn_search_path (name, config_path, NULL);
865 local_free (basename);
872 /* Hash table comparison function for ps_encoding's. */
874 compare_ps_encoding (const void *pa, const void *pb, void *foo unused)
876 const struct ps_encoding *a = pa;
877 const struct ps_encoding *b = pb;
879 return strcmp (a->filename, b->filename);
882 /* Hash table hash function for ps_encoding's. */
884 hash_ps_encoding (const void *pa, void *foo unused)
886 const struct ps_encoding *a = pa;
888 return hashpjw (a->filename);
891 /* Hash table free function for ps_encoding's. */
893 free_ps_encoding (void *pa, void *foo unused)
895 struct ps_encoding *a = pa;
901 /* Iterates through the list of encodings used for this driver
902 instance, reads each of them from disk, and writes them as
903 PostScript code to the output file. */
905 output_encodings (struct outp_driver *this)
907 struct ps_driver_ext *x = this->ext;
909 struct hsh_iterator iter;
910 struct ps_encoding *pe;
912 struct string line, buf;
914 ds_init (NULL, &line, 128);
915 ds_init (NULL, &buf, 128);
916 hsh_iterator_init (iter);
917 while ((pe = hsh_foreach (x->encodings, &iter)) != NULL)
921 msg (VM (1), _("%s: %s: Opening PostScript font encoding..."),
922 this->name, pe->filename);
924 f = fopen (pe->filename, "r");
927 msg (IE, _("PostScript driver: Cannot open encoding file `%s': %s. "
928 "Substituting ISOLatin1Encoding for missing encoding."),
929 pe->filename, strerror (errno));
930 fprintf (x->file.file, "/E%x ISOLatin1Encoding def%s",
935 struct file_locator where;
937 const char *tab[256];
944 const char *notdef = ".notdef";
948 for (i = 0; i < 256; i++)
951 where.filename = pe->filename;
952 where.line_number = 0;
953 err_push_file_locator (&where);
955 while (ds_get_config_line (f, &buf, &where))
959 pschar = strtok_r (ds_value (&buf), " \t\r\n", &sp);
960 code = strtok_r (NULL, " \t\r\n", &sp);
961 if (*pschar == 0 || *code == 0)
963 code_val = strtol (code, &fubar, 0);
966 msg (IS, _("PostScript driver: Invalid numeric format."));
969 if (code_val < 0 || code_val > 255)
971 msg (IS, _("PostScript driver: Codes must be between 0 "
972 "and 255. (%d is not allowed.)"), code_val);
975 tab[code_val] = local_alloc (strlen (pschar) + 1);
976 strcpy ((char *) (tab[code_val]), pschar);
978 err_pop_file_locator (&where);
981 ds_printf (&line, "/E%x[", pe->index);
982 for (i = 0; i < 257; i++)
988 quote_ps_name (temp, tab[i]);
989 if (tab[i] != notdef)
993 strcpy (temp, "]def");
995 if (ds_length (&line) + strlen (temp) > 70)
997 ds_concat (&line, x->eol);
998 fputs (ds_value (&line), x->file.file);
1001 ds_concat (&line, temp);
1003 ds_concat (&line, x->eol);
1004 fputs (ds_value (&line), x->file.file);
1006 if (fclose (f) == EOF)
1007 msg (MW, _("PostScript driver: Error closing encoding file `%s'."),
1010 msg (VM (2), _("%s: PostScript font encoding read successfully."),
1018 /* Finds the ps_encoding in THIS that corresponds to the file with
1019 name NORM_FILENAME, which must have previously been normalized with
1020 normalize_filename(). */
1021 static struct ps_encoding *
1022 get_encoding (struct outp_driver *this, const char *norm_filename)
1024 struct ps_driver_ext *x = this->ext;
1025 struct ps_encoding *pe;
1027 pe = (struct ps_encoding *) hsh_find (x->encodings, (void *) &norm_filename);
1031 /* Searches the filesystem for an encoding file with name FILENAME;
1032 returns its malloc'd, normalized name if found, otherwise NULL. */
1034 find_encoding_file (struct outp_driver *this, char *filename)
1038 if (filename == NULL)
1040 while (isspace ((unsigned char) *filename))
1042 for (cp = filename; *cp && !isspace ((unsigned char) *cp); cp++)
1048 temp = find_ps_file (this, filename);
1052 filename = fn_normalize (temp);
1053 assert (filename != NULL);
1059 /* Adds the encoding represented by the not-necessarily-normalized
1060 file FILENAME to the list of encodings, if it exists and is not
1061 already in the list. */
1063 add_encoding (struct outp_driver *this, char *filename)
1065 struct ps_driver_ext *x = this->ext;
1067 struct ps_encoding **pe;
1069 filename = find_encoding_file (this, filename);
1073 pe = (struct ps_encoding **) hsh_probe (x->encodings, (void *) &filename);
1079 *pe = xmalloc (sizeof **pe);
1080 (*pe)->filename = filename;
1081 (*pe)->index = x->next_encoding++;
1084 /* Finds the file on disk that contains the list of encodings to
1085 include in the output file, then adds those encodings to the list
1088 read_ps_encodings (struct outp_driver *this)
1090 struct ps_driver_ext *x = this->ext;
1092 /* Encodings file. */
1093 char *encoding_fn; /* `ps-encodings' filename. */
1097 struct file_locator where;
1099 /* It's okay if there's no list of encodings; not everyone cares. */
1100 encoding_fn = find_ps_file (this, x->encoding_fn);
1101 if (encoding_fn == NULL)
1105 msg (VM (1), _("%s: %s: Opening PostScript encoding list file."),
1106 this->name, encoding_fn);
1107 f = fopen (encoding_fn, "r");
1110 msg (IE, _("Opening %s: %s."), encoding_fn, strerror (errno));
1114 where.filename = encoding_fn;
1115 where.line_number = 0;
1116 err_push_file_locator (&where);
1118 ds_init (NULL, &line, 128);
1124 if (!ds_get_config_line (f, &line, &where))
1127 msg (ME, _("Reading %s: %s."), encoding_fn, strerror (errno));
1131 add_encoding (this, bp);
1135 err_pop_file_locator (&where);
1137 if (-1 == fclose (f))
1138 msg (MW, _("Closing %s: %s."), encoding_fn, strerror (errno));
1140 msg (VM (2), _("%s: PostScript encoding list file read successfully."), this->name);
1143 /* Creates a default encoding for driver D that can be substituted for
1144 an unavailable encoding. */
1145 struct ps_encoding *
1146 default_encoding (struct outp_driver *d)
1148 struct ps_driver_ext *x = d->ext;
1149 static struct ps_encoding *enc;
1153 enc = xmalloc (sizeof *enc);
1154 enc->filename = xstrdup (_("<<default encoding>>"));
1155 enc->index = x->next_encoding++;
1160 /* Basic file operations. */
1162 /* Variables for the prologue. */
1169 static struct ps_variable *ps_var_tab;
1171 /* Searches ps_var_tab for a ps_variable with key KEY, and returns the
1172 associated value. */
1174 ps_get_var (const char *key)
1176 struct ps_variable *v;
1178 for (v = ps_var_tab; v->key; v++)
1179 if (!strcmp (key, v->key))
1184 /* Writes the PostScript prologue to file F. */
1186 postopen (struct file_ext *f)
1188 static struct ps_variable dict[] =
1190 {"bounding-box", 0},
1199 {"scale-factor", 0},
1201 {"paper-length", 0},
1205 {"line-width-thick", 0},
1210 char boundbox[INT_DIGITS * 4 + 4];
1214 char scaling[INT_DIGITS + 5];
1218 char paper_width[INT_DIGITS + 1];
1219 char paper_length[INT_DIGITS + 1];
1220 char left_margin[INT_DIGITS + 1];
1221 char top_margin[INT_DIGITS + 1];
1222 char line_width[INT_DIGITS + 1];
1223 char line_width_thick[INT_DIGITS + 1];
1225 struct outp_driver *this = f->param;
1226 struct ps_driver_ext *x = this->ext;
1228 char *prologue_fn = find_ps_file (this, x->prologue_fn);
1229 FILE *prologue_file;
1232 size_t buf_size = 0;
1234 x->loaded = hsh_create (31, compare_font_entry, hash_font_entry,
1235 free_font_entry, NULL);
1238 char *font_name = local_alloc (2 + max (strlen (x->prop_family),
1239 strlen (x->fixed_family)));
1241 strcpy (stpcpy (font_name, x->prop_family), "R");
1242 x->prop = load_font (this, font_name);
1244 strcpy (stpcpy (font_name, x->fixed_family), "R");
1245 x->fixed = load_font (this, font_name);
1247 local_free(font_name);
1250 x->current = x->prop;
1251 x->family = xstrdup (x->prop_family);
1252 x->size = x->font_size;
1255 int *h = this->horiz_line_width, *v = this->vert_line_width;
1257 this->cp_x = this->cp_y = 0;
1258 this->font_height = x->font_size;
1260 struct char_metrics *metric;
1262 metric = font_get_char_metrics (x->prop->font, '0');
1263 this->prop_em_width = ((metric
1264 ? metric->width : x->prop->font->space_width)
1265 * x->font_size / 1000);
1267 metric = font_get_char_metrics (x->fixed->font, '0');
1268 this->fixed_width = ((metric
1269 ? metric->width : x->fixed->font->space_width)
1270 * x->font_size / 1000);
1274 h[1] = v[1] = 2 * x->line_gutter + x->line_width;
1275 if (x->output_options & OPO_DOUBLE_LINE)
1276 h[2] = v[2] = 2 * x->line_gutter + 2 * x->line_width + x->line_space;
1278 h[2] = v[2] = 2 * x->line_gutter + x->line_width_thick;
1279 h[3] = v[3] = 2 * x->line_gutter + x->line_width;
1284 for (i = 0; i < (1 << OUTP_L_COUNT); i++)
1288 /* Maximum width of any line type so far. */
1291 for (bit = 0; bit < OUTP_L_COUNT; bit++)
1292 if ((i & (1 << bit)) && h[bit] > max)
1294 this->horiz_line_spacing[i] = this->vert_line_spacing[i] = max;
1299 if (x->output_options & OPO_AUTO_ENCODE)
1301 /* It's okay if this is done more than once since add_encoding()
1302 is idempotent over identical encodings. */
1303 add_encoding (this, x->prop->font->encoding);
1304 add_encoding (this, x->fixed->font->encoding);
1307 x->file_page_number = 0;
1310 if (prologue_fn == NULL)
1312 msg (IE, _("Cannot find PostScript prologue. The use of `-vv' "
1313 "on the command line is suggested as a debugging aid."));
1317 msg (VM (1), _("%s: %s: Opening PostScript prologue..."),
1318 this->name, prologue_fn);
1319 prologue_file = fopen (prologue_fn, "rb");
1320 if (prologue_file == NULL)
1322 fclose (prologue_file);
1324 msg (IE, "%s: %s", prologue_fn, strerror (errno));
1328 sprintf (boundbox, "0 0 %d %d",
1329 x->w / (PSUS / 72) + (x->w % (PSUS / 72) > 0),
1330 x->l / (PSUS / 72) + (x->l % (PSUS / 72) > 0));
1331 dict[0].value = boundbox;
1333 dict[1].value = (char *) version;
1335 curtime = time (NULL);
1336 loctime = localtime (&curtime);
1337 dict[2].value = asctime (loctime);
1338 cp = strchr (dict[2].value, '\n');
1345 dict[3].value = "Clean7Bit";
1348 dict[3].value = "Clean8Bit";
1351 dict[3].value = "Binary";
1357 if (x->orientation == OTN_PORTRAIT)
1358 dict[4].value = "Portrait";
1360 dict[4].value = "Landscape";
1362 /* PORTME: Determine username, net address. */
1364 dict[5].value = getenv ("LOGNAME");
1366 dict[5].value = getlogin ();
1368 dict[5].value = _("nobody");
1370 if (gethostname (host, 128) == -1)
1372 if (errno == ENAMETOOLONG)
1375 strcpy (host, _("nowhere"));
1377 dict[6].value = host;
1378 #else /* !HAVE_UNISTD_H */
1379 dict[5].value = _("nobody");
1380 dict[6].value = _("nowhere");
1381 #endif /* !HAVE_UNISTD_H */
1383 cp = stpcpy (p = local_alloc (288), "font ");
1384 quote_ps_string (cp, x->prop->font->internal_name);
1387 cp = stpcpy (p = local_alloc (288), "font ");
1388 quote_ps_string (cp, x->fixed->font->internal_name);
1391 sprintf (scaling, "%.3f", PSUS / 72.0);
1392 dict[9].value = scaling;
1394 sprintf (paper_width, "%g", x->w / (PSUS / 72.0));
1395 dict[10].value = paper_width;
1397 sprintf (paper_length, "%g", x->l / (PSUS / 72.0));
1398 dict[11].value = paper_length;
1400 sprintf (left_margin, "%d", x->left_margin);
1401 dict[12].value = left_margin;
1403 sprintf (top_margin, "%d", x->top_margin);
1404 dict[13].value = top_margin;
1406 sprintf (line_width, "%d", x->line_width);
1407 dict[14].value = line_width;
1409 sprintf (line_width, "%d", x->line_width_thick);
1410 dict[15].value = line_width_thick;
1412 getl_location (&dict[17].value, NULL);
1413 if (dict[17].value == NULL)
1414 dict[17].value = "<stdin>";
1418 dict[16].value = cp = local_alloc (strlen (dict[17].value) + 30);
1419 sprintf (cp, "PSPP (%s)", dict[17].value);
1423 dict[16].value = local_alloc (strlen (outp_title) + 1);
1424 strcpy ((char *) (dict[16].value), outp_title);
1428 while (-1 != getline (&buf, &buf_size, prologue_file))
1434 cp = strstr (buf, "!eps");
1437 if (this->class->magic == MAGIC_PS)
1444 cp = strstr (buf, "!ps");
1447 if (this->class->magic == MAGIC_EPSF)
1452 if (strstr (buf, "!!!"))
1457 if (!strncmp (buf, "!encodings", 10))
1458 output_encodings (this);
1462 beg = buf2 = fn_interp_vars (buf, ps_get_var);
1463 len = strlen (buf2);
1464 while (isspace (*beg))
1466 if (beg[len - 1] == '\n')
1468 if (beg[len - 1] == '\r')
1470 fwrite (beg, len, 1, f->file);
1471 fputs (x->eol, f->file);
1475 if (ferror (f->file))
1476 msg (IE, _("Reading `%s': %s."), prologue_fn, strerror (errno));
1477 fclose (prologue_file);
1482 local_free (dict[7].value);
1483 local_free (dict[8].value);
1484 local_free (dict[16].value);
1486 if (ferror (f->file))
1489 msg (VM (2), _("%s: PostScript prologue read successfully."), this->name);
1493 msg (VM (1), _("%s: Error reading PostScript prologue."), this->name);
1497 /* Writes the string STRING to buffer DEST (of at least 288
1498 characters) as a PostScript name object. Returns a pointer
1499 to the null terminator of the resultant string. */
1501 quote_ps_name (char *dest, const char *string)
1505 for (sp = string; *sp; sp++)
1506 switch (*(unsigned char *) sp)
1579 for (sp = string; *sp && dp < &dest[256]; sp++)
1581 sprintf (dp, "%02x", *(unsigned char *) sp);
1584 return stpcpy (dp, ">cvn");
1588 return stpcpy (&dest[1], string);
1591 /* Adds the string STRING to buffer DEST as a PostScript quoted
1592 string; returns a pointer to the null terminator added. Will not
1593 add more than 235 characters. */
1595 quote_ps_string (char *dest, const char *string)
1597 const char *sp = string;
1601 for (; *sp && dp < &dest[235]; sp++)
1603 dp = stpcpy (dp, "\\(");
1604 else if (*sp == ')')
1605 dp = stpcpy (dp, "\\)");
1606 else if (*sp < 32 || *((unsigned char *) sp) > 127)
1607 dp = spprintf (dp, "\\%3o", *sp);
1610 return stpcpy (dp, ")");
1613 /* Writes the PostScript epilogue to file F. */
1615 preclose (struct file_ext *f)
1617 struct outp_driver *this = f->param;
1618 struct ps_driver_ext *x = this->ext;
1619 struct hsh_iterator iter;
1620 struct font_entry *fe;
1625 "%%%%DocumentNeededResources:%s"),
1626 x->eol, x->file_page_number, x->eol, x->eol);
1628 hsh_iterator_init (iter);
1629 while ((fe = hsh_foreach (x->loaded, &iter)) != NULL)
1633 cp = stpcpy (buf, "%%+ font ");
1634 cp = quote_ps_string (cp, fe->font->internal_name);
1635 strcpy (cp, x->eol);
1636 fputs (buf, f->file);
1639 hsh_destroy (x->loaded);
1641 hsh_destroy (x->combos);
1643 x->last_font = NULL;
1646 fprintf (f->file, "%%EOF%s", x->eol);
1647 if (ferror (f->file))
1653 ps_open_page (struct outp_driver *this)
1655 struct ps_driver_ext *x = this->ext;
1657 assert (this->driver_open && !this->page_open);
1660 if (!fn_open_ext (&x->file))
1663 msg (ME, _("PostScript output driver: %s: %s"), x->file.filename,
1667 x->file_page_number++;
1669 hsh_destroy (x->combos);
1670 x->combos = hsh_create (31, compare_ps_combo, hash_ps_combo,
1671 free_ps_combo, NULL);
1672 x->last_font = NULL;
1675 fprintf (x->file.file,
1677 "%%%%BeginPageSetup%s"
1678 "/pg save def 0.001 dup scale%s",
1679 x->page_number, x->file_page_number, x->eol,
1683 if (x->orientation == OTN_LANDSCAPE)
1684 fprintf (x->file.file,
1685 "%d 0 translate 90 rotate%s",
1688 if (x->bottom_margin != 0 || x->left_margin != 0)
1689 fprintf (x->file.file,
1690 "%d %d translate%s",
1691 x->left_margin, x->bottom_margin, x->eol);
1693 fprintf (x->file.file,
1694 "/LW %d def/TW %d def %d setlinewidth%s"
1695 "%%%%EndPageSetup%s",
1696 x->line_width, x->line_width_thick, x->line_width, x->eol,
1699 if (!ferror (x->file.file))
1701 this->page_open = 1;
1702 if (x->output_options & OPO_HEADERS)
1703 draw_headers (this);
1706 return !ferror (x->file.file);
1710 ps_close_page (struct outp_driver *this)
1712 struct ps_driver_ext *x = this->ext;
1714 assert (this->driver_open && this->page_open);
1719 fprintf (x->file.file,
1724 this->page_open = 0;
1725 return !ferror (x->file.file);
1730 /* qsort() comparison function for int tuples. */
1732 int_2_compare (const void *a, const void *b)
1734 return *((const int *) a) - *((const int *) b);
1737 /* Hash table comparison function for cached lines. */
1739 compare_line (const void *a, const void *b, void *foo unused)
1741 return ((struct line_form *) a)->ind - ((struct line_form *) b)->ind;
1744 /* Hash table hash function for cached lines. */
1746 hash_line (const void *pa, void *foo unused)
1748 const struct line_form *a = pa;
1753 /* Hash table free function for cached lines. */
1755 free_line (void *pa, void *foo unused)
1760 /* Writes PostScript code to draw a line from (x1,y1) to (x2,y2) to
1762 #define dump_line(x1, y1, x2, y2) \
1763 fprintf (ext->file.file, "%d %d %d %d L%s", \
1764 x1, YT (y1), x2, YT (y2), ext->eol)
1766 /* Write PostScript code to draw a thick line from (x1,y1) to (x2,y2)
1767 to the output file. */
1768 #define dump_thick_line(x1, y1, x2, y2) \
1769 fprintf (ext->file.file, "%d %d %d %d TL%s", \
1770 x1, YT (y1), x2, YT (y2), ext->eol)
1772 /* Writes a line of type TYPE to THIS driver's output file. The line
1773 (or its center, in the case of double lines) has its independent
1774 axis coordinate at IND; it extends from DEP1 to DEP2 on the
1777 dump_fancy_line (struct outp_driver *this, int type, int ind, int dep1, int dep2)
1779 struct ps_driver_ext *ext = this->ext;
1780 int ofs = ext->line_space / 2 + ext->line_width / 2;
1785 dump_line (dep1, ind, dep2, ind);
1788 if (ext->output_options & OPO_DOUBLE_LINE)
1790 dump_line (dep1, ind - ofs, dep2, ind - ofs);
1791 dump_line (dep1, ind + ofs, dep2, ind + ofs);
1794 dump_thick_line (dep1, ind, dep2, ind);
1799 dump_line (ind, dep1, ind, dep2);
1802 if (ext->output_options & OPO_DOUBLE_LINE)
1804 dump_line (ind - ofs, dep1, ind - ofs, dep2);
1805 dump_line (ind + ofs, dep1, ind + ofs, dep2);
1808 dump_thick_line (ind, dep1, ind, dep2);
1819 /* Writes all the cached lines to the output file, then clears the
1822 dump_lines (struct outp_driver *this)
1824 struct ps_driver_ext *x = this->ext;
1826 struct hsh_iterator iter;
1827 struct line_form *line;
1830 hsh_iterator_init (iter);
1831 for (type = 0; type < n_line_types; type++)
1833 while (NULL != (line = hsh_foreach (x->lines[type], &iter)))
1836 int lo = INT_MIN, hi;
1838 qsort (line->dep, line->ndep, sizeof *line->dep, int_2_compare);
1839 lo = line->dep[0][0];
1840 hi = line->dep[0][1];
1841 for (i = 1; i < line->ndep; i++)
1842 if (line->dep[i][0] <= hi + 1)
1844 int min_hi = line->dep[i][1];
1850 dump_fancy_line (this, type, line->ind, lo, hi);
1851 lo = line->dep[i][0];
1852 hi = line->dep[i][1];
1854 dump_fancy_line (this, type, line->ind, lo, hi);
1857 hsh_destroy (x->lines[type]);
1858 x->lines[type] = NULL;
1862 /* (Same args as dump_fancy_line()). Either dumps the line directly
1863 to the output file, or adds it to the cache, depending on the
1864 user-selected line optimization mode. */
1866 line (struct outp_driver *this, int type, int ind, int dep1, int dep2)
1868 struct ps_driver_ext *ext = this->ext;
1869 struct line_form **f;
1871 assert (dep2 >= dep1);
1872 if (ext->line_opt == 0)
1874 dump_fancy_line (this, type, ind, dep1, dep2);
1878 if (ext->lines[type] == NULL)
1879 ext->lines[type] = hsh_create (31, compare_line, hash_line,
1881 f = (struct line_form **) hsh_probe (ext->lines[type],
1882 (struct line_form *) & ind);
1885 *f = xmalloc (sizeof **f + sizeof (int[15][2]));
1889 (*f)->dep[0][0] = dep1;
1890 (*f)->dep[0][1] = dep2;
1893 if ((*f)->ndep >= (*f)->mdep)
1896 *f = xrealloc (*f, (sizeof **f + sizeof (int[2]) * ((*f)->mdep - 1)));
1898 (*f)->dep[(*f)->ndep][0] = dep1;
1899 (*f)->dep[(*f)->ndep][1] = dep2;
1904 ps_line_horz (struct outp_driver *this, const struct rect *r,
1905 const struct color *c unused, int style)
1907 /* Must match output.h:OUTP_L_*. */
1908 static const int types[OUTP_L_COUNT] =
1909 {-1, horz, dbl_horz, spl_horz};
1911 int y = (r->y1 + r->y2) / 2;
1913 assert (this->driver_open && this->page_open);
1914 assert (style >= 0 && style < OUTP_L_COUNT);
1915 style = types[style];
1917 line (this, style, y, r->x1, r->x2);
1921 ps_line_vert (struct outp_driver *this, const struct rect *r,
1922 const struct color *c unused, int style)
1924 /* Must match output.h:OUTP_L_*. */
1925 static const int types[OUTP_L_COUNT] =
1926 {-1, vert, dbl_vert, spl_vert};
1928 int x = (r->x1 + r->x2) / 2;
1930 assert (this->driver_open && this->page_open);
1931 assert (style >= 0 && style < OUTP_L_COUNT);
1932 style = types[style];
1934 line (this, style, x, r->y1, r->y2);
1937 #define L (style->l != OUTP_L_NONE)
1938 #define R (style->r != OUTP_L_NONE)
1939 #define T (style->t != OUTP_L_NONE)
1940 #define B (style->b != OUTP_L_NONE)
1943 ps_line_intersection (struct outp_driver *this, const struct rect *r,
1944 const struct color *c unused,
1945 const struct outp_styles *style)
1947 struct ps_driver_ext *ext = this->ext;
1949 int x = (r->x1 + r->x2) / 2;
1950 int y = (r->y1 + r->y2) / 2;
1951 int ofs = (ext->line_space + ext->line_width) / 2;
1952 int x1 = x - ofs, x2 = x + ofs;
1953 int y1 = y - ofs, y2 = y + ofs;
1955 assert (this->driver_open && this->page_open);
1956 assert (!((style->l != style->r && style->l != OUTP_L_NONE
1957 && style->r != OUTP_L_NONE)
1958 || (style->t != style->b && style->t != OUTP_L_NONE
1959 && style->b != OUTP_L_NONE)));
1961 switch ((style->l | style->r) | ((style->t | style->b) << 8))
1963 case (OUTP_L_SINGLE) | (OUTP_L_SINGLE << 8):
1964 case (OUTP_L_SINGLE) | (OUTP_L_NONE << 8):
1965 case (OUTP_L_NONE) | (OUTP_L_SINGLE << 8):
1967 line (this, horz, y, r->x1, x);
1969 line (this, horz, y, x, r->x2);
1971 line (this, vert, x, r->y1, y);
1973 line (this, vert, x, y, r->y2);
1975 case (OUTP_L_SINGLE) | (OUTP_L_DOUBLE << 8):
1976 case (OUTP_L_NONE) | (OUTP_L_DOUBLE << 8):
1978 line (this, horz, y, r->x1, x1);
1980 line (this, horz, y, x2, r->x2);
1982 line (this, dbl_vert, x, r->y1, y);
1984 line (this, dbl_vert, x, y, r->y2);
1985 if ((L && R) && !(T && B))
1986 line (this, horz, y, x1, x2);
1988 case (OUTP_L_DOUBLE) | (OUTP_L_SINGLE << 8):
1989 case (OUTP_L_DOUBLE) | (OUTP_L_NONE << 8):
1991 line (this, dbl_horz, y, r->x1, x);
1993 line (this, dbl_horz, y, x, r->x2);
1995 line (this, vert, x, r->y1, y);
1997 line (this, vert, x, y, r->y2);
1998 if ((T && B) && !(L && R))
1999 line (this, vert, x, y1, y2);
2001 case (OUTP_L_DOUBLE) | (OUTP_L_DOUBLE << 8):
2003 line (this, dbl_horz, y, r->x1, x);
2005 line (this, dbl_horz, y, x, r->x2);
2007 line (this, dbl_vert, x, r->y1, y);
2009 line (this, dbl_vert, x, y, r->y2);
2011 line (this, vert, x1, y1, y2);
2013 line (this, vert, x2, y1, y2);
2015 line (this, horz, y1, x1, x2);
2017 line (this, horz, y2, x1, x2);
2025 ps_line_width (struct outp_driver *this, int *width, int *height)
2027 struct ps_driver_ext *x = this->ext;
2029 assert (this->driver_open && this->page_open);
2030 width[0] = height[0] = 0;
2031 width[1] = height[1] = 2 * x->line_gutter + x->line_width;
2032 width[2] = height[2] = (2 * x->line_gutter + 2 * x->line_width
2034 width[3] = height[3] = 2 * x->line_gutter + x->line_width;
2038 ps_box (struct outp_driver *this unused, const struct rect *r unused,
2039 const struct color *bord unused, const struct color *fill unused)
2041 assert (this->driver_open && this->page_open);
2045 ps_polyline_begin (struct outp_driver *this unused,
2046 const struct color *c unused)
2048 assert (this->driver_open && this->page_open);
2051 ps_polyline_point (struct outp_driver *this unused, int x unused, int y unused)
2053 assert (this->driver_open && this->page_open);
2056 ps_polyline_end (struct outp_driver *this unused)
2058 assert (this->driver_open && this->page_open);
2061 /* Returns the width of string S for THIS driver. */
2063 text_width (struct outp_driver *this, char *s)
2065 struct outp_text text;
2067 text.options = OUTP_T_JUST_LEFT;
2068 ls_init (&text.s, s, strlen (s));
2069 this->class->text_metrics (this, &text);
2073 /* Write string S at location (X,Y) with width W for THIS driver. */
2075 out_text_plain (struct outp_driver *this, char *s, int x, int y, int w)
2077 struct outp_text text;
2079 text.options = OUTP_T_JUST_LEFT | OUTP_T_HORZ | OUTP_T_VERT;
2080 ls_init (&text.s, s, strlen (s));
2082 text.v = this->font_height;
2085 this->class->text_draw (this, &text);
2088 /* Draw top of page headers for THIS driver. */
2090 draw_headers (struct outp_driver *this)
2092 struct ps_driver_ext *ext = this->ext;
2094 struct font_entry *old_current = ext->current;
2095 char *old_family = xstrdup (ext->family); /* FIXME */
2096 int old_size = ext->size;
2098 int fh = this->font_height;
2101 fprintf (ext->file.file, "%d %d %d %d GB%s",
2102 0, YT (y), this->width, YT (y + 2 * fh + ext->line_gutter),
2104 this->class->text_set_font_family (this, "T");
2106 y += ext->line_width + ext->line_gutter;
2112 sprintf (buf, _("%s - Page %d"), curdate, ext->page_number);
2113 rh_width = text_width (this, buf);
2115 out_text_plain (this, buf, this->width - this->prop_em_width - rh_width,
2118 if (outp_title && outp_subtitle)
2119 out_text_plain (this, outp_title, this->prop_em_width, y,
2120 this->width - 3 * this->prop_em_width - rh_width);
2128 char *string = outp_subtitle ? outp_subtitle : outp_title;
2130 sprintf (buf, "%s - %s", version, host_system);
2131 rh_width = text_width (this, buf);
2133 out_text_plain (this, buf, this->width - this->prop_em_width - rh_width,
2137 out_text_plain (this, string, this->prop_em_width, y,
2138 this->width - 3 * this->prop_em_width - rh_width);
2143 ext->current = old_current;
2145 ext->family = old_family;
2146 ext->size = old_size;
2153 ps_text_set_font_by_name (struct outp_driver *this, const char *dit)
2155 struct ps_driver_ext *x = this->ext;
2156 struct font_entry *fe;
2158 assert (this->driver_open && this->page_open);
2160 /* Short-circuit common fonts. */
2161 if (!strcmp (dit, "PROP"))
2163 x->current = x->prop;
2164 x->size = x->font_size;
2167 else if (!strcmp (dit, "FIXED"))
2169 x->current = x->fixed;
2170 x->size = x->font_size;
2174 /* Find font_desc corresponding to Groff name dit. */
2175 fe = hsh_find (x->loaded, &dit);
2177 fe = load_font (this, dit);
2182 ps_text_set_font_by_position (struct outp_driver *this, int pos)
2184 struct ps_driver_ext *x = this->ext;
2187 assert (this->driver_open && this->page_open);
2189 /* Determine font name by suffixing position string to font family
2194 dit = local_alloc (strlen (x->family) + 3);
2195 cp = stpcpy (dit, x->family);
2217 /* Find font_desc corresponding to Groff name dit. */
2219 struct font_entry *fe = hsh_find (x->loaded, &dit);
2221 fe = load_font (this, dit);
2229 ps_text_set_font_family (struct outp_driver *this, const char *s)
2231 struct ps_driver_ext *x = this->ext;
2233 assert (this->driver_open && this->page_open);
2236 x->family = xstrdup (s);
2240 ps_text_get_font_name (struct outp_driver *this)
2242 struct ps_driver_ext *x = this->ext;
2244 assert (this->driver_open && this->page_open);
2245 return x->current->font->name;
2249 ps_text_get_font_family (struct outp_driver *this)
2251 struct ps_driver_ext *x = this->ext;
2253 assert (this->driver_open && this->page_open);
2258 ps_text_set_size (struct outp_driver *this, int size)
2260 struct ps_driver_ext *x = this->ext;
2262 assert (this->driver_open && this->page_open);
2263 x->size = PSUS / 72000 * size;
2268 ps_text_get_size (struct outp_driver *this, int *em_width)
2270 struct ps_driver_ext *x = this->ext;
2272 assert (this->driver_open && this->page_open);
2274 *em_width = (x->current->font->space_width * x->size) / 1000;
2275 return x->size / (PSUS / 72000);
2278 /* An output character. */
2281 struct font_entry *font; /* Font of character. */
2282 int size; /* Size of character. */
2283 int x, y; /* Location of character. */
2284 unsigned char ch; /* Character. */
2285 char separate; /* Must be separate from previous char. */
2288 /* Hash table comparison function for ps_combo structs. */
2290 compare_ps_combo (const void *pa, const void *pb, void *foo unused)
2292 const struct ps_font_combo *a = pa;
2293 const struct ps_font_combo *b = pb;
2295 return !((a->font == b->font) && (a->size == b->size));
2298 /* Hash table hash function for ps_combo structs. */
2300 hash_ps_combo (const void *pa, void *foo unused)
2302 const struct ps_font_combo *a = pa;
2304 return hashpjw (a->font->font->internal_name) ^ a->size;
2307 /* Hash table free function for ps_combo structs. */
2309 free_ps_combo (void *a, void *foo unused)
2314 /* Causes PostScript code to be output that switches to the font
2315 CP->FONT and font size CP->SIZE. The first time a particular
2316 font/size combination is used on a particular page, this involves
2317 outputting PostScript code to load the font. */
2319 switch_font (struct outp_driver *this, const struct output_char *cp)
2321 struct ps_driver_ext *ext = this->ext;
2322 struct ps_font_combo srch, **fc;
2324 srch.font = cp->font;
2325 srch.size = cp->size;
2327 fc = (struct ps_font_combo **) hsh_probe (ext->combos, &srch);
2330 fprintf (ext->file.file, "F%x%s", (*fc)->index, ext->eol);
2335 struct ps_encoding *encoding;
2338 *fc = xmalloc (sizeof **fc);
2339 (*fc)->font = cp->font;
2340 (*fc)->size = cp->size;
2341 (*fc)->index = ext->next_combo++;
2343 filename = find_encoding_file (this, cp->font->font->encoding);
2346 encoding = get_encoding (this, filename);
2351 msg (IE, _("PostScript driver: Cannot find encoding `%s' for "
2352 "PostScript font `%s'."), cp->font->font->encoding,
2353 cp->font->font->internal_name);
2354 encoding = default_encoding (this);
2357 if (cp->font != ext->fixed && cp->font != ext->prop)
2359 bp = stpcpy (buf, "%%IncludeResource: font ");
2360 bp = quote_ps_string (bp, cp->font->font->internal_name);
2361 bp = stpcpy (bp, ext->eol);
2366 bp = spprintf (bp, "/F%x E%x %d", (*fc)->index, encoding->index,
2368 bp = quote_ps_name (bp, cp->font->font->internal_name);
2369 sprintf (bp, " SF%s", ext->eol);
2370 fputs (buf, ext->file.file);
2372 ext->last_font = *fc;
2375 /* (write_text) Writes the accumulated line buffer to the output
2377 #define output_line() \
2380 lp = stpcpy (lp, ext->eol); \
2382 fputs (line, ext->file.file); \
2387 /* (write_text) Adds the string representing number X to the line
2388 buffer, flushing the buffer to disk beforehand if necessary. */
2389 #define put_number(X) \
2392 int n = nsprintf (number, "%d", X); \
2393 if (n + lp > &line[75]) \
2395 lp = stpcpy (lp, number); \
2399 /* Outputs PostScript code to THIS driver's output file to display the
2400 characters represented by the output_char's between CP and END,
2401 using the associated outp_text T to determine formatting. WIDTH is
2402 the width of the output region; WIDTH_LEFT is the amount of the
2403 WIDTH that is not taken up by text (so it can be used to determine
2406 write_text (struct outp_driver *this,
2407 const struct output_char *cp, const struct output_char *end,
2408 struct outp_text *t, int width unused, int width_left)
2410 struct ps_driver_ext *ext = this->ext;
2415 char number[INT_DIGITS + 1];
2419 switch (t->options & OUTP_T_JUST_MASK)
2421 case OUTP_T_JUST_LEFT:
2424 case OUTP_T_JUST_RIGHT:
2427 case OUTP_T_JUST_CENTER:
2428 ofs = width_left / 2;
2436 int x = cp->x + ofs;
2437 int y = cp->y + (cp->font->font->ascent * cp->size / 1000);
2439 if (ext->last_font == NULL
2440 || cp->font != ext->last_font->font
2441 || cp->size != ext->last_font->size)
2442 switch_font (this, cp);
2448 static unsigned char literal_chars[ODA_COUNT][32] =
2450 {0x00, 0x00, 0x00, 0xf8, 0xff, 0xfc, 0xff, 0xff,
2451 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f,
2452 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2453 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2455 {0x00, 0x00, 0x00, 0xf8, 0xff, 0xfc, 0xff, 0xff,
2456 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
2457 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
2458 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
2460 {0x7e, 0xd6, 0xff, 0xfb, 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,
2467 if (TEST_BIT (literal_chars[ext->data], cp->ch))
2473 lp = stpcpy (lp, "\\(");
2476 lp = stpcpy (lp, "\\)");
2479 lp = spprintf (lp, "\\%03o", cp->ch);
2484 while (cp < end && lp < &line[70] && cp->separate == 0);
2492 put_number (YT (y));
2503 if (lp >= &line[70])
2513 /* Displays the text in outp_text T, if DRAW is nonzero; or, merely
2514 determine the text metrics, if DRAW is zero. */
2516 text (struct outp_driver *this, struct outp_text *t, int draw)
2518 struct ps_driver_ext *ext = this->ext;
2521 struct output_char *buf; /* Output buffer. */
2522 struct output_char *buf_end; /* End of output buffer. */
2523 struct output_char *buf_loc; /* Current location in output buffer. */
2526 struct font_entry *old_current = ext->current;
2527 char *old_family = xstrdup (ext->family); /* FIXME */
2528 int old_size = ext->size;
2533 /* Current location. */
2536 /* Keeping track of what's left over. */
2537 int width; /* Width available for characters. */
2538 int width_left, height_left; /* Width, height left over. */
2539 int max_height; /* Tallest character on this line so far. */
2541 /* Previous character. */
2544 /* Information about location of previous space. */
2545 char *space_char; /* Character after space. */
2546 struct output_char *space_buf_loc; /* Buffer location after space. */
2547 int space_width_left; /* Width of characters before space. */
2549 /* Name of the current character. */
2550 const char *char_name;
2551 char local_char_name[2] = {0, 0};
2553 local_char_name[0] = local_char_name[1] = 0;
2555 buf = local_alloc (sizeof *buf * 128);
2556 buf_end = &buf[128];
2559 assert (!ls_null_p (&t->s));
2560 cp = ls_value (&t->s);
2561 end = ls_end (&t->s);
2567 width = width_left = (t->options & OUTP_T_HORZ) ? t->h : INT_MAX;
2568 height_left = (t->options & OUTP_T_VERT) ? t->v : INT_MAX;
2573 if (!width || !height_left)
2578 struct char_metrics *metric;
2584 /* Set char_name to the name of the character or ligature at
2586 if (ext->current->font->ligatures && *cp == 'f')
2594 lig = LIG_fi, char_name = "fi";
2597 lig = LIG_fl, char_name = "fl";
2604 lig = LIG_ffi, char_name = "ffi";
2607 lig = LIG_ffl, char_name = "ffl";
2610 lig = LIG_ff, char_name = "ff";
2614 if ((lig & ext->current->font->ligatures) == 0)
2616 local_char_name[0] = *cp++; /* 'f' */
2617 char_name = local_char_name;
2620 cp += strlen (char_name);
2622 else if (*cp == '\n')
2626 write_text (this, buf, buf_loc, t, width, width_left);
2633 height_left -= max_height;
2639 /* FIXME: when we're page buffering it will be necessary to
2640 set separate to 1. */
2645 local_char_name[0] = *cp++;
2646 char_name = local_char_name;
2649 /* Figure out what size this character is, and what kern
2650 adjustment we need. */
2651 cur_char = font_char_name_to_index (char_name);
2652 metric = font_get_char_metrics (ext->current->font, cur_char);
2655 static struct char_metrics m;
2657 m.width = ext->current->font->space_width;
2658 m.code = *char_name;
2660 kern_amt = font_get_kern_adjust (ext->current->font, prev_char,
2664 kern_amt = (kern_amt * ext->size / 1000);
2667 char_width = metric->width * ext->size / 1000;
2669 /* Record the current status if this is a space character. */
2670 if (cur_char == space_index && buf_loc > buf)
2673 space_buf_loc = buf_loc;
2674 space_width_left = width_left;
2677 /* Drop down to a new line if there's no room left on this
2679 if (char_width + kern_amt > width_left)
2681 /* Regress to previous space, if any. */
2685 width_left = space_width_left;
2686 buf_loc = space_buf_loc;
2691 write_text (this, buf, buf_loc, t, width, width_left);
2698 height_left -= max_height;
2706 /* FIXME: when we're page buffering it will be
2707 necessary to set separate to 1. */
2712 if (ext->size > max_height)
2713 max_height = ext->size;
2714 if (max_height > height_left)
2717 /* Actually draw the character. */
2720 if (buf_loc >= buf_end)
2722 int buf_len = buf_end - buf;
2726 struct output_char *new_buf;
2728 new_buf = xmalloc (sizeof *new_buf * 256);
2729 memcpy (new_buf, buf, sizeof *new_buf * 128);
2730 buf_loc = new_buf + 128;
2731 buf_end = new_buf + 256;
2737 buf = xrealloc (buf, sizeof *buf * buf_len * 2);
2738 buf_loc = buf + buf_len;
2739 buf_end = buf + buf_len * 2;
2744 buf_loc->font = ext->current;
2745 buf_loc->size = ext->size;
2748 buf_loc->ch = metric->code;
2749 buf_loc->separate = separate;
2754 /* Prepare for next iteration. */
2755 width_left -= char_width + kern_amt;
2756 prev_char = cur_char;
2758 height_left -= max_height;
2759 if (buf_loc > buf && draw)
2760 write_text (this, buf, buf_loc, t, width, width_left);
2763 if (!(t->options & OUTP_T_HORZ))
2764 t->h = INT_MAX - width_left;
2765 if (!(t->options & OUTP_T_VERT))
2766 t->v = INT_MAX - height_left;
2768 t->v -= height_left;
2769 if (buf_end - buf == 128)
2773 ext->current = old_current;
2775 ext->family = old_family;
2776 ext->size = old_size;
2780 ps_text_metrics (struct outp_driver *this, struct outp_text *t)
2782 assert (this->driver_open && this->page_open);
2787 ps_text_draw (struct outp_driver *this, struct outp_text *t)
2789 assert (this->driver_open && this->page_open);
2795 /* Translate a filename to a font. */
2796 struct filename2font
2798 char *filename; /* Normalized filename. */
2799 struct font_desc *font;
2802 /* Table of `filename2font's. */
2803 static struct hsh_table *ps_fonts;
2805 /* Hash table comparison function for filename2font structs. */
2807 compare_filename2font (const void *a, const void *b, void *param unused)
2809 return strcmp (((struct filename2font *) a)->filename,
2810 ((struct filename2font *) b)->filename);
2813 /* Hash table hash function for filename2font structs. */
2815 hash_filename2font (const void *a, void *param unused)
2817 /* I sure hope this works with long filenames. */
2818 return hashpjw (((struct filename2font *) a)->filename);
2821 /* Initializes the global font list by creating the hash table for
2822 translation of filenames to font_desc structs. */
2826 ps_fonts = hsh_create (31, compare_filename2font, hash_filename2font,
2830 /* Loads the font having Groff name DIT into THIS driver instance.
2831 Specifically, adds it into the THIS driver's `loaded' hash
2833 static struct font_entry *
2834 load_font (struct outp_driver *this, const char *dit)
2836 struct ps_driver_ext *x = this->ext;
2837 char *filename1, *filename2;
2839 struct font_entry *fe;
2841 filename1 = find_ps_file (this, dit);
2843 filename1 = xstrdup (dit);
2844 filename2 = fn_normalize (filename1);
2847 entry = hsh_probe (ps_fonts, &filename2);
2850 struct filename2font *f2f;
2851 struct font_desc *f = groff_read_font (filename2);
2858 f = default_font ();
2861 f2f = xmalloc (sizeof *f2f);
2862 f2f->filename = filename2;
2869 fe = xmalloc (sizeof *fe);
2870 fe->dit = xstrdup (dit);
2871 fe->font = ((struct filename2font *) * entry)->font;
2872 *hsh_probe (x->loaded, &dit) = fe;
2877 /* PostScript driver class. */
2878 struct outp_class postscript_class =
2900 ps_line_intersection,
2907 ps_text_set_font_by_name,
2908 ps_text_set_font_by_position,
2909 ps_text_set_font_family,
2910 ps_text_get_font_name,
2911 ps_text_get_font_family,
2918 /* EPSF driver class. FIXME: Probably doesn't work right. */
2919 struct outp_class epsf_class =
2941 ps_line_intersection,
2948 ps_text_set_font_by_name,
2949 ps_text_set_font_by_position,
2950 ps_text_set_font_family,
2951 ps_text_get_font_name,
2952 ps_text_get_font_family,
2959 #endif /* NO_POSTSCRIPT */