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_c_str (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;
667 x->output_options |= mask;
669 x->output_options &= ~mask;
678 arg = strtol (value, &tail, 0);
679 if (arg < 1 || errno == ERANGE || *tail)
681 msg (SE, _("Positive integer required as value for `%s'."), key);
684 if ((subcat == 4 || subcat == 5) && arg < 1000)
686 msg (SE, _("Default font size must be at least 1 point (value "
687 "of 1000 for key `%s')."), key);
693 x->left_margin = arg;
696 x->right_margin = arg;
702 x->bottom_margin = arg;
714 int dimension = outp_evaluate_dimension (value, NULL);
718 msg (SE, _("Value for `%s' must be a dimension of positive "
719 "length (i.e., `1in')."), key);
725 x->line_width = dimension;
728 x->line_gutter = dimension;
731 x->line_width = dimension;
734 x->line_width_thick = dimension;
750 dest = &x->prologue_fn;
756 dest = &x->encoding_fn;
759 dest = &x->prop_family;
762 dest = &x->fixed_family;
770 *dest = xstrdup (value);
779 arg = strtol (value, &tail, 0);
780 if (arg < 0 || errno == ERANGE || *tail)
782 msg (SE, _("Nonnegative integer required as value for `%s'."), key);
806 /* Looks for a PostScript font file or config file in all the
807 appropriate places. Returns the filename on success, NULL on
809 /* PORTME: Filename operations. */
811 find_ps_file (struct outp_driver *this, const char *name)
813 struct ps_driver_ext *x = this->ext;
816 /* x->font_dir + name: "devps/ps-encodings". */
819 /* Usually equal to groff_font_path. */
822 /* Final filename. */
826 basename = local_alloc (strlen (x->font_dir) + 1 + strlen (name) + 1);
827 cp = stpcpy (basename, x->font_dir);
828 *cp++ = DIR_SEPARATOR;
831 /* Decide on search path. */
833 const char *pre_pathname;
835 pre_pathname = getenv ("STAT_GROFF_FONT_PATH");
836 if (pre_pathname == NULL)
837 pre_pathname = getenv ("GROFF_FONT_PATH");
838 if (pre_pathname == NULL)
839 pre_pathname = groff_font_path;
840 pathname = fn_tilde_expand (pre_pathname);
843 /* Search all possible places for the file. */
844 fn = fn_search_path (basename, pathname, NULL);
846 fn = fn_search_path (basename, config_path, NULL);
848 fn = fn_search_path (name, pathname, NULL);
850 fn = fn_search_path (name, config_path, NULL);
852 local_free (basename);
859 /* Hash table comparison function for ps_encoding's. */
861 compare_ps_encoding (const void *pa, const void *pb, void *foo UNUSED)
863 const struct ps_encoding *a = pa;
864 const struct ps_encoding *b = pb;
866 return strcmp (a->filename, b->filename);
869 /* Hash table hash function for ps_encoding's. */
871 hash_ps_encoding (const void *pa, void *foo UNUSED)
873 const struct ps_encoding *a = pa;
875 return hsh_hash_string (a->filename);
878 /* Hash table free function for ps_encoding's. */
880 free_ps_encoding (void *pa, void *foo UNUSED)
882 struct ps_encoding *a = pa;
888 /* Iterates through the list of encodings used for this driver
889 instance, reads each of them from disk, and writes them as
890 PostScript code to the output file. */
892 output_encodings (struct outp_driver *this)
894 struct ps_driver_ext *x = this->ext;
896 struct hsh_iterator iter;
897 struct ps_encoding *pe;
899 struct string line, buf;
901 ds_init (&line, 128);
903 for (pe = hsh_first (x->encodings, &iter); pe != NULL;
904 pe = hsh_next (x->encodings, &iter))
908 msg (VM (1), _("%s: %s: Opening PostScript font encoding..."),
909 this->name, pe->filename);
911 f = fopen (pe->filename, "r");
914 msg (IE, _("PostScript driver: Cannot open encoding file `%s': %s. "
915 "Substituting ISOLatin1Encoding for missing encoding."),
916 pe->filename, strerror (errno));
917 fprintf (x->file.file, "/E%x ISOLatin1Encoding def%s",
922 struct file_locator where;
924 const char *tab[256];
931 const char *notdef = ".notdef";
935 for (i = 0; i < 256; i++)
938 where.filename = pe->filename;
939 where.line_number = 0;
940 err_push_file_locator (&where);
942 while (ds_get_config_line (f, &buf, &where))
949 pschar = strtok_r (ds_c_str (&buf), " \t\r\n", &sp);
950 code = strtok_r (NULL, " \t\r\n", &sp);
951 if (*pschar == 0 || *code == 0)
953 code_val = strtol (code, &fubar, 0);
956 msg (IS, _("PostScript driver: Invalid numeric format."));
959 if (code_val < 0 || code_val > 255)
961 msg (IS, _("PostScript driver: Codes must be between 0 "
962 "and 255. (%d is not allowed.)"), code_val);
965 tab[code_val] = local_alloc (strlen (pschar) + 1);
966 strcpy ((char *) (tab[code_val]), pschar);
968 err_pop_file_locator (&where);
971 ds_printf (&line, "/E%x[", pe->index);
972 for (i = 0; i < 257; i++)
978 quote_ps_name (temp, tab[i]);
979 if (tab[i] != notdef)
983 strcpy (temp, "]def");
985 if (ds_length (&line) + strlen (temp) > 70)
987 ds_puts (&line, x->eol);
988 fputs (ds_c_str (&line), x->file.file);
991 ds_puts (&line, temp);
993 ds_puts (&line, x->eol);
994 fputs (ds_c_str (&line), x->file.file);
996 if (fclose (f) == EOF)
997 msg (MW, _("PostScript driver: Error closing encoding file `%s'."),
1000 msg (VM (2), _("%s: PostScript font encoding read successfully."),
1008 /* Finds the ps_encoding in THIS that corresponds to the file with
1009 name NORM_FILENAME, which must have previously been normalized with
1010 normalize_filename(). */
1011 static struct ps_encoding *
1012 get_encoding (struct outp_driver *this, const char *norm_filename)
1014 struct ps_driver_ext *x = this->ext;
1015 struct ps_encoding *pe;
1017 pe = (struct ps_encoding *) hsh_find (x->encodings, (void *) &norm_filename);
1021 /* Searches the filesystem for an encoding file with name FILENAME;
1022 returns its malloc'd, normalized name if found, otherwise NULL. */
1024 find_encoding_file (struct outp_driver *this, char *filename)
1028 if (filename == NULL)
1030 while (isspace ((unsigned char) *filename))
1032 for (cp = filename; *cp && !isspace ((unsigned char) *cp); cp++)
1038 temp = find_ps_file (this, filename);
1042 filename = fn_normalize (temp);
1043 assert (filename != NULL);
1049 /* Adds the encoding represented by the not-necessarily-normalized
1050 file FILENAME to the list of encodings, if it exists and is not
1051 already in the list. */
1053 add_encoding (struct outp_driver *this, char *filename)
1055 struct ps_driver_ext *x = this->ext;
1056 struct ps_encoding **pe;
1058 filename = find_encoding_file (this, filename);
1062 pe = (struct ps_encoding **) hsh_probe (x->encodings, &filename);
1068 *pe = xmalloc (sizeof **pe);
1069 (*pe)->filename = filename;
1070 (*pe)->index = x->next_encoding++;
1073 /* Finds the file on disk that contains the list of encodings to
1074 include in the output file, then adds those encodings to the list
1077 read_ps_encodings (struct outp_driver *this)
1079 struct ps_driver_ext *x = this->ext;
1081 /* Encodings file. */
1082 char *encoding_fn; /* `ps-encodings' filename. */
1086 struct file_locator where;
1088 /* It's okay if there's no list of encodings; not everyone cares. */
1089 encoding_fn = find_ps_file (this, x->encoding_fn);
1090 if (encoding_fn == NULL)
1094 msg (VM (1), _("%s: %s: Opening PostScript encoding list file."),
1095 this->name, encoding_fn);
1096 f = fopen (encoding_fn, "r");
1099 msg (IE, _("Opening %s: %s."), encoding_fn, strerror (errno));
1103 where.filename = encoding_fn;
1104 where.line_number = 0;
1105 err_push_file_locator (&where);
1107 ds_init (&line, 128);
1111 if (!ds_get_config_line (f, &line, &where))
1114 msg (ME, _("Reading %s: %s."), encoding_fn, strerror (errno));
1118 add_encoding (this, line.string);
1122 err_pop_file_locator (&where);
1124 if (-1 == fclose (f))
1125 msg (MW, _("Closing %s: %s."), encoding_fn, strerror (errno));
1127 msg (VM (2), _("%s: PostScript encoding list file read successfully."), this->name);
1130 /* Creates a default encoding for driver D that can be substituted for
1131 an unavailable encoding. */
1132 struct ps_encoding *
1133 default_encoding (struct outp_driver *d)
1135 struct ps_driver_ext *x = d->ext;
1136 static struct ps_encoding *enc;
1140 enc = xmalloc (sizeof *enc);
1141 enc->filename = xstrdup (_("<<default encoding>>"));
1142 enc->index = x->next_encoding++;
1147 /* Basic file operations. */
1149 /* Variables for the prologue. */
1156 static struct ps_variable *ps_var_tab;
1158 /* Searches ps_var_tab for a ps_variable with key KEY, and returns the
1159 associated value. */
1161 ps_get_var (const char *key)
1163 struct ps_variable *v;
1165 for (v = ps_var_tab; v->key; v++)
1166 if (!strcmp (key, v->key))
1171 /* Writes the PostScript prologue to file F. */
1173 postopen (struct file_ext *f)
1175 static struct ps_variable dict[] =
1177 {"bounding-box", 0},
1186 {"scale-factor", 0},
1188 {"paper-length", 0},
1192 {"line-width-thick", 0},
1197 char boundbox[INT_DIGITS * 4 + 4];
1201 char scaling[INT_DIGITS + 5];
1205 char paper_width[INT_DIGITS + 1];
1206 char paper_length[INT_DIGITS + 1];
1207 char left_margin[INT_DIGITS + 1];
1208 char top_margin[INT_DIGITS + 1];
1209 char line_width[INT_DIGITS + 1];
1210 char line_width_thick[INT_DIGITS + 1];
1212 struct outp_driver *this = f->param;
1213 struct ps_driver_ext *x = this->ext;
1215 char *prologue_fn = find_ps_file (this, x->prologue_fn);
1216 FILE *prologue_file;
1219 size_t buf_size = 0;
1221 x->loaded = hsh_create (31, compare_font_entry, hash_font_entry,
1222 free_font_entry, NULL);
1225 char *font_name = local_alloc (2 + max (strlen (x->prop_family),
1226 strlen (x->fixed_family)));
1228 strcpy (stpcpy (font_name, x->prop_family), "R");
1229 x->prop = load_font (this, font_name);
1231 strcpy (stpcpy (font_name, x->fixed_family), "R");
1232 x->fixed = load_font (this, font_name);
1234 local_free(font_name);
1237 x->current = x->prop;
1238 x->family = xstrdup (x->prop_family);
1239 x->size = x->font_size;
1242 int *h = this->horiz_line_width, *v = this->vert_line_width;
1244 this->cp_x = this->cp_y = 0;
1245 this->font_height = x->font_size;
1247 struct char_metrics *metric;
1249 metric = font_get_char_metrics (x->prop->font, '0');
1250 this->prop_em_width = ((metric
1251 ? metric->width : x->prop->font->space_width)
1252 * x->font_size / 1000);
1254 metric = font_get_char_metrics (x->fixed->font, '0');
1255 this->fixed_width = ((metric
1256 ? metric->width : x->fixed->font->space_width)
1257 * x->font_size / 1000);
1261 h[1] = v[1] = 2 * x->line_gutter + x->line_width;
1262 if (x->output_options & OPO_DOUBLE_LINE)
1263 h[2] = v[2] = 2 * x->line_gutter + 2 * x->line_width + x->line_space;
1265 h[2] = v[2] = 2 * x->line_gutter + x->line_width_thick;
1266 h[3] = v[3] = 2 * x->line_gutter + x->line_width;
1271 for (i = 0; i < (1 << OUTP_L_COUNT); i++)
1275 /* Maximum width of any line type so far. */
1278 for (bit = 0; bit < OUTP_L_COUNT; bit++)
1279 if ((i & (1 << bit)) && h[bit] > max)
1281 this->horiz_line_spacing[i] = this->vert_line_spacing[i] = max;
1286 if (x->output_options & OPO_AUTO_ENCODE)
1288 /* It's okay if this is done more than once since add_encoding()
1289 is idempotent over identical encodings. */
1290 add_encoding (this, x->prop->font->encoding);
1291 add_encoding (this, x->fixed->font->encoding);
1294 x->file_page_number = 0;
1297 if (prologue_fn == NULL)
1299 msg (IE, _("Cannot find PostScript prologue. The use of `-vv' "
1300 "on the command line is suggested as a debugging aid."));
1304 msg (VM (1), _("%s: %s: Opening PostScript prologue..."),
1305 this->name, prologue_fn);
1306 prologue_file = fopen (prologue_fn, "rb");
1307 if (prologue_file == NULL)
1309 fclose (prologue_file);
1311 msg (IE, "%s: %s", prologue_fn, strerror (errno));
1315 sprintf (boundbox, "0 0 %d %d",
1316 x->w / (PSUS / 72) + (x->w % (PSUS / 72) > 0),
1317 x->l / (PSUS / 72) + (x->l % (PSUS / 72) > 0));
1318 dict[0].value = boundbox;
1320 dict[1].value = (char *) version;
1322 curtime = time (NULL);
1323 loctime = localtime (&curtime);
1324 dict[2].value = asctime (loctime);
1325 cp = strchr (dict[2].value, '\n');
1332 dict[3].value = "Clean7Bit";
1335 dict[3].value = "Clean8Bit";
1338 dict[3].value = "Binary";
1344 if (x->orientation == OTN_PORTRAIT)
1345 dict[4].value = "Portrait";
1347 dict[4].value = "Landscape";
1349 /* PORTME: Determine username, net address. */
1351 dict[5].value = getenv ("LOGNAME");
1353 dict[5].value = getlogin ();
1355 dict[5].value = _("nobody");
1357 if (gethostname (host, 128) == -1)
1359 if (errno == ENAMETOOLONG)
1362 strcpy (host, _("nowhere"));
1364 dict[6].value = host;
1365 #else /* !HAVE_UNISTD_H */
1366 dict[5].value = _("nobody");
1367 dict[6].value = _("nowhere");
1368 #endif /* !HAVE_UNISTD_H */
1370 cp = stpcpy (p = local_alloc (288), "font ");
1371 quote_ps_string (cp, x->prop->font->internal_name);
1374 cp = stpcpy (p = local_alloc (288), "font ");
1375 quote_ps_string (cp, x->fixed->font->internal_name);
1378 sprintf (scaling, "%.3f", PSUS / 72.0);
1379 dict[9].value = scaling;
1381 sprintf (paper_width, "%g", x->w / (PSUS / 72.0));
1382 dict[10].value = paper_width;
1384 sprintf (paper_length, "%g", x->l / (PSUS / 72.0));
1385 dict[11].value = paper_length;
1387 sprintf (left_margin, "%d", x->left_margin);
1388 dict[12].value = left_margin;
1390 sprintf (top_margin, "%d", x->top_margin);
1391 dict[13].value = top_margin;
1393 sprintf (line_width, "%d", x->line_width);
1394 dict[14].value = line_width;
1396 sprintf (line_width, "%d", x->line_width_thick);
1397 dict[15].value = line_width_thick;
1399 getl_location (&dict[17].value, NULL);
1400 if (dict[17].value == NULL)
1401 dict[17].value = "<stdin>";
1405 dict[16].value = cp = local_alloc (strlen (dict[17].value) + 30);
1406 sprintf (cp, "PSPP (%s)", dict[17].value);
1410 dict[16].value = local_alloc (strlen (outp_title) + 1);
1411 strcpy ((char *) (dict[16].value), outp_title);
1415 while (-1 != getline (&buf, &buf_size, prologue_file))
1421 cp = strstr (buf, "!eps");
1424 if (this->class->magic == MAGIC_PS)
1431 cp = strstr (buf, "!ps");
1434 if (this->class->magic == MAGIC_EPSF)
1439 if (strstr (buf, "!!!"))
1444 if (!strncmp (buf, "!encodings", 10))
1445 output_encodings (this);
1449 beg = buf2 = fn_interp_vars (buf, ps_get_var);
1450 len = strlen (buf2);
1451 while (isspace (*beg))
1453 if (beg[len - 1] == '\n')
1455 if (beg[len - 1] == '\r')
1457 fwrite (beg, len, 1, f->file);
1458 fputs (x->eol, f->file);
1462 if (ferror (f->file))
1463 msg (IE, _("Reading `%s': %s."), prologue_fn, strerror (errno));
1464 fclose (prologue_file);
1469 local_free (dict[7].value);
1470 local_free (dict[8].value);
1471 local_free (dict[16].value);
1473 if (ferror (f->file))
1476 msg (VM (2), _("%s: PostScript prologue read successfully."), this->name);
1480 msg (VM (1), _("%s: Error reading PostScript prologue."), this->name);
1484 /* Writes the string STRING to buffer DEST (of at least 288
1485 characters) as a PostScript name object. Returns a pointer
1486 to the null terminator of the resultant string. */
1488 quote_ps_name (char *dest, const char *string)
1492 for (sp = string; *sp; sp++)
1493 switch (*(unsigned char *) sp)
1566 for (sp = string; *sp && dp < &dest[256]; sp++)
1568 sprintf (dp, "%02x", *(unsigned char *) sp);
1571 return stpcpy (dp, ">cvn");
1575 return stpcpy (&dest[1], string);
1578 /* Adds the string STRING to buffer DEST as a PostScript quoted
1579 string; returns a pointer to the null terminator added. Will not
1580 add more than 235 characters. */
1582 quote_ps_string (char *dest, const char *string)
1584 const char *sp = string;
1588 for (; *sp && dp < &dest[235]; sp++)
1590 dp = stpcpy (dp, "\\(");
1591 else if (*sp == ')')
1592 dp = stpcpy (dp, "\\)");
1593 else if (*sp < 32 || *((unsigned char *) sp) > 127)
1594 dp = spprintf (dp, "\\%3o", *sp);
1597 return stpcpy (dp, ")");
1600 /* Writes the PostScript epilogue to file F. */
1602 preclose (struct file_ext *f)
1604 struct outp_driver *this = f->param;
1605 struct ps_driver_ext *x = this->ext;
1606 struct hsh_iterator iter;
1607 struct font_entry *fe;
1612 "%%%%DocumentNeededResources:%s"),
1613 x->eol, x->file_page_number, x->eol, x->eol);
1615 for (fe = hsh_first (x->loaded, &iter); fe != NULL;
1616 fe = hsh_next (x->loaded, &iter))
1620 cp = stpcpy (buf, "%%+ font ");
1621 cp = quote_ps_string (cp, fe->font->internal_name);
1622 strcpy (cp, x->eol);
1623 fputs (buf, f->file);
1626 hsh_destroy (x->loaded);
1628 hsh_destroy (x->combos);
1630 x->last_font = NULL;
1633 fprintf (f->file, "%%EOF%s", x->eol);
1634 if (ferror (f->file))
1640 ps_open_page (struct outp_driver *this)
1642 struct ps_driver_ext *x = this->ext;
1644 assert (this->driver_open && !this->page_open);
1647 if (!fn_open_ext (&x->file))
1650 msg (ME, _("PostScript output driver: %s: %s"), x->file.filename,
1654 x->file_page_number++;
1656 hsh_destroy (x->combos);
1657 x->combos = hsh_create (31, compare_ps_combo, hash_ps_combo,
1658 free_ps_combo, NULL);
1659 x->last_font = NULL;
1662 fprintf (x->file.file,
1664 "%%%%BeginPageSetup%s"
1665 "/pg save def 0.001 dup scale%s",
1666 x->page_number, x->file_page_number, x->eol,
1670 if (x->orientation == OTN_LANDSCAPE)
1671 fprintf (x->file.file,
1672 "%d 0 translate 90 rotate%s",
1675 if (x->bottom_margin != 0 || x->left_margin != 0)
1676 fprintf (x->file.file,
1677 "%d %d translate%s",
1678 x->left_margin, x->bottom_margin, x->eol);
1680 fprintf (x->file.file,
1681 "/LW %d def/TW %d def %d setlinewidth%s"
1682 "%%%%EndPageSetup%s",
1683 x->line_width, x->line_width_thick, x->line_width, x->eol,
1686 if (!ferror (x->file.file))
1688 this->page_open = 1;
1689 if (x->output_options & OPO_HEADERS)
1690 draw_headers (this);
1693 return !ferror (x->file.file);
1697 ps_close_page (struct outp_driver *this)
1699 struct ps_driver_ext *x = this->ext;
1701 assert (this->driver_open && this->page_open);
1706 fprintf (x->file.file,
1711 this->page_open = 0;
1712 return !ferror (x->file.file);
1717 /* qsort() comparison function for int tuples. */
1719 int_2_compare (const void *a_, const void *b_)
1724 return *a < *b ? -1 : *a > *b;
1727 /* Hash table comparison function for cached lines. */
1729 compare_line (const void *a_, const void *b_, void *foo UNUSED)
1731 const struct line_form *a = a_;
1732 const struct line_form *b = b_;
1734 return a->ind < b->ind ? -1 : a->ind > b->ind;
1737 /* Hash table hash function for cached lines. */
1739 hash_line (const void *pa, void *foo UNUSED)
1741 const struct line_form *a = pa;
1746 /* Hash table free function for cached lines. */
1748 free_line (void *pa, void *foo UNUSED)
1753 /* Writes PostScript code to draw a line from (x1,y1) to (x2,y2) to
1755 #define dump_line(x1, y1, x2, y2) \
1756 fprintf (ext->file.file, "%d %d %d %d L%s", \
1757 x1, YT (y1), x2, YT (y2), ext->eol)
1759 /* Write PostScript code to draw a thick line from (x1,y1) to (x2,y2)
1760 to the output file. */
1761 #define dump_thick_line(x1, y1, x2, y2) \
1762 fprintf (ext->file.file, "%d %d %d %d TL%s", \
1763 x1, YT (y1), x2, YT (y2), ext->eol)
1765 /* Writes a line of type TYPE to THIS driver's output file. The line
1766 (or its center, in the case of double lines) has its independent
1767 axis coordinate at IND; it extends from DEP1 to DEP2 on the
1770 dump_fancy_line (struct outp_driver *this, int type, int ind, int dep1, int dep2)
1772 struct ps_driver_ext *ext = this->ext;
1773 int ofs = ext->line_space / 2 + ext->line_width / 2;
1778 dump_line (dep1, ind, dep2, ind);
1781 if (ext->output_options & OPO_DOUBLE_LINE)
1783 dump_line (dep1, ind - ofs, dep2, ind - ofs);
1784 dump_line (dep1, ind + ofs, dep2, ind + ofs);
1787 dump_thick_line (dep1, ind, dep2, ind);
1792 dump_line (ind, dep1, ind, dep2);
1795 if (ext->output_options & OPO_DOUBLE_LINE)
1797 dump_line (ind - ofs, dep1, ind - ofs, dep2);
1798 dump_line (ind + ofs, dep1, ind + ofs, dep2);
1801 dump_thick_line (ind, dep1, ind, dep2);
1812 /* Writes all the cached lines to the output file, then clears the
1815 dump_lines (struct outp_driver *this)
1817 struct ps_driver_ext *x = this->ext;
1819 struct hsh_iterator iter;
1822 for (type = 0; type < n_line_types; type++)
1824 struct line_form *line;
1826 if (x->lines[type] == NULL)
1829 for (line = hsh_first (x->lines[type], &iter); line != NULL;
1830 line = hsh_next (x->lines[type], &iter))
1833 int lo = INT_MIN, hi;
1835 qsort (line->dep, line->ndep, sizeof *line->dep, int_2_compare);
1836 lo = line->dep[0][0];
1837 hi = line->dep[0][1];
1838 for (i = 1; i < line->ndep; i++)
1839 if (line->dep[i][0] <= hi + 1)
1841 int min_hi = line->dep[i][1];
1847 dump_fancy_line (this, type, line->ind, lo, hi);
1848 lo = line->dep[i][0];
1849 hi = line->dep[i][1];
1851 dump_fancy_line (this, type, line->ind, lo, hi);
1854 hsh_destroy (x->lines[type]);
1855 x->lines[type] = NULL;
1859 /* (Same args as dump_fancy_line()). Either dumps the line directly
1860 to the output file, or adds it to the cache, depending on the
1861 user-selected line optimization mode. */
1863 line (struct outp_driver *this, int type, int ind, int dep1, int dep2)
1865 struct ps_driver_ext *ext = this->ext;
1866 struct line_form **f;
1868 assert (dep2 >= dep1);
1869 if (ext->line_opt == 0)
1871 dump_fancy_line (this, type, ind, dep1, dep2);
1875 if (ext->lines[type] == NULL)
1876 ext->lines[type] = hsh_create (31, compare_line, hash_line,
1878 f = (struct line_form **) hsh_probe (ext->lines[type], &ind);
1881 *f = xmalloc (sizeof **f + sizeof (int[15][2]));
1885 (*f)->dep[0][0] = dep1;
1886 (*f)->dep[0][1] = dep2;
1889 if ((*f)->ndep >= (*f)->mdep)
1892 *f = xrealloc (*f, (sizeof **f + sizeof (int[2]) * ((*f)->mdep - 1)));
1894 (*f)->dep[(*f)->ndep][0] = dep1;
1895 (*f)->dep[(*f)->ndep][1] = dep2;
1900 ps_line_horz (struct outp_driver *this, const struct rect *r,
1901 const struct color *c UNUSED, int style)
1903 /* Must match output.h:OUTP_L_*. */
1904 static const int types[OUTP_L_COUNT] =
1905 {-1, horz, dbl_horz, spl_horz};
1907 int y = (r->y1 + r->y2) / 2;
1909 assert (this->driver_open && this->page_open);
1910 assert (style >= 0 && style < OUTP_L_COUNT);
1911 style = types[style];
1913 line (this, style, y, r->x1, r->x2);
1917 ps_line_vert (struct outp_driver *this, const struct rect *r,
1918 const struct color *c UNUSED, int style)
1920 /* Must match output.h:OUTP_L_*. */
1921 static const int types[OUTP_L_COUNT] =
1922 {-1, vert, dbl_vert, spl_vert};
1924 int x = (r->x1 + r->x2) / 2;
1926 assert (this->driver_open && this->page_open);
1927 assert (style >= 0 && style < OUTP_L_COUNT);
1928 style = types[style];
1930 line (this, style, x, r->y1, r->y2);
1933 #define L (style->l != OUTP_L_NONE)
1934 #define R (style->r != OUTP_L_NONE)
1935 #define T (style->t != OUTP_L_NONE)
1936 #define B (style->b != OUTP_L_NONE)
1939 ps_line_intersection (struct outp_driver *this, const struct rect *r,
1940 const struct color *c UNUSED,
1941 const struct outp_styles *style)
1943 struct ps_driver_ext *ext = this->ext;
1945 int x = (r->x1 + r->x2) / 2;
1946 int y = (r->y1 + r->y2) / 2;
1947 int ofs = (ext->line_space + ext->line_width) / 2;
1948 int x1 = x - ofs, x2 = x + ofs;
1949 int y1 = y - ofs, y2 = y + ofs;
1951 assert (this->driver_open && this->page_open);
1952 assert (!((style->l != style->r && style->l != OUTP_L_NONE
1953 && style->r != OUTP_L_NONE)
1954 || (style->t != style->b && style->t != OUTP_L_NONE
1955 && style->b != OUTP_L_NONE)));
1957 switch ((style->l | style->r) | ((style->t | style->b) << 8))
1959 case (OUTP_L_SINGLE) | (OUTP_L_SINGLE << 8):
1960 case (OUTP_L_SINGLE) | (OUTP_L_NONE << 8):
1961 case (OUTP_L_NONE) | (OUTP_L_SINGLE << 8):
1963 line (this, horz, y, r->x1, x);
1965 line (this, horz, y, x, r->x2);
1967 line (this, vert, x, r->y1, y);
1969 line (this, vert, x, y, r->y2);
1971 case (OUTP_L_SINGLE) | (OUTP_L_DOUBLE << 8):
1972 case (OUTP_L_NONE) | (OUTP_L_DOUBLE << 8):
1974 line (this, horz, y, r->x1, x1);
1976 line (this, horz, y, x2, r->x2);
1978 line (this, dbl_vert, x, r->y1, y);
1980 line (this, dbl_vert, x, y, r->y2);
1981 if ((L && R) && !(T && B))
1982 line (this, horz, y, x1, x2);
1984 case (OUTP_L_DOUBLE) | (OUTP_L_SINGLE << 8):
1985 case (OUTP_L_DOUBLE) | (OUTP_L_NONE << 8):
1987 line (this, dbl_horz, y, r->x1, x);
1989 line (this, dbl_horz, y, x, r->x2);
1991 line (this, vert, x, r->y1, y);
1993 line (this, vert, x, y, r->y2);
1994 if ((T && B) && !(L && R))
1995 line (this, vert, x, y1, y2);
1997 case (OUTP_L_DOUBLE) | (OUTP_L_DOUBLE << 8):
1999 line (this, dbl_horz, y, r->x1, x);
2001 line (this, dbl_horz, y, x, r->x2);
2003 line (this, dbl_vert, x, r->y1, y);
2005 line (this, dbl_vert, x, y, r->y2);
2007 line (this, vert, x1, y1, y2);
2009 line (this, vert, x2, y1, y2);
2011 line (this, horz, y1, x1, x2);
2013 line (this, horz, y2, x1, x2);
2021 ps_box (struct outp_driver *this UNUSED, const struct rect *r UNUSED,
2022 const struct color *bord UNUSED, const struct color *fill UNUSED)
2024 assert (this->driver_open && this->page_open);
2028 ps_polyline_begin (struct outp_driver *this UNUSED,
2029 const struct color *c UNUSED)
2031 assert (this->driver_open && this->page_open);
2034 ps_polyline_point (struct outp_driver *this UNUSED, int x UNUSED, int y UNUSED)
2036 assert (this->driver_open && this->page_open);
2039 ps_polyline_end (struct outp_driver *this UNUSED)
2041 assert (this->driver_open && this->page_open);
2044 /* Returns the width of string S for THIS driver. */
2046 text_width (struct outp_driver *this, char *s)
2048 struct outp_text text;
2050 text.options = OUTP_T_JUST_LEFT;
2051 ls_init (&text.s, s, strlen (s));
2052 this->class->text_metrics (this, &text);
2056 /* Write string S at location (X,Y) with width W for THIS driver. */
2058 out_text_plain (struct outp_driver *this, char *s, int x, int y, int w)
2060 struct outp_text text;
2062 text.options = OUTP_T_JUST_LEFT | OUTP_T_HORZ | OUTP_T_VERT;
2063 ls_init (&text.s, s, strlen (s));
2065 text.v = this->font_height;
2068 this->class->text_draw (this, &text);
2071 /* Draw top of page headers for THIS driver. */
2073 draw_headers (struct outp_driver *this)
2075 struct ps_driver_ext *ext = this->ext;
2077 struct font_entry *old_current = ext->current;
2078 char *old_family = xstrdup (ext->family); /* FIXME */
2079 int old_size = ext->size;
2081 int fh = this->font_height;
2084 fprintf (ext->file.file, "%d %d %d %d GB%s",
2085 0, YT (y), this->width, YT (y + 2 * fh + ext->line_gutter),
2087 this->class->text_set_font_family (this, "T");
2089 y += ext->line_width + ext->line_gutter;
2095 sprintf (buf, _("%s - Page %d"), curdate, ext->page_number);
2096 rh_width = text_width (this, buf);
2098 out_text_plain (this, buf, this->width - this->prop_em_width - rh_width,
2101 if (outp_title && outp_subtitle)
2102 out_text_plain (this, outp_title, this->prop_em_width, y,
2103 this->width - 3 * this->prop_em_width - rh_width);
2111 char *string = outp_subtitle ? outp_subtitle : outp_title;
2113 sprintf (buf, "%s - %s", version, host_system);
2114 rh_width = text_width (this, buf);
2116 out_text_plain (this, buf, this->width - this->prop_em_width - rh_width,
2120 out_text_plain (this, string, this->prop_em_width, y,
2121 this->width - 3 * this->prop_em_width - rh_width);
2126 ext->current = old_current;
2128 ext->family = old_family;
2129 ext->size = old_size;
2136 ps_text_set_font_by_name (struct outp_driver *this, const char *dit)
2138 struct ps_driver_ext *x = this->ext;
2139 struct font_entry *fe;
2141 assert (this->driver_open && this->page_open);
2143 /* Short-circuit common fonts. */
2144 if (!strcmp (dit, "PROP"))
2146 x->current = x->prop;
2147 x->size = x->font_size;
2150 else if (!strcmp (dit, "FIXED"))
2152 x->current = x->fixed;
2153 x->size = x->font_size;
2157 /* Find font_desc corresponding to Groff name dit. */
2158 fe = hsh_find (x->loaded, &dit);
2160 fe = load_font (this, dit);
2165 ps_text_set_font_by_position (struct outp_driver *this, int pos)
2167 struct ps_driver_ext *x = this->ext;
2170 assert (this->driver_open && this->page_open);
2172 /* Determine font name by suffixing position string to font family
2177 dit = local_alloc (strlen (x->family) + 3);
2178 cp = stpcpy (dit, x->family);
2200 /* Find font_desc corresponding to Groff name dit. */
2202 struct font_entry *fe = hsh_find (x->loaded, &dit);
2204 fe = load_font (this, dit);
2212 ps_text_set_font_family (struct outp_driver *this, const char *s)
2214 struct ps_driver_ext *x = this->ext;
2216 assert (this->driver_open && this->page_open);
2219 x->family = xstrdup (s);
2223 ps_text_get_font_name (struct outp_driver *this)
2225 struct ps_driver_ext *x = this->ext;
2227 assert (this->driver_open && this->page_open);
2228 return x->current->font->name;
2232 ps_text_get_font_family (struct outp_driver *this)
2234 struct ps_driver_ext *x = this->ext;
2236 assert (this->driver_open && this->page_open);
2241 ps_text_set_size (struct outp_driver *this, int size)
2243 struct ps_driver_ext *x = this->ext;
2245 assert (this->driver_open && this->page_open);
2246 x->size = PSUS / 72000 * size;
2251 ps_text_get_size (struct outp_driver *this, int *em_width)
2253 struct ps_driver_ext *x = this->ext;
2255 assert (this->driver_open && this->page_open);
2257 *em_width = (x->current->font->space_width * x->size) / 1000;
2258 return x->size / (PSUS / 72000);
2261 /* An output character. */
2264 struct font_entry *font; /* Font of character. */
2265 int size; /* Size of character. */
2266 int x, y; /* Location of character. */
2267 unsigned char ch; /* Character. */
2268 char separate; /* Must be separate from previous char. */
2271 /* Hash table comparison function for ps_combo structs. */
2273 compare_ps_combo (const void *pa, const void *pb, void *foo UNUSED)
2275 const struct ps_font_combo *a = pa;
2276 const struct ps_font_combo *b = pb;
2278 return !((a->font == b->font) && (a->size == b->size));
2281 /* Hash table hash function for ps_combo structs. */
2283 hash_ps_combo (const void *pa, void *foo UNUSED)
2285 const struct ps_font_combo *a = pa;
2286 unsigned name_hash = hsh_hash_string (a->font->font->internal_name);
2287 return name_hash ^ hsh_hash_int (a->size);
2290 /* Hash table free function for ps_combo structs. */
2292 free_ps_combo (void *a, void *foo UNUSED)
2297 /* Causes PostScript code to be output that switches to the font
2298 CP->FONT and font size CP->SIZE. The first time a particular
2299 font/size combination is used on a particular page, this involves
2300 outputting PostScript code to load the font. */
2302 switch_font (struct outp_driver *this, const struct output_char *cp)
2304 struct ps_driver_ext *ext = this->ext;
2305 struct ps_font_combo srch, **fc;
2307 srch.font = cp->font;
2308 srch.size = cp->size;
2310 fc = (struct ps_font_combo **) hsh_probe (ext->combos, &srch);
2313 fprintf (ext->file.file, "F%x%s", (*fc)->index, ext->eol);
2318 struct ps_encoding *encoding;
2321 *fc = xmalloc (sizeof **fc);
2322 (*fc)->font = cp->font;
2323 (*fc)->size = cp->size;
2324 (*fc)->index = ext->next_combo++;
2326 filename = find_encoding_file (this, cp->font->font->encoding);
2329 encoding = get_encoding (this, filename);
2334 msg (IE, _("PostScript driver: Cannot find encoding `%s' for "
2335 "PostScript font `%s'."), cp->font->font->encoding,
2336 cp->font->font->internal_name);
2337 encoding = default_encoding (this);
2340 if (cp->font != ext->fixed && cp->font != ext->prop)
2342 bp = stpcpy (buf, "%%IncludeResource: font ");
2343 bp = quote_ps_string (bp, cp->font->font->internal_name);
2344 bp = stpcpy (bp, ext->eol);
2349 bp = spprintf (bp, "/F%x E%x %d", (*fc)->index, encoding->index,
2351 bp = quote_ps_name (bp, cp->font->font->internal_name);
2352 sprintf (bp, " SF%s", ext->eol);
2353 fputs (buf, ext->file.file);
2355 ext->last_font = *fc;
2358 /* (write_text) Writes the accumulated line buffer to the output
2360 #define output_line() \
2363 lp = stpcpy (lp, ext->eol); \
2365 fputs (line, ext->file.file); \
2370 /* (write_text) Adds the string representing number X to the line
2371 buffer, flushing the buffer to disk beforehand if necessary. */
2372 #define put_number(X) \
2375 int n = nsprintf (number, "%d", X); \
2376 if (n + lp > &line[75]) \
2378 lp = stpcpy (lp, number); \
2382 /* Outputs PostScript code to THIS driver's output file to display the
2383 characters represented by the output_char's between CP and END,
2384 using the associated outp_text T to determine formatting. WIDTH is
2385 the width of the output region; WIDTH_LEFT is the amount of the
2386 WIDTH that is not taken up by text (so it can be used to determine
2389 write_text (struct outp_driver *this,
2390 const struct output_char *cp, const struct output_char *end,
2391 struct outp_text *t, int width UNUSED, int width_left)
2393 struct ps_driver_ext *ext = this->ext;
2398 char number[INT_DIGITS + 1];
2402 switch (t->options & OUTP_T_JUST_MASK)
2404 case OUTP_T_JUST_LEFT:
2407 case OUTP_T_JUST_RIGHT:
2410 case OUTP_T_JUST_CENTER:
2411 ofs = width_left / 2;
2422 int x = cp->x + ofs;
2423 int y = cp->y + (cp->font->font->ascent * cp->size / 1000);
2425 if (ext->last_font == NULL
2426 || cp->font != ext->last_font->font
2427 || cp->size != ext->last_font->size)
2428 switch_font (this, cp);
2434 static unsigned char literal_chars[ODA_COUNT][32] =
2436 {0x00, 0x00, 0x00, 0xf8, 0xff, 0xfc, 0xff, 0xff,
2437 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f,
2438 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2439 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2441 {0x00, 0x00, 0x00, 0xf8, 0xff, 0xfc, 0xff, 0xff,
2442 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
2443 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
2444 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
2446 {0x7e, 0xd6, 0xff, 0xfb, 0xff, 0xfc, 0xff, 0xff,
2447 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
2448 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
2449 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
2453 if (TEST_BIT (literal_chars[ext->data], cp->ch))
2459 lp = stpcpy (lp, "\\(");
2462 lp = stpcpy (lp, "\\)");
2465 lp = spprintf (lp, "\\%03o", cp->ch);
2470 while (cp < end && lp < &line[70] && cp->separate == 0);
2478 put_number (YT (y));
2489 if (lp >= &line[70])
2499 /* Displays the text in outp_text T, if DRAW is nonzero; or, merely
2500 determine the text metrics, if DRAW is zero. */
2502 text (struct outp_driver *this, struct outp_text *t, int draw)
2504 struct ps_driver_ext *ext = this->ext;
2507 struct output_char *buf; /* Output buffer. */
2508 struct output_char *buf_end; /* End of output buffer. */
2509 struct output_char *buf_loc; /* Current location in output buffer. */
2512 struct font_entry *old_current = ext->current;
2513 char *old_family = xstrdup (ext->family); /* FIXME */
2514 int old_size = ext->size;
2519 /* Current location. */
2522 /* Keeping track of what's left over. */
2523 int width; /* Width available for characters. */
2524 int width_left, height_left; /* Width, height left over. */
2525 int max_height; /* Tallest character on this line so far. */
2527 /* Previous character. */
2530 /* Information about location of previous space. */
2531 char *space_char; /* Character after space. */
2532 struct output_char *space_buf_loc; /* Buffer location after space. */
2533 int space_width_left; /* Width of characters before space. */
2535 /* Name of the current character. */
2536 const char *char_name;
2537 char local_char_name[2] = {0, 0};
2539 local_char_name[0] = local_char_name[1] = 0;
2541 buf = local_alloc (sizeof *buf * 128);
2542 buf_end = &buf[128];
2545 assert (!ls_null_p (&t->s));
2546 cp = ls_c_str (&t->s);
2547 end = ls_end (&t->s);
2555 width = width_left = (t->options & OUTP_T_HORZ) ? t->h : INT_MAX;
2556 height_left = (t->options & OUTP_T_VERT) ? t->v : INT_MAX;
2560 space_buf_loc = NULL;
2561 space_width_left = 0;
2564 if (!width || !height_left)
2569 struct char_metrics *metric;
2575 /* Set char_name to the name of the character or ligature at
2577 local_char_name[0] = *cp;
2578 char_name = local_char_name;
2579 if (ext->current->font->ligatures && *cp == 'f')
2588 lig = LIG_fi, char_name = "fi";
2591 lig = LIG_fl, char_name = "fl";
2598 lig = LIG_ffi, char_name = "ffi";
2601 lig = LIG_ffl, char_name = "ffl";
2604 lig = LIG_ff, char_name = "ff";
2608 if ((lig & ext->current->font->ligatures) == 0)
2610 local_char_name[0] = *cp; /* 'f' */
2611 char_name = local_char_name;
2614 else if (*cp == '\n')
2618 write_text (this, buf, buf_loc, t, width, width_left);
2625 height_left -= max_height;
2631 /* FIXME: when we're page buffering it will be necessary to
2632 set separate to 1. */
2635 cp += strlen (char_name);
2637 /* Figure out what size this character is, and what kern
2638 adjustment we need. */
2639 cur_char = font_char_name_to_index (char_name);
2640 metric = font_get_char_metrics (ext->current->font, cur_char);
2643 static struct char_metrics m;
2645 m.width = ext->current->font->space_width;
2646 m.code = *char_name;
2648 kern_amt = font_get_kern_adjust (ext->current->font, prev_char,
2652 kern_amt = (kern_amt * ext->size / 1000);
2655 char_width = metric->width * ext->size / 1000;
2657 /* Record the current status if this is a space character. */
2658 if (cur_char == space_index && buf_loc > buf)
2661 space_buf_loc = buf_loc;
2662 space_width_left = width_left;
2665 /* Drop down to a new line if there's no room left on this
2667 if (char_width + kern_amt > width_left)
2669 /* Regress to previous space, if any. */
2673 width_left = space_width_left;
2674 buf_loc = space_buf_loc;
2679 write_text (this, buf, buf_loc, t, width, width_left);
2686 height_left -= max_height;
2694 /* FIXME: when we're page buffering it will be
2695 necessary to set separate to 1. */
2700 if (ext->size > max_height)
2701 max_height = ext->size;
2702 if (max_height > height_left)
2705 /* Actually draw the character. */
2708 if (buf_loc >= buf_end)
2710 int buf_len = buf_end - buf;
2714 struct output_char *new_buf;
2716 new_buf = xmalloc (sizeof *new_buf * 256);
2717 memcpy (new_buf, buf, sizeof *new_buf * 128);
2718 buf_loc = new_buf + 128;
2719 buf_end = new_buf + 256;
2725 buf = xrealloc (buf, sizeof *buf * buf_len * 2);
2726 buf_loc = buf + buf_len;
2727 buf_end = buf + buf_len * 2;
2732 buf_loc->font = ext->current;
2733 buf_loc->size = ext->size;
2736 buf_loc->ch = metric->code;
2737 buf_loc->separate = separate;
2742 /* Prepare for next iteration. */
2743 width_left -= char_width + kern_amt;
2744 prev_char = cur_char;
2746 height_left -= max_height;
2747 if (buf_loc > buf && draw)
2748 write_text (this, buf, buf_loc, t, width, width_left);
2751 if (!(t->options & OUTP_T_HORZ))
2752 t->h = INT_MAX - width_left;
2753 if (!(t->options & OUTP_T_VERT))
2754 t->v = INT_MAX - height_left;
2756 t->v -= height_left;
2757 if (buf_end - buf == 128)
2761 ext->current = old_current;
2763 ext->family = old_family;
2764 ext->size = old_size;
2768 ps_text_metrics (struct outp_driver *this, struct outp_text *t)
2770 assert (this->driver_open && this->page_open);
2775 ps_text_draw (struct outp_driver *this, struct outp_text *t)
2777 assert (this->driver_open && this->page_open);
2783 /* Translate a filename to a font. */
2784 struct filename2font
2786 char *filename; /* Normalized filename. */
2787 struct font_desc *font;
2790 /* Table of `filename2font's. */
2791 static struct hsh_table *ps_fonts;
2793 /* Hash table comparison function for filename2font structs. */
2795 compare_filename2font (const void *a, const void *b, void *param UNUSED)
2797 return strcmp (((struct filename2font *) a)->filename,
2798 ((struct filename2font *) b)->filename);
2801 /* Hash table hash function for filename2font structs. */
2803 hash_filename2font (const void *f2f_, void *param UNUSED)
2805 const struct filename2font *f2f = f2f_;
2806 return hsh_hash_string (f2f->filename);
2809 /* Initializes the global font list by creating the hash table for
2810 translation of filenames to font_desc structs. */
2814 ps_fonts = hsh_create (31, compare_filename2font, hash_filename2font,
2818 /* Loads the font having Groff name DIT into THIS driver instance.
2819 Specifically, adds it into the THIS driver's `loaded' hash
2821 static struct font_entry *
2822 load_font (struct outp_driver *this, const char *dit)
2824 struct ps_driver_ext *x = this->ext;
2825 char *filename1, *filename2;
2827 struct font_entry *fe;
2829 filename1 = find_ps_file (this, dit);
2831 filename1 = xstrdup (dit);
2832 filename2 = fn_normalize (filename1);
2835 entry = hsh_probe (ps_fonts, &filename2);
2838 struct filename2font *f2f;
2839 struct font_desc *f = groff_read_font (filename2);
2846 f = default_font ();
2849 f2f = xmalloc (sizeof *f2f);
2850 f2f->filename = filename2;
2857 fe = xmalloc (sizeof *fe);
2858 fe->dit = xstrdup (dit);
2859 fe->font = ((struct filename2font *) * entry)->font;
2860 *hsh_probe (x->loaded, &dit) = fe;
2865 /* PostScript driver class. */
2866 struct outp_class postscript_class =
2888 ps_line_intersection,
2895 ps_text_set_font_by_name,
2896 ps_text_set_font_by_position,
2897 ps_text_set_font_family,
2898 ps_text_get_font_name,
2899 ps_text_get_font_family,
2906 /* EPSF driver class. FIXME: Probably doesn't work right. */
2907 struct outp_class epsf_class =
2929 ps_line_intersection,
2936 ps_text_set_font_by_name,
2937 ps_text_set_font_by_position,
2938 ps_text_set_font_family,
2939 ps_text_get_font_name,
2940 ps_text_get_font_family,
2947 #endif /* NO_POSTSCRIPT */