1 /* PSPP - computes sample statistics.
2 Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
3 Written by Ben Pfaff <blp@gnu.org>.
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 This program is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 /*this #if encloses the remainder of the file. */
38 #include "bit-vector.h"
49 #include "start-date.h"
53 #define _(msgid) gettext (msgid)
57 optimize-text-size not implemented.
59 Line buffering is the only possibility; page buffering should also
64 Should add a field to give a file that has a list of fonts
67 Should add an option that tells the driver it can emit %%Include:'s.
69 Should have auto-encode=true stream-edit or whatever to allow
70 addition to list of encodings.
72 Should align fonts of different sizes along their baselines (see
75 /* PostScript driver options: (defaults listed first)
79 data=clean7bit|clean8bit|binary
82 paper-size=letter (see "papersize" file)
83 orientation=portrait|landscape
92 prologue-file=ps-prologue
94 encoding-file=ps-encodings
95 auto-encode=true|false
101 line-style=thick|double
107 optimize-text-size=1|0|2
108 optimize-line-size=1|0
109 max-fonts-simult=0 Max # of fonts in printer memory at once (0=infinite)
112 /* The number of `psus' (PostScript driver UnitS) per inch. Although
113 this is a #define, the value is expected never to change. If it
114 does, review all uses. */
117 /* Magic numbers for PostScript and EPSF drivers. */
127 OTN_PORTRAIT, /* Portrait. */
128 OTN_LANDSCAPE /* Landscape. */
131 /* Output options. */
134 OPO_MIRROR_HORZ = 001, /* 1=Mirror across a horizontal axis. */
135 OPO_MIRROR_VERT = 002, /* 1=Mirror across a vertical axis. */
136 OPO_ROTATE_180 = 004, /* 1=Rotate the page 180 degrees. */
137 OPO_COLOR = 010, /* 1=Enable color. */
138 OPO_HEADERS = 020, /* 1=Draw headers at top of page. */
139 OPO_AUTO_ENCODE = 040, /* 1=Add encodings semi-intelligently. */
140 OPO_DOUBLE_LINE = 0100 /* 1=Double lines instead of thick lines. */
143 /* Data allowed in output. */
146 ODA_CLEAN7BIT, /* 0x09, 0x0a, 0x0d, 0x1b...0x7e */
147 ODA_CLEAN8BIT, /* 0x09, 0x0a, 0x0d, 0x1b...0xff */
148 ODA_BINARY, /* 0x00...0xff */
152 /* Types of lines for purpose of caching. */
155 horz, /* Single horizontal. */
156 dbl_horz, /* Double horizontal. */
157 spl_horz, /* Special horizontal. */
158 vert, /* Single vertical. */
159 dbl_vert, /* Double vertical. */
160 spl_vert, /* Special vertical. */
167 int ind; /* Independent var. Don't reorder. */
168 int mdep; /* Maximum number of dependent var pairs. */
169 int ndep; /* Current number of dependent var pairs. */
170 int dep[1][2]; /* Dependent var pairs. */
173 /* Contents of ps_driver_ext.loaded. */
176 char *dit; /* Font Groff name. */
177 struct font_desc *font; /* Font descriptor. */
180 /* Combines a font with a font size for benefit of generated code. */
183 struct font_entry *font; /* Font. */
184 int size; /* Font size. */
185 int index; /* PostScript index. */
188 /* A font encoding. */
191 char *filename; /* Normalized filename of this encoding. */
192 int index; /* Index value. */
195 /* PostScript output driver extension record. */
198 /* User parameters. */
199 int orientation; /* OTN_PORTRAIT or OTN_LANDSCAPE. */
200 int output_options; /* OPO_*. */
201 int data; /* ODA_*. */
203 int left_margin; /* Left margin in psus. */
204 int right_margin; /* Right margin in psus. */
205 int top_margin; /* Top margin in psus. */
206 int bottom_margin; /* Bottom margin in psus. */
208 char eol[3]; /* End of line--CR, LF, or CRLF. */
210 char *font_dir; /* Font directory relative to font path. */
211 char *prologue_fn; /* Prologue's filename relative to font dir. */
212 char *desc_fn; /* DESC filename relative to font dir. */
213 char *encoding_fn; /* Encoding's filename relative to font dir. */
215 char *prop_family; /* Default proportional font family. */
216 char *fixed_family; /* Default fixed-pitch font family. */
217 int font_size; /* Default font size (psus). */
219 int line_gutter; /* Space around lines. */
220 int line_space; /* Space between lines. */
221 int line_width; /* Width of lines. */
222 int line_width_thick; /* Width of thick lines. */
224 int text_opt; /* Text optimization level. */
225 int line_opt; /* Line optimization level. */
226 int max_fonts; /* Max # of simultaneous fonts (0=infinite). */
228 /* Internal state. */
229 struct file_ext file; /* Output file. */
230 int page_number; /* Current page number. */
231 int file_page_number; /* Page number in this file. */
232 int w, l; /* Paper size. */
233 struct hsh_table *lines[n_line_types]; /* Line buffers. */
235 struct font_entry *prop; /* Default Roman proportional font. */
236 struct font_entry *fixed; /* Default Roman fixed-pitch font. */
237 struct hsh_table *loaded; /* Fonts in memory. */
239 struct hsh_table *combos; /* Combinations of fonts with font sizes. */
240 struct ps_font_combo *last_font; /* PostScript selected font. */
241 int next_combo; /* Next font combo position index. */
243 struct hsh_table *encodings;/* Set of encodings. */
244 int next_encoding; /* Next font encoding index. */
246 /* Currently selected font. */
247 struct font_entry *current; /* Current font. */
248 char *family; /* Font family. */
249 int size; /* Size in psus. */
253 /* Transform logical y-ordinate Y into a page ordinate. */
254 #define YT(Y) (this->length - (Y))
257 static int postopen (struct file_ext *);
258 static int preclose (struct file_ext *);
259 static void draw_headers (struct outp_driver *this);
261 static int compare_font_entry (const void *, const void *, void *param);
262 static unsigned hash_font_entry (const void *, void *param);
263 static void free_font_entry (void *, void *foo);
264 static struct font_entry *load_font (struct outp_driver *, const char *dit);
265 static void init_fonts (void);
266 static void done_fonts (void);
268 static void dump_lines (struct outp_driver *this);
270 static void read_ps_encodings (struct outp_driver *this);
271 static int compare_ps_encoding (const void *pa, const void *pb, void *foo);
272 static unsigned hash_ps_encoding (const void *pa, void *foo);
273 static void free_ps_encoding (void *a, void *foo);
274 static void add_encoding (struct outp_driver *this, char *filename);
275 static struct ps_encoding *default_encoding (struct outp_driver *this);
277 static int compare_ps_combo (const void *pa, const void *pb, void *foo);
278 static unsigned hash_ps_combo (const void *pa, void *foo);
279 static void free_ps_combo (void *a, void *foo);
281 static char *quote_ps_name (char *dest, const char *string);
282 static char *quote_ps_string (char *dest, const char *string);
284 /* Driver initialization. */
287 ps_open_global (struct outp_class *this UNUSED)
295 ps_close_global (struct outp_class *this UNUSED)
303 ps_font_sizes (struct outp_class *this UNUSED, int *n_valid_sizes)
305 /* Allow fonts up to 1" in height. */
306 static int valid_sizes[] =
309 assert (n_valid_sizes != NULL);
315 ps_preopen_driver (struct outp_driver *this)
317 struct ps_driver_ext *x;
321 assert (this->driver_open == 0);
322 msg (VM (1), _("PostScript driver initializing as `%s'..."), this->name);
324 this->ext = x = xmalloc (sizeof *x);
326 this->horiz = this->vert = 1;
327 this->width = this->length = 0;
329 x->orientation = OTN_PORTRAIT;
330 x->output_options = OPO_COLOR | OPO_HEADERS | OPO_AUTO_ENCODE;
331 x->data = ODA_CLEAN7BIT;
333 x->left_margin = x->right_margin =
334 x->top_margin = x->bottom_margin = PSUS / 2;
336 strcpy (x->eol, "\n");
339 x->prologue_fn = NULL;
341 x->encoding_fn = NULL;
343 x->prop_family = NULL;
344 x->fixed_family = NULL;
345 x->font_size = PSUS * 10 / 72;
347 x->line_gutter = PSUS / 144;
348 x->line_space = PSUS / 144;
349 x->line_width = PSUS / 144;
350 x->line_width_thick = PSUS / 48;
356 x->file.filename = NULL;
359 x->file.sequence_no = &x->page_number;
360 x->file.param = this;
361 x->file.postopen = postopen;
362 x->file.preclose = preclose;
366 x->file_page_number = 0;
367 for (i = 0; i < n_line_types; i++)
378 x->encodings = hsh_create (31, compare_ps_encoding, hash_ps_encoding,
379 free_ps_encoding, NULL);
380 x->next_encoding = 0;
390 ps_postopen_driver (struct outp_driver *this)
392 struct ps_driver_ext *x = this->ext;
394 assert (this->driver_open == 0);
396 if (this->width == 0)
398 this->width = PSUS * 17 / 2; /* Defaults to 8.5"x11". */
399 this->length = PSUS * 11;
402 if (x->text_opt == -1)
403 x->text_opt = (this->device & OUTP_DEV_SCREEN) ? 0 : 1;
404 if (x->line_opt == -1)
405 x->line_opt = (this->device & OUTP_DEV_SCREEN) ? 0 : 1;
409 if (x->orientation == OTN_LANDSCAPE)
411 int temp = this->width;
412 this->width = this->length;
415 this->width -= x->left_margin + x->right_margin;
416 this->length -= x->top_margin + x->bottom_margin;
417 if (x->output_options & OPO_HEADERS)
419 this->length -= 3 * x->font_size;
420 x->top_margin += 3 * x->font_size;
422 if (NULL == x->file.filename)
423 x->file.filename = xstrdup ("pspp.ps");
425 if (x->font_dir == NULL)
426 x->font_dir = xstrdup ("devps");
427 if (x->prologue_fn == NULL)
428 x->prologue_fn = xstrdup ("ps-prologue");
429 if (x->desc_fn == NULL)
430 x->desc_fn = xstrdup ("DESC");
431 if (x->encoding_fn == NULL)
432 x->encoding_fn = xstrdup ("ps-encodings");
434 if (x->prop_family == NULL)
435 x->prop_family = xstrdup ("H");
436 if (x->fixed_family == NULL)
437 x->fixed_family = xstrdup ("C");
439 read_ps_encodings (this);
444 if (this->length / x->font_size < 15)
446 msg (SE, _("PostScript driver: The defined page is not long "
447 "enough to hold margins and headers, plus least 15 "
448 "lines of the default fonts. In fact, there's only "
449 "room for %d lines of each font at the default size "
450 "of %d.%03d points."),
451 this->length / x->font_size,
452 x->font_size / 1000, x->font_size % 1000);
456 this->driver_open = 1;
457 msg (VM (2), _("%s: Initialization complete."), this->name);
463 ps_close_driver (struct outp_driver *this)
465 struct ps_driver_ext *x = this->ext;
469 assert (this->driver_open == 1);
470 msg (VM (2), _("%s: Beginning closing..."), this->name);
472 fn_close_ext (&x->file);
473 free (x->file.filename);
475 free (x->prologue_fn);
477 free (x->encoding_fn);
478 free (x->prop_family);
479 free (x->fixed_family);
481 for (i = 0; i < n_line_types; i++)
482 hsh_destroy (x->lines[i]);
483 hsh_destroy (x->encodings);
484 hsh_destroy (x->combos);
485 hsh_destroy (x->loaded);
488 this->driver_open = 0;
489 msg (VM (3), _("%s: Finished closing."), this->name);
494 /* font_entry comparison function for hash tables. */
496 compare_font_entry (const void *a, const void *b, void *foobar UNUSED)
498 return strcmp (((struct font_entry *) a)->dit, ((struct font_entry *) b)->dit);
501 /* font_entry hash function for hash tables. */
503 hash_font_entry (const void *fe_, void *foobar UNUSED)
505 const struct font_entry *fe = fe_;
506 return hsh_hash_string (fe->dit);
509 /* font_entry destructor function for hash tables. */
511 free_font_entry (void *pa, void *foo UNUSED)
513 struct font_entry *a = pa;
518 /* Generic option types. */
528 /* All the options that the PostScript driver supports. */
529 static struct outp_option option_tab[] =
532 {"output-file", 1, 0},
533 {"paper-size", 2, 0},
534 {"orientation", 3, 0},
535 {"color", boolean_arg, 0},
537 {"auto-encode", boolean_arg, 5},
538 {"headers", boolean_arg, 1},
539 {"left-margin", pos_int_arg, 0},
540 {"right-margin", pos_int_arg, 1},
541 {"top-margin", pos_int_arg, 2},
542 {"bottom-margin", pos_int_arg, 3},
543 {"font-dir", string_arg, 0},
544 {"prologue-file", string_arg, 1},
545 {"device-file", string_arg, 2},
546 {"encoding-file", string_arg, 3},
547 {"prop-font-family", string_arg, 5},
548 {"fixed-font-family", string_arg, 6},
549 {"font-size", pos_int_arg, 4},
550 {"optimize-text-size", nonneg_int_arg, 0},
551 {"optimize-line-size", nonneg_int_arg, 1},
552 {"max-fonts-simult", nonneg_int_arg, 2},
554 {"line-style", 7, 0},
555 {"line-width", dimension_arg, 2},
556 {"line-gutter", dimension_arg, 3},
557 {"line-width", dimension_arg, 4},
558 {"line-width-thick", dimension_arg, 5},
562 static struct outp_option_info option_info;
565 ps_option (struct outp_driver *this, const char *key, const struct string *val)
567 struct ps_driver_ext *x = this->ext;
569 char *value = ds_c_str (val);
571 cat = outp_match_keyword (key, option_tab, &option_info, &subcat);
576 msg (SE, _("Unknown configuration parameter `%s' for PostScript device "
580 free (x->file.filename);
581 x->file.filename = xstrdup (value);
584 outp_get_paper_size (value, &this->width, &this->length);
587 if (!strcmp (value, "portrait"))
588 x->orientation = OTN_PORTRAIT;
589 else if (!strcmp (value, "landscape"))
590 x->orientation = OTN_LANDSCAPE;
592 msg (SE, _("Unknown orientation `%s'. Valid orientations are "
593 "`portrait' and `landscape'."), value);
596 if (!strcmp (value, "clean7bit") || !strcmp (value, "Clean7Bit"))
597 x->data = ODA_CLEAN7BIT;
598 else if (!strcmp (value, "clean8bit")
599 || !strcmp (value, "Clean8Bit"))
600 x->data = ODA_CLEAN8BIT;
601 else if (!strcmp (value, "binary") || !strcmp (value, "Binary"))
602 x->data = ODA_BINARY;
604 msg (SE, _("Unknown value for `data'. Valid values are `clean7bit', "
605 "`clean8bit', and `binary'."));
608 if (!strcmp (value, "lf"))
609 strcpy (x->eol, "\n");
610 else if (!strcmp (value, "crlf"))
611 strcpy (x->eol, "\r\n");
613 msg (SE, _("Unknown value for `line-ends'. Valid values are `lf' and "
617 if (!strcmp (value, "thick"))
618 x->output_options &= ~OPO_DOUBLE_LINE;
619 else if (!strcmp (value, "double"))
620 x->output_options |= OPO_DOUBLE_LINE;
622 msg (SE, _("Unknown value for `line-style'. Valid values are `thick' "
630 if (!strcmp (value, "on") || !strcmp (value, "true")
631 || !strcmp (value, "yes") || atoi (value))
633 else if (!strcmp (value, "off") || !strcmp (value, "false")
634 || !strcmp (value, "no") || !strcmp (value, "0"))
638 msg (SE, _("Boolean value expected for %s."), key);
650 mask = OPO_MIRROR_HORZ;
653 mask = OPO_MIRROR_VERT;
656 mask = OPO_ROTATE_180;
659 mask = OPO_AUTO_ENCODE;
666 x->output_options |= mask;
668 x->output_options &= ~mask;
677 arg = strtol (value, &tail, 0);
678 if (arg < 1 || errno == ERANGE || *tail)
680 msg (SE, _("Positive integer required as value for `%s'."), key);
683 if ((subcat == 4 || subcat == 5) && arg < 1000)
685 msg (SE, _("Default font size must be at least 1 point (value "
686 "of 1000 for key `%s')."), key);
692 x->left_margin = arg;
695 x->right_margin = arg;
701 x->bottom_margin = arg;
713 int dimension = outp_evaluate_dimension (value, NULL);
717 msg (SE, _("Value for `%s' must be a dimension of positive "
718 "length (i.e., `1in')."), key);
724 x->line_width = dimension;
727 x->line_gutter = dimension;
730 x->line_width = dimension;
733 x->line_width_thick = dimension;
749 dest = &x->prologue_fn;
755 dest = &x->encoding_fn;
758 dest = &x->prop_family;
761 dest = &x->fixed_family;
769 *dest = xstrdup (value);
778 arg = strtol (value, &tail, 0);
779 if (arg < 0 || errno == ERANGE || *tail)
781 msg (SE, _("Nonnegative integer required as value for `%s'."), key);
805 /* Looks for a PostScript font file or config file in all the
806 appropriate places. Returns the filename on success, NULL on
808 /* PORTME: Filename operations. */
810 find_ps_file (struct outp_driver *this, const char *name)
812 struct ps_driver_ext *x = this->ext;
815 /* x->font_dir + name: "devps/ps-encodings". */
818 /* Usually equal to groff_font_path. */
821 /* Final filename. */
825 basename = local_alloc (strlen (x->font_dir) + 1 + strlen (name) + 1);
826 cp = stpcpy (basename, x->font_dir);
827 *cp++ = DIR_SEPARATOR;
830 /* Decide on search path. */
832 const char *pre_pathname;
834 pre_pathname = getenv ("STAT_GROFF_FONT_PATH");
835 if (pre_pathname == NULL)
836 pre_pathname = getenv ("GROFF_FONT_PATH");
837 if (pre_pathname == NULL)
838 pre_pathname = groff_font_path;
839 pathname = fn_tilde_expand (pre_pathname);
842 /* Search all possible places for the file. */
843 fn = fn_search_path (basename, pathname, NULL);
845 fn = fn_search_path (basename, config_path, NULL);
847 fn = fn_search_path (name, pathname, NULL);
849 fn = fn_search_path (name, config_path, NULL);
851 local_free (basename);
858 /* Hash table comparison function for ps_encoding's. */
860 compare_ps_encoding (const void *pa, const void *pb, void *foo UNUSED)
862 const struct ps_encoding *a = pa;
863 const struct ps_encoding *b = pb;
865 return strcmp (a->filename, b->filename);
868 /* Hash table hash function for ps_encoding's. */
870 hash_ps_encoding (const void *pa, void *foo UNUSED)
872 const struct ps_encoding *a = pa;
874 return hsh_hash_string (a->filename);
877 /* Hash table free function for ps_encoding's. */
879 free_ps_encoding (void *pa, void *foo UNUSED)
881 struct ps_encoding *a = pa;
887 /* Iterates through the list of encodings used for this driver
888 instance, reads each of them from disk, and writes them as
889 PostScript code to the output file. */
891 output_encodings (struct outp_driver *this)
893 struct ps_driver_ext *x = this->ext;
895 struct hsh_iterator iter;
896 struct ps_encoding *pe;
898 struct string line, buf;
900 ds_init (&line, 128);
902 for (pe = hsh_first (x->encodings, &iter); pe != NULL;
903 pe = hsh_next (x->encodings, &iter))
907 msg (VM (1), _("%s: %s: Opening PostScript font encoding..."),
908 this->name, pe->filename);
910 f = fopen (pe->filename, "r");
913 msg (IE, _("PostScript driver: Cannot open encoding file `%s': %s. "
914 "Substituting ISOLatin1Encoding for missing encoding."),
915 pe->filename, strerror (errno));
916 fprintf (x->file.file, "/E%x ISOLatin1Encoding def%s",
921 struct file_locator where;
923 const char *tab[256];
930 const char *notdef = ".notdef";
934 for (i = 0; i < 256; i++)
937 where.filename = pe->filename;
938 where.line_number = 0;
939 err_push_file_locator (&where);
941 while (ds_get_config_line (f, &buf, &where))
948 pschar = strtok_r (ds_c_str (&buf), " \t\r\n", &sp);
949 code = strtok_r (NULL, " \t\r\n", &sp);
950 if (*pschar == 0 || *code == 0)
952 code_val = strtol (code, &fubar, 0);
955 msg (IS, _("PostScript driver: Invalid numeric format."));
958 if (code_val < 0 || code_val > 255)
960 msg (IS, _("PostScript driver: Codes must be between 0 "
961 "and 255. (%d is not allowed.)"), code_val);
964 tab[code_val] = local_alloc (strlen (pschar) + 1);
965 strcpy ((char *) (tab[code_val]), pschar);
967 err_pop_file_locator (&where);
970 ds_printf (&line, "/E%x[", pe->index);
971 for (i = 0; i < 257; i++)
977 quote_ps_name (temp, tab[i]);
978 if (tab[i] != notdef)
982 strcpy (temp, "]def");
984 if (ds_length (&line) + strlen (temp) > 70)
986 ds_puts (&line, x->eol);
987 fputs (ds_c_str (&line), x->file.file);
990 ds_puts (&line, temp);
992 ds_puts (&line, x->eol);
993 fputs (ds_c_str (&line), x->file.file);
995 if (fclose (f) == EOF)
996 msg (MW, _("PostScript driver: Error closing encoding file `%s'."),
999 msg (VM (2), _("%s: PostScript font encoding read successfully."),
1007 /* Finds the ps_encoding in THIS that corresponds to the file with
1008 name NORM_FILENAME, which must have previously been normalized with
1009 normalize_filename(). */
1010 static struct ps_encoding *
1011 get_encoding (struct outp_driver *this, const char *norm_filename)
1013 struct ps_driver_ext *x = this->ext;
1014 struct ps_encoding *pe;
1016 pe = (struct ps_encoding *) hsh_find (x->encodings, (void *) &norm_filename);
1020 /* Searches the filesystem for an encoding file with name FILENAME;
1021 returns its malloc'd, normalized name if found, otherwise NULL. */
1023 find_encoding_file (struct outp_driver *this, char *filename)
1027 if (filename == NULL)
1029 while (isspace ((unsigned char) *filename))
1031 for (cp = filename; *cp && !isspace ((unsigned char) *cp); cp++)
1037 temp = find_ps_file (this, filename);
1041 filename = fn_normalize (temp);
1042 assert (filename != NULL);
1048 /* Adds the encoding represented by the not-necessarily-normalized
1049 file FILENAME to the list of encodings, if it exists and is not
1050 already in the list. */
1052 add_encoding (struct outp_driver *this, char *filename)
1054 struct ps_driver_ext *x = this->ext;
1055 struct ps_encoding **pe;
1057 filename = find_encoding_file (this, filename);
1061 pe = (struct ps_encoding **) hsh_probe (x->encodings, &filename);
1067 *pe = xmalloc (sizeof **pe);
1068 (*pe)->filename = filename;
1069 (*pe)->index = x->next_encoding++;
1072 /* Finds the file on disk that contains the list of encodings to
1073 include in the output file, then adds those encodings to the list
1076 read_ps_encodings (struct outp_driver *this)
1078 struct ps_driver_ext *x = this->ext;
1080 /* Encodings file. */
1081 char *encoding_fn; /* `ps-encodings' filename. */
1085 struct file_locator where;
1087 /* It's okay if there's no list of encodings; not everyone cares. */
1088 encoding_fn = find_ps_file (this, x->encoding_fn);
1089 if (encoding_fn == NULL)
1093 msg (VM (1), _("%s: %s: Opening PostScript encoding list file."),
1094 this->name, encoding_fn);
1095 f = fopen (encoding_fn, "r");
1098 msg (IE, _("Opening %s: %s."), encoding_fn, strerror (errno));
1102 where.filename = encoding_fn;
1103 where.line_number = 0;
1104 err_push_file_locator (&where);
1106 ds_init (&line, 128);
1110 if (!ds_get_config_line (f, &line, &where))
1113 msg (ME, _("Reading %s: %s."), encoding_fn, strerror (errno));
1117 add_encoding (this, line.string);
1121 err_pop_file_locator (&where);
1123 if (-1 == fclose (f))
1124 msg (MW, _("Closing %s: %s."), encoding_fn, strerror (errno));
1126 msg (VM (2), _("%s: PostScript encoding list file read successfully."), this->name);
1129 /* Creates a default encoding for driver D that can be substituted for
1130 an unavailable encoding. */
1131 struct ps_encoding *
1132 default_encoding (struct outp_driver *d)
1134 struct ps_driver_ext *x = d->ext;
1135 static struct ps_encoding *enc;
1139 enc = xmalloc (sizeof *enc);
1140 enc->filename = xstrdup (_("<<default encoding>>"));
1141 enc->index = x->next_encoding++;
1146 /* Basic file operations. */
1148 /* Variables for the prologue. */
1155 static struct ps_variable *ps_var_tab;
1157 /* Searches ps_var_tab for a ps_variable with key KEY, and returns the
1158 associated value. */
1160 ps_get_var (const char *key)
1162 struct ps_variable *v;
1164 for (v = ps_var_tab; v->key; v++)
1165 if (!strcmp (key, v->key))
1170 /* Writes the PostScript prologue to file F. */
1172 postopen (struct file_ext *f)
1174 static struct ps_variable dict[] =
1176 {"bounding-box", 0},
1185 {"scale-factor", 0},
1187 {"paper-length", 0},
1191 {"line-width-thick", 0},
1195 char boundbox[INT_STRLEN_BOUND (int) * 4 + 4];
1199 char scaling[INT_STRLEN_BOUND (int) + 5];
1203 char paper_width[INT_STRLEN_BOUND (int) + 1];
1204 char paper_length[INT_STRLEN_BOUND (int) + 1];
1205 char left_margin[INT_STRLEN_BOUND (int) + 1];
1206 char top_margin[INT_STRLEN_BOUND (int) + 1];
1207 char line_width[INT_STRLEN_BOUND (int) + 1];
1208 char line_width_thick[INT_STRLEN_BOUND (int) + 1];
1210 struct outp_driver *this = f->param;
1211 struct ps_driver_ext *x = this->ext;
1213 char *prologue_fn = find_ps_file (this, x->prologue_fn);
1214 FILE *prologue_file;
1217 size_t buf_size = 0;
1219 x->loaded = hsh_create (31, compare_font_entry, hash_font_entry,
1220 free_font_entry, NULL);
1223 char *font_name = local_alloc (2 + max (strlen (x->prop_family),
1224 strlen (x->fixed_family)));
1226 strcpy (stpcpy (font_name, x->prop_family), "R");
1227 x->prop = load_font (this, font_name);
1229 strcpy (stpcpy (font_name, x->fixed_family), "R");
1230 x->fixed = load_font (this, font_name);
1232 local_free(font_name);
1235 x->current = x->prop;
1236 x->family = xstrdup (x->prop_family);
1237 x->size = x->font_size;
1240 int *h = this->horiz_line_width, *v = this->vert_line_width;
1242 this->cp_x = this->cp_y = 0;
1243 this->font_height = x->font_size;
1245 struct char_metrics *metric;
1247 metric = font_get_char_metrics (x->prop->font, '0');
1248 this->prop_em_width = ((metric
1249 ? metric->width : x->prop->font->space_width)
1250 * x->font_size / 1000);
1252 metric = font_get_char_metrics (x->fixed->font, '0');
1253 this->fixed_width = ((metric
1254 ? metric->width : x->fixed->font->space_width)
1255 * x->font_size / 1000);
1259 h[1] = v[1] = 2 * x->line_gutter + x->line_width;
1260 if (x->output_options & OPO_DOUBLE_LINE)
1261 h[2] = v[2] = 2 * x->line_gutter + 2 * x->line_width + x->line_space;
1263 h[2] = v[2] = 2 * x->line_gutter + x->line_width_thick;
1264 h[3] = v[3] = 2 * x->line_gutter + x->line_width;
1269 for (i = 0; i < (1 << OUTP_L_COUNT); i++)
1273 /* Maximum width of any line type so far. */
1276 for (bit = 0; bit < OUTP_L_COUNT; bit++)
1277 if ((i & (1 << bit)) && h[bit] > max)
1279 this->horiz_line_spacing[i] = this->vert_line_spacing[i] = max;
1284 if (x->output_options & OPO_AUTO_ENCODE)
1286 /* It's okay if this is done more than once since add_encoding()
1287 is idempotent over identical encodings. */
1288 add_encoding (this, x->prop->font->encoding);
1289 add_encoding (this, x->fixed->font->encoding);
1292 x->file_page_number = 0;
1295 if (prologue_fn == NULL)
1297 msg (IE, _("Cannot find PostScript prologue. The use of `-vv' "
1298 "on the command line is suggested as a debugging aid."));
1302 msg (VM (1), _("%s: %s: Opening PostScript prologue..."),
1303 this->name, prologue_fn);
1304 prologue_file = fopen (prologue_fn, "rb");
1305 if (prologue_file == NULL)
1307 fclose (prologue_file);
1309 msg (IE, "%s: %s", prologue_fn, strerror (errno));
1313 sprintf (boundbox, "0 0 %d %d",
1314 x->w / (PSUS / 72) + (x->w % (PSUS / 72) > 0),
1315 x->l / (PSUS / 72) + (x->l % (PSUS / 72) > 0));
1316 dict[0].value = boundbox;
1318 dict[1].value = (char *) version;
1320 curtime = time (NULL);
1321 loctime = localtime (&curtime);
1322 dict[2].value = asctime (loctime);
1323 cp = strchr (dict[2].value, '\n');
1330 dict[3].value = "Clean7Bit";
1333 dict[3].value = "Clean8Bit";
1336 dict[3].value = "Binary";
1342 if (x->orientation == OTN_PORTRAIT)
1343 dict[4].value = "Portrait";
1345 dict[4].value = "Landscape";
1347 /* PORTME: Determine username, net address. */
1349 dict[5].value = getenv ("LOGNAME");
1351 dict[5].value = getlogin ();
1353 dict[5].value = _("nobody");
1355 if (gethostname (host, 128) == -1)
1357 if (errno == ENAMETOOLONG)
1360 strcpy (host, _("nowhere"));
1362 dict[6].value = host;
1363 #else /* !HAVE_UNISTD_H */
1364 dict[5].value = _("nobody");
1365 dict[6].value = _("nowhere");
1366 #endif /* !HAVE_UNISTD_H */
1368 cp = stpcpy (p = local_alloc (288), "font ");
1369 quote_ps_string (cp, x->prop->font->internal_name);
1372 cp = stpcpy (p = local_alloc (288), "font ");
1373 quote_ps_string (cp, x->fixed->font->internal_name);
1376 sprintf (scaling, "%.3f", PSUS / 72.0);
1377 dict[9].value = scaling;
1379 sprintf (paper_width, "%g", x->w / (PSUS / 72.0));
1380 dict[10].value = paper_width;
1382 sprintf (paper_length, "%g", x->l / (PSUS / 72.0));
1383 dict[11].value = paper_length;
1385 sprintf (left_margin, "%d", x->left_margin);
1386 dict[12].value = left_margin;
1388 sprintf (top_margin, "%d", x->top_margin);
1389 dict[13].value = top_margin;
1391 sprintf (line_width, "%d", x->line_width);
1392 dict[14].value = line_width;
1394 sprintf (line_width, "%d", x->line_width_thick);
1395 dict[15].value = line_width_thick;
1399 dict[16].value = cp = local_alloc (strlen (dict[17].value) + 30);
1400 sprintf (cp, "PSPP (%s)", dict[17].value);
1404 dict[16].value = local_alloc (strlen (outp_title) + 1);
1405 strcpy ((char *) (dict[16].value), outp_title);
1409 while (-1 != getline (&buf, &buf_size, prologue_file))
1415 cp = strstr (buf, "!eps");
1418 if (this->class->magic == MAGIC_PS)
1425 cp = strstr (buf, "!ps");
1428 if (this->class->magic == MAGIC_EPSF)
1433 if (strstr (buf, "!!!"))
1438 if (!strncmp (buf, "!encodings", 10))
1439 output_encodings (this);
1443 beg = buf2 = fn_interp_vars (buf, ps_get_var);
1444 len = strlen (buf2);
1445 while (isspace ((unsigned char) *beg))
1447 if (beg[len - 1] == '\n')
1449 if (beg[len - 1] == '\r')
1451 fwrite (beg, len, 1, f->file);
1452 fputs (x->eol, f->file);
1456 if (ferror (f->file))
1457 msg (IE, _("Reading `%s': %s."), prologue_fn, strerror (errno));
1458 fclose (prologue_file);
1463 local_free (dict[7].value);
1464 local_free (dict[8].value);
1465 local_free (dict[16].value);
1467 if (ferror (f->file))
1470 msg (VM (2), _("%s: PostScript prologue read successfully."), this->name);
1474 msg (VM (1), _("%s: Error reading PostScript prologue."), this->name);
1478 /* Writes the string STRING to buffer DEST (of at least 288
1479 characters) as a PostScript name object. Returns a pointer
1480 to the null terminator of the resultant string. */
1482 quote_ps_name (char *dest, const char *string)
1486 for (sp = string; *sp; sp++)
1560 for (sp = string; *sp && dp < &dest[256]; sp++)
1562 sprintf (dp, "%02x", (unsigned char) *sp);
1565 return stpcpy (dp, ">cvn");
1569 return stpcpy (&dest[1], string);
1572 /* Adds the string STRING to buffer DEST as a PostScript quoted
1573 string; returns a pointer to the null terminator added. Will not
1574 add more than 235 characters. */
1576 quote_ps_string (char *dest, const char *string)
1578 const char *sp = string;
1582 for (; *sp && dp < &dest[235]; sp++)
1584 dp = stpcpy (dp, "\\(");
1585 else if (*sp == ')')
1586 dp = stpcpy (dp, "\\)");
1587 else if (*sp < 32 || (unsigned char) *sp > 127)
1588 dp = spprintf (dp, "\\%3o", *sp);
1591 return stpcpy (dp, ")");
1594 /* Writes the PostScript epilogue to file F. */
1596 preclose (struct file_ext *f)
1598 struct outp_driver *this = f->param;
1599 struct ps_driver_ext *x = this->ext;
1600 struct hsh_iterator iter;
1601 struct font_entry *fe;
1606 "%%%%DocumentNeededResources:%s"),
1607 x->eol, x->file_page_number, x->eol, x->eol);
1609 for (fe = hsh_first (x->loaded, &iter); fe != NULL;
1610 fe = hsh_next (x->loaded, &iter))
1614 cp = stpcpy (buf, "%%+ font ");
1615 cp = quote_ps_string (cp, fe->font->internal_name);
1616 strcpy (cp, x->eol);
1617 fputs (buf, f->file);
1620 hsh_destroy (x->loaded);
1622 hsh_destroy (x->combos);
1624 x->last_font = NULL;
1627 fprintf (f->file, "%%EOF%s", x->eol);
1628 if (ferror (f->file))
1634 ps_open_page (struct outp_driver *this)
1636 struct ps_driver_ext *x = this->ext;
1638 assert (this->driver_open && !this->page_open);
1641 if (!fn_open_ext (&x->file))
1644 msg (ME, _("PostScript output driver: %s: %s"), x->file.filename,
1648 x->file_page_number++;
1650 hsh_destroy (x->combos);
1651 x->combos = hsh_create (31, compare_ps_combo, hash_ps_combo,
1652 free_ps_combo, NULL);
1653 x->last_font = NULL;
1656 fprintf (x->file.file,
1658 "%%%%BeginPageSetup%s"
1659 "/pg save def 0.001 dup scale%s",
1660 x->page_number, x->file_page_number, x->eol,
1664 if (x->orientation == OTN_LANDSCAPE)
1665 fprintf (x->file.file,
1666 "%d 0 translate 90 rotate%s",
1669 if (x->bottom_margin != 0 || x->left_margin != 0)
1670 fprintf (x->file.file,
1671 "%d %d translate%s",
1672 x->left_margin, x->bottom_margin, x->eol);
1674 fprintf (x->file.file,
1675 "/LW %d def/TW %d def %d setlinewidth%s"
1676 "%%%%EndPageSetup%s",
1677 x->line_width, x->line_width_thick, x->line_width, x->eol,
1680 if (!ferror (x->file.file))
1682 this->page_open = 1;
1683 if (x->output_options & OPO_HEADERS)
1684 draw_headers (this);
1689 return !ferror (x->file.file);
1693 ps_close_page (struct outp_driver *this)
1695 struct ps_driver_ext *x = this->ext;
1697 assert (this->driver_open && this->page_open);
1702 fprintf (x->file.file,
1707 this->page_open = 0;
1708 return !ferror (x->file.file);
1712 ps_submit (struct outp_driver *this UNUSED, struct som_entity *s)
1726 /* qsort() comparison function for int tuples. */
1728 int_2_compare (const void *a_, const void *b_)
1733 return *a < *b ? -1 : *a > *b;
1736 /* Hash table comparison function for cached lines. */
1738 compare_line (const void *a_, const void *b_, void *foo UNUSED)
1740 const struct line_form *a = a_;
1741 const struct line_form *b = b_;
1743 return a->ind < b->ind ? -1 : a->ind > b->ind;
1746 /* Hash table hash function for cached lines. */
1748 hash_line (const void *pa, void *foo UNUSED)
1750 const struct line_form *a = pa;
1755 /* Hash table free function for cached lines. */
1757 free_line (void *pa, void *foo UNUSED)
1762 /* Writes PostScript code to draw a line from (x1,y1) to (x2,y2) to
1764 #define dump_line(x1, y1, x2, y2) \
1765 fprintf (ext->file.file, "%d %d %d %d L%s", \
1766 x1, YT (y1), x2, YT (y2), ext->eol)
1768 /* Write PostScript code to draw a thick line from (x1,y1) to (x2,y2)
1769 to the output file. */
1770 #define dump_thick_line(x1, y1, x2, y2) \
1771 fprintf (ext->file.file, "%d %d %d %d TL%s", \
1772 x1, YT (y1), x2, YT (y2), ext->eol)
1774 /* Writes a line of type TYPE to THIS driver's output file. The line
1775 (or its center, in the case of double lines) has its independent
1776 axis coordinate at IND; it extends from DEP1 to DEP2 on the
1779 dump_fancy_line (struct outp_driver *this, int type, int ind, int dep1, int dep2)
1781 struct ps_driver_ext *ext = this->ext;
1782 int ofs = ext->line_space / 2 + ext->line_width / 2;
1787 dump_line (dep1, ind, dep2, ind);
1790 if (ext->output_options & OPO_DOUBLE_LINE)
1792 dump_line (dep1, ind - ofs, dep2, ind - ofs);
1793 dump_line (dep1, ind + ofs, dep2, ind + ofs);
1796 dump_thick_line (dep1, ind, dep2, ind);
1801 dump_line (ind, dep1, ind, dep2);
1804 if (ext->output_options & OPO_DOUBLE_LINE)
1806 dump_line (ind - ofs, dep1, ind - ofs, dep2);
1807 dump_line (ind + ofs, dep1, ind + ofs, dep2);
1810 dump_thick_line (ind, dep1, ind, dep2);
1821 /* Writes all the cached lines to the output file, then clears the
1824 dump_lines (struct outp_driver *this)
1826 struct ps_driver_ext *x = this->ext;
1828 struct hsh_iterator iter;
1831 for (type = 0; type < n_line_types; type++)
1833 struct line_form *line;
1835 if (x->lines[type] == NULL)
1838 for (line = hsh_first (x->lines[type], &iter); line != NULL;
1839 line = hsh_next (x->lines[type], &iter))
1842 int lo = INT_MIN, hi;
1844 qsort (line->dep, line->ndep, sizeof *line->dep, int_2_compare);
1845 lo = line->dep[0][0];
1846 hi = line->dep[0][1];
1847 for (i = 1; i < line->ndep; i++)
1848 if (line->dep[i][0] <= hi + 1)
1850 int min_hi = line->dep[i][1];
1856 dump_fancy_line (this, type, line->ind, lo, hi);
1857 lo = line->dep[i][0];
1858 hi = line->dep[i][1];
1860 dump_fancy_line (this, type, line->ind, lo, hi);
1863 hsh_destroy (x->lines[type]);
1864 x->lines[type] = NULL;
1868 /* (Same args as dump_fancy_line()). Either dumps the line directly
1869 to the output file, or adds it to the cache, depending on the
1870 user-selected line optimization mode. */
1872 line (struct outp_driver *this, int type, int ind, int dep1, int dep2)
1874 struct ps_driver_ext *ext = this->ext;
1875 struct line_form **f;
1877 assert (dep2 >= dep1);
1878 if (ext->line_opt == 0)
1880 dump_fancy_line (this, type, ind, dep1, dep2);
1884 if (ext->lines[type] == NULL)
1885 ext->lines[type] = hsh_create (31, compare_line, hash_line,
1887 f = (struct line_form **) hsh_probe (ext->lines[type], &ind);
1890 *f = xmalloc (sizeof **f + sizeof (int[15][2]));
1894 (*f)->dep[0][0] = dep1;
1895 (*f)->dep[0][1] = dep2;
1898 if ((*f)->ndep >= (*f)->mdep)
1901 *f = xrealloc (*f, sizeof **f + sizeof (int[2]) * ((*f)->mdep - 1));
1903 (*f)->dep[(*f)->ndep][0] = dep1;
1904 (*f)->dep[(*f)->ndep][1] = dep2;
1909 ps_line_horz (struct outp_driver *this, const struct rect *r,
1910 const struct color *c UNUSED, int style)
1912 /* Must match output.h:OUTP_L_*. */
1913 static const int types[OUTP_L_COUNT] =
1914 {-1, horz, dbl_horz, spl_horz};
1916 int y = (r->y1 + r->y2) / 2;
1918 assert (this->driver_open && this->page_open);
1919 assert (style >= 0 && style < OUTP_L_COUNT);
1920 style = types[style];
1922 line (this, style, y, r->x1, r->x2);
1926 ps_line_vert (struct outp_driver *this, const struct rect *r,
1927 const struct color *c UNUSED, int style)
1929 /* Must match output.h:OUTP_L_*. */
1930 static const int types[OUTP_L_COUNT] =
1931 {-1, vert, dbl_vert, spl_vert};
1933 int x = (r->x1 + r->x2) / 2;
1935 assert (this->driver_open && this->page_open);
1936 assert (style >= 0 && style < OUTP_L_COUNT);
1937 style = types[style];
1939 line (this, style, x, r->y1, r->y2);
1942 #define L (style->l != OUTP_L_NONE)
1943 #define R (style->r != OUTP_L_NONE)
1944 #define T (style->t != OUTP_L_NONE)
1945 #define B (style->b != OUTP_L_NONE)
1948 ps_line_intersection (struct outp_driver *this, const struct rect *r,
1949 const struct color *c UNUSED,
1950 const struct outp_styles *style)
1952 struct ps_driver_ext *ext = this->ext;
1954 int x = (r->x1 + r->x2) / 2;
1955 int y = (r->y1 + r->y2) / 2;
1956 int ofs = (ext->line_space + ext->line_width) / 2;
1957 int x1 = x - ofs, x2 = x + ofs;
1958 int y1 = y - ofs, y2 = y + ofs;
1960 assert (this->driver_open && this->page_open);
1961 assert (!((style->l != style->r && style->l != OUTP_L_NONE
1962 && style->r != OUTP_L_NONE)
1963 || (style->t != style->b && style->t != OUTP_L_NONE
1964 && style->b != OUTP_L_NONE)));
1966 switch ((style->l | style->r) | ((style->t | style->b) << 8))
1968 case (OUTP_L_SINGLE) | (OUTP_L_SINGLE << 8):
1969 case (OUTP_L_SINGLE) | (OUTP_L_NONE << 8):
1970 case (OUTP_L_NONE) | (OUTP_L_SINGLE << 8):
1972 line (this, horz, y, r->x1, x);
1974 line (this, horz, y, x, r->x2);
1976 line (this, vert, x, r->y1, y);
1978 line (this, vert, x, y, r->y2);
1980 case (OUTP_L_SINGLE) | (OUTP_L_DOUBLE << 8):
1981 case (OUTP_L_NONE) | (OUTP_L_DOUBLE << 8):
1983 line (this, horz, y, r->x1, x1);
1985 line (this, horz, y, x2, r->x2);
1987 line (this, dbl_vert, x, r->y1, y);
1989 line (this, dbl_vert, x, y, r->y2);
1990 if ((L && R) && !(T && B))
1991 line (this, horz, y, x1, x2);
1993 case (OUTP_L_DOUBLE) | (OUTP_L_SINGLE << 8):
1994 case (OUTP_L_DOUBLE) | (OUTP_L_NONE << 8):
1996 line (this, dbl_horz, y, r->x1, x);
1998 line (this, dbl_horz, y, x, r->x2);
2000 line (this, vert, x, r->y1, y);
2002 line (this, vert, x, y, r->y2);
2003 if ((T && B) && !(L && R))
2004 line (this, vert, x, y1, y2);
2006 case (OUTP_L_DOUBLE) | (OUTP_L_DOUBLE << 8):
2008 line (this, dbl_horz, y, r->x1, x);
2010 line (this, dbl_horz, y, x, r->x2);
2012 line (this, dbl_vert, x, r->y1, y);
2014 line (this, dbl_vert, x, y, r->y2);
2016 line (this, vert, x1, y1, y2);
2018 line (this, vert, x2, y1, y2);
2020 line (this, horz, y1, x1, x2);
2022 line (this, horz, y2, x1, x2);
2030 ps_box (struct outp_driver *this UNUSED, const struct rect *r UNUSED,
2031 const struct color *bord UNUSED, const struct color *fill UNUSED)
2033 assert (this->driver_open && this->page_open);
2037 ps_polyline_begin (struct outp_driver *this UNUSED,
2038 const struct color *c UNUSED)
2040 assert (this->driver_open && this->page_open);
2043 ps_polyline_point (struct outp_driver *this UNUSED, int x UNUSED, int y UNUSED)
2045 assert (this->driver_open && this->page_open);
2048 ps_polyline_end (struct outp_driver *this UNUSED)
2050 assert (this->driver_open && this->page_open);
2053 /* Returns the width of string S for THIS driver. */
2055 text_width (struct outp_driver *this, char *s)
2057 struct outp_text text;
2059 text.options = OUTP_T_JUST_LEFT;
2060 ls_init (&text.s, s, strlen (s));
2061 this->class->text_metrics (this, &text);
2065 /* Write string S at location (X,Y) with width W for THIS driver. */
2067 out_text_plain (struct outp_driver *this, char *s, int x, int y, int w)
2069 struct outp_text text;
2071 text.options = OUTP_T_JUST_LEFT | OUTP_T_HORZ | OUTP_T_VERT;
2072 ls_init (&text.s, s, strlen (s));
2074 text.v = this->font_height;
2077 this->class->text_draw (this, &text);
2080 /* Draw top of page headers for THIS driver. */
2082 draw_headers (struct outp_driver *this)
2084 struct ps_driver_ext *ext = this->ext;
2086 struct font_entry *old_current = ext->current;
2087 char *old_family = xstrdup (ext->family); /* FIXME */
2088 int old_size = ext->size;
2090 int fh = this->font_height;
2093 fprintf (ext->file.file, "%d %d %d %d GB%s",
2094 0, YT (y), this->width, YT (y + 2 * fh + ext->line_gutter),
2096 this->class->text_set_font_family (this, "T");
2098 y += ext->line_width + ext->line_gutter;
2104 sprintf (buf, _("%s - Page %d"), get_start_date (), ext->page_number);
2105 rh_width = text_width (this, buf);
2107 out_text_plain (this, buf, this->width - this->prop_em_width - rh_width,
2110 if (outp_title && outp_subtitle)
2111 out_text_plain (this, outp_title, this->prop_em_width, y,
2112 this->width - 3 * this->prop_em_width - rh_width);
2120 char *string = outp_subtitle ? outp_subtitle : outp_title;
2122 sprintf (buf, "%s - %s", version, host_system);
2123 rh_width = text_width (this, buf);
2125 out_text_plain (this, buf, this->width - this->prop_em_width - rh_width,
2129 out_text_plain (this, string, this->prop_em_width, y,
2130 this->width - 3 * this->prop_em_width - rh_width);
2135 ext->current = old_current;
2137 ext->family = old_family;
2138 ext->size = old_size;
2145 ps_text_set_font_by_name (struct outp_driver *this, const char *dit)
2147 struct ps_driver_ext *x = this->ext;
2148 struct font_entry *fe;
2150 assert (this->driver_open && this->page_open);
2152 /* Short-circuit common fonts. */
2153 if (!strcmp (dit, "PROP"))
2155 x->current = x->prop;
2156 x->size = x->font_size;
2159 else if (!strcmp (dit, "FIXED"))
2161 x->current = x->fixed;
2162 x->size = x->font_size;
2166 /* Find font_desc corresponding to Groff name dit. */
2167 fe = hsh_find (x->loaded, &dit);
2169 fe = load_font (this, dit);
2174 ps_text_set_font_by_position (struct outp_driver *this, int pos)
2176 struct ps_driver_ext *x = this->ext;
2179 assert (this->driver_open && this->page_open);
2181 /* Determine font name by suffixing position string to font family
2186 dit = local_alloc (strlen (x->family) + 3);
2187 cp = stpcpy (dit, x->family);
2209 /* Find font_desc corresponding to Groff name dit. */
2211 struct font_entry *fe = hsh_find (x->loaded, &dit);
2213 fe = load_font (this, dit);
2221 ps_text_set_font_family (struct outp_driver *this, const char *s)
2223 struct ps_driver_ext *x = this->ext;
2225 assert (this->driver_open && this->page_open);
2228 x->family = xstrdup (s);
2232 ps_text_get_font_name (struct outp_driver *this)
2234 struct ps_driver_ext *x = this->ext;
2236 assert (this->driver_open && this->page_open);
2237 return x->current->font->name;
2241 ps_text_get_font_family (struct outp_driver *this)
2243 struct ps_driver_ext *x = this->ext;
2245 assert (this->driver_open && this->page_open);
2250 ps_text_set_size (struct outp_driver *this, int size)
2252 struct ps_driver_ext *x = this->ext;
2254 assert (this->driver_open && this->page_open);
2255 x->size = PSUS / 72000 * size;
2260 ps_text_get_size (struct outp_driver *this, int *em_width)
2262 struct ps_driver_ext *x = this->ext;
2264 assert (this->driver_open && this->page_open);
2266 *em_width = (x->current->font->space_width * x->size) / 1000;
2267 return x->size / (PSUS / 72000);
2270 /* An output character. */
2273 struct font_entry *font; /* Font of character. */
2274 int size; /* Size of character. */
2275 int x, y; /* Location of character. */
2276 unsigned char ch; /* Character. */
2277 char separate; /* Must be separate from previous char. */
2280 /* Hash table comparison function for ps_combo structs. */
2282 compare_ps_combo (const void *pa, const void *pb, void *foo UNUSED)
2284 const struct ps_font_combo *a = pa;
2285 const struct ps_font_combo *b = pb;
2287 return !((a->font == b->font) && (a->size == b->size));
2290 /* Hash table hash function for ps_combo structs. */
2292 hash_ps_combo (const void *pa, void *foo UNUSED)
2294 const struct ps_font_combo *a = pa;
2295 unsigned name_hash = hsh_hash_string (a->font->font->internal_name);
2296 return name_hash ^ hsh_hash_int (a->size);
2299 /* Hash table free function for ps_combo structs. */
2301 free_ps_combo (void *a, void *foo UNUSED)
2306 /* Causes PostScript code to be output that switches to the font
2307 CP->FONT and font size CP->SIZE. The first time a particular
2308 font/size combination is used on a particular page, this involves
2309 outputting PostScript code to load the font. */
2311 switch_font (struct outp_driver *this, const struct output_char *cp)
2313 struct ps_driver_ext *ext = this->ext;
2314 struct ps_font_combo srch, **fc;
2316 srch.font = cp->font;
2317 srch.size = cp->size;
2319 fc = (struct ps_font_combo **) hsh_probe (ext->combos, &srch);
2322 fprintf (ext->file.file, "F%x%s", (*fc)->index, ext->eol);
2327 struct ps_encoding *encoding;
2330 *fc = xmalloc (sizeof **fc);
2331 (*fc)->font = cp->font;
2332 (*fc)->size = cp->size;
2333 (*fc)->index = ext->next_combo++;
2335 filename = find_encoding_file (this, cp->font->font->encoding);
2338 encoding = get_encoding (this, filename);
2343 msg (IE, _("PostScript driver: Cannot find encoding `%s' for "
2344 "PostScript font `%s'."), cp->font->font->encoding,
2345 cp->font->font->internal_name);
2346 encoding = default_encoding (this);
2349 if (cp->font != ext->fixed && cp->font != ext->prop)
2351 bp = stpcpy (buf, "%%IncludeResource: font ");
2352 bp = quote_ps_string (bp, cp->font->font->internal_name);
2353 bp = stpcpy (bp, ext->eol);
2358 bp = spprintf (bp, "/F%x E%x %d", (*fc)->index, encoding->index,
2360 bp = quote_ps_name (bp, cp->font->font->internal_name);
2361 sprintf (bp, " SF%s", ext->eol);
2362 fputs (buf, ext->file.file);
2364 ext->last_font = *fc;
2367 /* (write_text) Writes the accumulated line buffer to the output
2369 #define output_line() \
2372 lp = stpcpy (lp, ext->eol); \
2374 fputs (line, ext->file.file); \
2379 /* (write_text) Adds the string representing number X to the line
2380 buffer, flushing the buffer to disk beforehand if necessary. */
2381 #define put_number(X) \
2384 int n = nsprintf (number, "%d", X); \
2385 if (n + lp > &line[75]) \
2387 lp = stpcpy (lp, number); \
2391 /* Outputs PostScript code to THIS driver's output file to display the
2392 characters represented by the output_char's between CP and END,
2393 using the associated outp_text T to determine formatting. WIDTH is
2394 the width of the output region; WIDTH_LEFT is the amount of the
2395 WIDTH that is not taken up by text (so it can be used to determine
2398 write_text (struct outp_driver *this,
2399 const struct output_char *cp, const struct output_char *end,
2400 struct outp_text *t, int width UNUSED, int width_left)
2402 struct ps_driver_ext *ext = this->ext;
2407 char number[INT_STRLEN_BOUND (int) + 1];
2411 switch (t->options & OUTP_T_JUST_MASK)
2413 case OUTP_T_JUST_LEFT:
2416 case OUTP_T_JUST_RIGHT:
2419 case OUTP_T_JUST_CENTER:
2420 ofs = width_left / 2;
2431 int x = cp->x + ofs;
2432 int y = cp->y + (cp->font->font->ascent * cp->size / 1000);
2434 if (ext->last_font == NULL
2435 || cp->font != ext->last_font->font
2436 || cp->size != ext->last_font->size)
2437 switch_font (this, cp);
2443 static unsigned char literal_chars[ODA_COUNT][32] =
2445 {0x00, 0x00, 0x00, 0xf8, 0xff, 0xfc, 0xff, 0xff,
2446 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f,
2447 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2448 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2450 {0x00, 0x00, 0x00, 0xf8, 0xff, 0xfc, 0xff, 0xff,
2451 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
2452 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
2453 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
2455 {0x7e, 0xd6, 0xff, 0xfb, 0xff, 0xfc, 0xff, 0xff,
2456 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
2457 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
2458 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
2462 if (TEST_BIT (literal_chars[ext->data], cp->ch))
2465 switch ((char) cp->ch)
2468 lp = stpcpy (lp, "\\(");
2471 lp = stpcpy (lp, "\\)");
2474 lp = spprintf (lp, "\\%03o", cp->ch);
2479 while (cp < end && lp < &line[70] && cp->separate == 0);
2487 put_number (YT (y));
2498 if (lp >= &line[70])
2508 /* Displays the text in outp_text T, if DRAW is nonzero; or, merely
2509 determine the text metrics, if DRAW is zero. */
2511 text (struct outp_driver *this, struct outp_text *t, int draw)
2513 struct ps_driver_ext *ext = this->ext;
2516 struct output_char *buf; /* Output buffer. */
2517 struct output_char *buf_end; /* End of output buffer. */
2518 struct output_char *buf_loc; /* Current location in output buffer. */
2521 struct font_entry *old_current = ext->current;
2522 char *old_family = xstrdup (ext->family); /* FIXME */
2523 int old_size = ext->size;
2528 /* Current location. */
2531 /* Keeping track of what's left over. */
2532 int width; /* Width available for characters. */
2533 int width_left, height_left; /* Width, height left over. */
2534 int max_height; /* Tallest character on this line so far. */
2536 /* Previous character. */
2539 /* Information about location of previous space. */
2540 char *space_char; /* Character after space. */
2541 struct output_char *space_buf_loc; /* Buffer location after space. */
2542 int space_width_left; /* Width of characters before space. */
2544 /* Name of the current character. */
2545 const char *char_name;
2546 char local_char_name[2] = {0, 0};
2548 local_char_name[0] = local_char_name[1] = 0;
2550 buf = local_alloc (sizeof *buf * 128);
2551 buf_end = &buf[128];
2554 assert (!ls_null_p (&t->s));
2555 cp = ls_c_str (&t->s);
2556 end = ls_end (&t->s);
2564 width = width_left = (t->options & OUTP_T_HORZ) ? t->h : INT_MAX;
2565 height_left = (t->options & OUTP_T_VERT) ? t->v : INT_MAX;
2569 space_buf_loc = NULL;
2570 space_width_left = 0;
2573 if (!width || !height_left)
2578 struct char_metrics *metric;
2584 /* Set char_name to the name of the character or ligature at
2586 local_char_name[0] = *cp;
2587 char_name = local_char_name;
2588 if (ext->current->font->ligatures && *cp == 'f')
2597 lig = LIG_fi, char_name = "fi";
2600 lig = LIG_fl, char_name = "fl";
2607 lig = LIG_ffi, char_name = "ffi";
2610 lig = LIG_ffl, char_name = "ffl";
2613 lig = LIG_ff, char_name = "ff";
2617 if ((lig & ext->current->font->ligatures) == 0)
2619 local_char_name[0] = *cp; /* 'f' */
2620 char_name = local_char_name;
2623 else if (*cp == '\n')
2627 write_text (this, buf, buf_loc, t, width, width_left);
2634 height_left -= max_height;
2640 /* FIXME: when we're page buffering it will be necessary to
2641 set separate to 1. */
2644 cp += strlen (char_name);
2646 /* Figure out what size this character is, and what kern
2647 adjustment we need. */
2648 cur_char = font_char_name_to_index (char_name);
2649 metric = font_get_char_metrics (ext->current->font, cur_char);
2652 static struct char_metrics m;
2654 m.width = ext->current->font->space_width;
2655 m.code = *char_name;
2657 kern_amt = font_get_kern_adjust (ext->current->font, prev_char,
2661 kern_amt = (kern_amt * ext->size / 1000);
2664 char_width = metric->width * ext->size / 1000;
2666 /* Record the current status if this is a space character. */
2667 if (cur_char == space_index && buf_loc > buf)
2670 space_buf_loc = buf_loc;
2671 space_width_left = width_left;
2674 /* Drop down to a new line if there's no room left on this
2676 if (char_width + kern_amt > width_left)
2678 /* Regress to previous space, if any. */
2682 width_left = space_width_left;
2683 buf_loc = space_buf_loc;
2688 write_text (this, buf, buf_loc, t, width, width_left);
2695 height_left -= max_height;
2703 /* FIXME: when we're page buffering it will be
2704 necessary to set separate to 1. */
2709 if (ext->size > max_height)
2710 max_height = ext->size;
2711 if (max_height > height_left)
2714 /* Actually draw the character. */
2717 if (buf_loc >= buf_end)
2719 int buf_len = buf_end - buf;
2723 struct output_char *new_buf;
2725 new_buf = xmalloc (sizeof *new_buf * 256);
2726 memcpy (new_buf, buf, sizeof *new_buf * 128);
2727 buf_loc = new_buf + 128;
2728 buf_end = new_buf + 256;
2734 buf = xnrealloc (buf, buf_len * 2, sizeof *buf);
2735 buf_loc = buf + buf_len;
2736 buf_end = buf + buf_len * 2;
2741 buf_loc->font = ext->current;
2742 buf_loc->size = ext->size;
2745 buf_loc->ch = metric->code;
2746 buf_loc->separate = separate;
2751 /* Prepare for next iteration. */
2752 width_left -= char_width + kern_amt;
2753 prev_char = cur_char;
2755 height_left -= max_height;
2756 if (buf_loc > buf && draw)
2757 write_text (this, buf, buf_loc, t, width, width_left);
2760 if (!(t->options & OUTP_T_HORZ))
2761 t->h = INT_MAX - width_left;
2762 if (!(t->options & OUTP_T_VERT))
2763 t->v = INT_MAX - height_left;
2765 t->v -= height_left;
2766 if (buf_end - buf == 128)
2770 ext->current = old_current;
2772 ext->family = old_family;
2773 ext->size = old_size;
2777 ps_text_metrics (struct outp_driver *this, struct outp_text *t)
2779 assert (this->driver_open && this->page_open);
2784 ps_text_draw (struct outp_driver *this, struct outp_text *t)
2786 assert (this->driver_open && this->page_open);
2792 /* Translate a filename to a font. */
2793 struct filename2font
2795 char *filename; /* Normalized filename. */
2796 struct font_desc *font;
2799 /* Table of `filename2font's. */
2800 static struct hsh_table *ps_fonts;
2802 /* Hash table comparison function for filename2font structs. */
2804 compare_filename2font (const void *a, const void *b, void *param UNUSED)
2806 return strcmp (((struct filename2font *) a)->filename,
2807 ((struct filename2font *) b)->filename);
2810 /* Hash table hash function for filename2font structs. */
2812 hash_filename2font (const void *f2f_, void *param UNUSED)
2814 const struct filename2font *f2f = f2f_;
2815 return hsh_hash_string (f2f->filename);
2818 /* Initializes the global font list by creating the hash table for
2819 translation of filenames to font_desc structs. */
2823 ps_fonts = hsh_create (31, compare_filename2font, hash_filename2font,
2830 hsh_destroy (ps_fonts);
2833 /* Loads the font having Groff name DIT into THIS driver instance.
2834 Specifically, adds it into the THIS driver's `loaded' hash
2836 static struct font_entry *
2837 load_font (struct outp_driver *this, const char *dit)
2839 struct ps_driver_ext *x = this->ext;
2840 char *filename1, *filename2;
2842 struct font_entry *fe;
2844 filename1 = find_ps_file (this, dit);
2846 filename1 = xstrdup (dit);
2847 filename2 = fn_normalize (filename1);
2850 entry = hsh_probe (ps_fonts, &filename2);
2853 struct filename2font *f2f;
2854 struct font_desc *f = groff_read_font (filename2);
2861 f = default_font ();
2864 f2f = xmalloc (sizeof *f2f);
2865 f2f->filename = filename2;
2872 fe = xmalloc (sizeof *fe);
2873 fe->dit = xstrdup (dit);
2874 fe->font = ((struct filename2font *) * entry)->font;
2875 *hsh_probe (x->loaded, &dit) = fe;
2881 ps_chart_initialise (struct outp_driver *this UNUSED, struct chart *ch)
2886 struct ps_driver_ext *x = this->ext;
2887 char page_size[128];
2889 int x_origin, y_origin;
2891 ch->file = tmpfile ();
2892 if (ch->file == NULL)
2898 size = this->width < this->length ? this->width : this->length;
2899 x_origin = x->left_margin + (size - this->width) / 2;
2900 y_origin = x->bottom_margin + (size - this->length) / 2;
2902 snprintf (page_size, sizeof page_size,
2903 "a,xsize=%.3f,ysize=%.3f,xorigin=%.3f,yorigin=%.3f",
2904 (double) size / PSUS, (double) size / PSUS,
2905 (double) x_origin / PSUS, (double) y_origin / PSUS);
2907 ch->pl_params = pl_newplparams ();
2908 pl_setplparam (ch->pl_params, "PAGESIZE", page_size);
2909 ch->lp = pl_newpl_r ("ps", NULL, ch->file, stderr, ch->pl_params);
2914 ps_chart_finalise (struct outp_driver *this UNUSED, struct chart *ch UNUSED)
2917 struct ps_driver_ext *x = this->ext;
2919 static int doc_num = 0;
2921 if (this->page_open)
2923 this->class->close_page (this);
2924 this->page_open = 0;
2926 this->class->open_page (this);
2927 fprintf (x->file.file,
2929 "%d %d translate 1000 dup scale%s"
2931 "/showpage { } def%s"
2932 "0 setgray 0 setlinecap 1 setlinewidth%s"
2933 "0 setlinejoin 10 setmiterlimit [ ] 0 setdash newpath clear%s"
2934 "%%%%BeginDocument: %d%s",
2936 -x->left_margin, -x->bottom_margin, x->eol,
2944 while (fwrite (buf, 1, fread (buf, 1, sizeof buf, ch->file), x->file.file))
2948 fprintf (x->file.file,
2955 this->class->close_page (this);
2956 this->page_open = 0;
2960 /* PostScript driver class. */
2961 struct outp_class postscript_class =
2983 ps_line_intersection,
2990 ps_text_set_font_by_name,
2991 ps_text_set_font_by_position,
2992 ps_text_set_font_family,
2993 ps_text_get_font_name,
2994 ps_text_get_font_family,
3000 ps_chart_initialise,
3004 /* EPSF driver class. FIXME: Probably doesn't work right. */
3005 struct outp_class epsf_class =
3027 ps_line_intersection,
3034 ps_text_set_font_by_name,
3035 ps_text_set_font_by_position,
3036 ps_text_set_font_family,
3037 ps_text_get_font_name,
3038 ps_text_get_font_family,
3044 ps_chart_initialise,
3049 #endif /* NO_POSTSCRIPT */