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.
58 box[x]="strng" Sets box character X (X in base 4: 0-3333).
59 italic-on=overstrike|"strng" Turns on italic (underline).
60 italic-off=""|"strng" Turns off italic; ignored for overstrike.
61 bold-on=overstrike|"strng" Turns on bold.
62 bold-off=""|"strng" Turns off bold; ignored for overstrike.
63 bold-italic-on=overstrike|"strng" Turns on bold-italic.
64 bold-italic-off=""|"strng" Turns off bold-italic; ignored for overstrike.
65 overstrike-style=single|line Can we print a whole line then BS over it, or
66 must we go char by char, as on a terminal?
67 carriage-return-style=bs|cr Must we return the carriage with a sequence of
68 BSes, or will a single CR do it?
71 /* Disable messages by failed range checks. */
72 /*#define SUPPRESS_WARNINGS 1 */
77 CHS_ASCII, /* 7-bit ASCII */
78 CHS_LATIN1 /* Latin 1; not really supported at the moment */
81 /* Overstrike style. */
84 OVS_SINGLE, /* Overstrike each character: "a\b_b\b_c\b_" */
85 OVS_LINE /* Overstrike lines: "abc\b\b\b___" (or if
86 newline is "\r\n", then "abc\r___"). Easier
87 on the printer, doesn't work on a tty. */
90 /* Basic output strings. */
93 OPS_INIT, /* Document initialization string. */
94 OPS_DONE, /* Document uninit string. */
95 OPS_FORMFEED, /* Formfeed string. */
96 OPS_NEWLINE, /* Newline string. */
98 OPS_COUNT /* Number of output strings. */
101 /* Line styles bit shifts. */
112 /* Carriage return style. */
115 CRS_BS, /* Multiple backspaces. */
116 CRS_CR /* Single carriage return. */
119 /* Assembles a byte from four taystes. */
120 #define TAYSTE2BYTE(T, L, B, R) \
122 | ((L) << LNS_LEFT) \
123 | ((B) << LNS_BOTTOM) \
124 | ((R) << LNS_RIGHT))
126 /* Extract tayste with shift value S from byte B. */
127 #define BYTE2TAYSTE(B, S) \
130 /* Font style; take one of the first group |'d with one of the second group. */
133 FSTY_ON = 000, /* Turn font on. */
134 FSTY_OFF = 001, /* Turn font off. */
136 FSTY_ITALIC = 0, /* Italic font. */
137 FSTY_BOLD = 2, /* Bold font. */
138 FSTY_BOLD_ITALIC = 4, /* Bold-italic font. */
140 FSTY_COUNT = 6 /* Number of font styles. */
143 /* ASCII output driver extension record. */
144 struct ascii_driver_ext
146 /* User parameters. */
147 int char_set; /* CHS_ASCII/CHS_LATIN1; no-op right now. */
148 int headers; /* 1=print headers at top of page. */
149 int page_length; /* Page length in lines. */
150 int page_width; /* Page width in characters. */
151 int lpi; /* Lines per inch. */
152 int cpi; /* Characters per inch. */
153 int left_margin; /* Left margin in characters. */
154 int right_margin; /* Right margin in characters. */
155 int top_margin; /* Top margin in lines. */
156 int bottom_margin; /* Bottom margin in lines. */
157 int paginate; /* 1=insert formfeeds. */
158 int tab_width; /* Width of a tab; 0 not to use tabs. */
159 struct len_string ops[OPS_COUNT]; /* Basic output strings. */
160 struct len_string box[LNS_COUNT]; /* Line & box drawing characters. */
161 struct len_string fonts[FSTY_COUNT]; /* Font styles; NULL=overstrike. */
162 int overstrike_style; /* OVS_SINGLE or OVS_LINE. */
163 int carriage_return_style; /* Carriage return style. */
165 /* Internal state. */
166 struct file_ext file; /* Output file. */
167 int page_number; /* Current page number. */
168 unsigned short *page; /* Page content. */
169 int page_size; /* Number of bytes allocated for page, attr. */
170 int *line_len; /* Length of each line in page, attr. */
171 int line_len_size; /* Number of ints allocated for line_len. */
172 int w, l; /* Actual width & length w/o margins, etc. */
173 int n_output; /* Number of lines output so far. */
174 int cur_font; /* Current font by OUTP_F_*. */
176 int debug; /* Set by som_text_draw(). */
180 static struct pool *ascii_pool;
182 static int postopen (struct file_ext *);
183 static int preclose (struct file_ext *);
186 ascii_open_global (struct outp_class *this unused)
188 ascii_pool = pool_create ();
193 ascii_close_global (struct outp_class *this unused)
195 pool_destroy (ascii_pool);
200 ascii_font_sizes (struct outp_class *this unused, int *n_valid_sizes)
202 static int valid_sizes[] = {12, 12, 0, 0};
204 assert (n_valid_sizes);
210 ascii_preopen_driver (struct outp_driver *this)
212 struct ascii_driver_ext *x;
215 assert (this->driver_open == 0);
216 msg (VM (1), _("ASCII driver initializing as `%s'..."), this->name);
217 this->ext = x = xmalloc (sizeof (struct ascii_driver_ext));
218 x->char_set = CHS_ASCII;
227 x->bottom_margin = 2;
230 for (i = 0; i < OPS_COUNT; i++)
231 ls_null (&x->ops[i]);
232 for (i = 0; i < LNS_COUNT; i++)
233 ls_null (&x->box[i]);
234 for (i = 0; i < FSTY_COUNT; i++)
235 ls_null (&x->fonts[i]);
236 x->overstrike_style = OVS_SINGLE;
237 x->carriage_return_style = CRS_BS;
238 x->file.filename = NULL;
241 x->file.sequence_no = &x->page_number;
243 x->file.postopen = postopen;
244 x->file.preclose = preclose;
249 x->line_len_size = 0;
251 x->cur_font = OUTP_F_R;
259 ascii_postopen_driver (struct outp_driver *this)
261 struct ascii_driver_ext *x = this->ext;
263 assert (this->driver_open == 0);
265 if (NULL == x->file.filename)
266 x->file.filename = xstrdup ("pspp.list");
268 x->w = x->page_width - x->left_margin - x->right_margin;
269 x->l = (x->page_length - (x->headers ? 3 : 0) - x->top_margin
270 - x->bottom_margin - 1);
271 if (x->w < 59 || x->l < 15)
273 msg (SE, _("ascii driver: Area of page excluding margins and headers "
274 "must be at least 59 characters wide by 15 lines long. Page as "
275 "configured is only %d characters by %d lines."), x->w, x->l);
279 this->res = x->lpi * x->cpi;
280 this->horiz = x->lpi;
282 this->width = x->w * this->horiz;
283 this->length = x->l * this->vert;
285 if (ls_null_p (&x->ops[OPS_FORMFEED]))
286 ls_create (ascii_pool, &x->ops[OPS_FORMFEED], "\f");
287 if (ls_null_p (&x->ops[OPS_NEWLINE])
288 || !strcmp (ls_value (&x->ops[OPS_NEWLINE]), "default"))
290 ls_create (ascii_pool, &x->ops[OPS_NEWLINE], "\n");
297 for (i = 0; i < LNS_COUNT; i++)
301 if (!ls_null_p (&x->box[i]))
305 case TAYSTE2BYTE (0, 0, 0, 0):
309 case TAYSTE2BYTE (0, 1, 0, 0):
310 case TAYSTE2BYTE (0, 1, 0, 1):
311 case TAYSTE2BYTE (0, 0, 0, 1):
315 case TAYSTE2BYTE (1, 0, 0, 0):
316 case TAYSTE2BYTE (1, 0, 1, 0):
317 case TAYSTE2BYTE (0, 0, 1, 0):
321 case TAYSTE2BYTE (0, 3, 0, 0):
322 case TAYSTE2BYTE (0, 3, 0, 3):
323 case TAYSTE2BYTE (0, 0, 0, 3):
324 case TAYSTE2BYTE (0, 2, 0, 0):
325 case TAYSTE2BYTE (0, 2, 0, 2):
326 case TAYSTE2BYTE (0, 0, 0, 2):
330 case TAYSTE2BYTE (3, 0, 0, 0):
331 case TAYSTE2BYTE (3, 0, 3, 0):
332 case TAYSTE2BYTE (0, 0, 3, 0):
333 case TAYSTE2BYTE (2, 0, 0, 0):
334 case TAYSTE2BYTE (2, 0, 2, 0):
335 case TAYSTE2BYTE (0, 0, 2, 0):
340 if (BYTE2TAYSTE (i, LNS_LEFT) > 1
341 || BYTE2TAYSTE (i, LNS_TOP) > 1
342 || BYTE2TAYSTE (i, LNS_RIGHT) > 1
343 || BYTE2TAYSTE (i, LNS_BOTTOM) > 1)
349 ls_create (ascii_pool, &x->box[i], c);
356 this->cp_x = this->cp_y = 0;
357 this->font_height = this->vert;
358 this->prop_em_width = this->horiz;
359 this->fixed_width = this->horiz;
361 this->horiz_line_width[0] = 0;
362 this->vert_line_width[0] = 0;
364 for (i = 1; i < OUTP_L_COUNT; i++)
366 this->horiz_line_width[i] = this->vert;
367 this->vert_line_width[i] = this->horiz;
370 for (i = 0; i < (1 << OUTP_L_COUNT); i++)
372 this->horiz_line_spacing[i] = (i & ~1) ? this->vert : 0;
373 this->vert_line_spacing[i] = (i & ~1) ? this->horiz : 0;
377 this->driver_open = 1;
378 msg (VM (2), _("%s: Initialization complete."), this->name);
384 ascii_close_driver (struct outp_driver *this)
386 struct ascii_driver_ext *x = this->ext;
388 assert (this->driver_open == 1);
389 msg (VM (2), _("%s: Beginning closing..."), this->name);
394 fn_close_ext (&x->file);
395 free (x->file.filename);
398 this->driver_open = 0;
399 msg (VM (3), _("%s: Finished closing."), this->name);
404 /* Generic option types. */
414 static struct outp_option option_tab[] =
416 {"headers", boolean_arg, 0},
417 {"output-file", 1, 0},
419 {"length", pos_int_arg, 0},
420 {"width", pos_int_arg, 1},
421 {"lpi", pos_int_arg, 2},
422 {"cpi", pos_int_arg, 3},
423 {"init", string_arg, 0},
424 {"done", string_arg, 1},
425 {"left-margin", nonneg_int_arg, 0},
426 {"right-margin", nonneg_int_arg, 1},
427 {"top-margin", nonneg_int_arg, 2},
428 {"bottom-margin", nonneg_int_arg, 3},
429 {"paginate", boolean_arg, 1},
430 {"form-feed-string", string_arg, 2},
431 {"newline-string", string_arg, 3},
432 {"italic-on", font_string_arg, 0},
433 {"italic-off", font_string_arg, 1},
434 {"bold-on", font_string_arg, 2},
435 {"bold-off", font_string_arg, 3},
436 {"bold-italic-on", font_string_arg, 4},
437 {"bold-italic-off", font_string_arg, 5},
438 {"overstrike-style", 3, 0},
439 {"tab-width", nonneg_int_arg, 4},
440 {"carriage-return-style", 4, 0},
443 static struct outp_option_info option_info;
446 ascii_option (struct outp_driver *this, const char *key,
447 const struct string *val)
449 struct ascii_driver_ext *x = this->ext;
453 value = ds_value (val);
454 if (!strncmp (key, "box[", 4))
457 int indx = strtol (&key[4], &tail, 4);
458 if (*tail != ']' || indx < 0 || indx > LNS_COUNT)
460 msg (SE, _("Bad index value for `box' key: syntax is box[INDEX], "
461 "0 <= INDEX < %d decimal, with INDEX expressed in base 4."),
465 if (!ls_null_p (&x->box[indx]))
466 msg (SW, _("Duplicate value for key `%s'."), key);
467 ls_create (ascii_pool, &x->box[indx], value);
471 cat = outp_match_keyword (key, option_tab, &option_info, &subcat);
476 msg (SE, _("Unknown configuration parameter `%s' for ascii device driver."),
480 free (x->file.filename);
481 x->file.filename = xstrdup (value);
484 if (!strcmp (value, "ascii"))
485 x->char_set = CHS_ASCII;
486 else if (!strcmp (value, "latin1"))
487 x->char_set = CHS_LATIN1;
489 msg (SE, _("Unknown character set `%s'. Valid character sets are "
490 "`ascii' and `latin1'."), value);
493 if (!strcmp (value, "single"))
494 x->overstrike_style = OVS_SINGLE;
495 else if (!strcmp (value, "line"))
496 x->overstrike_style = OVS_LINE;
498 msg (SE, _("Unknown overstrike style `%s'. Valid overstrike styles "
499 "are `single' and `line'."), value);
502 if (!strcmp (value, "bs"))
503 x->carriage_return_style = CRS_BS;
504 else if (!strcmp (value, "cr"))
505 x->carriage_return_style = CRS_CR;
507 msg (SE, _("Unknown carriage return style `%s'. Valid carriage "
508 "return styles are `cr' and `bs'."), value);
516 arg = strtol (value, &tail, 0);
517 if (arg < 1 || errno == ERANGE || *tail)
519 msg (SE, _("Positive integer required as value for `%s'."), key);
525 x->page_length = arg;
547 arg = strtol (value, &tail, 0);
548 if (arg < 0 || errno == ERANGE || *tail)
550 msg (SE, _("Zero or positive integer required as value for `%s'."),
557 x->left_margin = arg;
560 x->right_margin = arg;
566 x->bottom_margin = arg;
578 struct len_string *s;
582 s = &x->ops[OPS_INIT];
585 s = &x->ops[OPS_DONE];
588 s = &x->ops[OPS_FORMFEED];
591 s = &x->ops[OPS_NEWLINE];
596 ls_create (ascii_pool, s, value);
599 case font_string_arg:
601 if (!strcmp (value, "overstrike"))
603 ls_destroy (ascii_pool, &x->fonts[subcat]);
606 ls_create (ascii_pool, &x->fonts[subcat], value);
612 if (!strcmp (value, "on") || !strcmp (value, "true")
613 || !strcmp (value, "yes") || atoi (value))
615 else if (!strcmp (value, "off") || !strcmp (value, "false")
616 || !strcmp (value, "no") || !strcmp (value, "0"))
620 msg (SE, _("Boolean value expected for %s."), key);
629 x->paginate = setting;
642 postopen (struct file_ext *f)
644 struct ascii_driver_ext *x = f->param;
645 struct len_string *s = &x->ops[OPS_INIT];
647 if (!ls_empty_p (s) && fwrite (ls_value (s), ls_length (s), 1, f->file) < 1)
649 msg (ME, _("ASCII output driver: %s: %s"),
650 f->filename, strerror (errno));
657 preclose (struct file_ext *f)
659 struct ascii_driver_ext *x = f->param;
660 struct len_string *d = &x->ops[OPS_DONE];
662 if (!ls_empty_p (d) && fwrite (ls_value (d), ls_length (d), 1, f->file) < 1)
664 msg (ME, _("ASCII output driver: %s: %s"),
665 f->filename, strerror (errno));
672 ascii_open_page (struct outp_driver *this)
674 struct ascii_driver_ext *x = this->ext;
677 assert (this->driver_open && !this->page_open);
679 if (!fn_open_ext (&x->file))
681 msg (ME, _("ASCII output driver: %s: %s"), x->file.filename,
686 req_page_size = x->w * x->l;
687 if (req_page_size > x->page_size || req_page_size / 2 < x->page_size)
689 x->page_size = req_page_size;
690 x->page = xrealloc (x->page, sizeof *x->page * req_page_size);
693 if (x->l > x->line_len_size)
695 x->line_len_size = x->l;
696 x->line_len = xrealloc (x->line_len,
697 sizeof *x->line_len * x->line_len_size);
700 memset (x->line_len, 0, sizeof *x->line_len * x->l);
706 /* Ensures that at least the first L characters of line I in the
707 driver identified by struct ascii_driver_ext *X have been cleared out. */
709 expand_line (struct ascii_driver_ext *x, int i, int l)
711 int limit = i * x->w + l;
714 for (j = i * x->w + x->line_len[i]; j < limit; j++)
719 /* Puts line L at (H,K) in the current output page. Assumes
720 struct ascii_driver_ext named `ext'. */
721 #define draw_line(H, K, L) \
722 ext->page[ext->w * (K) + (H)] = (L) | 0x800
724 /* Line styles for each position. */
725 #define T(STYLE) (STYLE<<LNS_TOP)
726 #define L(STYLE) (STYLE<<LNS_LEFT)
727 #define B(STYLE) (STYLE<<LNS_BOTTOM)
728 #define R(STYLE) (STYLE<<LNS_RIGHT)
731 ascii_line_horz (struct outp_driver *this, const struct rect *r,
732 const struct color *c unused, int style)
734 struct ascii_driver_ext *ext = this->ext;
735 int x1 = r->x1 / this->horiz;
736 int x2 = r->x2 / this->horiz;
737 int y1 = r->y1 / this->vert;
740 assert (this->driver_open && this->page_open);
745 || x1 < 0 || x1 >= ext->w
746 || x2 <= 0 || x2 > ext->w
747 || y1 < 0 || y1 >= ext->l)
749 #if !SUPPRESS_WARNINGS
750 printf (_("ascii_line_horz: bad hline (%d,%d),%d out of (%d,%d)\n"),
751 x1, x2, y1, ext->w, ext->l);
757 if (ext->line_len[y1] < x2)
758 expand_line (ext, y1, x2);
760 for (x = x1; x < x2; x++)
761 draw_line (x, y1, (style << LNS_LEFT) | (style << LNS_RIGHT));
765 ascii_line_vert (struct outp_driver *this, const struct rect *r,
766 const struct color *c unused, int style)
768 struct ascii_driver_ext *ext = this->ext;
769 int x1 = r->x1 / this->horiz;
770 int y1 = r->y1 / this->vert;
771 int y2 = r->y2 / this->vert;
774 assert (this->driver_open && this->page_open);
779 || x1 < 0 || x1 >= ext->w
780 || y1 < 0 || y1 >= ext->l
781 || y2 < 0 || y2 > ext->l)
783 #if !SUPPRESS_WARNINGS
784 printf (_("ascii_line_vert: bad vline %d,(%d,%d) out of (%d,%d)\n"),
785 x1, y1, y2, ext->w, ext->l);
791 for (y = y1; y < y2; y++)
792 if (ext->line_len[y] <= x1)
793 expand_line (ext, y, x1 + 1);
795 for (y = y1; y < y2; y++)
796 draw_line (x1, y, (style << LNS_TOP) | (style << LNS_BOTTOM));
800 ascii_line_intersection (struct outp_driver *this, const struct rect *r,
801 const struct color *c unused,
802 const struct outp_styles *style)
804 struct ascii_driver_ext *ext = this->ext;
805 int x = r->x1 / this->horiz;
806 int y = r->y1 / this->vert;
809 assert (this->driver_open && this->page_open);
811 if (x < 0 || x >= ext->w || y < 0 || y >= ext->l)
813 #if !SUPPRESS_WARNINGS
814 printf (_("ascii_line_intersection: bad intsct (%d,%d) out of (%d,%d)\n"),
815 x, y, ext->w, ext->l);
821 l = ((style->l << LNS_LEFT) | (style->r << LNS_RIGHT)
822 | (style->t << LNS_TOP) | (style->b << LNS_BOTTOM));
824 if (ext->line_len[y] <= x)
825 expand_line (ext, y, x + 1);
830 ascii_line_width (struct outp_driver *this, int *width, int *height)
834 assert (this->driver_open && this->page_open);
835 width[0] = height[0] = 0;
836 for (i = 1; i < OUTP_L_COUNT; i++)
838 width[i] = this->horiz;
839 height[i] = this->vert;
843 /* FIXME: Later we could set this up so that for certain devices it
846 ascii_box (struct outp_driver *this unused, const struct rect *r unused,
847 const struct color *bord unused, const struct color *fill unused)
849 assert (this->driver_open && this->page_open);
852 /* Polylines not supported. */
854 ascii_polyline_begin (struct outp_driver *this unused, const struct color *c unused)
856 assert (this->driver_open && this->page_open);
859 ascii_polyline_point (struct outp_driver *this unused, int x unused, int y unused)
861 assert (this->driver_open && this->page_open);
864 ascii_polyline_end (struct outp_driver *this unused)
866 assert (this->driver_open && this->page_open);
870 ascii_text_set_font_by_name (struct outp_driver * this, const char *s)
872 struct ascii_driver_ext *x = this->ext;
873 int len = strlen (s);
875 assert (this->driver_open && this->page_open);
876 x->cur_font = OUTP_F_R;
879 if (s[len - 1] == 'I')
881 if (len > 1 && s[len - 2] == 'B')
882 x->cur_font = OUTP_F_BI;
884 x->cur_font = OUTP_F_I;
886 else if (s[len - 1] == 'B')
887 x->cur_font = OUTP_F_B;
891 ascii_text_set_font_by_position (struct outp_driver *this, int pos)
893 struct ascii_driver_ext *x = this->ext;
894 assert (this->driver_open && this->page_open);
895 x->cur_font = pos >= 0 && pos < 4 ? pos : 0;
899 ascii_text_set_font_by_family (struct outp_driver *this unused, const char *s unused)
901 assert (this->driver_open && this->page_open);
905 ascii_text_get_font_name (struct outp_driver *this)
907 struct ascii_driver_ext *x = this->ext;
909 assert (this->driver_open && this->page_open);
927 ascii_text_get_font_family (struct outp_driver *this unused)
929 assert (this->driver_open && this->page_open);
934 ascii_text_set_size (struct outp_driver *this, int size)
936 assert (this->driver_open && this->page_open);
937 return size == this->vert;
941 ascii_text_get_size (struct outp_driver *this, int *em_width)
943 assert (this->driver_open && this->page_open);
945 *em_width = this->horiz;
949 static void text_draw (struct outp_driver *this, struct outp_text *t);
951 /* Divides the text T->S into lines of width T->H. Sets T->V to the
952 number of lines necessary. Actually draws the text if DRAW is
955 You probably don't want to look at this code. */
957 delineate (struct outp_driver *this, struct outp_text *t, int draw)
959 /* Width we're fitting everything into. */
960 int width = t->h / this->horiz;
962 /* Maximum `y' position we can write to. */
965 /* Current position in string, character following end of string. */
966 const char *s = ls_value (&t->s);
967 const char *end = ls_end (&t->s);
969 /* Temporary struct outp_text to pass to low-level function. */
970 struct outp_text temp;
972 #if GLOBAL_DEBUGGING && 0
976 printf (_("%s: horiz=%d, vert=%d\n"), this->name, this->horiz, this->vert);
988 temp.options = t->options;
989 ls_shallow_copy (&temp.s, &t->s);
990 temp.h = t->h / this->horiz;
991 temp.x = t->x / this->horiz;
995 temp.y = t->y / this->vert;
997 if (t->options & OUTP_T_VERT)
998 max_y = (t->v / this->vert) + temp.y - 1;
1002 while (end - s > width)
1004 const char *beg = s;
1007 /* Find first space before &s[width]. */
1013 if (!isspace ((unsigned char) space[-1]))
1022 s = space = &s[width];
1029 ls_init (&temp.s, beg, space - beg);
1030 temp.w = space - beg;
1031 text_draw (this, &temp);
1033 if (++temp.y > max_y)
1036 /* Find first nonspace after space. */
1037 while (s < end && isspace ((unsigned char) *s))
1044 ls_init (&temp.s, s, end - s);
1046 text_draw (this, &temp);
1051 t->v = (temp.y * this->vert) - t->y;
1055 ascii_text_metrics (struct outp_driver *this, struct outp_text *t)
1057 assert (this->driver_open && this->page_open);
1058 if (!(t->options & OUTP_T_HORZ))
1061 t->h = ls_length (&t->s) * this->horiz;
1064 delineate (this, t, 0);
1068 ascii_text_draw (struct outp_driver *this, struct outp_text *t)
1070 /* FIXME: orientations not supported. */
1071 assert (this->driver_open && this->page_open);
1072 if (!(t->options & OUTP_T_HORZ))
1074 struct outp_text temp;
1076 temp.options = t->options;
1078 temp.h = temp.v = 0;
1079 temp.x = t->x / this->horiz;
1080 temp.y = t->y / this->vert;
1081 text_draw (this, &temp);
1082 ascii_text_metrics (this, t);
1086 delineate (this, t, 1);
1090 text_draw (struct outp_driver *this, struct outp_text *t)
1092 struct ascii_driver_ext *ext = this->ext;
1093 unsigned attr = ext->cur_font << 8;
1096 int y = t->y * ext->w;
1098 char *s = ls_value (&t->s);
1100 /* Expand the line with the assumption that S takes up LEN character
1101 spaces (sometimes it takes up less). */
1104 assert (this->driver_open && this->page_open);
1105 switch (t->options & OUTP_T_JUST_MASK)
1107 case OUTP_T_JUST_LEFT:
1109 case OUTP_T_JUST_CENTER:
1110 x -= (t->h - t->w) / 2; /* fall through */
1111 case OUTP_T_JUST_RIGHT:
1118 if (!(t->y < ext->l && x < ext->w))
1120 min_len = min (x + ls_length (&t->s), ext->w);
1121 if (ext->line_len[t->y] < min_len)
1122 expand_line (ext, t->y, min_len);
1125 int len = ls_length (&t->s);
1127 if (len + x > ext->w)
1130 ext->page[y + x++] = *s++ | attr;
1134 /* ascii_close_page () and support routines. */
1136 #define LINE_BUF_SIZE 1024
1137 static unsigned char *line_buf;
1138 static unsigned char *line_p;
1141 commit_line_buf (struct outp_driver *this)
1143 struct ascii_driver_ext *x = this->ext;
1145 if ((int) fwrite (line_buf, 1, line_p - line_buf, x->file.file)
1146 < line_p - line_buf)
1148 msg (ME, _("Writing `%s': %s"), x->file.filename, strerror (errno));
1156 /* Writes everything from BP to EP exclusive into line_buf, or to
1157 THIS->output if line_buf overflows. */
1159 output_string (struct outp_driver *this, const char *bp, const char *ep)
1161 if (LINE_BUF_SIZE - (line_p - line_buf) >= ep - bp)
1163 memcpy (line_p, bp, ep - bp);
1169 if (LINE_BUF_SIZE - (line_p - line_buf) <= 1 && !commit_line_buf (this))
1175 /* Writes everything from BP to EP exclusive into line_buf, or to
1176 THIS->output if line_buf overflows. Returns 1 if additional passes
1177 over the line are required. FIXME: probably could do a lot of
1178 optimization here. */
1180 output_shorts (struct outp_driver *this,
1181 const unsigned short *bp, const unsigned short *ep)
1183 struct ascii_driver_ext *ext = this->ext;
1184 size_t remaining = LINE_BUF_SIZE - (line_p - line_buf);
1187 for (; bp < ep; bp++)
1191 struct len_string *box = &ext->box[*bp & 0xff];
1192 size_t len = ls_length (box);
1194 if (remaining >= len)
1196 memcpy (line_p, ls_value (box), len);
1202 if (!commit_line_buf (this))
1204 output_string (this, ls_value (box), ls_end (box));
1205 remaining = LINE_BUF_SIZE - (line_p - line_buf);
1208 else if (*bp & 0x0300)
1210 struct len_string *on;
1214 switch (*bp & 0x0300)
1217 on = &ext->fonts[FSTY_ON | FSTY_ITALIC];
1220 on = &ext->fonts[FSTY_ON | FSTY_BOLD];
1222 case OUTP_F_BI << 8:
1223 on = &ext->fonts[FSTY_ON | FSTY_BOLD_ITALIC];
1230 if (ext->overstrike_style == OVS_SINGLE)
1231 switch (*bp & 0x0300)
1245 case OUTP_F_BI << 8:
1267 output_string (this, buf, &buf[len]);
1276 if (!commit_line_buf (this))
1278 remaining = LINE_BUF_SIZE - (line_p - line_buf);
1286 /* Writes CH into line_buf N times, or to THIS->output if line_buf
1289 output_char (struct outp_driver *this, int n, int ch)
1291 if (LINE_BUF_SIZE - (line_p - line_buf) >= n)
1293 memset (line_p, ch, n);
1299 if (LINE_BUF_SIZE - (line_p - line_buf) <= 1 && !commit_line_buf (this))
1305 /* Advance the carriage from column 0 to the left margin. */
1307 advance_to_left_margin (struct outp_driver *this)
1309 struct ascii_driver_ext *ext = this->ext;
1312 margin = ext->left_margin;
1315 if (ext->tab_width && margin >= ext->tab_width)
1317 output_char (this, margin / ext->tab_width, '\t');
1318 margin %= ext->tab_width;
1321 output_char (this, margin, ' ');
1324 /* Move the output file carriage N_CHARS left, to the left margin. */
1326 return_carriage (struct outp_driver *this, int n_chars)
1328 struct ascii_driver_ext *ext = this->ext;
1330 switch (ext->carriage_return_style)
1333 output_char (this, n_chars, '\b');
1336 output_char (this, 1, '\r');
1337 advance_to_left_margin (this);
1344 /* Writes COUNT lines from the line buffer in THIS, starting at line
1347 output_lines (struct outp_driver *this, int first, int count)
1349 struct ascii_driver_ext *ext = this->ext;
1351 unsigned short *p = &ext->page[ext->w * first];
1352 int *len = &ext->line_len[first];
1353 struct len_string *newline = &ext->ops[OPS_NEWLINE];
1358 if (NULL == ext->file.file)
1361 while (count--) /* Iterate over all the lines to be output. */
1363 unsigned short *end_p;
1364 unsigned short *bp, *ep;
1365 unsigned short attr = 0;
1368 assert (end_p >= p);
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:
1465 output_char (this, 1, ch);
1466 n_chars += ep - bp + 1;
1473 return_carriage (this, n_chars);
1477 while (ep < end_p && (*ep & 0x0300) != (OUTP_F_BI << 8))
1481 output_char (this, ep - bp, ' ');
1482 output_char (this, 1, '_');
1489 output_string (this, ls_value (newline), ls_end (newline));
1494 ascii_close_page (struct outp_driver *this)
1496 static unsigned char *s;
1499 struct ascii_driver_ext *x = this->ext;
1500 int nl_len, ff_len, total_len;
1504 assert (this->driver_open && this->page_open);
1507 line_buf = xmalloc (LINE_BUF_SIZE);
1510 nl_len = ls_length (&x->ops[OPS_NEWLINE]);
1513 total_len = x->top_margin * nl_len;
1514 if (s_len < total_len)
1517 s = xrealloc (s, s_len);
1519 for (cp = s, i = 0; i < x->top_margin; i++)
1521 memcpy (cp, ls_value (&x->ops[OPS_NEWLINE]), nl_len);
1524 output_string (this, s, &s[total_len]);
1530 total_len = nl_len + x->w;
1531 if (s_len < total_len + 1)
1533 s_len = total_len + 1;
1534 s = xrealloc (s, s_len);
1537 memset (s, ' ', x->w);
1542 snprintf (temp, 80, _("%s - Page %d"), curdate, x->page_number);
1543 memcpy (&s[x->w - strlen (temp)], temp, strlen (temp));
1546 if (outp_title && outp_subtitle)
1548 len = min ((int) strlen (outp_title), x->w);
1549 memcpy (s, outp_title, len);
1551 memcpy (&s[x->w], ls_value (&x->ops[OPS_NEWLINE]), nl_len);
1552 output_string (this, s, &s[total_len]);
1554 memset (s, ' ', x->w);
1555 len = strlen (version) + 3 + strlen (host_system);
1557 sprintf (&s[x->w - len], "%s - %s" , version, host_system);
1558 if (outp_subtitle || outp_title)
1560 char *string = outp_subtitle ? outp_subtitle : outp_title;
1561 len = min ((int) strlen (string), x->w);
1562 memcpy (s, string, len);
1564 memcpy (&s[x->w], ls_value (&x->ops[OPS_NEWLINE]), nl_len);
1565 output_string (this, s, &s[total_len]);
1566 output_string (this, &s[x->w], &s[total_len]);
1568 if (line_p != line_buf && !commit_line_buf (this))
1571 output_lines (this, x->n_output, x->l - x->n_output);
1573 ff_len = ls_length (&x->ops[OPS_FORMFEED]);
1574 total_len = x->bottom_margin * nl_len + ff_len;
1575 if (s_len < total_len)
1576 s = xrealloc (s, total_len);
1577 for (cp = s, i = 0; i < x->bottom_margin; i++)
1579 memcpy (cp, ls_value (&x->ops[OPS_NEWLINE]), nl_len);
1582 memcpy (cp, ls_value (&x->ops[OPS_FORMFEED]), ff_len);
1583 output_string (this, s, &s[total_len]);
1584 if (line_p != line_buf && !commit_line_buf (this))
1589 this->page_open = 0;
1593 struct outp_class ascii_class =
1603 ascii_preopen_driver,
1605 ascii_postopen_driver,
1615 ascii_line_intersection,
1618 ascii_polyline_begin,
1619 ascii_polyline_point,
1622 ascii_text_set_font_by_name,
1623 ascii_text_set_font_by_position,
1624 ascii_text_set_font_by_family,
1625 ascii_text_get_font_name,
1626 ascii_text_get_font_family,
1627 ascii_text_set_size,
1628 ascii_text_get_size,