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
37 #define _(msgid) gettext (msgid)
39 /* ASCII driver options: (defaults listed first)
41 output-file="pspp.list"
43 form-feed-string="\f" Written as a formfeed.
44 newline-string=default|"\r\n"|"\n"
46 paginate=on|off Formfeeds are desired?
47 tab-width=8 Width of a tab; 0 to not use tabs.
48 init="" Written at beginning of output.
49 done="" Written at end of output.
51 headers=on|off Put headers at top of page?
54 lpi=6 Only used to determine font size.
56 squeeze=off|on Squeeze multiple newlines into exactly one.
63 box[x]="strng" Sets box character X (X in base 4: 0-3333).
64 italic-on=overstrike|"strng" Turns on italic (underline).
65 italic-off=""|"strng" Turns off italic; ignored for overstrike.
66 bold-on=overstrike|"strng" Turns on bold.
67 bold-off=""|"strng" Turns off bold; ignored for overstrike.
68 bold-italic-on=overstrike|"strng" Turns on bold-italic.
69 bold-italic-off=""|"strng" Turns off bold-italic; ignored for overstrike.
70 overstrike-style=single|line Can we print a whole line then BS over it, or
71 must we go char by char, as on a terminal?
72 carriage-return-style=bs|cr Must we return the carriage with a sequence of
73 BSes, or will a single CR do it?
76 /* Disable messages by failed range checks. */
77 /*#define SUPPRESS_WARNINGS 1 */
82 CHS_ASCII, /* 7-bit ASCII */
83 CHS_LATIN1 /* Latin 1; not really supported at the moment */
86 /* Overstrike style. */
89 OVS_SINGLE, /* Overstrike each character: "a\b_b\b_c\b_" */
90 OVS_LINE /* Overstrike lines: "abc\b\b\b___" (or if
91 newline is "\r\n", then "abc\r___"). Easier
92 on the printer, doesn't work on a tty. */
95 /* Basic output strings. */
98 OPS_INIT, /* Document initialization string. */
99 OPS_DONE, /* Document uninit string. */
100 OPS_FORMFEED, /* Formfeed string. */
101 OPS_NEWLINE, /* Newline string. */
103 OPS_COUNT /* Number of output strings. */
106 /* Line styles bit shifts. */
117 /* Carriage return style. */
120 CRS_BS, /* Multiple backspaces. */
121 CRS_CR /* Single carriage return. */
124 /* Assembles a byte from four taystes. */
125 #define TAYSTE2BYTE(T, L, B, R) \
127 | ((L) << LNS_LEFT) \
128 | ((B) << LNS_BOTTOM) \
129 | ((R) << LNS_RIGHT))
131 /* Extract tayste with shift value S from byte B. */
132 #define BYTE2TAYSTE(B, S) \
135 /* Font style; take one of the first group |'d with one of the second group. */
138 FSTY_ON = 000, /* Turn font on. */
139 FSTY_OFF = 001, /* Turn font off. */
141 FSTY_ITALIC = 0, /* Italic font. */
142 FSTY_BOLD = 2, /* Bold font. */
143 FSTY_BOLD_ITALIC = 4, /* Bold-italic font. */
145 FSTY_COUNT = 6 /* Number of font styles. */
148 /* A line of text. */
151 unsigned short *chars; /* Characters and attributes. */
152 int char_cnt; /* Length. */
153 int char_cap; /* Allocated bytes. */
156 /* ASCII output driver extension record. */
157 struct ascii_driver_ext
159 /* User parameters. */
160 int char_set; /* CHS_ASCII/CHS_LATIN1; no-op right now. */
161 int headers; /* 1=print headers at top of page. */
162 int page_length; /* Page length in lines. */
163 int page_width; /* Page width in characters. */
164 int lpi; /* Lines per inch. */
165 int cpi; /* Characters per inch. */
166 int left_margin; /* Left margin in characters. */
167 int right_margin; /* Right margin in characters. */
168 int top_margin; /* Top margin in lines. */
169 int bottom_margin; /* Bottom margin in lines. */
170 int paginate; /* 1=insert formfeeds. */
171 int tab_width; /* Width of a tab; 0 not to use tabs. */
172 struct fixed_string ops[OPS_COUNT]; /* Basic output strings. */
173 struct fixed_string box[LNS_COUNT]; /* Line & box drawing characters. */
174 struct fixed_string fonts[FSTY_COUNT]; /* Font styles; NULL=overstrike. */
175 int overstrike_style; /* OVS_SINGLE or OVS_LINE. */
176 int carriage_return_style; /* Carriage return style. */
177 int squeeze_blank_lines; /* 1=squeeze multiple blank lines into one. */
179 /* Internal state. */
180 struct file_ext file; /* Output file. */
181 int page_number; /* Current page number. */
182 struct line *lines; /* Page content. */
183 int lines_cap; /* Number of lines allocated. */
184 int w, l; /* Actual width & length w/o margins, etc. */
185 int cur_font; /* Current font by OUTP_F_*. */
187 int debug; /* Set by som_text_draw(). */
191 static int postopen (struct file_ext *);
192 static int preclose (struct file_ext *);
194 static struct outp_option_info *option_info;
197 ascii_open_global (struct outp_class *this UNUSED)
199 option_info = xmalloc (sizeof *option_info);
200 option_info->initial = 0;
201 option_info->options = 0;
208 ascii_close_global (struct outp_class *this UNUSED)
210 free(option_info->initial);
211 free(option_info->options);
218 ascii_font_sizes (struct outp_class *this UNUSED, int *n_valid_sizes)
220 static int valid_sizes[] = {12, 12, 0, 0};
222 assert (n_valid_sizes);
228 ascii_preopen_driver (struct outp_driver *this)
230 struct ascii_driver_ext *x;
233 assert (this->driver_open == 0);
234 msg (VM (1), _("ASCII driver initializing as `%s'..."), this->name);
235 this->ext = x = xmalloc (sizeof *x);
236 x->char_set = CHS_ASCII;
245 x->bottom_margin = 2;
248 for (i = 0; i < OPS_COUNT; i++)
249 ls_null (&x->ops[i]);
250 for (i = 0; i < LNS_COUNT; i++)
251 ls_null (&x->box[i]);
252 for (i = 0; i < FSTY_COUNT; i++)
253 ls_null (&x->fonts[i]);
254 x->overstrike_style = OVS_SINGLE;
255 x->carriage_return_style = CRS_BS;
256 x->squeeze_blank_lines = 0;
257 x->file.filename = NULL;
260 x->file.sequence_no = &x->page_number;
262 x->file.postopen = postopen;
263 x->file.preclose = preclose;
267 x->cur_font = OUTP_F_R;
275 ascii_postopen_driver (struct outp_driver *this)
277 struct ascii_driver_ext *x = this->ext;
279 assert (this->driver_open == 0);
281 if (NULL == x->file.filename)
282 x->file.filename = xstrdup ("pspp.list");
284 x->w = x->page_width - x->left_margin - x->right_margin;
285 x->l = (x->page_length - (x->headers ? 3 : 0) - x->top_margin
286 - x->bottom_margin - 1);
287 if (x->w < 59 || x->l < 15)
289 msg (SE, _("ascii driver: Area of page excluding margins and headers "
290 "must be at least 59 characters wide by 15 lines long. Page as "
291 "configured is only %d characters by %d lines."), x->w, x->l);
295 this->res = x->lpi * x->cpi;
296 this->horiz = x->lpi;
298 this->width = x->w * this->horiz;
299 this->length = x->l * this->vert;
301 if (ls_null_p (&x->ops[OPS_FORMFEED]))
302 ls_create (&x->ops[OPS_FORMFEED], "\f");
303 if (ls_null_p (&x->ops[OPS_NEWLINE])
304 || !strcmp (ls_c_str (&x->ops[OPS_NEWLINE]), "default"))
306 ls_create (&x->ops[OPS_NEWLINE], "\n");
313 for (i = 0; i < LNS_COUNT; i++)
317 if (!ls_null_p (&x->box[i]))
321 case TAYSTE2BYTE (0, 0, 0, 0):
325 case TAYSTE2BYTE (0, 1, 0, 0):
326 case TAYSTE2BYTE (0, 1, 0, 1):
327 case TAYSTE2BYTE (0, 0, 0, 1):
331 case TAYSTE2BYTE (1, 0, 0, 0):
332 case TAYSTE2BYTE (1, 0, 1, 0):
333 case TAYSTE2BYTE (0, 0, 1, 0):
337 case TAYSTE2BYTE (0, 3, 0, 0):
338 case TAYSTE2BYTE (0, 3, 0, 3):
339 case TAYSTE2BYTE (0, 0, 0, 3):
340 case TAYSTE2BYTE (0, 2, 0, 0):
341 case TAYSTE2BYTE (0, 2, 0, 2):
342 case TAYSTE2BYTE (0, 0, 0, 2):
346 case TAYSTE2BYTE (3, 0, 0, 0):
347 case TAYSTE2BYTE (3, 0, 3, 0):
348 case TAYSTE2BYTE (0, 0, 3, 0):
349 case TAYSTE2BYTE (2, 0, 0, 0):
350 case TAYSTE2BYTE (2, 0, 2, 0):
351 case TAYSTE2BYTE (0, 0, 2, 0):
356 if (BYTE2TAYSTE (i, LNS_LEFT) > 1
357 || BYTE2TAYSTE (i, LNS_TOP) > 1
358 || BYTE2TAYSTE (i, LNS_RIGHT) > 1
359 || BYTE2TAYSTE (i, LNS_BOTTOM) > 1)
365 ls_create (&x->box[i], c);
372 this->cp_x = this->cp_y = 0;
373 this->font_height = this->vert;
374 this->prop_em_width = this->horiz;
375 this->fixed_width = this->horiz;
377 this->horiz_line_width[0] = 0;
378 this->vert_line_width[0] = 0;
380 for (i = 1; i < OUTP_L_COUNT; i++)
382 this->horiz_line_width[i] = this->vert;
383 this->vert_line_width[i] = this->horiz;
386 for (i = 0; i < (1 << OUTP_L_COUNT); i++)
388 this->horiz_line_spacing[i] = (i & ~1) ? this->vert : 0;
389 this->vert_line_spacing[i] = (i & ~1) ? this->horiz : 0;
393 this->driver_open = 1;
394 msg (VM (2), _("%s: Initialization complete."), this->name);
400 ascii_close_driver (struct outp_driver *this)
402 struct ascii_driver_ext *x = this->ext;
405 assert (this->driver_open == 1);
406 msg (VM (2), _("%s: Beginning closing..."), this->name);
409 for (i = 0; i < OPS_COUNT; i++)
410 ls_destroy (&x->ops[i]);
411 for (i = 0; i < LNS_COUNT; i++)
412 ls_destroy (&x->box[i]);
413 for (i = 0; i < FSTY_COUNT; i++)
414 ls_destroy (&x->fonts[i]);
415 if (x->lines != NULL)
419 for (line = 0; line < x->lines_cap; line++)
420 free (x->lines[line].chars);
423 fn_close_ext (&x->file);
424 free (x->file.filename);
427 this->driver_open = 0;
428 msg (VM (3), _("%s: Finished closing."), this->name);
433 /* Generic option types. */
443 static struct outp_option option_tab[] =
445 {"headers", boolean_arg, 0},
446 {"output-file", 1, 0},
448 {"length", pos_int_arg, 0},
449 {"width", pos_int_arg, 1},
450 {"lpi", pos_int_arg, 2},
451 {"cpi", pos_int_arg, 3},
452 {"init", string_arg, 0},
453 {"done", string_arg, 1},
454 {"left-margin", nonneg_int_arg, 0},
455 {"right-margin", nonneg_int_arg, 1},
456 {"top-margin", nonneg_int_arg, 2},
457 {"bottom-margin", nonneg_int_arg, 3},
458 {"paginate", boolean_arg, 1},
459 {"form-feed-string", string_arg, 2},
460 {"newline-string", string_arg, 3},
461 {"italic-on", font_string_arg, 0},
462 {"italic-off", font_string_arg, 1},
463 {"bold-on", font_string_arg, 2},
464 {"bold-off", font_string_arg, 3},
465 {"bold-italic-on", font_string_arg, 4},
466 {"bold-italic-off", font_string_arg, 5},
467 {"overstrike-style", 3, 0},
468 {"tab-width", nonneg_int_arg, 4},
469 {"carriage-return-style", 4, 0},
470 {"squeeze", boolean_arg, 2},
475 ascii_option (struct outp_driver *this, const char *key,
476 const struct string *val)
478 struct ascii_driver_ext *x = this->ext;
482 value = ds_c_str (val);
483 if (!strncmp (key, "box[", 4))
486 int indx = strtol (&key[4], &tail, 4);
487 if (*tail != ']' || indx < 0 || indx > LNS_COUNT)
489 msg (SE, _("Bad index value for `box' key: syntax is box[INDEX], "
490 "0 <= INDEX < %d decimal, with INDEX expressed in base 4."),
494 if (!ls_null_p (&x->box[indx]))
495 msg (SW, _("Duplicate value for key `%s'."), key);
496 ls_create (&x->box[indx], value);
500 cat = outp_match_keyword (key, option_tab, option_info, &subcat);
504 msg (SE, _("Unknown configuration parameter `%s' for ascii device driver."),
508 free (x->file.filename);
509 x->file.filename = xstrdup (value);
512 if (!strcmp (value, "ascii"))
513 x->char_set = CHS_ASCII;
514 else if (!strcmp (value, "latin1"))
515 x->char_set = CHS_LATIN1;
517 msg (SE, _("Unknown character set `%s'. Valid character sets are "
518 "`ascii' and `latin1'."), value);
521 if (!strcmp (value, "single"))
522 x->overstrike_style = OVS_SINGLE;
523 else if (!strcmp (value, "line"))
524 x->overstrike_style = OVS_LINE;
526 msg (SE, _("Unknown overstrike style `%s'. Valid overstrike styles "
527 "are `single' and `line'."), value);
530 if (!strcmp (value, "bs"))
531 x->carriage_return_style = CRS_BS;
532 else if (!strcmp (value, "cr"))
533 x->carriage_return_style = CRS_CR;
535 msg (SE, _("Unknown carriage return style `%s'. Valid carriage "
536 "return styles are `cr' and `bs'."), value);
544 arg = strtol (value, &tail, 0);
545 if (arg < 1 || errno == ERANGE || *tail)
547 msg (SE, _("Positive integer required as value for `%s'."), key);
553 x->page_length = arg;
575 arg = strtol (value, &tail, 0);
576 if (arg < 0 || errno == ERANGE || *tail)
578 msg (SE, _("Zero or positive integer required as value for `%s'."),
585 x->left_margin = arg;
588 x->right_margin = arg;
594 x->bottom_margin = arg;
606 struct fixed_string *s;
610 s = &x->ops[OPS_INIT];
613 s = &x->ops[OPS_DONE];
616 s = &x->ops[OPS_FORMFEED];
619 s = &x->ops[OPS_NEWLINE];
625 ls_create (s, value);
628 case font_string_arg:
630 if (!strcmp (value, "overstrike"))
632 ls_destroy (&x->fonts[subcat]);
635 ls_create (&x->fonts[subcat], value);
641 if (!strcmp (value, "on") || !strcmp (value, "true")
642 || !strcmp (value, "yes") || atoi (value))
644 else if (!strcmp (value, "off") || !strcmp (value, "false")
645 || !strcmp (value, "no") || !strcmp (value, "0"))
649 msg (SE, _("Boolean value expected for %s."), key);
655 x->headers = setting;
658 x->paginate = setting;
661 x->squeeze_blank_lines = setting;
674 postopen (struct file_ext *f)
676 struct ascii_driver_ext *x = f->param;
677 struct fixed_string *s = &x->ops[OPS_INIT];
679 if (!ls_empty_p (s) && fwrite (ls_c_str (s), ls_length (s), 1, f->file) < 1)
681 msg (ME, _("ASCII output driver: %s: %s"),
682 f->filename, strerror (errno));
689 preclose (struct file_ext *f)
691 struct ascii_driver_ext *x = f->param;
692 struct fixed_string *d = &x->ops[OPS_DONE];
694 if (!ls_empty_p (d) && fwrite (ls_c_str (d), ls_length (d), 1, f->file) < 1)
696 msg (ME, _("ASCII output driver: %s: %s"),
697 f->filename, strerror (errno));
704 ascii_open_page (struct outp_driver *this)
706 struct ascii_driver_ext *x = this->ext;
709 assert (this->driver_open && !this->page_open);
711 if (!fn_open_ext (&x->file))
713 msg (ME, _("ASCII output driver: %s: %s"), x->file.filename,
718 if (x->l > x->lines_cap)
720 x->lines = xnrealloc (x->lines, x->l, sizeof *x->lines);
721 for (i = x->lines_cap; i < x->l; i++)
723 struct line *line = &x->lines[i];
730 for (i = 0; i < x->l; i++)
731 x->lines[i].char_cnt = 0;
737 /* Ensures that at least the first L characters of line I in the
738 driver identified by struct ascii_driver_ext *X have been cleared out. */
740 expand_line (struct ascii_driver_ext *x, int i, int l)
745 assert (i < x->lines_cap);
747 if (l > line->char_cap)
749 line->char_cap = l * 2;
750 line->chars = xnrealloc (line->chars,
751 line->char_cap, sizeof *line->chars);
753 for (j = line->char_cnt; j < l; j++)
754 line->chars[j] = ' ';
758 /* Puts line L at (H,K) in the current output page. Assumes
759 struct ascii_driver_ext named `ext'. */
760 #define draw_line(H, K, L) \
761 ext->lines[K].chars[H] = (L) | 0x800
763 /* Line styles for each position. */
764 #define T(STYLE) (STYLE<<LNS_TOP)
765 #define L(STYLE) (STYLE<<LNS_LEFT)
766 #define B(STYLE) (STYLE<<LNS_BOTTOM)
767 #define R(STYLE) (STYLE<<LNS_RIGHT)
770 ascii_line_horz (struct outp_driver *this, const struct rect *r,
771 const struct color *c UNUSED, int style)
773 struct ascii_driver_ext *ext = this->ext;
774 int x1 = r->x1 / this->horiz;
775 int x2 = r->x2 / this->horiz;
776 int y1 = r->y1 / this->vert;
779 assert (this->driver_open && this->page_open);
784 || x1 < 0 || x1 >= ext->w
785 || x2 <= 0 || x2 > ext->w
786 || y1 < 0 || y1 >= ext->l)
788 #if !SUPPRESS_WARNINGS
789 printf (_("ascii_line_horz: bad hline (%d,%d),%d out of (%d,%d)\n"),
790 x1, x2, y1, ext->w, ext->l);
796 if (ext->lines[y1].char_cnt < x2)
797 expand_line (ext, y1, x2);
799 for (x = x1; x < x2; x++)
800 draw_line (x, y1, (style << LNS_LEFT) | (style << LNS_RIGHT));
804 ascii_line_vert (struct outp_driver *this, const struct rect *r,
805 const struct color *c UNUSED, int style)
807 struct ascii_driver_ext *ext = this->ext;
808 int x1 = r->x1 / this->horiz;
809 int y1 = r->y1 / this->vert;
810 int y2 = r->y2 / this->vert;
813 assert (this->driver_open && this->page_open);
818 || x1 < 0 || x1 >= ext->w
819 || y1 < 0 || y1 >= ext->l
820 || y2 < 0 || y2 > ext->l)
822 #if !SUPPRESS_WARNINGS
823 printf (_("ascii_line_vert: bad vline %d,(%d,%d) out of (%d,%d)\n"),
824 x1, y1, y2, ext->w, ext->l);
830 for (y = y1; y < y2; y++)
831 if (ext->lines[y].char_cnt <= x1)
832 expand_line (ext, y, x1 + 1);
834 for (y = y1; y < y2; y++)
835 draw_line (x1, y, (style << LNS_TOP) | (style << LNS_BOTTOM));
839 ascii_line_intersection (struct outp_driver *this, const struct rect *r,
840 const struct color *c UNUSED,
841 const struct outp_styles *style)
843 struct ascii_driver_ext *ext = this->ext;
844 int x = r->x1 / this->horiz;
845 int y = r->y1 / this->vert;
848 assert (this->driver_open && this->page_open);
850 if (x < 0 || x >= ext->w || y < 0 || y >= ext->l)
852 #if !SUPPRESS_WARNINGS
853 printf (_("ascii_line_intersection: bad intsct (%d,%d) out of (%d,%d)\n"),
854 x, y, ext->w, ext->l);
860 l = ((style->l << LNS_LEFT) | (style->r << LNS_RIGHT)
861 | (style->t << LNS_TOP) | (style->b << LNS_BOTTOM));
863 if (ext->lines[y].char_cnt <= x)
864 expand_line (ext, y, x + 1);
868 /* FIXME: Later we could set this up so that for certain devices it
871 ascii_box (struct outp_driver *this UNUSED, const struct rect *r UNUSED,
872 const struct color *bord UNUSED, const struct color *fill UNUSED)
874 assert (this->driver_open && this->page_open);
877 /* Polylines not supported. */
879 ascii_polyline_begin (struct outp_driver *this UNUSED, const struct color *c UNUSED)
881 assert (this->driver_open && this->page_open);
884 ascii_polyline_point (struct outp_driver *this UNUSED, int x UNUSED, int y UNUSED)
886 assert (this->driver_open && this->page_open);
889 ascii_polyline_end (struct outp_driver *this UNUSED)
891 assert (this->driver_open && this->page_open);
895 ascii_text_set_font_by_name (struct outp_driver * this, const char *s)
897 struct ascii_driver_ext *x = this->ext;
898 int len = strlen (s);
900 assert (this->driver_open && this->page_open);
901 x->cur_font = OUTP_F_R;
904 if (s[len - 1] == 'I')
906 if (len > 1 && s[len - 2] == 'B')
907 x->cur_font = OUTP_F_BI;
909 x->cur_font = OUTP_F_I;
911 else if (s[len - 1] == 'B')
912 x->cur_font = OUTP_F_B;
916 ascii_text_set_font_by_position (struct outp_driver *this, int pos)
918 struct ascii_driver_ext *x = this->ext;
919 assert (this->driver_open && this->page_open);
920 x->cur_font = pos >= 0 && pos < 4 ? pos : 0;
924 ascii_text_set_font_by_family (struct outp_driver *this UNUSED, const char *s UNUSED)
926 assert (this->driver_open && this->page_open);
930 ascii_text_get_font_name (struct outp_driver *this)
932 struct ascii_driver_ext *x = this->ext;
934 assert (this->driver_open && this->page_open);
952 ascii_text_get_font_family (struct outp_driver *this UNUSED)
954 assert (this->driver_open && this->page_open);
959 ascii_text_set_size (struct outp_driver *this, int size)
961 assert (this->driver_open && this->page_open);
962 return size == this->vert;
966 ascii_text_get_size (struct outp_driver *this, int *em_width)
968 assert (this->driver_open && this->page_open);
970 *em_width = this->horiz;
974 static void text_draw (struct outp_driver *this, struct outp_text *t);
976 /* Divides the text T->S into lines of width T->H. Sets T->V to the
977 number of lines necessary. Actually draws the text if DRAW is
980 You probably don't want to look at this code. */
982 delineate (struct outp_driver *this, struct outp_text *t, int draw)
984 /* Width we're fitting everything into. */
985 int width = t->h / this->horiz;
987 /* Maximum `y' position we can write to. */
990 /* Current position in string, character following end of string. */
991 const char *s = ls_c_str (&t->s);
992 const char *end = ls_end (&t->s);
994 /* Temporary struct outp_text to pass to low-level function. */
995 struct outp_text temp;
997 #if GLOBAL_DEBUGGING && 0
1001 printf (_("%s: horiz=%d, vert=%d\n"), this->name, this->horiz, this->vert);
1013 temp.options = t->options;
1014 ls_shallow_copy (&temp.s, &t->s);
1015 temp.h = t->h / this->horiz;
1016 temp.x = t->x / this->horiz;
1020 temp.y = t->y / this->vert;
1022 if (t->options & OUTP_T_VERT)
1023 max_y = (t->v / this->vert) + temp.y - 1;
1027 while (end - s > width)
1029 const char *beg = s;
1032 /* Find first space before &s[width]. */
1038 if (!isspace ((unsigned char) space[-1]))
1047 s = space = &s[width];
1054 ls_init (&temp.s, beg, space - beg);
1055 temp.w = space - beg;
1056 text_draw (this, &temp);
1058 if (++temp.y > max_y)
1061 /* Find first nonspace after space. */
1062 while (s < end && isspace ((unsigned char) *s))
1069 ls_init (&temp.s, s, end - s);
1071 text_draw (this, &temp);
1076 t->v = (temp.y * this->vert) - t->y;
1080 ascii_text_metrics (struct outp_driver *this, struct outp_text *t)
1082 assert (this->driver_open && this->page_open);
1083 if (!(t->options & OUTP_T_HORZ))
1086 t->h = ls_length (&t->s) * this->horiz;
1089 delineate (this, t, 0);
1093 ascii_text_draw (struct outp_driver *this, struct outp_text *t)
1095 /* FIXME: orientations not supported. */
1096 assert (this->driver_open && this->page_open);
1097 if (!(t->options & OUTP_T_HORZ))
1099 struct outp_text temp;
1101 temp.options = t->options;
1103 temp.h = temp.v = 0;
1104 temp.x = t->x / this->horiz;
1105 temp.y = t->y / this->vert;
1106 text_draw (this, &temp);
1107 ascii_text_metrics (this, t);
1111 delineate (this, t, 1);
1115 text_draw (struct outp_driver *this, struct outp_text *t)
1117 struct ascii_driver_ext *ext = this->ext;
1118 unsigned attr = ext->cur_font << 8;
1123 char *s = ls_c_str (&t->s);
1125 /* Expand the line with the assumption that S takes up LEN character
1126 spaces (sometimes it takes up less). */
1129 assert (this->driver_open && this->page_open);
1130 switch (t->options & OUTP_T_JUST_MASK)
1132 case OUTP_T_JUST_LEFT:
1134 case OUTP_T_JUST_CENTER:
1135 x -= (t->h - t->w) / 2; /* fall through */
1136 case OUTP_T_JUST_RIGHT:
1143 if (!(t->y < ext->l && x < ext->w))
1145 min_len = min (x + ls_length (&t->s), ext->w);
1146 if (ext->lines[t->y].char_cnt < min_len)
1147 expand_line (ext, t->y, min_len);
1150 int len = ls_length (&t->s);
1152 if (len + x > ext->w)
1155 ext->lines[y].chars[x++] = *s++ | attr;
1159 /* ascii_close_page () and support routines. */
1161 #define LINE_BUF_SIZE 1024
1162 static char *line_buf;
1163 static char *line_p;
1166 commit_line_buf (struct outp_driver *this)
1168 struct ascii_driver_ext *x = this->ext;
1170 if ((int) fwrite (line_buf, 1, line_p - line_buf, x->file.file)
1171 < line_p - line_buf)
1173 msg (ME, _("Writing `%s': %s"), x->file.filename, strerror (errno));
1181 /* Writes everything from BP to EP exclusive into line_buf, or to
1182 THIS->output if line_buf overflows. */
1184 output_string (struct outp_driver *this, const char *bp, const char *ep)
1186 if (LINE_BUF_SIZE - (line_p - line_buf) >= ep - bp)
1188 memcpy (line_p, bp, ep - bp);
1194 if (LINE_BUF_SIZE - (line_p - line_buf) <= 1 && !commit_line_buf (this))
1200 /* Writes everything from BP to EP exclusive into line_buf, or to
1201 THIS->output if line_buf overflows. Returns 1 if additional passes
1202 over the line are required. FIXME: probably could do a lot of
1203 optimization here. */
1205 output_shorts (struct outp_driver *this,
1206 const unsigned short *bp, const unsigned short *ep)
1208 struct ascii_driver_ext *ext = this->ext;
1209 size_t remaining = LINE_BUF_SIZE - (line_p - line_buf);
1212 for (; bp < ep; bp++)
1216 struct fixed_string *box = &ext->box[*bp & 0xff];
1217 size_t len = ls_length (box);
1219 if (remaining >= len)
1221 memcpy (line_p, ls_c_str (box), len);
1227 if (!commit_line_buf (this))
1229 output_string (this, ls_c_str (box), ls_end (box));
1230 remaining = LINE_BUF_SIZE - (line_p - line_buf);
1233 else if (*bp & 0x0300)
1235 struct fixed_string *on;
1239 switch (*bp & 0x0300)
1242 on = &ext->fonts[FSTY_ON | FSTY_ITALIC];
1245 on = &ext->fonts[FSTY_ON | FSTY_BOLD];
1247 case OUTP_F_BI << 8:
1248 on = &ext->fonts[FSTY_ON | FSTY_BOLD_ITALIC];
1256 if (ext->overstrike_style == OVS_SINGLE)
1257 switch (*bp & 0x0300)
1271 case OUTP_F_BI << 8:
1294 output_string (this, buf, &buf[len]);
1303 if (!commit_line_buf (this))
1305 remaining = LINE_BUF_SIZE - (line_p - line_buf);
1313 /* Writes CH into line_buf N times, or to THIS->output if line_buf
1316 output_char (struct outp_driver *this, int n, char ch)
1318 if (LINE_BUF_SIZE - (line_p - line_buf) >= n)
1320 memset (line_p, ch, n);
1326 if (LINE_BUF_SIZE - (line_p - line_buf) <= 1 && !commit_line_buf (this))
1332 /* Advance the carriage from column 0 to the left margin. */
1334 advance_to_left_margin (struct outp_driver *this)
1336 struct ascii_driver_ext *ext = this->ext;
1339 margin = ext->left_margin;
1342 if (ext->tab_width && margin >= ext->tab_width)
1344 output_char (this, margin / ext->tab_width, '\t');
1345 margin %= ext->tab_width;
1348 output_char (this, margin, ' ');
1351 /* Move the output file carriage N_CHARS left, to the left margin. */
1353 return_carriage (struct outp_driver *this, int n_chars)
1355 struct ascii_driver_ext *ext = this->ext;
1357 switch (ext->carriage_return_style)
1360 output_char (this, n_chars, '\b');
1363 output_char (this, 1, '\r');
1364 advance_to_left_margin (this);
1372 /* Writes COUNT lines from the line buffer in THIS, starting at line
1375 output_lines (struct outp_driver *this, int first, int count)
1377 struct ascii_driver_ext *ext = this->ext;
1380 struct fixed_string *newline = &ext->ops[OPS_NEWLINE];
1385 if (NULL == ext->file.file)
1388 /* Iterate over all the lines to be output. */
1389 for (line_num = first; line_num < first + count; line_num++)
1391 struct line *line = &ext->lines[line_num];
1392 unsigned short *p = line->chars;
1393 unsigned short *end_p = p + line->char_cnt;
1394 unsigned short *bp, *ep;
1395 unsigned short attr = 0;
1397 assert (end_p >= p);
1399 /* Squeeze multiple blank lines into a single blank line if
1401 if (ext->squeeze_blank_lines
1403 && ext->lines[line_num].char_cnt == 0
1404 && ext->lines[line_num - 1].char_cnt == 0)
1407 /* Output every character in the line in the appropriate
1412 advance_to_left_margin (this);
1415 while (ep < end_p && attr == (*ep & 0x0300))
1417 if (output_shorts (this, bp, ep))
1425 /* Turn off old font. */
1426 if (attr != (OUTP_F_R << 8))
1428 struct fixed_string *off;
1433 off = &ext->fonts[FSTY_OFF | FSTY_ITALIC];
1436 off = &ext->fonts[FSTY_OFF | FSTY_BOLD];
1438 case OUTP_F_BI << 8:
1439 off = &ext->fonts[FSTY_OFF | FSTY_BOLD_ITALIC];
1446 output_string (this, ls_c_str (off), ls_end (off));
1449 /* Turn on new font. */
1450 attr = (*bp & 0x0300);
1451 if (attr != (OUTP_F_R << 8))
1453 struct fixed_string *on;
1458 on = &ext->fonts[FSTY_ON | FSTY_ITALIC];
1461 on = &ext->fonts[FSTY_ON | FSTY_BOLD];
1463 case OUTP_F_BI << 8:
1464 on = &ext->fonts[FSTY_ON | FSTY_BOLD_ITALIC];
1471 output_string (this, ls_c_str (on), ls_end (on));
1480 return_carriage (this, n_chars);
1485 while (ep < end_p && (*ep & 0x0300) == (OUTP_F_R << 8))
1489 output_char (this, ep - bp, ' ');
1491 switch (*ep & 0x0300)
1499 case OUTP_F_BI << 8:
1507 output_char (this, 1, ch);
1508 n_chars += ep - bp + 1;
1515 return_carriage (this, n_chars);
1519 while (ep < end_p && (*ep & 0x0300) != (OUTP_F_BI << 8))
1523 output_char (this, ep - bp, ' ');
1524 output_char (this, 1, '_');
1530 output_string (this, ls_c_str (newline), ls_end (newline));
1536 ascii_close_page (struct outp_driver *this)
1540 struct ascii_driver_ext *x = this->ext;
1541 int nl_len, ff_len, total_len;
1545 assert (this->driver_open && this->page_open);
1548 line_buf = xmalloc (LINE_BUF_SIZE);
1551 nl_len = ls_length (&x->ops[OPS_NEWLINE]);
1554 total_len = x->top_margin * nl_len;
1555 if (s_len < total_len)
1558 s = xrealloc (s, s_len);
1560 for (cp = s, i = 0; i < x->top_margin; i++)
1562 memcpy (cp, ls_c_str (&x->ops[OPS_NEWLINE]), nl_len);
1565 output_string (this, s, &s[total_len]);
1571 total_len = nl_len + x->w;
1572 if (s_len < total_len + 1)
1574 s_len = total_len + 1;
1575 s = xrealloc (s, s_len);
1578 memset (s, ' ', x->w);
1583 snprintf (temp, 80, _("%s - Page %d"), get_start_date (),
1585 memcpy (&s[x->w - strlen (temp)], temp, strlen (temp));
1588 if (outp_title && outp_subtitle)
1590 len = min ((int) strlen (outp_title), x->w);
1591 memcpy (s, outp_title, len);
1593 memcpy (&s[x->w], ls_c_str (&x->ops[OPS_NEWLINE]), nl_len);
1594 output_string (this, s, &s[total_len]);
1596 memset (s, ' ', x->w);
1597 len = strlen (version) + 3 + strlen (host_system);
1599 sprintf (&s[x->w - len], "%s - %s" , version, host_system);
1600 if (outp_subtitle || outp_title)
1602 char *string = outp_subtitle ? outp_subtitle : outp_title;
1603 len = min ((int) strlen (string), x->w);
1604 memcpy (s, string, len);
1606 memcpy (&s[x->w], ls_c_str (&x->ops[OPS_NEWLINE]), nl_len);
1607 output_string (this, s, &s[total_len]);
1608 output_string (this, &s[x->w], &s[total_len]);
1610 if (line_p != line_buf && !commit_line_buf (this))
1613 output_lines (this, 0, x->l);
1615 ff_len = ls_length (&x->ops[OPS_FORMFEED]);
1616 total_len = x->bottom_margin * nl_len + ff_len;
1617 if (s_len < total_len)
1618 s = xrealloc (s, total_len);
1619 for (cp = s, i = 0; i < x->bottom_margin; i++)
1621 memcpy (cp, ls_c_str (&x->ops[OPS_NEWLINE]), nl_len);
1624 memcpy (cp, ls_c_str (&x->ops[OPS_FORMFEED]), ff_len);
1626 output_string (this, s, &s[total_len]);
1628 if (line_p != line_buf && !commit_line_buf (this))
1631 this->page_open = 0;
1638 ascii_chart_initialise(struct outp_driver *d UNUSED, struct chart *ch )
1640 msg(MW, _("Charts are unsupported with ascii drivers."));
1645 ascii_chart_finalise(struct outp_driver *d UNUSED, struct chart *ch UNUSED)
1650 struct outp_class ascii_class =
1660 ascii_preopen_driver,
1662 ascii_postopen_driver,
1672 ascii_line_intersection,
1675 ascii_polyline_begin,
1676 ascii_polyline_point,
1679 ascii_text_set_font_by_name,
1680 ascii_text_set_font_by_position,
1681 ascii_text_set_font_by_family,
1682 ascii_text_get_font_name,
1683 ascii_text_get_font_family,
1684 ascii_text_set_size,
1685 ascii_text_get_size,
1689 ascii_chart_initialise,
1690 ascii_chart_finalise