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
21 #include <libpspp/message.h>
26 #include <libpspp/alloc.h>
27 #include <libpspp/message.h>
29 #include <libpspp/compiler.h>
30 #include <data/filename.h>
31 #include <libpspp/misc.h>
33 #include <libpspp/pool.h>
34 #include <libpspp/start-date.h>
35 #include <libpspp/version.h>
38 #define _(msgid) gettext (msgid)
40 /* ASCII driver options: (defaults listed first)
42 output-file="pspp.list"
44 form-feed-string="\f" Written as a formfeed.
45 newline-string=default|"\r\n"|"\n"
47 paginate=on|off Formfeeds are desired?
48 tab-width=8 Width of a tab; 0 to not use tabs.
49 init="" Written at beginning of output.
50 done="" Written at end of output.
52 headers=on|off Put headers at top of page?
55 lpi=6 Only used to determine font size.
57 squeeze=off|on Squeeze multiple newlines into exactly one.
64 box[x]="strng" Sets box character X (X in base 4: 0-3333).
65 italic-on=overstrike|"strng" Turns on italic (underline).
66 italic-off=""|"strng" Turns off italic; ignored for overstrike.
67 bold-on=overstrike|"strng" Turns on bold.
68 bold-off=""|"strng" Turns off bold; ignored for overstrike.
69 bold-italic-on=overstrike|"strng" Turns on bold-italic.
70 bold-italic-off=""|"strng" Turns off bold-italic; ignored for overstrike.
71 overstrike-style=single|line Can we print a whole line then BS over it, or
72 must we go char by char, as on a terminal?
73 carriage-return-style=bs|cr Must we return the carriage with a sequence of
74 BSes, or will a single CR do it?
77 /* Disable messages by failed range checks. */
78 /*#define SUPPRESS_WARNINGS 1 */
83 CHS_ASCII, /* 7-bit ASCII */
84 CHS_LATIN1 /* Latin 1; not really supported at the moment */
87 /* Overstrike style. */
90 OVS_SINGLE, /* Overstrike each character: "a\b_b\b_c\b_" */
91 OVS_LINE /* Overstrike lines: "abc\b\b\b___" (or if
92 newline is "\r\n", then "abc\r___"). Easier
93 on the printer, doesn't work on a tty. */
96 /* Basic output strings. */
99 OPS_INIT, /* Document initialization string. */
100 OPS_DONE, /* Document uninit string. */
101 OPS_FORMFEED, /* Formfeed string. */
102 OPS_NEWLINE, /* Newline string. */
104 OPS_COUNT /* Number of output strings. */
107 /* Line styles bit shifts. */
118 /* Carriage return style. */
121 CRS_BS, /* Multiple backspaces. */
122 CRS_CR /* Single carriage return. */
125 /* Assembles a byte from four taystes. */
126 #define TAYSTE2BYTE(T, L, B, R) \
128 | ((L) << LNS_LEFT) \
129 | ((B) << LNS_BOTTOM) \
130 | ((R) << LNS_RIGHT))
132 /* Extract tayste with shift value S from byte B. */
133 #define BYTE2TAYSTE(B, S) \
136 /* Font style; take one of the first group |'d with one of the second group. */
139 FSTY_ON = 000, /* Turn font on. */
140 FSTY_OFF = 001, /* Turn font off. */
142 FSTY_ITALIC = 0, /* Italic font. */
143 FSTY_BOLD = 2, /* Bold font. */
144 FSTY_BOLD_ITALIC = 4, /* Bold-italic font. */
146 FSTY_COUNT = 6 /* Number of font styles. */
149 /* A line of text. */
152 unsigned short *chars; /* Characters and attributes. */
153 int char_cnt; /* Length. */
154 int char_cap; /* Allocated bytes. */
157 /* ASCII output driver extension record. */
158 struct ascii_driver_ext
160 /* User parameters. */
161 int char_set; /* CHS_ASCII/CHS_LATIN1; no-op right now. */
162 int headers; /* 1=print headers at top of page. */
163 int page_length; /* Page length in lines. */
164 int page_width; /* Page width in characters. */
165 int lpi; /* Lines per inch. */
166 int cpi; /* Characters per inch. */
167 int left_margin; /* Left margin in characters. */
168 int right_margin; /* Right margin in characters. */
169 int top_margin; /* Top margin in lines. */
170 int bottom_margin; /* Bottom margin in lines. */
171 int paginate; /* 1=insert formfeeds. */
172 int tab_width; /* Width of a tab; 0 not to use tabs. */
173 struct fixed_string ops[OPS_COUNT]; /* Basic output strings. */
174 struct fixed_string box[LNS_COUNT]; /* Line & box drawing characters. */
175 struct fixed_string fonts[FSTY_COUNT]; /* Font styles; NULL=overstrike. */
176 int overstrike_style; /* OVS_SINGLE or OVS_LINE. */
177 int carriage_return_style; /* Carriage return style. */
178 int squeeze_blank_lines; /* 1=squeeze multiple blank lines into one. */
180 /* Internal state. */
181 struct file_ext file; /* Output file. */
182 int page_number; /* Current page number. */
183 struct line *lines; /* Page content. */
184 int lines_cap; /* Number of lines allocated. */
185 int w, l; /* Actual width & length w/o margins, etc. */
186 int cur_font; /* Current font by OUTP_F_*. */
188 int debug; /* Set by som_text_draw(). */
192 static int postopen (struct file_ext *);
193 static int preclose (struct file_ext *);
195 static struct outp_option_info *option_info;
198 ascii_open_global (struct outp_class *this UNUSED)
200 option_info = xmalloc (sizeof *option_info);
201 option_info->initial = 0;
202 option_info->options = 0;
209 ascii_close_global (struct outp_class *this UNUSED)
211 free(option_info->initial);
212 free(option_info->options);
219 ascii_font_sizes (struct outp_class *this UNUSED, int *n_valid_sizes)
221 static int valid_sizes[] = {12, 12, 0, 0};
223 assert (n_valid_sizes);
229 ascii_preopen_driver (struct outp_driver *this)
231 struct ascii_driver_ext *x;
234 assert (this->driver_open == 0);
235 msg (VM (1), _("ASCII driver initializing as `%s'..."), this->name);
236 this->ext = x = xmalloc (sizeof *x);
237 x->char_set = CHS_ASCII;
246 x->bottom_margin = 2;
249 for (i = 0; i < OPS_COUNT; i++)
250 ls_null (&x->ops[i]);
251 for (i = 0; i < LNS_COUNT; i++)
252 ls_null (&x->box[i]);
253 for (i = 0; i < FSTY_COUNT; i++)
254 ls_null (&x->fonts[i]);
255 x->overstrike_style = OVS_SINGLE;
256 x->carriage_return_style = CRS_BS;
257 x->squeeze_blank_lines = 0;
258 x->file.filename = NULL;
261 x->file.sequence_no = &x->page_number;
263 x->file.postopen = postopen;
264 x->file.preclose = preclose;
268 x->cur_font = OUTP_F_R;
276 ascii_postopen_driver (struct outp_driver *this)
278 struct ascii_driver_ext *x = this->ext;
280 assert (this->driver_open == 0);
282 if (NULL == x->file.filename)
283 x->file.filename = xstrdup ("pspp.list");
285 x->w = x->page_width - x->left_margin - x->right_margin;
286 x->l = (x->page_length - (x->headers ? 3 : 0) - x->top_margin
287 - x->bottom_margin - 1);
288 if (x->w < 59 || x->l < 15)
290 msg (SE, _("ascii driver: Area of page excluding margins and headers "
291 "must be at least 59 characters wide by 15 lines long. Page as "
292 "configured is only %d characters by %d lines."), x->w, x->l);
296 this->res = x->lpi * x->cpi;
297 this->horiz = x->lpi;
299 this->width = x->w * this->horiz;
300 this->length = x->l * this->vert;
302 if (ls_null_p (&x->ops[OPS_FORMFEED]))
303 ls_create (&x->ops[OPS_FORMFEED], "\f");
304 if (ls_null_p (&x->ops[OPS_NEWLINE])
305 || !strcmp (ls_c_str (&x->ops[OPS_NEWLINE]), "default"))
307 ls_create (&x->ops[OPS_NEWLINE], "\n");
314 for (i = 0; i < LNS_COUNT; i++)
318 if (!ls_null_p (&x->box[i]))
322 case TAYSTE2BYTE (0, 0, 0, 0):
326 case TAYSTE2BYTE (0, 1, 0, 0):
327 case TAYSTE2BYTE (0, 1, 0, 1):
328 case TAYSTE2BYTE (0, 0, 0, 1):
332 case TAYSTE2BYTE (1, 0, 0, 0):
333 case TAYSTE2BYTE (1, 0, 1, 0):
334 case TAYSTE2BYTE (0, 0, 1, 0):
338 case TAYSTE2BYTE (0, 3, 0, 0):
339 case TAYSTE2BYTE (0, 3, 0, 3):
340 case TAYSTE2BYTE (0, 0, 0, 3):
341 case TAYSTE2BYTE (0, 2, 0, 0):
342 case TAYSTE2BYTE (0, 2, 0, 2):
343 case TAYSTE2BYTE (0, 0, 0, 2):
347 case TAYSTE2BYTE (3, 0, 0, 0):
348 case TAYSTE2BYTE (3, 0, 3, 0):
349 case TAYSTE2BYTE (0, 0, 3, 0):
350 case TAYSTE2BYTE (2, 0, 0, 0):
351 case TAYSTE2BYTE (2, 0, 2, 0):
352 case TAYSTE2BYTE (0, 0, 2, 0):
357 if (BYTE2TAYSTE (i, LNS_LEFT) > 1
358 || BYTE2TAYSTE (i, LNS_TOP) > 1
359 || BYTE2TAYSTE (i, LNS_RIGHT) > 1
360 || BYTE2TAYSTE (i, LNS_BOTTOM) > 1)
366 ls_create (&x->box[i], c);
373 this->cp_x = this->cp_y = 0;
374 this->font_height = this->vert;
375 this->prop_em_width = this->horiz;
376 this->fixed_width = this->horiz;
378 this->horiz_line_width[0] = 0;
379 this->vert_line_width[0] = 0;
381 for (i = 1; i < OUTP_L_COUNT; i++)
383 this->horiz_line_width[i] = this->vert;
384 this->vert_line_width[i] = this->horiz;
387 for (i = 0; i < (1 << OUTP_L_COUNT); i++)
389 this->horiz_line_spacing[i] = (i & ~1) ? this->vert : 0;
390 this->vert_line_spacing[i] = (i & ~1) ? this->horiz : 0;
394 this->driver_open = 1;
395 msg (VM (2), _("%s: Initialization complete."), this->name);
401 ascii_close_driver (struct outp_driver *this)
403 struct ascii_driver_ext *x = this->ext;
406 assert (this->driver_open == 1);
407 msg (VM (2), _("%s: Beginning closing..."), this->name);
410 for (i = 0; i < OPS_COUNT; i++)
411 ls_destroy (&x->ops[i]);
412 for (i = 0; i < LNS_COUNT; i++)
413 ls_destroy (&x->box[i]);
414 for (i = 0; i < FSTY_COUNT; i++)
415 ls_destroy (&x->fonts[i]);
416 if (x->lines != NULL)
420 for (line = 0; line < x->lines_cap; line++)
421 free (x->lines[line].chars);
424 fn_close_ext (&x->file);
425 free (x->file.filename);
428 this->driver_open = 0;
429 msg (VM (3), _("%s: Finished closing."), this->name);
434 /* Generic option types. */
444 static struct outp_option option_tab[] =
446 {"headers", boolean_arg, 0},
447 {"output-file", 1, 0},
449 {"length", pos_int_arg, 0},
450 {"width", pos_int_arg, 1},
451 {"lpi", pos_int_arg, 2},
452 {"cpi", pos_int_arg, 3},
453 {"init", string_arg, 0},
454 {"done", string_arg, 1},
455 {"left-margin", nonneg_int_arg, 0},
456 {"right-margin", nonneg_int_arg, 1},
457 {"top-margin", nonneg_int_arg, 2},
458 {"bottom-margin", nonneg_int_arg, 3},
459 {"paginate", boolean_arg, 1},
460 {"form-feed-string", string_arg, 2},
461 {"newline-string", string_arg, 3},
462 {"italic-on", font_string_arg, 0},
463 {"italic-off", font_string_arg, 1},
464 {"bold-on", font_string_arg, 2},
465 {"bold-off", font_string_arg, 3},
466 {"bold-italic-on", font_string_arg, 4},
467 {"bold-italic-off", font_string_arg, 5},
468 {"overstrike-style", 3, 0},
469 {"tab-width", nonneg_int_arg, 4},
470 {"carriage-return-style", 4, 0},
471 {"squeeze", boolean_arg, 2},
476 ascii_option (struct outp_driver *this, const char *key,
477 const struct string *val)
479 struct ascii_driver_ext *x = this->ext;
483 value = ds_c_str (val);
484 if (!strncmp (key, "box[", 4))
487 int indx = strtol (&key[4], &tail, 4);
488 if (*tail != ']' || indx < 0 || indx > LNS_COUNT)
490 msg (SE, _("Bad index value for `box' key: syntax is box[INDEX], "
491 "0 <= INDEX < %d decimal, with INDEX expressed in base 4."),
495 if (!ls_null_p (&x->box[indx]))
496 msg (SW, _("Duplicate value for key `%s'."), key);
497 ls_create (&x->box[indx], value);
501 cat = outp_match_keyword (key, option_tab, option_info, &subcat);
505 msg (SE, _("Unknown configuration parameter `%s' for ascii device driver."),
509 free (x->file.filename);
510 x->file.filename = xstrdup (value);
513 if (!strcmp (value, "ascii"))
514 x->char_set = CHS_ASCII;
515 else if (!strcmp (value, "latin1"))
516 x->char_set = CHS_LATIN1;
518 msg (SE, _("Unknown character set `%s'. Valid character sets are "
519 "`ascii' and `latin1'."), value);
522 if (!strcmp (value, "single"))
523 x->overstrike_style = OVS_SINGLE;
524 else if (!strcmp (value, "line"))
525 x->overstrike_style = OVS_LINE;
527 msg (SE, _("Unknown overstrike style `%s'. Valid overstrike styles "
528 "are `single' and `line'."), value);
531 if (!strcmp (value, "bs"))
532 x->carriage_return_style = CRS_BS;
533 else if (!strcmp (value, "cr"))
534 x->carriage_return_style = CRS_CR;
536 msg (SE, _("Unknown carriage return style `%s'. Valid carriage "
537 "return styles are `cr' and `bs'."), value);
545 arg = strtol (value, &tail, 0);
546 if (arg < 1 || errno == ERANGE || *tail)
548 msg (SE, _("Positive integer required as value for `%s'."), key);
554 x->page_length = arg;
576 arg = strtol (value, &tail, 0);
577 if (arg < 0 || errno == ERANGE || *tail)
579 msg (SE, _("Zero or positive integer required as value for `%s'."),
586 x->left_margin = arg;
589 x->right_margin = arg;
595 x->bottom_margin = arg;
607 struct fixed_string *s;
611 s = &x->ops[OPS_INIT];
614 s = &x->ops[OPS_DONE];
617 s = &x->ops[OPS_FORMFEED];
620 s = &x->ops[OPS_NEWLINE];
626 ls_create (s, value);
629 case font_string_arg:
631 if (!strcmp (value, "overstrike"))
633 ls_destroy (&x->fonts[subcat]);
636 ls_create (&x->fonts[subcat], value);
642 if (!strcmp (value, "on") || !strcmp (value, "true")
643 || !strcmp (value, "yes") || atoi (value))
645 else if (!strcmp (value, "off") || !strcmp (value, "false")
646 || !strcmp (value, "no") || !strcmp (value, "0"))
650 msg (SE, _("Boolean value expected for %s."), key);
656 x->headers = setting;
659 x->paginate = setting;
662 x->squeeze_blank_lines = setting;
675 postopen (struct file_ext *f)
677 struct ascii_driver_ext *x = f->param;
678 struct fixed_string *s = &x->ops[OPS_INIT];
680 if (!ls_empty_p (s) && fwrite (ls_c_str (s), ls_length (s), 1, f->file) < 1)
682 msg (ME, _("ASCII output driver: %s: %s"),
683 f->filename, strerror (errno));
690 preclose (struct file_ext *f)
692 struct ascii_driver_ext *x = f->param;
693 struct fixed_string *d = &x->ops[OPS_DONE];
695 if (!ls_empty_p (d) && fwrite (ls_c_str (d), ls_length (d), 1, f->file) < 1)
697 msg (ME, _("ASCII output driver: %s: %s"),
698 f->filename, strerror (errno));
705 ascii_open_page (struct outp_driver *this)
707 struct ascii_driver_ext *x = this->ext;
710 assert (this->driver_open && !this->page_open);
712 if (!fn_open_ext (&x->file))
714 msg (ME, _("ASCII output driver: %s: %s"), x->file.filename,
719 if (x->l > x->lines_cap)
721 x->lines = xnrealloc (x->lines, x->l, sizeof *x->lines);
722 for (i = x->lines_cap; i < x->l; i++)
724 struct line *line = &x->lines[i];
731 for (i = 0; i < x->l; i++)
732 x->lines[i].char_cnt = 0;
738 /* Ensures that at least the first L characters of line I in the
739 driver identified by struct ascii_driver_ext *X have been cleared out. */
741 expand_line (struct ascii_driver_ext *x, int i, int l)
746 assert (i < x->lines_cap);
748 if (l > line->char_cap)
750 line->char_cap = l * 2;
751 line->chars = xnrealloc (line->chars,
752 line->char_cap, sizeof *line->chars);
754 for (j = line->char_cnt; j < l; j++)
755 line->chars[j] = ' ';
759 /* Puts line L at (H,K) in the current output page. Assumes
760 struct ascii_driver_ext named `ext'. */
761 #define draw_line(H, K, L) \
762 ext->lines[K].chars[H] = (L) | 0x800
764 /* Line styles for each position. */
765 #define T(STYLE) (STYLE<<LNS_TOP)
766 #define L(STYLE) (STYLE<<LNS_LEFT)
767 #define B(STYLE) (STYLE<<LNS_BOTTOM)
768 #define R(STYLE) (STYLE<<LNS_RIGHT)
771 ascii_line_horz (struct outp_driver *this, const struct rect *r,
772 const struct color *c UNUSED, int style)
774 struct ascii_driver_ext *ext = this->ext;
775 int x1 = r->x1 / this->horiz;
776 int x2 = r->x2 / this->horiz;
777 int y1 = r->y1 / this->vert;
780 assert (this->driver_open && this->page_open);
785 || x1 < 0 || x1 >= ext->w
786 || x2 <= 0 || x2 > ext->w
787 || y1 < 0 || y1 >= ext->l)
789 #if !SUPPRESS_WARNINGS
790 printf (_("ascii_line_horz: bad hline (%d,%d),%d out of (%d,%d)\n"),
791 x1, x2, y1, ext->w, ext->l);
797 if (ext->lines[y1].char_cnt < x2)
798 expand_line (ext, y1, x2);
800 for (x = x1; x < x2; x++)
801 draw_line (x, y1, (style << LNS_LEFT) | (style << LNS_RIGHT));
805 ascii_line_vert (struct outp_driver *this, const struct rect *r,
806 const struct color *c UNUSED, int style)
808 struct ascii_driver_ext *ext = this->ext;
809 int x1 = r->x1 / this->horiz;
810 int y1 = r->y1 / this->vert;
811 int y2 = r->y2 / this->vert;
814 assert (this->driver_open && this->page_open);
819 || x1 < 0 || x1 >= ext->w
820 || y1 < 0 || y1 >= ext->l
821 || y2 < 0 || y2 > ext->l)
823 #if !SUPPRESS_WARNINGS
824 printf (_("ascii_line_vert: bad vline %d,(%d,%d) out of (%d,%d)\n"),
825 x1, y1, y2, ext->w, ext->l);
831 for (y = y1; y < y2; y++)
832 if (ext->lines[y].char_cnt <= x1)
833 expand_line (ext, y, x1 + 1);
835 for (y = y1; y < y2; y++)
836 draw_line (x1, y, (style << LNS_TOP) | (style << LNS_BOTTOM));
840 ascii_line_intersection (struct outp_driver *this, const struct rect *r,
841 const struct color *c UNUSED,
842 const struct outp_styles *style)
844 struct ascii_driver_ext *ext = this->ext;
845 int x = r->x1 / this->horiz;
846 int y = r->y1 / this->vert;
849 assert (this->driver_open && this->page_open);
851 if (x < 0 || x >= ext->w || y < 0 || y >= ext->l)
853 #if !SUPPRESS_WARNINGS
854 printf (_("ascii_line_intersection: bad intsct (%d,%d) out of (%d,%d)\n"),
855 x, y, ext->w, ext->l);
861 l = ((style->l << LNS_LEFT) | (style->r << LNS_RIGHT)
862 | (style->t << LNS_TOP) | (style->b << LNS_BOTTOM));
864 if (ext->lines[y].char_cnt <= x)
865 expand_line (ext, y, x + 1);
869 /* FIXME: Later we could set this up so that for certain devices it
872 ascii_box (struct outp_driver *this UNUSED, const struct rect *r UNUSED,
873 const struct color *bord UNUSED, const struct color *fill UNUSED)
875 assert (this->driver_open && this->page_open);
878 /* Polylines not supported. */
880 ascii_polyline_begin (struct outp_driver *this UNUSED, const struct color *c UNUSED)
882 assert (this->driver_open && this->page_open);
885 ascii_polyline_point (struct outp_driver *this UNUSED, int x UNUSED, int y UNUSED)
887 assert (this->driver_open && this->page_open);
890 ascii_polyline_end (struct outp_driver *this UNUSED)
892 assert (this->driver_open && this->page_open);
896 ascii_text_set_font_by_name (struct outp_driver * this, const char *s)
898 struct ascii_driver_ext *x = this->ext;
899 int len = strlen (s);
901 assert (this->driver_open && this->page_open);
902 x->cur_font = OUTP_F_R;
905 if (s[len - 1] == 'I')
907 if (len > 1 && s[len - 2] == 'B')
908 x->cur_font = OUTP_F_BI;
910 x->cur_font = OUTP_F_I;
912 else if (s[len - 1] == 'B')
913 x->cur_font = OUTP_F_B;
917 ascii_text_set_font_by_position (struct outp_driver *this, int pos)
919 struct ascii_driver_ext *x = this->ext;
920 assert (this->driver_open && this->page_open);
921 x->cur_font = pos >= 0 && pos < 4 ? pos : 0;
925 ascii_text_set_font_by_family (struct outp_driver *this UNUSED, const char *s UNUSED)
927 assert (this->driver_open && this->page_open);
931 ascii_text_get_font_name (struct outp_driver *this)
933 struct ascii_driver_ext *x = this->ext;
935 assert (this->driver_open && this->page_open);
953 ascii_text_get_font_family (struct outp_driver *this UNUSED)
955 assert (this->driver_open && this->page_open);
960 ascii_text_set_size (struct outp_driver *this, int size)
962 assert (this->driver_open && this->page_open);
963 return size == this->vert;
967 ascii_text_get_size (struct outp_driver *this, int *em_width)
969 assert (this->driver_open && this->page_open);
971 *em_width = this->horiz;
975 static void text_draw (struct outp_driver *this, struct outp_text *t);
977 /* Divides the text T->S into lines of width T->H. Sets T->V to the
978 number of lines necessary. Actually draws the text if DRAW is
981 You probably don't want to look at this code. */
983 delineate (struct outp_driver *this, struct outp_text *t, int draw)
985 /* Width we're fitting everything into. */
986 int width = t->h / this->horiz;
988 /* Maximum `y' position we can write to. */
991 /* Current position in string, character following end of string. */
992 const char *s = ls_c_str (&t->s);
993 const char *end = ls_end (&t->s);
995 /* Temporary struct outp_text to pass to low-level function. */
996 struct outp_text temp;
1002 printf (_("%s: horiz=%d, vert=%d\n"), this->name, this->horiz, this->vert);
1014 temp.options = t->options;
1015 ls_shallow_copy (&temp.s, &t->s);
1016 temp.h = t->h / this->horiz;
1017 temp.x = t->x / this->horiz;
1021 temp.y = t->y / this->vert;
1023 if (t->options & OUTP_T_VERT)
1024 max_y = (t->v / this->vert) + temp.y - 1;
1028 while (end - s > width)
1030 const char *beg = s;
1033 /* Find first space before &s[width]. */
1039 if (!isspace ((unsigned char) space[-1]))
1048 s = space = &s[width];
1055 ls_init (&temp.s, beg, space - beg);
1056 temp.w = space - beg;
1057 text_draw (this, &temp);
1059 if (++temp.y > max_y)
1062 /* Find first nonspace after space. */
1063 while (s < end && isspace ((unsigned char) *s))
1070 ls_init (&temp.s, s, end - s);
1072 text_draw (this, &temp);
1077 t->v = (temp.y * this->vert) - t->y;
1081 ascii_text_metrics (struct outp_driver *this, struct outp_text *t)
1083 assert (this->driver_open && this->page_open);
1084 if (!(t->options & OUTP_T_HORZ))
1087 t->h = ls_length (&t->s) * this->horiz;
1090 delineate (this, t, 0);
1094 ascii_text_draw (struct outp_driver *this, struct outp_text *t)
1096 /* FIXME: orientations not supported. */
1097 assert (this->driver_open && this->page_open);
1098 if (!(t->options & OUTP_T_HORZ))
1100 struct outp_text temp;
1102 temp.options = t->options;
1104 temp.h = temp.v = 0;
1105 temp.x = t->x / this->horiz;
1106 temp.y = t->y / this->vert;
1107 text_draw (this, &temp);
1108 ascii_text_metrics (this, t);
1112 delineate (this, t, 1);
1116 text_draw (struct outp_driver *this, struct outp_text *t)
1118 struct ascii_driver_ext *ext = this->ext;
1119 unsigned attr = ext->cur_font << 8;
1124 char *s = ls_c_str (&t->s);
1126 /* Expand the line with the assumption that S takes up LEN character
1127 spaces (sometimes it takes up less). */
1130 assert (this->driver_open && this->page_open);
1131 switch (t->options & OUTP_T_JUST_MASK)
1133 case OUTP_T_JUST_LEFT:
1135 case OUTP_T_JUST_CENTER:
1136 x -= (t->h - t->w) / 2; /* fall through */
1137 case OUTP_T_JUST_RIGHT:
1144 if (!(t->y < ext->l && x < ext->w))
1146 min_len = min (x + ls_length (&t->s), ext->w);
1147 if (ext->lines[t->y].char_cnt < min_len)
1148 expand_line (ext, t->y, min_len);
1151 int len = ls_length (&t->s);
1153 if (len + x > ext->w)
1156 ext->lines[y].chars[x++] = *s++ | attr;
1160 /* ascii_close_page () and support routines. */
1162 #define LINE_BUF_SIZE 1024
1163 static char *line_buf;
1164 static char *line_p;
1167 commit_line_buf (struct outp_driver *this)
1169 struct ascii_driver_ext *x = this->ext;
1171 if ((int) fwrite (line_buf, 1, line_p - line_buf, x->file.file)
1172 < line_p - line_buf)
1174 msg (ME, _("Writing `%s': %s"), x->file.filename, strerror (errno));
1182 /* Writes everything from BP to EP exclusive into line_buf, or to
1183 THIS->output if line_buf overflows. */
1185 output_string (struct outp_driver *this, const char *bp, const char *ep)
1187 if (LINE_BUF_SIZE - (line_p - line_buf) >= ep - bp)
1189 memcpy (line_p, bp, ep - bp);
1195 if (LINE_BUF_SIZE - (line_p - line_buf) <= 1 && !commit_line_buf (this))
1201 /* Writes everything from BP to EP exclusive into line_buf, or to
1202 THIS->output if line_buf overflows. Returns 1 if additional passes
1203 over the line are required. FIXME: probably could do a lot of
1204 optimization here. */
1206 output_shorts (struct outp_driver *this,
1207 const unsigned short *bp, const unsigned short *ep)
1209 struct ascii_driver_ext *ext = this->ext;
1210 size_t remaining = LINE_BUF_SIZE - (line_p - line_buf);
1213 for (; bp < ep; bp++)
1217 struct fixed_string *box = &ext->box[*bp & 0xff];
1218 size_t len = ls_length (box);
1220 if (remaining >= len)
1222 memcpy (line_p, ls_c_str (box), len);
1228 if (!commit_line_buf (this))
1230 output_string (this, ls_c_str (box), ls_end (box));
1231 remaining = LINE_BUF_SIZE - (line_p - line_buf);
1234 else if (*bp & 0x0300)
1236 struct fixed_string *on;
1240 switch (*bp & 0x0300)
1243 on = &ext->fonts[FSTY_ON | FSTY_ITALIC];
1246 on = &ext->fonts[FSTY_ON | FSTY_BOLD];
1248 case OUTP_F_BI << 8:
1249 on = &ext->fonts[FSTY_ON | FSTY_BOLD_ITALIC];
1257 if (ext->overstrike_style == OVS_SINGLE)
1258 switch (*bp & 0x0300)
1272 case OUTP_F_BI << 8:
1295 output_string (this, buf, &buf[len]);
1304 if (!commit_line_buf (this))
1306 remaining = LINE_BUF_SIZE - (line_p - line_buf);
1314 /* Writes CH into line_buf N times, or to THIS->output if line_buf
1317 output_char (struct outp_driver *this, int n, char ch)
1319 if (LINE_BUF_SIZE - (line_p - line_buf) >= n)
1321 memset (line_p, ch, n);
1327 if (LINE_BUF_SIZE - (line_p - line_buf) <= 1 && !commit_line_buf (this))
1333 /* Advance the carriage from column 0 to the left margin. */
1335 advance_to_left_margin (struct outp_driver *this)
1337 struct ascii_driver_ext *ext = this->ext;
1340 margin = ext->left_margin;
1343 if (ext->tab_width && margin >= ext->tab_width)
1345 output_char (this, margin / ext->tab_width, '\t');
1346 margin %= ext->tab_width;
1349 output_char (this, margin, ' ');
1352 /* Move the output file carriage N_CHARS left, to the left margin. */
1354 return_carriage (struct outp_driver *this, int n_chars)
1356 struct ascii_driver_ext *ext = this->ext;
1358 switch (ext->carriage_return_style)
1361 output_char (this, n_chars, '\b');
1364 output_char (this, 1, '\r');
1365 advance_to_left_margin (this);
1373 /* Writes COUNT lines from the line buffer in THIS, starting at line
1376 output_lines (struct outp_driver *this, int first, int count)
1378 struct ascii_driver_ext *ext = this->ext;
1381 struct fixed_string *newline = &ext->ops[OPS_NEWLINE];
1386 if (NULL == ext->file.file)
1389 /* Iterate over all the lines to be output. */
1390 for (line_num = first; line_num < first + count; line_num++)
1392 struct line *line = &ext->lines[line_num];
1393 unsigned short *p = line->chars;
1394 unsigned short *end_p = p + line->char_cnt;
1395 unsigned short *bp, *ep;
1396 unsigned short attr = 0;
1398 assert (end_p >= p);
1400 /* Squeeze multiple blank lines into a single blank line if
1402 if (ext->squeeze_blank_lines
1404 && ext->lines[line_num].char_cnt == 0
1405 && ext->lines[line_num - 1].char_cnt == 0)
1408 /* Output every character in the line in the appropriate
1413 advance_to_left_margin (this);
1416 while (ep < end_p && attr == (*ep & 0x0300))
1418 if (output_shorts (this, bp, ep))
1426 /* Turn off old font. */
1427 if (attr != (OUTP_F_R << 8))
1429 struct fixed_string *off;
1434 off = &ext->fonts[FSTY_OFF | FSTY_ITALIC];
1437 off = &ext->fonts[FSTY_OFF | FSTY_BOLD];
1439 case OUTP_F_BI << 8:
1440 off = &ext->fonts[FSTY_OFF | FSTY_BOLD_ITALIC];
1447 output_string (this, ls_c_str (off), ls_end (off));
1450 /* Turn on new font. */
1451 attr = (*bp & 0x0300);
1452 if (attr != (OUTP_F_R << 8))
1454 struct fixed_string *on;
1459 on = &ext->fonts[FSTY_ON | FSTY_ITALIC];
1462 on = &ext->fonts[FSTY_ON | FSTY_BOLD];
1464 case OUTP_F_BI << 8:
1465 on = &ext->fonts[FSTY_ON | FSTY_BOLD_ITALIC];
1472 output_string (this, ls_c_str (on), ls_end (on));
1481 return_carriage (this, n_chars);
1486 while (ep < end_p && (*ep & 0x0300) == (OUTP_F_R << 8))
1490 output_char (this, ep - bp, ' ');
1492 switch (*ep & 0x0300)
1500 case OUTP_F_BI << 8:
1508 output_char (this, 1, ch);
1509 n_chars += ep - bp + 1;
1516 return_carriage (this, n_chars);
1520 while (ep < end_p && (*ep & 0x0300) != (OUTP_F_BI << 8))
1524 output_char (this, ep - bp, ' ');
1525 output_char (this, 1, '_');
1531 output_string (this, ls_c_str (newline), ls_end (newline));
1537 ascii_close_page (struct outp_driver *this)
1541 struct ascii_driver_ext *x = this->ext;
1542 int nl_len, ff_len, total_len;
1546 assert (this->driver_open && this->page_open);
1549 line_buf = xmalloc (LINE_BUF_SIZE);
1552 nl_len = ls_length (&x->ops[OPS_NEWLINE]);
1555 total_len = x->top_margin * nl_len;
1556 if (s_len < total_len)
1559 s = xrealloc (s, s_len);
1561 for (cp = s, i = 0; i < x->top_margin; i++)
1563 memcpy (cp, ls_c_str (&x->ops[OPS_NEWLINE]), nl_len);
1566 output_string (this, s, &s[total_len]);
1572 total_len = nl_len + x->w;
1573 if (s_len < total_len + 1)
1575 s_len = total_len + 1;
1576 s = xrealloc (s, s_len);
1579 memset (s, ' ', x->w);
1584 snprintf (temp, 80, _("%s - Page %d"), get_start_date (),
1586 memcpy (&s[x->w - strlen (temp)], temp, strlen (temp));
1589 if (outp_title && outp_subtitle)
1591 len = min ((int) strlen (outp_title), x->w);
1592 memcpy (s, outp_title, len);
1594 memcpy (&s[x->w], ls_c_str (&x->ops[OPS_NEWLINE]), nl_len);
1595 output_string (this, s, &s[total_len]);
1597 memset (s, ' ', x->w);
1598 len = strlen (version) + 3 + strlen (host_system);
1600 sprintf (&s[x->w - len], "%s - %s" , version, host_system);
1601 if (outp_subtitle || outp_title)
1603 char *string = outp_subtitle ? outp_subtitle : outp_title;
1604 len = min ((int) strlen (string), x->w);
1605 memcpy (s, string, len);
1607 memcpy (&s[x->w], ls_c_str (&x->ops[OPS_NEWLINE]), nl_len);
1608 output_string (this, s, &s[total_len]);
1609 output_string (this, &s[x->w], &s[total_len]);
1611 if (line_p != line_buf && !commit_line_buf (this))
1614 output_lines (this, 0, x->l);
1616 ff_len = ls_length (&x->ops[OPS_FORMFEED]);
1617 total_len = x->bottom_margin * nl_len + ff_len;
1618 if (s_len < total_len)
1619 s = xrealloc (s, total_len);
1620 for (cp = s, i = 0; i < x->bottom_margin; i++)
1622 memcpy (cp, ls_c_str (&x->ops[OPS_NEWLINE]), nl_len);
1625 memcpy (cp, ls_c_str (&x->ops[OPS_FORMFEED]), ff_len);
1627 output_string (this, s, &s[total_len]);
1629 if (line_p != line_buf && !commit_line_buf (this))
1632 this->page_open = 0;
1639 ascii_chart_initialise(struct outp_driver *d UNUSED, struct chart *ch )
1641 msg(MW, _("Charts are unsupported with ascii drivers."));
1646 ascii_chart_finalise(struct outp_driver *d UNUSED, struct chart *ch UNUSED)
1651 struct outp_class ascii_class =
1661 ascii_preopen_driver,
1663 ascii_postopen_driver,
1673 ascii_line_intersection,
1676 ascii_polyline_begin,
1677 ascii_polyline_point,
1680 ascii_text_set_font_by_name,
1681 ascii_text_set_font_by_position,
1682 ascii_text_set_font_by_family,
1683 ascii_text_get_font_name,
1684 ascii_text_get_font_family,
1685 ascii_text_set_size,
1686 ascii_text_get_size,
1690 ascii_chart_initialise,
1691 ascii_chart_finalise