1 /* PSPP - computes sample statistics.
2 Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
3 Written by Ben Pfaff <blp@gnu.org>.
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 This program is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
35 /* ASCII driver options: (defaults listed first)
37 output-file="pspp.list"
39 form-feed-string="\f" Written as a formfeed.
40 newline-string=default|"\r\n"|"\n"
42 paginate=on|off Formfeeds are desired?
43 tab-width=8 Width of a tab; 0 to not use tabs.
44 init="" Written at beginning of output.
45 done="" Written at end of output.
47 headers=on|off Put headers at top of page?
50 lpi=6 Only used to determine font size.
52 squeeze=off|on Squeeze multiple newlines into exactly one.
59 box[x]="strng" Sets box character X (X in base 4: 0-3333).
60 italic-on=overstrike|"strng" Turns on italic (underline).
61 italic-off=""|"strng" Turns off italic; ignored for overstrike.
62 bold-on=overstrike|"strng" Turns on bold.
63 bold-off=""|"strng" Turns off bold; ignored for overstrike.
64 bold-italic-on=overstrike|"strng" Turns on bold-italic.
65 bold-italic-off=""|"strng" Turns off bold-italic; ignored for overstrike.
66 overstrike-style=single|line Can we print a whole line then BS over it, or
67 must we go char by char, as on a terminal?
68 carriage-return-style=bs|cr Must we return the carriage with a sequence of
69 BSes, or will a single CR do it?
72 /* Disable messages by failed range checks. */
73 /*#define SUPPRESS_WARNINGS 1 */
78 CHS_ASCII, /* 7-bit ASCII */
79 CHS_LATIN1 /* Latin 1; not really supported at the moment */
82 /* Overstrike style. */
85 OVS_SINGLE, /* Overstrike each character: "a\b_b\b_c\b_" */
86 OVS_LINE /* Overstrike lines: "abc\b\b\b___" (or if
87 newline is "\r\n", then "abc\r___"). Easier
88 on the printer, doesn't work on a tty. */
91 /* Basic output strings. */
94 OPS_INIT, /* Document initialization string. */
95 OPS_DONE, /* Document uninit string. */
96 OPS_FORMFEED, /* Formfeed string. */
97 OPS_NEWLINE, /* Newline string. */
99 OPS_COUNT /* Number of output strings. */
102 /* Line styles bit shifts. */
113 /* Carriage return style. */
116 CRS_BS, /* Multiple backspaces. */
117 CRS_CR /* Single carriage return. */
120 /* Assembles a byte from four taystes. */
121 #define TAYSTE2BYTE(T, L, B, R) \
123 | ((L) << LNS_LEFT) \
124 | ((B) << LNS_BOTTOM) \
125 | ((R) << LNS_RIGHT))
127 /* Extract tayste with shift value S from byte B. */
128 #define BYTE2TAYSTE(B, S) \
131 /* Font style; take one of the first group |'d with one of the second group. */
134 FSTY_ON = 000, /* Turn font on. */
135 FSTY_OFF = 001, /* Turn font off. */
137 FSTY_ITALIC = 0, /* Italic font. */
138 FSTY_BOLD = 2, /* Bold font. */
139 FSTY_BOLD_ITALIC = 4, /* Bold-italic font. */
141 FSTY_COUNT = 6 /* Number of font styles. */
144 /* ASCII output driver extension record. */
145 struct ascii_driver_ext
147 /* User parameters. */
148 int char_set; /* CHS_ASCII/CHS_LATIN1; no-op right now. */
149 int headers; /* 1=print headers at top of page. */
150 int page_length; /* Page length in lines. */
151 int page_width; /* Page width in characters. */
152 int lpi; /* Lines per inch. */
153 int cpi; /* Characters per inch. */
154 int left_margin; /* Left margin in characters. */
155 int right_margin; /* Right margin in characters. */
156 int top_margin; /* Top margin in lines. */
157 int bottom_margin; /* Bottom margin in lines. */
158 int paginate; /* 1=insert formfeeds. */
159 int tab_width; /* Width of a tab; 0 not to use tabs. */
160 struct len_string ops[OPS_COUNT]; /* Basic output strings. */
161 struct len_string box[LNS_COUNT]; /* Line & box drawing characters. */
162 struct len_string fonts[FSTY_COUNT]; /* Font styles; NULL=overstrike. */
163 int overstrike_style; /* OVS_SINGLE or OVS_LINE. */
164 int carriage_return_style; /* Carriage return style. */
165 int squeeze_blank_lines; /* 1=squeeze multiple blank lines into one. */
167 /* Internal state. */
168 struct file_ext file; /* Output file. */
169 int page_number; /* Current page number. */
170 unsigned short *page; /* Page content. */
171 int page_size; /* Number of bytes allocated for page, attr. */
172 int *line_len; /* Length of each line in page, attr. */
173 int line_len_size; /* Number of ints allocated for line_len. */
174 int w, l; /* Actual width & length w/o margins, etc. */
175 int n_output; /* Number of lines output so far. */
176 int cur_font; /* Current font by OUTP_F_*. */
178 int debug; /* Set by som_text_draw(). */
182 static struct pool *ascii_pool;
184 static int postopen (struct file_ext *);
185 static int preclose (struct file_ext *);
188 ascii_open_global (struct outp_class *this UNUSED)
190 ascii_pool = pool_create ();
195 ascii_close_global (struct outp_class *this UNUSED)
197 pool_destroy (ascii_pool);
202 ascii_font_sizes (struct outp_class *this UNUSED, int *n_valid_sizes)
204 static int valid_sizes[] = {12, 12, 0, 0};
206 assert (n_valid_sizes);
212 ascii_preopen_driver (struct outp_driver *this)
214 struct ascii_driver_ext *x;
217 assert (this->driver_open == 0);
218 msg (VM (1), _("ASCII driver initializing as `%s'..."), this->name);
219 this->ext = x = xmalloc (sizeof (struct ascii_driver_ext));
220 x->char_set = CHS_ASCII;
229 x->bottom_margin = 2;
232 for (i = 0; i < OPS_COUNT; i++)
233 ls_null (&x->ops[i]);
234 for (i = 0; i < LNS_COUNT; i++)
235 ls_null (&x->box[i]);
236 for (i = 0; i < FSTY_COUNT; i++)
237 ls_null (&x->fonts[i]);
238 x->overstrike_style = OVS_SINGLE;
239 x->carriage_return_style = CRS_BS;
240 x->squeeze_blank_lines = 0;
241 x->file.filename = NULL;
244 x->file.sequence_no = &x->page_number;
246 x->file.postopen = postopen;
247 x->file.preclose = preclose;
252 x->line_len_size = 0;
254 x->cur_font = OUTP_F_R;
262 ascii_postopen_driver (struct outp_driver *this)
264 struct ascii_driver_ext *x = this->ext;
266 assert (this->driver_open == 0);
268 if (NULL == x->file.filename)
269 x->file.filename = xstrdup ("pspp.list");
271 x->w = x->page_width - x->left_margin - x->right_margin;
272 x->l = (x->page_length - (x->headers ? 3 : 0) - x->top_margin
273 - x->bottom_margin - 1);
274 if (x->w < 59 || x->l < 15)
276 msg (SE, _("ascii driver: Area of page excluding margins and headers "
277 "must be at least 59 characters wide by 15 lines long. Page as "
278 "configured is only %d characters by %d lines."), x->w, x->l);
282 this->res = x->lpi * x->cpi;
283 this->horiz = x->lpi;
285 this->width = x->w * this->horiz;
286 this->length = x->l * this->vert;
288 if (ls_null_p (&x->ops[OPS_FORMFEED]))
289 ls_create (ascii_pool, &x->ops[OPS_FORMFEED], "\f");
290 if (ls_null_p (&x->ops[OPS_NEWLINE])
291 || !strcmp (ls_value (&x->ops[OPS_NEWLINE]), "default"))
293 ls_create (ascii_pool, &x->ops[OPS_NEWLINE], "\n");
300 for (i = 0; i < LNS_COUNT; i++)
304 if (!ls_null_p (&x->box[i]))
308 case TAYSTE2BYTE (0, 0, 0, 0):
312 case TAYSTE2BYTE (0, 1, 0, 0):
313 case TAYSTE2BYTE (0, 1, 0, 1):
314 case TAYSTE2BYTE (0, 0, 0, 1):
318 case TAYSTE2BYTE (1, 0, 0, 0):
319 case TAYSTE2BYTE (1, 0, 1, 0):
320 case TAYSTE2BYTE (0, 0, 1, 0):
324 case TAYSTE2BYTE (0, 3, 0, 0):
325 case TAYSTE2BYTE (0, 3, 0, 3):
326 case TAYSTE2BYTE (0, 0, 0, 3):
327 case TAYSTE2BYTE (0, 2, 0, 0):
328 case TAYSTE2BYTE (0, 2, 0, 2):
329 case TAYSTE2BYTE (0, 0, 0, 2):
333 case TAYSTE2BYTE (3, 0, 0, 0):
334 case TAYSTE2BYTE (3, 0, 3, 0):
335 case TAYSTE2BYTE (0, 0, 3, 0):
336 case TAYSTE2BYTE (2, 0, 0, 0):
337 case TAYSTE2BYTE (2, 0, 2, 0):
338 case TAYSTE2BYTE (0, 0, 2, 0):
343 if (BYTE2TAYSTE (i, LNS_LEFT) > 1
344 || BYTE2TAYSTE (i, LNS_TOP) > 1
345 || BYTE2TAYSTE (i, LNS_RIGHT) > 1
346 || BYTE2TAYSTE (i, LNS_BOTTOM) > 1)
352 ls_create (ascii_pool, &x->box[i], c);
359 this->cp_x = this->cp_y = 0;
360 this->font_height = this->vert;
361 this->prop_em_width = this->horiz;
362 this->fixed_width = this->horiz;
364 this->horiz_line_width[0] = 0;
365 this->vert_line_width[0] = 0;
367 for (i = 1; i < OUTP_L_COUNT; i++)
369 this->horiz_line_width[i] = this->vert;
370 this->vert_line_width[i] = this->horiz;
373 for (i = 0; i < (1 << OUTP_L_COUNT); i++)
375 this->horiz_line_spacing[i] = (i & ~1) ? this->vert : 0;
376 this->vert_line_spacing[i] = (i & ~1) ? this->horiz : 0;
380 this->driver_open = 1;
381 msg (VM (2), _("%s: Initialization complete."), this->name);
387 ascii_close_driver (struct outp_driver *this)
389 struct ascii_driver_ext *x = this->ext;
391 assert (this->driver_open == 1);
392 msg (VM (2), _("%s: Beginning closing..."), this->name);
397 fn_close_ext (&x->file);
398 free (x->file.filename);
401 this->driver_open = 0;
402 msg (VM (3), _("%s: Finished closing."), this->name);
407 /* Generic option types. */
417 static struct outp_option option_tab[] =
419 {"headers", boolean_arg, 0},
420 {"output-file", 1, 0},
422 {"length", pos_int_arg, 0},
423 {"width", pos_int_arg, 1},
424 {"lpi", pos_int_arg, 2},
425 {"cpi", pos_int_arg, 3},
426 {"init", string_arg, 0},
427 {"done", string_arg, 1},
428 {"left-margin", nonneg_int_arg, 0},
429 {"right-margin", nonneg_int_arg, 1},
430 {"top-margin", nonneg_int_arg, 2},
431 {"bottom-margin", nonneg_int_arg, 3},
432 {"paginate", boolean_arg, 1},
433 {"form-feed-string", string_arg, 2},
434 {"newline-string", string_arg, 3},
435 {"italic-on", font_string_arg, 0},
436 {"italic-off", font_string_arg, 1},
437 {"bold-on", font_string_arg, 2},
438 {"bold-off", font_string_arg, 3},
439 {"bold-italic-on", font_string_arg, 4},
440 {"bold-italic-off", font_string_arg, 5},
441 {"overstrike-style", 3, 0},
442 {"tab-width", nonneg_int_arg, 4},
443 {"carriage-return-style", 4, 0},
444 {"squeeze", boolean_arg, 2},
447 static struct outp_option_info option_info;
450 ascii_option (struct outp_driver *this, const char *key,
451 const struct string *val)
453 struct ascii_driver_ext *x = this->ext;
457 value = ds_value (val);
458 if (!strncmp (key, "box[", 4))
461 int indx = strtol (&key[4], &tail, 4);
462 if (*tail != ']' || indx < 0 || indx > LNS_COUNT)
464 msg (SE, _("Bad index value for `box' key: syntax is box[INDEX], "
465 "0 <= INDEX < %d decimal, with INDEX expressed in base 4."),
469 if (!ls_null_p (&x->box[indx]))
470 msg (SW, _("Duplicate value for key `%s'."), key);
471 ls_create (ascii_pool, &x->box[indx], value);
475 cat = outp_match_keyword (key, option_tab, &option_info, &subcat);
479 msg (SE, _("Unknown configuration parameter `%s' for ascii device driver."),
483 free (x->file.filename);
484 x->file.filename = xstrdup (value);
487 if (!strcmp (value, "ascii"))
488 x->char_set = CHS_ASCII;
489 else if (!strcmp (value, "latin1"))
490 x->char_set = CHS_LATIN1;
492 msg (SE, _("Unknown character set `%s'. Valid character sets are "
493 "`ascii' and `latin1'."), value);
496 if (!strcmp (value, "single"))
497 x->overstrike_style = OVS_SINGLE;
498 else if (!strcmp (value, "line"))
499 x->overstrike_style = OVS_LINE;
501 msg (SE, _("Unknown overstrike style `%s'. Valid overstrike styles "
502 "are `single' and `line'."), value);
505 if (!strcmp (value, "bs"))
506 x->carriage_return_style = CRS_BS;
507 else if (!strcmp (value, "cr"))
508 x->carriage_return_style = CRS_CR;
510 msg (SE, _("Unknown carriage return style `%s'. Valid carriage "
511 "return styles are `cr' and `bs'."), value);
519 arg = strtol (value, &tail, 0);
520 if (arg < 1 || errno == ERANGE || *tail)
522 msg (SE, _("Positive integer required as value for `%s'."), key);
528 x->page_length = arg;
550 arg = strtol (value, &tail, 0);
551 if (arg < 0 || errno == ERANGE || *tail)
553 msg (SE, _("Zero or positive integer required as value for `%s'."),
560 x->left_margin = arg;
563 x->right_margin = arg;
569 x->bottom_margin = arg;
581 struct len_string *s;
585 s = &x->ops[OPS_INIT];
588 s = &x->ops[OPS_DONE];
591 s = &x->ops[OPS_FORMFEED];
594 s = &x->ops[OPS_NEWLINE];
599 ls_create (ascii_pool, s, value);
602 case font_string_arg:
604 if (!strcmp (value, "overstrike"))
606 ls_destroy (ascii_pool, &x->fonts[subcat]);
609 ls_create (ascii_pool, &x->fonts[subcat], value);
615 if (!strcmp (value, "on") || !strcmp (value, "true")
616 || !strcmp (value, "yes") || atoi (value))
618 else if (!strcmp (value, "off") || !strcmp (value, "false")
619 || !strcmp (value, "no") || !strcmp (value, "0"))
623 msg (SE, _("Boolean value expected for %s."), key);
629 x->headers = setting;
632 x->paginate = setting;
635 x->squeeze_blank_lines = setting;
648 postopen (struct file_ext *f)
650 struct ascii_driver_ext *x = f->param;
651 struct len_string *s = &x->ops[OPS_INIT];
653 if (!ls_empty_p (s) && fwrite (ls_value (s), ls_length (s), 1, f->file) < 1)
655 msg (ME, _("ASCII output driver: %s: %s"),
656 f->filename, strerror (errno));
663 preclose (struct file_ext *f)
665 struct ascii_driver_ext *x = f->param;
666 struct len_string *d = &x->ops[OPS_DONE];
668 if (!ls_empty_p (d) && fwrite (ls_value (d), ls_length (d), 1, f->file) < 1)
670 msg (ME, _("ASCII output driver: %s: %s"),
671 f->filename, strerror (errno));
678 ascii_open_page (struct outp_driver *this)
680 struct ascii_driver_ext *x = this->ext;
683 assert (this->driver_open && !this->page_open);
685 if (!fn_open_ext (&x->file))
687 msg (ME, _("ASCII output driver: %s: %s"), x->file.filename,
692 req_page_size = x->w * x->l;
693 if (req_page_size > x->page_size || req_page_size / 2 < x->page_size)
695 x->page_size = req_page_size;
696 x->page = xrealloc (x->page, sizeof *x->page * req_page_size);
699 if (x->l > x->line_len_size)
701 x->line_len_size = x->l;
702 x->line_len = xrealloc (x->line_len,
703 sizeof *x->line_len * x->line_len_size);
706 memset (x->line_len, 0, sizeof *x->line_len * x->l);
712 /* Ensures that at least the first L characters of line I in the
713 driver identified by struct ascii_driver_ext *X have been cleared out. */
715 expand_line (struct ascii_driver_ext *x, int i, int l)
717 int limit = i * x->w + l;
720 for (j = i * x->w + x->line_len[i]; j < limit; j++)
725 /* Puts line L at (H,K) in the current output page. Assumes
726 struct ascii_driver_ext named `ext'. */
727 #define draw_line(H, K, L) \
728 ext->page[ext->w * (K) + (H)] = (L) | 0x800
730 /* Line styles for each position. */
731 #define T(STYLE) (STYLE<<LNS_TOP)
732 #define L(STYLE) (STYLE<<LNS_LEFT)
733 #define B(STYLE) (STYLE<<LNS_BOTTOM)
734 #define R(STYLE) (STYLE<<LNS_RIGHT)
737 ascii_line_horz (struct outp_driver *this, const struct rect *r,
738 const struct color *c UNUSED, int style)
740 struct ascii_driver_ext *ext = this->ext;
741 int x1 = r->x1 / this->horiz;
742 int x2 = r->x2 / this->horiz;
743 int y1 = r->y1 / this->vert;
746 assert (this->driver_open && this->page_open);
751 || x1 < 0 || x1 >= ext->w
752 || x2 <= 0 || x2 > ext->w
753 || y1 < 0 || y1 >= ext->l)
755 #if !SUPPRESS_WARNINGS
756 printf (_("ascii_line_horz: bad hline (%d,%d),%d out of (%d,%d)\n"),
757 x1, x2, y1, ext->w, ext->l);
763 if (ext->line_len[y1] < x2)
764 expand_line (ext, y1, x2);
766 for (x = x1; x < x2; x++)
767 draw_line (x, y1, (style << LNS_LEFT) | (style << LNS_RIGHT));
771 ascii_line_vert (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 y1 = r->y1 / this->vert;
777 int y2 = r->y2 / this->vert;
780 assert (this->driver_open && this->page_open);
785 || x1 < 0 || x1 >= ext->w
786 || y1 < 0 || y1 >= ext->l
787 || y2 < 0 || y2 > ext->l)
789 #if !SUPPRESS_WARNINGS
790 printf (_("ascii_line_vert: bad vline %d,(%d,%d) out of (%d,%d)\n"),
791 x1, y1, y2, ext->w, ext->l);
797 for (y = y1; y < y2; y++)
798 if (ext->line_len[y] <= x1)
799 expand_line (ext, y, x1 + 1);
801 for (y = y1; y < y2; y++)
802 draw_line (x1, y, (style << LNS_TOP) | (style << LNS_BOTTOM));
806 ascii_line_intersection (struct outp_driver *this, const struct rect *r,
807 const struct color *c UNUSED,
808 const struct outp_styles *style)
810 struct ascii_driver_ext *ext = this->ext;
811 int x = r->x1 / this->horiz;
812 int y = r->y1 / this->vert;
815 assert (this->driver_open && this->page_open);
817 if (x < 0 || x >= ext->w || y < 0 || y >= ext->l)
819 #if !SUPPRESS_WARNINGS
820 printf (_("ascii_line_intersection: bad intsct (%d,%d) out of (%d,%d)\n"),
821 x, y, ext->w, ext->l);
827 l = ((style->l << LNS_LEFT) | (style->r << LNS_RIGHT)
828 | (style->t << LNS_TOP) | (style->b << LNS_BOTTOM));
830 if (ext->line_len[y] <= x)
831 expand_line (ext, y, x + 1);
835 /* FIXME: Later we could set this up so that for certain devices it
838 ascii_box (struct outp_driver *this UNUSED, const struct rect *r UNUSED,
839 const struct color *bord UNUSED, const struct color *fill UNUSED)
841 assert (this->driver_open && this->page_open);
844 /* Polylines not supported. */
846 ascii_polyline_begin (struct outp_driver *this UNUSED, const struct color *c UNUSED)
848 assert (this->driver_open && this->page_open);
851 ascii_polyline_point (struct outp_driver *this UNUSED, int x UNUSED, int y UNUSED)
853 assert (this->driver_open && this->page_open);
856 ascii_polyline_end (struct outp_driver *this UNUSED)
858 assert (this->driver_open && this->page_open);
862 ascii_text_set_font_by_name (struct outp_driver * this, const char *s)
864 struct ascii_driver_ext *x = this->ext;
865 int len = strlen (s);
867 assert (this->driver_open && this->page_open);
868 x->cur_font = OUTP_F_R;
871 if (s[len - 1] == 'I')
873 if (len > 1 && s[len - 2] == 'B')
874 x->cur_font = OUTP_F_BI;
876 x->cur_font = OUTP_F_I;
878 else if (s[len - 1] == 'B')
879 x->cur_font = OUTP_F_B;
883 ascii_text_set_font_by_position (struct outp_driver *this, int pos)
885 struct ascii_driver_ext *x = this->ext;
886 assert (this->driver_open && this->page_open);
887 x->cur_font = pos >= 0 && pos < 4 ? pos : 0;
891 ascii_text_set_font_by_family (struct outp_driver *this UNUSED, const char *s UNUSED)
893 assert (this->driver_open && this->page_open);
897 ascii_text_get_font_name (struct outp_driver *this)
899 struct ascii_driver_ext *x = this->ext;
901 assert (this->driver_open && this->page_open);
919 ascii_text_get_font_family (struct outp_driver *this UNUSED)
921 assert (this->driver_open && this->page_open);
926 ascii_text_set_size (struct outp_driver *this, int size)
928 assert (this->driver_open && this->page_open);
929 return size == this->vert;
933 ascii_text_get_size (struct outp_driver *this, int *em_width)
935 assert (this->driver_open && this->page_open);
937 *em_width = this->horiz;
941 static void text_draw (struct outp_driver *this, struct outp_text *t);
943 /* Divides the text T->S into lines of width T->H. Sets T->V to the
944 number of lines necessary. Actually draws the text if DRAW is
947 You probably don't want to look at this code. */
949 delineate (struct outp_driver *this, struct outp_text *t, int draw)
951 /* Width we're fitting everything into. */
952 int width = t->h / this->horiz;
954 /* Maximum `y' position we can write to. */
957 /* Current position in string, character following end of string. */
958 const char *s = ls_value (&t->s);
959 const char *end = ls_end (&t->s);
961 /* Temporary struct outp_text to pass to low-level function. */
962 struct outp_text temp;
964 #if GLOBAL_DEBUGGING && 0
968 printf (_("%s: horiz=%d, vert=%d\n"), this->name, this->horiz, this->vert);
980 temp.options = t->options;
981 ls_shallow_copy (&temp.s, &t->s);
982 temp.h = t->h / this->horiz;
983 temp.x = t->x / this->horiz;
987 temp.y = t->y / this->vert;
989 if (t->options & OUTP_T_VERT)
990 max_y = (t->v / this->vert) + temp.y - 1;
994 while (end - s > width)
999 /* Find first space before &s[width]. */
1005 if (!isspace ((unsigned char) space[-1]))
1014 s = space = &s[width];
1021 ls_init (&temp.s, beg, space - beg);
1022 temp.w = space - beg;
1023 text_draw (this, &temp);
1025 if (++temp.y > max_y)
1028 /* Find first nonspace after space. */
1029 while (s < end && isspace ((unsigned char) *s))
1036 ls_init (&temp.s, s, end - s);
1038 text_draw (this, &temp);
1043 t->v = (temp.y * this->vert) - t->y;
1047 ascii_text_metrics (struct outp_driver *this, struct outp_text *t)
1049 assert (this->driver_open && this->page_open);
1050 if (!(t->options & OUTP_T_HORZ))
1053 t->h = ls_length (&t->s) * this->horiz;
1056 delineate (this, t, 0);
1060 ascii_text_draw (struct outp_driver *this, struct outp_text *t)
1062 /* FIXME: orientations not supported. */
1063 assert (this->driver_open && this->page_open);
1064 if (!(t->options & OUTP_T_HORZ))
1066 struct outp_text temp;
1068 temp.options = t->options;
1070 temp.h = temp.v = 0;
1071 temp.x = t->x / this->horiz;
1072 temp.y = t->y / this->vert;
1073 text_draw (this, &temp);
1074 ascii_text_metrics (this, t);
1078 delineate (this, t, 1);
1082 text_draw (struct outp_driver *this, struct outp_text *t)
1084 struct ascii_driver_ext *ext = this->ext;
1085 unsigned attr = ext->cur_font << 8;
1088 int y = t->y * ext->w;
1090 char *s = ls_value (&t->s);
1092 /* Expand the line with the assumption that S takes up LEN character
1093 spaces (sometimes it takes up less). */
1096 assert (this->driver_open && this->page_open);
1097 switch (t->options & OUTP_T_JUST_MASK)
1099 case OUTP_T_JUST_LEFT:
1101 case OUTP_T_JUST_CENTER:
1102 x -= (t->h - t->w) / 2; /* fall through */
1103 case OUTP_T_JUST_RIGHT:
1110 if (!(t->y < ext->l && x < ext->w))
1112 min_len = min (x + ls_length (&t->s), ext->w);
1113 if (ext->line_len[t->y] < min_len)
1114 expand_line (ext, t->y, min_len);
1117 int len = ls_length (&t->s);
1119 if (len + x > ext->w)
1122 ext->page[y + x++] = *s++ | attr;
1126 /* ascii_close_page () and support routines. */
1128 #define LINE_BUF_SIZE 1024
1129 static unsigned char *line_buf;
1130 static unsigned char *line_p;
1133 commit_line_buf (struct outp_driver *this)
1135 struct ascii_driver_ext *x = this->ext;
1137 if ((int) fwrite (line_buf, 1, line_p - line_buf, x->file.file)
1138 < line_p - line_buf)
1140 msg (ME, _("Writing `%s': %s"), x->file.filename, strerror (errno));
1148 /* Writes everything from BP to EP exclusive into line_buf, or to
1149 THIS->output if line_buf overflows. */
1151 output_string (struct outp_driver *this, const char *bp, const char *ep)
1153 if (LINE_BUF_SIZE - (line_p - line_buf) >= ep - bp)
1155 memcpy (line_p, bp, ep - bp);
1161 if (LINE_BUF_SIZE - (line_p - line_buf) <= 1 && !commit_line_buf (this))
1167 /* Writes everything from BP to EP exclusive into line_buf, or to
1168 THIS->output if line_buf overflows. Returns 1 if additional passes
1169 over the line are required. FIXME: probably could do a lot of
1170 optimization here. */
1172 output_shorts (struct outp_driver *this,
1173 const unsigned short *bp, const unsigned short *ep)
1175 struct ascii_driver_ext *ext = this->ext;
1176 size_t remaining = LINE_BUF_SIZE - (line_p - line_buf);
1179 for (; bp < ep; bp++)
1183 struct len_string *box = &ext->box[*bp & 0xff];
1184 size_t len = ls_length (box);
1186 if (remaining >= len)
1188 memcpy (line_p, ls_value (box), len);
1194 if (!commit_line_buf (this))
1196 output_string (this, ls_value (box), ls_end (box));
1197 remaining = LINE_BUF_SIZE - (line_p - line_buf);
1200 else if (*bp & 0x0300)
1202 struct len_string *on;
1206 switch (*bp & 0x0300)
1209 on = &ext->fonts[FSTY_ON | FSTY_ITALIC];
1212 on = &ext->fonts[FSTY_ON | FSTY_BOLD];
1214 case OUTP_F_BI << 8:
1215 on = &ext->fonts[FSTY_ON | FSTY_BOLD_ITALIC];
1222 if (ext->overstrike_style == OVS_SINGLE)
1223 switch (*bp & 0x0300)
1237 case OUTP_F_BI << 8:
1259 output_string (this, buf, &buf[len]);
1268 if (!commit_line_buf (this))
1270 remaining = LINE_BUF_SIZE - (line_p - line_buf);
1278 /* Writes CH into line_buf N times, or to THIS->output if line_buf
1281 output_char (struct outp_driver *this, int n, int ch)
1283 if (LINE_BUF_SIZE - (line_p - line_buf) >= n)
1285 memset (line_p, ch, n);
1291 if (LINE_BUF_SIZE - (line_p - line_buf) <= 1 && !commit_line_buf (this))
1297 /* Advance the carriage from column 0 to the left margin. */
1299 advance_to_left_margin (struct outp_driver *this)
1301 struct ascii_driver_ext *ext = this->ext;
1304 margin = ext->left_margin;
1307 if (ext->tab_width && margin >= ext->tab_width)
1309 output_char (this, margin / ext->tab_width, '\t');
1310 margin %= ext->tab_width;
1313 output_char (this, margin, ' ');
1316 /* Move the output file carriage N_CHARS left, to the left margin. */
1318 return_carriage (struct outp_driver *this, int n_chars)
1320 struct ascii_driver_ext *ext = this->ext;
1322 switch (ext->carriage_return_style)
1325 output_char (this, n_chars, '\b');
1328 output_char (this, 1, '\r');
1329 advance_to_left_margin (this);
1336 /* Writes COUNT lines from the line buffer in THIS, starting at line
1339 output_lines (struct outp_driver *this, int first, int count)
1341 struct ascii_driver_ext *ext = this->ext;
1344 struct len_string *newline = &ext->ops[OPS_NEWLINE];
1349 if (NULL == ext->file.file)
1352 /* Iterate over all the lines to be output. */
1353 for (line_num = first; line_num < first + count; line_num++)
1355 unsigned short *p = &ext->page[ext->w * line_num];
1356 unsigned short *end_p = p + ext->line_len[line_num];
1357 unsigned short *bp, *ep;
1358 unsigned short attr = 0;
1360 assert (end_p >= p);
1362 /* Squeeze multiple blank lines into a single blank line if
1364 if (ext->squeeze_blank_lines
1366 && ext->line_len[line_num] == 0
1367 && ext->line_len[line_num - 1] == 0)
1370 /* Output every character in the line in the appropriate
1375 advance_to_left_margin (this);
1378 while (ep < end_p && attr == (*ep & 0x0300))
1380 if (output_shorts (this, bp, ep))
1388 /* Turn off old font. */
1389 if (attr != (OUTP_F_R << 8))
1391 struct len_string *off;
1396 off = &ext->fonts[FSTY_OFF | FSTY_ITALIC];
1399 off = &ext->fonts[FSTY_OFF | FSTY_BOLD];
1401 case OUTP_F_BI << 8:
1402 off = &ext->fonts[FSTY_OFF | FSTY_BOLD_ITALIC];
1408 output_string (this, ls_value (off), ls_end (off));
1411 /* Turn on new font. */
1412 attr = (*bp & 0x0300);
1413 if (attr != (OUTP_F_R << 8))
1415 struct len_string *on;
1420 on = &ext->fonts[FSTY_ON | FSTY_ITALIC];
1423 on = &ext->fonts[FSTY_ON | FSTY_BOLD];
1425 case OUTP_F_BI << 8:
1426 on = &ext->fonts[FSTY_ON | FSTY_BOLD_ITALIC];
1432 output_string (this, ls_value (on), ls_end (on));
1441 return_carriage (this, n_chars);
1446 while (ep < end_p && (*ep & 0x0300) == (OUTP_F_R << 8))
1450 output_char (this, ep - bp, ' ');
1452 switch (*ep & 0x0300)
1460 case OUTP_F_BI << 8:
1467 output_char (this, 1, ch);
1468 n_chars += ep - bp + 1;
1475 return_carriage (this, n_chars);
1479 while (ep < end_p && (*ep & 0x0300) != (OUTP_F_BI << 8))
1483 output_char (this, ep - bp, ' ');
1484 output_char (this, 1, '_');
1490 output_string (this, ls_value (newline), ls_end (newline));
1495 ascii_close_page (struct outp_driver *this)
1497 static unsigned char *s;
1500 struct ascii_driver_ext *x = this->ext;
1501 int nl_len, ff_len, total_len;
1505 assert (this->driver_open && this->page_open);
1508 line_buf = xmalloc (LINE_BUF_SIZE);
1511 nl_len = ls_length (&x->ops[OPS_NEWLINE]);
1514 total_len = x->top_margin * nl_len;
1515 if (s_len < total_len)
1518 s = xrealloc (s, s_len);
1520 for (cp = s, i = 0; i < x->top_margin; i++)
1522 memcpy (cp, ls_value (&x->ops[OPS_NEWLINE]), nl_len);
1525 output_string (this, s, &s[total_len]);
1531 total_len = nl_len + x->w;
1532 if (s_len < total_len + 1)
1534 s_len = total_len + 1;
1535 s = xrealloc (s, s_len);
1538 memset (s, ' ', x->w);
1543 snprintf (temp, 80, _("%s - Page %d"), curdate, x->page_number);
1544 memcpy (&s[x->w - strlen (temp)], temp, strlen (temp));
1547 if (outp_title && outp_subtitle)
1549 len = min ((int) strlen (outp_title), x->w);
1550 memcpy (s, outp_title, len);
1552 memcpy (&s[x->w], ls_value (&x->ops[OPS_NEWLINE]), nl_len);
1553 output_string (this, s, &s[total_len]);
1555 memset (s, ' ', x->w);
1556 len = strlen (version) + 3 + strlen (host_system);
1558 sprintf (&s[x->w - len], "%s - %s" , version, host_system);
1559 if (outp_subtitle || outp_title)
1561 char *string = outp_subtitle ? outp_subtitle : outp_title;
1562 len = min ((int) strlen (string), x->w);
1563 memcpy (s, string, len);
1565 memcpy (&s[x->w], ls_value (&x->ops[OPS_NEWLINE]), nl_len);
1566 output_string (this, s, &s[total_len]);
1567 output_string (this, &s[x->w], &s[total_len]);
1569 if (line_p != line_buf && !commit_line_buf (this))
1572 output_lines (this, x->n_output, x->l - x->n_output);
1574 ff_len = ls_length (&x->ops[OPS_FORMFEED]);
1575 total_len = x->bottom_margin * nl_len + ff_len;
1576 if (s_len < total_len)
1577 s = xrealloc (s, total_len);
1578 for (cp = s, i = 0; i < x->bottom_margin; i++)
1580 memcpy (cp, ls_value (&x->ops[OPS_NEWLINE]), nl_len);
1583 memcpy (cp, ls_value (&x->ops[OPS_FORMFEED]), ff_len);
1585 output_string (this, s, &s[total_len]);
1586 if (line_p != line_buf && !commit_line_buf (this))
1591 this->page_open = 0;
1595 struct outp_class ascii_class =
1605 ascii_preopen_driver,
1607 ascii_postopen_driver,
1617 ascii_line_intersection,
1620 ascii_polyline_begin,
1621 ascii_polyline_point,
1624 ascii_text_set_font_by_name,
1625 ascii_text_set_font_by_position,
1626 ascii_text_set_font_by_family,
1627 ascii_text_get_font_name,
1628 ascii_text_get_font_family,
1629 ascii_text_set_size,
1630 ascii_text_get_size,