Make the expression code a little nicer and fix bugs found
[pspp-builds.git] / src / ascii.c
1 /* PSPP - computes sample statistics.
2    Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
3    Written by Ben Pfaff <blp@gnu.org>.
4
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.
9
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.
14
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
18    02111-1307, USA. */
19
20 #include <config.h>
21 #include "error.h"
22 #include <ctype.h>
23 #include <errno.h>
24 #include <limits.h>
25 #include <stdlib.h>
26 #include "alloc.h"
27 #include "error.h"
28 #include "filename.h"
29 #include "main.h"
30 #include "misc.h"
31 #include "output.h"
32 #include "pool.h"
33 #include "version.h"
34
35 /* ASCII driver options: (defaults listed first)
36
37    output-file="pspp.list"
38    char-set=ascii|latin1
39    form-feed-string="\f"        Written as a formfeed.
40    newline-string=default|"\r\n"|"\n"   
41                                 Written as a newline.
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.
46    
47    headers=on|off               Put headers at top of page?
48    length=66
49    width=130
50    lpi=6                        Only used to determine font size.
51    cpi=10                       
52    squeeze=off|on               Squeeze multiple newlines into exactly one.
53
54    left-margin=0
55    right-margin=0
56    top-margin=2
57    bottom-margin=2
58
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?
70  */
71
72 /* Disable messages by failed range checks. */
73 /*#define SUPPRESS_WARNINGS 1 */
74
75 /* Character set. */
76 enum
77   {
78     CHS_ASCII,                  /* 7-bit ASCII */
79     CHS_LATIN1                  /* Latin 1; not really supported at the moment */
80   };
81
82 /* Overstrike style. */
83 enum
84   {
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. */
89   };
90
91 /* Basic output strings. */
92 enum
93   {
94     OPS_INIT,                   /* Document initialization string. */
95     OPS_DONE,                   /* Document uninit string. */
96     OPS_FORMFEED,               /* Formfeed string. */
97     OPS_NEWLINE,                /* Newline string. */
98
99     OPS_COUNT                   /* Number of output strings. */
100   };
101
102 /* Line styles bit shifts. */
103 enum
104   {
105     LNS_TOP = 0,
106     LNS_LEFT = 2,
107     LNS_BOTTOM = 4,
108     LNS_RIGHT = 6,
109
110     LNS_COUNT = 256
111   };
112
113 /* Carriage return style. */
114 enum
115   {
116     CRS_BS,                     /* Multiple backspaces. */
117     CRS_CR                      /* Single carriage return. */
118   };
119
120 /* Assembles a byte from four taystes. */
121 #define TAYSTE2BYTE(T, L, B, R)                 \
122         (((T) << LNS_TOP)                       \
123          | ((L) << LNS_LEFT)                    \
124          | ((B) << LNS_BOTTOM)                  \
125          | ((R) << LNS_RIGHT))
126
127 /* Extract tayste with shift value S from byte B. */
128 #define BYTE2TAYSTE(B, S)                       \
129         (((B) >> (S)) & 3)
130
131 /* Font style; take one of the first group |'d with one of the second group. */
132 enum
133   {
134     FSTY_ON = 000,              /* Turn font on. */
135     FSTY_OFF = 001,             /* Turn font off. */
136
137     FSTY_ITALIC = 0,            /* Italic font. */
138     FSTY_BOLD = 2,              /* Bold font. */
139     FSTY_BOLD_ITALIC = 4,       /* Bold-italic font. */
140
141     FSTY_COUNT = 6              /* Number of font styles. */
142   };
143
144 /* ASCII output driver extension record. */
145 struct ascii_driver_ext
146   {
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. */
166
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_*. */
177 #if GLOBAL_DEBUGGING
178     int debug;                  /* Set by som_text_draw(). */
179 #endif
180   };
181
182 static struct pool *ascii_pool;
183
184 static int postopen (struct file_ext *);
185 static int preclose (struct file_ext *);
186
187 static int
188 ascii_open_global (struct outp_class *this UNUSED)
189 {
190   ascii_pool = pool_create ();
191   return 1;
192 }
193
194 static int
195 ascii_close_global (struct outp_class *this UNUSED)
196 {
197   pool_destroy (ascii_pool);
198   return 1;
199 }
200
201 static int *
202 ascii_font_sizes (struct outp_class *this UNUSED, int *n_valid_sizes)
203 {
204   static int valid_sizes[] = {12, 12, 0, 0};
205
206   assert (n_valid_sizes);
207   *n_valid_sizes = 1;
208   return valid_sizes;
209 }
210
211 static int
212 ascii_preopen_driver (struct outp_driver *this)
213 {
214   struct ascii_driver_ext *x;
215   int i;
216   
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;
221   x->headers = 1;
222   x->page_length = 66;
223   x->page_width = 79;
224   x->lpi = 6;
225   x->cpi = 10;
226   x->left_margin = 0;
227   x->right_margin = 0;
228   x->top_margin = 2;
229   x->bottom_margin = 2;
230   x->paginate = 1;
231   x->tab_width = 8;
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;
242   x->file.mode = "wb";
243   x->file.file = NULL;
244   x->file.sequence_no = &x->page_number;
245   x->file.param = x;
246   x->file.postopen = postopen;
247   x->file.preclose = preclose;
248   x->page_number = 0;
249   x->page = NULL;
250   x->page_size = 0;
251   x->line_len = NULL;
252   x->line_len_size = 0;
253   x->n_output = 0;
254   x->cur_font = OUTP_F_R;
255 #if GLOBAL_DEBUGGING
256   x->debug = 0;
257 #endif
258   return 1;
259 }
260
261 static int
262 ascii_postopen_driver (struct outp_driver *this)
263 {
264   struct ascii_driver_ext *x = this->ext;
265   
266   assert (this->driver_open == 0);
267   
268   if (NULL == x->file.filename)
269     x->file.filename = xstrdup ("pspp.list");
270   
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)
275     {
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);
279       return 0;
280     }
281   
282   this->res = x->lpi * x->cpi;
283   this->horiz = x->lpi;
284   this->vert = x->cpi;
285   this->width = x->w * this->horiz;
286   this->length = x->l * this->vert;
287   
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"))
292     {
293       ls_create (ascii_pool, &x->ops[OPS_NEWLINE], "\n");
294       x->file.mode = "wt";
295     }
296   
297   {
298     int i;
299     
300     for (i = 0; i < LNS_COUNT; i++)
301       {
302         char c[2];
303         c[1] = 0;
304         if (!ls_null_p (&x->box[i]))
305           continue;
306         switch (i)
307           {
308           case TAYSTE2BYTE (0, 0, 0, 0):
309             c[0] = ' ';
310             break;
311
312           case TAYSTE2BYTE (0, 1, 0, 0):
313           case TAYSTE2BYTE (0, 1, 0, 1):
314           case TAYSTE2BYTE (0, 0, 0, 1):
315             c[0] = '-';
316             break;
317
318           case TAYSTE2BYTE (1, 0, 0, 0):
319           case TAYSTE2BYTE (1, 0, 1, 0):
320           case TAYSTE2BYTE (0, 0, 1, 0):
321             c[0] = '|';
322             break;
323
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):
330             c[0] = '=';
331             break;
332
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):
339             c[0] = '#';
340             break;
341
342           default:
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)
347               c[0] = '#';
348             else
349               c[0] = '+';
350             break;
351           }
352         ls_create (ascii_pool, &x->box[i], c);
353       }
354   }
355   
356   {
357     int i;
358     
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;
363
364     this->horiz_line_width[0] = 0;
365     this->vert_line_width[0] = 0;
366     
367     for (i = 1; i < OUTP_L_COUNT; i++)
368       {
369         this->horiz_line_width[i] = this->vert;
370         this->vert_line_width[i] = this->horiz;
371       }
372     
373     for (i = 0; i < (1 << OUTP_L_COUNT); i++)
374       {
375         this->horiz_line_spacing[i] = (i & ~1) ? this->vert : 0;
376         this->vert_line_spacing[i] = (i & ~1) ? this->horiz : 0;
377       }
378   }
379   
380   this->driver_open = 1;
381   msg (VM (2), _("%s: Initialization complete."), this->name);
382
383   return 1;
384 }
385
386 static int
387 ascii_close_driver (struct outp_driver *this)
388 {
389   struct ascii_driver_ext *x = this->ext;
390   
391   assert (this->driver_open == 1);
392   msg (VM (2), _("%s: Beginning closing..."), this->name);
393   
394   x = this->ext;
395   free (x->page);
396   free (x->line_len);
397   fn_close_ext (&x->file);
398   free (x->file.filename);
399   free (x);
400   
401   this->driver_open = 0;
402   msg (VM (3), _("%s: Finished closing."), this->name);
403   
404   return 1;
405 }
406
407 /* Generic option types. */
408 enum
409   {
410     pos_int_arg = -10,
411     nonneg_int_arg,
412     string_arg,
413     font_string_arg,
414     boolean_arg
415   };
416
417 static struct outp_option option_tab[] =
418   {
419     {"headers", boolean_arg, 0},
420     {"output-file", 1, 0},
421     {"char-set", 2, 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},
445     {"", 0, 0},
446   };
447 static struct outp_option_info option_info;
448
449 static void
450 ascii_option (struct outp_driver *this, const char *key,
451               const struct string *val)
452 {
453   struct ascii_driver_ext *x = this->ext;
454   int cat, subcat;
455   const char *value;
456
457   value = ds_value (val);
458   if (!strncmp (key, "box[", 4))
459     {
460       char *tail;
461       int indx = strtol (&key[4], &tail, 4);
462       if (*tail != ']' || indx < 0 || indx > LNS_COUNT)
463         {
464           msg (SE, _("Bad index value for `box' key: syntax is box[INDEX], "
465                "0 <= INDEX < %d decimal, with INDEX expressed in base 4."),
466                LNS_COUNT);
467           return;
468         }
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);
472       return;
473     }
474
475   cat = outp_match_keyword (key, option_tab, &option_info, &subcat);
476   switch (cat)
477     {
478     case 0:
479       msg (SE, _("Unknown configuration parameter `%s' for ascii device driver."),
480            key);
481       break;
482     case 1:
483       free (x->file.filename);
484       x->file.filename = xstrdup (value);
485       break;
486     case 2:
487       if (!strcmp (value, "ascii"))
488         x->char_set = CHS_ASCII;
489       else if (!strcmp (value, "latin1"))
490         x->char_set = CHS_LATIN1;
491       else
492         msg (SE, _("Unknown character set `%s'.  Valid character sets are "
493              "`ascii' and `latin1'."), value);
494       break;
495     case 3:
496       if (!strcmp (value, "single"))
497         x->overstrike_style = OVS_SINGLE;
498       else if (!strcmp (value, "line"))
499         x->overstrike_style = OVS_LINE;
500       else
501         msg (SE, _("Unknown overstrike style `%s'.  Valid overstrike styles "
502              "are `single' and `line'."), value);
503       break;
504     case 4:
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;
509       else
510         msg (SE, _("Unknown carriage return style `%s'.  Valid carriage "
511              "return styles are `cr' and `bs'."), value);
512       break;
513     case pos_int_arg:
514       {
515         char *tail;
516         int arg;
517
518         errno = 0;
519         arg = strtol (value, &tail, 0);
520         if (arg < 1 || errno == ERANGE || *tail)
521           {
522             msg (SE, _("Positive integer required as value for `%s'."), key);
523             break;
524           }
525         switch (subcat)
526           {
527           case 0:
528             x->page_length = arg;
529             break;
530           case 1:
531             x->page_width = arg;
532             break;
533           case 2:
534             x->lpi = arg;
535             break;
536           case 3:
537             x->cpi = arg;
538             break;
539           default:
540             assert (0);
541           }
542       }
543       break;
544     case nonneg_int_arg:
545       {
546         char *tail;
547         int arg;
548
549         errno = 0;
550         arg = strtol (value, &tail, 0);
551         if (arg < 0 || errno == ERANGE || *tail)
552           {
553             msg (SE, _("Zero or positive integer required as value for `%s'."),
554                  key);
555             break;
556           }
557         switch (subcat)
558           {
559           case 0:
560             x->left_margin = arg;
561             break;
562           case 1:
563             x->right_margin = arg;
564             break;
565           case 2:
566             x->top_margin = arg;
567             break;
568           case 3:
569             x->bottom_margin = arg;
570             break;
571           case 4:
572             x->tab_width = arg;
573             break;
574           default:
575             assert (0);
576           }
577       }
578       break;
579     case string_arg:
580       {
581         struct len_string *s;
582         switch (subcat)
583           {
584           case 0:
585             s = &x->ops[OPS_INIT];
586             break;
587           case 1:
588             s = &x->ops[OPS_DONE];
589             break;
590           case 2:
591             s = &x->ops[OPS_FORMFEED];
592             break;
593           case 3:
594             s = &x->ops[OPS_NEWLINE];
595             break;
596           default:
597             assert (0);
598             abort ();
599           }
600         ls_create (ascii_pool, s, value);
601       }
602       break;
603     case font_string_arg:
604       {
605         if (!strcmp (value, "overstrike"))
606           {
607             ls_destroy (ascii_pool, &x->fonts[subcat]);
608             return;
609           }
610         ls_create (ascii_pool, &x->fonts[subcat], value);
611       }
612       break;
613     case boolean_arg:
614       {
615         int setting;
616         if (!strcmp (value, "on") || !strcmp (value, "true")
617             || !strcmp (value, "yes") || atoi (value))
618           setting = 1;
619         else if (!strcmp (value, "off") || !strcmp (value, "false")
620                  || !strcmp (value, "no") || !strcmp (value, "0"))
621           setting = 0;
622         else
623           {
624             msg (SE, _("Boolean value expected for %s."), key);
625             return;
626           }
627         switch (subcat)
628           {
629           case 0:
630             x->headers = setting;
631             break;
632           case 1:
633             x->paginate = setting;
634             break;
635           case 2:
636             x->squeeze_blank_lines = setting;
637             break;
638           default:
639             assert (0);
640           }
641       }
642       break;
643     default:
644       assert (0);
645     }
646 }
647
648 int
649 postopen (struct file_ext *f)
650 {
651   struct ascii_driver_ext *x = f->param;
652   struct len_string *s = &x->ops[OPS_INIT];
653
654   if (!ls_empty_p (s) && fwrite (ls_value (s), ls_length (s), 1, f->file) < 1)
655     {
656       msg (ME, _("ASCII output driver: %s: %s"),
657            f->filename, strerror (errno));
658       return 0;
659     }
660   return 1;
661 }
662
663 int
664 preclose (struct file_ext *f)
665 {
666   struct ascii_driver_ext *x = f->param;
667   struct len_string *d = &x->ops[OPS_DONE];
668
669   if (!ls_empty_p (d) && fwrite (ls_value (d), ls_length (d), 1, f->file) < 1)
670     {
671       msg (ME, _("ASCII output driver: %s: %s"),
672            f->filename, strerror (errno));
673       return 0;
674     }
675   return 1;
676 }
677
678 static int
679 ascii_open_page (struct outp_driver *this)
680 {
681   struct ascii_driver_ext *x = this->ext;
682   int req_page_size;
683
684   assert (this->driver_open && !this->page_open);
685   x->page_number++;
686   if (!fn_open_ext (&x->file))
687     {
688       msg (ME, _("ASCII output driver: %s: %s"), x->file.filename,
689            strerror (errno));
690       return 0;
691     }
692
693   req_page_size = x->w * x->l;
694   if (req_page_size > x->page_size || req_page_size / 2 < x->page_size)
695     {
696       x->page_size = req_page_size;
697       x->page = xrealloc (x->page, sizeof *x->page * req_page_size);
698     }
699
700   if (x->l > x->line_len_size)
701     {
702       x->line_len_size = x->l;
703       x->line_len = xrealloc (x->line_len,
704                               sizeof *x->line_len * x->line_len_size);
705     }
706
707   memset (x->line_len, 0, sizeof *x->line_len * x->l);
708
709   this->page_open = 1;
710   return 1;
711 }
712
713 /* Ensures that at least the first L characters of line I in the
714    driver identified by struct ascii_driver_ext *X have been cleared out. */
715 static inline void
716 expand_line (struct ascii_driver_ext *x, int i, int l)
717 {
718   int limit = i * x->w + l;
719   int j;
720
721   for (j = i * x->w + x->line_len[i]; j < limit; j++)
722     x->page[j] = ' ';
723   x->line_len[i] = l;
724 }
725
726 /* Puts line L at (H,K) in the current output page.  Assumes
727    struct ascii_driver_ext named `ext'. */
728 #define draw_line(H, K, L)                              \
729         ext->page[ext->w * (K) + (H)] = (L) | 0x800
730
731 /* Line styles for each position. */
732 #define T(STYLE) (STYLE<<LNS_TOP)
733 #define L(STYLE) (STYLE<<LNS_LEFT)
734 #define B(STYLE) (STYLE<<LNS_BOTTOM)
735 #define R(STYLE) (STYLE<<LNS_RIGHT)
736
737 static void
738 ascii_line_horz (struct outp_driver *this, const struct rect *r,
739                  const struct color *c UNUSED, int style)
740 {
741   struct ascii_driver_ext *ext = this->ext;
742   int x1 = r->x1 / this->horiz;
743   int x2 = r->x2 / this->horiz;
744   int y1 = r->y1 / this->vert;
745   int x;
746
747   assert (this->driver_open && this->page_open);
748   if (x1 == x2)
749     return;
750 #if GLOBAL_DEBUGGING
751   if (x1 > x2
752       || x1 < 0 || x1 >= ext->w
753       || x2 <= 0 || x2 > ext->w
754       || y1 < 0 || y1 >= ext->l)
755     {
756 #if !SUPPRESS_WARNINGS
757       printf (_("ascii_line_horz: bad hline (%d,%d),%d out of (%d,%d)\n"),
758               x1, x2, y1, ext->w, ext->l);
759 #endif
760       return;
761     }
762 #endif
763
764   if (ext->line_len[y1] < x2)
765     expand_line (ext, y1, x2);
766
767   for (x = x1; x < x2; x++)
768     draw_line (x, y1, (style << LNS_LEFT) | (style << LNS_RIGHT));
769 }
770
771 static void
772 ascii_line_vert (struct outp_driver *this, const struct rect *r,
773                  const struct color *c UNUSED, int style)
774 {
775   struct ascii_driver_ext *ext = this->ext;
776   int x1 = r->x1 / this->horiz;
777   int y1 = r->y1 / this->vert;
778   int y2 = r->y2 / this->vert;
779   int y;
780
781   assert (this->driver_open && this->page_open);
782   if (y1 == y2)
783     return;
784 #if GLOBAL_DEBUGGING
785   if (y1 > y2
786       || x1 < 0 || x1 >= ext->w
787       || y1 < 0 || y1 >= ext->l
788       || y2 < 0 || y2 > ext->l)
789     {
790 #if !SUPPRESS_WARNINGS
791       printf (_("ascii_line_vert: bad vline %d,(%d,%d) out of (%d,%d)\n"),
792               x1, y1, y2, ext->w, ext->l);
793 #endif
794       return;
795     }
796 #endif
797
798   for (y = y1; y < y2; y++)
799     if (ext->line_len[y] <= x1)
800       expand_line (ext, y, x1 + 1);
801
802   for (y = y1; y < y2; y++)
803     draw_line (x1, y, (style << LNS_TOP) | (style << LNS_BOTTOM));
804 }
805
806 static void
807 ascii_line_intersection (struct outp_driver *this, const struct rect *r,
808                          const struct color *c UNUSED,
809                          const struct outp_styles *style)
810 {
811   struct ascii_driver_ext *ext = this->ext;
812   int x = r->x1 / this->horiz;
813   int y = r->y1 / this->vert;
814   int l;
815
816   assert (this->driver_open && this->page_open);
817 #if GLOBAL_DEBUGGING
818   if (x < 0 || x >= ext->w || y < 0 || y >= ext->l)
819     {
820 #if !SUPPRESS_WARNINGS
821       printf (_("ascii_line_intersection: bad intsct (%d,%d) out of (%d,%d)\n"),
822               x, y, ext->w, ext->l);
823 #endif
824       return;
825     }
826 #endif
827
828   l = ((style->l << LNS_LEFT) | (style->r << LNS_RIGHT)
829        | (style->t << LNS_TOP) | (style->b << LNS_BOTTOM));
830
831   if (ext->line_len[y] <= x)
832     expand_line (ext, y, x + 1);
833   draw_line (x, y, l);
834 }
835
836 /* FIXME: Later we could set this up so that for certain devices it
837    performs shading? */
838 static void
839 ascii_box (struct outp_driver *this UNUSED, const struct rect *r UNUSED,
840            const struct color *bord UNUSED, const struct color *fill UNUSED)
841 {
842   assert (this->driver_open && this->page_open);
843 }
844
845 /* Polylines not supported. */
846 static void
847 ascii_polyline_begin (struct outp_driver *this UNUSED, const struct color *c UNUSED)
848 {
849   assert (this->driver_open && this->page_open);
850 }
851 static void
852 ascii_polyline_point (struct outp_driver *this UNUSED, int x UNUSED, int y UNUSED)
853 {
854   assert (this->driver_open && this->page_open);
855 }
856 static void
857 ascii_polyline_end (struct outp_driver *this UNUSED)
858 {
859   assert (this->driver_open && this->page_open);
860 }
861
862 static void
863 ascii_text_set_font_by_name (struct outp_driver * this, const char *s)
864 {
865   struct ascii_driver_ext *x = this->ext;
866   int len = strlen (s);
867
868   assert (this->driver_open && this->page_open);
869   x->cur_font = OUTP_F_R;
870   if (len == 0)
871     return;
872   if (s[len - 1] == 'I')
873     {
874       if (len > 1 && s[len - 2] == 'B')
875         x->cur_font = OUTP_F_BI;
876       else
877         x->cur_font = OUTP_F_I;
878     }
879   else if (s[len - 1] == 'B')
880     x->cur_font = OUTP_F_B;
881 }
882
883 static void
884 ascii_text_set_font_by_position (struct outp_driver *this, int pos)
885 {
886   struct ascii_driver_ext *x = this->ext;
887   assert (this->driver_open && this->page_open);
888   x->cur_font = pos >= 0 && pos < 4 ? pos : 0;
889 }
890
891 static void
892 ascii_text_set_font_by_family (struct outp_driver *this UNUSED, const char *s UNUSED)
893 {
894   assert (this->driver_open && this->page_open);
895 }
896
897 static const char *
898 ascii_text_get_font_name (struct outp_driver *this)
899 {
900   struct ascii_driver_ext *x = this->ext;
901
902   assert (this->driver_open && this->page_open);
903   switch (x->cur_font)
904     {
905     case OUTP_F_R:
906       return "R";
907     case OUTP_F_I:
908       return "I";
909     case OUTP_F_B:
910       return "B";
911     case OUTP_F_BI:
912       return "BI";
913     default:
914       assert (0);
915     }
916   abort ();
917 }
918
919 static const char *
920 ascii_text_get_font_family (struct outp_driver *this UNUSED)
921 {
922   assert (this->driver_open && this->page_open);
923   return "";
924 }
925
926 static int
927 ascii_text_set_size (struct outp_driver *this, int size)
928 {
929   assert (this->driver_open && this->page_open);
930   return size == this->vert;
931 }
932
933 static int
934 ascii_text_get_size (struct outp_driver *this, int *em_width)
935 {
936   assert (this->driver_open && this->page_open);
937   if (em_width)
938     *em_width = this->horiz;
939   return this->vert;
940 }
941
942 static void text_draw (struct outp_driver *this, struct outp_text *t);
943
944 /* Divides the text T->S into lines of width T->H.  Sets T->V to the
945    number of lines necessary.  Actually draws the text if DRAW is
946    nonzero.
947
948    You probably don't want to look at this code. */
949 static void
950 delineate (struct outp_driver *this, struct outp_text *t, int draw)
951 {
952   /* Width we're fitting everything into. */
953   int width = t->h / this->horiz;
954
955   /* Maximum `y' position we can write to. */
956   int max_y;
957
958   /* Current position in string, character following end of string. */
959   const char *s = ls_value (&t->s);
960   const char *end = ls_end (&t->s);
961
962   /* Temporary struct outp_text to pass to low-level function. */
963   struct outp_text temp;
964
965 #if GLOBAL_DEBUGGING && 0
966   if (!ext->debug)
967     {
968       ext->debug = 1;
969       printf (_("%s: horiz=%d, vert=%d\n"), this->name, this->horiz, this->vert);
970     }
971 #endif
972
973   if (!width)
974     {
975       t->h = t->v = 0;
976       return;
977     }
978
979   if (draw)
980     {
981       temp.options = t->options;
982       ls_shallow_copy (&temp.s, &t->s);
983       temp.h = t->h / this->horiz;
984       temp.x = t->x / this->horiz;
985     }
986   else
987     t->y = 0;
988   temp.y = t->y / this->vert;
989
990   if (t->options & OUTP_T_VERT)
991     max_y = (t->v / this->vert) + temp.y - 1;
992   else
993     max_y = INT_MAX;
994   
995   while (end - s > width)
996     {
997       const char *beg = s;
998       const char *space;
999
1000       /* Find first space before &s[width]. */
1001       space = &s[width];
1002       for (;;)
1003         {
1004           if (space > s)
1005             {
1006               if (!isspace ((unsigned char) space[-1]))
1007                 {
1008                   space--;
1009                   continue;
1010                 }
1011               else
1012                 s = space;
1013             }
1014           else
1015             s = space = &s[width];
1016           break;
1017         }
1018
1019       /* Draw text. */
1020       if (draw)
1021         {
1022           ls_init (&temp.s, beg, space - beg);
1023           temp.w = space - beg;
1024           text_draw (this, &temp);
1025         }
1026       if (++temp.y > max_y)
1027         return;
1028
1029       /* Find first nonspace after space. */
1030       while (s < end && isspace ((unsigned char) *s))
1031         s++;
1032     }
1033   if (s < end)
1034     {
1035       if (draw)
1036         {
1037           ls_init (&temp.s, s, end - s);
1038           temp.w = end - s;
1039           text_draw (this, &temp);
1040         }
1041       temp.y++;
1042     }
1043
1044   t->v = (temp.y * this->vert) - t->y;
1045 }
1046
1047 static void
1048 ascii_text_metrics (struct outp_driver *this, struct outp_text *t)
1049 {
1050   assert (this->driver_open && this->page_open);
1051   if (!(t->options & OUTP_T_HORZ))
1052     {
1053       t->v = this->vert;
1054       t->h = ls_length (&t->s) * this->horiz;
1055     }
1056   else
1057     delineate (this, t, 0);
1058 }
1059
1060 static void
1061 ascii_text_draw (struct outp_driver *this, struct outp_text *t)
1062 {
1063   /* FIXME: orientations not supported. */
1064   assert (this->driver_open && this->page_open);
1065   if (!(t->options & OUTP_T_HORZ))
1066     {
1067       struct outp_text temp;
1068
1069       temp.options = t->options;
1070       temp.s = t->s;
1071       temp.h = temp.v = 0;
1072       temp.x = t->x / this->horiz;
1073       temp.y = t->y / this->vert;
1074       text_draw (this, &temp);
1075       ascii_text_metrics (this, t);
1076       
1077       return;
1078     }
1079   delineate (this, t, 1);
1080 }
1081
1082 static void
1083 text_draw (struct outp_driver *this, struct outp_text *t)
1084 {
1085   struct ascii_driver_ext *ext = this->ext;
1086   unsigned attr = ext->cur_font << 8;
1087
1088   int x = t->x;
1089   int y = t->y * ext->w;
1090
1091   char *s = ls_value (&t->s);
1092
1093   /* Expand the line with the assumption that S takes up LEN character
1094      spaces (sometimes it takes up less). */
1095   int min_len;
1096
1097   assert (this->driver_open && this->page_open);
1098   switch (t->options & OUTP_T_JUST_MASK)
1099     {
1100     case OUTP_T_JUST_LEFT:
1101       break;
1102     case OUTP_T_JUST_CENTER:
1103       x -= (t->h - t->w) / 2;   /* fall through */
1104     case OUTP_T_JUST_RIGHT:
1105       x += (t->h - t->w);
1106       break;
1107     default:
1108       assert (0);
1109     }
1110
1111   if (!(t->y < ext->l && x < ext->w))
1112     return;
1113   min_len = min (x + ls_length (&t->s), ext->w);
1114   if (ext->line_len[t->y] < min_len)
1115     expand_line (ext, t->y, min_len);
1116
1117   {
1118     int len = ls_length (&t->s);
1119
1120     if (len + x > ext->w)
1121       len = ext->w - x;
1122     while (len--)
1123       ext->page[y + x++] = *s++ | attr;
1124   }
1125 }
1126 \f
1127 /* ascii_close_page () and support routines. */
1128
1129 #define LINE_BUF_SIZE 1024
1130 static unsigned char *line_buf;
1131 static unsigned char *line_p;
1132
1133 static inline int
1134 commit_line_buf (struct outp_driver *this)
1135 {
1136   struct ascii_driver_ext *x = this->ext;
1137   
1138   if ((int) fwrite (line_buf, 1, line_p - line_buf, x->file.file)
1139       < line_p - line_buf)
1140     {
1141       msg (ME, _("Writing `%s': %s"), x->file.filename, strerror (errno));
1142       return 0;
1143     }
1144
1145   line_p = line_buf;
1146   return 1;
1147 }
1148
1149 /* Writes everything from BP to EP exclusive into line_buf, or to
1150    THIS->output if line_buf overflows. */
1151 static inline void
1152 output_string (struct outp_driver *this, const char *bp, const char *ep)
1153 {
1154   if (LINE_BUF_SIZE - (line_p - line_buf) >= ep - bp)
1155     {
1156       memcpy (line_p, bp, ep - bp);
1157       line_p += ep - bp;
1158     }
1159   else
1160     while (bp < ep)
1161       {
1162         if (LINE_BUF_SIZE - (line_p - line_buf) <= 1 && !commit_line_buf (this))
1163           return;
1164         *line_p++ = *bp++;
1165       }
1166 }
1167
1168 /* Writes everything from BP to EP exclusive into line_buf, or to
1169    THIS->output if line_buf overflows.  Returns 1 if additional passes
1170    over the line are required.  FIXME: probably could do a lot of
1171    optimization here. */
1172 static inline int
1173 output_shorts (struct outp_driver *this,
1174                const unsigned short *bp, const unsigned short *ep)
1175 {
1176   struct ascii_driver_ext *ext = this->ext;
1177   size_t remaining = LINE_BUF_SIZE - (line_p - line_buf);
1178   int result = 0;
1179
1180   for (; bp < ep; bp++)
1181     {
1182       if (*bp & 0x800)
1183         {
1184           struct len_string *box = &ext->box[*bp & 0xff];
1185           size_t len = ls_length (box);
1186
1187           if (remaining >= len)
1188             {
1189               memcpy (line_p, ls_value (box), len);
1190               line_p += len;
1191               remaining -= len;
1192             }
1193           else
1194             {
1195               if (!commit_line_buf (this))
1196                 return 0;
1197               output_string (this, ls_value (box), ls_end (box));
1198               remaining = LINE_BUF_SIZE - (line_p - line_buf);
1199             }
1200         }
1201       else if (*bp & 0x0300)
1202         {
1203           struct len_string *on;
1204           char buf[5];
1205           int len;
1206
1207           switch (*bp & 0x0300)
1208             {
1209             case OUTP_F_I << 8:
1210               on = &ext->fonts[FSTY_ON | FSTY_ITALIC];
1211               break;
1212             case OUTP_F_B << 8:
1213               on = &ext->fonts[FSTY_ON | FSTY_BOLD];
1214               break;
1215             case OUTP_F_BI << 8:
1216               on = &ext->fonts[FSTY_ON | FSTY_BOLD_ITALIC];
1217               break;
1218             default:
1219               assert (0);
1220               abort ();
1221             }
1222           if (!on)
1223             {
1224               if (ext->overstrike_style == OVS_SINGLE)
1225                 switch (*bp & 0x0300)
1226                   {
1227                   case OUTP_F_I << 8:
1228                     buf[0] = '_';
1229                     buf[1] = '\b';
1230                     buf[2] = *bp;
1231                     len = 3;
1232                     break;
1233                   case OUTP_F_B << 8:
1234                     buf[0] = *bp;
1235                     buf[1] = '\b';
1236                     buf[2] = *bp;
1237                     len = 3;
1238                     break;
1239                   case OUTP_F_BI << 8:
1240                     buf[0] = '_';
1241                     buf[1] = '\b';
1242                     buf[2] = *bp;
1243                     buf[3] = '\b';
1244                     buf[4] = *bp;
1245                     len = 5;
1246                     break;
1247                   default:
1248                     assert (0);
1249                     abort ();
1250                   }
1251               else
1252                 {
1253                   buf[0] = *bp;
1254                   result = len = 1;
1255                 }
1256             }
1257           else
1258             {
1259               buf[0] = *bp;
1260               len = 1;
1261             }
1262           output_string (this, buf, &buf[len]);
1263         }
1264       else if (remaining)
1265         {
1266           *line_p++ = *bp;
1267           remaining--;
1268         }
1269       else
1270         {
1271           if (!commit_line_buf (this))
1272             return 0;
1273           remaining = LINE_BUF_SIZE - (line_p - line_buf);
1274           *line_p++ = *bp;
1275         }
1276     }
1277
1278   return result;
1279 }
1280
1281 /* Writes CH into line_buf N times, or to THIS->output if line_buf
1282    overflows. */
1283 static inline void
1284 output_char (struct outp_driver *this, int n, int ch)
1285 {
1286   if (LINE_BUF_SIZE - (line_p - line_buf) >= n)
1287     {
1288       memset (line_p, ch, n);
1289       line_p += n;
1290     }
1291   else
1292     while (n--)
1293       {
1294         if (LINE_BUF_SIZE - (line_p - line_buf) <= 1 && !commit_line_buf (this))
1295           return;
1296         *line_p++ = ch;
1297       }
1298 }
1299
1300 /* Advance the carriage from column 0 to the left margin. */
1301 static void
1302 advance_to_left_margin (struct outp_driver *this)
1303 {
1304   struct ascii_driver_ext *ext = this->ext;
1305   int margin;
1306
1307   margin = ext->left_margin;
1308   if (margin == 0)
1309     return;
1310   if (ext->tab_width && margin >= ext->tab_width)
1311     {
1312       output_char (this, margin / ext->tab_width, '\t');
1313       margin %= ext->tab_width;
1314     }
1315   if (margin)
1316     output_char (this, margin, ' ');
1317 }
1318
1319 /* Move the output file carriage N_CHARS left, to the left margin. */
1320 static void
1321 return_carriage (struct outp_driver *this, int n_chars)
1322 {
1323   struct ascii_driver_ext *ext = this->ext;
1324
1325   switch (ext->carriage_return_style)
1326     {
1327     case CRS_BS:
1328       output_char (this, n_chars, '\b');
1329       break;
1330     case CRS_CR:
1331       output_char (this, 1, '\r');
1332       advance_to_left_margin (this);
1333       break;
1334     default:
1335       assert (0);
1336       abort ();
1337     }
1338 }
1339
1340 /* Writes COUNT lines from the line buffer in THIS, starting at line
1341    number FIRST. */
1342 static void
1343 output_lines (struct outp_driver *this, int first, int count)
1344 {
1345   struct ascii_driver_ext *ext = this->ext;
1346   int line_num;
1347
1348   struct len_string *newline = &ext->ops[OPS_NEWLINE];
1349
1350   int n_chars;
1351   int n_passes;
1352
1353   if (NULL == ext->file.file)
1354     return;
1355
1356   /* Iterate over all the lines to be output. */
1357   for (line_num = first; line_num < first + count; line_num++)
1358     {
1359       unsigned short *p = &ext->page[ext->w * line_num];
1360       unsigned short *end_p = p + ext->line_len[line_num];
1361       unsigned short *bp, *ep;
1362       unsigned short attr = 0;
1363
1364       assert (end_p >= p);
1365
1366       /* Squeeze multiple blank lines into a single blank line if
1367          requested. */
1368       if (ext->squeeze_blank_lines
1369           && line_num > first
1370           && ext->line_len[line_num] == 0
1371           && ext->line_len[line_num - 1] == 0)
1372         continue;
1373
1374       /* Output every character in the line in the appropriate
1375          manner. */
1376       n_passes = 1;
1377       bp = ep = p;
1378       n_chars = 0;
1379       advance_to_left_margin (this);
1380       for (;;)                  
1381         {
1382           while (ep < end_p && attr == (*ep & 0x0300))
1383             ep++;
1384           if (output_shorts (this, bp, ep))
1385             n_passes = 2;
1386           n_chars += ep - bp;
1387           bp = ep;
1388
1389           if (bp >= end_p)
1390             break;
1391
1392           /* Turn off old font. */
1393           if (attr != (OUTP_F_R << 8))
1394             {
1395               struct len_string *off;
1396
1397               switch (attr)
1398                 {
1399                 case OUTP_F_I << 8:
1400                   off = &ext->fonts[FSTY_OFF | FSTY_ITALIC];
1401                   break;
1402                 case OUTP_F_B << 8:
1403                   off = &ext->fonts[FSTY_OFF | FSTY_BOLD];
1404                   break;
1405                 case OUTP_F_BI << 8:
1406                   off = &ext->fonts[FSTY_OFF | FSTY_BOLD_ITALIC];
1407                   break;
1408                 default:
1409                   assert (0);
1410                   abort ();
1411                 }
1412               if (off)
1413                 output_string (this, ls_value (off), ls_end (off));
1414             }
1415
1416           /* Turn on new font. */
1417           attr = (*bp & 0x0300);
1418           if (attr != (OUTP_F_R << 8))
1419             {
1420               struct len_string *on;
1421
1422               switch (attr)
1423                 {
1424                 case OUTP_F_I << 8:
1425                   on = &ext->fonts[FSTY_ON | FSTY_ITALIC];
1426                   break;
1427                 case OUTP_F_B << 8:
1428                   on = &ext->fonts[FSTY_ON | FSTY_BOLD];
1429                   break;
1430                 case OUTP_F_BI << 8:
1431                   on = &ext->fonts[FSTY_ON | FSTY_BOLD_ITALIC];
1432                   break;
1433                 default:
1434                   assert (0);
1435                   abort ();
1436                 }
1437               if (on)
1438                 output_string (this, ls_value (on), ls_end (on));
1439             }
1440
1441           ep = bp + 1;
1442         }
1443       if (n_passes > 1)
1444         {
1445           unsigned char ch;
1446
1447           return_carriage (this, n_chars);
1448           n_chars = 0;
1449           bp = ep = p;
1450           for (;;)
1451             {
1452               while (ep < end_p && (*ep & 0x0300) == (OUTP_F_R << 8))
1453                 ep++;
1454               if (ep >= end_p)
1455                 break;
1456               output_char (this, ep - bp, ' ');
1457
1458               switch (*ep & 0x0300)
1459                 {
1460                 case OUTP_F_I << 8:
1461                   ch = '_';
1462                   break;
1463                 case OUTP_F_B << 8:
1464                   ch = *ep;
1465                   break;
1466                 case OUTP_F_BI << 8:
1467                   ch = *ep;
1468                   n_passes = 3;
1469                   break;
1470                 default:
1471                   assert (0);
1472                   abort ();
1473                 }
1474               output_char (this, 1, ch);
1475               n_chars += ep - bp + 1;
1476               bp = ep + 1;
1477               ep = bp;
1478             }
1479         }
1480       if (n_passes > 2)
1481         {
1482           return_carriage (this, n_chars);
1483           bp = ep = p;
1484           for (;;)
1485             {
1486               while (ep < end_p && (*ep & 0x0300) != (OUTP_F_BI << 8))
1487                 ep++;
1488               if (ep >= end_p)
1489                 break;
1490               output_char (this, ep - bp, ' ');
1491               output_char (this, 1, '_');
1492               bp = ep + 1;
1493               ep = bp;
1494             }
1495         }
1496
1497       output_string (this, ls_value (newline), ls_end (newline));
1498     }
1499 }
1500
1501 static int
1502 ascii_close_page (struct outp_driver *this)
1503 {
1504   static unsigned char *s;
1505   static int s_len;
1506
1507   struct ascii_driver_ext *x = this->ext;
1508   int nl_len, ff_len, total_len;
1509   unsigned char *cp;
1510   int i;
1511
1512   assert (this->driver_open && this->page_open);
1513   
1514   if (!line_buf)
1515     line_buf = xmalloc (LINE_BUF_SIZE);
1516   line_p = line_buf;
1517
1518   nl_len = ls_length (&x->ops[OPS_NEWLINE]);
1519   if (x->top_margin)
1520     {
1521       total_len = x->top_margin * nl_len;
1522       if (s_len < total_len)
1523         {
1524           s_len = total_len;
1525           s = xrealloc (s, s_len);
1526         }
1527       for (cp = s, i = 0; i < x->top_margin; i++)
1528         {
1529           memcpy (cp, ls_value (&x->ops[OPS_NEWLINE]), nl_len);
1530           cp += nl_len;
1531         }
1532       output_string (this, s, &s[total_len]);
1533     }
1534   if (x->headers)
1535     {
1536       int len;
1537
1538       total_len = nl_len + x->w;
1539       if (s_len < total_len + 1)
1540         {
1541           s_len = total_len + 1;
1542           s = xrealloc (s, s_len);
1543         }
1544       
1545       memset (s, ' ', x->w);
1546
1547       {
1548         char temp[40];
1549
1550         snprintf (temp, 80, _("%s - Page %d"), curdate, x->page_number);
1551         memcpy (&s[x->w - strlen (temp)], temp, strlen (temp));
1552       }
1553
1554       if (outp_title && outp_subtitle)
1555         {
1556           len = min ((int) strlen (outp_title), x->w);
1557           memcpy (s, outp_title, len);
1558         }
1559       memcpy (&s[x->w], ls_value (&x->ops[OPS_NEWLINE]), nl_len);
1560       output_string (this, s, &s[total_len]);
1561
1562       memset (s, ' ', x->w);
1563       len = strlen (version) + 3 + strlen (host_system);
1564       if (len < x->w)
1565         sprintf (&s[x->w - len], "%s - %s" , version, host_system);
1566       if (outp_subtitle || outp_title)
1567         {
1568           char *string = outp_subtitle ? outp_subtitle : outp_title;
1569           len = min ((int) strlen (string), x->w);
1570           memcpy (s, string, len);
1571         }
1572       memcpy (&s[x->w], ls_value (&x->ops[OPS_NEWLINE]), nl_len);
1573       output_string (this, s, &s[total_len]);
1574       output_string (this, &s[x->w], &s[total_len]);
1575     }
1576   if (line_p != line_buf && !commit_line_buf (this))
1577     return 0;
1578
1579   output_lines (this, x->n_output, x->l - x->n_output);
1580
1581   ff_len = ls_length (&x->ops[OPS_FORMFEED]);
1582   total_len = x->bottom_margin * nl_len + ff_len;
1583   if (s_len < total_len)
1584     s = xrealloc (s, total_len);
1585   for (cp = s, i = 0; i < x->bottom_margin; i++)
1586     {
1587       memcpy (cp, ls_value (&x->ops[OPS_NEWLINE]), nl_len);
1588       cp += nl_len;
1589     }
1590   memcpy (cp, ls_value (&x->ops[OPS_FORMFEED]), ff_len);
1591   if ( x->paginate ) 
1592           output_string (this, s, &s[total_len]);
1593   if (line_p != line_buf && !commit_line_buf (this))
1594     return 0;
1595
1596   x->n_output = 0;
1597   
1598   this->page_open = 0;
1599   return 1;
1600 }
1601
1602 struct outp_class ascii_class =
1603 {
1604   "ascii",
1605   0,
1606   0,
1607
1608   ascii_open_global,
1609   ascii_close_global,
1610   ascii_font_sizes,
1611
1612   ascii_preopen_driver,
1613   ascii_option,
1614   ascii_postopen_driver,
1615   ascii_close_driver,
1616
1617   ascii_open_page,
1618   ascii_close_page,
1619
1620   NULL,
1621
1622   ascii_line_horz,
1623   ascii_line_vert,
1624   ascii_line_intersection,
1625
1626   ascii_box,
1627   ascii_polyline_begin,
1628   ascii_polyline_point,
1629   ascii_polyline_end,
1630
1631   ascii_text_set_font_by_name,
1632   ascii_text_set_font_by_position,
1633   ascii_text_set_font_by_family,
1634   ascii_text_get_font_name,
1635   ascii_text_get_font_family,
1636   ascii_text_set_size,
1637   ascii_text_get_size,
1638   ascii_text_metrics,
1639   ascii_text_draw,
1640 };