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
22 /*this #if encloses the remainder of the file. */
35 #if TIME_WITH_SYS_TIME
47 #include "bitvector.h"
61 optimize-text-size not implemented.
63 Line buffering is the only possibility; page buffering should also
68 Should add a field to give a file that has a list of fonts
71 Should add an option that tells the driver it can emit %%Include:'s.
73 Should have auto-encode=true stream-edit or whatever to allow
74 addition to list of encodings.
76 Should align fonts of different sizes along their baselines (see
79 /* PostScript driver options: (defaults listed first)
83 data=clean7bit|clean8bit|binary
86 paper-size=letter (see "papersize" file)
87 orientation=portrait|landscape
96 prologue-file=ps-prologue
98 encoding-file=ps-encodings
99 auto-encode=true|false
105 line-style=thick|double
111 optimize-text-size=1|0|2
112 optimize-line-size=1|0
113 max-fonts-simult=0 Max # of fonts in printer memory at once (0=infinite)
116 /* The number of `psus' (PostScript driver UnitS) per inch. Although
117 this is a #define, the value is expected never to change. If it
118 does, review all uses. */
121 /* Magic numbers for PostScript and EPSF drivers. */
131 OTN_PORTRAIT, /* Portrait. */
132 OTN_LANDSCAPE /* Landscape. */
135 /* Output options. */
138 OPO_MIRROR_HORZ = 001, /* 1=Mirror across a horizontal axis. */
139 OPO_MIRROR_VERT = 002, /* 1=Mirror across a vertical axis. */
140 OPO_ROTATE_180 = 004, /* 1=Rotate the page 180 degrees. */
141 OPO_COLOR = 010, /* 1=Enable color. */
142 OPO_HEADERS = 020, /* 1=Draw headers at top of page. */
143 OPO_AUTO_ENCODE = 040, /* 1=Add encodings semi-intelligently. */
144 OPO_DOUBLE_LINE = 0100 /* 1=Double lines instead of thick lines. */
147 /* Data allowed in output. */
150 ODA_CLEAN7BIT, /* 0x09, 0x0a, 0x0d, 0x1b...0x7e */
151 ODA_CLEAN8BIT, /* 0x09, 0x0a, 0x0d, 0x1b...0xff */
152 ODA_BINARY, /* 0x00...0xff */
156 /* Types of lines for purpose of caching. */
159 horz, /* Single horizontal. */
160 dbl_horz, /* Double horizontal. */
161 spl_horz, /* Special horizontal. */
162 vert, /* Single vertical. */
163 dbl_vert, /* Double vertical. */
164 spl_vert, /* Special vertical. */
171 int ind; /* Independent var. Don't reorder. */
172 int mdep; /* Maximum number of dependent var pairs. */
173 int ndep; /* Current number of dependent var pairs. */
174 int dep[1][2]; /* Dependent var pairs. */
177 /* Contents of ps_driver_ext.loaded. */
180 char *dit; /* Font Groff name. */
181 struct font_desc *font; /* Font descriptor. */
184 /* Combines a font with a font size for benefit of generated code. */
187 struct font_entry *font; /* Font. */
188 int size; /* Font size. */
189 int index; /* PostScript index. */
192 /* A font encoding. */
195 char *filename; /* Normalized filename of this encoding. */
196 int index; /* Index value. */
199 /* PostScript output driver extension record. */
202 /* User parameters. */
203 int orientation; /* OTN_PORTRAIT or OTN_LANDSCAPE. */
204 int output_options; /* OPO_*. */
205 int data; /* ODA_*. */
207 int left_margin; /* Left margin in psus. */
208 int right_margin; /* Right margin in psus. */
209 int top_margin; /* Top margin in psus. */
210 int bottom_margin; /* Bottom margin in psus. */
212 char eol[3]; /* End of line--CR, LF, or CRLF. */
214 char *font_dir; /* Font directory relative to font path. */
215 char *prologue_fn; /* Prologue's filename relative to font dir. */
216 char *desc_fn; /* DESC filename relative to font dir. */
217 char *encoding_fn; /* Encoding's filename relative to font dir. */
219 char *prop_family; /* Default proportional font family. */
220 char *fixed_family; /* Default fixed-pitch font family. */
221 int font_size; /* Default font size (psus). */
223 int line_gutter; /* Space around lines. */
224 int line_space; /* Space between lines. */
225 int line_width; /* Width of lines. */
226 int line_width_thick; /* Width of thick lines. */
228 int text_opt; /* Text optimization level. */
229 int line_opt; /* Line optimization level. */
230 int max_fonts; /* Max # of simultaneous fonts (0=infinite). */
232 /* Internal state. */
233 struct file_ext file; /* Output file. */
234 int page_number; /* Current page number. */
235 int file_page_number; /* Page number in this file. */
236 int w, l; /* Paper size. */
237 struct hsh_table *lines[n_line_types]; /* Line buffers. */
239 struct font_entry *prop; /* Default Roman proportional font. */
240 struct font_entry *fixed; /* Default Roman fixed-pitch font. */
241 struct hsh_table *loaded; /* Fonts in memory. */
243 struct hsh_table *combos; /* Combinations of fonts with font sizes. */
244 struct ps_font_combo *last_font; /* PostScript selected font. */
245 int next_combo; /* Next font combo position index. */
247 struct hsh_table *encodings;/* Set of encodings. */
248 int next_encoding; /* Next font encoding index. */
250 /* Currently selected font. */
251 struct font_entry *current; /* Current font. */
252 char *family; /* Font family. */
253 int size; /* Size in psus. */
257 /* Transform logical y-ordinate Y into a page ordinate. */
258 #define YT(Y) (this->length - (Y))
261 static int postopen (struct file_ext *);
262 static int preclose (struct file_ext *);
263 static void draw_headers (struct outp_driver *this);
265 static int compare_font_entry (const void *, const void *, void *param);
266 static unsigned hash_font_entry (const void *, void *param);
267 static void free_font_entry (void *, void *foo);
268 static struct font_entry *load_font (struct outp_driver *, const char *dit);
269 static void init_fonts (void);
271 static void dump_lines (struct outp_driver *this);
273 static void read_ps_encodings (struct outp_driver *this);
274 static int compare_ps_encoding (const void *pa, const void *pb, void *foo);
275 static unsigned hash_ps_encoding (const void *pa, void *foo);
276 static void free_ps_encoding (void *a, void *foo);
277 static void add_encoding (struct outp_driver *this, char *filename);
278 static struct ps_encoding *default_encoding (struct outp_driver *this);
280 static int compare_ps_combo (const void *pa, const void *pb, void *foo);
281 static unsigned hash_ps_combo (const void *pa, void *foo);
282 static void free_ps_combo (void *a, void *foo);
284 static char *quote_ps_name (char *dest, const char *string);
285 static char *quote_ps_string (char *dest, const char *string);
287 /* Driver initialization. */
290 ps_open_global (struct outp_class *this unused)
298 ps_close_global (struct outp_class *this unused)
304 ps_font_sizes (struct outp_class *this unused, int *n_valid_sizes)
306 /* Allow fonts up to 1" in height. */
307 static int valid_sizes[] =
310 assert (n_valid_sizes != NULL);
316 ps_preopen_driver (struct outp_driver *this)
318 struct ps_driver_ext *x;
322 assert (this->driver_open == 0);
323 msg (VM (1), _("PostScript driver initializing as `%s'..."), this->name);
325 this->ext = x = xmalloc (sizeof (struct ps_driver_ext));
327 this->horiz = this->vert = 1;
328 this->width = this->length = 0;
330 x->orientation = OTN_PORTRAIT;
331 x->output_options = OPO_COLOR | OPO_HEADERS | OPO_AUTO_ENCODE;
332 x->data = ODA_CLEAN7BIT;
334 x->left_margin = x->right_margin =
335 x->top_margin = x->bottom_margin = PSUS / 2;
337 strcpy (x->eol, "\n");
340 x->prologue_fn = NULL;
342 x->encoding_fn = NULL;
344 x->prop_family = NULL;
345 x->fixed_family = NULL;
346 x->font_size = PSUS * 10 / 72;
348 x->line_gutter = PSUS / 144;
349 x->line_space = PSUS / 144;
350 x->line_width = PSUS / 144;
351 x->line_width_thick = PSUS / 48;
357 x->file.filename = NULL;
360 x->file.sequence_no = &x->page_number;
361 x->file.param = this;
362 x->file.postopen = postopen;
363 x->file.preclose = preclose;
367 x->file_page_number = 0;
368 for (i = 0; i < n_line_types; i++)
379 x->encodings = hsh_create (31, compare_ps_encoding, hash_ps_encoding,
380 free_ps_encoding, NULL);
381 x->next_encoding = 0;
391 ps_postopen_driver (struct outp_driver *this)
393 struct ps_driver_ext *x = this->ext;
395 assert (this->driver_open == 0);
397 if (this->width == 0)
399 this->width = PSUS * 17 / 2; /* Defaults to 8.5"x11". */
400 this->length = PSUS * 11;
403 if (x->text_opt == -1)
404 x->text_opt = (this->device & OUTP_DEV_SCREEN) ? 0 : 1;
405 if (x->line_opt == -1)
406 x->line_opt = (this->device & OUTP_DEV_SCREEN) ? 0 : 1;
410 if (x->orientation == OTN_LANDSCAPE)
412 int temp = this->width;
413 this->width = this->length;
416 this->width -= x->left_margin + x->right_margin;
417 this->length -= x->top_margin + x->bottom_margin;
418 if (x->output_options & OPO_HEADERS)
420 this->length -= 3 * x->font_size;
421 x->top_margin += 3 * x->font_size;
423 if (NULL == x->file.filename)
424 x->file.filename = xstrdup ("pspp.ps");
426 if (x->font_dir == NULL)
427 x->font_dir = xstrdup ("devps");
428 if (x->prologue_fn == NULL)
429 x->prologue_fn = xstrdup ("ps-prologue");
430 if (x->desc_fn == NULL)
431 x->desc_fn = xstrdup ("DESC");
432 if (x->encoding_fn == NULL)
433 x->encoding_fn = xstrdup ("ps-encodings");
435 if (x->prop_family == NULL)
436 x->prop_family = xstrdup ("H");
437 if (x->fixed_family == NULL)
438 x->fixed_family = xstrdup ("C");
440 read_ps_encodings (this);
445 if (this->length / x->font_size < 15)
447 msg (SE, _("PostScript driver: The defined page is not long "
448 "enough to hold margins and headers, plus least 15 "
449 "lines of the default fonts. In fact, there's only "
450 "room for %d lines of each font at the default size "
451 "of %d.%03d points."),
452 this->length / x->font_size,
453 x->font_size / 1000, x->font_size % 1000);
457 this->driver_open = 1;
458 msg (VM (2), _("%s: Initialization complete."), this->name);
464 ps_close_driver (struct outp_driver *this)
466 struct ps_driver_ext *x = this->ext;
470 assert (this->driver_open == 1);
471 msg (VM (2), _("%s: Beginning closing..."), this->name);
473 fn_close_ext (&x->file);
474 free (x->file.filename);
476 free (x->prologue_fn);
478 free (x->encoding_fn);
479 free (x->prop_family);
480 free (x->fixed_family);
482 for (i = 0; i < n_line_types; i++)
483 hsh_destroy (x->lines[i]);
484 hsh_destroy (x->encodings);
485 hsh_destroy (x->combos);
486 hsh_destroy (x->loaded);
489 this->driver_open = 0;
490 msg (VM (3), _("%s: Finished closing."), this->name);
495 /* font_entry comparison function for hash tables. */
497 compare_font_entry (const void *a, const void *b, void *foobar unused)
499 return strcmp (((struct font_entry *) a)->dit, ((struct font_entry *) b)->dit);
502 /* font_entry hash function for hash tables. */
504 hash_font_entry (const void *fe_, void *foobar unused)
506 const struct font_entry *fe = fe_;
507 return hsh_hash_string (fe->dit);
510 /* font_entry destructor function for hash tables. */
512 free_font_entry (void *pa, void *foo unused)
514 struct font_entry *a = pa;
519 /* Generic option types. */
529 /* All the options that the PostScript driver supports. */
530 static struct outp_option option_tab[] =
533 {"output-file", 1, 0},
534 {"paper-size", 2, 0},
535 {"orientation", 3, 0},
536 {"color", boolean_arg, 0},
538 {"auto-encode", boolean_arg, 5},
539 {"headers", boolean_arg, 1},
540 {"left-margin", pos_int_arg, 0},
541 {"right-margin", pos_int_arg, 1},
542 {"top-margin", pos_int_arg, 2},
543 {"bottom-margin", pos_int_arg, 3},
544 {"font-dir", string_arg, 0},
545 {"prologue-file", string_arg, 1},
546 {"device-file", string_arg, 2},
547 {"encoding-file", string_arg, 3},
548 {"prop-font-family", string_arg, 5},
549 {"fixed-font-family", string_arg, 6},
550 {"font-size", pos_int_arg, 4},
551 {"optimize-text-size", nonneg_int_arg, 0},
552 {"optimize-line-size", nonneg_int_arg, 1},
553 {"max-fonts-simult", nonneg_int_arg, 2},
555 {"line-style", 7, 0},
556 {"line-width", dimension_arg, 2},
557 {"line-gutter", dimension_arg, 3},
558 {"line-width", dimension_arg, 4},
559 {"line-width-thick", dimension_arg, 5},
563 static struct outp_option_info option_info;
566 ps_option (struct outp_driver *this, const char *key, const struct string *val)
568 struct ps_driver_ext *x = this->ext;
570 char *value = ds_value (val);
572 cat = outp_match_keyword (key, option_tab, &option_info, &subcat);
577 msg (SE, _("Unknown configuration parameter `%s' for PostScript device "
581 free (x->file.filename);
582 x->file.filename = xstrdup (value);
585 outp_get_paper_size (value, &this->width, &this->length);
588 if (!strcmp (value, "portrait"))
589 x->orientation = OTN_PORTRAIT;
590 else if (!strcmp (value, "landscape"))
591 x->orientation = OTN_LANDSCAPE;
593 msg (SE, _("Unknown orientation `%s'. Valid orientations are "
594 "`portrait' and `landscape'."), value);
597 if (!strcmp (value, "clean7bit") || !strcmp (value, "Clean7Bit"))
598 x->data = ODA_CLEAN7BIT;
599 else if (!strcmp (value, "clean8bit")
600 || !strcmp (value, "Clean8Bit"))
601 x->data = ODA_CLEAN8BIT;
602 else if (!strcmp (value, "binary") || !strcmp (value, "Binary"))
603 x->data = ODA_BINARY;
605 msg (SE, _("Unknown value for `data'. Valid values are `clean7bit', "
606 "`clean8bit', and `binary'."));
609 if (!strcmp (value, "lf"))
610 strcpy (x->eol, "\n");
611 else if (!strcmp (value, "crlf"))
612 strcpy (x->eol, "\r\n");
614 msg (SE, _("Unknown value for `line-ends'. Valid values are `lf' and "
618 if (!strcmp (value, "thick"))
619 x->output_options &= ~OPO_DOUBLE_LINE;
620 else if (!strcmp (value, "double"))
621 x->output_options |= OPO_DOUBLE_LINE;
623 msg (SE, _("Unknown value for `line-style'. Valid values are `thick' "
631 if (!strcmp (value, "on") || !strcmp (value, "true")
632 || !strcmp (value, "yes") || atoi (value))
634 else if (!strcmp (value, "off") || !strcmp (value, "false")
635 || !strcmp (value, "no") || !strcmp (value, "0"))
639 msg (SE, _("Boolean value expected for %s."), key);
651 mask = OPO_MIRROR_HORZ;
654 mask = OPO_MIRROR_VERT;
657 mask = OPO_ROTATE_180;
660 mask = OPO_AUTO_ENCODE;
666 x->output_options |= mask;
668 x->output_options &= ~mask;
677 arg = strtol (value, &tail, 0);
678 if (arg < 1 || errno == ERANGE || *tail)
680 msg (SE, _("Positive integer required as value for `%s'."), key);
683 if ((subcat == 4 || subcat == 5) && arg < 1000)
685 msg (SE, _("Default font size must be at least 1 point (value "
686 "of 1000 for key `%s')."), key);
692 x->left_margin = arg;
695 x->right_margin = arg;
701 x->bottom_margin = arg;
713 int dimension = outp_evaluate_dimension (value, NULL);
717 msg (SE, _("Value for `%s' must be a dimension of positive "
718 "length (i.e., `1in')."), key);
724 x->line_width = dimension;
727 x->line_gutter = dimension;
730 x->line_width = dimension;
733 x->line_width_thick = dimension;
749 dest = &x->prologue_fn;
755 dest = &x->encoding_fn;
758 dest = &x->prop_family;
761 dest = &x->fixed_family;
768 *dest = xstrdup (value);
777 arg = strtol (value, &tail, 0);
778 if (arg < 0 || errno == ERANGE || *tail)
780 msg (SE, _("Nonnegative integer required as value for `%s'."), key);
804 /* Looks for a PostScript font file or config file in all the
805 appropriate places. Returns the filename on success, NULL on
807 /* PORTME: Filename operations. */
809 find_ps_file (struct outp_driver *this, const char *name)
811 struct ps_driver_ext *x = this->ext;
814 /* x->font_dir + name: "devps/ps-encodings". */
817 /* Usually equal to groff_font_path. */
820 /* Final filename. */
824 basename = local_alloc (strlen (x->font_dir) + 1 + strlen (name) + 1);
825 cp = stpcpy (basename, x->font_dir);
826 *cp++ = DIR_SEPARATOR;
829 /* Decide on search path. */
831 const char *pre_pathname;
833 pre_pathname = getenv ("STAT_GROFF_FONT_PATH");
834 if (pre_pathname == NULL)
835 pre_pathname = getenv ("GROFF_FONT_PATH");
836 if (pre_pathname == NULL)
837 pre_pathname = groff_font_path;
838 pathname = fn_tilde_expand (pre_pathname);
841 /* Search all possible places for the file. */
842 fn = fn_search_path (basename, pathname, NULL);
844 fn = fn_search_path (basename, config_path, NULL);
846 fn = fn_search_path (name, pathname, NULL);
848 fn = fn_search_path (name, config_path, NULL);
850 local_free (basename);
857 /* Hash table comparison function for ps_encoding's. */
859 compare_ps_encoding (const void *pa, const void *pb, void *foo unused)
861 const struct ps_encoding *a = pa;
862 const struct ps_encoding *b = pb;
864 return strcmp (a->filename, b->filename);
867 /* Hash table hash function for ps_encoding's. */
869 hash_ps_encoding (const void *pa, void *foo unused)
871 const struct ps_encoding *a = pa;
873 return hsh_hash_string (a->filename);
876 /* Hash table free function for ps_encoding's. */
878 free_ps_encoding (void *pa, void *foo unused)
880 struct ps_encoding *a = pa;
886 /* Iterates through the list of encodings used for this driver
887 instance, reads each of them from disk, and writes them as
888 PostScript code to the output file. */
890 output_encodings (struct outp_driver *this)
892 struct ps_driver_ext *x = this->ext;
894 struct hsh_iterator iter;
895 struct ps_encoding *pe;
897 struct string line, buf;
899 ds_init (NULL, &line, 128);
900 ds_init (NULL, &buf, 128);
901 for (pe = hsh_first (x->encodings, &iter); pe != NULL;
902 pe = hsh_next (x->encodings, &iter))
906 msg (VM (1), _("%s: %s: Opening PostScript font encoding..."),
907 this->name, pe->filename);
909 f = fopen (pe->filename, "r");
912 msg (IE, _("PostScript driver: Cannot open encoding file `%s': %s. "
913 "Substituting ISOLatin1Encoding for missing encoding."),
914 pe->filename, strerror (errno));
915 fprintf (x->file.file, "/E%x ISOLatin1Encoding def%s",
920 struct file_locator where;
922 const char *tab[256];
929 const char *notdef = ".notdef";
933 for (i = 0; i < 256; i++)
936 where.filename = pe->filename;
937 where.line_number = 0;
938 err_push_file_locator (&where);
940 while (ds_get_config_line (f, &buf, &where))
947 pschar = strtok_r (ds_value (&buf), " \t\r\n", &sp);
948 code = strtok_r (NULL, " \t\r\n", &sp);
949 if (*pschar == 0 || *code == 0)
951 code_val = strtol (code, &fubar, 0);
954 msg (IS, _("PostScript driver: Invalid numeric format."));
957 if (code_val < 0 || code_val > 255)
959 msg (IS, _("PostScript driver: Codes must be between 0 "
960 "and 255. (%d is not allowed.)"), code_val);
963 tab[code_val] = local_alloc (strlen (pschar) + 1);
964 strcpy ((char *) (tab[code_val]), pschar);
966 err_pop_file_locator (&where);
969 ds_printf (&line, "/E%x[", pe->index);
970 for (i = 0; i < 257; i++)
976 quote_ps_name (temp, tab[i]);
977 if (tab[i] != notdef)
981 strcpy (temp, "]def");
983 if (ds_length (&line) + strlen (temp) > 70)
985 ds_concat (&line, x->eol);
986 fputs (ds_value (&line), x->file.file);
989 ds_concat (&line, temp);
991 ds_concat (&line, x->eol);
992 fputs (ds_value (&line), x->file.file);
994 if (fclose (f) == EOF)
995 msg (MW, _("PostScript driver: Error closing encoding file `%s'."),
998 msg (VM (2), _("%s: PostScript font encoding read successfully."),
1006 /* Finds the ps_encoding in THIS that corresponds to the file with
1007 name NORM_FILENAME, which must have previously been normalized with
1008 normalize_filename(). */
1009 static struct ps_encoding *
1010 get_encoding (struct outp_driver *this, const char *norm_filename)
1012 struct ps_driver_ext *x = this->ext;
1013 struct ps_encoding *pe;
1015 pe = (struct ps_encoding *) hsh_find (x->encodings, (void *) &norm_filename);
1019 /* Searches the filesystem for an encoding file with name FILENAME;
1020 returns its malloc'd, normalized name if found, otherwise NULL. */
1022 find_encoding_file (struct outp_driver *this, char *filename)
1026 if (filename == NULL)
1028 while (isspace ((unsigned char) *filename))
1030 for (cp = filename; *cp && !isspace ((unsigned char) *cp); cp++)
1036 temp = find_ps_file (this, filename);
1040 filename = fn_normalize (temp);
1041 assert (filename != NULL);
1047 /* Adds the encoding represented by the not-necessarily-normalized
1048 file FILENAME to the list of encodings, if it exists and is not
1049 already in the list. */
1051 add_encoding (struct outp_driver *this, char *filename)
1053 struct ps_driver_ext *x = this->ext;
1054 struct ps_encoding **pe;
1056 filename = find_encoding_file (this, filename);
1060 pe = (struct ps_encoding **) hsh_probe (x->encodings, &filename);
1066 *pe = xmalloc (sizeof **pe);
1067 (*pe)->filename = filename;
1068 (*pe)->index = x->next_encoding++;
1071 /* Finds the file on disk that contains the list of encodings to
1072 include in the output file, then adds those encodings to the list
1075 read_ps_encodings (struct outp_driver *this)
1077 struct ps_driver_ext *x = this->ext;
1079 /* Encodings file. */
1080 char *encoding_fn; /* `ps-encodings' filename. */
1084 struct file_locator where;
1086 /* It's okay if there's no list of encodings; not everyone cares. */
1087 encoding_fn = find_ps_file (this, x->encoding_fn);
1088 if (encoding_fn == NULL)
1092 msg (VM (1), _("%s: %s: Opening PostScript encoding list file."),
1093 this->name, encoding_fn);
1094 f = fopen (encoding_fn, "r");
1097 msg (IE, _("Opening %s: %s."), encoding_fn, strerror (errno));
1101 where.filename = encoding_fn;
1102 where.line_number = 0;
1103 err_push_file_locator (&where);
1105 ds_init (NULL, &line, 128);
1111 if (!ds_get_config_line (f, &line, &where))
1114 msg (ME, _("Reading %s: %s."), encoding_fn, strerror (errno));
1118 add_encoding (this, bp);
1122 err_pop_file_locator (&where);
1124 if (-1 == fclose (f))
1125 msg (MW, _("Closing %s: %s."), encoding_fn, strerror (errno));
1127 msg (VM (2), _("%s: PostScript encoding list file read successfully."), this->name);
1130 /* Creates a default encoding for driver D that can be substituted for
1131 an unavailable encoding. */
1132 struct ps_encoding *
1133 default_encoding (struct outp_driver *d)
1135 struct ps_driver_ext *x = d->ext;
1136 static struct ps_encoding *enc;
1140 enc = xmalloc (sizeof *enc);
1141 enc->filename = xstrdup (_("<<default encoding>>"));
1142 enc->index = x->next_encoding++;
1147 /* Basic file operations. */
1149 /* Variables for the prologue. */
1156 static struct ps_variable *ps_var_tab;
1158 /* Searches ps_var_tab for a ps_variable with key KEY, and returns the
1159 associated value. */
1161 ps_get_var (const char *key)
1163 struct ps_variable *v;
1165 for (v = ps_var_tab; v->key; v++)
1166 if (!strcmp (key, v->key))
1171 /* Writes the PostScript prologue to file F. */
1173 postopen (struct file_ext *f)
1175 static struct ps_variable dict[] =
1177 {"bounding-box", 0},
1186 {"scale-factor", 0},
1188 {"paper-length", 0},
1192 {"line-width-thick", 0},
1197 char boundbox[INT_DIGITS * 4 + 4];
1201 char scaling[INT_DIGITS + 5];
1205 char paper_width[INT_DIGITS + 1];
1206 char paper_length[INT_DIGITS + 1];
1207 char left_margin[INT_DIGITS + 1];
1208 char top_margin[INT_DIGITS + 1];
1209 char line_width[INT_DIGITS + 1];
1210 char line_width_thick[INT_DIGITS + 1];
1212 struct outp_driver *this = f->param;
1213 struct ps_driver_ext *x = this->ext;
1215 char *prologue_fn = find_ps_file (this, x->prologue_fn);
1216 FILE *prologue_file;
1219 size_t buf_size = 0;
1221 x->loaded = hsh_create (31, compare_font_entry, hash_font_entry,
1222 free_font_entry, NULL);
1225 char *font_name = local_alloc (2 + max (strlen (x->prop_family),
1226 strlen (x->fixed_family)));
1228 strcpy (stpcpy (font_name, x->prop_family), "R");
1229 x->prop = load_font (this, font_name);
1231 strcpy (stpcpy (font_name, x->fixed_family), "R");
1232 x->fixed = load_font (this, font_name);
1234 local_free(font_name);
1237 x->current = x->prop;
1238 x->family = xstrdup (x->prop_family);
1239 x->size = x->font_size;
1242 int *h = this->horiz_line_width, *v = this->vert_line_width;
1244 this->cp_x = this->cp_y = 0;
1245 this->font_height = x->font_size;
1247 struct char_metrics *metric;
1249 metric = font_get_char_metrics (x->prop->font, '0');
1250 this->prop_em_width = ((metric
1251 ? metric->width : x->prop->font->space_width)
1252 * x->font_size / 1000);
1254 metric = font_get_char_metrics (x->fixed->font, '0');
1255 this->fixed_width = ((metric
1256 ? metric->width : x->fixed->font->space_width)
1257 * x->font_size / 1000);
1261 h[1] = v[1] = 2 * x->line_gutter + x->line_width;
1262 if (x->output_options & OPO_DOUBLE_LINE)
1263 h[2] = v[2] = 2 * x->line_gutter + 2 * x->line_width + x->line_space;
1265 h[2] = v[2] = 2 * x->line_gutter + x->line_width_thick;
1266 h[3] = v[3] = 2 * x->line_gutter + x->line_width;
1271 for (i = 0; i < (1 << OUTP_L_COUNT); i++)
1275 /* Maximum width of any line type so far. */
1278 for (bit = 0; bit < OUTP_L_COUNT; bit++)
1279 if ((i & (1 << bit)) && h[bit] > max)
1281 this->horiz_line_spacing[i] = this->vert_line_spacing[i] = max;
1286 if (x->output_options & OPO_AUTO_ENCODE)
1288 /* It's okay if this is done more than once since add_encoding()
1289 is idempotent over identical encodings. */
1290 add_encoding (this, x->prop->font->encoding);
1291 add_encoding (this, x->fixed->font->encoding);
1294 x->file_page_number = 0;
1297 if (prologue_fn == NULL)
1299 msg (IE, _("Cannot find PostScript prologue. The use of `-vv' "
1300 "on the command line is suggested as a debugging aid."));
1304 msg (VM (1), _("%s: %s: Opening PostScript prologue..."),
1305 this->name, prologue_fn);
1306 prologue_file = fopen (prologue_fn, "rb");
1307 if (prologue_file == NULL)
1309 fclose (prologue_file);
1311 msg (IE, "%s: %s", prologue_fn, strerror (errno));
1315 sprintf (boundbox, "0 0 %d %d",
1316 x->w / (PSUS / 72) + (x->w % (PSUS / 72) > 0),
1317 x->l / (PSUS / 72) + (x->l % (PSUS / 72) > 0));
1318 dict[0].value = boundbox;
1320 dict[1].value = (char *) version;
1322 curtime = time (NULL);
1323 loctime = localtime (&curtime);
1324 dict[2].value = asctime (loctime);
1325 cp = strchr (dict[2].value, '\n');
1332 dict[3].value = "Clean7Bit";
1335 dict[3].value = "Clean8Bit";
1338 dict[3].value = "Binary";
1344 if (x->orientation == OTN_PORTRAIT)
1345 dict[4].value = "Portrait";
1347 dict[4].value = "Landscape";
1349 /* PORTME: Determine username, net address. */
1351 dict[5].value = getenv ("LOGNAME");
1353 dict[5].value = getlogin ();
1355 dict[5].value = _("nobody");
1357 if (gethostname (host, 128) == -1)
1359 if (errno == ENAMETOOLONG)
1362 strcpy (host, _("nowhere"));
1364 dict[6].value = host;
1365 #else /* !HAVE_UNISTD_H */
1366 dict[5].value = _("nobody");
1367 dict[6].value = _("nowhere");
1368 #endif /* !HAVE_UNISTD_H */
1370 cp = stpcpy (p = local_alloc (288), "font ");
1371 quote_ps_string (cp, x->prop->font->internal_name);
1374 cp = stpcpy (p = local_alloc (288), "font ");
1375 quote_ps_string (cp, x->fixed->font->internal_name);
1378 sprintf (scaling, "%.3f", PSUS / 72.0);
1379 dict[9].value = scaling;
1381 sprintf (paper_width, "%g", x->w / (PSUS / 72.0));
1382 dict[10].value = paper_width;
1384 sprintf (paper_length, "%g", x->l / (PSUS / 72.0));
1385 dict[11].value = paper_length;
1387 sprintf (left_margin, "%d", x->left_margin);
1388 dict[12].value = left_margin;
1390 sprintf (top_margin, "%d", x->top_margin);
1391 dict[13].value = top_margin;
1393 sprintf (line_width, "%d", x->line_width);
1394 dict[14].value = line_width;
1396 sprintf (line_width, "%d", x->line_width_thick);
1397 dict[15].value = line_width_thick;
1399 getl_location (&dict[17].value, NULL);
1400 if (dict[17].value == NULL)
1401 dict[17].value = "<stdin>";
1405 dict[16].value = cp = local_alloc (strlen (dict[17].value) + 30);
1406 sprintf (cp, "PSPP (%s)", dict[17].value);
1410 dict[16].value = local_alloc (strlen (outp_title) + 1);
1411 strcpy ((char *) (dict[16].value), outp_title);
1415 while (-1 != getline (&buf, &buf_size, prologue_file))
1421 cp = strstr (buf, "!eps");
1424 if (this->class->magic == MAGIC_PS)
1431 cp = strstr (buf, "!ps");
1434 if (this->class->magic == MAGIC_EPSF)
1439 if (strstr (buf, "!!!"))
1444 if (!strncmp (buf, "!encodings", 10))
1445 output_encodings (this);
1449 beg = buf2 = fn_interp_vars (buf, ps_get_var);
1450 len = strlen (buf2);
1451 while (isspace (*beg))
1453 if (beg[len - 1] == '\n')
1455 if (beg[len - 1] == '\r')
1457 fwrite (beg, len, 1, f->file);
1458 fputs (x->eol, f->file);
1462 if (ferror (f->file))
1463 msg (IE, _("Reading `%s': %s."), prologue_fn, strerror (errno));
1464 fclose (prologue_file);
1469 local_free (dict[7].value);
1470 local_free (dict[8].value);
1471 local_free (dict[16].value);
1473 if (ferror (f->file))
1476 msg (VM (2), _("%s: PostScript prologue read successfully."), this->name);
1480 msg (VM (1), _("%s: Error reading PostScript prologue."), this->name);
1484 /* Writes the string STRING to buffer DEST (of at least 288
1485 characters) as a PostScript name object. Returns a pointer
1486 to the null terminator of the resultant string. */
1488 quote_ps_name (char *dest, const char *string)
1492 for (sp = string; *sp; sp++)
1493 switch (*(unsigned char *) sp)
1566 for (sp = string; *sp && dp < &dest[256]; sp++)
1568 sprintf (dp, "%02x", *(unsigned char *) sp);
1571 return stpcpy (dp, ">cvn");
1575 return stpcpy (&dest[1], string);
1578 /* Adds the string STRING to buffer DEST as a PostScript quoted
1579 string; returns a pointer to the null terminator added. Will not
1580 add more than 235 characters. */
1582 quote_ps_string (char *dest, const char *string)
1584 const char *sp = string;
1588 for (; *sp && dp < &dest[235]; sp++)
1590 dp = stpcpy (dp, "\\(");
1591 else if (*sp == ')')
1592 dp = stpcpy (dp, "\\)");
1593 else if (*sp < 32 || *((unsigned char *) sp) > 127)
1594 dp = spprintf (dp, "\\%3o", *sp);
1597 return stpcpy (dp, ")");
1600 /* Writes the PostScript epilogue to file F. */
1602 preclose (struct file_ext *f)
1604 struct outp_driver *this = f->param;
1605 struct ps_driver_ext *x = this->ext;
1606 struct hsh_iterator iter;
1607 struct font_entry *fe;
1612 "%%%%DocumentNeededResources:%s"),
1613 x->eol, x->file_page_number, x->eol, x->eol);
1615 for (fe = hsh_first (x->loaded, &iter); fe != NULL;
1616 fe = hsh_next (x->loaded, &iter))
1620 cp = stpcpy (buf, "%%+ font ");
1621 cp = quote_ps_string (cp, fe->font->internal_name);
1622 strcpy (cp, x->eol);
1623 fputs (buf, f->file);
1626 hsh_destroy (x->loaded);
1628 hsh_destroy (x->combos);
1630 x->last_font = NULL;
1633 fprintf (f->file, "%%EOF%s", x->eol);
1634 if (ferror (f->file))
1640 ps_open_page (struct outp_driver *this)
1642 struct ps_driver_ext *x = this->ext;
1644 assert (this->driver_open && !this->page_open);
1647 if (!fn_open_ext (&x->file))
1650 msg (ME, _("PostScript output driver: %s: %s"), x->file.filename,
1654 x->file_page_number++;
1656 hsh_destroy (x->combos);
1657 x->combos = hsh_create (31, compare_ps_combo, hash_ps_combo,
1658 free_ps_combo, NULL);
1659 x->last_font = NULL;
1662 fprintf (x->file.file,
1664 "%%%%BeginPageSetup%s"
1665 "/pg save def 0.001 dup scale%s",
1666 x->page_number, x->file_page_number, x->eol,
1670 if (x->orientation == OTN_LANDSCAPE)
1671 fprintf (x->file.file,
1672 "%d 0 translate 90 rotate%s",
1675 if (x->bottom_margin != 0 || x->left_margin != 0)
1676 fprintf (x->file.file,
1677 "%d %d translate%s",
1678 x->left_margin, x->bottom_margin, x->eol);
1680 fprintf (x->file.file,
1681 "/LW %d def/TW %d def %d setlinewidth%s"
1682 "%%%%EndPageSetup%s",
1683 x->line_width, x->line_width_thick, x->line_width, x->eol,
1686 if (!ferror (x->file.file))
1688 this->page_open = 1;
1689 if (x->output_options & OPO_HEADERS)
1690 draw_headers (this);
1693 return !ferror (x->file.file);
1697 ps_close_page (struct outp_driver *this)
1699 struct ps_driver_ext *x = this->ext;
1701 assert (this->driver_open && this->page_open);
1706 fprintf (x->file.file,
1711 this->page_open = 0;
1712 return !ferror (x->file.file);
1717 /* qsort() comparison function for int tuples. */
1719 int_2_compare (const void *a_, const void *b_)
1724 return *a < *b ? -1 : *a > *b;
1727 /* Hash table comparison function for cached lines. */
1729 compare_line (const void *a_, const void *b_, void *foo unused)
1731 const struct line_form *a = a_;
1732 const struct line_form *b = b_;
1734 return a->ind < b->ind ? -1 : a->ind > b->ind;
1737 /* Hash table hash function for cached lines. */
1739 hash_line (const void *pa, void *foo unused)
1741 const struct line_form *a = pa;
1746 /* Hash table free function for cached lines. */
1748 free_line (void *pa, void *foo unused)
1753 /* Writes PostScript code to draw a line from (x1,y1) to (x2,y2) to
1755 #define dump_line(x1, y1, x2, y2) \
1756 fprintf (ext->file.file, "%d %d %d %d L%s", \
1757 x1, YT (y1), x2, YT (y2), ext->eol)
1759 /* Write PostScript code to draw a thick line from (x1,y1) to (x2,y2)
1760 to the output file. */
1761 #define dump_thick_line(x1, y1, x2, y2) \
1762 fprintf (ext->file.file, "%d %d %d %d TL%s", \
1763 x1, YT (y1), x2, YT (y2), ext->eol)
1765 /* Writes a line of type TYPE to THIS driver's output file. The line
1766 (or its center, in the case of double lines) has its independent
1767 axis coordinate at IND; it extends from DEP1 to DEP2 on the
1770 dump_fancy_line (struct outp_driver *this, int type, int ind, int dep1, int dep2)
1772 struct ps_driver_ext *ext = this->ext;
1773 int ofs = ext->line_space / 2 + ext->line_width / 2;
1778 dump_line (dep1, ind, dep2, ind);
1781 if (ext->output_options & OPO_DOUBLE_LINE)
1783 dump_line (dep1, ind - ofs, dep2, ind - ofs);
1784 dump_line (dep1, ind + ofs, dep2, ind + ofs);
1787 dump_thick_line (dep1, ind, dep2, ind);
1792 dump_line (ind, dep1, ind, dep2);
1795 if (ext->output_options & OPO_DOUBLE_LINE)
1797 dump_line (ind - ofs, dep1, ind - ofs, dep2);
1798 dump_line (ind + ofs, dep1, ind + ofs, dep2);
1801 dump_thick_line (ind, dep1, ind, dep2);
1812 /* Writes all the cached lines to the output file, then clears the
1815 dump_lines (struct outp_driver *this)
1817 struct ps_driver_ext *x = this->ext;
1819 struct hsh_iterator iter;
1822 for (type = 0; type < n_line_types; type++)
1824 struct line_form *line;
1826 if (x->lines[type] == NULL)
1829 for (line = hsh_first (x->lines[type], &iter); line != NULL;
1830 line = hsh_next (x->lines[type], &iter))
1833 int lo = INT_MIN, hi;
1835 qsort (line->dep, line->ndep, sizeof *line->dep, int_2_compare);
1836 lo = line->dep[0][0];
1837 hi = line->dep[0][1];
1838 for (i = 1; i < line->ndep; i++)
1839 if (line->dep[i][0] <= hi + 1)
1841 int min_hi = line->dep[i][1];
1847 dump_fancy_line (this, type, line->ind, lo, hi);
1848 lo = line->dep[i][0];
1849 hi = line->dep[i][1];
1851 dump_fancy_line (this, type, line->ind, lo, hi);
1854 hsh_destroy (x->lines[type]);
1855 x->lines[type] = NULL;
1859 /* (Same args as dump_fancy_line()). Either dumps the line directly
1860 to the output file, or adds it to the cache, depending on the
1861 user-selected line optimization mode. */
1863 line (struct outp_driver *this, int type, int ind, int dep1, int dep2)
1865 struct ps_driver_ext *ext = this->ext;
1866 struct line_form **f;
1868 assert (dep2 >= dep1);
1869 if (ext->line_opt == 0)
1871 dump_fancy_line (this, type, ind, dep1, dep2);
1875 if (ext->lines[type] == NULL)
1876 ext->lines[type] = hsh_create (31, compare_line, hash_line,
1878 f = (struct line_form **) hsh_probe (ext->lines[type], &ind);
1881 *f = xmalloc (sizeof **f + sizeof (int[15][2]));
1885 (*f)->dep[0][0] = dep1;
1886 (*f)->dep[0][1] = dep2;
1889 if ((*f)->ndep >= (*f)->mdep)
1892 *f = xrealloc (*f, (sizeof **f + sizeof (int[2]) * ((*f)->mdep - 1)));
1894 (*f)->dep[(*f)->ndep][0] = dep1;
1895 (*f)->dep[(*f)->ndep][1] = dep2;
1900 ps_line_horz (struct outp_driver *this, const struct rect *r,
1901 const struct color *c unused, int style)
1903 /* Must match output.h:OUTP_L_*. */
1904 static const int types[OUTP_L_COUNT] =
1905 {-1, horz, dbl_horz, spl_horz};
1907 int y = (r->y1 + r->y2) / 2;
1909 assert (this->driver_open && this->page_open);
1910 assert (style >= 0 && style < OUTP_L_COUNT);
1911 style = types[style];
1913 line (this, style, y, r->x1, r->x2);
1917 ps_line_vert (struct outp_driver *this, const struct rect *r,
1918 const struct color *c unused, int style)
1920 /* Must match output.h:OUTP_L_*. */
1921 static const int types[OUTP_L_COUNT] =
1922 {-1, vert, dbl_vert, spl_vert};
1924 int x = (r->x1 + r->x2) / 2;
1926 assert (this->driver_open && this->page_open);
1927 assert (style >= 0 && style < OUTP_L_COUNT);
1928 style = types[style];
1930 line (this, style, x, r->y1, r->y2);
1933 #define L (style->l != OUTP_L_NONE)
1934 #define R (style->r != OUTP_L_NONE)
1935 #define T (style->t != OUTP_L_NONE)
1936 #define B (style->b != OUTP_L_NONE)
1939 ps_line_intersection (struct outp_driver *this, const struct rect *r,
1940 const struct color *c unused,
1941 const struct outp_styles *style)
1943 struct ps_driver_ext *ext = this->ext;
1945 int x = (r->x1 + r->x2) / 2;
1946 int y = (r->y1 + r->y2) / 2;
1947 int ofs = (ext->line_space + ext->line_width) / 2;
1948 int x1 = x - ofs, x2 = x + ofs;
1949 int y1 = y - ofs, y2 = y + ofs;
1951 assert (this->driver_open && this->page_open);
1952 assert (!((style->l != style->r && style->l != OUTP_L_NONE
1953 && style->r != OUTP_L_NONE)
1954 || (style->t != style->b && style->t != OUTP_L_NONE
1955 && style->b != OUTP_L_NONE)));
1957 switch ((style->l | style->r) | ((style->t | style->b) << 8))
1959 case (OUTP_L_SINGLE) | (OUTP_L_SINGLE << 8):
1960 case (OUTP_L_SINGLE) | (OUTP_L_NONE << 8):
1961 case (OUTP_L_NONE) | (OUTP_L_SINGLE << 8):
1963 line (this, horz, y, r->x1, x);
1965 line (this, horz, y, x, r->x2);
1967 line (this, vert, x, r->y1, y);
1969 line (this, vert, x, y, r->y2);
1971 case (OUTP_L_SINGLE) | (OUTP_L_DOUBLE << 8):
1972 case (OUTP_L_NONE) | (OUTP_L_DOUBLE << 8):
1974 line (this, horz, y, r->x1, x1);
1976 line (this, horz, y, x2, r->x2);
1978 line (this, dbl_vert, x, r->y1, y);
1980 line (this, dbl_vert, x, y, r->y2);
1981 if ((L && R) && !(T && B))
1982 line (this, horz, y, x1, x2);
1984 case (OUTP_L_DOUBLE) | (OUTP_L_SINGLE << 8):
1985 case (OUTP_L_DOUBLE) | (OUTP_L_NONE << 8):
1987 line (this, dbl_horz, y, r->x1, x);
1989 line (this, dbl_horz, y, x, r->x2);
1991 line (this, vert, x, r->y1, y);
1993 line (this, vert, x, y, r->y2);
1994 if ((T && B) && !(L && R))
1995 line (this, vert, x, y1, y2);
1997 case (OUTP_L_DOUBLE) | (OUTP_L_DOUBLE << 8):
1999 line (this, dbl_horz, y, r->x1, x);
2001 line (this, dbl_horz, y, x, r->x2);
2003 line (this, dbl_vert, x, r->y1, y);
2005 line (this, dbl_vert, x, y, r->y2);
2007 line (this, vert, x1, y1, y2);
2009 line (this, vert, x2, y1, y2);
2011 line (this, horz, y1, x1, x2);
2013 line (this, horz, y2, x1, x2);
2021 ps_box (struct outp_driver *this unused, const struct rect *r unused,
2022 const struct color *bord unused, const struct color *fill unused)
2024 assert (this->driver_open && this->page_open);
2028 ps_polyline_begin (struct outp_driver *this unused,
2029 const struct color *c unused)
2031 assert (this->driver_open && this->page_open);
2034 ps_polyline_point (struct outp_driver *this unused, int x unused, int y unused)
2036 assert (this->driver_open && this->page_open);
2039 ps_polyline_end (struct outp_driver *this unused)
2041 assert (this->driver_open && this->page_open);
2044 /* Returns the width of string S for THIS driver. */
2046 text_width (struct outp_driver *this, char *s)
2048 struct outp_text text;
2050 text.options = OUTP_T_JUST_LEFT;
2051 ls_init (&text.s, s, strlen (s));
2052 this->class->text_metrics (this, &text);
2056 /* Write string S at location (X,Y) with width W for THIS driver. */
2058 out_text_plain (struct outp_driver *this, char *s, int x, int y, int w)
2060 struct outp_text text;
2062 text.options = OUTP_T_JUST_LEFT | OUTP_T_HORZ | OUTP_T_VERT;
2063 ls_init (&text.s, s, strlen (s));
2065 text.v = this->font_height;
2068 this->class->text_draw (this, &text);
2071 /* Draw top of page headers for THIS driver. */
2073 draw_headers (struct outp_driver *this)
2075 struct ps_driver_ext *ext = this->ext;
2077 struct font_entry *old_current = ext->current;
2078 char *old_family = xstrdup (ext->family); /* FIXME */
2079 int old_size = ext->size;
2081 int fh = this->font_height;
2084 fprintf (ext->file.file, "%d %d %d %d GB%s",
2085 0, YT (y), this->width, YT (y + 2 * fh + ext->line_gutter),
2087 this->class->text_set_font_family (this, "T");
2089 y += ext->line_width + ext->line_gutter;
2095 sprintf (buf, _("%s - Page %d"), curdate, ext->page_number);
2096 rh_width = text_width (this, buf);
2098 out_text_plain (this, buf, this->width - this->prop_em_width - rh_width,
2101 if (outp_title && outp_subtitle)
2102 out_text_plain (this, outp_title, this->prop_em_width, y,
2103 this->width - 3 * this->prop_em_width - rh_width);
2111 char *string = outp_subtitle ? outp_subtitle : outp_title;
2113 sprintf (buf, "%s - %s", version, host_system);
2114 rh_width = text_width (this, buf);
2116 out_text_plain (this, buf, this->width - this->prop_em_width - rh_width,
2120 out_text_plain (this, string, this->prop_em_width, y,
2121 this->width - 3 * this->prop_em_width - rh_width);
2126 ext->current = old_current;
2128 ext->family = old_family;
2129 ext->size = old_size;
2136 ps_text_set_font_by_name (struct outp_driver *this, const char *dit)
2138 struct ps_driver_ext *x = this->ext;
2139 struct font_entry *fe;
2141 assert (this->driver_open && this->page_open);
2143 /* Short-circuit common fonts. */
2144 if (!strcmp (dit, "PROP"))
2146 x->current = x->prop;
2147 x->size = x->font_size;
2150 else if (!strcmp (dit, "FIXED"))
2152 x->current = x->fixed;
2153 x->size = x->font_size;
2157 /* Find font_desc corresponding to Groff name dit. */
2158 fe = hsh_find (x->loaded, &dit);
2160 fe = load_font (this, dit);
2165 ps_text_set_font_by_position (struct outp_driver *this, int pos)
2167 struct ps_driver_ext *x = this->ext;
2170 assert (this->driver_open && this->page_open);
2172 /* Determine font name by suffixing position string to font family
2177 dit = local_alloc (strlen (x->family) + 3);
2178 cp = stpcpy (dit, x->family);
2200 /* Find font_desc corresponding to Groff name dit. */
2202 struct font_entry *fe = hsh_find (x->loaded, &dit);
2204 fe = load_font (this, dit);
2212 ps_text_set_font_family (struct outp_driver *this, const char *s)
2214 struct ps_driver_ext *x = this->ext;
2216 assert (this->driver_open && this->page_open);
2219 x->family = xstrdup (s);
2223 ps_text_get_font_name (struct outp_driver *this)
2225 struct ps_driver_ext *x = this->ext;
2227 assert (this->driver_open && this->page_open);
2228 return x->current->font->name;
2232 ps_text_get_font_family (struct outp_driver *this)
2234 struct ps_driver_ext *x = this->ext;
2236 assert (this->driver_open && this->page_open);
2241 ps_text_set_size (struct outp_driver *this, int size)
2243 struct ps_driver_ext *x = this->ext;
2245 assert (this->driver_open && this->page_open);
2246 x->size = PSUS / 72000 * size;
2251 ps_text_get_size (struct outp_driver *this, int *em_width)
2253 struct ps_driver_ext *x = this->ext;
2255 assert (this->driver_open && this->page_open);
2257 *em_width = (x->current->font->space_width * x->size) / 1000;
2258 return x->size / (PSUS / 72000);
2261 /* An output character. */
2264 struct font_entry *font; /* Font of character. */
2265 int size; /* Size of character. */
2266 int x, y; /* Location of character. */
2267 unsigned char ch; /* Character. */
2268 char separate; /* Must be separate from previous char. */
2271 /* Hash table comparison function for ps_combo structs. */
2273 compare_ps_combo (const void *pa, const void *pb, void *foo unused)
2275 const struct ps_font_combo *a = pa;
2276 const struct ps_font_combo *b = pb;
2278 return !((a->font == b->font) && (a->size == b->size));
2281 /* Hash table hash function for ps_combo structs. */
2283 hash_ps_combo (const void *pa, void *foo unused)
2285 const struct ps_font_combo *a = pa;
2286 unsigned name_hash = hsh_hash_string (a->font->font->internal_name);
2287 return name_hash ^ hsh_hash_int (a->size);
2290 /* Hash table free function for ps_combo structs. */
2292 free_ps_combo (void *a, void *foo unused)
2297 /* Causes PostScript code to be output that switches to the font
2298 CP->FONT and font size CP->SIZE. The first time a particular
2299 font/size combination is used on a particular page, this involves
2300 outputting PostScript code to load the font. */
2302 switch_font (struct outp_driver *this, const struct output_char *cp)
2304 struct ps_driver_ext *ext = this->ext;
2305 struct ps_font_combo srch, **fc;
2307 srch.font = cp->font;
2308 srch.size = cp->size;
2310 fc = (struct ps_font_combo **) hsh_probe (ext->combos, &srch);
2313 fprintf (ext->file.file, "F%x%s", (*fc)->index, ext->eol);
2318 struct ps_encoding *encoding;
2321 *fc = xmalloc (sizeof **fc);
2322 (*fc)->font = cp->font;
2323 (*fc)->size = cp->size;
2324 (*fc)->index = ext->next_combo++;
2326 filename = find_encoding_file (this, cp->font->font->encoding);
2329 encoding = get_encoding (this, filename);
2334 msg (IE, _("PostScript driver: Cannot find encoding `%s' for "
2335 "PostScript font `%s'."), cp->font->font->encoding,
2336 cp->font->font->internal_name);
2337 encoding = default_encoding (this);
2340 if (cp->font != ext->fixed && cp->font != ext->prop)
2342 bp = stpcpy (buf, "%%IncludeResource: font ");
2343 bp = quote_ps_string (bp, cp->font->font->internal_name);
2344 bp = stpcpy (bp, ext->eol);
2349 bp = spprintf (bp, "/F%x E%x %d", (*fc)->index, encoding->index,
2351 bp = quote_ps_name (bp, cp->font->font->internal_name);
2352 sprintf (bp, " SF%s", ext->eol);
2353 fputs (buf, ext->file.file);
2355 ext->last_font = *fc;
2358 /* (write_text) Writes the accumulated line buffer to the output
2360 #define output_line() \
2363 lp = stpcpy (lp, ext->eol); \
2365 fputs (line, ext->file.file); \
2370 /* (write_text) Adds the string representing number X to the line
2371 buffer, flushing the buffer to disk beforehand if necessary. */
2372 #define put_number(X) \
2375 int n = nsprintf (number, "%d", X); \
2376 if (n + lp > &line[75]) \
2378 lp = stpcpy (lp, number); \
2382 /* Outputs PostScript code to THIS driver's output file to display the
2383 characters represented by the output_char's between CP and END,
2384 using the associated outp_text T to determine formatting. WIDTH is
2385 the width of the output region; WIDTH_LEFT is the amount of the
2386 WIDTH that is not taken up by text (so it can be used to determine
2389 write_text (struct outp_driver *this,
2390 const struct output_char *cp, const struct output_char *end,
2391 struct outp_text *t, int width unused, int width_left)
2393 struct ps_driver_ext *ext = this->ext;
2398 char number[INT_DIGITS + 1];
2402 switch (t->options & OUTP_T_JUST_MASK)
2404 case OUTP_T_JUST_LEFT:
2407 case OUTP_T_JUST_RIGHT:
2410 case OUTP_T_JUST_CENTER:
2411 ofs = width_left / 2;
2419 int x = cp->x + ofs;
2420 int y = cp->y + (cp->font->font->ascent * cp->size / 1000);
2422 if (ext->last_font == NULL
2423 || cp->font != ext->last_font->font
2424 || cp->size != ext->last_font->size)
2425 switch_font (this, cp);
2431 static unsigned char literal_chars[ODA_COUNT][32] =
2433 {0x00, 0x00, 0x00, 0xf8, 0xff, 0xfc, 0xff, 0xff,
2434 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f,
2435 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2436 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2438 {0x00, 0x00, 0x00, 0xf8, 0xff, 0xfc, 0xff, 0xff,
2439 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
2440 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
2441 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
2443 {0x7e, 0xd6, 0xff, 0xfb, 0xff, 0xfc, 0xff, 0xff,
2444 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
2445 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
2446 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
2450 if (TEST_BIT (literal_chars[ext->data], cp->ch))
2456 lp = stpcpy (lp, "\\(");
2459 lp = stpcpy (lp, "\\)");
2462 lp = spprintf (lp, "\\%03o", cp->ch);
2467 while (cp < end && lp < &line[70] && cp->separate == 0);
2475 put_number (YT (y));
2486 if (lp >= &line[70])
2496 /* Displays the text in outp_text T, if DRAW is nonzero; or, merely
2497 determine the text metrics, if DRAW is zero. */
2499 text (struct outp_driver *this, struct outp_text *t, int draw)
2501 struct ps_driver_ext *ext = this->ext;
2504 struct output_char *buf; /* Output buffer. */
2505 struct output_char *buf_end; /* End of output buffer. */
2506 struct output_char *buf_loc; /* Current location in output buffer. */
2509 struct font_entry *old_current = ext->current;
2510 char *old_family = xstrdup (ext->family); /* FIXME */
2511 int old_size = ext->size;
2516 /* Current location. */
2519 /* Keeping track of what's left over. */
2520 int width; /* Width available for characters. */
2521 int width_left, height_left; /* Width, height left over. */
2522 int max_height; /* Tallest character on this line so far. */
2524 /* Previous character. */
2527 /* Information about location of previous space. */
2528 char *space_char; /* Character after space. */
2529 struct output_char *space_buf_loc; /* Buffer location after space. */
2530 int space_width_left; /* Width of characters before space. */
2532 /* Name of the current character. */
2533 const char *char_name;
2534 char local_char_name[2] = {0, 0};
2536 local_char_name[0] = local_char_name[1] = 0;
2538 buf = local_alloc (sizeof *buf * 128);
2539 buf_end = &buf[128];
2542 assert (!ls_null_p (&t->s));
2543 cp = ls_value (&t->s);
2544 end = ls_end (&t->s);
2550 width = width_left = (t->options & OUTP_T_HORZ) ? t->h : INT_MAX;
2551 height_left = (t->options & OUTP_T_VERT) ? t->v : INT_MAX;
2556 if (!width || !height_left)
2561 struct char_metrics *metric;
2567 /* Set char_name to the name of the character or ligature at
2569 if (ext->current->font->ligatures && *cp == 'f')
2577 lig = LIG_fi, char_name = "fi";
2580 lig = LIG_fl, char_name = "fl";
2587 lig = LIG_ffi, char_name = "ffi";
2590 lig = LIG_ffl, char_name = "ffl";
2593 lig = LIG_ff, char_name = "ff";
2597 if ((lig & ext->current->font->ligatures) == 0)
2599 local_char_name[0] = *cp++; /* 'f' */
2600 char_name = local_char_name;
2603 cp += strlen (char_name);
2605 else if (*cp == '\n')
2609 write_text (this, buf, buf_loc, t, width, width_left);
2616 height_left -= max_height;
2622 /* FIXME: when we're page buffering it will be necessary to
2623 set separate to 1. */
2628 local_char_name[0] = *cp++;
2629 char_name = local_char_name;
2632 /* Figure out what size this character is, and what kern
2633 adjustment we need. */
2634 cur_char = font_char_name_to_index (char_name);
2635 metric = font_get_char_metrics (ext->current->font, cur_char);
2638 static struct char_metrics m;
2640 m.width = ext->current->font->space_width;
2641 m.code = *char_name;
2643 kern_amt = font_get_kern_adjust (ext->current->font, prev_char,
2647 kern_amt = (kern_amt * ext->size / 1000);
2650 char_width = metric->width * ext->size / 1000;
2652 /* Record the current status if this is a space character. */
2653 if (cur_char == space_index && buf_loc > buf)
2656 space_buf_loc = buf_loc;
2657 space_width_left = width_left;
2660 /* Drop down to a new line if there's no room left on this
2662 if (char_width + kern_amt > width_left)
2664 /* Regress to previous space, if any. */
2668 width_left = space_width_left;
2669 buf_loc = space_buf_loc;
2674 write_text (this, buf, buf_loc, t, width, width_left);
2681 height_left -= max_height;
2689 /* FIXME: when we're page buffering it will be
2690 necessary to set separate to 1. */
2695 if (ext->size > max_height)
2696 max_height = ext->size;
2697 if (max_height > height_left)
2700 /* Actually draw the character. */
2703 if (buf_loc >= buf_end)
2705 int buf_len = buf_end - buf;
2709 struct output_char *new_buf;
2711 new_buf = xmalloc (sizeof *new_buf * 256);
2712 memcpy (new_buf, buf, sizeof *new_buf * 128);
2713 buf_loc = new_buf + 128;
2714 buf_end = new_buf + 256;
2720 buf = xrealloc (buf, sizeof *buf * buf_len * 2);
2721 buf_loc = buf + buf_len;
2722 buf_end = buf + buf_len * 2;
2727 buf_loc->font = ext->current;
2728 buf_loc->size = ext->size;
2731 buf_loc->ch = metric->code;
2732 buf_loc->separate = separate;
2737 /* Prepare for next iteration. */
2738 width_left -= char_width + kern_amt;
2739 prev_char = cur_char;
2741 height_left -= max_height;
2742 if (buf_loc > buf && draw)
2743 write_text (this, buf, buf_loc, t, width, width_left);
2746 if (!(t->options & OUTP_T_HORZ))
2747 t->h = INT_MAX - width_left;
2748 if (!(t->options & OUTP_T_VERT))
2749 t->v = INT_MAX - height_left;
2751 t->v -= height_left;
2752 if (buf_end - buf == 128)
2756 ext->current = old_current;
2758 ext->family = old_family;
2759 ext->size = old_size;
2763 ps_text_metrics (struct outp_driver *this, struct outp_text *t)
2765 assert (this->driver_open && this->page_open);
2770 ps_text_draw (struct outp_driver *this, struct outp_text *t)
2772 assert (this->driver_open && this->page_open);
2778 /* Translate a filename to a font. */
2779 struct filename2font
2781 char *filename; /* Normalized filename. */
2782 struct font_desc *font;
2785 /* Table of `filename2font's. */
2786 static struct hsh_table *ps_fonts;
2788 /* Hash table comparison function for filename2font structs. */
2790 compare_filename2font (const void *a, const void *b, void *param unused)
2792 return strcmp (((struct filename2font *) a)->filename,
2793 ((struct filename2font *) b)->filename);
2796 /* Hash table hash function for filename2font structs. */
2798 hash_filename2font (const void *f2f_, void *param unused)
2800 const struct filename2font *f2f = f2f_;
2801 return hsh_hash_string (f2f->filename);
2804 /* Initializes the global font list by creating the hash table for
2805 translation of filenames to font_desc structs. */
2809 ps_fonts = hsh_create (31, compare_filename2font, hash_filename2font,
2813 /* Loads the font having Groff name DIT into THIS driver instance.
2814 Specifically, adds it into the THIS driver's `loaded' hash
2816 static struct font_entry *
2817 load_font (struct outp_driver *this, const char *dit)
2819 struct ps_driver_ext *x = this->ext;
2820 char *filename1, *filename2;
2822 struct font_entry *fe;
2824 filename1 = find_ps_file (this, dit);
2826 filename1 = xstrdup (dit);
2827 filename2 = fn_normalize (filename1);
2830 entry = hsh_probe (ps_fonts, &filename2);
2833 struct filename2font *f2f;
2834 struct font_desc *f = groff_read_font (filename2);
2841 f = default_font ();
2844 f2f = xmalloc (sizeof *f2f);
2845 f2f->filename = filename2;
2852 fe = xmalloc (sizeof *fe);
2853 fe->dit = xstrdup (dit);
2854 fe->font = ((struct filename2font *) * entry)->font;
2855 *hsh_probe (x->loaded, &dit) = fe;
2860 /* PostScript driver class. */
2861 struct outp_class postscript_class =
2883 ps_line_intersection,
2890 ps_text_set_font_by_name,
2891 ps_text_set_font_by_position,
2892 ps_text_set_font_family,
2893 ps_text_get_font_name,
2894 ps_text_get_font_family,
2901 /* EPSF driver class. FIXME: Probably doesn't work right. */
2902 struct outp_class epsf_class =
2924 ps_line_intersection,
2931 ps_text_set_font_by_name,
2932 ps_text_set_font_by_position,
2933 ps_text_set_font_family,
2934 ps_text_get_font_name,
2935 ps_text_get_font_family,
2942 #endif /* NO_POSTSCRIPT */