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
36 #define _(msgid) gettext (msgid)
38 /* ASCII driver options: (defaults listed first)
40 output-file="pspp.list"
42 form-feed-string="\f" Written as a formfeed.
43 newline-string=default|"\r\n"|"\n"
45 paginate=on|off Formfeeds are desired?
46 tab-width=8 Width of a tab; 0 to not use tabs.
47 init="" Written at beginning of output.
48 done="" Written at end of output.
50 headers=on|off Put headers at top of page?
53 lpi=6 Only used to determine font size.
55 squeeze=off|on Squeeze multiple newlines into exactly one.
62 box[x]="strng" Sets box character X (X in base 4: 0-3333).
63 italic-on=overstrike|"strng" Turns on italic (underline).
64 italic-off=""|"strng" Turns off italic; ignored for overstrike.
65 bold-on=overstrike|"strng" Turns on bold.
66 bold-off=""|"strng" Turns off bold; ignored for overstrike.
67 bold-italic-on=overstrike|"strng" Turns on bold-italic.
68 bold-italic-off=""|"strng" Turns off bold-italic; ignored for overstrike.
69 overstrike-style=single|line Can we print a whole line then BS over it, or
70 must we go char by char, as on a terminal?
71 carriage-return-style=bs|cr Must we return the carriage with a sequence of
72 BSes, or will a single CR do it?
75 /* Disable messages by failed range checks. */
76 /*#define SUPPRESS_WARNINGS 1 */
81 CHS_ASCII, /* 7-bit ASCII */
82 CHS_LATIN1 /* Latin 1; not really supported at the moment */
85 /* Overstrike style. */
88 OVS_SINGLE, /* Overstrike each character: "a\b_b\b_c\b_" */
89 OVS_LINE /* Overstrike lines: "abc\b\b\b___" (or if
90 newline is "\r\n", then "abc\r___"). Easier
91 on the printer, doesn't work on a tty. */
94 /* Basic output strings. */
97 OPS_INIT, /* Document initialization string. */
98 OPS_DONE, /* Document uninit string. */
99 OPS_FORMFEED, /* Formfeed string. */
100 OPS_NEWLINE, /* Newline string. */
102 OPS_COUNT /* Number of output strings. */
105 /* Line styles bit shifts. */
116 /* Carriage return style. */
119 CRS_BS, /* Multiple backspaces. */
120 CRS_CR /* Single carriage return. */
123 /* Assembles a byte from four taystes. */
124 #define TAYSTE2BYTE(T, L, B, R) \
126 | ((L) << LNS_LEFT) \
127 | ((B) << LNS_BOTTOM) \
128 | ((R) << LNS_RIGHT))
130 /* Extract tayste with shift value S from byte B. */
131 #define BYTE2TAYSTE(B, S) \
134 /* Font style; take one of the first group |'d with one of the second group. */
137 FSTY_ON = 000, /* Turn font on. */
138 FSTY_OFF = 001, /* Turn font off. */
140 FSTY_ITALIC = 0, /* Italic font. */
141 FSTY_BOLD = 2, /* Bold font. */
142 FSTY_BOLD_ITALIC = 4, /* Bold-italic font. */
144 FSTY_COUNT = 6 /* Number of font styles. */
147 /* A line of text. */
150 unsigned short *chars; /* Characters and attributes. */
151 int char_cnt; /* Length. */
152 int char_cap; /* Allocated bytes. */
155 /* ASCII output driver extension record. */
156 struct ascii_driver_ext
158 /* User parameters. */
159 int char_set; /* CHS_ASCII/CHS_LATIN1; no-op right now. */
160 int headers; /* 1=print headers at top of page. */
161 int page_length; /* Page length in lines. */
162 int page_width; /* Page width in characters. */
163 int lpi; /* Lines per inch. */
164 int cpi; /* Characters per inch. */
165 int left_margin; /* Left margin in characters. */
166 int right_margin; /* Right margin in characters. */
167 int top_margin; /* Top margin in lines. */
168 int bottom_margin; /* Bottom margin in lines. */
169 int paginate; /* 1=insert formfeeds. */
170 int tab_width; /* Width of a tab; 0 not to use tabs. */
171 struct fixed_string ops[OPS_COUNT]; /* Basic output strings. */
172 struct fixed_string box[LNS_COUNT]; /* Line & box drawing characters. */
173 struct fixed_string fonts[FSTY_COUNT]; /* Font styles; NULL=overstrike. */
174 int overstrike_style; /* OVS_SINGLE or OVS_LINE. */
175 int carriage_return_style; /* Carriage return style. */
176 int squeeze_blank_lines; /* 1=squeeze multiple blank lines into one. */
178 /* Internal state. */
179 struct file_ext file; /* Output file. */
180 int page_number; /* Current page number. */
181 struct line *lines; /* Page content. */
182 int lines_cap; /* Number of lines allocated. */
183 int w, l; /* Actual width & length w/o margins, etc. */
184 int cur_font; /* Current font by OUTP_F_*. */
186 int debug; /* Set by som_text_draw(). */
190 static int postopen (struct file_ext *);
191 static int preclose (struct file_ext *);
193 static struct outp_option_info *option_info;
196 ascii_open_global (struct outp_class *this UNUSED)
198 option_info = xmalloc ( sizeof (struct outp_option_info ) ) ;
199 option_info->initial = 0;
200 option_info->options = 0;
205 static unsigned char *s=0;
207 ascii_close_global (struct outp_class *this UNUSED)
209 free(option_info->initial);
210 free(option_info->options);
217 ascii_font_sizes (struct outp_class *this UNUSED, int *n_valid_sizes)
219 static int valid_sizes[] = {12, 12, 0, 0};
221 assert (n_valid_sizes);
227 ascii_preopen_driver (struct outp_driver *this)
229 struct ascii_driver_ext *x;
232 assert (this->driver_open == 0);
233 msg (VM (1), _("ASCII driver initializing as `%s'..."), this->name);
234 this->ext = x = xmalloc (sizeof (struct ascii_driver_ext));
235 x->char_set = CHS_ASCII;
244 x->bottom_margin = 2;
247 for (i = 0; i < OPS_COUNT; i++)
248 ls_null (&x->ops[i]);
249 for (i = 0; i < LNS_COUNT; i++)
250 ls_null (&x->box[i]);
251 for (i = 0; i < FSTY_COUNT; i++)
252 ls_null (&x->fonts[i]);
253 x->overstrike_style = OVS_SINGLE;
254 x->carriage_return_style = CRS_BS;
255 x->squeeze_blank_lines = 0;
256 x->file.filename = NULL;
259 x->file.sequence_no = &x->page_number;
261 x->file.postopen = postopen;
262 x->file.preclose = preclose;
266 x->cur_font = OUTP_F_R;
274 ascii_postopen_driver (struct outp_driver *this)
276 struct ascii_driver_ext *x = this->ext;
278 assert (this->driver_open == 0);
280 if (NULL == x->file.filename)
281 x->file.filename = xstrdup ("pspp.list");
283 x->w = x->page_width - x->left_margin - x->right_margin;
284 x->l = (x->page_length - (x->headers ? 3 : 0) - x->top_margin
285 - x->bottom_margin - 1);
286 if (x->w < 59 || x->l < 15)
288 msg (SE, _("ascii driver: Area of page excluding margins and headers "
289 "must be at least 59 characters wide by 15 lines long. Page as "
290 "configured is only %d characters by %d lines."), x->w, x->l);
294 this->res = x->lpi * x->cpi;
295 this->horiz = x->lpi;
297 this->width = x->w * this->horiz;
298 this->length = x->l * this->vert;
300 if (ls_null_p (&x->ops[OPS_FORMFEED]))
301 ls_create (&x->ops[OPS_FORMFEED], "\f");
302 if (ls_null_p (&x->ops[OPS_NEWLINE])
303 || !strcmp (ls_c_str (&x->ops[OPS_NEWLINE]), "default"))
305 ls_create (&x->ops[OPS_NEWLINE], "\n");
312 for (i = 0; i < LNS_COUNT; i++)
316 if (!ls_null_p (&x->box[i]))
320 case TAYSTE2BYTE (0, 0, 0, 0):
324 case TAYSTE2BYTE (0, 1, 0, 0):
325 case TAYSTE2BYTE (0, 1, 0, 1):
326 case TAYSTE2BYTE (0, 0, 0, 1):
330 case TAYSTE2BYTE (1, 0, 0, 0):
331 case TAYSTE2BYTE (1, 0, 1, 0):
332 case TAYSTE2BYTE (0, 0, 1, 0):
336 case TAYSTE2BYTE (0, 3, 0, 0):
337 case TAYSTE2BYTE (0, 3, 0, 3):
338 case TAYSTE2BYTE (0, 0, 0, 3):
339 case TAYSTE2BYTE (0, 2, 0, 0):
340 case TAYSTE2BYTE (0, 2, 0, 2):
341 case TAYSTE2BYTE (0, 0, 0, 2):
345 case TAYSTE2BYTE (3, 0, 0, 0):
346 case TAYSTE2BYTE (3, 0, 3, 0):
347 case TAYSTE2BYTE (0, 0, 3, 0):
348 case TAYSTE2BYTE (2, 0, 0, 0):
349 case TAYSTE2BYTE (2, 0, 2, 0):
350 case TAYSTE2BYTE (0, 0, 2, 0):
355 if (BYTE2TAYSTE (i, LNS_LEFT) > 1
356 || BYTE2TAYSTE (i, LNS_TOP) > 1
357 || BYTE2TAYSTE (i, LNS_RIGHT) > 1
358 || BYTE2TAYSTE (i, LNS_BOTTOM) > 1)
364 ls_create (&x->box[i], c);
371 this->cp_x = this->cp_y = 0;
372 this->font_height = this->vert;
373 this->prop_em_width = this->horiz;
374 this->fixed_width = this->horiz;
376 this->horiz_line_width[0] = 0;
377 this->vert_line_width[0] = 0;
379 for (i = 1; i < OUTP_L_COUNT; i++)
381 this->horiz_line_width[i] = this->vert;
382 this->vert_line_width[i] = this->horiz;
385 for (i = 0; i < (1 << OUTP_L_COUNT); i++)
387 this->horiz_line_spacing[i] = (i & ~1) ? this->vert : 0;
388 this->vert_line_spacing[i] = (i & ~1) ? this->horiz : 0;
392 this->driver_open = 1;
393 msg (VM (2), _("%s: Initialization complete."), this->name);
399 ascii_close_driver (struct outp_driver *this)
401 struct ascii_driver_ext *x = this->ext;
404 assert (this->driver_open == 1);
405 msg (VM (2), _("%s: Beginning closing..."), this->name);
408 for (i = 0; i < OPS_COUNT; i++)
409 ls_destroy (&x->ops[i]);
410 for (i = 0; i < LNS_COUNT; i++)
411 ls_destroy (&x->box[i]);
412 for (i = 0; i < FSTY_COUNT; i++)
413 ls_destroy (&x->fonts[i]);
414 if (x->lines != NULL)
418 for (line = 0; line < x->lines_cap; line++)
419 free (x->lines[line].chars);
422 fn_close_ext (&x->file);
423 free (x->file.filename);
426 this->driver_open = 0;
427 msg (VM (3), _("%s: Finished closing."), this->name);
432 /* Generic option types. */
442 static struct outp_option option_tab[] =
444 {"headers", boolean_arg, 0},
445 {"output-file", 1, 0},
447 {"length", pos_int_arg, 0},
448 {"width", pos_int_arg, 1},
449 {"lpi", pos_int_arg, 2},
450 {"cpi", pos_int_arg, 3},
451 {"init", string_arg, 0},
452 {"done", string_arg, 1},
453 {"left-margin", nonneg_int_arg, 0},
454 {"right-margin", nonneg_int_arg, 1},
455 {"top-margin", nonneg_int_arg, 2},
456 {"bottom-margin", nonneg_int_arg, 3},
457 {"paginate", boolean_arg, 1},
458 {"form-feed-string", string_arg, 2},
459 {"newline-string", string_arg, 3},
460 {"italic-on", font_string_arg, 0},
461 {"italic-off", font_string_arg, 1},
462 {"bold-on", font_string_arg, 2},
463 {"bold-off", font_string_arg, 3},
464 {"bold-italic-on", font_string_arg, 4},
465 {"bold-italic-off", font_string_arg, 5},
466 {"overstrike-style", 3, 0},
467 {"tab-width", nonneg_int_arg, 4},
468 {"carriage-return-style", 4, 0},
469 {"squeeze", boolean_arg, 2},
474 ascii_option (struct outp_driver *this, const char *key,
475 const struct string *val)
477 struct ascii_driver_ext *x = this->ext;
481 value = ds_c_str (val);
482 if (!strncmp (key, "box[", 4))
485 int indx = strtol (&key[4], &tail, 4);
486 if (*tail != ']' || indx < 0 || indx > LNS_COUNT)
488 msg (SE, _("Bad index value for `box' key: syntax is box[INDEX], "
489 "0 <= INDEX < %d decimal, with INDEX expressed in base 4."),
493 if (!ls_null_p (&x->box[indx]))
494 msg (SW, _("Duplicate value for key `%s'."), key);
495 ls_create (&x->box[indx], value);
499 cat = outp_match_keyword (key, option_tab, option_info, &subcat);
503 msg (SE, _("Unknown configuration parameter `%s' for ascii device driver."),
507 free (x->file.filename);
508 x->file.filename = xstrdup (value);
511 if (!strcmp (value, "ascii"))
512 x->char_set = CHS_ASCII;
513 else if (!strcmp (value, "latin1"))
514 x->char_set = CHS_LATIN1;
516 msg (SE, _("Unknown character set `%s'. Valid character sets are "
517 "`ascii' and `latin1'."), value);
520 if (!strcmp (value, "single"))
521 x->overstrike_style = OVS_SINGLE;
522 else if (!strcmp (value, "line"))
523 x->overstrike_style = OVS_LINE;
525 msg (SE, _("Unknown overstrike style `%s'. Valid overstrike styles "
526 "are `single' and `line'."), value);
529 if (!strcmp (value, "bs"))
530 x->carriage_return_style = CRS_BS;
531 else if (!strcmp (value, "cr"))
532 x->carriage_return_style = CRS_CR;
534 msg (SE, _("Unknown carriage return style `%s'. Valid carriage "
535 "return styles are `cr' and `bs'."), value);
543 arg = strtol (value, &tail, 0);
544 if (arg < 1 || errno == ERANGE || *tail)
546 msg (SE, _("Positive integer required as value for `%s'."), key);
552 x->page_length = arg;
574 arg = strtol (value, &tail, 0);
575 if (arg < 0 || errno == ERANGE || *tail)
577 msg (SE, _("Zero or positive integer required as value for `%s'."),
584 x->left_margin = arg;
587 x->right_margin = arg;
593 x->bottom_margin = arg;
605 struct fixed_string *s;
609 s = &x->ops[OPS_INIT];
612 s = &x->ops[OPS_DONE];
615 s = &x->ops[OPS_FORMFEED];
618 s = &x->ops[OPS_NEWLINE];
624 ls_create (s, value);
627 case font_string_arg:
629 if (!strcmp (value, "overstrike"))
631 ls_destroy (&x->fonts[subcat]);
634 ls_create (&x->fonts[subcat], value);
640 if (!strcmp (value, "on") || !strcmp (value, "true")
641 || !strcmp (value, "yes") || atoi (value))
643 else if (!strcmp (value, "off") || !strcmp (value, "false")
644 || !strcmp (value, "no") || !strcmp (value, "0"))
648 msg (SE, _("Boolean value expected for %s."), key);
654 x->headers = setting;
657 x->paginate = setting;
660 x->squeeze_blank_lines = setting;
673 postopen (struct file_ext *f)
675 struct ascii_driver_ext *x = f->param;
676 struct fixed_string *s = &x->ops[OPS_INIT];
678 if (!ls_empty_p (s) && fwrite (ls_c_str (s), ls_length (s), 1, f->file) < 1)
680 msg (ME, _("ASCII output driver: %s: %s"),
681 f->filename, strerror (errno));
688 preclose (struct file_ext *f)
690 struct ascii_driver_ext *x = f->param;
691 struct fixed_string *d = &x->ops[OPS_DONE];
693 if (!ls_empty_p (d) && fwrite (ls_c_str (d), ls_length (d), 1, f->file) < 1)
695 msg (ME, _("ASCII output driver: %s: %s"),
696 f->filename, strerror (errno));
703 ascii_open_page (struct outp_driver *this)
705 struct ascii_driver_ext *x = this->ext;
708 assert (this->driver_open && !this->page_open);
710 if (!fn_open_ext (&x->file))
712 msg (ME, _("ASCII output driver: %s: %s"), x->file.filename,
717 if (x->l > x->lines_cap)
719 x->lines = xrealloc (x->lines, sizeof *x->lines * x->l);
720 for (i = x->lines_cap; i < x->l; i++)
722 struct line *line = &x->lines[i];
729 for (i = 0; i < x->l; i++)
730 x->lines[i].char_cnt = 0;
736 /* Ensures that at least the first L characters of line I in the
737 driver identified by struct ascii_driver_ext *X have been cleared out. */
739 expand_line (struct ascii_driver_ext *x, int i, int l)
744 assert (i < x->lines_cap);
746 if (l > line->char_cap)
748 line->char_cap = l * 2;
749 line->chars = xrealloc (line->chars,
750 line->char_cap * sizeof *line->chars);
752 for (j = line->char_cnt; j < l; j++)
753 line->chars[j] = ' ';
757 /* Puts line L at (H,K) in the current output page. Assumes
758 struct ascii_driver_ext named `ext'. */
759 #define draw_line(H, K, L) \
760 ext->lines[K].chars[H] = (L) | 0x800
762 /* Line styles for each position. */
763 #define T(STYLE) (STYLE<<LNS_TOP)
764 #define L(STYLE) (STYLE<<LNS_LEFT)
765 #define B(STYLE) (STYLE<<LNS_BOTTOM)
766 #define R(STYLE) (STYLE<<LNS_RIGHT)
769 ascii_line_horz (struct outp_driver *this, const struct rect *r,
770 const struct color *c UNUSED, int style)
772 struct ascii_driver_ext *ext = this->ext;
773 int x1 = r->x1 / this->horiz;
774 int x2 = r->x2 / this->horiz;
775 int y1 = r->y1 / this->vert;
778 assert (this->driver_open && this->page_open);
783 || x1 < 0 || x1 >= ext->w
784 || x2 <= 0 || x2 > ext->w
785 || y1 < 0 || y1 >= ext->l)
787 #if !SUPPRESS_WARNINGS
788 printf (_("ascii_line_horz: bad hline (%d,%d),%d out of (%d,%d)\n"),
789 x1, x2, y1, ext->w, ext->l);
795 if (ext->lines[y1].char_cnt < x2)
796 expand_line (ext, y1, x2);
798 for (x = x1; x < x2; x++)
799 draw_line (x, y1, (style << LNS_LEFT) | (style << LNS_RIGHT));
803 ascii_line_vert (struct outp_driver *this, const struct rect *r,
804 const struct color *c UNUSED, int style)
806 struct ascii_driver_ext *ext = this->ext;
807 int x1 = r->x1 / this->horiz;
808 int y1 = r->y1 / this->vert;
809 int y2 = r->y2 / this->vert;
812 assert (this->driver_open && this->page_open);
817 || x1 < 0 || x1 >= ext->w
818 || y1 < 0 || y1 >= ext->l
819 || y2 < 0 || y2 > ext->l)
821 #if !SUPPRESS_WARNINGS
822 printf (_("ascii_line_vert: bad vline %d,(%d,%d) out of (%d,%d)\n"),
823 x1, y1, y2, ext->w, ext->l);
829 for (y = y1; y < y2; y++)
830 if (ext->lines[y].char_cnt <= x1)
831 expand_line (ext, y, x1 + 1);
833 for (y = y1; y < y2; y++)
834 draw_line (x1, y, (style << LNS_TOP) | (style << LNS_BOTTOM));
838 ascii_line_intersection (struct outp_driver *this, const struct rect *r,
839 const struct color *c UNUSED,
840 const struct outp_styles *style)
842 struct ascii_driver_ext *ext = this->ext;
843 int x = r->x1 / this->horiz;
844 int y = r->y1 / this->vert;
847 assert (this->driver_open && this->page_open);
849 if (x < 0 || x >= ext->w || y < 0 || y >= ext->l)
851 #if !SUPPRESS_WARNINGS
852 printf (_("ascii_line_intersection: bad intsct (%d,%d) out of (%d,%d)\n"),
853 x, y, ext->w, ext->l);
859 l = ((style->l << LNS_LEFT) | (style->r << LNS_RIGHT)
860 | (style->t << LNS_TOP) | (style->b << LNS_BOTTOM));
862 if (ext->lines[y].char_cnt <= x)
863 expand_line (ext, y, x + 1);
867 /* FIXME: Later we could set this up so that for certain devices it
870 ascii_box (struct outp_driver *this UNUSED, const struct rect *r UNUSED,
871 const struct color *bord UNUSED, const struct color *fill UNUSED)
873 assert (this->driver_open && this->page_open);
876 /* Polylines not supported. */
878 ascii_polyline_begin (struct outp_driver *this UNUSED, const struct color *c UNUSED)
880 assert (this->driver_open && this->page_open);
883 ascii_polyline_point (struct outp_driver *this UNUSED, int x UNUSED, int y UNUSED)
885 assert (this->driver_open && this->page_open);
888 ascii_polyline_end (struct outp_driver *this UNUSED)
890 assert (this->driver_open && this->page_open);
894 ascii_text_set_font_by_name (struct outp_driver * this, const char *s)
896 struct ascii_driver_ext *x = this->ext;
897 int len = strlen (s);
899 assert (this->driver_open && this->page_open);
900 x->cur_font = OUTP_F_R;
903 if (s[len - 1] == 'I')
905 if (len > 1 && s[len - 2] == 'B')
906 x->cur_font = OUTP_F_BI;
908 x->cur_font = OUTP_F_I;
910 else if (s[len - 1] == 'B')
911 x->cur_font = OUTP_F_B;
915 ascii_text_set_font_by_position (struct outp_driver *this, int pos)
917 struct ascii_driver_ext *x = this->ext;
918 assert (this->driver_open && this->page_open);
919 x->cur_font = pos >= 0 && pos < 4 ? pos : 0;
923 ascii_text_set_font_by_family (struct outp_driver *this UNUSED, const char *s UNUSED)
925 assert (this->driver_open && this->page_open);
929 ascii_text_get_font_name (struct outp_driver *this)
931 struct ascii_driver_ext *x = this->ext;
933 assert (this->driver_open && this->page_open);
951 ascii_text_get_font_family (struct outp_driver *this UNUSED)
953 assert (this->driver_open && this->page_open);
958 ascii_text_set_size (struct outp_driver *this, int size)
960 assert (this->driver_open && this->page_open);
961 return size == this->vert;
965 ascii_text_get_size (struct outp_driver *this, int *em_width)
967 assert (this->driver_open && this->page_open);
969 *em_width = this->horiz;
973 static void text_draw (struct outp_driver *this, struct outp_text *t);
975 /* Divides the text T->S into lines of width T->H. Sets T->V to the
976 number of lines necessary. Actually draws the text if DRAW is
979 You probably don't want to look at this code. */
981 delineate (struct outp_driver *this, struct outp_text *t, int draw)
983 /* Width we're fitting everything into. */
984 int width = t->h / this->horiz;
986 /* Maximum `y' position we can write to. */
989 /* Current position in string, character following end of string. */
990 const char *s = ls_c_str (&t->s);
991 const char *end = ls_end (&t->s);
993 /* Temporary struct outp_text to pass to low-level function. */
994 struct outp_text temp;
996 #if GLOBAL_DEBUGGING && 0
1000 printf (_("%s: horiz=%d, vert=%d\n"), this->name, this->horiz, this->vert);
1012 temp.options = t->options;
1013 ls_shallow_copy (&temp.s, &t->s);
1014 temp.h = t->h / this->horiz;
1015 temp.x = t->x / this->horiz;
1019 temp.y = t->y / this->vert;
1021 if (t->options & OUTP_T_VERT)
1022 max_y = (t->v / this->vert) + temp.y - 1;
1026 while (end - s > width)
1028 const char *beg = s;
1031 /* Find first space before &s[width]. */
1037 if (!isspace ((unsigned char) space[-1]))
1046 s = space = &s[width];
1053 ls_init (&temp.s, beg, space - beg);
1054 temp.w = space - beg;
1055 text_draw (this, &temp);
1057 if (++temp.y > max_y)
1060 /* Find first nonspace after space. */
1061 while (s < end && isspace ((unsigned char) *s))
1068 ls_init (&temp.s, s, end - s);
1070 text_draw (this, &temp);
1075 t->v = (temp.y * this->vert) - t->y;
1079 ascii_text_metrics (struct outp_driver *this, struct outp_text *t)
1081 assert (this->driver_open && this->page_open);
1082 if (!(t->options & OUTP_T_HORZ))
1085 t->h = ls_length (&t->s) * this->horiz;
1088 delineate (this, t, 0);
1092 ascii_text_draw (struct outp_driver *this, struct outp_text *t)
1094 /* FIXME: orientations not supported. */
1095 assert (this->driver_open && this->page_open);
1096 if (!(t->options & OUTP_T_HORZ))
1098 struct outp_text temp;
1100 temp.options = t->options;
1102 temp.h = temp.v = 0;
1103 temp.x = t->x / this->horiz;
1104 temp.y = t->y / this->vert;
1105 text_draw (this, &temp);
1106 ascii_text_metrics (this, t);
1110 delineate (this, t, 1);
1114 text_draw (struct outp_driver *this, struct outp_text *t)
1116 struct ascii_driver_ext *ext = this->ext;
1117 unsigned attr = ext->cur_font << 8;
1122 char *s = ls_c_str (&t->s);
1124 /* Expand the line with the assumption that S takes up LEN character
1125 spaces (sometimes it takes up less). */
1128 assert (this->driver_open && this->page_open);
1129 switch (t->options & OUTP_T_JUST_MASK)
1131 case OUTP_T_JUST_LEFT:
1133 case OUTP_T_JUST_CENTER:
1134 x -= (t->h - t->w) / 2; /* fall through */
1135 case OUTP_T_JUST_RIGHT:
1142 if (!(t->y < ext->l && x < ext->w))
1144 min_len = min (x + ls_length (&t->s), ext->w);
1145 if (ext->lines[t->y].char_cnt < min_len)
1146 expand_line (ext, t->y, min_len);
1149 int len = ls_length (&t->s);
1151 if (len + x > ext->w)
1154 ext->lines[y].chars[x++] = *s++ | attr;
1158 /* ascii_close_page () and support routines. */
1160 #define LINE_BUF_SIZE 1024
1161 static unsigned char *line_buf;
1162 static unsigned char *line_p;
1165 commit_line_buf (struct outp_driver *this)
1167 struct ascii_driver_ext *x = this->ext;
1169 if ((int) fwrite (line_buf, 1, line_p - line_buf, x->file.file)
1170 < line_p - line_buf)
1172 msg (ME, _("Writing `%s': %s"), x->file.filename, strerror (errno));
1180 /* Writes everything from BP to EP exclusive into line_buf, or to
1181 THIS->output if line_buf overflows. */
1183 output_string (struct outp_driver *this, const char *bp, const char *ep)
1185 if (LINE_BUF_SIZE - (line_p - line_buf) >= ep - bp)
1187 memcpy (line_p, bp, ep - bp);
1193 if (LINE_BUF_SIZE - (line_p - line_buf) <= 1 && !commit_line_buf (this))
1199 /* Writes everything from BP to EP exclusive into line_buf, or to
1200 THIS->output if line_buf overflows. Returns 1 if additional passes
1201 over the line are required. FIXME: probably could do a lot of
1202 optimization here. */
1204 output_shorts (struct outp_driver *this,
1205 const unsigned short *bp, const unsigned short *ep)
1207 struct ascii_driver_ext *ext = this->ext;
1208 size_t remaining = LINE_BUF_SIZE - (line_p - line_buf);
1211 for (; bp < ep; bp++)
1215 struct fixed_string *box = &ext->box[*bp & 0xff];
1216 size_t len = ls_length (box);
1218 if (remaining >= len)
1220 memcpy (line_p, ls_c_str (box), len);
1226 if (!commit_line_buf (this))
1228 output_string (this, ls_c_str (box), ls_end (box));
1229 remaining = LINE_BUF_SIZE - (line_p - line_buf);
1232 else if (*bp & 0x0300)
1234 struct fixed_string *on;
1238 switch (*bp & 0x0300)
1241 on = &ext->fonts[FSTY_ON | FSTY_ITALIC];
1244 on = &ext->fonts[FSTY_ON | FSTY_BOLD];
1246 case OUTP_F_BI << 8:
1247 on = &ext->fonts[FSTY_ON | FSTY_BOLD_ITALIC];
1255 if (ext->overstrike_style == OVS_SINGLE)
1256 switch (*bp & 0x0300)
1270 case OUTP_F_BI << 8:
1293 output_string (this, buf, &buf[len]);
1302 if (!commit_line_buf (this))
1304 remaining = LINE_BUF_SIZE - (line_p - line_buf);
1312 /* Writes CH into line_buf N times, or to THIS->output if line_buf
1315 output_char (struct outp_driver *this, int n, int ch)
1317 if (LINE_BUF_SIZE - (line_p - line_buf) >= n)
1319 memset (line_p, ch, n);
1325 if (LINE_BUF_SIZE - (line_p - line_buf) <= 1 && !commit_line_buf (this))
1331 /* Advance the carriage from column 0 to the left margin. */
1333 advance_to_left_margin (struct outp_driver *this)
1335 struct ascii_driver_ext *ext = this->ext;
1338 margin = ext->left_margin;
1341 if (ext->tab_width && margin >= ext->tab_width)
1343 output_char (this, margin / ext->tab_width, '\t');
1344 margin %= ext->tab_width;
1347 output_char (this, margin, ' ');
1350 /* Move the output file carriage N_CHARS left, to the left margin. */
1352 return_carriage (struct outp_driver *this, int n_chars)
1354 struct ascii_driver_ext *ext = this->ext;
1356 switch (ext->carriage_return_style)
1359 output_char (this, n_chars, '\b');
1362 output_char (this, 1, '\r');
1363 advance_to_left_margin (this);
1371 /* Writes COUNT lines from the line buffer in THIS, starting at line
1374 output_lines (struct outp_driver *this, int first, int count)
1376 struct ascii_driver_ext *ext = this->ext;
1379 struct fixed_string *newline = &ext->ops[OPS_NEWLINE];
1384 if (NULL == ext->file.file)
1387 /* Iterate over all the lines to be output. */
1388 for (line_num = first; line_num < first + count; line_num++)
1390 struct line *line = &ext->lines[line_num];
1391 unsigned short *p = line->chars;
1392 unsigned short *end_p = p + line->char_cnt;
1393 unsigned short *bp, *ep;
1394 unsigned short attr = 0;
1396 assert (end_p >= p);
1398 /* Squeeze multiple blank lines into a single blank line if
1400 if (ext->squeeze_blank_lines
1402 && ext->lines[line_num].char_cnt == 0
1403 && ext->lines[line_num - 1].char_cnt == 0)
1406 /* Output every character in the line in the appropriate
1411 advance_to_left_margin (this);
1414 while (ep < end_p && attr == (*ep & 0x0300))
1416 if (output_shorts (this, bp, ep))
1424 /* Turn off old font. */
1425 if (attr != (OUTP_F_R << 8))
1427 struct fixed_string *off;
1432 off = &ext->fonts[FSTY_OFF | FSTY_ITALIC];
1435 off = &ext->fonts[FSTY_OFF | FSTY_BOLD];
1437 case OUTP_F_BI << 8:
1438 off = &ext->fonts[FSTY_OFF | FSTY_BOLD_ITALIC];
1445 output_string (this, ls_c_str (off), ls_end (off));
1448 /* Turn on new font. */
1449 attr = (*bp & 0x0300);
1450 if (attr != (OUTP_F_R << 8))
1452 struct fixed_string *on;
1457 on = &ext->fonts[FSTY_ON | FSTY_ITALIC];
1460 on = &ext->fonts[FSTY_ON | FSTY_BOLD];
1462 case OUTP_F_BI << 8:
1463 on = &ext->fonts[FSTY_ON | FSTY_BOLD_ITALIC];
1470 output_string (this, ls_c_str (on), ls_end (on));
1479 return_carriage (this, n_chars);
1484 while (ep < end_p && (*ep & 0x0300) == (OUTP_F_R << 8))
1488 output_char (this, ep - bp, ' ');
1490 switch (*ep & 0x0300)
1498 case OUTP_F_BI << 8:
1506 output_char (this, 1, ch);
1507 n_chars += ep - bp + 1;
1514 return_carriage (this, n_chars);
1518 while (ep < end_p && (*ep & 0x0300) != (OUTP_F_BI << 8))
1522 output_char (this, ep - bp, ' ');
1523 output_char (this, 1, '_');
1529 output_string (this, ls_c_str (newline), ls_end (newline));
1535 ascii_close_page (struct outp_driver *this)
1539 struct ascii_driver_ext *x = this->ext;
1540 int nl_len, ff_len, total_len;
1544 assert (this->driver_open && this->page_open);
1547 line_buf = xmalloc (LINE_BUF_SIZE);
1550 nl_len = ls_length (&x->ops[OPS_NEWLINE]);
1553 total_len = x->top_margin * nl_len;
1554 if (s_len < total_len)
1557 s = xrealloc (s, s_len);
1559 for (cp = s, i = 0; i < x->top_margin; i++)
1561 memcpy (cp, ls_c_str (&x->ops[OPS_NEWLINE]), nl_len);
1564 output_string (this, s, &s[total_len]);
1570 total_len = nl_len + x->w;
1571 if (s_len < total_len + 1)
1573 s_len = total_len + 1;
1574 s = xrealloc (s, s_len);
1577 memset (s, ' ', x->w);
1582 snprintf (temp, 80, _("%s - Page %d"), curdate, x->page_number);
1583 memcpy (&s[x->w - strlen (temp)], temp, strlen (temp));
1586 if (outp_title && outp_subtitle)
1588 len = min ((int) strlen (outp_title), x->w);
1589 memcpy (s, outp_title, len);
1591 memcpy (&s[x->w], ls_c_str (&x->ops[OPS_NEWLINE]), nl_len);
1592 output_string (this, s, &s[total_len]);
1594 memset (s, ' ', x->w);
1595 len = strlen (version) + 3 + strlen (host_system);
1597 sprintf (&s[x->w - len], "%s - %s" , version, host_system);
1598 if (outp_subtitle || outp_title)
1600 char *string = outp_subtitle ? outp_subtitle : outp_title;
1601 len = min ((int) strlen (string), x->w);
1602 memcpy (s, string, len);
1604 memcpy (&s[x->w], ls_c_str (&x->ops[OPS_NEWLINE]), nl_len);
1605 output_string (this, s, &s[total_len]);
1606 output_string (this, &s[x->w], &s[total_len]);
1608 if (line_p != line_buf && !commit_line_buf (this))
1611 output_lines (this, 0, x->l);
1613 ff_len = ls_length (&x->ops[OPS_FORMFEED]);
1614 total_len = x->bottom_margin * nl_len + ff_len;
1615 if (s_len < total_len)
1616 s = xrealloc (s, total_len);
1617 for (cp = s, i = 0; i < x->bottom_margin; i++)
1619 memcpy (cp, ls_c_str (&x->ops[OPS_NEWLINE]), nl_len);
1622 memcpy (cp, ls_c_str (&x->ops[OPS_FORMFEED]), ff_len);
1624 output_string (this, s, &s[total_len]);
1626 if (line_p != line_buf && !commit_line_buf (this))
1633 this->page_open = 0;
1640 ascii_chart_initialise(struct outp_driver *d UNUSED, struct chart *ch )
1642 msg(MW, _("Charts are unsupported with ascii drivers."));
1647 ascii_chart_finalise(struct outp_driver *d UNUSED, struct chart *ch UNUSED)
1652 struct outp_class ascii_class =
1662 ascii_preopen_driver,
1664 ascii_postopen_driver,
1674 ascii_line_intersection,
1677 ascii_polyline_begin,
1678 ascii_polyline_point,
1681 ascii_text_set_font_by_name,
1682 ascii_text_set_font_by_position,
1683 ascii_text_set_font_by_family,
1684 ascii_text_get_font_name,
1685 ascii_text_get_font_family,
1686 ascii_text_set_size,
1687 ascii_text_get_size,
1691 ascii_chart_initialise,
1692 ascii_chart_finalise