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 *fe_, void *foobar unused)
522 const struct font_entry *fe = fe_;
523 return hsh_hash_string (fe->dit);
526 /* font_entry destructor function for hash tables. */
528 free_font_entry (void *pa, void *foo unused)
530 struct font_entry *a = pa;
535 /* Generic option types. */
545 /* All the options that the PostScript driver supports. */
546 static struct outp_option option_tab[] =
549 {"output-file", 1, 0},
550 {"paper-size", 2, 0},
551 {"orientation", 3, 0},
552 {"color", boolean_arg, 0},
554 {"auto-encode", boolean_arg, 5},
555 {"headers", boolean_arg, 1},
556 {"left-margin", pos_int_arg, 0},
557 {"right-margin", pos_int_arg, 1},
558 {"top-margin", pos_int_arg, 2},
559 {"bottom-margin", pos_int_arg, 3},
560 {"font-dir", string_arg, 0},
561 {"prologue-file", string_arg, 1},
562 {"device-file", string_arg, 2},
563 {"encoding-file", string_arg, 3},
564 {"prop-font-family", string_arg, 5},
565 {"fixed-font-family", string_arg, 6},
566 {"font-size", pos_int_arg, 4},
567 {"optimize-text-size", nonneg_int_arg, 0},
568 {"optimize-line-size", nonneg_int_arg, 1},
569 {"max-fonts-simult", nonneg_int_arg, 2},
571 {"line-style", 7, 0},
572 {"line-width", dimension_arg, 2},
573 {"line-gutter", dimension_arg, 3},
574 {"line-width", dimension_arg, 4},
575 {"line-width-thick", dimension_arg, 5},
579 static struct outp_option_info option_info;
582 ps_option (struct outp_driver *this, const char *key, const struct string *val)
584 struct ps_driver_ext *x = this->ext;
586 char *value = ds_value (val);
588 cat = outp_match_keyword (key, option_tab, &option_info, &subcat);
593 msg (SE, _("Unknown configuration parameter `%s' for PostScript device "
597 free (x->file.filename);
598 x->file.filename = xstrdup (value);
601 outp_get_paper_size (value, &this->width, &this->length);
604 if (!strcmp (value, "portrait"))
605 x->orientation = OTN_PORTRAIT;
606 else if (!strcmp (value, "landscape"))
607 x->orientation = OTN_LANDSCAPE;
609 msg (SE, _("Unknown orientation `%s'. Valid orientations are "
610 "`portrait' and `landscape'."), value);
613 if (!strcmp (value, "clean7bit") || !strcmp (value, "Clean7Bit"))
614 x->data = ODA_CLEAN7BIT;
615 else if (!strcmp (value, "clean8bit")
616 || !strcmp (value, "Clean8Bit"))
617 x->data = ODA_CLEAN8BIT;
618 else if (!strcmp (value, "binary") || !strcmp (value, "Binary"))
619 x->data = ODA_BINARY;
621 msg (SE, _("Unknown value for `data'. Valid values are `clean7bit', "
622 "`clean8bit', and `binary'."));
625 if (!strcmp (value, "lf"))
626 strcpy (x->eol, "\n");
627 else if (!strcmp (value, "crlf"))
628 strcpy (x->eol, "\r\n");
630 msg (SE, _("Unknown value for `line-ends'. Valid values are `lf' and "
634 if (!strcmp (value, "thick"))
635 x->output_options &= ~OPO_DOUBLE_LINE;
636 else if (!strcmp (value, "double"))
637 x->output_options |= OPO_DOUBLE_LINE;
639 msg (SE, _("Unknown value for `line-style'. Valid values are `thick' "
647 if (!strcmp (value, "on") || !strcmp (value, "true")
648 || !strcmp (value, "yes") || atoi (value))
650 else if (!strcmp (value, "off") || !strcmp (value, "false")
651 || !strcmp (value, "no") || !strcmp (value, "0"))
655 msg (SE, _("Boolean value expected for %s."), key);
667 mask = OPO_MIRROR_HORZ;
670 mask = OPO_MIRROR_VERT;
673 mask = OPO_ROTATE_180;
676 mask = OPO_AUTO_ENCODE;
682 x->output_options |= mask;
684 x->output_options &= ~mask;
693 arg = strtol (value, &tail, 0);
694 if (arg < 1 || errno == ERANGE || *tail)
696 msg (SE, _("Positive integer required as value for `%s'."), key);
699 if ((subcat == 4 || subcat == 5) && arg < 1000)
701 msg (SE, _("Default font size must be at least 1 point (value "
702 "of 1000 for key `%s')."), key);
708 x->left_margin = arg;
711 x->right_margin = arg;
717 x->bottom_margin = arg;
729 int dimension = outp_evaluate_dimension (value, NULL);
733 msg (SE, _("Value for `%s' must be a dimension of positive "
734 "length (i.e., `1in')."), key);
740 x->line_width = dimension;
743 x->line_gutter = dimension;
746 x->line_width = dimension;
749 x->line_width_thick = dimension;
765 dest = &x->prologue_fn;
771 dest = &x->encoding_fn;
774 dest = &x->prop_family;
777 dest = &x->fixed_family;
784 *dest = xstrdup (value);
793 arg = strtol (value, &tail, 0);
794 if (arg < 0 || errno == ERANGE || *tail)
796 msg (SE, _("Nonnegative integer required as value for `%s'."), key);
820 /* Looks for a PostScript font file or config file in all the
821 appropriate places. Returns the filename on success, NULL on
823 /* PORTME: Filename operations. */
825 find_ps_file (struct outp_driver *this, const char *name)
827 struct ps_driver_ext *x = this->ext;
830 /* x->font_dir + name: "devps/ps-encodings". */
833 /* Usually equal to groff_font_path. */
836 /* Final filename. */
840 basename = local_alloc (strlen (x->font_dir) + 1 + strlen (name) + 1);
841 cp = stpcpy (basename, x->font_dir);
842 *cp++ = DIR_SEPARATOR;
845 /* Decide on search path. */
847 const char *pre_pathname;
849 pre_pathname = getenv ("STAT_GROFF_FONT_PATH");
850 if (pre_pathname == NULL)
851 pre_pathname = getenv ("GROFF_FONT_PATH");
852 if (pre_pathname == NULL)
853 pre_pathname = groff_font_path;
854 pathname = fn_tilde_expand (pre_pathname);
857 /* Search all possible places for the file. */
858 fn = fn_search_path (basename, pathname, NULL);
860 fn = fn_search_path (basename, config_path, NULL);
862 fn = fn_search_path (name, pathname, NULL);
864 fn = fn_search_path (name, config_path, NULL);
866 local_free (basename);
873 /* Hash table comparison function for ps_encoding's. */
875 compare_ps_encoding (const void *pa, const void *pb, void *foo unused)
877 const struct ps_encoding *a = pa;
878 const struct ps_encoding *b = pb;
880 return strcmp (a->filename, b->filename);
883 /* Hash table hash function for ps_encoding's. */
885 hash_ps_encoding (const void *pa, void *foo unused)
887 const struct ps_encoding *a = pa;
889 return hsh_hash_string (a->filename);
892 /* Hash table free function for ps_encoding's. */
894 free_ps_encoding (void *pa, void *foo unused)
896 struct ps_encoding *a = pa;
902 /* Iterates through the list of encodings used for this driver
903 instance, reads each of them from disk, and writes them as
904 PostScript code to the output file. */
906 output_encodings (struct outp_driver *this)
908 struct ps_driver_ext *x = this->ext;
910 struct hsh_iterator iter;
911 struct ps_encoding *pe;
913 struct string line, buf;
915 ds_init (NULL, &line, 128);
916 ds_init (NULL, &buf, 128);
917 for (pe = hsh_first (x->encodings, &iter); pe != NULL;
918 pe = hsh_next (x->encodings, &iter))
922 msg (VM (1), _("%s: %s: Opening PostScript font encoding..."),
923 this->name, pe->filename);
925 f = fopen (pe->filename, "r");
928 msg (IE, _("PostScript driver: Cannot open encoding file `%s': %s. "
929 "Substituting ISOLatin1Encoding for missing encoding."),
930 pe->filename, strerror (errno));
931 fprintf (x->file.file, "/E%x ISOLatin1Encoding def%s",
936 struct file_locator where;
938 const char *tab[256];
945 const char *notdef = ".notdef";
949 for (i = 0; i < 256; i++)
952 where.filename = pe->filename;
953 where.line_number = 0;
954 err_push_file_locator (&where);
956 while (ds_get_config_line (f, &buf, &where))
963 pschar = strtok_r (ds_value (&buf), " \t\r\n", &sp);
964 code = strtok_r (NULL, " \t\r\n", &sp);
965 if (*pschar == 0 || *code == 0)
967 code_val = strtol (code, &fubar, 0);
970 msg (IS, _("PostScript driver: Invalid numeric format."));
973 if (code_val < 0 || code_val > 255)
975 msg (IS, _("PostScript driver: Codes must be between 0 "
976 "and 255. (%d is not allowed.)"), code_val);
979 tab[code_val] = local_alloc (strlen (pschar) + 1);
980 strcpy ((char *) (tab[code_val]), pschar);
982 err_pop_file_locator (&where);
985 ds_printf (&line, "/E%x[", pe->index);
986 for (i = 0; i < 257; i++)
992 quote_ps_name (temp, tab[i]);
993 if (tab[i] != notdef)
997 strcpy (temp, "]def");
999 if (ds_length (&line) + strlen (temp) > 70)
1001 ds_concat (&line, x->eol);
1002 fputs (ds_value (&line), x->file.file);
1005 ds_concat (&line, temp);
1007 ds_concat (&line, x->eol);
1008 fputs (ds_value (&line), x->file.file);
1010 if (fclose (f) == EOF)
1011 msg (MW, _("PostScript driver: Error closing encoding file `%s'."),
1014 msg (VM (2), _("%s: PostScript font encoding read successfully."),
1022 /* Finds the ps_encoding in THIS that corresponds to the file with
1023 name NORM_FILENAME, which must have previously been normalized with
1024 normalize_filename(). */
1025 static struct ps_encoding *
1026 get_encoding (struct outp_driver *this, const char *norm_filename)
1028 struct ps_driver_ext *x = this->ext;
1029 struct ps_encoding *pe;
1031 pe = (struct ps_encoding *) hsh_find (x->encodings, (void *) &norm_filename);
1035 /* Searches the filesystem for an encoding file with name FILENAME;
1036 returns its malloc'd, normalized name if found, otherwise NULL. */
1038 find_encoding_file (struct outp_driver *this, char *filename)
1042 if (filename == NULL)
1044 while (isspace ((unsigned char) *filename))
1046 for (cp = filename; *cp && !isspace ((unsigned char) *cp); cp++)
1052 temp = find_ps_file (this, filename);
1056 filename = fn_normalize (temp);
1057 assert (filename != NULL);
1063 /* Adds the encoding represented by the not-necessarily-normalized
1064 file FILENAME to the list of encodings, if it exists and is not
1065 already in the list. */
1067 add_encoding (struct outp_driver *this, char *filename)
1069 struct ps_driver_ext *x = this->ext;
1070 struct ps_encoding **pe;
1072 filename = find_encoding_file (this, filename);
1076 pe = (struct ps_encoding **) hsh_probe (x->encodings, &filename);
1082 *pe = xmalloc (sizeof **pe);
1083 (*pe)->filename = filename;
1084 (*pe)->index = x->next_encoding++;
1087 /* Finds the file on disk that contains the list of encodings to
1088 include in the output file, then adds those encodings to the list
1091 read_ps_encodings (struct outp_driver *this)
1093 struct ps_driver_ext *x = this->ext;
1095 /* Encodings file. */
1096 char *encoding_fn; /* `ps-encodings' filename. */
1100 struct file_locator where;
1102 /* It's okay if there's no list of encodings; not everyone cares. */
1103 encoding_fn = find_ps_file (this, x->encoding_fn);
1104 if (encoding_fn == NULL)
1108 msg (VM (1), _("%s: %s: Opening PostScript encoding list file."),
1109 this->name, encoding_fn);
1110 f = fopen (encoding_fn, "r");
1113 msg (IE, _("Opening %s: %s."), encoding_fn, strerror (errno));
1117 where.filename = encoding_fn;
1118 where.line_number = 0;
1119 err_push_file_locator (&where);
1121 ds_init (NULL, &line, 128);
1127 if (!ds_get_config_line (f, &line, &where))
1130 msg (ME, _("Reading %s: %s."), encoding_fn, strerror (errno));
1134 add_encoding (this, bp);
1138 err_pop_file_locator (&where);
1140 if (-1 == fclose (f))
1141 msg (MW, _("Closing %s: %s."), encoding_fn, strerror (errno));
1143 msg (VM (2), _("%s: PostScript encoding list file read successfully."), this->name);
1146 /* Creates a default encoding for driver D that can be substituted for
1147 an unavailable encoding. */
1148 struct ps_encoding *
1149 default_encoding (struct outp_driver *d)
1151 struct ps_driver_ext *x = d->ext;
1152 static struct ps_encoding *enc;
1156 enc = xmalloc (sizeof *enc);
1157 enc->filename = xstrdup (_("<<default encoding>>"));
1158 enc->index = x->next_encoding++;
1163 /* Basic file operations. */
1165 /* Variables for the prologue. */
1172 static struct ps_variable *ps_var_tab;
1174 /* Searches ps_var_tab for a ps_variable with key KEY, and returns the
1175 associated value. */
1177 ps_get_var (const char *key)
1179 struct ps_variable *v;
1181 for (v = ps_var_tab; v->key; v++)
1182 if (!strcmp (key, v->key))
1187 /* Writes the PostScript prologue to file F. */
1189 postopen (struct file_ext *f)
1191 static struct ps_variable dict[] =
1193 {"bounding-box", 0},
1202 {"scale-factor", 0},
1204 {"paper-length", 0},
1208 {"line-width-thick", 0},
1213 char boundbox[INT_DIGITS * 4 + 4];
1217 char scaling[INT_DIGITS + 5];
1221 char paper_width[INT_DIGITS + 1];
1222 char paper_length[INT_DIGITS + 1];
1223 char left_margin[INT_DIGITS + 1];
1224 char top_margin[INT_DIGITS + 1];
1225 char line_width[INT_DIGITS + 1];
1226 char line_width_thick[INT_DIGITS + 1];
1228 struct outp_driver *this = f->param;
1229 struct ps_driver_ext *x = this->ext;
1231 char *prologue_fn = find_ps_file (this, x->prologue_fn);
1232 FILE *prologue_file;
1235 size_t buf_size = 0;
1237 x->loaded = hsh_create (31, compare_font_entry, hash_font_entry,
1238 free_font_entry, NULL);
1241 char *font_name = local_alloc (2 + max (strlen (x->prop_family),
1242 strlen (x->fixed_family)));
1244 strcpy (stpcpy (font_name, x->prop_family), "R");
1245 x->prop = load_font (this, font_name);
1247 strcpy (stpcpy (font_name, x->fixed_family), "R");
1248 x->fixed = load_font (this, font_name);
1250 local_free(font_name);
1253 x->current = x->prop;
1254 x->family = xstrdup (x->prop_family);
1255 x->size = x->font_size;
1258 int *h = this->horiz_line_width, *v = this->vert_line_width;
1260 this->cp_x = this->cp_y = 0;
1261 this->font_height = x->font_size;
1263 struct char_metrics *metric;
1265 metric = font_get_char_metrics (x->prop->font, '0');
1266 this->prop_em_width = ((metric
1267 ? metric->width : x->prop->font->space_width)
1268 * x->font_size / 1000);
1270 metric = font_get_char_metrics (x->fixed->font, '0');
1271 this->fixed_width = ((metric
1272 ? metric->width : x->fixed->font->space_width)
1273 * x->font_size / 1000);
1277 h[1] = v[1] = 2 * x->line_gutter + x->line_width;
1278 if (x->output_options & OPO_DOUBLE_LINE)
1279 h[2] = v[2] = 2 * x->line_gutter + 2 * x->line_width + x->line_space;
1281 h[2] = v[2] = 2 * x->line_gutter + x->line_width_thick;
1282 h[3] = v[3] = 2 * x->line_gutter + x->line_width;
1287 for (i = 0; i < (1 << OUTP_L_COUNT); i++)
1291 /* Maximum width of any line type so far. */
1294 for (bit = 0; bit < OUTP_L_COUNT; bit++)
1295 if ((i & (1 << bit)) && h[bit] > max)
1297 this->horiz_line_spacing[i] = this->vert_line_spacing[i] = max;
1302 if (x->output_options & OPO_AUTO_ENCODE)
1304 /* It's okay if this is done more than once since add_encoding()
1305 is idempotent over identical encodings. */
1306 add_encoding (this, x->prop->font->encoding);
1307 add_encoding (this, x->fixed->font->encoding);
1310 x->file_page_number = 0;
1313 if (prologue_fn == NULL)
1315 msg (IE, _("Cannot find PostScript prologue. The use of `-vv' "
1316 "on the command line is suggested as a debugging aid."));
1320 msg (VM (1), _("%s: %s: Opening PostScript prologue..."),
1321 this->name, prologue_fn);
1322 prologue_file = fopen (prologue_fn, "rb");
1323 if (prologue_file == NULL)
1325 fclose (prologue_file);
1327 msg (IE, "%s: %s", prologue_fn, strerror (errno));
1331 sprintf (boundbox, "0 0 %d %d",
1332 x->w / (PSUS / 72) + (x->w % (PSUS / 72) > 0),
1333 x->l / (PSUS / 72) + (x->l % (PSUS / 72) > 0));
1334 dict[0].value = boundbox;
1336 dict[1].value = (char *) version;
1338 curtime = time (NULL);
1339 loctime = localtime (&curtime);
1340 dict[2].value = asctime (loctime);
1341 cp = strchr (dict[2].value, '\n');
1348 dict[3].value = "Clean7Bit";
1351 dict[3].value = "Clean8Bit";
1354 dict[3].value = "Binary";
1360 if (x->orientation == OTN_PORTRAIT)
1361 dict[4].value = "Portrait";
1363 dict[4].value = "Landscape";
1365 /* PORTME: Determine username, net address. */
1367 dict[5].value = getenv ("LOGNAME");
1369 dict[5].value = getlogin ();
1371 dict[5].value = _("nobody");
1373 if (gethostname (host, 128) == -1)
1375 if (errno == ENAMETOOLONG)
1378 strcpy (host, _("nowhere"));
1380 dict[6].value = host;
1381 #else /* !HAVE_UNISTD_H */
1382 dict[5].value = _("nobody");
1383 dict[6].value = _("nowhere");
1384 #endif /* !HAVE_UNISTD_H */
1386 cp = stpcpy (p = local_alloc (288), "font ");
1387 quote_ps_string (cp, x->prop->font->internal_name);
1390 cp = stpcpy (p = local_alloc (288), "font ");
1391 quote_ps_string (cp, x->fixed->font->internal_name);
1394 sprintf (scaling, "%.3f", PSUS / 72.0);
1395 dict[9].value = scaling;
1397 sprintf (paper_width, "%g", x->w / (PSUS / 72.0));
1398 dict[10].value = paper_width;
1400 sprintf (paper_length, "%g", x->l / (PSUS / 72.0));
1401 dict[11].value = paper_length;
1403 sprintf (left_margin, "%d", x->left_margin);
1404 dict[12].value = left_margin;
1406 sprintf (top_margin, "%d", x->top_margin);
1407 dict[13].value = top_margin;
1409 sprintf (line_width, "%d", x->line_width);
1410 dict[14].value = line_width;
1412 sprintf (line_width, "%d", x->line_width_thick);
1413 dict[15].value = line_width_thick;
1415 getl_location (&dict[17].value, NULL);
1416 if (dict[17].value == NULL)
1417 dict[17].value = "<stdin>";
1421 dict[16].value = cp = local_alloc (strlen (dict[17].value) + 30);
1422 sprintf (cp, "PSPP (%s)", dict[17].value);
1426 dict[16].value = local_alloc (strlen (outp_title) + 1);
1427 strcpy ((char *) (dict[16].value), outp_title);
1431 while (-1 != getline (&buf, &buf_size, prologue_file))
1437 cp = strstr (buf, "!eps");
1440 if (this->class->magic == MAGIC_PS)
1447 cp = strstr (buf, "!ps");
1450 if (this->class->magic == MAGIC_EPSF)
1455 if (strstr (buf, "!!!"))
1460 if (!strncmp (buf, "!encodings", 10))
1461 output_encodings (this);
1465 beg = buf2 = fn_interp_vars (buf, ps_get_var);
1466 len = strlen (buf2);
1467 while (isspace (*beg))
1469 if (beg[len - 1] == '\n')
1471 if (beg[len - 1] == '\r')
1473 fwrite (beg, len, 1, f->file);
1474 fputs (x->eol, f->file);
1478 if (ferror (f->file))
1479 msg (IE, _("Reading `%s': %s."), prologue_fn, strerror (errno));
1480 fclose (prologue_file);
1485 local_free (dict[7].value);
1486 local_free (dict[8].value);
1487 local_free (dict[16].value);
1489 if (ferror (f->file))
1492 msg (VM (2), _("%s: PostScript prologue read successfully."), this->name);
1496 msg (VM (1), _("%s: Error reading PostScript prologue."), this->name);
1500 /* Writes the string STRING to buffer DEST (of at least 288
1501 characters) as a PostScript name object. Returns a pointer
1502 to the null terminator of the resultant string. */
1504 quote_ps_name (char *dest, const char *string)
1508 for (sp = string; *sp; sp++)
1509 switch (*(unsigned char *) sp)
1582 for (sp = string; *sp && dp < &dest[256]; sp++)
1584 sprintf (dp, "%02x", *(unsigned char *) sp);
1587 return stpcpy (dp, ">cvn");
1591 return stpcpy (&dest[1], string);
1594 /* Adds the string STRING to buffer DEST as a PostScript quoted
1595 string; returns a pointer to the null terminator added. Will not
1596 add more than 235 characters. */
1598 quote_ps_string (char *dest, const char *string)
1600 const char *sp = string;
1604 for (; *sp && dp < &dest[235]; sp++)
1606 dp = stpcpy (dp, "\\(");
1607 else if (*sp == ')')
1608 dp = stpcpy (dp, "\\)");
1609 else if (*sp < 32 || *((unsigned char *) sp) > 127)
1610 dp = spprintf (dp, "\\%3o", *sp);
1613 return stpcpy (dp, ")");
1616 /* Writes the PostScript epilogue to file F. */
1618 preclose (struct file_ext *f)
1620 struct outp_driver *this = f->param;
1621 struct ps_driver_ext *x = this->ext;
1622 struct hsh_iterator iter;
1623 struct font_entry *fe;
1628 "%%%%DocumentNeededResources:%s"),
1629 x->eol, x->file_page_number, x->eol, x->eol);
1631 for (fe = hsh_first (x->loaded, &iter); fe != NULL;
1632 fe = hsh_next (x->loaded, &iter))
1636 cp = stpcpy (buf, "%%+ font ");
1637 cp = quote_ps_string (cp, fe->font->internal_name);
1638 strcpy (cp, x->eol);
1639 fputs (buf, f->file);
1642 hsh_destroy (x->loaded);
1644 hsh_destroy (x->combos);
1646 x->last_font = NULL;
1649 fprintf (f->file, "%%EOF%s", x->eol);
1650 if (ferror (f->file))
1656 ps_open_page (struct outp_driver *this)
1658 struct ps_driver_ext *x = this->ext;
1660 assert (this->driver_open && !this->page_open);
1663 if (!fn_open_ext (&x->file))
1666 msg (ME, _("PostScript output driver: %s: %s"), x->file.filename,
1670 x->file_page_number++;
1672 hsh_destroy (x->combos);
1673 x->combos = hsh_create (31, compare_ps_combo, hash_ps_combo,
1674 free_ps_combo, NULL);
1675 x->last_font = NULL;
1678 fprintf (x->file.file,
1680 "%%%%BeginPageSetup%s"
1681 "/pg save def 0.001 dup scale%s",
1682 x->page_number, x->file_page_number, x->eol,
1686 if (x->orientation == OTN_LANDSCAPE)
1687 fprintf (x->file.file,
1688 "%d 0 translate 90 rotate%s",
1691 if (x->bottom_margin != 0 || x->left_margin != 0)
1692 fprintf (x->file.file,
1693 "%d %d translate%s",
1694 x->left_margin, x->bottom_margin, x->eol);
1696 fprintf (x->file.file,
1697 "/LW %d def/TW %d def %d setlinewidth%s"
1698 "%%%%EndPageSetup%s",
1699 x->line_width, x->line_width_thick, x->line_width, x->eol,
1702 if (!ferror (x->file.file))
1704 this->page_open = 1;
1705 if (x->output_options & OPO_HEADERS)
1706 draw_headers (this);
1709 return !ferror (x->file.file);
1713 ps_close_page (struct outp_driver *this)
1715 struct ps_driver_ext *x = this->ext;
1717 assert (this->driver_open && this->page_open);
1722 fprintf (x->file.file,
1727 this->page_open = 0;
1728 return !ferror (x->file.file);
1733 /* qsort() comparison function for int tuples. */
1735 int_2_compare (const void *a_, const void *b_)
1740 return *a < *b ? -1 : *a > *b;
1743 /* Hash table comparison function for cached lines. */
1745 compare_line (const void *a_, const void *b_, void *foo unused)
1747 const struct line_form *a = a_;
1748 const struct line_form *b = b_;
1750 return a->ind < b->ind ? -1 : a->ind > b->ind;
1753 /* Hash table hash function for cached lines. */
1755 hash_line (const void *pa, void *foo unused)
1757 const struct line_form *a = pa;
1762 /* Hash table free function for cached lines. */
1764 free_line (void *pa, void *foo unused)
1769 /* Writes PostScript code to draw a line from (x1,y1) to (x2,y2) to
1771 #define dump_line(x1, y1, x2, y2) \
1772 fprintf (ext->file.file, "%d %d %d %d L%s", \
1773 x1, YT (y1), x2, YT (y2), ext->eol)
1775 /* Write PostScript code to draw a thick line from (x1,y1) to (x2,y2)
1776 to the output file. */
1777 #define dump_thick_line(x1, y1, x2, y2) \
1778 fprintf (ext->file.file, "%d %d %d %d TL%s", \
1779 x1, YT (y1), x2, YT (y2), ext->eol)
1781 /* Writes a line of type TYPE to THIS driver's output file. The line
1782 (or its center, in the case of double lines) has its independent
1783 axis coordinate at IND; it extends from DEP1 to DEP2 on the
1786 dump_fancy_line (struct outp_driver *this, int type, int ind, int dep1, int dep2)
1788 struct ps_driver_ext *ext = this->ext;
1789 int ofs = ext->line_space / 2 + ext->line_width / 2;
1794 dump_line (dep1, ind, dep2, ind);
1797 if (ext->output_options & OPO_DOUBLE_LINE)
1799 dump_line (dep1, ind - ofs, dep2, ind - ofs);
1800 dump_line (dep1, ind + ofs, dep2, ind + ofs);
1803 dump_thick_line (dep1, ind, dep2, ind);
1808 dump_line (ind, dep1, ind, dep2);
1811 if (ext->output_options & OPO_DOUBLE_LINE)
1813 dump_line (ind - ofs, dep1, ind - ofs, dep2);
1814 dump_line (ind + ofs, dep1, ind + ofs, dep2);
1817 dump_thick_line (ind, dep1, ind, dep2);
1828 /* Writes all the cached lines to the output file, then clears the
1831 dump_lines (struct outp_driver *this)
1833 struct ps_driver_ext *x = this->ext;
1835 struct hsh_iterator iter;
1838 for (type = 0; type < n_line_types; type++)
1840 struct line_form *line;
1842 if (x->lines[type] == NULL)
1845 for (line = hsh_first (x->lines[type], &iter); line != NULL;
1846 line = hsh_next (x->lines[type], &iter))
1849 int lo = INT_MIN, hi;
1851 qsort (line->dep, line->ndep, sizeof *line->dep, int_2_compare);
1852 lo = line->dep[0][0];
1853 hi = line->dep[0][1];
1854 for (i = 1; i < line->ndep; i++)
1855 if (line->dep[i][0] <= hi + 1)
1857 int min_hi = line->dep[i][1];
1863 dump_fancy_line (this, type, line->ind, lo, hi);
1864 lo = line->dep[i][0];
1865 hi = line->dep[i][1];
1867 dump_fancy_line (this, type, line->ind, lo, hi);
1870 hsh_destroy (x->lines[type]);
1871 x->lines[type] = NULL;
1875 /* (Same args as dump_fancy_line()). Either dumps the line directly
1876 to the output file, or adds it to the cache, depending on the
1877 user-selected line optimization mode. */
1879 line (struct outp_driver *this, int type, int ind, int dep1, int dep2)
1881 struct ps_driver_ext *ext = this->ext;
1882 struct line_form **f;
1884 assert (dep2 >= dep1);
1885 if (ext->line_opt == 0)
1887 dump_fancy_line (this, type, ind, dep1, dep2);
1891 if (ext->lines[type] == NULL)
1892 ext->lines[type] = hsh_create (31, compare_line, hash_line,
1894 f = (struct line_form **) hsh_probe (ext->lines[type], &ind);
1897 *f = xmalloc (sizeof **f + sizeof (int[15][2]));
1901 (*f)->dep[0][0] = dep1;
1902 (*f)->dep[0][1] = dep2;
1905 if ((*f)->ndep >= (*f)->mdep)
1908 *f = xrealloc (*f, (sizeof **f + sizeof (int[2]) * ((*f)->mdep - 1)));
1910 (*f)->dep[(*f)->ndep][0] = dep1;
1911 (*f)->dep[(*f)->ndep][1] = dep2;
1916 ps_line_horz (struct outp_driver *this, const struct rect *r,
1917 const struct color *c unused, int style)
1919 /* Must match output.h:OUTP_L_*. */
1920 static const int types[OUTP_L_COUNT] =
1921 {-1, horz, dbl_horz, spl_horz};
1923 int y = (r->y1 + r->y2) / 2;
1925 assert (this->driver_open && this->page_open);
1926 assert (style >= 0 && style < OUTP_L_COUNT);
1927 style = types[style];
1929 line (this, style, y, r->x1, r->x2);
1933 ps_line_vert (struct outp_driver *this, const struct rect *r,
1934 const struct color *c unused, int style)
1936 /* Must match output.h:OUTP_L_*. */
1937 static const int types[OUTP_L_COUNT] =
1938 {-1, vert, dbl_vert, spl_vert};
1940 int x = (r->x1 + r->x2) / 2;
1942 assert (this->driver_open && this->page_open);
1943 assert (style >= 0 && style < OUTP_L_COUNT);
1944 style = types[style];
1946 line (this, style, x, r->y1, r->y2);
1949 #define L (style->l != OUTP_L_NONE)
1950 #define R (style->r != OUTP_L_NONE)
1951 #define T (style->t != OUTP_L_NONE)
1952 #define B (style->b != OUTP_L_NONE)
1955 ps_line_intersection (struct outp_driver *this, const struct rect *r,
1956 const struct color *c unused,
1957 const struct outp_styles *style)
1959 struct ps_driver_ext *ext = this->ext;
1961 int x = (r->x1 + r->x2) / 2;
1962 int y = (r->y1 + r->y2) / 2;
1963 int ofs = (ext->line_space + ext->line_width) / 2;
1964 int x1 = x - ofs, x2 = x + ofs;
1965 int y1 = y - ofs, y2 = y + ofs;
1967 assert (this->driver_open && this->page_open);
1968 assert (!((style->l != style->r && style->l != OUTP_L_NONE
1969 && style->r != OUTP_L_NONE)
1970 || (style->t != style->b && style->t != OUTP_L_NONE
1971 && style->b != OUTP_L_NONE)));
1973 switch ((style->l | style->r) | ((style->t | style->b) << 8))
1975 case (OUTP_L_SINGLE) | (OUTP_L_SINGLE << 8):
1976 case (OUTP_L_SINGLE) | (OUTP_L_NONE << 8):
1977 case (OUTP_L_NONE) | (OUTP_L_SINGLE << 8):
1979 line (this, horz, y, r->x1, x);
1981 line (this, horz, y, x, r->x2);
1983 line (this, vert, x, r->y1, y);
1985 line (this, vert, x, y, r->y2);
1987 case (OUTP_L_SINGLE) | (OUTP_L_DOUBLE << 8):
1988 case (OUTP_L_NONE) | (OUTP_L_DOUBLE << 8):
1990 line (this, horz, y, r->x1, x1);
1992 line (this, horz, y, x2, r->x2);
1994 line (this, dbl_vert, x, r->y1, y);
1996 line (this, dbl_vert, x, y, r->y2);
1997 if ((L && R) && !(T && B))
1998 line (this, horz, y, x1, x2);
2000 case (OUTP_L_DOUBLE) | (OUTP_L_SINGLE << 8):
2001 case (OUTP_L_DOUBLE) | (OUTP_L_NONE << 8):
2003 line (this, dbl_horz, y, r->x1, x);
2005 line (this, dbl_horz, y, x, r->x2);
2007 line (this, vert, x, r->y1, y);
2009 line (this, vert, x, y, r->y2);
2010 if ((T && B) && !(L && R))
2011 line (this, vert, x, y1, y2);
2013 case (OUTP_L_DOUBLE) | (OUTP_L_DOUBLE << 8):
2015 line (this, dbl_horz, y, r->x1, x);
2017 line (this, dbl_horz, y, x, r->x2);
2019 line (this, dbl_vert, x, r->y1, y);
2021 line (this, dbl_vert, x, y, r->y2);
2023 line (this, vert, x1, y1, y2);
2025 line (this, vert, x2, y1, y2);
2027 line (this, horz, y1, x1, x2);
2029 line (this, horz, y2, x1, x2);
2037 ps_line_width (struct outp_driver *this, int *width, int *height)
2039 struct ps_driver_ext *x = this->ext;
2041 assert (this->driver_open && this->page_open);
2042 width[0] = height[0] = 0;
2043 width[1] = height[1] = 2 * x->line_gutter + x->line_width;
2044 width[2] = height[2] = (2 * x->line_gutter + 2 * x->line_width
2046 width[3] = height[3] = 2 * x->line_gutter + x->line_width;
2050 ps_box (struct outp_driver *this unused, const struct rect *r unused,
2051 const struct color *bord unused, const struct color *fill unused)
2053 assert (this->driver_open && this->page_open);
2057 ps_polyline_begin (struct outp_driver *this unused,
2058 const struct color *c unused)
2060 assert (this->driver_open && this->page_open);
2063 ps_polyline_point (struct outp_driver *this unused, int x unused, int y unused)
2065 assert (this->driver_open && this->page_open);
2068 ps_polyline_end (struct outp_driver *this unused)
2070 assert (this->driver_open && this->page_open);
2073 /* Returns the width of string S for THIS driver. */
2075 text_width (struct outp_driver *this, char *s)
2077 struct outp_text text;
2079 text.options = OUTP_T_JUST_LEFT;
2080 ls_init (&text.s, s, strlen (s));
2081 this->class->text_metrics (this, &text);
2085 /* Write string S at location (X,Y) with width W for THIS driver. */
2087 out_text_plain (struct outp_driver *this, char *s, int x, int y, int w)
2089 struct outp_text text;
2091 text.options = OUTP_T_JUST_LEFT | OUTP_T_HORZ | OUTP_T_VERT;
2092 ls_init (&text.s, s, strlen (s));
2094 text.v = this->font_height;
2097 this->class->text_draw (this, &text);
2100 /* Draw top of page headers for THIS driver. */
2102 draw_headers (struct outp_driver *this)
2104 struct ps_driver_ext *ext = this->ext;
2106 struct font_entry *old_current = ext->current;
2107 char *old_family = xstrdup (ext->family); /* FIXME */
2108 int old_size = ext->size;
2110 int fh = this->font_height;
2113 fprintf (ext->file.file, "%d %d %d %d GB%s",
2114 0, YT (y), this->width, YT (y + 2 * fh + ext->line_gutter),
2116 this->class->text_set_font_family (this, "T");
2118 y += ext->line_width + ext->line_gutter;
2124 sprintf (buf, _("%s - Page %d"), curdate, ext->page_number);
2125 rh_width = text_width (this, buf);
2127 out_text_plain (this, buf, this->width - this->prop_em_width - rh_width,
2130 if (outp_title && outp_subtitle)
2131 out_text_plain (this, outp_title, this->prop_em_width, y,
2132 this->width - 3 * this->prop_em_width - rh_width);
2140 char *string = outp_subtitle ? outp_subtitle : outp_title;
2142 sprintf (buf, "%s - %s", version, host_system);
2143 rh_width = text_width (this, buf);
2145 out_text_plain (this, buf, this->width - this->prop_em_width - rh_width,
2149 out_text_plain (this, string, this->prop_em_width, y,
2150 this->width - 3 * this->prop_em_width - rh_width);
2155 ext->current = old_current;
2157 ext->family = old_family;
2158 ext->size = old_size;
2165 ps_text_set_font_by_name (struct outp_driver *this, const char *dit)
2167 struct ps_driver_ext *x = this->ext;
2168 struct font_entry *fe;
2170 assert (this->driver_open && this->page_open);
2172 /* Short-circuit common fonts. */
2173 if (!strcmp (dit, "PROP"))
2175 x->current = x->prop;
2176 x->size = x->font_size;
2179 else if (!strcmp (dit, "FIXED"))
2181 x->current = x->fixed;
2182 x->size = x->font_size;
2186 /* Find font_desc corresponding to Groff name dit. */
2187 fe = hsh_find (x->loaded, &dit);
2189 fe = load_font (this, dit);
2194 ps_text_set_font_by_position (struct outp_driver *this, int pos)
2196 struct ps_driver_ext *x = this->ext;
2199 assert (this->driver_open && this->page_open);
2201 /* Determine font name by suffixing position string to font family
2206 dit = local_alloc (strlen (x->family) + 3);
2207 cp = stpcpy (dit, x->family);
2229 /* Find font_desc corresponding to Groff name dit. */
2231 struct font_entry *fe = hsh_find (x->loaded, &dit);
2233 fe = load_font (this, dit);
2241 ps_text_set_font_family (struct outp_driver *this, const char *s)
2243 struct ps_driver_ext *x = this->ext;
2245 assert (this->driver_open && this->page_open);
2248 x->family = xstrdup (s);
2252 ps_text_get_font_name (struct outp_driver *this)
2254 struct ps_driver_ext *x = this->ext;
2256 assert (this->driver_open && this->page_open);
2257 return x->current->font->name;
2261 ps_text_get_font_family (struct outp_driver *this)
2263 struct ps_driver_ext *x = this->ext;
2265 assert (this->driver_open && this->page_open);
2270 ps_text_set_size (struct outp_driver *this, int size)
2272 struct ps_driver_ext *x = this->ext;
2274 assert (this->driver_open && this->page_open);
2275 x->size = PSUS / 72000 * size;
2280 ps_text_get_size (struct outp_driver *this, int *em_width)
2282 struct ps_driver_ext *x = this->ext;
2284 assert (this->driver_open && this->page_open);
2286 *em_width = (x->current->font->space_width * x->size) / 1000;
2287 return x->size / (PSUS / 72000);
2290 /* An output character. */
2293 struct font_entry *font; /* Font of character. */
2294 int size; /* Size of character. */
2295 int x, y; /* Location of character. */
2296 unsigned char ch; /* Character. */
2297 char separate; /* Must be separate from previous char. */
2300 /* Hash table comparison function for ps_combo structs. */
2302 compare_ps_combo (const void *pa, const void *pb, void *foo unused)
2304 const struct ps_font_combo *a = pa;
2305 const struct ps_font_combo *b = pb;
2307 return !((a->font == b->font) && (a->size == b->size));
2310 /* Hash table hash function for ps_combo structs. */
2312 hash_ps_combo (const void *pa, void *foo unused)
2314 const struct ps_font_combo *a = pa;
2315 unsigned name_hash = hsh_hash_string (a->font->font->internal_name);
2316 return name_hash ^ hsh_hash_int (a->size);
2319 /* Hash table free function for ps_combo structs. */
2321 free_ps_combo (void *a, void *foo unused)
2326 /* Causes PostScript code to be output that switches to the font
2327 CP->FONT and font size CP->SIZE. The first time a particular
2328 font/size combination is used on a particular page, this involves
2329 outputting PostScript code to load the font. */
2331 switch_font (struct outp_driver *this, const struct output_char *cp)
2333 struct ps_driver_ext *ext = this->ext;
2334 struct ps_font_combo srch, **fc;
2336 srch.font = cp->font;
2337 srch.size = cp->size;
2339 fc = (struct ps_font_combo **) hsh_probe (ext->combos, &srch);
2342 fprintf (ext->file.file, "F%x%s", (*fc)->index, ext->eol);
2347 struct ps_encoding *encoding;
2350 *fc = xmalloc (sizeof **fc);
2351 (*fc)->font = cp->font;
2352 (*fc)->size = cp->size;
2353 (*fc)->index = ext->next_combo++;
2355 filename = find_encoding_file (this, cp->font->font->encoding);
2358 encoding = get_encoding (this, filename);
2363 msg (IE, _("PostScript driver: Cannot find encoding `%s' for "
2364 "PostScript font `%s'."), cp->font->font->encoding,
2365 cp->font->font->internal_name);
2366 encoding = default_encoding (this);
2369 if (cp->font != ext->fixed && cp->font != ext->prop)
2371 bp = stpcpy (buf, "%%IncludeResource: font ");
2372 bp = quote_ps_string (bp, cp->font->font->internal_name);
2373 bp = stpcpy (bp, ext->eol);
2378 bp = spprintf (bp, "/F%x E%x %d", (*fc)->index, encoding->index,
2380 bp = quote_ps_name (bp, cp->font->font->internal_name);
2381 sprintf (bp, " SF%s", ext->eol);
2382 fputs (buf, ext->file.file);
2384 ext->last_font = *fc;
2387 /* (write_text) Writes the accumulated line buffer to the output
2389 #define output_line() \
2392 lp = stpcpy (lp, ext->eol); \
2394 fputs (line, ext->file.file); \
2399 /* (write_text) Adds the string representing number X to the line
2400 buffer, flushing the buffer to disk beforehand if necessary. */
2401 #define put_number(X) \
2404 int n = nsprintf (number, "%d", X); \
2405 if (n + lp > &line[75]) \
2407 lp = stpcpy (lp, number); \
2411 /* Outputs PostScript code to THIS driver's output file to display the
2412 characters represented by the output_char's between CP and END,
2413 using the associated outp_text T to determine formatting. WIDTH is
2414 the width of the output region; WIDTH_LEFT is the amount of the
2415 WIDTH that is not taken up by text (so it can be used to determine
2418 write_text (struct outp_driver *this,
2419 const struct output_char *cp, const struct output_char *end,
2420 struct outp_text *t, int width unused, int width_left)
2422 struct ps_driver_ext *ext = this->ext;
2427 char number[INT_DIGITS + 1];
2431 switch (t->options & OUTP_T_JUST_MASK)
2433 case OUTP_T_JUST_LEFT:
2436 case OUTP_T_JUST_RIGHT:
2439 case OUTP_T_JUST_CENTER:
2440 ofs = width_left / 2;
2448 int x = cp->x + ofs;
2449 int y = cp->y + (cp->font->font->ascent * cp->size / 1000);
2451 if (ext->last_font == NULL
2452 || cp->font != ext->last_font->font
2453 || cp->size != ext->last_font->size)
2454 switch_font (this, cp);
2460 static unsigned char literal_chars[ODA_COUNT][32] =
2462 {0x00, 0x00, 0x00, 0xf8, 0xff, 0xfc, 0xff, 0xff,
2463 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f,
2464 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2465 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2467 {0x00, 0x00, 0x00, 0xf8, 0xff, 0xfc, 0xff, 0xff,
2468 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
2469 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
2470 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
2472 {0x7e, 0xd6, 0xff, 0xfb, 0xff, 0xfc, 0xff, 0xff,
2473 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
2474 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
2475 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
2479 if (TEST_BIT (literal_chars[ext->data], cp->ch))
2485 lp = stpcpy (lp, "\\(");
2488 lp = stpcpy (lp, "\\)");
2491 lp = spprintf (lp, "\\%03o", cp->ch);
2496 while (cp < end && lp < &line[70] && cp->separate == 0);
2504 put_number (YT (y));
2515 if (lp >= &line[70])
2525 /* Displays the text in outp_text T, if DRAW is nonzero; or, merely
2526 determine the text metrics, if DRAW is zero. */
2528 text (struct outp_driver *this, struct outp_text *t, int draw)
2530 struct ps_driver_ext *ext = this->ext;
2533 struct output_char *buf; /* Output buffer. */
2534 struct output_char *buf_end; /* End of output buffer. */
2535 struct output_char *buf_loc; /* Current location in output buffer. */
2538 struct font_entry *old_current = ext->current;
2539 char *old_family = xstrdup (ext->family); /* FIXME */
2540 int old_size = ext->size;
2545 /* Current location. */
2548 /* Keeping track of what's left over. */
2549 int width; /* Width available for characters. */
2550 int width_left, height_left; /* Width, height left over. */
2551 int max_height; /* Tallest character on this line so far. */
2553 /* Previous character. */
2556 /* Information about location of previous space. */
2557 char *space_char; /* Character after space. */
2558 struct output_char *space_buf_loc; /* Buffer location after space. */
2559 int space_width_left; /* Width of characters before space. */
2561 /* Name of the current character. */
2562 const char *char_name;
2563 char local_char_name[2] = {0, 0};
2565 local_char_name[0] = local_char_name[1] = 0;
2567 buf = local_alloc (sizeof *buf * 128);
2568 buf_end = &buf[128];
2571 assert (!ls_null_p (&t->s));
2572 cp = ls_value (&t->s);
2573 end = ls_end (&t->s);
2579 width = width_left = (t->options & OUTP_T_HORZ) ? t->h : INT_MAX;
2580 height_left = (t->options & OUTP_T_VERT) ? t->v : INT_MAX;
2585 if (!width || !height_left)
2590 struct char_metrics *metric;
2596 /* Set char_name to the name of the character or ligature at
2598 if (ext->current->font->ligatures && *cp == 'f')
2606 lig = LIG_fi, char_name = "fi";
2609 lig = LIG_fl, char_name = "fl";
2616 lig = LIG_ffi, char_name = "ffi";
2619 lig = LIG_ffl, char_name = "ffl";
2622 lig = LIG_ff, char_name = "ff";
2626 if ((lig & ext->current->font->ligatures) == 0)
2628 local_char_name[0] = *cp++; /* 'f' */
2629 char_name = local_char_name;
2632 cp += strlen (char_name);
2634 else if (*cp == '\n')
2638 write_text (this, buf, buf_loc, t, width, width_left);
2645 height_left -= max_height;
2651 /* FIXME: when we're page buffering it will be necessary to
2652 set separate to 1. */
2657 local_char_name[0] = *cp++;
2658 char_name = local_char_name;
2661 /* Figure out what size this character is, and what kern
2662 adjustment we need. */
2663 cur_char = font_char_name_to_index (char_name);
2664 metric = font_get_char_metrics (ext->current->font, cur_char);
2667 static struct char_metrics m;
2669 m.width = ext->current->font->space_width;
2670 m.code = *char_name;
2672 kern_amt = font_get_kern_adjust (ext->current->font, prev_char,
2676 kern_amt = (kern_amt * ext->size / 1000);
2679 char_width = metric->width * ext->size / 1000;
2681 /* Record the current status if this is a space character. */
2682 if (cur_char == space_index && buf_loc > buf)
2685 space_buf_loc = buf_loc;
2686 space_width_left = width_left;
2689 /* Drop down to a new line if there's no room left on this
2691 if (char_width + kern_amt > width_left)
2693 /* Regress to previous space, if any. */
2697 width_left = space_width_left;
2698 buf_loc = space_buf_loc;
2703 write_text (this, buf, buf_loc, t, width, width_left);
2710 height_left -= max_height;
2718 /* FIXME: when we're page buffering it will be
2719 necessary to set separate to 1. */
2724 if (ext->size > max_height)
2725 max_height = ext->size;
2726 if (max_height > height_left)
2729 /* Actually draw the character. */
2732 if (buf_loc >= buf_end)
2734 int buf_len = buf_end - buf;
2738 struct output_char *new_buf;
2740 new_buf = xmalloc (sizeof *new_buf * 256);
2741 memcpy (new_buf, buf, sizeof *new_buf * 128);
2742 buf_loc = new_buf + 128;
2743 buf_end = new_buf + 256;
2749 buf = xrealloc (buf, sizeof *buf * buf_len * 2);
2750 buf_loc = buf + buf_len;
2751 buf_end = buf + buf_len * 2;
2756 buf_loc->font = ext->current;
2757 buf_loc->size = ext->size;
2760 buf_loc->ch = metric->code;
2761 buf_loc->separate = separate;
2766 /* Prepare for next iteration. */
2767 width_left -= char_width + kern_amt;
2768 prev_char = cur_char;
2770 height_left -= max_height;
2771 if (buf_loc > buf && draw)
2772 write_text (this, buf, buf_loc, t, width, width_left);
2775 if (!(t->options & OUTP_T_HORZ))
2776 t->h = INT_MAX - width_left;
2777 if (!(t->options & OUTP_T_VERT))
2778 t->v = INT_MAX - height_left;
2780 t->v -= height_left;
2781 if (buf_end - buf == 128)
2785 ext->current = old_current;
2787 ext->family = old_family;
2788 ext->size = old_size;
2792 ps_text_metrics (struct outp_driver *this, struct outp_text *t)
2794 assert (this->driver_open && this->page_open);
2799 ps_text_draw (struct outp_driver *this, struct outp_text *t)
2801 assert (this->driver_open && this->page_open);
2807 /* Translate a filename to a font. */
2808 struct filename2font
2810 char *filename; /* Normalized filename. */
2811 struct font_desc *font;
2814 /* Table of `filename2font's. */
2815 static struct hsh_table *ps_fonts;
2817 /* Hash table comparison function for filename2font structs. */
2819 compare_filename2font (const void *a, const void *b, void *param unused)
2821 return strcmp (((struct filename2font *) a)->filename,
2822 ((struct filename2font *) b)->filename);
2825 /* Hash table hash function for filename2font structs. */
2827 hash_filename2font (const void *f2f_, void *param unused)
2829 const struct filename2font *f2f = f2f_;
2830 return hsh_hash_string (f2f->filename);
2833 /* Initializes the global font list by creating the hash table for
2834 translation of filenames to font_desc structs. */
2838 ps_fonts = hsh_create (31, compare_filename2font, hash_filename2font,
2842 /* Loads the font having Groff name DIT into THIS driver instance.
2843 Specifically, adds it into the THIS driver's `loaded' hash
2845 static struct font_entry *
2846 load_font (struct outp_driver *this, const char *dit)
2848 struct ps_driver_ext *x = this->ext;
2849 char *filename1, *filename2;
2851 struct font_entry *fe;
2853 filename1 = find_ps_file (this, dit);
2855 filename1 = xstrdup (dit);
2856 filename2 = fn_normalize (filename1);
2859 entry = hsh_probe (ps_fonts, &filename2);
2862 struct filename2font *f2f;
2863 struct font_desc *f = groff_read_font (filename2);
2870 f = default_font ();
2873 f2f = xmalloc (sizeof *f2f);
2874 f2f->filename = filename2;
2881 fe = xmalloc (sizeof *fe);
2882 fe->dit = xstrdup (dit);
2883 fe->font = ((struct filename2font *) * entry)->font;
2884 *hsh_probe (x->loaded, &dit) = fe;
2889 /* PostScript driver class. */
2890 struct outp_class postscript_class =
2912 ps_line_intersection,
2919 ps_text_set_font_by_name,
2920 ps_text_set_font_by_position,
2921 ps_text_set_font_family,
2922 ps_text_get_font_name,
2923 ps_text_get_font_family,
2930 /* EPSF driver class. FIXME: Probably doesn't work right. */
2931 struct outp_class epsf_class =
2953 ps_line_intersection,
2960 ps_text_set_font_by_name,
2961 ps_text_set_font_by_position,
2962 ps_text_set_font_family,
2963 ps_text_get_font_name,
2964 ps_text_get_font_family,
2971 #endif /* NO_POSTSCRIPT */