1 /* PSPP - computes sample statistics.
2 Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
3 Written by Ben Pfaff <blp@gnu.org>.
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 This program is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 /*this #if encloses the remainder of the file. */
38 #include "bit-vector.h"
47 #include "start-date.h"
51 #define _(msgid) gettext (msgid)
55 optimize-text-size not implemented.
57 Line buffering is the only possibility; page buffering should also
62 Should add a field to give a file that has a list of fonts
65 Should add an option that tells the driver it can emit %%Include:'s.
67 Should have auto-encode=true stream-edit or whatever to allow
68 addition to list of encodings.
70 Should align fonts of different sizes along their baselines (see
73 /* PostScript driver options: (defaults listed first)
77 data=clean7bit|clean8bit|binary
80 paper-size=letter (see "papersize" file)
81 orientation=portrait|landscape
90 prologue-file=ps-prologue
92 encoding-file=ps-encodings
93 auto-encode=true|false
99 line-style=thick|double
105 optimize-text-size=1|0|2
106 optimize-line-size=1|0
107 max-fonts-simult=0 Max # of fonts in printer memory at once (0=infinite)
110 /* The number of `psus' (PostScript driver UnitS) per inch. Although
111 this is a #define, the value is expected never to change. If it
112 does, review all uses. */
115 /* Magic numbers for PostScript and EPSF drivers. */
125 OTN_PORTRAIT, /* Portrait. */
126 OTN_LANDSCAPE /* Landscape. */
129 /* Output options. */
132 OPO_MIRROR_HORZ = 001, /* 1=Mirror across a horizontal axis. */
133 OPO_MIRROR_VERT = 002, /* 1=Mirror across a vertical axis. */
134 OPO_ROTATE_180 = 004, /* 1=Rotate the page 180 degrees. */
135 OPO_COLOR = 010, /* 1=Enable color. */
136 OPO_HEADERS = 020, /* 1=Draw headers at top of page. */
137 OPO_AUTO_ENCODE = 040, /* 1=Add encodings semi-intelligently. */
138 OPO_DOUBLE_LINE = 0100 /* 1=Double lines instead of thick lines. */
141 /* Data allowed in output. */
144 ODA_CLEAN7BIT, /* 0x09, 0x0a, 0x0d, 0x1b...0x7e */
145 ODA_CLEAN8BIT, /* 0x09, 0x0a, 0x0d, 0x1b...0xff */
146 ODA_BINARY, /* 0x00...0xff */
150 /* Types of lines for purpose of caching. */
153 horz, /* Single horizontal. */
154 dbl_horz, /* Double horizontal. */
155 spl_horz, /* Special horizontal. */
156 vert, /* Single vertical. */
157 dbl_vert, /* Double vertical. */
158 spl_vert, /* Special vertical. */
165 int ind; /* Independent var. Don't reorder. */
166 int mdep; /* Maximum number of dependent var pairs. */
167 int ndep; /* Current number of dependent var pairs. */
168 int dep[1][2]; /* Dependent var pairs. */
171 /* Contents of ps_driver_ext.loaded. */
174 char *dit; /* Font Groff name. */
175 struct font_desc *font; /* Font descriptor. */
178 /* Combines a font with a font size for benefit of generated code. */
181 struct font_entry *font; /* Font. */
182 int size; /* Font size. */
183 int index; /* PostScript index. */
186 /* A font encoding. */
189 char *filename; /* Normalized filename of this encoding. */
190 int index; /* Index value. */
193 /* PostScript output driver extension record. */
196 /* User parameters. */
197 int orientation; /* OTN_PORTRAIT or OTN_LANDSCAPE. */
198 int output_options; /* OPO_*. */
199 int data; /* ODA_*. */
201 int left_margin; /* Left margin in psus. */
202 int right_margin; /* Right margin in psus. */
203 int top_margin; /* Top margin in psus. */
204 int bottom_margin; /* Bottom margin in psus. */
206 char eol[3]; /* End of line--CR, LF, or CRLF. */
208 char *font_dir; /* Font directory relative to font path. */
209 char *prologue_fn; /* Prologue's filename relative to font dir. */
210 char *desc_fn; /* DESC filename relative to font dir. */
211 char *encoding_fn; /* Encoding's filename relative to font dir. */
213 char *prop_family; /* Default proportional font family. */
214 char *fixed_family; /* Default fixed-pitch font family. */
215 int font_size; /* Default font size (psus). */
217 int line_gutter; /* Space around lines. */
218 int line_space; /* Space between lines. */
219 int line_width; /* Width of lines. */
220 int line_width_thick; /* Width of thick lines. */
222 int text_opt; /* Text optimization level. */
223 int line_opt; /* Line optimization level. */
224 int max_fonts; /* Max # of simultaneous fonts (0=infinite). */
226 /* Internal state. */
227 struct file_ext file; /* Output file. */
228 int page_number; /* Current page number. */
229 int file_page_number; /* Page number in this file. */
230 int w, l; /* Paper size. */
231 struct hsh_table *lines[n_line_types]; /* Line buffers. */
233 struct font_entry *prop; /* Default Roman proportional font. */
234 struct font_entry *fixed; /* Default Roman fixed-pitch font. */
235 struct hsh_table *loaded; /* Fonts in memory. */
237 struct hsh_table *combos; /* Combinations of fonts with font sizes. */
238 struct ps_font_combo *last_font; /* PostScript selected font. */
239 int next_combo; /* Next font combo position index. */
241 struct hsh_table *encodings;/* Set of encodings. */
242 int next_encoding; /* Next font encoding index. */
244 /* Currently selected font. */
245 struct font_entry *current; /* Current font. */
246 char *family; /* Font family. */
247 int size; /* Size in psus. */
251 /* Transform logical y-ordinate Y into a page ordinate. */
252 #define YT(Y) (this->length - (Y))
255 static int postopen (struct file_ext *);
256 static int preclose (struct file_ext *);
257 static void draw_headers (struct outp_driver *this);
259 static int compare_font_entry (const void *, const void *, void *param);
260 static unsigned hash_font_entry (const void *, void *param);
261 static void free_font_entry (void *, void *foo);
262 static struct font_entry *load_font (struct outp_driver *, const char *dit);
263 static void init_fonts (void);
264 static void done_fonts (void);
266 static void dump_lines (struct outp_driver *this);
268 static void read_ps_encodings (struct outp_driver *this);
269 static int compare_ps_encoding (const void *pa, const void *pb, void *foo);
270 static unsigned hash_ps_encoding (const void *pa, void *foo);
271 static void free_ps_encoding (void *a, void *foo);
272 static void add_encoding (struct outp_driver *this, char *filename);
273 static struct ps_encoding *default_encoding (struct outp_driver *this);
275 static int compare_ps_combo (const void *pa, const void *pb, void *foo);
276 static unsigned hash_ps_combo (const void *pa, void *foo);
277 static void free_ps_combo (void *a, void *foo);
279 static char *quote_ps_name (char *dest, const char *string);
280 static char *quote_ps_string (char *dest, const char *string);
282 /* Driver initialization. */
285 ps_open_global (struct outp_class *this UNUSED)
293 ps_close_global (struct outp_class *this UNUSED)
301 ps_font_sizes (struct outp_class *this UNUSED, int *n_valid_sizes)
303 /* Allow fonts up to 1" in height. */
304 static int valid_sizes[] =
307 assert (n_valid_sizes != NULL);
313 ps_preopen_driver (struct outp_driver *this)
315 struct ps_driver_ext *x;
319 assert (this->driver_open == 0);
320 msg (VM (1), _("PostScript driver initializing as `%s'..."), this->name);
322 this->ext = x = xmalloc (sizeof *x);
324 this->horiz = this->vert = 1;
325 this->width = this->length = 0;
327 x->orientation = OTN_PORTRAIT;
328 x->output_options = OPO_COLOR | OPO_HEADERS | OPO_AUTO_ENCODE;
329 x->data = ODA_CLEAN7BIT;
331 x->left_margin = x->right_margin =
332 x->top_margin = x->bottom_margin = PSUS / 2;
334 strcpy (x->eol, "\n");
337 x->prologue_fn = NULL;
339 x->encoding_fn = NULL;
341 x->prop_family = NULL;
342 x->fixed_family = NULL;
343 x->font_size = PSUS * 10 / 72;
345 x->line_gutter = PSUS / 144;
346 x->line_space = PSUS / 144;
347 x->line_width = PSUS / 144;
348 x->line_width_thick = PSUS / 48;
354 x->file.filename = NULL;
357 x->file.sequence_no = &x->page_number;
358 x->file.param = this;
359 x->file.postopen = postopen;
360 x->file.preclose = preclose;
364 x->file_page_number = 0;
365 for (i = 0; i < n_line_types; i++)
376 x->encodings = hsh_create (31, compare_ps_encoding, hash_ps_encoding,
377 free_ps_encoding, NULL);
378 x->next_encoding = 0;
388 ps_postopen_driver (struct outp_driver *this)
390 struct ps_driver_ext *x = this->ext;
392 assert (this->driver_open == 0);
394 if (this->width == 0)
396 this->width = PSUS * 17 / 2; /* Defaults to 8.5"x11". */
397 this->length = PSUS * 11;
400 if (x->text_opt == -1)
401 x->text_opt = (this->device & OUTP_DEV_SCREEN) ? 0 : 1;
402 if (x->line_opt == -1)
403 x->line_opt = (this->device & OUTP_DEV_SCREEN) ? 0 : 1;
407 if (x->orientation == OTN_LANDSCAPE)
409 int temp = this->width;
410 this->width = this->length;
413 this->width -= x->left_margin + x->right_margin;
414 this->length -= x->top_margin + x->bottom_margin;
415 if (x->output_options & OPO_HEADERS)
417 this->length -= 3 * x->font_size;
418 x->top_margin += 3 * x->font_size;
420 if (NULL == x->file.filename)
421 x->file.filename = xstrdup ("pspp.ps");
423 if (x->font_dir == NULL)
424 x->font_dir = xstrdup ("devps");
425 if (x->prologue_fn == NULL)
426 x->prologue_fn = xstrdup ("ps-prologue");
427 if (x->desc_fn == NULL)
428 x->desc_fn = xstrdup ("DESC");
429 if (x->encoding_fn == NULL)
430 x->encoding_fn = xstrdup ("ps-encodings");
432 if (x->prop_family == NULL)
433 x->prop_family = xstrdup ("H");
434 if (x->fixed_family == NULL)
435 x->fixed_family = xstrdup ("C");
437 read_ps_encodings (this);
442 if (this->length / x->font_size < 15)
444 msg (SE, _("PostScript driver: The defined page is not long "
445 "enough to hold margins and headers, plus least 15 "
446 "lines of the default fonts. In fact, there's only "
447 "room for %d lines of each font at the default size "
448 "of %d.%03d points."),
449 this->length / x->font_size,
450 x->font_size / 1000, x->font_size % 1000);
454 this->driver_open = 1;
455 msg (VM (2), _("%s: Initialization complete."), this->name);
461 ps_close_driver (struct outp_driver *this)
463 struct ps_driver_ext *x = this->ext;
467 assert (this->driver_open == 1);
468 msg (VM (2), _("%s: Beginning closing..."), this->name);
470 fn_close_ext (&x->file);
471 free (x->file.filename);
473 free (x->prologue_fn);
475 free (x->encoding_fn);
476 free (x->prop_family);
477 free (x->fixed_family);
479 for (i = 0; i < n_line_types; i++)
480 hsh_destroy (x->lines[i]);
481 hsh_destroy (x->encodings);
482 hsh_destroy (x->combos);
483 hsh_destroy (x->loaded);
486 this->driver_open = 0;
487 msg (VM (3), _("%s: Finished closing."), this->name);
492 /* font_entry comparison function for hash tables. */
494 compare_font_entry (const void *a, const void *b, void *foobar UNUSED)
496 return strcmp (((struct font_entry *) a)->dit, ((struct font_entry *) b)->dit);
499 /* font_entry hash function for hash tables. */
501 hash_font_entry (const void *fe_, void *foobar UNUSED)
503 const struct font_entry *fe = fe_;
504 return hsh_hash_string (fe->dit);
507 /* font_entry destructor function for hash tables. */
509 free_font_entry (void *pa, void *foo UNUSED)
511 struct font_entry *a = pa;
516 /* Generic option types. */
526 /* All the options that the PostScript driver supports. */
527 static struct outp_option option_tab[] =
530 {"output-file", 1, 0},
531 {"paper-size", 2, 0},
532 {"orientation", 3, 0},
533 {"color", boolean_arg, 0},
535 {"auto-encode", boolean_arg, 5},
536 {"headers", boolean_arg, 1},
537 {"left-margin", pos_int_arg, 0},
538 {"right-margin", pos_int_arg, 1},
539 {"top-margin", pos_int_arg, 2},
540 {"bottom-margin", pos_int_arg, 3},
541 {"font-dir", string_arg, 0},
542 {"prologue-file", string_arg, 1},
543 {"device-file", string_arg, 2},
544 {"encoding-file", string_arg, 3},
545 {"prop-font-family", string_arg, 5},
546 {"fixed-font-family", string_arg, 6},
547 {"font-size", pos_int_arg, 4},
548 {"optimize-text-size", nonneg_int_arg, 0},
549 {"optimize-line-size", nonneg_int_arg, 1},
550 {"max-fonts-simult", nonneg_int_arg, 2},
552 {"line-style", 7, 0},
553 {"line-width", dimension_arg, 2},
554 {"line-gutter", dimension_arg, 3},
555 {"line-width", dimension_arg, 4},
556 {"line-width-thick", dimension_arg, 5},
560 static struct outp_option_info option_info;
563 ps_option (struct outp_driver *this, const char *key, const struct string *val)
565 struct ps_driver_ext *x = this->ext;
567 char *value = ds_c_str (val);
569 cat = outp_match_keyword (key, option_tab, &option_info, &subcat);
574 msg (SE, _("Unknown configuration parameter `%s' for PostScript device "
578 free (x->file.filename);
579 x->file.filename = xstrdup (value);
582 outp_get_paper_size (value, &this->width, &this->length);
585 if (!strcmp (value, "portrait"))
586 x->orientation = OTN_PORTRAIT;
587 else if (!strcmp (value, "landscape"))
588 x->orientation = OTN_LANDSCAPE;
590 msg (SE, _("Unknown orientation `%s'. Valid orientations are "
591 "`portrait' and `landscape'."), value);
594 if (!strcmp (value, "clean7bit") || !strcmp (value, "Clean7Bit"))
595 x->data = ODA_CLEAN7BIT;
596 else if (!strcmp (value, "clean8bit")
597 || !strcmp (value, "Clean8Bit"))
598 x->data = ODA_CLEAN8BIT;
599 else if (!strcmp (value, "binary") || !strcmp (value, "Binary"))
600 x->data = ODA_BINARY;
602 msg (SE, _("Unknown value for `data'. Valid values are `clean7bit', "
603 "`clean8bit', and `binary'."));
606 if (!strcmp (value, "lf"))
607 strcpy (x->eol, "\n");
608 else if (!strcmp (value, "crlf"))
609 strcpy (x->eol, "\r\n");
611 msg (SE, _("Unknown value for `line-ends'. Valid values are `lf' and "
615 if (!strcmp (value, "thick"))
616 x->output_options &= ~OPO_DOUBLE_LINE;
617 else if (!strcmp (value, "double"))
618 x->output_options |= OPO_DOUBLE_LINE;
620 msg (SE, _("Unknown value for `line-style'. Valid values are `thick' "
628 if (!strcmp (value, "on") || !strcmp (value, "true")
629 || !strcmp (value, "yes") || atoi (value))
631 else if (!strcmp (value, "off") || !strcmp (value, "false")
632 || !strcmp (value, "no") || !strcmp (value, "0"))
636 msg (SE, _("Boolean value expected for %s."), key);
648 mask = OPO_MIRROR_HORZ;
651 mask = OPO_MIRROR_VERT;
654 mask = OPO_ROTATE_180;
657 mask = OPO_AUTO_ENCODE;
664 x->output_options |= mask;
666 x->output_options &= ~mask;
675 arg = strtol (value, &tail, 0);
676 if (arg < 1 || errno == ERANGE || *tail)
678 msg (SE, _("Positive integer required as value for `%s'."), key);
681 if ((subcat == 4 || subcat == 5) && arg < 1000)
683 msg (SE, _("Default font size must be at least 1 point (value "
684 "of 1000 for key `%s')."), key);
690 x->left_margin = arg;
693 x->right_margin = arg;
699 x->bottom_margin = arg;
711 int dimension = outp_evaluate_dimension (value, NULL);
715 msg (SE, _("Value for `%s' must be a dimension of positive "
716 "length (i.e., `1in')."), key);
722 x->line_width = dimension;
725 x->line_gutter = dimension;
728 x->line_width = dimension;
731 x->line_width_thick = dimension;
747 dest = &x->prologue_fn;
753 dest = &x->encoding_fn;
756 dest = &x->prop_family;
759 dest = &x->fixed_family;
767 *dest = xstrdup (value);
776 arg = strtol (value, &tail, 0);
777 if (arg < 0 || errno == ERANGE || *tail)
779 msg (SE, _("Nonnegative integer required as value for `%s'."), key);
803 /* Looks for a PostScript font file or config file in all the
804 appropriate places. Returns the filename on success, NULL on
806 /* PORTME: Filename operations. */
808 find_ps_file (struct outp_driver *this, const char *name)
810 struct ps_driver_ext *x = this->ext;
813 /* x->font_dir + name: "devps/ps-encodings". */
816 /* Usually equal to groff_font_path. */
819 /* Final filename. */
823 basename = local_alloc (strlen (x->font_dir) + 1 + strlen (name) + 1);
824 cp = stpcpy (basename, x->font_dir);
825 *cp++ = DIR_SEPARATOR;
828 /* Decide on search path. */
830 const char *pre_pathname;
832 pre_pathname = getenv ("STAT_GROFF_FONT_PATH");
833 if (pre_pathname == NULL)
834 pre_pathname = getenv ("GROFF_FONT_PATH");
835 if (pre_pathname == NULL)
836 pre_pathname = groff_font_path;
837 pathname = fn_tilde_expand (pre_pathname);
840 /* Search all possible places for the file. */
841 fn = fn_search_path (basename, pathname, NULL);
843 fn = fn_search_path (basename, config_path, NULL);
845 fn = fn_search_path (name, pathname, NULL);
847 fn = fn_search_path (name, config_path, NULL);
849 local_free (basename);
856 /* Hash table comparison function for ps_encoding's. */
858 compare_ps_encoding (const void *pa, const void *pb, void *foo UNUSED)
860 const struct ps_encoding *a = pa;
861 const struct ps_encoding *b = pb;
863 return strcmp (a->filename, b->filename);
866 /* Hash table hash function for ps_encoding's. */
868 hash_ps_encoding (const void *pa, void *foo UNUSED)
870 const struct ps_encoding *a = pa;
872 return hsh_hash_string (a->filename);
875 /* Hash table free function for ps_encoding's. */
877 free_ps_encoding (void *pa, void *foo UNUSED)
879 struct ps_encoding *a = pa;
885 /* Iterates through the list of encodings used for this driver
886 instance, reads each of them from disk, and writes them as
887 PostScript code to the output file. */
889 output_encodings (struct outp_driver *this)
891 struct ps_driver_ext *x = this->ext;
893 struct hsh_iterator iter;
894 struct ps_encoding *pe;
896 struct string line, buf;
898 ds_init (&line, 128);
900 for (pe = hsh_first (x->encodings, &iter); pe != NULL;
901 pe = hsh_next (x->encodings, &iter))
905 msg (VM (1), _("%s: %s: Opening PostScript font encoding..."),
906 this->name, pe->filename);
908 f = fopen (pe->filename, "r");
911 msg (IE, _("PostScript driver: Cannot open encoding file `%s': %s. "
912 "Substituting ISOLatin1Encoding for missing encoding."),
913 pe->filename, strerror (errno));
914 fprintf (x->file.file, "/E%x ISOLatin1Encoding def%s",
919 struct file_locator where;
921 const char *tab[256];
928 const char *notdef = ".notdef";
932 for (i = 0; i < 256; i++)
935 where.filename = pe->filename;
936 where.line_number = 0;
937 err_push_file_locator (&where);
939 while (ds_get_config_line (f, &buf, &where))
946 pschar = strtok_r (ds_c_str (&buf), " \t\r\n", &sp);
947 code = strtok_r (NULL, " \t\r\n", &sp);
948 if (*pschar == 0 || *code == 0)
950 code_val = strtol (code, &fubar, 0);
953 msg (IS, _("PostScript driver: Invalid numeric format."));
956 if (code_val < 0 || code_val > 255)
958 msg (IS, _("PostScript driver: Codes must be between 0 "
959 "and 255. (%d is not allowed.)"), code_val);
962 tab[code_val] = local_alloc (strlen (pschar) + 1);
963 strcpy ((char *) (tab[code_val]), pschar);
965 err_pop_file_locator (&where);
968 ds_printf (&line, "/E%x[", pe->index);
969 for (i = 0; i < 257; i++)
975 quote_ps_name (temp, tab[i]);
976 if (tab[i] != notdef)
980 strcpy (temp, "]def");
982 if (ds_length (&line) + strlen (temp) > 70)
984 ds_puts (&line, x->eol);
985 fputs (ds_c_str (&line), x->file.file);
988 ds_puts (&line, temp);
990 ds_puts (&line, x->eol);
991 fputs (ds_c_str (&line), x->file.file);
993 if (fclose (f) == EOF)
994 msg (MW, _("PostScript driver: Error closing encoding file `%s'."),
997 msg (VM (2), _("%s: PostScript font encoding read successfully."),
1005 /* Finds the ps_encoding in THIS that corresponds to the file with
1006 name NORM_FILENAME, which must have previously been normalized with
1007 normalize_filename(). */
1008 static struct ps_encoding *
1009 get_encoding (struct outp_driver *this, const char *norm_filename)
1011 struct ps_driver_ext *x = this->ext;
1012 struct ps_encoding *pe;
1014 pe = (struct ps_encoding *) hsh_find (x->encodings, (void *) &norm_filename);
1018 /* Searches the filesystem for an encoding file with name FILENAME;
1019 returns its malloc'd, normalized name if found, otherwise NULL. */
1021 find_encoding_file (struct outp_driver *this, char *filename)
1025 if (filename == NULL)
1027 while (isspace ((unsigned char) *filename))
1029 for (cp = filename; *cp && !isspace ((unsigned char) *cp); cp++)
1035 temp = find_ps_file (this, filename);
1039 filename = fn_normalize (temp);
1040 assert (filename != NULL);
1046 /* Adds the encoding represented by the not-necessarily-normalized
1047 file FILENAME to the list of encodings, if it exists and is not
1048 already in the list. */
1050 add_encoding (struct outp_driver *this, char *filename)
1052 struct ps_driver_ext *x = this->ext;
1053 struct ps_encoding **pe;
1055 filename = find_encoding_file (this, filename);
1059 pe = (struct ps_encoding **) hsh_probe (x->encodings, &filename);
1065 *pe = xmalloc (sizeof **pe);
1066 (*pe)->filename = filename;
1067 (*pe)->index = x->next_encoding++;
1070 /* Finds the file on disk that contains the list of encodings to
1071 include in the output file, then adds those encodings to the list
1074 read_ps_encodings (struct outp_driver *this)
1076 struct ps_driver_ext *x = this->ext;
1078 /* Encodings file. */
1079 char *encoding_fn; /* `ps-encodings' filename. */
1083 struct file_locator where;
1085 /* It's okay if there's no list of encodings; not everyone cares. */
1086 encoding_fn = find_ps_file (this, x->encoding_fn);
1087 if (encoding_fn == NULL)
1091 msg (VM (1), _("%s: %s: Opening PostScript encoding list file."),
1092 this->name, encoding_fn);
1093 f = fopen (encoding_fn, "r");
1096 msg (IE, _("Opening %s: %s."), encoding_fn, strerror (errno));
1100 where.filename = encoding_fn;
1101 where.line_number = 0;
1102 err_push_file_locator (&where);
1104 ds_init (&line, 128);
1108 if (!ds_get_config_line (f, &line, &where))
1111 msg (ME, _("Reading %s: %s."), encoding_fn, strerror (errno));
1115 add_encoding (this, line.string);
1119 err_pop_file_locator (&where);
1121 if (-1 == fclose (f))
1122 msg (MW, _("Closing %s: %s."), encoding_fn, strerror (errno));
1124 msg (VM (2), _("%s: PostScript encoding list file read successfully."), this->name);
1127 /* Creates a default encoding for driver D that can be substituted for
1128 an unavailable encoding. */
1129 struct ps_encoding *
1130 default_encoding (struct outp_driver *d)
1132 struct ps_driver_ext *x = d->ext;
1133 static struct ps_encoding *enc;
1137 enc = xmalloc (sizeof *enc);
1138 enc->filename = xstrdup (_("<<default encoding>>"));
1139 enc->index = x->next_encoding++;
1144 /* Basic file operations. */
1146 /* Variables for the prologue. */
1153 static struct ps_variable *ps_var_tab;
1155 /* Searches ps_var_tab for a ps_variable with key KEY, and returns the
1156 associated value. */
1158 ps_get_var (const char *key)
1160 struct ps_variable *v;
1162 for (v = ps_var_tab; v->key; v++)
1163 if (!strcmp (key, v->key))
1168 /* Writes the PostScript prologue to file F. */
1170 postopen (struct file_ext *f)
1172 static struct ps_variable dict[] =
1174 {"bounding-box", 0},
1183 {"scale-factor", 0},
1185 {"paper-length", 0},
1189 {"line-width-thick", 0},
1193 char boundbox[INT_DIGITS * 4 + 4];
1197 char scaling[INT_DIGITS + 5];
1201 char paper_width[INT_DIGITS + 1];
1202 char paper_length[INT_DIGITS + 1];
1203 char left_margin[INT_DIGITS + 1];
1204 char top_margin[INT_DIGITS + 1];
1205 char line_width[INT_DIGITS + 1];
1206 char line_width_thick[INT_DIGITS + 1];
1208 struct outp_driver *this = f->param;
1209 struct ps_driver_ext *x = this->ext;
1211 char *prologue_fn = find_ps_file (this, x->prologue_fn);
1212 FILE *prologue_file;
1215 size_t buf_size = 0;
1217 x->loaded = hsh_create (31, compare_font_entry, hash_font_entry,
1218 free_font_entry, NULL);
1221 char *font_name = local_alloc (2 + max (strlen (x->prop_family),
1222 strlen (x->fixed_family)));
1224 strcpy (stpcpy (font_name, x->prop_family), "R");
1225 x->prop = load_font (this, font_name);
1227 strcpy (stpcpy (font_name, x->fixed_family), "R");
1228 x->fixed = load_font (this, font_name);
1230 local_free(font_name);
1233 x->current = x->prop;
1234 x->family = xstrdup (x->prop_family);
1235 x->size = x->font_size;
1238 int *h = this->horiz_line_width, *v = this->vert_line_width;
1240 this->cp_x = this->cp_y = 0;
1241 this->font_height = x->font_size;
1243 struct char_metrics *metric;
1245 metric = font_get_char_metrics (x->prop->font, '0');
1246 this->prop_em_width = ((metric
1247 ? metric->width : x->prop->font->space_width)
1248 * x->font_size / 1000);
1250 metric = font_get_char_metrics (x->fixed->font, '0');
1251 this->fixed_width = ((metric
1252 ? metric->width : x->fixed->font->space_width)
1253 * x->font_size / 1000);
1257 h[1] = v[1] = 2 * x->line_gutter + x->line_width;
1258 if (x->output_options & OPO_DOUBLE_LINE)
1259 h[2] = v[2] = 2 * x->line_gutter + 2 * x->line_width + x->line_space;
1261 h[2] = v[2] = 2 * x->line_gutter + x->line_width_thick;
1262 h[3] = v[3] = 2 * x->line_gutter + x->line_width;
1267 for (i = 0; i < (1 << OUTP_L_COUNT); i++)
1271 /* Maximum width of any line type so far. */
1274 for (bit = 0; bit < OUTP_L_COUNT; bit++)
1275 if ((i & (1 << bit)) && h[bit] > max)
1277 this->horiz_line_spacing[i] = this->vert_line_spacing[i] = max;
1282 if (x->output_options & OPO_AUTO_ENCODE)
1284 /* It's okay if this is done more than once since add_encoding()
1285 is idempotent over identical encodings. */
1286 add_encoding (this, x->prop->font->encoding);
1287 add_encoding (this, x->fixed->font->encoding);
1290 x->file_page_number = 0;
1293 if (prologue_fn == NULL)
1295 msg (IE, _("Cannot find PostScript prologue. The use of `-vv' "
1296 "on the command line is suggested as a debugging aid."));
1300 msg (VM (1), _("%s: %s: Opening PostScript prologue..."),
1301 this->name, prologue_fn);
1302 prologue_file = fopen (prologue_fn, "rb");
1303 if (prologue_file == NULL)
1305 fclose (prologue_file);
1307 msg (IE, "%s: %s", prologue_fn, strerror (errno));
1311 sprintf (boundbox, "0 0 %d %d",
1312 x->w / (PSUS / 72) + (x->w % (PSUS / 72) > 0),
1313 x->l / (PSUS / 72) + (x->l % (PSUS / 72) > 0));
1314 dict[0].value = boundbox;
1316 dict[1].value = (char *) version;
1318 curtime = time (NULL);
1319 loctime = localtime (&curtime);
1320 dict[2].value = asctime (loctime);
1321 cp = strchr (dict[2].value, '\n');
1328 dict[3].value = "Clean7Bit";
1331 dict[3].value = "Clean8Bit";
1334 dict[3].value = "Binary";
1340 if (x->orientation == OTN_PORTRAIT)
1341 dict[4].value = "Portrait";
1343 dict[4].value = "Landscape";
1345 /* PORTME: Determine username, net address. */
1347 dict[5].value = getenv ("LOGNAME");
1349 dict[5].value = getlogin ();
1351 dict[5].value = _("nobody");
1353 if (gethostname (host, 128) == -1)
1355 if (errno == ENAMETOOLONG)
1358 strcpy (host, _("nowhere"));
1360 dict[6].value = host;
1361 #else /* !HAVE_UNISTD_H */
1362 dict[5].value = _("nobody");
1363 dict[6].value = _("nowhere");
1364 #endif /* !HAVE_UNISTD_H */
1366 cp = stpcpy (p = local_alloc (288), "font ");
1367 quote_ps_string (cp, x->prop->font->internal_name);
1370 cp = stpcpy (p = local_alloc (288), "font ");
1371 quote_ps_string (cp, x->fixed->font->internal_name);
1374 sprintf (scaling, "%.3f", PSUS / 72.0);
1375 dict[9].value = scaling;
1377 sprintf (paper_width, "%g", x->w / (PSUS / 72.0));
1378 dict[10].value = paper_width;
1380 sprintf (paper_length, "%g", x->l / (PSUS / 72.0));
1381 dict[11].value = paper_length;
1383 sprintf (left_margin, "%d", x->left_margin);
1384 dict[12].value = left_margin;
1386 sprintf (top_margin, "%d", x->top_margin);
1387 dict[13].value = top_margin;
1389 sprintf (line_width, "%d", x->line_width);
1390 dict[14].value = line_width;
1392 sprintf (line_width, "%d", x->line_width_thick);
1393 dict[15].value = line_width_thick;
1397 dict[16].value = cp = local_alloc (strlen (dict[17].value) + 30);
1398 sprintf (cp, "PSPP (%s)", dict[17].value);
1402 dict[16].value = local_alloc (strlen (outp_title) + 1);
1403 strcpy ((char *) (dict[16].value), outp_title);
1407 while (-1 != getline (&buf, &buf_size, prologue_file))
1413 cp = strstr (buf, "!eps");
1416 if (this->class->magic == MAGIC_PS)
1423 cp = strstr (buf, "!ps");
1426 if (this->class->magic == MAGIC_EPSF)
1431 if (strstr (buf, "!!!"))
1436 if (!strncmp (buf, "!encodings", 10))
1437 output_encodings (this);
1441 beg = buf2 = fn_interp_vars (buf, ps_get_var);
1442 len = strlen (buf2);
1443 while (isspace ((unsigned char) *beg))
1445 if (beg[len - 1] == '\n')
1447 if (beg[len - 1] == '\r')
1449 fwrite (beg, len, 1, f->file);
1450 fputs (x->eol, f->file);
1454 if (ferror (f->file))
1455 msg (IE, _("Reading `%s': %s."), prologue_fn, strerror (errno));
1456 fclose (prologue_file);
1461 local_free (dict[7].value);
1462 local_free (dict[8].value);
1463 local_free (dict[16].value);
1465 if (ferror (f->file))
1468 msg (VM (2), _("%s: PostScript prologue read successfully."), this->name);
1472 msg (VM (1), _("%s: Error reading PostScript prologue."), this->name);
1476 /* Writes the string STRING to buffer DEST (of at least 288
1477 characters) as a PostScript name object. Returns a pointer
1478 to the null terminator of the resultant string. */
1480 quote_ps_name (char *dest, const char *string)
1484 for (sp = string; *sp; sp++)
1558 for (sp = string; *sp && dp < &dest[256]; sp++)
1560 sprintf (dp, "%02x", (unsigned char) *sp);
1563 return stpcpy (dp, ">cvn");
1567 return stpcpy (&dest[1], string);
1570 /* Adds the string STRING to buffer DEST as a PostScript quoted
1571 string; returns a pointer to the null terminator added. Will not
1572 add more than 235 characters. */
1574 quote_ps_string (char *dest, const char *string)
1576 const char *sp = string;
1580 for (; *sp && dp < &dest[235]; sp++)
1582 dp = stpcpy (dp, "\\(");
1583 else if (*sp == ')')
1584 dp = stpcpy (dp, "\\)");
1585 else if (*sp < 32 || (unsigned char) *sp > 127)
1586 dp = spprintf (dp, "\\%3o", *sp);
1589 return stpcpy (dp, ")");
1592 /* Writes the PostScript epilogue to file F. */
1594 preclose (struct file_ext *f)
1596 struct outp_driver *this = f->param;
1597 struct ps_driver_ext *x = this->ext;
1598 struct hsh_iterator iter;
1599 struct font_entry *fe;
1604 "%%%%DocumentNeededResources:%s"),
1605 x->eol, x->file_page_number, x->eol, x->eol);
1607 for (fe = hsh_first (x->loaded, &iter); fe != NULL;
1608 fe = hsh_next (x->loaded, &iter))
1612 cp = stpcpy (buf, "%%+ font ");
1613 cp = quote_ps_string (cp, fe->font->internal_name);
1614 strcpy (cp, x->eol);
1615 fputs (buf, f->file);
1618 hsh_destroy (x->loaded);
1620 hsh_destroy (x->combos);
1622 x->last_font = NULL;
1625 fprintf (f->file, "%%EOF%s", x->eol);
1626 if (ferror (f->file))
1632 ps_open_page (struct outp_driver *this)
1634 struct ps_driver_ext *x = this->ext;
1636 assert (this->driver_open && !this->page_open);
1639 if (!fn_open_ext (&x->file))
1642 msg (ME, _("PostScript output driver: %s: %s"), x->file.filename,
1646 x->file_page_number++;
1648 hsh_destroy (x->combos);
1649 x->combos = hsh_create (31, compare_ps_combo, hash_ps_combo,
1650 free_ps_combo, NULL);
1651 x->last_font = NULL;
1654 fprintf (x->file.file,
1656 "%%%%BeginPageSetup%s"
1657 "/pg save def 0.001 dup scale%s",
1658 x->page_number, x->file_page_number, x->eol,
1662 if (x->orientation == OTN_LANDSCAPE)
1663 fprintf (x->file.file,
1664 "%d 0 translate 90 rotate%s",
1667 if (x->bottom_margin != 0 || x->left_margin != 0)
1668 fprintf (x->file.file,
1669 "%d %d translate%s",
1670 x->left_margin, x->bottom_margin, x->eol);
1672 fprintf (x->file.file,
1673 "/LW %d def/TW %d def %d setlinewidth%s"
1674 "%%%%EndPageSetup%s",
1675 x->line_width, x->line_width_thick, x->line_width, x->eol,
1678 if (!ferror (x->file.file))
1680 this->page_open = 1;
1681 if (x->output_options & OPO_HEADERS)
1682 draw_headers (this);
1687 return !ferror (x->file.file);
1691 ps_close_page (struct outp_driver *this)
1693 struct ps_driver_ext *x = this->ext;
1695 assert (this->driver_open && this->page_open);
1700 fprintf (x->file.file,
1705 this->page_open = 0;
1706 return !ferror (x->file.file);
1710 ps_submit (struct outp_driver *this UNUSED, struct som_entity *s)
1724 /* qsort() comparison function for int tuples. */
1726 int_2_compare (const void *a_, const void *b_)
1731 return *a < *b ? -1 : *a > *b;
1734 /* Hash table comparison function for cached lines. */
1736 compare_line (const void *a_, const void *b_, void *foo UNUSED)
1738 const struct line_form *a = a_;
1739 const struct line_form *b = b_;
1741 return a->ind < b->ind ? -1 : a->ind > 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;
1829 for (type = 0; type < n_line_types; type++)
1831 struct line_form *line;
1833 if (x->lines[type] == NULL)
1836 for (line = hsh_first (x->lines[type], &iter); line != NULL;
1837 line = hsh_next (x->lines[type], &iter))
1840 int lo = INT_MIN, hi;
1842 qsort (line->dep, line->ndep, sizeof *line->dep, int_2_compare);
1843 lo = line->dep[0][0];
1844 hi = line->dep[0][1];
1845 for (i = 1; i < line->ndep; i++)
1846 if (line->dep[i][0] <= hi + 1)
1848 int min_hi = line->dep[i][1];
1854 dump_fancy_line (this, type, line->ind, lo, hi);
1855 lo = line->dep[i][0];
1856 hi = line->dep[i][1];
1858 dump_fancy_line (this, type, line->ind, lo, hi);
1861 hsh_destroy (x->lines[type]);
1862 x->lines[type] = NULL;
1866 /* (Same args as dump_fancy_line()). Either dumps the line directly
1867 to the output file, or adds it to the cache, depending on the
1868 user-selected line optimization mode. */
1870 line (struct outp_driver *this, int type, int ind, int dep1, int dep2)
1872 struct ps_driver_ext *ext = this->ext;
1873 struct line_form **f;
1875 assert (dep2 >= dep1);
1876 if (ext->line_opt == 0)
1878 dump_fancy_line (this, type, ind, dep1, dep2);
1882 if (ext->lines[type] == NULL)
1883 ext->lines[type] = hsh_create (31, compare_line, hash_line,
1885 f = (struct line_form **) hsh_probe (ext->lines[type], &ind);
1888 *f = xmalloc (sizeof **f + sizeof (int[15][2]));
1892 (*f)->dep[0][0] = dep1;
1893 (*f)->dep[0][1] = dep2;
1896 if ((*f)->ndep >= (*f)->mdep)
1899 *f = xrealloc (*f, sizeof **f + sizeof (int[2]) * ((*f)->mdep - 1));
1901 (*f)->dep[(*f)->ndep][0] = dep1;
1902 (*f)->dep[(*f)->ndep][1] = dep2;
1907 ps_line_horz (struct outp_driver *this, const struct rect *r,
1908 const struct color *c UNUSED, int style)
1910 /* Must match output.h:OUTP_L_*. */
1911 static const int types[OUTP_L_COUNT] =
1912 {-1, horz, dbl_horz, spl_horz};
1914 int y = (r->y1 + r->y2) / 2;
1916 assert (this->driver_open && this->page_open);
1917 assert (style >= 0 && style < OUTP_L_COUNT);
1918 style = types[style];
1920 line (this, style, y, r->x1, r->x2);
1924 ps_line_vert (struct outp_driver *this, const struct rect *r,
1925 const struct color *c UNUSED, int style)
1927 /* Must match output.h:OUTP_L_*. */
1928 static const int types[OUTP_L_COUNT] =
1929 {-1, vert, dbl_vert, spl_vert};
1931 int x = (r->x1 + r->x2) / 2;
1933 assert (this->driver_open && this->page_open);
1934 assert (style >= 0 && style < OUTP_L_COUNT);
1935 style = types[style];
1937 line (this, style, x, r->y1, r->y2);
1940 #define L (style->l != OUTP_L_NONE)
1941 #define R (style->r != OUTP_L_NONE)
1942 #define T (style->t != OUTP_L_NONE)
1943 #define B (style->b != OUTP_L_NONE)
1946 ps_line_intersection (struct outp_driver *this, const struct rect *r,
1947 const struct color *c UNUSED,
1948 const struct outp_styles *style)
1950 struct ps_driver_ext *ext = this->ext;
1952 int x = (r->x1 + r->x2) / 2;
1953 int y = (r->y1 + r->y2) / 2;
1954 int ofs = (ext->line_space + ext->line_width) / 2;
1955 int x1 = x - ofs, x2 = x + ofs;
1956 int y1 = y - ofs, y2 = y + ofs;
1958 assert (this->driver_open && this->page_open);
1959 assert (!((style->l != style->r && style->l != OUTP_L_NONE
1960 && style->r != OUTP_L_NONE)
1961 || (style->t != style->b && style->t != OUTP_L_NONE
1962 && style->b != OUTP_L_NONE)));
1964 switch ((style->l | style->r) | ((style->t | style->b) << 8))
1966 case (OUTP_L_SINGLE) | (OUTP_L_SINGLE << 8):
1967 case (OUTP_L_SINGLE) | (OUTP_L_NONE << 8):
1968 case (OUTP_L_NONE) | (OUTP_L_SINGLE << 8):
1970 line (this, horz, y, r->x1, x);
1972 line (this, horz, y, x, r->x2);
1974 line (this, vert, x, r->y1, y);
1976 line (this, vert, x, y, r->y2);
1978 case (OUTP_L_SINGLE) | (OUTP_L_DOUBLE << 8):
1979 case (OUTP_L_NONE) | (OUTP_L_DOUBLE << 8):
1981 line (this, horz, y, r->x1, x1);
1983 line (this, horz, y, x2, r->x2);
1985 line (this, dbl_vert, x, r->y1, y);
1987 line (this, dbl_vert, x, y, r->y2);
1988 if ((L && R) && !(T && B))
1989 line (this, horz, y, x1, x2);
1991 case (OUTP_L_DOUBLE) | (OUTP_L_SINGLE << 8):
1992 case (OUTP_L_DOUBLE) | (OUTP_L_NONE << 8):
1994 line (this, dbl_horz, y, r->x1, x);
1996 line (this, dbl_horz, y, x, r->x2);
1998 line (this, vert, x, r->y1, y);
2000 line (this, vert, x, y, r->y2);
2001 if ((T && B) && !(L && R))
2002 line (this, vert, x, y1, y2);
2004 case (OUTP_L_DOUBLE) | (OUTP_L_DOUBLE << 8):
2006 line (this, dbl_horz, y, r->x1, x);
2008 line (this, dbl_horz, y, x, r->x2);
2010 line (this, dbl_vert, x, r->y1, y);
2012 line (this, dbl_vert, x, y, r->y2);
2014 line (this, vert, x1, y1, y2);
2016 line (this, vert, x2, y1, y2);
2018 line (this, horz, y1, x1, x2);
2020 line (this, horz, y2, x1, x2);
2028 ps_box (struct outp_driver *this UNUSED, const struct rect *r UNUSED,
2029 const struct color *bord UNUSED, const struct color *fill UNUSED)
2031 assert (this->driver_open && this->page_open);
2035 ps_polyline_begin (struct outp_driver *this UNUSED,
2036 const struct color *c UNUSED)
2038 assert (this->driver_open && this->page_open);
2041 ps_polyline_point (struct outp_driver *this UNUSED, int x UNUSED, int y UNUSED)
2043 assert (this->driver_open && this->page_open);
2046 ps_polyline_end (struct outp_driver *this UNUSED)
2048 assert (this->driver_open && this->page_open);
2051 /* Returns the width of string S for THIS driver. */
2053 text_width (struct outp_driver *this, char *s)
2055 struct outp_text text;
2057 text.options = OUTP_T_JUST_LEFT;
2058 ls_init (&text.s, s, strlen (s));
2059 this->class->text_metrics (this, &text);
2063 /* Write string S at location (X,Y) with width W for THIS driver. */
2065 out_text_plain (struct outp_driver *this, char *s, int x, int y, int w)
2067 struct outp_text text;
2069 text.options = OUTP_T_JUST_LEFT | OUTP_T_HORZ | OUTP_T_VERT;
2070 ls_init (&text.s, s, strlen (s));
2072 text.v = this->font_height;
2075 this->class->text_draw (this, &text);
2078 /* Draw top of page headers for THIS driver. */
2080 draw_headers (struct outp_driver *this)
2082 struct ps_driver_ext *ext = this->ext;
2084 struct font_entry *old_current = ext->current;
2085 char *old_family = xstrdup (ext->family); /* FIXME */
2086 int old_size = ext->size;
2088 int fh = this->font_height;
2091 fprintf (ext->file.file, "%d %d %d %d GB%s",
2092 0, YT (y), this->width, YT (y + 2 * fh + ext->line_gutter),
2094 this->class->text_set_font_family (this, "T");
2096 y += ext->line_width + ext->line_gutter;
2102 sprintf (buf, _("%s - Page %d"), get_start_date (), ext->page_number);
2103 rh_width = text_width (this, buf);
2105 out_text_plain (this, buf, this->width - this->prop_em_width - rh_width,
2108 if (outp_title && outp_subtitle)
2109 out_text_plain (this, outp_title, this->prop_em_width, y,
2110 this->width - 3 * this->prop_em_width - rh_width);
2118 char *string = outp_subtitle ? outp_subtitle : outp_title;
2120 sprintf (buf, "%s - %s", version, host_system);
2121 rh_width = text_width (this, buf);
2123 out_text_plain (this, buf, this->width - this->prop_em_width - rh_width,
2127 out_text_plain (this, string, this->prop_em_width, y,
2128 this->width - 3 * this->prop_em_width - rh_width);
2133 ext->current = old_current;
2135 ext->family = old_family;
2136 ext->size = old_size;
2143 ps_text_set_font_by_name (struct outp_driver *this, const char *dit)
2145 struct ps_driver_ext *x = this->ext;
2146 struct font_entry *fe;
2148 assert (this->driver_open && this->page_open);
2150 /* Short-circuit common fonts. */
2151 if (!strcmp (dit, "PROP"))
2153 x->current = x->prop;
2154 x->size = x->font_size;
2157 else if (!strcmp (dit, "FIXED"))
2159 x->current = x->fixed;
2160 x->size = x->font_size;
2164 /* Find font_desc corresponding to Groff name dit. */
2165 fe = hsh_find (x->loaded, &dit);
2167 fe = load_font (this, dit);
2172 ps_text_set_font_by_position (struct outp_driver *this, int pos)
2174 struct ps_driver_ext *x = this->ext;
2177 assert (this->driver_open && this->page_open);
2179 /* Determine font name by suffixing position string to font family
2184 dit = local_alloc (strlen (x->family) + 3);
2185 cp = stpcpy (dit, x->family);
2207 /* Find font_desc corresponding to Groff name dit. */
2209 struct font_entry *fe = hsh_find (x->loaded, &dit);
2211 fe = load_font (this, dit);
2219 ps_text_set_font_family (struct outp_driver *this, const char *s)
2221 struct ps_driver_ext *x = this->ext;
2223 assert (this->driver_open && this->page_open);
2226 x->family = xstrdup (s);
2230 ps_text_get_font_name (struct outp_driver *this)
2232 struct ps_driver_ext *x = this->ext;
2234 assert (this->driver_open && this->page_open);
2235 return x->current->font->name;
2239 ps_text_get_font_family (struct outp_driver *this)
2241 struct ps_driver_ext *x = this->ext;
2243 assert (this->driver_open && this->page_open);
2248 ps_text_set_size (struct outp_driver *this, int size)
2250 struct ps_driver_ext *x = this->ext;
2252 assert (this->driver_open && this->page_open);
2253 x->size = PSUS / 72000 * size;
2258 ps_text_get_size (struct outp_driver *this, int *em_width)
2260 struct ps_driver_ext *x = this->ext;
2262 assert (this->driver_open && this->page_open);
2264 *em_width = (x->current->font->space_width * x->size) / 1000;
2265 return x->size / (PSUS / 72000);
2268 /* An output character. */
2271 struct font_entry *font; /* Font of character. */
2272 int size; /* Size of character. */
2273 int x, y; /* Location of character. */
2274 unsigned char ch; /* Character. */
2275 char separate; /* Must be separate from previous char. */
2278 /* Hash table comparison function for ps_combo structs. */
2280 compare_ps_combo (const void *pa, const void *pb, void *foo UNUSED)
2282 const struct ps_font_combo *a = pa;
2283 const struct ps_font_combo *b = pb;
2285 return !((a->font == b->font) && (a->size == b->size));
2288 /* Hash table hash function for ps_combo structs. */
2290 hash_ps_combo (const void *pa, void *foo UNUSED)
2292 const struct ps_font_combo *a = pa;
2293 unsigned name_hash = hsh_hash_string (a->font->font->internal_name);
2294 return name_hash ^ hsh_hash_int (a->size);
2297 /* Hash table free function for ps_combo structs. */
2299 free_ps_combo (void *a, void *foo UNUSED)
2304 /* Causes PostScript code to be output that switches to the font
2305 CP->FONT and font size CP->SIZE. The first time a particular
2306 font/size combination is used on a particular page, this involves
2307 outputting PostScript code to load the font. */
2309 switch_font (struct outp_driver *this, const struct output_char *cp)
2311 struct ps_driver_ext *ext = this->ext;
2312 struct ps_font_combo srch, **fc;
2314 srch.font = cp->font;
2315 srch.size = cp->size;
2317 fc = (struct ps_font_combo **) hsh_probe (ext->combos, &srch);
2320 fprintf (ext->file.file, "F%x%s", (*fc)->index, ext->eol);
2325 struct ps_encoding *encoding;
2328 *fc = xmalloc (sizeof **fc);
2329 (*fc)->font = cp->font;
2330 (*fc)->size = cp->size;
2331 (*fc)->index = ext->next_combo++;
2333 filename = find_encoding_file (this, cp->font->font->encoding);
2336 encoding = get_encoding (this, filename);
2341 msg (IE, _("PostScript driver: Cannot find encoding `%s' for "
2342 "PostScript font `%s'."), cp->font->font->encoding,
2343 cp->font->font->internal_name);
2344 encoding = default_encoding (this);
2347 if (cp->font != ext->fixed && cp->font != ext->prop)
2349 bp = stpcpy (buf, "%%IncludeResource: font ");
2350 bp = quote_ps_string (bp, cp->font->font->internal_name);
2351 bp = stpcpy (bp, ext->eol);
2356 bp = spprintf (bp, "/F%x E%x %d", (*fc)->index, encoding->index,
2358 bp = quote_ps_name (bp, cp->font->font->internal_name);
2359 sprintf (bp, " SF%s", ext->eol);
2360 fputs (buf, ext->file.file);
2362 ext->last_font = *fc;
2365 /* (write_text) Writes the accumulated line buffer to the output
2367 #define output_line() \
2370 lp = stpcpy (lp, ext->eol); \
2372 fputs (line, ext->file.file); \
2377 /* (write_text) Adds the string representing number X to the line
2378 buffer, flushing the buffer to disk beforehand if necessary. */
2379 #define put_number(X) \
2382 int n = nsprintf (number, "%d", X); \
2383 if (n + lp > &line[75]) \
2385 lp = stpcpy (lp, number); \
2389 /* Outputs PostScript code to THIS driver's output file to display the
2390 characters represented by the output_char's between CP and END,
2391 using the associated outp_text T to determine formatting. WIDTH is
2392 the width of the output region; WIDTH_LEFT is the amount of the
2393 WIDTH that is not taken up by text (so it can be used to determine
2396 write_text (struct outp_driver *this,
2397 const struct output_char *cp, const struct output_char *end,
2398 struct outp_text *t, int width UNUSED, int width_left)
2400 struct ps_driver_ext *ext = this->ext;
2405 char number[INT_DIGITS + 1];
2409 switch (t->options & OUTP_T_JUST_MASK)
2411 case OUTP_T_JUST_LEFT:
2414 case OUTP_T_JUST_RIGHT:
2417 case OUTP_T_JUST_CENTER:
2418 ofs = width_left / 2;
2429 int x = cp->x + ofs;
2430 int y = cp->y + (cp->font->font->ascent * cp->size / 1000);
2432 if (ext->last_font == NULL
2433 || cp->font != ext->last_font->font
2434 || cp->size != ext->last_font->size)
2435 switch_font (this, cp);
2441 static unsigned char literal_chars[ODA_COUNT][32] =
2443 {0x00, 0x00, 0x00, 0xf8, 0xff, 0xfc, 0xff, 0xff,
2444 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f,
2445 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2446 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2448 {0x00, 0x00, 0x00, 0xf8, 0xff, 0xfc, 0xff, 0xff,
2449 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
2450 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
2451 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
2453 {0x7e, 0xd6, 0xff, 0xfb, 0xff, 0xfc, 0xff, 0xff,
2454 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
2455 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
2456 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
2460 if (TEST_BIT (literal_chars[ext->data], cp->ch))
2463 switch ((char) cp->ch)
2466 lp = stpcpy (lp, "\\(");
2469 lp = stpcpy (lp, "\\)");
2472 lp = spprintf (lp, "\\%03o", cp->ch);
2477 while (cp < end && lp < &line[70] && cp->separate == 0);
2485 put_number (YT (y));
2496 if (lp >= &line[70])
2506 /* Displays the text in outp_text T, if DRAW is nonzero; or, merely
2507 determine the text metrics, if DRAW is zero. */
2509 text (struct outp_driver *this, struct outp_text *t, int draw)
2511 struct ps_driver_ext *ext = this->ext;
2514 struct output_char *buf; /* Output buffer. */
2515 struct output_char *buf_end; /* End of output buffer. */
2516 struct output_char *buf_loc; /* Current location in output buffer. */
2519 struct font_entry *old_current = ext->current;
2520 char *old_family = xstrdup (ext->family); /* FIXME */
2521 int old_size = ext->size;
2526 /* Current location. */
2529 /* Keeping track of what's left over. */
2530 int width; /* Width available for characters. */
2531 int width_left, height_left; /* Width, height left over. */
2532 int max_height; /* Tallest character on this line so far. */
2534 /* Previous character. */
2537 /* Information about location of previous space. */
2538 char *space_char; /* Character after space. */
2539 struct output_char *space_buf_loc; /* Buffer location after space. */
2540 int space_width_left; /* Width of characters before space. */
2542 /* Name of the current character. */
2543 const char *char_name;
2544 char local_char_name[2] = {0, 0};
2546 local_char_name[0] = local_char_name[1] = 0;
2548 buf = local_alloc (sizeof *buf * 128);
2549 buf_end = &buf[128];
2552 assert (!ls_null_p (&t->s));
2553 cp = ls_c_str (&t->s);
2554 end = ls_end (&t->s);
2562 width = width_left = (t->options & OUTP_T_HORZ) ? t->h : INT_MAX;
2563 height_left = (t->options & OUTP_T_VERT) ? t->v : INT_MAX;
2567 space_buf_loc = NULL;
2568 space_width_left = 0;
2571 if (!width || !height_left)
2576 struct char_metrics *metric;
2582 /* Set char_name to the name of the character or ligature at
2584 local_char_name[0] = *cp;
2585 char_name = local_char_name;
2586 if (ext->current->font->ligatures && *cp == 'f')
2595 lig = LIG_fi, char_name = "fi";
2598 lig = LIG_fl, char_name = "fl";
2605 lig = LIG_ffi, char_name = "ffi";
2608 lig = LIG_ffl, char_name = "ffl";
2611 lig = LIG_ff, char_name = "ff";
2615 if ((lig & ext->current->font->ligatures) == 0)
2617 local_char_name[0] = *cp; /* 'f' */
2618 char_name = local_char_name;
2621 else if (*cp == '\n')
2625 write_text (this, buf, buf_loc, t, width, width_left);
2632 height_left -= max_height;
2638 /* FIXME: when we're page buffering it will be necessary to
2639 set separate to 1. */
2642 cp += strlen (char_name);
2644 /* Figure out what size this character is, and what kern
2645 adjustment we need. */
2646 cur_char = font_char_name_to_index (char_name);
2647 metric = font_get_char_metrics (ext->current->font, cur_char);
2650 static struct char_metrics m;
2652 m.width = ext->current->font->space_width;
2653 m.code = *char_name;
2655 kern_amt = font_get_kern_adjust (ext->current->font, prev_char,
2659 kern_amt = (kern_amt * ext->size / 1000);
2662 char_width = metric->width * ext->size / 1000;
2664 /* Record the current status if this is a space character. */
2665 if (cur_char == space_index && buf_loc > buf)
2668 space_buf_loc = buf_loc;
2669 space_width_left = width_left;
2672 /* Drop down to a new line if there's no room left on this
2674 if (char_width + kern_amt > width_left)
2676 /* Regress to previous space, if any. */
2680 width_left = space_width_left;
2681 buf_loc = space_buf_loc;
2686 write_text (this, buf, buf_loc, t, width, width_left);
2693 height_left -= max_height;
2701 /* FIXME: when we're page buffering it will be
2702 necessary to set separate to 1. */
2707 if (ext->size > max_height)
2708 max_height = ext->size;
2709 if (max_height > height_left)
2712 /* Actually draw the character. */
2715 if (buf_loc >= buf_end)
2717 int buf_len = buf_end - buf;
2721 struct output_char *new_buf;
2723 new_buf = xmalloc (sizeof *new_buf * 256);
2724 memcpy (new_buf, buf, sizeof *new_buf * 128);
2725 buf_loc = new_buf + 128;
2726 buf_end = new_buf + 256;
2732 buf = xnrealloc (buf, buf_len * 2, sizeof *buf);
2733 buf_loc = buf + buf_len;
2734 buf_end = buf + buf_len * 2;
2739 buf_loc->font = ext->current;
2740 buf_loc->size = ext->size;
2743 buf_loc->ch = metric->code;
2744 buf_loc->separate = separate;
2749 /* Prepare for next iteration. */
2750 width_left -= char_width + kern_amt;
2751 prev_char = cur_char;
2753 height_left -= max_height;
2754 if (buf_loc > buf && draw)
2755 write_text (this, buf, buf_loc, t, width, width_left);
2758 if (!(t->options & OUTP_T_HORZ))
2759 t->h = INT_MAX - width_left;
2760 if (!(t->options & OUTP_T_VERT))
2761 t->v = INT_MAX - height_left;
2763 t->v -= height_left;
2764 if (buf_end - buf == 128)
2768 ext->current = old_current;
2770 ext->family = old_family;
2771 ext->size = old_size;
2775 ps_text_metrics (struct outp_driver *this, struct outp_text *t)
2777 assert (this->driver_open && this->page_open);
2782 ps_text_draw (struct outp_driver *this, struct outp_text *t)
2784 assert (this->driver_open && this->page_open);
2790 /* Translate a filename to a font. */
2791 struct filename2font
2793 char *filename; /* Normalized filename. */
2794 struct font_desc *font;
2797 /* Table of `filename2font's. */
2798 static struct hsh_table *ps_fonts;
2800 /* Hash table comparison function for filename2font structs. */
2802 compare_filename2font (const void *a, const void *b, void *param UNUSED)
2804 return strcmp (((struct filename2font *) a)->filename,
2805 ((struct filename2font *) b)->filename);
2808 /* Hash table hash function for filename2font structs. */
2810 hash_filename2font (const void *f2f_, void *param UNUSED)
2812 const struct filename2font *f2f = f2f_;
2813 return hsh_hash_string (f2f->filename);
2816 /* Initializes the global font list by creating the hash table for
2817 translation of filenames to font_desc structs. */
2821 ps_fonts = hsh_create (31, compare_filename2font, hash_filename2font,
2828 hsh_destroy (ps_fonts);
2831 /* Loads the font having Groff name DIT into THIS driver instance.
2832 Specifically, adds it into the THIS driver's `loaded' hash
2834 static struct font_entry *
2835 load_font (struct outp_driver *this, const char *dit)
2837 struct ps_driver_ext *x = this->ext;
2838 char *filename1, *filename2;
2840 struct font_entry *fe;
2842 filename1 = find_ps_file (this, dit);
2844 filename1 = xstrdup (dit);
2845 filename2 = fn_normalize (filename1);
2848 entry = hsh_probe (ps_fonts, &filename2);
2851 struct filename2font *f2f;
2852 struct font_desc *f = groff_read_font (filename2);
2859 f = default_font ();
2862 f2f = xmalloc (sizeof *f2f);
2863 f2f->filename = filename2;
2870 fe = xmalloc (sizeof *fe);
2871 fe->dit = xstrdup (dit);
2872 fe->font = ((struct filename2font *) * entry)->font;
2873 *hsh_probe (x->loaded, &dit) = fe;
2879 ps_chart_initialise (struct outp_driver *this UNUSED, struct chart *ch)
2884 struct ps_driver_ext *x = this->ext;
2885 char page_size[128];
2887 int x_origin, y_origin;
2889 ch->file = tmpfile ();
2890 if (ch->file == NULL)
2896 size = this->width < this->length ? this->width : this->length;
2897 x_origin = x->left_margin + (size - this->width) / 2;
2898 y_origin = x->bottom_margin + (size - this->length) / 2;
2900 snprintf (page_size, sizeof page_size,
2901 "a,xsize=%.3f,ysize=%.3f,xorigin=%.3f,yorigin=%.3f",
2902 (double) size / PSUS, (double) size / PSUS,
2903 (double) x_origin / PSUS, (double) y_origin / PSUS);
2905 ch->pl_params = pl_newplparams ();
2906 pl_setplparam (ch->pl_params, "PAGESIZE", page_size);
2907 ch->lp = pl_newpl_r ("ps", NULL, ch->file, stderr, ch->pl_params);
2912 ps_chart_finalise (struct outp_driver *this UNUSED, struct chart *ch UNUSED)
2915 struct ps_driver_ext *x = this->ext;
2917 static int doc_num = 0;
2919 if (this->page_open)
2921 this->class->close_page (this);
2922 this->page_open = 0;
2924 this->class->open_page (this);
2925 fprintf (x->file.file,
2927 "%d %d translate 1000 dup scale%s"
2929 "/showpage { } def%s"
2930 "0 setgray 0 setlinecap 1 setlinewidth%s"
2931 "0 setlinejoin 10 setmiterlimit [ ] 0 setdash newpath clear%s"
2932 "%%%%BeginDocument: %d%s",
2934 -x->left_margin, -x->bottom_margin, x->eol,
2942 while (fwrite (buf, 1, fread (buf, 1, sizeof buf, ch->file), x->file.file))
2946 fprintf (x->file.file,
2953 this->class->close_page (this);
2954 this->page_open = 0;
2958 /* PostScript driver class. */
2959 struct outp_class postscript_class =
2981 ps_line_intersection,
2988 ps_text_set_font_by_name,
2989 ps_text_set_font_by_position,
2990 ps_text_set_font_family,
2991 ps_text_get_font_name,
2992 ps_text_get_font_family,
2998 ps_chart_initialise,
3002 /* EPSF driver class. FIXME: Probably doesn't work right. */
3003 struct outp_class epsf_class =
3025 ps_line_intersection,
3032 ps_text_set_font_by_name,
3033 ps_text_set_font_by_position,
3034 ps_text_set_font_family,
3035 ps_text_get_font_name,
3036 ps_text_get_font_family,
3042 ps_chart_initialise,
3047 #endif /* NO_POSTSCRIPT */