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);
1109 if (!ds_get_config_line (f, &line, &where))
1112 msg (ME, _("Reading %s: %s."), encoding_fn, strerror (errno));
1116 add_encoding (this, line.string);
1120 err_pop_file_locator (&where);
1122 if (-1 == fclose (f))
1123 msg (MW, _("Closing %s: %s."), encoding_fn, strerror (errno));
1125 msg (VM (2), _("%s: PostScript encoding list file read successfully."), this->name);
1128 /* Creates a default encoding for driver D that can be substituted for
1129 an unavailable encoding. */
1130 struct ps_encoding *
1131 default_encoding (struct outp_driver *d)
1133 struct ps_driver_ext *x = d->ext;
1134 static struct ps_encoding *enc;
1138 enc = xmalloc (sizeof *enc);
1139 enc->filename = xstrdup (_("<<default encoding>>"));
1140 enc->index = x->next_encoding++;
1145 /* Basic file operations. */
1147 /* Variables for the prologue. */
1154 static struct ps_variable *ps_var_tab;
1156 /* Searches ps_var_tab for a ps_variable with key KEY, and returns the
1157 associated value. */
1159 ps_get_var (const char *key)
1161 struct ps_variable *v;
1163 for (v = ps_var_tab; v->key; v++)
1164 if (!strcmp (key, v->key))
1169 /* Writes the PostScript prologue to file F. */
1171 postopen (struct file_ext *f)
1173 static struct ps_variable dict[] =
1175 {"bounding-box", 0},
1184 {"scale-factor", 0},
1186 {"paper-length", 0},
1190 {"line-width-thick", 0},
1195 char boundbox[INT_DIGITS * 4 + 4];
1199 char scaling[INT_DIGITS + 5];
1203 char paper_width[INT_DIGITS + 1];
1204 char paper_length[INT_DIGITS + 1];
1205 char left_margin[INT_DIGITS + 1];
1206 char top_margin[INT_DIGITS + 1];
1207 char line_width[INT_DIGITS + 1];
1208 char line_width_thick[INT_DIGITS + 1];
1210 struct outp_driver *this = f->param;
1211 struct ps_driver_ext *x = this->ext;
1213 char *prologue_fn = find_ps_file (this, x->prologue_fn);
1214 FILE *prologue_file;
1217 size_t buf_size = 0;
1219 x->loaded = hsh_create (31, compare_font_entry, hash_font_entry,
1220 free_font_entry, NULL);
1223 char *font_name = local_alloc (2 + max (strlen (x->prop_family),
1224 strlen (x->fixed_family)));
1226 strcpy (stpcpy (font_name, x->prop_family), "R");
1227 x->prop = load_font (this, font_name);
1229 strcpy (stpcpy (font_name, x->fixed_family), "R");
1230 x->fixed = load_font (this, font_name);
1232 local_free(font_name);
1235 x->current = x->prop;
1236 x->family = xstrdup (x->prop_family);
1237 x->size = x->font_size;
1240 int *h = this->horiz_line_width, *v = this->vert_line_width;
1242 this->cp_x = this->cp_y = 0;
1243 this->font_height = x->font_size;
1245 struct char_metrics *metric;
1247 metric = font_get_char_metrics (x->prop->font, '0');
1248 this->prop_em_width = ((metric
1249 ? metric->width : x->prop->font->space_width)
1250 * x->font_size / 1000);
1252 metric = font_get_char_metrics (x->fixed->font, '0');
1253 this->fixed_width = ((metric
1254 ? metric->width : x->fixed->font->space_width)
1255 * x->font_size / 1000);
1259 h[1] = v[1] = 2 * x->line_gutter + x->line_width;
1260 if (x->output_options & OPO_DOUBLE_LINE)
1261 h[2] = v[2] = 2 * x->line_gutter + 2 * x->line_width + x->line_space;
1263 h[2] = v[2] = 2 * x->line_gutter + x->line_width_thick;
1264 h[3] = v[3] = 2 * x->line_gutter + x->line_width;
1269 for (i = 0; i < (1 << OUTP_L_COUNT); i++)
1273 /* Maximum width of any line type so far. */
1276 for (bit = 0; bit < OUTP_L_COUNT; bit++)
1277 if ((i & (1 << bit)) && h[bit] > max)
1279 this->horiz_line_spacing[i] = this->vert_line_spacing[i] = max;
1284 if (x->output_options & OPO_AUTO_ENCODE)
1286 /* It's okay if this is done more than once since add_encoding()
1287 is idempotent over identical encodings. */
1288 add_encoding (this, x->prop->font->encoding);
1289 add_encoding (this, x->fixed->font->encoding);
1292 x->file_page_number = 0;
1295 if (prologue_fn == NULL)
1297 msg (IE, _("Cannot find PostScript prologue. The use of `-vv' "
1298 "on the command line is suggested as a debugging aid."));
1302 msg (VM (1), _("%s: %s: Opening PostScript prologue..."),
1303 this->name, prologue_fn);
1304 prologue_file = fopen (prologue_fn, "rb");
1305 if (prologue_file == NULL)
1307 fclose (prologue_file);
1309 msg (IE, "%s: %s", prologue_fn, strerror (errno));
1313 sprintf (boundbox, "0 0 %d %d",
1314 x->w / (PSUS / 72) + (x->w % (PSUS / 72) > 0),
1315 x->l / (PSUS / 72) + (x->l % (PSUS / 72) > 0));
1316 dict[0].value = boundbox;
1318 dict[1].value = (char *) version;
1320 curtime = time (NULL);
1321 loctime = localtime (&curtime);
1322 dict[2].value = asctime (loctime);
1323 cp = strchr (dict[2].value, '\n');
1330 dict[3].value = "Clean7Bit";
1333 dict[3].value = "Clean8Bit";
1336 dict[3].value = "Binary";
1342 if (x->orientation == OTN_PORTRAIT)
1343 dict[4].value = "Portrait";
1345 dict[4].value = "Landscape";
1347 /* PORTME: Determine username, net address. */
1349 dict[5].value = getenv ("LOGNAME");
1351 dict[5].value = getlogin ();
1353 dict[5].value = _("nobody");
1355 if (gethostname (host, 128) == -1)
1357 if (errno == ENAMETOOLONG)
1360 strcpy (host, _("nowhere"));
1362 dict[6].value = host;
1363 #else /* !HAVE_UNISTD_H */
1364 dict[5].value = _("nobody");
1365 dict[6].value = _("nowhere");
1366 #endif /* !HAVE_UNISTD_H */
1368 cp = stpcpy (p = local_alloc (288), "font ");
1369 quote_ps_string (cp, x->prop->font->internal_name);
1372 cp = stpcpy (p = local_alloc (288), "font ");
1373 quote_ps_string (cp, x->fixed->font->internal_name);
1376 sprintf (scaling, "%.3f", PSUS / 72.0);
1377 dict[9].value = scaling;
1379 sprintf (paper_width, "%g", x->w / (PSUS / 72.0));
1380 dict[10].value = paper_width;
1382 sprintf (paper_length, "%g", x->l / (PSUS / 72.0));
1383 dict[11].value = paper_length;
1385 sprintf (left_margin, "%d", x->left_margin);
1386 dict[12].value = left_margin;
1388 sprintf (top_margin, "%d", x->top_margin);
1389 dict[13].value = top_margin;
1391 sprintf (line_width, "%d", x->line_width);
1392 dict[14].value = line_width;
1394 sprintf (line_width, "%d", x->line_width_thick);
1395 dict[15].value = line_width_thick;
1397 getl_location (&dict[17].value, NULL);
1398 if (dict[17].value == NULL)
1399 dict[17].value = "<stdin>";
1403 dict[16].value = cp = local_alloc (strlen (dict[17].value) + 30);
1404 sprintf (cp, "PSPP (%s)", dict[17].value);
1408 dict[16].value = local_alloc (strlen (outp_title) + 1);
1409 strcpy ((char *) (dict[16].value), outp_title);
1413 while (-1 != getline (&buf, &buf_size, prologue_file))
1419 cp = strstr (buf, "!eps");
1422 if (this->class->magic == MAGIC_PS)
1429 cp = strstr (buf, "!ps");
1432 if (this->class->magic == MAGIC_EPSF)
1437 if (strstr (buf, "!!!"))
1442 if (!strncmp (buf, "!encodings", 10))
1443 output_encodings (this);
1447 beg = buf2 = fn_interp_vars (buf, ps_get_var);
1448 len = strlen (buf2);
1449 while (isspace (*beg))
1451 if (beg[len - 1] == '\n')
1453 if (beg[len - 1] == '\r')
1455 fwrite (beg, len, 1, f->file);
1456 fputs (x->eol, f->file);
1460 if (ferror (f->file))
1461 msg (IE, _("Reading `%s': %s."), prologue_fn, strerror (errno));
1462 fclose (prologue_file);
1467 local_free (dict[7].value);
1468 local_free (dict[8].value);
1469 local_free (dict[16].value);
1471 if (ferror (f->file))
1474 msg (VM (2), _("%s: PostScript prologue read successfully."), this->name);
1478 msg (VM (1), _("%s: Error reading PostScript prologue."), this->name);
1482 /* Writes the string STRING to buffer DEST (of at least 288
1483 characters) as a PostScript name object. Returns a pointer
1484 to the null terminator of the resultant string. */
1486 quote_ps_name (char *dest, const char *string)
1490 for (sp = string; *sp; sp++)
1491 switch (*(unsigned char *) sp)
1564 for (sp = string; *sp && dp < &dest[256]; sp++)
1566 sprintf (dp, "%02x", *(unsigned char *) sp);
1569 return stpcpy (dp, ">cvn");
1573 return stpcpy (&dest[1], string);
1576 /* Adds the string STRING to buffer DEST as a PostScript quoted
1577 string; returns a pointer to the null terminator added. Will not
1578 add more than 235 characters. */
1580 quote_ps_string (char *dest, const char *string)
1582 const char *sp = string;
1586 for (; *sp && dp < &dest[235]; sp++)
1588 dp = stpcpy (dp, "\\(");
1589 else if (*sp == ')')
1590 dp = stpcpy (dp, "\\)");
1591 else if (*sp < 32 || *((unsigned char *) sp) > 127)
1592 dp = spprintf (dp, "\\%3o", *sp);
1595 return stpcpy (dp, ")");
1598 /* Writes the PostScript epilogue to file F. */
1600 preclose (struct file_ext *f)
1602 struct outp_driver *this = f->param;
1603 struct ps_driver_ext *x = this->ext;
1604 struct hsh_iterator iter;
1605 struct font_entry *fe;
1610 "%%%%DocumentNeededResources:%s"),
1611 x->eol, x->file_page_number, x->eol, x->eol);
1613 for (fe = hsh_first (x->loaded, &iter); fe != NULL;
1614 fe = hsh_next (x->loaded, &iter))
1618 cp = stpcpy (buf, "%%+ font ");
1619 cp = quote_ps_string (cp, fe->font->internal_name);
1620 strcpy (cp, x->eol);
1621 fputs (buf, f->file);
1624 hsh_destroy (x->loaded);
1626 hsh_destroy (x->combos);
1628 x->last_font = NULL;
1631 fprintf (f->file, "%%EOF%s", x->eol);
1632 if (ferror (f->file))
1638 ps_open_page (struct outp_driver *this)
1640 struct ps_driver_ext *x = this->ext;
1642 assert (this->driver_open && !this->page_open);
1645 if (!fn_open_ext (&x->file))
1648 msg (ME, _("PostScript output driver: %s: %s"), x->file.filename,
1652 x->file_page_number++;
1654 hsh_destroy (x->combos);
1655 x->combos = hsh_create (31, compare_ps_combo, hash_ps_combo,
1656 free_ps_combo, NULL);
1657 x->last_font = NULL;
1660 fprintf (x->file.file,
1662 "%%%%BeginPageSetup%s"
1663 "/pg save def 0.001 dup scale%s",
1664 x->page_number, x->file_page_number, x->eol,
1668 if (x->orientation == OTN_LANDSCAPE)
1669 fprintf (x->file.file,
1670 "%d 0 translate 90 rotate%s",
1673 if (x->bottom_margin != 0 || x->left_margin != 0)
1674 fprintf (x->file.file,
1675 "%d %d translate%s",
1676 x->left_margin, x->bottom_margin, x->eol);
1678 fprintf (x->file.file,
1679 "/LW %d def/TW %d def %d setlinewidth%s"
1680 "%%%%EndPageSetup%s",
1681 x->line_width, x->line_width_thick, x->line_width, x->eol,
1684 if (!ferror (x->file.file))
1686 this->page_open = 1;
1687 if (x->output_options & OPO_HEADERS)
1688 draw_headers (this);
1691 return !ferror (x->file.file);
1695 ps_close_page (struct outp_driver *this)
1697 struct ps_driver_ext *x = this->ext;
1699 assert (this->driver_open && this->page_open);
1704 fprintf (x->file.file,
1709 this->page_open = 0;
1710 return !ferror (x->file.file);
1715 /* qsort() comparison function for int tuples. */
1717 int_2_compare (const void *a_, const void *b_)
1722 return *a < *b ? -1 : *a > *b;
1725 /* Hash table comparison function for cached lines. */
1727 compare_line (const void *a_, const void *b_, void *foo UNUSED)
1729 const struct line_form *a = a_;
1730 const struct line_form *b = b_;
1732 return a->ind < b->ind ? -1 : a->ind > b->ind;
1735 /* Hash table hash function for cached lines. */
1737 hash_line (const void *pa, void *foo UNUSED)
1739 const struct line_form *a = pa;
1744 /* Hash table free function for cached lines. */
1746 free_line (void *pa, void *foo UNUSED)
1751 /* Writes PostScript code to draw a line from (x1,y1) to (x2,y2) to
1753 #define dump_line(x1, y1, x2, y2) \
1754 fprintf (ext->file.file, "%d %d %d %d L%s", \
1755 x1, YT (y1), x2, YT (y2), ext->eol)
1757 /* Write PostScript code to draw a thick line from (x1,y1) to (x2,y2)
1758 to the output file. */
1759 #define dump_thick_line(x1, y1, x2, y2) \
1760 fprintf (ext->file.file, "%d %d %d %d TL%s", \
1761 x1, YT (y1), x2, YT (y2), ext->eol)
1763 /* Writes a line of type TYPE to THIS driver's output file. The line
1764 (or its center, in the case of double lines) has its independent
1765 axis coordinate at IND; it extends from DEP1 to DEP2 on the
1768 dump_fancy_line (struct outp_driver *this, int type, int ind, int dep1, int dep2)
1770 struct ps_driver_ext *ext = this->ext;
1771 int ofs = ext->line_space / 2 + ext->line_width / 2;
1776 dump_line (dep1, ind, dep2, ind);
1779 if (ext->output_options & OPO_DOUBLE_LINE)
1781 dump_line (dep1, ind - ofs, dep2, ind - ofs);
1782 dump_line (dep1, ind + ofs, dep2, ind + ofs);
1785 dump_thick_line (dep1, ind, dep2, ind);
1790 dump_line (ind, dep1, ind, dep2);
1793 if (ext->output_options & OPO_DOUBLE_LINE)
1795 dump_line (ind - ofs, dep1, ind - ofs, dep2);
1796 dump_line (ind + ofs, dep1, ind + ofs, dep2);
1799 dump_thick_line (ind, dep1, ind, dep2);
1810 /* Writes all the cached lines to the output file, then clears the
1813 dump_lines (struct outp_driver *this)
1815 struct ps_driver_ext *x = this->ext;
1817 struct hsh_iterator iter;
1820 for (type = 0; type < n_line_types; type++)
1822 struct line_form *line;
1824 if (x->lines[type] == NULL)
1827 for (line = hsh_first (x->lines[type], &iter); line != NULL;
1828 line = hsh_next (x->lines[type], &iter))
1831 int lo = INT_MIN, hi;
1833 qsort (line->dep, line->ndep, sizeof *line->dep, int_2_compare);
1834 lo = line->dep[0][0];
1835 hi = line->dep[0][1];
1836 for (i = 1; i < line->ndep; i++)
1837 if (line->dep[i][0] <= hi + 1)
1839 int min_hi = line->dep[i][1];
1845 dump_fancy_line (this, type, line->ind, lo, hi);
1846 lo = line->dep[i][0];
1847 hi = line->dep[i][1];
1849 dump_fancy_line (this, type, line->ind, lo, hi);
1852 hsh_destroy (x->lines[type]);
1853 x->lines[type] = NULL;
1857 /* (Same args as dump_fancy_line()). Either dumps the line directly
1858 to the output file, or adds it to the cache, depending on the
1859 user-selected line optimization mode. */
1861 line (struct outp_driver *this, int type, int ind, int dep1, int dep2)
1863 struct ps_driver_ext *ext = this->ext;
1864 struct line_form **f;
1866 assert (dep2 >= dep1);
1867 if (ext->line_opt == 0)
1869 dump_fancy_line (this, type, ind, dep1, dep2);
1873 if (ext->lines[type] == NULL)
1874 ext->lines[type] = hsh_create (31, compare_line, hash_line,
1876 f = (struct line_form **) hsh_probe (ext->lines[type], &ind);
1879 *f = xmalloc (sizeof **f + sizeof (int[15][2]));
1883 (*f)->dep[0][0] = dep1;
1884 (*f)->dep[0][1] = dep2;
1887 if ((*f)->ndep >= (*f)->mdep)
1890 *f = xrealloc (*f, (sizeof **f + sizeof (int[2]) * ((*f)->mdep - 1)));
1892 (*f)->dep[(*f)->ndep][0] = dep1;
1893 (*f)->dep[(*f)->ndep][1] = dep2;
1898 ps_line_horz (struct outp_driver *this, const struct rect *r,
1899 const struct color *c UNUSED, int style)
1901 /* Must match output.h:OUTP_L_*. */
1902 static const int types[OUTP_L_COUNT] =
1903 {-1, horz, dbl_horz, spl_horz};
1905 int y = (r->y1 + r->y2) / 2;
1907 assert (this->driver_open && this->page_open);
1908 assert (style >= 0 && style < OUTP_L_COUNT);
1909 style = types[style];
1911 line (this, style, y, r->x1, r->x2);
1915 ps_line_vert (struct outp_driver *this, const struct rect *r,
1916 const struct color *c UNUSED, int style)
1918 /* Must match output.h:OUTP_L_*. */
1919 static const int types[OUTP_L_COUNT] =
1920 {-1, vert, dbl_vert, spl_vert};
1922 int x = (r->x1 + r->x2) / 2;
1924 assert (this->driver_open && this->page_open);
1925 assert (style >= 0 && style < OUTP_L_COUNT);
1926 style = types[style];
1928 line (this, style, x, r->y1, r->y2);
1931 #define L (style->l != OUTP_L_NONE)
1932 #define R (style->r != OUTP_L_NONE)
1933 #define T (style->t != OUTP_L_NONE)
1934 #define B (style->b != OUTP_L_NONE)
1937 ps_line_intersection (struct outp_driver *this, const struct rect *r,
1938 const struct color *c UNUSED,
1939 const struct outp_styles *style)
1941 struct ps_driver_ext *ext = this->ext;
1943 int x = (r->x1 + r->x2) / 2;
1944 int y = (r->y1 + r->y2) / 2;
1945 int ofs = (ext->line_space + ext->line_width) / 2;
1946 int x1 = x - ofs, x2 = x + ofs;
1947 int y1 = y - ofs, y2 = y + ofs;
1949 assert (this->driver_open && this->page_open);
1950 assert (!((style->l != style->r && style->l != OUTP_L_NONE
1951 && style->r != OUTP_L_NONE)
1952 || (style->t != style->b && style->t != OUTP_L_NONE
1953 && style->b != OUTP_L_NONE)));
1955 switch ((style->l | style->r) | ((style->t | style->b) << 8))
1957 case (OUTP_L_SINGLE) | (OUTP_L_SINGLE << 8):
1958 case (OUTP_L_SINGLE) | (OUTP_L_NONE << 8):
1959 case (OUTP_L_NONE) | (OUTP_L_SINGLE << 8):
1961 line (this, horz, y, r->x1, x);
1963 line (this, horz, y, x, r->x2);
1965 line (this, vert, x, r->y1, y);
1967 line (this, vert, x, y, r->y2);
1969 case (OUTP_L_SINGLE) | (OUTP_L_DOUBLE << 8):
1970 case (OUTP_L_NONE) | (OUTP_L_DOUBLE << 8):
1972 line (this, horz, y, r->x1, x1);
1974 line (this, horz, y, x2, r->x2);
1976 line (this, dbl_vert, x, r->y1, y);
1978 line (this, dbl_vert, x, y, r->y2);
1979 if ((L && R) && !(T && B))
1980 line (this, horz, y, x1, x2);
1982 case (OUTP_L_DOUBLE) | (OUTP_L_SINGLE << 8):
1983 case (OUTP_L_DOUBLE) | (OUTP_L_NONE << 8):
1985 line (this, dbl_horz, y, r->x1, x);
1987 line (this, dbl_horz, y, x, r->x2);
1989 line (this, vert, x, r->y1, y);
1991 line (this, vert, x, y, r->y2);
1992 if ((T && B) && !(L && R))
1993 line (this, vert, x, y1, y2);
1995 case (OUTP_L_DOUBLE) | (OUTP_L_DOUBLE << 8):
1997 line (this, dbl_horz, y, r->x1, x);
1999 line (this, dbl_horz, y, x, r->x2);
2001 line (this, dbl_vert, x, r->y1, y);
2003 line (this, dbl_vert, x, y, r->y2);
2005 line (this, vert, x1, y1, y2);
2007 line (this, vert, x2, y1, y2);
2009 line (this, horz, y1, x1, x2);
2011 line (this, horz, y2, x1, x2);
2019 ps_box (struct outp_driver *this UNUSED, const struct rect *r UNUSED,
2020 const struct color *bord UNUSED, const struct color *fill UNUSED)
2022 assert (this->driver_open && this->page_open);
2026 ps_polyline_begin (struct outp_driver *this UNUSED,
2027 const struct color *c UNUSED)
2029 assert (this->driver_open && this->page_open);
2032 ps_polyline_point (struct outp_driver *this UNUSED, int x UNUSED, int y UNUSED)
2034 assert (this->driver_open && this->page_open);
2037 ps_polyline_end (struct outp_driver *this UNUSED)
2039 assert (this->driver_open && this->page_open);
2042 /* Returns the width of string S for THIS driver. */
2044 text_width (struct outp_driver *this, char *s)
2046 struct outp_text text;
2048 text.options = OUTP_T_JUST_LEFT;
2049 ls_init (&text.s, s, strlen (s));
2050 this->class->text_metrics (this, &text);
2054 /* Write string S at location (X,Y) with width W for THIS driver. */
2056 out_text_plain (struct outp_driver *this, char *s, int x, int y, int w)
2058 struct outp_text text;
2060 text.options = OUTP_T_JUST_LEFT | OUTP_T_HORZ | OUTP_T_VERT;
2061 ls_init (&text.s, s, strlen (s));
2063 text.v = this->font_height;
2066 this->class->text_draw (this, &text);
2069 /* Draw top of page headers for THIS driver. */
2071 draw_headers (struct outp_driver *this)
2073 struct ps_driver_ext *ext = this->ext;
2075 struct font_entry *old_current = ext->current;
2076 char *old_family = xstrdup (ext->family); /* FIXME */
2077 int old_size = ext->size;
2079 int fh = this->font_height;
2082 fprintf (ext->file.file, "%d %d %d %d GB%s",
2083 0, YT (y), this->width, YT (y + 2 * fh + ext->line_gutter),
2085 this->class->text_set_font_family (this, "T");
2087 y += ext->line_width + ext->line_gutter;
2093 sprintf (buf, _("%s - Page %d"), curdate, ext->page_number);
2094 rh_width = text_width (this, buf);
2096 out_text_plain (this, buf, this->width - this->prop_em_width - rh_width,
2099 if (outp_title && outp_subtitle)
2100 out_text_plain (this, outp_title, this->prop_em_width, y,
2101 this->width - 3 * this->prop_em_width - rh_width);
2109 char *string = outp_subtitle ? outp_subtitle : outp_title;
2111 sprintf (buf, "%s - %s", version, host_system);
2112 rh_width = text_width (this, buf);
2114 out_text_plain (this, buf, this->width - this->prop_em_width - rh_width,
2118 out_text_plain (this, string, this->prop_em_width, y,
2119 this->width - 3 * this->prop_em_width - rh_width);
2124 ext->current = old_current;
2126 ext->family = old_family;
2127 ext->size = old_size;
2134 ps_text_set_font_by_name (struct outp_driver *this, const char *dit)
2136 struct ps_driver_ext *x = this->ext;
2137 struct font_entry *fe;
2139 assert (this->driver_open && this->page_open);
2141 /* Short-circuit common fonts. */
2142 if (!strcmp (dit, "PROP"))
2144 x->current = x->prop;
2145 x->size = x->font_size;
2148 else if (!strcmp (dit, "FIXED"))
2150 x->current = x->fixed;
2151 x->size = x->font_size;
2155 /* Find font_desc corresponding to Groff name dit. */
2156 fe = hsh_find (x->loaded, &dit);
2158 fe = load_font (this, dit);
2163 ps_text_set_font_by_position (struct outp_driver *this, int pos)
2165 struct ps_driver_ext *x = this->ext;
2168 assert (this->driver_open && this->page_open);
2170 /* Determine font name by suffixing position string to font family
2175 dit = local_alloc (strlen (x->family) + 3);
2176 cp = stpcpy (dit, x->family);
2198 /* Find font_desc corresponding to Groff name dit. */
2200 struct font_entry *fe = hsh_find (x->loaded, &dit);
2202 fe = load_font (this, dit);
2210 ps_text_set_font_family (struct outp_driver *this, const char *s)
2212 struct ps_driver_ext *x = this->ext;
2214 assert (this->driver_open && this->page_open);
2217 x->family = xstrdup (s);
2221 ps_text_get_font_name (struct outp_driver *this)
2223 struct ps_driver_ext *x = this->ext;
2225 assert (this->driver_open && this->page_open);
2226 return x->current->font->name;
2230 ps_text_get_font_family (struct outp_driver *this)
2232 struct ps_driver_ext *x = this->ext;
2234 assert (this->driver_open && this->page_open);
2239 ps_text_set_size (struct outp_driver *this, int size)
2241 struct ps_driver_ext *x = this->ext;
2243 assert (this->driver_open && this->page_open);
2244 x->size = PSUS / 72000 * size;
2249 ps_text_get_size (struct outp_driver *this, int *em_width)
2251 struct ps_driver_ext *x = this->ext;
2253 assert (this->driver_open && this->page_open);
2255 *em_width = (x->current->font->space_width * x->size) / 1000;
2256 return x->size / (PSUS / 72000);
2259 /* An output character. */
2262 struct font_entry *font; /* Font of character. */
2263 int size; /* Size of character. */
2264 int x, y; /* Location of character. */
2265 unsigned char ch; /* Character. */
2266 char separate; /* Must be separate from previous char. */
2269 /* Hash table comparison function for ps_combo structs. */
2271 compare_ps_combo (const void *pa, const void *pb, void *foo UNUSED)
2273 const struct ps_font_combo *a = pa;
2274 const struct ps_font_combo *b = pb;
2276 return !((a->font == b->font) && (a->size == b->size));
2279 /* Hash table hash function for ps_combo structs. */
2281 hash_ps_combo (const void *pa, void *foo UNUSED)
2283 const struct ps_font_combo *a = pa;
2284 unsigned name_hash = hsh_hash_string (a->font->font->internal_name);
2285 return name_hash ^ hsh_hash_int (a->size);
2288 /* Hash table free function for ps_combo structs. */
2290 free_ps_combo (void *a, void *foo UNUSED)
2295 /* Causes PostScript code to be output that switches to the font
2296 CP->FONT and font size CP->SIZE. The first time a particular
2297 font/size combination is used on a particular page, this involves
2298 outputting PostScript code to load the font. */
2300 switch_font (struct outp_driver *this, const struct output_char *cp)
2302 struct ps_driver_ext *ext = this->ext;
2303 struct ps_font_combo srch, **fc;
2305 srch.font = cp->font;
2306 srch.size = cp->size;
2308 fc = (struct ps_font_combo **) hsh_probe (ext->combos, &srch);
2311 fprintf (ext->file.file, "F%x%s", (*fc)->index, ext->eol);
2316 struct ps_encoding *encoding;
2319 *fc = xmalloc (sizeof **fc);
2320 (*fc)->font = cp->font;
2321 (*fc)->size = cp->size;
2322 (*fc)->index = ext->next_combo++;
2324 filename = find_encoding_file (this, cp->font->font->encoding);
2327 encoding = get_encoding (this, filename);
2332 msg (IE, _("PostScript driver: Cannot find encoding `%s' for "
2333 "PostScript font `%s'."), cp->font->font->encoding,
2334 cp->font->font->internal_name);
2335 encoding = default_encoding (this);
2338 if (cp->font != ext->fixed && cp->font != ext->prop)
2340 bp = stpcpy (buf, "%%IncludeResource: font ");
2341 bp = quote_ps_string (bp, cp->font->font->internal_name);
2342 bp = stpcpy (bp, ext->eol);
2347 bp = spprintf (bp, "/F%x E%x %d", (*fc)->index, encoding->index,
2349 bp = quote_ps_name (bp, cp->font->font->internal_name);
2350 sprintf (bp, " SF%s", ext->eol);
2351 fputs (buf, ext->file.file);
2353 ext->last_font = *fc;
2356 /* (write_text) Writes the accumulated line buffer to the output
2358 #define output_line() \
2361 lp = stpcpy (lp, ext->eol); \
2363 fputs (line, ext->file.file); \
2368 /* (write_text) Adds the string representing number X to the line
2369 buffer, flushing the buffer to disk beforehand if necessary. */
2370 #define put_number(X) \
2373 int n = nsprintf (number, "%d", X); \
2374 if (n + lp > &line[75]) \
2376 lp = stpcpy (lp, number); \
2380 /* Outputs PostScript code to THIS driver's output file to display the
2381 characters represented by the output_char's between CP and END,
2382 using the associated outp_text T to determine formatting. WIDTH is
2383 the width of the output region; WIDTH_LEFT is the amount of the
2384 WIDTH that is not taken up by text (so it can be used to determine
2387 write_text (struct outp_driver *this,
2388 const struct output_char *cp, const struct output_char *end,
2389 struct outp_text *t, int width UNUSED, int width_left)
2391 struct ps_driver_ext *ext = this->ext;
2396 char number[INT_DIGITS + 1];
2400 switch (t->options & OUTP_T_JUST_MASK)
2402 case OUTP_T_JUST_LEFT:
2405 case OUTP_T_JUST_RIGHT:
2408 case OUTP_T_JUST_CENTER:
2409 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);
2552 width = width_left = (t->options & OUTP_T_HORZ) ? t->h : INT_MAX;
2553 height_left = (t->options & OUTP_T_VERT) ? t->v : INT_MAX;
2557 space_buf_loc = NULL;
2558 space_width_left = 0;
2561 if (!width || !height_left)
2566 struct char_metrics *metric;
2572 /* Set char_name to the name of the character or ligature at
2574 local_char_name[0] = *cp;
2575 char_name = local_char_name;
2576 if (ext->current->font->ligatures && *cp == 'f')
2585 lig = LIG_fi, char_name = "fi";
2588 lig = LIG_fl, char_name = "fl";
2595 lig = LIG_ffi, char_name = "ffi";
2598 lig = LIG_ffl, char_name = "ffl";
2601 lig = LIG_ff, char_name = "ff";
2605 if ((lig & ext->current->font->ligatures) == 0)
2607 local_char_name[0] = *cp; /* 'f' */
2608 char_name = local_char_name;
2611 else if (*cp == '\n')
2615 write_text (this, buf, buf_loc, t, width, width_left);
2622 height_left -= max_height;
2628 /* FIXME: when we're page buffering it will be necessary to
2629 set separate to 1. */
2632 cp += strlen (char_name);
2634 /* Figure out what size this character is, and what kern
2635 adjustment we need. */
2636 cur_char = font_char_name_to_index (char_name);
2637 metric = font_get_char_metrics (ext->current->font, cur_char);
2640 static struct char_metrics m;
2642 m.width = ext->current->font->space_width;
2643 m.code = *char_name;
2645 kern_amt = font_get_kern_adjust (ext->current->font, prev_char,
2649 kern_amt = (kern_amt * ext->size / 1000);
2652 char_width = metric->width * ext->size / 1000;
2654 /* Record the current status if this is a space character. */
2655 if (cur_char == space_index && buf_loc > buf)
2658 space_buf_loc = buf_loc;
2659 space_width_left = width_left;
2662 /* Drop down to a new line if there's no room left on this
2664 if (char_width + kern_amt > width_left)
2666 /* Regress to previous space, if any. */
2670 width_left = space_width_left;
2671 buf_loc = space_buf_loc;
2676 write_text (this, buf, buf_loc, t, width, width_left);
2683 height_left -= max_height;
2691 /* FIXME: when we're page buffering it will be
2692 necessary to set separate to 1. */
2697 if (ext->size > max_height)
2698 max_height = ext->size;
2699 if (max_height > height_left)
2702 /* Actually draw the character. */
2705 if (buf_loc >= buf_end)
2707 int buf_len = buf_end - buf;
2711 struct output_char *new_buf;
2713 new_buf = xmalloc (sizeof *new_buf * 256);
2714 memcpy (new_buf, buf, sizeof *new_buf * 128);
2715 buf_loc = new_buf + 128;
2716 buf_end = new_buf + 256;
2722 buf = xrealloc (buf, sizeof *buf * buf_len * 2);
2723 buf_loc = buf + buf_len;
2724 buf_end = buf + buf_len * 2;
2729 buf_loc->font = ext->current;
2730 buf_loc->size = ext->size;
2733 buf_loc->ch = metric->code;
2734 buf_loc->separate = separate;
2739 /* Prepare for next iteration. */
2740 width_left -= char_width + kern_amt;
2741 prev_char = cur_char;
2743 height_left -= max_height;
2744 if (buf_loc > buf && draw)
2745 write_text (this, buf, buf_loc, t, width, width_left);
2748 if (!(t->options & OUTP_T_HORZ))
2749 t->h = INT_MAX - width_left;
2750 if (!(t->options & OUTP_T_VERT))
2751 t->v = INT_MAX - height_left;
2753 t->v -= height_left;
2754 if (buf_end - buf == 128)
2758 ext->current = old_current;
2760 ext->family = old_family;
2761 ext->size = old_size;
2765 ps_text_metrics (struct outp_driver *this, struct outp_text *t)
2767 assert (this->driver_open && this->page_open);
2772 ps_text_draw (struct outp_driver *this, struct outp_text *t)
2774 assert (this->driver_open && this->page_open);
2780 /* Translate a filename to a font. */
2781 struct filename2font
2783 char *filename; /* Normalized filename. */
2784 struct font_desc *font;
2787 /* Table of `filename2font's. */
2788 static struct hsh_table *ps_fonts;
2790 /* Hash table comparison function for filename2font structs. */
2792 compare_filename2font (const void *a, const void *b, void *param UNUSED)
2794 return strcmp (((struct filename2font *) a)->filename,
2795 ((struct filename2font *) b)->filename);
2798 /* Hash table hash function for filename2font structs. */
2800 hash_filename2font (const void *f2f_, void *param UNUSED)
2802 const struct filename2font *f2f = f2f_;
2803 return hsh_hash_string (f2f->filename);
2806 /* Initializes the global font list by creating the hash table for
2807 translation of filenames to font_desc structs. */
2811 ps_fonts = hsh_create (31, compare_filename2font, hash_filename2font,
2815 /* Loads the font having Groff name DIT into THIS driver instance.
2816 Specifically, adds it into the THIS driver's `loaded' hash
2818 static struct font_entry *
2819 load_font (struct outp_driver *this, const char *dit)
2821 struct ps_driver_ext *x = this->ext;
2822 char *filename1, *filename2;
2824 struct font_entry *fe;
2826 filename1 = find_ps_file (this, dit);
2828 filename1 = xstrdup (dit);
2829 filename2 = fn_normalize (filename1);
2832 entry = hsh_probe (ps_fonts, &filename2);
2835 struct filename2font *f2f;
2836 struct font_desc *f = groff_read_font (filename2);
2843 f = default_font ();
2846 f2f = xmalloc (sizeof *f2f);
2847 f2f->filename = filename2;
2854 fe = xmalloc (sizeof *fe);
2855 fe->dit = xstrdup (dit);
2856 fe->font = ((struct filename2font *) * entry)->font;
2857 *hsh_probe (x->loaded, &dit) = fe;
2862 /* PostScript driver class. */
2863 struct outp_class postscript_class =
2885 ps_line_intersection,
2892 ps_text_set_font_by_name,
2893 ps_text_set_font_by_position,
2894 ps_text_set_font_family,
2895 ps_text_get_font_name,
2896 ps_text_get_font_family,
2903 /* EPSF driver class. FIXME: Probably doesn't work right. */
2904 struct outp_class epsf_class =
2926 ps_line_intersection,
2933 ps_text_set_font_by_name,
2934 ps_text_set_font_by_position,
2935 ps_text_set_font_family,
2936 ps_text_get_font_name,
2937 ps_text_get_font_family,
2944 #endif /* NO_POSTSCRIPT */