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