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