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