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);
270 static void done_fonts (void);
272 static void dump_lines (struct outp_driver *this);
274 static void read_ps_encodings (struct outp_driver *this);
275 static int compare_ps_encoding (const void *pa, const void *pb, void *foo);
276 static unsigned hash_ps_encoding (const void *pa, void *foo);
277 static void free_ps_encoding (void *a, void *foo);
278 static void add_encoding (struct outp_driver *this, char *filename);
279 static struct ps_encoding *default_encoding (struct outp_driver *this);
281 static int compare_ps_combo (const void *pa, const void *pb, void *foo);
282 static unsigned hash_ps_combo (const void *pa, void *foo);
283 static void free_ps_combo (void *a, void *foo);
285 static char *quote_ps_name (char *dest, const char *string);
286 static char *quote_ps_string (char *dest, const char *string);
288 /* Driver initialization. */
291 ps_open_global (struct outp_class *this UNUSED)
299 ps_close_global (struct outp_class *this UNUSED)
307 ps_font_sizes (struct outp_class *this UNUSED, int *n_valid_sizes)
309 /* Allow fonts up to 1" in height. */
310 static int valid_sizes[] =
313 assert (n_valid_sizes != NULL);
319 ps_preopen_driver (struct outp_driver *this)
321 struct ps_driver_ext *x;
325 assert (this->driver_open == 0);
326 msg (VM (1), _("PostScript driver initializing as `%s'..."), this->name);
328 this->ext = x = xmalloc (sizeof (struct ps_driver_ext));
330 this->horiz = this->vert = 1;
331 this->width = this->length = 0;
333 x->orientation = OTN_PORTRAIT;
334 x->output_options = OPO_COLOR | OPO_HEADERS | OPO_AUTO_ENCODE;
335 x->data = ODA_CLEAN7BIT;
337 x->left_margin = x->right_margin =
338 x->top_margin = x->bottom_margin = PSUS / 2;
340 strcpy (x->eol, "\n");
343 x->prologue_fn = NULL;
345 x->encoding_fn = NULL;
347 x->prop_family = NULL;
348 x->fixed_family = NULL;
349 x->font_size = PSUS * 10 / 72;
351 x->line_gutter = PSUS / 144;
352 x->line_space = PSUS / 144;
353 x->line_width = PSUS / 144;
354 x->line_width_thick = PSUS / 48;
360 x->file.filename = NULL;
363 x->file.sequence_no = &x->page_number;
364 x->file.param = this;
365 x->file.postopen = postopen;
366 x->file.preclose = preclose;
370 x->file_page_number = 0;
371 for (i = 0; i < n_line_types; i++)
382 x->encodings = hsh_create (31, compare_ps_encoding, hash_ps_encoding,
383 free_ps_encoding, NULL);
384 x->next_encoding = 0;
394 ps_postopen_driver (struct outp_driver *this)
396 struct ps_driver_ext *x = this->ext;
398 assert (this->driver_open == 0);
400 if (this->width == 0)
402 this->width = PSUS * 17 / 2; /* Defaults to 8.5"x11". */
403 this->length = PSUS * 11;
406 if (x->text_opt == -1)
407 x->text_opt = (this->device & OUTP_DEV_SCREEN) ? 0 : 1;
408 if (x->line_opt == -1)
409 x->line_opt = (this->device & OUTP_DEV_SCREEN) ? 0 : 1;
413 if (x->orientation == OTN_LANDSCAPE)
415 int temp = this->width;
416 this->width = this->length;
419 this->width -= x->left_margin + x->right_margin;
420 this->length -= x->top_margin + x->bottom_margin;
421 if (x->output_options & OPO_HEADERS)
423 this->length -= 3 * x->font_size;
424 x->top_margin += 3 * x->font_size;
426 if (NULL == x->file.filename)
427 x->file.filename = xstrdup ("pspp.ps");
429 if (x->font_dir == NULL)
430 x->font_dir = xstrdup ("devps");
431 if (x->prologue_fn == NULL)
432 x->prologue_fn = xstrdup ("ps-prologue");
433 if (x->desc_fn == NULL)
434 x->desc_fn = xstrdup ("DESC");
435 if (x->encoding_fn == NULL)
436 x->encoding_fn = xstrdup ("ps-encodings");
438 if (x->prop_family == NULL)
439 x->prop_family = xstrdup ("H");
440 if (x->fixed_family == NULL)
441 x->fixed_family = xstrdup ("C");
443 read_ps_encodings (this);
448 if (this->length / x->font_size < 15)
450 msg (SE, _("PostScript driver: The defined page is not long "
451 "enough to hold margins and headers, plus least 15 "
452 "lines of the default fonts. In fact, there's only "
453 "room for %d lines of each font at the default size "
454 "of %d.%03d points."),
455 this->length / x->font_size,
456 x->font_size / 1000, x->font_size % 1000);
460 this->driver_open = 1;
461 msg (VM (2), _("%s: Initialization complete."), this->name);
467 ps_close_driver (struct outp_driver *this)
469 struct ps_driver_ext *x = this->ext;
473 assert (this->driver_open == 1);
474 msg (VM (2), _("%s: Beginning closing..."), this->name);
476 fn_close_ext (&x->file);
477 free (x->file.filename);
479 free (x->prologue_fn);
481 free (x->encoding_fn);
482 free (x->prop_family);
483 free (x->fixed_family);
485 for (i = 0; i < n_line_types; i++)
486 hsh_destroy (x->lines[i]);
487 hsh_destroy (x->encodings);
488 hsh_destroy (x->combos);
489 hsh_destroy (x->loaded);
492 this->driver_open = 0;
493 msg (VM (3), _("%s: Finished closing."), this->name);
498 /* font_entry comparison function for hash tables. */
500 compare_font_entry (const void *a, const void *b, void *foobar UNUSED)
502 return strcmp (((struct font_entry *) a)->dit, ((struct font_entry *) b)->dit);
505 /* font_entry hash function for hash tables. */
507 hash_font_entry (const void *fe_, void *foobar UNUSED)
509 const struct font_entry *fe = fe_;
510 return hsh_hash_string (fe->dit);
513 /* font_entry destructor function for hash tables. */
515 free_font_entry (void *pa, void *foo UNUSED)
517 struct font_entry *a = pa;
522 /* Generic option types. */
532 /* All the options that the PostScript driver supports. */
533 static struct outp_option option_tab[] =
536 {"output-file", 1, 0},
537 {"paper-size", 2, 0},
538 {"orientation", 3, 0},
539 {"color", boolean_arg, 0},
541 {"auto-encode", boolean_arg, 5},
542 {"headers", boolean_arg, 1},
543 {"left-margin", pos_int_arg, 0},
544 {"right-margin", pos_int_arg, 1},
545 {"top-margin", pos_int_arg, 2},
546 {"bottom-margin", pos_int_arg, 3},
547 {"font-dir", string_arg, 0},
548 {"prologue-file", string_arg, 1},
549 {"device-file", string_arg, 2},
550 {"encoding-file", string_arg, 3},
551 {"prop-font-family", string_arg, 5},
552 {"fixed-font-family", string_arg, 6},
553 {"font-size", pos_int_arg, 4},
554 {"optimize-text-size", nonneg_int_arg, 0},
555 {"optimize-line-size", nonneg_int_arg, 1},
556 {"max-fonts-simult", nonneg_int_arg, 2},
558 {"line-style", 7, 0},
559 {"line-width", dimension_arg, 2},
560 {"line-gutter", dimension_arg, 3},
561 {"line-width", dimension_arg, 4},
562 {"line-width-thick", dimension_arg, 5},
566 static struct outp_option_info option_info;
569 ps_option (struct outp_driver *this, const char *key, const struct string *val)
571 struct ps_driver_ext *x = this->ext;
573 char *value = ds_c_str (val);
575 cat = outp_match_keyword (key, option_tab, &option_info, &subcat);
580 msg (SE, _("Unknown configuration parameter `%s' for PostScript device "
584 free (x->file.filename);
585 x->file.filename = xstrdup (value);
588 outp_get_paper_size (value, &this->width, &this->length);
591 if (!strcmp (value, "portrait"))
592 x->orientation = OTN_PORTRAIT;
593 else if (!strcmp (value, "landscape"))
594 x->orientation = OTN_LANDSCAPE;
596 msg (SE, _("Unknown orientation `%s'. Valid orientations are "
597 "`portrait' and `landscape'."), value);
600 if (!strcmp (value, "clean7bit") || !strcmp (value, "Clean7Bit"))
601 x->data = ODA_CLEAN7BIT;
602 else if (!strcmp (value, "clean8bit")
603 || !strcmp (value, "Clean8Bit"))
604 x->data = ODA_CLEAN8BIT;
605 else if (!strcmp (value, "binary") || !strcmp (value, "Binary"))
606 x->data = ODA_BINARY;
608 msg (SE, _("Unknown value for `data'. Valid values are `clean7bit', "
609 "`clean8bit', and `binary'."));
612 if (!strcmp (value, "lf"))
613 strcpy (x->eol, "\n");
614 else if (!strcmp (value, "crlf"))
615 strcpy (x->eol, "\r\n");
617 msg (SE, _("Unknown value for `line-ends'. Valid values are `lf' and "
621 if (!strcmp (value, "thick"))
622 x->output_options &= ~OPO_DOUBLE_LINE;
623 else if (!strcmp (value, "double"))
624 x->output_options |= OPO_DOUBLE_LINE;
626 msg (SE, _("Unknown value for `line-style'. Valid values are `thick' "
634 if (!strcmp (value, "on") || !strcmp (value, "true")
635 || !strcmp (value, "yes") || atoi (value))
637 else if (!strcmp (value, "off") || !strcmp (value, "false")
638 || !strcmp (value, "no") || !strcmp (value, "0"))
642 msg (SE, _("Boolean value expected for %s."), key);
654 mask = OPO_MIRROR_HORZ;
657 mask = OPO_MIRROR_VERT;
660 mask = OPO_ROTATE_180;
663 mask = OPO_AUTO_ENCODE;
670 x->output_options |= mask;
672 x->output_options &= ~mask;
681 arg = strtol (value, &tail, 0);
682 if (arg < 1 || errno == ERANGE || *tail)
684 msg (SE, _("Positive integer required as value for `%s'."), key);
687 if ((subcat == 4 || subcat == 5) && arg < 1000)
689 msg (SE, _("Default font size must be at least 1 point (value "
690 "of 1000 for key `%s')."), key);
696 x->left_margin = arg;
699 x->right_margin = arg;
705 x->bottom_margin = arg;
717 int dimension = outp_evaluate_dimension (value, NULL);
721 msg (SE, _("Value for `%s' must be a dimension of positive "
722 "length (i.e., `1in')."), key);
728 x->line_width = dimension;
731 x->line_gutter = dimension;
734 x->line_width = dimension;
737 x->line_width_thick = dimension;
753 dest = &x->prologue_fn;
759 dest = &x->encoding_fn;
762 dest = &x->prop_family;
765 dest = &x->fixed_family;
773 *dest = xstrdup (value);
782 arg = strtol (value, &tail, 0);
783 if (arg < 0 || errno == ERANGE || *tail)
785 msg (SE, _("Nonnegative integer required as value for `%s'."), key);
809 /* Looks for a PostScript font file or config file in all the
810 appropriate places. Returns the filename on success, NULL on
812 /* PORTME: Filename operations. */
814 find_ps_file (struct outp_driver *this, const char *name)
816 struct ps_driver_ext *x = this->ext;
819 /* x->font_dir + name: "devps/ps-encodings". */
822 /* Usually equal to groff_font_path. */
825 /* Final filename. */
829 basename = local_alloc (strlen (x->font_dir) + 1 + strlen (name) + 1);
830 cp = stpcpy (basename, x->font_dir);
831 *cp++ = DIR_SEPARATOR;
834 /* Decide on search path. */
836 const char *pre_pathname;
838 pre_pathname = getenv ("STAT_GROFF_FONT_PATH");
839 if (pre_pathname == NULL)
840 pre_pathname = getenv ("GROFF_FONT_PATH");
841 if (pre_pathname == NULL)
842 pre_pathname = groff_font_path;
843 pathname = fn_tilde_expand (pre_pathname);
846 /* Search all possible places for the file. */
847 fn = fn_search_path (basename, pathname, NULL);
849 fn = fn_search_path (basename, config_path, NULL);
851 fn = fn_search_path (name, pathname, NULL);
853 fn = fn_search_path (name, config_path, NULL);
855 local_free (basename);
862 /* Hash table comparison function for ps_encoding's. */
864 compare_ps_encoding (const void *pa, const void *pb, void *foo UNUSED)
866 const struct ps_encoding *a = pa;
867 const struct ps_encoding *b = pb;
869 return strcmp (a->filename, b->filename);
872 /* Hash table hash function for ps_encoding's. */
874 hash_ps_encoding (const void *pa, void *foo UNUSED)
876 const struct ps_encoding *a = pa;
878 return hsh_hash_string (a->filename);
881 /* Hash table free function for ps_encoding's. */
883 free_ps_encoding (void *pa, void *foo UNUSED)
885 struct ps_encoding *a = pa;
891 /* Iterates through the list of encodings used for this driver
892 instance, reads each of them from disk, and writes them as
893 PostScript code to the output file. */
895 output_encodings (struct outp_driver *this)
897 struct ps_driver_ext *x = this->ext;
899 struct hsh_iterator iter;
900 struct ps_encoding *pe;
902 struct string line, buf;
904 ds_init (&line, 128);
906 for (pe = hsh_first (x->encodings, &iter); pe != NULL;
907 pe = hsh_next (x->encodings, &iter))
911 msg (VM (1), _("%s: %s: Opening PostScript font encoding..."),
912 this->name, pe->filename);
914 f = fopen (pe->filename, "r");
917 msg (IE, _("PostScript driver: Cannot open encoding file `%s': %s. "
918 "Substituting ISOLatin1Encoding for missing encoding."),
919 pe->filename, strerror (errno));
920 fprintf (x->file.file, "/E%x ISOLatin1Encoding def%s",
925 struct file_locator where;
927 const char *tab[256];
934 const char *notdef = ".notdef";
938 for (i = 0; i < 256; i++)
941 where.filename = pe->filename;
942 where.line_number = 0;
943 err_push_file_locator (&where);
945 while (ds_get_config_line (f, &buf, &where))
952 pschar = strtok_r (ds_c_str (&buf), " \t\r\n", &sp);
953 code = strtok_r (NULL, " \t\r\n", &sp);
954 if (*pschar == 0 || *code == 0)
956 code_val = strtol (code, &fubar, 0);
959 msg (IS, _("PostScript driver: Invalid numeric format."));
962 if (code_val < 0 || code_val > 255)
964 msg (IS, _("PostScript driver: Codes must be between 0 "
965 "and 255. (%d is not allowed.)"), code_val);
968 tab[code_val] = local_alloc (strlen (pschar) + 1);
969 strcpy ((char *) (tab[code_val]), pschar);
971 err_pop_file_locator (&where);
974 ds_printf (&line, "/E%x[", pe->index);
975 for (i = 0; i < 257; i++)
981 quote_ps_name (temp, tab[i]);
982 if (tab[i] != notdef)
986 strcpy (temp, "]def");
988 if (ds_length (&line) + strlen (temp) > 70)
990 ds_puts (&line, x->eol);
991 fputs (ds_c_str (&line), x->file.file);
994 ds_puts (&line, temp);
996 ds_puts (&line, x->eol);
997 fputs (ds_c_str (&line), x->file.file);
999 if (fclose (f) == EOF)
1000 msg (MW, _("PostScript driver: Error closing encoding file `%s'."),
1003 msg (VM (2), _("%s: PostScript font encoding read successfully."),
1011 /* Finds the ps_encoding in THIS that corresponds to the file with
1012 name NORM_FILENAME, which must have previously been normalized with
1013 normalize_filename(). */
1014 static struct ps_encoding *
1015 get_encoding (struct outp_driver *this, const char *norm_filename)
1017 struct ps_driver_ext *x = this->ext;
1018 struct ps_encoding *pe;
1020 pe = (struct ps_encoding *) hsh_find (x->encodings, (void *) &norm_filename);
1024 /* Searches the filesystem for an encoding file with name FILENAME;
1025 returns its malloc'd, normalized name if found, otherwise NULL. */
1027 find_encoding_file (struct outp_driver *this, char *filename)
1031 if (filename == NULL)
1033 while (isspace ((unsigned char) *filename))
1035 for (cp = filename; *cp && !isspace ((unsigned char) *cp); cp++)
1041 temp = find_ps_file (this, filename);
1045 filename = fn_normalize (temp);
1046 assert (filename != NULL);
1052 /* Adds the encoding represented by the not-necessarily-normalized
1053 file FILENAME to the list of encodings, if it exists and is not
1054 already in the list. */
1056 add_encoding (struct outp_driver *this, char *filename)
1058 struct ps_driver_ext *x = this->ext;
1059 struct ps_encoding **pe;
1061 filename = find_encoding_file (this, filename);
1065 pe = (struct ps_encoding **) hsh_probe (x->encodings, &filename);
1071 *pe = xmalloc (sizeof **pe);
1072 (*pe)->filename = filename;
1073 (*pe)->index = x->next_encoding++;
1076 /* Finds the file on disk that contains the list of encodings to
1077 include in the output file, then adds those encodings to the list
1080 read_ps_encodings (struct outp_driver *this)
1082 struct ps_driver_ext *x = this->ext;
1084 /* Encodings file. */
1085 char *encoding_fn; /* `ps-encodings' filename. */
1089 struct file_locator where;
1091 /* It's okay if there's no list of encodings; not everyone cares. */
1092 encoding_fn = find_ps_file (this, x->encoding_fn);
1093 if (encoding_fn == NULL)
1097 msg (VM (1), _("%s: %s: Opening PostScript encoding list file."),
1098 this->name, encoding_fn);
1099 f = fopen (encoding_fn, "r");
1102 msg (IE, _("Opening %s: %s."), encoding_fn, strerror (errno));
1106 where.filename = encoding_fn;
1107 where.line_number = 0;
1108 err_push_file_locator (&where);
1110 ds_init (&line, 128);
1114 if (!ds_get_config_line (f, &line, &where))
1117 msg (ME, _("Reading %s: %s."), encoding_fn, strerror (errno));
1121 add_encoding (this, line.string);
1125 err_pop_file_locator (&where);
1127 if (-1 == fclose (f))
1128 msg (MW, _("Closing %s: %s."), encoding_fn, strerror (errno));
1130 msg (VM (2), _("%s: PostScript encoding list file read successfully."), this->name);
1133 /* Creates a default encoding for driver D that can be substituted for
1134 an unavailable encoding. */
1135 struct ps_encoding *
1136 default_encoding (struct outp_driver *d)
1138 struct ps_driver_ext *x = d->ext;
1139 static struct ps_encoding *enc;
1143 enc = xmalloc (sizeof *enc);
1144 enc->filename = xstrdup (_("<<default encoding>>"));
1145 enc->index = x->next_encoding++;
1150 /* Basic file operations. */
1152 /* Variables for the prologue. */
1159 static struct ps_variable *ps_var_tab;
1161 /* Searches ps_var_tab for a ps_variable with key KEY, and returns the
1162 associated value. */
1164 ps_get_var (const char *key)
1166 struct ps_variable *v;
1168 for (v = ps_var_tab; v->key; v++)
1169 if (!strcmp (key, v->key))
1174 /* Writes the PostScript prologue to file F. */
1176 postopen (struct file_ext *f)
1178 static struct ps_variable dict[] =
1180 {"bounding-box", 0},
1189 {"scale-factor", 0},
1191 {"paper-length", 0},
1195 {"line-width-thick", 0},
1200 char boundbox[INT_DIGITS * 4 + 4];
1204 char scaling[INT_DIGITS + 5];
1208 char paper_width[INT_DIGITS + 1];
1209 char paper_length[INT_DIGITS + 1];
1210 char left_margin[INT_DIGITS + 1];
1211 char top_margin[INT_DIGITS + 1];
1212 char line_width[INT_DIGITS + 1];
1213 char line_width_thick[INT_DIGITS + 1];
1215 struct outp_driver *this = f->param;
1216 struct ps_driver_ext *x = this->ext;
1218 char *prologue_fn = find_ps_file (this, x->prologue_fn);
1219 FILE *prologue_file;
1222 size_t buf_size = 0;
1224 x->loaded = hsh_create (31, compare_font_entry, hash_font_entry,
1225 free_font_entry, NULL);
1228 char *font_name = local_alloc (2 + max (strlen (x->prop_family),
1229 strlen (x->fixed_family)));
1231 strcpy (stpcpy (font_name, x->prop_family), "R");
1232 x->prop = load_font (this, font_name);
1234 strcpy (stpcpy (font_name, x->fixed_family), "R");
1235 x->fixed = load_font (this, font_name);
1237 local_free(font_name);
1240 x->current = x->prop;
1241 x->family = xstrdup (x->prop_family);
1242 x->size = x->font_size;
1245 int *h = this->horiz_line_width, *v = this->vert_line_width;
1247 this->cp_x = this->cp_y = 0;
1248 this->font_height = x->font_size;
1250 struct char_metrics *metric;
1252 metric = font_get_char_metrics (x->prop->font, '0');
1253 this->prop_em_width = ((metric
1254 ? metric->width : x->prop->font->space_width)
1255 * x->font_size / 1000);
1257 metric = font_get_char_metrics (x->fixed->font, '0');
1258 this->fixed_width = ((metric
1259 ? metric->width : x->fixed->font->space_width)
1260 * x->font_size / 1000);
1264 h[1] = v[1] = 2 * x->line_gutter + x->line_width;
1265 if (x->output_options & OPO_DOUBLE_LINE)
1266 h[2] = v[2] = 2 * x->line_gutter + 2 * x->line_width + x->line_space;
1268 h[2] = v[2] = 2 * x->line_gutter + x->line_width_thick;
1269 h[3] = v[3] = 2 * x->line_gutter + x->line_width;
1274 for (i = 0; i < (1 << OUTP_L_COUNT); i++)
1278 /* Maximum width of any line type so far. */
1281 for (bit = 0; bit < OUTP_L_COUNT; bit++)
1282 if ((i & (1 << bit)) && h[bit] > max)
1284 this->horiz_line_spacing[i] = this->vert_line_spacing[i] = max;
1289 if (x->output_options & OPO_AUTO_ENCODE)
1291 /* It's okay if this is done more than once since add_encoding()
1292 is idempotent over identical encodings. */
1293 add_encoding (this, x->prop->font->encoding);
1294 add_encoding (this, x->fixed->font->encoding);
1297 x->file_page_number = 0;
1300 if (prologue_fn == NULL)
1302 msg (IE, _("Cannot find PostScript prologue. The use of `-vv' "
1303 "on the command line is suggested as a debugging aid."));
1307 msg (VM (1), _("%s: %s: Opening PostScript prologue..."),
1308 this->name, prologue_fn);
1309 prologue_file = fopen (prologue_fn, "rb");
1310 if (prologue_file == NULL)
1312 fclose (prologue_file);
1314 msg (IE, "%s: %s", prologue_fn, strerror (errno));
1318 sprintf (boundbox, "0 0 %d %d",
1319 x->w / (PSUS / 72) + (x->w % (PSUS / 72) > 0),
1320 x->l / (PSUS / 72) + (x->l % (PSUS / 72) > 0));
1321 dict[0].value = boundbox;
1323 dict[1].value = (char *) version;
1325 curtime = time (NULL);
1326 loctime = localtime (&curtime);
1327 dict[2].value = asctime (loctime);
1328 cp = strchr (dict[2].value, '\n');
1335 dict[3].value = "Clean7Bit";
1338 dict[3].value = "Clean8Bit";
1341 dict[3].value = "Binary";
1347 if (x->orientation == OTN_PORTRAIT)
1348 dict[4].value = "Portrait";
1350 dict[4].value = "Landscape";
1352 /* PORTME: Determine username, net address. */
1354 dict[5].value = getenv ("LOGNAME");
1356 dict[5].value = getlogin ();
1358 dict[5].value = _("nobody");
1360 if (gethostname (host, 128) == -1)
1362 if (errno == ENAMETOOLONG)
1365 strcpy (host, _("nowhere"));
1367 dict[6].value = host;
1368 #else /* !HAVE_UNISTD_H */
1369 dict[5].value = _("nobody");
1370 dict[6].value = _("nowhere");
1371 #endif /* !HAVE_UNISTD_H */
1373 cp = stpcpy (p = local_alloc (288), "font ");
1374 quote_ps_string (cp, x->prop->font->internal_name);
1377 cp = stpcpy (p = local_alloc (288), "font ");
1378 quote_ps_string (cp, x->fixed->font->internal_name);
1381 sprintf (scaling, "%.3f", PSUS / 72.0);
1382 dict[9].value = scaling;
1384 sprintf (paper_width, "%g", x->w / (PSUS / 72.0));
1385 dict[10].value = paper_width;
1387 sprintf (paper_length, "%g", x->l / (PSUS / 72.0));
1388 dict[11].value = paper_length;
1390 sprintf (left_margin, "%d", x->left_margin);
1391 dict[12].value = left_margin;
1393 sprintf (top_margin, "%d", x->top_margin);
1394 dict[13].value = top_margin;
1396 sprintf (line_width, "%d", x->line_width);
1397 dict[14].value = line_width;
1399 sprintf (line_width, "%d", x->line_width_thick);
1400 dict[15].value = line_width_thick;
1402 getl_location (&dict[17].value, NULL);
1403 if (dict[17].value == NULL)
1404 dict[17].value = "<stdin>";
1408 dict[16].value = cp = local_alloc (strlen (dict[17].value) + 30);
1409 sprintf (cp, "PSPP (%s)", dict[17].value);
1413 dict[16].value = local_alloc (strlen (outp_title) + 1);
1414 strcpy ((char *) (dict[16].value), outp_title);
1418 while (-1 != getline (&buf, &buf_size, prologue_file))
1424 cp = strstr (buf, "!eps");
1427 if (this->class->magic == MAGIC_PS)
1434 cp = strstr (buf, "!ps");
1437 if (this->class->magic == MAGIC_EPSF)
1442 if (strstr (buf, "!!!"))
1447 if (!strncmp (buf, "!encodings", 10))
1448 output_encodings (this);
1452 beg = buf2 = fn_interp_vars (buf, ps_get_var);
1453 len = strlen (buf2);
1454 while (isspace (*beg))
1456 if (beg[len - 1] == '\n')
1458 if (beg[len - 1] == '\r')
1460 fwrite (beg, len, 1, f->file);
1461 fputs (x->eol, f->file);
1465 if (ferror (f->file))
1466 msg (IE, _("Reading `%s': %s."), prologue_fn, strerror (errno));
1467 fclose (prologue_file);
1472 local_free (dict[7].value);
1473 local_free (dict[8].value);
1474 local_free (dict[16].value);
1476 if (ferror (f->file))
1479 msg (VM (2), _("%s: PostScript prologue read successfully."), this->name);
1483 msg (VM (1), _("%s: Error reading PostScript prologue."), this->name);
1487 /* Writes the string STRING to buffer DEST (of at least 288
1488 characters) as a PostScript name object. Returns a pointer
1489 to the null terminator of the resultant string. */
1491 quote_ps_name (char *dest, const char *string)
1495 for (sp = string; *sp; sp++)
1496 switch (*(unsigned char *) sp)
1569 for (sp = string; *sp && dp < &dest[256]; sp++)
1571 sprintf (dp, "%02x", *(unsigned char *) sp);
1574 return stpcpy (dp, ">cvn");
1578 return stpcpy (&dest[1], string);
1581 /* Adds the string STRING to buffer DEST as a PostScript quoted
1582 string; returns a pointer to the null terminator added. Will not
1583 add more than 235 characters. */
1585 quote_ps_string (char *dest, const char *string)
1587 const char *sp = string;
1591 for (; *sp && dp < &dest[235]; sp++)
1593 dp = stpcpy (dp, "\\(");
1594 else if (*sp == ')')
1595 dp = stpcpy (dp, "\\)");
1596 else if (*sp < 32 || *((unsigned char *) sp) > 127)
1597 dp = spprintf (dp, "\\%3o", *sp);
1600 return stpcpy (dp, ")");
1603 /* Writes the PostScript epilogue to file F. */
1605 preclose (struct file_ext *f)
1607 struct outp_driver *this = f->param;
1608 struct ps_driver_ext *x = this->ext;
1609 struct hsh_iterator iter;
1610 struct font_entry *fe;
1615 "%%%%DocumentNeededResources:%s"),
1616 x->eol, x->file_page_number, x->eol, x->eol);
1618 for (fe = hsh_first (x->loaded, &iter); fe != NULL;
1619 fe = hsh_next (x->loaded, &iter))
1623 cp = stpcpy (buf, "%%+ font ");
1624 cp = quote_ps_string (cp, fe->font->internal_name);
1625 strcpy (cp, x->eol);
1626 fputs (buf, f->file);
1629 hsh_destroy (x->loaded);
1631 hsh_destroy (x->combos);
1633 x->last_font = NULL;
1636 fprintf (f->file, "%%EOF%s", x->eol);
1637 if (ferror (f->file))
1643 ps_open_page (struct outp_driver *this)
1645 struct ps_driver_ext *x = this->ext;
1647 assert (this->driver_open && !this->page_open);
1650 if (!fn_open_ext (&x->file))
1653 msg (ME, _("PostScript output driver: %s: %s"), x->file.filename,
1657 x->file_page_number++;
1659 hsh_destroy (x->combos);
1660 x->combos = hsh_create (31, compare_ps_combo, hash_ps_combo,
1661 free_ps_combo, NULL);
1662 x->last_font = NULL;
1665 fprintf (x->file.file,
1667 "%%%%BeginPageSetup%s"
1668 "/pg save def 0.001 dup scale%s",
1669 x->page_number, x->file_page_number, x->eol,
1673 if (x->orientation == OTN_LANDSCAPE)
1674 fprintf (x->file.file,
1675 "%d 0 translate 90 rotate%s",
1678 if (x->bottom_margin != 0 || x->left_margin != 0)
1679 fprintf (x->file.file,
1680 "%d %d translate%s",
1681 x->left_margin, x->bottom_margin, x->eol);
1683 fprintf (x->file.file,
1684 "/LW %d def/TW %d def %d setlinewidth%s"
1685 "%%%%EndPageSetup%s",
1686 x->line_width, x->line_width_thick, x->line_width, x->eol,
1689 if (!ferror (x->file.file))
1691 this->page_open = 1;
1692 if (x->output_options & OPO_HEADERS)
1693 draw_headers (this);
1696 return !ferror (x->file.file);
1700 ps_close_page (struct outp_driver *this)
1702 struct ps_driver_ext *x = this->ext;
1704 assert (this->driver_open && this->page_open);
1709 fprintf (x->file.file,
1714 this->page_open = 0;
1715 return !ferror (x->file.file);
1720 /* qsort() comparison function for int tuples. */
1722 int_2_compare (const void *a_, const void *b_)
1727 return *a < *b ? -1 : *a > *b;
1730 /* Hash table comparison function for cached lines. */
1732 compare_line (const void *a_, const void *b_, void *foo UNUSED)
1734 const struct line_form *a = a_;
1735 const struct line_form *b = b_;
1737 return a->ind < b->ind ? -1 : a->ind > b->ind;
1740 /* Hash table hash function for cached lines. */
1742 hash_line (const void *pa, void *foo UNUSED)
1744 const struct line_form *a = pa;
1749 /* Hash table free function for cached lines. */
1751 free_line (void *pa, void *foo UNUSED)
1756 /* Writes PostScript code to draw a line from (x1,y1) to (x2,y2) to
1758 #define dump_line(x1, y1, x2, y2) \
1759 fprintf (ext->file.file, "%d %d %d %d L%s", \
1760 x1, YT (y1), x2, YT (y2), ext->eol)
1762 /* Write PostScript code to draw a thick line from (x1,y1) to (x2,y2)
1763 to the output file. */
1764 #define dump_thick_line(x1, y1, x2, y2) \
1765 fprintf (ext->file.file, "%d %d %d %d TL%s", \
1766 x1, YT (y1), x2, YT (y2), ext->eol)
1768 /* Writes a line of type TYPE to THIS driver's output file. The line
1769 (or its center, in the case of double lines) has its independent
1770 axis coordinate at IND; it extends from DEP1 to DEP2 on the
1773 dump_fancy_line (struct outp_driver *this, int type, int ind, int dep1, int dep2)
1775 struct ps_driver_ext *ext = this->ext;
1776 int ofs = ext->line_space / 2 + ext->line_width / 2;
1781 dump_line (dep1, ind, dep2, ind);
1784 if (ext->output_options & OPO_DOUBLE_LINE)
1786 dump_line (dep1, ind - ofs, dep2, ind - ofs);
1787 dump_line (dep1, ind + ofs, dep2, ind + ofs);
1790 dump_thick_line (dep1, ind, dep2, ind);
1795 dump_line (ind, dep1, ind, dep2);
1798 if (ext->output_options & OPO_DOUBLE_LINE)
1800 dump_line (ind - ofs, dep1, ind - ofs, dep2);
1801 dump_line (ind + ofs, dep1, ind + ofs, dep2);
1804 dump_thick_line (ind, dep1, ind, dep2);
1815 /* Writes all the cached lines to the output file, then clears the
1818 dump_lines (struct outp_driver *this)
1820 struct ps_driver_ext *x = this->ext;
1822 struct hsh_iterator iter;
1825 for (type = 0; type < n_line_types; type++)
1827 struct line_form *line;
1829 if (x->lines[type] == NULL)
1832 for (line = hsh_first (x->lines[type], &iter); line != NULL;
1833 line = hsh_next (x->lines[type], &iter))
1836 int lo = INT_MIN, hi;
1838 qsort (line->dep, line->ndep, sizeof *line->dep, int_2_compare);
1839 lo = line->dep[0][0];
1840 hi = line->dep[0][1];
1841 for (i = 1; i < line->ndep; i++)
1842 if (line->dep[i][0] <= hi + 1)
1844 int min_hi = line->dep[i][1];
1850 dump_fancy_line (this, type, line->ind, lo, hi);
1851 lo = line->dep[i][0];
1852 hi = line->dep[i][1];
1854 dump_fancy_line (this, type, line->ind, lo, hi);
1857 hsh_destroy (x->lines[type]);
1858 x->lines[type] = NULL;
1862 /* (Same args as dump_fancy_line()). Either dumps the line directly
1863 to the output file, or adds it to the cache, depending on the
1864 user-selected line optimization mode. */
1866 line (struct outp_driver *this, int type, int ind, int dep1, int dep2)
1868 struct ps_driver_ext *ext = this->ext;
1869 struct line_form **f;
1871 assert (dep2 >= dep1);
1872 if (ext->line_opt == 0)
1874 dump_fancy_line (this, type, ind, dep1, dep2);
1878 if (ext->lines[type] == NULL)
1879 ext->lines[type] = hsh_create (31, compare_line, hash_line,
1881 f = (struct line_form **) hsh_probe (ext->lines[type], &ind);
1884 *f = xmalloc (sizeof **f + sizeof (int[15][2]));
1888 (*f)->dep[0][0] = dep1;
1889 (*f)->dep[0][1] = dep2;
1892 if ((*f)->ndep >= (*f)->mdep)
1895 *f = xrealloc (*f, (sizeof **f + sizeof (int[2]) * ((*f)->mdep - 1)));
1897 (*f)->dep[(*f)->ndep][0] = dep1;
1898 (*f)->dep[(*f)->ndep][1] = dep2;
1903 ps_line_horz (struct outp_driver *this, const struct rect *r,
1904 const struct color *c UNUSED, int style)
1906 /* Must match output.h:OUTP_L_*. */
1907 static const int types[OUTP_L_COUNT] =
1908 {-1, horz, dbl_horz, spl_horz};
1910 int y = (r->y1 + r->y2) / 2;
1912 assert (this->driver_open && this->page_open);
1913 assert (style >= 0 && style < OUTP_L_COUNT);
1914 style = types[style];
1916 line (this, style, y, r->x1, r->x2);
1920 ps_line_vert (struct outp_driver *this, const struct rect *r,
1921 const struct color *c UNUSED, int style)
1923 /* Must match output.h:OUTP_L_*. */
1924 static const int types[OUTP_L_COUNT] =
1925 {-1, vert, dbl_vert, spl_vert};
1927 int x = (r->x1 + r->x2) / 2;
1929 assert (this->driver_open && this->page_open);
1930 assert (style >= 0 && style < OUTP_L_COUNT);
1931 style = types[style];
1933 line (this, style, x, r->y1, r->y2);
1936 #define L (style->l != OUTP_L_NONE)
1937 #define R (style->r != OUTP_L_NONE)
1938 #define T (style->t != OUTP_L_NONE)
1939 #define B (style->b != OUTP_L_NONE)
1942 ps_line_intersection (struct outp_driver *this, const struct rect *r,
1943 const struct color *c UNUSED,
1944 const struct outp_styles *style)
1946 struct ps_driver_ext *ext = this->ext;
1948 int x = (r->x1 + r->x2) / 2;
1949 int y = (r->y1 + r->y2) / 2;
1950 int ofs = (ext->line_space + ext->line_width) / 2;
1951 int x1 = x - ofs, x2 = x + ofs;
1952 int y1 = y - ofs, y2 = y + ofs;
1954 assert (this->driver_open && this->page_open);
1955 assert (!((style->l != style->r && style->l != OUTP_L_NONE
1956 && style->r != OUTP_L_NONE)
1957 || (style->t != style->b && style->t != OUTP_L_NONE
1958 && style->b != OUTP_L_NONE)));
1960 switch ((style->l | style->r) | ((style->t | style->b) << 8))
1962 case (OUTP_L_SINGLE) | (OUTP_L_SINGLE << 8):
1963 case (OUTP_L_SINGLE) | (OUTP_L_NONE << 8):
1964 case (OUTP_L_NONE) | (OUTP_L_SINGLE << 8):
1966 line (this, horz, y, r->x1, x);
1968 line (this, horz, y, x, r->x2);
1970 line (this, vert, x, r->y1, y);
1972 line (this, vert, x, y, r->y2);
1974 case (OUTP_L_SINGLE) | (OUTP_L_DOUBLE << 8):
1975 case (OUTP_L_NONE) | (OUTP_L_DOUBLE << 8):
1977 line (this, horz, y, r->x1, x1);
1979 line (this, horz, y, x2, r->x2);
1981 line (this, dbl_vert, x, r->y1, y);
1983 line (this, dbl_vert, x, y, r->y2);
1984 if ((L && R) && !(T && B))
1985 line (this, horz, y, x1, x2);
1987 case (OUTP_L_DOUBLE) | (OUTP_L_SINGLE << 8):
1988 case (OUTP_L_DOUBLE) | (OUTP_L_NONE << 8):
1990 line (this, dbl_horz, y, r->x1, x);
1992 line (this, dbl_horz, y, x, r->x2);
1994 line (this, vert, x, r->y1, y);
1996 line (this, vert, x, y, r->y2);
1997 if ((T && B) && !(L && R))
1998 line (this, vert, x, y1, y2);
2000 case (OUTP_L_DOUBLE) | (OUTP_L_DOUBLE << 8):
2002 line (this, dbl_horz, y, r->x1, x);
2004 line (this, dbl_horz, y, x, r->x2);
2006 line (this, dbl_vert, x, r->y1, y);
2008 line (this, dbl_vert, x, y, r->y2);
2010 line (this, vert, x1, y1, y2);
2012 line (this, vert, x2, y1, y2);
2014 line (this, horz, y1, x1, x2);
2016 line (this, horz, y2, x1, x2);
2024 ps_box (struct outp_driver *this UNUSED, const struct rect *r UNUSED,
2025 const struct color *bord UNUSED, const struct color *fill UNUSED)
2027 assert (this->driver_open && this->page_open);
2031 ps_polyline_begin (struct outp_driver *this UNUSED,
2032 const struct color *c UNUSED)
2034 assert (this->driver_open && this->page_open);
2037 ps_polyline_point (struct outp_driver *this UNUSED, int x UNUSED, int y UNUSED)
2039 assert (this->driver_open && this->page_open);
2042 ps_polyline_end (struct outp_driver *this UNUSED)
2044 assert (this->driver_open && this->page_open);
2047 /* Returns the width of string S for THIS driver. */
2049 text_width (struct outp_driver *this, char *s)
2051 struct outp_text text;
2053 text.options = OUTP_T_JUST_LEFT;
2054 ls_init (&text.s, s, strlen (s));
2055 this->class->text_metrics (this, &text);
2059 /* Write string S at location (X,Y) with width W for THIS driver. */
2061 out_text_plain (struct outp_driver *this, char *s, int x, int y, int w)
2063 struct outp_text text;
2065 text.options = OUTP_T_JUST_LEFT | OUTP_T_HORZ | OUTP_T_VERT;
2066 ls_init (&text.s, s, strlen (s));
2068 text.v = this->font_height;
2071 this->class->text_draw (this, &text);
2074 /* Draw top of page headers for THIS driver. */
2076 draw_headers (struct outp_driver *this)
2078 struct ps_driver_ext *ext = this->ext;
2080 struct font_entry *old_current = ext->current;
2081 char *old_family = xstrdup (ext->family); /* FIXME */
2082 int old_size = ext->size;
2084 int fh = this->font_height;
2087 fprintf (ext->file.file, "%d %d %d %d GB%s",
2088 0, YT (y), this->width, YT (y + 2 * fh + ext->line_gutter),
2090 this->class->text_set_font_family (this, "T");
2092 y += ext->line_width + ext->line_gutter;
2098 sprintf (buf, _("%s - Page %d"), curdate, ext->page_number);
2099 rh_width = text_width (this, buf);
2101 out_text_plain (this, buf, this->width - this->prop_em_width - rh_width,
2104 if (outp_title && outp_subtitle)
2105 out_text_plain (this, outp_title, this->prop_em_width, y,
2106 this->width - 3 * this->prop_em_width - rh_width);
2114 char *string = outp_subtitle ? outp_subtitle : outp_title;
2116 sprintf (buf, "%s - %s", version, host_system);
2117 rh_width = text_width (this, buf);
2119 out_text_plain (this, buf, this->width - this->prop_em_width - rh_width,
2123 out_text_plain (this, string, this->prop_em_width, y,
2124 this->width - 3 * this->prop_em_width - rh_width);
2129 ext->current = old_current;
2131 ext->family = old_family;
2132 ext->size = old_size;
2139 ps_text_set_font_by_name (struct outp_driver *this, const char *dit)
2141 struct ps_driver_ext *x = this->ext;
2142 struct font_entry *fe;
2144 assert (this->driver_open && this->page_open);
2146 /* Short-circuit common fonts. */
2147 if (!strcmp (dit, "PROP"))
2149 x->current = x->prop;
2150 x->size = x->font_size;
2153 else if (!strcmp (dit, "FIXED"))
2155 x->current = x->fixed;
2156 x->size = x->font_size;
2160 /* Find font_desc corresponding to Groff name dit. */
2161 fe = hsh_find (x->loaded, &dit);
2163 fe = load_font (this, dit);
2168 ps_text_set_font_by_position (struct outp_driver *this, int pos)
2170 struct ps_driver_ext *x = this->ext;
2173 assert (this->driver_open && this->page_open);
2175 /* Determine font name by suffixing position string to font family
2180 dit = local_alloc (strlen (x->family) + 3);
2181 cp = stpcpy (dit, x->family);
2203 /* Find font_desc corresponding to Groff name dit. */
2205 struct font_entry *fe = hsh_find (x->loaded, &dit);
2207 fe = load_font (this, dit);
2215 ps_text_set_font_family (struct outp_driver *this, const char *s)
2217 struct ps_driver_ext *x = this->ext;
2219 assert (this->driver_open && this->page_open);
2222 x->family = xstrdup (s);
2226 ps_text_get_font_name (struct outp_driver *this)
2228 struct ps_driver_ext *x = this->ext;
2230 assert (this->driver_open && this->page_open);
2231 return x->current->font->name;
2235 ps_text_get_font_family (struct outp_driver *this)
2237 struct ps_driver_ext *x = this->ext;
2239 assert (this->driver_open && this->page_open);
2244 ps_text_set_size (struct outp_driver *this, int size)
2246 struct ps_driver_ext *x = this->ext;
2248 assert (this->driver_open && this->page_open);
2249 x->size = PSUS / 72000 * size;
2254 ps_text_get_size (struct outp_driver *this, int *em_width)
2256 struct ps_driver_ext *x = this->ext;
2258 assert (this->driver_open && this->page_open);
2260 *em_width = (x->current->font->space_width * x->size) / 1000;
2261 return x->size / (PSUS / 72000);
2264 /* An output character. */
2267 struct font_entry *font; /* Font of character. */
2268 int size; /* Size of character. */
2269 int x, y; /* Location of character. */
2270 unsigned char ch; /* Character. */
2271 char separate; /* Must be separate from previous char. */
2274 /* Hash table comparison function for ps_combo structs. */
2276 compare_ps_combo (const void *pa, const void *pb, void *foo UNUSED)
2278 const struct ps_font_combo *a = pa;
2279 const struct ps_font_combo *b = pb;
2281 return !((a->font == b->font) && (a->size == b->size));
2284 /* Hash table hash function for ps_combo structs. */
2286 hash_ps_combo (const void *pa, void *foo UNUSED)
2288 const struct ps_font_combo *a = pa;
2289 unsigned name_hash = hsh_hash_string (a->font->font->internal_name);
2290 return name_hash ^ hsh_hash_int (a->size);
2293 /* Hash table free function for ps_combo structs. */
2295 free_ps_combo (void *a, void *foo UNUSED)
2300 /* Causes PostScript code to be output that switches to the font
2301 CP->FONT and font size CP->SIZE. The first time a particular
2302 font/size combination is used on a particular page, this involves
2303 outputting PostScript code to load the font. */
2305 switch_font (struct outp_driver *this, const struct output_char *cp)
2307 struct ps_driver_ext *ext = this->ext;
2308 struct ps_font_combo srch, **fc;
2310 srch.font = cp->font;
2311 srch.size = cp->size;
2313 fc = (struct ps_font_combo **) hsh_probe (ext->combos, &srch);
2316 fprintf (ext->file.file, "F%x%s", (*fc)->index, ext->eol);
2321 struct ps_encoding *encoding;
2324 *fc = xmalloc (sizeof **fc);
2325 (*fc)->font = cp->font;
2326 (*fc)->size = cp->size;
2327 (*fc)->index = ext->next_combo++;
2329 filename = find_encoding_file (this, cp->font->font->encoding);
2332 encoding = get_encoding (this, filename);
2337 msg (IE, _("PostScript driver: Cannot find encoding `%s' for "
2338 "PostScript font `%s'."), cp->font->font->encoding,
2339 cp->font->font->internal_name);
2340 encoding = default_encoding (this);
2343 if (cp->font != ext->fixed && cp->font != ext->prop)
2345 bp = stpcpy (buf, "%%IncludeResource: font ");
2346 bp = quote_ps_string (bp, cp->font->font->internal_name);
2347 bp = stpcpy (bp, ext->eol);
2352 bp = spprintf (bp, "/F%x E%x %d", (*fc)->index, encoding->index,
2354 bp = quote_ps_name (bp, cp->font->font->internal_name);
2355 sprintf (bp, " SF%s", ext->eol);
2356 fputs (buf, ext->file.file);
2358 ext->last_font = *fc;
2361 /* (write_text) Writes the accumulated line buffer to the output
2363 #define output_line() \
2366 lp = stpcpy (lp, ext->eol); \
2368 fputs (line, ext->file.file); \
2373 /* (write_text) Adds the string representing number X to the line
2374 buffer, flushing the buffer to disk beforehand if necessary. */
2375 #define put_number(X) \
2378 int n = nsprintf (number, "%d", X); \
2379 if (n + lp > &line[75]) \
2381 lp = stpcpy (lp, number); \
2385 /* Outputs PostScript code to THIS driver's output file to display the
2386 characters represented by the output_char's between CP and END,
2387 using the associated outp_text T to determine formatting. WIDTH is
2388 the width of the output region; WIDTH_LEFT is the amount of the
2389 WIDTH that is not taken up by text (so it can be used to determine
2392 write_text (struct outp_driver *this,
2393 const struct output_char *cp, const struct output_char *end,
2394 struct outp_text *t, int width UNUSED, int width_left)
2396 struct ps_driver_ext *ext = this->ext;
2401 char number[INT_DIGITS + 1];
2405 switch (t->options & OUTP_T_JUST_MASK)
2407 case OUTP_T_JUST_LEFT:
2410 case OUTP_T_JUST_RIGHT:
2413 case OUTP_T_JUST_CENTER:
2414 ofs = width_left / 2;
2425 int x = cp->x + ofs;
2426 int y = cp->y + (cp->font->font->ascent * cp->size / 1000);
2428 if (ext->last_font == NULL
2429 || cp->font != ext->last_font->font
2430 || cp->size != ext->last_font->size)
2431 switch_font (this, cp);
2437 static unsigned char literal_chars[ODA_COUNT][32] =
2439 {0x00, 0x00, 0x00, 0xf8, 0xff, 0xfc, 0xff, 0xff,
2440 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f,
2441 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2442 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2444 {0x00, 0x00, 0x00, 0xf8, 0xff, 0xfc, 0xff, 0xff,
2445 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
2446 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
2447 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
2449 {0x7e, 0xd6, 0xff, 0xfb, 0xff, 0xfc, 0xff, 0xff,
2450 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
2451 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
2452 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
2456 if (TEST_BIT (literal_chars[ext->data], cp->ch))
2462 lp = stpcpy (lp, "\\(");
2465 lp = stpcpy (lp, "\\)");
2468 lp = spprintf (lp, "\\%03o", cp->ch);
2473 while (cp < end && lp < &line[70] && cp->separate == 0);
2481 put_number (YT (y));
2492 if (lp >= &line[70])
2502 /* Displays the text in outp_text T, if DRAW is nonzero; or, merely
2503 determine the text metrics, if DRAW is zero. */
2505 text (struct outp_driver *this, struct outp_text *t, int draw)
2507 struct ps_driver_ext *ext = this->ext;
2510 struct output_char *buf; /* Output buffer. */
2511 struct output_char *buf_end; /* End of output buffer. */
2512 struct output_char *buf_loc; /* Current location in output buffer. */
2515 struct font_entry *old_current = ext->current;
2516 char *old_family = xstrdup (ext->family); /* FIXME */
2517 int old_size = ext->size;
2522 /* Current location. */
2525 /* Keeping track of what's left over. */
2526 int width; /* Width available for characters. */
2527 int width_left, height_left; /* Width, height left over. */
2528 int max_height; /* Tallest character on this line so far. */
2530 /* Previous character. */
2533 /* Information about location of previous space. */
2534 char *space_char; /* Character after space. */
2535 struct output_char *space_buf_loc; /* Buffer location after space. */
2536 int space_width_left; /* Width of characters before space. */
2538 /* Name of the current character. */
2539 const char *char_name;
2540 char local_char_name[2] = {0, 0};
2542 local_char_name[0] = local_char_name[1] = 0;
2544 buf = local_alloc (sizeof *buf * 128);
2545 buf_end = &buf[128];
2548 assert (!ls_null_p (&t->s));
2549 cp = ls_c_str (&t->s);
2550 end = ls_end (&t->s);
2558 width = width_left = (t->options & OUTP_T_HORZ) ? t->h : INT_MAX;
2559 height_left = (t->options & OUTP_T_VERT) ? t->v : INT_MAX;
2563 space_buf_loc = NULL;
2564 space_width_left = 0;
2567 if (!width || !height_left)
2572 struct char_metrics *metric;
2578 /* Set char_name to the name of the character or ligature at
2580 local_char_name[0] = *cp;
2581 char_name = local_char_name;
2582 if (ext->current->font->ligatures && *cp == 'f')
2591 lig = LIG_fi, char_name = "fi";
2594 lig = LIG_fl, char_name = "fl";
2601 lig = LIG_ffi, char_name = "ffi";
2604 lig = LIG_ffl, char_name = "ffl";
2607 lig = LIG_ff, char_name = "ff";
2611 if ((lig & ext->current->font->ligatures) == 0)
2613 local_char_name[0] = *cp; /* 'f' */
2614 char_name = local_char_name;
2617 else if (*cp == '\n')
2621 write_text (this, buf, buf_loc, t, width, width_left);
2628 height_left -= max_height;
2634 /* FIXME: when we're page buffering it will be necessary to
2635 set separate to 1. */
2638 cp += strlen (char_name);
2640 /* Figure out what size this character is, and what kern
2641 adjustment we need. */
2642 cur_char = font_char_name_to_index (char_name);
2643 metric = font_get_char_metrics (ext->current->font, cur_char);
2646 static struct char_metrics m;
2648 m.width = ext->current->font->space_width;
2649 m.code = *char_name;
2651 kern_amt = font_get_kern_adjust (ext->current->font, prev_char,
2655 kern_amt = (kern_amt * ext->size / 1000);
2658 char_width = metric->width * ext->size / 1000;
2660 /* Record the current status if this is a space character. */
2661 if (cur_char == space_index && buf_loc > buf)
2664 space_buf_loc = buf_loc;
2665 space_width_left = width_left;
2668 /* Drop down to a new line if there's no room left on this
2670 if (char_width + kern_amt > width_left)
2672 /* Regress to previous space, if any. */
2676 width_left = space_width_left;
2677 buf_loc = space_buf_loc;
2682 write_text (this, buf, buf_loc, t, width, width_left);
2689 height_left -= max_height;
2697 /* FIXME: when we're page buffering it will be
2698 necessary to set separate to 1. */
2703 if (ext->size > max_height)
2704 max_height = ext->size;
2705 if (max_height > height_left)
2708 /* Actually draw the character. */
2711 if (buf_loc >= buf_end)
2713 int buf_len = buf_end - buf;
2717 struct output_char *new_buf;
2719 new_buf = xmalloc (sizeof *new_buf * 256);
2720 memcpy (new_buf, buf, sizeof *new_buf * 128);
2721 buf_loc = new_buf + 128;
2722 buf_end = new_buf + 256;
2728 buf = xrealloc (buf, sizeof *buf * buf_len * 2);
2729 buf_loc = buf + buf_len;
2730 buf_end = buf + buf_len * 2;
2735 buf_loc->font = ext->current;
2736 buf_loc->size = ext->size;
2739 buf_loc->ch = metric->code;
2740 buf_loc->separate = separate;
2745 /* Prepare for next iteration. */
2746 width_left -= char_width + kern_amt;
2747 prev_char = cur_char;
2749 height_left -= max_height;
2750 if (buf_loc > buf && draw)
2751 write_text (this, buf, buf_loc, t, width, width_left);
2754 if (!(t->options & OUTP_T_HORZ))
2755 t->h = INT_MAX - width_left;
2756 if (!(t->options & OUTP_T_VERT))
2757 t->v = INT_MAX - height_left;
2759 t->v -= height_left;
2760 if (buf_end - buf == 128)
2764 ext->current = old_current;
2766 ext->family = old_family;
2767 ext->size = old_size;
2771 ps_text_metrics (struct outp_driver *this, struct outp_text *t)
2773 assert (this->driver_open && this->page_open);
2778 ps_text_draw (struct outp_driver *this, struct outp_text *t)
2780 assert (this->driver_open && this->page_open);
2786 /* Translate a filename to a font. */
2787 struct filename2font
2789 char *filename; /* Normalized filename. */
2790 struct font_desc *font;
2793 /* Table of `filename2font's. */
2794 static struct hsh_table *ps_fonts;
2796 /* Hash table comparison function for filename2font structs. */
2798 compare_filename2font (const void *a, const void *b, void *param UNUSED)
2800 return strcmp (((struct filename2font *) a)->filename,
2801 ((struct filename2font *) b)->filename);
2804 /* Hash table hash function for filename2font structs. */
2806 hash_filename2font (const void *f2f_, void *param UNUSED)
2808 const struct filename2font *f2f = f2f_;
2809 return hsh_hash_string (f2f->filename);
2812 /* Initializes the global font list by creating the hash table for
2813 translation of filenames to font_desc structs. */
2817 ps_fonts = hsh_create (31, compare_filename2font, hash_filename2font,
2824 hsh_destroy (ps_fonts);
2827 /* Loads the font having Groff name DIT into THIS driver instance.
2828 Specifically, adds it into the THIS driver's `loaded' hash
2830 static struct font_entry *
2831 load_font (struct outp_driver *this, const char *dit)
2833 struct ps_driver_ext *x = this->ext;
2834 char *filename1, *filename2;
2836 struct font_entry *fe;
2838 filename1 = find_ps_file (this, dit);
2840 filename1 = xstrdup (dit);
2841 filename2 = fn_normalize (filename1);
2844 entry = hsh_probe (ps_fonts, &filename2);
2847 struct filename2font *f2f;
2848 struct font_desc *f = groff_read_font (filename2);
2855 f = default_font ();
2858 f2f = xmalloc (sizeof *f2f);
2859 f2f->filename = filename2;
2866 fe = xmalloc (sizeof *fe);
2867 fe->dit = xstrdup (dit);
2868 fe->font = ((struct filename2font *) * entry)->font;
2869 *hsh_probe (x->loaded, &dit) = fe;
2874 /* PostScript driver class. */
2875 struct outp_class postscript_class =
2897 ps_line_intersection,
2904 ps_text_set_font_by_name,
2905 ps_text_set_font_by_position,
2906 ps_text_set_font_family,
2907 ps_text_get_font_name,
2908 ps_text_get_font_family,
2918 /* EPSF driver class. FIXME: Probably doesn't work right. */
2919 struct outp_class epsf_class =
2941 ps_line_intersection,
2948 ps_text_set_font_by_name,
2949 ps_text_set_font_by_position,
2950 ps_text_set_font_family,
2951 ps_text_get_font_name,
2952 ps_text_get_font_family,
2962 #endif /* NO_POSTSCRIPT */